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
529081737b6e1ffc7fc8696981c29149b1a5521f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/nat/factorial/big_operators.lean
0c095d9f8f6b4936dbacfd905bd50b2ccd22f2b0
[ "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
1,105
lean
/- Copyright (c) 2022 Pim Otte. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller, Pim Otte -/ import data.nat.factorial.basic import algebra.big_operators.order /-! # Factorial with big operators This file contains some lemmas on factorials in combination with big operators. While in terms of semantics they could be in the `basic.lean` file, importing `algebra.big_operators.basic` leads to a cyclic import. -/ open_locale nat big_operators namespace nat variables {α : Type*} (s : finset α) (f : α → ℕ) lemma prod_factorial_pos : 0 < ∏ i in s, (f i)! := finset.prod_pos (λ i _, factorial_pos (f i)) lemma prod_factorial_dvd_factorial_sum : (∏ i in s, (f i)!) ∣ (∑ i in s, f i)! := begin classical, induction s using finset.induction with a' s' has ih, { simp only [finset.sum_empty, finset.prod_empty, factorial], }, { simp only [finset.prod_insert has, finset.sum_insert has], refine dvd_trans (mul_dvd_mul_left ((f a')!) ih) _, apply nat.factorial_mul_factorial_dvd_factorial_add, }, end end nat
c5589275eb2ad9a53c995acbc37dfe75fe1eae63
36938939954e91f23dec66a02728db08a7acfcf9
/old-lean4/reader.lean
73d6dca51e354273e62bf003b0e170210731bb3d
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
1,811
lean
namespace ReadMonad variable {m: Type → Type _} variable [h:Monad m] include h def readN' (rc : m Char) : String → ℕ → m String | s 0 := pure s | s (Nat.succ i) := do c ← rc, readN' (s.push c) i end ReadMonad class ReadMonad (m : Type → Type _) extends Monad m, MonadFail m := (atEnd {} : m Bool) (peek {} : m Char) (skip {} : m PUnit) (read {} : m Char := seqLeft peek skip) (readN {} : ℕ → m String := ReadMonad.readN' read "") def StringReader := EState String Substring namespace StringReader section attribute [reducible] StringReader protected def fail {α:Type} : String → StringReader α := throw protected def atEnd : StringReader Bool := do s ← get, pure (s.startPos ≥ s.stopPos) protected def peek : StringReader Char := do s ← get, when (s.startPos + 1 > s.stopPos) (throw "At end of string"), pure s.front protected def skip : StringReader Unit := do s ← get, when (s.startPos + 1 > s.stopPos) (throw "At end of string"), set (s.drop 1) protected def read : StringReader Char := do s ← get, when (s.startPos + 1 > s.stopPos) (throw "At end of string"), set (s.drop 1), pure s.front protected def readN (n:ℕ) : StringReader String := do s ← get, when (s.startPos + n > s.stopPos) (throw "At end of string"), set (s.drop n), pure (s.take n).toString end instance : MonadFail StringReader := { fail := @StringReader.fail } instance : ReadMonad StringReader := { atEnd := StringReader.atEnd , peek := StringReader.peek , skip := StringReader.skip , read := StringReader.read , readN := StringReader.readN } end StringReader section readHandle variable {α : Type} def readFromString (s : String) (m : StringReader α) : EState.Result String Substring α := m (EState.resultOk.mk () s.toSubstring) end readHandle
0382f0fcbea8bdb0186eba96505696ea167de818
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/omega/misc.lean
31703e585caf2ff592a26171b843b3bba134e0e6
[ "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
2,135
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Miscellaneous. -/ import tactic.localized variables {α β γ : Type} namespace omega lemma fun_mono_2 {p : α → β → γ} {a1 a2 : α} {b1 b2 : β} : a1 = a2 → b1 = b2 → (p a1 b1 = p a2 b2) := λ h1 h2, by rw [h1, h2] lemma pred_mono_2 {p : α → β → Prop} {a1 a2 : α} {b1 b2 : β} : a1 = a2 → b1 = b2 → (p a1 b1 ↔ p a2 b2) := λ h1 h2, by rw [h1, h2] lemma pred_mono_2' {c : Prop → Prop → Prop} {a1 a2 b1 b2 : Prop} : (a1 ↔ a2) → (b1 ↔ b2) → (c a1 b1 ↔ c a2 b2) := λ h1 h2, by rw [h1, h2] /-- Update variable assignment for a specific variable and leave everything else unchanged -/ def update (m : nat) (a : α) (v : nat → α) : nat → α | n := if n = m then a else v n localized "notation v ` ⟨` m ` ↦ ` a `⟩` := omega.update m a v" in omega lemma update_eq (m : nat) (a : α) (v : nat → α) : (v ⟨m ↦ a⟩) m = a := by simp only [update, if_pos rfl] lemma update_eq_of_ne {m : nat} {a : α} {v : nat → α} (k : nat) : k ≠ m → update m a v k = v k := by {intro h1, unfold update, rw if_neg h1} /-- Assign a new value to the zeroth variable, and push all other assignments up by 1 -/ def update_zero (a : α) (v : nat → α) : nat → α | 0 := a | (k+1) := v k open tactic /-- Intro with a fresh name -/ meta def intro_fresh : tactic unit := do n ← mk_fresh_name, intro n, skip /-- Revert an expr if it passes the given test -/ meta def revert_cond (t : expr → tactic unit) (x : expr) : tactic unit := (t x >> revert x >> skip) <|> skip /-- Revert all exprs in the context that pass the given test -/ meta def revert_cond_all (t : expr → tactic unit) : tactic unit := do hs ← local_context, mmap (revert_cond t) hs, skip /-- Try applying a tactic to each of the element in a list until success, and return the first successful result -/ meta def app_first {α β : Type} (t : α → tactic β) : list α → tactic β | [] := failed | (a :: as) := t a <|> app_first as end omega
ac5751487f9f2feaf3cbbfa6f338cb49c6359513
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/inductive2.lean
cf4446512004c97faa2e353f309124293af28d2d
[ "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
460
lean
new_frontend mutual universe u variable (α : Type u) inductive isEvenList : List α → Prop | nil {} : isEvenList [] | cons (h : α) {t : List α} : isOddList t → isEvenList (h::t) inductive isOddList : List α → Prop | cons (h : α) {t : List α} : isEvenList t → isOddList (h::t) end #check @isEvenList.nil #check @isEvenList.cons #check @isOddList.cons #check isEvenList.nil Nat #check isEvenList.cons 1 $ isOddList.cons 2 $ isEvenList.nil Nat
a14e0424c568f68904ffa145ba4e8fd0741aa781
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Elab.lean
08848508d1f57a13ab7e7a1f1e06555860e938c7
[ "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
669
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Elab.Import import Init.Lean.Elab.Exception import Init.Lean.Elab.StrategyAttrs import Init.Lean.Elab.Command import Init.Lean.Elab.Term import Init.Lean.Elab.App import Init.Lean.Elab.Binders import Init.Lean.Elab.Quotation import Init.Lean.Elab.Frontend import Init.Lean.Elab.BuiltinNotation import Init.Lean.Elab.Declaration import Init.Lean.Elab.Tactic import Init.Lean.Elab.Syntax import Init.Lean.Elab.Match import Init.Lean.Elab.DoNotation import Init.Lean.Elab.StructInst
953d73ea8d50db4c8608fc7daa24f4e0dcdf7d3f
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/analysis/calculus/deriv.lean
a7d75cd62539049fe3afc8c86ecf4af33a5b79c2
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
47,919
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Sébastien Gouëzel The Fréchet derivative. Let `E` and `F` be normed spaces, `f : E → F`, and `f' : E →L[𝕜] F` a continuous 𝕜-linear map, where `𝕜` is a non-discrete normed field. Then `has_fderiv_within_at f f' s x` says that `f` has derivative `f'` at `x`, where the domain of interest is restricted to `s`. We also have `has_fderiv_at f f' x := has_fderiv_within_at f f' x univ` The derivative is defined in terms of the `is_o` relation, but also characterized in terms of the `tendsto` relation. We also introduce predicates `differentiable_within_at 𝕜 f s x` (where `𝕜` is the base field, `f` the function to be differentiated, `x` the point at which the derivative is asserted to exist, and `s` the set along which the derivative is defined), as well as `differentiable_at 𝕜 f x`, `differentiable_on 𝕜 f s` and `differentiable 𝕜 f` to express the existence of a derivative. To be able to compute with derivatives, we write `fderiv_within 𝕜 f s x` and `fderiv 𝕜 f x` for some choice of a derivative if it exists, and the zero function otherwise. This choice only behaves well along sets for which the derivative is unique, i.e., those for which the tangent directions span a dense subset of the whole space. The predicates `unique_diff_within_at s x` and `unique_diff_on s`, defined in `tangent_cone.lean` express this property. We prove that indeed they imply the uniqueness of the derivative. This is satisfied for open subsets, and in particular for `univ`. This uniqueness only holds when the field is non-discrete, which we request at the very beginning: otherwise, a derivative can be defined, but it has no interesting properties whatsoever. In addition to the definition and basic properties of the derivative, this file contains the usual formulas (and existence assertions) for the derivative of * constants * the identity * bounded linear maps * bounded bilinear maps * sum of two functions * multiplication of a function by a scalar constant * negative of a function * subtraction of two functions * multiplication of a function by a scalar function * multiplication of two scalar functions * composition of functions (the chain rule) -/ import analysis.asymptotics analysis.calculus.tangent_cone open filter asymptotics continuous_linear_map set noncomputable theory local attribute [instance, priority 10] classical.decidable_inhabited classical.prop_decidable set_option class.instance_max_depth 90 section 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] def has_fderiv_at_filter (f : E → F) (f' : E →L[𝕜] F) (x : E) (L : filter E) := is_o (λ x', f x' - f x - f' (x' - x)) (λ x', x' - x) L def has_fderiv_within_at (f : E → F) (f' : E →L[𝕜] F) (s : set E) (x : E) := has_fderiv_at_filter f f' x (nhds_within x s) def has_fderiv_at (f : E → F) (f' : E →L[𝕜] F) (x : E) := has_fderiv_at_filter f f' x (nhds x) variables (𝕜) def differentiable_within_at (f : E → F) (s : set E) (x : E) := ∃f' : E →L[𝕜] F, has_fderiv_within_at f f' s x def differentiable_at (f : E → F) (x : E) := ∃f' : E →L[𝕜] F, has_fderiv_at f f' x def fderiv_within (f : E → F) (s : set E) (x : E) : E →L[𝕜] F := if h : ∃f', has_fderiv_within_at f f' s x then classical.some h else 0 def fderiv (f : E → F) (x : E) : E →L[𝕜] F := if h : ∃f', has_fderiv_at f f' x then classical.some h else 0 def differentiable_on (f : E → F) (s : set E) := ∀x ∈ s, differentiable_within_at 𝕜 f s x def differentiable (f : E → F) := ∀x, differentiable_at 𝕜 f x variables {𝕜} variables {f f₀ f₁ g : E → F} variables {f' f₀' f₁' g' : E →L[𝕜] F} variables {x : E} variables {s t : set E} variables {L L₁ L₂ : filter E} section derivative_uniqueness /- In this section, we discuss the uniqueness of the derivative. We prove that the definitions `unique_diff_within_at` and `unique_diff_on` indeed imply the uniqueness of the derivative. -/ /-- `unique_diff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/ theorem unique_diff_within_at.eq (H : unique_diff_within_at 𝕜 s x) (h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' := begin have A : ∀y ∈ tangent_cone_at 𝕜 s x, f' y = f₁' y, { assume y hy, rcases hy with ⟨c, d, hd, hc, ylim⟩, have at_top_is_finer : at_top ≤ comap (λ (n : ℕ), x + d n) (nhds_within (x + 0) s), { rw [←tendsto_iff_comap, nhds_within, tendsto_inf], split, { apply tendsto_add tendsto_const_nhds (tangent_cone_at.lim_zero hc ylim) }, { rwa tendsto_principal } }, rw add_zero at at_top_is_finer, have : is_o (λ y, f₁' (y - x) - f' (y - x)) (λ y, y - x) (nhds_within x s), by simpa using h.sub h₁, have : is_o (λ n:ℕ, f₁' ((x + d n) - x) - f' ((x + d n) - x)) (λ n, (x + d n) - x) ((nhds_within x s).comap (λn, x+ d n)) := is_o.comp this _, have L1 : is_o (λ n:ℕ, f₁' (d n) - f' (d n)) d ((nhds_within x s).comap (λn, x + d n)) := by simpa using this, have L2 : is_o (λn:ℕ, f₁' (d n) - f' (d n)) d at_top := is_o.mono at_top_is_finer L1, have L3 : is_o (λn:ℕ, c n • (f₁' (d n) - f' (d n))) (λn, c n • d n) at_top := is_o_smul L2, have L4 : is_o (λn:ℕ, c n • (f₁' (d n) - f' (d n))) (λn, (1:ℝ)) at_top := L3.trans_is_O (is_O_one_of_tendsto ylim), have L : tendsto (λn:ℕ, c n • (f₁' (d n) - f' (d n))) at_top (nhds 0) := is_o_one_iff.1 L4, have L' : tendsto (λ (n : ℕ), c n • (f₁' (d n) - f' (d n))) at_top (nhds (f₁' y - f' y)), { simp only [smul_sub, (continuous_linear_map.map_smul _ _ _).symm], apply tendsto_sub ((f₁'.continuous.tendsto _).comp ylim) ((f'.continuous.tendsto _).comp ylim) }, have : f₁' y - f' y = 0 := tendsto_nhds_unique (by simp) L' L, exact (sub_eq_zero_iff_eq.1 this).symm }, have B : ∀y ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 s x), f' y = f₁' y, { assume y hy, apply submodule.span_induction hy, { exact λy hy, A y hy }, { simp only [continuous_linear_map.map_zero] }, { simp {contextual := tt} }, { simp {contextual := tt} } }, have C : ∀y ∈ closure ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E), f' y = f₁' y, { assume y hy, let K := {y | f' y = f₁' y}, have : (submodule.span 𝕜 (tangent_cone_at 𝕜 s x) : set E) ⊆ K := B, have : closure (submodule.span 𝕜 (tangent_cone_at 𝕜 s x) : set E) ⊆ closure K := closure_mono this, have : y ∈ closure K := this hy, rwa closure_eq_of_is_closed (is_closed_eq f'.continuous f₁'.continuous) at this }, rw H.1 at C, ext y, exact C y (mem_univ _) end theorem unique_diff_on.eq (H : unique_diff_on 𝕜 s) (hx : x ∈ s) (h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' := unique_diff_within_at.eq (H x hx) h h₁ end derivative_uniqueness /- Basic properties of the derivative -/ section fderiv_properties theorem has_fderiv_at_filter_iff_tendsto : has_fderiv_at_filter f f' x L ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) L (nhds 0) := have h : ∀ x', ∥x' - x∥ = 0 → ∥f x' - f x - f' (x' - x)∥ = 0, from λ x' hx', by { rw [sub_eq_zero.1 ((norm_eq_zero (x' - x)).1 hx')], simp }, begin unfold has_fderiv_at_filter, rw [←is_o_norm_left, ←is_o_norm_right, is_o_iff_tendsto h], exact tendsto.congr'r (λ _, div_eq_inv_mul), end theorem has_fderiv_within_at_iff_tendsto : has_fderiv_within_at f f' s x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) (nhds_within x s) (nhds 0) := has_fderiv_at_filter_iff_tendsto theorem has_fderiv_at_iff_tendsto : has_fderiv_at f f' x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) (nhds x) (nhds 0) := has_fderiv_at_filter_iff_tendsto theorem has_fderiv_at_filter.mono (h : has_fderiv_at_filter f f' x L₂) (hst : L₁ ≤ L₂) : has_fderiv_at_filter f f' x L₁ := is_o.mono hst h theorem has_fderiv_within_at.mono (h : has_fderiv_within_at f f' t x) (hst : s ⊆ t) : has_fderiv_within_at f f' s x := h.mono (nhds_within_mono _ hst) theorem has_fderiv_at.has_fderiv_at_filter (h : has_fderiv_at f f' x) (hL : L ≤ nhds x) : has_fderiv_at_filter f f' x L := h.mono hL theorem has_fderiv_at.has_fderiv_within_at (h : has_fderiv_at f f' x) : has_fderiv_within_at f f' s x := h.has_fderiv_at_filter lattice.inf_le_left lemma has_fderiv_within_at.differentiable_within_at (h : has_fderiv_within_at f f' s x) : differentiable_within_at 𝕜 f s x := ⟨f', h⟩ lemma has_fderiv_at.differentiable_at (h : has_fderiv_at f f' x) : differentiable_at 𝕜 f x := ⟨f', h⟩ @[simp] lemma has_fderiv_within_at_univ : has_fderiv_within_at f f' univ x ↔ has_fderiv_at f f' x := by { simp only [has_fderiv_within_at, nhds_within_univ], refl } theorem has_fderiv_at_unique (h₀ : has_fderiv_at f f₀' x) (h₁ : has_fderiv_at f f₁' x) : f₀' = f₁' := begin rw ← has_fderiv_within_at_univ at h₀ h₁, exact unique_diff_within_at_univ.eq h₀ h₁ end lemma has_fderiv_within_at_inter' (h : t ∈ nhds_within x s) : has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x := by simp [has_fderiv_within_at, nhds_within_restrict'' s h] lemma has_fderiv_within_at_inter (h : t ∈ nhds x) : has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x := by simp [has_fderiv_within_at, nhds_within_restrict' s h] lemma differentiable_within_at.has_fderiv_within_at (h : differentiable_within_at 𝕜 f s x) : has_fderiv_within_at f (fderiv_within 𝕜 f s x) s x := begin dunfold fderiv_within, dunfold differentiable_within_at at h, rw dif_pos h, exact classical.some_spec h end lemma differentiable_at.has_fderiv_at (h : differentiable_at 𝕜 f x) : has_fderiv_at f (fderiv 𝕜 f x) x := begin dunfold fderiv, dunfold differentiable_at at h, rw dif_pos h, exact classical.some_spec h end lemma has_fderiv_at.fderiv (h : has_fderiv_at f f' x) : fderiv 𝕜 f x = f' := by { ext, rw has_fderiv_at_unique h h.differentiable_at.has_fderiv_at } lemma has_fderiv_within_at.fderiv_within (h : has_fderiv_within_at f f' s x) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 f s x = f' := by { ext, rw hxs.eq h h.differentiable_within_at.has_fderiv_within_at } lemma differentiable_within_at.mono (h : differentiable_within_at 𝕜 f t x) (st : s ⊆ t) : differentiable_within_at 𝕜 f s x := begin rcases h with ⟨f', hf'⟩, exact ⟨f', hf'.mono st⟩ end lemma differentiable_within_at_univ : differentiable_within_at 𝕜 f univ x ↔ differentiable_at 𝕜 f x := begin simp [differentiable_within_at, has_fderiv_within_at, nhds_within_univ], refl end lemma differentiable_within_at_inter (ht : t ∈ nhds x) : differentiable_within_at 𝕜 f (s ∩ t) x ↔ differentiable_within_at 𝕜 f s x := by simp only [differentiable_within_at, has_fderiv_within_at, has_fderiv_at_filter, nhds_within_restrict' s ht] lemma differentiable_at.differentiable_within_at (h : differentiable_at 𝕜 f x) : differentiable_within_at 𝕜 f s x := (differentiable_within_at_univ.2 h).mono (subset_univ _) lemma differentiable_within_at.differentiable_at (h : differentiable_within_at 𝕜 f s x) (hs : s ∈ nhds x) : differentiable_at 𝕜 f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, differentiable_within_at_inter hs, differentiable_within_at_univ] at h end lemma differentiable.fderiv_within (h : differentiable_at 𝕜 f x) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 f s x = fderiv 𝕜 f x := begin apply has_fderiv_within_at.fderiv_within _ hxs, exact h.has_fderiv_at.has_fderiv_within_at end lemma differentiable_on.mono (h : differentiable_on 𝕜 f t) (st : s ⊆ t) : differentiable_on 𝕜 f s := λx hx, (h x (st hx)).mono st lemma differentiable_on_univ : differentiable_on 𝕜 f univ ↔ differentiable 𝕜 f := by { simp [differentiable_on, differentiable_within_at_univ], refl } lemma differentiable.differentiable_on (h : differentiable 𝕜 f) : differentiable_on 𝕜 f s := (differentiable_on_univ.2 h).mono (subset_univ _) lemma differentiable_on_of_locally_differentiable_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ differentiable_on 𝕜 f (s ∩ u)) : differentiable_on 𝕜 f s := begin assume x xs, rcases h x xs with ⟨t, t_open, xt, ht⟩, exact (differentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩) end lemma fderiv_within_subset (st : s ⊆ t) (ht : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f t x) : fderiv_within 𝕜 f s x = fderiv_within 𝕜 f t x := ((differentiable_within_at.has_fderiv_within_at h).mono st).fderiv_within ht @[simp] lemma fderiv_within_univ : fderiv_within 𝕜 f univ = fderiv 𝕜 f := begin ext x : 1, by_cases h : differentiable_at 𝕜 f x, { apply has_fderiv_within_at.fderiv_within _ (is_open_univ.unique_diff_within_at (mem_univ _)), rw has_fderiv_within_at_univ, apply h.has_fderiv_at }, { have : fderiv 𝕜 f x = 0, by { unfold differentiable_at at h, simp [fderiv, h] }, rw this, have : ¬(differentiable_within_at 𝕜 f univ x), by rwa differentiable_within_at_univ, unfold differentiable_within_at at this, simp [fderiv_within, this, -has_fderiv_within_at_univ] } end lemma fderiv_within_inter (ht : t ∈ nhds x) (hs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 f (s ∩ t) x = fderiv_within 𝕜 f s x := begin by_cases h : differentiable_within_at 𝕜 f (s ∩ t) x, { apply fderiv_within_subset (inter_subset_left _ _) _ ((differentiable_within_at_inter ht).1 h), apply hs.inter ht }, { have : fderiv_within 𝕜 f (s ∩ t) x = 0, by { unfold differentiable_within_at at h, simp [fderiv_within, h] }, rw this, rw differentiable_within_at_inter ht at h, have : fderiv_within 𝕜 f s x = 0, by { unfold differentiable_within_at at h, simp [fderiv_within, h] }, rw this } end end fderiv_properties /- Congr -/ section congr theorem has_fderiv_at_filter_congr_of_mem_sets (hx : f₀ x = f₁ x) (h₀ : {x | f₀ x = f₁ x} ∈ L) (h₁ : ∀ x, f₀' x = f₁' x) : has_fderiv_at_filter f₀ f₀' x L ↔ has_fderiv_at_filter f₁ f₁' x L := by { rw (ext h₁), exact is_o_congr (by filter_upwards [h₀] λ x (h : _ = _), by simp [h, hx]) (univ_mem_sets' $ λ _, rfl) } lemma has_fderiv_at_filter.congr_of_mem_sets (h : has_fderiv_at_filter f f' x L) (hL : {x | f₁ x = f x} ∈ L) (hx : f₁ x = f x) : has_fderiv_at_filter f₁ f' x L := begin apply (has_fderiv_at_filter_congr_of_mem_sets hx hL _).2 h, exact λx, rfl end lemma has_fderiv_within_at.congr_mono (h : has_fderiv_within_at f f' s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_fderiv_within_at f₁ f' t x := has_fderiv_at_filter.congr_of_mem_sets (h.mono h₁) (filter.mem_inf_sets_of_right ht) hx lemma has_fderiv_within_at.congr_of_mem_nhds_within (h : has_fderiv_within_at f f' s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : has_fderiv_within_at f₁ f' s x := has_fderiv_at_filter.congr_of_mem_sets h h₁ hx lemma has_fderiv_at.congr_of_mem_nhds (h : has_fderiv_at f f' x) (h₁ : {y | f₁ y = f y} ∈ nhds x) : has_fderiv_at f₁ f' x := has_fderiv_at_filter.congr_of_mem_sets h h₁ (mem_of_nhds h₁ : _) lemma differentiable_within_at.congr_mono (h : differentiable_within_at 𝕜 f s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : differentiable_within_at 𝕜 f₁ t x := (has_fderiv_within_at.congr_mono h.has_fderiv_within_at ht hx h₁).differentiable_within_at lemma differentiable_within_at.congr_of_mem_nhds_within (h : differentiable_within_at 𝕜 f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : differentiable_within_at 𝕜 f₁ s x := (h.has_fderiv_within_at.congr_of_mem_nhds_within h₁ hx).differentiable_within_at lemma differentiable_on.congr_mono (h : differentiable_on 𝕜 f s) (h' : ∀x ∈ t, f₁ x = f x) (h₁ : t ⊆ s) : differentiable_on 𝕜 f₁ t := λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁ lemma differentiable_at.congr_of_mem_nhds (h : differentiable_at 𝕜 f x) (hL : {y | f₁ y = f y} ∈ nhds x) : differentiable_at 𝕜 f₁ x := has_fderiv_at.differentiable_at (has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_at hL (mem_of_nhds hL : _)) lemma differentiable_within_at.fderiv_within_congr_mono (h : differentiable_within_at 𝕜 f s x) (hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_diff_within_at 𝕜 t x) (h₁ : t ⊆ s) : fderiv_within 𝕜 f₁ t x = fderiv_within 𝕜 f s x := (has_fderiv_within_at.congr_mono h.has_fderiv_within_at hs hx h₁).fderiv_within hxt lemma fderiv_within_congr_of_mem_nhds_within (hs : unique_diff_within_at 𝕜 s x) (hL : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : fderiv_within 𝕜 f₁ s x = fderiv_within 𝕜 f s x := begin by_cases h : differentiable_within_at 𝕜 f s x ∨ differentiable_within_at 𝕜 f₁ s x, { cases h, { apply has_fderiv_within_at.fderiv_within _ hs, exact has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at hL hx }, { symmetry, apply has_fderiv_within_at.fderiv_within _ hs, apply has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at _ hx.symm, convert hL, ext y, exact eq_comm } }, { push_neg at h, have A : fderiv_within 𝕜 f s x = 0, by { unfold differentiable_within_at at h, simp [fderiv_within, h] }, have A₁ : fderiv_within 𝕜 f₁ s x = 0, by { unfold differentiable_within_at at h, simp [fderiv_within, h] }, rw [A, A₁] } end lemma fderiv_within_congr (hs : unique_diff_within_at 𝕜 s x) (hL : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) : fderiv_within 𝕜 f₁ s x = fderiv_within 𝕜 f s x := begin apply fderiv_within_congr_of_mem_nhds_within hs _ hx, apply mem_sets_of_superset self_mem_nhds_within, exact hL end lemma fderiv_congr_of_mem_nhds (hL : {y | f₁ y = f y} ∈ nhds x) : fderiv 𝕜 f₁ x = fderiv 𝕜 f x := begin have A : f₁ x = f x := (mem_of_nhds hL : _), rw [← fderiv_within_univ, ← fderiv_within_univ], rw ← nhds_within_univ at hL, exact fderiv_within_congr_of_mem_nhds_within unique_diff_within_at_univ hL A end end congr /- id -/ section id theorem has_fderiv_at_filter_id (x : E) (L : filter E) : has_fderiv_at_filter id (id : E →L[𝕜] E) x L := (is_o_zero _ _).congr_left $ by simp theorem has_fderiv_within_at_id (x : E) (s : set E) : has_fderiv_within_at id (id : E →L[𝕜] E) s x := has_fderiv_at_filter_id _ _ theorem has_fderiv_at_id (x : E) : has_fderiv_at id (id : E →L[𝕜] E) x := has_fderiv_at_filter_id _ _ lemma differentiable_at_id : differentiable_at 𝕜 id x := (has_fderiv_at_id x).differentiable_at lemma differentiable_within_at_id : differentiable_within_at 𝕜 id s x := differentiable_at_id.differentiable_within_at lemma differentiable_id : differentiable 𝕜 (id : E → E) := λx, differentiable_at_id lemma differentiable_on_id : differentiable_on 𝕜 id s := differentiable_id.differentiable_on lemma fderiv_id : fderiv 𝕜 id x = id := has_fderiv_at.fderiv (has_fderiv_at_id x) lemma fderiv_within_id (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 id s x = id := begin rw differentiable.fderiv_within (differentiable_at_id) hxs, exact fderiv_id end end id /- constants -/ section const theorem has_fderiv_at_filter_const (c : F) (x : E) (L : filter E) : has_fderiv_at_filter (λ x, c) (0 : E →L[𝕜] F) x L := (is_o_zero _ _).congr_left $ λ _, by simp only [zero_apply, sub_self] theorem has_fderiv_within_at_const (c : F) (x : E) (s : set E) : has_fderiv_within_at (λ x, c) (0 : E →L[𝕜] F) s x := has_fderiv_at_filter_const _ _ _ theorem has_fderiv_at_const (c : F) (x : E) : has_fderiv_at (λ x, c) (0 : E →L[𝕜] F) x := has_fderiv_at_filter_const _ _ _ lemma differentiable_at_const (c : F) : differentiable_at 𝕜 (λx, c) x := ⟨0, has_fderiv_at_const c x⟩ lemma differentiable_within_at_const (c : F) : differentiable_within_at 𝕜 (λx, c) s x := differentiable_at.differentiable_within_at (differentiable_at_const _) lemma fderiv_const (c : F) : fderiv 𝕜 (λy, c) x = 0 := has_fderiv_at.fderiv (has_fderiv_at_const c x) lemma fderiv_within_const (c : F) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λy, c) s x = 0 := begin rw differentiable.fderiv_within (differentiable_at_const _) hxs, exact fderiv_const _ end lemma differentiable_const (c : F) : differentiable 𝕜 (λx : E, c) := λx, differentiable_at_const _ lemma differentiable_on_const (c : F) : differentiable_on 𝕜 (λx, c) s := (differentiable_const _).differentiable_on end const /- Bounded linear maps -/ section is_bounded_linear_map lemma is_bounded_linear_map.has_fderiv_at_filter (h : is_bounded_linear_map 𝕜 f) : has_fderiv_at_filter f h.to_continuous_linear_map x L := begin have : (λ (x' : E), f x' - f x - h.to_continuous_linear_map (x' - x)) = λx', 0, { ext, have : ∀a, h.to_continuous_linear_map a = f a := λa, rfl, simp, simp [this] }, rw [has_fderiv_at_filter, this], exact asymptotics.is_o_zero _ _ end lemma is_bounded_linear_map.has_fderiv_within_at (h : is_bounded_linear_map 𝕜 f) : has_fderiv_within_at f h.to_continuous_linear_map s x := h.has_fderiv_at_filter lemma is_bounded_linear_map.has_fderiv_at (h : is_bounded_linear_map 𝕜 f) : has_fderiv_at f h.to_continuous_linear_map x := h.has_fderiv_at_filter lemma is_bounded_linear_map.differentiable_at (h : is_bounded_linear_map 𝕜 f) : differentiable_at 𝕜 f x := h.has_fderiv_at.differentiable_at lemma is_bounded_linear_map.differentiable_within_at (h : is_bounded_linear_map 𝕜 f) : differentiable_within_at 𝕜 f s x := h.differentiable_at.differentiable_within_at lemma is_bounded_linear_map.fderiv (h : is_bounded_linear_map 𝕜 f) : fderiv 𝕜 f x = h.to_continuous_linear_map := has_fderiv_at.fderiv (h.has_fderiv_at) lemma is_bounded_linear_map.fderiv_within (h : is_bounded_linear_map 𝕜 f) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 f s x = h.to_continuous_linear_map := begin rw differentiable.fderiv_within h.differentiable_at hxs, exact h.fderiv end lemma is_bounded_linear_map.differentiable (h : is_bounded_linear_map 𝕜 f) : differentiable 𝕜 f := λx, h.differentiable_at lemma is_bounded_linear_map.differentiable_on (h : is_bounded_linear_map 𝕜 f) : differentiable_on 𝕜 f s := h.differentiable.differentiable_on end is_bounded_linear_map /- multiplication by a constant -/ section smul_const theorem has_fderiv_at_filter.smul (h : has_fderiv_at_filter f f' x L) (c : 𝕜) : has_fderiv_at_filter (λ x, c • f x) (c • f') x L := (is_o_const_smul_left h c).congr_left $ λ x, by simp [smul_neg, smul_add] theorem has_fderiv_within_at.smul (h : has_fderiv_within_at f f' s x) (c : 𝕜) : has_fderiv_within_at (λ x, c • f x) (c • f') s x := h.smul c theorem has_fderiv_at.smul (h : has_fderiv_at f f' x) (c : 𝕜) : has_fderiv_at (λ x, c • f x) (c • f') x := h.smul c lemma differentiable_within_at.smul (h : differentiable_within_at 𝕜 f s x) (c : 𝕜) : differentiable_within_at 𝕜 (λy, c • f y) s x := (h.has_fderiv_within_at.smul c).differentiable_within_at lemma differentiable_at.smul (h : differentiable_at 𝕜 f x) (c : 𝕜) : differentiable_at 𝕜 (λy, c • f y) x := (h.has_fderiv_at.smul c).differentiable_at lemma differentiable_on.smul (h : differentiable_on 𝕜 f s) (c : 𝕜) : differentiable_on 𝕜 (λy, c • f y) s := λx hx, (h x hx).smul c lemma differentiable.smul (h : differentiable 𝕜 f) (c : 𝕜) : differentiable 𝕜 (λy, c • f y) := λx, (h x).smul c lemma fderiv_within_smul (hxs : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f s x) (c : 𝕜) : fderiv_within 𝕜 (λy, c • f y) s x = c • fderiv_within 𝕜 f s x := (h.has_fderiv_within_at.smul c).fderiv_within hxs lemma fderiv_smul (h : differentiable_at 𝕜 f x) (c : 𝕜) : fderiv 𝕜 (λy, c • f y) x = c • fderiv 𝕜 f x := (h.has_fderiv_at.smul c).fderiv end smul_const /- add -/ section add theorem has_fderiv_at_filter.add (hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) : has_fderiv_at_filter (λ y, f y + g y) (f' + g') x L := (hf.add hg).congr_left $ λ _, by simp theorem has_fderiv_within_at.add (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) : has_fderiv_within_at (λ y, f y + g y) (f' + g') s x := hf.add hg theorem has_fderiv_at.add (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) : has_fderiv_at (λ x, f x + g x) (f' + g') x := hf.add hg lemma differentiable_within_at.add (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : differentiable_within_at 𝕜 (λ y, f y + g y) s x := (hf.has_fderiv_within_at.add hg.has_fderiv_within_at).differentiable_within_at lemma differentiable_at.add (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : differentiable_at 𝕜 (λ y, f y + g y) x := (hf.has_fderiv_at.add hg.has_fderiv_at).differentiable_at lemma differentiable_on.add (hf : differentiable_on 𝕜 f s) (hg : differentiable_on 𝕜 g s) : differentiable_on 𝕜 (λy, f y + g y) s := λx hx, (hf x hx).add (hg x hx) lemma differentiable.add (hf : differentiable 𝕜 f) (hg : differentiable 𝕜 g) : differentiable 𝕜 (λy, f y + g y) := λx, (hf x).add (hg x) lemma fderiv_within_add (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : fderiv_within 𝕜 (λy, f y + g y) s x = fderiv_within 𝕜 f s x + fderiv_within 𝕜 g s x := (hf.has_fderiv_within_at.add hg.has_fderiv_within_at).fderiv_within hxs lemma fderiv_add (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : fderiv 𝕜 (λy, f y + g y) x = fderiv 𝕜 f x + fderiv 𝕜 g x := (hf.has_fderiv_at.add hg.has_fderiv_at).fderiv end add /- neg -/ section neg theorem has_fderiv_at_filter.neg (h : has_fderiv_at_filter f f' x L) : has_fderiv_at_filter (λ x, -f x) (-f') x L := (h.smul (-1:𝕜)).congr (by simp) (by simp) theorem has_fderiv_within_at.neg (h : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, -f x) (-f') s x := h.neg theorem has_fderiv_at.neg (h : has_fderiv_at f f' x) : has_fderiv_at (λ x, -f x) (-f') x := h.neg lemma differentiable_within_at.neg (h : differentiable_within_at 𝕜 f s x) : differentiable_within_at 𝕜 (λy, -f y) s x := h.has_fderiv_within_at.neg.differentiable_within_at lemma differentiable_at.neg (h : differentiable_at 𝕜 f x) : differentiable_at 𝕜 (λy, -f y) x := h.has_fderiv_at.neg.differentiable_at lemma differentiable_on.neg (h : differentiable_on 𝕜 f s) : differentiable_on 𝕜 (λy, -f y) s := λx hx, (h x hx).neg lemma differentiable.neg (h : differentiable 𝕜 f) : differentiable 𝕜 (λy, -f y) := λx, (h x).neg lemma fderiv_within_neg (hxs : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f s x) : fderiv_within 𝕜 (λy, -f y) s x = - fderiv_within 𝕜 f s x := h.has_fderiv_within_at.neg.fderiv_within hxs lemma fderiv_neg (h : differentiable_at 𝕜 f x) : fderiv 𝕜 (λy, -f y) x = - fderiv 𝕜 f x := h.has_fderiv_at.neg.fderiv end neg /- sub -/ section sub theorem has_fderiv_at_filter.sub (hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) : has_fderiv_at_filter (λ x, f x - g x) (f' - g') x L := hf.add hg.neg theorem has_fderiv_within_at.sub (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) : has_fderiv_within_at (λ x, f x - g x) (f' - g') s x := hf.sub hg theorem has_fderiv_at.sub (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) : has_fderiv_at (λ x, f x - g x) (f' - g') x := hf.sub hg lemma differentiable_within_at.sub (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : differentiable_within_at 𝕜 (λ y, f y - g y) s x := (hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).differentiable_within_at lemma differentiable_at.sub (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : differentiable_at 𝕜 (λ y, f y - g y) x := (hf.has_fderiv_at.sub hg.has_fderiv_at).differentiable_at lemma differentiable_on.sub (hf : differentiable_on 𝕜 f s) (hg : differentiable_on 𝕜 g s) : differentiable_on 𝕜 (λy, f y - g y) s := λx hx, (hf x hx).sub (hg x hx) lemma differentiable.sub (hf : differentiable 𝕜 f) (hg : differentiable 𝕜 g) : differentiable 𝕜 (λy, f y - g y) := λx, (hf x).sub (hg x) lemma fderiv_within_sub (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : fderiv_within 𝕜 (λy, f y - g y) s x = fderiv_within 𝕜 f s x - fderiv_within 𝕜 g s x := (hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).fderiv_within hxs lemma fderiv_sub (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : fderiv 𝕜 (λy, f y - g y) x = fderiv 𝕜 f x - fderiv 𝕜 g x := (hf.has_fderiv_at.sub hg.has_fderiv_at).fderiv theorem has_fderiv_at_filter.is_O_sub (h : has_fderiv_at_filter f f' x L) : is_O (λ x', f x' - f x) (λ x', x' - x) L := h.to_is_O.congr_of_sub.2 (f'.is_O_sub _ _) end sub /- Continuity -/ section continuous theorem has_fderiv_at_filter.tendsto_nhds (hL : L ≤ nhds x) (h : has_fderiv_at_filter f f' x L) : tendsto f L (nhds (f x)) := begin have : tendsto (λ x', f x' - f x) L (nhds 0), { refine h.is_O_sub.trans_tendsto (tendsto_le_left hL _), rw ← sub_self x, exact tendsto_sub tendsto_id tendsto_const_nhds }, have := tendsto_add this tendsto_const_nhds, rw zero_add (f x) at this, exact this.congr (by simp) end theorem has_fderiv_within_at.continuous_within_at (h : has_fderiv_within_at f f' s x) : continuous_within_at f s x := has_fderiv_at_filter.tendsto_nhds lattice.inf_le_left h theorem has_fderiv_at.continuous_at (h : has_fderiv_at f f' x) : continuous_at f x := has_fderiv_at_filter.tendsto_nhds (le_refl _) h lemma differentiable_within_at.continuous_within_at (h : differentiable_within_at 𝕜 f s x) : continuous_within_at f s x := let ⟨f', hf'⟩ := h in hf'.continuous_within_at lemma differentiable_at.continuous_at (h : differentiable_at 𝕜 f x) : continuous_at f x := let ⟨f', hf'⟩ := h in hf'.continuous_at lemma differentiable_on.continuous_on (h : differentiable_on 𝕜 f s) : continuous_on f s := λx hx, (h x hx).continuous_within_at lemma differentiable.continuous (h : differentiable 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λx, (h x).continuous_at end continuous /- Bounded bilinear maps -/ section bilinear_map variables {b : E × F → G} {u : set (E × F) } open normed_field lemma is_bounded_bilinear_map.has_fderiv_at (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) : has_fderiv_at b (h.deriv p) p := begin have : (λ (x : E × F), b x - b p - (h.deriv p) (x - p)) = (λx, b (x.1 - p.1, x.2 - p.2)), { ext x, delta is_bounded_bilinear_map.deriv, change b x - b p - (b (p.1, x.2-p.2) + b (x.1-p.1, p.2)) = b (x.1 - p.1, x.2 - p.2), have : b x = b (x.1, x.2), by { cases x, refl }, rw this, have : b p = b (p.1, p.2), by { cases p, refl }, rw this, simp only [h.map_sub_left, h.map_sub_right], abel }, rw [has_fderiv_at, has_fderiv_at_filter, this], rcases h.bound with ⟨C, Cpos, hC⟩, have A : asymptotics.is_O (λx : E × F, b (x.1 - p.1, x.2 - p.2)) (λx, ∥x - p∥ * ∥x - p∥) (nhds p) := ⟨C, Cpos, filter.univ_mem_sets' (λx, begin simp only [mem_set_of_eq, norm_mul, norm_norm], calc ∥b (x.1 - p.1, x.2 - p.2)∥ ≤ C * ∥x.1 - p.1∥ * ∥x.2 - p.2∥ : hC _ _ ... ≤ C * ∥x-p∥ * ∥x-p∥ : by apply_rules [mul_le_mul, le_max_left, le_max_right, norm_nonneg, le_of_lt Cpos, le_refl, mul_nonneg, norm_nonneg, norm_nonneg] ... = C * (∥x-p∥ * ∥x-p∥) : mul_assoc _ _ _ end)⟩, have B : asymptotics.is_o (λ (x : E × F), ∥x - p∥ * ∥x - p∥) (λx, 1 * ∥x - p∥) (nhds p), { apply asymptotics.is_o_mul_right _ (asymptotics.is_O_refl _ _), rw [asymptotics.is_o_iff_tendsto], { simp only [div_one], have : 0 = ∥p - p∥, by simp, rw this, have : continuous (λx, ∥x-p∥) := continuous_norm.comp (continuous_sub continuous_id continuous_const), exact this.tendsto p }, simp only [forall_prop_of_false, not_false_iff, one_ne_zero, forall_true_iff] }, simp only [one_mul, asymptotics.is_o_norm_right] at B, exact A.trans_is_o B end lemma is_bounded_bilinear_map.has_fderiv_within_at (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) : has_fderiv_within_at b (h.deriv p) u p := (h.has_fderiv_at p).has_fderiv_within_at lemma is_bounded_bilinear_map.differentiable_at (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) : differentiable_at 𝕜 b p := (h.has_fderiv_at p).differentiable_at lemma is_bounded_bilinear_map.differentiable_within_at (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) : differentiable_within_at 𝕜 b u p := (h.differentiable_at p).differentiable_within_at lemma is_bounded_bilinear_map.fderiv (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) : fderiv 𝕜 b p = h.deriv p := has_fderiv_at.fderiv (h.has_fderiv_at p) lemma is_bounded_bilinear_map.fderiv_within (h : is_bounded_bilinear_map 𝕜 b) (p : E × F) (hxs : unique_diff_within_at 𝕜 u p) : fderiv_within 𝕜 b u p = h.deriv p := begin rw differentiable.fderiv_within (h.differentiable_at p) hxs, exact h.fderiv p end lemma is_bounded_bilinear_map.differentiable (h : is_bounded_bilinear_map 𝕜 b) : differentiable 𝕜 b := λx, h.differentiable_at x lemma is_bounded_bilinear_map.differentiable_on (h : is_bounded_bilinear_map 𝕜 b) : differentiable_on 𝕜 b u := h.differentiable.differentiable_on lemma is_bounded_bilinear_map.continuous (h : is_bounded_bilinear_map 𝕜 b) : continuous b := h.differentiable.continuous end bilinear_map /- Cartesian products -/ section cartesian_product variables {f₂ : E → G} {f₂' : E →L[𝕜] G} lemma has_fderiv_at_filter.prod (hf₁ : has_fderiv_at_filter f₁ f₁' x L) (hf₂ : has_fderiv_at_filter f₂ f₂' x L) : has_fderiv_at_filter (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') x L := begin have : (λ (x' : E), (f₁ x', f₂ x') - (f₁ x, f₂ x) - (continuous_linear_map.prod f₁' f₂') (x' -x)) = (λ (x' : E), (f₁ x' - f₁ x - f₁' (x' - x), f₂ x' - f₂ x - f₂' (x' - x))) := rfl, rw [has_fderiv_at_filter, this], rw [asymptotics.is_o_prod_left], exact ⟨hf₁, hf₂⟩ end lemma has_fderiv_within_at.prod (hf₁ : has_fderiv_within_at f₁ f₁' s x) (hf₂ : has_fderiv_within_at f₂ f₂' s x) : has_fderiv_within_at (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') s x := hf₁.prod hf₂ lemma has_fderiv_at.prod (hf₁ : has_fderiv_at f₁ f₁' x) (hf₂ : has_fderiv_at f₂ f₂' x) : has_fderiv_at (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') x := hf₁.prod hf₂ lemma differentiable_within_at.prod (hf₁ : differentiable_within_at 𝕜 f₁ s x) (hf₂ : differentiable_within_at 𝕜 f₂ s x) : differentiable_within_at 𝕜 (λx:E, (f₁ x, f₂ x)) s x := (hf₁.has_fderiv_within_at.prod hf₂.has_fderiv_within_at).differentiable_within_at lemma differentiable_at.prod (hf₁ : differentiable_at 𝕜 f₁ x) (hf₂ : differentiable_at 𝕜 f₂ x) : differentiable_at 𝕜 (λx:E, (f₁ x, f₂ x)) x := (hf₁.has_fderiv_at.prod hf₂.has_fderiv_at).differentiable_at lemma differentiable_on.prod (hf₁ : differentiable_on 𝕜 f₁ s) (hf₂ : differentiable_on 𝕜 f₂ s) : differentiable_on 𝕜 (λx:E, (f₁ x, f₂ x)) s := λx hx, differentiable_within_at.prod (hf₁ x hx) (hf₂ x hx) lemma differentiable.prod (hf₁ : differentiable 𝕜 f₁) (hf₂ : differentiable 𝕜 f₂) : differentiable 𝕜 (λx:E, (f₁ x, f₂ x)) := λ x, differentiable_at.prod (hf₁ x) (hf₂ x) lemma differentiable_at.fderiv_prod (hf₁ : differentiable_at 𝕜 f₁ x) (hf₂ : differentiable_at 𝕜 f₂ x) : fderiv 𝕜 (λx:E, (f₁ x, f₂ x)) x = continuous_linear_map.prod (fderiv 𝕜 f₁ x) (fderiv 𝕜 f₂ x) := has_fderiv_at.fderiv (has_fderiv_at.prod hf₁.has_fderiv_at hf₂.has_fderiv_at) lemma differentiable_at.fderiv_within_prod (hf₁ : differentiable_within_at 𝕜 f₁ s x) (hf₂ : differentiable_within_at 𝕜 f₂ s x) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λx:E, (f₁ x, f₂ x)) s x = continuous_linear_map.prod (fderiv_within 𝕜 f₁ s x) (fderiv_within 𝕜 f₂ s x) := begin apply has_fderiv_within_at.fderiv_within _ hxs, exact has_fderiv_within_at.prod hf₁.has_fderiv_within_at hf₂.has_fderiv_within_at end end cartesian_product /- Composition -/ section composition /- For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to get confused since there are too many possibilities for composition -/ variable (x) theorem has_fderiv_at_filter.comp {g : F → G} {g' : F →L[𝕜] G} (hg : has_fderiv_at_filter g g' (f x) (L.map f)) (hf : has_fderiv_at_filter f f' x L) : has_fderiv_at_filter (g ∘ f) (g'.comp f') x L := let eq₁ := (g'.is_O_comp _ _).trans_is_o hf in let eq₂ := ((hg.comp f).mono le_comap_map).trans_is_O hf.is_O_sub in by { refine eq₂.tri (eq₁.congr_left (λ x', _)), simp } /- A readable version of the previous theorem, a general form of the chain rule. -/ example {g : F → G} {g' : F →L[𝕜] G} (hg : has_fderiv_at_filter g g' (f x) (L.map f)) (hf : has_fderiv_at_filter f f' x L) : has_fderiv_at_filter (g ∘ f) (g'.comp f') x L := begin unfold has_fderiv_at_filter at hg, have : is_o (λ x', g (f x') - g (f x) - g' (f x' - f x)) (λ x', f x' - f x) L, from (hg.comp f).mono le_comap_map, have eq₁ : is_o (λ x', g (f x') - g (f x) - g' (f x' - f x)) (λ x', x' - x) L, from this.trans_is_O hf.is_O_sub, have eq₂ : is_o (λ x', f x' - f x - f' (x' - x)) (λ x', x' - x) L, from hf, have : is_O (λ x', g' (f x' - f x - f' (x' - x))) (λ x', f x' - f x - f' (x' - x)) L, from g'.is_O_comp _ _, have : is_o (λ x', g' (f x' - f x - f' (x' - x))) (λ x', x' - x) L, from this.trans_is_o eq₂, have eq₃ : is_o (λ x', g' (f x' - f x) - (g' (f' (x' - x)))) (λ x', x' - x) L, by { refine this.congr_left _, simp}, exact eq₁.tri eq₃ end theorem has_fderiv_within_at.comp {g : F → G} {g' : F →L[𝕜] G} (hg : has_fderiv_within_at g g' (f '' s) (f x)) (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (g ∘ f) (g'.comp f') s x := (has_fderiv_at_filter.mono hg hf.continuous_within_at.tendsto_nhds_within_image).comp x hf /-- The chain rule. -/ theorem has_fderiv_at.comp {g : F → G} {g' : F →L[𝕜] G} (hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_at f f' x) : has_fderiv_at (g ∘ f) (g'.comp f') x := (hg.mono hf.continuous_at).comp x hf theorem has_fderiv_at.comp_has_fderiv_within_at {g : F → G} {g' : F →L[𝕜] G} (hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (g ∘ f) (g'.comp f') s x := begin rw ← has_fderiv_within_at_univ at hg, exact has_fderiv_within_at.comp x (hg.mono (subset_univ _)) hf end lemma differentiable_within_at.comp {g : F → G} {t : set F} (hg : differentiable_within_at 𝕜 g t (f x)) (hf : differentiable_within_at 𝕜 f s x) (h : f '' s ⊆ t) : differentiable_within_at 𝕜 (g ∘ f) s x := begin rcases hf with ⟨f', hf'⟩, rcases hg with ⟨g', hg'⟩, exact ⟨continuous_linear_map.comp g' f', (hg'.mono h).comp x hf'⟩ end lemma differentiable_at.comp {g : F → G} (hg : differentiable_at 𝕜 g (f x)) (hf : differentiable_at 𝕜 f x) : differentiable_at 𝕜 (g ∘ f) x := (hg.has_fderiv_at.comp x hf.has_fderiv_at).differentiable_at lemma fderiv_within.comp {g : F → G} {t : set F} (hg : differentiable_within_at 𝕜 g t (f x)) (hf : differentiable_within_at 𝕜 f s x) (h : f '' s ⊆ t) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (g ∘ f) s x = continuous_linear_map.comp (fderiv_within 𝕜 g t (f x)) (fderiv_within 𝕜 f s x) := begin apply has_fderiv_within_at.fderiv_within _ hxs, apply has_fderiv_within_at.comp x _ (hf.has_fderiv_within_at), apply hg.has_fderiv_within_at.mono h end lemma fderiv.comp {g : F → G} (hg : differentiable_at 𝕜 g (f x)) (hf : differentiable_at 𝕜 f x) : fderiv 𝕜 (g ∘ f) x = continuous_linear_map.comp (fderiv 𝕜 g (f x)) (fderiv 𝕜 f x) := begin apply has_fderiv_at.fderiv, exact has_fderiv_at.comp x hg.has_fderiv_at hf.has_fderiv_at end lemma differentiable_on.comp {g : F → G} {t : set F} (hg : differentiable_on 𝕜 g t) (hf : differentiable_on 𝕜 f s) (st : f '' s ⊆ t) : differentiable_on 𝕜 (g ∘ f) s := λx hx, differentiable_within_at.comp x (hg (f x) (st (mem_image_of_mem _ hx))) (hf x hx) st lemma differentiable.comp {g : F → G} (hg : differentiable 𝕜 g) (hf : differentiable 𝕜 f) : differentiable 𝕜 (g ∘ f) := λx, differentiable_at.comp x (hg (f x)) (hf x) end composition /- Multiplication by a scalar function -/ section smul variables {c : E → 𝕜} {c' : E →L[𝕜] 𝕜} theorem has_fderiv_within_at.smul' (hc : has_fderiv_within_at c c' s x) (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ y, c y • f y) (c x • f' + c'.scalar_prod_space_iso (f x)) s x := begin have : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × F), p.1 • p.2) := is_bounded_bilinear_map_smul, exact has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, f x)) (hc.prod hf) end theorem has_fderiv_at.smul' (hc : has_fderiv_at c c' x) (hf : has_fderiv_at f f' x) : has_fderiv_at (λ y, c y • f y) (c x • f' + c'.scalar_prod_space_iso (f x)) x := begin have : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × F), p.1 • p.2) := is_bounded_bilinear_map_smul, exact has_fderiv_at.comp x (this.has_fderiv_at (c x, f x)) (hc.prod hf) end lemma differentiable_within_at.smul' (hc : differentiable_within_at 𝕜 c s x) (hf : differentiable_within_at 𝕜 f s x) : differentiable_within_at 𝕜 (λ y, c y • f y) s x := (hc.has_fderiv_within_at.smul' hf.has_fderiv_within_at).differentiable_within_at lemma differentiable_at.smul' (hc : differentiable_at 𝕜 c x) (hf : differentiable_at 𝕜 f x) : differentiable_at 𝕜 (λ y, c y • f y) x := (hc.has_fderiv_at.smul' hf.has_fderiv_at).differentiable_at lemma differentiable_on.smul' (hc : differentiable_on 𝕜 c s) (hf : differentiable_on 𝕜 f s) : differentiable_on 𝕜 (λ y, c y • f y) s := λx hx, (hc x hx).smul' (hf x hx) lemma differentiable.smul' (hc : differentiable 𝕜 c) (hf : differentiable 𝕜 f) : differentiable 𝕜 (λ y, c y • f y) := λx, (hc x).smul' (hf x) lemma fderiv_within_smul' (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hf : differentiable_within_at 𝕜 f s x) : fderiv_within 𝕜 (λ y, c y • f y) s x = c x • fderiv_within 𝕜 f s x + (fderiv_within 𝕜 c s x).scalar_prod_space_iso (f x) := (hc.has_fderiv_within_at.smul' hf.has_fderiv_within_at).fderiv_within hxs lemma fderiv_smul' (hc : differentiable_at 𝕜 c x) (hf : differentiable_at 𝕜 f x) : fderiv 𝕜 (λ y, c y • f y) x = c x • fderiv 𝕜 f x + (fderiv 𝕜 c x).scalar_prod_space_iso (f x) := (hc.has_fderiv_at.smul' hf.has_fderiv_at).fderiv end smul /- Multiplication of scalar functions -/ section mul set_option class.instance_max_depth 120 variables {c d : E → 𝕜} {c' d' : E →L[𝕜] 𝕜} theorem has_fderiv_within_at.mul (hc : has_fderiv_within_at c c' s x) (hd : has_fderiv_within_at d d' s x) : has_fderiv_within_at (λ y, c y * d y) (c x • d' + d x • c') s x := begin have : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := is_bounded_bilinear_map_mul, convert has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, d x)) (hc.prod hd), ext z, change c x * d' z + d x * c' z = c x * d' z + c' z * d x, ring end theorem has_fderiv_at.mul (hc : has_fderiv_at c c' x) (hd : has_fderiv_at d d' x) : has_fderiv_at (λ y, c y * d y) (c x • d' + d x • c') x := begin have : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := is_bounded_bilinear_map_mul, convert has_fderiv_at.comp x (this.has_fderiv_at (c x, d x)) (hc.prod hd), ext z, change c x * d' z + d x * c' z = c x * d' z + c' z * d x, ring end lemma differentiable_within_at.mul (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) : differentiable_within_at 𝕜 (λ y, c y * d y) s x := (hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).differentiable_within_at lemma differentiable_at.mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) : differentiable_at 𝕜 (λ y, c y * d y) x := (hc.has_fderiv_at.mul hd.has_fderiv_at).differentiable_at lemma differentiable_on.mul (hc : differentiable_on 𝕜 c s) (hd : differentiable_on 𝕜 d s) : differentiable_on 𝕜 (λ y, c y * d y) s := λx hx, (hc x hx).mul (hd x hx) lemma differentiable.mul (hc : differentiable 𝕜 c) (hd : differentiable 𝕜 d) : differentiable 𝕜 (λ y, c y * d y) := λx, (hc x).mul (hd x) lemma fderiv_within_mul (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) : fderiv_within 𝕜 (λ y, c y * d y) s x = c x • fderiv_within 𝕜 d s x + d x • fderiv_within 𝕜 c s x := (hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).fderiv_within hxs lemma fderiv_mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) : fderiv 𝕜 (λ y, c y * d y) x = c x • fderiv 𝕜 d x + d x • fderiv 𝕜 c x := (hc.has_fderiv_at.mul hd.has_fderiv_at).fderiv end mul end /- In the special case of a normed space over the reals, we can use scalar multiplication in the `tendsto` characterization of the Fréchet derivative. -/ section 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] theorem has_fderiv_at_filter_real_equiv {f : E → F} {f' : E →L[ℝ] F} {x : E} {L : filter E} : tendsto (λ x' : E, ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) L (nhds 0) ↔ tendsto (λ x' : E, ∥x' - x∥⁻¹ • (f x' - f x - f' (x' - x))) L (nhds 0) := begin symmetry, rw [tendsto_iff_norm_tendsto_zero], refine tendsto.congr'r (λ x', _), have : ∥x' + -x∥⁻¹ ≥ 0, from inv_nonneg.mpr (norm_nonneg _), simp [norm_smul, real.norm_eq_abs, abs_of_nonneg this] end end
4749b2e98231108af90f70c5f18349773025bd60
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/covariant_and_contravariant.lean
b234d416f397415b6a6f8ac3eb4d8da902146f8e
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
14,187
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.group.defs import order.basic import order.monotone /-! # Covariants and contravariants This file contains general lemmas and instances to work with the interactions between a relation and an action on a Type. The intended application is the splitting of the ordering from the algebraic assumptions on the operations in the `ordered_[...]` hierarchy. The strategy is to introduce two more flexible typeclasses, `covariant_class` and `contravariant_class`: * `covariant_class` models the implication `a ≤ b → c * a ≤ c * b` (multiplication is monotone), * `contravariant_class` models the implication `a * b < a * c → b < c`. Since `co(ntra)variant_class` takes as input the operation (typically `(+)` or `(*)`) and the order relation (typically `(≤)` or `(<)`), these are the only two typeclasses that I have used. The general approach is to formulate the lemma that you are interested in and prove it, with the `ordered_[...]` typeclass of your liking. After that, you convert the single typeclass, say `[ordered_cancel_monoid M]`, into three typeclasses, e.g. `[left_cancel_semigroup M] [partial_order M] [covariant_class M M (function.swap (*)) (≤)]` and have a go at seeing if the proof still works! Note that it is possible to combine several co(ntra)variant_class assumptions together. Indeed, the usual ordered typeclasses arise from assuming the pair `[covariant_class M M (*) (≤)] [contravariant_class M M (*) (<)]` on top of order/algebraic assumptions. A formal remark is that normally `covariant_class` uses the `(≤)`-relation, while `contravariant_class` uses the `(<)`-relation. This need not be the case in general, but seems to be the most common usage. In the opposite direction, the implication ```lean [semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] => left_cancel_semigroup α ``` holds -- note the `co*ntra*` assumption on the `(≤)`-relation. # Formalization notes We stick to the convention of using `function.swap (*)` (or `function.swap (+)`), for the typeclass assumptions, since `function.swap` is slightly better behaved than `flip`. However, sometimes as a **non-typeclass** assumption, we prefer `flip (*)` (or `flip (+)`), as it is easier to use. -/ -- TODO: convert `has_exists_mul_of_le`, `has_exists_add_of_le`? -- TODO: relationship with `con/add_con` -- TODO: include equivalence of `left_cancel_semigroup` with -- `semigroup partial_order contravariant_class α α (*) (≤)`? -- TODO : use ⇒, as per Eric's suggestion? See -- https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/ordered.20stuff/near/236148738 -- for a discussion. open function section variants variables {M N : Type*} (μ : M → N → N) (r : N → N → Prop) variables (M N) /-- `covariant` is useful to formulate succintly statements about the interactions between an action of a Type on another one and a relation on the acted-upon Type. See the `covariant_class` doc-string for its meaning. -/ def covariant : Prop := ∀ (m) {n₁ n₂}, r n₁ n₂ → r (μ m n₁) (μ m n₂) /-- `contravariant` is useful to formulate succintly statements about the interactions between an action of a Type on another one and a relation on the acted-upon Type. See the `contravariant_class` doc-string for its meaning. -/ def contravariant : Prop := ∀ (m) {n₁ n₂}, r (μ m n₁) (μ m n₂) → r n₁ n₂ /-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the `covariant_class` says that "the action `μ` preserves the relation `r`." More precisely, the `covariant_class` is a class taking two Types `M N`, together with an "action" `μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the assertion that for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)`, obtained from `(n₁, n₂)` by acting upon it by `m`. If `m : M` and `h : r n₁ n₂`, then `covariant_class.elim m h : r (μ m n₁) (μ m n₂)`. -/ @[protect_proj] class covariant_class : Prop := (elim : covariant M N μ r) /-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the `contravariant_class` says that "if the result of the action `μ` on a pair satisfies the relation `r`, then the initial pair satisfied the relation `r`." More precisely, the `contravariant_class` is a class taking two Types `M N`, together with an "action" `μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the assertion that for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by acting upon it by `m`, then, the relation `r` also holds for the pair `(n₁, n₂)`. If `m : M` and `h : r (μ m n₁) (μ m n₂)`, then `contravariant_class.elim m h : r n₁ n₂`. -/ @[protect_proj] class contravariant_class : Prop := (elim : contravariant M N μ r) lemma rel_iff_cov [covariant_class M N μ r] [contravariant_class M N μ r] (m : M) {a b : N} : r (μ m a) (μ m b) ↔ r a b := ⟨contravariant_class.elim _, covariant_class.elim _⟩ section flip variables {M N μ r} lemma covariant.flip (h : covariant M N μ r) : covariant M N μ (flip r) := λ a b c hbc, h a hbc lemma contravariant.flip (h : contravariant M N μ r) : contravariant M N μ (flip r) := λ a b c hbc, h a hbc end flip section covariant variables {M N μ r} [covariant_class M N μ r] lemma act_rel_act_of_rel (m : M) {a b : N} (ab : r a b) : r (μ m a) (μ m b) := covariant_class.elim _ ab @[to_additive] lemma group.covariant_iff_contravariant [group N] : covariant N N (*) r ↔ contravariant N N (*) r := begin refine ⟨λ h a b c bc, _, λ h a b c bc, _⟩, { rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c], exact h a⁻¹ bc }, { rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c] at bc, exact h a⁻¹ bc } end @[priority 100, to_additive] instance group.covconv [group N] [covariant_class N N (*) r] : contravariant_class N N (*) r := ⟨group.covariant_iff_contravariant.mp covariant_class.elim⟩ @[to_additive] lemma group.covariant_swap_iff_contravariant_swap [group N] : covariant N N (swap (*)) r ↔ contravariant N N (swap (*)) r := begin refine ⟨λ h a b c bc, _, λ h a b c bc, _⟩, { rw [← mul_inv_cancel_right b a, ← mul_inv_cancel_right c a], exact h a⁻¹ bc }, { rw [← mul_inv_cancel_right b a, ← mul_inv_cancel_right c a] at bc, exact h a⁻¹ bc } end @[priority 100, to_additive] instance group.covconv_swap [group N] [covariant_class N N (swap (*)) r] : contravariant_class N N (swap (*)) r := ⟨group.covariant_swap_iff_contravariant_swap.mp covariant_class.elim⟩ section is_trans variables [is_trans N r] (m n : M) {a b c d : N} /- Lemmas with 3 elements. -/ lemma act_rel_of_rel_of_act_rel (ab : r a b) (rl : r (μ m b) c) : r (μ m a) c := trans (act_rel_act_of_rel m ab) rl lemma rel_act_of_rel_of_rel_act (ab : r a b) (rr : r c (μ m a)) : r c (μ m b) := trans rr (act_rel_act_of_rel _ ab) end is_trans end covariant /- Lemma with 4 elements. -/ section M_eq_N variables {M N μ r} {mu : N → N → N} [is_trans N r] [covariant_class N N mu r] [covariant_class N N (swap mu) r] {a b c d : N} lemma act_rel_act_of_rel_of_rel (ab : r a b) (cd : r c d) : r (mu a c) (mu b d) := trans (act_rel_act_of_rel c ab : _) (act_rel_act_of_rel b cd) end M_eq_N section contravariant variables {M N μ r} [contravariant_class M N μ r] lemma rel_of_act_rel_act (m : M) {a b : N} (ab : r (μ m a) (μ m b)) : r a b := contravariant_class.elim _ ab section is_trans variables [is_trans N r] (m n : M) {a b c d : N} /- Lemmas with 3 elements. -/ lemma act_rel_of_act_rel_of_rel_act_rel (ab : r (μ m a) b) (rl : r (μ m b) (μ m c)) : r (μ m a) c := trans ab (rel_of_act_rel_act m rl) lemma rel_act_of_act_rel_act_of_rel_act (ab : r (μ m a) (μ m b)) (rr : r b (μ m c)) : r a (μ m c) := trans (rel_of_act_rel_act m ab) rr end is_trans end contravariant section monotone variables {α : Type*} {M N μ} [preorder α] [preorder N] variable {f : N → α} /-- The partial application of a constant to a covariant operator is monotone. -/ lemma covariant.monotone_of_const [covariant_class M N μ (≤)] (m : M) : monotone (μ m) := λ a b ha, covariant_class.elim m ha /-- A monotone function remains monotone when composed with the partial application of a covariant operator. E.g., `∀ (m : ℕ), monotone f → monotone (λ n, f (m + n))`. -/ lemma monotone.covariant_of_const [covariant_class M N μ (≤)] (hf : monotone f) (m : M) : monotone (λ n, f (μ m n)) := hf.comp $ covariant.monotone_of_const m /-- Same as `monotone.covariant_of_const`, but with the constant on the other side of the operator. E.g., `∀ (m : ℕ), monotone f → monotone (λ n, f (n + m))`. -/ lemma monotone.covariant_of_const' {μ : N → N → N} [covariant_class N N (swap μ) (≤)] (hf : monotone f) (m : N) : monotone (λ n, f (μ n m)) := hf.comp $ covariant.monotone_of_const m /-- Dual of `monotone.covariant_of_const` -/ lemma antitone.covariant_of_const [covariant_class M N μ (≤)] (hf : antitone f) (m : M) : antitone (λ n, f (μ m n)) := hf.comp_monotone $ covariant.monotone_of_const m /-- Dual of `monotone.covariant_of_const'` -/ lemma antitone.covariant_of_const' {μ : N → N → N} [covariant_class N N (swap μ) (≤)] (hf : antitone f) (m : N) : antitone (λ n, f (μ n m)) := hf.comp_monotone $ covariant.monotone_of_const m end monotone lemma covariant_le_of_covariant_lt [partial_order N] : covariant M N μ (<) → covariant M N μ (≤) := begin refine λ h a b c bc, _, rcases le_iff_eq_or_lt.mp bc with rfl | bc, { exact rfl.le }, { exact (h _ bc).le } end lemma contravariant_lt_of_contravariant_le [partial_order N] : contravariant M N μ (≤) → contravariant M N μ (<) := begin refine λ h a b c bc, lt_iff_le_and_ne.mpr ⟨h a bc.le, _⟩, rintro rfl, exact lt_irrefl _ bc, end lemma covariant_le_iff_contravariant_lt [linear_order N] : covariant M N μ (≤) ↔ contravariant M N μ (<) := ⟨ λ h a b c bc, not_le.mp (λ k, not_le.mpr bc (h _ k)), λ h a b c bc, not_lt.mp (λ k, not_lt.mpr bc (h _ k))⟩ lemma covariant_lt_iff_contravariant_le [linear_order N] : covariant M N μ (<) ↔ contravariant M N μ (≤) := ⟨ λ h a b c bc, not_lt.mp (λ k, not_lt.mpr bc (h _ k)), λ h a b c bc, not_le.mp (λ k, not_le.mpr bc (h _ k))⟩ @[to_additive] lemma covariant_flip_mul_iff [comm_semigroup N] : covariant N N (flip (*)) (r) ↔ covariant N N (*) (r) := by rw is_symm_op.flip_eq @[to_additive] lemma contravariant_flip_mul_iff [comm_semigroup N] : contravariant N N (flip (*)) (r) ↔ contravariant N N (*) (r) := by rw is_symm_op.flip_eq @[to_additive] instance contravariant_mul_lt_of_covariant_mul_le [has_mul N] [linear_order N] [covariant_class N N (*) (≤)] : contravariant_class N N (*) (<) := { elim := (covariant_le_iff_contravariant_lt N N (*)).mp covariant_class.elim } @[to_additive] instance covariant_mul_lt_of_contravariant_mul_le [has_mul N] [linear_order N] [contravariant_class N N (*) (≤)] : covariant_class N N (*) (<) := { elim := (covariant_lt_iff_contravariant_le N N (*)).mpr contravariant_class.elim } @[to_additive] instance covariant_swap_mul_le_of_covariant_mul_le [comm_semigroup N] [has_le N] [covariant_class N N (*) (≤)] : covariant_class N N (swap (*)) (≤) := { elim := (covariant_flip_mul_iff N (≤)).mpr covariant_class.elim } @[to_additive] instance contravariant_swap_mul_le_of_contravariant_mul_le [comm_semigroup N] [has_le N] [contravariant_class N N (*) (≤)] : contravariant_class N N (swap (*)) (≤) := { elim := (contravariant_flip_mul_iff N (≤)).mpr contravariant_class.elim } @[to_additive] instance contravariant_swap_mul_lt_of_contravariant_mul_lt [comm_semigroup N] [has_lt N] [contravariant_class N N (*) (<)] : contravariant_class N N (swap (*)) (<) := { elim := (contravariant_flip_mul_iff N (<)).mpr contravariant_class.elim } @[to_additive] instance covariant_swap_mul_lt_of_covariant_mul_lt [comm_semigroup N] [has_lt N] [covariant_class N N (*) (<)] : covariant_class N N (swap (*)) (<) := { elim := (covariant_flip_mul_iff N (<)).mpr covariant_class.elim } @[to_additive] instance left_cancel_semigroup.covariant_mul_lt_of_covariant_mul_le [left_cancel_semigroup N] [partial_order N] [covariant_class N N (*) (≤)] : covariant_class N N (*) (<) := { elim := λ a b c bc, by { cases lt_iff_le_and_ne.mp bc with bc cb, exact lt_iff_le_and_ne.mpr ⟨covariant_class.elim a bc, (mul_ne_mul_right a).mpr cb⟩ } } @[to_additive] instance right_cancel_semigroup.covariant_swap_mul_lt_of_covariant_swap_mul_le [right_cancel_semigroup N] [partial_order N] [covariant_class N N (swap (*)) (≤)] : covariant_class N N (swap (*)) (<) := { elim := λ a b c bc, by { cases lt_iff_le_and_ne.mp bc with bc cb, exact lt_iff_le_and_ne.mpr ⟨covariant_class.elim a bc, (mul_ne_mul_left a).mpr cb⟩ } } @[to_additive] instance left_cancel_semigroup.contravariant_mul_le_of_contravariant_mul_lt [left_cancel_semigroup N] [partial_order N] [contravariant_class N N (*) (<)] : contravariant_class N N (*) (≤) := { elim := λ a b c bc, by { cases le_iff_eq_or_lt.mp bc with h h, { exact ((mul_right_inj a).mp h).le }, { exact (contravariant_class.elim _ h).le } } } @[to_additive] instance right_cancel_semigroup.contravariant_swap_mul_le_of_contravariant_swap_mul_lt [right_cancel_semigroup N] [partial_order N] [contravariant_class N N (swap (*)) (<)] : contravariant_class N N (swap (*)) (≤) := { elim := λ a b c bc, by { cases le_iff_eq_or_lt.mp bc with h h, { exact ((mul_left_inj a).mp h).le }, { exact (contravariant_class.elim _ h).le } } } end variants
e5a251be82bd5f4d31c967f7e37237a017f762bc
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/data/finset.lean
fbe0802e22a11c1b994a246687d91e05aefe75c9
[ "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
58,319
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro Finite sets. -/ import logic.embedding order.boolean_algebra algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ h := by congr; assumption @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 end finset namespace finset instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /- set coercion -/ /-- Convert a finset to a set in the natural way. -/ def to_set (s : finset α) : set α := {x | x ∈ s} instance : has_lift (finset α) (set α) := ⟨to_set⟩ @[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl /- extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[extensionality] theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext.2 @[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := (set.ext_iff _ _).trans ext.symm /- subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp [set.ssubset_iff_subset_not_subset] {contextual := tt} @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff /- empty -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ | e := not_mem_empty a $ e ▸ h @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨λ h, by simp [h], λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := by simp [set.ext_iff] /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩ local prefix `ι`:90 := singleton @[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := by simp [singleton] theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := by simp theorem mem_singleton_self (a : α) : a ∈ ι a := by simp theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := by simp [set.ext_iff] /- insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ @[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by simp [erase_dup_cons] theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := by simp theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := by simp * theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := by simp [set.ext_iff] @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext.2 $ by simp [or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext.2 $ by simp @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem subset_insert [h : decidable_eq α] (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume ⟨h₁, h₂⟩, have ∃a, a ∈ t ∧ a ∉ s, by simpa [finset.subset_iff, classical.not_forall] using h₂, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s @[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl @[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a := by simp [singleton] /- union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := by simp * theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := by simp * theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by simp [not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := by simp [set.ext_iff] theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left {s₁ s₂ : finset α} : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right {s₁ s₂ : finset α} : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ @[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := by simp [ext, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := by simp [ext, or_comm, or.left_comm] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ by simp instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext.2 $ by simp [or_comm, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by simp @[simp] theorem union_self (s : finset α) : s ∪ s = s := by simp @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := by simp [ext] @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := by simp [ext] theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := by simp [ext, or_comm, or.left_comm] @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp [ext, or_comm, or.left_comm] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp [ext, or.left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp [ext] /- inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp [subset_iff] {contextual:=tt}; finish @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := by simp [set.ext_iff] @[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ by simp [and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ by simp [and_comm, and.left_comm] @[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ by simp @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ by simp @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ by simp @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext.2 $ by simp; intro x; constructor; finish @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext.2 $ assume a', by by_cases h' : a' = a; simp [mem_inter, mem_insert, h, h', and_comm] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} : a ∈ s → ι a ∩ s = ι a := show a ∈ s → insert a ∅ ∩ s = insert a ∅, by simp {contextual := tt} @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} : a ∉ s → ι a ∩ s = ∅ := show a ∉ s → insert a ∅ ∩ s = ∅, by simp {contextual := tt} @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] /- lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := assume a b, subset_union_left, le_sup_right := assume a b, subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := assume a b, inter_subset_left, inf_le_right := assume a b, inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp [subset_iff, and_imp, or_imp_distrib] {contextual:=tt}, ..finset.lattice.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish /- erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := by simp @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp {contextual:=tt} theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp {contextual:=tt} theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext.2 $ assume x, by simp; constructor; finish theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext.2 $ assume x, by simp; constructor; finish theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := by simp [set.ext_iff, and_comm] lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp [subset_iff, or_iff_not_imp_left]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /- sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 @[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext.2 $ λ a, by simpa [or_and_distrib_left, dec_em] using or_iff_right_of_imp (@h a) @[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := ext.2 $ by simp {contextual := tt} @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := by simp theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa [subset_iff] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := by simp [set.ext_iff] end decidable_eq /- attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /- filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext.2 $ assume a, by simp [and_comm, and.left_comm] @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext.2 $ assume a, by simp variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext.2 $ by simp [or_and_distrib_right] theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext.2 $ by simp [and_or_distrib_left] theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ by simp [and_comm, and.left_comm] theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext.2 $ by simpa [and_comm] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ by simp theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp [filter_not] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp [filter_not] @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := by simp [set.ext_iff] end filter /- range -/ section range variables {n m l : ℕ} /-- `range n` is the set of integers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ by simp @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := finset.induction_on s ⟨0, by simp⟩ $ λ a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa using le_max_left (a+1) n, subset.trans hn (by simp [le_max_right])⟩⟩ end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp [or_and_distrib_right, exists_or_distrib] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := by simp theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp [or_imp_distrib, forall_and_distrib] end finset /- erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset section map open function def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) (s : finset α) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.map f := mem_map.2 ⟨_, h, rfl⟩ theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ by simp theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ by simp [embedding.refl] theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem map_subset_map {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.map f ⊆ s₂.map f := by simp [subset_def, map_subset_map h] theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext.2 $ λ b, by simp; rw ← exists_and_distrib_right; refine exists_congr (λ a, (and_congr_right $ λ e, _).trans and.right_comm); simp [e.2.symm] theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext.2 $ by simp [mem_map, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext.2 $ by simp [mem_map]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, f.2 (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) := ext.2 $ by simp [mem_map, eq_comm] @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp [insert_eq, map_union] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by simp [embedding.subtype]; rw attach_val; simp [multiset.attach_map_val] end map section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := by simp [set.ext_iff] theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ by simp @[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ by simp theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp [subset_def, multiset.map_subset_map h] theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ λ b, by simp [and_comm]; rw ← exists_and_distrib_left; exact exists_congr (λ a, and.left_comm.trans $ and_congr_right $ λ e, by simp [e.symm]) theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext.2 $ by simp [mem_image, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext.2 $ by simp [mem_image]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (f a) := ext.2 $ by simp [mem_image, eq_comm] @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp [insert_eq, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by simp [multiset.attach_map_val] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := begin apply eq_of_veq, dsimp, rw [attach_ndinsert, multiset.erase_dup_eq_self.2], { refl }, apply nodup_map_on, exact assume ⟨a', _⟩ _ ⟨b', _⟩ _ h, by simp at h; simp [h], exact multiset.nodup_attach.2 s.2 end theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm end image /- card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ := pos_iff_ne_zero.trans $ not_congr card_eq_zero @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa [card] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; simp [h, nat.le_add_right] @[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_range (n : ℕ) : card (range n) = n := card_range n theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp [card, image_val_of_inj_on H] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa [ext], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n lemma card_eq_succ [decidable_eq α] {s : finset α} {a : α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have card s > 0, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp [eq, card_erase_of_mem has]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card [decidable_eq α] {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on [decidable_eq α] [decidable_eq β] {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simp; assumption) (by simp; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) @[elab_as_eliminator] lemma strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) end card section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp [mem_def] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext.2 $ by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a := show (insert a ∅ : finset α).bind t = t a, by simp theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp {contextual := tt}) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [image_union] {contextual := tt}) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext.2 $ by simp lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa [finset.subset_iff] lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := finset.ext.mpr $ by simp [eq_comm] end bind section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext.2 $ by simp [and.left_comm] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} : s₁ ⊆ s₂ → (∀a, t₁ a ⊆ t₂ a) → s₁.sigma t₁ ⊆ s₂.sigma t₂ := by simp [subset_iff, mem_sigma] {contextual := tt} theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext.2 $ by simp [and.left_comm] end sigma section pi variables {δ : α → Type*} [decidable_eq α] def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa using h) = pi.cons s a b e₂ e (by simpa using h), by rw [eq], this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end end pi section powerset def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp [powerset]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) end powerset section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by simp [fold, ndinsert_of_not_mem h] @[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b := by simp [fold] @[simp] theorem fold_image [decidable_eq α] [decidable_eq γ] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp [fold, image_val_of_inj_on H, map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp [fold] end fold section sup variables [semilattice_sup_bot α] [decidable_eq α] [decidable_eq β] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_val : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := calc _ = f b ⊔ (∅:finset β).sup f : sup_insert ... = f b : by simp lemma sup_union : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g := finset.induction_on s (by simp) (by simp [-sup_le_iff, sup_le_sup] {contextual := tt}) lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := calc f b ≤ f b ⊔ s.sup f : le_sup_left ... = (insert b s).sup f : by simp ... = s.sup f : by simp [hb] lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := finset.induction_on s (by simp) (by simp {contextual := tt}) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) end sup section inf variables [semilattice_inf_top α] [decidable_eq α] [decidable_eq β] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_val : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty @[simp] lemma inf_insert {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := calc _ = f b ⊓ (∅:finset β).inf f : inf_insert ... = f b : by simp lemma inf_union : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g := finset.induction_on s (by simp) (by simp [inf_le_inf] {contextual := tt}) lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := calc f b ≥ f b ⊓ s.inf f : inf_le_left ... = (insert b s).inf f : by simp ... = s.inf f : by simp [hb] lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := finset.induction_on s (by simp) (by simp {contextual := tt}) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) end inf /- max and min of finite sets -/ section max_min variables [decidable_linear_order α] protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ _ _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := by simp [finset.max] @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := by simp [finset.max, fold_insert_idem] @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by simp [finset.max, option.lift_or_get] theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := max_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (by simp) $ λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ _ _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := by simp [finset.min] @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := by simp [finset.min, fold_insert_idem] @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by simp [finset.min, option.lift_or_get] theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := min_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (by simp) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end theorem le_min_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption end max_min section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ end sort section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp [_root_.disjoint, subset_iff]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp [disjoint_left]; refl @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := by rw disjoint.comm; simp @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp [disjoint_left, or_imp_distrib, forall_and_distrib]; refl @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint_left, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint_right, or_imp_distrib, forall_and_distrib] @[simp] theorem card_disjoint_union {s t : finset α} : disjoint s t → card (s ∪ t) = card s + card t := finset.induction_on s (by simp) $ by simp {contextual := tt} end disjoint theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ end finset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} : l.nodup → l.to_finset.card = l.length := begin induction l, case list.nil { simp }, case list.cons : _ _ ih { intros nd, simp at nd, simp [finset.card_insert_of_not_mem ((not_iff_not_of_iff mem_to_finset).mpr nd.1), ih nd.2] } end end list
476518f3fa3c6bd417b2d2da89c0a8d227298fc0
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/test/linarith.lean
087080f337bcbd9aa2db043d509eee2e5ef5478c
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
3,877
lean
import tactic.linarith example (e b c a v0 v1 : ℚ) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 + ε / 3 + ε / 7 < ε := by linarith example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0) (h3 : 12*y - z < 0) : false := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 < ε := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 3 + ε / 3 + ε / 3 = ε := by linarith example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith {discharger := `[ring SOP]} example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith example (a b c : ℚ) (x y : ℤ) (h1 : x ≤ 3*y) (h2 : b + 2 > 3 + b) : false := by linarith {restrict_type := ℚ} example (g v V c h : ℚ) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≤ c) (h6 : c < 1) : v ≤ V := by linarith example (x y z : ℚ) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℚ) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z)) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) : ¬ 12*y - 4* z < 0 := by linarith example (w x y z : ℤ) (h1 : 4*x + (-3)*y + 6*w ≤ 0) (h2 : (-1)*x < 0) (h3 : y < 0) (h4 : w ≥ 0) (h5 : nat.prime x.nat_abs) : false := by linarith example (a b c : ℚ) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : ¬ b ≥ 0) : false := by linarith example (a b c : ℚ) (h2 : (2 : ℚ) > 3) : a + b - c ≥ 3 := by linarith {exfalso := ff} example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false := by linarith using [rat.num_pos_iff_pos.mpr hx] example (x y z : ℚ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℕ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℚ) (hx : ¬ x > 3*y) (h2 : ¬ y > 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (h1 : (1 : ℕ) < 1) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 := by linarith example (a b c : ℕ) : a + b ≥ a := by linarith example (a b c : ℕ) : ¬ a + b < a := by linarith example (x y : ℚ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x ≥ 0) (h'' : (6 + 3 * y) * y ≥ 0) : false := by linarith example (x y : ℕ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) : false := by linarith example (a b i : ℕ) (h1 : ¬ a < i) (h2 : b < i) (h3 : a ≤ b) : false := by linarith example (n : ℕ) (h1 : n ≤ 3) (h2 : n > 2) : n = 3 := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) (h2 : ¬ z + 1 ≤ 2) : false := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) : z + 1 ≤ 2 := by linarith example (a b c : ℚ) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : ℕ) (n : ℕ) (Hirrelevant : n > N) (A : ℚ) (l : ℚ) (h : A - l ≤ -(A - l)) (h_1 : ¬A ≤ -A) (h_2 : ¬l ≤ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) : d ≤ ((q : ℚ) - 1)*n := by linarith example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) : ((q : ℚ) - 1)*n - d = 1/3 * (((q : ℚ) - 1)*n) := by linarith example (a : ℚ) (ha : 0 ≤ a): 0 * 0 ≤ 2 * a := by linarith
fba3f0986037aa74fc4454c409f79a31e3114fdf
3d2a7f1582fe5bae4d0bdc2fe86e997521239a65
/spatial-reasoning/spatial-reasoning-problem-03.lean
c51c3abef06a4c7b7f11804a1fc800ac8f4ec592
[]
no_license
own-pt/common-sense-lean
e4fa643ae010459de3d1bf673be7cbc7062563c9
f672210aecb4172f5bae265e43e6867397e13b1c
refs/heads/master
1,622,065,660,261
1,589,487,533,000
1,589,487,533,000
254,167,782
3
2
null
1,589,487,535,000
1,586,370,214,000
Lean
UTF-8
Lean
false
false
1,483
lean
/- Spatial Reasoning Problem 03 -/ /- It can be found at: SpatialQs.txt -/ /- (3) John is in his house. John's house is in San Jose. Is John in San Jose? -/ constant U : Type constants Relation BinaryPredicate Entity located Physical TransitiveRelation : U constant subclass : U → U → Prop constant domain: U → ℕ → U → Prop constant ins : U → U → Prop constants City SanJose JohnsHouse John Inside : U constant orientation : U → U → U → Prop constant holds : U → U → U → Prop axiom a1 : subclass City Physical -- (subclass City Physical) axiom a2 : ins JohnsHouse Physical -- (instance JohnsHouse Physical) axiom a3 : ins John Physical -- (instance John Physical) axiom a4 : ins SanJose City -- (instance SanJose City) axiom a5 : orientation John JohnsHouse Inside -- (orientation John JohnsHouse Inside) axiom a6 : ins located TransitiveRelation -- (instance located TransitiveRelation) axiom a7 : holds located JohnsHouse SanJose -- (located JohnsHouse SanJose) /- axioms from SUMO -/ axiom a8 : ∀ (R A B C), (ins R TransitiveRelation) ∧ (holds R A B) ∧ (holds R B C) → holds R A C /- axioms added -/ axiom a9 : ∀ OBJ1 OBJ2, (orientation OBJ1 OBJ2 Inside) → (holds located OBJ1 OBJ2) /- Demonstration Start -/ theorem john_is_in_san_jose : holds located John SanJose := begin have h1, from (a9 _ _) a5, exact (a8 _ _ _ _) ⟨a6,⟨h1,a7⟩⟩ end
7628b5d1a03eec19756a007f48ff9463adfd4b91
ba306bac106b8f27befb2a800f4357237f86c760
/lean/love12_basic_mathematical_structures_exercise_sheet.lean
2dee39b7318a7df7171f1b528534b8aca131ae62
[]
no_license
qyqnl/logical_verification_2020
406aa4cc47797501275ea07de5d6f59f01e977d0
17dff35b56336acb671ddfb36871f98475bc0b96
refs/heads/master
1,672,501,336,112
1,603,724,260,000
1,603,724,260,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,684
lean
import .love12_basic_mathematical_structures_demo /- # LoVe Exercise 12: Basic Mathematical Structures -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Question 1: Type Classes Recall the datatype `btree` we introduced earlier: -/ #check btree /- The following function takes two trees and attaches copies of the second tree to each leaf of the first tree. -/ def btree.graft {α : Type} : btree α → btree α → btree α | btree.empty u := u | (btree.node a l r) u := btree.node a (btree.graft l u) (btree.graft r u) #reduce btree.graft (btree.node 1 btree.empty btree.empty) (btree.node 2 btree.empty btree.empty) /- 1.1. Prove the following two lemmas by structural induction on `t`. -/ lemma btree.graft_assoc {α : Type} (t u v : btree α) : btree.graft (btree.graft t u) v = btree.graft t (btree.graft u v) := sorry lemma btree.graft_empty {α : Type} (t : btree α) : btree.graft t btree.empty = t := sorry /- 1.2. Declare btree an instance of `add_monoid` using `graft` as addition operator. -/ #print add_monoid @[instance] def btree.add_monid {α : Type} : add_monoid (btree α) := sorry /- 1.3. Explain why `btree` with `graft` as addition cannot be declared an instance of `add_group`. -/ #print add_group /- 1.4 (**optional**). Prove the following lemma illustrating why `btree` with `graft` as addition does not constitute an `add_group`. -/ lemma btree.add_left_neg_counterexample : ∃x : btree ℕ, ∀ y : btree ℕ, btree.graft y x ≠ btree.empty := sorry /- ## Question 2: Multisets and Finsets Recall the following definitions from the lecture: -/ #check multiset.elems #check finset.elems #check list.elems /- 2.1. Prove that the multiset of nodes does not change when mirroring a tree. Hints: * Perform structural induction on `t`. * The `cc` tactic also works with set operations. -/ lemma multiset.elems_mirror (t : btree ℕ) : multiset.elems (mirror t) = multiset.elems t := sorry /- 2.2. Prove that the finite set of nodes does not change when mirroring a tree. -/ lemma finset.elems_mirror (t : btree ℕ) : finset.elems (mirror t) = finset.elems t := sorry /- 2.3. Show that this does not hold for the list of nodes by providing a tree `t` for which `nodes_list t ≠ nodes_list (mirror t)`. If you define a suitable counterexample, the proof below will succeed. -/ def rotten_tree : btree ℕ := sorry #eval list.elems rotten_tree #eval list.elems (mirror rotten_tree) lemma list.elems_mirror_counterexample : ∃t : btree ℕ, list.elems t ≠ list.elems (mirror t) := begin apply exists.intro rotten_tree, exact dec_trivial end end LoVe
baa6940b99ad28400eb88ef277b62e265c6cac36
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/calc_bug.lean
adf110aa2d9d473905a78c900ddb75615b0b803b
[ "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
60
lean
import logic definition foo (a : Type) := calc a = a : rfl
8b7c18b3691a7c38b6f3405ef80d8a4cc922b27b
26ac254ecb57ffcb886ff709cf018390161a9225
/src/data/real/nnreal.lean
3604f4997daebd25f80c7225789c37b1e3458d86
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
25,745
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import algebra.linear_ordered_comm_group_with_zero import data.finset.lattice import data.real.basic import data.indicator_function noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext_iff_val.symm @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { exists_pair_ne := ⟨0, 1, assume h, zero_ne_one $ nnreal.eq_iff.2 h⟩, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n •ℕ r) = n •ℕ (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective @[norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : nnreal), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := mul_ne_zero h₁ h₂ end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : nnreal} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [nnreal.div_def, finset.sum_mul] @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝ≥0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ protected lemma inv_pow {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝ≥0} (h : r ≠ 0) : r / r = 1 := mul_inv_cancel h lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma mul_div_cancel {r p : ℝ≥0} (h : p ≠ 0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝ≥0} (h : r ≠ 0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel _ h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq (inv_inv' _) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ zero_lt_iff_ne_zero.2 hr lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] theorem div_pow {a b : ℝ≥0} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝ≥0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma inv_eq_one_div (a : ℝ≥0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝ≥0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma one_div_eq_inv (a : ℝ≥0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝ≥0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝ≥0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝ≥0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝ≥0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝ≥0} (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝ≥0} (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb end inv section pow theorem pow_eq_zero {a : ℝ≥0} {n : ℕ} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝ≥0} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h end pow end nnreal
a46a99a776ed6475c33c301d79d9da494167b1f8
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/tests/lean/run/rewrite4.lean
01dad3705a6d4f7fb882735ff4ee51131fe8b17f
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
462
lean
import data.nat open algebra constant f {A : Type} : A → A → A theorem test1 {A : Type} [s : comm_ring A] (a b c : A) (H : a + 0 = 0) : f a a = f 0 0 := by rewrite ⟨add_zero at H, H⟩ theorem test2 {A : Type} [s : comm_ring A] (a b c : A) (H : a + 0 = 0) : f a a = f 0 0 := by rewrite ⟨add_zero at *, H⟩ theorem test3 {A : Type} [s : comm_ring A] (a b c : A) (H : a + 0 = 0 + 0) : f a a = f 0 0 := by rewrite ⟨add_zero at H, zero_add at H, H⟩
2e881dd97ea665da24daeab232733f4e08afd1c0
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/t6.lean
0b1666df00bbbfbf735f4a4bcb691b4260f947b9
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
340
lean
prelude definition Prop : Type := Sort 0 section variable {A : Type*} -- Mark A as implicit parameter variable R : A → A → Prop definition id (a : A) : A := a definition refl : Prop := forall (a : A), R a a definition symm : Prop := forall (a b : A), R a b -> R b a end #check id.{2} #check refl.{1} #check symm.{1}
6b0933a6c2855c61f8670f797b19731b39d758d1
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Data/Json/Parser.lean
5b69170092761b8681c7ab5ec574f49aebb30ca1
[ "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
5,429
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ import Lean.Data.Json.Basic import Lean.Data.Parsec namespace Lean.Json.Parser open Lean.Parsec @[inline] def hexChar : Parsec Nat := do let c ← anyChar if '0' ≤ c ∧ c ≤ '9' then pure $ c.val.toNat - '0'.val.toNat else if 'a' ≤ c ∧ c ≤ 'f' then pure $ c.val.toNat - 'a'.val.toNat else if 'A' ≤ c ∧ c ≤ 'F' then pure $ c.val.toNat - 'A'.val.toNat else fail "invalid hex character" def escapedChar : Parsec Char := do let c ← anyChar match c with | '\\' => return '\\' | '"' => return '"' | '/' => return '/' | 'b' => return '\x08' | 'f' => return '\x0c' | 'n' => return '\n' | 'r' => return '\x0d' | 't' => return '\t' | 'u' => let u1 ← hexChar; let u2 ← hexChar; let u3 ← hexChar; let u4 ← hexChar return Char.ofNat $ 4096*u1 + 256*u2 + 16*u3 + u4 | _ => fail "illegal \\u escape" partial def strCore (acc : String) : Parsec String := do let c ← peek! if c = '"' then -- " skip return acc else let c ← anyChar if c = '\\' then strCore (acc.push (← escapedChar)) -- as to whether c.val > 0xffff should be split up and encoded with multiple \u, -- the JSON standard is not definite: both directly printing the character -- and encoding it with multiple \u is allowed. we choose the former. else if 0x0020 ≤ c.val ∧ c.val ≤ 0x10ffff then strCore (acc.push c) else fail "unexpected character in string" def str : Parsec String := strCore "" partial def natCore (acc digits : Nat) : Parsec (Nat × Nat) := do let some c ← peek? | return (acc, digits) if '0' ≤ c ∧ c ≤ '9' then skip let acc' := 10*acc + (c.val.toNat - '0'.val.toNat) natCore acc' (digits+1) else return (acc, digits) @[inline] def lookahead (p : Char → Prop) (desc : String) [DecidablePred p] : Parsec Unit := do let c ← peek! if p c then return () else fail <| "expected " ++ desc @[inline] def natNonZero : Parsec Nat := do lookahead (fun c => '1' ≤ c ∧ c ≤ '9') "1-9" let (n, _) ← natCore 0 0 return n @[inline] def natNumDigits : Parsec (Nat × Nat) := do lookahead (fun c => '0' ≤ c ∧ c ≤ '9') "digit" natCore 0 0 @[inline] def natMaybeZero : Parsec Nat := do let (n, _) ← natNumDigits return n def num : Parsec JsonNumber := do let c ← peek! let sign ← if c = '-' then skip pure (-1 : Int) else pure 1 let c ← peek! let res ← if c = '0' then skip pure 0 else natNonZero let c? ← peek? let res : JsonNumber ← if c? = some '.' then skip let (n, d) ← natNumDigits if d > USize.size then fail "too many decimals" let mantissa' := sign * (res * (10^d : Nat) + n) let exponent' := d pure <| JsonNumber.mk mantissa' exponent' else pure <| JsonNumber.fromInt (sign * res) let c? ← peek? if c? = some 'e' ∨ c? = some 'E' then skip let c ← peek! if c = '-' then skip let n ← natMaybeZero return res.shiftr n else if c = '+' then skip let n ← natMaybeZero if n > USize.size then fail "exp too large" return res.shiftl n else return res partial def arrayCore (anyCore : Parsec Json) (acc : Array Json) : Parsec (Array Json) := do let hd ← anyCore let acc' := acc.push hd let c ← anyChar if c = ']' then ws return acc' else if c = ',' then ws arrayCore anyCore acc' else fail "unexpected character in array" partial def objectCore (anyCore : Parsec Json) : Parsec (RBNode String (fun _ => Json)) := do lookahead (fun c => c = '"') "\""; skip; -- " let k ← strCore ""; ws lookahead (fun c => c = ':') ":"; skip; ws let v ← anyCore let c ← anyChar if c = '}' then ws return RBNode.singleton k v else if c = ',' then ws let kvs ← objectCore anyCore return kvs.insert compare k v else fail "unexpected character in object" partial def anyCore : Parsec Json := do let c ← peek! if c = '[' then skip; ws let c ← peek! if c = ']' then skip; ws return Json.arr (Array.mkEmpty 0) else let a ← arrayCore anyCore (Array.mkEmpty 4) return Json.arr a else if c = '{' then skip; ws let c ← peek! if c = '}' then skip; ws return Json.obj (RBNode.leaf) else let kvs ← objectCore anyCore return Json.obj kvs else if c = '\"' then skip let s ← strCore "" ws return Json.str s else if c = 'f' then skipString "false"; ws return Json.bool false else if c = 't' then skipString "true"; ws return Json.bool true else if c = 'n' then skipString "null"; ws return Json.null else if c = '-' ∨ ('0' ≤ c ∧ c ≤ '9') then let n ← num ws return Json.num n else fail "unexpected input" def any : Parsec Json := do ws let res ← anyCore eof return res end Json.Parser namespace Json def parse (s : String) : Except String Lean.Json := match Json.Parser.any s.mkIterator with | Parsec.ParseResult.success _ res => Except.ok res | Parsec.ParseResult.error it err => Except.error s!"offset {repr it.i.byteIdx}: {err}" end Json end Lean
4508fcc28947380ffc796900bcd1d0441585e140
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/src/Lean/Elab/StructInst.lean
4d4280021aca8372cc952efc926d7d39cb41f8bb
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
34,712
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.FindExpr import Lean.Parser.Term import Lean.Elab.App import Lean.Elab.Binders namespace Lean.Elab.Term.StructInst open Std (HashMap) open Meta /- Structure instances are of the form: "{" >> optional (atomic (termParser >> " with ")) >> manyIndent (group ((structInstFieldAbbrev <|> structInstField) >> optional ", ")) >> optEllipsis >> optional (" : " >> termParser) >> " }" -/ @[builtinMacro Lean.Parser.Term.structInst] def expandStructInstExpectedType : Macro := fun stx => let expectedArg := stx[4] if expectedArg.isNone then Macro.throwUnsupported else let expected := expectedArg[1] let stxNew := stx.setArg 4 mkNullNode `(($stxNew : $expected)) /- If `stx` is of the form `{ s with ... }` and `s` is not a local variable, expand into `let src := s; { src with ... }`. Note that this one is not a `Macro` because we need to access the local context. -/ private def expandNonAtomicExplicitSource (stx : Syntax) : TermElabM (Option Syntax) := withFreshMacroScope do let sourceOpt := stx[1] if sourceOpt.isNone then pure none else let source := sourceOpt[0] match (← isLocalIdent? source) with | some _ => pure none | none => if source.isMissing then throwAbortTerm else let src ← `(src) let sourceOpt := sourceOpt.setArg 0 src let stxNew := stx.setArg 1 sourceOpt `(let src := $source; $stxNew) inductive Source where | none -- structure instance source has not been provieded | implicit (stx : Syntax) -- `..` | explicit (stx : Syntax) (src : Expr) -- `src with` deriving Inhabited def Source.isNone : Source → Bool | Source.none => true | _ => false def setStructSourceSyntax (structStx : Syntax) : Source → Syntax | Source.none => (structStx.setArg 1 mkNullNode).setArg 3 mkNullNode | Source.implicit stx => (structStx.setArg 1 mkNullNode).setArg 3 stx | Source.explicit stx _ => (structStx.setArg 1 stx).setArg 3 mkNullNode private def getStructSource (stx : Syntax) : TermElabM Source := withRef stx do let explicitSource := stx[1] let implicitSource := stx[3] if explicitSource.isNone && implicitSource[0].isNone then return Source.none else if explicitSource.isNone then return Source.implicit implicitSource else if implicitSource[0].isNone then let fvar? ← isLocalIdent? explicitSource[0] match fvar? with | none => unreachable! -- expandNonAtomicExplicitSource must have been used when we get here | some src => return Source.explicit explicitSource src else throwError "invalid structure instance `with` and `..` cannot be used together" /- We say a `{ ... }` notation is a `modifyOp` if it contains only one ``` def structInstArrayRef := leading_parser "[" >> termParser >>"]" ``` -/ private def isModifyOp? (stx : Syntax) : TermElabM (Option Syntax) := do let s? ← stx[2].getArgs.foldlM (init := none) fun s? p => /- p is of the form `(group ((structInstFieldAbbrev <|> structInstField) >> optional ", "))` -/ let arg := p[0] if arg.getKind == ``Lean.Parser.Term.structInstField then /- Remark: the syntax for `structInstField` is ``` def structInstLVal := leading_parser (ident <|> numLit <|> structInstArrayRef) >> many (group ("." >> (ident <|> numLit)) <|> structInstArrayRef) def structInstField := leading_parser structInstLVal >> " := " >> termParser ``` -/ let lval := arg[0] let k := lval[0].getKind if k == ``Lean.Parser.Term.structInstArrayRef then match s? with | none => pure (some arg) | some s => if s.getKind == ``Lean.Parser.Term.structInstArrayRef then throwErrorAt arg "invalid \{...} notation, at most one `[..]` at a given level" else throwErrorAt arg "invalid \{...} notation, can't mix field and `[..]` at a given level" else match s? with | none => pure (some arg) | some s => if s.getKind == ``Lean.Parser.Term.structInstArrayRef then throwErrorAt arg "invalid \{...} notation, can't mix field and `[..]` at a given level" else pure s? else pure s? match s? with | none => pure none | some s => if s[0][0].getKind == ``Lean.Parser.Term.structInstArrayRef then pure s? else pure none private def elabModifyOp (stx modifyOp source : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let cont (val : Syntax) : TermElabM Expr := do let lval := modifyOp[0][0] let idx := lval[1] let self := source[0] let stxNew ← `($(self).modifyOp (idx := $idx) (fun s => $val)) trace[Elab.struct.modifyOp] "{stx}\n===>\n{stxNew}" withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? trace[Elab.struct.modifyOp] "{modifyOp}\nSource: {source}" let rest := modifyOp[0][1] if rest.isNone then cont modifyOp[2] else let s ← `(s) let valFirst := rest[0] let valFirst := if valFirst.getKind == ``Lean.Parser.Term.structInstArrayRef then valFirst else valFirst[1] let restArgs := rest.getArgs let valRest := mkNullNode restArgs[1:restArgs.size] let valField := modifyOp.setArg 0 <| Syntax.node ``Parser.Term.structInstLVal #[valFirst, valRest] let valSource := source.modifyArg 0 fun _ => s let val := stx.setArg 1 valSource let val := val.setArg 2 <| mkNullNode #[mkNullNode #[valField, mkNullNode]] trace[Elab.struct.modifyOp] "{stx}\nval: {val}" cont val /- Get structure name and elaborate explicit source (if available) -/ private def getStructName (stx : Syntax) (expectedType? : Option Expr) (sourceView : Source) : TermElabM (Name × Expr) := do tryPostponeIfNoneOrMVar expectedType? let useSource : Unit → TermElabM (Name × Expr) := fun _ => match sourceView, expectedType? with | Source.explicit _ src, _ => do let srcType ← inferType src let srcType ← whnf srcType tryPostponeIfMVar srcType match srcType.getAppFn with | Expr.const constName _ _ => return (constName, srcType) | _ => throwUnexpectedExpectedType srcType "source" | _, some expectedType => throwUnexpectedExpectedType expectedType | _, none => throwUnknownExpectedType match expectedType? with | none => useSource () | some expectedType => let expectedType ← whnf expectedType match expectedType.getAppFn with | Expr.const constName _ _ => return (constName, expectedType) | _ => useSource () where throwUnknownExpectedType := throwError "invalid \{...} notation, expected type is not known" throwUnexpectedExpectedType type (kind := "expected") := do let type ← instantiateMVars type if type.getAppFn.isMVar then throwUnknownExpectedType else throwError "invalid \{...} notation, {kind} type is not of the form (C ...){indentExpr type}" inductive FieldLHS where | fieldName (ref : Syntax) (name : Name) | fieldIndex (ref : Syntax) (idx : Nat) | modifyOp (ref : Syntax) (index : Syntax) deriving Inhabited instance : ToFormat FieldLHS := ⟨fun lhs => match lhs with | FieldLHS.fieldName _ n => fmt n | FieldLHS.fieldIndex _ i => fmt i | FieldLHS.modifyOp _ i => "[" ++ i.prettyPrint ++ "]"⟩ inductive FieldVal (σ : Type) where | term (stx : Syntax) : FieldVal σ | nested (s : σ) : FieldVal σ | default : FieldVal σ -- mark that field must be synthesized using default value deriving Inhabited structure Field (σ : Type) where ref : Syntax lhs : List FieldLHS val : FieldVal σ expr? : Option Expr := none deriving Inhabited def Field.isSimple {σ} : Field σ → Bool | { lhs := [_], .. } => true | _ => false inductive Struct where | mk (ref : Syntax) (structName : Name) (fields : List (Field Struct)) (source : Source) deriving Inhabited abbrev Fields := List (Field Struct) /- true if all fields of the given structure are marked as `default` -/ partial def Struct.allDefault : Struct → Bool | ⟨_, _, fields, _⟩ => fields.all fun ⟨_, _, val, _⟩ => match val with | FieldVal.term _ => false | FieldVal.default => true | FieldVal.nested s => allDefault s def Struct.ref : Struct → Syntax | ⟨ref, _, _, _⟩ => ref def Struct.structName : Struct → Name | ⟨_, structName, _, _⟩ => structName def Struct.fields : Struct → Fields | ⟨_, _, fields, _⟩ => fields def Struct.source : Struct → Source | ⟨_, _, _, s⟩ => s def formatField (formatStruct : Struct → Format) (field : Field Struct) : Format := Format.joinSep field.lhs " . " ++ " := " ++ match field.val with | FieldVal.term v => v.prettyPrint | FieldVal.nested s => formatStruct s | FieldVal.default => "<default>" partial def formatStruct : Struct → Format | ⟨_, structName, fields, source⟩ => let fieldsFmt := Format.joinSep (fields.map (formatField formatStruct)) ", " match source with | Source.none => "{" ++ fieldsFmt ++ "}" | Source.implicit _ => "{" ++ fieldsFmt ++ " .. }" | Source.explicit _ src => "{" ++ format src ++ " with " ++ fieldsFmt ++ "}" instance : ToFormat Struct := ⟨formatStruct⟩ instance : ToString Struct := ⟨toString ∘ format⟩ instance : ToFormat (Field Struct) := ⟨formatField formatStruct⟩ instance : ToString (Field Struct) := ⟨toString ∘ format⟩ /- Recall that `structInstField` elements have the form ``` def structInstField := leading_parser structInstLVal >> " := " >> termParser def structInstLVal := leading_parser (ident <|> numLit <|> structInstArrayRef) >> many (("." >> (ident <|> numLit)) <|> structInstArrayRef) def structInstArrayRef := leading_parser "[" >> termParser >>"]" ``` -/ -- Remark: this code relies on the fact that `expandStruct` only transforms `fieldLHS.fieldName` def FieldLHS.toSyntax (first : Bool) : FieldLHS → Syntax | FieldLHS.modifyOp stx _ => stx | FieldLHS.fieldName stx name => if first then mkIdentFrom stx name else mkGroupNode #[mkAtomFrom stx ".", mkIdentFrom stx name] | FieldLHS.fieldIndex stx _ => if first then stx else mkGroupNode #[mkAtomFrom stx ".", stx] def FieldVal.toSyntax : FieldVal Struct → Syntax | FieldVal.term stx => stx | _ => unreachable! def Field.toSyntax : Field Struct → Syntax | field => let stx := field.ref let stx := stx.setArg 2 field.val.toSyntax match field.lhs with | first::rest => stx.setArg 0 <| mkNullNode #[first.toSyntax true, mkNullNode <| rest.toArray.map (FieldLHS.toSyntax false) ] | _ => unreachable! private def toFieldLHS (stx : Syntax) : MacroM FieldLHS := if stx.getKind == ``Lean.Parser.Term.structInstArrayRef then return FieldLHS.modifyOp stx stx[1] else -- Note that the representation of the first field is different. let stx := if stx.getKind == groupKind then stx[1] else stx if stx.isIdent then return FieldLHS.fieldName stx stx.getId.eraseMacroScopes else match stx.isFieldIdx? with | some idx => return FieldLHS.fieldIndex stx idx | none => Macro.throwError "unexpected structure syntax" private def mkStructView (stx : Syntax) (structName : Name) (source : Source) : MacroM Struct := do /- Recall that `stx` is of the form ``` leading_parser "{" >> optional (atomic (termParser >> " with ")) >> manyIndent (group ((structInstFieldAbbrev <|> structInstField) >> optional ", ")) >> optional ".." >> optional (" : " >> termParser) >> " }" ``` -/ let fieldsStx ← stx[2].getArgs.mapM fun stx => let stx := stx[0] if stx.getKind == ``Lean.Parser.Term.structInstField then return stx else let id := stx[0] `(Lean.Parser.Term.structInstField| $id:ident := $id:ident) let fields ← fieldsStx.toList.mapM fun fieldStx => do let val := fieldStx[2] let first ← toFieldLHS fieldStx[0][0] let rest ← fieldStx[0][1].getArgs.toList.mapM toFieldLHS pure { ref := fieldStx, lhs := first :: rest, val := FieldVal.term val : Field Struct } pure ⟨stx, structName, fields, source⟩ def Struct.modifyFieldsM {m : Type → Type} [Monad m] (s : Struct) (f : Fields → m Fields) : m Struct := match s with | ⟨ref, structName, fields, source⟩ => return ⟨ref, structName, (← f fields), source⟩ @[inline] def Struct.modifyFields (s : Struct) (f : Fields → Fields) : Struct := Id.run <| s.modifyFieldsM f def Struct.setFields (s : Struct) (fields : Fields) : Struct := s.modifyFields fun _ => fields private def expandCompositeFields (s : Struct) : Struct := s.modifyFields fun fields => fields.map fun field => match field with | { lhs := FieldLHS.fieldName ref (Name.str Name.anonymous _ _) :: rest, .. } => field | { lhs := FieldLHS.fieldName ref n@(Name.str _ _ _) :: rest, .. } => let newEntries := n.components.map <| FieldLHS.fieldName ref { field with lhs := newEntries ++ rest } | _ => field private def expandNumLitFields (s : Struct) : TermElabM Struct := s.modifyFieldsM fun fields => do let env ← getEnv let fieldNames := getStructureFields env s.structName fields.mapM fun field => match field with | { lhs := FieldLHS.fieldIndex ref idx :: rest, .. } => if idx == 0 then throwErrorAt ref "invalid field index, index must be greater than 0" else if idx > fieldNames.size then throwErrorAt ref "invalid field index, structure has only #{fieldNames.size} fields" else pure { field with lhs := FieldLHS.fieldName ref fieldNames[idx - 1] :: rest } | _ => pure field /- For example, consider the following structures: ``` structure A where x : Nat structure B extends A where y : Nat structure C extends B where z : Bool ``` This method expands parent structure fields using the path to the parent structure. For example, ``` { x := 0, y := 0, z := true : C } ``` is expanded into ``` { toB.toA.x := 0, toB.y := 0, z := true : C } ``` -/ private def expandParentFields (s : Struct) : TermElabM Struct := do let env ← getEnv s.modifyFieldsM fun fields => fields.mapM fun field => match field with | { lhs := FieldLHS.fieldName ref fieldName :: rest, .. } => match findField? env s.structName fieldName with | none => throwErrorAt ref "'{fieldName}' is not a field of structure '{s.structName}'" | some baseStructName => if baseStructName == s.structName then pure field else match getPathToBaseStructure? env baseStructName s.structName with | some path => do let path := path.map fun funName => match funName with | Name.str _ s _ => FieldLHS.fieldName ref (Name.mkSimple s) | _ => unreachable! pure { field with lhs := path ++ field.lhs } | _ => throwErrorAt ref "failed to access field '{fieldName}' in parent structure" | _ => pure field private abbrev FieldMap := HashMap Name Fields private def mkFieldMap (fields : Fields) : TermElabM FieldMap := fields.foldlM (init := {}) fun fieldMap field => match field.lhs with | FieldLHS.fieldName _ fieldName :: rest => match fieldMap.find? fieldName with | some (prevField::restFields) => if field.isSimple || prevField.isSimple then throwErrorAt field.ref "field '{fieldName}' has already beed specified" else return fieldMap.insert fieldName (field::prevField::restFields) | _ => return fieldMap.insert fieldName [field] | _ => unreachable! private def isSimpleField? : Fields → Option (Field Struct) | [field] => if field.isSimple then some field else none | _ => none private def getFieldIdx (structName : Name) (fieldNames : Array Name) (fieldName : Name) : TermElabM Nat := do match fieldNames.findIdx? fun n => n == fieldName with | some idx => pure idx | none => throwError "field '{fieldName}' is not a valid field of '{structName}'" private def mkProjStx (s : Syntax) (fieldName : Name) : Syntax := Syntax.node ``Lean.Parser.Term.proj #[s, mkAtomFrom s ".", mkIdentFrom s fieldName] private def mkSubstructSource (structName : Name) (fieldNames : Array Name) (fieldName : Name) (src : Source) : TermElabM Source := match src with | Source.explicit stx src => do let idx ← getFieldIdx structName fieldNames fieldName let stx := stx.modifyArg 0 fun stx => mkProjStx stx fieldName return Source.explicit stx (mkProj structName idx src) | s => return s @[specialize] private def groupFields (expandStruct : Struct → TermElabM Struct) (s : Struct) : TermElabM Struct := do let env ← getEnv let fieldNames := getStructureFields env s.structName withRef s.ref do s.modifyFieldsM fun fields => do let fieldMap ← mkFieldMap fields fieldMap.toList.mapM fun ⟨fieldName, fields⟩ => do match isSimpleField? fields with | some field => pure field | none => let substructFields := fields.map fun field => { field with lhs := field.lhs.tail! } let substructSource ← mkSubstructSource s.structName fieldNames fieldName s.source let field := fields.head! match Lean.isSubobjectField? env s.structName fieldName with | some substructName => let substruct := Struct.mk s.ref substructName substructFields substructSource let substruct ← expandStruct substruct pure { field with lhs := [field.lhs.head!], val := FieldVal.nested substruct } | none => do -- It is not a substructure field. Thus, we wrap fields using `Syntax`, and use `elabTerm` to process them. let valStx := s.ref -- construct substructure syntax using s.ref as template let valStx := valStx.setArg 4 mkNullNode -- erase optional expected type let args := substructFields.toArray.map fun field => mkNullNode #[field.toSyntax, mkNullNode] let valStx := valStx.setArg 2 (mkNullNode args) let valStx := setStructSourceSyntax valStx substructSource pure { field with lhs := [field.lhs.head!], val := FieldVal.term valStx } def findField? (fields : Fields) (fieldName : Name) : Option (Field Struct) := fields.find? fun field => match field.lhs with | [FieldLHS.fieldName _ n] => n == fieldName | _ => false @[specialize] private def addMissingFields (expandStruct : Struct → TermElabM Struct) (s : Struct) : TermElabM Struct := do let env ← getEnv let fieldNames := getStructureFields env s.structName let ref := s.ref withRef ref do let fields ← fieldNames.foldlM (init := []) fun fields fieldName => do match findField? s.fields fieldName with | some field => return field::fields | none => let addField (val : FieldVal Struct) : TermElabM Fields := do return { ref := s.ref, lhs := [FieldLHS.fieldName s.ref fieldName], val := val } :: fields match Lean.isSubobjectField? env s.structName fieldName with | some substructName => do let substructSource ← mkSubstructSource s.structName fieldNames fieldName s.source let substruct := Struct.mk s.ref substructName [] substructSource let substruct ← expandStruct substruct addField (FieldVal.nested substruct) | none => match s.source with | Source.none => addField FieldVal.default | Source.implicit _ => addField (FieldVal.term (mkHole s.ref)) | Source.explicit stx _ => -- stx is of the form `optional (try (termParser >> "with"))` let src := stx[0] let val := mkProjStx src fieldName addField (FieldVal.term val) return s.setFields fields.reverse private partial def expandStruct (s : Struct) : TermElabM Struct := do let s := expandCompositeFields s let s ← expandNumLitFields s let s ← expandParentFields s let s ← groupFields expandStruct s addMissingFields expandStruct s structure CtorHeaderResult where ctorFn : Expr ctorFnType : Expr instMVars : Array MVarId := #[] private def mkCtorHeaderAux : Nat → Expr → Expr → Array MVarId → TermElabM CtorHeaderResult | 0, type, ctorFn, instMVars => pure { ctorFn := ctorFn, ctorFnType := type, instMVars := instMVars } | n+1, type, ctorFn, instMVars => do let type ← whnfForall type match type with | Expr.forallE _ d b c => match c.binderInfo with | BinderInfo.instImplicit => let a ← mkFreshExprMVar d MetavarKind.synthetic mkCtorHeaderAux n (b.instantiate1 a) (mkApp ctorFn a) (instMVars.push a.mvarId!) | _ => let a ← mkFreshExprMVar d mkCtorHeaderAux n (b.instantiate1 a) (mkApp ctorFn a) instMVars | _ => throwError "unexpected constructor type" private partial def getForallBody : Nat → Expr → Option Expr | i+1, Expr.forallE _ _ b _ => getForallBody i b | i+1, _ => none | 0, type => type private def propagateExpectedType (type : Expr) (numFields : Nat) (expectedType? : Option Expr) : TermElabM Unit := match expectedType? with | none => pure () | some expectedType => do match getForallBody numFields type with | none => pure () | some typeBody => unless typeBody.hasLooseBVars do discard <| isDefEq expectedType typeBody private def mkCtorHeader (ctorVal : ConstructorVal) (expectedType? : Option Expr) : TermElabM CtorHeaderResult := do let us ← mkFreshLevelMVars ctorVal.levelParams.length let val := Lean.mkConst ctorVal.name us let type := (ConstantInfo.ctorInfo ctorVal).instantiateTypeLevelParams us let r ← mkCtorHeaderAux ctorVal.numParams type val #[] propagateExpectedType r.ctorFnType ctorVal.numFields expectedType? synthesizeAppInstMVars r.instMVars pure r def markDefaultMissing (e : Expr) : Expr := mkAnnotation `structInstDefault e def defaultMissing? (e : Expr) : Option Expr := annotation? `structInstDefault e def throwFailedToElabField {α} (fieldName : Name) (structName : Name) (msgData : MessageData) : TermElabM α := throwError "failed to elaborate field '{fieldName}' of '{structName}, {msgData}" def trySynthStructInstance? (s : Struct) (expectedType : Expr) : TermElabM (Option Expr) := do if !s.allDefault then pure none else try synthInstance? expectedType catch _ => pure none private partial def elabStruct (s : Struct) (expectedType? : Option Expr) : TermElabM (Expr × Struct) := withRef s.ref do let env ← getEnv let ctorVal := getStructureCtor env s.structName let { ctorFn := ctorFn, ctorFnType := ctorFnType, .. } ← mkCtorHeader ctorVal expectedType? let (e, _, fields) ← s.fields.foldlM (init := (ctorFn, ctorFnType, [])) fun (e, type, fields) field => match field.lhs with | [FieldLHS.fieldName ref fieldName] => do let type ← whnfForall type match type with | Expr.forallE _ d b c => let cont (val : Expr) (field : Field Struct) : TermElabM (Expr × Expr × Fields) := do pushInfoTree <| InfoTree.node (children := {}) <| Info.ofFieldInfo { lctx := (← getLCtx), val := val, name := fieldName, stx := ref } let e := mkApp e val let type := b.instantiate1 val let field := { field with expr? := some val } pure (e, type, field::fields) match field.val with | FieldVal.term stx => cont (← elabTermEnsuringType stx d) field | FieldVal.nested s => do -- if all fields of `s` are marked as `default`, then try to synthesize instance match (← trySynthStructInstance? s d) with | some val => cont val { field with val := FieldVal.term (mkHole field.ref) } | none => do let (val, sNew) ← elabStruct s (some d); let val ← ensureHasType d val; cont val { field with val := FieldVal.nested sNew } | FieldVal.default => do let val ← withRef field.ref <| mkFreshExprMVar (some d); cont (markDefaultMissing val) field | _ => withRef field.ref <| throwFailedToElabField fieldName s.structName m!"unexpected constructor type{indentExpr type}" | _ => throwErrorAt field.ref "unexpected unexpanded structure field" pure (e, s.setFields fields.reverse) namespace DefaultFields structure Context where -- We must search for default values overriden in derived structures structs : Array Struct := #[] allStructNames : Array Name := #[] /-- Consider the following example: ``` structure A where x : Nat := 1 structure B extends A where y : Nat := x + 1 x := y + 1 structure C extends B where z : Nat := 2*y x := z + 3 ``` And we are trying to elaborate a structure instance for `C`. There are default values for `x` at `A`, `B`, and `C`. We say the default value at `C` has distance 0, the one at `B` distance 1, and the one at `A` distance 2. The field `maxDistance` specifies the maximum distance considered in a round of Default field computation. Remark: since `C` does not set a default value of `y`, the default value at `B` is at distance 0. The fixpoint for setting default values works in the following way. - Keep computing default values using `maxDistance == 0`. - We increase `maxDistance` whenever we failed to compute a new default value in a round. - If `maxDistance > 0`, then we interrupt a round as soon as we compute some default value. We use depth-first search. - We sign an error if no progress is made when `maxDistance` == structure hierarchy depth (2 in the example above). -/ maxDistance : Nat := 0 structure State where progress : Bool := false partial def collectStructNames (struct : Struct) (names : Array Name) : Array Name := let names := names.push struct.structName struct.fields.foldl (init := names) fun names field => match field.val with | FieldVal.nested struct => collectStructNames struct names | _ => names partial def getHierarchyDepth (struct : Struct) : Nat := struct.fields.foldl (init := 0) fun max field => match field.val with | FieldVal.nested struct => Nat.max max (getHierarchyDepth struct + 1) | _ => max partial def findDefaultMissing? (mctx : MetavarContext) (struct : Struct) : Option (Field Struct) := struct.fields.findSome? fun field => match field.val with | FieldVal.nested struct => findDefaultMissing? mctx struct | _ => match field.expr? with | none => unreachable! | some expr => match defaultMissing? expr with | some (Expr.mvar mvarId _) => if mctx.isExprAssigned mvarId then none else some field | _ => none def getFieldName (field : Field Struct) : Name := match field.lhs with | [FieldLHS.fieldName _ fieldName] => fieldName | _ => unreachable! abbrev M := ReaderT Context (StateRefT State TermElabM) def isRoundDone : M Bool := do return (← get).progress && (← read).maxDistance > 0 def getFieldValue? (struct : Struct) (fieldName : Name) : Option Expr := struct.fields.findSome? fun field => if getFieldName field == fieldName then field.expr? else none partial def mkDefaultValueAux? (struct : Struct) : Expr → TermElabM (Option Expr) | Expr.lam n d b c => withRef struct.ref do if c.binderInfo.isExplicit then let fieldName := n match getFieldValue? struct fieldName with | none => pure none | some val => let valType ← inferType val if (← isDefEq valType d) then mkDefaultValueAux? struct (b.instantiate1 val) else pure none else let arg ← mkFreshExprMVar d mkDefaultValueAux? struct (b.instantiate1 arg) | e => if e.isAppOfArity ``id 2 then pure (some e.appArg!) else pure (some e) def mkDefaultValue? (struct : Struct) (cinfo : ConstantInfo) : TermElabM (Option Expr) := withRef struct.ref do let us ← mkFreshLevelMVarsFor cinfo mkDefaultValueAux? struct (cinfo.instantiateValueLevelParams us) /-- If `e` is a projection function of one of the given structures, then reduce it -/ def reduceProjOf? (structNames : Array Name) (e : Expr) : MetaM (Option Expr) := do if !e.isApp then pure none else match e.getAppFn with | Expr.const name _ _ => do let env ← getEnv match env.getProjectionStructureName? name with | some structName => if structNames.contains structName then Meta.unfoldDefinition? e else pure none | none => pure none | _ => pure none /-- Reduce default value. It performs beta reduction and projections of the given structures. -/ partial def reduce (structNames : Array Name) : Expr → MetaM Expr | e@(Expr.lam _ _ _ _) => lambdaLetTelescope e fun xs b => do mkLambdaFVars xs (← reduce structNames b) | e@(Expr.forallE _ _ _ _) => forallTelescope e fun xs b => do mkForallFVars xs (← reduce structNames b) | e@(Expr.letE _ _ _ _ _) => lambdaLetTelescope e fun xs b => do mkLetFVars xs (← reduce structNames b) | e@(Expr.proj _ i b _) => do match (← Meta.project? b i) with | some r => reduce structNames r | none => return e.updateProj! (← reduce structNames b) | e@(Expr.app f _ _) => do match (← reduceProjOf? structNames e) with | some r => reduce structNames r | none => let f := f.getAppFn let f' ← reduce structNames f if f'.isLambda then let revArgs := e.getAppRevArgs reduce structNames (f'.betaRev revArgs) else let args ← e.getAppArgs.mapM (reduce structNames) return (mkAppN f' args) | e@(Expr.mdata _ b _) => do let b ← reduce structNames b if (defaultMissing? e).isSome && !b.isMVar then return b else return e.updateMData! b | e@(Expr.mvar mvarId _) => do match (← getExprMVarAssignment? mvarId) with | some val => if val.isMVar then reduce structNames val else pure val | none => return e | e => return e partial def tryToSynthesizeDefault (structs : Array Struct) (allStructNames : Array Name) (maxDistance : Nat) (fieldName : Name) (mvarId : MVarId) : TermElabM Bool := let rec loop (i : Nat) (dist : Nat) := do if dist > maxDistance then pure false else if h : i < structs.size then do let struct := structs.get ⟨i, h⟩ let defaultName := struct.structName ++ fieldName ++ `_default let env ← getEnv match env.find? defaultName with | some cinfo@(ConstantInfo.defnInfo defVal) => do let mctx ← getMCtx let val? ← mkDefaultValue? struct cinfo match val? with | none => do setMCtx mctx; loop (i+1) (dist+1) | some val => do let val ← reduce allStructNames val match val.find? fun e => (defaultMissing? e).isSome with | some _ => setMCtx mctx; loop (i+1) (dist+1) | none => let mvarDecl ← getMVarDecl mvarId let val ← ensureHasType mvarDecl.type val assignExprMVar mvarId val pure true | _ => loop (i+1) dist else pure false loop 0 0 partial def step (struct : Struct) : M Unit := unless (← isRoundDone) do withReader (fun ctx => { ctx with structs := ctx.structs.push struct }) do for field in struct.fields do match field.val with | FieldVal.nested struct => step struct | _ => match field.expr? with | none => unreachable! | some expr => match defaultMissing? expr with | some (Expr.mvar mvarId _) => unless (← isExprMVarAssigned mvarId) do let ctx ← read if (← withRef field.ref <| tryToSynthesizeDefault ctx.structs ctx.allStructNames ctx.maxDistance (getFieldName field) mvarId) then modify fun s => { s with progress := true } | _ => pure () partial def propagateLoop (hierarchyDepth : Nat) (d : Nat) (struct : Struct) : M Unit := do match findDefaultMissing? (← getMCtx) struct with | none => pure () -- Done | some field => if d > hierarchyDepth then throwErrorAt field.ref "field '{getFieldName field}' is missing" else withReader (fun ctx => { ctx with maxDistance := d }) do modify fun s => { s with progress := false } step struct if (← get).progress then do propagateLoop hierarchyDepth 0 struct else propagateLoop hierarchyDepth (d+1) struct def propagate (struct : Struct) : TermElabM Unit := let hierarchyDepth := getHierarchyDepth struct let structNames := collectStructNames struct #[] (propagateLoop hierarchyDepth 0 struct { allStructNames := structNames }).run' {} end DefaultFields private def elabStructInstAux (stx : Syntax) (expectedType? : Option Expr) (source : Source) : TermElabM Expr := do let (structName, structType) ← getStructName stx expectedType? source unless isStructureLike (← getEnv) structName do throwError "invalid \{...} notation, structure type expected{indentExpr structType}" let struct ← liftMacroM <| mkStructView stx structName source let struct ← expandStruct struct trace[Elab.struct] "{struct}" let (r, struct) ← elabStruct struct expectedType? DefaultFields.propagate struct return r @[builtinTermElab structInst] def elabStructInst : TermElab := fun stx expectedType? => do match (← expandNonAtomicExplicitSource stx) with | some stxNew => withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? | none => let sourceView ← getStructSource stx match (← isModifyOp? stx), sourceView with | some modifyOp, Source.explicit source _ => elabModifyOp stx modifyOp source expectedType? | some _, _ => throwError "invalid \{...} notation, explicit source is required when using '[<index>] := <value>'" | _, _ => elabStructInstAux stx expectedType? sourceView builtin_initialize registerTraceClass `Elab.struct end Lean.Elab.Term.StructInst
39df149b598dd78961bd5bd483bf7a620f37fc45
bdb33f8b7ea65f7705fc342a178508e2722eb851
/data/set/disjointed.lean
29019a4a18ade484d93342da229580c91b567965
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
2,977
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 Disjointed sets -/ import data.set.lattice data.nat.basic open set classical lattice local attribute [instance] prop_decidable universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {s t u : set α} /-- A relation `p` holds pairwise if `p i j` for all `i ≠ j`. -/ def pairwise {α : Type*} (p : α → α → Prop) := ∀i j, i ≠ j → p i j namespace set /-- If `f : ℕ → set α` is a sequence of sets, then `disjointed f` is the sequence formed with each set subtracted from the later ones in the sequence, to form a disjoint sequence. -/ def disjointed (f : ℕ → set α) (n : ℕ) : set α := f n ∩ (⋂i<n, - f i) lemma disjoint_disjointed {f : ℕ → set α} : pairwise (disjoint on disjointed f) := assume i j h, have ∀i j, i < j → disjointed f i ∩ disjointed f j = ∅, from assume i j hij, eq_empty_iff_forall_not_mem.2 $ assume x h, have x ∈ f i, from h.left.left, have x ∈ (⋂i<j, - f i), from h.right.right, have x ∉ f i, begin simp at this; exact this _ hij end, absurd ‹x ∈ f i› this, show disjointed f i ∩ disjointed f j = ∅, from (ne_iff_lt_or_gt.mp h).elim (this i j) begin rw [inter_comm], exact this j i end lemma disjointed_Union {f : ℕ → set α} : (⋃n, disjointed f n) = (⋃n, f n) := subset.antisymm (Union_subset_Union $ assume i, inter_subset_left _ _) $ assume x h, have ∃n, x ∈ f n, from (mem_Union_eq _ _).mp h, have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i, from assume i, nat.find_min this, (mem_Union_eq _ _).mpr ⟨nat.find this, nat.find_spec this, by simp; assumption⟩ lemma disjointed_induct {f : ℕ → set α} {n : ℕ} {p : set α → Prop} (h₁ : p (f n)) (h₂ : ∀t i, p t → p (t - f i)) : p (disjointed f n) := have ∀n t, p t → p (t ∩ (⋂i<n, - f i)), begin intro n, induction n, case nat.zero { have h : (⋂ (i : ℕ) (H : i < 0), -f i) = univ, { apply eq_univ_of_forall, simp [mem_Inter, nat.not_lt_zero] }, simp [h, inter_univ] }, case nat.succ : n ih { intro t, have h : (⨅i (H : i < n.succ), -f i) = (⨅i (H : i < n), -f i) ⊓ - f n, by simp [nat.lt_succ_iff_lt_or_eq, infi_or, infi_inf_eq, inf_comm], change (⋂ (i : ℕ) (H : i < n.succ), -f i) = (⋂ (i : ℕ) (H : i < n), -f i) ∩ - f n at h, rw [h, ←inter_assoc], exact assume ht, h₂ _ _ (ih _ ht) } end, this _ _ h₁ lemma disjointed_of_mono {f : ℕ → set α} {n : ℕ} (hf : monotone f) : disjointed f (n + 1) = f (n + 1) \ f n := have (⋂i (h : i < n + 1), -f i) = - f n, from le_antisymm (infi_le_of_le n $ infi_le_of_le (nat.lt_succ_self _) $ subset.refl _) (le_infi $ assume i, le_infi $ assume hi, neg_le_neg $ hf $ nat.le_of_succ_le_succ hi), by simp [disjointed, this, sdiff_eq] end set
346c040c0ad304120434b751049b394740a07990
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/constantCompilerBug.lean
e622f51f03295626af6346eb77f2646eafa77c37
[ "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
260
lean
import Lean open Lean open Lean.Parser def regBlaParserAttribute : IO Unit := registerBuiltinDynamicParserAttribute (Name.mkSimple "bla_parser") (Name.mkSimple "bla") @[inline] def parser : Parser := categoryParser (Name.mkSimple "bla") 0 #check @parser
930c182518cd37910bfac464ada5ee535139ba30
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/ImportingFlag.lean
2a43538dd9b979cccd7d1f283d8b736ee0428076
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
2,251
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ namespace Lean private builtin_initialize importingRef : IO.Ref Bool ← IO.mkRef false private builtin_initialize runInitializersRef : IO.Ref Bool ← IO.mkRef false /-- By default the `initialize` code is not executed when importing .olean files. When this flag is set to `true`, the initializers are executed. This method is meant to be used by the Lean frontend only. Remark: it is not safe to run `initialize` code when using multiple threads. Remark: Any loaded native Lean code must match its imported version. In particular, no two versions of the same module may be loaded when this flag is set. No native code may be loaded after its module has been imported. Remark: Compacted module regions must not be freed when using this flag as the cached initializer results may reference objects in them. Remark: The Lean frontend executes this method at startup time. -/ @[export lean_enable_initializer_execution] unsafe def enableInitializersExecution : IO Unit := runInitializersRef.set true def isInitializerExecutionEnabled : IO Bool := runInitializersRef.get /-- We say Lean is "initializing" when it is executing `builtin_initialize` declarations or importing modules. Recall that Lean executes `initialize` declarations while importing modules. -/ def initializing : IO Bool := IO.initializing <||> importingRef.get /-- Execute `x` with "importing" flag turned on. When the "importing" flag is set to true, we allow user-extensions defined with with the `initialize` command to update global references. IMPORTANT: There is no semaphore controlling the access to these global references. We assume these global references are updated by a single execution thread. This is true in the Lean frontend where we process the `import` commands at the beginning of the execution only. Users must make sure that `importModules` is used, there is only one execution thread accessing the global references. -/ def withImporting (x : IO α) : IO α := try importingRef.set true x finally importingRef.set false runInitializersRef.set false end Lean
4ebb68cc34e7b7970c203de90f3323695a32b031
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/def3.lean
6683e66b0298fddf474ed339977e66a146bc723d
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
191
lean
definition f (a b : nat) : nat := nat.cases_on a (a + b + a + a + b) (λ a', a + a + b) definition g (a b : nat) := f (f a b) a set_option trace.compiler true #eval g (g (f 2 3) 2) 3
643c8ae4faeaafe359b1efd401ca4d98f367da2a
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/tactic/library_search.lean
62c96e2828484671f578c3aaf07cdf5ecca78a1a
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
4,645
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 tactic.solve_by_elim import data.list.defs /- A basic `library_search` tactic. -/ namespace tactic open native namespace library_search meta def head_symbol : expr → name | (expr.pi _ _ _ t) := head_symbol t | (expr.app f _) := head_symbol f | (expr.const n _) := -- TODO this is a hack; if you suspect more cases here would help, please report them match n with | `gt := `has_lt.lt | `ge := `has_le.le | _ := n end | _ := `_ inductive head_symbol_match | ex | mp | mpr | both open head_symbol_match def head_symbol_match.to_string : head_symbol_match → string | ex := "exact" | mp := "iff.mp" | mpr := "iff.mpr" | both := "iff.mp and iff.mpr" meta def unfold_head_symbol : name → list name | `false := [`not, `false] | n := [n] meta def match_head_symbol (hs : name) : expr → option head_symbol_match | (expr.pi _ _ _ t) := match_head_symbol t | `(%%a ↔ %%b) := if `iff = hs then some ex else match (match_head_symbol a, match_head_symbol b) with | (some ex, some ex) := some both | (some ex, _) := some mpr | (_, some ex) := some mp | _ := none end | (expr.app f _) := match_head_symbol f | (expr.const n _) := if list.mem hs (unfold_head_symbol n) then some ex else none | _ := none meta structure decl_data := (d : declaration) (n : name) (m : head_symbol_match) (l : ℕ) -- cached length of name -- We used to check here for private declarations, or declarations with certain suffixes. -- It turns out `apply` is so fast, it's better to just try them all. meta def process_declaration (hs : name) (d : declaration) : option decl_data := let n := d.to_name in if ¬ d.is_trusted ∨ n.is_internal then none else (λ m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type /-- Retrieve all library definitions with a given head symbol. -/ meta def library_defs (hs : name) : tactic (list decl_data) := do env ← get_env, return $ env.decl_filter_map (process_declaration hs) meta def apply_and_solve (e : expr) := apply e >> (done <|> solve_by_elim { all_goals := tt }) meta def apply_declaration (d : decl_data) : tactic unit := do (e, t) ← decl_mk_const d.d, match d.m with | ex := apply_and_solve e | mp := do l ← iff_mp_core e t, apply_and_solve l | mpr := do l ← iff_mpr_core e t, apply_and_solve l | both := (do l ← iff_mp_core e t, apply_and_solve l) <|> (do l ← iff_mpr_core e t, apply_and_solve l) end end library_search open library_search open library_search.head_symbol_match declare_trace silence_library_search -- Turn off `exact ...` trace message declare_trace library_search -- Trace a list of all relevant lemmas meta def library_search : tactic string := do [g] ← get_goals | fail "`library_search` should be called with exactly one goal", t ← infer_type g, -- Collect all definitions with the correct head symbol defs ← library_defs (head_symbol t), -- Sort by length; people like short proofs let defs := defs.qsort(λ d₁ d₂, d₁.l ≤ d₂.l), when (is_trace_enabled_for `library_search) $ (do trace format!"Found {defs.length} relevant lemmas:", trace $ defs.map (λ ⟨d, n, m, l⟩, (n, m.to_string))), -- Try `apply` followed by `solve_by_elim`, for each definition. defs.mfirst apply_declaration, -- If something worked, prepare a string to print. p ← instantiate_mvars g >>= head_beta >>= pp, let r := format!"exact {p}", when (¬ is_trace_enabled_for `silence_library_search) $ tactic.trace r, return $ to_string r namespace interactive /-- `library_search` attempts to apply every definition in the library whose head symbol matches the goal, and then discharge any new goals using `solve_by_elim`. If it succeeds, it prints a trace message `exact ...` which can replace the invocation of `library_search`. -/ meta def library_search := tactic.library_search end interactive @[hole_command] meta def library_search_hole_cmd : hole_command := { name := "library_search", descr := "Use `library_search` to complete the goal.", action := λ _, do script ← library_search, -- Is there a better API for dropping the 'exact ' prefix on this string? return [((script.mk_iterator.remove 6).to_string, "by library_search")] } end tactic
4777aa7e0af70002b714ffed6d0f52f5cc981efb
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/diagonal.lean
646c9da5c390e384e5a79afffd0c2eaf1e99625a
[ "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,092
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.to_lin /-! # Diagonal matrices This file contains some results on the linear map corresponding to a diagonal matrix (`range`, `ker` and `rank`). ## Tags matrix, diagonal, linear_map -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix universes u v w namespace matrix section comm_ring variables {n : Type*} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R] lemma proj_diagonal (i : n) (w : n → R) : (proj i).comp (to_lin' (diagonal w)) = (w i) • proj i := by ext j; simp [mul_vec_diagonal] lemma diagonal_comp_std_basis (w : n → R) (i : n) : (diagonal w).to_lin'.comp (linear_map.std_basis R (λ_:n, R) i) = (w i) • linear_map.std_basis R (λ_:n, R) i := begin ext j, simp_rw [linear_map.comp_apply, to_lin'_apply, mul_vec_diagonal, linear_map.smul_apply, pi.smul_apply, algebra.id.smul_eq_mul], by_cases i = j, { subst h }, { rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] } end lemma diagonal_to_lin' (w : n → R) : (diagonal w).to_lin' = linear_map.pi (λi, w i • linear_map.proj i) := by ext v j; simp [mul_vec_diagonal] end comm_ring section field variables {m n : Type*} [fintype m] [fintype n] variables {K : Type u} [field K] -- maybe try to relax the universe constraint lemma ker_diagonal_to_lin' [decidable_eq m] (w : m → K) : ker (diagonal w).to_lin' = (⨆i∈{i | w i = 0 }, range (linear_map.std_basis K (λi, K) i)) := begin rw [← comap_bot, ← infi_ker_proj, comap_infi], have := λ i : m, ker_comp (to_lin' (diagonal w)) (proj i), simp only [comap_infi, ← this, proj_diagonal, ker_smul'], have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self }, exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) disjoint_compl_right this (finite.of_fintype _)).symm end lemma range_diagonal [decidable_eq m] (w : m → K) : (diagonal w).to_lin'.range = (⨆ i ∈ {i | w i ≠ 0}, (linear_map.std_basis K (λi, K) i).range) := begin dsimp only [mem_set_of_eq], rw [← map_top, ← supr_range_std_basis, map_supr], congr, funext i, rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul'] end lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) : rank (diagonal w).to_lin' = fintype.card { i // w i ≠ 0 } := begin have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self }, have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := disjoint_compl_left, have B₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _), have B₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu, rw [rank, range_diagonal, B₁, ←@dim_fun' K], apply linear_equiv.dim_eq, apply B₂, end end field end matrix
a3adbd26b7c301c75243347f7163268d293f9db0
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/ring_theory/localization.lean
68c84acaaaad122dfdcfb7efbd5c71b4663f90a5
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
71,526
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import data.equiv.ring import group_theory.monoid_localization import ring_theory.ideal.operations import ring_theory.algebraic import ring_theory.integral_closure import ring_theory.non_zero_divisors /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. Given such a localization map `f : R →+* S`, we can define the surjection `localization_map.mk'` sending `(x, y) : R × M` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. Similarly, given commutative rings `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : R →+* P` such that `g(M) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `S` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We show the localization as a quotient type, defined in `group_theory.monoid_localization` as `submonoid.localization`, is a `comm_ring` and that the natural ring hom `of : R →+* localization M` is a localization map. We show that a localization at the complement of a prime ideal is a local ring. We prove some lemmas about the `R`-algebra structure of `S`. When `R` is an integral domain, we define `fraction_map R K` as an abbreviation for `localization (non_zero_divisors R) K`, the natural map to `R`'s field of fractions. We show that a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field. We use this to show the field of fractions as a quotient type, `fraction_ring`, is a field. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A ring localization map is defined to be a localization map of the underlying `comm_monoid` (a `submonoid.localization_map`) which is also a ring hom. To prove most lemmas about a `localization_map` `f` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `f.to_localization_map`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `of : localization_map M (localization M)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] {P : Type*} [comm_ring P] open function open_locale big_operators set_option old_structure_cmd true /-- The type of ring homomorphisms satisfying the characteristic predicate: if `f : R →+* S` satisfies this predicate, then `S` is isomorphic to the localization of `R` at `M`. We later define an instance coercing a localization map `f` to its codomain `S` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. -/ @[nolint has_inhabited_instance] structure localization_map extends ring_hom R S, submonoid.localization_map M S /-- The ring hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_ring_hom /-- The `comm_monoid` `localization_map` underlying a `comm_ring` `localization_map`. See `group_theory.monoid_localization` for its definition. -/ add_decl_doc localization_map.to_localization_map variables {M S} namespace ring_hom /-- Makes a localization map from a `comm_ring` hom satisfying the characteristic predicate. -/ def to_localization_map (f : R →+* S) (H1 : ∀ y : M, is_unit (f y)) (H2 : ∀ z, ∃ x : R × M, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : M, x * c = y * c) : localization_map M S := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end ring_hom /-- Makes a `comm_ring` localization map from an additive `comm_monoid` localization map of `comm_ring`s. -/ def submonoid.localization_map.to_ring_localization (f : submonoid.localization_map M S) (h : ∀ x y, f.to_map (x + y) = f.to_map x + f.to_map y) : localization_map M S := { ..ring_hom.mk' f.to_monoid_hom h, ..f } namespace localization_map variables (f : localization_map M S) /-- We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know` the map needed to induce the instance. -/ @[nolint unused_arguments has_inhabited_instance] def codomain (f : localization_map M S) := S instance : comm_ring f.codomain := by assumption instance {K : Type*} [field K] (f : localization_map M K) : field f.codomain := by assumption /-- Short for `to_ring_hom`; used for applying a localization map as a function. -/ abbreviation to_map := f.to_ring_hom lemma map_units (y : M) : is_unit (f.to_map y) := f.6 y lemma surj (z) : ∃ x : R × M, z * f.to_map x.2 = f.to_map x.1 := f.7 z lemma eq_iff_exists {x y} : f.to_map x = f.to_map y ↔ ∃ c : M, x * c = y * c := f.8 x y @[ext] lemma ext {f g : localization_map M S} (h : ∀ x, f.to_map x = g.to_map x) : f = g := begin cases f, cases g, simp only at *, exact funext h end lemma ext_iff {f g : localization_map M S} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ lemma to_map_injective : injective (@localization_map.to_map _ _ M S _) := λ _ _ h, ext $ ring_hom.ext_iff.1 h /-- Given `a : S`, `S` a localization of `R`, `is_integer a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ set.range f.to_map -- TODO: define a subalgebra of `is_integer`s lemma is_integer_zero : f.is_integer 0 := ⟨0, f.to_map.map_zero⟩ lemma is_integer_one : f.is_integer 1 := ⟨1, f.to_map.map_one⟩ variables {f} lemma is_integer_add {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a + b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' + b', rw [f.to_map.map_add, ha, hb] end lemma is_integer_mul {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a * b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' * b', rw [f.to_map.map_mul, ha, hb] end lemma is_integer_smul {a : R} {b} (hb : f.is_integer b) : f.is_integer (f.to_map a * b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, f.to_map.map_mul] end variables (f) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : ∃ (b : M), is_integer f (a * f.to_map b) := let ⟨⟨num, denom⟩, h⟩ := f.surj a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : ∃ (b : M), is_integer f (f.to_map b * a) := by { simp_rw mul_comm _ a, apply exists_integer_multiple' } /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec {f : localization_map M S} (z : S) : z * f.to_map (f.to_localization_map.sec z).2 = f.to_map (f.to_localization_map.sec z).1 := classical.some_spec $ f.surj z /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' {f : localization_map M S} (z : S) : f.to_map (f.to_localization_map.sec z).1 = f.to_map (f.to_localization_map.sec z).2 * z := by rw [mul_comm, sec_spec] open_locale big_operators /-- We can clear the denominators of a finite set of fractions. -/ lemma exist_integer_multiples_of_finset (s : finset S) : ∃ (b : M), ∀ a ∈ s, is_integer f (f.to_map b * a) := begin haveI := classical.prop_decidable, use ∏ a in s, (f.to_localization_map.sec a).2, intros a ha, use (∏ x in s.erase a, (f.to_localization_map.sec x).2) * (f.to_localization_map.sec a).1, rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←f.to_map.map_mul], congr' 2, refine trans _ ((submonoid.subtype M).map_prod _ _).symm, rw [mul_comm, ←finset.prod_insert (s.not_mem_erase a), finset.insert_erase ha], refl, end lemma map_right_cancel {x y} {c : M} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := f.to_localization_map.map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.to_localization_map.map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * f.to_map y = f.to_map x) (hx : x = 0) : z = 0 := by rw [hx, f.to_map.map_zero] at h; exact (f.map_units y).mul_left_eq_zero.1 h /-- Given a localization map `f : R →+* S`, the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (f : localization_map M S) (x : R) (y : M) : S := f.to_localization_map.mk' x y @[simp] lemma mk'_sec (z : S) : f.mk' (f.to_localization_map.sec z).1 (f.to_localization_map.sec z).2 = z := f.to_localization_map.mk'_sec _ lemma mk'_mul (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := f.to_localization_map.mk'_mul _ _ _ _ lemma mk'_one (x) : f.mk' x (1 : M) = f.to_map x := f.to_localization_map.mk'_one _ @[simp] lemma mk'_spec (x) (y : M) : f.mk' x y * f.to_map y = f.to_map x := f.to_localization_map.mk'_spec _ _ @[simp] lemma mk'_spec' (x) (y : M) : f.to_map y * f.mk' x y = f.to_map x := f.to_localization_map.mk'_spec' _ _ theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := f.to_localization_map.eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := f.to_localization_map.mk'_eq_iff_eq_mul lemma mk'_surjective (z : S) : ∃ x (y : M), f.mk' x y = z := let ⟨r, hr⟩ := f.surj z in ⟨r.1, r.2, (f.eq_mk'_iff_mul_eq.2 hr).symm⟩ lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := f.to_localization_map.mk'_eq_iff_eq lemma mk'_mem_iff {x} {y : M} {I : ideal S} : f.mk' x y ∈ I ↔ f.to_map x ∈ I := begin split; intro h, { rw [← mk'_spec f x y, mul_comm], exact I.mul_mem_left (f.to_map y) h }, { rw ← mk'_spec f x y at h, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (map_units f y), have := I.mul_mem_left b h, rwa [mul_comm, mul_assoc, hb, mul_one] at this } end protected lemma eq {a₁ b₁} {a₂ b₂ : M} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : M, a₁ * b₂ * c = b₁ * a₂ * c := f.to_localization_map.eq lemma eq_iff_eq (g : localization_map M P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.to_localization_map.eq_iff_eq g.to_localization_map lemma mk'_eq_iff_mk'_eq (g : localization_map M P) {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.to_localization_map.mk'_eq_iff_mk'_eq g.to_localization_map lemma mk'_eq_of_eq {a₁ b₁ : R} {a₂ b₂ : M} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.to_localization_map.mk'_eq_of_eq H @[simp] lemma mk'_self {x : R} (hx : x ∈ M) : f.mk' x ⟨x, hx⟩ = 1 := f.to_localization_map.mk'_self _ hx @[simp] lemma mk'_self' {x : M} : f.mk' x x = 1 := f.to_localization_map.mk'_self' _ lemma mk'_self'' {x : M} : f.mk' x.1 x = 1 := f.mk'_self' lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : f.to_map x * f.mk' y z = f.mk' (x * y) z := f.to_localization_map.mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : f.mk' x y = f.to_map x * f.mk' 1 y := (f.to_localization_map.mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : f.mk' (y * x) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : f.mk' (x * y) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_right _ _ @[simp] lemma mk'_mul_mk'_eq_one (x y : M) : f.mk' x y * f.mk' y x = 1 := by rw [←f.mk'_mul, mul_comm]; exact f.mk'_self _ lemma mk'_mul_mk'_eq_one' (x : R) (y : M) (h : x ∈ M) : f.mk' x y * f.mk' y ⟨x, h⟩ = 1 := f.mk'_mul_mk'_eq_one ⟨x, h⟩ _ lemma is_unit_comp (j : S →+* P) (y : M) : is_unit (j.comp f.to_map y) := f.to_localization_map.is_unit_comp j.to_monoid_hom _ /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * y₂ + x₂ * y₁) (y₁ * y₂) = f.mk' x₁ y₁ + f.mk' x₂ y₂ := f.mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ (f.to_map _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←f.to_map.map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [f.to_map.map_add, submonoid.coe_mul, f.to_map.map_mul], ring_exp, end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) : S →+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg) $ begin intros x y, rw [f.to_localization_map.lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, f.to_localization_map.lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, f.to_localization_map.lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), repeat {rw ←g.map_mul}, rw [←g.map_sub, ←g.map_mul], apply f.eq_of_eq hg, erw [f.to_map.map_mul, sec_spec', mul_sub, f.to_map.map_sub], simp only [f.to_map.map_mul, sec_spec'], ring_exp, end variables {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := f.to_localization_map.lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := f.to_localization_map.lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : f.lift hg (f.to_map x) = g x := f.to_localization_map.lift_eq _ _ lemma lift_eq_iff {x y : R × M} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := f.to_localization_map.lift_eq_iff _ @[simp] lemma lift_comp : (f.lift hg).comp f.to_map = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_comp _ @[simp] lemma lift_of_comp (j : S →+* P) : f.lift (f.is_unit_comp j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_of_comp j.to_monoid_hom lemma epic_of_localization_map {j k : S →+* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.epic_of_localization_map _ _ _ _ _ _ _ f.to_localization_map j.to_monoid_hom k.to_monoid_hom h lemma lift_unique {j : S →+* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : f.lift f.map_units x = x := f.to_localization_map.lift_id _ /-- Given two localization maps `f : R →+* S, k : R →+* P` for a submonoid `M ⊆ R`, the hom from `P` to `S` induced by `f` is left inverse to the hom from `S` to `P` induced by `k`. -/ @[simp] lemma lift_left_inverse {k : localization_map M S} (z : S) : k.lift f.map_units (f.lift k.map_units z) = z := f.to_localization_map.lift_left_inverse _ lemma lift_surjective_iff : surjective (f.lift hg) ↔ ∀ v : P, ∃ x : R × M, v * g x.2 = g x.1 := f.to_localization_map.lift_surjective_iff hg lemma lift_injective_iff : injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := f.to_localization_map.lift_injective_iff hg variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) /-- Given a `comm_ring` homomorphism `g : R →+* P` where for submonoids `M ⊆ R, T ⊆ P` we have `g(M) ⊆ T`, the induced ring homomorphism from the localization of `R` at `M` to the localization of `P` at `T`: if `f : R →+* S` and `k : P →+* Q` are localization maps for `M` and `T` respectively, we send `z : S` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map : S →+* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ lemma map_mk' (x) (y : M) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ @[simp] lemma map_id (z : S) : f.map (λ y, show ring_hom.id R y ∈ M, from y.2) f z = z := f.lift_id _ lemma map_unique {j : S →+* Q} (hj : ∀ x : R, j (f.to_map x) = k.to_map (g x)) : f.map hy k = j := f.lift_unique (λ y, k.map_units ⟨g y, hy y⟩) hj /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_comp_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.map_comp_map _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ _ _ _ j.to_localization_map l.to_monoid_hom hl /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl /-- Given localization maps `f : R →+* S, k : P →+* Q` for submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ noncomputable def ring_equiv_of_ring_equiv (k : localization_map T Q) (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := (f.to_localization_map.mul_equiv_of_mul_equiv k.to_localization_map H).to_ring_equiv $ (@lift _ _ _ _ _ _ _ f (k.to_map.comp h.to_ring_hom) (λ y, k.map_units ⟨(h y), H ▸ set.mem_image_of_mem h y.2⟩)).map_add @[simp] lemma ring_equiv_of_ring_equiv_eq_map_apply {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H x = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (f.ring_equiv_of_ring_equiv k j H).to_monoid_hom = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H (f.to_map x) = k.to_map (j x) := f.to_localization_map.mul_equiv_of_mul_equiv_eq H _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x y) : f.ring_equiv_of_ring_equiv k j H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.to_localization_map.mul_equiv_of_mul_equiv_mk' H _ _ section away_map variables (x : R) /-- Given `x : R`, the type of homomorphisms `f : R →* S` such that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away_map (S' : Type*) [comm_ring S'] := localization_map (submonoid.powers x) S' variables (F : away_map x S) /-- Given `x : R` and a localization map `F : R →+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : S := F.mk' 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : R`, a localization map `F : R →+* S` away from `x`, and a map of `comm_ring`s `g : R →+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : S →+* P := localization_map.lift F $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (monoid_hom.of $ ((^ n) : P → P)) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : R) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : R` and localization maps `F : R →+* S, G : R →+* P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ noncomputable def away_to_away_right (y : R) (G : away_map (x * y) P) : S →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, submonoid.mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map namespace localization variables {M} instance : has_add (localization M) := ⟨λ z w, con.lift_on₂ z w (λ x y : R × M, mk ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $ λ r1 r2 r3 r4 h1 h2, (con.eq _).2 begin rw r_eq_r' at h1 h2 ⊢, cases h1 with t₅ ht₅, cases h2 with t₆ ht₆, use t₆ * t₅, calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) = (r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring ... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring end⟩ instance : has_neg (localization M) := ⟨λ z, con.lift_on z (λ x : R × M, mk (-x.1) x.2) $ λ r1 r2 h, (con.eq _).2 begin rw r_eq_r' at h ⊢, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring_nf, end⟩ instance : has_zero (localization M) := ⟨mk 0 1⟩ private meta def tac := `[{ intros, refine quotient.sound' (r_of_eq _), simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), add_assoc := λ m n k, quotient.induction_on₃' m n k (by tac), zero_add := λ y, quotient.induction_on' y (by tac), add_zero := λ y, quotient.induction_on' y (by tac), neg := has_neg.neg, sub := λ x y, x + -y, sub_eq_add_neg := λ x y, rfl, add_left_neg := λ y, by exact quotient.induction_on' y (by tac), add_comm := λ y z, quotient.induction_on₂' z y (by tac), left_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), right_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), ..localization.comm_monoid M } variables (M) /-- Natural hom sending `x : R`, `R` a `comm_ring`, to the equivalence class of `(x, 1)` in the localization of `R` at a submonoid. -/ def of : localization_map M (localization M) := (localization.monoid_of M).to_ring_localization $ λ x y, (con.eq _).2 $ r_of_eq $ by simp [add_comm] variables {M} lemma monoid_of_eq_of (x) : (monoid_of M).to_map x = (of M).to_map x := rfl lemma mk_one_eq_of (x) : mk x 1 = (of M).to_map x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = (of M).mk' x y := mk_eq_monoid_of_mk'_apply _ _ @[simp] lemma mk_eq_mk' : mk = (of M).mk' := mk_eq_monoid_of_mk' variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def ring_equiv_of_quotient : localization M ≃+* S := (mul_equiv_of_quotient f.to_localization_map).to_ring_equiv $ ((of M).lift f.map_units).map_add variables {f} @[simp] lemma ring_equiv_of_quotient_apply (x) : ring_equiv_of_quotient f x = (of M).lift f.map_units x := rfl @[simp] lemma ring_equiv_of_quotient_mk' (x y) : ring_equiv_of_quotient f ((of M).mk' x y) = f.mk' x y := mul_equiv_of_quotient_mk' _ _ lemma ring_equiv_of_quotient_mk (x y) : ring_equiv_of_quotient f (mk x y) = f.mk' x y := mul_equiv_of_quotient_mk _ _ @[simp] lemma ring_equiv_of_quotient_of (x) : ring_equiv_of_quotient f ((of M).to_map x) = f.to_map x := mul_equiv_of_quotient_monoid_of _ @[simp] lemma ring_equiv_of_quotient_symm_mk' (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = (of M).mk' x y := mul_equiv_of_quotient_symm_mk' _ _ lemma ring_equiv_of_quotient_symm_mk (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = mk x y := mul_equiv_of_quotient_symm_mk _ _ @[simp] lemma ring_equiv_of_quotient_symm_of (x) : (ring_equiv_of_quotient f).symm (f.to_map x) = (of M).to_map x := mul_equiv_of_quotient_symm_monoid_of _ section away variables (x : R) /-- Given `x : R`, the natural hom sending `y : R`, `R` a `comm_ring`, to the equivalence class of `(y, 1)` in the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away.of : localization_map.away_map x (away x) := of (submonoid.powers x) @[simp] lemma away.mk_eq_mk' : mk = (away.of x).mk' := mk_eq_mk' /-- Given `x : R` and a localization map `f : R →+* S` away from `x`, we get an isomorphism between the localization of `R` at the submonoid generated by `x` as a quotient type and `S`. -/ noncomputable def away.ring_equiv_of_quotient (f : localization_map.away_map x S) : away x ≃+* S := ring_equiv_of_quotient f end away end localization variables {M} section at_prime variables (I : ideal R) [hp : I.is_prime] include hp namespace ideal /-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/ def prime_compl : submonoid R := { carrier := (Iᶜ : set R), one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl, mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny } end ideal namespace localization_map variables (S) /-- A localization map from `R` to `S` where the submonoid is the complement of a prime ideal of `R`. -/ @[reducible] def at_prime := localization_map I.prime_compl S end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal, as a quotient type. -/ @[reducible] def at_prime := localization I.prime_compl end localization namespace localization_map variables {I} /-- When `f` is a localization map from `R` at the complement of a prime ideal `I`, we use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `local_ring` instance on `S` can 'know' the map needed to induce the instance. -/ instance at_prime.local_ring (f : at_prime S I) : local_ring f.codomain := local_of_nonunits_ideal (λ hze, begin rw [←f.to_map.map_one, ←f.to_map.map_zero] at hze, obtain ⟨t, ht⟩ := f.eq_iff_exists.1 hze, exact ((show (t : R) ∉ I, from t.2) (have htz : (t : R) = 0, by simpa using ht.symm, htz.symm ▸ I.zero_mem)) end) (begin intros x y hx hy hu, cases is_unit_iff_exists_inv.1 hu with z hxyz, have : ∀ {r s}, f.mk' r s ∈ nonunits S → r ∈ I, from λ r s, not_imp_comm.1 (λ nr, is_unit_iff_exists_inv.2 ⟨f.mk' s ⟨r, nr⟩, f.mk'_mul_mk'_eq_one' _ _ nr⟩), rcases f.mk'_surjective x with ⟨rx, sx, hrx⟩, rcases f.mk'_surjective y with ⟨ry, sy, hry⟩, rcases f.mk'_surjective z with ⟨rz, sz, hrz⟩, rw [←hrx, ←hry, ←hrz, ←f.mk'_add, ←f.mk'_mul, ←f.mk'_self I.prime_compl.one_mem] at hxyz, rw ←hrx at hx, rw ←hry at hy, cases f.eq.1 hxyz with t ht, simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht, rw [←sub_eq_zero, ←sub_mul] at ht, have hr := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right t.2, rw sub_eq_add_neg at hr, have := I.neg_mem_iff.1 ((ideal.add_mem_iff_right _ _).1 hr), { exact not_or (mt hp.mem_or_mem (not_or sx.2 sy.2)) sz.2 (hp.mem_or_mem this)}, { exact I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ (this hx)) (I.mul_mem_right _ (this hy)))} end) end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ instance at_prime.local_ring : local_ring (localization I.prime_compl) := localization_map.at_prime.local_ring (of I.prime_compl) end localization end at_prime namespace localization_map variables (f : localization_map M S) section ideals /-- Explicit characterization of the ideal given by `ideal.map f.to_map I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ private def to_map_ideal (I : ideal R) : ideal S := { carrier := { z : S | ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1}, zero_mem' := ⟨⟨0, 1⟩, by simp⟩, add_mem' := begin rintros a b ⟨a', ha⟩ ⟨b', hb⟩, use ⟨a'.2 * b'.1 + b'.2 * a'.1, I.add_mem (I.mul_mem_left _ b'.1.2) (I.mul_mem_left _ a'.1.2)⟩, use a'.2 * b'.2, simp only [ring_hom.map_add, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], rw [add_mul, ← mul_assoc a, ha, mul_comm (f.to_map a'.2) (f.to_map b'.2), ← mul_assoc b, hb], ring end, smul_mem' := begin rintros c x ⟨x', hx⟩, obtain ⟨c', hc⟩ := localization_map.surj f c, use ⟨c'.1 * x'.1, I.mul_mem_left c'.1 x'.1.2⟩, use c'.2 * x'.2, simp only [←hx, ←hc, smul_eq_mul, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], ring end } theorem mem_map_to_map_iff {I : ideal R} {z} : z ∈ ideal.map f.to_map I ↔ ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1 := begin split, { show _ → z ∈ to_map_ideal f I, refine λ h, ideal.mem_Inf.1 h (λ z hz, _), obtain ⟨y, hy⟩ := hz, use ⟨⟨⟨y, hy.left⟩, 1⟩, by simp [hy.right]⟩ }, { rintros ⟨⟨a, s⟩, h⟩, rw [← ideal.unit_mul_mem_iff_mem _ (map_units f s), mul_comm], exact h.symm ▸ ideal.mem_map_of_mem _ a.2 } end theorem map_comap (J : ideal S) : ideal.map f.to_map (ideal.comap f.to_map J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x hJ, begin obtain ⟨r, s, hx⟩ := f.mk'_surjective x, rw ←hx at ⊢ hJ, exact ideal.mul_mem_right _ _ (ideal.mem_map_of_mem _ (show f.to_map r ∈ J, from f.mk'_spec r s ▸ J.mul_mem_right (f.to_map s) hJ)), end theorem comap_map_of_is_prime_disjoint (I : ideal R) (hI : I.is_prime) (hM : disjoint (M : set R) I) : ideal.comap f.to_map (ideal.map f.to_map I) = I := begin refine le_antisymm (λ a ha, _) ideal.le_comap_map, rw [ideal.mem_comap, mem_map_to_map_iff] at ha, obtain ⟨⟨b, s⟩, h⟩ := ha, have : f.to_map (a * ↑s - b) = 0 := by simpa [sub_eq_zero] using h, rw [← f.to_map.map_zero, eq_iff_exists] at this, obtain ⟨c, hc⟩ := this, have : a * s ∈ I, { rw zero_mul at hc, let this : (a * ↑s - ↑b) * ↑c ∈ I := hc.symm ▸ I.zero_mem, cases hI.mem_or_mem this with h1 h2, { simpa using I.add_mem h1 b.2 }, { exfalso, refine hM ⟨c.2, h2⟩ } }, cases hI.mem_or_mem this with h1 h2, { exact h1 }, { exfalso, refine hM ⟨s.2, h2⟩ } end /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding : ideal S ↪o ideal R := { to_fun := λ J, ideal.comap f.to_map J, inj' := function.left_inverse.injective f.map_comap, map_rel_iff' := λ J₁ J₂, ⟨λ hJ, f.map_comap J₁ ▸ f.map_comap J₂ ▸ ideal.map_mono hJ, ideal.comap_mono⟩ } /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ lemma is_prime_iff_is_prime_disjoint (J : ideal S) : J.is_prime ↔ (ideal.comap f.to_map J).is_prime ∧ disjoint (M : set R) ↑(ideal.comap f.to_map J) := begin split, { refine λ h, ⟨⟨_, _⟩, λ m hm, h.ne_top (ideal.eq_top_of_is_unit_mem _ hm.2 (map_units f ⟨m, hm.left⟩))⟩, { refine λ hJ, h.ne_top _, rw [eq_top_iff, ← f.order_embedding.le_iff_le], exact le_of_eq hJ.symm }, { intros x y hxy, rw [ideal.mem_comap, ring_hom.map_mul] at hxy, exact h.mem_or_mem hxy } }, { refine λ h, ⟨λ hJ, h.left.ne_top (eq_top_iff.2 _), _⟩, { rwa [eq_top_iff, ← f.order_embedding.le_iff_le] at hJ }, { intros x y hxy, obtain ⟨a, s, ha⟩ := mk'_surjective f x, obtain ⟨b, t, hb⟩ := mk'_surjective f y, have : f.mk' (a * b) (s * t) ∈ J := by rwa [mk'_mul, ha, hb], rw [mk'_mem_iff, ← ideal.mem_comap] at this, replace this := h.left.mem_or_mem this, rw [ideal.mem_comap, ideal.mem_comap] at this, rwa [← ha, ← hb, mk'_mem_iff, mk'_mem_iff] } } end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ lemma is_prime_of_is_prime_disjoint (I : ideal R) (hp : I.is_prime) (hd : disjoint (M : set R) ↑I) : (ideal.map f.to_map I).is_prime := begin rw [is_prime_iff_is_prime_disjoint f, comap_map_of_is_prime_disjoint f I hp hd], exact ⟨hp, hd⟩ end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime (f : localization_map M S) : {p : ideal S // p.is_prime} ≃o {p : ideal R // p.is_prime ∧ disjoint (M : set R) ↑p} := { to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_prime_iff_is_prime_disjoint f p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_prime_of_is_prime_disjoint f p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap f J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 I.2.1 I.2.2), map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val, from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ } /-- `quotient_map` applied to maximal ideals of a localization is `surjective`. The quotient by a maximal ideal is a field, so inverses to elements already exist, and the localization necessarily maps the equivalence class of the inverse in the localization -/ lemma surjective_quotient_map_of_maximal_of_localization {f : localization_map M S} {I : ideal S} [I.is_prime] {J : ideal R} {H : J ≤ I.comap f.to_map} (hI : (I.comap f.to_map).is_maximal) : function.surjective (I.quotient_map f.to_map H) := begin intro s, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective s, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := f.mk'_surjective s, by_cases hM : (ideal.quotient.mk (I.comap f.to_map)) m = 0, { have : I = ⊤, { rw ideal.eq_top_iff_one, rw [ideal.quotient.eq_zero_iff_mem, ideal.mem_comap] at hM, convert I.mul_mem_right (f.mk' 1 ⟨m, hm⟩) hM, rw [← f.mk'_eq_mul_mk'_one, f.mk'_self] }, exact ⟨0, eq_comm.1 (by simp [ideal.quotient.eq_zero_iff_mem, this])⟩ }, { rw ideal.quotient.maximal_ideal_iff_is_field_quotient at hI, obtain ⟨n, hn⟩ := hI.3 hM, obtain ⟨rn, rfl⟩ := ideal.quotient.mk_surjective n, refine ⟨(ideal.quotient.mk J) (r * rn), _⟩, -- The rest of the proof is essentially just algebraic manipulations to prove the equality rw ← ring_hom.map_mul at hn, replace hn := congr_arg (ideal.quotient_map I f.to_map le_rfl) hn, simp only [ring_hom.map_one, ideal.quotient_map_mk, ring_hom.map_mul] at hn, rw [ideal.quotient_map_mk, ← sub_eq_zero, ← ring_hom.map_sub, ideal.quotient.eq_zero_iff_mem, ← ideal.quotient.eq_zero_iff_mem, ring_hom.map_sub, sub_eq_zero, localization_map.mk'_eq_mul_mk'_one], simp only [mul_eq_mul_left_iff, ring_hom.map_mul], exact or.inl (mul_left_cancel' (λ hn, hM (ideal.quotient.eq_zero_iff_mem.2 (ideal.mem_comap.2 (ideal.quotient.eq_zero_iff_mem.1 hn)))) (trans hn (by rw [← ring_hom.map_mul, ← f.mk'_eq_mul_mk'_one, f.mk'_self, ring_hom.map_one]))) } end end ideals /-! ### `algebra` section Defines the `R`-algebra instance on a copy of `S` carrying the data of the localization map `f` needed to induce the `R`-algebra structure. -/ /-- We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ instance algebra : algebra R f.codomain := f.to_map.to_algebra end localization_map namespace localization instance : algebra R (localization M) := localization_map.algebra (of M) end localization namespace localization_map variables (f : localization_map M S) @[simp] lemma of_id (a : R) : (algebra.of_id R f.codomain) a = f.to_map a := rfl @[simp] lemma algebra_map_eq : algebra_map R f.codomain = f.to_map := rfl variables (f) /-- Localization map `f` from `R` to `S` as an `R`-linear map. -/ def lin_coe : R →ₗ[R] f.codomain := { to_fun := f.to_map, map_add' := f.to_map.map_add, map_smul' := f.to_map.map_mul } /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `f.codomain = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule (I : ideal R) : submodule R f.codomain := submodule.map f.lin_coe I variables {f} lemma mem_coe_submodule (I : ideal R) {x : S} : x ∈ f.coe_submodule I ↔ ∃ y : R, y ∈ I ∧ f.to_map y = x := iff.rfl @[simp] lemma lin_coe_apply {x} : f.lin_coe x = f.to_map x := rfl variables {g : R →+* P} variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) lemma map_smul (x : f.codomain) (z : R) : f.map hy k (z • x : f.codomain) = @has_scalar.smul P k.codomain _ (g z) (f.map hy k x) := show f.map hy k (f.to_map z * x) = k.to_map (g z) * f.map hy k x, by rw [ring_hom.map_mul, map_eq] lemma is_noetherian_ring (h : is_noetherian_ring R) : is_noetherian_ring f.codomain := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at h ⊢, exact order_embedding.well_founded (f.order_embedding.dual) h end end localization_map namespace localization variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an `R`-preserving isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def alg_equiv_of_quotient : localization M ≃ₐ[R] f.codomain := { commutes' := ring_equiv_of_quotient_of, ..ring_equiv_of_quotient f } lemma alg_equiv_of_quotient_apply (x : localization M) : alg_equiv_of_quotient f x = ring_equiv_of_quotient f x := rfl lemma alg_equiv_of_quotient_symm_apply (x : f.codomain) : (alg_equiv_of_quotient f).symm x = (ring_equiv_of_quotient f).symm x := rfl end localization namespace localization_map section integer_normalization variables {f : localization_map M S} open polynomial open_locale classical /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ noncomputable def coeff_integer_normalization (p : polynomial f.codomain) (i : ℕ) : R := if hi : i ∈ p.support then classical.some (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) else 0 lemma coeff_integer_normalization_of_not_mem_support (p : polynomial f.codomain) (i : ℕ) (h : coeff p i = 0) : coeff_integer_normalization p i = 0 := by simp only [coeff_integer_normalization, h, mem_support_iff, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff] lemma coeff_integer_normalization_mem_support (p : polynomial f.codomain) (i : ℕ) (h : coeff_integer_normalization p i ≠ 0) : i ∈ p.support := begin contrapose h, rw [ne.def, not_not, coeff_integer_normalization, dif_neg h] end /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ noncomputable def integer_normalization (f : localization_map M S) (p : polynomial f.codomain) : polynomial R := ∑ i in p.support, monomial i (coeff_integer_normalization p i) @[simp] lemma integer_normalization_coeff (p : polynomial f.codomain) (i : ℕ) : (f.integer_normalization p).coeff i = coeff_integer_normalization p i := by simp [integer_normalization, coeff_monomial, coeff_integer_normalization_of_not_mem_support] {contextual := tt} lemma integer_normalization_spec (p : polynomial f.codomain) : ∃ (b : M), ∀ i, f.to_map ((f.integer_normalization p).coeff i) = f.to_map b * p.coeff i := begin use classical.some (f.exist_integer_multiples_of_finset (p.support.image p.coeff)), intro i, rw [integer_normalization_coeff, coeff_integer_normalization], split_ifs with hi, { exact classical.some_spec (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) }, { convert (_root_.mul_zero (f.to_map _)).symm, { exact f.to_ring_hom.map_zero }, { exact not_mem_support_iff.mp hi } } end lemma integer_normalization_map_to_map (p : polynomial f.codomain) : ∃ (b : M), (f.integer_normalization p).map f.to_map = f.to_map b • p := let ⟨b, hb⟩ := integer_normalization_spec p in ⟨b, polynomial.ext (λ i, by { rw [coeff_map, coeff_smul], exact hb i })⟩ variables {R' : Type*} [comm_ring R'] lemma integer_normalization_eval₂_eq_zero (g : f.codomain →+* R') (p : polynomial f.codomain) {x : R'} (hx : eval₂ g x p = 0) : eval₂ (g.comp f.to_map) x (f.integer_normalization p) = 0 := let ⟨b, hb⟩ := integer_normalization_map_to_map p in trans (eval₂_map f.to_map g x).symm (by rw [hb, eval₂_smul, hx, mul_zero]) lemma integer_normalization_aeval_eq_zero [algebra R R'] [algebra f.codomain R'] [is_scalar_tower R f.codomain R'] (p : polynomial f.codomain) {x : R'} (hx : aeval x p = 0) : aeval x (f.integer_normalization p) = 0 := by rw [aeval_def, is_scalar_tower.algebra_map_eq R f.codomain R', algebra_map_eq, integer_normalization_eval₂_eq_zero _ _ hx] end integer_normalization variables {R} {A K : Type*} [integral_domain A] lemma to_map_eq_zero_iff (f : localization_map M S) {x : R} (hM : M ≤ non_zero_divisors R) : f.to_map x = 0 ↔ x = 0 := begin rw ← f.to_map.map_zero, split; intro h, { cases f.eq_iff_exists.mp h with c hc, rw zero_mul at hc, exact hM c.2 x hc }, { rw h }, end lemma injective (f : localization_map M S) (hM : M ≤ non_zero_divisors R) : injective f.to_map := begin rw ring_hom.injective_iff f.to_map, intros a ha, rw [← f.to_map.map_zero, f.eq_iff_exists] at ha, cases ha with c hc, rw zero_mul at hc, exact hM c.2 a hc, end protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] (f : localization_map M S) (hM : M ≤ non_zero_divisors R) (x : non_zero_divisors R) : f.to_map x ≠ 0 := map_ne_zero_of_mem_non_zero_divisors (f.injective hM) /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. -/ def integral_domain_of_le_non_zero_divisors {M : submonoid A} (f : localization_map M S) (hM : M ≤ non_zero_divisors A) : integral_domain S := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases f.surj z with x hx, cases f.surj w with y hy, have : z * w * f.to_map y.2 * f.to_map x.2 = f.to_map x.1 * f.to_map y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, rw [h, zero_mul, zero_mul, ← f.to_map.map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero ((to_map_eq_zero_iff f hM).mp this.symm) with H H, { exact or.inl (f.eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (f.eq_zero_of_fst_eq_zero hy H) }, end, exists_pair_ne := ⟨f.to_map 0, f.to_map 1, λ h, zero_ne_one (f.injective hM h)⟩, ..(infer_instance : comm_ring S) } /-- The localization at of an integral domain to a set of non-zero elements is an integral domain -/ def integral_domain_localization {M : submonoid A} (hM : M ≤ non_zero_divisors A) : integral_domain (localization M) := (localization.of M).integral_domain_of_le_non_zero_divisors hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ instance integral_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) : integral_domain (localization.at_prime P) := integral_domain_localization (le_non_zero_divisors_of_domain (by simpa only [] using P.zero_mem)) end localization_map section at_prime namespace localization_map variables (I : ideal R) [hI : I.is_prime] (f : at_prime S I) include hI lemma at_prime.is_unit_to_map_iff (x : R) : is_unit (f.to_map x) ↔ x ∈ I.prime_compl := ⟨λ h hx, (f.is_prime_of_is_prime_disjoint I hI disjoint_compl_left).ne_top $ (ideal.map f.to_map I).eq_top_of_is_unit_mem (ideal.mem_map_of_mem _ hx) h, λ h, f.map_units ⟨x, h⟩⟩ lemma at_prime.to_map_mem_maximal_iff (x : R) : f.to_map x ∈ local_ring.maximal_ideal (f.codomain) ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal (f.codomain), mem_nonunits_iff, not_not] using f.is_unit_to_map_iff I x lemma at_prime.is_unit_mk'_iff (x : R) (y : I.prime_compl) : is_unit (f.mk' x y) ↔ x ∈ I.prime_compl := ⟨λ h hx, (mk'_mem_iff f).mpr ((f.to_map_mem_maximal_iff I x).mpr hx) h, λ h, is_unit_iff_exists_inv.mpr ⟨f.mk' y ⟨x, h⟩, f.mk'_mul_mk'_eq_one ⟨x, h⟩ y⟩⟩ lemma at_prime.mk'_mem_maximal_iff (x : R) (y : I.prime_compl) : f.mk' x y ∈ local_ring.maximal_ideal (f.codomain) ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal (f.codomain), mem_nonunits_iff, not_not] using f.is_unit_mk'_iff I x y end localization_map namespace localization open localization_map local attribute [instance] classical.prop_decidable variables (I : ideal R) [hI : I.is_prime] include hI variables {I} /-- The unique maximal ideal of the localization at `I.prime_compl` lies over the ideal `I`. -/ lemma at_prime.comap_maximal_ideal : ideal.comap (localization.of I.prime_compl).to_map (local_ring.maximal_ideal (localization I.prime_compl)) = I := ideal.ext $ λ x, by simpa only [ideal.mem_comap] using at_prime.to_map_mem_maximal_iff I _ x /-- The image of `I` in the localization at `I.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ lemma at_prime.map_eq_maximal_ideal : ideal.map (localization.of I.prime_compl).to_map I = (local_ring.maximal_ideal (localization I.prime_compl)) := begin convert congr_arg (ideal.map (localization.of _).to_map) at_prime.comap_maximal_ideal.symm, rw map_comap, end variables (I) (f : P →+* R) /-- For a ring hom `f : P →+* R` and a prime ideal `I` in `R`, the induced ring hom from the localization of `P` at `ideal.comap f I` to the localization of `R` at `I` -/ noncomputable def local_ring_hom : at_prime (ideal.comap f I) →+* at_prime I := (localization.of _).map (λ y, show f y ∈ I.prime_compl, from y.2) (localization.of _) lemma local_ring_hom_to_map (x : P) : local_ring_hom I f ((localization.of _).to_map x) = (localization.of _).to_map (f x) := map_eq _ _ _ lemma local_ring_hom_mk' (x : P) (y : (ideal.comap f I).prime_compl) : local_ring_hom I f ((localization.of _).mk' x y) = (localization.of _).mk' (f x) ⟨f y, y.2⟩ := map_mk' _ _ _ _ instance is_local_ring_hom_local_ring_hom : is_local_ring_hom (local_ring_hom I f) := begin constructor, intros x hx, rcases (localization.of _).mk'_surjective x with ⟨r, s, rfl⟩, rw local_ring_hom_mk' at hx, rw at_prime.is_unit_mk'_iff at hx ⊢, exact hx end lemma local_ring_hom_unique {j : at_prime (ideal.comap f I) →+* at_prime I} (hj : ∀ x : P, j ((localization.of _).to_map x) = (localization.of _).to_map (f x)) : local_ring_hom I f = j := map_unique _ _ hj end localization end at_prime /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ lemma localization_map_bijective_of_field {R Rₘ : Type*} [integral_domain R] [comm_ring Rₘ] {M : submonoid R} (hM : (0 : R) ∉ M) (hR : is_field R) (f : localization_map M Rₘ) : function.bijective f.to_map := begin refine ⟨f.injective (le_non_zero_divisors_of_domain hM), λ x, _⟩, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := f.mk'_surjective x, obtain ⟨n, hn⟩ := hR.mul_inv_cancel (λ hm0, hM (hm0 ▸ hm) : m ≠ 0), exact ⟨r * n, by erw [f.eq_mk'_iff_mul_eq, ← f.to_map.map_mul, mul_assoc, mul_comm n, hn, mul_one]⟩ end variables (R) {A : Type*} [integral_domain A] variables (K : Type*) /-- Localization map from an integral domain `R` to its field of fractions. -/ @[reducible] def fraction_map [comm_ring K] := localization_map (non_zero_divisors R) K namespace fraction_map open localization_map variables {R K} lemma to_map_eq_zero_iff [comm_ring K] (φ : fraction_map R K) {x : R} : φ.to_map x = 0 ↔ x = 0 := φ.to_map_eq_zero_iff (le_of_eq rfl) protected theorem injective [comm_ring K] (φ : fraction_map R K) : function.injective φ.to_map := φ.injective (le_of_eq rfl) protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] [comm_ring K] (φ : fraction_map R K) (x : non_zero_divisors R) : φ.to_map x ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (le_of_eq rfl) x /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain [comm_ring K] (φ : fraction_map A K) : integral_domain K := φ.integral_domain_of_le_non_zero_divisors (le_of_eq rfl) local attribute [instance] classical.dec_eq /-- The inverse of an element in the field of fractions of an integral domain. -/ protected noncomputable def inv [comm_ring K] (φ : fraction_map A K) (z : K) : K := if h : z = 0 then 0 else φ.mk' (φ.to_localization_map.sec z).2 ⟨(φ.to_localization_map.sec z).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, h $ φ.eq_zero_of_fst_eq_zero (sec_spec z) h0⟩ protected lemma mul_inv_cancel [comm_ring K] (φ : fraction_map A K) (x : K) (hx : x ≠ 0) : x * φ.inv x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (φ.map_units ⟨(φ.to_localization_map.sec x).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, hx $ φ.eq_zero_of_fst_eq_zero (sec_spec x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (φ.mk'_sec x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ noncomputable def to_field [comm_ring K] (φ : fraction_map A K) : field K := { inv := φ.inv, mul_inv_cancel := φ.mul_inv_cancel, inv_zero := dif_pos rfl, ..φ.to_integral_domain } variables {B : Type*} [integral_domain B] [field K] {L : Type*} [field L] (f : fraction_map A K) {g : A →+* L} lemma mk'_eq_div {r s} : f.mk' r s = f.to_map r / f.to_map s := f.mk'_eq_iff_eq_mul.2 $ (div_mul_cancel _ (f.to_map_ne_zero_of_mem_non_zero_divisors _)).symm lemma is_unit_map_of_injective (hg : function.injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ map_ne_zero_of_mem_non_zero_divisors hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K →+* L := f.lift $ is_unit_map_of_injective hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] lemma lift_mk' (hg : injective g) (x y) : f.lift hg (f.mk' x y) = g x / g y := begin erw f.lift_mk' (is_unit_map_of_injective hg), erw submonoid.localization_map.mul_inv_left (λ y : non_zero_divisors A, show is_unit (g.to_monoid_hom y), from is_unit_map_of_injective hg y), exact (mul_div_cancel' _ (map_ne_zero_of_mem_non_zero_divisors hg)).symm, end /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : fraction_map B L) {j : A →+* B} (hj : injective j) : K →+* L := f.map (λ y, mem_non_zero_divisors_iff_ne_zero.2 $ map_ne_zero_of_mem_non_zero_divisors hj) g /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv (g : fraction_map B L) (h : A ≃+* B) : K ≃+* L := f.ring_equiv_of_ring_equiv g h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end /-- The cast from `int` to `rat` as a `fraction_map`. -/ def int.fraction_map : fraction_map ℤ ℚ := { to_fun := coe, map_units' := begin rintro ⟨x, hx⟩, rw mem_non_zero_divisors_iff_ne_zero at hx, simpa only [is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj' := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists' := begin intros x y, rw [int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa mem_non_zero_divisors_iff_ne_zero at hc, end, ..int.cast_ring_hom ℚ } lemma integer_normalization_eq_zero_iff {p : polynomial f.codomain} : f.integer_normalization p = 0 ↔ p = 0 := begin refine (polynomial.ext_iff.trans (polynomial.ext_iff.trans _).symm), obtain ⟨⟨b, nonzero⟩, hb⟩ := integer_normalization_spec p, split; intros h i, { apply f.to_map_eq_zero_iff.mp, rw [hb i, h i], exact _root_.mul_zero _ }, { have hi := h i, rw [polynomial.coeff_zero, ← f.to_map_eq_zero_iff, hb i] at hi, apply or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hi), intro h, apply mem_non_zero_divisors_iff_ne_zero.mp nonzero, exact f.to_map_eq_zero_iff.mp h } end /-- A field is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma comap_is_algebraic_iff [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] : algebra.is_algebraic A L ↔ algebra.is_algebraic f.codomain L := begin split; intros h x; obtain ⟨p, hp, px⟩ := h x, { refine ⟨p.map f.to_map, λ h, hp (polynomial.ext (λ i, _)), _⟩, { have : f.to_map (p.coeff i) = 0 := trans (polynomial.coeff_map _ _).symm (by simp [h]), exact f.to_map_eq_zero_iff.mp this }, { rwa [is_scalar_tower.aeval_apply _ f.codomain, algebra_map_eq] at px } }, { exact ⟨f.integer_normalization p, mt f.integer_normalization_eq_zero_iff.mp hp, integer_normalization_aeval_eq_zero p px⟩ }, end section num_denom variables [unique_factorization_monoid A] (φ : fraction_map A K) lemma exists_reduced_fraction (x : φ.codomain) : ∃ (a : A) (b : non_zero_divisors A), (∀ {d}, d ∣ a → d ∣ b → is_unit d) ∧ φ.mk' a b = x := begin obtain ⟨⟨b, b_nonzero⟩, a, hab⟩ := φ.exists_integer_multiple x, obtain ⟨a', b', c', no_factor, rfl, rfl⟩ := unique_factorization_monoid.exists_reduced_factors' a b (mem_non_zero_divisors_iff_ne_zero.mp b_nonzero), obtain ⟨c'_nonzero, b'_nonzero⟩ := mul_mem_non_zero_divisors.mp b_nonzero, refine ⟨a', ⟨b', b'_nonzero⟩, @no_factor, _⟩, apply mul_left_cancel' (φ.to_map_ne_zero_of_mem_non_zero_divisors ⟨c' * b', b_nonzero⟩), simp only [subtype.coe_mk, φ.to_map.map_mul] at *, erw [←hab, mul_assoc, φ.mk'_spec' a' ⟨b', b'_nonzero⟩], end /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ noncomputable def num (x : φ.codomain) : A := classical.some (φ.exists_reduced_fraction x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ noncomputable def denom (x : φ.codomain) : non_zero_divisors A := classical.some (classical.some_spec (φ.exists_reduced_fraction x)) lemma num_denom_reduced (x : φ.codomain) : ∀ {d}, d ∣ φ.num x → d ∣ φ.denom x → is_unit d := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).1 @[simp] lemma mk'_num_denom (x : φ.codomain) : φ.mk' (φ.num x) (φ.denom x) = x := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).2 lemma num_mul_denom_eq_num_iff_eq {x y : φ.codomain} : x * φ.to_map (φ.denom y) = φ.to_map (φ.num y) ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_iff_eq' {x y : φ.codomain} : y * φ.to_map (φ.denom x) = φ.to_map (φ.num x) ↔ x = y := ⟨ λ h, by simpa only [eq_comm, mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_mul_denom_iff_eq {x y : φ.codomain} : φ.num y * φ.denom x = φ.num x * φ.denom y ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.mk'_eq_of_eq h, λ h, by rw h ⟩ lemma eq_zero_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : x = 0 := φ.num_mul_denom_eq_num_iff_eq'.mp (by rw [zero_mul, h, ring_hom.map_zero]) lemma is_integer_of_is_unit_denom {x : φ.codomain} (h : is_unit (φ.denom x : A)) : φ.is_integer x := begin cases h with d hd, have d_ne_zero : φ.to_map (φ.denom x) ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (φ.denom x), use ↑d⁻¹ * φ.num x, refine trans _ (φ.mk'_num_denom x), rw [φ.to_map.map_mul, φ.to_map.map_units_inv, hd], apply mul_left_cancel' d_ne_zero, rw [←mul_assoc, mul_inv_cancel d_ne_zero, one_mul, φ.mk'_spec'] end lemma is_unit_denom_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : is_unit (φ.denom x : A) := φ.num_denom_reduced x (h.symm ▸ dvd_zero _) (dvd_refl _) end num_denom end fraction_map section algebra section is_integral variables {R S} {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ noncomputable def localization_algebra (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) : algebra Rₘ Sₘ := (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).to_algebra variables (f : localization_map M Rₘ) variables (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) lemma algebra_map_mk' (r : R) (m : M) : (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) (f.mk' r m) = g.mk' (algebra_map R S r) ⟨algebra_map R S m, algebra.mem_algebra_map_submonoid_of_mem m⟩ := localization_map.map_mk' f _ r m /-- Injectivity of a map descends to the map induced on localizations. -/ lemma map_injective_of_injective {R S : Type*} [comm_ring R] [comm_ring S] (ϕ : R →+* S) (hϕ : function.injective ϕ) (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (M.map ϕ : submonoid S) Sₘ) (hM : (M.map ϕ : submonoid S) ≤ non_zero_divisors S) : function.injective (f.map (M.apply_coe_mem_map (ϕ : R →* S)) g) := begin rintros x y hxy, obtain ⟨a, b, rfl⟩ := localization_map.mk'_surjective f x, obtain ⟨c, d, rfl⟩ := localization_map.mk'_surjective f y, rw [localization_map.map_mk' f _ a b, localization_map.map_mk' f _ c d, localization_map.mk'_eq_iff_eq] at hxy, refine (localization_map.mk'_eq_iff_eq f).2 (congr_arg f.to_map (hϕ _)), convert g.injective hM hxy; simp, end /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization. -/ lemma localization_algebra_injective (hRS : function.injective (algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≤ non_zero_divisors S) : function.injective (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) := map_injective_of_injective (algebra_map R S) hRS M f g hM open polynomial lemma ring_hom.is_integral_elem_localization_at_leading_coeff {R S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) (x : S) (p : polynomial R) (hf : p.eval₂ f x = 0) (M : submonoid R) (hM : p.leading_coeff ∈ M) {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] (ϕ : localization_map M Rₘ) (ϕ' : localization_map (M.map ↑f : submonoid S) Sₘ) : (ϕ.map (M.apply_coe_mem_map (f : R →* S)) ϕ').is_integral_elem (ϕ'.to_map x) := begin by_cases triv : (1 : Rₘ) = 0, { exact ⟨0, ⟨trans leading_coeff_zero triv.symm, eval₂_zero _ _⟩⟩ }, haveI : nontrivial Rₘ := nontrivial_of_ne 1 0 triv, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.mp (localization_map.map_units ϕ ⟨p.leading_coeff, hM⟩), refine ⟨(p.map ϕ.to_map) * C b, ⟨_, _⟩⟩, { refine monic_mul_C_of_leading_coeff_mul_eq_one _, rwa leading_coeff_map_of_leading_coeff_ne_zero ϕ.to_map, refine λ hfp, zero_ne_one (trans (zero_mul b).symm (hfp ▸ hb) : (0 : Rₘ) = 1) }, { refine eval₂_mul_eq_zero_of_left _ _ _ _, erw [eval₂_map, localization_map.map_comp, ← hom_eval₂ _ f ϕ'.to_map x], exact trans (congr_arg ϕ'.to_map hf) ϕ'.to_map.map_zero } end /-- Given a particular witness to an element being algebraic over an algebra `R → S`, We can localize to a submonoid containing the leading coefficient to make it integral. Explicitly, the map between the localizations will be an integral ring morphism -/ theorem is_integral_localization_at_leading_coeff {x : S} (p : polynomial R) (hp : aeval x p = 0) (hM : p.leading_coeff ∈ M) : (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).is_integral_elem (g.to_map x) := (algebra_map R S).is_integral_elem_localization_at_leading_coeff x p hp M hM f g /-- If `R → S` is an integral extension, `M` is a submonoid of `R`, `Rₘ` is the localization of `R` at `M`, and `Sₘ` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rₘ → Sₘ` is also an integral extension -/ theorem is_integral_localization (H : algebra.is_integral R S) : (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).is_integral := begin intro x, by_cases triv : (1 : R) = 0, { have : (1 : Rₘ) = 0 := by convert congr_arg f.to_map triv; simp, exact ⟨0, ⟨trans leading_coeff_zero this.symm, eval₂_zero _ _⟩⟩ }, { haveI : nontrivial R := nontrivial_of_ne 1 0 triv, obtain ⟨⟨s, ⟨u, hu⟩⟩, hx⟩ := g.surj x, obtain ⟨v, hv⟩ := hu, obtain ⟨v', hv'⟩ := is_unit_iff_exists_inv'.1 (f.map_units ⟨v, hv.1⟩), refine @is_integral_of_is_integral_mul_unit Rₘ _ _ _ (localization_algebra M f g) x (g.to_map u) v' _ _, { replace hv' := congr_arg (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) hv', rw [ring_hom.map_mul, ring_hom.map_one, ← ring_hom.comp_apply _ f.to_map] at hv', erw localization_map.map_comp at hv', exact hv.2 ▸ hv' }, { obtain ⟨p, hp⟩ := H s, exact hx.symm ▸ is_integral_localization_at_leading_coeff f g p hp.2 (hp.1.symm ▸ M.one_mem) } } end lemma is_integral_localization' {R S : Type*} [comm_ring R] [comm_ring S] {f : R →+* S} (hf : f.is_integral) (M : submonoid R) : ((localization.of M).map (M.apply_coe_mem_map (f : R →* S)) (localization.of (M.map ↑f))).is_integral := @is_integral_localization R _ M S _ _ _ _ _ f.to_algebra _ _ hf end is_integral namespace integral_closure variables {L : Type*} [field K] [field L] (f : fraction_map A K) open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_algebraic [algebra A L] (alg : is_algebraic A L) (inj : ∀ x, algebra_map A L x = 0 → x = 0) : fraction_map (integral_closure A L) L := (algebra_map (integral_closure A L) L).to_localization_map (λ ⟨⟨y, integral⟩, nonzero⟩, have y ≠ 0 := λ h, mem_non_zero_divisors_iff_ne_zero.mp nonzero (subtype.ext_iff_val.mpr h), show is_unit y, from ⟨⟨y, y⁻¹, mul_inv_cancel this, inv_mul_cancel this⟩, rfl⟩) (λ z, let ⟨x, y, hy, hxy⟩ := exists_integral_multiple (alg z) inj in ⟨⟨x, ⟨y, mem_non_zero_divisors_iff_ne_zero.mpr hy⟩⟩, hxy⟩) (λ x y, ⟨ λ (h : x.1 = y.1), ⟨1, by simpa using subtype.ext_iff_val.mpr h⟩, λ ⟨c, hc⟩, congr_arg (algebra_map _ L) (mul_right_cancel' (mem_non_zero_divisors_iff_ne_zero.mp c.2) hc) ⟩) variables {K} (L) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_finite_extension [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] [finite_dimensional f.codomain L] : fraction_map (integral_closure A L) L := fraction_map_of_algebraic (f.comap_is_algebraic_iff.mpr is_algebraic_of_finite) (λ x hx, f.to_map_eq_zero_iff.mp ((algebra_map f.codomain L).map_eq_zero.mp $ (is_scalar_tower.algebra_map_apply _ _ _ _).symm.trans hx)) end integral_closure end algebra variables (A) /-- The fraction field of an integral domain as a quotient type. -/ @[reducible] def fraction_ring := localization (non_zero_divisors A) namespace fraction_ring /-- Natural hom sending `x : A`, `A` an integral domain, to the equivalence class of `(x, 1)` in the field of fractions of `A`. -/ def of : fraction_map A (localization (non_zero_divisors A)) := localization.of (non_zero_divisors A) variables {A} noncomputable instance : field (fraction_ring A) := (of A).to_field @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = ((of A).to_map r / (of A).to_map s : fraction_ring A) := by erw [localization.mk_eq_mk', (of A).mk'_eq_div] /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def alg_equiv_of_quotient {K : Type*} [field K] (f : fraction_map A K) : fraction_ring A ≃ₐ[A] f.codomain := localization.alg_equiv_of_quotient f instance : algebra A (fraction_ring A) := (of A).to_map.to_algebra end fraction_ring
5f0e61849d7d9d080ca17993595d9a1b8eb79723
82e44445c70db0f03e30d7be725775f122d72f3e
/test/norm_num_ext.lean
d135656f6aa58854bcc3cb225c74c150d175070e
[ "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
7,563
lean
/- Copyright (c) 2021 Mario Carneiro All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Tests for norm_num extensions -/ import data.nat.prime import data.int.gcd -- coverage tests example : nat.coprime 1 2 := by norm_num example : nat.coprime 2 1 := by norm_num example : ¬ nat.coprime 0 0 := by norm_num example : ¬ nat.coprime 0 3 := by norm_num example : ¬ nat.coprime 2 0 := by norm_num example : nat.coprime 2 3 := by norm_num example : ¬ nat.coprime 2 4 := by norm_num example : nat.gcd 1 2 = 1 := by norm_num example : nat.gcd 2 1 = 1 := by norm_num example : nat.gcd 0 0 = 0 := by norm_num example : nat.gcd 0 3 = 3 := by norm_num example : nat.gcd 2 0 = 2 := by norm_num example : nat.gcd 2 3 = 1 := by norm_num example : nat.gcd 2 4 = 2 := by norm_num example : nat.lcm 1 2 = 2 := by norm_num example : nat.lcm 2 1 = 2 := by norm_num example : nat.lcm 0 0 = 0 := by norm_num example : nat.lcm 0 3 = 0 := by norm_num example : nat.lcm 2 0 = 0 := by norm_num example : nat.lcm 2 3 = 6 := by norm_num example : nat.lcm 2 4 = 4 := by norm_num example : int.gcd 2 3 = 1 := by norm_num example : int.gcd (-2) 3 = 1 := by norm_num example : int.gcd 2 (-3) = 1 := by norm_num example : int.gcd (-2) (-3) = 1 := by norm_num example : int.lcm 2 3 = 6 := by norm_num example : int.lcm (-2) 3 = 6 := by norm_num example : int.lcm 2 (-3) = 6 := by norm_num example : int.lcm (-2) (-3) = 6 := by norm_num example : ¬ nat.prime 0 := by norm_num example : ¬ nat.prime 1 := by norm_num example : nat.prime 2 := by norm_num example : nat.prime 3 := by norm_num example : ¬ nat.prime 4 := by norm_num example : nat.prime 5 := by norm_num example : nat.prime 109 := by norm_num example : nat.prime 1277 := by norm_num example : ¬ nat.prime 1000000000000000000000000000000000000000000000000 := by norm_num example : nat.min_fac 0 = 2 := by norm_num example : nat.min_fac 1 = 1 := by norm_num example : nat.min_fac 2 = 2 := by norm_num example : nat.min_fac 3 = 3 := by norm_num example : nat.min_fac 4 = 2 := by norm_num example : nat.min_fac 121 = 11 := by norm_num example : nat.min_fac 221 = 13 := by norm_num example : nat.factors 0 = [] := by norm_num example : nat.factors 1 = [] := by norm_num example : nat.factors 2 = [2] := by norm_num example : nat.factors 3 = [3] := by norm_num example : nat.factors 4 = [2, 2] := by norm_num example : nat.factors 12 = [2, 2, 3] := by norm_num example : nat.factors 221 = [13, 17] := by norm_num -- randomized tests example : nat.gcd 35 29 = 1 := by norm_num example : int.gcd 35 29 = 1 := by norm_num example : nat.lcm 35 29 = 1015 := by norm_num example : int.gcd 35 29 = 1 := by norm_num example : nat.coprime 35 29 := by norm_num example : nat.gcd 80 2 = 2 := by norm_num example : int.gcd 80 2 = 2 := by norm_num example : nat.lcm 80 2 = 80 := by norm_num example : int.gcd 80 2 = 2 := by norm_num example : ¬ nat.coprime 80 2 := by norm_num example : nat.gcd 19 17 = 1 := by norm_num example : int.gcd 19 17 = 1 := by norm_num example : nat.lcm 19 17 = 323 := by norm_num example : int.gcd 19 17 = 1 := by norm_num example : nat.coprime 19 17 := by norm_num example : nat.gcd 11 18 = 1 := by norm_num example : int.gcd 11 18 = 1 := by norm_num example : nat.lcm 11 18 = 198 := by norm_num example : int.gcd 11 18 = 1 := by norm_num example : nat.coprime 11 18 := by norm_num example : nat.gcd 23 73 = 1 := by norm_num example : int.gcd 23 73 = 1 := by norm_num example : nat.lcm 23 73 = 1679 := by norm_num example : int.gcd 23 73 = 1 := by norm_num example : nat.coprime 23 73 := by norm_num example : nat.gcd 73 68 = 1 := by norm_num example : int.gcd 73 68 = 1 := by norm_num example : nat.lcm 73 68 = 4964 := by norm_num example : int.gcd 73 68 = 1 := by norm_num example : nat.coprime 73 68 := by norm_num example : nat.gcd 28 16 = 4 := by norm_num example : int.gcd 28 16 = 4 := by norm_num example : nat.lcm 28 16 = 112 := by norm_num example : int.gcd 28 16 = 4 := by norm_num example : ¬ nat.coprime 28 16 := by norm_num example : nat.gcd 44 98 = 2 := by norm_num example : int.gcd 44 98 = 2 := by norm_num example : nat.lcm 44 98 = 2156 := by norm_num example : int.gcd 44 98 = 2 := by norm_num example : ¬ nat.coprime 44 98 := by norm_num example : nat.gcd 21 79 = 1 := by norm_num example : int.gcd 21 79 = 1 := by norm_num example : nat.lcm 21 79 = 1659 := by norm_num example : int.gcd 21 79 = 1 := by norm_num example : nat.coprime 21 79 := by norm_num example : nat.gcd 93 34 = 1 := by norm_num example : int.gcd 93 34 = 1 := by norm_num example : nat.lcm 93 34 = 3162 := by norm_num example : int.gcd 93 34 = 1 := by norm_num example : nat.coprime 93 34 := by norm_num example : ¬ nat.prime 912 := by norm_num example : nat.min_fac 912 = 2 := by norm_num example : nat.factors 912 = [2, 2, 2, 2, 3, 19] := by norm_num example : ¬ nat.prime 681 := by norm_num example : nat.min_fac 681 = 3 := by norm_num example : nat.factors 681 = [3, 227] := by norm_num example : ¬ nat.prime 728 := by norm_num example : nat.min_fac 728 = 2 := by norm_num example : nat.factors 728 = [2, 2, 2, 7, 13] := by norm_num example : ¬ nat.prime 248 := by norm_num example : nat.min_fac 248 = 2 := by norm_num example : nat.factors 248 = [2, 2, 2, 31] := by norm_num example : ¬ nat.prime 682 := by norm_num example : nat.min_fac 682 = 2 := by norm_num example : nat.factors 682 = [2, 11, 31] := by norm_num example : ¬ nat.prime 115 := by norm_num example : nat.min_fac 115 = 5 := by norm_num example : nat.factors 115 = [5, 23] := by norm_num example : ¬ nat.prime 824 := by norm_num example : nat.min_fac 824 = 2 := by norm_num example : nat.factors 824 = [2, 2, 2, 103] := by norm_num example : ¬ nat.prime 942 := by norm_num example : nat.min_fac 942 = 2 := by norm_num example : nat.factors 942 = [2, 3, 157] := by norm_num example : ¬ nat.prime 34 := by norm_num example : nat.min_fac 34 = 2 := by norm_num example : nat.factors 34 = [2, 17] := by norm_num example : ¬ nat.prime 754 := by norm_num example : nat.min_fac 754 = 2 := by norm_num example : nat.factors 754 = [2, 13, 29] := by norm_num example : ¬ nat.prime 663 := by norm_num example : nat.min_fac 663 = 3 := by norm_num example : nat.factors 663 = [3, 13, 17] := by norm_num example : ¬ nat.prime 923 := by norm_num example : nat.min_fac 923 = 13 := by norm_num example : nat.factors 923 = [13, 71] := by norm_num example : ¬ nat.prime 77 := by norm_num example : nat.min_fac 77 = 7 := by norm_num example : nat.factors 77 = [7, 11] := by norm_num example : ¬ nat.prime 162 := by norm_num example : nat.min_fac 162 = 2 := by norm_num example : nat.factors 162 = [2, 3, 3, 3, 3] := by norm_num example : ¬ nat.prime 669 := by norm_num example : nat.min_fac 669 = 3 := by norm_num example : nat.factors 669 = [3, 223] := by norm_num example : ¬ nat.prime 476 := by norm_num example : nat.min_fac 476 = 2 := by norm_num example : nat.factors 476 = [2, 2, 7, 17] := by norm_num example : nat.prime 251 := by norm_num example : nat.min_fac 251 = 251 := by norm_num example : nat.factors 251 = [251] := by norm_num example : ¬ nat.prime 129 := by norm_num example : nat.min_fac 129 = 3 := by norm_num example : nat.factors 129 = [3, 43] := by norm_num example : ¬ nat.prime 471 := by norm_num example : nat.min_fac 471 = 3 := by norm_num example : nat.factors 471 = [3, 157] := by norm_num example : ¬ nat.prime 851 := by norm_num example : nat.min_fac 851 = 23 := by norm_num example : nat.factors 851 = [23, 37] := by norm_num
492a46c7b6b70d2893d603d1614440754e4fc8b5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/floatarray.lean
9aa0a5d79cea5598535b4067a098c35de16c090f
[ "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
260
lean
def tst : IO Unit := do let bs := [(1 : Float), 2, 3].toFloatArray; IO.println bs; let bs := bs.push (4 : Float); let bs := bs.set! 1 (20 / 3); IO.println bs; let bs₁ := bs.set! 2 30; IO.println bs₁; IO.println bs; IO.println bs.size; pure () #eval tst
40c7586723b4c3bce0e0560a4ee5caf54f03b67a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/277b.lean
1d988b8264059ca30a35a43d72ba686cdb2926fb
[ "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
130
lean
structure Point where x : Int y : Int def foo (inputs : List Point) : (Float × Float) := (0.0, 0.0) #eval foo ⟨0, 0⟩
6781b9d63e509feb5d6c778ff90b20f84bd70708
63abd62053d479eae5abf4951554e1064a4c45b4
/src/group_theory/order_of_element.lean
e6059743bf4dcce61e4e902bf381a5c270d71685
[ "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
24,293
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.order import group_theory.coset import data.nat.totient import data.int.gcd import data.set.finite open function open_locale big_operators variables {α : Type*} {s : set α} {a a₁ a₂ b c: α} -- TODO mem_range_iff_mem_finset_range_of_mod_eq should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset lemma conj_injective [group α] {x : α} : function.injective (λ (g : α), x * g * x⁻¹) := λ a b h, by simpa [mul_left_inj, mul_right_inj] using h lemma mem_normalizer_fintype [group α] {s : set α} [fintype s] {x : α} (h : ∀ n, n ∈ s → x * n * x⁻¹ ∈ s) : x ∈ subgroup.set_normalizer s := by haveI := classical.prop_decidable; haveI := set.fintype_image s (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective s conj_injective), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' s := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩ section order_of variable [group α] open quotient_group set instance fintype_bot : fintype (⊥ : subgroup α) := ⟨{1}, by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩ @[simp] lemma card_trivial : fintype.card (⊥ : subgroup α) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : α), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩ variables [fintype α] [dec : decidable_eq α] instance quotient_group.fintype (s : subgroup α) [d : decidable_pred (λ a, a ∈ s)] : fintype (quotient s) := @quotient.fintype _ _ (left_rel s) (λ _ _, d _) lemma card_eq_card_quotient_mul_card_subgroup (s : subgroup α) [fintype s] [decidable_pred (λ a, a ∈ s)] : fintype.card α = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (subgroup.group_equiv_quotient_times_subgroup) lemma card_subgroup_dvd_card (s : subgroup α) [fintype s] : fintype.card s ∣ fintype.card α := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card (s : subgroup α) [decidable_pred (λ a, a ∈ s)] [fintype s] : fintype.card (quotient s) ∣ fintype.card α := by simp [card_eq_card_quotient_mul_card_subgroup s] lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 := have ¬ injective (λi:ℤ, a ^ i), from not_injective_infinite_fintype _, let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j, by rw [injective] at this; simpa [not_forall] in have a ^ (i - j) = 1, by simp [sub_eq_add_neg, gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end include dec /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂ lemma order_of_pos (a : α) : 0 < order_of a := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n ≠ m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≤ fintype.card α := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : α} : a' ∈ subgroup.gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (subgroup.gpowers a : set α) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (λ h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_dvd_iff_pow_eq_one {n : ℕ} : order_of a ∣ n ↔ a ^ n = 1 := ⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩ lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) : ∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card = (finset.univ.filter (λ a : α, a ^ n = 1)).card := calc ∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card = _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext (begin assume a, suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, one_pow], λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (subgroup.gpowers a : set α) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≤ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end @[simp] lemma order_of_one : order_of (1 : α) = 1 := by rw [order_eq_card_gpowers, fintype.card_eq_one_iff]; exact ⟨⟨1, 0, rfl⟩, λ ⟨a, i, ha⟩, by simp [ha.symm]⟩ @[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 := ⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, λ h, by simp [h]⟩ lemma order_of_eq_prime {p : ℕ} [hp : fact p.prime] (hg : a^p = 1) (hg1 : a ≠ 1) : order_of a = p := (hp.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1) section classical open_locale classical open quotient_group subgroup /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α := have ft_prod : fintype (quotient (gpowers a) × (gpowers a)), from fintype.of_equiv α group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, (gpowers a).one_mem⟩⟩, have ft : fintype (quotient (gpowers a) × (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card α = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eq₂ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (subgroup.gpowers a)) ft_cosets) $ by rw [eq₁, eq₂, mul_comm] omit dec @[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma mem_powers_iff_mem_gpowers {a x : α} : x ∈ submonoid.powers a ↔ x ∈ gpowers a := ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩ lemma powers_eq_gpowers (a : α) : (submonoid.powers a : set α) = gpowers a := set.ext $ λ x, mem_powers_iff_mem_gpowers end classical open nat subgroup lemma order_of_pow (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / gcd (order_of a) n := dvd_antisymm (order_of_dvd_of_pow_eq_one (by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, one_pow])) (have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a), have hdvd : order_of a ∣ n * order_of (a ^ n), from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]), coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos) (dvd_of_mul_dvd_mul_right gcd_pos (by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm]))) lemma image_range_order_of (a : α) : finset.image (λ i, a ^ i) (finset.range (order_of a)) = (gpowers a : set α).to_finset := by { ext x, rw [set.mem_to_finset, mem_coe, mem_gpowers_iff_mem_range_order_of] } omit dec open_locale classical lemma pow_gcd_card_eq_one_iff {n : ℕ} {a : α} : a ^ n = 1 ↔ a ^ (gcd n (fintype.card α)) = 1 := ⟨λ h, pow_gcd_eq_one _ h $ pow_card_eq_one _, λ h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card α) in by rw [hm, pow_mul, h, one_pow]⟩ end end order_of section cyclic local attribute [instance] set_fintype open subgroup /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (α : Type*) [group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ gpowers g) /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator α in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm ▸ hn ▸ gpow_mul_comm _ _ _, ..hg } lemma is_cyclic_of_order_of_eq_card [group α] [decidable_eq α] [fintype α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by {rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers], refl})⟩⟩ lemma order_of_eq_card_of_forall_mem_gpowers [group α] [decidable_eq α] [fintype α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by {rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers], simp [hx], apply fintype.card_of_finset', simp, intro x, exact hx x} instance bot.is_cyclic [group α] : is_cyclic (⊥ : subgroup α) := ⟨⟨1, λ x, ⟨0, subtype.eq $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩ instance subgroup.is_cyclic [group α] [is_cyclic α] (H : subgroup α) : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by rw gpow_mul; exact H.gpow_mem (nat.find_spec hex).2 _, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (subgroup.mul_mem_cancel_right H hk₂).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.ext_iff_val.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = (⊥ : subgroup α), from subgroup.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [subgroup.mem_bot.1 h]; exact H.one_mem⟩, by clear _let_match; substI this; apply_instance open finset nat lemma is_cyclic.card_pow_eq_one_le [group α] [decidable_eq α] [fintype α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ ((gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))) : set α).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ submonoid.powers g, from mem_powers_iff_mem_gpowers.2 $ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)), begin rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end lemma is_cyclic.exists_monoid_generator (α : Type*) [group α] [fintype α] [is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ submonoid.powers x := by { simp only [mem_powers_iff_mem_gpowers], exact is_cyclic.exists_generator α } section variables [group α] [decidable_eq α] [fintype α] lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (order_of a)) = univ := begin simp_rw [←subgroup.mem_coe] at ha, simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha], convert set.to_finset_univ end lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (fintype.card α)) = univ := by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [group α] [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos _)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d | 0 := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a | (d+1) := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : ∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card = ∑ m in (range d.succ).filter (∣ d.succ), φ m, from finset.sum_congr rfl (λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2 ⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _, by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)]⟩⟩)), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_left_inj (∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card)).1 (calc _ = ∑ m in insert d.succ (filter (∣ d.succ) (range d.succ)), (univ.filter (λ a : α, order_of a = m)).card : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card : sum_congr hinsert (λ _ _, rfl) ... = (univ.filter (λ a : α, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), φ m : ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (λ _ _, rfl)) lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := by_contradiction $ λ h, have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 := not_not.1 (mt nat.pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card α in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (λ a : α, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext_iff, c] ... = ∑ m in (range c.succ).filter (∣ c), (univ.filter (λ a : α, order_of a = m)).card : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = ∑ m in ((range c.succ).filter (∣ c)).erase d, (univ.filter (λ a : α, order_of a = m)).card : eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, have m = d, by simp at *; cc, by simp [*, finset.ext_iff] at *; exact h0)) ... ≤ ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : sum_le_sum (λ m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (λ h, by rw h)) ... < φ d + ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (λ h, nat.pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd)))) ... = ∑ m in insert d (((range c.succ).filter (∣ c)).erase d), φ m : eq.symm (sum_insert (by simp)) ... = ∑ m in (range c.succ).filter (∣ c), φ m : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [decidable_eq α] [fintype α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end cyclic
de841d18e7a68f9911882dd0d14352100d4166d7
c37c89b934e368a90005cf2a72457da4d59a48a1
/Orderingtheory/src/Ordering.lean
a97b7d782c9ce2f343ecf212cdfdade1c2dbba17
[ "Apache-2.0" ]
permissive
mukeshtiwari/Leanplayground
aa1cd9c69d4f1db6c824778a560c72f1e5aeaf65
773deaf73fbb677cdf518d0db34ad62a79bad642
refs/heads/master
1,667,860,698,346
1,664,052,419,000
1,664,052,419,000
209,532,791
0
0
null
null
null
null
UTF-8
Lean
false
false
4,191
lean
/- https://github.com/coq-community/corn/tree/master/order http://matt.might.net/articles/partial-orders/ -/ namespace Ordering universes u v def dual_relation (A : Type u) (R : A -> A -> Prop) := ∀ x y : A, R x y ↔ R y x /- Preorder is reflexive and transitive -/ class preorder (A : Type u) (R : A -> A -> Prop) := (Hreflexive : ∀ x : A, R x x) (Htransitive : ∀ x y z, R x y → R y z → R x z) class total_preorder (A : Type u) (R : A → A → Prop) := (Hp : preorder A R) (Htotal : ∀ x y : A, R x y ∨ R y x) /- Equivalence is a preorder set with symmetry -/ class equivalence (A : Type u) (R : A -> A -> Prop) := (Hp : preorder A R) (Hsymmetric : ∀ x y : A, R x y → R y x) /- Partial order is -/ class partial_order (A : Type u) (R : A -> A -> Prop) := (Hp : preorder A R) (Hantisymmetric : ∀ x y, R x y → R y x -> x = y) /- Meet semilattic is poset with meet -/ class meet_semilattice (A : Type u) (R : A -> A -> Prop) (meet : A -> A -> A) := (Hp : partial_order A R) (H₁ : ∀ x y : A, R (meet x y) x) (H₂ : ∀ x y : A, R (meet x y) y) (H₃ : ∀ x y z : A, R z x → R z y → R z (meet x y)) lemma meet_idempotent : forall (A : Type u) (R : A -> A -> Prop) (x : A) (meet : A → A → A), meet_semilattice A R meet → meet x x = x := begin intros A R x meet Hmeet, apply Hmeet.Hp.Hantisymmetric, apply Hmeet.H₁, apply Hmeet.H₃; apply Hmeet.Hp.Hp.Hreflexive end lemma meet_commutative : ∀ (A : Type u) (R : A -> A -> Prop) (x y : A) (meet : A → A → A), meet_semilattice A R meet → meet x y = meet y x := begin intros A R x y meet Hmeet, apply Hmeet.Hp.Hantisymmetric, /- How to combine top and below one in one go ? -/ apply Hmeet.H₃, apply Hmeet.H₂, apply Hmeet.H₁, apply Hmeet.H₃, apply Hmeet.H₂, apply Hmeet.H₁, end lemma meet_associative : ∀ (A : Type u) (R : A -> A -> Prop) (x y z : A) (meet : A → A → A), meet_semilattice A R meet -> meet x (meet y z) = meet (meet x y) z := begin sorry end class join_semilattice (A : Type u) (R : A -> A -> Prop) (join : A -> A -> A) := (Hp : partial_order A R) (H₁ : ∀ x y : A, R x (join x y)) (H₂ : ∀ x y : A, R y (join x y)) (H₃ : ∀ x y z : A, R x z -> R y z -> R (join x y) z) lemma join_idempotent : forall (A : Type u) (R : A -> A -> Prop) (x : A) (join : A → A → A), join_semilattice A R join → join x x = x := begin intros A R x join Hjoin, apply Hjoin.Hp.Hantisymmetric, apply Hjoin.H₃; apply Hjoin.Hp.Hp.Hreflexive, apply Hjoin.H₁ end /- Can I pull some category theoretic duality notation about meet and join. This proof is same as meet_commutative -/ lemma join_commutative : ∀ (A : Type u) (R : A -> A -> Prop) (x y : A) (join : A → A → A), join_semilattice A R join → join x y = join y x := begin intros A R x y join Hjoin, apply Hjoin.Hp.Hantisymmetric, /- How to combine top and below one in one go ? -/ apply Hjoin.H₃, apply Hjoin.H₂, apply Hjoin.H₁, apply Hjoin.H₃, apply Hjoin.H₂, apply Hjoin.H₁, end lemma join_associative : ∀ (A : Type u) (R : A -> A -> Prop) (x y z : A) (join : A → A → A), meet_semilattice A R join -> join x (join y z) = join (join x y) z := begin /- Write a pen and paper proof first -/ sorry end class lattice (A : Type u) (R : A -> A -> Prop) (meet : A -> A -> A) (join : A -> A -> A) := (Hmeet : meet_semilattice A R meet) (Hjoin : join_semilattice A R join) class bounded_lattic (A : Type u) (R : A -> A -> Prop) (Top : A) (Bot : A) (meet : A -> A -> A) (join : A -> A -> A) := (Hla : lattice A R meet join) (Hb : ∀ x, R Bot x) (Ht : ∀ x, R x Top) /- f is subset of g -/ def subset (A : Type u) (f : A -> Prop) (g : A -> Prop) := ∀ x : A, f x -> g x class complete_lattic (A : Type u) (R : A -> A -> Prop) def monotone_fun (A : Type u) (B : Type v) (f : A -> B) (Ra : A -> A -> Prop) (Rb : B -> B -> Prop) := partial_order A Ra -> partial_order B Rb -> ∀ (x y : A), Ra x y -> Rb (f x) (f y) def fixed_point (A : Type u) (f : A -> A) (x : A) := f x = x end Ordering
44f11d411ad89d12328c360eebe0c3d59e38fe80
43390109ab88557e6090f3245c47479c123ee500
/src/M1F/problem_bank/0102/Q0102.lean
5002dfb22c876aa3bde80ae488b29c197731f2e3
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
943
lean
/- M1F 2017-18 Sheet 1 Question 1 Author : Kevin Buzzard This file should work with any version of lean -- whether you installed it yourself or are running the version on https://leanprover.github.io/live/latest/ -/ -- We probably need the "law of the excluded middle" for this question -- every -- proposition is either true or false! Don't even ask me to explain what the -- other options are, but Lean does not come with this axiom by default (blame -- the computer scientists) and mathematicians have to add it themselves. -- It's easy to add though. "em" for excluded middle. axiom em (X : Prop) : X ∨ ¬ X variables P Q R S : Prop -- A "Prop" is a proposition, that is, a true/false statement. -- Sheet 1 Q2. Prove one result and delete the other. theorem m1f_sheet01_q02_is_T (HQP : Q → P) (HnQnR : ¬ Q → ¬ R) : R → P := sorry theorem m1f_sheet01_q02_is_F (HQP : Q → P) (HnQnR : ¬ Q → ¬ R) : ¬ (R → P) := sorry
40e82049b8bda88efab42efc193d151735c14ff2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/direct_sum/module.lean
cc94ec98c350e7d9036f2b20b8bde867e7be147f
[ "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
14,901
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.direct_sum.basic import linear_algebra.dfinsupp /-! # Direct sum of modules The first part of the file provides constructors for direct sums of modules. It provides a construction of the direct sum using the universal property and proves its uniqueness (`direct_sum.to_module.unique`). The second part of the file covers the special case of direct sums of submodules of a fixed module `M`. There is a canonical linear map from this direct sum to `M` (`direct_sum.coe_linear_map`), and the construction is of particular importance when this linear map is an equivalence; that is, when the submodules provide an internal decomposition of `M`. The property is defined more generally elsewhere as `direct_sum.is_internal`, but its basic consequences on `submodule`s are established in this file. -/ universes u v w u₁ namespace direct_sum open_locale direct_sum section general variables {R : Type u} [semiring R] variables {ι : Type v} [dec_ι : decidable_eq ι] include R variables {M : ι → Type w} [Π i, add_comm_monoid (M i)] [Π i, module R (M i)] instance : module R (⨁ i, M i) := dfinsupp.module instance {S : Type*} [semiring S] [Π i, module S (M i)] [Π i, smul_comm_class R S (M i)] : smul_comm_class R S (⨁ i, M i) := dfinsupp.smul_comm_class instance {S : Type*} [semiring S] [has_smul R S] [Π i, module S (M i)] [Π i, is_scalar_tower R S (M i)] : is_scalar_tower R S (⨁ i, M i) := dfinsupp.is_scalar_tower instance [Π i, module Rᵐᵒᵖ (M i)] [Π i, is_central_scalar R (M i)] : is_central_scalar R (⨁ i, M i) := dfinsupp.is_central_scalar lemma smul_apply (b : R) (v : ⨁ i, M i) (i : ι) : (b • v) i = b • (v i) := dfinsupp.smul_apply _ _ _ include dec_ι variables R ι M /-- Create the direct sum given a family `M` of `R` modules indexed over `ι`. -/ def lmk : Π s : finset ι, (Π i : (↑s : set ι), M i.val) →ₗ[R] (⨁ i, M i) := dfinsupp.lmk /-- Inclusion of each component into the direct sum. -/ def lof : Π i : ι, M i →ₗ[R] (⨁ i, M i) := dfinsupp.lsingle lemma lof_eq_of (i : ι) (b : M i) : lof R ι M i b = of M i b := rfl variables {ι M} lemma single_eq_lof (i : ι) (b : M i) : dfinsupp.single i b = lof R ι M i b := rfl /-- Scalar multiplication commutes with direct sums. -/ theorem mk_smul (s : finset ι) (c : R) (x) : mk M s (c • x) = c • mk M s x := (lmk R ι M s).map_smul c x /-- Scalar multiplication commutes with the inclusion of each component into the direct sum. -/ theorem of_smul (i : ι) (c : R) (x) : of M i (c • x) = c • of M i x := (lof R ι M i).map_smul c x variables {R} lemma support_smul [Π (i : ι) (x : M i), decidable (x ≠ 0)] (c : R) (v : ⨁ i, M i) : (c • v).support ⊆ v.support := dfinsupp.support_smul _ _ variables {N : Type u₁} [add_comm_monoid N] [module R N] variables (φ : Π i, M i →ₗ[R] N) variables (R ι N φ) /-- The linear map constructed using the universal property of the coproduct. -/ def to_module : (⨁ i, M i) →ₗ[R] N := dfinsupp.lsum ℕ φ /-- Coproducts in the categories of modules and additive monoids commute with the forgetful functor from modules to additive monoids. -/ lemma coe_to_module_eq_coe_to_add_monoid : (to_module R ι N φ : (⨁ i, M i) → N) = to_add_monoid (λ i, (φ i).to_add_monoid_hom) := rfl variables {ι N φ} /-- The map constructed using the universal property gives back the original maps when restricted to each component. -/ @[simp] lemma to_module_lof (i) (x : M i) : to_module R ι N φ (lof R ι M i x) = φ i x := to_add_monoid_of (λ i, (φ i).to_add_monoid_hom) i x variables (ψ : (⨁ i, M i) →ₗ[R] N) /-- Every linear map from a direct sum agrees with the one obtained by applying the universal property to each of its components. -/ theorem to_module.unique (f : ⨁ i, M i) : ψ f = to_module R ι N (λ i, ψ.comp $ lof R ι M i) f := to_add_monoid.unique ψ.to_add_monoid_hom f variables {ψ} {ψ' : (⨁ i, M i) →ₗ[R] N} /-- Two `linear_map`s out of a direct sum are equal if they agree on the generators. See note [partially-applied ext lemmas]. -/ @[ext] theorem linear_map_ext ⦃ψ ψ' : (⨁ i, M i) →ₗ[R] N⦄ (H : ∀ i, ψ.comp (lof R ι M i) = ψ'.comp (lof R ι M i)) : ψ = ψ' := dfinsupp.lhom_ext' H /-- The inclusion of a subset of the direct summands into a larger subset of the direct summands, as a linear map. -/ def lset_to_set (S T : set ι) (H : S ⊆ T) : (⨁ (i : S), M i) →ₗ[R] (⨁ (i : T), M i) := to_module R _ _ $ λ i, lof R T (λ (i : subtype T), M i) ⟨i, H i.prop⟩ omit dec_ι variables (ι M) /-- Given `fintype α`, `linear_equiv_fun_on_fintype R` is the natural `R`-linear equivalence between `⨁ i, M i` and `Π i, M i`. -/ @[simps apply] def linear_equiv_fun_on_fintype [fintype ι] : (⨁ i, M i) ≃ₗ[R] (Π i, M i) := { to_fun := coe_fn, map_add' := λ f g, by { ext, simp only [add_apply, pi.add_apply] }, map_smul' := λ c f, by { ext, simp only [dfinsupp.coe_smul, ring_hom.id_apply] }, .. dfinsupp.equiv_fun_on_fintype } variables {ι M} @[simp] lemma linear_equiv_fun_on_fintype_lof [fintype ι] [decidable_eq ι] (i : ι) (m : M i) : (linear_equiv_fun_on_fintype R ι M) (lof R ι M i m) = pi.single i m := begin ext a, change (dfinsupp.equiv_fun_on_fintype (lof R ι M i m)) a = _, convert _root_.congr_fun (dfinsupp.equiv_fun_on_fintype_single i m) a, end @[simp] lemma linear_equiv_fun_on_fintype_symm_single [fintype ι] [decidable_eq ι] (i : ι) (m : M i) : (linear_equiv_fun_on_fintype R ι M).symm (pi.single i m) = lof R ι M i m := begin ext a, change (dfinsupp.equiv_fun_on_fintype.symm (pi.single i m)) a = _, rw (dfinsupp.equiv_fun_on_fintype_symm_single i m), refl end @[simp] lemma linear_equiv_fun_on_fintype_symm_coe [fintype ι] (f : ⨁ i, M i) : (linear_equiv_fun_on_fintype R ι M).symm f = f := by { ext, simp [linear_equiv_fun_on_fintype], } /-- The natural linear equivalence between `⨁ _ : ι, M` and `M` when `unique ι`. -/ protected def lid (M : Type v) (ι : Type* := punit) [add_comm_monoid M] [module R M] [unique ι] : (⨁ (_ : ι), M) ≃ₗ[R] M := { .. direct_sum.id M ι, .. to_module R ι M (λ i, linear_map.id) } variables (ι M) /-- The projection map onto one component, as a linear map. -/ def component (i : ι) : (⨁ i, M i) →ₗ[R] M i := dfinsupp.lapply i variables {ι M} lemma apply_eq_component (f : ⨁ i, M i) (i : ι) : f i = component R ι M i f := rfl @[ext] lemma ext {f g : ⨁ i, M i} (h : ∀ i, component R ι M i f = component R ι M i g) : f = g := dfinsupp.ext h lemma ext_iff {f g : ⨁ i, M i} : f = g ↔ ∀ i, component R ι M i f = component R ι M i g := ⟨λ h _, by rw h, ext R⟩ include dec_ι @[simp] lemma lof_apply (i : ι) (b : M i) : ((lof R ι M i) b) i = b := dfinsupp.single_eq_same @[simp] lemma component.lof_self (i : ι) (b : M i) : component R ι M i ((lof R ι M i) b) = b := lof_apply R i b lemma component.of (i j : ι) (b : M j) : component R ι M i ((lof R ι M j) b) = if h : j = i then eq.rec_on h b else 0 := dfinsupp.single_apply omit dec_ι section congr_left variables {κ : Type*} /--Reindexing terms of a direct sum is linear.-/ def lequiv_congr_left (h : ι ≃ κ) : (⨁ i, M i) ≃ₗ[R] ⨁ k, M (h.symm k) := { map_smul' := dfinsupp.comap_domain'_smul _ _, ..equiv_congr_left h } @[simp] lemma lequiv_congr_left_apply (h : ι ≃ κ) (f : ⨁ i, M i) (k : κ) : lequiv_congr_left R h f k = f (h.symm k) := equiv_congr_left_apply _ _ _ end congr_left section sigma variables {α : ι → Type*} {δ : Π i, α i → Type w} variables [Π i j, add_comm_monoid (δ i j)] [Π i j, module R (δ i j)] /--`curry` as a linear map.-/ noncomputable def sigma_lcurry : (⨁ (i : Σ i, _), δ i.1 i.2) →ₗ[R] ⨁ i j, δ i j := { map_smul' := λ r, by convert (@dfinsupp.sigma_curry_smul _ _ _ δ _ _ _ r), ..sigma_curry } @[simp] lemma sigma_lcurry_apply (f : ⨁ (i : Σ i, _), δ i.1 i.2) (i : ι) (j : α i) : sigma_lcurry R f i j = f ⟨i, j⟩ := sigma_curry_apply f i j /--`uncurry` as a linear map.-/ noncomputable def sigma_luncurry : (⨁ i j, δ i j) →ₗ[R] ⨁ (i : Σ i, _), δ i.1 i.2 := { map_smul' := dfinsupp.sigma_uncurry_smul, ..sigma_uncurry } @[simp] lemma sigma_luncurry_apply (f : ⨁ i j, δ i j) (i : ι) (j : α i) : sigma_luncurry R f ⟨i, j⟩ = f i j := sigma_uncurry_apply f i j /--`curry_equiv` as a linear equiv.-/ noncomputable def sigma_lcurry_equiv : (⨁ (i : Σ i, _), δ i.1 i.2) ≃ₗ[R] ⨁ i j, δ i j := { ..sigma_curry_equiv, ..sigma_lcurry R } end sigma section option variables {α : option ι → Type w} [Π i, add_comm_monoid (α i)] [Π i, module R (α i)] include dec_ι /--Linear isomorphism obtained by separating the term of index `none` of a direct sum over `option ι`.-/ @[simps] noncomputable def lequiv_prod_direct_sum : (⨁ i, α i) ≃ₗ[R] α none × ⨁ i, α (some i) := { map_smul' := dfinsupp.equiv_prod_dfinsupp_smul, ..add_equiv_prod_direct_sum } end option end general section submodule section semiring variables {R : Type u} [semiring R] variables {ι : Type v} [dec_ι : decidable_eq ι] include dec_ι variables {M : Type*} [add_comm_monoid M] [module R M] variables (A : ι → submodule R M) /-- The canonical embedding from `⨁ i, A i` to `M` where `A` is a collection of `submodule R M` indexed by `ι`. This is `direct_sum.coe_add_monoid_hom` as a `linear_map`. -/ def coe_linear_map : (⨁ i, A i) →ₗ[R] M := to_module R ι M (λ i, (A i).subtype) @[simp] lemma coe_linear_map_of (i : ι) (x : A i) : direct_sum.coe_linear_map A (of (λ i, A i) i x) = x := to_add_monoid_of _ _ _ variables {A} /-- If a direct sum of submodules is internal then the submodules span the module. -/ lemma is_internal.submodule_supr_eq_top (h : is_internal A) : supr A = ⊤ := begin rw [submodule.supr_eq_range_dfinsupp_lsum, linear_map.range_eq_top], exact function.bijective.surjective h, end /-- If a direct sum of submodules is internal then the submodules are independent. -/ lemma is_internal.submodule_independent (h : is_internal A) : complete_lattice.independent A := complete_lattice.independent_of_dfinsupp_lsum_injective _ h.injective /-- Given an internal direct sum decomposition of a module `M`, and a basis for each of the components of the direct sum, the disjoint union of these bases is a basis for `M`. -/ noncomputable def is_internal.collected_basis (h : is_internal A) {α : ι → Type*} (v : Π i, basis (α i) R (A i)) : basis (Σ i, α i) R M := { repr := (linear_equiv.of_bijective (direct_sum.coe_linear_map A) h.injective h.surjective).symm ≪≫ₗ (dfinsupp.map_range.linear_equiv (λ i, (v i).repr)) ≪≫ₗ (sigma_finsupp_lequiv_dfinsupp R).symm } @[simp] lemma is_internal.collected_basis_coe (h : is_internal A) {α : ι → Type*} (v : Π i, basis (α i) R (A i)) : ⇑(h.collected_basis v) = λ a : Σ i, (α i), ↑(v a.1 a.2) := begin funext a, simp only [is_internal.collected_basis, to_module, coe_linear_map, add_equiv.to_fun_eq_coe, basis.coe_of_repr, basis.repr_symm_apply, dfinsupp.lsum_apply_apply, dfinsupp.map_range.linear_equiv_apply, dfinsupp.map_range.linear_equiv_symm, dfinsupp.map_range_single, finsupp.total_single, linear_equiv.of_bijective_apply, linear_equiv.symm_symm, linear_equiv.symm_trans_apply, one_smul, sigma_finsupp_add_equiv_dfinsupp_apply, sigma_finsupp_equiv_dfinsupp_single, sigma_finsupp_lequiv_dfinsupp_apply], convert dfinsupp.sum_add_hom_single (λ i, (A i).subtype.to_add_monoid_hom) a.1 (v a.1 a.2), end lemma is_internal.collected_basis_mem (h : is_internal A) {α : ι → Type*} (v : Π i, basis (α i) R (A i)) (a : Σ i, α i) : h.collected_basis v a ∈ A a.1 := by simp /-- When indexed by only two distinct elements, `direct_sum.is_internal` implies the two submodules are complementary. Over a `ring R`, this is true as an iff, as `direct_sum.is_internal_iff_is_compl`. -/ lemma is_internal.is_compl {A : ι → submodule R M} {i j : ι} (hij : i ≠ j) (h : (set.univ : set ι) = {i, j}) (hi : is_internal A) : is_compl (A i) (A j) := ⟨hi.submodule_independent.pairwise_disjoint hij, codisjoint_iff.mpr $ eq.symm $ hi.submodule_supr_eq_top.symm.trans $ by rw [←Sup_pair, supr, ←set.image_univ, h, set.image_insert_eq, set.image_singleton]⟩ end semiring section ring variables {R : Type u} [ring R] variables {ι : Type v} [dec_ι : decidable_eq ι] include dec_ι variables {M : Type*} [add_comm_group M] [module R M] /-- Note that this is not generally true for `[semiring R]`; see `complete_lattice.independent.dfinsupp_lsum_injective` for details. -/ lemma is_internal_submodule_of_independent_of_supr_eq_top {A : ι → submodule R M} (hi : complete_lattice.independent A) (hs : supr A = ⊤) : is_internal A := ⟨hi.dfinsupp_lsum_injective, linear_map.range_eq_top.1 $ (submodule.supr_eq_range_dfinsupp_lsum _).symm.trans hs⟩ /-- `iff` version of `direct_sum.is_internal_submodule_of_independent_of_supr_eq_top`, `direct_sum.is_internal.independent`, and `direct_sum.is_internal.supr_eq_top`. -/ lemma is_internal_submodule_iff_independent_and_supr_eq_top (A : ι → submodule R M) : is_internal A ↔ complete_lattice.independent A ∧ supr A = ⊤ := ⟨λ i, ⟨i.submodule_independent, i.submodule_supr_eq_top⟩, and.rec is_internal_submodule_of_independent_of_supr_eq_top⟩ /-- If a collection of submodules has just two indices, `i` and `j`, then `direct_sum.is_internal` is equivalent to `is_compl`. -/ lemma is_internal_submodule_iff_is_compl (A : ι → submodule R M) {i j : ι} (hij : i ≠ j) (h : (set.univ : set ι) = {i, j}) : is_internal A ↔ is_compl (A i) (A j) := begin have : ∀ k, k = i ∨ k = j := λ k, by simpa using set.ext_iff.mp h k, rw [is_internal_submodule_iff_independent_and_supr_eq_top, supr, ←set.image_univ, h, set.image_insert_eq, set.image_singleton, Sup_pair, complete_lattice.independent_pair hij this], exact ⟨λ ⟨hd, ht⟩, ⟨hd, codisjoint_iff.mpr ht⟩, λ ⟨hd, ht⟩, ⟨hd, ht.eq_top⟩⟩, end /-! Now copy the lemmas for subgroup and submonoids. -/ lemma is_internal.add_submonoid_independent {M : Type*} [add_comm_monoid M] {A : ι → add_submonoid M} (h : is_internal A) : complete_lattice.independent A := complete_lattice.independent_of_dfinsupp_sum_add_hom_injective _ h.injective lemma is_internal.add_subgroup_independent {M : Type*} [add_comm_group M] {A : ι → add_subgroup M} (h : is_internal A) : complete_lattice.independent A := complete_lattice.independent_of_dfinsupp_sum_add_hom_injective' _ h.injective end ring end submodule end direct_sum
cb6fb52274730836d54ce166c99a994854ddb74e
649957717d58c43b5d8d200da34bf374293fe739
/src/data/prod.lean
1290edf086685a54bcca8a05dbf033e6be5392c5
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
3,218
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 Extends theory on products -/ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} @[simp] theorem prod.forall {p : α × β → Prop} : (∀ x, p x) ↔ (∀ a b, p (a, b)) := ⟨assume h a b, h (a, b), assume h ⟨a, b⟩, h a b⟩ @[simp] theorem prod.exists {p : α × β → Prop} : (∃ x, p x) ↔ (∃ a b, p (a, b)) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ namespace prod attribute [simp] prod.map @[simp] lemma map_fst (f : α → γ) (g : β → δ) : ∀(p : α × β), (map f g p).1 = f (p.1) | ⟨a, b⟩ := rfl @[simp] lemma map_snd (f : α → γ) (g : β → δ) : ∀(p : α × β), (map f g p).2 = g (p.2) | ⟨a, b⟩ := rfl @[simp] theorem mk.inj_iff {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ (a₁ = a₂ ∧ b₁ = b₂) := ⟨prod.mk.inj, by cc⟩ lemma ext_iff {p q : α × β} : p = q ↔ p.1 = q.1 ∧ p.2 = q.2 := by rw [← @mk.eta _ _ p, ← @mk.eta _ _ q, mk.inj_iff] lemma ext {α β} {p q : α × β} (h₁ : p.1 = q.1) (h₂ : p.2 = q.2) : p = q := ext_iff.2 ⟨h₁, h₂⟩ lemma map_def {f : α → γ} {g : β → δ} : prod.map f g = λ (p : α × β), (f p.1, g p.2) := funext (λ p, ext (map_fst f g p) (map_snd f g p)) lemma id_prod : (λ (p : α × α), (p.1, p.2)) = id := funext $ λ ⟨a, b⟩, rfl /-- Swap the factors of a product. `swap (a, b) = (b, a)` -/ def swap : α × β → β × α := λp, (p.2, p.1) @[simp] lemma swap_swap : ∀ x : α × β, swap (swap x) = x | ⟨a, b⟩ := rfl @[simp] lemma fst_swap {p : α × β} : (swap p).1 = p.2 := rfl @[simp] lemma snd_swap {p : α × β} : (swap p).2 = p.1 := rfl @[simp] lemma swap_prod_mk {a : α} {b : β} : swap (a, b) = (b, a) := rfl @[simp] lemma swap_swap_eq : swap ∘ swap = @id (α × β) := funext swap_swap @[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap := swap_swap @[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap := swap_swap lemma eq_iff_fst_eq_snd_eq : ∀{p q : α × β}, p = q ↔ (p.1 = q.1 ∧ p.2 = q.2) | ⟨p₁, p₂⟩ ⟨q₁, q₂⟩ := by simp theorem lex_def (r : α → α → Prop) (s : β → β → Prop) {p q : α × β} : prod.lex r s p q ↔ r p.1 q.1 ∨ p.1 = q.1 ∧ s p.2 q.2 := ⟨λ h, by cases h; simp *, λ h, match p, q, h with | (a, b), (c, d), or.inl h := lex.left _ _ _ h | (a, b), (c, d), or.inr ⟨e, h⟩ := by change a = c at e; subst e; exact lex.right _ _ h end⟩ instance lex.decidable [decidable_eq α] [decidable_eq β] (r : α → α → Prop) (s : β → β → Prop) [decidable_rel r] [decidable_rel s] : decidable_rel (prod.lex r s) := λ p q, decidable_of_decidable_of_iff (by apply_instance) (lex_def r s).symm end prod open function lemma function.injective_prod {f : α → γ} {g : β → δ} (hf : injective f) (hg : injective g) : injective (λ p : α × β, (f p.1, g p.2)) := assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, by { simp [prod.mk.inj_iff],exact λ ⟨eq₁, eq₂⟩, ⟨hf eq₁, hg eq₂⟩ }
073aaaa9a733189c748e6499f31fbcefc47cae7f
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/dimension.lean
7358edcb5ac169449eaa43720a999d1c3dee1e6b
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,035
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl, Sander Dahmen -/ import linear_algebra.basis import linear_algebra.std_basis import set_theory.cardinal_ordinal /-! # Dimension of modules and vector spaces ## Main definitions * The dimension of a vector space is defined as `vector_space.dim : cardinal`. ## Main statements * `mk_eq_mk_of_basis`: the dimension theorem, any two bases of the same vector space have the same cardinality. * `dim_quotient_add_dim`: if V₁ is a submodule of V, then dim (V/V₁) + dim V₁ = dim V. * `dim_range_add_dim_ker`: the rank-nullity theorem. ## Implementation notes Many theorems in this file are not universe-generic when they relate dimensions in different universes. They should be as general as they can be without inserting `lift`s. The types `V`, `V'`, ... all live in different universes, and `V₁`, `V₂`, ... all live in the same universe. -/ noncomputable theory universes u v v' v'' u₁' w w' variables {K : Type u} {V V₁ V₂ V₃ : Type v} {V' V'₁ : Type v'} {V'' : Type v''} variables {ι : Type w} {ι' : Type w'} {η : Type u₁'} {φ : η → Type*} open_locale classical big_operators section vector_space variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group V₁] [vector_space K V₁] include K open submodule function set variables (K V) /-- the dimension of a vector space, defined as a term of type `cardinal` -/ def vector_space.dim : cardinal := cardinal.min (nonempty_subtype.2 (@exists_is_basis K V _ _ _)) (λ b, cardinal.mk b.1) variables {K V} open vector_space section theorem is_basis.le_span {v : ι → V} {J : set V} (hv : is_basis K v) (hJ : span K J = ⊤) : cardinal.mk (range v) ≤ cardinal.mk J := begin cases le_or_lt cardinal.omega (cardinal.mk J) with oJ oJ, { have := cardinal.mk_range_eq_of_injective (linear_independent.injective hv.1), let S : J → set ι := λ j, ↑(is_basis.repr hv j).support, let S' : J → set V := λ j, v '' S j, have hs : range v ⊆ ⋃ j, S' j, { intros b hb, rcases mem_range.1 hb with ⟨i, hi⟩, have : span K J ≤ comap hv.repr (finsupp.supported K K (⋃ j, S j)) := span_le.2 (λ j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩), rw hJ at this, replace : hv.repr (v i) ∈ (finsupp.supported K K (⋃ j, S j)) := this trivial, rw [hv.repr_eq_single, finsupp.mem_supported, finsupp.support_single_ne_zero one_ne_zero] at this, { subst b, rcases mem_Union.1 (this (finset.mem_singleton_self _)) with ⟨j, hj⟩, exact mem_Union.2 ⟨j, (mem_image _ _ _).2 ⟨i, hj, rfl⟩⟩ }, { apply_instance } }, refine le_of_not_lt (λ IJ, _), suffices : cardinal.mk (⋃ j, S' j) < cardinal.mk (range v), { exact not_le_of_lt this ⟨set.embedding_of_subset _ _ hs⟩ }, refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk (cardinal.sum_le_sum _ (λ _, cardinal.omega) _)) _, { exact λ j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ (finset.finite_to_set _).image _) }, { rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } }, { rcases exists_finite_card_le_of_finite_of_linear_independent_of_span (cardinal.lt_omega_iff_finite.1 oJ) hv.1.to_subtype_range _ with ⟨fI, hi⟩, { rwa [← cardinal.nat_cast_le, cardinal.finset_card, set.finite.coe_to_finset, cardinal.finset_card, set.finite.coe_to_finset] at hi, }, { rw hJ, apply set.subset_univ } }, end end /-- dimension theorem -/ theorem mk_eq_mk_of_basis {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.lift.{w w'} (cardinal.mk ι) = cardinal.lift.{w' w} (cardinal.mk ι') := begin rw ←cardinal.lift_inj.{(max w w') v}, rw [cardinal.lift_lift, cardinal.lift_lift], apply le_antisymm, { convert cardinal.lift_le.{v (max w w')}.2 (hv.le_span hv'.2), { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, }, { convert cardinal.lift_le.{v (max w w')}.2 (hv'.le_span hv.2), { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, } end theorem mk_eq_mk_of_basis' {ι' : Type w} {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.mk ι = cardinal.mk ι' := cardinal.lift_inj.1 $ mk_eq_mk_of_basis hv hv' theorem is_basis.mk_eq_dim'' {ι : Type v} {v : ι → V} (h : is_basis K v) : cardinal.mk ι = dim K V := begin obtain ⟨v', e : dim K V = _⟩ := cardinal.min_eq _ _, rw e, rw ← cardinal.mk_range_eq _ h.injective, exact mk_eq_mk_of_basis' h.range v'.2 end theorem is_basis.mk_range_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.mk (range v) = dim K V := h.range.mk_eq_dim'' theorem is_basis.mk_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (dim K V) := by rw [←h.mk_range_eq_dim, cardinal.mk_range_eq_of_injective h.injective] theorem {m} is_basis.mk_eq_dim' {v : ι → V} (h : is_basis K v) : cardinal.lift.{w (max v m)} (cardinal.mk ι) = cardinal.lift.{v (max w m)} (dim K V) := by simpa using h.mk_eq_dim theorem dim_le {n : ℕ} (H : ∀ s : finset V, linear_independent K (λ i : (↑s : set V), (i : V)) → s.card ≤ n) : dim K V ≤ n := let ⟨b, hb⟩ := exists_is_basis K V in hb.mk_eq_dim'' ▸ cardinal.card_le_of (λ s, @finset.card_map _ _ ⟨_, subtype.val_injective⟩ s ▸ H _ (by { refine hb.1.mono (λ y h, _), rw [finset.mem_coe, finset.mem_map] at h, rcases h with ⟨x, hx, rfl⟩, exact x.2 } )) variables [add_comm_group V'] [vector_space K V'] /-- Two linearly equivalent vector spaces have the same dimension, a version with different universes. -/ theorem linear_equiv.lift_dim_eq (f : V ≃ₗ[K] V') : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V') := let ⟨b, hb⟩ := exists_is_basis K V in calc cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v v'} (cardinal.mk b) : congr_arg _ hb.mk_eq_dim''.symm ... = cardinal.lift.{v' v} (dim K V') : (f.is_basis hb).mk_eq_dim /-- Two linearly equivalent vector spaces have the same dimension. -/ theorem linear_equiv.dim_eq (f : V ≃ₗ[K] V₁) : dim K V = dim K V₁ := cardinal.lift_inj.1 f.lift_dim_eq /-- Two vector spaces are isomorphic if they have the same dimension. -/ theorem nonempty_linear_equiv_of_lift_dim_eq (cond : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V')) : nonempty (V ≃ₗ[K] V') := begin obtain ⟨B, h⟩ := exists_is_basis K V, obtain ⟨B', h'⟩ := exists_is_basis K V', have : cardinal.lift.{v v'} (cardinal.mk B) = cardinal.lift.{v' v} (cardinal.mk B'), by rw [h.mk_eq_dim'', cond, h'.mk_eq_dim''], exact (cardinal.lift_mk_eq.{v v' 0}.1 this).map (linear_equiv_of_is_basis h h') end /-- Two vector spaces are isomorphic if they have the same dimension. -/ theorem nonempty_linear_equiv_of_dim_eq (cond : dim K V = dim K V₁) : nonempty (V ≃ₗ[K] V₁) := nonempty_linear_equiv_of_lift_dim_eq $ congr_arg _ cond section variables (V V' V₁) /-- Two vector spaces are isomorphic if they have the same dimension. -/ def linear_equiv.of_lift_dim_eq (cond : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V')) : V ≃ₗ[K] V' := classical.choice (nonempty_linear_equiv_of_lift_dim_eq cond) /-- Two vector spaces are isomorphic if they have the same dimension. -/ def linear_equiv.of_dim_eq (cond : dim K V = dim K V₁) : V ≃ₗ[K] V₁ := classical.choice (nonempty_linear_equiv_of_dim_eq cond) end /-- Two vector spaces are isomorphic if and only if they have the same dimension. -/ theorem linear_equiv.nonempty_equiv_iff_lift_dim_eq : nonempty (V ≃ₗ[K] V') ↔ cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V') := ⟨λ ⟨h⟩, linear_equiv.lift_dim_eq h, λ h, nonempty_linear_equiv_of_lift_dim_eq h⟩ /-- Two vector spaces are isomorphic if and only if they have the same dimension. -/ theorem linear_equiv.nonempty_equiv_iff_dim_eq : nonempty (V ≃ₗ[K] V₁) ↔ dim K V = dim K V₁ := ⟨λ ⟨h⟩, linear_equiv.dim_eq h, λ h, nonempty_linear_equiv_of_dim_eq h⟩ @[simp] lemma dim_bot : dim K (⊥ : submodule K V) = 0 := by letI := classical.dec_eq V; rw [← cardinal.lift_inj, ← (is_basis_empty (⊥ : submodule K V) not_nonempty_pempty).mk_eq_dim, cardinal.mk_pempty] @[simp] lemma dim_top : dim K (⊤ : submodule K V) = dim K V := linear_equiv.dim_eq (linear_equiv.of_top _ rfl) lemma dim_of_field (K : Type*) [field K] : dim K K = 1 := by rw [←cardinal.lift_inj, ← (@is_basis_singleton_one punit K _ _).mk_eq_dim, cardinal.mk_punit] lemma dim_span {v : ι → V} (hv : linear_independent K v) : dim K ↥(span K (range v)) = cardinal.mk (range v) := by rw [←cardinal.lift_inj, ← (is_basis_span hv).mk_eq_dim, cardinal.mk_range_eq_of_injective (@linear_independent.injective ι K V v _ _ _ _ hv)] lemma dim_span_set {s : set V} (hs : linear_independent K (λ x, x : s → V)) : dim K ↥(span K s) = cardinal.mk s := by { rw [← @set_of_mem_eq _ s, ← subtype.range_coe_subtype], exact dim_span hs } lemma {m} cardinal_lift_le_dim_of_linear_independent {ι : Type w} {v : ι → V} (hv : linear_independent K v) : cardinal.lift.{w (max v m)} (cardinal.mk ι) ≤ cardinal.lift.{v (max w m)} (dim K V) := begin obtain ⟨ι', v', is⟩ := exists_sum_is_basis hv, rw [← cardinal.lift_umax, ← cardinal.lift_umax.{v}], simpa using le_trans (cardinal.lift_mk_le.{w _ (max v m)}.2 ⟨@function.embedding.inl ι ι'⟩) (le_of_eq $ is_basis.mk_eq_dim'.{_ _ _ (max w m)} is), end lemma cardinal_le_dim_of_linear_independent {ι : Type v} {v : ι → V} (hv : linear_independent K v) : (cardinal.mk ι) ≤ (dim.{u v} K V) := by simpa using cardinal_lift_le_dim_of_linear_independent hv lemma cardinal_le_dim_of_linear_independent' {s : set V} (hs : linear_independent K (λ x, x : s → V)) : cardinal.mk s ≤ dim K V := cardinal_le_dim_of_linear_independent hs lemma dim_span_le (s : set V) : dim K (span K s) ≤ cardinal.mk s := begin classical, rcases exists_linear_independent (linear_independent_empty K V) (set.empty_subset s) with ⟨b, hb, _, hsb, hlib⟩, have hsab : span K s = span K b, from span_eq_of_le _ hsb (span_le.2 (λ x hx, subset_span (hb hx))), convert cardinal.mk_le_mk_of_subset hb, rw [hsab, dim_span_set hlib] end lemma dim_span_of_finset (s : finset V) : dim K (span K (↑s : set V)) < cardinal.omega := calc dim K (span K (↑s : set V)) ≤ cardinal.mk (↑s : set V) : dim_span_le ↑s ... = s.card : by rw ←cardinal.finset_card ... < cardinal.omega : cardinal.nat_lt_omega _ theorem dim_prod : dim K (V × V₁) = dim K V + dim K V₁ := begin rcases exists_is_basis K V with ⟨b, hb⟩, rcases exists_is_basis K V₁ with ⟨c, hc⟩, rw [← cardinal.lift_inj, ← @is_basis.mk_eq_dim K (V × V₁) _ _ _ _ _ (is_basis_inl_union_inr hb hc), cardinal.lift_add, cardinal.lift_mk, ← hb.mk_eq_dim, ← hc.mk_eq_dim, cardinal.lift_mk, cardinal.lift_mk, cardinal.add_def (ulift b) (ulift c)], exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2 ⟨equiv.ulift.trans (equiv.sum_congr (@equiv.ulift b) (@equiv.ulift c)).symm ⟩), end theorem dim_quotient_add_dim (p : submodule K V) : dim K p.quotient + dim K p = dim K V := by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq theorem dim_quotient_le (p : submodule K V) : dim K p.quotient ≤ dim K V := by { rw ← dim_quotient_add_dim p, exact self_le_add_right _ _ } /-- rank-nullity theorem -/ theorem dim_range_add_dim_ker (f : V →ₗ[K] V₁) : dim K f.range + dim K f.ker = dim K V := begin haveI := λ (p : submodule K V), classical.dec_eq p.quotient, rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim] end lemma dim_range_le (f : V →ₗ[K] V₁) : dim K f.range ≤ dim K V := by { rw ← dim_range_add_dim_ker f, exact self_le_add_right _ _ } lemma dim_map_le (f : V →ₗ V₁) (p : submodule K V) : dim K (p.map f) ≤ dim K p := begin have h := dim_range_le (f.comp (submodule.subtype p)), rwa [linear_map.range_comp, range_subtype] at h, end lemma dim_range_of_surjective (f : V →ₗ[K] V') (h : surjective f) : dim K f.range = dim K V' := by rw [linear_map.range_eq_top.2 h, dim_top] lemma dim_eq_of_surjective (f : V →ₗ[K] V₁) (h : surjective f) : dim K V = dim K V₁ + dim K f.ker := by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h] lemma dim_le_of_surjective (f : V →ₗ[K] V₁) (h : surjective f) : dim K V₁ ≤ dim K V := by { rw [dim_eq_of_surjective f h], refine self_le_add_right _ _ } lemma dim_eq_of_injective (f : V →ₗ[K] V₁) (h : injective f) : dim K V = dim K f.range := by rw [← dim_range_add_dim_ker f, linear_map.ker_eq_bot.2 h]; simp [dim_bot] lemma dim_submodule_le (s : submodule K V) : dim K s ≤ dim K V := by { rw ← dim_quotient_add_dim s, exact self_le_add_left _ _ } lemma dim_le_of_injective (f : V →ₗ[K] V₁) (h : injective f) : dim K V ≤ dim K V₁ := by { rw [dim_eq_of_injective f h], exact dim_submodule_le _ } lemma dim_le_of_submodule (s t : submodule K V) (h : s ≤ t) : dim K s ≤ dim K t := dim_le_of_injective (of_le h) $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq, subtype.eq $ show x = y, from subtype.ext_iff_val.1 eq lemma linear_independent_le_dim {v : ι → V} (hv : linear_independent K v) : cardinal.lift.{w v} (cardinal.mk ι) ≤ cardinal.lift.{v w} (dim K V) := calc cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (cardinal.mk (set.range v)) : (cardinal.mk_range_eq_of_injective (linear_independent.injective hv)).symm ... = cardinal.lift.{v w} (dim K (submodule.span K (set.range v))) : by rw (dim_span hv).symm ... ≤ cardinal.lift.{v w} (dim K V) : cardinal.lift_le.2 (dim_submodule_le (submodule.span K _)) theorem {u₁} linear_independent_le_dim' {v : ι → V} (hs : linear_independent K v) : ((cardinal.mk ι).lift : cardinal.{(max w v u₁)}) ≤ ((vector_space.dim K V).lift : cardinal.{(max v w u₁)}) := cardinal.mk_range_eq_lift hs.injective ▸ dim_span hs ▸ cardinal.lift_le.2 (dim_submodule_le _) section variables [add_comm_group V₂] [vector_space K V₂] variables [add_comm_group V₃] [vector_space K V₃] open linear_map /-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq`. -/ lemma dim_add_dim_split (db : V₂ →ₗ[K] V) (eb : V₃ →ₗ[K] V) (cd : V₁ →ₗ[K] V₂) (ce : V₁ →ₗ[K] V₃) (hde : ⊤ ≤ db.range ⊔ eb.range) (hgd : ker cd = ⊥) (eq : db.comp cd = eb.comp ce) (eq₂ : ∀d e, db d = eb e → (∃c, cd c = d ∧ ce c = e)) : dim K V + dim K V₁ = dim K V₂ + dim K V₃ := have hf : surjective (coprod db eb), begin refine (range_eq_top.1 $ top_unique $ _), rwa [← map_top, ← prod_top, map_coprod_prod] end, begin conv {to_rhs, rw [← dim_prod, dim_eq_of_surjective _ hf] }, congr' 1, apply linear_equiv.dim_eq, refine linear_equiv.of_bijective _ _ _, { refine cod_restrict _ (prod cd (- ce)) _, { assume c, simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, coprod_apply, neg_neg, map_neg, neg_apply], exact linear_map.ext_iff.1 eq c } }, { rw [ker_cod_restrict, ker_prod, hgd, bot_inf_eq] }, { rw [eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap, map_top, range_subtype], rintros ⟨d, e⟩, have h := eq₂ d (-e), simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, set_like.mem_coe, prod.mk.inj_iff, coprod_apply, map_neg, neg_apply, linear_map.mem_range] at ⊢ h, assume hde, rcases h hde with ⟨c, h₁, h₂⟩, refine ⟨c, h₁, _⟩, rw [h₂, _root_.neg_neg] } end lemma dim_sup_add_dim_inf_eq (s t : submodule K V) : dim K (s ⊔ t : submodule K V) + dim K (s ⊓ t : submodule K V) = dim K s + dim K t := dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right) begin rw [← map_le_map_iff' (ker_subtype $ s ⊔ t), map_sup, map_top, ← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le, range_subtype, range_subtype, range_subtype], exact le_refl _ end (ker_of_le _ _ _) begin ext ⟨x, hx⟩, refl end begin rintros ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ eq, have : b₁ = b₂ := congr_arg subtype.val eq, subst this, exact ⟨⟨b₁, hb₁, hb₂⟩, rfl, rfl⟩ end lemma dim_add_le_dim_add_dim (s t : submodule K V) : dim K (s ⊔ t : submodule K V) ≤ dim K s + dim K t := by { rw [← dim_sup_add_dim_inf_eq], exact self_le_add_right _ _ } end section fintype variable [fintype η] variables [∀i, add_comm_group (φ i)] [∀i, vector_space K (φ i)] open linear_map lemma dim_pi : vector_space.dim K (Πi, φ i) = cardinal.sum (λi, vector_space.dim K (φ i)) := begin choose b hb using assume i, exists_is_basis K (φ i), have : is_basis K (λ (ji : Σ j, b j), std_basis K (λ j, φ j) ji.fst ji.snd.val), by apply pi.is_basis_std_basis _ hb, rw [←cardinal.lift_inj, ← this.mk_eq_dim], simp [λ i, (hb i).mk_range_eq_dim.symm, cardinal.sum_mk] end lemma dim_fun {V η : Type u} [fintype η] [add_comm_group V] [vector_space K V] : vector_space.dim K (η → V) = fintype.card η * vector_space.dim K V := by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card] lemma dim_fun_eq_lift_mul : vector_space.dim K (η → V) = (fintype.card η : cardinal.{max u₁' v}) * cardinal.lift.{v u₁'} (vector_space.dim K V) := by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast] lemma dim_fun' : vector_space.dim K (η → K) = fintype.card η := by rw [dim_fun_eq_lift_mul, dim_of_field K, cardinal.lift_one, mul_one, cardinal.nat_cast_inj] lemma dim_fin_fun (n : ℕ) : dim K (fin n → K) = n := by simp [dim_fun'] end fintype lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s ≠ ⊥) : ∃ b : V, b ∈ s ∧ b ≠ 0 := begin classical, by_contradiction hex, have : ∀x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex }, exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs) end lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : 0 < vector_space.dim K s) : ∃ b : V, b ∈ s ∧ b ≠ 0 := exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [eq, dim_bot] at h; exact lt_irrefl _ h lemma exists_is_basis_fintype (h : dim K V < cardinal.omega) : ∃ s : (set V), (is_basis K (subtype.val : s → V)) ∧ nonempty (fintype s) := begin cases exists_is_basis K V with s hs, rw [←cardinal.lift_lt, ← is_basis.mk_eq_dim hs, cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h, exact ⟨s, hs, h⟩ end section rank /-- `rank f` is the rank of a `linear_map f`, defined as the dimension of `f.range`. -/ def rank (f : V →ₗ[K] V') : cardinal := dim K f.range lemma rank_le_domain (f : V →ₗ[K] V₁) : rank f ≤ dim K V := by { rw [← dim_range_add_dim_ker f], exact self_le_add_right _ _ } lemma rank_le_range (f : V →ₗ[K] V₁) : rank f ≤ dim K V₁ := dim_submodule_le _ lemma rank_add_le (f g : V →ₗ[K] V') : rank (f + g) ≤ rank f + rank g := calc rank (f + g) ≤ dim K (f.range ⊔ g.range : submodule K V') : begin refine dim_le_of_submodule _ _ _, exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $ assume x, show f x + g x ∈ (f.range ⊔ g.range : submodule K V'), from mem_sup.2 ⟨_, mem_image_of_mem _ (mem_univ _), _, mem_image_of_mem _ (mem_univ _), rfl⟩) end ... ≤ rank f + rank g : dim_add_le_dim_add_dim _ _ @[simp] lemma rank_zero : rank (0 : V →ₗ[K] V') = 0 := by rw [rank, linear_map.range_zero, dim_bot] lemma rank_finset_sum_le {η} (s : finset η) (f : η → V →ₗ[K] V') : rank (∑ d in s, f d) ≤ ∑ d in s, rank (f d) := @finset.sum_hom_rel _ _ _ _ _ (λa b, rank a ≤ b) f (λ d, rank (f d)) s (le_of_eq rank_zero) (λ i g c h, le_trans (rank_add_le _ _) (add_le_add_left h _)) variables [add_comm_group V''] [vector_space K V''] lemma rank_comp_le1 (g : V →ₗ[K] V') (f : V' →ₗ[K] V'') : rank (f.comp g) ≤ rank f := begin refine dim_le_of_submodule _ _ _, rw [linear_map.range_comp], exact image_subset _ (subset_univ _) end variables [add_comm_group V'₁] [vector_space K V'₁] lemma rank_comp_le2 (g : V →ₗ[K] V') (f : V' →ₗ V'₁) : rank (f.comp g) ≤ rank g := by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _ end rank lemma dim_zero_iff_forall_zero : vector_space.dim K V = 0 ↔ ∀ x : V, x = 0 := begin split, { intros h x, cases exists_is_basis K V with w hw, have card_mk_range := hw.mk_range_eq_dim, rw [h, cardinal.mk_emptyc_iff, subtype.range_coe] at card_mk_range, simpa [card_mk_range] using hw.mem_span x }, { intro h, have : (⊤ : submodule K V) = ⊥, { ext x, simp [h x] }, rw [←dim_top, this, dim_bot] } end lemma dim_zero_iff : vector_space.dim K V = 0 ↔ subsingleton V := dim_zero_iff_forall_zero.trans (subsingleton_iff_forall_eq 0).symm lemma is_basis_of_dim_eq_zero {ι : Type*} (h : ¬ nonempty ι) (hV : dim K V = 0) : is_basis K (λ x : ι, (0 : V)) := begin haveI : subsingleton V := dim_zero_iff.1 hV, exact is_basis_empty _ h end lemma is_basis_of_dim_eq_zero' (hV : dim K V = 0) : is_basis K (λ x : fin 0, (0 : V)) := is_basis_of_dim_eq_zero (finset.univ_eq_empty.mp rfl) hV lemma dim_pos_iff_exists_ne_zero : 0 < vector_space.dim K V ↔ ∃ x : V, x ≠ 0 := begin rw ←not_iff_not, simpa using dim_zero_iff_forall_zero end lemma dim_pos_iff_nontrivial : 0 < vector_space.dim K V ↔ nontrivial V := dim_pos_iff_exists_ne_zero.trans (nontrivial_iff_exists_ne 0).symm lemma dim_pos [h : nontrivial V] : 0 < vector_space.dim K V := dim_pos_iff_nontrivial.2 h lemma le_dim_iff_exists_linear_independent {c : cardinal} : c ≤ dim K V ↔ ∃ s : set V, cardinal.mk s = c ∧ linear_independent K (coe : s → V) := begin split, { intro h, rcases exists_is_basis K V with ⟨t, ht⟩, rw [← ht.mk_eq_dim'', cardinal.le_mk_iff_exists_subset] at h, rcases h with ⟨s, hst, hsc⟩, exact ⟨s, hsc, ht.1.mono hst⟩ }, { rintro ⟨s, rfl, si⟩, exact cardinal_le_dim_of_linear_independent si } end lemma le_dim_iff_exists_linear_independent_finset {n : ℕ} : ↑n ≤ dim K V ↔ ∃ s : finset V, s.card = n ∧ linear_independent K (coe : (s : set V) → V) := begin simp only [le_dim_iff_exists_linear_independent, cardinal.mk_eq_nat_iff_finset], split, { rintro ⟨s, ⟨t, rfl, rfl⟩, si⟩, exact ⟨t, rfl, si⟩ }, { rintro ⟨s, rfl, si⟩, exact ⟨s, ⟨s, rfl, rfl⟩, si⟩ } end lemma le_rank_iff_exists_linear_independent {c : cardinal} {f : V →ₗ[K] V'} : c ≤ rank f ↔ ∃ s : set V, cardinal.lift.{v v'} (cardinal.mk s) = cardinal.lift.{v' v} c ∧ linear_independent K (λ x : s, f x) := begin rcases f.range_restrict.exists_right_inverse_of_surjective f.range_range_restrict with ⟨g, hg⟩, have fg : left_inverse f.range_restrict g, from linear_map.congr_fun hg, refine ⟨λ h, _, _⟩, { rcases le_dim_iff_exists_linear_independent.1 h with ⟨s, rfl, si⟩, refine ⟨g '' s, cardinal.mk_image_eq_lift _ _ fg.injective, _⟩, replace fg : ∀ x, f (g x) = x, by { intro x, convert congr_arg subtype.val (fg x) }, replace si : linear_independent K (λ x : s, f (g x)), by simpa only [fg] using si.map' _ (ker_subtype _), exact si.image_of_comp s g f }, { rintro ⟨s, hsc, si⟩, have : linear_independent K (λ x : s, f.range_restrict x), from linear_independent.of_comp (f.range.subtype) (by convert si), convert cardinal_le_dim_of_linear_independent this.image, rw [← cardinal.lift_inj, ← hsc, cardinal.mk_image_eq_of_inj_on_lift], exact inj_on_iff_injective.2 this.injective } end lemma le_rank_iff_exists_linear_independent_finset {n : ℕ} {f : V →ₗ[K] V'} : ↑n ≤ rank f ↔ ∃ s : finset V, s.card = n ∧ linear_independent K (λ x : (s : set V), f x) := begin simp only [le_rank_iff_exists_linear_independent, cardinal.lift_nat_cast, cardinal.lift_eq_nat_iff, cardinal.mk_eq_nat_iff_finset], split, { rintro ⟨s, ⟨t, rfl, rfl⟩, si⟩, exact ⟨t, rfl, si⟩ }, { rintro ⟨s, rfl, si⟩, exact ⟨s, ⟨s, rfl, rfl⟩, si⟩ } end /-- A vector space has dimension at most `1` if and only if there is a single vector of which all vectors are multiples. -/ lemma dim_le_one_iff : dim K V ≤ 1 ↔ ∃ v₀ : V, ∀ v, ∃ r : K, r • v₀ = v := begin obtain ⟨b, h⟩ := exists_is_basis K V, split, { intro hd, rw [←is_basis.mk_eq_dim'' h, cardinal.le_one_iff_subsingleton, subsingleton_coe] at hd, rcases eq_empty_or_nonempty b with rfl | ⟨⟨v₀, hv₀⟩⟩, { use 0, have h' : ∀ v : V, v = 0, { simpa [submodule.eq_bot_iff] using h.2.symm }, intro v, simp [h' v] }, { use v₀, have h' : (K ∙ v₀) = ⊤, { simpa [hd.eq_singleton_of_mem hv₀] using h.2 }, intro v, have hv : v ∈ (⊤ : submodule K V) := mem_top, rwa [←h', mem_span_singleton] at hv } }, { rintros ⟨v₀, hv₀⟩, have h : (K ∙ v₀) = ⊤, { ext, simp [mem_span_singleton, hv₀] }, rw [←dim_top, ←h], convert dim_span_le _, simp } end /-- A submodule has dimension at most `1` if and only if there is a single vector in the submodule such that the submodule is contained in its span. -/ lemma dim_submodule_le_one_iff (s : submodule K V) : dim K s ≤ 1 ↔ ∃ v₀ ∈ s, s ≤ K ∙ v₀ := begin simp_rw [dim_le_one_iff, le_span_singleton_iff], split, { rintro ⟨⟨v₀, hv₀⟩, h⟩, use [v₀, hv₀], intros v hv, obtain ⟨r, hr⟩ := h ⟨v, hv⟩, use r, simp_rw [subtype.ext_iff, coe_smul, coe_mk] at hr, exact hr }, { rintro ⟨v₀, hv₀, h⟩, use ⟨v₀, hv₀⟩, rintro ⟨v, hv⟩, obtain ⟨r, hr⟩ := h v hv, use r, simp_rw [subtype.ext_iff, coe_smul, coe_mk], exact hr } end /-- A submodule has dimension at most `1` if and only if there is a single vector, not necessarily in the submodule, such that the submodule is contained in its span. -/ lemma dim_submodule_le_one_iff' (s : submodule K V) : dim K s ≤ 1 ↔ ∃ v₀, s ≤ K ∙ v₀ := begin rw dim_submodule_le_one_iff, split, { rintros ⟨v₀, hv₀, h⟩, exact ⟨v₀, h⟩ }, { rintros ⟨v₀, h⟩, by_cases hw : ∃ w : V, w ∈ s ∧ w ≠ 0, { rcases hw with ⟨w, hw, hw0⟩, use [w, hw], rcases mem_span_singleton.1 (h hw) with ⟨r', rfl⟩, have h0 : r' ≠ 0, { rintro rfl, simpa using hw0 }, rwa span_singleton_smul_eq _ h0 }, { push_neg at hw, rw ←submodule.eq_bot_iff at hw, simp [hw] } } end end vector_space section unconstrained_universes variables {E : Type v'} variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group E] [vector_space K E] open vector_space /-- Version of linear_equiv.dim_eq without universe constraints. -/ theorem linear_equiv.dim_eq_lift (f : V ≃ₗ[K] E) : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K E) := begin cases exists_is_basis K V with b hb, rw [← cardinal.lift_inj.1 hb.mk_eq_dim, ← (f.is_basis hb).mk_eq_dim, cardinal.lift_mk], end end unconstrained_universes
2b2a1662b056503fc77669b86a8c0b6531a24cb5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/forParallel.lean
656a52f8e3807ff04d0b39d8fe6bfb63cdbf121a
[ "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
273
lean
-- set_option trace.Elab true def f (xs : Array Nat) (ys : List (Nat × Nat)) (s : String) : IO Unit := do for x in xs, (y₁, y₂) in ys, c in s do IO.println s!"x: {x}, y₁: {y₁}, y₂: {y₂}, c: {c}" #eval f #[1, 2, 3, 4] [(5, 15), (6, 16), (7, 17)] "hello"
14d88edaea52d5e7110d68fd5a9ac5236ef50a0a
ecdf4e083eb363cd3a0d6880399f86e2cd7f5adb
/src/basic.lean
61b12632ca7d1102feb08cf14bdc40deca62a6e8
[]
no_license
fpvandoorn/formalabstracts
29aa71772da418f18994c38379e2192a6ef361f7
cea2f9f96d89ee1187d1b01e33f22305cdfe4d59
refs/heads/master
1,609,476,761,601
1,558,130,287,000
1,558,130,287,000
97,261,457
0
2
null
1,550,879,230,000
1,500,056,313,000
Lean
UTF-8
Lean
false
false
2,479
lean
/- This file contains various definitions and lemmas which don't fit anywhere else, or when there is not enough material to make its own file -/ import data.pfun data.set.finite data.nat.enat topology.basic import tactic.fattribute universes u v variables {α : Type*} {β : Type*} axiom omitted {P : Prop} : P notation `ℕ∞` := enat def is_finite (α : Type*) : Prop := nonempty (fintype α) -- set.finite (set.univ : set α) noncomputable def roption.classical_to_option {α} (x : roption α) : option α := by haveI := classical.dec; exact x.to_option namespace vector def vector_one_equiv : vector α 1 ≃ α := { to_fun := λ x, x.head, inv_fun := λ x, ⟨[x], dec_trivial⟩, left_inv := omitted, right_inv := omitted} end vector namespace set lemma finite_of_subset_finset {s : set α} (t : finset α) (h : s ⊆ ↑t) : s.finite := finite_subset (finset.finite_to_set t) h /-- The cardinality of any subset of a finite type. -/ noncomputable def cardinality [fintype α] (s : set α) : ℕ := by haveI := classical.prop_decidable; haveI := (set_fintype s); exact fintype.card s end set namespace finset variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] lemma sort_length [decidable_eq α] (s : finset α) : (sort r s).length = s.card := by rw [←list.to_finset_card_of_nodup (sort_nodup r s), sort_to_finset r s] end finset /-- the pullback of a relation along a function -/ def pullback_rel (f : α → β) (r : β → β → Prop) : α → α → Prop := λ x y, r (f x) (f y) namespace pullback_rel instance (f : α → β) (r : β → β → Prop) [is_trans β r] : is_trans α (pullback_rel f r) := ⟨λ x y z h₁ h₂, (trans h₁ h₂ : r (f x) (f z))⟩ protected def is_antisymm (f : α → β) (r : β → β → Prop) (h : function.injective f) [is_antisymm β r] : is_antisymm α (pullback_rel f r) := ⟨λ x y h₁ h₂, h $ antisymm h₁ h₂⟩ instance (f : α → β) (r : β → β → Prop) [is_total β r] : is_total α (pullback_rel f r) := ⟨λ x y, total_of r (f x) (f y)⟩ instance (f : α → β) (r : β → β → Prop) [decidable_rel r] : decidable_rel (pullback_rel f r) := by dsimp [pullback_rel]; apply_instance end pullback_rel def is_maximal {α : Type*} [preorder α] (s : set α) (x : α) : Prop := x ∈ s ∧ ∀(y ∈ s), ¬y > x def is_minimal {α : Type*} [preorder α] (s : set α) (x : α) : Prop := x ∈ s ∧ ∀(y ∈ s), ¬y < x
668d0fdaa838d182a52b529b04530a2a2553ad49
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/monoid/min_max.lean
dc98f8e681c756482e8a249fef9aa86bad3b907b
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,115
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import order.min_max import algebra.order.monoid.lemmas /-! # Lemmas about `min` and `max` in an ordered monoid. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/763 > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α β : Type*} /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ @[to_additive] lemma fn_min_mul_fn_max [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } @[to_additive] lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m section covariant_class_mul_le variables [linear_order α] section has_mul variable [has_mul α] section left variable [covariant_class α α (*) (≤)] @[to_additive] lemma min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] lemma max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm @[to_additive] lemma lt_or_lt_of_mul_lt_mul [covariant_class α α (function.swap (*)) (≤)] {a b m n : α} (h : m * n < a * b) : m < a ∨ n < b := by { contrapose! h, exact mul_le_mul' h.1 h.2 } @[to_additive] lemma mul_lt_mul_iff_of_le_of_le [covariant_class α α (function.swap (*)) (<)] [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] {a b c d : α} (ac : a ≤ c) (bd : b ≤ d) : a * b < c * d ↔ (a < c) ∨ (b < d) := begin refine ⟨lt_or_lt_of_mul_lt_mul, λ h, _⟩, cases h with ha hb, { exact mul_lt_mul_of_lt_of_le ha bd }, { exact mul_lt_mul_of_le_of_lt ac hb } end end left section right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] lemma max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm end right end has_mul variable [mul_one_class α] @[to_additive] lemma min_le_mul_of_one_le_right [covariant_class α α (*) (≤)] {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb @[to_additive] lemma min_le_mul_of_one_le_left [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha @[to_additive] lemma max_le_mul_of_one_le [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ end covariant_class_mul_le
6891d01e4a7ad9981b53485836fa0c456cef6051
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/FVarUtil.lean
0f9f170794a2d35586a062aacd847edb5d4e0cfe
[ "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
7,879
lean
/- Copyright (c) 2022 Henrik Böving. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Henrik Böving -/ import Lean.Expr import Lean.Compiler.LCNF.Basic import Lean.Compiler.LCNF.CompilerM namespace Lean.Compiler.LCNF class TraverseFVar (α : Type) where mapFVarM {m : Type → Type} [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (val : α) : m α forFVarM {m : Type → Type} [Monad m] (f : FVarId → m Unit) (val : α) : m Unit export TraverseFVar (mapFVarM forFVarM) partial def Expr.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (e : Expr) : m Expr := do match e with | .app fn arg => return e.updateApp! (← mapFVarM f fn) (← mapFVarM f arg) | .fvar fvarId => return e.updateFVar! (← f fvarId) | .lam _ ty body _ => return e.updateLambdaE! (← mapFVarM f ty) (← mapFVarM f body) | .forallE _ ty body _ => return e.updateForallE! (← mapFVarM f ty) (← mapFVarM f body) | .bvar .. | .sort .. => return e | .mdata .. | .const .. | .lit .. => return e | .letE .. | .proj .. | .mvar .. => unreachable! -- LCNF types do not have this kind of expr partial def Expr.forFVarM [Monad m] (f : FVarId → m Unit) (e : Expr) : m Unit := do match e with | .app fn arg => forFVarM f fn forFVarM f arg | .fvar fvarId => f fvarId | .lam _ ty body .. => forFVarM f ty forFVarM f body | .forallE _ ty body .. => forFVarM f ty forFVarM f body | .bvar .. | .sort .. => return | .mdata .. | .const .. | .lit .. => return | .mvar .. | .letE .. | .proj .. => unreachable! -- LCNF types do not have this kind of expr instance : TraverseFVar Expr where mapFVarM := Expr.mapFVarM forFVarM := Expr.forFVarM def Arg.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (arg : Arg) : m Arg := do match arg with | .erased => return .erased | .type e => return arg.updateType! (← TraverseFVar.mapFVarM f e) | .fvar fvarId => return arg.updateFVar! (← f fvarId) def Arg.forFVarM [Monad m] (f : FVarId → m Unit) (arg : Arg) : m Unit := do match arg with | .erased => return () | .type e => TraverseFVar.forFVarM f e | .fvar fvarId => f fvarId instance : TraverseFVar Arg where mapFVarM := Arg.mapFVarM forFVarM := Arg.forFVarM def LetValue.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (e : LetValue) : m LetValue := do match e with | .value .. | .erased => return e | .proj _ _ fvarId => return e.updateProj! (← f fvarId) | .const _ _ args => return e.updateArgs! (← args.mapM (TraverseFVar.mapFVarM f)) | .fvar fvarId args => return e.updateFVar! (← f fvarId) (← args.mapM (TraverseFVar.mapFVarM f)) def LetValue.forFVarM [Monad m] (f : FVarId → m Unit) (e : LetValue) : m Unit := do match e with | .value .. | .erased => return () | .proj _ _ fvarId => f fvarId | .const _ _ args => args.forM (TraverseFVar.forFVarM f) | .fvar fvarId args => f fvarId; args.forM (TraverseFVar.forFVarM f) instance : TraverseFVar LetValue where mapFVarM := LetValue.mapFVarM forFVarM := LetValue.forFVarM partial def LetDecl.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (decl : LetDecl) : m LetDecl := do decl.update (← Expr.mapFVarM f decl.type) (← LetValue.mapFVarM f decl.value) partial def LetDecl.forFVarM [Monad m] (f : FVarId → m Unit) (decl : LetDecl) : m Unit := do Expr.forFVarM f decl.type LetValue.forFVarM f decl.value instance : TraverseFVar LetDecl where mapFVarM := LetDecl.mapFVarM forFVarM := LetDecl.forFVarM partial def Param.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (param : Param) : m Param := do param.update (← Expr.mapFVarM f param.type) partial def Param.forFVarM [Monad m] (f : FVarId → m Unit) (param : Param) : m Unit := do Expr.forFVarM f param.type instance : TraverseFVar Param where mapFVarM := Param.mapFVarM forFVarM := Param.forFVarM partial def Code.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (c : Code) : m Code := do match c with | .let decl k => let decl ← LetDecl.mapFVarM f decl return Code.updateLet! c decl (← mapFVarM f k) | .fun decl k => let params ← decl.params.mapM (Param.mapFVarM f) let decl ← decl.update (← Expr.mapFVarM f decl.type) params (← mapFVarM f decl.value) return Code.updateFun! c decl (← mapFVarM f k) | .jp decl k => let params ← decl.params.mapM (Param.mapFVarM f) let decl ← decl.update (← Expr.mapFVarM f decl.type) params (← mapFVarM f decl.value) return Code.updateFun! c decl (← mapFVarM f k) | .cases cs => return Code.updateCases! c (← Expr.mapFVarM f cs.resultType) (← f cs.discr) (← cs.alts.mapM (·.mapCodeM (mapFVarM f))) | .jmp fn args => return Code.updateJmp! c (← f fn) (← args.mapM (Arg.mapFVarM f)) | .return var => return Code.updateReturn! c (← f var) | .unreach typ => return Code.updateUnreach! c (← Expr.mapFVarM f typ) partial def Code.forFVarM [Monad m] (f : FVarId → m Unit) (c : Code) : m Unit := do match c with | .let decl k => LetDecl.forFVarM f decl forFVarM f k | .fun decl k => decl.params.forM (Param.forFVarM f) Expr.forFVarM f decl.type forFVarM f decl.value forFVarM f k | .jp decl k => decl.params.forM (Param.forFVarM f) Expr.forFVarM f decl.type forFVarM f decl.value forFVarM f k | .cases cs => Expr.forFVarM f cs.resultType f cs.discr cs.alts.forM (·.forCodeM (forFVarM f)) | .jmp fn args => f fn args.forM (Arg.forFVarM f) | .return var => f var | .unreach typ => Expr.forFVarM f typ instance : TraverseFVar Code where mapFVarM := Code.mapFVarM forFVarM := Code.forFVarM def FunDecl.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m FVarId) (decl : FunDecl) : m FunDecl := do let params ← decl.params.mapM (Param.mapFVarM f) decl.update (← Expr.mapFVarM f decl.type) params (← Code.mapFVarM f decl.value) def FunDecl.forFVarM [Monad m] (f : FVarId → m Unit) (decl : FunDecl) : m Unit := do decl.params.forM (Param.forFVarM f) Expr.forFVarM f decl.type Code.forFVarM f decl.value instance : TraverseFVar FunDecl where mapFVarM := FunDecl.mapFVarM forFVarM := FunDecl.forFVarM instance : TraverseFVar CodeDecl where mapFVarM f decl := do match decl with | .fun decl => return .fun (← mapFVarM f decl) | .jp decl => return .jp (← mapFVarM f decl) | .let decl => return .let (← mapFVarM f decl) forFVarM f decl := match decl with | .fun decl => forFVarM f decl | .jp decl => forFVarM f decl | .let decl => forFVarM f decl instance : TraverseFVar Alt where mapFVarM f alt := do match alt with | .alt ctor params c => let params ← params.mapM (Param.mapFVarM f) return .alt ctor params (← Code.mapFVarM f c) | .default c => return .default (← Code.mapFVarM f c) forFVarM f alt := do match alt with | .alt _ params c => params.forM (Param.forFVarM f) Code.forFVarM f c | .default c => Code.forFVarM f c def anyFVarM [Monad m] [TraverseFVar α] (f : FVarId → m Bool) (x : α) : m Bool := do return (← TraverseFVar.forFVarM go x |>.run) matches none where go (fvar : FVarId) : OptionT m Unit := do if (← f fvar) then failure def allFVarM [Monad m] [TraverseFVar α] (f : FVarId → m Bool) (x : α) : m Bool := do return (← TraverseFVar.forFVarM go x |>.run) matches .some .. where go (fvar : FVarId) : OptionT m Unit := do if !(← f fvar) then failure def anyFVar [TraverseFVar α] (f : FVarId → Bool) (x : α) : Bool := Id.run <| anyFVarM f x def allFVar [TraverseFVar α] (f : FVarId → Bool) (x : α) : Bool := Id.run <| allFVarM f x end Lean.Compiler.LCNF
12501b558a2db7a91e68e368e28dcb26f9f1724e
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/tests/lean/run/check_constants.lean
3b55e55aed80fd076b37dccc05703b8e00ebf20e
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
10,922
lean
-- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py import smt system.io open tactic meta def script_check_id (n : name) : tactic unit := do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute") run_cmd script_check_id `absurd run_cmd script_check_id `acc.cases_on run_cmd script_check_id `acc.rec run_cmd script_check_id `and run_cmd script_check_id `and.cases_on run_cmd script_check_id `and.elim_left run_cmd script_check_id `and.elim_right run_cmd script_check_id `and.intro run_cmd script_check_id `and.rec run_cmd script_check_id `auto_param run_cmd script_check_id `bin_tree.empty run_cmd script_check_id `bin_tree.leaf run_cmd script_check_id `bin_tree.node run_cmd script_check_id `bit0 run_cmd script_check_id `bit1 run_cmd script_check_id `bool run_cmd script_check_id `bool.ff run_cmd script_check_id `bool.tt run_cmd script_check_id `cast run_cmd script_check_id `cast_heq run_cmd script_check_id `char run_cmd script_check_id `char.mk run_cmd script_check_id `char.ne_of_vne run_cmd script_check_id `char.of_nat run_cmd script_check_id `char.of_nat_ne_of_ne run_cmd script_check_id `coe run_cmd script_check_id `coe_fn run_cmd script_check_id `coe_sort run_cmd script_check_id `coe_to_lift run_cmd script_check_id `combinator.K run_cmd script_check_id `congr run_cmd script_check_id `congr_arg run_cmd script_check_id `congr_fun run_cmd script_check_id `decidable run_cmd script_check_id `decidable.to_bool run_cmd script_check_id `dite run_cmd script_check_id `empty run_cmd script_check_id `eq run_cmd script_check_id `eq.cases_on run_cmd script_check_id `eq.drec run_cmd script_check_id `eq.mp run_cmd script_check_id `eq.mpr run_cmd script_check_id `eq.rec run_cmd script_check_id `eq.refl run_cmd script_check_id `eq.subst run_cmd script_check_id `eq.symm run_cmd script_check_id `eq.trans run_cmd script_check_id `eq_false_intro run_cmd script_check_id `eq_of_heq run_cmd script_check_id `eq_rec_heq run_cmd script_check_id `eq_self_iff_true run_cmd script_check_id `eq_true_intro run_cmd script_check_id `Exists run_cmd script_check_id `expr run_cmd script_check_id `expr.subst run_cmd script_check_id `false run_cmd script_check_id `false.rec run_cmd script_check_id `false_of_true_eq_false run_cmd script_check_id `fin.mk run_cmd script_check_id `fin.ne_of_vne run_cmd script_check_id `forall_congr run_cmd script_check_id `forall_congr_eq run_cmd script_check_id `forall_not_of_not_exists run_cmd script_check_id `format run_cmd script_check_id `funext run_cmd script_check_id `has_add run_cmd script_check_id `has_add.add run_cmd script_check_id `has_andthen.andthen run_cmd script_check_id `has_bind.and_then run_cmd script_check_id `has_bind.seq run_cmd script_check_id `has_coe_t run_cmd script_check_id `has_div.div run_cmd script_check_id `has_emptyc.emptyc run_cmd script_check_id `has_insert.insert run_cmd script_check_id `has_neg.neg run_cmd script_check_id `has_one run_cmd script_check_id `has_one.one run_cmd script_check_id `has_orelse.orelse run_cmd script_check_id `has_repr run_cmd script_check_id `has_sep.sep run_cmd script_check_id `has_singleton.singleton run_cmd script_check_id `has_sizeof run_cmd script_check_id `has_sizeof.mk run_cmd script_check_id `has_sub.sub run_cmd script_check_id `has_to_format run_cmd script_check_id `has_well_founded run_cmd script_check_id `has_well_founded.r run_cmd script_check_id `has_well_founded.wf run_cmd script_check_id `has_zero run_cmd script_check_id `has_zero.zero run_cmd script_check_id `heq run_cmd script_check_id `heq.refl run_cmd script_check_id `heq.symm run_cmd script_check_id `heq.trans run_cmd script_check_id `heq_of_eq run_cmd script_check_id `hole_command run_cmd script_check_id `id run_cmd script_check_id `id_delta run_cmd script_check_id `id_rhs run_cmd script_check_id `if_neg run_cmd script_check_id `if_pos run_cmd script_check_id `iff run_cmd script_check_id `iff.intro run_cmd script_check_id `iff.mp run_cmd script_check_id `iff.mpr run_cmd script_check_id `iff.refl run_cmd script_check_id `iff.symm run_cmd script_check_id `iff.trans run_cmd script_check_id `iff_false_intro run_cmd script_check_id `iff_true_intro run_cmd script_check_id `imp_congr run_cmd script_check_id `imp_congr_ctx run_cmd script_check_id `imp_congr_ctx_eq run_cmd script_check_id `imp_congr_eq run_cmd script_check_id `implies run_cmd script_check_id `implies_of_if_neg run_cmd script_check_id `implies_of_if_pos run_cmd script_check_id `int run_cmd script_check_id `int.bit0_nonneg run_cmd script_check_id `int.bit0_pos run_cmd script_check_id `int.bit1_nonneg run_cmd script_check_id `int.bit1_pos run_cmd script_check_id `int.nat_abs_bit0_step run_cmd script_check_id `int.nat_abs_bit1_nonneg_step run_cmd script_check_id `int.nat_abs_one run_cmd script_check_id `int.nat_abs_zero run_cmd script_check_id `int.ne_neg_of_ne run_cmd script_check_id `int.ne_neg_of_pos run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case run_cmd script_check_id `int.neg_ne_of_pos run_cmd script_check_id `int.neg_ne_zero_of_ne run_cmd script_check_id `int.one_nonneg run_cmd script_check_id `int.one_pos run_cmd script_check_id `int.zero_ne_neg_of_ne run_cmd script_check_id `int.zero_nonneg run_cmd script_check_id `interactive.executor run_cmd script_check_id `interactive.param_desc run_cmd script_check_id `interactive.parse run_cmd script_check_id `io run_cmd script_check_id `io_core run_cmd script_check_id `io_rand_nat run_cmd script_check_id `is_associative run_cmd script_check_id `is_associative.assoc run_cmd script_check_id `is_commutative run_cmd script_check_id `is_commutative.comm run_cmd script_check_id `is_valid_char_range_1 run_cmd script_check_id `is_valid_char_range_2 run_cmd script_check_id `ite run_cmd script_check_id `lean.parser run_cmd script_check_id `lean.parser.pexpr run_cmd script_check_id `lean.parser.reflectable.expr run_cmd script_check_id `lean.parser.tk run_cmd script_check_id `left_comm run_cmd script_check_id `list run_cmd script_check_id `list.cons run_cmd script_check_id `list.nil run_cmd script_check_id `match_failed run_cmd script_check_id `monad run_cmd script_check_id `monad_fail run_cmd script_check_id `monad_from_pure_bind run_cmd script_check_id `monad_io_environment_impl run_cmd script_check_id `monad_io_file_system_impl run_cmd script_check_id `monad_io_impl run_cmd script_check_id `monad_io_net_system_impl run_cmd script_check_id `monad_io_process_impl run_cmd script_check_id `monad_io_random_impl run_cmd script_check_id `monad_io_terminal_impl run_cmd script_check_id `name.anonymous run_cmd script_check_id `name.mk_numeral run_cmd script_check_id `name.mk_string run_cmd script_check_id `nat run_cmd script_check_id `nat.add run_cmd script_check_id `nat.bit0_lt run_cmd script_check_id `nat.bit0_lt_bit1 run_cmd script_check_id `nat.bit0_ne run_cmd script_check_id `nat.bit0_ne_bit1 run_cmd script_check_id `nat.bit0_ne_one run_cmd script_check_id `nat.bit0_ne_zero run_cmd script_check_id `nat.bit1_lt run_cmd script_check_id `nat.bit1_lt_bit0 run_cmd script_check_id `nat.bit1_ne run_cmd script_check_id `nat.bit1_ne_bit0 run_cmd script_check_id `nat.bit1_ne_one run_cmd script_check_id `nat.bit1_ne_zero run_cmd script_check_id `nat.cases_on run_cmd script_check_id `nat.has_add run_cmd script_check_id `nat.has_one run_cmd script_check_id `nat.has_zero run_cmd script_check_id `nat.le_of_lt run_cmd script_check_id `nat.le_refl run_cmd script_check_id `nat.one_lt_bit0 run_cmd script_check_id `nat.one_lt_bit1 run_cmd script_check_id `nat.one_ne_bit0 run_cmd script_check_id `nat.one_ne_bit1 run_cmd script_check_id `nat.one_ne_zero run_cmd script_check_id `nat.succ run_cmd script_check_id `nat.zero run_cmd script_check_id `nat.zero_lt_bit0 run_cmd script_check_id `nat.zero_lt_bit1 run_cmd script_check_id `nat.zero_lt_one run_cmd script_check_id `nat.zero_ne_bit0 run_cmd script_check_id `nat.zero_ne_bit1 run_cmd script_check_id `nat.zero_ne_one run_cmd script_check_id `ne run_cmd script_check_id `neq_of_not_iff run_cmd script_check_id `not run_cmd script_check_id `not_of_eq_false run_cmd script_check_id `of_eq_true run_cmd script_check_id `opt_param run_cmd script_check_id `or run_cmd script_check_id `out_param run_cmd script_check_id `pprod run_cmd script_check_id `pprod.fst run_cmd script_check_id `pprod.mk run_cmd script_check_id `pprod.snd run_cmd script_check_id `prod.mk run_cmd script_check_id `propext run_cmd script_check_id `psigma run_cmd script_check_id `psigma.cases_on run_cmd script_check_id `psigma.fst run_cmd script_check_id `psigma.mk run_cmd script_check_id `psigma.snd run_cmd script_check_id `psum run_cmd script_check_id `psum.cases_on run_cmd script_check_id `psum.inl run_cmd script_check_id `psum.inr run_cmd script_check_id `punit run_cmd script_check_id `punit.cases_on run_cmd script_check_id `punit.star run_cmd script_check_id `quot.lift run_cmd script_check_id `quot.mk run_cmd script_check_id `reflected run_cmd script_check_id `reflected.subst run_cmd script_check_id `repr run_cmd script_check_id `rfl run_cmd script_check_id `scope_trace run_cmd script_check_id `set_of run_cmd script_check_id `sizeof run_cmd script_check_id `string run_cmd script_check_id `string.empty run_cmd script_check_id `string.empty_ne_str run_cmd script_check_id `string.str run_cmd script_check_id `string.str_ne_empty run_cmd script_check_id `string.str_ne_str_left run_cmd script_check_id `string.str_ne_str_right run_cmd script_check_id `subsingleton run_cmd script_check_id `subsingleton.elim run_cmd script_check_id `subsingleton.helim run_cmd script_check_id `subtype run_cmd script_check_id `subtype.mk run_cmd script_check_id `subtype.rec run_cmd script_check_id `subtype.val run_cmd script_check_id `tactic run_cmd script_check_id `tactic.mk_inj_eq run_cmd script_check_id `tactic.triv run_cmd script_check_id `tactic.try run_cmd script_check_id `thunk run_cmd script_check_id `to_fmt run_cmd script_check_id `to_pexpr run_cmd script_check_id `trans_rel_left run_cmd script_check_id `trans_rel_right run_cmd script_check_id `true run_cmd script_check_id `true.intro run_cmd script_check_id `true_eq_false_of_false run_cmd script_check_id `unification_hint run_cmd script_check_id `unification_hint.mk run_cmd script_check_id `unit run_cmd script_check_id `unit.star run_cmd script_check_id `user_attribute run_cmd script_check_id `user_attribute.parse_reflect run_cmd script_check_id `vm_monitor run_cmd script_check_id `well_founded.fix run_cmd script_check_id `well_founded.fix_eq run_cmd script_check_id `well_founded_tactics run_cmd script_check_id `well_founded_tactics.dec_tac run_cmd script_check_id `well_founded_tactics.default run_cmd script_check_id `well_founded_tactics.rel_tac run_cmd script_check_id `widget.term_goal_widget
76af8a7eb0622e8b2650c1e88469d50996b476f3
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/countable/defs.lean
d845f635ef8d0f9e769b0325c41e1b3f72e16abe
[ "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
3,248
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 data.finite.defs /-! # Countable types In this file we define a typeclass saying that a given `Sort*` is countable. See also `encodable` for a version that singles out a specific encoding of elements of `α` by natural numbers. This file also provides a few instances of this typeclass. More instances can be found in other files. -/ open function universes u v variables {α : Sort u} {β : Sort v} /-! ### Definition and basic properties -/ /-- A type `α` is countable if there exists an injective map `α → ℕ`. -/ @[mk_iff countable_iff_exists_injective] class countable (α : Sort u) : Prop := (exists_injective_nat [] : ∃ f : α → ℕ, injective f) instance : countable ℕ := ⟨⟨id, injective_id⟩⟩ export countable (exists_injective_nat) protected lemma function.injective.countable [countable β] {f : α → β} (hf : injective f) : countable α := let ⟨g, hg⟩ := exists_injective_nat β in ⟨⟨g ∘ f, hg.comp hf⟩⟩ protected lemma function.surjective.countable [countable α] {f : α → β} (hf : surjective f) : countable β := (injective_surj_inv hf).countable lemma exists_surjective_nat (α : Sort u) [nonempty α] [countable α] : ∃ f : ℕ → α, surjective f := let ⟨f, hf⟩ := exists_injective_nat α in ⟨inv_fun f, inv_fun_surjective hf⟩ lemma countable_iff_exists_surjective [nonempty α] : countable α ↔ ∃ f : ℕ → α, surjective f := ⟨@exists_surjective_nat _ _, λ ⟨f, hf⟩, hf.countable⟩ lemma countable.of_equiv (α : Sort*) [countable α] (e : α ≃ β) : countable β := e.symm.injective.countable lemma equiv.countable_iff (e : α ≃ β) : countable α ↔ countable β := ⟨λ h, @countable.of_equiv _ _ h e, λ h, @countable.of_equiv _ _ h e.symm⟩ instance {β : Type v} [countable β] : countable (ulift.{u} β) := countable.of_equiv _ equiv.ulift.symm /-! ### Operations on `Sort*`s -/ instance [countable α] : countable (plift α) := equiv.plift.injective.countable @[priority 100] instance subsingleton.to_countable [subsingleton α] : countable α := ⟨⟨λ _, 0, λ x y h, subsingleton.elim x y⟩⟩ @[priority 500] instance [countable α] {p : α → Prop} : countable {x // p x} := subtype.val_injective.countable instance {n : ℕ} : countable (fin n) := function.injective.countable (@fin.eq_of_veq n) @[priority 100] instance finite.to_countable [finite α] : countable α := let ⟨n, ⟨e⟩⟩ := finite.exists_equiv_fin α in countable.of_equiv _ e.symm instance : countable punit.{u} := subsingleton.to_countable @[nolint instance_priority] instance Prop.countable (p : Prop) : countable p := subsingleton.to_countable instance bool.countable : countable bool := ⟨⟨λ b, cond b 0 1, bool.injective_iff.2 nat.one_ne_zero⟩⟩ instance Prop.countable' : countable Prop := countable.of_equiv bool equiv.Prop_equiv_bool.symm @[priority 500] instance [countable α] {r : α → α → Prop} : countable (quot r) := (surjective_quot_mk r).countable @[priority 500] instance [countable α] {s : setoid α} : countable (quotient s) := quot.countable
567539ec620e00aba91aae5d6639b7af73f13270
ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f
/qp/p1_categories/c7_cat_of_cats/s2_conduche.lean
18afce9e4fe240c802bcacdcb0df14e4e6a7fb0b
[]
no_license
intoverflow/qvr
34b9ef23604738381ca20b7d622fd0399d88f2dd
0cfcd33fe4bf8d93851a00cec5bfd21e77105d74
refs/heads/master
1,616,591,570,371
1,492,575,772,000
1,492,575,772,000
80,061,627
0
0
null
null
null
null
UTF-8
Lean
false
false
2,555
lean
/- ----------------------------------------------------------------------- Fibrations. ----------------------------------------------------------------------- -/ import ..c1_basic namespace qp open stdaux universe variables ℓobjb ℓhomb ℓobj ℓhom /- ----------------------------------------------------------------------- Conduché functors. ----------------------------------------------------------------------- -/ /-! #brief A Conduché factorization. -/ structure ConducheFact {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}} (P : Fun E B) {a c : E^.obj} (f : E^.hom a c) {b : B^.obj} (h : B^.hom b (P^.obj c)) (g : B^.hom (P^.obj a) b) (ω : P^.hom f = h ∘∘ g) := (obj : E^.obj) (obj_im : P^.obj obj = b) (left : E^.hom obj c) (left_im : P^.hom left = h ∘∘ cast_hom obj_im) (right : E^.hom a obj) (right_im : P^.hom right = cast_hom (eq.symm obj_im) ∘∘ g) (comm : f = left ∘∘ right) /-! #brief A Conduché functor. -/ structure Conduche {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}} (P : Fun E B) := (factorize : ∀ {a c : E^.obj} (f : E^.hom a c) {b : B^.obj} (h : B^.hom b (P^.obj c)) (g : B^.hom (P^.obj a) b) (ω : P^.hom f = h ∘∘ g) , ConducheFact P f h g ω) (zigzag : ∀ {a c : E^.obj} {f : E^.hom a c} {b : B^.obj} {h : B^.hom b (P^.obj c)} {g : B^.hom (P^.obj a) b} {ω : P^.hom f = h ∘∘ g} (fac₁ fac₂ : ConducheFact P f h g ω) , E^.hom fac₁^.obj fac₂^.obj) (zigzag_im : ∀ {a c : E^.obj} {f : E^.hom a c} {b : B^.obj} {h : B^.hom b (P^.obj c)} {g : B^.hom (P^.obj a) b} {ω : P^.hom f = h ∘∘ g} (fac₁ fac₂ : ConducheFact P f h g ω) , P^.hom (zigzag fac₁ fac₂) = cast_hom (eq.trans fac₁^.obj_im (eq.symm fac₂^.obj_im))) (zigzag_left : ∀ {a c : E^.obj} {f : E^.hom a c} {b : B^.obj} {h : B^.hom b (P^.obj c)} {g : B^.hom (P^.obj a) b} {ω : P^.hom f = h ∘∘ g} (fac₁ fac₂ : ConducheFact P f h g ω) , fac₁^.left = fac₂^.left ∘∘ zigzag fac₁ fac₂) (zigzag_right : ∀ {a c : E^.obj} {f : E^.hom a c} {b : B^.obj} {h : B^.hom b (P^.obj c)} {g : B^.hom (P^.obj a) b} {ω : P^.hom f = h ∘∘ g} (fac₁ fac₂ : ConducheFact P f h g ω) , fac₂^.right = zigzag fac₁ fac₂ ∘∘ fac₁^.right) end qp
a240fd265049cf0cc9beef2693f4b1a6d8f16318
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/analysis/special_functions/trigonometric/complex.lean
59fe2ef0faf75ac009196cc97e7200332763d5fb
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,922
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import algebra.quadratic_discriminant import analysis.special_functions.complex.log /-! # Complex trigonometric functions Basic facts and derivatives for the complex trigonometric functions. -/ noncomputable theory namespace complex open set filter open_locale real theorem cos_eq_zero_iff {θ : ℂ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := begin have h : (exp (θ * I) + exp (-θ * I)) / 2 = 0 ↔ exp (2 * θ * I) = -1, { rw [@div_eq_iff _ _ (exp (θ * I) + exp (-θ * I)) 2 0 two_ne_zero', zero_mul, add_eq_zero_iff_eq_neg, neg_eq_neg_one_mul, ← div_eq_iff (exp_ne_zero _), ← exp_sub], field_simp only, congr' 3, ring }, rw [cos, h, ← exp_pi_mul_I, exp_eq_exp_iff_exists_int, mul_right_comm], refine exists_congr (λ x, _), refine (iff_of_eq $ congr_arg _ _).trans (mul_right_inj' $ mul_ne_zero two_ne_zero' I_ne_zero), ring, end theorem cos_ne_zero_iff {θ : ℂ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] theorem sin_eq_zero_iff {θ : ℂ} : sin θ = 0 ↔ ∃ k : ℤ, θ = k * π := begin rw [← complex.cos_sub_pi_div_two, cos_eq_zero_iff], split, { rintros ⟨k, hk⟩, use k + 1, field_simp [eq_add_of_sub_eq hk], ring }, { rintros ⟨k, rfl⟩, use k - 1, field_simp, ring } end theorem sin_ne_zero_iff {θ : ℂ} : sin θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π := by rw [← not_exists, not_iff_not, sin_eq_zero_iff] lemma tan_eq_zero_iff {θ : ℂ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 := begin have h := (sin_two_mul θ).symm, rw mul_assoc at h, rw [tan, div_eq_zero_iff, ← mul_eq_zero, ← zero_mul ((1/2):ℂ), mul_one_div, cancel_factors.cancel_factors_eq_div h two_ne_zero', mul_comm], simpa only [zero_div, zero_mul, ne.def, not_false_iff] with field_simps using sin_eq_zero_iff, end lemma tan_ne_zero_iff {θ : ℂ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 := by rw [← not_exists, not_iff_not, tan_eq_zero_iff] lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 := tan_eq_zero_iff.mpr (by use n) lemma cos_eq_cos_iff {x y : ℂ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := calc cos x = cos y ↔ cos x - cos y = 0 : sub_eq_zero.symm ... ↔ -2 * sin((x + y)/2) * sin((x - y)/2) = 0 : by rw cos_sub_cos ... ↔ sin((x + y)/2) = 0 ∨ sin((x - y)/2) = 0 : by simp [(by norm_num : (2:ℂ) ≠ 0)] ... ↔ sin((x - y)/2) = 0 ∨ sin((x + y)/2) = 0 : or.comm ... ↔ (∃ k : ℤ, y = 2 * k * π + x) ∨ (∃ k :ℤ, y = 2 * k * π - x) : begin apply or_congr; field_simp [sin_eq_zero_iff, (by norm_num : -(2:ℂ) ≠ 0), eq_sub_iff_add_eq', sub_eq_iff_eq_add, mul_comm (2:ℂ), mul_right_comm _ (2:ℂ)], split; { rintros ⟨k, rfl⟩, use -k, simp, }, end ... ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x : exists_or_distrib.symm lemma sin_eq_sin_iff {x y : ℂ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := begin simp only [← complex.cos_sub_pi_div_two, cos_eq_cos_iff, sub_eq_iff_eq_add], refine exists_congr (λ k, or_congr _ _); refine eq.congr rfl _; field_simp; ring end lemma tan_add {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := begin rcases h with ⟨h1, h2⟩ | ⟨⟨k, rfl⟩, ⟨l, rfl⟩⟩, { rw [tan, sin_add, cos_add, ← div_div_div_cancel_right (sin x * cos y + cos x * sin y) (mul_ne_zero (cos_ne_zero_iff.mpr h1) (cos_ne_zero_iff.mpr h2)), add_div, sub_div], simp only [←div_mul_div, ←tan, mul_one, one_mul, div_self (cos_ne_zero_iff.mpr h1), div_self (cos_ne_zero_iff.mpr h2)] }, { obtain ⟨t, hx, hy, hxy⟩ := ⟨tan_int_mul_pi_div_two, t (2*k+1), t (2*l+1), t (2*k+1+(2*l+1))⟩, simp only [int.cast_add, int.cast_bit0, int.cast_mul, int.cast_one, hx, hy] at hx hy hxy, rw [hx, hy, add_zero, zero_div, mul_div_assoc, mul_div_assoc, ← add_mul (2*(k:ℂ)+1) (2*l+1) (π/2), ← mul_div_assoc, hxy] }, end lemma tan_add' {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := tan_add (or.inl h) lemma tan_two_mul {z : ℂ} : tan (2 * z) = 2 * tan z / (1 - tan z ^ 2) := begin by_cases h : ∀ k : ℤ, z ≠ (2 * k + 1) * π / 2, { rw [two_mul, two_mul, sq, tan_add (or.inl ⟨h, h⟩)] }, { rw not_forall_not at h, rw [two_mul, two_mul, sq, tan_add (or.inr ⟨h, h⟩)] }, end lemma tan_add_mul_I {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y * I = (2 * l + 1) * π / 2)) : tan (x + y*I) = (tan x + tanh y * I) / (1 - tan x * tanh y * I) := by rw [tan_add h, tan_mul_I, mul_assoc] lemma tan_eq {z : ℂ} (h : ((∀ k : ℤ, (z.re:ℂ) ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, (z.im:ℂ) * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, (z.re:ℂ) = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, (z.im:ℂ) * I = (2 * l + 1) * π / 2)) : tan z = (tan z.re + tanh z.im * I) / (1 - tan z.re * tanh z.im * I) := by convert tan_add_mul_I h; exact (re_add_im z).symm lemma has_strict_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) : has_strict_deriv_at tan (1 / (cos x)^2) x := begin convert (has_strict_deriv_at_sin x).div (has_strict_deriv_at_cos x) h, rw ← sin_sq_add_cos_sq x, ring, end lemma has_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) : has_deriv_at tan (1 / (cos x)^2) x := (has_strict_deriv_at_tan h).has_deriv_at open_locale topological_space lemma tendsto_abs_tan_of_cos_eq_zero {x : ℂ} (hx : cos x = 0) : tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top := begin simp only [tan_eq_sin_div_cos, ← norm_eq_abs, normed_field.norm_div], have A : sin x ≠ 0 := λ h, by simpa [*, sq] using sin_sq_add_cos_sq x, have B : tendsto cos (𝓝[{x}ᶜ] (x)) (𝓝[{0}ᶜ] 0), { refine tendsto_inf.2 ⟨tendsto.mono_left _ inf_le_left, tendsto_principal.2 _⟩, exacts [continuous_cos.tendsto' x 0 hx, hx ▸ (has_deriv_at_cos _).eventually_ne (neg_ne_zero.2 A)] }, exact continuous_sin.continuous_within_at.norm.mul_at_top (norm_pos_iff.2 A) (tendsto_norm_nhds_within_zero.comp B).inv_tendsto_zero, end lemma tendsto_abs_tan_at_top (k : ℤ) : tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top := tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩ @[simp] lemma continuous_at_tan {x : ℂ} : continuous_at tan x ↔ cos x ≠ 0 := begin refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩, exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _ (hc.norm.tendsto.mono_left inf_le_left) end @[simp] lemma differentiable_at_tan {x : ℂ} : differentiable_at ℂ tan x ↔ cos x ≠ 0:= ⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩ @[simp] lemma deriv_tan (x : ℂ) : deriv tan x = 1 / (cos x)^2 := if h : cos x = 0 then have ¬differentiable_at ℂ tan x := mt differentiable_at_tan.1 (not_not.2 h), by simp [deriv_zero_of_not_differentiable_at this, h, sq] else (has_deriv_at_tan h).deriv lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} := continuous_on_sin.div continuous_on_cos $ λ x, id @[continuity] lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) := continuous_on_iff_continuous_restrict.1 continuous_on_tan @[simp] lemma times_cont_diff_at_tan {x : ℂ} {n : with_top ℕ} : times_cont_diff_at ℂ n tan x ↔ cos x ≠ 0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, times_cont_diff_sin.times_cont_diff_at.div times_cont_diff_cos.times_cont_diff_at⟩ lemma cos_eq_iff_quadratic {z w : ℂ} : cos z = w ↔ (exp (z * I)) ^ 2 - 2 * w * exp (z * I) + 1 = 0 := begin rw ← sub_eq_zero, field_simp [cos, exp_neg, exp_ne_zero], refine eq.congr _ rfl, ring end lemma cos_surjective : function.surjective cos := begin intro x, obtain ⟨w, w₀, hw⟩ : ∃ w ≠ 0, 1 * w * w + (-2 * x) * w + 1 = 0, { rcases exists_quadratic_eq_zero (@one_ne_zero ℂ _ _) (exists_eq_mul_self _) with ⟨w, hw⟩, refine ⟨w, _, hw⟩, rintro rfl, simpa only [zero_add, one_ne_zero, mul_zero] using hw }, refine ⟨log w / I, cos_eq_iff_quadratic.2 _⟩, rw [div_mul_cancel _ I_ne_zero, exp_log w₀], convert hw, ring end @[simp] lemma range_cos : range cos = set.univ := cos_surjective.range_eq lemma sin_surjective : function.surjective sin := begin intro x, rcases cos_surjective x with ⟨z, rfl⟩, exact ⟨z + π / 2, sin_add_pi_div_two z⟩ end @[simp] lemma range_sin : range sin = set.univ := sin_surjective.range_eq end complex namespace real open_locale real theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := by exact_mod_cast @complex.cos_eq_zero_iff θ theorem cos_ne_zero_iff {θ : ℝ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] lemma cos_eq_cos_iff {x y : ℝ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := by exact_mod_cast @complex.cos_eq_cos_iff x y lemma sin_eq_sin_iff {x y : ℝ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := by exact_mod_cast @complex.sin_eq_sin_iff x y end real
f1f60043755e55a85bbcd5439637112ea4a72511
f68ef9a599ec5575db7b285d4960e63c5d464ccc
/Exercises/Lista 3/PHP-LucasMoschen.lean
da486244a364852080f697b58dc3a82e3feb7273
[]
no_license
lucasmoschen/discrete-mathematics
a38d5970cc571b0b9d202bf6a43efeb8ed6f66e3
0f1945cc5eb094814c926cd6ae4a8b4c5c579a1e
refs/heads/master
1,677,111,757,003
1,611,500,097,000
1,611,500,097,000
205,903,359
1
0
null
null
null
null
UTF-8
Lean
false
false
4,339
lean
variables {p11 p21 p31 p12 p22 p32 : Prop} -- esses dois lemas, que eu chamo de segundos, fazem o processo de eliminação para p31 e p32. -- O processo nas três eliminações é o mesmo. lemma second_elim2 (h: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32)) (h1: p12) (h2: p21) : (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32) := show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.elim (h.right.right) (assume : p31, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inr (or.inr (or.inl (and.intro h2 this)))) (assume : p32, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inr (or.inr (or.inr (or.inr (or.inl (and.intro h1 this)))))) lemma second_elim1 (h: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32)) (h1: p11) (h2: p22) : (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32) := show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.elim (h.right.right) (assume : p31, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inr (or.inl (and.intro h1 this))) (assume : p32, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inr (or.inr (or.inr (or.inr (or.inr (and.intro h2 this)))))) -- Demonstro supondo p11 no primeiro e p12 no segundo. -- Mais uma vez utilizo a ideia da eliminação da união, agora de p21, p22 -- Note que as introduçõs tornam-se complexas pois tenho que inserir um ou no meio de outros. lemma first_elim2 (h: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32)) (h1: p12) : (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32) := show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.elim (h.right.left) (assume : p21, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from second_elim2 h h1 this) (assume : p22, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inr (or.inr (or.inr (or.inl (and.intro h1 this))))) lemma first_elim1 (h: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32)) (h1: p11) : (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32) := show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.elim (h.right.left) (assume : p21, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.inl (and.intro h1 this)) (assume : p22, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from second_elim1 h h1 this) -- Eu quero provar a seguinte aplicação. A ideia é utilizar a eliminação da união de p11 e p12. -- Para cada um deles, abrir nas possibilidades onde p21 e p22 passam pelo mesmo processo. theorem pigeons3: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32) → (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32) := assume h: (p11 ∨ p12) ∧ (p21 ∨ p22) ∧ (p31 ∨ p32), show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from or.elim (h.left) (assume : p11, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from first_elim1 h this) (assume : p12, show (p11 ∧ p21) ∨ (p11 ∧ p31) ∨ (p21 ∧ p31) ∨ (p12 ∧ p22) ∨ (p12 ∧ p32) ∨ (p22 ∧ p32), from first_elim2 h this)
b5d7362c99f6370337b3c89ea772662889ab8fd0
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed_space/spectrum.lean
1529e7b2467791b9a0dbf164e2319800c4cbb04b
[ "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
23,502
lean
/- Copyright (c) 2021 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import algebra.algebra.spectrum import analysis.special_functions.pow import analysis.special_functions.exponential import analysis.complex.liouville import analysis.analytic.radius_liminf import topology.algebra.module.character_space /-! # The spectrum of elements in a complete normed algebra This file contains the basic theory for the resolvent and spectrum of a Banach algebra. ## Main definitions * `spectral_radius : ℝ≥0∞`: supremum of `∥k∥₊` for all `k ∈ spectrum 𝕜 a` * `normed_ring.alg_equiv_complex_of_complete`: **Gelfand-Mazur theorem** For a complex Banach division algebra, the natural `algebra_map ℂ A` is an algebra isomorphism whose inverse is given by selecting the (unique) element of `spectrum ℂ a` ## Main statements * `spectrum.is_open_resolvent_set`: the resolvent set is open. * `spectrum.is_closed`: the spectrum is closed. * `spectrum.subset_closed_ball_norm`: the spectrum is a subset of closed disk of radius equal to the norm. * `spectrum.is_compact`: the spectrum is compact. * `spectrum.spectral_radius_le_nnnorm`: the spectral radius is bounded above by the norm. * `spectrum.has_deriv_at_resolvent`: the resolvent function is differentiable on the resolvent set. * `spectrum.pow_nnnorm_pow_one_div_tendsto_nhds_spectral_radius`: Gelfand's formula for the spectral radius in Banach algebras over `ℂ`. * `spectrum.nonempty`: the spectrum of any element in a complex Banach algebra is nonempty. ## TODO * compute all derivatives of `resolvent a`. -/ open_locale ennreal /-- The *spectral radius* is the supremum of the `nnnorm` (`∥⬝∥₊`) of elements in the spectrum, coerced into an element of `ℝ≥0∞`. Note that it is possible for `spectrum 𝕜 a = ∅`. In this case, `spectral_radius a = 0`. It is also possible that `spectrum 𝕜 a` be unbounded (though not for Banach algebras, see `spectrum.is_bounded`, below). In this case, `spectral_radius a = ∞`. -/ noncomputable def spectral_radius (𝕜 : Type*) {A : Type*} [normed_field 𝕜] [ring A] [algebra 𝕜 A] (a : A) : ℝ≥0∞ := ⨆ k ∈ spectrum 𝕜 a, ∥k∥₊ variables {𝕜 : Type*} {A : Type*} namespace spectrum section spectrum_compact variables [normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] local notation `σ` := spectrum 𝕜 local notation `ρ` := resolvent_set 𝕜 local notation `↑ₐ` := algebra_map 𝕜 A lemma mem_resolvent_set_of_spectral_radius_lt {a : A} {k : 𝕜} (h : spectral_radius 𝕜 a < ∥k∥₊) : k ∈ ρ a := not_not.mp $ λ hn, h.not_le $ le_supr₂ k hn variable [complete_space A] lemma is_open_resolvent_set (a : A) : is_open (ρ a) := units.is_open.preimage ((algebra_map_clm 𝕜 A).continuous.sub continuous_const) lemma is_closed (a : A) : is_closed (σ a) := (is_open_resolvent_set a).is_closed_compl lemma mem_resolvent_of_norm_lt [norm_one_class A] {a : A} {k : 𝕜} (h : ∥a∥ < ∥k∥) : k ∈ ρ a := begin rw [resolvent_set, set.mem_set_of_eq, algebra.algebra_map_eq_smul_one], have hk : k ≠ 0 := ne_zero_of_norm_ne_zero (by linarith [norm_nonneg a]), let ku := units.map (↑ₐ).to_monoid_hom (units.mk0 k hk), have hku : ∥-a∥ < ∥(↑ku⁻¹:A)∥⁻¹ := by simpa [ku, algebra_map_isometry] using h, simpa [ku, sub_eq_add_neg, algebra.algebra_map_eq_smul_one] using (ku.add (-a) hku).is_unit, end lemma norm_le_norm_of_mem [norm_one_class A] {a : A} {k : 𝕜} (hk : k ∈ σ a) : ∥k∥ ≤ ∥a∥ := le_of_not_lt $ mt mem_resolvent_of_norm_lt hk lemma subset_closed_ball_norm [norm_one_class A] (a : A) : σ a ⊆ metric.closed_ball (0 : 𝕜) (∥a∥) := λ k hk, by simp [norm_le_norm_of_mem hk] lemma is_bounded [norm_one_class A] (a : A) : metric.bounded (σ a) := (metric.bounded_iff_subset_ball 0).mpr ⟨∥a∥, subset_closed_ball_norm a⟩ theorem is_compact [norm_one_class A] [proper_space 𝕜] (a : A) : is_compact (σ a) := metric.is_compact_of_is_closed_bounded (is_closed a) (is_bounded a) theorem spectral_radius_le_nnnorm [norm_one_class A] (a : A) : spectral_radius 𝕜 a ≤ ∥a∥₊ := by { refine supr₂_le (λ k hk, _), exact_mod_cast norm_le_norm_of_mem hk } open ennreal polynomial variable (𝕜) theorem spectral_radius_le_pow_nnnorm_pow_one_div [norm_one_class A] (a : A) (n : ℕ) : spectral_radius 𝕜 a ≤ ∥a ^ (n + 1)∥₊ ^ (1 / (n + 1) : ℝ) := begin refine supr₂_le (λ k hk, _), /- apply easy direction of the spectral mapping theorem for polynomials -/ have pow_mem : k ^ (n + 1) ∈ σ (a ^ (n + 1)), by simpa only [one_mul, algebra.algebra_map_eq_smul_one, one_smul, aeval_monomial, one_mul, eval_monomial] using subset_polynomial_aeval a (monomial (n + 1) (1 : 𝕜)) ⟨k, hk, rfl⟩, /- power of the norm is bounded by norm of the power -/ have nnnorm_pow_le : (↑(∥k∥₊ ^ (n + 1)) : ℝ≥0∞) ≤ ↑∥a ^ (n + 1)∥₊, by simpa only [norm_to_nnreal, nnnorm_pow k (n+1)] using coe_mono (real.to_nnreal_mono (norm_le_norm_of_mem pow_mem)), /- take (n + 1)ᵗʰ roots and clean up the left-hand side -/ have hn : 0 < ((n + 1 : ℕ) : ℝ), by exact_mod_cast nat.succ_pos', convert monotone_rpow_of_nonneg (one_div_pos.mpr hn).le nnnorm_pow_le, erw [coe_pow, ←rpow_nat_cast, ←rpow_mul, mul_one_div_cancel hn.ne', rpow_one], rw nat.cast_succ, end end spectrum_compact section resolvent open filter asymptotics variables [nontrivially_normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] [complete_space A] local notation `ρ` := resolvent_set 𝕜 local notation `↑ₐ` := algebra_map 𝕜 A theorem has_deriv_at_resolvent {a : A} {k : 𝕜} (hk : k ∈ ρ a) : has_deriv_at (resolvent a) (-(resolvent a k) ^ 2) k := begin have H₁ : has_fderiv_at ring.inverse _ (↑ₐk - a) := has_fderiv_at_ring_inverse hk.unit, have H₂ : has_deriv_at (λ k, ↑ₐk - a) 1 k, { simpa using (algebra.linear_map 𝕜 A).has_deriv_at.sub_const a }, simpa [resolvent, sq, hk.unit_spec, ← ring.inverse_unit hk.unit] using H₁.comp_has_deriv_at k H₂, end /- TODO: Once there is sufficient API for bornology, we should get a nice filter / asymptotics version of this, for example: `tendsto (resolvent a) (cobounded 𝕜) (𝓝 0)` or more specifically `(resolvent a) =O[cobounded 𝕜] (λ z, z⁻¹)`. -/ lemma norm_resolvent_le_forall (a : A) : ∀ ε > 0, ∃ R > 0, ∀ z : 𝕜, R ≤ ∥z∥ → ∥resolvent a z∥ ≤ ε := begin obtain ⟨c, c_pos, hc⟩ := (@normed_ring.inverse_one_sub_norm A _ _).exists_pos, rw [is_O_with_iff, eventually_iff, metric.mem_nhds_iff] at hc, rcases hc with ⟨δ, δ_pos, hδ⟩, simp only [cstar_ring.norm_one, mul_one] at hδ, intros ε hε, have ha₁ : 0 < ∥a∥ + 1 := lt_of_le_of_lt (norm_nonneg a) (lt_add_one _), have min_pos : 0 < min (δ * (∥a∥ + 1)⁻¹) (ε * c⁻¹), from lt_min (mul_pos δ_pos (inv_pos.mpr ha₁)) (mul_pos hε (inv_pos.mpr c_pos)), refine ⟨(min (δ * (∥a∥ + 1)⁻¹) (ε * c⁻¹))⁻¹, inv_pos.mpr min_pos, (λ z hz, _)⟩, have hnz : z ≠ 0 := norm_pos_iff.mp (lt_of_lt_of_le (inv_pos.mpr min_pos) hz), replace hz := inv_le_of_inv_le min_pos hz, rcases (⟨units.mk0 z hnz, units.coe_mk0 hnz⟩ : is_unit z) with ⟨z, rfl⟩, have lt_δ : ∥z⁻¹ • a∥ < δ, { rw [units.smul_def, norm_smul, units.coe_inv, norm_inv], calc ∥(z : 𝕜)∥⁻¹ * ∥a∥ ≤ δ * (∥a∥ + 1)⁻¹ * ∥a∥ : mul_le_mul_of_nonneg_right (hz.trans (min_le_left _ _)) (norm_nonneg _) ... < δ : by { conv { rw mul_assoc, to_rhs, rw (mul_one δ).symm }, exact mul_lt_mul_of_pos_left ((inv_mul_lt_iff ha₁).mpr ((mul_one (∥a∥ + 1)).symm ▸ (lt_add_one _))) δ_pos } }, rw [←inv_smul_smul z (resolvent a (z : 𝕜)), units_smul_resolvent_self, resolvent, algebra.algebra_map_eq_smul_one, one_smul, units.smul_def, norm_smul, units.coe_inv, norm_inv], calc _ ≤ ε * c⁻¹ * c : mul_le_mul (hz.trans (min_le_right _ _)) (hδ (mem_ball_zero_iff.mpr lt_δ)) (norm_nonneg _) (mul_pos hε (inv_pos.mpr c_pos)).le ... = _ : inv_mul_cancel_right₀ c_pos.ne.symm ε, end end resolvent section one_sub_smul open continuous_multilinear_map ennreal formal_multilinear_series open_locale nnreal ennreal variables [nontrivially_normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] variable (𝕜) /-- In a Banach algebra `A` over a nontrivially normed field `𝕜`, for any `a : A` the power series with coefficients `a ^ n` represents the function `(1 - z • a)⁻¹` in a disk of radius `∥a∥₊⁻¹`. -/ lemma has_fpower_series_on_ball_inverse_one_sub_smul [complete_space A] (a : A) : has_fpower_series_on_ball (λ z : 𝕜, ring.inverse (1 - z • a)) (λ n, continuous_multilinear_map.mk_pi_field 𝕜 (fin n) (a ^ n)) 0 (∥a∥₊)⁻¹ := { r_le := begin refine le_of_forall_nnreal_lt (λ r hr, le_radius_of_bound_nnreal _ (max 1 ∥(1 : A)∥₊) (λ n, _)), rw [←norm_to_nnreal, norm_mk_pi_field, norm_to_nnreal], cases n, { simp only [le_refl, mul_one, or_true, le_max_iff, pow_zero] }, { refine le_trans (le_trans (mul_le_mul_right' (nnnorm_pow_le' a n.succ_pos) (r ^ n.succ)) _) (le_max_left _ _), { by_cases ∥a∥₊ = 0, { simp only [h, zero_mul, zero_le', pow_succ], }, { rw [←coe_inv h, coe_lt_coe, nnreal.lt_inv_iff_mul_lt h] at hr, simpa only [←mul_pow, mul_comm] using pow_le_one' hr.le n.succ } } } end, r_pos := ennreal.inv_pos.mpr coe_ne_top, has_sum := λ y hy, begin have norm_lt : ∥y • a∥ < 1, { by_cases h : ∥a∥₊ = 0, { simp only [nnnorm_eq_zero.mp h, norm_zero, zero_lt_one, smul_zero] }, { have nnnorm_lt : ∥y∥₊ < ∥a∥₊⁻¹, by simpa only [←coe_inv h, mem_ball_zero_iff, metric.emetric_ball_nnreal] using hy, rwa [←coe_nnnorm, ←real.lt_to_nnreal_iff_coe_lt, real.to_nnreal_one, nnnorm_smul, ←nnreal.lt_inv_iff_mul_lt h] } }, simpa [←smul_pow, (normed_ring.summable_geometric_of_norm_lt_1 _ norm_lt).has_sum_iff] using (normed_ring.inverse_one_sub _ norm_lt).symm, end } variable {𝕜} lemma is_unit_one_sub_smul_of_lt_inv_radius {a : A} {z : 𝕜} (h : ↑∥z∥₊ < (spectral_radius 𝕜 a)⁻¹) : is_unit (1 - z • a) := begin by_cases hz : z = 0, { simp only [hz, is_unit_one, sub_zero, zero_smul] }, { let u := units.mk0 z hz, suffices hu : is_unit (u⁻¹ • 1 - a), { rwa [is_unit.smul_sub_iff_sub_inv_smul, inv_inv u] at hu }, { rw [units.smul_def, ←algebra.algebra_map_eq_smul_one, ←mem_resolvent_set_iff], refine mem_resolvent_set_of_spectral_radius_lt _, rwa [units.coe_inv, nnnorm_inv, coe_inv (nnnorm_ne_zero_iff.mpr (units.coe_mk0 hz ▸ hz : (u : 𝕜) ≠ 0)), lt_inv_iff_lt_inv] } } end /-- In a Banach algebra `A` over `𝕜`, for `a : A` the function `λ z, (1 - z • a)⁻¹` is differentiable on any closed ball centered at zero of radius `r < (spectral_radius 𝕜 a)⁻¹`. -/ theorem differentiable_on_inverse_one_sub_smul [complete_space A] {a : A} {r : ℝ≥0} (hr : (r : ℝ≥0∞) < (spectral_radius 𝕜 a)⁻¹) : differentiable_on 𝕜 (λ z : 𝕜, ring.inverse (1 - z • a)) (metric.closed_ball 0 r) := begin intros z z_mem, apply differentiable_at.differentiable_within_at, have hu : is_unit (1 - z • a), { refine is_unit_one_sub_smul_of_lt_inv_radius (lt_of_le_of_lt (coe_mono _) hr), simpa only [norm_to_nnreal, real.to_nnreal_coe] using real.to_nnreal_mono (mem_closed_ball_zero_iff.mp z_mem) }, have H₁ : differentiable 𝕜 (λ w : 𝕜, 1 - w • a) := (differentiable_id.smul_const a).const_sub 1, exact differentiable_at.comp z (differentiable_at_inverse hu.unit) (H₁.differentiable_at), end end one_sub_smul section gelfand_formula open filter ennreal continuous_multilinear_map open_locale topological_space variables [normed_ring A] [normed_algebra ℂ A] [complete_space A] /-- The `limsup` relationship for the spectral radius used to prove `spectrum.gelfand_formula`. -/ lemma limsup_pow_nnnorm_pow_one_div_le_spectral_radius (a : A) : limsup at_top (λ n : ℕ, ↑∥a ^ n∥₊ ^ (1 / n : ℝ)) ≤ spectral_radius ℂ a := begin refine ennreal.inv_le_inv.mp (le_of_forall_pos_nnreal_lt (λ r r_pos r_lt, _)), simp_rw [inv_limsup, ←one_div], let p : formal_multilinear_series ℂ ℂ A := λ n, continuous_multilinear_map.mk_pi_field ℂ (fin n) (a ^ n), suffices h : (r : ℝ≥0∞) ≤ p.radius, { convert h, simp only [p.radius_eq_liminf, ←norm_to_nnreal, norm_mk_pi_field], refine congr_arg _ (funext (λ n, congr_arg _ _)), rw [norm_to_nnreal, ennreal.coe_rpow_def (∥a ^ n∥₊) (1 / n : ℝ), if_neg], exact λ ha, by linarith [ha.2, (one_div_nonneg.mpr n.cast_nonneg : 0 ≤ (1 / n : ℝ))], }, { have H₁ := (differentiable_on_inverse_one_sub_smul r_lt).has_fpower_series_on_ball r_pos, exact ((has_fpower_series_on_ball_inverse_one_sub_smul ℂ a).exchange_radius H₁).r_le, } end /-- **Gelfand's formula**: Given an element `a : A` of a complex Banach algebra, the `spectral_radius` of `a` is the limit of the sequence `∥a ^ n∥₊ ^ (1 / n)` -/ theorem pow_nnnorm_pow_one_div_tendsto_nhds_spectral_radius [norm_one_class A] (a : A) : tendsto (λ n : ℕ, ((∥a ^ n∥₊ ^ (1 / n : ℝ)) : ℝ≥0∞)) at_top (𝓝 (spectral_radius ℂ a)) := begin refine tendsto_of_le_liminf_of_limsup_le _ _ (by apply_auto_param) (by apply_auto_param), { rw [←liminf_nat_add _ 1, liminf_eq_supr_infi_of_nat], refine le_trans _ (le_supr _ 0), simp only [nat.cast_succ], exact le_infi₂ (λ i hi, spectral_radius_le_pow_nnnorm_pow_one_div ℂ a i) }, { exact limsup_pow_nnnorm_pow_one_div_le_spectral_radius a }, end /- This is the same as `pow_nnnorm_pow_one_div_tendsto_nhds_spectral_radius` but for `norm` instead of `nnnorm`. -/ /-- **Gelfand's formula**: Given an element `a : A` of a complex Banach algebra, the `spectral_radius` of `a` is the limit of the sequence `∥a ^ n∥₊ ^ (1 / n)` -/ theorem pow_norm_pow_one_div_tendsto_nhds_spectral_radius [norm_one_class A] (a : A) : tendsto (λ n : ℕ, ennreal.of_real (∥a ^ n∥ ^ (1 / n : ℝ))) at_top (𝓝 (spectral_radius ℂ a)) := begin convert pow_nnnorm_pow_one_div_tendsto_nhds_spectral_radius a, ext1, rw [←of_real_rpow_of_nonneg (norm_nonneg _) _, ←coe_nnnorm, coe_nnreal_eq], exact one_div_nonneg.mpr (by exact_mod_cast zero_le _), end end gelfand_formula /-- In a (nontrivial) complex Banach algebra, every element has nonempty spectrum. -/ theorem nonempty {A : Type*} [normed_ring A] [normed_algebra ℂ A] [complete_space A] [nontrivial A] (a : A) : (spectrum ℂ a).nonempty := begin /- Suppose `σ a = ∅`, then resolvent set is `ℂ`, any `(z • 1 - a)` is a unit, and `resolvent` is differentiable on `ℂ`. -/ rw ←set.ne_empty_iff_nonempty, by_contra h, have H₀ : resolvent_set ℂ a = set.univ, by rwa [spectrum, set.compl_empty_iff] at h, have H₁ : differentiable ℂ (λ z : ℂ, resolvent a z), from λ z, (has_deriv_at_resolvent (H₀.symm ▸ set.mem_univ z : z ∈ resolvent_set ℂ a)).differentiable_at, /- The norm of the resolvent is small for all sufficently large `z`, and by compactness and continuity it is bounded on the complement of a large ball, thus uniformly bounded on `ℂ`. By Liouville's theorem `λ z, resolvent a z` is constant -/ have H₂ := norm_resolvent_le_forall a, have H₃ : ∀ z : ℂ, resolvent a z = resolvent a (0 : ℂ), { refine λ z, H₁.apply_eq_apply_of_bounded (bounded_iff_exists_norm_le.mpr _) z 0, rcases H₂ 1 zero_lt_one with ⟨R, R_pos, hR⟩, rcases (proper_space.is_compact_closed_ball (0 : ℂ) R).exists_bound_of_continuous_on H₁.continuous.continuous_on with ⟨C, hC⟩, use max C 1, rintros _ ⟨w, rfl⟩, refine or.elim (em (∥w∥ ≤ R)) (λ hw, _) (λ hw, _), { exact (hC w (mem_closed_ball_zero_iff.mpr hw)).trans (le_max_left _ _) }, { exact (hR w (not_le.mp hw).le).trans (le_max_right _ _), }, }, /- `resolvent a 0 = 0`, which is a contradition because it isn't a unit. -/ have H₅ : resolvent a (0 : ℂ) = 0, { refine norm_eq_zero.mp (le_antisymm (le_of_forall_pos_le_add (λ ε hε, _)) (norm_nonneg _)), rcases H₂ ε hε with ⟨R, R_pos, hR⟩, simpa only [H₃ R] using (zero_add ε).symm.subst (hR R (by exact_mod_cast (real.norm_of_nonneg R_pos.lt.le).symm.le)), }, /- `not_is_unit_zero` is where we need `nontrivial A`, it is unavoidable. -/ exact not_is_unit_zero (H₅.subst (is_unit_resolvent.mp (mem_resolvent_set_iff.mp (H₀.symm ▸ set.mem_univ 0)))), end section gelfand_mazur_isomorphism variables [normed_ring A] [normed_algebra ℂ A] (hA : ∀ {a : A}, is_unit a ↔ a ≠ 0) include hA local notation `σ` := spectrum ℂ lemma algebra_map_eq_of_mem {a : A} {z : ℂ} (h : z ∈ σ a) : algebra_map ℂ A z = a := by rwa [mem_iff, hA, not_not, sub_eq_zero] at h /-- **Gelfand-Mazur theorem**: For a complex Banach division algebra, the natural `algebra_map ℂ A` is an algebra isomorphism whose inverse is given by selecting the (unique) element of `spectrum ℂ a`. In addition, `algebra_map_isometry` guarantees this map is an isometry. Note: because `normed_division_ring` requires the field `norm_mul' : ∀ a b, ∥a * b∥ = ∥a∥ * ∥b∥`, we don't use this type class and instead opt for a `normed_ring` in which the nonzero elements are precisely the units. This allows for the application of this isomorphism in broader contexts, e.g., to the quotient of a complex Banach algebra by a maximal ideal. In the case when `A` is actually a `normed_division_ring`, one may fill in the argument `hA` with the lemma `is_unit_iff_ne_zero`. -/ @[simps] noncomputable def _root_.normed_ring.alg_equiv_complex_of_complete [complete_space A] : ℂ ≃ₐ[ℂ] A := let nt : nontrivial A := ⟨⟨1, 0, hA.mp ⟨⟨1, 1, mul_one _, mul_one _⟩, rfl⟩⟩⟩ in { to_fun := algebra_map ℂ A, inv_fun := λ a, (@spectrum.nonempty _ _ _ _ nt a).some, left_inv := λ z, by simpa only [@scalar_eq _ _ _ _ _ nt _] using (@spectrum.nonempty _ _ _ _ nt $ algebra_map ℂ A z).some_mem, right_inv := λ a, algebra_map_eq_of_mem @hA (@spectrum.nonempty _ _ _ _ nt a).some_mem, ..algebra.of_id ℂ A } end gelfand_mazur_isomorphism section exp_mapping local notation `↑ₐ` := algebra_map 𝕜 A /-- For `𝕜 = ℝ` or `𝕜 = ℂ`, `exp 𝕜` maps the spectrum of `a` into the spectrum of `exp 𝕜 a`. -/ theorem exp_mem_exp [is_R_or_C 𝕜] [normed_ring A] [normed_algebra 𝕜 A] [complete_space A] (a : A) {z : 𝕜} (hz : z ∈ spectrum 𝕜 a) : exp 𝕜 z ∈ spectrum 𝕜 (exp 𝕜 a) := begin have hexpmul : exp 𝕜 a = exp 𝕜 (a - ↑ₐ z) * ↑ₐ (exp 𝕜 z), { rw [algebra_map_exp_comm z, ←exp_add_of_commute (algebra.commutes z (a - ↑ₐz)).symm, sub_add_cancel] }, let b := ∑' n : ℕ, ((n + 1).factorial⁻¹ : 𝕜) • (a - ↑ₐz) ^ n, have hb : summable (λ n : ℕ, ((n + 1).factorial⁻¹ : 𝕜) • (a - ↑ₐz) ^ n), { refine summable_of_norm_bounded_eventually _ (real.summable_pow_div_factorial ∥a - ↑ₐz∥) _, filter_upwards [filter.eventually_cofinite_ne 0] with n hn, rw [norm_smul, mul_comm, norm_inv, is_R_or_C.norm_eq_abs, is_R_or_C.abs_cast_nat, ←div_eq_mul_inv], exact div_le_div (pow_nonneg (norm_nonneg _) n) (norm_pow_le' (a - ↑ₐz) (zero_lt_iff.mpr hn)) (by exact_mod_cast nat.factorial_pos n) (by exact_mod_cast nat.factorial_le (lt_add_one n).le) }, have h₀ : ∑' n : ℕ, ((n + 1).factorial⁻¹ : 𝕜) • (a - ↑ₐz) ^ (n + 1) = (a - ↑ₐz) * b, { simpa only [mul_smul_comm, pow_succ] using hb.tsum_mul_left (a - ↑ₐz) }, have h₁ : ∑' n : ℕ, ((n + 1).factorial⁻¹ : 𝕜) • (a - ↑ₐz) ^ (n + 1) = b * (a - ↑ₐz), { simpa only [pow_succ', algebra.smul_mul_assoc] using hb.tsum_mul_right (a - ↑ₐz) }, have h₃ : exp 𝕜 (a - ↑ₐz) = 1 + (a - ↑ₐz) * b, { rw exp_eq_tsum, convert tsum_eq_zero_add (exp_series_summable' (a - ↑ₐz)), simp only [nat.factorial_zero, nat.cast_one, inv_one, pow_zero, one_smul], exact h₀.symm }, rw [spectrum.mem_iff, is_unit.sub_iff, ←one_mul (↑ₐ(exp 𝕜 z)), hexpmul, ←_root_.sub_mul, commute.is_unit_mul_iff (algebra.commutes (exp 𝕜 z) (exp 𝕜 (a - ↑ₐz) - 1)).symm, sub_eq_iff_eq_add'.mpr h₃, commute.is_unit_mul_iff (h₀ ▸ h₁ : (a - ↑ₐz) * b = b * (a - ↑ₐz))], exact not_and_of_not_left _ (not_and_of_not_left _ ((not_iff_not.mpr is_unit.sub_iff).mp hz)), end end exp_mapping end spectrum namespace alg_hom section normed_field variables [normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] [complete_space A] local notation `↑ₐ` := algebra_map 𝕜 A /-- An algebra homomorphism into the base field, as a continuous linear map (since it is automatically bounded). -/ instance [norm_one_class A] : continuous_linear_map_class (A →ₐ[𝕜] 𝕜) 𝕜 A 𝕜 := { map_continuous := λ φ, add_monoid_hom_class.continuous_of_bound φ 1 $ λ a, (one_mul ∥a∥).symm ▸ spectrum.norm_le_norm_of_mem (apply_mem_spectrum φ _), .. alg_hom_class.linear_map_class } /-- An algebra homomorphism into the base field, as a continuous linear map (since it is automatically bounded). -/ def to_continuous_linear_map [norm_one_class A] (φ : A →ₐ[𝕜] 𝕜) : A →L[𝕜] 𝕜 := { cont := map_continuous φ, .. φ.to_linear_map } @[simp] lemma coe_to_continuous_linear_map [norm_one_class A] (φ : A →ₐ[𝕜] 𝕜) : ⇑φ.to_continuous_linear_map = φ := rfl end normed_field section nontrivially_normed_field variables [nontrivially_normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] [complete_space A] local notation `↑ₐ` := algebra_map 𝕜 A @[simp] lemma to_continuous_linear_map_norm [norm_one_class A] (φ : A →ₐ[𝕜] 𝕜) : ∥φ.to_continuous_linear_map∥ = 1 := continuous_linear_map.op_norm_eq_of_bounds zero_le_one (λ a, (one_mul ∥a∥).symm ▸ spectrum.norm_le_norm_of_mem (apply_mem_spectrum φ _)) (λ _ _ h, by simpa only [coe_to_continuous_linear_map, map_one, norm_one, mul_one] using h 1) end nontrivially_normed_field end alg_hom namespace weak_dual namespace character_space variables [normed_field 𝕜] [normed_ring A] [complete_space A] [norm_one_class A] variables [normed_algebra 𝕜 A] /-- The equivalence between characters and algebra homomorphisms into the base field. -/ def equiv_alg_hom : (character_space 𝕜 A) ≃ (A →ₐ[𝕜] 𝕜) := { to_fun := to_alg_hom, inv_fun := λ f, { val := f.to_continuous_linear_map, property := by { rw eq_set_map_one_map_mul, exact ⟨map_one f, map_mul f⟩ } }, left_inv := λ f, subtype.ext $ continuous_linear_map.ext $ λ x, rfl, right_inv := λ f, alg_hom.ext $ λ x, rfl } @[simp] lemma equiv_alg_hom_coe (f : character_space 𝕜 A) : ⇑(equiv_alg_hom f) = f := rfl @[simp] lemma equiv_alg_hom_symm_coe (f : A →ₐ[𝕜] 𝕜) : ⇑(equiv_alg_hom.symm f) = f := rfl end character_space end weak_dual
38e943a9ae46fdf80f2173feaca40e4097c54f0f
aa101d73b1a3173c7ec56de02b96baa8ca64c42e
/src/solutions/08_limits_negation.lean
bdf96cc49b772a3f395f11a02b27a234eeb4b8cb
[ "Apache-2.0" ]
permissive
gihanmarasingha/tutorials
b554d4d53866c493c4341dc13e914b01444e95a6
56617114ef0f9f7b808476faffd11e22e4380918
refs/heads/master
1,671,141,758,153
1,599,173,318,000
1,599,173,318,000
282,405,870
0
0
Apache-2.0
1,595,666,751,000
1,595,666,750,000
null
UTF-8
Lean
false
false
5,079
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| ≤ ε) ↔ -- sorry ∃ ε > 0, ∀ N, ∃ n ≥ N, |u n - l| > ε -- sorry := begin -- sorry check_me, -- sorry end /- Negation of "f is continuous at x₀" -/ -- 0063 example : ¬ (∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - f x₀| ≤ ε) ↔ -- sorry ∃ ε > 0, ∀ δ > 0, ∃ x, |x - x₀| ≤ δ ∧ |f x - f x₀| > ε -- sorry := begin -- sorry check_me, -- sorry 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| ≤ ε) ↔ -- sorry ∃ ε > 0, ∀ δ > 0, ∃ x x', |x' - x| ≤ δ ∧ |f x' - f x| > ε -- sorry := begin -- sorry check_me, -- sorry 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₀| ≤ ε)) ↔ -- sorry ∃ u : ℕ → ℝ, (∀ δ > 0, ∃ N, ∀ n ≥ N, |u n - x₀| ≤ δ) ∧ (∃ ε > 0, ∀ N, ∃ n ≥ N, |f (u n) - f x₀| > ε) -- sorry := begin -- sorry check_me, -- sorry 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 -- sorry intros lim_infinie l lim_l, cases lim_l 1 (by linarith) with N hN, cases lim_infinie (l+2) with N' hN', let N₀ := max N N', specialize hN N₀ (le_max_left _ _), specialize hN' N₀ (le_max_right _ _), rw abs_le at hN, linarith, -- sorry 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 -- sorry intro n, by_contradiction H, push_neg at H, cases h ((u n - l)/2) (by linarith) with N hN, specialize hN (max n N) (le_max_right _ _), specialize h' n (max n N) (le_max_left _ _), rw abs_le at hN, linarith, -- sorry 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 -- sorry intro y, contrapose!, exact hx.right y, -- sorry 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 -- sorry contrapose!, intro h, use (y-x)/2, split ; linarith, -- sorry end -- 0070 example {x y : ℝ} {u : ℕ → ℝ} (hu : seq_limit u x) (ineg : ∀ n, u n ≤ y) : x ≤ y := begin -- sorry apply le_of_le_add_all', intros ε ε_pos, cases hu ε ε_pos with N hN, specialize hN N (by linarith), rw abs_le at hN, linarith [ineg N], -- sorry end
b6e258760b6791802ba274b1b61d4dc6618fd519
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/buffer/basic.lean
4ad7589694ea556f0ebb7ed4885522ae91d4c45b
[ "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
7,361
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon General utility functions for buffers. -/ import data.buffer import data.array.lemmas import control.traversable.instances namespace buffer open function variables {α : Type*} {xs : list α} instance : inhabited (buffer α) := ⟨nil⟩ @[ext] lemma ext : ∀ {b₁ b₂ : buffer α}, to_list b₁ = to_list b₂ → b₁ = b₂ | ⟨n₁, a₁⟩ ⟨n₂, a₂⟩ h := begin simp [to_list, to_array] at h, have e : n₁ = n₂ := by rw [←array.to_list_length a₁, ←array.to_list_length a₂, h], subst e, have h : a₁ == a₂.to_list.to_array := h ▸ a₁.to_list_to_array.symm, rw eq_of_heq (h.trans a₂.to_list_to_array) end lemma ext_iff {b₁ b₂ : buffer α} : b₁ = b₂ ↔ to_list b₁ = to_list b₂ := ⟨λ h, h ▸ rfl, ext⟩ lemma size_eq_zero_iff {b : buffer α} : b.size = 0 ↔ b = nil := begin rcases b with ⟨_|n, ⟨a⟩⟩, { simp only [size, nil, mk_buffer, true_and, true_iff, eq_self_iff_true, heq_iff_eq, sigma.mk.inj_iff], ext i, exact fin.elim0 i }, { simp [size, nil, mk_buffer, nat.succ_ne_zero] } end @[simp] lemma size_nil : (@nil α).size = 0 := by rw size_eq_zero_iff @[simp] lemma to_list_nil : to_list (@nil α) = [] := rfl instance (α) [decidable_eq α] : decidable_eq (buffer α) := by tactic.mk_dec_eq_instance @[simp] lemma to_list_append_list {b : buffer α} : to_list (append_list b xs) = to_list b ++ xs := by induction xs generalizing b; simp! [*]; cases b; simp! [to_list,to_array] @[simp] lemma append_list_mk_buffer : append_list mk_buffer xs = array.to_buffer (list.to_array xs) := by ext x : 1; simp [array.to_buffer,to_list,to_list_append_list]; induction xs; [refl,skip]; simp [to_array]; refl @[simp] lemma to_buffer_to_list (b : buffer α) : b.to_list.to_buffer = b := begin cases b, rw [to_list, to_array, list.to_buffer, append_list_mk_buffer], congr, { simpa }, { apply array.to_list_to_array } end @[simp] lemma to_list_to_buffer (l : list α) : l.to_buffer.to_list = l := begin cases l, { refl }, { rw [list.to_buffer, to_list_append_list], refl } end @[simp] lemma to_list_to_array (b : buffer α) : b.to_array.to_list = b.to_list := by { cases b, simp [to_list] } @[simp] lemma append_list_nil (b : buffer α) : b.append_list [] = b := rfl lemma to_buffer_cons (c : α) (l : list α) : (c :: l).to_buffer = [c].to_buffer.append_list l := begin induction l with hd tl hl, { simp }, { apply ext, simp [hl] } end @[simp] lemma size_push_back (b : buffer α) (a : α) : (b.push_back a).size = b.size + 1 := by { cases b, simp [size, push_back] } @[simp] lemma size_append_list (b : buffer α) (l : list α) : (b.append_list l).size = b.size + l.length := begin induction l with hd tl hl generalizing b, { simp }, { simp [append_list, hl, add_comm, add_assoc] } end @[simp] lemma size_to_buffer (l : list α) : l.to_buffer.size = l.length := begin induction l with hd tl hl, { simpa }, { rw [to_buffer_cons], have : [hd].to_buffer.size = 1 := rfl, simp [add_comm, this] } end @[simp] lemma length_to_list (b : buffer α) : b.to_list.length = b.size := by rw [←to_buffer_to_list b, to_list_to_buffer, size_to_buffer] lemma size_singleton (a : α) : [a].to_buffer.size = 1 := rfl lemma read_push_back_left (b : buffer α) (a : α) {i : ℕ} (h : i < b.size) : (b.push_back a).read ⟨i, by { convert nat.lt_succ_of_lt h, simp }⟩ = b.read ⟨i, h⟩ := by { cases b, convert array.read_push_back_left _, simp } @[simp] lemma read_push_back_right (b : buffer α) (a : α) : (b.push_back a).read ⟨b.size, by simp⟩ = a := by { cases b, convert array.read_push_back_right } lemma read_append_list_left' (b : buffer α) (l : list α) {i : ℕ} (h : i < (b.append_list l).size) (h' : i < b.size) : (b.append_list l).read ⟨i, h⟩ = b.read ⟨i, h'⟩ := begin induction l with hd tl hl generalizing b, { refl }, { have hb : i < ((b.push_back hd).append_list tl).size := by convert h using 1, have hb' : i < (b.push_back hd).size := by { convert nat.lt_succ_of_lt h', simp }, have : (append_list b (hd :: tl)).read ⟨i, h⟩ = read ((push_back b hd).append_list tl) ⟨i, hb⟩ := rfl, simp [this, hl _ hb hb', read_push_back_left _ _ h'] } end lemma read_append_list_left (b : buffer α) (l : list α) {i : ℕ} (h : i < b.size) : (b.append_list l).read ⟨i, by simpa using nat.lt_add_right _ _ _ h⟩ = b.read ⟨i, h⟩ := read_append_list_left' b l _ h @[simp] lemma read_append_list_right (b : buffer α) (l : list α) {i : ℕ} (h : i < l.length) : (b.append_list l).read ⟨b.size + i, by simp [h]⟩ = l.nth_le i h := begin induction l with hd tl hl generalizing b i, { exact absurd i.zero_le (not_le_of_lt h) }, { convert_to ((b.push_back hd).append_list tl).read _ = _, cases i, { convert read_append_list_left _ _ _; simp }, { rw [list.length, nat.succ_lt_succ_iff] at h, have : b.size + i.succ = (b.push_back hd).size + i, { simp [add_comm, add_left_comm, nat.succ_eq_add_one] }, convert hl (b.push_back hd) h using 1, simpa [nat.add_succ, nat.succ_add] } } end lemma read_to_buffer' (l : list α) {i : ℕ} (h : i < l.to_buffer.size) (h' : i < l.length) : l.to_buffer.read ⟨i, h⟩ = l.nth_le i h' := begin cases l with hd tl, { simpa using h' }, { have hi : i < ([hd].to_buffer.append_list tl).size := by simpa [add_comm] using h, convert_to ([hd].to_buffer.append_list tl).read ⟨i, hi⟩ = _, cases i, { convert read_append_list_left _ _ _, simp }, { rw list.nth_le, convert read_append_list_right _ _ _, simp [nat.succ_eq_add_one, add_comm] } } end @[simp] lemma read_to_buffer (l : list α) (i) : l.to_buffer.read i = l.nth_le i (by { convert i.property, simp }) := by { convert read_to_buffer' _ _ _, { simp }, { simpa using i.property } } lemma nth_le_to_list' (b : buffer α) {i : ℕ} (h h') : b.to_list.nth_le i h = b.read ⟨i, h'⟩ := begin have : b.to_list.to_buffer.read ⟨i, (by simpa using h')⟩ = b.read ⟨i, h'⟩, { congr' 1; simp [fin.heq_ext_iff] }, simp [←this] end lemma nth_le_to_list (b : buffer α) {i : ℕ} (h) : b.to_list.nth_le i h = b.read ⟨i, by simpa using h⟩ := nth_le_to_list' _ _ _ lemma read_eq_nth_le_to_list (b : buffer α) (i) : b.read i = b.to_list.nth_le i (by simpa using i.is_lt) := by simp [nth_le_to_list] lemma read_singleton (c : α) : [c].to_buffer.read ⟨0, by simp⟩ = c := by simp /-- The natural equivalence between lists and buffers, using `list.to_buffer` and `buffer.to_list`. -/ def list_equiv_buffer (α : Type*) : list α ≃ buffer α := begin refine { to_fun := list.to_buffer, inv_fun := buffer.to_list, .. }; simp [left_inverse,function.right_inverse] end instance : traversable buffer := equiv.traversable list_equiv_buffer instance : is_lawful_traversable buffer := equiv.is_lawful_traversable list_equiv_buffer /-- A convenience wrapper around `read` that just fails if the index is out of bounds. -/ meta def read_t (b : buffer α) (i : ℕ) : tactic α := if h : i < b.size then return $ b.read (fin.mk i h) else tactic.fail "invalid buffer access" end buffer
84ffb13f8fc5f140fff5416f4fcef366d2cc76a5
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/real/hyperreal.lean
db6cd237ad63abc0031849d79f2ecaa9efac80cf
[ "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
37,196
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 : discrete_linear_ordered_field ℝ* := germ.discrete_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 _ _ @[simp, norm_cast] lemma coe_max (x y : ℝ) : ((max x y : ℝ) : ℝ*) = max x y := germ.const_max _ _ _ @[simp, norm_cast] lemma coe_min (x y : ℝ) : ((min x y : ℝ) : ℝ*) = min x y := germ.const_min _ _ _ /-- 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 : ℕ | ¬ n > 0} = {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 : ℝ}, r > 0 → (-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 : ℝ, x > r /-- 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 two_pos).2 (hip $ r + 2)) (λ hin, not_lt_of_lt (hr 2 two_pos).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 : ℝ, x > r := 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 → x > 0 := λ 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 two_pos), 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 ∧ x > 0) := ⟨ λ 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 : x > 0) : 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 : x ≥ 0) : 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_of_ne_zero 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_iff_lt $ 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 : ℝ, r > 0 → x < r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).2 theorem lt_neg_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r > 0 → x > -r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).1 theorem gt_of_neg_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r < 0 → x > r := λ 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_of_ne_zero 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⁻¹ ∧ x⁻¹ > 0) := ⟨ λ 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) (x > (r : ℝ*)) (λ 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_of_ne_zero h))).mp ((infinitesimal_def.mp hi) ((abs r)⁻¹) (inv_pos.2 (abs_pos_of_ne_zero 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, have h0 : x ≠ 0 := λ h0, (lt_irrefl (0 : ℝ*) (by convert hin.2; rw [h0, inv_zero])), 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 ∧ x > 0) := 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_eq_inv 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 → y > 0 → 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 → x > 0 → 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 ∨ y > 0 := 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
f2fb3ab8c3d9532db771e40dfd85116d096eb4e8
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/commutative_algebra/book/sec_elements.lean
975796f0a8e9cb59e48328d7b0bc77e399455eeb
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,807
lean
import algebra.ring data.equiv.basic data.complex.basic data.zmod.basic import data.nat.prime data.int.gcd data.nat.choose algebra.gcd_domain data.finsupp import data.list.min_max data.polynomial import order.lattice_extra data.nat.square_free import tactic.ring tactic.squeeze namespace sec_elements universe u variables {A : Type u} [comm_ring A] /- -------------------------------------------------------- -/ /- defn-el-props -/ def inverse (a : A) := {b : A // a * b = 1} def is_invertible (a : A) := nonempty (inverse a) noncomputable def inv (a : A) (h : is_invertible a) := classical.choice h def is_regular (a : A) : Prop := ∀ (x : A) (e : a * x = 0), x = 0 def is_zero_divisor (a : A) := ¬ (is_regular a) def nilpotent_witness (a : A) := {n : ℕ // a ^ n = 0} def is_nilpotent (a : A) := nonempty (nilpotent_witness a) def is_idempotent (a : A) := a ^ 2 = a lemma is_idempotent' (a : A) : is_idempotent a ↔ a * (1 - a) = 0 := by { dsimp[is_idempotent], rw[pow_two,mul_add,mul_one,mul_neg_eq_neg_mul_symm], rw[← sub_eq_add_neg,sub_eq_zero], split; intro e; exact e.symm} /- -------------------------------------------------------- -/ /- prop-inv-unique -/ instance inverse_unique (a : A) : subsingleton (inverse a) := ⟨λ ⟨b₀,h₀⟩ ⟨b₁,h₁⟩ , begin apply subtype.eq, change b₀ = b₁, rw[← mul_one b₀,← h₁,← mul_assoc,mul_comm b₀,h₀,one_mul], end ⟩ /- -------------------------------------------------------- -/ /- eg-C-el-props -/ namespace integral_domain variables {D : Type*} [integral_domain D] (a : D) lemma is_regular_iff : (is_regular a ↔ a ≠ 0) := begin split, {rintro h_reg a_eq_0, exact zero_ne_one (h_reg 1 (a_eq_0.symm ▸ (zero_mul 1))).symm}, {intros a_ne_0 b e, rcases eq_zero_or_eq_zero_of_mul_eq_zero e with ha | hb, {exact (a_ne_0 ha).elim},{exact hb}} end lemma is_nilpotent_iff : (is_nilpotent a ↔ a = 0) := begin split, {rintro ⟨⟨n,e⟩⟩,induction n with n ih, {rw[pow_zero] at e,exact (zero_ne_one e.symm).elim}, {rw[pow_succ] at e,rcases eq_zero_or_eq_zero_of_mul_eq_zero e with h | h, {exact h},{exact ih h} } },{ intro h,rw[h],exact ⟨⟨1,pow_one 0⟩⟩ } end lemma is_idempotent_iff : (is_idempotent a ↔ a = 0 ∨ a = 1) := begin rw[is_idempotent'],split, {intro e,rcases eq_zero_or_eq_zero_of_mul_eq_zero e with h0 | h1, {exact or.inl h0}, {exact or.inr (sub_eq_zero_iff_eq.mp h1).symm,} },{ rintro (h | h); rw[h],rw[zero_mul],rw[sub_self,mul_zero], } end end integral_domain namespace field variables {F : Type*} [field F] (a : F) lemma is_invertible_iff : (is_invertible a ↔ a ≠ 0) := begin split, {rintro ⟨⟨ai,ai_prop⟩⟩ a_eq_0,rw[a_eq_0,zero_mul] at ai_prop, exact zero_ne_one ai_prop },{intro a_ne_0,use a⁻¹,exact mul_inv_cancel a_ne_0,} end lemma is_regular_iff : (is_regular a ↔ a ≠ 0) := begin split, {rintro h_reg a_eq_0, exact zero_ne_one (h_reg 1 (a_eq_0.symm ▸ (zero_mul 1))).symm}, {intros a_ne_0 b e,exact calc b = 1 * b : (one_mul b).symm ... = 0 : by rw[← inv_mul_cancel a_ne_0,mul_assoc,e,mul_zero],} end end field namespace C_el_props lemma ℂ_el_props (z : ℂ) : (is_invertible z ↔ z ≠ 0) ∧ (is_regular z ↔ z ≠ 0) ∧ (is_nilpotent z ↔ z = 0) ∧ (is_idempotent z ↔ (z = 0 ∨ z = 1)) := begin split,exact field.is_invertible_iff z, split,exact integral_domain.is_regular_iff z, split,exact integral_domain.is_nilpotent_iff z, exact integral_domain.is_idempotent_iff z, end end C_el_props /- -------------------------------------------------------- -/ /- eg-Zn-el-props -/ namespace zmod_el_props variable (n : ℕ+) namespace int def gcd_a (x y : ℤ) : ℤ := x.sign * (nat.gcd_a x.nat_abs y.nat_abs) def gcd_b (x y : ℤ) : ℤ := y.sign * (nat.gcd_b x.nat_abs y.nat_abs) lemma nat_abs_eq_sign_mul_self : ∀ (x : ℤ), (x.nat_abs : ℤ) = x.sign * x | (0 : ℕ) := rfl | ((n + 1) : ℕ) := by {rw[int.nat_abs_of_nat,int.sign,_root_.one_mul],} | (-[1+ m]) := by {rw[int.nat_abs,int.sign,← neg_eq_neg_one_mul],refl} lemma gcd_eq_gcd_ab (x y : ℤ) : (gcd x y) = x * (gcd_a x y) + y * (gcd_b x y) := begin let d := nat.gcd x.nat_abs y.nat_abs, let a := nat.gcd_a x.nat_abs y.nat_abs, let b := nat.gcd_b x.nat_abs y.nat_abs, change (d : ℤ) = x * (x.sign * a) + y * (y.sign * b), let h : (d : ℤ) = x.nat_abs * a + y.nat_abs * b:= by {dsimp[d,a,b],exact nat.gcd_eq_gcd_ab x.nat_abs y.nat_abs}, rw[← _root_.mul_assoc,← _root_.mul_assoc,mul_comm x,mul_comm y], rw[← nat_abs_eq_sign_mul_self x,← nat_abs_eq_sign_mul_self y], exact h, end end int namespace nat def sup_monoid : add_comm_monoid ℕ := { zero := 0, add := λ (a b : ℕ), ite (a ≤ b) b a, add_zero := λ a, by {dsimp[has_add.add],split_ifs,exact (nat.eq_zero_of_le_zero h).symm,refl}, zero_add := λ a, by {dsimp[has_add.add],split_ifs,refl,exact (h (nat.zero_le a)).elim}, add_comm := λ a b, by {change ℕ at a, dsimp[has_add.add],split_ifs with hab hba, {exact le_antisymm hba hab},{refl},{refl}, {exact (h (le_of_lt (lt_of_not_ge hab))).elim}, }, add_assoc := λ a b c, by { dsimp[has_add.add],split_ifs with hab hbc hac hac hbc; try {refl}, {exact (hac (le_trans hab hbc)).elim}, {replace hbc := lt_of_not_ge hbc, replace hab := lt_of_not_ge hab, exact (not_le_of_gt (lt_trans hbc hab) hac).elim, } }, } end nat lemma is_invertible_iff (a : ℤ) : (is_invertible (a : zmod n)) ↔ gcd a n = 1 := begin let en : ((n : ℕ) : ℤ) = n := rfl, split, { rintro ⟨b₀,hab⟩, let d := gcd a n,change d = 1, have : d = d.nat_abs := ((int.coe_nat_abs_eq_normalize d).trans (normalize_gcd a n)).symm, rw[this,← int.coe_nat_one],congr, let b : ℤ := b₀.val, have : (b : zmod n) = b₀ := by {rw[int.cast_coe_nat,zmod.cast_val b₀],}, rw[← this,← int.cast_mul] at hab, have : (((a * b - 1) : ℤ) : (zmod n)) = 0 := by {rw[int.cast_sub,hab,int.cast_one,sub_self],}, rcases (zmod.eq_zero_iff_dvd_int.mp this) with ⟨c,e⟩, change a * b - 1 = n * c at e, replace e : a * b = n * c + 1 := by {rw[← e,sub_add,sub_self,sub_zero],}, let ha : d ∣ a := by {dsimp[d],rw[en], exact gcd_dvd_left a n}, let hn : d ∣ n := by {dsimp[d],rw[en], exact gcd_dvd_right a n}, let hab : d ∣ (n * c + 1) := e ▸ (dvd_mul_of_dvd_left ha b), rcases ((dvd_add_iff_right (dvd_mul_of_dvd_left hn c)).mpr hab) with ⟨u,eu⟩, replace eu := congr_arg int.nat_abs eu, change 1 = (d * u).nat_abs at eu,rw[int.nat_abs_mul] at eu, exact (nat.mul_eq_one_iff.mp eu.symm).left, },{ let d := gcd a n, let b := int.gcd_a a n, let q := int.gcd_b a n, have e : d = a * b + n * q := int.gcd_eq_gcd_ab a n, intro h, change d = 1 at h,rw[e,← en] at h, replace h := eq_sub_of_add_eq h, have : (a : zmod n) * (b : zmod n) = 1 := by {rw[← int.cast_mul,h,int.cast_sub,int.cast_mul,int.cast_coe_nat], rw[zmod.cast_self_eq_zero,zero_mul,sub_zero,int.cast_one],}, exact ⟨⟨(b : zmod n),this⟩⟩ } end lemma is_invertible_iff' (a : ℕ) : (is_invertible (a : zmod n)) ↔ (∀ (p : ℕ), (nat.prime p) → (p ∣ n) → ¬ (p ∣ a)) := begin rw[← int.cast_coe_nat,is_invertible_iff], dsimp[gcd,int.gcd],rw[← int.coe_nat_one,int.coe_nat_inj'], split, {intros h p p_prime p_dvd_n p_dvd_a, have : p ∣ 1 := h ▸ (nat.dvd_gcd p_dvd_a p_dvd_n), exact nat.prime.not_dvd_one p_prime this, }, {intro h,by_contradiction h', let d := nat.gcd a n, let p := nat.min_fac d, let p_prime : p.prime := nat.min_fac_prime h', let p_dvd_d : p ∣ d := nat.min_fac_dvd d, let p_dvd_a : p ∣ a := dvd_trans p_dvd_d (nat.gcd_dvd_left a n), let p_dvd_n : p ∣ n := dvd_trans p_dvd_d (nat.gcd_dvd_right a n), exact h p p_prime p_dvd_n p_dvd_a, } end lemma is_regular_iff (a : ℕ) : (is_regular (a : zmod n)) ↔ (is_invertible (a : zmod n)) := begin split, {intro h, let f : (zmod n) → (zmod n) := λ x, a * x, have : function.injective f := λ x₁ x₂ e, by {rw[← sub_eq_zero_iff_eq] at e ⊢, dsimp[f] at e,rw[neg_mul_eq_mul_neg,← mul_add] at e, replace e := h _ e,rw[sub_eq_add_neg],exact e, }, rcases (fintype.injective_iff_surjective.mp this 1) with ⟨b,e⟩, dsimp[f] at e,exact ⟨⟨b,e⟩⟩, },{ rintros ⟨⟨b,eab⟩⟩ x eax,rw[mul_comm] at eab, exact calc x = 1 * x : (one_mul x).symm ... = 0 : by rw[← eab,mul_assoc,eax,mul_zero b], } end lemma is_nilpotent_iff (a : ℕ) : (is_nilpotent (a : zmod n)) ↔ (∀ (p : ℕ), (nat.prime p) → (p ∣ n) → (p ∣ a)) := begin split, {rintro ⟨⟨k,e⟩⟩ p p_prime p_dvd_n, rw[← nat.cast_pow] at e, replace e := dvd_trans p_dvd_n (zmod.eq_zero_iff_dvd_nat.mp e), exact nat.prime.dvd_of_dvd_pow p_prime e, },{ rcases n with ⟨n,hn⟩, rcases nat.dvd_square_free_radical hn with ⟨k,⟨q,hq⟩⟩, intro h, rcases (nat.square_free_radical_dvd_iff hn a).mpr h with ⟨r,hr⟩, have ha : a ^ k = n * (q * r ^ k) := by {rw[hr,nat.mul_pow,hq,mul_assoc]}, have hz : (n : zmod ⟨n,hn⟩) = 0 := zmod.cast_self_eq_zero, have := calc (a : zmod ⟨n,hn⟩) ^ k = (((a ^ k) : ℕ) : zmod ⟨n,hn⟩) : by rw[nat.cast_pow] ... = 0 : by {rw[ha,nat.cast_mul,nat.cast_mul,nat.cast_pow,hz,zero_mul]}, exact ⟨⟨k,this⟩⟩, } end end zmod_el_props /- -------------------------------------------------------- -/ /- prop-inv-prod -/ variable (A) def inverse_one : inverse (1 : A) := ⟨1,mul_one 1⟩ variable {A} def inverse_prod_equiv (a b : A) : inverse (a * b) ≃ ((inverse b) × (inverse a)) := { to_fun := λ ⟨u,e⟩, ⟨⟨a * u,by {rw[← e],ring}⟩, ⟨b * u,by {rw[← e],ring}⟩⟩ , inv_fun := λ ⟨⟨v,ev⟩,⟨u,eu⟩⟩, ⟨v * u, calc (a * b) * (v * u) = (a * u) * (b * v) : by ring ... = 1 : by rw[eu,ev,mul_one], ⟩, left_inv := λ u, by {apply subsingleton.elim}, right_inv := λ ⟨v,u⟩, by {apply prod.ext;apply subsingleton.elim}, } def inverse_inv (a : A) (u : inverse a) : inverse u.val := ⟨a,by {rw[mul_comm,u.property]}⟩ variable (A) lemma is_invertible_one : is_invertible (1 : A) := ⟨inverse_one A⟩ variable {A} lemma is_invertible_mul_iff (a b : A) : is_invertible (a * b) ↔ (is_invertible a) ∧ (is_invertible b) := begin split, {rintro ⟨uv⟩,let uv' := (inverse_prod_equiv a b).to_fun uv, exact ⟨⟨uv'.2⟩,⟨uv'.1⟩⟩}, {rintro ⟨⟨u⟩,⟨v⟩⟩, exact ⟨(inverse_prod_equiv a b).inv_fun ⟨v,u⟩⟩,} end /- -------------------------------------------------------- -/ /- prop-regular-prod -/ variable (A) def is_regular_one : is_regular (1 : A) := λ x h, by {rw[one_mul] at h, exact h} variable {A} def is_regular_mul_iff {a b : A} : is_regular (a * b) ↔ (is_regular a) ∧ (is_regular b) := begin split, {intro hab,split, {intros x e, have e' : (a * b) * x = b * (a * x) := by ring, rw[e,mul_zero] at e',exact hab _ e',}, {intros x e, have e' : (a * b) * x = a * (b * x) := by ring, rw[e,mul_zero] at e',exact hab _ e',}, }, {rintro ⟨ha,hb⟩ x e,rw[mul_assoc] at e, replace e := ha (b * x) e,replace e := hb x e,exact e, } end def is_regular_of_invertible {a : A} : is_invertible a → is_regular a := λ ⟨u⟩ x e, calc x = (1 : A) * x : (one_mul x).symm ... = (a * u.val) * x : by rw[u.property] ... = u.val * (a * x) : by ring ... = 0 : by rw[e,mul_zero] /- -------------------------------------------------------- -/ /- prop-finite-regular -/ lemma invertible_of_regular_of_finite [fintype A] {a : A} : (is_regular a) → (is_invertible a) := begin intro ha, let f : A → A := λ x, a * x, have hf : function.injective f := λ x₀ x₁ e, begin let e' := calc a * (x₀ - x₁) = (a * x₀) - (a * x₁) : mul_sub _ _ _ ... = (f x₀) - (f x₁) : rfl ... = 0 : by rw[e,sub_self], exact sub_eq_zero.mp (ha _ e') end, let hf' := fintype.injective_iff_surjective.mp hf, rcases (hf' 1) with ⟨u,hu⟩, exact ⟨⟨u,hu⟩⟩ end /- -------------------------------------------------------- -/ /- prop-nilpotent-sum -/ lemma is_nilpotent_zero : is_nilpotent (0 : A) := ⟨⟨1,pow_one 0⟩⟩ lemma is_nilpotent_add {a b : A} : is_nilpotent a → is_nilpotent b → is_nilpotent (a + b) := begin intros ha hb, rcases ha with ⟨_|n,ea⟩, {exact ⟨⟨0,by {rw[pow_zero] at ea ⊢,exact ea} ⟩⟩}, rcases hb with ⟨_|m,eb⟩, {exact ⟨⟨0,by {rw[pow_zero] at eb ⊢,exact eb} ⟩⟩}, constructor, use n + m + 1,rw[add_pow], rw[← @finset.sum_const_zero ℕ A (finset.range (n + m + 1).succ)], congr,ext i, by_cases hi : i ≥ n + 1, {rw[← nat.add_sub_of_le hi,pow_add,ea],repeat {rw[zero_mul]},}, {replace hi := le_of_not_gt hi, have := nat.add_sub_of_le hi, have : n + m + 1 - i = (m + 1) + (n - i) := by { rw[← this,add_comm i,add_assoc,nat.add_sub_cancel], rw[add_assoc,add_comm i,← add_assoc,nat.add_sub_cancel,add_comm], }, rw[this,pow_add,eb,zero_mul,mul_zero,zero_mul], } end lemma is_nilpotent_smul (a : A) {b : A} : is_nilpotent b → is_nilpotent (a * b) := λ ⟨⟨n,e⟩⟩, by { have e' : (a * b) ^ n = 0 := by { rw[mul_pow,e,mul_zero],}, exact ⟨⟨n,e'⟩⟩ } lemma is_nilpotent_neg {b : A} : is_nilpotent b → is_nilpotent (-b) := λ h, by {rw[neg_eq_neg_one_mul],exact is_nilpotent_smul (-1) h} lemma is_nilpotent_sub {a b : A} : is_nilpotent a → is_nilpotent b → is_nilpotent (a - b) := λ ha hb, by {rw[sub_eq_add_neg],apply is_nilpotent_add, exact ha,apply is_nilpotent_neg,exact hb} /- -------------------------------------------------------- -/ /- prop-nilpotent-inv -/ def geometric_series (a : A) (n : ℕ) := (finset.range n).sum (λ i, a ^ i) lemma geometric_sum (a : A) (n : ℕ) : (1 - a) * (geometric_series a n) = 1 - a ^ n := begin induction n with n ih, {change (1 - a) * 0 = 1 - a ^ 0,rw[mul_zero,pow_zero,sub_self],}, {have : geometric_series a n.succ = a ^ n + geometric_series a n := by {dsimp[geometric_series],rw[finset.sum_range_succ]}, rw[this,mul_add,ih,nat.succ_eq_add_one,pow_add,pow_one], rw[sub_mul,one_mul,mul_comm a], repeat {rw[sub_eq_add_neg]}, rw[add_comm,add_assoc,← add_assoc (- (a^n)),neg_add_self,zero_add], } end def one_add_nilp_inv {a : A} : ∀ (h : nilpotent_witness a), inverse (1 + a) | ⟨n,e⟩ := ⟨geometric_series (-a) n, by { let u := geometric_series (-a) n, change (1 + a) * u = 1, let e' : (1 - (- a)) * u = 1 - (- a) ^ n := by {dsimp[u],exact geometric_sum (- a) n}, rw[sub_neg_eq_add,neg_eq_neg_one_mul,mul_pow,e,mul_zero,sub_zero] at e', exact e' } ⟩ lemma one_add_nilp_inv' {a : A} : is_nilpotent a → is_invertible (1 + a) := λ ⟨h⟩, ⟨one_add_nilp_inv h⟩ /- -------------------------------------------------------- -/ /- cor-nilp-inv -/ lemma inv_add_nilp_inv {u a : A} : is_invertible u → is_nilpotent a → is_invertible (u + a) := λ ⟨⟨v,euv⟩⟩ ea, begin have : u + a = u * (1 + v * a) := by { rw[mul_add,← mul_assoc,euv,mul_one,one_mul], }, rw[this,is_invertible_mul_iff], split, {exact ⟨⟨v,euv⟩⟩}, {exact one_add_nilp_inv' (is_nilpotent_smul v ea)} end /- -------------------------------------------------------- -/ /- prop-idempotent-ops -/ namespace idempotent variable (A) lemma zero_mem : is_idempotent (0 : A) := by {dsimp[is_idempotent],rw[pow_two,mul_zero]} lemma one_mem : is_idempotent (1 : A) := by {dsimp[is_idempotent],rw[pow_two,mul_one]} variable {A} def not (a : A) := 1 - a def and (a b : A) := a * b def or (a b : A) := a + b - a * b def xor (a b : A) := a + b - 2 * a * b lemma one_sub_mem {a : A} : is_idempotent a → is_idempotent (1 - a) := by { dsimp[is_idempotent],rw[pow_two,pow_two],intro e, rw[mul_add,mul_one,add_mul,one_mul,neg_mul_neg,e,neg_add_self,add_zero], } lemma mul_mem {a b : A} : is_idempotent a → is_idempotent b → is_idempotent (a * b) := by { intros ea eb, dsimp[is_idempotent] at *, rw[pow_two] at *, rw[mul_assoc,mul_comm b,mul_assoc a,eb,← mul_assoc,ea], } lemma add_sub_mul_mem {a b : A} : is_idempotent a → is_idempotent b → is_idempotent (a + b - a * b) := by { intros ea eb, have : a + b - a * b = 1 - (1 - a) * (1 - b) := by ring, rw[this], apply one_sub_mem, apply mul_mem; apply one_sub_mem; assumption, } def invol (a : A) := 1 - 2 * a lemma invol_square {a : A} (ha : is_idempotent a) : (invol a) ^ 2 = 1 := begin dsimp[invol,is_idempotent] at ha, have : (1 - 2 * a) ^ 2 = 1 - 4 * (a - a ^ 2) := by {rw[pow_two,pow_two],ring}, rw[ha,sub_self,mul_zero,sub_zero] at this, exact this, end lemma eq_of_sub_nilp {e₀ e₁ : A} (h₀ : is_idempotent e₀) (h₁ : is_idempotent e₁) (h : is_nilpotent (e₀ - e₁)) : e₀ = e₁ := begin dsimp[is_idempotent] at h₀ h₁,rw[pow_two] at h₀ h₁, let x := e₀ - e₁, let u := 1 - 2 * e₀, let v := 1 + u * x, have hv : is_invertible v := one_add_nilp_inv' (is_nilpotent_smul u h), have hvx := calc v * x = (e₀ * e₀ - e₀) * (4 * e₁ - 2 * e₀ - 1) + (e₁ * e₁ - e₁) * (1 - 2 * e₀) : by {dsimp[v,u,x],ring} ... = 0 : by {rw[h₀,h₁,sub_self,sub_self,zero_mul,zero_mul,zero_add],}, have hx : x = 0 := (is_regular_of_invertible hv) x hvx, rw[← sub_eq_zero], exact hx, end def lift : ∀ (e : A) (h : nilpotent_witness (e * (1 - e))), A := λ e ⟨n,hx⟩, e ^ (n + 2) * geometric_series (1 - e ^ (n + 2) - (1 - e) ^ (n + 2)) n lemma lift_spec (e : A) (h : nilpotent_witness (e * (1 - e))) : pprod (is_idempotent (lift e h)) (nilpotent_witness ((lift e h) - e)) := begin rcases h with ⟨n,hx⟩, let x := e * (1 - e), change x ^ n = 0 at hx, let y := 1 - e ^ (n + 2) - (1 - e) ^ (n + 2), let u := geometric_series y n, let e₁ := e ^ (n + 2) * u, have : lift e ⟨n,hx⟩ = e₁ := rfl, rw[this], let f := λ (i : ℕ), e ^ i * (1 - e) ^ (n + 2 - i) * nat.choose (n + 2) i, let z := (finset.range (n + 1)).sum (λ j, e ^ j * (1 - e) ^ (n - j) * (nat.choose (n + 2) (j + 1))), let xz := (finset.range (n + 1)).sum (f ∘ nat.succ), have hxz : x * z = xz := by { dsimp[xz,x],rw[finset.mul_sum],apply finset.sum_congr rfl,intros i hi, replace hi : i ≤ n := nat.le_of_lt_succ (finset.mem_range.mp hi), have : (f ∘ nat.succ) i = f (i + 1) := rfl, rw[this], dsimp[f], have : n + 2 - (i + 1) = (n - i) + 1 := calc n + 2 - (i + 1) = n + 1 - i : by {rw[nat.succ_sub_succ]} ... = (i + (n - i)) + 1 - i : by {rw[nat.add_sub_of_le hi]} ... = (n - i) + 1 : by {simp only [add_comm,add_assoc,nat.add_sub_cancel_left],}, rw[this,pow_succ,pow_succ],repeat {rw[mul_assoc]},congr' 1, repeat {rw[← mul_assoc]},rw[mul_comm (1 + - e) (e ^ i)], }, have hf₀ : f 0 = (1 - e) ^ (n + 2) := by {dsimp[f],rw[nat.choose,nat.cast_one,one_mul,mul_one],}, have hf₁ : f (n + 2) = e ^ (n + 2) := by {dsimp[f],rw[nat.choose_self,nat.cast_one,nat.sub_self,pow_zero,mul_one,mul_one],}, have := calc (1 : A) = (1 : A) ^ (n + 2) : (one_pow (n + 2)).symm ... = (e + (1 - e)) ^ (n + 2) : by {congr,rw[add_sub,add_comm,add_sub_cancel]} ... = (finset.range (n + 2).succ).sum f : add_pow e (1 - e) (n + 2) ... = e ^ (n + 2) + (finset.range (n + 2)).sum f : by {rw[finset.sum_range_succ,hf₁],} ... = e ^ (n + 2) + (xz + (1 - e) ^ (n + 2)) : by {rw[finset.sum_range_succ',hf₀]} ... = e ^ (n + 2) + (x * z + (1 - e) ^ (n + 2)) : by {rw[hxz]}, have hxyz := calc y = 1 - e ^ (n + 2) - (1 - e) ^ (n + 2) : rfl ... = (e ^ (n + 2) + (x * z + (1 - e) ^ (n + 2))) - e ^ (n + 2) - (1 - e) ^ (n + 2) : by {congr' 2,exact this} ... = x * z : by {simp only [sub_eq_add_neg,add_comm,add_left_comm, add_neg_cancel_left,add_neg_cancel_right],}, have hy : y ^ n = 0 := by {rw[hxyz,mul_pow,hx,zero_mul],}, have hu : (1 - y) * u = 1 := by {rw[geometric_sum y n,hy,sub_zero]}, have : 1 - y = e ^ (n + 2) + (1 - e) ^ (n + 2) := calc 1 - y = 1 - (1 - e ^ (n + 2) - (1 - e) ^ (n + 2)) : by {simp only [y]} ... = e ^ (n + 2) + (1 - e) ^ (n + 2) : by rw[sub_sub,sub_sub_cancel], let hu' := hu, rw[this] at hu', have := calc 1 - e₁ = (e ^ (n + 2) + (1 - e) ^ (n + 2)) * u - e₁ : by {rw[hu'],} ... = e ^ (n + 2) * u + (1 - e) ^ (n + 2) * u - e ^ (n + 2) * u : by {rw[add_mul],} ... = (1 - e) ^ (n + 2) * u : by {rw[add_comm,add_sub_cancel]}, have := calc e₁ * (1 - e₁) = (e ^ (n + 2) * u) * (1 - e₁) : rfl ... = u * (e ^ (n + 2) * (1 - e₁)) : by {rw[mul_comm (e ^ (n + 2))],rw[← mul_assoc],} ... = u * (e ^ (n + 2) * (1 - e) ^ (n + 2) * u) : by {rw[this,mul_assoc]} ... = u * (x ^ (n + 2) * u) : by {rw[mul_pow,pow_add],} ... = 0 : by {rw[pow_add,hx,zero_mul,zero_mul,mul_zero],}, split, exact (is_idempotent' e₁).mpr this, let w := geometric_series e (n + 1), have hw : x * w = e - e ^ (n + 2) := calc x * w = e * ((1 - e) * w) : by {dsimp[x],rw[mul_assoc]} ... = e * (1 - e ^ (n + 1)) : by {rw[geometric_sum e (n + 1)],} ... = e - e ^ (n + 2) : by {rw[mul_sub,mul_one,pow_succ e (n + 1)],}, have hu'' : u = 1 + x * z * u := by { rw[sub_mul,hxyz,one_mul] at hu,rw[← hu,sub_add_cancel], }, have := calc e₁ - e = e ^ (n + 2) * u - e : rfl ... = e ^ (n + 2) * (1 + x * z * u) - e : by {congr' 2,exact hu''} ... = (e ^ (n + 2) * (x * z * u) + e ^ (n + 2)) - e : by {rw[mul_add,mul_one,add_comm],} ... = x * (e ^ (n + 2) * z * u) - (e - e ^ (n + 2)) : by {rw[← sub_add,sub_eq_add_neg,sub_eq_add_neg], rw[← mul_assoc,← mul_assoc,mul_comm (e ^ (n + 2))], repeat {rw[add_assoc]},rw[add_comm (e ^ (n + 2))], repeat {rw[mul_assoc]},} ... = x * (e ^ (n + 2) * z * u - w) : by {rw[mul_sub,hw],}, have : (e₁ - e) ^ n = 0 := by {rw[this,mul_pow,hx,zero_mul],}, exact ⟨n,this⟩, end lemma lift_unique (e e₁ : A) (h : nilpotent_witness (e * (1 - e))) (hi : is_idempotent e₁) (hn : is_nilpotent (e₁ - e)) : e₁ = lift e h := begin rcases lift_spec e h with ⟨hi',hn'⟩, apply eq_of_sub_nilp hi hi', have : e₁ - lift e h = (e₁ - e) - (lift e h - e) := by {rw[sub_sub_sub_cancel_right],}, rw[this],apply is_nilpotent_sub hn ⟨hn'⟩ end /- -------------------------------------------------------- -/ /- rem-lifting -/ def lift' : ∀ (e : A) (h : nilpotent_witness (e * (1 - e))), A := λ e ⟨n,hx⟩, let x := e * (1 - e) in let b := (finset.range n).sum (λ k, ((x ^ k) * nat.choose (2 * k + 1) k)) in e + (2 * e - 1) * b * x lemma lift_eq (e : A) (h : nilpotent_witness (e * (1 - e))) : lift' e h = lift e h := sorry end idempotent /- -------------------------------------------------------- -/ /- prop-poly-el-props -/ namespace poly_el_props open polynomial variables [decidable_eq A] (p : polynomial A) lemma is_invertible_iff : is_invertible p ↔ (is_invertible (coeff p 0)) ∧ (∀ n : ℕ, (is_nilpotent (coeff p n.succ))) := sorry lemma is_regular_bot (n : ℕ) (h₀ : ∀ i, i < n → coeff p i = 0) (h₁ : is_invertible (coeff p n)) : is_regular p := sorry lemma is_regular_top (n : ℕ) (h₀ : ∀ i, i > n → coeff p i = 0) (h₁ : is_invertible (coeff p n)) : is_regular p := sorry lemma is_nilpotent_iff : is_nilpotent p ↔ (∀ n, is_nilpotent (coeff p n)) := sorry lemma is_idempotent_iff : is_idempotent p ↔ (is_idempotent (coeff p 0)) ∧ (∀ n : ℕ, (coeff p n.succ) = 0) := sorry end poly_el_props /- -------------------------------------------------------- -/ /- prop-idempotent-splitting -/ namespace idempotent variables {e : A} (he : is_idempotent e) include he def axis := {b : A // b * e = b} namespace axis def mk (b : A) (hb : b * e = b) : axis he := ⟨b,hb⟩ instance : has_zero (axis he) := ⟨⟨(0 : A),zero_mul e⟩⟩ instance : has_one (axis he) := ⟨⟨e,by {rw[← pow_two],exact he}⟩⟩ instance : has_neg (axis he) := ⟨λ b, axis.mk he (- b.val) (by {rw[← neg_mul_eq_neg_mul,b.property]})⟩ instance : has_add (axis he) := ⟨λ b₁ b₂, axis.mk he (b₁.val + b₂.val) (by {rw[add_mul,b₁.property,b₂.property]})⟩ instance : has_mul (axis he) := ⟨λ b₁ b₂, axis.mk he (b₁.val * b₂.val) (by {rw[mul_assoc,b₂.property]})⟩ @[simp] lemma val_zero : (0 : axis he).val = 0 := rfl @[simp] lemma val_one : (1 : axis he).val = e := rfl @[simp] lemma val_neg (b : axis he) : (- b).val = - (b.val) := rfl @[simp] lemma val_add (b₁ b₂ : axis he) : (b₁ + b₂).val = b₁.val + b₂.val := rfl @[simp] lemma val_mul (b₁ b₂ : axis he) : (b₁ * b₂).val = b₁.val * b₂.val := rfl instance : comm_ring (axis he) := by { refine_struct { zero := has_zero.zero _, one := has_one.one _, neg := has_neg.neg, add := has_add.add, mul := has_mul.mul, .. }; try { rintro ⟨a,ha⟩ }; try { rintro ⟨b,hb⟩ }; try { rintro ⟨c,hc⟩ }; apply subtype.eq, {repeat {rw[val_add]},apply add_assoc}, {rw[val_add,val_zero,zero_add]}, {rw[val_add,val_zero,add_zero]}, {rw[val_add,val_neg,val_zero,neg_add_self]}, {rw[val_add,val_add,add_comm]}, {repeat {rw[val_mul]},rw[mul_assoc]}, {rw[val_mul,val_one,mul_comm,ha]}, {rw[val_mul,val_one,ha]}, {rw[val_mul,val_add,val_add,val_mul,val_mul,mul_add]}, {rw[val_mul,val_add,val_add,val_mul,val_mul,add_mul]}, {rw[val_mul,val_mul,mul_comm]} } def proj : A → axis he := λ a, ⟨a * e,by {dsimp[is_idempotent] at he, rw[mul_assoc,← pow_two,he]}⟩ instance proj_ring_hom : is_ring_hom (proj he) := { map_one := by { apply subtype.eq, change 1 * e = e, rw[one_mul] }, map_add := λ a b, by { apply subtype.eq, change (a + b) * e = a * e + b * e, rw[add_mul]}, map_mul := λ a b, by { dsimp[is_idempotent] at he, rw[pow_two] at he, apply subtype.eq, change (a * b) * e = (a * e) * (b * e), rw[mul_assoc a e,← mul_assoc e b e,mul_comm e b, mul_assoc b e,he,mul_assoc]} } def split : A → (axis he) × (axis (one_sub_mem he)) := λ a, ⟨(proj he a),(proj (one_sub_mem he) a)⟩ instance split_ring_hom : is_ring_hom (split he) := let he' := one_sub_mem he in { map_one := by { ext, exact is_ring_hom.map_one (proj he), exact is_ring_hom.map_one (proj he'),}, map_add := λ a b, by { dsimp[split],ext, exact @is_ring_hom.map_add _ _ _ _ (proj he ) _ a b, exact @is_ring_hom.map_add _ _ _ _ (proj he') _ a b,}, map_mul := λ a b, by { dsimp[split],ext, exact @is_ring_hom.map_mul _ _ _ _ (proj he ) _ a b, exact @is_ring_hom.map_mul _ _ _ _ (proj he') _ a b,}, } lemma mul_eq_zero (b : axis he) (c : axis (one_sub_mem he)) : b.val * c.val = 0 := by { rcases b with ⟨b,hb⟩, rcases c with ⟨c,hc⟩, change b * c = 0, exact calc b * c = (b * e) * (c * (1 - e)) : by rw[hb,hc] ... = b * (e * (1 - e)) * c : by {rw[mul_comm c,mul_assoc,mul_assoc,mul_assoc]} ... = 0 : by {rw[(is_idempotent' e).mp he,mul_zero,zero_mul]} } def combine : (axis he) × (axis (one_sub_mem he)) → A := λ bc, bc.1.val + bc.2.val instance combine_ring_hom : is_ring_hom (combine he) := { map_one := by {change e + (1 - e) = 1,rw[add_sub_cancel'_right]}, map_add := λ bc₁ bc₂, by { rcases bc₁ with ⟨⟨b₁,hb₁⟩,⟨c₁,hc₁⟩⟩, rcases bc₂ with ⟨⟨b₂,hb₂⟩,⟨c₂,hc₂⟩⟩, change (b₁ + b₂) + (c₁ + c₂) = (b₁ + c₁) + (b₂ + c₂), rw[add_assoc,← add_assoc b₂,add_comm b₂ c₁,add_assoc,add_assoc], }, map_mul := λ bc₁ bc₂, by { rcases bc₁ with ⟨⟨b₁,hb₁⟩,⟨c₁,hc₁⟩⟩, rcases bc₂ with ⟨⟨b₂,hb₂⟩,⟨c₂,hc₂⟩⟩, change (b₁ * b₂) + (c₁ * c₂) = (b₁ + c₁) * (b₂ + c₂), have ebc : b₁ * c₂ = 0 := mul_eq_zero he ⟨b₁,hb₁⟩ ⟨c₂,hc₂⟩, have ecb : b₂ * c₁ = 0 := mul_eq_zero he ⟨b₂,hb₂⟩ ⟨c₁,hc₁⟩, rw[mul_comm] at ecb, rw[mul_add,add_mul,add_mul,ebc,ecb,zero_add,add_zero], } } lemma combine_split (a : A) : combine he (split he a) = a := by { change a * e + a * (1 - e) = a, rw[mul_sub,mul_one,add_sub_cancel'_right]} lemma split_combine (bc : (axis he) × (axis (one_sub_mem he))) : split he (combine he bc) = bc := by { have he' : e * (1 - e) = 0 := (is_idempotent' e).mp he, rcases bc with ⟨⟨b,hb⟩,⟨c,hc⟩⟩, ext; apply subtype.eq, {change (b + c) * e = b, rw[← hc,add_mul,hb,mul_assoc,mul_comm (1 - e),he',mul_zero,add_zero],}, {change (b + c) * (1 - e) = c, rw[← hb,add_mul,hc,mul_assoc,he',mul_zero,zero_add],} } end axis end idempotent end sec_elements
7c3a250ad80a09131457b8804e745ddf73e616cf
a537b538f2bea3181e24409d8a52590603d1ddd9
/examples/rubiks_cube.lean
78b1cbdf0fdbf64be3e31dfd18aa9b11d72977ae
[]
no_license
rwbarton/lean-tidy
6134813ded72b275d19d4d32514dba80c21708e3
fe1125d32adb60decda7a77d0f679614ba9f6fbb
refs/heads/master
1,585,549,718,705
1,538,120,619,000
1,538,120,624,000
150,864,330
0
0
null
1,538,225,790,000
1,538,225,790,000
null
UTF-8
Lean
false
false
17,572
lean
import tidy.tidy abbreviation C := ℕ variables X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 : C constant Rubik : C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C → C @[ematch] constant Rubik_1 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_3 X_5 X_8 X_2 X_7 X_1 X_4 X_6 X_33 X_34 X_35 X_12 X_13 X_14 X_15 X_16 X_9 X_10 X_11 X_20 X_21 X_22 X_23 X_24 X_17 X_18 X_19 X_28 X_29 X_30 X_31 X_32 X_25 X_26 X_27 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 @[ematch] constant Rubik_2 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_17 X_2 X_3 X_20 X_5 X_22 X_7 X_8 X_11 X_13 X_16 X_10 X_15 X_9 X_12 X_14 X_41 X_18 X_19 X_44 X_21 X_46 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_6 X_36 X_4 X_38 X_39 X_1 X_40 X_42 X_43 X_37 X_45 X_35 X_47 X_48 @[ematch] constant Rubik_3 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_3 X_4 X_5 X_25 X_28 X_30 X_9 X_10 X_8 X_12 X_7 X_14 X_15 X_6 X_19 X_21 X_24 X_18 X_23 X_17 X_20 X_22 X_43 X_26 X_27 X_42 X_29 X_41 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_11 X_13 X_16 X_44 X_45 X_46 X_47 X_48 @[ematch] constant Rubik_4 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_38 X_4 X_36 X_6 X_7 X_33 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_3 X_20 X_5 X_22 X_23 X_8 X_27 X_29 X_32 X_26 X_31 X_25 X_28 X_30 X_48 X_34 X_35 X_45 X_37 X_43 X_39 X_40 X_41 X_42 X_19 X_44 X_21 X_46 X_47 X_24 @[ematch] constant Rubik_5 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_14 X_12 X_9 X_4 X_5 X_6 X_7 X_8 X_46 X_10 X_11 X_47 X_13 X_48 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_1 X_28 X_2 X_30 X_31 X_3 X_35 X_37 X_40 X_34 X_39 X_33 X_36 X_38 X_41 X_42 X_43 X_44 X_45 X_32 X_29 X_27 @[ematch] constant Rubik_6 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_22 X_23 X_24 X_17 X_18 X_19 X_20 X_21 X_30 X_31 X_32 X_25 X_26 X_27 X_28 X_29 X_38 X_39 X_40 X_33 X_34 X_35 X_36 X_37 X_14 X_15 X_16 X_43 X_45 X_48 X_42 X_47 X_41 X_44 X_46 -- @[ematch] constant Rubik_7 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_6 X_4 X_1 X_7 X_2 X_8 X_5 X_3 X_17 X_18 X_19 X_12 X_13 X_14 X_15 X_16 X_25 X_26 X_27 X_20 X_21 X_22 X_23 X_24 X_33 X_34 X_35 X_28 X_29 X_30 X_31 X_32 X_9 X_10 X_11 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 -- @[ematch] constant Rubik_8 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_40 X_2 X_3 X_37 X_5 X_35 X_7 X_8 X_14 X_12 X_9 X_15 X_10 X_16 X_13 X_11 X_1 X_18 X_19 X_4 X_21 X_6 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_46 X_36 X_44 X_38 X_39 X_41 X_17 X_42 X_43 X_20 X_45 X_22 X_47 X_48 -- @[ematch] constant Rubik_9 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_3 X_4 X_5 X_16 X_13 X_11 X_9 X_10 X_41 X_12 X_42 X_14 X_15 X_43 X_22 X_20 X_17 X_23 X_18 X_24 X_21 X_19 X_6 X_26 X_27 X_7 X_29 X_8 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_30 X_28 X_25 X_44 X_45 X_46 X_47 X_48 -- @[ematch] constant Rubik_10 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_19 X_4 X_21 X_6 X_7 X_24 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_43 X_20 X_45 X_22 X_23 X_48 X_30 X_28 X_25 X_31 X_26 X_32 X_29 X_27 X_8 X_34 X_35 X_5 X_37 X_3 X_39 X_40 X_41 X_42 X_38 X_44 X_36 X_46 X_47 X_33 -- @[ematch] constant Rubik_11 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_27 X_29 X_32 X_4 X_5 X_6 X_7 X_8 X_3 X_10 X_11 X_2 X_13 X_1 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_48 X_28 X_47 X_30 X_31 X_46 X_38 X_36 X_33 X_39 X_34 X_40 X_37 X_35 X_41 X_42 X_43 X_44 X_45 X_9 X_12 X_14 -- @[ematch] constant Rubik_12 : Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_14 X_15 X_16 X_17 X_18 X_19 X_20 X_21 X_22 X_23 X_24 X_25 X_26 X_27 X_28 X_29 X_30 X_31 X_32 X_33 X_34 X_35 X_36 X_37 X_38 X_39 X_40 X_41 X_42 X_43 X_44 X_45 X_46 X_47 X_48 = Rubik X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8 X_9 X_10 X_11 X_12 X_13 X_38 X_39 X_40 X_17 X_18 X_19 X_20 X_21 X_14 X_15 X_16 X_25 X_26 X_27 X_28 X_29 X_22 X_23 X_24 X_33 X_34 X_35 X_36 X_37 X_30 X_31 X_32 X_46 X_44 X_41 X_47 X_42 X_48 X_45 X_43 lemma Rubik_test_1 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 0 0 0 0 0 0 0 4 4 4 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 5 := begin -- rewrite_search_using [`ematch], rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, rw Rubik_2, rw ← Rubik_2, rw Rubik_3, rw ← Rubik_3, sorry end -- lemma Rubik_test_2 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 1 0 0 1 0 1 0 0 4 4 4 5 1 5 1 1 5 1 1 2 2 2 2 2 2 2 2 3 0 3 3 0 3 3 0 4 4 4 4 4 5 5 5 5 5 3 3 3 := by rewrite_search_using [`ematch] {visualiser:=tt} -- lemma Rubik_test_3 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 0 1 0 4 3 3 4 1 1 0 1 0 2 2 2 2 2 0 2 0 5 3 3 3 3 4 3 4 5 5 4 5 4 4 5 4 5 1 0 1 5 2 1 2 1 5 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_4 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 1 4 2 4 2 3 4 0 0 0 1 1 5 5 5 1 2 1 5 0 3 2 3 0 0 0 3 3 5 5 5 3 4 3 5 0 1 4 1 4 1 2 4 2 4 3 2 := by rewrite_search_using [`ematch] -- lemma Rubik_test_5 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 0 0 2 5 2 5 5 3 3 3 1 1 1 1 1 0 2 4 5 4 5 2 4 3 3 1 3 1 3 3 1 2 4 5 2 0 2 4 0 4 0 0 4 0 4 5 5 := by rewrite_search_using [`ematch] -- lemma Rubik_test_6 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 3 3 3 1 4 1 0 5 2 0 0 5 1 2 1 1 4 4 3 5 3 5 2 2 2 3 4 2 0 1 5 4 0 0 0 2 4 5 5 1 4 1 0 4 2 5 3 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_7 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 4 0 0 2 0 1 3 4 3 1 0 3 0 5 4 0 4 2 3 4 3 3 1 1 5 1 1 5 3 5 2 2 2 2 0 4 0 5 5 3 2 4 4 5 5 2 1 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_8 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 1 5 3 4 3 0 4 0 4 4 2 1 2 1 1 0 2 1 0 0 0 3 5 0 0 1 3 1 3 5 2 4 4 3 5 5 5 3 5 2 2 4 2 4 3 5 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_9 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 5 2 5 2 1 2 4 3 1 0 3 0 0 3 5 4 0 0 4 0 4 4 2 3 1 1 1 3 1 5 4 5 4 5 2 5 5 3 2 1 1 0 0 2 3 4 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_10 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 5 1 2 2 5 4 0 0 4 3 3 4 3 4 2 2 5 4 3 4 5 3 2 0 4 4 0 1 3 3 2 2 1 0 1 0 1 5 5 1 5 5 2 0 1 0 3 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_20 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 1 0 5 1 5 3 5 3 2 4 5 5 5 0 4 2 4 2 4 4 1 1 3 1 0 3 1 2 2 4 2 3 4 1 5 0 1 5 4 2 0 0 0 0 3 3 3 2 := by rewrite_search_using [`ematch] -- lemma Rubik_test_30 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 5 3 4 0 4 3 2 5 1 1 4 4 0 3 2 1 0 5 1 4 3 0 5 4 4 3 1 5 1 5 1 3 0 0 2 4 5 2 2 2 2 1 3 3 2 5 0 0 := by rewrite_search_using [`ematch] -- lemma Rubik_test_40 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 4 5 4 1 0 5 3 4 0 2 1 3 0 1 4 3 3 2 4 5 0 2 3 5 2 2 1 2 4 2 5 1 5 3 3 0 4 3 1 1 0 5 4 5 2 0 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_50 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 1 0 1 4 4 3 5 0 5 1 4 1 0 3 3 0 0 3 2 2 1 2 5 5 3 0 0 2 1 1 2 3 4 3 4 5 0 5 5 5 1 2 2 4 3 4 4 2 := by rewrite_search_using [`ematch] -- lemma Rubik_test_60 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 4 5 1 3 5 5 1 0 5 5 3 4 4 1 3 4 2 4 2 0 2 3 4 1 3 2 5 0 3 0 2 4 2 1 3 2 3 1 0 0 0 5 2 0 1 4 1 5 := by rewrite_search_using [`ematch] -- lemma Rubik_test_70 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 3 1 5 4 5 4 0 1 4 3 4 2 1 5 2 2 1 3 5 0 1 2 5 4 3 4 1 2 3 2 3 5 5 4 0 0 2 3 5 0 3 4 1 1 2 0 0 := by rewrite_search_using [`ematch] -- lemma Rubik_test_80 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 5 0 2 0 1 0 4 1 2 3 3 4 3 5 1 5 4 5 5 4 2 4 2 0 2 2 3 5 5 1 2 1 0 1 3 3 1 0 4 4 1 3 4 5 0 3 0 2 := by rewrite_search_using [`ematch] -- lemma Rubik_test_90 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 1 3 1 3 2 4 4 3 2 0 3 4 4 1 5 0 5 1 2 0 0 3 2 0 5 1 4 1 5 1 5 3 5 4 5 2 5 0 2 2 2 0 4 3 1 0 3 4 := by rewrite_search_using [`ematch] -- lemma Rubik_test_100 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 2 3 2 1 3 5 1 1 3 4 1 0 2 1 1 0 1 4 3 4 2 0 0 5 0 5 0 3 2 5 5 4 5 4 4 4 2 3 3 5 2 1 2 4 0 5 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_200 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 3 3 1 1 1 1 2 0 0 2 2 3 4 3 5 0 4 1 5 5 3 5 1 5 2 2 1 3 4 2 4 5 2 3 0 5 1 0 0 4 4 0 4 0 3 4 5 := by rewrite_search_using [`ematch] -- lemma Rubik_test_300 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 1 3 4 3 4 1 0 1 3 1 2 4 0 4 3 0 5 3 5 2 5 1 5 4 2 5 5 0 4 3 2 4 4 0 3 0 5 2 2 2 0 1 0 5 3 1 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_400 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 4 5 4 0 3 0 0 4 1 0 5 5 5 1 0 2 3 4 3 3 2 0 1 1 1 1 4 4 2 2 5 4 0 3 5 2 3 1 3 1 2 5 5 3 2 2 4 := by rewrite_search_using [`ematch] -- lemma Rubik_test_500 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 2 2 3 2 0 1 3 5 5 0 2 2 1 2 5 0 0 4 1 0 1 4 1 5 4 3 0 4 2 3 4 4 2 3 1 5 1 0 0 5 3 5 4 3 5 3 4 1 := by rewrite_search_using [`ematch] -- lemma Rubik_test_600 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 4 4 4 3 0 1 5 5 1 0 5 0 2 0 3 2 2 1 3 3 5 0 4 4 2 2 0 2 5 5 2 1 3 1 5 3 4 4 0 1 3 5 3 4 1 2 1 0 := by rewrite_search_using [`ematch] -- lemma Rubik_test_700 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 5 0 3 4 4 5 2 4 2 3 2 1 1 5 4 0 1 3 0 5 2 2 4 1 3 1 5 5 3 4 0 2 4 3 3 5 0 0 1 1 1 0 0 5 2 4 2 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_800 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 1 5 3 0 5 3 5 2 4 1 0 5 3 1 0 4 5 3 4 5 1 1 0 2 1 2 2 3 3 3 4 1 5 1 0 2 3 0 5 4 2 2 4 2 4 4 0 := by rewrite_search_using [`ematch] -- lemma Rubik_test_900 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 0 0 5 0 4 1 1 0 2 2 4 2 1 4 2 2 5 5 2 0 4 5 2 3 3 5 4 1 0 0 3 5 3 3 1 4 3 2 5 1 1 5 4 1 4 0 3 3 := by rewrite_search_using [`ematch] -- lemma Rubik_test_1000 : Rubik 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 = Rubik 3 2 4 0 5 5 5 0 4 4 2 4 3 3 2 5 1 1 2 5 1 2 0 4 3 4 1 2 4 1 2 1 0 5 5 3 1 0 1 4 3 3 5 3 0 0 0 2 := by rewrite_search_using [`ematch]
88d4a5c50ec58201479e83f34e28cbc5cf4a9186
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/measure_theory/borel_space.lean
c5a56aa3f43a1db8b821f53dc6a678e1ebbc36c4
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
40,962
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import measure_theory.measure_space import analysis.normed_space.finite_dimension /-! # Borel (measurable) space ## Main definitions * `borel α` : the least `σ`-algebra that contains all open sets; * `class borel_space` : a space with `topological_space` and `measurable_space` structures such that `‹measurable_space α› = borel α`; * `class opens_measurable_space` : a space with `topological_space` and `measurable_space` structures such that all open sets are measurable; equivalently, `borel α ≤ ‹measurable_space α›`. * `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`; * `measurable` and `borel_space` instances on `ℝ`, `ℝ≥0`, `ennreal`. * A measure is `regular` if it is finite on compact sets, inner regular and outer regular. ## Main statements * `is_open.is_measurable`, `is_closed.is_measurable`: open and closed sets are measurable; * `continuous.measurable` : a continuous function is measurable; * `continuous.measurable2` : if `f : α → β` and `g : α → γ` are measurable and `op : β × γ → δ` is continuous, then `λ x, op (f x, g y)` is measurable; * `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates, and similarly for `dist` and `edist`; * `measurable.ennreal*` : special cases for arithmetic operations on `ennreal`s. -/ noncomputable theory open classical set open_locale classical big_operators topological_space universes u v w x y variables {α β γ δ : Type*} {ι : Sort y} {s t u : set α} open measurable_space topological_space /-- `measurable_space` structure generated by `topological_space`. -/ def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_top_of_discrete [topological_space α] [discrete_topology α] : borel α = ⊤ := top_le_iff.1 $ λ s hs, generate_measurable.basic s (is_open_discrete s) lemma borel_eq_top_of_encodable [topological_space α] [t1_space α] [encodable α] : borel α = ⊤ := begin refine (top_le_iff.1 $ λ s hs, bUnion_of_singleton s ▸ _), apply is_measurable.bUnion s.countable_encodable, intros x hx, apply is_measurable.of_compl, apply generate_measurable.basic, exact is_closed_singleton end lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @is_measurable.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @is_measurable.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @is_measurable.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma is_pi_system_is_open [topological_space α] : is_pi_system (is_open : set α → Prop) := λ s t hs ht hst, is_open_inter hs ht section order_topology variable (α) variables [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] lemma borel_eq_generate_Iio : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals α _ _ _), letI : measurable_space α := measurable_space.generate_from (range Iio), have H : ∀ a:α, is_measurable (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = (Iio a')ᶜ), { exact (H _).compl }, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, (Iio x.1.1)ᶜ, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl } }, { rw forall_range_iff, intro a, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi : borel α = generate_from (range Ioi) := @borel_eq_generate_Iio (order_dual α) _ (by apply_instance : second_countable_topology α) _ _ end order_topology lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := comap_generate_from.symm lemma continuous.borel_measurable [topological_space α] [topological_space β] {f : α → β} (hf : continuous f) : @measurable α β (borel α) (borel β) f := measurable.of_le_map $ generate_from_le $ λ s hs, generate_measurable.basic (f ⁻¹' s) (hf s hs) /-- A space with `measurable_space` and `topological_space` structures such that all open sets are measurable. -/ class opens_measurable_space (α : Type*) [topological_space α] [h : measurable_space α] : Prop := (borel_le : borel α ≤ h) /-- A space with `measurable_space` and `topological_space` structures such that the `σ`-algebra of measurable sets is exactly the `σ`-algebra generated by open sets. -/ class borel_space (α : Type*) [topological_space α] [measurable_space α] : Prop := (measurable_eq : ‹measurable_space α› = borel α) /-- In a `borel_space` all open sets are measurable. -/ @[priority 100] instance borel_space.opens_measurable {α : Type*} [topological_space α] [measurable_space α] [borel_space α] : opens_measurable_space α := ⟨ge_of_eq $ borel_space.measurable_eq⟩ instance subtype.borel_space {α : Type*} [topological_space α] [measurable_space α] [hα : borel_space α] (s : set α) : borel_space s := ⟨by { rw [hα.1, subtype.measurable_space, ← borel_comap], refl }⟩ instance subtype.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α] [h : opens_measurable_space α] (s : set α) : opens_measurable_space s := ⟨by { rw [borel_comap], exact comap_mono h.1 }⟩ section variables [topological_space α] [measurable_space α] [opens_measurable_space α] [topological_space β] [measurable_space β] [opens_measurable_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [measurable_space δ] lemma is_open.is_measurable (h : is_open s) : is_measurable s := opens_measurable_space.borel_le _ $ generate_measurable.basic _ h lemma is_measurable_interior : is_measurable (interior s) := is_open_interior.is_measurable lemma is_closed.is_measurable (h : is_closed s) : is_measurable s := is_measurable.compl_iff.1 $ h.is_measurable lemma is_compact.is_measurable [t2_space α] (h : is_compact s) : is_measurable s := h.is_closed.is_measurable lemma is_measurable_closure : is_measurable (closure s) := is_closed_closure.is_measurable lemma measurable_of_is_open {f : δ → γ} (hf : ∀ s, is_open s → is_measurable (f ⁻¹' s)) : measurable f := by { rw [‹borel_space γ›.measurable_eq], exact measurable_generate_from hf } lemma measurable_of_is_closed {f : δ → γ} (hf : ∀ s, is_closed s → is_measurable (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_open, intros s hs, rw [← is_measurable.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs end lemma measurable_of_is_closed' {f : δ → γ} (hf : ∀ s, is_closed s → s.nonempty → s ≠ univ → is_measurable (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_closed, intros s hs, cases eq_empty_or_nonempty s with h1 h1, { simp [h1] }, by_cases h2 : s = univ, { simp [h2] }, exact hf s hs h1 h2 end instance nhds_is_measurably_generated (a : α) : (𝓝 a).is_measurably_generated := begin rw [nhds, infi_subtype'], refine @filter.infi_is_measurably_generated _ _ _ _ (λ i, _), exact i.2.2.is_measurable.principal_is_measurably_generated end /-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for each `a`. This cannot be an `instance` because it depends on a non-instance `hs : is_measurable s`. -/ lemma is_measurable.nhds_within_is_measurably_generated {s : set α} (hs : is_measurable s) (a : α) : (𝓝[s] a).is_measurably_generated := by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _ @[priority 100] -- see Note [lower instance priority] instance opens_measurable_space.to_measurable_singleton_class [t1_space α] : measurable_singleton_class α := ⟨λ x, is_closed_singleton.is_measurable⟩ instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] : opens_measurable_space (α × β) := begin refine ⟨_⟩, rcases is_open_generated_countable_inter α with ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩, rcases is_open_generated_countable_inter β with ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩, have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v}, { rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨u, hu, v, hv, rfl⟩, have hu : is_open u, by { rw [ha₅], exact generate_open.basic _ hu }, have hv : is_open v, by { rw [hb₅], exact generate_open.basic _ hv }, exact hu.is_measurable.prod hv.is_measurable end section preorder variables [preorder α] [order_closed_topology α] {a b : α} lemma is_measurable_Ici : is_measurable (Ici a) := is_closed_Ici.is_measurable lemma is_measurable_Iic : is_measurable (Iic a) := is_closed_Iic.is_measurable lemma is_measurable_Icc : is_measurable (Icc a b) := is_closed_Icc.is_measurable instance nhds_within_Ici_is_measurably_generated : (𝓝[Ici b] a).is_measurably_generated := is_measurable_Ici.nhds_within_is_measurably_generated _ instance nhds_within_Iic_is_measurably_generated : (𝓝[Iic b] a).is_measurably_generated := is_measurable_Iic.nhds_within_is_measurably_generated _ instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (is_measurable_Ici : is_measurable (Ici a)).principal_is_measurably_generated instance at_bot_is_measurably_generated : (filter.at_bot : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (is_measurable_Iic : is_measurable (Iic a)).principal_is_measurably_generated end preorder section partial_order variables [partial_order α] [order_closed_topology α] [second_countable_topology α] {a b : α} lemma is_measurable_le' : is_measurable {p : α × α | p.1 ≤ p.2} := order_closed_topology.is_closed_le'.is_measurable lemma is_measurable_le {f g : δ → α} (hf : measurable f) (hg : measurable g) : is_measurable {a | f a ≤ g a} := hf.prod_mk hg is_measurable_le' end partial_order section linear_order variables [linear_order α] [order_closed_topology α] {a b : α} lemma is_measurable_Iio : is_measurable (Iio a) := is_open_Iio.is_measurable lemma is_measurable_Ioi : is_measurable (Ioi a) := is_open_Ioi.is_measurable lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_open_Ioo.is_measurable lemma is_measurable_Ioc : is_measurable (Ioc a b) := is_measurable_Ioi.inter is_measurable_Iic lemma is_measurable_Ico : is_measurable (Ico a b) := is_measurable_Ici.inter is_measurable_Iio instance nhds_within_Ioi_is_measurably_generated : (𝓝[Ioi b] a).is_measurably_generated := is_measurable_Ioi.nhds_within_is_measurably_generated _ instance nhds_within_Iio_is_measurably_generated : (𝓝[Iio b] a).is_measurably_generated := is_measurable_Iio.nhds_within_is_measurably_generated _ variables [second_countable_topology α] lemma is_measurable_lt' : is_measurable {p : α × α | p.1 < p.2} := (is_open_lt continuous_fst continuous_snd).is_measurable lemma is_measurable_lt {f g : δ → α} (hf : measurable f) (hg : measurable g) : is_measurable {a | f a < g a} := hf.prod_mk hg is_measurable_lt' end linear_order section decidable_linear_order variables [decidable_linear_order α] [order_closed_topology α] lemma is_measurable_interval {a b : α} : is_measurable (interval a b) := is_measurable_Icc variables [second_countable_topology α] lemma measurable.max {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λa, max (f a) (g a)) := hf.piecewise (is_measurable_le hg hf) hg lemma measurable.min {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λa, min (f a) (g a)) := hf.piecewise (is_measurable_le hf hg) hg end decidable_linear_order /-- A continuous function from an `opens_measurable_space` to a `borel_space` is measurable. -/ lemma continuous.measurable {f : α → γ} (hf : continuous f) : measurable f := hf.borel_measurable.mono opens_measurable_space.borel_le (le_of_eq $ borel_space.measurable_eq) /-- A homeomorphism between two Borel spaces is a measurable equivalence.-/ def homeomorph.to_measurable_equiv {α : Type*} {β : Type*} [topological_space α] [measurable_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] (h : α ≃ₜ β) : measurable_equiv α β := { measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable, .. h } lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α) (hf : continuous_on f {x | x ≠ a}) : measurable f := measurable_of_measurable_on_compl_singleton a (continuous_on_iff_continuous_restrict.1 hf).measurable lemma continuous.measurable2 [second_countable_topology α] [second_countable_topology β] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λp:α×β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λa, c (f a) (g a)) := h.measurable.comp (hf.prod_mk hg) lemma measurable.smul [semiring α] [second_countable_topology α] [add_comm_monoid γ] [second_countable_topology γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → α} {g : δ → γ} (hf : measurable f) (hg : measurable g) : measurable (λ c, f c • g c) := continuous_smul.measurable2 hf hg lemma measurable.const_smul {R M : Type*} [topological_space R] [semiring R] [add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M] [measurable_space M] [borel_space M] {f : δ → M} (hf : measurable f) (c : R) : measurable (λ x, c • f x) := (continuous_const.smul continuous_id).measurable.comp hf lemma measurable_const_smul_iff {α : Type*} [topological_space α] [division_ring α] [add_comm_monoid γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → γ} {c : α} (hc : c ≠ 0) : measurable (λ x, c • f x) ↔ measurable f := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹, λ h, h.const_smul c⟩ lemma measurable.const_mul {R : Type*} [topological_space R] [measurable_space R] [borel_space R] [semiring R] [topological_semiring R] {f : δ → R} (hf : measurable f) (c : R) : measurable (λ x, c * f x) := hf.const_smul c lemma measurable.mul_const {R : Type*} [topological_space R] [measurable_space R] [borel_space R] [semiring R] [topological_semiring R] {f : δ → R} (hf : measurable f) (c : R) : measurable (λ x, f x * c) := (continuous_id.mul continuous_const).measurable.comp hf end section borel_space variables [topological_space α] [measurable_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [measurable_space δ] lemma prod_le_borel_prod : prod.measurable_space ≤ borel (α × β) := begin rw [‹borel_space α›.measurable_eq, ‹borel_space β›.measurable_eq], refine sup_le _ _, { exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable }, { exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable } end instance prod.borel_space [second_countable_topology α] [second_countable_topology β] : borel_space (α × β) := ⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩ @[to_additive] lemma measurable_mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] : measurable (λ p : α × α, p.1 * p.2) := continuous_mul.measurable @[to_additive] lemma measurable.mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] {f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (λa, f a * g a) := continuous_mul.measurable2 /-- A variant of `measurable.mul` that uses `*` on functions -/ @[to_additive] lemma measurable.mul' [has_mul α] [has_continuous_mul α] [second_countable_topology α] {f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (f * g) := measurable.mul @[to_additive] lemma measurable_mul_left [has_mul α] [has_continuous_mul α] (x : α) : measurable (λ y : α, x * y) := continuous.measurable $ continuous_const.mul continuous_id @[to_additive] lemma measurable_mul_right [has_mul α] [has_continuous_mul α] (x : α) : measurable (λ y : α, y * x) := continuous.measurable $ continuous_id.mul continuous_const @[to_additive] lemma finset.measurable_prod {ι : Type*} [comm_monoid α] [has_continuous_mul α] [second_countable_topology α] {f : ι → δ → α} (s : finset ι) (hf : ∀i, measurable (f i)) : measurable (λa, ∏ i in s, f i a) := finset.induction_on s (by simp only [finset.prod_empty, measurable_const]) (assume i s his ih, by simpa [his] using (hf i).mul ih) @[to_additive] lemma measurable_inv [group α] [topological_group α] : measurable (has_inv.inv : α → α) := continuous_inv.measurable @[to_additive] lemma measurable.inv [group α] [topological_group α] {f : δ → α} (hf : measurable f) : measurable (λa, (f a)⁻¹) := measurable_inv.comp hf lemma measurable_inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] : measurable (has_inv.inv : α → α) := measurable_of_continuous_on_compl_singleton 0 normed_field.continuous_on_inv lemma measurable.inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] {f : δ → α} (hf : measurable f) : measurable (λa, (f a)⁻¹) := measurable_inv'.comp hf @[to_additive] lemma measurable.of_inv [group α] [topological_group α] {f : δ → α} (hf : measurable (λ a, (f a)⁻¹)) : measurable f := by simpa only [inv_inv] using hf.inv @[simp, to_additive] lemma measurable_inv_iff [group α] [topological_group α] {f : δ → α} : measurable (λ a, (f a)⁻¹) ↔ measurable f := ⟨measurable.of_inv, measurable.inv⟩ lemma measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α] {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ x, f x - g x) := hf.add hg.neg lemma measurable_comp_iff_of_closed_embedding {f : δ → β} (g : β → γ) (hg : closed_embedding g) : measurable (g ∘ f) ↔ measurable f := begin refine ⟨λ hf, _, λ hf, hg.continuous.measurable.comp hf⟩, apply measurable_of_is_closed, intros s hs, convert hf (hg.is_closed_map s hs).is_measurable, rw [@preimage_comp _ _ _ f g, preimage_image_eq _ hg.to_embedding.inj] end section linear_order variables [linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_of_Iio {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Iio x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Iio _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Ioi {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Ioi x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Ioi _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Iic {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Iic x)) : measurable f := begin apply measurable_of_Ioi, simp_rw [← compl_Iic, preimage_compl, is_measurable.compl_iff], assumption end lemma measurable_of_Ici {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Ici x)) : measurable f := begin apply measurable_of_Iio, simp_rw [← compl_Ici, preimage_compl, is_measurable.compl_iff], assumption end lemma measurable.is_lub {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_lub (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_Ioi α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp only [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists], exact is_measurable.Union (λ i, hf i (is_open_lt' _).is_measurable) end lemma measurable.is_glb {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_glb (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_Iio α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp only [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists], exact is_measurable.Union (λ i, hf i (is_open_gt' _).is_measurable) end end linear_order lemma measurable.supr_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end) lemma measurable.infi_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨅ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end) section complete_linear_order variables [complete_linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_supr {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr lemma measurable_infi {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi lemma measurable_bsupr {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact measurable_supr (λ i, hf i) } lemma measurable_binfi {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact measurable_infi (λ i, hf i) } end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] [second_countable_topology α] [order_topology α] lemma measurable_cSup {ι} {f : ι → δ → α} {s : set ι} (hs : s.countable) (hf : ∀ i, measurable (f i)) (bdd : ∀ x, bdd_above ((λ i, f i x) '' s)) : measurable (λ x, Sup ((λ i, f i x) '' s)) := begin cases eq_empty_or_nonempty s with h2s h2s, { simp [h2s, measurable_const] }, { apply measurable_of_Iic, intro y, simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall], exact is_measurable.bInter hs (λ i hi, is_measurable_le (hf i) measurable_const) } end end conditionally_complete_linear_order /-- Convert a `homeomorph` to a `measurable_equiv`. -/ def homemorph.to_measurable_equiv (h : α ≃ₜ β) : measurable_equiv α β := { to_equiv := h.to_equiv, measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable } end borel_space instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩ instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩ instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩ instance nat.borel_space : borel_space ℕ := ⟨borel_eq_top_of_discrete.symm⟩ instance int.borel_space : borel_space ℤ := ⟨borel_eq_top_of_discrete.symm⟩ instance rat.borel_space : borel_space ℚ := ⟨borel_eq_top_of_encodable.symm⟩ instance real.measurable_space : measurable_space ℝ := borel ℝ instance real.borel_space : borel_space ℝ := ⟨rfl⟩ instance nnreal.measurable_space : measurable_space nnreal := borel nnreal instance nnreal.borel_space : borel_space nnreal := ⟨rfl⟩ instance ennreal.measurable_space : measurable_space ennreal := borel ennreal instance ennreal.borel_space : borel_space ennreal := ⟨rfl⟩ instance complex.measurable_space : measurable_space ℂ := borel ℂ instance complex.borel_space : borel_space ℂ := ⟨rfl⟩ section metric_space variables [metric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ℝ} open metric lemma is_measurable_ball : is_measurable (metric.ball x ε) := metric.is_open_ball.is_measurable lemma is_measurable_closed_ball : is_measurable (metric.closed_ball x ε) := metric.is_closed_ball.is_measurable lemma measurable_inf_dist {s : set α} : measurable (λ x, inf_dist x s) := (continuous_inf_dist_pt s).measurable lemma measurable.inf_dist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_dist (f x) s) := measurable_inf_dist.comp hf variables [second_countable_topology α] lemma measurable_dist : measurable (λp:α×α, dist p.1 p.2) := continuous_dist.measurable lemma measurable.dist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) := continuous_dist.measurable2 hf hg lemma measurable_nndist : measurable (λp:α×α, nndist p.1 p.2) := continuous_nndist.measurable lemma measurable.nndist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, nndist (f b) (g b)) := continuous_nndist.measurable2 hf hg end metric_space section emetric_space variables [emetric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ennreal} open emetric lemma is_measurable_eball : is_measurable (emetric.ball x ε) := emetric.is_open_ball.is_measurable lemma measurable_edist_right : measurable (edist x) := (continuous_const.edist continuous_id).measurable lemma measurable_edist_left : measurable (λ y, edist y x) := (continuous_id.edist continuous_const).measurable lemma measurable_inf_edist {s : set α} : measurable (λ x, inf_edist x s) := continuous_inf_edist.measurable lemma measurable.inf_edist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_edist (f x) s) := measurable_inf_edist.comp hf variables [second_countable_topology α] lemma measurable_edist : measurable (λp:α×α, edist p.1 p.2) := continuous_edist.measurable lemma measurable.edist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, edist (f b) (g b)) := continuous_edist.measurable2 hf hg end emetric_space namespace real open measurable_space measure_theory lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma measure_ext_Ioo_rat {μ ν : measure ℝ} [locally_finite_measure μ] (h : ∀ a b : ℚ, μ (Ioo a b) = ν (Ioo a b)) : μ = ν := begin refine measure.ext_of_generate_from_of_cover_subset borel_eq_generate_from_Ioo_rat _ (subset.refl _) _ _ _ _, { simp only [is_pi_system, mem_Union, mem_singleton_iff], rintros _ _ ⟨a₁, b₁, h₁, rfl⟩ ⟨a₂, b₂, h₂, rfl⟩ ne, simp only [Ioo_inter_Ioo, sup_eq_max, inf_eq_min, ← rat.cast_max, ← rat.cast_min, nonempty_Ioo] at ne ⊢, refine ⟨_, _, _, rfl⟩, assumption_mod_cast }, { exact countable_Union (λ a, (countable_encodable _).bUnion $ λ _ _, countable_singleton _) }, { exact is_topological_basis_Ioo_rat.2.1 }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, h, rfl⟩, refine (measure_mono subset_closure).trans_lt _, rw [closure_Ioo], exacts [compact_Icc.finite_measure, rat.cast_lt.2 h] }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, hab, rfl⟩, exact h a b } end lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃a:ℚ, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union], rintro ⟨a, b, h, H⟩, rw [mem_singleton_iff.1 H], rw (set.ext (λ x, _) : Ioo (a:ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b), { have hg : ∀ q : ℚ, g.is_measurable' (Iio q) := λ q, generate_measurable.basic (Iio q) (by { simp, exact ⟨_, rfl⟩ }), refine @is_measurable.inter _ g _ _ _ (hg _), refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @is_measurable.compl _ _ g (hg _) }, { simp [Ioo, Iio], refine and_congr _ iff.rfl, exact ⟨λ h, let ⟨c, ac, cx⟩ := exists_rat_btwn h in ⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩, λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } }, { simp, rintro r rfl, exact is_open_Iio.is_measurable } end end real variable [measurable_space α] lemma measurable.sub_nnreal {f g : α → nnreal} : measurable f → measurable g → measurable (λ a, f a - g a) := nnreal.continuous_sub.measurable2 lemma measurable.nnreal_of_real {f : α → ℝ} (hf : measurable f) : measurable (λ x, nnreal.of_real (f x)) := nnreal.continuous_of_real.measurable.comp hf lemma nnreal.measurable_coe : measurable (coe : nnreal → ℝ) := nnreal.continuous_coe.measurable lemma measurable.nnreal_coe {f : α → nnreal} (hf : measurable f) : measurable (λ x, (f x : ℝ)) := nnreal.measurable_coe.comp hf lemma measurable.ennreal_coe {f : α → nnreal} (hf : measurable f) : measurable (λ x, (f x : ennreal)) := ennreal.continuous_coe.measurable.comp hf lemma measurable.ennreal_of_real {f : α → ℝ} (hf : measurable f) : measurable (λ x, ennreal.of_real (f x)) := ennreal.continuous_of_real.measurable.comp hf /-- The set of finite `ennreal` numbers is `measurable_equiv` to `nnreal`. -/ def measurable_equiv.ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r ≠ ⊤} nnreal := ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv namespace ennreal open filter lemma measurable_coe : measurable (coe : nnreal → ennreal) := measurable_id.ennreal_coe lemma measurable_of_measurable_nnreal {f : ennreal → α} (h : measurable (λp:nnreal, f p)) : measurable f := measurable_of_measurable_on_compl_singleton ⊤ (measurable_equiv.ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) /-- `ennreal` is `measurable_equiv` to `nnreal ⊕ unit`. -/ def ennreal_equiv_sum : measurable_equiv ennreal (nnreal ⊕ unit) := { measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤), .. equiv.option_equiv_sum_punit nnreal } open function (uncurry) lemma measurable_of_measurable_nnreal_prod [measurable_space β] [measurable_space γ] {f : ennreal × β → γ} (H₁ : measurable (λ p : nnreal × β, f (p.1, p.2))) (H₂ : measurable (λ x, f (⊤, x))) : measurable f := let e : measurable_equiv (ennreal × β) (nnreal × β ⊕ unit × β) := (ennreal_equiv_sum.prod_congr (measurable_equiv.refl β)).trans (measurable_equiv.sum_prod_distrib _ _ _) in e.symm.measurable_coe_iff.1 $ measurable_sum H₁ (H₂.comp measurable_id.snd) lemma measurable_of_measurable_nnreal_nnreal [measurable_space β] {f : ennreal × ennreal → β} (h₁ : measurable (λp:nnreal × nnreal, f (p.1, p.2))) (h₂ : measurable (λr:nnreal, f (⊤, r))) (h₃ : measurable (λr:nnreal, f (r, ⊤))) : measurable f := measurable_of_measurable_nnreal_prod (measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃) (measurable_of_measurable_nnreal h₂) lemma measurable_of_real : measurable ennreal.of_real := ennreal.continuous_of_real.measurable lemma measurable_to_real : measurable ennreal.to_real := ennreal.measurable_of_measurable_nnreal nnreal.measurable_coe lemma measurable_to_nnreal : measurable ennreal.to_nnreal := ennreal.measurable_of_measurable_nnreal measurable_id lemma measurable_mul : measurable (λ p : ennreal × ennreal, p.1 * p.2) := begin apply measurable_of_measurable_nnreal_nnreal, { simp only [← ennreal.coe_mul, measurable_mul.ennreal_coe] }, { simp only [ennreal.top_mul, ennreal.coe_eq_zero], exact measurable_const.piecewise (is_measurable_singleton _) measurable_const }, { simp only [ennreal.mul_top, ennreal.coe_eq_zero], exact measurable_const.piecewise (is_measurable_singleton _) measurable_const } end lemma measurable_sub : measurable (λ p : ennreal × ennreal, p.1 - p.2) := by apply measurable_of_measurable_nnreal_nnreal; simp [← ennreal.coe_sub, nnreal.continuous_sub.measurable.ennreal_coe] end ennreal lemma measurable.to_nnreal {f : α → ennreal} (hf : measurable f) : measurable (λ x, (f x).to_nnreal) := ennreal.measurable_to_nnreal.comp hf lemma measurable_ennreal_coe_iff {f : α → nnreal} : measurable (λ x, (f x : ennreal)) ↔ measurable f := ⟨λ h, h.to_nnreal, λ h, h.ennreal_coe⟩ lemma measurable.to_real {f : α → ennreal} (hf : measurable f) : measurable (λ x, ennreal.to_real (f x)) := ennreal.measurable_to_real.comp hf lemma measurable.ennreal_mul {f g : α → ennreal} (hf : measurable f) (hg : measurable g) : measurable (λa, f a * g a) := ennreal.measurable_mul.comp (hf.prod_mk hg) lemma measurable.ennreal_add {f g : α → ennreal} (hf : measurable f) (hg : measurable g) : measurable (λa, f a + g a) := hf.add hg lemma measurable.ennreal_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g) : measurable (λa, f a - g a) := ennreal.measurable_sub.comp (hf.prod_mk hg) /-- note: `ennreal` can probably be generalized in a future version of this lemma. -/ lemma measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ennreal} (h : ∀ i, measurable (f i)) : measurable (λ x, ∑' i, f i x) := by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr, exact λ s, s.measurable_sum h } section normed_group variables [normed_group α] [opens_measurable_space α] [measurable_space β] lemma measurable_norm : measurable (norm : α → ℝ) := continuous_norm.measurable lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λa, norm (f a)) := measurable_norm.comp hf lemma measurable_nnnorm : measurable (nnnorm : α → nnreal) := continuous_nnnorm.measurable lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λa, nnnorm (f a)) := measurable_nnnorm.comp hf lemma measurable_ennnorm : measurable (λ x : α, (nnnorm x : ennreal)) := measurable_nnnorm.ennreal_coe lemma measurable.ennnorm {f : β → α} (hf : measurable f) : measurable (λa, (nnnorm (f a) : ennreal)) := hf.nnnorm.ennreal_coe end normed_group namespace continuous_linear_map variables {𝕜 : Type*} [normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] variables [opens_measurable_space E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F] protected lemma measurable (L : E →L[𝕜] F) : measurable L := L.continuous.measurable lemma measurable_comp (L : E →L[𝕜] F) {φ : α → E} (φ_meas : measurable φ) : measurable (λ (a : α), L (φ a)) := L.measurable.comp φ_meas end continuous_linear_map section normed_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜] variables [borel_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] lemma measurable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : measurable (λ x, f x • c) ↔ measurable f := measurable_comp_iff_of_closed_embedding (λ y : 𝕜, y • c) (closed_embedding_smul_left hc) end normed_space namespace measure_theory namespace measure variables [topological_space α] /-- A measure `μ` is regular if - it is finite on all compact sets; - it is outer regular: `μ(A) = inf { μ(U) | A ⊆ U open }` for `A` measurable; - it is inner regular: `μ(U) = sup { μ(K) | K ⊆ U compact }` for `U` open. -/ structure regular (μ : measure α) : Prop := (lt_top_of_is_compact : ∀ {{K : set α}}, is_compact K → μ K < ⊤) (outer_regular : ∀ {{A : set α}}, is_measurable A → (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) ≤ μ A) (inner_regular : ∀ {{U : set α}}, is_open U → μ U ≤ ⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) namespace regular lemma outer_regular_eq {μ : measure α} (hμ : μ.regular) {{A : set α}} (hA : is_measurable A) : (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) = μ A := le_antisymm (hμ.outer_regular hA) $ le_infi $ λ s, le_infi $ λ hs, le_infi $ λ h2s, μ.mono h2s lemma inner_regular_eq {μ : measure α} (hμ : μ.regular) {{U : set α}} (hU : is_open U) : (⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) = μ U := le_antisymm (supr_le $ λ s, supr_le $ λ hs, supr_le $ λ h2s, μ.mono h2s) (hμ.inner_regular hU) protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β] [t2_space β] [borel_space β] {μ : measure α} (hμ : μ.regular) (f : α ≃ₜ β) : (measure.map f μ).regular := begin have hf := f.continuous.measurable, have h2f := f.to_equiv.injective.preimage_surjective, have h3f := f.to_equiv.surjective, split, { intros K hK, rw [map_apply hf hK.is_measurable], apply hμ.lt_top_of_is_compact, rwa f.compact_preimage }, { intros A hA, rw [map_apply hf hA, ← hμ.outer_regular_eq (hf hA)], refine le_of_eq _, apply infi_congr (preimage f) h2f, intro U, apply infi_congr_Prop f.is_open_preimage, intro hU, apply infi_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hU.is_measurable], }, { intros U hU, rw [map_apply hf hU.is_measurable, ← hμ.inner_regular_eq (f.continuous U hU)], refine ge_of_eq _, apply supr_congr (preimage f) h2f, intro K, apply supr_congr_Prop f.compact_preimage, intro hK, apply supr_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hK.is_measurable] } end protected lemma smul {μ : measure α} (hμ : μ.regular) {x : ennreal} (hx : x < ⊤) : (x • μ).regular := begin split, { intros K hK, exact ennreal.mul_lt_top hx (hμ.lt_top_of_is_compact hK) }, { intros A hA, rw [coe_smul], refine le_trans _ (ennreal.mul_left_mono $ hμ.outer_regular hA), simp only [infi_and'], simp only [infi_subtype'], haveI : nonempty {s : set α // is_open s ∧ A ⊆ s} := ⟨⟨set.univ, is_open_univ, subset_univ _⟩⟩, rw [ennreal.mul_infi], refl', exact ne_of_lt hx }, { intros U hU, rw [coe_smul], refine le_trans (ennreal.mul_left_mono $ hμ.inner_regular hU) _, simp only [supr_and'], simp only [supr_subtype'], rw [ennreal.mul_supr], refl' } end end regular end measure end measure_theory
559e0114c10b540e834164d59e94ee95df31c726
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/09_Type_Classes.org.11.lean
5641730fe1f5a322b21e7f77861a1bd29717cbd7
[]
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
1,049
lean
import standard import data.prod namespace hide inductive inhabited [class] (A : Type) : Type := mk : A → inhabited A definition Prop.is_inhabited [instance] : inhabited Prop := inhabited.mk true definition bool.is_inhabited [instance] : inhabited bool := inhabited.mk bool.tt definition nat.is_inhabited [instance] : inhabited nat := inhabited.mk nat.zero definition unit.is_inhabited [instance] : inhabited unit := inhabited.mk unit.star definition default (A : Type) [H : inhabited A] : A := inhabited.rec (λ a, a) H definition arbitrary [irreducible] (A : Type) [H : inhabited A] : A := inhabited.rec (λ a, a) H definition prod.is_inhabited [instance] {A B : Type} [H1 : inhabited A] [H2 : inhabited B] : inhabited (prod A B) := inhabited.mk ((default A, default B)) -- BEGIN definition inhabited_fun [instance] (A : Type) {B : Type} [H : inhabited B] : inhabited (A → B) := inhabited.rec_on H (λ b, inhabited.mk (λ a, b)) check default (nat → nat × bool × unit) eval default (nat → nat × bool × unit) -- END end hide
7c165da4951f173456d8971ad80f2b4f8249a016
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/while/HoareSyntax.lean
fb82e099c423ab8de4fb6776cd81e463296fef92
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
778
lean
import Stmt import Subst open Stmt inductive Hoare : Pred → Stmt → Pred → Prop | SKIP : ∀ (p:Pred), Hoare p skip p | ASN : ∀ (p:Pred) (x:string) (a:AExp), Hoare (subst x a p) (x :== a) p | SEQ : ∀ (p q r:Pred) (e₁ e₂:Stmt), Hoare p e₁ q → Hoare q e₂ r → Hoare p (e₁ ;; e₂) r | IF : ∀ (p q:Pred) (b:BExp) (e₁ e₂:Stmt), Hoare (λ s, p s ∧ b s) e₁ q → Hoare (λ s, p s ∧ ¬b s) e₂ q → Hoare p (ite b e₁ e₂) q | WHILE : ∀ (p:Pred) (b:BExp) (e₁:Stmt), Hoare (λ s, p s ∧ b s) e₁ p → Hoare p (while b e₁) (λ s, p s ∧ ¬ b s) | WEAKEN : ∀ (p p' q q':Pred) (e₁:Stmt), (∀ s, p' s → p s) → Hoare p e₁ q → (∀ s, q s → q' s) → Hoare p' e₁ q'
5abf490e22cdf15b8b0ab5028e6a72a6621dcfc2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/direct_sum/internal.lean
a953c4d1bba516af0d5f42575e0c89dbf9bbc1e8
[ "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
10,531
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Kevin Buzzard, Jujian Zhang -/ import algebra.algebra.operations import algebra.algebra.subalgebra import algebra.direct_sum.algebra /-! # Internally graded rings and algebras This module provides `gsemiring` and `gcomm_semiring` instances for a collection of subobjects `A` when a `set_like.graded_monoid` instance is available: * on `add_submonoid R`s: `add_submonoid.gsemiring`, `add_submonoid.gcomm_semiring`. * on `add_subgroup R`s: `add_subgroup.gsemiring`, `add_subgroup.gcomm_semiring`. * on `submodule S R`s: `submodule.gsemiring`, `submodule.gcomm_semiring`. With these instances in place, it provides the bundled canonical maps out of a direct sum of subobjects into their carrier type: * `direct_sum.add_submonoid_coe_ring_hom` (a `ring_hom` version of `direct_sum.add_submonoid_coe`) * `direct_sum.add_subgroup_coe_ring_hom` (a `ring_hom` version of `direct_sum.add_subgroup_coe`) * `direct_sum.submodule_coe_alg_hom` (an `alg_hom` version of `direct_sum.submodule_coe`) Strictly the definitions in this file are not sufficient to fully define an "internal" direct sum; to represent this case, `(h : direct_sum.submodule_is_internal A) [set_like.graded_monoid A]` is needed. In the future there will likely be a data-carrying, constructive, typeclass version of `direct_sum.submodule_is_internal` for providing an explicit decomposition function. When `complete_lattice.independent (set.range A)` (a weaker condition than `direct_sum.submodule_is_internal A`), these provide a grading of `⨆ i, A i`, and the mapping `⨁ i, A i →+ ⨆ i, A i` can be obtained as `direct_sum.to_monoid (λ i, add_submonoid.inclusion $ le_supr A i)`. ## tags internally graded ring -/ open_locale direct_sum big_operators variables {ι : Type*} {S R : Type*} section direct_sum variables [decidable_eq ι] /-! #### From `add_submonoid`s -/ namespace add_submonoid /-- Build a `gsemiring` instance for a collection of `add_submonoid`s. -/ instance gsemiring [add_monoid ι] [semiring R] (A : ι → add_submonoid R) [set_like.graded_monoid A] : direct_sum.gsemiring (λ i, A i) := { mul_zero := λ i j _, subtype.ext (mul_zero _), zero_mul := λ i j _, subtype.ext (zero_mul _), mul_add := λ i j _ _ _, subtype.ext (mul_add _ _ _), add_mul := λ i j _ _ _, subtype.ext (add_mul _ _ _), ..set_like.gmonoid A } /-- Build a `gcomm_semiring` instance for a collection of `add_submonoid`s. -/ instance gcomm_semiring [add_comm_monoid ι] [comm_semiring R] (A : ι → add_submonoid R) [set_like.graded_monoid A] : direct_sum.gcomm_semiring (λ i, A i) := { ..set_like.gcomm_monoid A, ..add_submonoid.gsemiring A, } end add_submonoid /-- The canonical ring isomorphism between `⨁ i, A i` and `R`-/ def direct_sum.submonoid_coe_ring_hom [add_monoid ι] [semiring R] (A : ι → add_submonoid R) [h : set_like.graded_monoid A] : (⨁ i, A i) →+* R := direct_sum.to_semiring (λ i, (A i).subtype) rfl (λ _ _ _ _, rfl) /-- The canonical ring isomorphism between `⨁ i, A i` and `R`-/ @[simp] lemma direct_sum.submonoid_coe_ring_hom_of [add_monoid ι] [semiring R] (A : ι → add_submonoid R) [h : set_like.graded_monoid A] (i : ι) (x : A i) : direct_sum.submonoid_coe_ring_hom A (direct_sum.of (λ i, A i) i x) = x := direct_sum.to_semiring_of _ _ _ _ _ lemma direct_sum.coe_mul_apply_add_submonoid [add_monoid ι] [semiring R] (A : ι → add_submonoid R) [set_like.graded_monoid A] [Π (i : ι) (x : A i), decidable (x ≠ 0)] (r r' : ⨁ i, A i) (i : ι) : ((r * r') i : R) = ∑ ij in finset.filter (λ ij : ι × ι, ij.1 + ij.2 = i) (r.support.product r'.support), r ij.1 * r' ij.2 := begin rw [direct_sum.mul_eq_sum_support_ghas_mul, dfinsupp.finset_sum_apply, add_submonoid.coe_finset_sum], simp_rw [direct_sum.coe_of_add_submonoid_apply, ←finset.sum_filter, set_like.coe_ghas_mul], end /-! #### From `add_subgroup`s -/ namespace add_subgroup /-- Build a `gsemiring` instance for a collection of `add_subgroup`s. -/ instance gsemiring [add_monoid ι] [ring R] (A : ι → add_subgroup R) [h : set_like.graded_monoid A] : direct_sum.gsemiring (λ i, A i) := have i' : set_like.graded_monoid (λ i, (A i).to_add_submonoid) := {..h}, by exactI add_submonoid.gsemiring (λ i, (A i).to_add_submonoid) /-- Build a `gcomm_semiring` instance for a collection of `add_subgroup`s. -/ instance gcomm_semiring [add_comm_group ι] [comm_ring R] (A : ι → add_subgroup R) [h : set_like.graded_monoid A] : direct_sum.gsemiring (λ i, A i) := have i' : set_like.graded_monoid (λ i, (A i).to_add_submonoid) := {..h}, by exactI add_submonoid.gsemiring (λ i, (A i).to_add_submonoid) end add_subgroup /-- The canonical ring isomorphism between `⨁ i, A i` and `R`. -/ def direct_sum.subgroup_coe_ring_hom [add_monoid ι] [ring R] (A : ι → add_subgroup R) [set_like.graded_monoid A] : (⨁ i, A i) →+* R := direct_sum.to_semiring (λ i, (A i).subtype) rfl (λ _ _ _ _, rfl) @[simp] lemma direct_sum.subgroup_coe_ring_hom_of [add_monoid ι] [ring R] (A : ι → add_subgroup R) [set_like.graded_monoid A] (i : ι) (x : A i) : direct_sum.subgroup_coe_ring_hom A (direct_sum.of (λ i, A i) i x) = x := direct_sum.to_semiring_of _ _ _ _ _ lemma direct_sum.coe_mul_apply_add_subgroup [add_monoid ι] [ring R] (A : ι → add_subgroup R) [set_like.graded_monoid A] [Π (i : ι) (x : A i), decidable (x ≠ 0)] (r r' : ⨁ i, A i) (i : ι) : ((r * r') i : R) = ∑ ij in finset.filter (λ ij : ι × ι, ij.1 + ij.2 = i) (r.support.product r'.support), r ij.1 * r' ij.2 := begin rw [direct_sum.mul_eq_sum_support_ghas_mul, dfinsupp.finset_sum_apply, add_subgroup.coe_finset_sum], simp_rw [direct_sum.coe_of_add_subgroup_apply, ←finset.sum_filter, set_like.coe_ghas_mul], end /-! #### From `submodules`s -/ namespace submodule /-- Build a `gsemiring` instance for a collection of `submodule`s. -/ instance gsemiring [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] : direct_sum.gsemiring (λ i, A i) := have i' : set_like.graded_monoid (λ i, (A i).to_add_submonoid) := {..h}, by exactI add_submonoid.gsemiring (λ i, (A i).to_add_submonoid) /-- Build a `gsemiring` instance for a collection of `submodule`s. -/ instance gcomm_semiring [add_comm_monoid ι] [comm_semiring S] [comm_semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] : direct_sum.gcomm_semiring (λ i, A i) := have i' : set_like.graded_monoid (λ i, (A i).to_add_submonoid) := {..h}, by exactI add_submonoid.gcomm_semiring (λ i, (A i).to_add_submonoid) /-- Build a `galgebra` instance for a collection of `submodule`s. -/ instance galgebra [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] : direct_sum.galgebra S (λ i, A i) := { to_fun := begin refine ((algebra.linear_map S R).cod_restrict (A 0) $ λ r, _).to_add_monoid_hom, exact submodule.one_le.mpr set_like.has_graded_one.one_mem (submodule.algebra_map_mem _), end, map_one := subtype.ext $ by exact (algebra_map S R).map_one, map_mul := λ x y, sigma.subtype_ext (add_zero 0).symm $ (algebra_map S R).map_mul _ _, commutes := λ r ⟨i, xi⟩, sigma.subtype_ext ((zero_add i).trans (add_zero i).symm) $ algebra.commutes _ _, smul_def := λ r ⟨i, xi⟩, sigma.subtype_ext (zero_add i).symm $ algebra.smul_def _ _ } @[simp] lemma set_like.coe_galgebra_to_fun [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] (s : S) : ↑(@direct_sum.galgebra.to_fun _ S (λ i, A i) _ _ _ _ _ _ _ s) = (algebra_map S R s : R) := rfl /-- A direct sum of powers of a submodule of an algebra has a multiplicative structure. -/ instance nat_power_graded_monoid [comm_semiring S] [semiring R] [algebra S R] (p : submodule S R) : set_like.graded_monoid (λ i : ℕ, p ^ i) := { one_mem := by { rw [←one_le, pow_zero], exact le_rfl }, mul_mem := λ i j p q hp hq, by { rw pow_add, exact submodule.mul_mem_mul hp hq } } end submodule /-- The canonical algebra isomorphism between `⨁ i, A i` and `R`. -/ def direct_sum.submodule_coe_alg_hom [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] : (⨁ i, A i) →ₐ[S] R := direct_sum.to_algebra S _ (λ i, (A i).subtype) rfl (λ _ _ _ _, rfl) (λ _, rfl) /-- The supremum of submodules that form a graded monoid is a subalgebra, and equal to the range of `direct_sum.submodule_coe_alg_hom`. -/ lemma submodule.supr_eq_to_submodule_range [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [set_like.graded_monoid A] : (⨆ i, A i) = (direct_sum.submodule_coe_alg_hom A).range.to_submodule := (submodule.supr_eq_range_dfinsupp_lsum A).trans $ set_like.coe_injective rfl @[simp] lemma direct_sum.submodule_coe_alg_hom_of [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [h : set_like.graded_monoid A] (i : ι) (x : A i) : direct_sum.submodule_coe_alg_hom A (direct_sum.of (λ i, A i) i x) = x := direct_sum.to_semiring_of _ rfl (λ _ _ _ _, rfl) _ _ lemma direct_sum.coe_mul_apply_submodule [add_monoid ι] [comm_semiring S] [semiring R] [algebra S R] (A : ι → submodule S R) [Π (i : ι) (x : A i), decidable (x ≠ 0)] [set_like.graded_monoid A] (r r' : ⨁ i, A i) (i : ι) : ((r * r') i : R) = ∑ ij in finset.filter (λ ij : ι × ι, ij.1 + ij.2 = i) (r.support.product r'.support), r ij.1 * r' ij.2 := begin rw [direct_sum.mul_eq_sum_support_ghas_mul, dfinsupp.finset_sum_apply, submodule.coe_sum], simp_rw [direct_sum.coe_of_submodule_apply, ←finset.sum_filter, set_like.coe_ghas_mul], end end direct_sum section homogeneous_element lemma set_like.is_homogeneous_zero_submodule [has_zero ι] [semiring S] [add_comm_monoid R] [module S R] (A : ι → submodule S R) : set_like.is_homogeneous A (0 : R) := ⟨0, submodule.zero_mem _⟩ lemma set_like.is_homogeneous.smul [comm_semiring S] [semiring R] [algebra S R] {A : ι → submodule S R} {s : S} {r : R} (hr : set_like.is_homogeneous A r) : set_like.is_homogeneous A (s • r) := let ⟨i, hi⟩ := hr in ⟨i, submodule.smul_mem _ _ hi⟩ end homogeneous_element
cbedf1452937044965992d778f30f542c69d1498
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpOnly.lean
358d28605c899fc4427430599e0ffdf05180ae8f
[ "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
475
lean
theorem ex1 (n m : Nat) : 0 + (n, m).1 = n := by simp only rw [Nat.zero_add] theorem ex2 (n m : Nat) : 0 + (n, m).1 = n := by simp theorem ex3 (n m : Nat) : 0 + (n, m).1 + 0 = n := by simp only [Nat.add_zero] rw [Nat.zero_add] theorem ex4 (n m : Nat) : 0 + (n, m).1 + 0 = n := by simp theorem ex5 (m n : Nat) : m + n = n + m := by induction n with | zero => rw [Nat.zero_add, Nat.add_zero] | succ n ih => simp only [Nat.add_succ, Nat.succ_add, ih]
57f0af2b011466fdc3f7d4bcc8245b558853ecd3
2731214ea32f2a1a985300e281fb3117640a16c3
/portmanteau_conclusions.lean
dfd7c25978252dc159bd7e3bcff622fdeb019175
[ "Apache-2.0" ]
permissive
kkytola/lean_portmanteau
5d6a156db959974ebc4f5bed9118a7a2438a33fa
ac55eb4e24be43032cbc082e2b68d8fb8bd63f22
refs/heads/main
1,686,107,117,334
1,625,177,052,000
1,625,177,052,000
381,514,032
1
0
null
null
null
null
UTF-8
Lean
false
false
7,681
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import tactic import measure_theory.measurable_space import measure_theory.integration import measure_theory.borel_space import measure_theory.bochner_integration import topology.metric_space.basic import topology.instances.real import topology.instances.ennreal import order.liminf_limsup import portmanteau_definitions import portmanteau_integrals import portmanteau_cont_imp_closed import portmanteau_open_imp_cont import portmanteau_open_closed_imp_borel import portmanteau_borel_imp_closed_cond noncomputable theory open set open classical open measure_theory open measurable_space open filter open_locale topological_space namespace portmanteau section concluding_the_equivalence_of_portmanteau_conditions /-- `tfae_weak_conv_seq'` : Equivalent conditions for the weak convergence of a sequence of Borel probability measures on a topological space, in terms of integrals of bounded continuous functions. The second of these is the "textbook definition". (Further equivalent conditions can be obtained if one assumes that the space is a complete separable metric space, or at least that it is a metric space, or at least that indicators of all closed sets have decreasing continuous bounded approximations.) -/ theorem tfae_weak_conv_seq' {α : Type*} [topological_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tfae [ tendsto μseq at_top (𝓝 μ) , ∀ (f : α → ℝ) , continuous f → bdd_Rval f → tendsto (λ n, (@integral α ℝ (borel(α)) _ _ _ _ _ _ (μseq(n)) f)) at_top (𝓝 (@integral α ℝ (borel(α)) _ _ _ _ _ _ μ f)) , ∀ (f : cont_bdd_ennval α) , tendsto (λ n, integrate_cont_bdd_ennval (μseq(n)) f) at_top (𝓝 (integrate_cont_bdd_ennval μ f)) ] := begin have equiv₁ := @weak_conv_seq_iff α _ μseq μ , have equiv₂ := @weak_conv_seq_iff' α _ μseq μ , tfae_finish , end theorem weak_conv_seq_iff_portmanteau_open' {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ portmanteau_open (λ n , (μseq(n)).val) μ.val := begin split , { intros hdefcond , have hcontcond := weak_conv_seq_iff_portmanteau_continuous_ennval.mp hdefcond, have has_approx := metric_space_has_cont_approx_of_closed α , have hclcond := @portmanteau_continuous_imp_closed_cond α _ has_approx (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop hcontcond , exact (portmanteau_closed_and_open_cond_equivalent (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop).mp hclcond , } , { intros hopcond , have textbook' := portmanteau_open_imp_cont (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop hopcond , apply weak_conv_seq_iff'.mpr , intros f , exact textbook' f.val f.prop.1 f.prop.2 , } , end /-- `weak_conv_seq_iff_portmanteau_open'` : Equivalent characterization for the weak convergence of a sequence of Borel probability measures using open sets. -/ theorem weak_conv_seq_iff_portmanteau_open {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ ( ∀ (G : set α) , (is_open G) → μ(G) ≤ liminf at_top (λ n , (μseq(n))(G)) ) := begin split , { intros h , exact weak_conv_seq_iff_portmanteau_open'.mp h , } , { intros h , exact weak_conv_seq_iff_portmanteau_open'.mpr h , } , end theorem weak_conv_seq_iff_portmanteau_closed' {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ portmanteau_closed (λ n , (μseq(n)).val) μ.val := begin have eq_open_closed := portmanteau_closed_and_open_cond_equivalent (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop , have eq_open_def := @weak_conv_seq_iff_portmanteau_open' _ _ μseq μ , cc , end /-- `weak_conv_seq_iff_portmanteau_closed'` : Equivalent characterization for the weak convergence of a sequence of Borel probability measures using closed sets. -/ theorem weak_conv_seq_iff_portmanteau_closed {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ ( ∀ (F : set α) , (is_closed F) → limsup at_top (λ n , (μseq(n))(F)) ≤ μ(F) ) := begin split , { intros h , exact weak_conv_seq_iff_portmanteau_closed'.mp h , } , { intros h , exact weak_conv_seq_iff_portmanteau_closed'.mpr h , } , end theorem weak_conv_seq_iff_portmanteau_borel' {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ portmanteau_borel (λ n , (μseq(n)).val) μ.val := begin have closed_imp_borel := portmanteau_closed_imp_borel (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop , have borel_imp_closed := portmanteau_borel_imp_closed (λ n , (μseq(n)).val) (λ n , (μseq(n)).prop) μ.val μ.prop , have eq_closed_def := @weak_conv_seq_iff_portmanteau_closed _ _ μseq μ , split , { intros hdefcond , exact closed_imp_borel (eq_closed_def.mp hdefcond) , } , { intros hborcond , exact eq_closed_def.mpr (borel_imp_closed hborcond) , } , end /-- `weak_conv_seq_iff_portmanteau_borel'` : Equivalent characterization for the weak convergence of a sequence of Borel probability measures using Borel sets whose frontiers (boundaries) carry no probability mass in the limit measure. -/ theorem weak_conv_seq_iff_portmanteau_borel {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tendsto μseq at_top (𝓝 μ) ↔ ( ∀ (E : set α) , (borel_set(α) E) → (μ(frontier E) = 0) → (tendsto (λ n , (μseq(n))(E)) at_top (𝓝 (μ(E))) ) ) := begin split , { intros h , exact weak_conv_seq_iff_portmanteau_borel'.mp h , } , { intros h , exact weak_conv_seq_iff_portmanteau_borel'.mpr h , } , end /-- `tfae_weak_conv_seq` : Equivalent conditions for the weak convergence of a sequence of Borel probability measures on a metric space. -/ theorem tfae_weak_conv_seq {α : Type*} [metric_space α] {μseq : ℕ → borel_proba α} {μ : borel_proba α} : tfae [ tendsto μseq at_top (𝓝 μ) , ∀ (f : α → ℝ) , continuous f → bdd_Rval f → tendsto (λ n, (@integral α ℝ (borel(α)) _ _ _ _ _ _ (μseq(n)) f)) at_top (𝓝 (@integral α ℝ (borel(α)) _ _ _ _ _ _ μ f)) , ∀ (f : cont_bdd_ennval α) , tendsto (λ n, integrate_cont_bdd_ennval (μseq(n)) f) at_top (𝓝 (integrate_cont_bdd_ennval μ f)) , ( ∀ (G : set α) , (is_open G) → μ(G) ≤ liminf at_top (λ n , (μseq(n))(G)) ) , ( ∀ (F : set α) , (is_closed F) → limsup at_top (λ n , (μseq(n))(F)) ≤ μ(F) ) , ( ∀ (E : set α) , (borel_set(α) E) → (μ(frontier E) = 0) → (tendsto (λ n , (μseq(n))(E)) at_top (𝓝 (μ(E))) ) ) ] := begin have equiv₁ := @weak_conv_seq_iff α _ μseq μ , have equiv₂ := @weak_conv_seq_iff' α _ μseq μ , have equiv₃ := @weak_conv_seq_iff_portmanteau_open α _ μseq μ , have equiv₄ := @weak_conv_seq_iff_portmanteau_closed α _ μseq μ , have equiv₅ := @weak_conv_seq_iff_portmanteau_borel α _ μseq μ , tfae_finish , end end concluding_the_equivalence_of_portmanteau_conditions end portmanteau
9883c83f7e5182d12a48081bfda4d6fc7e85d84f
90edd5cdcf93124fe15627f7304069fdce3442dd
/stage0/src/Lean/Aesop/Util.lean
bd584c5ba5915acccb4fe54f404c67a8a4a56964
[ "Apache-2.0" ]
permissive
JLimperg/lean4-aesop
8a9d9cd3ee484a8e67fda2dd9822d76708098712
5c4b9a3e05c32f69a4357c3047c274f4b94f9c71
refs/heads/master
1,689,415,944,104
1,627,383,284,000
1,627,383,284,000
377,536,770
0
0
null
null
null
null
UTF-8
Lean
false
false
10,263
lean
/- Copyright (c) 2021 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg, Asta Halkjær From -/ import Lean.Elab.Syntax import Lean.Elab.Tactic.Basic import Lean.Message import Lean.Meta.DiscrTree import Lean.Meta.Tactic.Simp.SimpLemmas import Lean.Syntax import Std.Data.BinomialHeap namespace String def joinSep (sep : String) : List String → String | [] => "" | "" :: ss => joinSep sep ss | s :: ss => let tail := joinSep sep ss match tail with | "" => s | _ => s ++ sep ++ tail end String namespace Std.Format @[inlineIfReduce] def isEmptyShallow : Format → Bool | nil => true | text "" => true | _ => false @[inline] def indentDSkipEmpty [ToFormat α] (f : α) : Format := let f := format f if f.isEmptyShallow then nil else indentD f @[inline] def unlines [ToFormat α] (fs : List α) : Format := Format.joinSep fs line @[inline] def indentDUnlines [ToFormat α] : List α → Format := indentDSkipEmpty ∘ unlines @[inline] def indentDUnlinesSkipEmpty [ToFormat α] (fs : List α) : Format := indentDSkipEmpty $ unlines (fs.map format |>.filter (¬ ·.isEmptyShallow)) def formatIf (b : Bool) (f : Thunk Format) : Format := if b then f.get else nil end Std.Format namespace Lean.MessageData @[inline] def join (ms : List MessageData) : MessageData := ms.foldl (· ++ ·) nil @[inlineIfReduce] def isEmptyShallow : MessageData → Bool | ofFormat f => f.isEmptyShallow | _ => false @[inline] def indentDSkipEmpty (m : MessageData) : MessageData := if m.isEmptyShallow then nil else indentD m @[inline] def unlines (ms : List MessageData) : MessageData := joinSep ms Format.line @[inline] def indentDUnlines : List MessageData → MessageData := indentDSkipEmpty ∘ unlines @[inline] def indentDUnlinesSkipEmpty (fs : List MessageData) : MessageData := indentDSkipEmpty $ unlines $ fs.filter (¬ ·.isEmptyShallow) def toMessageDataIf (b : Bool) (f : Thunk MessageData) : MessageData := if b then f.get else nil def nodeFiltering (fs : Array (Option MessageData)) : MessageData := node $ fs.filterMap id end Lean.MessageData namespace Std.PersistentHashSet @[inline] def merge [BEq α] [Hashable α] (s t : PersistentHashSet α) : PersistentHashSet α := if s.size < t.size then loop s t else loop t s where @[inline] loop s t := s.fold (init := t) λ s a => s.insert a -- Elements are returned in unspecified order. def toList [BEq α] [Hashable α] (s : PersistentHashSet α) : List α := s.fold (init := []) λ as a => a :: as -- Elements are returned in unspecified order. (In fact, they are currently -- returned in reverse order of `toList`.) def toArray [BEq α] [Hashable α] (s : PersistentHashSet α) : Array α := s.fold (init := #[]) λ as a => as.push a end Std.PersistentHashSet namespace Std.PersistentHashMap @[inline] def merge [BEq α] [Hashable α] (m n : PersistentHashMap α β) (f : α → β → β → β) : PersistentHashMap α β := if m.size < n.size then loop m n f else loop n m (λ a b b' => f a b' b) where @[inline] loop m n f := m.foldl (init := n) λ map k v => match map.find? k with | some v' => map.insert k (f k v v') | none => map.insert k v end Std.PersistentHashMap namespace Lean.Meta.DiscrTree.Trie unsafe def foldMUnsafe [Monad m] (initialKeys : Array Key) (f : σ → Array Key → α → m σ) (init : σ) : Trie α → m σ | Trie.node vs children => do let s ← vs.foldlM (init := init) λ s v => f s initialKeys v children.foldlM (init := s) λ s (k, t) => t.foldMUnsafe (initialKeys.push k) f s @[implementedBy foldMUnsafe] constant foldM [Monad m] (initalKeys : Array Key) (f : σ → Array Key → α → m σ) (init : σ) (t : Trie α) : m σ := pure init @[inline] def fold (initialKeys : Array Key) (f : σ → Array Key → α → σ) (init : σ) (t : Trie α) : σ := Id.run $ t.foldM initialKeys (init := init) λ s k a => return f s k a end Trie @[inline] def foldM [Monad m] (f : σ → Array Key → α → m σ) (init : σ) (t : DiscrTree α) : m σ := t.root.foldlM (init := init) λ s k t => t.foldM #[k] (init := s) f @[inline] def fold (f : σ → Array Key → α → σ) (init : σ) (t : DiscrTree α) : σ := Id.run $ t.foldM (init := init) λ s keys a => return f s keys a -- TODO inefficient since it doesn't take advantage of the Trie structure at all @[inline] def merge [BEq α] (t u : DiscrTree α) : DiscrTree α := if t.root.size < u.root.size then loop t u else loop u t where @[inline] loop t u := t.fold (init := u) DiscrTree.insertCore def values (t : DiscrTree α) : Array α := t.fold (init := #[]) λ as _ a => as.push a def toArray (t : DiscrTree α) : Array (Array Key × α) := t.fold (init := #[]) λ as keys a => as.push (keys, a) end DiscrTree namespace SimpLemmas def merge (s t : SimpLemmas) : SimpLemmas where pre := s.pre.merge t.pre post := s.post.merge t.post lemmaNames := s.lemmaNames.merge t.lemmaNames toUnfold := s.toUnfold.merge t.toUnfold erased := s.erased.merge t.erased def addSimpEntry (s : SimpLemmas) : SimpEntry → SimpLemmas | SimpEntry.lemma l => addSimpLemmaEntry s l | SimpEntry.toUnfold d => s.addDeclToUnfold d open MessageData in protected def toMessageData (s : SimpLemmas) : MessageData := node #[ "pre lemmas:" ++ node (s.pre.values.map toMessageData), "post lemmas:" ++ node (s.post.values.map toMessageData), "definitions to unfold:" ++ node (s.toUnfold.toArray.qsort Name.lt |>.map toMessageData), "erased entries:" ++ node (s.erased.toArray.qsort Name.lt |>.map toMessageData) ] end SimpLemmas def copyMVar (mvarId : MVarId) : MetaM MVarId := do let decl ← getMVarDecl mvarId let mv ← mkFreshExprMVarAt decl.lctx decl.localInstances decl.type decl.kind decl.userName decl.numScopeArgs return mv.mvarId! end Lean.Meta namespace Std.BinomialHeap @[inline] def removeMin {lt : α → α → Bool} (h : BinomialHeap α lt) : Option (α × BinomialHeap α lt) := match h.head? with | some hd => some (hd, h.tail) | none => none end Std.BinomialHeap namespace MonadStateOf @[inline] def ofLens [Monad m] [MonadStateOf α m] (project : α → β) (inject : β → α → α) : MonadStateOf β m where get := return project (← get) set b := modify λ a => inject b a modifyGet f := modifyGet λ a => let (r, b) := f (project a) (r, inject b a) end MonadStateOf @[inline] abbrev setThe (σ) {m} [MonadStateOf σ m] (s : σ) : m PUnit := MonadStateOf.set s namespace ST.Ref variable {m} [Monad m] [MonadLiftT (ST σ) m] @[inline] unsafe def modifyMUnsafe (r : Ref σ α) (f : α → m α) : m Unit := do let v ← r.take r.set (← f v) @[implementedBy modifyMUnsafe] def modifyM (r : Ref σ α) (f : α → m α) : m Unit := do let v ← r.get r.set (← f v) @[inline] unsafe def modifyGetMUnsafe (r : Ref σ α) (f : α → m (β × α)) : m β := do let v ← r.take let (b, a) ← f v r.set a return b @[implementedBy modifyGetMUnsafe] def modifyGetM (r : Ref σ α) (f : α → m (β × α)) : m β := do let v ← r.get let (b, a) ← f v r.set a return b end ST.Ref namespace Lean.Meta def instantiateMVarsMVarType (mvarId : MVarId) : MetaM Expr := do let type ← instantiateMVars (← getMVarDecl mvarId).type setMVarType mvarId type return type end Lean.Meta namespace Lean.Syntax -- TODO for debugging, maybe remove partial def formatRaw : Syntax → String | missing => "missing" | node kind args => let args := ", ".joinSep $ args.map formatRaw |>.toList s!"(node {kind} [{args}])" | atom _ val => s!"(atom {val})" | ident _ _ val _ => s!"(ident {val})" end Lean.Syntax namespace Lean open Lean.Elab.Tactic def runTacticMAsMetaM (tac : TacticM Unit) (goal : MVarId) : MetaM (List MVarId) := run goal tac |>.run' def runMetaMAsImportM (x : MetaM α) : ImportM α := do let ctx : Core.Context := { options := (← read).opts } let state : Core.State := { env := (← read).env } let r ← x |>.run {} {} |>.run ctx state |>.toIO' match r with | Except.ok ((a, _), _) => pure a | Except.error e => throw $ IO.userError (← e.toMessageData.toString) def runMetaMAsCoreM (x : MetaM α) : CoreM α := Prod.fst <$> x.run {} {} end Lean namespace Lean.Elab.Command syntax (name := syntaxCatWithUnreservedTokens) "declare_syntax_cat' " ident (&"allow_leading_unreserved_tokens" <|> &"force_leading_unreserved_tokens")? : command -- Copied from Lean/Elab/Syntax.lean private def declareSyntaxCatQuotParser (catName : Name) : CommandElabM Unit := do if let Name.str _ suffix _ := catName then let quotSymbol := "`(" ++ suffix ++ "|" let name := catName ++ `quot -- TODO(Sebastian): this might confuse the pretty printer, but it lets us reuse the elaborator let kind := ``Lean.Parser.Term.quot let cmd ← `( @[termParser] def $(mkIdent name) : Lean.ParserDescr := Lean.ParserDescr.node $(quote kind) $(quote Lean.Parser.maxPrec) (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol $(quote quotSymbol)) (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.unary `incQuotDepth (Lean.ParserDescr.cat $(quote catName) 0)) (Lean.ParserDescr.symbol ")")))) elabCommand cmd open Lean.Parser (LeadingIdentBehavior) in @[builtinCommandElab syntaxCatWithUnreservedTokens] def elabDeclareSyntaxCatWithUnreservedTokens : CommandElab := fun stx => do let catName := stx[1].getId let leadingIdentBehavior := match stx[2].getOptional? with | none => LeadingIdentBehavior.default | some b => match b.getAtomVal! with | "allow_leading_unreserved_tokens" => LeadingIdentBehavior.both | "force_leading_unreserved_tokens" => LeadingIdentBehavior.symbol | _ => unreachable! let attrName := catName.appendAfter "Parser" let env ← getEnv let env ← liftIO $ Parser.registerParserCategory env attrName catName leadingIdentBehavior setEnv env declareSyntaxCatQuotParser catName end Lean.Elab.Command
628708eb31c61f6393fd4d845bc290f869568c7d
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/inductive1.lean
9237910e4e1fa03df29b13473b647ab6b71f08ea
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
1,921
lean
-- Test1 inductive T1 : Nat -- Error, resultant type is not a sort -- Test2 mutual inductive T1 : Prop inductive T2 : Type -- Error resulting universe mismatch end -- Test3 universes u v mutual inductive T1 (x : Nat) : Type u inductive T2 (x : Nat) : Nat → Type v -- Error resulting universe mismatch end -- Test4 mutual inductive T1 (b : Bool) (x : Nat) : Type inductive T2 (b : Bool) (x : Bool) : Type -- Type mismatch at 'x' end -- Test5 mutual inductive T1 (b : Bool) (x : Nat) : Type inductive T2 (x : Bool) : Type -- number of parameters mismatch end -- Test6 mutual inductive T1 (b : Bool) (x : Nat) : Type inductive T2 (b : Bool) {x : Nat} : Type -- binder annotation mismatch at 'x' end -- Test7 mutual inductive T1.{w1} (b : Bool) (x : Nat) : Type inductive T2.{w2} (b : Bool) (x : Nat) : Type -- universe parameter mismatch end -- Test8 namespace Boo def T1.bla := 10 inductive T1 : Type | bla : T1 -- Boo.T1.bla has already been defined def T1 := 20 inductive T1 : Type -- Boo.T1 has already been defined | bla : T1 end Boo -- Test9 partial inductive T1 : Type -- invalid use of partial noncomputable inductive T1 : Type -- invalid use of noncomputable @[inline] inductive T1 : Type -- invalid use of attributes private inductive T1 : Type | private mk : T1 -- invalid private constructor in private inductive type -- Test10 mutual inductive T1 : Type unsafe inductive T2 : Type end -- Test11 inductive T1 : Nat → Type | z1 : T1 0 | z2 -- constructor resulting type must be specified in inductive family declaration -- Test12 inductive T1 : Nat → Type | z1 : T1 -- unexpected constructor resulting type inductive T1 : Nat → Type | z1 : Nat -- unexpected constructor resulting type -- Test13 inductive A (α : Type u) (β : Type v) | nil {} | protected cons : α → β → A α β → A α β open A #check cons -- unknown `cons`, it is protected
7273460afff8bf34e6808d49d933f7d1ab89d514
2cf781335f4a6706b7452ab07ce323201e2e101f
/lean/file_input.lean
9d146dd2d80340d36a62b78cac5280105d6fb676
[ "Apache-2.0" ]
permissive
simonjwinwood/reopt-vcg
697cdd5e68366b5aa3298845eebc34fc97ccfbe2
6aca24e759bff4f2230bb58270bac6746c13665e
refs/heads/master
1,586,353,878,347
1,549,667,148,000
1,549,667,148,000
159,409,828
0
0
null
1,543,358,444,000
1,543,358,444,000
null
UTF-8
Lean
false
false
1,106
lean
import system.io -- init.category.lift structure file_input_state := (handle : io.handle) (pos : ℕ) (filename : string) -- for reopening /- A monad for dealing with the fact that lean doesn't support seeking backwards -/ @[reducible] def file_input := state_t file_input_state (except_t string io) namespace file_input def new_state (filename : string) : io file_input_state := do hdl <- io.mk_file_handle filename io.mode.read tt, return { handle := hdl, pos := 0, filename := filename } def run_file_input {a} (filename : string) (fr : file_input a) : io (except string a) := do st <- new_state filename, rv <- (fr.run st).run, return (rv.map prod.fst) def read (n:ℕ) : file_input char_buffer := do s <- get, monad_lift (io.fs.read s.handle n) def seek (n : ℕ) : file_input unit := do s <- get, if s.pos < n then monad_lift (io.fs.read s.handle (n - s.pos)) >> put { pos := n, ..s } else monad_lift (do s' <- new_state s.filename, _ <- io.fs.read s'.handle n, return { pos := n, ..s'}) >>= put end file_input
cdd4c8350aab88689ab13641b7ac41a10bd3f301
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Hygiene.lean
3b66972089eab4765679159e55dc3e70a8a95afa
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
4,636
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Data.Name import Lean.Data.Options import Lean.Data.Format namespace Lean /- Remark: `MonadQuotation` class is part of the `Init` package and loaded by default since it is used in the builtin command `macro`. -/ structure Unhygienic.Context where ref : Syntax scope : MacroScope /-- Simplistic MonadQuotation that does not guarantee globally fresh names, that is, between different runs of this or other MonadQuotation implementations. It is only safe if the syntax quotations do not introduce bindings around antiquotations, and if references to globals are prefixed with `_root_.` (which is not allowed to refer to a local variable). `Unhygienic` can also be seen as a model implementation of `MonadQuotation` (since it is completely hygienic as long as it is "run" only once and can assume that there are no other implentations in use, as is the case for the elaboration monads that carry their macro scope state through the entire processing of a file). It uses the state monad to query and allocate the next macro scope, and uses the reader monad to store the stack of scopes corresponding to `withFreshMacroScope` calls. -/ abbrev Unhygienic := ReaderT Lean.Unhygienic.Context $ StateM MacroScope namespace Unhygienic instance : MonadQuotation Unhygienic where getRef := do (← read).ref withRef := fun ref => withReader ({ · with ref := ref }) getCurrMacroScope := do (← read).scope getMainModule := pure `UnhygienicMain withFreshMacroScope := fun x => do let fresh ← modifyGet fun n => (n, n + 1) withReader ({ · with scope := fresh}) x protected def run {α : Type} (x : Unhygienic α) : α := (x ⟨Syntax.missing, firstFrontendMacroScope⟩).run' (firstFrontendMacroScope+1) end Unhygienic private def mkInaccessibleUserNameAux (unicode : Bool) (name : Name) (idx : Nat) : Name := if unicode then if idx == 0 then name.appendAfter "✝" else name.appendAfter ("✝" ++ idx.toSuperscriptString) else name ++ Name.mkNum "_inaccessible" idx private def mkInaccessibleUserName (unicode : Bool) : Name → Name | Name.num p@(Name.str _ _ _) idx _ => mkInaccessibleUserNameAux unicode p idx | Name.num Name.anonymous idx _ => mkInaccessibleUserNameAux unicode Name.anonymous idx | Name.num p idx _ => if unicode then (mkInaccessibleUserName unicode p).appendAfter ("⁻" ++ idx.toSuperscriptString) else Name.mkNum (mkInaccessibleUserName unicode p) idx | n => n def sanitizeNamesDefault := true def getSanitizeNames (o : Options) : Bool:= o.get `pp.sanitizeNames sanitizeNamesDefault builtin_initialize registerOption `pp.sanitizeNames { defValue := sanitizeNamesDefault, group := "pp", descr := "add suffix '_{<idx>}' to shadowed/inaccessible variables when pretty printing" } structure NameSanitizerState where options : Options -- `x` ~> 2 if we're already using `x✝`, `x✝¹` nameStem2Idx : NameMap Nat := {} -- `x._hyg...` ~> `x✝` userName2Sanitized : NameMap Name := {} private partial def mkFreshInaccessibleUserName (userName : Name) (idx : Nat) : StateM NameSanitizerState Name := do let s ← get let userNameNew := mkInaccessibleUserName (Std.Format.getUnicode s.options) (Name.mkNum userName idx) if s.nameStem2Idx.contains userNameNew then mkFreshInaccessibleUserName userName (idx+1) else do modify fun s => { s with nameStem2Idx := s.nameStem2Idx.insert userName (idx+1) } pure userNameNew def sanitizeName (userName : Name) : StateM NameSanitizerState Name := do let stem := userName.eraseMacroScopes; let idx := (← get).nameStem2Idx.find? stem |>.getD 0 let san ← mkFreshInaccessibleUserName stem idx modify fun s => { s with userName2Sanitized := s.userName2Sanitized.insert userName san } pure san private partial def sanitizeSyntaxAux : Syntax → StateM NameSanitizerState Syntax | stx@(Syntax.ident _ _ n _) => do mkIdentFrom stx <$> match (← get).userName2Sanitized.find? n with | some n' => pure n' | none => if n.hasMacroScopes then sanitizeName n else pure n | Syntax.node info k args => Syntax.node info k <$> args.mapM sanitizeSyntaxAux | stx => pure stx def sanitizeSyntax (stx : Syntax) : StateM NameSanitizerState Syntax := do if getSanitizeNames (← get).options then sanitizeSyntaxAux stx else pure stx end Lean
f2b5f2a95202364b4598fe8efd7434bb1681af17
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Elab/Tactic/Rewrite.lean
8cef19f4a55ae42ec03f694449d0b9b2e2c2ee3d
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,746
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.Rewrite import Lean.Meta.Tactic.Replace import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Location namespace Lean.Elab.Tactic open Meta @[builtinMacro Lean.Parser.Tactic.rewriteSeq] def expandRewriteTactic : Macro := fun stx => let seq := stx[1][1].getSepArgs let loc := stx[2] pure $ mkNullNode $ seq.map fun rwRule => Syntax.node `Lean.Parser.Tactic.rewrite #[mkAtomFrom rwRule "rewrite ", rwRule, loc] def rewriteTarget (stx : Syntax) (symm : Bool) : TacticM Unit := do let (g, gs) ← getMainGoal withMVarContext g do let e ← elabTerm stx none true let target ← instantiateMVars (← getMVarDecl g).type let r ← rewrite g target e symm let g' ← replaceTargetEq g r.eNew r.eqProof setGoals (g' :: r.mvarIds ++ gs) def rewriteLocalDeclFVarId (stx : Syntax) (symm : Bool) (fvarId : FVarId) : TacticM Unit := do let (g, gs) ← getMainGoal withMVarContext g do let e ← elabTerm stx none true let localDecl ← getLocalDecl fvarId let rwResult ← rewrite g localDecl.type e symm let replaceResult ← replaceLocalDecl g fvarId rwResult.eNew rwResult.eqProof setGoals (replaceResult.mvarId :: rwResult.mvarIds ++ gs) def rewriteLocalDecl (stx : Syntax) (symm : Bool) (userName : Name) : TacticM Unit := withMainMVarContext do let localDecl ← getLocalDeclFromUserName userName rewriteLocalDeclFVarId stx symm localDecl.fvarId def rewriteAll (stx : Syntax) (symm : Bool) : TacticM Unit := do let worked ← «try» $ rewriteTarget stx symm withMainMVarContext do let mut worked := worked -- We must traverse backwards because `replaceLocalDecl` uses the revert/intro idiom for fvarId in (← getLCtx).getFVarIds.reverse do worked := worked || (← «try» $ rewriteLocalDeclFVarId stx symm fvarId) unless worked do let (mvarId, _) ← getMainGoal throwTacticEx `rewrite mvarId "did not find instance of the pattern in the current goal" /- ``` def rwRule := parser! optional (unicodeSymbol "←" "<-") >> termParser def «rewrite» := parser! "rewrite" >> rwRule >> optional location ``` -/ @[builtinTactic Lean.Parser.Tactic.rewrite] def evalRewrite : Tactic := fun stx => do let rule := stx[1] let symm := !rule[0].isNone let term := rule[1] let loc := expandOptLocation stx[2] match loc with | Location.target => rewriteTarget term symm | Location.localDecls userNames => userNames.forM (rewriteLocalDecl term symm) | Location.wildcard => rewriteAll term symm end Lean.Elab.Tactic
93afa9b26f03fac96340c06d5db91318db3ab67f
4727251e0cd73359b15b664c3170e5d754078599
/src/data/nat/pow.lean
d8201c1f32127f7d7bbfb561603b31c1dc57461f
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
11,360
lean
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import algebra.group_power.order /-! # `nat.pow` Results on the power operation on natural numbers. -/ namespace nat /-! ### `pow` -/ -- This is redundant with `pow_le_pow_of_le_left'`, -- We leave a version in the `nat` namespace as well. -- (The global `pow_le_pow_of_le_left` needs an extra hypothesis `0 ≤ x`.) protected theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i : ℕ, x^i ≤ y^i := pow_le_pow_of_le_left' H theorem pow_le_pow_of_le_right {x : ℕ} (H : 0 < x) {i j : ℕ} (h : i ≤ j) : x ^ i ≤ x ^ j := pow_le_pow' H h theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : 0 < i) : x^i < y^i := pow_lt_pow_of_lt_left H (zero_le _) h theorem pow_lt_pow_of_lt_right {x : ℕ} (H : 1 < x) {i j : ℕ} (h : i < j) : x^i < x^j := pow_lt_pow H h lemma pow_lt_pow_succ {p : ℕ} (h : 1 < p) (n : ℕ) : p^n < p^(n+1) := pow_lt_pow_of_lt_right h n.lt_succ_self lemma lt_pow_self {p : ℕ} (h : 1 < p) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma lt_two_pow (n : ℕ) : n < 2^n := lt_pow_self dec_trivial n lemma one_le_pow (n m : ℕ) (h : 0 < m) : 1 ≤ m^n := by { rw ←one_pow n, exact nat.pow_le_pow_of_le_left h n } lemma one_le_pow' (n m : ℕ) : 1 ≤ (m+1)^n := one_le_pow n (m+1) (succ_pos m) lemma one_le_two_pow (n : ℕ) : 1 ≤ 2^n := one_le_pow n 2 dec_trivial lemma one_lt_pow (n m : ℕ) (h₀ : 0 < n) (h₁ : 1 < m) : 1 < m^n := by { rw ←one_pow n, exact pow_lt_pow_of_lt_left h₁ h₀ } lemma one_lt_pow' (n m : ℕ) : 1 < (m+2)^(n+1) := one_lt_pow (n+1) (m+2) (succ_pos n) (nat.lt_of_sub_eq_succ rfl) @[simp] lemma one_lt_pow_iff {k n : ℕ} (h : 0 ≠ k) : 1 < n ^ k ↔ 1 < n := begin cases n, { cases k; simp [zero_pow_eq] }, cases n, { rw one_pow }, refine ⟨λ _, one_lt_succ_succ n, λ _, _⟩, induction k with k hk, { exact absurd rfl h }, cases k, { simp }, exact one_lt_mul (one_lt_succ_succ _).le (hk (succ_ne_zero k).symm), end lemma one_lt_two_pow (n : ℕ) (h₀ : 0 < n) : 1 < 2^n := one_lt_pow n 2 h₀ dec_trivial lemma one_lt_two_pow' (n : ℕ) : 1 < 2^(n+1) := one_lt_pow (n+1) 2 (succ_pos n) dec_trivial lemma pow_right_strict_mono {x : ℕ} (k : 2 ≤ x) : strict_mono (λ (n : ℕ), x^n) := λ _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : ℕ} (k : 2 ≤ x) : x^m ≤ x^n ↔ m ≤ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : ℕ} (k : 2 ≤ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : ℕ} (k : 2 ≤ x) : function.injective (λ (n : ℕ), x^n) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : ℕ} (k : 1 ≤ m) : strict_mono (λ (x : ℕ), x^m) := λ _ _ h, pow_lt_pow_of_lt_left h k lemma mul_lt_mul_pow_succ {n a q : ℕ} (a0 : 0 < a) (q1 : 1 < q) : n * q < a * q ^ (n + 1) := begin rw [pow_succ', ← mul_assoc, mul_lt_mul_right (zero_lt_one.trans q1)], exact lt_mul_of_one_le_of_lt (nat.succ_le_iff.mpr a0) (nat.lt_pow_self q1 n), end end nat lemma strict_mono.nat_pow {n : ℕ} (hn : 1 ≤ n) {f : ℕ → ℕ} (hf : strict_mono f) : strict_mono (λ m, (f m) ^ n) := (nat.pow_left_strict_mono hn).comp hf namespace nat lemma pow_le_iff_le_left {m x y : ℕ} (k : 1 ≤ m) : x^m ≤ y^m ↔ x ≤ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : ℕ} (k : 1 ≤ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : ℕ} (k : 1 ≤ m) : function.injective (λ (x : ℕ), x^m) := strict_mono.injective (pow_left_strict_mono k) theorem sq_sub_sq (a b : ℕ) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by { rw [sq, sq], exact nat.mul_self_sub_mul_self_eq a b } alias nat.sq_sub_sq ← nat.pow_two_sub_pow_two /-! ### `pow` and `mod` / `dvd` -/ theorem pow_mod (a b n : ℕ) : a ^ b % n = (a % n) ^ b % n := begin induction b with b ih, refl, simp [pow_succ, nat.mul_mod, ih], end theorem mod_pow_succ {b : ℕ} (w m : ℕ) : m % (b^succ w) = b * (m/b % b^w) + m % b := begin by_cases b_h : b = 0, { simp [b_h, pow_succ], }, have b_pos := nat.pos_of_ne_zero b_h, apply nat.strong_induction_on m, clear m, intros p IH, cases lt_or_ge p (b^succ w) with h₁ h₁, -- base case: p < b^succ w { have h₂ : p / b < b^w, { rw [div_lt_iff_lt_mul p _ b_pos], simpa [pow_succ'] using h₁ }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [div_add_mod] }, -- step: p ≥ b^succ w { -- Generate condition for induction hypothesis have h₂ : p - b^succ w < p, { exact tsub_lt_self ((pow_pos b_pos _).trans_le h₁) (pow_pos b_pos _) }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp only [pow_succ], simp only [ge, pow_succ] at h₁, -- Pull subtraction outside mod and div rw [sub_mul_mod _ _ _ h₁, sub_mul_div _ _ _ h₁], -- Cancel subtraction inside mod b^w have p_b_ge : b^w ≤ p / b, { rw [le_div_iff_mul_le _ _ b_pos, mul_comm], exact h₁ }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end lemma pow_dvd_pow_iff_pow_le_pow {k l : ℕ} : Π {x : ℕ} (w : 0 < x), x^k ∣ x^l ↔ x^k ≤ x^l | (x+1) w := begin split, { intro a, exact le_of_dvd (pow_pos (succ_pos x) l) a, }, { intro a, cases x with x, { simp only [one_pow], }, { have le := (pow_le_iff_le_right (nat.le_add_left _ _)).mp a, use (x+2)^(l-k), rw [←pow_add, add_comm k, tsub_add_cancel_of_le le], } } end /-- If `1 < x`, then `x^k` divides `x^l` if and only if `k` is at most `l`. -/ lemma pow_dvd_pow_iff_le_right {x k l : ℕ} (w : 1 < x) : x^k ∣ x^l ↔ k ≤ l := by rw [pow_dvd_pow_iff_pow_le_pow (lt_of_succ_lt w), pow_le_iff_le_right w] lemma pow_dvd_pow_iff_le_right' {b k l : ℕ} : (b+2)^k ∣ (b+2)^l ↔ k ≤ l := pow_dvd_pow_iff_le_right (nat.lt_of_sub_eq_succ rfl) lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : 1 < p) (hk : 1 < k), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have succ p * (succ p)^k ∣ succ p * 1, by simpa [pow_succ] using h, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_left (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from nat.eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := (pow_dvd_pow _ hmn).trans hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma pow_div {x m n : ℕ} (h : n ≤ m) (hx : 0 < x) : x ^ m / x ^ n = x ^ (m - n) := by rw [nat.div_eq_iff_eq_mul_left (pow_pos hx n) (pow_dvd_pow _ h), pow_sub_mul_pow _ h] /-! ### `shiftl` and `shiftr` -/ lemma shiftl_eq_mul_pow (m) : ∀ n, shiftl m n = m * 2 ^ n | 0 := (nat.mul_one _).symm | (k+1) := show bit0 (shiftl m k) = m * (2 * 2 ^ k), by rw [bit0_val, shiftl_eq_mul_pow, mul_left_comm, mul_comm 2] lemma shiftl'_tt_eq_mul_pow (m) : ∀ n, shiftl' tt m n + 1 = (m + 1) * 2 ^ n | 0 := by simp [shiftl, shiftl', pow_zero, nat.one_mul] | (k+1) := begin change bit1 (shiftl' tt m k) + 1 = (m + 1) * (2 * 2 ^ k), rw bit1_val, change 2 * (shiftl' tt m k + 1) = _, rw [shiftl'_tt_eq_mul_pow, mul_left_comm, mul_comm 2], end lemma one_shiftl (n) : shiftl 1 n = 2 ^ n := (shiftl_eq_mul_pow _ _).trans (nat.one_mul _) @[simp] lemma zero_shiftl (n) : shiftl 0 n = 0 := (shiftl_eq_mul_pow _ _).trans (nat.zero_mul _) lemma shiftr_eq_div_pow (m) : ∀ n, shiftr m n = m / 2 ^ n | 0 := (nat.div_one _).symm | (k+1) := (congr_arg div2 (shiftr_eq_div_pow k)).trans $ by rw [div2_val, nat.div_div_eq_div_mul, mul_comm]; refl @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := (shiftr_eq_div_pow _ _).trans (nat.zero_div _) theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ /-! ### `size` -/ @[simp] theorem size_zero : size 0 = 0 := by simp [size] @[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := show size (bit1 0) = 1, by rw [size_bit1, size_zero] @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊢, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊢, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, obtain rfl := succ.inj (eq_one_of_dvd_one ⟨_, this.symm⟩), rw one_mul at this, obtain rfl : n = 0 := nat.eq_zero_of_le_zero (le_of_not_gt $ λ hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), refl end @[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : ℕ) : n < 2^size n := begin rw [← one_shiftl], have : ∀ {n}, n = 0 → n < shiftl 1 (size n), { simp }, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, simp }, { intros b m IH n h, by_cases e : bit b m = 0, { simp [e] }, rw [size_bit e], cases n with n, { exact e.elim (nat.eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n := by rw [← not_lt, decidable.iff_not_comm, not_lt, size_le] theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact decidable.not_iff_not.1 this theorem size_pow {n : ℕ} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_rfl) theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) end nat
ac837e1f39874e952c5b36a7e27619a0625098b1
7571914d3f4d9677288f35ab1a53a2ad70a62bd7
/library/init/data/bitvec.lean
b3ab1e39c6c753b29093b933f33578d40cbec89e
[ "Apache-2.0" ]
permissive
picrin/lean-1
a395fed5287995f09a15a190bb24609919a0727f
b50597228b42a7eaa01bf8cb7a4fb1a98e7a8aab
refs/heads/master
1,610,757,735,162
1,502,008,413,000
1,502,008,413,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,667
lean
/- Copyright (c) 2015 Joe Hendrix. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joe Hendrix, Sebastian Ullrich Basic operations on bitvectors. This is a work-in-progress, and contains additions to other theories. -/ prelude import init.data.nat init.data.int init.data.vector init.meta.interactive @[reducible] def bitvec (n : ℕ) := vector bool n namespace bitvec open nat open vector local infix `++ₜ`:65 := vector.append -- Create a zero bitvector @[reducible] protected def zero (n : ℕ) : bitvec n := repeat ff n -- Create a bitvector with the constant one. @[reducible] protected def one : Π (n : ℕ), bitvec n | 0 := nil | (succ n) := repeat ff n ++ₜ tt::nil protected def cong {a b : ℕ} (h : a = b) : bitvec a → bitvec b | ⟨x, p⟩ := ⟨x, h ▸ p⟩ -- bitvec specific version of vector.append def append {m n} : bitvec m → bitvec n → bitvec (m + n) := vector.append section shift variable {n : ℕ} def shl (x : bitvec n) (i : ℕ) : bitvec n := bitvec.cong (by simp) $ drop i x ++ₜ repeat ff (min n i) def fill_shr (x : bitvec n) (i : ℕ) (fill : bool) : bitvec n := bitvec.cong begin by_cases (i ≤ n), { have h₁ := sub_le n i, rw [min_eq_right h], rw [min_eq_left h₁, ← nat.add_sub_assoc h, add_comm, nat.add_sub_cancel] }, { have h₁ := le_of_not_ge h, rw [min_eq_left h₁, sub_eq_zero_of_le h₁, zero_min, add_zero] } end $ repeat fill (min n i) ++ₜ take (n-i) x -- unsigned shift right def ushr (x : bitvec n) (i : ℕ) : bitvec n := fill_shr x i ff -- signed shift right def sshr : Π {m : ℕ}, bitvec m → ℕ → bitvec m | 0 _ _ := nil | (succ m) x i := head x :: fill_shr (tail x) i (head x) end shift section bitwise variable {n : ℕ} def not : bitvec n → bitvec n := map bnot def and : bitvec n → bitvec n → bitvec n := map₂ band def or : bitvec n → bitvec n → bitvec n := map₂ bor def xor : bitvec n → bitvec n → bitvec n := map₂ bxor end bitwise section arith variable {n : ℕ} protected def xor3 (x y c : bool) := bxor (bxor x y) c protected def carry (x y c : bool) := x && y || x && c || y && c protected def neg (x : bitvec n) : bitvec n := let f := λ y c, (y || c, bxor y c) in prod.snd (map_accumr f x ff) -- Add with carry (no overflow) def adc (x y : bitvec n) (c : bool) : bitvec (n+1) := let f := λ x y c, (bitvec.carry x y c, bitvec.xor3 x y c) in let ⟨c, z⟩ := vector.map_accumr₂ f x y c in c :: z protected def add (x y : bitvec n) : bitvec n := tail (adc x y ff) -- Subtract with borrow def sbb (x y : bitvec n) (b : bool) : bool × bitvec n := let f := λ x y c, (bitvec.carry (bnot x) y c, bitvec.xor3 x y c) in vector.map_accumr₂ f x y b protected def sub (x y : bitvec n) : bitvec n := prod.snd (sbb x y ff) instance : has_zero (bitvec n) := ⟨bitvec.zero n⟩ instance : has_one (bitvec n) := ⟨bitvec.one n⟩ instance : has_add (bitvec n) := ⟨bitvec.add⟩ instance : has_sub (bitvec n) := ⟨bitvec.sub⟩ instance : has_neg (bitvec n) := ⟨bitvec.neg⟩ protected def mul (x y : bitvec n) : bitvec n := let f := λ r b, cond b (r + r + y) (r + r) in (to_list x).foldl f 0 instance : has_mul (bitvec n) := ⟨bitvec.mul⟩ end arith section comparison variable {n : ℕ} def uborrow (x y : bitvec n) : bool := prod.fst (sbb x y ff) def ult (x y : bitvec n) : Prop := uborrow x y def ugt (x y : bitvec n) : Prop := ult y x def ule (x y : bitvec n) : Prop := ¬ (ult y x) def uge (x y : bitvec n) : Prop := ule y x def sborrow : Π {n : ℕ}, bitvec n → bitvec n → bool | 0 _ _ := ff | (succ n) x y := match (head x, head y) with | (tt, ff) := tt | (ff, tt) := ff | _ := uborrow (tail x) (tail y) end def slt (x y : bitvec n) : Prop := sborrow x y def sgt (x y : bitvec n) : Prop := slt y x def sle (x y : bitvec n) : Prop := ¬ (slt y x) def sge (x y : bitvec n) : Prop := sle y x end comparison section conversion variable {α : Type} protected def of_nat : Π (n : ℕ), nat → bitvec n | 0 x := nil | (succ n) x := of_nat n (x / 2) ++ₜ to_bool (x % 2 = 1) :: nil protected def of_int : Π (n : ℕ), int → bitvec (succ n) | n (int.of_nat m) := ff :: bitvec.of_nat n m | n (int.neg_succ_of_nat m) := tt :: not (bitvec.of_nat n m) def add_lsb (r : ℕ) (b : bool) := r + r + cond b 1 0 def bits_to_nat (v : list bool) : nat := v.foldl add_lsb 0 protected def to_nat {n : nat} (v : bitvec n) : nat := bits_to_nat (to_list v) theorem bits_to_nat_to_list {n : ℕ} (x : bitvec n) : bitvec.to_nat x = bits_to_nat (vector.to_list x) := rfl theorem to_nat_append {m : ℕ} (xs : bitvec m) (b : bool) : bitvec.to_nat (xs ++ₜ b::nil) = bitvec.to_nat xs * 2 + bitvec.to_nat (b::nil) := begin cases xs with xs P, simp [bits_to_nat_to_list], clear P, unfold bits_to_nat list.foldl, -- generalize the accumulator of foldl generalize h : 0 = x, conv in (add_lsb x b) { rw ←h }, clear h, simp, induction xs with x xs generalizing x, { simp, unfold list.foldl add_lsb, simp [nat.mul_succ] }, { simp, apply ih_1 } end theorem bits_to_nat_to_bool (n : ℕ) : bitvec.to_nat (to_bool (n % 2 = 1) :: nil) = n % 2 := begin simp [bits_to_nat_to_list], unfold bits_to_nat add_lsb list.foldl cond, simp [cond_to_bool_mod_two], end theorem of_nat_succ {k n : ℕ} : bitvec.of_nat (succ k) n = bitvec.of_nat k (n / 2) ++ₜ to_bool (n % 2 = 1) :: nil := rfl theorem to_nat_of_nat {k n : ℕ} : bitvec.to_nat (bitvec.of_nat k n) = n % 2^k := begin induction k with k generalizing n, { unfold pow, simp [nat.mod_one], refl }, { have h : 0 < 2, { apply le_succ }, rw [ of_nat_succ , to_nat_append , ih_1 , bits_to_nat_to_bool , mod_pow_succ h], ac_refl, } end protected def to_int : Π {n : nat}, bitvec n → int | 0 _ := 0 | (succ n) v := cond (head v) (int.neg_succ_of_nat $ bitvec.to_nat $ not $ tail v) (int.of_nat $ bitvec.to_nat $ tail v) end conversion private def repr {n : nat} : bitvec n → string | ⟨bs, p⟩ := "0b" ++ (bs.map (λ b : bool, if b then '1' else '0')).as_string instance (n : nat) : has_repr (bitvec n) := ⟨repr⟩ end bitvec instance {n} {x y : bitvec n} : decidable (bitvec.ult x y) := bool.decidable_eq _ _ instance {n} {x y : bitvec n} : decidable (bitvec.ugt x y) := bool.decidable_eq _ _
3a3dd9f292be6098dadf12a3e4bed08af3408b73
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/ring_theory/ideal/over.lean
8e338312796d0ff67ab9229c8363aeda00e84dce
[ "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
7,282
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen -/ import ring_theory.algebraic /-! # Ideals over/under ideals This file concerns ideals lying over other ideals. Let `f : R →+* S` be a ring homomorphism (typically a ring extension), `I` an ideal of `R` and `J` an ideal of `S`. We say `J` lies over `I` (and `I` under `J`) if `I` is the `f`-preimage of `J`. This is expressed here by writing `I = J.comap f`. ## Implementation notes The proofs of the `comap_ne_bot` and `comap_lt_comap` families use an approach specific for their situation: we construct an element in `I.comap f` from the coefficients of a minimal polynomial. Once mathlib has more material on the localization at a prime ideal, the results can be proven using more general going-up/going-down theory. -/ variables {R : Type*} [comm_ring R] namespace ideal open polynomial open submodule section comm_ring variables {S : Type*} [comm_ring S] {f : R →+* S} {I J : ideal S} lemma coeff_zero_mem_comap_of_root_mem_of_eval_mem {r : S} (hr : r ∈ I) {p : polynomial R} (hp : p.eval₂ f r ∈ I) : p.coeff 0 ∈ I.comap f := begin rw [←p.div_X_mul_X_add, eval₂_add, eval₂_C, eval₂_mul, eval₂_X] at hp, refine mem_comap.mpr ((I.add_mem_iff_right _).mp hp), exact I.mul_mem_left hr end lemma coeff_zero_mem_comap_of_root_mem {r : S} (hr : r ∈ I) {p : polynomial R} (hp : p.eval₂ f r = 0) : p.coeff 0 ∈ I.comap f := coeff_zero_mem_comap_of_root_mem_of_eval_mem hr (hp.symm ▸ I.zero_mem) lemma exists_coeff_ne_zero_mem_comap_of_non_zero_divisor_root_mem {r : S} (r_non_zero_divisor : ∀ {x}, x * r = 0 → x = 0) (hr : r ∈ I) {p : polynomial R} : ∀ (p_ne_zero : p ≠ 0) (hp : p.eval₂ f r = 0), ∃ i, p.coeff i ≠ 0 ∧ p.coeff i ∈ I.comap f := begin refine p.rec_on_horner _ _ _, { intro h, contradiction }, { intros p a coeff_eq_zero a_ne_zero ih p_ne_zero hp, refine ⟨0, _, coeff_zero_mem_comap_of_root_mem hr hp⟩, simp [coeff_eq_zero, a_ne_zero] }, { intros p p_nonzero ih mul_nonzero hp, rw [eval₂_mul, eval₂_X] at hp, obtain ⟨i, hi, mem⟩ := ih p_nonzero (r_non_zero_divisor hp), refine ⟨i + 1, _, _⟩; simp [hi, mem] } end end comm_ring section integral_domain variables {S : Type*} [integral_domain S] {f : R →+* S} {I J : ideal S} lemma exists_coeff_ne_zero_mem_comap_of_root_mem {r : S} (r_ne_zero : r ≠ 0) (hr : r ∈ I) {p : polynomial R} : ∀ (p_ne_zero : p ≠ 0) (hp : p.eval₂ f r = 0), ∃ i, p.coeff i ≠ 0 ∧ p.coeff i ∈ I.comap f := exists_coeff_ne_zero_mem_comap_of_non_zero_divisor_root_mem (λ _ h, or.resolve_right (mul_eq_zero.mp h) r_ne_zero) hr lemma exists_coeff_mem_comap_sdiff_comap_of_root_mem_sdiff [is_prime I] (hIJ : I ≤ J) {r : S} (hr : r ∈ (J : set S) \ I) {p : polynomial R} (p_ne_zero : p.map (quotient.mk (I.comap f)) ≠ 0) (hpI : p.eval₂ f r ∈ I) : ∃ i, p.coeff i ∈ (J.comap f : set R) \ (I.comap f) := begin obtain ⟨hrJ, hrI⟩ := hr, have rbar_ne_zero : quotient.mk I r ≠ 0 := mt (quotient.mk_eq_zero I).mp hrI, have rbar_mem_J : quotient.mk I r ∈ J.map (quotient.mk I) := mem_map_of_mem hrJ, have quotient_f : ∀ x ∈ I.comap f, (quotient.mk I).comp f x = 0, { simp [quotient.eq_zero_iff_mem] }, have rbar_root : (p.map (quotient.mk (I.comap f))).eval₂ (quotient.lift (I.comap f) _ quotient_f) (quotient.mk I r) = 0, { convert quotient.eq_zero_iff_mem.mpr hpI, exact trans (eval₂_map _ _ _) (hom_eval₂ p f (quotient.mk I) r).symm }, obtain ⟨i, ne_zero, mem⟩ := exists_coeff_ne_zero_mem_comap_of_root_mem rbar_ne_zero rbar_mem_J p_ne_zero rbar_root, rw coeff_map at ne_zero mem, refine ⟨i, (mem_quotient_iff_mem hIJ).mp _, mt _ ne_zero⟩, { simpa using mem }, simp [quotient.eq_zero_iff_mem], end lemma comap_ne_bot_of_root_mem {r : S} (r_ne_zero : r ≠ 0) (hr : r ∈ I) {p : polynomial R} (p_ne_zero : p ≠ 0) (hp : p.eval₂ f r = 0) : I.comap f ≠ ⊥ := λ h, let ⟨i, hi, mem⟩ := exists_coeff_ne_zero_mem_comap_of_root_mem r_ne_zero hr p_ne_zero hp in absurd ((mem_bot _).mp (eq_bot_iff.mp h mem)) hi lemma comap_lt_comap_of_root_mem_sdiff [I.is_prime] (hIJ : I ≤ J) {r : S} (hr : r ∈ (J : set S) \ I) {p : polynomial R} (p_ne_zero : p.map (quotient.mk (I.comap f)) ≠ 0) (hp : p.eval₂ f r ∈ I) : I.comap f < J.comap f := let ⟨i, hJ, hI⟩ := exists_coeff_mem_comap_sdiff_comap_of_root_mem_sdiff hIJ hr p_ne_zero hp in lt_iff_le_and_exists.mpr ⟨comap_mono hIJ, p.coeff i, hJ, hI⟩ variables [algebra R S] lemma comap_ne_bot_of_algebraic_mem {x : S} (x_ne_zero : x ≠ 0) (x_mem : x ∈ I) (hx : is_algebraic R x) : I.comap (algebra_map R S) ≠ ⊥ := let ⟨p, p_ne_zero, hp⟩ := hx in comap_ne_bot_of_root_mem x_ne_zero x_mem p_ne_zero hp lemma comap_ne_bot_of_integral_mem [nontrivial R] {x : S} (x_ne_zero : x ≠ 0) (x_mem : x ∈ I) (hx : is_integral R x) : I.comap (algebra_map R S) ≠ ⊥ := comap_ne_bot_of_algebraic_mem x_ne_zero x_mem (hx.is_algebraic R) lemma mem_of_one_mem (h : (1 : S) ∈ I) (x) : x ∈ I := (I.eq_top_iff_one.mpr h).symm ▸ mem_top lemma comap_lt_comap_of_integral_mem_sdiff [hI : I.is_prime] (hIJ : I ≤ J) {x : S} (mem : x ∈ (J : set S) \ I) (integral : is_integral R x) : I.comap (algebra_map R S) < J.comap (algebra_map _ _) := begin obtain ⟨p, p_monic, hpx⟩ := integral, refine comap_lt_comap_of_root_mem_sdiff hIJ mem _ _, swap, { apply map_monic_ne_zero p_monic, apply quotient.nontrivial, apply mt comap_eq_top_iff.mp, apply hI.1 }, convert I.zero_mem end lemma is_maximal_of_is_integral_of_is_maximal_comap (hRS : ∀ (x : S), is_integral R x) (I : ideal S) [I.is_prime] (hI : is_maximal (I.comap (algebra_map R S))) : is_maximal I := ⟨ mt comap_eq_top_iff.mpr hI.1, λ J I_lt_J, let ⟨I_le_J, x, hxJ, hxI⟩ := lt_iff_le_and_exists.mp I_lt_J in comap_eq_top_iff.mp (hI.2 _ (comap_lt_comap_of_integral_mem_sdiff I_le_J ⟨hxJ, hxI⟩ (hRS x))) ⟩ lemma integral_closure.comap_ne_bot [nontrivial R] {I : ideal (integral_closure R S)} (I_ne_bot : I ≠ ⊥) : I.comap (algebra_map R (integral_closure R S)) ≠ ⊥ := let ⟨x, x_mem, x_ne_zero⟩ := I.ne_bot_iff.mp I_ne_bot in comap_ne_bot_of_integral_mem x_ne_zero x_mem (integral_closure.is_integral x) lemma integral_closure.eq_bot_of_comap_eq_bot [nontrivial R] {I : ideal (integral_closure R S)} : I.comap (algebra_map R (integral_closure R S)) = ⊥ → I = ⊥ := imp_of_not_imp_not _ _ integral_closure.comap_ne_bot lemma integral_closure.comap_lt_comap {I J : ideal (integral_closure R S)} [I.is_prime] (I_lt_J : I < J) : I.comap (algebra_map R (integral_closure R S)) < J.comap (algebra_map _ _) := let ⟨I_le_J, x, hxJ, hxI⟩ := lt_iff_le_and_exists.mp I_lt_J in comap_lt_comap_of_integral_mem_sdiff I_le_J ⟨hxJ, hxI⟩ (integral_closure.is_integral x) lemma integral_closure.is_maximal_of_is_maximal_comap (I : ideal (integral_closure R S)) [I.is_prime] (hI : is_maximal (I.comap (algebra_map R (integral_closure R S)))) : is_maximal I := is_maximal_of_is_integral_of_is_maximal_comap (λ x, integral_closure.is_integral x) I hI end integral_domain end ideal
e795d845cbe9cbad6a947ce53849dd40933a713b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/simp_command_auto.lean
ed869d4c6507e5e776040e078a72062d7adb0788
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,439
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Keeley Hoek, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.core import Mathlib.PostPort namespace Mathlib /-! ## #simp A user command to run the simplifier. -/ namespace tactic /-- Strip all annotations of non local constants in the passed `expr`. (This is required in an incantation later on in order to make the C++ simplifier happy.) -/ /-- `simp_arg_type.to_pexpr` retrieves the `pexpr` underlying the given `simp_arg_type`, if there is one. -/ /-- Incantation which prepares a `pexpr` in a `simp_arg_type` for use by the simplifier after `expr.replace_subexprs` as been called to replace some of its local variables. -/ /-- `simp_arg_type.replace_subexprs` calls `expr.replace_subexprs` on the underlying `pexpr`, if there is one, and then prepares the result for use by the simplifier. -/ /- Turn off the messages if the result is exactly `true` with this option. -/ /-- The basic usage is `#simp e`, where `e` is an expression, which will print the simplified form of `e`. You can specify additional simp lemmas as usual for example using `#simp [f, g] : e`, or `#simp with attr : e`. (The colon is optional, but helpful for the parser.) `#simp` understands local variables, so you can use them to introduce parameters. -/ end Mathlib
efc2ee60e4c989473f3171e15d22f53bfba14bdf
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/norm_cast.lean
e343ea1789a643675b76c037e1163605bbfd270e
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,212
lean
/- Tests for norm_cast -/ import tactic.norm_cast import data.complex.basic -- ℕ, ℤ, ℚ, ℝ, ℂ import data.real.ennreal open_locale ennreal constants (an bn cn dn : ℕ) (az bz cz dz : ℤ) (aq bq cq dq : ℚ) constants (ar br cr dr : ℝ) (ac bc cc dc : ℂ) example : (an : ℤ) = bn → an = bn := by {intro h, exact_mod_cast h} example : an = bn → (an : ℤ) = bn := by {intro h, exact_mod_cast h} example : az = bz ↔ (az : ℚ) = bz := by norm_cast example : (aq : ℝ) = br ↔ (aq : ℂ) = br := by norm_cast example : (an : ℚ) = bz ↔ (an : ℂ) = bz := by norm_cast example : (((an : ℤ) : ℚ) : ℝ) = bq ↔ ((an : ℚ) : ℂ) = (bq : ℝ) := by norm_cast example : (an : ℤ) < bn ↔ an < bn := by norm_cast example : (an : ℚ) < bz ↔ (an : ℝ) < bz := by norm_cast example : ((an : ℤ) : ℝ) < bq ↔ (an : ℚ) < bq := by norm_cast example : (an : ℤ) ≠ (bn : ℤ) ↔ an ≠ bn := by norm_cast -- zero and one cause special problems example : 0 < (bq : ℝ) ↔ 0 < bq := by norm_cast example : az > (1 : ℕ) ↔ az > 1 := by norm_cast example : az > (0 : ℕ) ↔ az > 0 := by norm_cast example : (an : ℤ) ≠ 0 ↔ an ≠ 0 := by norm_cast example : aq < (1 : ℕ) ↔ (aq : ℝ) < (1 : ℤ) := by norm_cast example : (an : ℤ) + bn = (an + bn : ℕ) := by norm_cast example : (an : ℂ) + bq = ((an + bq) : ℚ) := by norm_cast example : (((an : ℤ) : ℚ) : ℝ) + bn = (an + (bn : ℤ)) := by norm_cast example : (((((an : ℚ) : ℝ) * bq) + (cq : ℝ) ^ dn) : ℂ) = (an : ℂ) * (bq : ℝ) + cq ^ dn := by norm_cast example : ((an : ℤ) : ℝ) < bq ∧ (cr : ℂ) ^ 2 = dz ↔ (an : ℚ) < bq ∧ ((cr ^ 2) : ℂ) = dz := by norm_cast --testing numerals example : ((42 : ℕ) : ℤ) = 42 := by norm_cast example : ((42 : ℕ) : ℂ) = 42 := by norm_cast example : ((42 : ℤ) : ℚ) = 42 := by norm_cast example : ((42 : ℚ) : ℝ) = 42 := by norm_cast example (h : (an : ℝ) = 0) : an = 0 := by exact_mod_cast h example (h : (an : ℝ) = 42) : an = 42 := by exact_mod_cast h example (h : (an + 42) ≠ 42) : (an : ℝ) + 42 ≠ 42 := by exact_mod_cast h -- testing the heuristic example (h : bn ≤ an) : an - bn = 1 ↔ (an - bn : ℤ) = 1 := by norm_cast example (h : (cz : ℚ) = az / bz) : (cz : ℝ) = az / bz := by assumption_mod_cast namespace hidden def with_zero (α) := option α variables {α : Type*} instance : has_coe_t α (with_zero α) := ⟨some⟩ instance : has_zero (with_zero α) := ⟨none⟩ instance [has_one α]: has_one (with_zero α) := ⟨some 1⟩ instance [has_mul α] : mul_zero_class (with_zero α) := { mul := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a * b)), zero_mul := λ a, rfl, mul_zero := λ a, by cases a; refl, ..hidden.with_zero.has_zero } @[norm_cast] lemma coe_one [has_one α] : ((1 : α) : with_zero α) = 1 := rfl @[norm_cast] lemma coe_inj {a b : α} : (a : with_zero α) = b ↔ a = b := option.some_inj @[norm_cast] lemma mul_coe {α : Type*} [has_mul α] (a b : α) : ((a * b : α) : with_zero α) = (a : with_zero α) * b := rfl example [has_mul α] [has_one α] (x y : α) (h : (x : with_zero α) * y = 1) : x*y = 1 := by exact_mod_cast h end hidden example (k : ℕ) {x y : ℕ} : (x * x + y * y : ℤ) - ↑((x * y + 1) * k) = ↑y * ↑y - ↑k * ↑x * ↑y + (↑x * ↑x - ↑k) := begin push_cast, ring end example (k : ℕ) {x y : ℕ} (h : ((x + y + k : ℕ) : ℤ) = 0) : x + y + k = 0 := begin push_cast at h, guard_hyp h : (x : ℤ) + y + k = 0, assumption_mod_cast end example (a b : ℕ) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast [int.add_zero] at h2, exact h2 end example {x : ℚ} : ((x + 42 : ℚ) : ℝ) = x + 42 := by push_cast namespace ennreal --TODO: debug lemma half_lt_self_bis {a : ℝ≥0∞} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / 2 < a := begin lift a to nnreal using ht, have h : (2 : ℝ≥0∞) = ((2 : nnreal) : ℝ≥0∞), from rfl, have h' : (2 : nnreal) ≠ 0, from _root_.two_ne_zero', rw [h, ← coe_div h', coe_lt_coe], -- `norm_cast` fails to apply `coe_div` norm_cast at hz, exact nnreal.half_lt_self hz end end ennreal
949418f966324659bd16d1cb19f8f2a36b8e2491
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/init/tactic.lean
7fe558fd81a0622faa30af502f4119803241714d
[ "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
7,913
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 This is just a trick to embed the 'tactic language' as a Lean expression. We should view 'tactic' as automation that when execute produces a term. tactic.builtin is just a "dummy" for creating the definitions that are actually implemented in C++ -/ prelude import init.datatypes init.reserved_notation init.num inductive tactic : Type := builtin : tactic namespace tactic -- Remark the following names are not arbitrary, the tactic module -- uses them when converting Lean expressions into actual tactic objects. -- The bultin 'by' construct triggers the process of converting a -- a term of type 'tactic' into a tactic that sythesizes a term definition and_then (t1 t2 : tactic) : tactic := builtin definition or_else (t1 t2 : tactic) : tactic := builtin definition par (t1 t2 : tactic) : tactic := builtin definition fixpoint (f : tactic → tactic) : tactic := builtin definition repeat (t : tactic) : tactic := builtin definition at_most (t : tactic) (k : num) : tactic := builtin definition discard (t : tactic) (k : num) : tactic := builtin definition focus_at (t : tactic) (i : num) : tactic := builtin definition try_for (t : tactic) (ms : num) : tactic := builtin definition all_goals (t : tactic) : tactic := builtin definition now : tactic := builtin definition assumption : tactic := builtin definition eassumption : tactic := builtin definition state : tactic := builtin definition fail : tactic := builtin definition id : tactic := builtin definition beta : tactic := builtin definition info : tactic := builtin definition whnf : tactic := builtin definition contradiction : tactic := builtin definition exfalso : tactic := builtin definition congruence : tactic := builtin definition rotate_left (k : num) := builtin definition rotate_right (k : num) := builtin definition rotate (k : num) := rotate_left k definition norm_num : tactic := builtin -- This is just a trick to embed expressions into tactics. -- The nested expressions are "raw". They tactic should -- elaborate them when it is executed. inductive expr : Type := builtin : expr inductive expr_list : Type := | nil : expr_list | cons : expr → expr_list → expr_list -- auxiliary type used to mark optional list of arguments definition opt_expr_list := expr_list -- auxiliary types used to mark that the expression is suppose to be an identifier, optional, or a list. definition identifier := expr definition identifier_list := expr_list definition opt_identifier_list := expr_list -- Remark: the parser has special support for tactics containing `location` parameters. -- It will parse the optional `at ...` modifier. definition location := expr -- Marker for instructing the parser to parse it as 'with <expr>' definition with_expr := expr -- Marker for instructing the parser to parse it as '?(using <expr>)' definition using_expr := expr -- Constant used to denote the case were no expression was provided definition none_expr : expr := expr.builtin definition apply (e : expr) : tactic := builtin definition eapply (e : expr) : tactic := builtin definition fapply (e : expr) : tactic := builtin definition rename (a b : identifier) : tactic := builtin definition intro (e : opt_identifier_list) : tactic := builtin definition generalize_tac (e : expr) (id : identifier) : tactic := builtin definition clear (e : identifier_list) : tactic := builtin definition revert (e : identifier_list) : tactic := builtin definition refine (e : expr) : tactic := builtin definition exact (e : expr) : tactic := builtin -- Relaxed version of exact that does not enforce goal type definition rexact (e : expr) : tactic := builtin definition check_expr (e : expr) : tactic := builtin definition trace (s : string) : tactic := builtin -- rewrite_tac is just a marker for the builtin 'rewrite' notation -- used to create instances of this tactic. definition rewrite_tac (e : expr_list) : tactic := builtin definition xrewrite_tac (e : expr_list) : tactic := builtin definition krewrite_tac (e : expr_list) : tactic := builtin definition replace (old : expr) (new : with_expr) (loc : location) : tactic := builtin -- Arguments: -- - ls : lemmas to be used (if not provided, then blast will choose them) -- - ds : definitions that can be unfolded (if not provided, then blast will choose them) definition blast (ls : opt_identifier_list) (ds : opt_identifier_list) : tactic := builtin -- with_options_tac is just a marker for the builtin 'with_options' notation definition with_options_tac (o : expr) (t : tactic) : tactic := builtin -- with_options_tac is just a marker for the builtin 'with_attributes' notation definition with_attributes_tac (o : expr) (n : identifier_list) (t : tactic) : tactic := builtin definition simp : tactic := #tactic with_options [blast.strategy "simp"] blast definition simp_nohyps : tactic := #tactic with_options [blast.strategy "simp_nohyps"] blast definition simp_topdown : tactic := #tactic with_options [blast.strategy "simp", simplify.top_down true] blast definition inst_simp : tactic := #tactic with_options [blast.strategy "ematch_simp"] blast definition rec_simp : tactic := #tactic with_options [blast.strategy "rec_simp"] blast definition rec_inst_simp : tactic := #tactic with_options [blast.strategy "rec_ematch_simp"] blast definition grind : tactic := #tactic with_options [blast.strategy "grind"] blast definition grind_simp : tactic := #tactic with_options [blast.strategy "grind_simp"] blast definition cases (h : expr) (ids : opt_identifier_list) : tactic := builtin definition induction (h : expr) (rec : using_expr) (ids : opt_identifier_list) : tactic := builtin definition intros (ids : opt_identifier_list) : tactic := builtin definition generalizes (es : expr_list) : tactic := builtin definition clears (ids : identifier_list) : tactic := builtin definition reverts (ids : identifier_list) : tactic := builtin definition change (e : expr) : tactic := builtin definition assert_hypothesis (id : identifier) (e : expr) : tactic := builtin definition note_tac (id : identifier) (e : expr) : tactic := builtin definition constructor (k : option num) : tactic := builtin definition fconstructor (k : option num) : tactic := builtin definition existsi (e : expr) : tactic := builtin definition split : tactic := builtin definition left : tactic := builtin definition right : tactic := builtin definition injection (e : expr) (ids : opt_identifier_list) : tactic := builtin definition subst (ids : identifier_list) : tactic := builtin definition substvars : tactic := builtin definition reflexivity : tactic := builtin definition symmetry : tactic := builtin definition transitivity (e : expr) : tactic := builtin definition try (t : tactic) : tactic := or_else t id definition repeat1 (t : tactic) : tactic := and_then t (repeat t) definition focus (t : tactic) : tactic := focus_at t 0 definition determ (t : tactic) : tactic := at_most t 1 definition trivial : tactic := or_else (or_else (apply eq.refl) (apply true.intro)) assumption definition do (n : nat) (t : tactic) : tactic := nat.rec id (λn t', and_then t t') n end tactic tactic_infixl `;`:15 := tactic.and_then tactic_notation T1 `:`:15 T2 := tactic.focus (tactic.and_then T1 (tactic.all_goals T2)) tactic_notation `(` h `|` r:(foldl `|` (e r, tactic.or_else r e) h) `)` := r --tactic_notation `replace` s `with` t := tactic.replace_tac s t
dcf9d75017e05421861814f37eca1ddfdf66f5eb
1446f520c1db37e157b631385707cc28a17a595e
/tests/bench/rbmap.lean
367c14f4093cdea1ac5c24678aef1f50c491bf5d
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,875
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 -/ prelude import Init.Data.Option.Basic import Init.Data.List.BasicAux import Init.System.IO universes u v w w' inductive color | Red | Black inductive Tree | Leaf {} : Tree | Node (color : color) (lchild : Tree) (key : Nat) (val : Bool) (rchild : Tree) : Tree variables {σ : Type w} open color Nat Tree def fold (f : Nat → Bool → σ → σ) : Tree → σ → σ | Leaf, b => b | Node _ l k v r, b => fold r (f k v (fold l b)) @[inline] def balance1 : Nat → Bool → Tree → Tree → Tree | kv, vv, t, Node _ (Node Red l kx vx r₁) ky vy r₂ => Node Red (Node Black l kx vx r₁) ky vy (Node Black r₂ kv vv t) | kv, vv, t, Node _ l₁ ky vy (Node Red l₂ kx vx r) => Node Red (Node Black l₁ ky vy l₂) kx vx (Node Black r kv vv t) | kv, vv, t, Node _ l ky vy r => Node Black (Node Red l ky vy r) kv vv t | _, _, _, _ => Leaf @[inline] def balance2 : Tree → Nat → Bool → Tree → Tree | t, kv, vv, Node _ (Node Red l kx₁ vx₁ r₁) ky vy r₂ => Node Red (Node Black t kv vv l) kx₁ vx₁ (Node Black r₁ ky vy r₂) | t, kv, vv, Node _ l₁ ky vy (Node Red l₂ kx₂ vx₂ r₂) => Node Red (Node Black t kv vv l₁) ky vy (Node Black l₂ kx₂ vx₂ r₂) | t, kv, vv, Node _ l ky vy r => Node Black t kv vv (Node Red l ky vy r) | _, _, _, _ => Leaf def isRed : Tree → Bool | Node Red _ _ _ _ => true | _ => false def ins : Tree → Nat → Bool → Tree | Leaf, kx, vx => Node Red Leaf kx vx Leaf | Node Red a ky vy b, kx, vx => (if kx < ky then Node Red (ins a kx vx) ky vy b else if kx = ky then Node Red a kx vx b else Node Red a ky vy (ins b kx vx)) | Node Black a ky vy b, kx, vx => if kx < ky then (if isRed a then balance1 ky vy b (ins a kx vx) else Node Black (ins a kx vx) ky vy b) else if kx = ky then Node Black a kx vx b else if isRed b then balance2 a ky vy (ins b kx vx) else Node Black a ky vy (ins b kx vx) def setBlack : Tree → Tree | Node _ l k v r => Node Black l k v r | e => e def insert (t : Tree) (k : Nat) (v : Bool) : Tree := if isRed t then setBlack (ins t k v) else ins t k v def mkMapAux : Nat → Tree → Tree | 0, m => m | n+1, m => mkMapAux n (insert m n (n % 10 = 0)) def mkMap (n : Nat) := mkMapAux n Leaf def main (xs : List String) : IO UInt32 := let m := mkMap xs.head!.toNat; let v := fold (fun (k : Nat) (v : Bool) (r : Nat) => if v then r + 1 else r) m 0; IO.println (toString v) *> pure 0
f83b6d02f6b8566fe44581c5cb0cb24c142ef3d7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/fibrant_class2.lean
59fffb7c81bfdddad4ef4a7c340e7b3d249c2a0b
[ "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,215
lean
inductive fibrant [class] (T : Type) : Type := fibrant_mk : fibrant T axiom pi_fibrant {A : Type} {B : A → Type} [C1 : fibrant A] [C2 : Πx : A, fibrant (B x)] : fibrant (Πx : A, B x) attribute pi_fibrant [instance] inductive path {A : Type} [fA : fibrant A] (a : A) : A → Type := idpath : path a a axiom path_fibrant {A : Type} [fA : fibrant A] (a b : A) : fibrant (path a b) attribute path_fibrant [instance] notation a ≈ b := path a b noncomputable definition test {A : Type} [fA : fibrant A] {x y : A} : Π (z : A), y ≈ z → fibrant (x ≈ y → x ≈ z) := take z p, _ noncomputable definition test2 {A : Type} [fA : fibrant A] {x y : A} : Π (z : A), y ≈ z → fibrant (x ≈ y → x ≈ z) := _ noncomputable definition test3 {A : Type} [fA : fibrant A] {x y : A} : Π (z : A), y ≈ z → fibrant (x ≈ z) := _ noncomputable definition test4 {A : Type} [fA : fibrant A] {x y z : A} : fibrant (x ≈ y → x ≈ z) := _ axiom imp_fibrant {A : Type} {B : Type} [C1 : fibrant A] [C2 : fibrant B] : fibrant (A → B) attribute imp_fibrant [instance] noncomputable definition test5 {A : Type} [fA : fibrant A] {x y : A} : Π (z : A), y ≈ z → fibrant (x ≈ y → x ≈ z) := _
6b33be194747abbb659726b2e9fbc8bcb999acc4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monad/adjunction.lean
57653a3e06d932371de2e8758bcce70cd19e6a0e
[]
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,436
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.monad.algebra import Mathlib.category_theory.adjunction.default import Mathlib.PostPort universes u₁ u₂ v₁ v₂ l namespace Mathlib namespace category_theory namespace adjunction @[simp] theorem monad_μ {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [is_right_adjoint R] : μ_ = whisker_right (whisker_left (left_adjoint R) (counit (of_right_adjoint R))) R := Eq.refl μ_ @[simp] theorem comonad_ε {C : Type u₁} [category C] {D : Type u₂} [category D] (L : C ⥤ D) [is_left_adjoint L] : ε_ = counit (of_left_adjoint L) := Eq.refl ε_ end adjunction namespace monad /-- Gven any adjunction `L ⊣ R`, there is a comparison functor `category_theory.monad.comparison R` sending objects `Y : D` to Eilenberg-Moore algebras for `L ⋙ R` with underlying object `R.obj X`. We later show that this is full when `R` is full, faithful when `R` is faithful, and essentially surjective when `R` is reflective. -/ @[simp] theorem comparison_map_f {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [is_right_adjoint R] (X : D) (Y : D) (f : X ⟶ Y) : algebra.hom.f (functor.map (comparison R) f) = functor.map R f := Eq.refl (algebra.hom.f (functor.map (comparison R) f)) /-- The underlying object of `(monad.comparison R).obj X` is just `R.obj X`. -/ def comparison_forget {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [is_right_adjoint R] : comparison R ⋙ forget (left_adjoint R ⋙ R) ≅ R := iso.mk (nat_trans.mk fun (X : D) => 𝟙) (nat_trans.mk fun (X : D) => 𝟙) end monad namespace comonad /-- Gven any adjunction `L ⊣ R`, there is a comparison functor `category_theory.comonad.comparison L` sending objects `X : C` to Eilenberg-Moore coalgebras for `L ⋙ R` with underlying object `L.obj X`. -/ @[simp] theorem comparison_obj_a {C : Type u₁} [category C] {D : Type u₂} [category D] (L : C ⥤ D) [is_left_adjoint L] (X : C) : coalgebra.a (functor.obj (comparison L) X) = functor.map L (nat_trans.app (adjunction.unit (adjunction.of_left_adjoint L)) X) := Eq.refl (coalgebra.a (functor.obj (comparison L) X)) /-- The underlying object of `(comonad.comparison L).obj X` is just `L.obj X`. -/ def comparison_forget {C : Type u₁} [category C] {D : Type u₂} [category D] (L : C ⥤ D) [is_left_adjoint L] : comparison L ⋙ forget (right_adjoint L ⋙ L) ≅ L := iso.mk (nat_trans.mk fun (X : C) => 𝟙) (nat_trans.mk fun (X : C) => 𝟙) end comonad /-- A right adjoint functor `R : D ⥤ C` is *monadic* if the comparison functor `monad.comparison R` from `D` to the category of Eilenberg-Moore algebras for the adjunction is an equivalence. -/ class monadic_right_adjoint {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) extends is_right_adjoint R where eqv : is_equivalence (monad.comparison R) /-- A left adjoint functor `L : C ⥤ D` is *comonadic* if the comparison functor `comonad.comparison L` from `C` to the category of Eilenberg-Moore algebras for the adjunction is an equivalence. -/ class comonadic_left_adjoint {C : Type u₁} [category C] {D : Type u₂} [category D] (L : C ⥤ D) extends is_left_adjoint L where eqv : is_equivalence (comonad.comparison L) -- TODO: This holds more generally for idempotent adjunctions, not just reflective adjunctions. protected instance μ_iso_of_reflective {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [reflective R] : is_iso μ_ := id (category_theory.is_iso_whisker_right (whisker_left (left_adjoint R) (adjunction.counit (adjunction.of_right_adjoint R))) R) namespace reflective protected instance app.category_theory.is_iso {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [reflective R] (X : monad.algebra (left_adjoint R ⋙ R)) : is_iso (nat_trans.app (adjunction.unit (adjunction.of_right_adjoint R)) (monad.algebra.A X)) := is_iso.mk (monad.algebra.a X) protected instance comparison_ess_surj {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [reflective R] : ess_surj (monad.comparison R) := sorry protected instance comparison_full {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [full R] [is_right_adjoint R] : full (monad.comparison R) := full.mk fun (X Y : D) (f : functor.obj (monad.comparison R) X ⟶ functor.obj (monad.comparison R) Y) => functor.preimage R (monad.algebra.hom.f f) protected instance comparison_faithful {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [faithful R] [is_right_adjoint R] : faithful (monad.comparison R) := faithful.mk end reflective -- It is possible to do this computably since the construction gives the data of the inverse, not -- just the existence of an inverse on each object. /-- Any reflective inclusion has a monadic right adjoint. cf Prop 5.3.3 of [Riehl][riehl2017] -/ protected instance monadic_of_reflective {C : Type u₁} [category C] {D : Type u₂} [category D] (R : D ⥤ C) [reflective R] : monadic_right_adjoint R := monadic_right_adjoint.mk (equivalence.equivalence_of_fully_faithfully_ess_surj (monad.comparison R))
0351e31837eb539a0c07002906012e654f4a6aed
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/category_theory/limits/over.lean
cd10aed7233e14007f2476f2218e8a734311fcc0
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
5,405
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Johan Commelin, Reid Barton import category_theory.comma import category_theory.limits.preserves universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation open category_theory category_theory.limits variables {J : Type v} [small_category J] variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variable {X : C} namespace category_theory.functor def to_cocone (F : J ⥤ over X) : cocone (F ⋙ over.forget) := { X := X, ι := { app := λ j, (F.obj j).hom } } @[simp] lemma to_cocone_X (F : J ⥤ over X) : F.to_cocone.X = X := rfl @[simp] lemma to_cocone_ι (F : J ⥤ over X) (j : J) : F.to_cocone.ι.app j = (F.obj j).hom := rfl def to_cone (F : J ⥤ under X) : cone (F ⋙ under.forget) := { X := X, π := { app := λ j, (F.obj j).hom } } @[simp] lemma to_cone_X (F : J ⥤ under X) : F.to_cone.X = X := rfl @[simp] lemma to_cone_π (F : J ⥤ under X) (j : J) : F.to_cone.π.app j = (F.obj j).hom := rfl end category_theory.functor namespace category_theory.over def colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : cocone F := { X := mk $ colimit.desc (F ⋙ forget) F.to_cocone, ι := { app := λ j, hom_mk $ colimit.ι (F ⋙ forget) j, naturality' := begin intros j j' f, have := colimit.w (F ⋙ forget) f, tidy end } } @[simp] lemma colimit_X_hom (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : ((colimit F).X).hom = colimit.desc (F ⋙ forget) F.to_cocone := rfl @[simp] lemma colimit_ι_app (F : J ⥤ over X) [has_colimit (F ⋙ forget)] (j : J) : ((colimit F).ι).app j = hom_mk (colimit.ι (F ⋙ forget) j) := rfl def forget_colimit_is_colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : is_colimit (forget.map_cocone (colimit F)) := is_colimit.of_iso_colimit (colimit.is_colimit (F ⋙ forget)) (cocones.ext (iso.refl _) (by tidy)) instance : reflects_colimits (forget : over X ⥤ C) := λ J 𝒥 F, by constructor; exactI λ t ht, { desc := λ s, hom_mk (ht.desc (forget.map_cocone s)) begin apply ht.hom_ext, intro j, rw [←category.assoc, ht.fac], transitivity (F.obj j).hom, exact w (s.ι.app j), -- TODO: How to write (s.ι.app j).w? exact (w (t.ι.app j)).symm, end, fac' := begin intros s j, ext, exact ht.fac (forget.map_cocone s) j -- TODO: Ask Simon about multiple ext lemmas for defeq types (comma_morphism & over.category.hom) end, uniq' := begin intros s m w, ext1 j, exact ht.uniq (forget.map_cocone s) m.left (λ j, congr_arg comma_morphism.left (w j)) end } instance has_colimit {F : J ⥤ over X} [has_colimit (F ⋙ forget)] : has_colimit F := { cocone := colimit F, is_colimit := reflects_colimit.reflects (forget_colimit_is_colimit F) } instance has_colimits_of_shape [has_colimits_of_shape J C] : has_colimits_of_shape J (over X) := λ F, infer_instance instance has_colimits [has_colimits C] : has_colimits (over X) := λ J 𝒥, by resetI; apply_instance instance forget_preserves_colimits [has_colimits C] {X : C} : preserves_colimits (forget : over X ⥤ C) := λ J 𝒥 F, by exactI preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit F) (forget_colimit_is_colimit F) end category_theory.over namespace category_theory.under def limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : cone F := { X := mk $ limit.lift (F ⋙ forget) F.to_cone, π := { app := λ j, hom_mk $ limit.π (F ⋙ forget) j, naturality' := begin intros j j' f, have := (limit.w (F ⋙ forget) f).symm, tidy end } } @[simp] lemma limit_X_hom (F : J ⥤ under X) [has_limit (F ⋙ forget)] : ((limit F).X).hom = limit.lift (F ⋙ forget) F.to_cone := rfl @[simp] lemma limit_π_app (F : J ⥤ under X) [has_limit (F ⋙ forget)] (j : J) : ((limit F).π).app j = hom_mk (limit.π (F ⋙ forget) j) := rfl def forget_limit_is_limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : is_limit (forget.map_cone (limit F)) := is_limit.of_iso_limit (limit.is_limit (F ⋙ forget)) (cones.ext (iso.refl _) (by tidy)) instance : reflects_limits (forget : under X ⥤ C) := λ J 𝒥 F, by constructor; exactI λ t ht, { lift := λ s, hom_mk (ht.lift (forget.map_cone s)) begin apply ht.hom_ext, intro j, rw [category.assoc, ht.fac], transitivity (F.obj j).hom, exact w (s.π.app j), exact (w (t.π.app j)).symm, end, fac' := begin intros s j, ext, exact ht.fac (forget.map_cone s) j end, uniq' := begin intros s m w, ext1 j, exact ht.uniq (forget.map_cone s) m.right (λ j, congr_arg comma_morphism.right (w j)) end } instance has_limit {F : J ⥤ under X} [has_limit (F ⋙ forget)] : has_limit F := { cone := limit F, is_limit := reflects_limit.reflects (forget_limit_is_limit F) } instance has_limits_of_shape [has_limits_of_shape J C] : has_limits_of_shape J (under X) := λ F, infer_instance instance has_limits [has_limits C] : has_limits (under X) := λ J 𝒥, by resetI; apply_instance instance forget_preserves_limits [has_limits C] {X : C} : preserves_limits (forget : under X ⥤ C) := λ J 𝒥 F, by exactI preserves_limit_of_preserves_limit_cone (limit.is_limit F) (forget_limit_is_limit F) end category_theory.under
2560650c04acae06004d0c73f9fcbd749e0b9839
d1bbf1801b3dcb214451d48214589f511061da63
/src/topology/algebra/infinite_sum.lean
7c22845ebc152c1598ea9815e3bb1b867e3933ce
[ "Apache-2.0" ]
permissive
cheraghchi/mathlib
5c366f8c4f8e66973b60c37881889da8390cab86
f29d1c3038422168fbbdb2526abf7c0ff13e86db
refs/heads/master
1,676,577,831,283
1,610,894,638,000
1,610,894,638,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
43,120
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.intervals import topology.instances.real import topology.algebra.module import data.indicator_function import data.equiv.encodable.lattice import order.filter.at_top_bot /-! # Infinite sum over a topological monoid This sum is known as unconditionally convergent, as it sums to the same value under all possible permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute convergence. Note: There are summable sequences which are not unconditionally convergent! The other way holds generally, see `has_sum.tendsto_sum_nat`. ## References * Bourbaki: General Topology (1995), Chapter 3 §5 (Infinite sums in commutative groups) -/ noncomputable theory open finset filter function classical open_locale topological_space classical big_operators nnreal variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section has_sum variables [add_comm_monoid α] [topological_space α] /-- Infinite sum on a topological monoid The `at_top` filter on `finset β` is the limit of all finite sets towards the entire type. So we sum up bigger and bigger sets. This sum operation is invariant under reordering. In particular, the function `ℕ → ℝ` sending `n` to `(-1)^n / (n+1)` does not have a sum for this definition, but a series which is absolutely convergent will have the correct sum. This is based on Mario Carneiro's [infinite sum `df-tsms` in Metamath](http://us.metamath.org/mpeuni/df-tsms.html). For the definition or many statements, `α` does not need to be a topological monoid. We only add this assumption later, for the lemmas where it is relevant. -/ def has_sum (f : β → α) (a : α) : Prop := tendsto (λs:finset β, ∑ b in s, f b) at_top (𝓝 a) /-- `summable f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/ def summable (f : β → α) : Prop := ∃a, has_sum f a /-- `∑' i, f i` is the sum of `f` it exists, or 0 otherwise -/ @[irreducible] def tsum {β} (f : β → α) := if h : summable f then classical.some h else 0 -- see Note [operator precedence of big operators] notation `∑'` binders `, ` r:(scoped:67 f, tsum f) := r variables {f g : β → α} {a b : α} {s : finset β} lemma summable.has_sum (ha : summable f) : has_sum f (∑'b, f b) := by simp [ha, tsum]; exact some_spec ha lemma has_sum.summable (h : has_sum f a) : summable f := ⟨a, h⟩ /-- Constant zero function has sum `0` -/ lemma has_sum_zero : has_sum (λb, 0 : β → α) 0 := by simp [has_sum, tendsto_const_nhds] lemma summable_zero : summable (λb, 0 : β → α) := has_sum_zero.summable lemma tsum_eq_zero_of_not_summable (h : ¬ summable f) : ∑'b, f b = 0 := by simp [tsum, h] lemma has_sum.has_sum_of_sum_eq {g : γ → α} (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (hf : has_sum g a) : has_sum f a := le_trans (map_at_top_finset_sum_le_of_sum_eq h_eq) hf lemma has_sum_iff_has_sum {g : γ → α} (h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (h₂ : ∀v:finset β, ∃u:finset γ, ∀u', u ⊆ u' → ∃v', v ⊆ v' ∧ ∑ b in v', f b = ∑ x in u', g x) : has_sum f a ↔ has_sum g a := ⟨has_sum.has_sum_of_sum_eq h₂, has_sum.has_sum_of_sum_eq h₁⟩ lemma function.injective.has_sum_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : has_sum (f ∘ g) a ↔ has_sum f a := by simp only [has_sum, tendsto, hg.map_at_top_finset_sum_eq hf] lemma function.injective.summable_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : summable (f ∘ g) ↔ summable f := exists_congr $ λ _, hg.has_sum_iff hf lemma has_sum_subtype_iff_of_support_subset {s : set β} (hf : support f ⊆ s) : has_sum (f ∘ coe : s → α) a ↔ has_sum f a := subtype.coe_injective.has_sum_iff $ by simpa using support_subset_iff'.1 hf lemma has_sum_subtype_iff_indicator {s : set β} : has_sum (f ∘ coe : s → α) a ↔ has_sum (s.indicator f) a := by rw [← set.indicator_range_comp, subtype.range_coe, has_sum_subtype_iff_of_support_subset set.support_indicator] @[simp] lemma has_sum_subtype_support : has_sum (f ∘ coe : support f → α) a ↔ has_sum f a := has_sum_subtype_iff_of_support_subset $ set.subset.refl _ lemma has_sum_fintype [fintype β] (f : β → α) : has_sum f (∑ b, f b) := order_top.tendsto_at_top_nhds _ protected lemma finset.has_sum (s : finset β) (f : β → α) : has_sum (f ∘ coe : (↑s : set β) → α) (∑ b in s, f b) := by { rw ← sum_attach, exact has_sum_fintype _ } protected lemma finset.summable (s : finset β) (f : β → α) : summable (f ∘ coe : (↑s : set β) → α) := (s.has_sum f).summable protected lemma set.finite.summable {s : set β} (hs : s.finite) (f : β → α) : summable (f ∘ coe : s → α) := by convert hs.to_finset.summable f; simp only [hs.coe_to_finset] /-- If a function `f` vanishes outside of a finite set `s`, then it `has_sum` `∑ b in s, f b`. -/ lemma has_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : has_sum f (∑ b in s, f b) := (has_sum_subtype_iff_of_support_subset $ support_subset_iff'.2 hf).1 $ s.has_sum f lemma summable_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : summable f := (has_sum_sum_of_ne_finset_zero hf).summable lemma has_sum_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : has_sum f (f b) := suffices has_sum f (∑ b' in {b}, f b'), by simpa using this, has_sum_sum_of_ne_finset_zero $ by simpa [hf] lemma has_sum_ite_eq (b : β) (a : α) : has_sum (λb', if b' = b then a else 0) a := begin convert has_sum_single b _, { exact (if_pos rfl).symm }, assume b' hb', exact if_neg hb' end lemma equiv.has_sum_iff (e : γ ≃ β) : has_sum (f ∘ e) a ↔ has_sum f a := e.injective.has_sum_iff $ by simp lemma equiv.summable_iff (e : γ ≃ β) : summable (f ∘ e) ↔ summable f := exists_congr $ λ a, e.has_sum_iff lemma summable.prod_symm {f : β × γ → α} (hf : summable f) : summable (λ p : γ × β, f p.swap) := (equiv.prod_comm γ β).summable_iff.2 hf lemma equiv.has_sum_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : has_sum f a ↔ has_sum g a := have (g ∘ coe) ∘ e = f ∘ coe, from funext he, by rw [← has_sum_subtype_support, ← this, e.has_sum_iff, has_sum_subtype_support] lemma has_sum_iff_has_sum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : has_sum f a ↔ has_sum g a := iff.symm $ equiv.has_sum_iff_of_support (equiv.of_bijective (λ x, ⟨i x, λ hx, x.coe_prop $ hfg x ▸ hx⟩) ⟨λ x y h, subtype.ext $ hi $ subtype.ext_iff.1 h, λ y, (hf y.coe_prop).imp $ λ x hx, subtype.ext hx⟩) hfg lemma equiv.summable_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : summable f ↔ summable g := exists_congr $ λ _, e.has_sum_iff_of_support he protected lemma has_sum.map [add_comm_monoid γ] [topological_space γ] (hf : has_sum f a) (g : α →+ γ) (hg : continuous g) : has_sum (g ∘ f) (g a) := have g ∘ (λs:finset β, ∑ b in s, f b) = (λs:finset β, ∑ b in s, g (f b)), from funext $ g.map_sum _, show tendsto (λs:finset β, ∑ b in s, g (f b)) at_top (𝓝 (g a)), from this ▸ (hg.tendsto a).comp hf protected lemma summable.map [add_comm_monoid γ] [topological_space γ] (hf : summable f) (g : α →+ γ) (hg : continuous g) : summable (g ∘ f) := (hf.has_sum.map g hg).summable /-- If `f : ℕ → α` has sum `a`, then the partial sums `∑_{i=0}^{n-1} f i` converge to `a`. -/ lemma has_sum.tendsto_sum_nat {f : ℕ → α} (h : has_sum f a) : tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := h.comp tendsto_finset_range lemma has_sum.unique {a₁ a₂ : α} [t2_space α] : has_sum f a₁ → has_sum f a₂ → a₁ = a₂ := tendsto_nhds_unique lemma summable.has_sum_iff_tendsto_nat [t2_space α] {f : ℕ → α} {a : α} (hf : summable f) : has_sum f a ↔ tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := begin refine ⟨λ h, h.tendsto_sum_nat, λ h, _⟩, rw tendsto_nhds_unique h hf.has_sum.tendsto_sum_nat, exact hf.has_sum end lemma equiv.summable_iff_of_has_sum_iff {α' : Type*} [add_comm_monoid α'] [topological_space α'] (e : α' ≃ α) {f : β → α} {g : γ → α'} (he : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : summable f ↔ summable g := ⟨λ ⟨a, ha⟩, ⟨e.symm a, he.1 $ by rwa [e.apply_symm_apply]⟩, λ ⟨a, ha⟩, ⟨e a, he.2 ha⟩⟩ variable [has_continuous_add α] lemma has_sum.add (hf : has_sum f a) (hg : has_sum g b) : has_sum (λb, f b + g b) (a + b) := by simp only [has_sum, sum_add_distrib]; exact hf.add hg lemma summable.add (hf : summable f) (hg : summable g) : summable (λb, f b + g b) := (hf.has_sum.add hg.has_sum).summable lemma has_sum_sum {f : γ → β → α} {a : γ → α} {s : finset γ} : (∀i∈s, has_sum (f i) (a i)) → has_sum (λb, ∑ i in s, f i b) (∑ i in s, a i) := finset.induction_on s (by simp only [has_sum_zero, sum_empty, forall_true_iff]) (by simp only [has_sum.add, sum_insert, mem_insert, forall_eq_or_imp, forall_2_true_iff, not_false_iff, forall_true_iff] {contextual := tt}) lemma summable_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : summable (λb, ∑ i in s, f i b) := (has_sum_sum $ assume i hi, (hf i hi).has_sum).summable lemma has_sum.add_compl {s : set β} (ha : has_sum (f ∘ coe : s → α) a) (hb : has_sum (f ∘ coe : sᶜ → α) b) : has_sum f (a + b) := by simpa using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma summable.add_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : summable f := (hs.has_sum.add_compl hsc.has_sum).summable lemma has_sum.compl_add {s : set β} (ha : has_sum (f ∘ coe : sᶜ → α) a) (hb : has_sum (f ∘ coe : s → α) b) : has_sum f (a + b) := by simpa using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma summable.compl_add {s : set β} (hs : summable (f ∘ coe : sᶜ → α)) (hsc : summable (f ∘ coe : s → α)) : summable f := (hs.has_sum.compl_add hsc.has_sum).summable lemma has_sum.sigma [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) : has_sum g a := begin refine (at_top_basis.tendsto_iff (closed_nhds_basis a)).mpr _, rintros s ⟨hs, hsc⟩, rcases mem_at_top_sets.mp (ha hs) with ⟨u, hu⟩, use [u.image sigma.fst, trivial], intros bs hbs, simp only [set.mem_preimage, ge_iff_le, finset.le_iff_subset] at hu, have : tendsto (λ t : finset (Σ b, γ b), ∑ p in t.filter (λ p, p.1 ∈ bs), f p) at_top (𝓝 $ ∑ b in bs, g b), { simp only [← sigma_preimage_mk, sum_sigma], refine tendsto_finset_sum _ (λ b hb, _), change tendsto (λ t, (λ t, ∑ s in t, f ⟨b, s⟩) (preimage t (sigma.mk b) _)) at_top (𝓝 (g b)), exact tendsto.comp (hf b) (tendsto_finset_preimage_at_top_at_top _) }, refine hsc.mem_of_tendsto this (eventually_at_top.2 ⟨u, λ t ht, hu _ (λ x hx, _)⟩), exact mem_filter.2 ⟨ht hx, hbs $ mem_image_of_mem _ hx⟩ end /-- If a series `f` on `β × γ` has sum `a` and for each `b` the restriction of `f` to `{b} × γ` has sum `g b`, then the series `g` has sum `a`. -/ lemma has_sum.prod_fiberwise [regular_space α] {f : β × γ → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f (b, c)) (g b)) : has_sum g a := has_sum.sigma ((equiv.sigma_equiv_prod β γ).has_sum_iff.2 ha) hf lemma summable.sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (hf : ∀b, summable (λc, f ⟨b, c⟩)) : summable (λb, ∑'c, f ⟨b, c⟩) := (ha.has_sum.sigma (assume b, (hf b).has_sum)).summable lemma has_sum.sigma_of_has_sum [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum g a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) (hf' : summable f) : has_sum f a := by simpa [(hf'.has_sum.sigma hf).unique ha] using hf'.has_sum end has_sum section tsum variables [add_comm_monoid α] [topological_space α] [t2_space α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum.tsum_eq (ha : has_sum f a) : ∑'b, f b = a := (summable.has_sum ⟨a, ha⟩).unique ha lemma summable.has_sum_iff (h : summable f) : has_sum f a ↔ ∑'b, f b = a := iff.intro has_sum.tsum_eq (assume eq, eq ▸ h.has_sum) @[simp] lemma tsum_zero : ∑'b:β, (0:α) = 0 := has_sum_zero.tsum_eq lemma tsum_eq_sum {f : β → α} {s : finset β} (hf : ∀b∉s, f b = 0) : ∑' b, f b = ∑ b in s, f b := (has_sum_sum_of_ne_finset_zero hf).tsum_eq lemma tsum_fintype [fintype β] (f : β → α) : ∑'b, f b = ∑ b, f b := (has_sum_fintype f).tsum_eq @[simp] lemma finset.tsum_subtype (s : finset β) (f : β → α) : ∑' x : {x // x ∈ s}, f x = ∑ x in s, f x := (s.has_sum f).tsum_eq @[simp] lemma finset.tsum_subtype' (s : finset β) (f : β → α) : ∑' x : (s : set β), f x = ∑ x in s, f x := s.tsum_subtype f lemma tsum_eq_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : ∑'b, f b = f b := (has_sum_single b hf).tsum_eq @[simp] lemma tsum_ite_eq (b : β) (a : α) : ∑'b', (if b' = b then a else 0) = a := (has_sum_ite_eq b a).tsum_eq lemma equiv.tsum_eq_tsum_of_has_sum_iff_has_sum {α' : Type*} [add_comm_monoid α'] [topological_space α'] (e : α' ≃ α) (h0 : e 0 = 0) {f : β → α} {g : γ → α'} (h : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : ∑' b, f b = e (∑' c, g c) := by_cases (assume : summable g, (h.mpr this.has_sum).tsum_eq) (assume hg : ¬ summable g, have hf : ¬ summable f, from mt (e.summable_iff_of_has_sum_iff @h).1 hg, by simp [tsum, hf, hg, h0]) lemma tsum_eq_tsum_of_has_sum_iff_has_sum {f : β → α} {g : γ → α} (h : ∀{a}, has_sum f a ↔ has_sum g a) : ∑'b, f b = ∑'c, g c := (equiv.refl α).tsum_eq_tsum_of_has_sum_iff_has_sum rfl @h lemma equiv.tsum_eq (j : γ ≃ β) (f : β → α) : ∑'c, f (j c) = ∑'b, f b := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ a, j.has_sum_iff lemma equiv.tsum_eq_tsum_of_support {f : β → α} {g : γ → α} (e : support f ≃ support g) (he : ∀ x, g (e x) = f x) : (∑' x, f x) = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, e.has_sum_iff_of_support he lemma tsum_eq_tsum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : ∑' x, f x = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_iff_has_sum_of_ne_zero_bij i hi hf hfg lemma tsum_subtype (s : set β) (f : β → α) : ∑' x:s, f x = ∑' x, s.indicator f x := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_subtype_iff_indicator section has_continuous_add variable [has_continuous_add α] lemma tsum_add (hf : summable f) (hg : summable g) : ∑'b, (f b + g b) = (∑'b, f b) + (∑'b, g b) := (hf.has_sum.add hg.has_sum).tsum_eq lemma tsum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : ∑'b, ∑ i in s, f i b = ∑ i in s, ∑'b, f i b := (has_sum_sum $ assume i hi, (hf i hi).has_sum).tsum_eq lemma tsum_sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (h₁ : ∀b, summable (λc, f ⟨b, c⟩)) (h₂ : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := (h₂.has_sum.sigma (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_prod' [regular_space α] {f : β × γ → α} (h : summable f) (h₁ : ∀b, summable (λc, f (b, c))) : ∑'p, f p = ∑'b c, f (b, c) := (h.has_sum.prod_fiberwise (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_comm' [regular_space α] {f : β → γ → α} (h : summable (function.uncurry f)) (h₁ : ∀b, summable (f b)) (h₂ : ∀ c, summable (λ b, f b c)) : ∑' c b, f b c = ∑' b c, f b c := begin erw [← tsum_prod' h h₁, ← tsum_prod' h.prod_symm h₂, ← (equiv.prod_comm β γ).tsum_eq], refl, assumption end end has_continuous_add section encodable open encodable variable [encodable γ] /-- You can compute a sum over an encodably type by summing over the natural numbers and taking a supremum. This is useful for outer measures. -/ theorem tsum_supr_decode2 [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (s : γ → β) : ∑' i : ℕ, m (⨆ b ∈ decode2 γ i, s b) = ∑' b : γ, m (s b) := begin have H : ∀ n, m (⨆ b ∈ decode2 γ n, s b) ≠ 0 → (decode2 γ n).is_some, { intros n h, cases decode2 γ n with b, { refine (h $ by simp [m0]).elim }, { exact rfl } }, symmetry, refine tsum_eq_tsum_of_ne_zero_bij (λ a, option.get (H a.1 a.2)) _ _ _, { rintros ⟨m, hm⟩ ⟨n, hn⟩ e, have := mem_decode2.1 (option.get_mem (H n hn)), rwa [← e, mem_decode2.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨⟨encode b, _⟩, _⟩, { simp only [mem_support, encodek2] at h ⊢, convert h, simp [set.ext_iff, encodek2] }, { exact option.get_of_mem _ (encodek2 _) } }, { rintros ⟨n, h⟩, dsimp only [subtype.coe_mk], transitivity, swap, rw [show decode2 γ n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end /-- `tsum_supr_decode2` specialized to the complete lattice of sets. -/ theorem tsum_Union_decode2 (m : set β → α) (m0 : m ∅ = 0) (s : γ → set β) : ∑' i, m (⋃ b ∈ decode2 γ i, s b) = ∑' b, m (s b) := tsum_supr_decode2 m m0 s /-! Some properties about measure-like functions. These could also be functions defined on complete sublattices of sets, with the property that they are countably sub-additive. `R` will probably be instantiated with `(≤)` in all applications. -/ /-- If a function is countably sub-additive then it is sub-additive on encodable types -/ theorem rel_supr_tsum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) ∑' i, m (s i)) (s : γ → β) : R (m (⨆ b : γ, s b)) ∑' b : γ, m (s b) := by { rw [← supr_decode2, ← tsum_supr_decode2 _ m0 s], exact m_supr _ } /-- If a function is countably sub-additive then it is sub-additive on finite sets -/ theorem rel_supr_sum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s : δ → β) (t : finset δ) : R (m (⨆ d ∈ t, s d)) (∑ d in t, m (s d)) := by { cases t.nonempty_encodable, rw [supr_subtype'], convert rel_supr_tsum m m0 R m_supr _, rw [← finset.tsum_subtype], assumption } /-- If a function is countably sub-additive then it is binary sub-additive -/ theorem rel_sup_add [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s₁ s₂ : β) : R (m (s₁ ⊔ s₂)) (m s₁ + m s₂) := begin convert rel_supr_tsum m m0 R m_supr (λ b, cond b s₁ s₂), { simp only [supr_bool_eq, cond] }, { rw [tsum_fintype, fintype.sum_bool, cond, cond] } end end encodable end tsum section pi variables {ι : Type*} {π : α → Type*} [∀ x, add_comm_monoid (π x)] [∀ x, topological_space (π x)] lemma pi.has_sum {f : ι → ∀ x, π x} {g : ∀ x, π x} : has_sum f g ↔ ∀ x, has_sum (λ i, f i x) (g x) := by simp [has_sum, tendsto_pi] lemma pi.summable {f : ι → ∀ x, π x} : summable f ↔ ∀ x, summable (λ i, f i x) := by simp [summable, pi.has_sum, classical.skolem] lemma tsum_apply [∀ x, t2_space (π x)] {f : ι → ∀ x, π x}{x : α} (hf : summable f) : (∑' i, f i) x = ∑' i, f i x := (pi.has_sum.mp hf.has_sum x).tsum_eq.symm end pi section topological_group variables [add_comm_group α] [topological_space α] [topological_add_group α] variables {f g : β → α} {a a₁ a₂ : α} -- `by simpa using` speeds up elaboration. Why? lemma has_sum.neg (h : has_sum f a) : has_sum (λb, - f b) (- a) := by simpa only using h.map (-add_monoid_hom.id α) continuous_neg lemma summable.neg (hf : summable f) : summable (λb, - f b) := hf.has_sum.neg.summable lemma summable.of_neg (hf : summable (λb, - f b)) : summable f := by simpa only [neg_neg] using hf.neg lemma summable_neg_iff : summable (λ b, - f b) ↔ summable f := ⟨summable.of_neg, summable.neg⟩ lemma has_sum.sub (hf : has_sum f a₁) (hg : has_sum g a₂) : has_sum (λb, f b - g b) (a₁ - a₂) := by { simp [sub_eq_add_neg], exact hf.add hg.neg } lemma summable.sub (hf : summable f) (hg : summable g) : summable (λb, f b - g b) := (hf.has_sum.sub hg.has_sum).summable lemma has_sum.has_sum_compl_iff {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum (f ∘ coe : sᶜ → α) a₂ ↔ has_sum f (a₁ + a₂) := begin refine ⟨λ h, hf.add_compl h, λ h, _⟩, rw [has_sum_subtype_iff_indicator] at hf ⊢, rw [set.indicator_compl], simpa only [add_sub_cancel'] using h.sub hf end lemma has_sum.has_sum_iff_compl {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum f a₂ ↔ has_sum (f ∘ coe : sᶜ → α) (a₂ - a₁) := iff.symm $ hf.has_sum_compl_iff.trans $ by rw [add_sub_cancel'_right] lemma summable.summable_compl_iff {s : set β} (hf : summable (f ∘ coe : s → α)) : summable (f ∘ coe : sᶜ → α) ↔ summable f := ⟨λ ⟨a, ha⟩, (hf.has_sum.has_sum_compl_iff.1 ha).summable, λ ⟨a, ha⟩, (hf.has_sum.has_sum_iff_compl.1 ha).summable⟩ protected lemma finset.has_sum_compl_iff (s : finset β) : has_sum (λ x : {x // x ∉ s}, f x) a ↔ has_sum f (a + ∑ i in s, f i) := (s.has_sum f).has_sum_compl_iff.trans $ by rw [add_comm] protected lemma finset.has_sum_iff_compl (s : finset β) : has_sum f a ↔ has_sum (λ x : {x // x ∉ s}, f x) (a - ∑ i in s, f i) := (s.has_sum f).has_sum_iff_compl protected lemma finset.summable_compl_iff (s : finset β) : summable (λ x : {x // x ∉ s}, f x) ↔ summable f := (s.summable f).summable_compl_iff lemma set.finite.summable_compl_iff {s : set β} (hs : s.finite) : summable (f ∘ coe : sᶜ → α) ↔ summable f := (hs.summable f).summable_compl_iff section tsum variables [t2_space α] lemma tsum_neg (hf : summable f) : ∑'b, - f b = - ∑'b, f b := hf.has_sum.neg.tsum_eq lemma tsum_sub (hf : summable f) (hg : summable g) : ∑'b, (f b - g b) = ∑'b, f b - ∑'b, g b := (hf.has_sum.sub hg.has_sum).tsum_eq lemma tsum_add_tsum_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : (∑' x : s, f x) + (∑' x : sᶜ, f x) = ∑' x, f x := (hs.has_sum.add_compl hsc.has_sum).tsum_eq.symm lemma sum_add_tsum_compl {s : finset β} (hf : summable f) : (∑ x in s, f x) + (∑' x : (↑s : set β)ᶜ, f x) = ∑' x, f x := ((s.has_sum f).add_compl (s.summable_compl_iff.2 hf).has_sum).tsum_eq.symm end tsum /-! ### Sums on subtypes If `s` is a finset of `α`, we show that the summability of `f` in the whole space and on the subtype `univ - s` are equivalent, and relate their sums. For a function defined on `ℕ`, we deduce the formula `(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i)`, in `sum_add_tsum_nat_add`. -/ section subtype lemma has_sum_nat_add_iff {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) := begin refine iff.trans _ ((range k).has_sum_compl_iff), rw [← (not_mem_range_equiv k).symm.has_sum_iff], refl end lemma summable_nat_add_iff {f : ℕ → α} (k : ℕ) : summable (λ n, f (n + k)) ↔ summable f := iff.symm $ (equiv.add_right (∑ i in range k, f i)).summable_iff_of_has_sum_iff $ λ a, (has_sum_nat_add_iff k).symm lemma has_sum_nat_add_iff' {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) (a - ∑ i in range k, f i) ↔ has_sum f a := by simp [has_sum_nat_add_iff] lemma sum_add_tsum_nat_add [t2_space α] {f : ℕ → α} (k : ℕ) (h : summable f) : (∑ i in range k, f i) + (∑' i, f (i + k)) = ∑' i, f i := by simpa [add_comm] using ((has_sum_nat_add_iff k).1 ((summable_nat_add_iff k).2 h).has_sum).unique h.has_sum lemma tsum_eq_zero_add [t2_space α] {f : ℕ → α} (hf : summable f) : ∑'b, f b = f 0 + ∑'b, f (b + 1) := by simpa only [range_one, sum_singleton] using (sum_add_tsum_nat_add 1 hf).symm /-- For `f : ℕ → α`, then `∑' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add [t2_space α] (f : ℕ → α) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin by_cases hf : summable f, { have h₀ : (λ i, (∑' i, f i) - ∑ j in range i, f j) = λ i, ∑' (k : ℕ), f (k + i), { ext1 i, rw [sub_eq_iff_eq_add, add_comm, sum_add_tsum_nat_add i hf] }, have h₁ : tendsto (λ i : ℕ, ∑' i, f i) at_top (𝓝 (∑' i, f i)) := tendsto_const_nhds, simpa only [h₀, sub_self] using tendsto.sub h₁ hf.has_sum.tendsto_sum_nat }, { convert tendsto_const_nhds, ext1 i, rw ← summable_nat_add_iff i at hf, { exact tsum_eq_zero_of_not_summable hf }, { apply_instance } } end end subtype end topological_group section topological_semiring variables [semiring α] [topological_space α] [topological_semiring α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum.mul_left (a₂) (h : has_sum f a₁) : has_sum (λb, a₂ * f b) (a₂ * a₁) := by simpa only using h.map (add_monoid_hom.mul_left a₂) (continuous_const.mul continuous_id) lemma has_sum.mul_right (a₂) (hf : has_sum f a₁) : has_sum (λb, f b * a₂) (a₁ * a₂) := by simpa only using hf.map (add_monoid_hom.mul_right a₂) (continuous_id.mul continuous_const) lemma summable.mul_left (a) (hf : summable f) : summable (λb, a * f b) := (hf.has_sum.mul_left _).summable lemma summable.mul_right (a) (hf : summable f) : summable (λb, f b * a) := (hf.has_sum.mul_right _).summable section tsum variables [t2_space α] lemma summable.tsum_mul_left (a) (hf : summable f) : ∑'b, a * f b = a * ∑'b, f b := (hf.has_sum.mul_left _).tsum_eq lemma summable.tsum_mul_right (a) (hf : summable f) : (∑'b, f b * a) = (∑'b, f b) * a := (hf.has_sum.mul_right _).tsum_eq end tsum end topological_semiring section topological_semimodule variables {R : Type*} [semiring R] [topological_space R] [topological_space α] [add_comm_monoid α] [semimodule R α] [topological_semimodule R α] {f : β → α} lemma has_sum.smul {a : α} {r : R} (hf : has_sum f a) : has_sum (λ z, r • f z) (r • a) := hf.map (const_smul_hom α r) (continuous_const.smul continuous_id) lemma summable.smul {r : R} (hf : summable f) : summable (λ z, r • f z) := hf.has_sum.smul.summable lemma tsum_smul [t2_space α] {r : R} (hf : summable f) : ∑' z, r • f z = r • ∑' z, f z := hf.has_sum.smul.tsum_eq end topological_semimodule section division_ring variables [division_ring α] [topological_space α] [topological_semiring α] {f g : β → α} {a a₁ a₂ : α} lemma has_sum.div_const (h : has_sum f a) (b : α) : has_sum (λ x, f x / b) (a / b) := by simp only [div_eq_mul_inv, h.mul_right b⁻¹] lemma has_sum_mul_left_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, a₂ * f b) (a₂ * a₁) := ⟨has_sum.mul_left _, λ H, by simpa only [inv_mul_cancel_left' h] using H.mul_left a₂⁻¹⟩ lemma has_sum_mul_right_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, f b * a₂) (a₁ * a₂) := ⟨has_sum.mul_right _, λ H, by simpa only [mul_inv_cancel_right' h] using H.mul_right a₂⁻¹⟩ lemma summable_mul_left_iff (h : a ≠ 0) : summable f ↔ summable (λb, a * f b) := ⟨λ H, H.mul_left _, λ H, by simpa only [inv_mul_cancel_left' h] using H.mul_left a⁻¹⟩ lemma summable_mul_right_iff (h : a ≠ 0) : summable f ↔ summable (λb, f b * a) := ⟨λ H, H.mul_right _, λ H, by simpa only [mul_inv_cancel_right' h] using H.mul_right a⁻¹⟩ lemma tsum_mul_left [t2_space α] : (∑' x, a * f x) = a * ∑' x, f x := if hf : summable f then hf.tsum_mul_left a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_left_iff ha).2 hf), mul_zero] lemma tsum_mul_right [t2_space α] : (∑' x, f x * a) = (∑' x, f x) * a := if hf : summable f then hf.tsum_mul_right a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_right_iff ha).2 hf), zero_mul] end division_ring section order_topology variables [ordered_add_comm_monoid α] [topological_space α] [order_closed_topology α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum_le (h : ∀b, f b ≤ g b) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto' hf hg $ assume s, sum_le_sum $ assume b _, h b lemma has_sum_le_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := have has_sum (λc, (partial_inv i c).cases_on' 0 f) a₁, begin refine (has_sum_iff_has_sum_of_ne_zero_bij (i ∘ coe) _ _ _).2 hf, { exact assume c₁ c₂ eq, hi eq }, { intros c hc, rw [mem_support] at hc, cases eq : partial_inv i c with b; rw eq at hc, { contradiction }, { rw [partial_inv_of_injective hi] at eq, exact ⟨⟨b, hc⟩, eq⟩ } }, { assume c, simp [partial_inv_left hi, option.cases_on'] } end, begin refine has_sum_le (assume c, _) this hg, by_cases c ∈ set.range i, { rcases h with ⟨b, rfl⟩, rw [partial_inv_left hi, option.cases_on'], exact h _ }, { have : partial_inv i c = none := dif_neg h, rw [this, option.cases_on'], exact hs _ h } end lemma tsum_le_tsum_of_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : summable f) (hg : summable g) : tsum f ≤ tsum g := has_sum_le_inj i hi hs h hf.has_sum hg.has_sum lemma sum_le_has_sum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : has_sum f a) : ∑ b in s, f b ≤ a := ge_of_tendsto hf (eventually_at_top.2 ⟨s, λ t hst, sum_le_sum_of_subset_of_nonneg hst $ λ b hbt hbs, hs b hbs⟩) lemma le_has_sum (hf : has_sum f a) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ a := calc f b = ∑ b in {b}, f b : finset.sum_singleton.symm ... ≤ a : sum_le_has_sum _ (by { convert hb, simp }) hf lemma sum_le_tsum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : summable f) : ∑ b in s, f b ≤ tsum f := sum_le_has_sum s hs hf.has_sum lemma le_tsum (hf : summable f) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ ∑' b, f b := le_has_sum (summable.has_sum hf) b hb lemma tsum_le_tsum (h : ∀b, f b ≤ g b) (hf : summable f) (hg : summable g) : ∑'b, f b ≤ ∑'b, g b := has_sum_le h hf.has_sum hg.has_sum lemma has_sum.nonneg (h : ∀ b, 0 ≤ g b) (ha : has_sum g a) : 0 ≤ a := has_sum_le h has_sum_zero ha lemma has_sum.nonpos (h : ∀ b, g b ≤ 0) (ha : has_sum g a) : a ≤ 0 := has_sum_le h ha has_sum_zero lemma tsum_nonneg (h : ∀ b, 0 ≤ g b) : 0 ≤ ∑'b, g b := begin by_cases hg : summable g, { exact hg.has_sum.nonneg h }, { simp [tsum_eq_zero_of_not_summable hg] } end lemma tsum_nonpos (h : ∀ b, f b ≤ 0) : ∑'b, f b ≤ 0 := begin by_cases hf : summable f, { exact hf.has_sum.nonpos h }, { simp [tsum_eq_zero_of_not_summable hf] } end end order_topology section canonically_ordered variables [canonically_ordered_add_monoid α] [topological_space α] [order_closed_topology α] variables {f : β → α} {a : α} lemma le_has_sum' (hf : has_sum f a) (b : β) : f b ≤ a := le_has_sum hf b $ λ _ _, zero_le _ lemma le_tsum' (hf : summable f) (b : β) : f b ≤ ∑' b, f b := le_tsum hf b $ λ _ _, zero_le _ lemma has_sum_zero_iff : has_sum f 0 ↔ ∀ x, f x = 0 := begin refine ⟨_, λ h, _⟩, { contrapose!, exact λ ⟨x, hx⟩ h, irrefl _ (lt_of_lt_of_le (pos_iff_ne_zero.2 hx) (le_has_sum' h x)) }, { convert has_sum_zero, exact funext h } end lemma tsum_eq_zero_iff (hf : summable f) : ∑' i, f i = 0 ↔ ∀ x, f x = 0 := by rw [←has_sum_zero_iff, hf.has_sum_iff] end canonically_ordered section uniform_group variables [add_comm_group α] [uniform_space α] lemma summable_iff_cauchy_seq_finset [complete_space α] {f : β → α} : summable f ↔ cauchy_seq (λ (s : finset β), ∑ b in s, f b) := cauchy_map_iff_exists_tendsto.symm variables [uniform_add_group α] {f g : β → α} {a a₁ a₂ : α} lemma cauchy_seq_finset_iff_vanishing : cauchy_seq (λ (s : finset β), ∑ b in s, f b) ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := begin simp only [cauchy_seq, cauchy_map_iff, and_iff_right at_top_ne_bot, prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero α, tendsto_comap_iff, (∘)], rw [tendsto_at_top'], split, { assume h e he, rcases h e he with ⟨⟨s₁, s₂⟩, h⟩, use [s₁ ∪ s₂], assume t ht, specialize h (s₁ ∪ s₂, (s₁ ∪ s₂) ∪ t) ⟨le_sup_left, le_sup_left_of_le le_sup_right⟩, simpa only [finset.sum_union ht.symm, add_sub_cancel'] using h }, { assume h e he, rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩, rcases h d hd with ⟨s, h⟩, use [(s, s)], rintros ⟨t₁, t₂⟩ ⟨ht₁, ht₂⟩, have : ∑ b in t₂, f b - ∑ b in t₁, f b = ∑ b in t₂ \ s, f b - ∑ b in t₁ \ s, f b, { simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff ht₂).symm, add_sub_add_right_eq_sub] }, simp only [this], exact hde _ (h _ finset.sdiff_disjoint) _ (h _ finset.sdiff_disjoint) } end variable [complete_space α] lemma summable_iff_vanishing : summable f ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing] /- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/ lemma summable.summable_of_eq_zero_or_self (hf : summable f) (h : ∀b, g b = 0 ∨ g b = f b) : summable g := summable_iff_vanishing.2 $ assume e he, let ⟨s, hs⟩ := summable_iff_vanishing.1 hf e he in ⟨s, assume t ht, have eq : ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t, g b := calc ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t.filter (λb, g b = f b), g b : finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm) ... = ∑ b in t, g b : begin refine finset.sum_subset (finset.filter_subset _ _) _, assume b hbt hb, simp only [(∉), finset.mem_filter, and_iff_right hbt] at hb, exact (h b).resolve_right hb end, eq ▸ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _ _) ht⟩ protected lemma summable.indicator (hf : summable f) (s : set β) : summable (s.indicator f) := hf.summable_of_eq_zero_or_self $ set.indicator_eq_zero_or_self _ _ lemma summable.comp_injective {i : γ → β} (hf : summable f) (hi : injective i) : summable (f ∘ i) := begin simpa only [set.indicator_range_comp] using (hi.summable_iff _).2 (hf.indicator (set.range i)), exact λ x hx, set.indicator_of_not_mem hx _ end lemma summable.subtype (hf : summable f) (s : set β) : summable (f ∘ coe : s → α) := hf.comp_injective subtype.coe_injective lemma summable_subtype_and_compl {s : set β} : summable (λ x : s, f x) ∧ summable (λ x : sᶜ, f x) ↔ summable f := ⟨and_imp.2 summable.add_compl, λ h, ⟨h.subtype s, h.subtype sᶜ⟩⟩ lemma summable.sigma_factor {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (b : β) : summable (λc, f ⟨b, c⟩) := ha.comp_injective sigma_mk_injective lemma summable.sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : summable (λb, ∑'c, f ⟨b, c⟩) := ha.sigma' (λ b, ha.sigma_factor b) lemma summable.prod_factor {f : β × γ → α} (h : summable f) (b : β) : summable (λ c, f (b, c)) := h.comp_injective $ λ c₁ c₂ h, (prod.ext_iff.1 h).2 lemma tsum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_sigma' (λ b, ha.sigma_factor b) ha lemma tsum_prod [regular_space α] {f : β × γ → α} (h : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_prod' h h.prod_factor lemma tsum_comm [regular_space α] {f : β → γ → α} (h : summable (function.uncurry f)) : ∑' c b, f b c = ∑' b c, f b c := tsum_comm' h h.prod_factor h.prod_symm.prod_factor end uniform_group section topological_group variables {G : Type*} [topological_space G] [add_comm_group G] [topological_add_group G] {f : α → G} lemma summable.vanishing (hf : summable f) ⦃e : set G⦄ (he : e ∈ 𝓝 (0 : G)) : ∃ s : finset α, ∀ t, disjoint t s → ∑ k in t, f k ∈ e := begin letI : uniform_space G := topological_add_group.to_uniform_space G, letI : uniform_add_group G := topological_add_group_is_uniform, rcases hf with ⟨y, hy⟩, exact cauchy_seq_finset_iff_vanishing.1 hy.cauchy_seq e he end /-- Series divergence test: if `f` is a convergent series, then `f x` tends to zero along `cofinite`. -/ lemma summable.tendsto_cofinite_zero (hf : summable f) : tendsto f cofinite (𝓝 0) := begin intros e he, rw [filter.mem_map], rcases hf.vanishing he with ⟨s, hs⟩, refine s.eventually_cofinite_nmem.mono (λ x hx, _), by simpa using hs {x} (singleton_disjoint.2 hx) end end topological_group lemma summable_abs_iff [linear_ordered_add_comm_group β] [uniform_space β] [uniform_add_group β] [complete_space β] {f : α → β} : summable (λ x, abs (f x)) ↔ summable f := have h1 : ∀ x : {x | 0 ≤ f x}, abs (f x) = f x := λ x, abs_of_nonneg x.2, have h2 : ∀ x : {x | 0 ≤ f x}ᶜ, abs (f x) = -f x := λ x, abs_of_neg (not_le.1 x.2), calc summable (λ x, abs (f x)) ↔ summable (λ x : {x | 0 ≤ f x}, abs (f x)) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, abs (f x)) : summable_subtype_and_compl.symm ... ↔ summable (λ x : {x | 0 ≤ f x}, f x) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, -f x) : by simp only [h1, h2] ... ↔ _ : by simp only [summable_neg_iff, summable_subtype_and_compl] alias summable_abs_iff ↔ summable.of_abs summable.abs section cauchy_seq open finset.Ico filter /-- If the extended distance between consequent points of a sequence is estimated by a summable series of `nnreal`s, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_edist_le_of_summable [emetric_space α] {f : ℕ → α} (d : ℕ → ℝ≥0) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine emetric.cauchy_seq_iff_nnreal.2 (λ ε εpos, _), -- Actually we need partial sums of `d` to be a Cauchy sequence replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, -- Now we take the same `N` as in one of the definitions of a Cauchy sequence refine (metric.cauchy_seq_iff'.1 hd ε (nnreal.coe_pos.2 εpos)).imp (λ N hN n hn, _), have hsum := hN n hn, -- We simplify the known inequality rw [dist_nndist, nnreal.nndist_eq, ← sum_range_add_sum_Ico _ hn, nnreal.add_sub_cancel'] at hsum, norm_cast at hsum, replace hsum := lt_of_le_of_lt (le_max_left _ _) hsum, rw edist_comm, -- Then use `hf` to simplify the goal to the same form apply lt_of_le_of_lt (edist_le_Ico_sum_of_edist_le hn (λ k _ _, hf k)), assumption_mod_cast end /-- If the distance between consequent points of a sequence is estimated by a summable series, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_dist_le_of_summable [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine metric.cauchy_seq_iff'.2 (λε εpos, _), replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, refine (metric.cauchy_seq_iff'.1 hd ε εpos).imp (λ N hN n hn, _), have hsum := hN n hn, rw [real.dist_eq, ← sum_Ico_eq_sub _ hn] at hsum, calc dist (f n) (f N) = dist (f N) (f n) : dist_comm _ _ ... ≤ ∑ x in Ico N n, d x : dist_le_Ico_sum_of_dist_le hn (λ k _ _, hf k) ... ≤ abs (∑ x in Ico N n, d x) : le_abs_self _ ... < ε : hsum end lemma cauchy_seq_of_summable_dist [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ (λ _, le_refl _) h lemma dist_le_tsum_of_dist_le_of_tendsto [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.dist ha) (eventually_at_top.2 ⟨n, λ m hnm, _⟩), refine le_trans (dist_le_Ico_sum_of_dist_le hnm (λ k _ _, hf k)) _, rw [sum_Ico_eq_sum_range], refine sum_le_tsum (range _) (λ _ _, le_trans dist_nonneg (hf _)) _, exact hd.comp_injective (add_right_injective n) end lemma dist_le_tsum_of_dist_le_of_tendsto₀ [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ tsum d := by simpa only [zero_add] using dist_le_tsum_of_dist_le_of_tendsto d hf hd ha 0 lemma dist_le_tsum_dist_of_tendsto [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) (n) : dist (f n) a ≤ ∑' m, dist (f (n+m)) (f (n+m).succ) := show dist (f n) a ≤ ∑' m, (λx, dist (f x) (f x.succ)) (n + m), from dist_le_tsum_of_dist_le_of_tendsto (λ n, dist (f n) (f n.succ)) (λ _, le_refl _) h ha n lemma dist_le_tsum_dist_of_tendsto₀ [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ ∑' n, dist (f n) (f n.succ) := by simpa only [zero_add] using dist_le_tsum_dist_of_tendsto h ha 0 end cauchy_seq
866116cef95cffaa9c996fb5ca451c77078a9759
7cef822f3b952965621309e88eadf618da0c8ae9
/src/algebra/order_functions.lean
dd707546baccdd1fbe7469056f74b63d4275b760
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
12,791
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 algebra.ordered_group order.lattice open lattice universes u v variables {α : Type u} {β : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right /-- A function `f` is strictly monotone if `a < b` implies `f a < f b`. -/ def strict_mono [has_lt α] [has_lt β] (f : α → β) : Prop := ∀ ⦃a b⦄, a < b → f a < f b namespace strict_mono open ordering function section variables [linear_order α] [preorder β] {f : α → β} lemma lt_iff_lt (H : strict_mono f) {a b} : f a < f b ↔ a < b := ⟨λ h, ((lt_trichotomy b a) .resolve_left $ λ h', lt_asymm h $ H h') .resolve_left $ λ e, ne_of_gt h $ congr_arg _ e, @H _ _⟩ lemma injective (H : strict_mono f) : injective f | a b e := ((lt_trichotomy a b) .resolve_left $ λ h, ne_of_lt (H h) e) .resolve_right $ λ h, ne_of_gt (H h) e theorem compares (H : strict_mono f) {a b} : ∀ {o}, compares o (f a) (f b) ↔ compares o a b | lt := H.lt_iff_lt | eq := ⟨λ h, H.injective h, congr_arg _⟩ | gt := H.lt_iff_lt lemma le_iff_le (H : strict_mono f) {a b} : f a ≤ f b ↔ a ≤ b := ⟨λ h, le_of_not_gt $ λ h', not_le_of_lt (H h') h, λ h, (lt_or_eq_of_le h).elim (λ h', le_of_lt (H h')) (λ h', h' ▸ le_refl _)⟩ end protected lemma nat {β} [preorder β] {f : ℕ → β} (h : ∀n, f n < f (n+1)) : strict_mono f := by { intros n m hnm, induction hnm with m' hnm' ih, apply h, exact lt.trans ih (h _) } -- `preorder α` isn't strong enough: if the preorder on α is an equivalence relation, -- then `strict_mono f` is vacuously true. lemma monotone [partial_order α] [preorder β] {f : α → β} (H : strict_mono f) : monotone f := λ a b h, (lt_or_eq_of_le h).rec (le_of_lt ∘ (@H _ _)) (by rintro rfl; refl) end strict_mono section open function variables [partial_order α] [partial_order β] {f : α → β} lemma strict_mono_of_monotone_of_injective (h₁ : monotone f) (h₂ : injective f) : strict_mono f := λ a b h, begin rw lt_iff_le_and_ne at ⊢ h, exact ⟨h₁ h.1, λ e, h.2 (h₂ e)⟩ end end section variables [decidable_linear_order α] [decidable_linear_order β] {f : α → β} {a b c d : α} -- translate from lattices to linear orders (sup → max, inf → min) @[simp] lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_left_of_le : a ≤ b → a ≤ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≤ c → a ≤ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≤ c → min a b ≤ c := inf_le_left_of_le lemma min_le_right_of_le : b ≤ c → min a b ≤ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right lemma min_le_max : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b) instance max_idem : is_idempotent α max := by apply_instance -- short-circuit type class inference instance min_idem : is_idempotent α min := by apply_instance -- short-circuit type class inference @[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := have a ≤ b → (a ≤ c ∨ b ≤ c ↔ a ≤ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≤ a → (a ≤ c ∨ b ≤ c ↔ b ≤ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp * @[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := have b ≤ c → (a ≤ b ∨ a ≤ c ↔ a ≤ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≤ b → (a ≤ b ∨ a ≤ c ↔ a ≤ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp * @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(≥), le_max_iff, not_or_distrib] @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(≥), min_le_iff, not_or_distrib] @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(≥), max_le_iff, not_and_distrib] @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(≥), le_min_iff, not_and_distrib] lemma max_lt_max (h₁ : a < c) (h₂ : b < d) : max a b < max c d := by apply max_lt; simp [lt_max_iff, h₁, h₂] lemma min_lt_min (h₁ : a < c) (h₂ : b < d) : min a b < min c d := by apply lt_min; simp [min_lt_iff, h₁, h₂] theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma monotone.map_max (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma monotone.map_min (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] theorem min_choice (a b : α) : min a b = a ∨ min a b = b := by by_cases h : a ≤ b; simp [min, h] theorem max_choice (a b : α) : max a b = a ∨ max a b = b := by by_cases h : a ≤ b; simp [max, h] lemma le_of_max_le_left {a b c : α} (h : max a b ≤ c) : a ≤ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : α} (h : max a b ≤ c) : b ≤ c := le_trans (le_max_right _ _) h end lemma min_add {α : Type u} [decidable_linear_ordered_comm_group α] (a b c : α) : min a b + c = min (a + c) (b + c) := if hle : a ≤ b then have a - c ≤ b - c, from sub_le_sub hle (le_refl _), by simp * at * else have b - c ≤ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _), by simp * at * lemma min_sub {α : Type u} [decidable_linear_ordered_comm_group α] (a b c : α) : min a b - c = min (a - c) (b - c) := by simp [min_add, sub_eq_add_neg] /- Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ lemma fn_min_add_fn_max [decidable_linear_order α] [add_comm_semigroup β] (f : α → β) (n m : α) : f (min n m) + f (max n m) = f n + f m := by { cases le_total n m with h h; simp [h] } lemma min_add_max [decidable_linear_order α] [add_comm_semigroup α] (n m : α) : min n m + max n m = n + m := fn_min_add_fn_max id n m lemma fn_min_mul_fn_max [decidable_linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } lemma min_mul_max [decidable_linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m section decidable_linear_ordered_comm_group variables [decidable_linear_ordered_comm_group α] {a b c : α} attribute [simp] abs_zero abs_neg lemma abs_add (a b : α) : abs (a + b) ≤ abs a + abs b := abs_add_le_abs_add_abs a b theorem abs_le : abs a ≤ b ↔ - b ≤ a ∧ a ≤ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_le_of_le_of_neg_le h₂ $ neg_le_of_neg_le h₁⟩ lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_lt_of_lt_of_neg_lt h₂ $ neg_lt_of_neg_lt h₁⟩ lemma abs_sub_le_iff : abs (a - b) ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] lemma sub_abs_le_abs_sub (a b : α) : abs a - abs b ≤ abs (a - b) := abs_sub_abs_le_abs_sub a b lemma abs_abs_sub_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≤ b) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, λ e, e.symm ▸ abs_zero⟩ lemma abs_pos_iff {a : α} : 0 < abs a ↔ a ≠ 0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ @[simp] lemma abs_nonpos_iff {a : α} : abs a ≤ 0 ↔ a = 0 := by rw [← not_lt, abs_pos_iff, not_not] lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : abs b ≤ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) theorem abs_le_abs {α : Type*} [decidable_linear_ordered_comm_group α] {a b : α} (h₀ : a ≤ b) (h₁ : -a ≤ b) : abs a ≤ abs b := calc abs a ≤ b : by { apply abs_le_of_le_of_neg_le; assumption } ... ≤ abs b : le_abs_self _ lemma min_le_add_of_nonneg_right {a b : α} (hb : b ≥ 0) : min a b ≤ a + b := calc min a b ≤ a : by apply min_le_left ... ≤ a + b : le_add_of_nonneg_right hb lemma min_le_add_of_nonneg_left {a b : α} (ha : a ≥ 0) : min a b ≤ a + b := calc min a b ≤ b : by apply min_le_right ... ≤ a + b : le_add_of_nonneg_left ha lemma max_le_add_of_nonneg {a b : α} (ha : a ≥ 0) (hb : b ≥ 0) : max a b ≤ a + b := max_le_iff.2 (by split; simpa) lemma max_zero_sub_eq_self (a : α) : max a 0 - max (-a) 0 = a := begin rcases le_total a 0, { rw [max_eq_right h, max_eq_left, zero_sub, neg_neg], { rwa [le_neg, neg_zero] } }, { rw [max_eq_left, max_eq_right, sub_zero], { rwa [neg_le, neg_zero] }, exact h } end lemma abs_max_sub_max_le_abs (a b c : α) : abs (max a c - max b c) ≤ abs (a - b) := begin simp only [max], split_ifs, { rw [sub_self, abs_zero], exact abs_nonneg _ }, { calc abs (c - b) = - (c - b) : abs_of_neg (sub_neg_of_lt (lt_of_not_ge h_1)) ... = b - c : neg_sub _ _ ... ≤ b - a : by { rw sub_le_sub_iff_left, exact h } ... = - (a - b) : by rw neg_sub ... ≤ abs (a - b) : neg_le_abs_self _ }, { calc abs (a - c) = a - c : abs_of_pos (sub_pos_of_lt (lt_of_not_ge h)) ... ≤ a - b : by { rw sub_le_sub_iff_left, exact h_1 } ... ≤ abs (a - b) : le_abs_self _ }, { refl } end end decidable_linear_ordered_comm_group section decidable_linear_ordered_semiring variables [decidable_linear_ordered_semiring α] {a b c d : α} lemma monotone_mul_left_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma monotone_mul_right_of_nonneg (ha : 0 ≤ a) : monotone (λ x, x*a) := assume b c b_le_c, mul_le_mul_of_nonneg_right b_le_c ha lemma mul_max_of_nonneg (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_max lemma mul_min_of_nonneg (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_min lemma max_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : max a b * c = max (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_max lemma min_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : min a b * c = min (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_min end decidable_linear_ordered_semiring section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring α] {a b c d : α} @[simp] lemma abs_one : abs (1 : α) = 1 := abs_of_pos zero_lt_one lemma max_mul_mul_le_max_mul_max (b c : α) (ha : 0 ≤ a) (hd: 0 ≤ d) : max (a * b) (d * c) ≤ max a c * max d b := have ba : b * a ≤ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≤ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
f94e46d98173a46ef4180d9c7874ad7f5b7b9afd
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/analysis/normed_space/dual.lean
b1349d6ff1d12aa95746b444bc2e41ff6791cb8a
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
12,284
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Frédéric Dupuis -/ import analysis.normed_space.hahn_banach import analysis.normed_space.inner_product /-! # The topological dual of a normed space In this file we define the topological dual of a normed space, and the bounded linear map from a normed space into its double dual. We also prove that, for base field `𝕜` with `[is_R_or_C 𝕜]`, this map is an isometry. We then consider inner product spaces, with base field over `ℝ` (the corresponding results for `ℂ` will require the definition of conjugate-linear maps). We define `to_dual_map`, a continuous linear map from `E` to its dual, which maps an element `x` of the space to `λ y, ⟪x, y⟫`. We check (`to_dual_map_isometry`) that this map is an isometry onto its image, and particular is injective. We also define `to_dual'` as the function taking taking a vector to its dual for a base field `𝕜` with `[is_R_or_C 𝕜]`; this is a function and not a linear map. Finally, under the hypothesis of completeness (i.e., for Hilbert spaces), we prove the Fréchet-Riesz representation (`to_dual_map_eq_top`), which states the surjectivity: every element of the dual of a Hilbert space `E` has the form `λ u, ⟪x, u⟫` for some `x : E`. This permits the map `to_dual_map` to be upgraded to an (isometric) continuous linear equivalence, `to_dual`, between a Hilbert space and its dual. Since a lot of elementary properties don't require `eq_of_dist_eq_zero` we start setting up the theory for `semi_normed_space` and we specialize to `normed_space` when needed. ## References * [M. Einsiedler and T. Ward, *Functional Analysis, Spectral Theory, and Applications*] [EinsiedlerWard2017] ## Tags dual, Fréchet-Riesz -/ noncomputable theory open_locale classical universes u v namespace normed_space section general variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] variables (E : Type*) [semi_normed_group E] [semi_normed_space 𝕜 E] variables (F : Type*) [normed_group F] [normed_space 𝕜 F] /-- The topological dual of a seminormed space `E`. -/ @[derive [has_coe_to_fun, semi_normed_group, semi_normed_space 𝕜]] def dual := E →L[𝕜] 𝕜 instance : inhabited (dual 𝕜 E) := ⟨0⟩ instance : normed_group (dual 𝕜 F) := continuous_linear_map.to_normed_group instance : normed_space 𝕜 (dual 𝕜 F) := continuous_linear_map.to_normed_space /-- The inclusion of a normed space in its double (topological) dual. -/ def inclusion_in_double_dual' (x : E) : (dual 𝕜 (dual 𝕜 E)) := linear_map.mk_continuous { to_fun := λ f, f x, map_add' := by simp, map_smul' := by simp } ∥x∥ (λ f, by { rw mul_comm, exact f.le_op_norm x } ) @[simp] lemma dual_def (x : E) (f : dual 𝕜 E) : ((inclusion_in_double_dual' 𝕜 E) x) f = f x := rfl lemma double_dual_bound (x : E) : ∥(inclusion_in_double_dual' 𝕜 E) x∥ ≤ ∥x∥ := begin apply continuous_linear_map.op_norm_le_bound, { simp }, { intros f, rw mul_comm, exact f.le_op_norm x, } end /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual : E →L[𝕜] (dual 𝕜 (dual 𝕜 E)) := linear_map.mk_continuous { to_fun := λ (x : E), (inclusion_in_double_dual' 𝕜 E) x, map_add' := λ x y, by { ext, simp }, map_smul' := λ (c : 𝕜) x, by { ext, simp } } 1 (λ x, by { convert double_dual_bound _ _ _, simp } ) end general section bidual_isometry variables {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), ∥f x∥ ≤ M * ∥f∥) : ∥x∥ ≤ M := begin classical, by_cases h : x = 0, { simp only [h, hMp, norm_zero] }, { obtain ⟨f, hf⟩ : ∃ g : E →L[𝕜] 𝕜, _ := exists_dual_vector x h, calc ∥x∥ = ∥norm' 𝕜 x∥ : (norm_norm' _ _ _).symm ... = ∥f x∥ : by rw hf.2 ... ≤ M * ∥f∥ : hM f ... = M : by rw [hf.1, mul_one] } end /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ lemma inclusion_in_double_dual_isometry (x : E) : ∥inclusion_in_double_dual 𝕜 E x∥ = ∥x∥ := begin apply le_antisymm, { exact double_dual_bound 𝕜 E x }, { rw continuous_linear_map.norm_def, apply real.lb_le_Inf _ continuous_linear_map.bounds_nonempty, rintros c ⟨hc1, hc2⟩, exact norm_le_dual_bound x hc1 hc2 }, end end bidual_isometry end normed_space namespace inner_product_space open is_R_or_C continuous_linear_map section is_R_or_C variables (𝕜 : Type*) variables {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ /-- Given some `x` in an inner product space, we can define its dual as the continuous linear map `λ y, ⟪x, y⟫`. Consider using `to_dual` or `to_dual_map` instead in the real case. -/ def to_dual' : E →+ normed_space.dual 𝕜 E := { to_fun := λ x, linear_map.mk_continuous { to_fun := λ y, ⟪x, y⟫, map_add' := λ _ _, inner_add_right, map_smul' := λ _ _, inner_smul_right } ∥x∥ (λ y, by { rw [is_R_or_C.norm_eq_abs], exact abs_inner_le_norm _ _ }), map_zero' := by { ext z, simp }, map_add' := λ x y, by { ext z, simp [inner_add_left] } } @[simp] lemma to_dual'_apply {x y : E} : to_dual' 𝕜 x y = ⟪x, y⟫ := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] lemma norm_to_dual'_apply (x : E) : ∥to_dual' 𝕜 x∥ = ∥x∥ := begin refine le_antisymm _ _, { exact linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ }, { cases eq_or_lt_of_le (norm_nonneg x) with h h, { have : x = 0 := norm_eq_zero.mp (eq.symm h), simp [this] }, { refine (mul_le_mul_right h).mp _, calc ∥x∥ * ∥x∥ = ∥x∥ ^ 2 : by ring ... = re ⟪x, x⟫ : norm_sq_eq_inner _ ... ≤ abs ⟪x, x⟫ : re_le_abs _ ... = ∥to_dual' 𝕜 x x∥ : by simp [norm_eq_abs] ... ≤ ∥to_dual' 𝕜 x∥ * ∥x∥ : le_op_norm (to_dual' 𝕜 x) x } } end variables (E) lemma to_dual'_isometry : isometry (@to_dual' 𝕜 E _ _) := add_monoid_hom.isometry_of_norm _ (norm_to_dual'_apply 𝕜) /-- Fréchet-Riesz representation: any `ℓ` in the dual of a Hilbert space `E` is of the form `λ u, ⟪y, u⟫` for some `y : E`, i.e. `to_dual'` is surjective. -/ lemma to_dual'_surjective [complete_space E] : function.surjective (@to_dual' 𝕜 E _ _) := begin intros ℓ, set Y := ker ℓ with hY, by_cases htriv : Y = ⊤, { have hℓ : ℓ = 0, { have h' := linear_map.ker_eq_top.mp htriv, rw [←coe_zero] at h', apply coe_injective, exact h' }, exact ⟨0, by simp [hℓ]⟩ }, { have Ycomplete := is_complete_ker ℓ, rw [← submodule.orthogonal_eq_bot_iff Ycomplete, ←hY] at htriv, change Yᗮ ≠ ⊥ at htriv, rw [submodule.ne_bot_iff] at htriv, obtain ⟨z : E, hz : z ∈ Yᗮ, z_ne_0 : z ≠ 0⟩ := htriv, refine ⟨((ℓ z)† / ⟪z, z⟫) • z, _⟩, ext x, have h₁ : (ℓ z) • x - (ℓ x) • z ∈ Y, { rw [mem_ker, map_sub, map_smul, map_smul, algebra.id.smul_eq_mul, algebra.id.smul_eq_mul, mul_comm], exact sub_self (ℓ x * ℓ z) }, have h₂ : (ℓ z) * ⟪z, x⟫ = (ℓ x) * ⟪z, z⟫, { have h₃ := calc 0 = ⟪z, (ℓ z) • x - (ℓ x) • z⟫ : by { rw [(Y.mem_orthogonal' z).mp hz], exact h₁ } ... = ⟪z, (ℓ z) • x⟫ - ⟪z, (ℓ x) • z⟫ : by rw [inner_sub_right] ... = (ℓ z) * ⟪z, x⟫ - (ℓ x) * ⟪z, z⟫ : by simp [inner_smul_right], exact sub_eq_zero.mp (eq.symm h₃) }, have h₄ := calc ⟪((ℓ z)† / ⟪z, z⟫) • z, x⟫ = (ℓ z) / ⟪z, z⟫ * ⟪z, x⟫ : by simp [inner_smul_left, conj_div, conj_conj] ... = (ℓ z) * ⟪z, x⟫ / ⟪z, z⟫ : by rw [←div_mul_eq_mul_div] ... = (ℓ x) * ⟪z, z⟫ / ⟪z, z⟫ : by rw [h₂] ... = ℓ x : begin have : ⟪z, z⟫ ≠ 0, { change z = 0 → false at z_ne_0, rwa ←inner_self_eq_zero at z_ne_0 }, field_simp [this] end, exact h₄ } end end is_R_or_C section real variables {F : Type*} [inner_product_space ℝ F] /-- In a real inner product space `F`, the function that takes a vector `x` in `F` to its dual `λ y, ⟪x, y⟫` is a continuous linear map. If the space is complete (i.e. is a Hilbert space), consider using `to_dual` instead. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) def to_dual_map : F →L[ℝ] (normed_space.dual ℝ F) := linear_map.mk_continuous { to_fun := to_dual' ℝ, map_add' := λ x y, by { ext, simp [inner_add_left] }, map_smul' := λ c x, by { ext, simp [inner_smul_left] } } 1 (λ x, by simp only [norm_to_dual'_apply, one_mul, linear_map.coe_mk]) @[simp] lemma to_dual_map_apply {x y : F} : to_dual_map x y = ⟪x, y⟫_ℝ := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] lemma norm_to_dual_map_apply (x : F) : ∥to_dual_map x∥ = ∥x∥ := norm_to_dual'_apply _ _ lemma to_dual_map_isometry : isometry (@to_dual_map F _) := add_monoid_hom.isometry_of_norm _ norm_to_dual_map_apply lemma to_dual_map_injective : function.injective (@to_dual_map F _) := (@to_dual_map_isometry F _).injective @[simp] lemma ker_to_dual_map : (@to_dual_map F _).ker = ⊥ := linear_map.ker_eq_bot.mpr to_dual_map_injective @[simp] lemma to_dual_map_eq_iff_eq {x y : F} : to_dual_map x = to_dual_map y ↔ x = y := ((linear_map.ker_eq_bot).mp (@ker_to_dual_map F _)).eq_iff variables [complete_space F] /-- Fréchet-Riesz representation: any `ℓ` in the dual of a real Hilbert space `F` is of the form `λ u, ⟪y, u⟫` for some `y` in `F`. See `inner_product_space.to_dual` for the continuous linear equivalence thus induced. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) lemma range_to_dual_map : (@to_dual_map F _).range = ⊤ := linear_map.range_eq_top.mpr (to_dual'_surjective ℝ F) /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is a continuous linear equivalence. -/ def to_dual : F ≃L[ℝ] (normed_space.dual ℝ F) := continuous_linear_equiv.of_isometry to_dual_map.to_linear_map to_dual_map_isometry range_to_dual_map /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is an isometry. -/ def isometric.to_dual : F ≃ᵢ normed_space.dual ℝ F := { to_equiv := to_dual.to_linear_equiv.to_equiv, isometry_to_fun := to_dual'_isometry ℝ F} @[simp] lemma to_dual_apply {x y : F} : to_dual x y = ⟪x, y⟫_ℝ := rfl @[simp] lemma to_dual_eq_iff_eq {x y : F} : to_dual x = to_dual y ↔ x = y := (@to_dual F _ _).injective.eq_iff lemma to_dual_eq_iff_eq' {x x' : F} : (∀ y : F, ⟪x, y⟫_ℝ = ⟪x', y⟫_ℝ) ↔ x = x' := begin split, { intros h, have : to_dual x = to_dual x' → x = x' := to_dual_eq_iff_eq.mp, apply this, simp_rw [←to_dual_apply] at h, ext z, exact h z }, { rintros rfl y, refl } end @[simp] lemma norm_to_dual_apply (x : F) : ∥to_dual x∥ = ∥x∥ := norm_to_dual_map_apply x /-- In a Hilbert space, the norm of a vector in the dual space is the norm of its corresponding primal vector. -/ lemma norm_to_dual_symm_apply (ℓ : normed_space.dual ℝ F) : ∥to_dual.symm ℓ∥ = ∥ℓ∥ := begin have : ℓ = to_dual (to_dual.symm ℓ) := by simp only [continuous_linear_equiv.apply_symm_apply], conv_rhs { rw [this] }, refine eq.symm (norm_to_dual_apply _), end end real end inner_product_space
df6a15397a4ec6161b5ccb98f856cd200a507392
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/bases.lean
79046cc2004a644f889dc3060a4afec79944442b
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
38,750
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import topology.constructions import topology.continuous_on /-! # Bases of topologies. Countability axioms. A topological basis on a topological space `t` is a collection of sets, such that all open sets can be generated as unions of these sets, without the need to take finite intersections of them. This file introduces a framework for dealing with these collections, and also what more we can say under certain countability conditions on bases, which are referred to as first- and second-countable. We also briefly cover the theory of separable spaces, which are those with a countable, dense subset. If a space is second-countable, and also has a countably generated uniformity filter (for example, if `t` is a metric space), it will automatically be separable (and indeed, these conditions are equivalent in this case). ## Main definitions * `is_topological_basis s`: The topological space `t` has basis `s`. * `separable_space α`: The topological space `t` has a countable, dense subset. * `is_separable s`: The set `s` is contained in the closure of a countable set. * `first_countable_topology α`: A topology in which `𝓝 x` is countably generated for every `x`. * `second_countable_topology α`: A topology which has a topological basis which is countable. ## Main results * `first_countable_topology.tendsto_subseq`: In a first-countable space, cluster points are limits of subsequences. * `second_countable_topology.is_open_Union_countable`: In a second-countable space, the union of arbitrarily-many open sets is equal to a sub-union of only countably many of these sets. * `second_countable_topology.countable_cover_nhds`: Consider `f : α → set α` with the property that `f x ∈ 𝓝 x` for all `x`. Then there is some countable set `s` whose image covers the space. ## Implementation Notes For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. ### TODO: More fine grained instances for `first_countable_topology`, `separable_space`, `t2_space`, and more (see the comment below `subtype.second_countable_topology`.) -/ open set filter function open_locale topological_space filter noncomputable theory namespace topological_space universe u variables {α : Type u} [t : topological_space α] include t /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ structure is_topological_basis (s : set (set α)) : Prop := (exists_subset_inter : ∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) (sUnion_eq : (⋃₀ s) = univ) (eq_generate_from : t = generate_from s) /-- If a family of sets `s` generates the topology, then nonempty intersections of finite subcollections of `s` form a topological basis. -/ lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((λ f, ⋂₀ f) '' {f : set (set α) | f.finite ∧ f ⊆ s ∧ (⋂₀ f).nonempty}) := begin refine ⟨_, _, _⟩, { rintro _ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, rfl⟩ _ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, rfl⟩ x h, have : ⋂₀ (t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂ := sInter_union t₁ t₂, exact ⟨_, ⟨t₁ ∪ t₂, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b, this.symm ▸ ⟨x, h⟩⟩, this⟩, h, subset.rfl⟩ }, { rw [sUnion_image, Union₂_eq_univ_iff], intro x, have : x ∈ ⋂₀ ∅, { rw sInter_empty, exact mem_univ x }, exact ⟨∅, ⟨finite_empty, empty_subset _, x, this⟩, this⟩ }, { rw hs, apply le_antisymm; apply le_generate_from, { rintro _ ⟨t, ⟨hft, htb, ht⟩, rfl⟩, exact @is_open_sInter _ (generate_from s) _ hft (λ s hs, generate_open.basic _ $ htb hs) }, { intros t ht, rcases t.eq_empty_or_nonempty with rfl|hne, { apply @is_open_empty _ _ }, rw ← sInter_singleton t at hne ⊢, exact generate_open.basic _ ⟨{t}, ⟨finite_singleton t, singleton_subset_iff.2 ht, hne⟩, rfl⟩ } } end /-- If a family of open sets `s` is such that every open neighbourhood contains some member of `s`, then `s` is a topological basis. -/ lemma is_topological_basis_of_open_of_nhds {s : set (set α)} (h_open : ∀ u ∈ s, is_open u) (h_nhds : ∀(a:α) (u : set α), a ∈ u → is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) : is_topological_basis s := begin refine ⟨λ t₁ ht₁ t₂ ht₂ x hx, h_nhds _ _ hx (is_open.inter (h_open _ ht₁) (h_open _ ht₂)), _, _⟩, { refine sUnion_eq_univ_iff.2 (λ a, _), rcases h_nhds a univ trivial is_open_univ with ⟨u, h₁, h₂, -⟩, exact ⟨u, h₁, h₂⟩ }, { refine (le_generate_from h_open).antisymm (λ u hu, _), refine (@is_open_iff_nhds α (generate_from s) u).mpr (λ a ha, _), rcases h_nhds a u ha hu with ⟨v, hvs, hav, hvu⟩, rw nhds_generate_from, exact infi₂_le_of_le v ⟨hav, hvs⟩ (le_principal_iff.2 hvu) } end /-- A set `s` is in the neighbourhood of `a` iff there is some basis set `t`, which contains `a` and is itself contained in `s`. -/ lemma is_topological_basis.mem_nhds_iff {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ 𝓝 a ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s := begin change s ∈ (𝓝 a).sets ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s, rw [hb.eq_generate_from, nhds_generate_from, binfi_sets_eq], { simp [and_assoc, and.left_comm] }, { exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩, have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩, let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in ⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)), le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ }, { rcases eq_univ_iff_forall.1 hb.sUnion_eq a with ⟨i, h1, h2⟩, exact ⟨i, h2, h1⟩ } end lemma is_topological_basis.is_open_iff {s : set α} {b : set (set α)} (hb : is_topological_basis b) : is_open s ↔ ∀ a ∈ s, ∃ t ∈ b, a ∈ t ∧ t ⊆ s := by simp [is_open_iff_mem_nhds, hb.mem_nhds_iff] lemma is_topological_basis.nhds_has_basis {b : set (set α)} (hb : is_topological_basis b) {a : α} : (𝓝 a).has_basis (λ t : set α, t ∈ b ∧ a ∈ t) (λ t, t) := ⟨λ s, hb.mem_nhds_iff.trans $ by simp only [exists_prop, and_assoc]⟩ protected lemma is_topological_basis.is_open {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : is_open s := by { rw hb.eq_generate_from, exact generate_open.basic s hs } protected lemma is_topological_basis.mem_nhds {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) (ha : a ∈ s) : s ∈ 𝓝 a := (hb.is_open hs).mem_nhds ha lemma is_topological_basis.exists_subset_of_mem_open {b : set (set α)} (hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u) (ou : is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u := hb.mem_nhds_iff.1 $ is_open.mem_nhds ou au /-- Any open set is the union of the basis sets contained in it. -/ lemma is_topological_basis.open_eq_sUnion' {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : u = ⋃₀ {s ∈ B | s ⊆ u} := ext $ λ a, ⟨λ ha, let ⟨b, hb, ab, bu⟩ := hB.exists_subset_of_mem_open ha ou in ⟨b, ⟨hb, bu⟩, ab⟩, λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩ lemma is_topological_basis.open_eq_sUnion {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ S ⊆ B, u = ⋃₀ S := ⟨{s ∈ B | s ⊆ u}, λ s h, h.1, hB.open_eq_sUnion' ou⟩ lemma is_topological_basis.open_eq_Union {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B := ⟨↥{s ∈ B | s ⊆ u}, coe, by { rw ← sUnion_eq_Union, apply hB.open_eq_sUnion' ou }, λ s, and.left s.2⟩ /-- A point `a` is in the closure of `s` iff all basis sets containing `a` intersect `s`. -/ lemma is_topological_basis.mem_closure_iff {b : set (set α)} (hb : is_topological_basis b) {s : set α} {a : α} : a ∈ closure s ↔ ∀ o ∈ b, a ∈ o → (o ∩ s).nonempty := (mem_closure_iff_nhds_basis' hb.nhds_has_basis).trans $ by simp only [and_imp] /-- A set is dense iff it has non-trivial intersection with all basis sets. -/ lemma is_topological_basis.dense_iff {b : set (set α)} (hb : is_topological_basis b) {s : set α} : dense s ↔ ∀ o ∈ b, set.nonempty o → (o ∩ s).nonempty := begin simp only [dense, hb.mem_closure_iff], exact ⟨λ h o hb ⟨a, ha⟩, h a o hb ha, λ h a o hb ha, h o hb ⟨a, ha⟩⟩ end lemma is_topological_basis.is_open_map_iff {β} [topological_space β] {B : set (set α)} (hB : is_topological_basis B) {f : α → β} : is_open_map f ↔ ∀ s ∈ B, is_open (f '' s) := begin refine ⟨λ H o ho, H _ (hB.is_open ho), λ hf o ho, _⟩, rw [hB.open_eq_sUnion' ho, sUnion_eq_Union, image_Union], exact is_open_Union (λ s, hf s s.2.1) end lemma is_topological_basis.exists_nonempty_subset {B : set (set α)} (hb : is_topological_basis B) {u : set α} (hu : u.nonempty) (ou : is_open u) : ∃ v ∈ B, set.nonempty v ∧ v ⊆ u := begin cases hu with x hx, rw [hb.open_eq_sUnion' ou, mem_sUnion] at hx, rcases hx with ⟨v, hv, hxv⟩, exact ⟨v, hv.1, ⟨x, hxv⟩, hv.2⟩ end lemma is_topological_basis_opens : is_topological_basis { U : set α | is_open U } := is_topological_basis_of_open_of_nhds (by tauto) (by tauto) protected lemma is_topological_basis.prod {β} [topological_space β] {B₁ : set (set α)} {B₂ : set (set β)} (h₁ : is_topological_basis B₁) (h₂ : is_topological_basis B₂) : is_topological_basis (image2 (×ˢ) B₁ B₂) := begin refine is_topological_basis_of_open_of_nhds _ _, { rintro _ ⟨u₁, u₂, hu₁, hu₂, rfl⟩, exact (h₁.is_open hu₁).prod (h₂.is_open hu₂) }, { rintro ⟨a, b⟩ u hu uo, rcases (h₁.nhds_has_basis.prod_nhds h₂.nhds_has_basis).mem_iff.1 (is_open.mem_nhds uo hu) with ⟨⟨s, t⟩, ⟨⟨hs, ha⟩, ht, hb⟩, hu⟩, exact ⟨s ×ˢ t, mem_image2_of_mem hs ht, ⟨ha, hb⟩, hu⟩ } end protected lemma is_topological_basis.inducing {β} [topological_space β] {f : α → β} {T : set (set β)} (hf : inducing f) (h : is_topological_basis T) : is_topological_basis (image (preimage f) T) := begin refine is_topological_basis_of_open_of_nhds _ _, { rintros _ ⟨V, hV, rfl⟩, rwa hf.is_open_iff, refine ⟨V, h.is_open hV, rfl⟩ }, { intros a U ha hU, rw hf.is_open_iff at hU, obtain ⟨V, hV, rfl⟩ := hU, obtain ⟨S, hS, rfl⟩ := h.open_eq_sUnion hV, obtain ⟨W, hW, ha⟩ := ha, refine ⟨f ⁻¹' W, ⟨_, hS hW, rfl⟩, ha, set.preimage_mono $ set.subset_sUnion_of_mem hW⟩ } end lemma is_topological_basis_of_cover {ι} {U : ι → set α} (Uo : ∀ i, is_open (U i)) (Uc : (⋃ i, U i) = univ) {b : Π i, set (set (U i))} (hb : ∀ i, is_topological_basis (b i)) : is_topological_basis (⋃ i : ι, image (coe : U i → α) '' (b i)) := begin refine is_topological_basis_of_open_of_nhds (λ u hu, _) _, { simp only [mem_Union, mem_image] at hu, rcases hu with ⟨i, s, sb, rfl⟩, exact (Uo i).is_open_map_subtype_coe _ ((hb i).is_open sb) }, { intros a u ha uo, rcases Union_eq_univ_iff.1 Uc a with ⟨i, hi⟩, lift a to ↥(U i) using hi, rcases (hb i).exists_subset_of_mem_open (by exact ha) (uo.preimage continuous_subtype_coe) with ⟨v, hvb, hav, hvu⟩, exact ⟨coe '' v, mem_Union.2 ⟨i, mem_image_of_mem _ hvb⟩, mem_image_of_mem _ hav, image_subset_iff.2 hvu⟩ } end protected lemma is_topological_basis.continuous {β : Type*} [topological_space β] {B : set (set β)} (hB : is_topological_basis B) (f : α → β) (hf : ∀ s ∈ B, is_open (f ⁻¹' s)) : continuous f := begin rw hB.eq_generate_from, exact continuous_generated_from hf end variables (α) /-- A separable space is one with a countable dense subset, available through `topological_space.exists_countable_dense`. If `α` is also known to be nonempty, then `topological_space.dense_seq` provides a sequence `ℕ → α` with dense range, see `topological_space.dense_range_dense_seq`. If `α` is a uniform space with countably generated uniformity filter (e.g., an `emetric_space`), then this condition is equivalent to `topological_space.second_countable_topology α`. In this case the latter should be used as a typeclass argument in theorems because Lean can automatically deduce `separable_space` from `second_countable_topology` but it can't deduce `second_countable_topology` and `emetric_space`. -/ class separable_space : Prop := (exists_countable_dense : ∃s:set α, s.countable ∧ dense s) lemma exists_countable_dense [separable_space α] : ∃ s : set α, s.countable ∧ dense s := separable_space.exists_countable_dense /-- A nonempty separable space admits a sequence with dense range. Instead of running `cases` on the conclusion of this lemma, you might want to use `topological_space.dense_seq` and `topological_space.dense_range_dense_seq`. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ lemma exists_dense_seq [separable_space α] [nonempty α] : ∃ u : ℕ → α, dense_range u := begin obtain ⟨s : set α, hs, s_dense⟩ := exists_countable_dense α, cases set.countable_iff_exists_surjective.mp hs with u hu, exact ⟨u, s_dense.mono hu⟩, end /-- A dense sequence in a non-empty separable topological space. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ def dense_seq [separable_space α] [nonempty α] : ℕ → α := classical.some (exists_dense_seq α) /-- The sequence `dense_seq α` has dense range. -/ @[simp] lemma dense_range_dense_seq [separable_space α] [nonempty α] : dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α) variable {α} @[priority 100] instance encodable.to_separable_space [encodable α] : separable_space α := { exists_countable_dense := ⟨set.univ, set.countable_encodable set.univ, dense_univ⟩ } lemma separable_space_of_dense_range {ι : Type*} [encodable ι] (u : ι → α) (hu : dense_range u) : separable_space α := ⟨⟨range u, countable_range u, hu⟩⟩ /-- In a separable space, a family of nonempty disjoint open sets is countable. -/ lemma _root_.set.pairwise_disjoint.countable_of_is_open [separable_space α] {ι : Type*} {s : ι → set α} {a : set ι} (h : a.pairwise_disjoint s) (ha : ∀ i ∈ a, is_open (s i)) (h'a : ∀ i ∈ a, (s i).nonempty) : a.countable := begin rcases exists_countable_dense α with ⟨u, ⟨u_encodable⟩, u_dense⟩, have : ∀ i : a, ∃ y, y ∈ s i ∩ u := λ i, dense_iff_inter_open.1 u_dense (s i) (ha i i.2) (h'a i i.2), choose f hfs hfu using this, lift f to a → u using hfu, have f_inj : injective f, { refine injective_iff_pairwise_ne.mpr ((h.subtype _ _).mono $ λ i j hij hfij, hij ⟨hfs i, _⟩), simp only [congr_arg coe hfij, hfs j] }, exact ⟨@encodable.of_inj _ _ u_encodable f f_inj⟩ end /-- In a separable space, a family of disjoint sets with nonempty interiors is countable. -/ lemma _root_.set.pairwise_disjoint.countable_of_nonempty_interior [separable_space α] {ι : Type*} {s : ι → set α} {a : set ι} (h : a.pairwise_disjoint s) (ha : ∀ i ∈ a, (interior (s i)).nonempty) : a.countable := (h.mono $ λ i, interior_subset).countable_of_is_open (λ i hi, is_open_interior) ha /-- A set `s` in a topological space is separable if it is contained in the closure of a countable set `c`. Beware that this definition does not require that `c` is contained in `s` (to express the latter, use `separable_space s` or `is_separable (univ : set s))`. In metric spaces, the two definitions are equivalent, see `topological_space.is_separable.separable_space`. -/ def is_separable (s : set α) := ∃ c : set α, c.countable ∧ s ⊆ closure c lemma is_separable.mono {s u : set α} (hs : is_separable s) (hu : u ⊆ s) : is_separable u := begin rcases hs with ⟨c, c_count, hs⟩, exact ⟨c, c_count, hu.trans hs⟩ end lemma is_separable.union {s u : set α} (hs : is_separable s) (hu : is_separable u) : is_separable (s ∪ u) := begin rcases hs with ⟨cs, cs_count, hcs⟩, rcases hu with ⟨cu, cu_count, hcu⟩, refine ⟨cs ∪ cu, cs_count.union cu_count, _⟩, exact union_subset (hcs.trans (closure_mono (subset_union_left _ _))) (hcu.trans (closure_mono (subset_union_right _ _))) end lemma is_separable.closure {s : set α} (hs : is_separable s) : is_separable (closure s) := begin rcases hs with ⟨c, c_count, hs⟩, exact ⟨c, c_count, by simpa using closure_mono hs⟩, end lemma is_separable_Union {ι : Type*} [encodable ι] {s : ι → set α} (hs : ∀ i, is_separable (s i)) : is_separable (⋃ i, s i) := begin choose c hc h'c using hs, refine ⟨⋃ i, c i, countable_Union hc, Union_subset_iff.2 (λ i, _)⟩, exact (h'c i).trans (closure_mono (subset_Union _ i)) end lemma _root_.set.countable.is_separable {s : set α} (hs : s.countable) : is_separable s := ⟨s, hs, subset_closure⟩ lemma _root_.set.finite.is_separable {s : set α} (hs : s.finite) : is_separable s := hs.countable.is_separable lemma is_separable_univ_iff : is_separable (univ : set α) ↔ separable_space α := begin split, { rintros ⟨c, c_count, hc⟩, refine ⟨⟨c, c_count, by rwa [dense_iff_closure_eq, ← univ_subset_iff]⟩⟩ }, { introsI h, rcases exists_countable_dense α with ⟨c, c_count, hc⟩, exact ⟨c, c_count, by rwa [univ_subset_iff, ← dense_iff_closure_eq]⟩ } end lemma is_separable_of_separable_space [h : separable_space α] (s : set α) : is_separable s := is_separable.mono (is_separable_univ_iff.2 h) (subset_univ _) lemma is_separable.image {β : Type*} [topological_space β] {s : set α} (hs : is_separable s) {f : α → β} (hf : continuous f) : is_separable (f '' s) := begin rcases hs with ⟨c, c_count, hc⟩, refine ⟨f '' c, c_count.image _, _⟩, rw image_subset_iff, exact hc.trans (closure_subset_preimage_closure_image hf) end lemma is_separable_of_separable_space_subtype (s : set α) [separable_space s] : is_separable s := begin have : is_separable ((coe : s → α) '' (univ : set s)) := (is_separable_of_separable_space _).image continuous_subtype_coe, simpa only [image_univ, subtype.range_coe_subtype], end end topological_space open topological_space lemma is_topological_basis_pi {ι : Type*} {X : ι → Type*} [∀ i, topological_space (X i)] {T : Π i, set (set (X i))} (cond : ∀ i, is_topological_basis (T i)) : is_topological_basis {S : set (Π i, X i) | ∃ (U : Π i, set (X i)) (F : finset ι), (∀ i, i ∈ F → (U i) ∈ T i) ∧ S = (F : set ι).pi U } := begin refine is_topological_basis_of_open_of_nhds _ _, { rintro _ ⟨U, F, h1, rfl⟩, apply is_open_set_pi F.finite_to_set, intros i hi, exact (cond i).is_open (h1 i hi) }, { intros a U ha hU, obtain ⟨I, t, hta, htU⟩ : ∃ (I : finset ι) (t : Π (i : ι), set (X i)), (∀ i, t i ∈ 𝓝 (a i)) ∧ set.pi ↑I t ⊆ U, { rw [← filter.mem_pi', ← nhds_pi], exact hU.mem_nhds ha }, have : ∀ i, ∃ V ∈ T i, a i ∈ V ∧ V ⊆ t i := λ i, (cond i).mem_nhds_iff.1 (hta i), choose V hVT haV hVt, exact ⟨_, ⟨V, I, λ i hi, hVT i, rfl⟩, λ i hi, haV i, (pi_mono $ λ i hi, hVt i).trans htU⟩ }, end lemma is_topological_basis_infi {β : Type*} {ι : Type*} {X : ι → Type*} [t : ∀ i, topological_space (X i)] {T : Π i, set (set (X i))} (cond : ∀ i, is_topological_basis (T i)) (f : Π i, β → X i) : @is_topological_basis β (⨅ i, induced (f i) (t i)) { S | ∃ (U : Π i, set (X i)) (F : finset ι), (∀ i, i ∈ F → U i ∈ T i) ∧ S = ⋂ i (hi : i ∈ F), (f i) ⁻¹' (U i) } := begin convert (is_topological_basis_pi cond).inducing (inducing_infi_to_pi _), ext V, split, { rintros ⟨U, F, h1, h2⟩, have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F), (λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp }, refine ⟨(F : set ι).pi U, ⟨U, F, h1, rfl⟩, _⟩, rw [this, h2, set.preimage_Inter], congr' 1, ext1, rw set.preimage_Inter, refl }, { rintros ⟨U, ⟨U, F, h1, rfl⟩, h⟩, refine ⟨U, F, h1, _⟩, have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F), (λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp }, rw [← h, this, set.preimage_Inter], congr' 1, ext1, rw set.preimage_Inter, refl } end lemma is_topological_basis_singletons (α : Type*) [topological_space α] [discrete_topology α] : is_topological_basis {s | ∃ (x : α), (s : set α) = {x}} := is_topological_basis_of_open_of_nhds (λ u hu, is_open_discrete _) $ λ x u hx u_open, ⟨{x}, ⟨x, rfl⟩, mem_singleton x, singleton_subset_iff.2 hx⟩ /-- If `α` is a separable space and `f : α → β` is a continuous map with dense range, then `β` is a separable space as well. E.g., the completion of a separable uniform space is separable. -/ protected lemma dense_range.separable_space {α β : Type*} [topological_space α] [separable_space α] [topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) : separable_space β := let ⟨s, s_cnt, s_dense⟩ := exists_countable_dense α in ⟨⟨f '' s, countable.image s_cnt f, h.dense_image h' s_dense⟩⟩ lemma dense.exists_countable_dense_subset {α : Type*} [topological_space α] {s : set α} [separable_space s] (hs : dense s) : ∃ t ⊆ s, t.countable ∧ dense t := let ⟨t, htc, htd⟩ := exists_countable_dense s in ⟨coe '' t, image_subset_iff.2 $ λ x _, mem_preimage.2 $ subtype.coe_prop _, htc.image coe, hs.dense_range_coe.dense_image continuous_subtype_val htd⟩ /-- Let `s` be a dense set in a topological space `α` with partial order structure. If `s` is a separable space (e.g., if `α` has a second countable topology), then there exists a countable dense subset `t ⊆ s` such that `t` contains bottom/top element of `α` when they exist and belong to `s`. For a dense subset containing neither bot nor top elements, see `dense.exists_countable_dense_subset_no_bot_top`. -/ lemma dense.exists_countable_dense_subset_bot_top {α : Type*} [topological_space α] [partial_order α] {s : set α} [separable_space s] (hs : dense s) : ∃ t ⊆ s, t.countable ∧ dense t ∧ (∀ x, is_bot x → x ∈ s → x ∈ t) ∧ (∀ x, is_top x → x ∈ s → x ∈ t) := begin rcases hs.exists_countable_dense_subset with ⟨t, hts, htc, htd⟩, refine ⟨(t ∪ ({x | is_bot x} ∪ {x | is_top x})) ∩ s, _, _, _, _, _⟩, exacts [inter_subset_right _ _, (htc.union ((countable_is_bot α).union (countable_is_top α))).mono (inter_subset_left _ _), htd.mono (subset_inter (subset_union_left _ _) hts), λ x hx hxs, ⟨or.inr $ or.inl hx, hxs⟩, λ x hx hxs, ⟨or.inr $ or.inr hx, hxs⟩] end instance separable_space_univ {α : Type*} [topological_space α] [separable_space α] : separable_space (univ : set α) := (equiv.set.univ α).symm.surjective.dense_range.separable_space (continuous_subtype_mk _ continuous_id) /-- If `α` is a separable topological space with a partial order, then there exists a countable dense set `s : set α` that contains those of both bottom and top elements of `α` that actually exist. For a dense set containing neither bot nor top elements, see `exists_countable_dense_no_bot_top`. -/ lemma exists_countable_dense_bot_top (α : Type*) [topological_space α] [separable_space α] [partial_order α] : ∃ s : set α, s.countable ∧ dense s ∧ (∀ x, is_bot x → x ∈ s) ∧ (∀ x, is_top x → x ∈ s) := by simpa using dense_univ.exists_countable_dense_subset_bot_top namespace topological_space universe u variables (α : Type u) [t : topological_space α] include t /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology : Prop := (nhds_generated_countable : ∀a:α, (𝓝 a).is_countably_generated) attribute [instance] first_countable_topology.nhds_generated_countable namespace first_countable_topology variable {α} /-- In a first-countable space, a cluster point `x` of a sequence is the limit of some subsequence. -/ lemma tendsto_subseq [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x at_top u) : ∃ (ψ : ℕ → ℕ), (strict_mono ψ) ∧ (tendsto (u ∘ ψ) at_top (𝓝 x)) := subseq_tendsto_of_ne_bot hx end first_countable_topology variables {α} instance is_countably_generated_nhds_within (x : α) [is_countably_generated (𝓝 x)] (s : set α) : is_countably_generated (𝓝[s] x) := inf.is_countably_generated _ _ variable (α) /-- A second-countable space is one with a countable basis. -/ class second_countable_topology : Prop := (is_open_generated_countable [] : ∃ b : set (set α), b.countable ∧ t = topological_space.generate_from b) variable {α} protected lemma is_topological_basis.second_countable_topology {b : set (set α)} (hb : is_topological_basis b) (hc : b.countable) : second_countable_topology α := ⟨⟨b, hc, hb.eq_generate_from⟩⟩ variable (α) lemma exists_countable_basis [second_countable_topology α] : ∃b:set (set α), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b := let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in let b' := (λs, ⋂₀ s) '' {s:set (set α) | s.finite ∧ s ⊆ b ∧ (⋂₀ s).nonempty} in ⟨b', ((countable_set_of_finite_subset hb₁).mono (by { simp only [← and_assoc], apply inter_subset_left })).image _, assume ⟨s, ⟨_, _, hn⟩, hp⟩, absurd hn (not_nonempty_iff_eq_empty.2 hp), is_topological_basis_of_subbasis hb₂⟩ /-- A countable topological basis of `α`. -/ def countable_basis [second_countable_topology α] : set (set α) := (exists_countable_basis α).some lemma countable_countable_basis [second_countable_topology α] : (countable_basis α).countable := (exists_countable_basis α).some_spec.1 instance encodable_countable_basis [second_countable_topology α] : encodable (countable_basis α) := (countable_countable_basis α).to_encodable lemma empty_nmem_countable_basis [second_countable_topology α] : ∅ ∉ countable_basis α := (exists_countable_basis α).some_spec.2.1 lemma is_basis_countable_basis [second_countable_topology α] : is_topological_basis (countable_basis α) := (exists_countable_basis α).some_spec.2.2 lemma eq_generate_from_countable_basis [second_countable_topology α] : ‹topological_space α› = generate_from (countable_basis α) := (is_basis_countable_basis α).eq_generate_from variable {α} lemma is_open_of_mem_countable_basis [second_countable_topology α] {s : set α} (hs : s ∈ countable_basis α) : is_open s := (is_basis_countable_basis α).is_open hs lemma nonempty_of_mem_countable_basis [second_countable_topology α] {s : set α} (hs : s ∈ countable_basis α) : s.nonempty := ne_empty_iff_nonempty.1 $ ne_of_mem_of_not_mem hs $ empty_nmem_countable_basis α variable (α) @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_first_countable_topology [second_countable_topology α] : first_countable_topology α := ⟨λ x, has_countable_basis.is_countably_generated $ ⟨(is_basis_countable_basis α).nhds_has_basis, (countable_countable_basis α).mono $ inter_subset_left _ _⟩⟩ /-- If `β` is a second-countable space, then its induced topology via `f` on `α` is also second-countable. -/ lemma second_countable_topology_induced (β) [t : topological_space β] [second_countable_topology β] (f : α → β) : @second_countable_topology α (t.induced f) := begin rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩, refine { is_open_generated_countable := ⟨preimage f '' b, hb.image _, _⟩ }, rw [eq, induced_generate_from_eq] end instance subtype.second_countable_topology (s : set α) [second_countable_topology α] : second_countable_topology s := second_countable_topology_induced s α coe /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance {β : Type*} [topological_space β] [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ((is_basis_countable_basis α).prod (is_basis_countable_basis β)).second_countable_topology $ (countable_countable_basis α).image2 (countable_countable_basis β) _ instance second_countable_topology_encodable {ι : Type*} {π : ι → Type*} [encodable ι] [t : ∀a, topological_space (π a)] [∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := begin have : t = (λa, generate_from (countable_basis (π a))), from funext (assume a, (is_basis_countable_basis (π a)).eq_generate_from), rw [this, pi_generate_from_eq], constructor, refine ⟨_, _, rfl⟩, have : set.countable {T : set (Π i, π i) | ∃ (I : finset ι) (s : Π i : I, set (π i)), (∀ i, s i ∈ countable_basis (π i)) ∧ T = {f | ∀ i : I, f i ∈ s i}}, { simp only [set_of_exists, ← exists_prop], refine countable_Union (λ I, countable.bUnion _ (λ _ _, countable_singleton _)), change set.countable {s : Π i : I, set (π i) | ∀ i, s i ∈ countable_basis (π i)}, exact countable_pi (λ i, countable_countable_basis _) }, convert this using 1, ext1 T, split, { rintro ⟨s, I, hs, rfl⟩, refine ⟨I, λ i, s i, λ i, hs i i.2, _⟩, simp only [set.pi, set_coe.forall'], refl }, { rintro ⟨I, s, hs, rfl⟩, rcases @subtype.surjective_restrict ι (λ i, set (π i)) _ (λ i, i ∈ I) s with ⟨s, rfl⟩, exact ⟨s, I, λ i hi, hs ⟨i, hi⟩, set.ext $ λ f, subtype.forall⟩ } end instance second_countable_topology_fintype {ι : Type*} {π : ι → Type*} [fintype ι] [t : ∀a, topological_space (π a)] [∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := by { letI := fintype.to_encodable ι, exact topological_space.second_countable_topology_encodable } @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_separable_space [second_countable_topology α] : separable_space α := begin choose p hp using λ s : countable_basis α, nonempty_of_mem_countable_basis s.2, exact ⟨⟨range p, countable_range _, (is_basis_countable_basis α).dense_iff.2 $ λ o ho _, ⟨p ⟨o, ho⟩, hp _, mem_range_self _⟩⟩⟩ end variables {α} /-- A countable open cover induces a second-countable topology if all open covers are themselves second countable. -/ lemma second_countable_topology_of_countable_cover {ι} [encodable ι] {U : ι → set α} [∀ i, second_countable_topology (U i)] (Uo : ∀ i, is_open (U i)) (hc : (⋃ i, U i) = univ) : second_countable_topology α := begin have : is_topological_basis (⋃ i, image (coe : U i → α) '' (countable_basis (U i))), from is_topological_basis_of_cover Uo hc (λ i, is_basis_countable_basis (U i)), exact this.second_countable_topology (countable_Union $ λ i, (countable_countable_basis _).image _) end /-- In a second-countable space, an open set, given as a union of open sets, is equal to the union of countably many of those sets. -/ lemma is_open_Union_countable [second_countable_topology α] {ι} (s : ι → set α) (H : ∀ i, is_open (s i)) : ∃ T : set ι, T.countable ∧ (⋃ i ∈ T, s i) = ⋃ i, s i := begin let B := {b ∈ countable_basis α | ∃ i, b ⊆ s i}, choose f hf using λ b : B, b.2.2, haveI : encodable B := ((countable_countable_basis α).mono (sep_subset _ _)).to_encodable, refine ⟨_, countable_range f, (Union₂_subset_Union _ _).antisymm (sUnion_subset _)⟩, rintro _ ⟨i, rfl⟩ x xs, rcases (is_basis_countable_basis α).exists_subset_of_mem_open xs (H _) with ⟨b, hb, xb, bs⟩, exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩ end lemma is_open_sUnion_countable [second_countable_topology α] (S : set (set α)) (H : ∀ s ∈ S, is_open s) : ∃ T : set (set α), T.countable ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S := let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in ⟨subtype.val '' T, cT.image _, image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs, by rwa [sUnion_image, sUnion_eq_Union]⟩ /-- In a topological space with second countable topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ lemma countable_cover_nhds [second_countable_topology α] {f : α → set α} (hf : ∀ x, f x ∈ 𝓝 x) : ∃ s : set α, s.countable ∧ (⋃ x ∈ s, f x) = univ := begin rcases is_open_Union_countable (λ x, interior (f x)) (λ x, is_open_interior) with ⟨s, hsc, hsU⟩, suffices : (⋃ x ∈ s, interior (f x)) = univ, from ⟨s, hsc, flip eq_univ_of_subset this $ Union₂_mono $ λ _ _, interior_subset⟩, simp only [hsU, eq_univ_iff_forall, mem_Union], exact λ x, ⟨x, mem_interior_iff_mem_nhds.2 (hf x)⟩ end lemma countable_cover_nhds_within [second_countable_topology α] {f : α → set α} {s : set α} (hf : ∀ x ∈ s, f x ∈ 𝓝[s] x) : ∃ t ⊆ s, t.countable ∧ s ⊆ (⋃ x ∈ t, f x) := begin have : ∀ x : s, coe ⁻¹' (f x) ∈ 𝓝 x, from λ x, preimage_coe_mem_nhds_subtype.2 (hf x x.2), rcases countable_cover_nhds this with ⟨t, htc, htU⟩, refine ⟨coe '' t, subtype.coe_image_subset _ _, htc.image _, λ x hx, _⟩, simp only [bUnion_image, eq_univ_iff_forall, ← preimage_Union, mem_preimage] at htU ⊢, exact htU ⟨x, hx⟩ end section sigma variables {ι : Type*} {E : ι → Type*} [∀ i, topological_space (E i)] omit t /-- In a disjoint union space `Σ i, E i`, one can form a topological basis by taking the union of topological bases on each of the parts of the space. -/ lemma is_topological_basis.sigma {s : Π (i : ι), set (set (E i))} (hs : ∀ i, is_topological_basis (s i)) : is_topological_basis (⋃ (i : ι), (λ u, ((sigma.mk i) '' u : set (Σ i, E i))) '' (s i)) := begin apply is_topological_basis_of_open_of_nhds, { assume u hu, obtain ⟨i, t, ts, rfl⟩ : ∃ (i : ι) (t : set (E i)), t ∈ s i ∧ sigma.mk i '' t = u, by simpa only [mem_Union, mem_image] using hu, exact is_open_map_sigma_mk _ ((hs i).is_open ts) }, { rintros ⟨i, x⟩ u hxu u_open, have hx : x ∈ sigma.mk i ⁻¹' u := hxu, obtain ⟨v, vs, xv, hv⟩ : ∃ (v : set (E i)) (H : v ∈ s i), x ∈ v ∧ v ⊆ sigma.mk i ⁻¹' u := (hs i).exists_subset_of_mem_open hx (is_open_sigma_iff.1 u_open i), exact ⟨(sigma.mk i) '' v, mem_Union.2 ⟨i, mem_image_of_mem _ vs⟩, mem_image_of_mem _ xv, image_subset_iff.2 hv⟩ } end /-- A countable disjoint union of second countable spaces is second countable. -/ instance [encodable ι] [∀ i, second_countable_topology (E i)] : second_countable_topology (Σ i, E i) := begin let b := (⋃ (i : ι), (λ u, ((sigma.mk i) '' u : set (Σ i, E i))) '' (countable_basis (E i))), have A : is_topological_basis b := is_topological_basis.sigma (λ i, is_basis_countable_basis _), have B : b.countable := countable_Union (λ i, countable.image (countable_countable_basis _) _), exact A.second_countable_topology B, end end sigma section sum omit t variables {β : Type*} [topological_space α] [topological_space β] /-- In a sum space `α ⊕ β`, one can form a topological basis by taking the union of topological bases on each of the two components. -/ lemma is_topological_basis.sum {s : set (set α)} (hs : is_topological_basis s) {t : set (set β)} (ht : is_topological_basis t) : is_topological_basis (((λ u, sum.inl '' u) '' s) ∪ ((λ u, sum.inr '' u) '' t)) := begin apply is_topological_basis_of_open_of_nhds, { assume u hu, cases hu, { rcases hu with ⟨w, hw, rfl⟩, exact open_embedding_inl.is_open_map w (hs.is_open hw) }, { rcases hu with ⟨w, hw, rfl⟩, exact open_embedding_inr.is_open_map w (ht.is_open hw) } }, { rintros x u hxu u_open, cases x, { have h'x : x ∈ sum.inl ⁻¹' u := hxu, obtain ⟨v, vs, xv, vu⟩ : ∃ (v : set α) (H : v ∈ s), x ∈ v ∧ v ⊆ sum.inl ⁻¹' u := hs.exists_subset_of_mem_open h'x (is_open_sum_iff.1 u_open).1, exact ⟨sum.inl '' v, mem_union_left _ (mem_image_of_mem _ vs), mem_image_of_mem _ xv, image_subset_iff.2 vu⟩ }, { have h'x : x ∈ sum.inr ⁻¹' u := hxu, obtain ⟨v, vs, xv, vu⟩ : ∃ (v : set β) (H : v ∈ t), x ∈ v ∧ v ⊆ sum.inr ⁻¹' u := ht.exists_subset_of_mem_open h'x (is_open_sum_iff.1 u_open).2, exact ⟨sum.inr '' v, mem_union_right _ (mem_image_of_mem _ vs), mem_image_of_mem _ xv, image_subset_iff.2 vu⟩ } } end /-- A sum type of two second countable spaces is second countable. -/ instance [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α ⊕ β) := begin let b := (λ u, sum.inl '' u) '' (countable_basis α) ∪ (λ u, sum.inr '' u) '' (countable_basis β), have A : is_topological_basis b := (is_basis_countable_basis α).sum (is_basis_countable_basis β), have B : b.countable := (countable.image (countable_countable_basis _) _).union (countable.image (countable_countable_basis _) _), exact A.second_countable_topology B, end end sum end topological_space open topological_space variables {α β : Type*} [topological_space α] [topological_space β] {f : α → β} protected lemma inducing.second_countable_topology [second_countable_topology β] (hf : inducing f) : second_countable_topology α := by { rw hf.1, exact second_countable_topology_induced α β f } protected lemma embedding.second_countable_topology [second_countable_topology β] (hf : embedding f) : second_countable_topology α := hf.1.second_countable_topology
5f6c045eb9e86d596f41b48ec500555c29d4395b
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/match_bug.lean
4d9b73be187ead5f186f322b58385df647dfa644
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
64
lean
definition foo : nat -> nat := fun x, match x with | bar -> bar
9a0f645fe18baa8c2ac2099dcc496cc41c577333
38a6d5def645a1887e1306ceb4da06ff71452096
/_3_The_Network_Layer/Kaushiks_paper.lean
3d7ac59bc3c661f7c88a8d1a7652e07d1263afa5
[]
no_license
QTM3x/Quantum-Internet
bcc2d61e2ae7233bb2b369fedaed22a1feb6fba1
f90e09fb6c03d35043654d8b1bec1c63d6012268
refs/heads/master
1,609,224,401,937
1,599,911,583,000
1,599,911,583,000
238,495,221
45
33
null
1,603,625,079,000
1,580,919,815,000
Jupyter Notebook
UTF-8
Lean
false
false
13,946
lean
import data.real.basic import analysis.special_functions.exp_log import _5_The_Physical_Layer.qubit_carriers.quantum_state import _5_The_Physical_Layer.qubit_carriers.entropy_of_states ---- OPENING REMARKS /- This file formalizes the math in https://arxiv.org/pdf/2005.14304.pdf. The comments and docstrings in quotations are copied directly from the text of the paper. -/ /- "In this paper, we are interested in computing the maximum entanglement distribution rate (flow). Given a quantum network G and a set of demands D, we investigate how to produce a set of paths P_i and an end-to-end entanglement generation rate r_i (flow), corresponding to each demand (si, ei, Fi), such that the total entanglement generation rate ∑ i ∈ [1,|D|], r_i is maximised." -/ /- "We consider the problem of optimising the achievable EPR-pair distribution rate between multiple source-destination pairs in a quantum internet, where the repeaters may perform a probabilistic bell-state measurement and we may impose a minimum end-to-end fidelity as a requirement. 1- We construct an efficient linear programming formulation that computes the maximum total achievable entanglement distribution rate, satisfying the end-to-end fidelity constraint in polynomial time (in the number of nodes in the network). 2- We also propose an efficient algorithm that takes the output of the linear programming solver as an input and runs in polynomial time (in the number of nodes) to produce the set of paths to be used to achieve the entanglement distribution rate. 3- Moreover, we point out a practical entanglement generation protocol which can achieve those rates." -/ ---- DEFINITIONS /- Linear program -/ -- structure maximization_problem_with_constraints : Type := -- (problem : Prop) -- (constraint_1 : Prop) -- (constraint_2 : Prop) -- (constraint_3 : Prop) /- Node on a network -/ structure node : Type := (id : ℕ) -- we identify each node by a unique natural number /- Edge on a network: represents the physical connections (e.g., optical fibers between the nodes). -/ structure edge : Type := (node1 : node) (node2 : node) (cap : ℝ) -- we attach to each edge a real number called the capacity of the edge -- which represents the maximum rate at which EPR pairs can be distributed -- between the two nodes of the edge. /- Path "Corresponding to a demand (si, ei, Fi), if we start generating the EPR-pairs along a path p = ((si, u1),(u1, u2), . . . ,(u|p|−1, ei)), then the total number of required entanglement swap operations is |p|−1." -/ structure path' : Type := -- Have to use path' because path is taken by Lean. (edges : list edge) -- a path is a list of edges. -- path-length notation notation `|`p`|` := p.edges.length -- broken /- Entanglement distribution rate through a path -/ def rate_of_path (p : path') : ℝ := sorry /- "In a quantum internet, we abstract the entire network as a graph G = (V, E, C), where V represents the set of repeaters as well as the set of end nodes, and the set of edges, E, abstracts the physical communication links." -/ -- variable {F : ℝ} {hF : F > 0.5} structure quantum_internet : Type := (V : list node) -- V is a list of nodes (E : list edge) -- E is a list of edges. -- (C : list (edge → ℝ)) -- C is a list of functions that each eat an edge and spit out a real number. -- WE DEFINE THIS ABOVE AS A PROPERTY OF EDGES. -- (hF : -- ∀ (u v : node) {u ∈ V ∧ v ∈ V}, -- ⟨u,v⟩ ∈ E → ⟨u,v⟩.fidelity ≥ F -- ) /- Demand "Depending on the applications, the end nodes may need to generate EPR pairs with a certain fidelity. Keeping the analogy with the classical internet, here we refer to such requirement as a demand (commodity) and it consists of four items, a source s ∈ V , a destination e ∈ V , end-to-end desired entanglement distribution rate r and an end-to-end fidelity requirement F_end." -/ -- variable {qInternet : quantum_internet} structure demand : Type := (source : node) (dest : node) -- (rate : ℝ) (min_fid : ℝ) -- minimum fidelity required ---- ASSUMPTIONS /- "In this paper we assume that all the mixed entangled states in the network are Werner states. The main reason is that Werner states can be written as mixing with isotropic noise and hence form the worst case assumption." -/ variables {n : ℕ} {ρ : Werner_state n} /- "We assume that the fidelity of all the EPR-pairs, generated between any two nodes u, v ∈ V such that (u, v) ∈ E, is the same (say F)." -/ variable {F : ℝ} -- F is the elementary link fidelity of all elementary links /- I think the Bell-state measurement success probability is assumed to be the same for all measurements. -/ variables {q : ℝ} {hq : q > 0.5} -- BSM success probability /- I think the elementary link creation probability is assumed to be 1 throughout. -/ ---- PATH LENGTH CONSTRAINT /- Length-constraint of a demand: computed from the minimum fidelity "For the routing algorithms, one possible way to guarantee the end-to-end fidelity is to put an upper bound on the discovered path-lengths." -/ /- "In a quantum network, the fidelity of the EPR pairs drops with each entanglement swap operation. The fidelity of the output state after a successful swap operation depends on the fidelity of the two input states. If a mixed state ρ has fidelity F with respect to an EPR pair, say |Ψ⁺⟩ = 1/√2(|00⟩+|11⟩, then the corresponding Werner state [32] with parameter W can be written as ρ = W * |Ψ⁺⟩⟨ψ⁺| + (1 - W)/4 * I_4, where I_4 is the identity matrix with dimension 4. The fidelity of this state is (1 + 3*W)/4 [Write Lean theorems about this stuff]. For the Werner states, if a node performs a noise-free entanglement swap operation between two EPR-pairs with fidelities F, then the fidelity of the resulting state is (1 + 3*W^2)/4, which is equal to 1 + 3/4*((4*F - 1) / 3)^2 [33]." -/ theorem Werner_state_fidelity_after_k_swaps {n : ℕ} {ρ : density_operator n} (k : ℕ) (ws := Werner_state(ρ)): fidelity swap_gate_channel^k ws |ψ⁺⟩⟨ψ⁺| = (1 + 3*W)/4 := begin sorry end /- The goal is to have 1 + 3/4* ((4*F - 1) / 3)^(|p|) ≥ Fᵢ, where Fᵢ is the minimum fidelity required in a given demand. -/ def satisfies_fidelity_constraint_of_demand (p : path') (d : demand) : Prop := 1 + 3/4* ((4*F - 1) / 3)^(p.edges.length) ≥ d.min_fid /- "From this relation, we get the following constraint on the length of the p: |p| ≤ floor( real.log ((4*Fᵢ - 1) / 3) / real.log ((4*F - 1) / 3) )." -/ theorem max_path_length_of_fidelity (p : path') (D : list demand) : ∀ (d : demand) {hd : d ∈ D}, satisfies_fidelity_constraint_of_demand p d → p.edges.length ≤ floor( real.log ((4*d.min_fid - 1) / 3) / real.log ((4*F - 1) / 3) ) := begin sorry end /- "For the entanglement distribution rate, here we let the achievable rate between two along a repeater chain (a path) p be any real number rₚ such that rₚ ≤ q^(|p| - 1) * min{C(⟨s,u1⟩), C(⟨u1,u2⟩), ..., C(⟨u*|p|-1,e⟩)}, where C(⟨u,v⟩) denotes the capacities of the edges on the path and q is the success probability of the Bell-state measurements." -/ def is_achievable_rate (r : ℝ) (p : path') : Prop := r ≤ q^(p.edges.length - 1) * min{p.edges[0].cap, p.edges[1].cap, ..., p.edges[p.edges.length-1].cap} ---- GRAPH MODIFICATION /- "In this paper, we borrow ideas from the length-constrained multi-commodity flow [22] to handle this problem. In order to implement the length constraint we need to modify the network graph G as well as the demand set D." -/ /- Proposition 1 The run-time helper proposition. I am going to ignore the run-time proofs for the first pass. -/ theorem Proposition_1 : |P i j| ≤ |E| * |V| := begin sorry end /- Theorem 1 -/ theorem Theorem_1 : algorithm1.runtime ≤ |D| * |V| * |E| := begin sorry end ---- EDGE-BASED IS AT LEAST AS GOOD AS PATH-BASED /- Proposition 2 -/ def g_tilde (e : edge) : ℝ := sorry theorem Proposition_2 : ∀ (e' ∈ extended_network.edges), ∑_i=1^k ∑_j=1^lᵢ ∑_p∈Pᵢⱼ rₚ = ∑_i=1^k ∑_j=1^lᵢ q^(j-1) * ∑_v¹:(sᵢ⁰,v¹)∈E' g_tilde(⟨s_i⁰,v¹⟩) := begin -- According to the definition of ˜gij in equation ... unfold g_tilde, -- This implies, rw mul_iff_div, -- sum over the two intervals and done rw sum_eq_sum, sorry end /- Proposition 3 -/ theorem Proposition_3 : ∀ ... , ∑_i=1^k ∑_j=1^lᵢ ∑_t=0^{l_max-1} g_tilde i j ⟨u(t),v(t+1)⟩ ≤ C(⟨u,v⟩) := begin unfold g_tilde, sorry end /- Proposition 4 -/ theorem Proposition_4 : ∀ ... , ∑ g_tilde i j ⟨u',v'⟩ = ∑ g_tilde i j ⟨v',w'⟩ := begin unfold g_tilde, sorry end ---- PATH-BASED IS AT LEAST AS GOOD AS EDGE-BASED /- Proposition 5 -/ theorem Proposition_5 : ∀ e' ∈ extended_network.edges, F i j m e' ≥ 0 := begin sorry end /- Proposition 6 Depends on stuff in algorithm 3. -/ theorem Proposition_6 : ∀ ... , |P i j| ≤ extended_network.edges.length := begin sorry end /- Proposition 7 -/ theorem Proposition_7 : ∀ ... , ∑ F i j m ⟨u',v'⟩ = ∑ F i j m ⟨v',w'⟩ := begin sorry end /- Proposition 8 -/ def P : list (list path') := sorry def F (i j m : ℕ) (e : edge) : ℝ := sorry theorem F_recursion_relation (i j m : ℕ) : -- should this be a theorem? ∀ e' ∈ extended_network.edges, F i j m e' = F i j (m-1) e' - (r_tilde ()) / q^(j-1) begin sorry end def r_tilde (p : path') (j m : ℕ) : ℝ := q^(j-1) * min_{e ∈ (p j m).edges} {F i j m e} theorem Proposition_8 : ∀ e ∈ network.edges , ∑ i ∈ finset.range(k), ∑ j ∈ finset.range(l[i]), ∑ t ∈ finset.range(l.max), ∑ (r_tilde p j m) / q^(j-1) ≤ e.cap := begin sorry end /- Proposition 9 -/ theorem Proposition_9 : ∀ ... , ∑ ∑ q^(j-1) ∑ g i j ⟨sᵢ⁰,v¹⟩ = ∑ ∑ ∑ r_tilde p := begin sorry end ---- PREPARE AND SWAP /- "We point out that there exists a practical entanglement distribution protocol along a path, called the prepare and swap protocol, which achieves the rates (asymptotically) proposed by path extraction algorithm." -/ /- Theorem (Lemma 3 in Appendix D in the paper) "In a repeater chain network with n + 1 repeaters {u0, u1, . . . , un}, if the probability of generating an elementary pair per attempt is one and the probability of a successful BSM is (q) and the capacity of an elementary link (ui, ui+1) (for 0 ≤ i ≤ n − 1) is denoted by Ci and if the repeaters follow the prepare and swap protocol for generating EPR-pairs, then the expected end-to-end entanglement generation rate r is ... " -/ theorem prepare_and_swap_rate (stuff) : r = q^(n-1) * min{C₀, ..., C_n} := begin sorry end ---- EDGE-BASED FORMULATION def edge_based_maximization_problem : maximization_problem_with_constraints := ⟨ ∑ ∑ q^(j-1) * ∑ g i j ⟨sᵢ⁰,v¹⟩, ∀ ..., g i j ⟨u(t),v(t+1)⟩, ∀ ..., ∑ ∑ ∑ g i j ⟨u(t),v(t+1)⟩ ≤ C ⟨u,v⟩, ∀ ..., g i j ⟨u',v'⟩ = g i j ⟨v',w'⟩ ⟩ ---- PATH-BASED FORMULATION /- Path-based problem on the original network -/ def path_based_maximization_problem : maximization_problem_with_constraints := ⟨ ∑ ∑ rₚ, ∑ ∑ rₚ / q^(|p|-1) ≤ C ⟨u,v⟩, ⟩ /- Path-based problem on the modified network -/ def path_based_maximization_problem : maximization_problem_with_constraints := ⟨ ∑ ∑ ∑ rₚ, ∑ ∑ ∑ ∑ rₚ / q^(|p|-1) ≤ C ⟨u,v⟩, ⟩ ---- SOLUTION TO EDGE-BASED PROBLEM IFF SOLUTION TO PATH-BASED PROBLEM ---- PATH EXTRACTION ALGORITHM /- "we provide an algorithm, called the path extraction algorithm, which takes the solutions of the edge-based formulation and for each of the commodities it extracts the set of paths to be used and the corresponding entanglement distribution rate along that path." -/ ---- CLOSING REMARKS /- "One can use any entanglement generation protocol for distributing EPR-pairs across the paths that are discovered by the path-extraction algorithm." "Some repeater technologies are unable to perform such quantum operations (for instance, the atomic ensemble-based quantum repeaters). Hence, for such cases, one can achieve the end-to-end fidelity requirement only by increasing the fidelity F of the elementary pairs and reducing the length of the discovered path." But if you can do distillation you can increase the fidelity of the end-to-end links: "One can enhance the end-to-end fidelity using entanglement distillation." -/ /- "There exists a practical protocol, called prepare and swap protocol, which can be implemented using atomic ensemble based repeaters and if one uses this protocol for distributing entanglement across each of the paths, then one can generate EPR-pairs with the rate proposed by our path-extraction algorithm." -/ /- "In this paper, we focus on maximising the end-to-end entanglement generation rate. However, one can easily extend our results for other objective functions, like minimising the weighted sum of congestion at edges." -/ /- "In future work, it would be interesting to include the more realistic parameters like the bounded storage capacity, time to perform the swap operation, etc., in our model and modify our current formulations to come up with more sophisticated routing algorithms." -/ /- Where the authors took inspiration from: "Our algorithm is inspired by multi-commodity flow optimisation which is a very well-studied subject and has been used in many optimisation problems, including classical internet routing [13]." -/ ---- SCRATCH WORK AND NOTES /- "Don't make a new structure for the linear programs. Just prove the theroems about them and use the PuLP implementation in Kaushik's Github." -/
ddb66fc2091df1d9813e511448c54219d24245bd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/golden_ratio_auto.lean
47fa3f2f63faa0b5d6a2be3d9986eb5e487522e9
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,344
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.real.irrational import Mathlib.data.nat.fib import Mathlib.data.matrix.notation import Mathlib.tactic.ring_exp import Mathlib.algebra.linear_recurrence import Mathlib.PostPort universes u_1 namespace Mathlib /-! # The golden ratio and its conjugate This file defines the golden ratio `φ := (1 + √5)/2` and its conjugate `ψ := (1 - √5)/2`, which are the two real roots of `X² - X - 1`. Along with various computational facts about them, we prove their irrationality, and we link them to the Fibonacci sequence by proving Binet's formula. -/ /-- The golden ratio `φ := (1 + √5)/2`. -/ def golden_ratio : ℝ := (1 + real.sqrt (bit1 (bit0 1))) / bit0 1 /-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/ def golden_conj : ℝ := (1 - real.sqrt (bit1 (bit0 1))) / bit0 1 /-- The inverse of the golden ratio is the opposite of its conjugate. -/ theorem inv_gold : golden_ratio⁻¹ = -golden_conj := sorry /-- The opposite of the golden ratio is the inverse of its conjugate. -/ theorem inv_gold_conj : golden_conj⁻¹ = -golden_ratio := sorry @[simp] theorem gold_mul_gold_conj : golden_ratio * golden_conj = -1 := sorry @[simp] theorem gold_conj_mul_gold : golden_conj * golden_ratio = -1 := eq.mpr (id (Eq._oldrec (Eq.refl (golden_conj * golden_ratio = -1)) (mul_comm golden_conj golden_ratio))) gold_mul_gold_conj @[simp] theorem gold_add_gold_conj : golden_ratio + golden_conj = 1 := sorry theorem one_sub_gold_conj : 1 - golden_ratio = golden_conj := sorry theorem one_sub_gold : 1 - golden_conj = golden_ratio := sorry @[simp] theorem gold_sub_gold_conj : golden_ratio - golden_conj = real.sqrt (bit1 (bit0 1)) := sorry @[simp] theorem gold_sq : golden_ratio ^ bit0 1 = golden_ratio + 1 := sorry @[simp] theorem gold_conj_sq : golden_conj ^ bit0 1 = golden_conj + 1 := sorry theorem gold_pos : 0 < golden_ratio := sorry theorem gold_ne_zero : golden_ratio ≠ 0 := ne_of_gt gold_pos theorem one_lt_gold : 1 < golden_ratio := sorry theorem gold_conj_neg : golden_conj < 0 := sorry theorem gold_conj_ne_zero : golden_conj ≠ 0 := ne_of_lt gold_conj_neg theorem neg_one_lt_gold_conj : -1 < golden_conj := eq.mpr (id (Eq._oldrec (Eq.refl (-1 < golden_conj)) (propext neg_lt))) (eq.mpr (id (Eq._oldrec (Eq.refl (-golden_conj < 1)) (Eq.symm inv_gold))) (inv_lt_one one_lt_gold)) /-! ## Irrationality -/ /-- The golden ratio is irrational. -/ theorem gold_irrational : irrational golden_ratio := sorry /-- The conjugate of the golden ratio is irrational. -/ theorem gold_conj_irrational : irrational golden_conj := sorry /-! ## Links with Fibonacci sequence -/ /-- The recurrence relation satisfied by the Fibonacci sequence. -/ def fib_rec {α : Type u_1} [comm_semiring α] : linear_recurrence α := linear_recurrence.mk (bit0 1) (matrix.vec_cons 1 (matrix.vec_cons 1 matrix.vec_empty)) /-- The characteristic polynomial of `fib_rec` is `X² - (X + 1)`. -/ theorem fib_rec_char_poly_eq {β : Type u_1} [comm_ring β] : linear_recurrence.char_poly fib_rec = polynomial.X ^ bit0 1 - (polynomial.X + 1) := sorry /-- As expected, the Fibonacci sequence is a solution of `fib_rec`. -/ theorem fib_is_sol_fib_rec {α : Type u_1} [comm_semiring α] : linear_recurrence.is_solution fib_rec fun (x : ℕ) => ↑(nat.fib x) := sorry /-- The geometric sequence `λ n, φ^n` is a solution of `fib_rec`. -/ theorem geom_gold_is_sol_fib_rec : linear_recurrence.is_solution fib_rec (pow golden_ratio) := sorry /-- The geometric sequence `λ n, ψ^n` is a solution of `fib_rec`. -/ theorem geom_gold_conj_is_sol_fib_rec : linear_recurrence.is_solution fib_rec (pow golden_conj) := sorry /-- Binet's formula as a function equality. -/ theorem real.coe_fib_eq' : (fun (n : ℕ) => ↑(nat.fib n)) = fun (n : ℕ) => (golden_ratio ^ n - golden_conj ^ n) / real.sqrt (bit1 (bit0 1)) := sorry /-- Binet's formula as a dependent equality. -/ theorem real.coe_fib_eq (n : ℕ) : ↑(nat.fib n) = (golden_ratio ^ n - golden_conj ^ n) / real.sqrt (bit1 (bit0 1)) := sorry end Mathlib
bf61053d2f45c4290df9136562a4dd3341a01a6a
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/concrete_category/reflects_isomorphisms.lean
1ee47b2c46c164892af1937fd0f8d6e2935fdeeb
[ "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
1,449
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.concrete_category.basic import category_theory.reflects_isomorphisms /-! A `forget₂ C D` forgetful functor between concrete categories `C` and `D` whose forgetful functors both reflect isomorphisms, itself reflects isomorphisms. -/ universes u namespace category_theory instance : reflects_isomorphisms (forget (Type u)) := { reflects := λ X Y f i, i } variables (C : Type (u+1)) [category C] [concrete_category.{u} C] variables (D : Type (u+1)) [category D] [concrete_category.{u} D] /-- A `forget₂ C D` forgetful functor between concrete categories `C` and `D` where `forget C` reflects isomorphisms, itself reflects isomorphisms. -/ -- This should not be an instance, as it causes a typeclass loop -- with `category_theory.has_forget_to_Type` lemma reflects_isomorphisms_forget₂ [has_forget₂ C D] [reflects_isomorphisms (forget C)] : reflects_isomorphisms (forget₂ C D) := { reflects := λ X Y f i, begin resetI, haveI i' : is_iso ((forget D).map ((forget₂ C D).map f)) := functor.map_is_iso (forget D) _, haveI : is_iso ((forget C).map f) := begin have := has_forget₂.forget_comp, dsimp at this, rw ←this, exact i', end, apply is_iso_of_reflects_iso f (forget C), end } end category_theory
60096696f81e34d4dd91f20d8b2b534c08a17855
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/mean_inequalities.lean
62cbe6a4393f3033b7502bbca2a38d410d3b5037
[ "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
38,457
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import analysis.convex.jensen import analysis.convex.specific_functions.basic import analysis.special_functions.pow.nnreal import data.real.conjugate_exponents /-! # Mean value inequalities > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove several inequalities for finite sums, including AM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. Versions for integrals of some of these inequalities are available in `measure_theory.mean_inequalities`. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≤ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `real` namespace, and a version for `nnreal`-valued functions is in the `nnreal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≤ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It is then used to prove Hölder's inequality (see below). ### Hölder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≤ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `ℝ`, `ℝ≥0` and `ℝ≥0∞`. There are at least two short proofs of this inequality. In our proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. Another possible proof would be to deduce this inequality from the generalized mean inequality for well-chosen vectors and weights. ### Minkowski's inequality The inequality says that for `p ≥ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `real`, `ℝ≥0` and `ℝ≥0∞`. We deduce this inequality from Hölder's inequality. Namely, Hölder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. ## TODO - each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `strict_convex_on` functions. - generalized mean inequality with any `p ≤ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. -/ universes u v open finset open_locale classical big_operators nnreal ennreal noncomputable theory variables {ι : Type u} (s : finset ι) section geom_mean_le_arith_mean /-! ### AM-GM inequality -/ namespace real /-- AM-GM inequality: the **geometric mean is less than or equal to the arithmetic mean**, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) : (∏ i in s, (z i) ^ (w i)) ≤ ∑ i in s, w i * z i := begin -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0, { rcases A with ⟨i, his, hzi, hwi⟩, rw [prod_eq_zero his], { exact sum_nonneg (λ j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow hwi } }, -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. { simp only [not_exists, not_and, ne.def, not_not] at A, have := convex_on_exp.map_sum_le hw hw' (λ i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { exact rpow_def_of_pos hz _ } }, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { rw [exp_log hz] } } } end theorem geom_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : (∏ i in s, (z i) ^ (w i)) = x := calc (∏ i in s, (z i) ^ (w i)) = ∏ i in s, x ^ w i : begin refine prod_congr rfl (λ i hi, _), cases eq_or_ne (w i) 0 with h₀ h₀, { rw [h₀, rpow_zero, rpow_zero] }, { rw hx i hi h₀ } end ... = x : begin rw [← rpow_sum_of_nonneg _ hw, hw', rpow_one], have : (∑ i in s, w i) ≠ 0, { rw hw', exact one_ne_zero }, obtain ⟨i, his, hi⟩ := exists_ne_zero_of_sum_ne_zero this, rw ← hx i his hi, exact hz i his end theorem arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw' : ∑ i in s, w i = 1) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : ∑ i in s, w i * z i = x := calc ∑ i in s, w i * z i = ∑ i in s, w i * x : begin refine sum_congr rfl (λ i hi, _), cases eq_or_ne (w i) 0 with hwi hwi, { rw [hwi, zero_mul, zero_mul] }, { rw hx i hi hwi }, end ... = x : by rw [←sum_mul, hw', one_mul] theorem geom_mean_eq_arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : (∏ i in s, (z i) ^ (w i)) = ∑ i in s, w i * z i := by rw [geom_mean_weighted_of_constant, arith_mean_weighted_of_constant]; assumption end real namespace nnreal /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for `nnreal`-valued functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) : (∏ i in s, (z i) ^ (w i:ℝ)) ≤ ∑ i in s, w i * z i := by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (λ i _, (w i).coe_nonneg) (by assumption_mod_cast) (λ i _, (z i).coe_nonneg) /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for two `nnreal` numbers. -/ theorem geom_mean_le_arith_mean2_weighted (w₁ w₂ p₁ p₂ : ℝ≥0) : w₁ + w₂ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) ≤ w₁ * p₁ + w₂ * p₂ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂] ![p₁, p₂] theorem geom_mean_le_arith_mean3_weighted (w₁ w₂ w₃ p₁ p₂ p₃ : ℝ≥0) : w₁ + w₂ + w₃ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂, w₃] ![p₁, p₂, p₃] theorem geom_mean_le_arith_mean4_weighted (w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ≥0) : w₁ + w₂ + w₃ + w₄ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ)* p₄ ^ (w₄:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂, w₃, w₄] ![p₁, p₂, p₃, p₄] end nnreal namespace real theorem geom_mean_le_arith_mean2_weighted {w₁ w₂ p₁ p₂ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hw : w₁ + w₂ = 1) : p₁ ^ w₁ * p₂ ^ w₂ ≤ w₁ * p₁ + w₂ * p₂ := nnreal.geom_mean_le_arith_mean2_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean3_weighted {w₁ w₂ w₃ p₁ p₂ p₃ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hw : w₁ + w₂ + w₃ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ $ nnreal.coe_eq.1 hw theorem geom_mean_le_arith_mean4_weighted {w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hw₄ : 0 ≤ w₄) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hp₄ : 0 ≤ p₄) (hw : w₁ + w₂ + w₃ + w₄ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ * p₄ ^ w₄ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨w₄, hw₄⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ ⟨p₄, hp₄⟩ $ nnreal.coe_eq.1 $ by assumption end real end geom_mean_le_arith_mean section young /-! ### Young's inequality -/ namespace real /-- Young's inequality, a version for nonnegative real numbers. -/ theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) (hpq : p.is_conjugate_exponent q) : a * b ≤ a^p / p + b^q / q := by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul] using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg (rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj /-- Young's inequality, a version for arbitrary real numbers. -/ theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ |a|^p / p + |b|^q / q := calc a * b ≤ |a * b| : le_abs_self (a * b) ... = |a| * |b| : abs_mul a b ... ≤ |a|^p / p + |b|^q / q : real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq end real namespace nnreal /-- Young's inequality, `ℝ≥0` version. We use `{p q : ℝ≥0}` in order to avoid constructing witnesses of `0 ≤ p` and `0 ≤ q` for the denominators. -/ theorem young_inequality (a b : ℝ≥0) {p q : ℝ≥0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) : a * b ≤ a^(p:ℝ) / p + b^(q:ℝ) / q := real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩ /-- Young's inequality, `ℝ≥0` version with real conjugate exponents. -/ theorem young_inequality_real (a b : ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / real.to_nnreal p + b ^ q / real.to_nnreal q := begin nth_rewrite 0 ← real.coe_to_nnreal p hpq.nonneg, nth_rewrite 0 ← real.coe_to_nnreal q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, end end nnreal namespace ennreal /-- Young's inequality, `ℝ≥0∞` version with real conjugate exponents. -/ theorem young_inequality (a b : ℝ≥0∞) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / ennreal.of_real p + b ^ q / ennreal.of_real q := begin by_cases h : a = ⊤ ∨ b = ⊤, { refine le_trans le_top (le_of_eq _), repeat { rw div_eq_mul_inv }, cases h; rw h; simp [h, hpq.pos, hpq.symm.pos], }, push_neg at h, -- if a ≠ ⊤ and b ≠ ⊤, use the nnreal version: nnreal.young_inequality_real rw [←coe_to_nnreal h.left, ←coe_to_nnreal h.right, ←coe_mul, coe_rpow_of_nonneg _ hpq.nonneg, coe_rpow_of_nonneg _ hpq.symm.nonneg, ennreal.of_real, ennreal.of_real, ←@coe_div (real.to_nnreal p) _ (by simp [hpq.pos]), ←@coe_div (real.to_nnreal q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end end ennreal end young section holder_minkowski /-! ### Hölder's and Minkowski's inequalities -/ namespace nnreal private lemma inner_le_Lp_mul_Lp_of_norm_le_one (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : ∑ i in s, (f i) ^ p ≤ 1) (hg : ∑ i in s, (g i) ^ q ≤ 1) : ∑ i in s, f i * g i ≤ 1 := begin have hp_ne_zero : real.to_nnreal p ≠ 0, from (zero_lt_one.trans hpq.one_lt_nnreal).ne.symm, have hq_ne_zero : real.to_nnreal q ≠ 0, from (zero_lt_one.trans hpq.symm.one_lt_nnreal).ne.symm, calc ∑ i in s, f i * g i ≤ ∑ i in s, ((f i) ^ p / real.to_nnreal p + (g i) ^ q / real.to_nnreal q) : finset.sum_le_sum (λ i his, young_inequality_real (f i) (g i) hpq) ... = (∑ i in s, (f i) ^ p) / real.to_nnreal p + (∑ i in s, (g i) ^ q) / real.to_nnreal q : by rw [sum_add_distrib, sum_div, sum_div] ... ≤ 1 / real.to_nnreal p + 1 / real.to_nnreal q : by { refine add_le_add _ _, { rwa [div_le_iff hp_ne_zero, div_mul_cancel _ hp_ne_zero], }, { rwa [div_le_iff hq_ne_zero, div_mul_cancel _ hq_ne_zero], }, } ... = 1 : hpq.inv_add_inv_conj_nnreal, end private lemma inner_le_Lp_mul_Lp_of_norm_eq_zero (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : ∑ i in s, (f i) ^ p = 0) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) := begin simp only [hf, hpq.ne_zero, one_div, sum_eq_zero_iff, zero_rpow, zero_mul, inv_eq_zero, ne.def, not_false_iff, le_zero_iff, mul_eq_zero], intros i his, left, rw sum_eq_zero_iff at hf, exact (rpow_eq_zero_iff.mp (hf i his)).left, end /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0`-valued functions. -/ theorem inner_le_Lp_mul_Lq (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) := begin by_cases hF_zero : ∑ i in s, (f i) ^ p = 0, { exact inner_le_Lp_mul_Lp_of_norm_eq_zero s f g hpq hF_zero, }, by_cases hG_zero : ∑ i in s, (g i) ^ q = 0, { calc ∑ i in s, f i * g i = ∑ i in s, g i * f i : by { congr' with i, rw mul_comm, } ... ≤ (∑ i in s, (g i) ^ q) ^ (1 / q) * (∑ i in s, (f i) ^ p) ^ (1 / p) : inner_le_Lp_mul_Lp_of_norm_eq_zero s g f hpq.symm hG_zero ... = (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) : mul_comm _ _, }, let f' := λ i, (f i) / (∑ i in s, (f i) ^ p) ^ (1 / p), let g' := λ i, (g i) / (∑ i in s, (g i) ^ q) ^ (1 / q), suffices : ∑ i in s, f' i * g' i ≤ 1, { simp_rw [f', g', div_mul_div_comm, ← sum_div] at this, rwa [div_le_iff, one_mul] at this, refine mul_ne_zero _ _, { rw [ne.def, rpow_eq_zero_iff, not_and_distrib], exact or.inl hF_zero, }, { rw [ne.def, rpow_eq_zero_iff, not_and_distrib], exact or.inl hG_zero, }, }, refine inner_le_Lp_mul_Lp_of_norm_le_one s f' g' hpq (le_of_eq _) (le_of_eq _), { simp_rw [f', div_rpow, ← sum_div, ← rpow_mul, one_div, inv_mul_cancel hpq.ne_zero, rpow_one, div_self hF_zero], }, { simp_rw [g', div_rpow, ← sum_div, ← rpow_mul, one_div, inv_mul_cancel hpq.symm.ne_zero, rpow_one, div_self hG_zero], }, end /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_has_sum`. -/ theorem inner_le_Lp_mul_Lq_tsum {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) ∧ ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := begin have H₁ : ∀ s : finset ι, ∑ i in s, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q), { intros s, refine le_trans (inner_le_Lp_mul_Lq s f g hpq) (mul_le_mul _ _ bot_le bot_le), { rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr hpq.pos), exact sum_le_tsum _ (λ _ _, zero_le _) hf }, { rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr hpq.symm.pos), exact sum_le_tsum _ (λ _ _, zero_le _) hg } }, have bdd : bdd_above (set.range (λ s, ∑ i in s, f i * g i)), { refine ⟨(∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q), _⟩, rintros a ⟨s, rfl⟩, exact H₁ s }, have H₂ : summable _ := (has_sum_of_is_lub _ (is_lub_csupr bdd)).summable, exact ⟨H₂, tsum_le_of_sum_le H₂ H₁⟩, end theorem summable_mul_of_Lp_Lq {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) := (inner_le_Lp_mul_Lq_tsum hpq hf hg).1 theorem inner_le_Lp_mul_Lq_tsum' {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := (inner_le_Lp_mul_Lq_tsum hpq hf hg).2 /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_tsum`. -/ theorem inner_le_Lp_mul_Lq_has_sum {f g : ι → ℝ≥0} {A B : ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg : has_sum (λ i, (g i) ^ q) (B ^ q)) : ∃ C, C ≤ A * B ∧ has_sum (λ i, f i * g i) C := begin obtain ⟨H₁, H₂⟩ := inner_le_Lp_mul_Lq_tsum hpq hf.summable hg.summable, have hA : A = (∑' (i : ι), f i ^ p) ^ (1 / p), { rw [hf.tsum_eq, rpow_inv_rpow_self hpq.ne_zero] }, have hB : B = (∑' (i : ι), g i ^ q) ^ (1 / q), { rw [hg.tsum_eq, rpow_inv_rpow_self hpq.symm.ne_zero] }, refine ⟨∑' i, f i * g i, _, _⟩, { simpa [hA, hB] using H₂ }, { simpa only [rpow_self_rpow_inv hpq.ne_zero] using H₁.has_sum } end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ≥0`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (f : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, (f i) ^ p := begin cases eq_or_lt_of_le hp with hp hp, { simp [← hp] }, let q : ℝ := p / (p - 1), have hpq : p.is_conjugate_exponent q, { rw real.is_conjugate_exponent_iff hp }, have hp₁ : 1 / p * p = 1 := one_div_mul_cancel hpq.ne_zero, have hq : 1 / q * p = (p - 1), { rw [← hpq.div_conj_eq_sub_one], ring }, simpa only [nnreal.mul_rpow, ← nnreal.rpow_mul, hp₁, hq, one_mul, one_rpow, rpow_one, pi.one_apply, sum_const, nat.smul_one_eq_coe] using nnreal.rpow_le_rpow (inner_le_Lp_mul_Lq s 1 f hpq.symm) hpq.nonneg, end /-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product `∑ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/ theorem is_greatest_Lp (f : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : is_greatest ((λ g : ι → ℝ≥0, ∑ i in s, f i * g i) '' {g | ∑ i in s, (g i)^q ≤ 1}) ((∑ i in s, (f i)^p) ^ (1 / p)) := begin split, { use λ i, ((f i) ^ p / f i / (∑ i in s, (f i) ^ p) ^ (1 / q)), by_cases hf : ∑ i in s, (f i)^p = 0, { simp [hf, hpq.ne_zero, hpq.symm.ne_zero] }, { have A : p + q - q ≠ 0, by simp [hpq.ne_zero], have B : ∀ y : ℝ≥0, y * y^p / y = y^p, { refine λ y, mul_div_cancel_left_of_imp (λ h, _), simpa [h, hpq.ne_zero] }, simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul, div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add, ← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B], rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one], simpa [hpq.symm.ne_zero] using hf } }, { rintros _ ⟨g, hg, rfl⟩, apply le_trans (inner_le_Lp_mul_Lq s f g hpq), simpa only [mul_one] using mul_le_mul_left' (nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) _ } end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/ theorem Lp_add_le (f g : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := begin -- The result is trivial when `p = 1`, so we can assume `1 < p`. rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp, have := is_greatest_Lp s (f + g) hpq, simp only [pi.add_apply, add_mul, sum_add_distrib] at this, rcases this.1 with ⟨φ, hφ, H⟩, rw ← H, exact add_le_add ((is_greatest_Lp s f hpq).2 ⟨φ, hφ, rfl⟩) ((is_greatest_Lp s g hpq).2 ⟨φ, hφ, rfl⟩) end /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `Lp_add_le_has_sum_of_nonneg`. -/ theorem Lp_add_le_tsum {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) ∧ (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := begin have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, have H₁ : ∀ s : finset ι, ∑ i in s, (f i + g i) ^ p ≤ ((∑' i, (f i)^p) ^ (1/p) + (∑' i, (g i)^p) ^ (1/p)) ^ p, { intros s, rw ← nnreal.rpow_one_div_le_iff pos, refine le_trans (Lp_add_le s f g hp) (add_le_add _ _); rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr pos); refine sum_le_tsum _ (λ _ _, zero_le _) _, exacts [hf, hg] }, have bdd : bdd_above (set.range (λ s, ∑ i in s, (f i + g i) ^ p)), { refine ⟨((∑' i, (f i)^p) ^ (1/p) + (∑' i, (g i)^p) ^ (1/p)) ^ p, _⟩, rintros a ⟨s, rfl⟩, exact H₁ s }, have H₂ : summable _ := (has_sum_of_is_lub _ (is_lub_csupr bdd)).summable, refine ⟨H₂, _⟩, rw nnreal.rpow_one_div_le_iff pos, refine tsum_le_of_sum_le H₂ H₁, end theorem summable_Lp_add {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) := (Lp_add_le_tsum hp hf hg).1 theorem Lp_add_le_tsum' {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := (Lp_add_le_tsum hp hf hg).2 /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `Lp_add_le_tsum_of_nonneg`. -/ theorem Lp_add_le_has_sum {f g : ι → ℝ≥0} {A B : ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg : has_sum (λ i, (g i) ^ p) (B ^ p)) : ∃ C, C ≤ A + B ∧ has_sum (λ i, (f i + g i) ^ p) (C ^ p) := begin have hp' : p ≠ 0 := (lt_of_lt_of_le zero_lt_one hp).ne', obtain ⟨H₁, H₂⟩ := Lp_add_le_tsum hp hf.summable hg.summable, have hA : A = (∑' (i : ι), f i ^ p) ^ (1 / p) := by rw [hf.tsum_eq, rpow_inv_rpow_self hp'], have hB : B = (∑' (i : ι), g i ^ p) ^ (1 / p) := by rw [hg.tsum_eq, rpow_inv_rpow_self hp'], refine ⟨(∑' i, (f i + g i) ^ p) ^ (1 / p), _, _⟩, { simpa [hA, hB] using H₂ }, { simpa only [rpow_self_rpow_inv hp'] using H₁.has_sum } end end nnreal namespace real variables (f g : ι → ℝ) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) : ∑ i in s, f i * g i ≤ (∑ i in s, |f i| ^ p) ^ (1 / p) * (∑ i in s, |g i| ^ q) ^ (1 / q) := begin have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hpq), push_cast at this, refine le_trans (sum_le_sum $ λ i hi, _) this, simp only [← abs_mul, le_abs_self] end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (hp : 1 ≤ p) : (∑ i in s, |f i|) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, |f i| ^ p := begin have := nnreal.coe_le_coe.2 (nnreal.rpow_sum_le_const_mul_sum_rpow s (λ i, ⟨_, abs_nonneg (f i)⟩) hp), push_cast at this, exact this, -- for some reason `exact_mod_cast` can't replace this argument end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, |f i + g i| ^ p) ^ (1 / p) ≤ (∑ i in s, |f i| ^ p) ^ (1 / p) + (∑ i in s, |g i| ^ p) ^ (1 / p) := begin have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hp), push_cast at this, refine le_trans (rpow_le_rpow _ (sum_le_sum $ λ i hi, _) _) this; simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add, rpow_le_rpow] end variables {f g} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued nonnegative functions. -/ theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i)^p) ^ (1 / p) * (∑ i in s, (g i)^q) ^ (1 / q) := by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi] /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_has_sum_of_nonneg`. -/ theorem inner_le_Lp_mul_Lq_tsum_of_nonneg (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) ∧ ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, norm_cast at *, exact nnreal.inner_le_Lp_mul_Lq_tsum hpq hf_sum hg_sum, end theorem summable_mul_of_Lp_Lq_of_nonneg (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) := (inner_le_Lp_mul_Lq_tsum_of_nonneg hpq hf hg hf_sum hg_sum).1 theorem inner_le_Lp_mul_Lq_tsum_of_nonneg' (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := (inner_le_Lp_mul_Lq_tsum_of_nonneg hpq hf hg hf_sum hg_sum).2 /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_tsum_of_nonneg`. -/ theorem inner_le_Lp_mul_Lq_has_sum_of_nonneg (hpq : p.is_conjugate_exponent q) {A B : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg_sum : has_sum (λ i, (g i) ^ q) (B ^ q)) : ∃ C : ℝ, 0 ≤ C ∧ C ≤ A * B ∧ has_sum (λ i, f i * g i) C := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, lift A to ℝ≥0 using hA, lift B to ℝ≥0 using hB, norm_cast at hf_sum hg_sum, obtain ⟨C, hC, H⟩ := nnreal.inner_le_Lp_mul_Lq_has_sum hpq hf_sum hg_sum, refine ⟨C, C.prop, hC, _⟩, norm_cast, exact H end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with nonnegative `ℝ`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, f i ^ p := by convert rpow_sum_le_const_mul_sum_rpow s f hp using 2; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi] /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝ`-valued nonnegative functions. -/ theorem Lp_add_le_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1]; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg] /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `Lp_add_le_has_sum_of_nonneg`. -/ theorem Lp_add_le_tsum_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) ∧ (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, norm_cast at *, exact nnreal.Lp_add_le_tsum hp hf_sum hg_sum, end theorem summable_Lp_add_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) := (Lp_add_le_tsum_of_nonneg hp hf hg hf_sum hg_sum).1 theorem Lp_add_le_tsum_of_nonneg' (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := (Lp_add_le_tsum_of_nonneg hp hf hg hf_sum hg_sum).2 /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `Lp_add_le_tsum_of_nonneg`. -/ theorem Lp_add_le_has_sum_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) {A B : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) (hfA : has_sum (λ i, (f i) ^ p) (A ^ p)) (hgB : has_sum (λ i, (g i) ^ p) (B ^ p)) : ∃ C, 0 ≤ C ∧ C ≤ A + B ∧ has_sum (λ i, (f i + g i) ^ p) (C ^ p) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, lift A to ℝ≥0 using hA, lift B to ℝ≥0 using hB, norm_cast at hfA hgB, obtain ⟨C, hC₁, hC₂⟩ := nnreal.Lp_add_le_has_sum hp hfA hgB, use C, norm_cast, exact ⟨zero_le _, hC₁, hC₂⟩, end end real namespace ennreal variables (f g : ι → ℝ≥0∞) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0∞`-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) : (∑ i in s, f i * g i) ≤ (∑ i in s, (f i)^p) ^ (1/p) * (∑ i in s, (g i)^q) ^ (1/q) := begin by_cases H : (∑ i in s, (f i)^p) ^ (1/p) = 0 ∨ (∑ i in s, (g i)^q) ^ (1/q) = 0, { replace H : (∀ i ∈ s, f i = 0) ∨ (∀ i ∈ s, g i = 0), by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos, sum_eq_zero_iff_of_nonneg] using H, have : ∀ i ∈ s, f i * g i = 0 := λ i hi, by cases H; simp [H i hi], have : (∑ i in s, f i * g i) = (∑ i in s, 0) := sum_congr rfl this, simp [this] }, push_neg at H, by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^q) ^ (1/q) = ⊤, { cases H'; simp [H', -one_div, H] }, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ _ hpq), simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos), le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this, convert this using 1; [skip, congr' 2]; [skip, skip, simp, skip, simp]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul, with_top.coe_mul.symm] }, end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ≥0∞`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (hp : 1 ≤ p) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, (f i) ^ p := begin cases eq_or_lt_of_le hp with hp hp, { simp [← hp] }, let q : ℝ := p / (p - 1), have hpq : p.is_conjugate_exponent q, { rw real.is_conjugate_exponent_iff hp }, have hp₁ : 1 / p * p = 1 := one_div_mul_cancel hpq.ne_zero, have hq : 1 / q * p = (p - 1), { rw [← hpq.div_conj_eq_sub_one], ring }, simpa only [ennreal.mul_rpow_of_nonneg _ _ hpq.nonneg, ← ennreal.rpow_mul, hp₁, hq, coe_one, one_mul, one_rpow, rpow_one, pi.one_apply, sum_const, nat.smul_one_eq_coe] using ennreal.rpow_le_rpow (inner_le_Lp_mul_Lq s 1 f hpq.symm) hpq.nonneg, end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝ≥0∞` valued nonnegative functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p)^(1/p) ≤ (∑ i in s, (f i)^p) ^ (1/p) + (∑ i in s, (g i)^p) ^ (1/p) := begin by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^p) ^ (1/p) = ⊤, { cases H'; simp [H', -one_div] }, have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ hp), push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this, convert this using 2; [skip, congr' 1, congr' 1]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi] } end end ennreal end holder_minkowski
4cee3e7833ffcd74dac671b46da0008082fd4f97
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/doErrorMsg.lean
d6e9cc13a7246c66fdb1e8d92814a016e6f331bb
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
481
lean
def f : IO Nat := do IO.println "hello" IO.getStdin return 10 def f1 : ExceptT String (StateT Nat Id) Nat := do modify (· + 1) get def f2 (x : Nat) : ExceptT String (StateT Nat Id) Nat := do modify (· + x) get def g1 : ExceptT String (StateT Nat Id) Unit := do let x : String ← f1 return () def g2 : ExceptT String (StateT Nat Id) Unit := do let x : String ← f2 10 return () def g3 : ExceptT String (StateT Nat Id) String := do let x ← f2 f1
1256901966dc62645d267fb444fe871f4c7cf341
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex20.lean
0847c3f889f0d344b666c6d90718d80857384c70
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
207
lean
section useful variables (A B C : Type) variables (g : B → C) (f : A → B) (h : A → A) variable x : A def compose := g (f x) def do_twice := h (h x) def do_thrice := h (h (h x)) end useful
71a00d6b0652cee8289b5cf1668f1889a08bf888
4727251e0cd73359b15b664c3170e5d754078599
/src/algebraic_geometry/open_immersion.lean
d60fc7c67d23a1990d367a6b98e8acccc148dd45
[ "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
61,379
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import algebraic_geometry.presheafed_space.has_colimits import category_theory.limits.shapes.binary_products import category_theory.limits.preserves.shapes.pullbacks import topology.sheaves.functors import algebraic_geometry.Scheme import category_theory.limits.shapes.strict_initial import algebra.category.Ring.instances /-! # Open immersions of structured spaces We say that a morphism of presheafed spaces `f : X ⟶ Y` is an open immersions if the underlying map of spaces is an open embedding `f : X ⟶ U ⊆ Y`, and the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. Abbreviations are also provided for `SheafedSpace`, `LocallyRingedSpace` and `Scheme`. ## Main definitions * `algebraic_geometry.PresheafedSpace.is_open_immersion`: the `Prop`-valued typeclass asserting that a PresheafedSpace hom `f` is an open_immersion. * `algebraic_geometry.is_open_immersion`: the `Prop`-valued typeclass asserting that a Scheme morphism `f` is an open_immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.iso_restrict`: The source of an open immersion is isomorphic to the restriction of the target onto the image. * `algebraic_geometry.PresheafedSpace.is_open_immersion.lift`: Any morphism whose range is contained in an open immersion factors though the open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_SheafedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a sheafed space, then `X` is also a sheafed space. The morphism as morphisms of sheafed spaces is given by `to_SheafedSpace_hom`. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_LocallyRingedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a locally ringed space, then `X` is also a locally ringed space. The morphism as morphisms of locally ringed spaces is given by `to_LocallyRingedSpace_hom`. ## Main results * `algebraic_geometry.PresheafedSpace.is_open_immersion.comp`: The composition of two open immersions is an open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.of_iso`: An iso is an open immersion. * `algebraic_geometry.PresheafedSpace.is_open_immersion.to_iso`: A surjective open immersion is an isomorphism. * `algebraic_geometry.PresheafedSpace.is_open_immersion.stalk_iso`: An open immersion induces an isomorphism on stalks. * `algebraic_geometry.PresheafedSpace.is_open_immersion.has_pullback_of_left`: If `f` is an open immersion, then the pullback `(f, g)` exists (and the forgetful functor to `Top` preserves it). * `algebraic_geometry.PresheafedSpace.is_open_immersion.pullback_snd_of_left`: Open immersions are stable under pullbacks. * `algebraic_geometry.SheafedSpace.is_open_immersion.of_stalk_iso` An (topological) open embedding between two sheafed spaces is an open immersion if all the stalk maps are isomorphisms. -/ open topological_space category_theory opposite open category_theory.limits namespace algebraic_geometry universes v u variables {C : Type u} [category.{v} C] /-- An open immersion of PresheafedSpaces is an open embedding `f : X ⟶ U ⊆ Y` of the underlying spaces, such that the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. -/ class PresheafedSpace.is_open_immersion {X Y : PresheafedSpace C} (f : X ⟶ Y) : Prop := (base_open : open_embedding f.base) (c_iso : ∀ U : opens X, is_iso (f.c.app (op (base_open.is_open_map.functor.obj U)))) /-- A morphism of SheafedSpaces is an open immersion if it is an open immersion as a morphism of PresheafedSpaces -/ abbreviation SheafedSpace.is_open_immersion [has_products C] {X Y : SheafedSpace C} (f : X ⟶ Y) : Prop := PresheafedSpace.is_open_immersion f /-- A morphism of LocallyRingedSpaces is an open immersion if it is an open immersion as a morphism of SheafedSpaces -/ abbreviation LocallyRingedSpace.is_open_immersion {X Y : LocallyRingedSpace} (f : X ⟶ Y) : Prop := SheafedSpace.is_open_immersion f.1 /-- A morphism of Schemes is an open immersion if it is an open immersion as a morphism of LocallyRingedSpaces -/ abbreviation is_open_immersion {X Y : Scheme} (f : X ⟶ Y) : Prop := LocallyRingedSpace.is_open_immersion f namespace PresheafedSpace.is_open_immersion open PresheafedSpace local notation `is_open_immersion` := PresheafedSpace.is_open_immersion attribute [instance] is_open_immersion.c_iso section variables {X Y : PresheafedSpace C} {f : X ⟶ Y} (H : is_open_immersion f) /-- The functor `opens X ⥤ opens Y` associated with an open immersion `f : X ⟶ Y`. -/ abbreviation open_functor := H.base_open.is_open_map.functor /- We want to keep `eq_to_hom`s in the form of `F.map (eq_to_hom _)` so that the lemmas about naturality can be applied. -/ local attribute [-simp] eq_to_hom_map eq_to_iso_map /-- An open immersion `f : X ⟶ Y` induces an isomorphism `X ≅ Y|_{f(X)}`. -/ @[simps] noncomputable def iso_restrict : X ≅ Y.restrict H.base_open := PresheafedSpace.iso_of_components (iso.refl _) begin symmetry, fapply nat_iso.of_components, intro U, refine as_iso (f.c.app (op (H.open_functor.obj (unop U)))) ≪≫ X.presheaf.map_iso (eq_to_iso _), { induction U using opposite.rec, cases U, dsimp only [is_open_map.functor, functor.op, opens.map], congr' 2, erw set.preimage_image_eq _ H.base_open.inj, refl }, { intros U V i, simp only [category_theory.eq_to_iso.hom, Top.presheaf.pushforward_obj_map, category.assoc, functor.op_map, iso.trans_hom, as_iso_hom, functor.map_iso_hom, ←X.presheaf.map_comp], erw [f.c.naturality_assoc, ←X.presheaf.map_comp], congr } end @[simp] lemma iso_restrict_hom_of_restrict : H.iso_restrict.hom ≫ Y.of_restrict _ = f := begin ext, { simp only [comp_c_app, iso_restrict_hom_c_app, nat_trans.comp_app, eq_to_hom_refl, of_restrict_c_app, category.assoc, whisker_right_id'], erw [category.comp_id, f.c.naturality_assoc, ←X.presheaf.map_comp], transitivity f.c.app x ≫ X.presheaf.map (𝟙 _), { congr }, { erw [X.presheaf.map_id, category.comp_id] } }, { simp } end @[simp] lemma iso_restrict_inv_of_restrict : H.iso_restrict.inv ≫ f = Y.of_restrict _ := by { rw iso.inv_comp_eq, simp } instance mono [H : is_open_immersion f] : mono f := by { rw ← H.iso_restrict_hom_of_restrict, apply mono_comp } /-- The composition of two open immersions is an open immersion. -/ instance comp {Z : PresheafedSpace C} (f : X ⟶ Y) [hf : is_open_immersion f] (g : Y ⟶ Z) [hg : is_open_immersion g] : is_open_immersion (f ≫ g) := { base_open := hg.base_open.comp hf.base_open, c_iso := λ U, begin generalize_proofs h, dsimp only [algebraic_geometry.PresheafedSpace.comp_c_app, unop_op, functor.op, comp_base, Top.presheaf.pushforward_obj_obj, opens.map_comp_obj], apply_with is_iso.comp_is_iso { instances := ff }, swap, { have : (opens.map g.base).obj (h.functor.obj U) = hf.open_functor.obj U, { dsimp only [opens.map, is_open_map.functor, PresheafedSpace.comp_base], congr' 1, rw [coe_comp, ←set.image_image, set.preimage_image_eq _ hg.base_open.inj] }, rw this, apply_instance }, { have : h.functor.obj U = hg.open_functor.obj (hf.open_functor.obj U), { dsimp only [is_open_map.functor], congr' 1, rw [comp_base, coe_comp, ←set.image_image], congr }, rw this, apply_instance } end } /-- For an open immersion `f : X ⟶ Y` and an open set `U ⊆ X`, we have the map `X(U) ⟶ Y(U)`. -/ noncomputable def inv_app (U : opens X) : X.presheaf.obj (op U) ⟶ Y.presheaf.obj (op (H.open_functor.obj U)) := X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) ≫ inv (f.c.app (op (H.open_functor.obj U))) @[simp, reassoc] lemma inv_naturality {U V : (opens X)ᵒᵖ} (i : U ⟶ V) : X.presheaf.map i ≫ H.inv_app (unop V) = H.inv_app (unop U) ≫ Y.presheaf.map (H.open_functor.op.map i) := begin simp only [inv_app, ←category.assoc], rw [is_iso.comp_inv_eq], simp only [category.assoc, f.c.naturality, is_iso.inv_hom_id_assoc, ← X.presheaf.map_comp], erw ← X.presheaf.map_comp, congr end instance (U : opens X) : is_iso (H.inv_app U) := by { delta inv_app, apply_instance } lemma inv_inv_app (U : opens X) : inv (H.inv_app U) = f.c.app (op (H.open_functor.obj U)) ≫ X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) := begin rw ← cancel_epi (H.inv_app U), rw is_iso.hom_inv_id, delta inv_app, simp [← functor.map_comp] end @[simp, reassoc] lemma inv_app_app (U : opens X) : H.inv_app U ≫ f.c.app (op (H.open_functor.obj U)) = X.presheaf.map (eq_to_hom (by simp [opens.map, set.preimage_image_eq _ H.base_open.inj])) := by rw [inv_app, category.assoc, is_iso.inv_hom_id, category.comp_id] @[simp, reassoc] lemma app_inv_app (U : opens Y) : f.c.app (op U) ≫ H.inv_app ((opens.map f.base).obj U) = Y.presheaf.map ((hom_of_le (by exact set.image_preimage_subset f.base U)).op : op U ⟶ op (H.open_functor.obj ((opens.map f.base).obj U))) := by { erw ← category.assoc, rw [is_iso.comp_inv_eq, f.c.naturality], congr } /-- A variant of `app_inv_app` that gives an `eq_to_hom` instead of `hom_of_le`. -/ @[reassoc] lemma app_inv_app' (U : opens Y) (hU : (U : set Y) ⊆ set.range f.base) : f.c.app (op U) ≫ H.inv_app ((opens.map f.base).obj U) = Y.presheaf.map (eq_to_hom (by { apply has_le.le.antisymm, { exact set.image_preimage_subset f.base U.1 }, { change U ⊆ _, refine has_le.le.trans_eq _ (@set.image_preimage_eq_inter_range _ _ f.base U.1).symm, exact set.subset_inter_iff.mpr ⟨λ _ h, h, hU⟩ } })).op := by { erw ← category.assoc, rw [is_iso.comp_inv_eq, f.c.naturality], congr } /-- An isomorphism is an open immersion. -/ instance of_iso {X Y : PresheafedSpace C} (H : X ≅ Y) : is_open_immersion H.hom := { base_open := (Top.homeo_of_iso ((forget C).map_iso H)).open_embedding, c_iso := λ _, infer_instance } @[priority 100] instance of_is_iso {X Y : PresheafedSpace C} (f : X ⟶ Y) [is_iso f] : is_open_immersion f := algebraic_geometry.PresheafedSpace.is_open_immersion.of_iso (as_iso f) instance of_restrict {X : Top} (Y : PresheafedSpace C) {f : X ⟶ Y.carrier} (hf : open_embedding f) : is_open_immersion (Y.of_restrict hf) := { base_open := hf, c_iso := λ U, begin dsimp, have : (opens.map f).obj (hf.is_open_map.functor.obj U) = U, { cases U, dsimp only [opens.map, is_open_map.functor], congr' 1, rw set.preimage_image_eq _ hf.inj, refl }, convert (show is_iso (Y.presheaf.map (𝟙 _)), from infer_instance), { apply subsingleton.helim, rw this }, { rw Y.presheaf.map_id, apply_instance } end } /-- An open immersion is an iso if the underlying continuous map is epi. -/ lemma to_iso (f : X ⟶ Y) [h : is_open_immersion f] [h' : epi f.base] : is_iso f := begin apply_with is_iso_of_components { instances := ff }, { let : X ≃ₜ Y := (homeomorph.of_embedding _ h.base_open.to_embedding).trans { to_fun := subtype.val, inv_fun := λ x, ⟨x, by { rw set.range_iff_surjective.mpr ((Top.epi_iff_surjective _).mp h'), trivial }⟩, left_inv := λ ⟨_,_⟩, rfl, right_inv := λ _, rfl }, convert is_iso.of_iso (Top.iso_of_homeo this), { ext, refl } }, { apply_with nat_iso.is_iso_of_is_iso_app { instances := ff }, intro U, have : U = op (h.open_functor.obj ((opens.map f.base).obj (unop U))), { induction U using opposite.rec, cases U, dsimp only [functor.op, opens.map], congr, exact (set.image_preimage_eq _ ((Top.epi_iff_surjective _).mp h')).symm }, convert @@is_open_immersion.c_iso _ h ((opens.map f.base).obj (unop U)) } end instance stalk_iso [has_colimits C] [H : is_open_immersion f] (x : X) : is_iso (stalk_map f x) := begin rw ← H.iso_restrict_hom_of_restrict, rw PresheafedSpace.stalk_map.comp, apply_instance end end section pullback noncomputable theory variables {X Y Z : PresheafedSpace C} (f : X ⟶ Z) [hf : is_open_immersion f] (g : Y ⟶ Z) include hf /-- (Implementation.) The projection map when constructing the pullback along an open immersion. -/ def pullback_cone_of_left_fst : Y.restrict (Top.snd_open_embedding_of_left_open_embedding hf.base_open g.base) ⟶ X := { base := pullback.fst, c := { app := λ U, hf.inv_app (unop U) ≫ g.c.app (op (hf.base_open.is_open_map.functor.obj (unop U))) ≫ Y.presheaf.map (eq_to_hom (begin simp only [is_open_map.functor, subtype.mk_eq_mk, unop_op, op_inj_iff, opens.map, subtype.coe_mk, functor.op_obj, subtype.val_eq_coe], apply has_le.le.antisymm, { rintros _ ⟨_, h₁, h₂⟩, use (Top.pullback_iso_prod_subtype _ _).inv ⟨⟨_, _⟩, h₂⟩, simpa using h₁ }, { rintros _ ⟨x, h₁, rfl⟩, exact ⟨_, h₁, concrete_category.congr_hom pullback.condition x⟩ } end)), naturality' := begin intros U V i, induction U using opposite.rec, induction V using opposite.rec, simp only [quiver.hom.unop_op, Top.presheaf.pushforward_obj_map, category.assoc, nat_trans.naturality_assoc, functor.op_map, inv_naturality_assoc, ← Y.presheaf.map_comp], erw ← Y.presheaf.map_comp, congr end } } lemma pullback_cone_of_left_condition : pullback_cone_of_left_fst f g ≫ f = Y.of_restrict _ ≫ g := begin ext U, { induction U using opposite.rec, dsimp only [comp_c_app, nat_trans.comp_app, unop_op, whisker_right_app, pullback_cone_of_left_fst], simp only [quiver.hom.unop_op, Top.presheaf.pushforward_obj_map, app_inv_app_assoc, eq_to_hom_app, eq_to_hom_unop, category.assoc, nat_trans.naturality_assoc, functor.op_map], erw [← Y.presheaf.map_comp, ← Y.presheaf.map_comp], congr }, { simpa using pullback.condition } end /-- We construct the pullback along an open immersion via restricting along the pullback of the maps of underlying spaces (which is also an open embedding). -/ def pullback_cone_of_left : pullback_cone f g := pullback_cone.mk (pullback_cone_of_left_fst f g) (Y.of_restrict _) (pullback_cone_of_left_condition f g) variable (s : pullback_cone f g) /-- (Implementation.) Any cone over `cospan f g` indeed factors through the constructed cone. -/ def pullback_cone_of_left_lift : s.X ⟶ (pullback_cone_of_left f g).X := { base := pullback.lift s.fst.base s.snd.base (congr_arg (λ x, PresheafedSpace.hom.base x) s.condition), c := { app := λ U, s.snd.c.app _ ≫ s.X.presheaf.map (eq_to_hom (begin dsimp only [opens.map, is_open_map.functor, functor.op], congr' 2, let s' : pullback_cone f.base g.base := pullback_cone.mk s.fst.base s.snd.base _, have : _ = s.snd.base := limit.lift_π s' walking_cospan.right, conv_lhs { erw ← this, rw coe_comp, erw ← set.preimage_preimage }, erw set.preimage_image_eq _ (Top.snd_open_embedding_of_left_open_embedding hf.base_open g.base).inj, simp, end)), naturality' := λ U V i, begin erw s.snd.c.naturality_assoc, rw category.assoc, erw [← s.X.presheaf.map_comp, ← s.X.presheaf.map_comp], congr end } } -- this lemma is not a `simp` lemma, because it is an implementation detail lemma pullback_cone_of_left_lift_fst : pullback_cone_of_left_lift f g s ≫ (pullback_cone_of_left f g).fst = s.fst := begin ext x, { induction x using opposite.rec, change ((_ ≫ _) ≫ _ ≫ _) ≫ _ = _, simp_rw [category.assoc], erw ← s.X.presheaf.map_comp, erw s.snd.c.naturality_assoc, have := congr_app s.condition (op (hf.open_functor.obj x)), dsimp only [comp_c_app, unop_op] at this, rw ← is_iso.comp_inv_eq at this, reassoc! this, erw [← this, hf.inv_app_app_assoc, s.fst.c.naturality_assoc], simpa }, { change pullback.lift _ _ _ ≫ pullback.fst = _, simp } end -- this lemma is not a `simp` lemma, because it is an implementation detail lemma pullback_cone_of_left_lift_snd : pullback_cone_of_left_lift f g s ≫ (pullback_cone_of_left f g).snd = s.snd := begin ext x, { change (_ ≫ _ ≫ _) ≫ _ = _, simp_rw category.assoc, erw s.snd.c.naturality_assoc, erw [← s.X.presheaf.map_comp, ← s.X.presheaf.map_comp], transitivity s.snd.c.app x ≫ s.X.presheaf.map (𝟙 _), { congr }, { rw s.X.presheaf.map_id, erw category.comp_id } }, { change pullback.lift _ _ _ ≫ pullback.snd = _, simp } end instance pullback_cone_snd_is_open_immersion : is_open_immersion (pullback_cone_of_left f g).snd := begin erw category_theory.limits.pullback_cone.mk_snd, apply_instance end /-- The constructed pullback cone is indeed the pullback. -/ def pullback_cone_of_left_is_limit : is_limit (pullback_cone_of_left f g) := begin apply pullback_cone.is_limit_aux', intro s, use pullback_cone_of_left_lift f g s, use pullback_cone_of_left_lift_fst f g s, use pullback_cone_of_left_lift_snd f g s, intros m h₁ h₂, rw ← cancel_mono (pullback_cone_of_left f g).snd, exact (h₂.trans (pullback_cone_of_left_lift_snd f g s).symm) end instance has_pullback_of_left : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_left_is_limit f g⟩⟩⟩ instance has_pullback_of_right : has_pullback g f := has_pullback_symmetry f g /-- Open immersions are stable under base-change. -/ instance pullback_snd_of_left : is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, apply_instance end /-- Open immersions are stable under base-change. -/ instance pullback_fst_of_right : is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_one_is_open_immersion [is_open_immersion g] : is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw [←limit.w (cospan f g) walking_cospan.hom.inl, cospan_map_inl], apply_instance end instance forget_preserves_limits_of_left : preserves_limit (cospan f g) (forget C) := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit.postcompose_hom_equiv (diagram_iso_cospan.{v} _) _).to_fun, refine (is_limit.equiv_iso_limit _).to_fun (limit.is_limit (cospan f.base g.base)), fapply cones.ext, exact (iso.refl _), change ∀ j, _ = 𝟙 _ ≫ _ ≫ _, simp_rw category.id_comp, rintros (_|_|_); symmetry, { erw category.comp_id, exact limit.w (cospan f.base g.base) walking_cospan.hom.inl }, { exact category.comp_id _ }, { exact category.comp_id _ }, end instance forget_preserves_limits_of_right : preserves_limit (cospan g f) (forget C) := preserves_pullback_symmetry (forget C) f g lemma pullback_snd_is_iso_of_range_subset (H : set.range g.base ⊆ set.range f.base) : is_iso (pullback.snd : pullback f g ⟶ _) := begin haveI := Top.snd_iso_of_left_embedding_range_subset hf.base_open.to_embedding g.base H, haveI : is_iso (pullback.snd : pullback f g ⟶ _).base, { delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, change is_iso (_ ≫ pullback.snd), apply_instance }, apply to_iso end /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H : set.range g.base ⊆ set.range f.base) : Y ⟶ X := begin haveI := pullback_snd_is_iso_of_range_subset f g H, exact inv (pullback.snd : pullback f g ⟶ _) ≫ pullback.fst, end @[simp, reassoc] lemma lift_fac (H : set.range g.base ⊆ set.range f.base) : lift f g H ≫ f = g := by { erw category.assoc, rw is_iso.inv_comp_eq, exact pullback.condition } lemma lift_uniq (H : set.range g.base ⊆ set.range f.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H := by rw [← cancel_mono f, hl, lift_fac] /-- Two open immersions with equal range is isomorphic. -/ @[simps] def iso_of_range_eq [is_open_immersion g] (e : set.range f.base = set.range g.base) : X ≅ Y := { hom := lift g f (le_of_eq e), inv := lift f g (le_of_eq e.symm), hom_inv_id' := by { rw ← cancel_mono f, simp }, inv_hom_id' := by { rw ← cancel_mono g, simp } } end pullback open category_theory.limits.walking_cospan section to_SheafedSpace variables [has_products C] {X : PresheafedSpace C} (Y : SheafedSpace C) variables (f : X ⟶ Y.to_PresheafedSpace) [H : is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a SheafedSpace, then so is `X`. -/ def to_SheafedSpace : SheafedSpace C := { is_sheaf := begin apply Top.presheaf.is_sheaf_of_iso (sheaf_iso_of_iso H.iso_restrict.symm).symm, apply Top.sheaf.pushforward_sheaf_of_sheaf, exact (Y.restrict H.base_open).is_sheaf end, to_PresheafedSpace := X } @[simp] lemma to_SheafedSpace_to_PresheafedSpace : (to_SheafedSpace Y f).to_PresheafedSpace = X := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a SheafedSpace, we can upgrade it into a morphism of SheafedSpaces. -/ def to_SheafedSpace_hom : to_SheafedSpace Y f ⟶ Y := f @[simp] lemma to_SheafedSpace_hom_base : (to_SheafedSpace_hom Y f).base = f.base := rfl @[simp] lemma to_SheafedSpace_hom_c : (to_SheafedSpace_hom Y f).c = f.c := rfl instance to_SheafedSpace_is_open_immersion : SheafedSpace.is_open_immersion (to_SheafedSpace_hom Y f) := H omit H @[simp] lemma SheafedSpace_to_SheafedSpace {X Y : SheafedSpace C} (f : X ⟶ Y) [is_open_immersion f] : to_SheafedSpace Y f = X := by unfreezingI { cases X, refl } end to_SheafedSpace section to_LocallyRingedSpace variables {X : PresheafedSpace CommRing.{u}} (Y : LocallyRingedSpace.{u}) variables (f : X ⟶ Y.to_PresheafedSpace) [H : is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a LocallyRingedSpace, then so is `X`. -/ def to_LocallyRingedSpace : LocallyRingedSpace := { to_SheafedSpace := to_SheafedSpace Y.to_SheafedSpace f, local_ring := λ x, begin haveI : local_ring (Y.to_SheafedSpace.to_PresheafedSpace.stalk (f.base x)) := Y.local_ring _, exact (as_iso (stalk_map f x)).CommRing_iso_to_ring_equiv.local_ring end } @[simp] lemma to_LocallyRingedSpace_to_SheafedSpace : (to_LocallyRingedSpace Y f).to_SheafedSpace = (to_SheafedSpace Y.1 f) := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a LocallyRingedSpace, we can upgrade it into a morphism of LocallyRingedSpace. -/ def to_LocallyRingedSpace_hom : to_LocallyRingedSpace Y f ⟶ Y := ⟨f, λ x, infer_instance⟩ @[simp] lemma to_LocallyRingedSpace_hom_val : (to_LocallyRingedSpace_hom Y f).val = f := rfl instance to_LocallyRingedSpace_is_open_immersion : LocallyRingedSpace.is_open_immersion (to_LocallyRingedSpace_hom Y f) := H omit H @[simp] lemma LocallyRingedSpace_to_LocallyRingedSpace {X Y : LocallyRingedSpace} (f : X ⟶ Y) [LocallyRingedSpace.is_open_immersion f] : @to_LocallyRingedSpace X.to_PresheafedSpace Y (@@coe (@@coe_to_lift (@@coe_base coe_subtype)) f) (show is_open_immersion f.val, by apply_instance) = X := by unfreezingI { cases X, delta to_LocallyRingedSpace, simp } end to_LocallyRingedSpace end PresheafedSpace.is_open_immersion namespace SheafedSpace.is_open_immersion variables [has_products C] @[priority 100] instance of_is_iso {X Y : SheafedSpace C} (f : X ⟶ Y) [is_iso f] : SheafedSpace.is_open_immersion f := @@PresheafedSpace.is_open_immersion.of_is_iso _ f (SheafedSpace.forget_to_PresheafedSpace.map_is_iso _) instance comp {X Y Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) [SheafedSpace.is_open_immersion f] [SheafedSpace.is_open_immersion g] : SheafedSpace.is_open_immersion (f ≫ g) := PresheafedSpace.is_open_immersion.comp f g section pullback variables {X Y Z : SheafedSpace C} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : SheafedSpace.is_open_immersion f] include H local notation `forget` := SheafedSpace.forget_to_PresheafedSpace open category_theory.limits.walking_cospan instance : mono f := faithful_reflects_mono forget (show @mono (PresheafedSpace C) _ _ _ f, by apply_instance) instance forget_map_is_open_immersion : PresheafedSpace.is_open_immersion (forget .map f) := ⟨H.base_open, H.c_iso⟩ instance has_limit_cospan_forget_of_left : has_limit (cospan f g ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{v} _).symm, change has_limit (cospan (forget .map f) (forget .map g)), apply_instance end instance has_limit_cospan_forget_of_left' : has_limit (cospan ((cospan f g ⋙ forget).map hom.inl) ((cospan f g ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map f) (forget .map g)), from infer_instance instance has_limit_cospan_forget_of_right : has_limit (cospan g f ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{v} _).symm, change has_limit (cospan (forget .map g) (forget .map f)), apply_instance end instance has_limit_cospan_forget_of_right' : has_limit (cospan ((cospan g f ⋙ forget).map hom.inl) ((cospan g f ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map g) (forget .map f)), from infer_instance instance forget_creates_pullback_of_left : creates_limit (cospan f g) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_SheafedSpace Y (@pullback.snd (PresheafedSpace C) _ _ _ _ f g _)) (eq_to_iso (show pullback _ _ = pullback _ _, by congr) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_creates_pullback_of_right : creates_limit (cospan g f) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_SheafedSpace Y (@pullback.fst (PresheafedSpace C) _ _ _ _ g f _)) (eq_to_iso (show pullback _ _ = pullback _ _, by congr) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance SheafedSpace_forget_preserves_of_left : preserves_limit (cospan f g) (SheafedSpace.forget C) := @@limits.comp_preserves_limit _ _ _ _ forget (PresheafedSpace.forget C) _ begin apply_with (preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{v} _).symm) { instances := tt }, dsimp, apply_instance end instance SheafedSpace_forget_preserves_of_right : preserves_limit (cospan g f) (SheafedSpace.forget C) := preserves_pullback_symmetry _ _ _ instance SheafedSpace_has_pullback_of_left : has_pullback f g := has_limit_of_created (cospan f g) forget instance SheafedSpace_has_pullback_of_right : has_pullback g f := has_limit_of_created (cospan g f) forget /-- Open immersions are stable under base-change. -/ instance SheafedSpace_pullback_snd_of_left : SheafedSpace.is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, have : _ = limit.π (cospan f g) right := preserves_limits_iso_hom_π forget (cospan f g) right, rw ← this, have := has_limit.iso_of_nat_iso_hom_π (diagram_iso_cospan.{v} (cospan f g ⋙ forget)) right, erw category.comp_id at this, rw ← this, dsimp, apply_instance end instance SheafedSpace_pullback_fst_of_right : SheafedSpace.is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin delta pullback.fst, have : _ = limit.π (cospan g f) left := preserves_limits_iso_hom_π forget (cospan g f) left, rw ← this, have := has_limit.iso_of_nat_iso_hom_π (diagram_iso_cospan.{v} (cospan g f ⋙ forget)) left, erw category.comp_id at this, rw ← this, dsimp, apply_instance end instance SheafedSpace_pullback_one_is_open_immersion [SheafedSpace.is_open_immersion g] : SheafedSpace.is_open_immersion (limit.π (cospan f g) one : pullback f g ⟶ Z) := begin rw [←limit.w (cospan f g) hom.inl, cospan_map_inl], apply_instance end end pullback section of_stalk_iso variables [has_limits C] [has_colimits C] [concrete_category.{v} C] variables [reflects_isomorphisms (forget C)] [preserves_limits (forget C)] variables [preserves_filtered_colimits (forget C)] /-- Suppose `X Y : SheafedSpace C`, where `C` is a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then a morphism `X ⟶ Y` that is a topological open embedding is an open immersion iff every stalk map is an iso. -/ lemma of_stalk_iso {X Y : SheafedSpace C} (f : X ⟶ Y) (hf : open_embedding f.base) [H : ∀ x : X, is_iso (PresheafedSpace.stalk_map f x)] : SheafedSpace.is_open_immersion f := { base_open := hf, c_iso := λ U, begin apply_with (Top.presheaf.app_is_iso_of_stalk_functor_map_iso (show Y.sheaf ⟶ (Top.sheaf.pushforward f.base).obj X.sheaf, from f.c)) { instances := ff }, rintros ⟨_, y, hy, rfl⟩, specialize H y, delta PresheafedSpace.stalk_map at H, haveI H' := Top.presheaf.stalk_pushforward.stalk_pushforward_iso_of_open_embedding C hf X.presheaf y, have := @@is_iso.comp_is_iso _ H (@@is_iso.inv_is_iso _ H'), rw [category.assoc, is_iso.hom_inv_id, category.comp_id] at this, exact this end } end of_stalk_iso section prod variables [has_limits C] {ι : Type v} (F : discrete ι ⥤ SheafedSpace C) [has_colimit F] (i : ι) lemma sigma_ι_open_embedding : open_embedding (colimit.ι F i).base := begin rw ← (show _ = (colimit.ι F i).base, from ι_preserves_colimits_iso_inv (SheafedSpace.forget C) F i), have : _ = _ ≫ colimit.ι (discrete.functor (F ⋙ SheafedSpace.forget C).obj) i := has_colimit.iso_of_nat_iso_ι_hom discrete.nat_iso_functor i, rw ← iso.eq_comp_inv at this, rw this, have : colimit.ι _ _ ≫ _ = _ := Top.sigma_iso_sigma_hom_ι.{v} (F ⋙ SheafedSpace.forget C).obj i, rw ← iso.eq_comp_inv at this, rw this, simp_rw [← category.assoc, Top.open_embedding_iff_comp_is_iso, Top.open_embedding_iff_is_iso_comp], exact open_embedding_sigma_mk end lemma image_preimage_is_empty (j : ι) (h : i ≠ j) (U : opens (F.obj i)) : (opens.map (colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) j).base).obj ((opens.map (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv.base).obj ((sigma_ι_open_embedding F i).is_open_map.functor.obj U)) = ∅ := begin ext, apply iff_false_intro, rintro ⟨y, hy, eq⟩, replace eq := concrete_category.congr_arg (preserves_colimit_iso (SheafedSpace.forget C) F ≪≫ has_colimit.iso_of_nat_iso discrete.nat_iso_functor ≪≫ Top.sigma_iso_sigma.{v} _).hom eq, simp_rw [category_theory.iso.trans_hom, ← Top.comp_app, ← PresheafedSpace.comp_base] at eq, rw ι_preserves_colimits_iso_inv at eq, change ((SheafedSpace.forget C).map (colimit.ι F i) ≫ _) y = ((SheafedSpace.forget C).map (colimit.ι F j) ≫ _) x at eq, rw [ι_preserves_colimits_iso_hom_assoc, ι_preserves_colimits_iso_hom_assoc, has_colimit.iso_of_nat_iso_ι_hom_assoc, has_colimit.iso_of_nat_iso_ι_hom_assoc, Top.sigma_iso_sigma_hom_ι.{v}, Top.sigma_iso_sigma_hom_ι.{v}] at eq, exact h (congr_arg sigma.fst eq) end instance sigma_ι_is_open_immersion [has_strict_terminal_objects C] : SheafedSpace.is_open_immersion (colimit.ι F i) := { base_open := sigma_ι_open_embedding F i, c_iso := λ U, begin have e : colimit.ι F i = _ := (ι_preserves_colimits_iso_inv SheafedSpace.forget_to_PresheafedSpace F i).symm, have H : open_embedding (colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) i ≫ (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv).base := e ▸ sigma_ι_open_embedding F i, suffices : is_iso ((colimit.ι (F ⋙ SheafedSpace.forget_to_PresheafedSpace) i ≫ (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv).c.app (op (H.is_open_map.functor.obj U))), { convert this }, rw [PresheafedSpace.comp_c_app, ← PresheafedSpace.colimit_presheaf_obj_iso_componentwise_limit_hom_π], suffices : is_iso (limit.π (PresheafedSpace.componentwise_diagram (F ⋙ SheafedSpace.forget_to_PresheafedSpace) ((opens.map (preserves_colimit_iso SheafedSpace.forget_to_PresheafedSpace F).inv.base).obj (unop $ op $ H.is_open_map.functor.obj U))) (op i)), { resetI, apply_instance }, apply limit_π_is_iso_of_is_strict_terminal, intros j hj, induction j using opposite.rec, dsimp, convert (F.obj j).sheaf.is_terminal_of_empty, convert image_preimage_is_empty F i j (λ h, hj (congr_arg op h.symm)) U, exact (congr_arg PresheafedSpace.hom.base e).symm end } end prod end SheafedSpace.is_open_immersion namespace LocallyRingedSpace.is_open_immersion section pullback variables {X Y Z : LocallyRingedSpace.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : LocallyRingedSpace.is_open_immersion f] @[priority 100] instance of_is_iso [is_iso g] : LocallyRingedSpace.is_open_immersion g := @@PresheafedSpace.is_open_immersion.of_is_iso _ g.1 ⟨⟨(inv g).1, by { erw ← LocallyRingedSpace.comp_val, rw is_iso.hom_inv_id, erw ← LocallyRingedSpace.comp_val, rw is_iso.inv_hom_id, split; simpa }⟩⟩ include H instance comp (g : Z ⟶ Y) [LocallyRingedSpace.is_open_immersion g] : LocallyRingedSpace.is_open_immersion (f ≫ g) := PresheafedSpace.is_open_immersion.comp f.1 g.1 instance mono : mono f := faithful_reflects_mono (LocallyRingedSpace.forget_to_SheafedSpace) (show mono f.1, by apply_instance) instance : SheafedSpace.is_open_immersion (LocallyRingedSpace.forget_to_SheafedSpace.map f) := H /-- An explicit pullback cone over `cospan f g` if `f` is an open immersion. -/ def pullback_cone_of_left : pullback_cone f g := begin refine pullback_cone.mk _ (Y.of_restrict (Top.snd_open_embedding_of_left_open_embedding H.base_open g.1.base)) _, { use PresheafedSpace.is_open_immersion.pullback_cone_of_left_fst f.1 g.1, intro x, have := PresheafedSpace.stalk_map.congr_hom _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_condition f.1 g.1) x, rw [PresheafedSpace.stalk_map.comp, PresheafedSpace.stalk_map.comp] at this, rw ← is_iso.eq_inv_comp at this, rw this, apply_instance }, { exact subtype.eq (PresheafedSpace.is_open_immersion.pullback_cone_of_left_condition _ _) }, end instance : LocallyRingedSpace.is_open_immersion (pullback_cone_of_left f g).snd := show PresheafedSpace.is_open_immersion (Y.to_PresheafedSpace.of_restrict _), by apply_instance /-- The constructed `pullback_cone_of_left` is indeed limiting. -/ def pullback_cone_of_left_is_limit : is_limit (pullback_cone_of_left f g) := pullback_cone.is_limit_aux' _ $ λ s, begin use PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg subtype.val s.condition)), { intro x, have := PresheafedSpace.stalk_map.congr_hom _ _ (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg subtype.val s.condition))) x, change _ = _ ≫ PresheafedSpace.stalk_map s.snd.1 x at this, rw [PresheafedSpace.stalk_map.comp, ← is_iso.eq_inv_comp] at this, rw this, apply_instance }, split, exact subtype.eq (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_fst f.1 g.1 _), split, exact subtype.eq (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 _), intros m h₁ h₂, rw ← cancel_mono (pullback_cone_of_left f g).snd, exact (h₂.trans (subtype.eq (PresheafedSpace.is_open_immersion.pullback_cone_of_left_lift_snd f.1 g.1 (pullback_cone.mk s.fst.1 s.snd.1 (congr_arg subtype.val s.condition))).symm)) end instance has_pullback_of_left : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_left_is_limit f g⟩⟩⟩ instance has_pullback_of_right : has_pullback g f := has_pullback_symmetry f g /-- Open immersions are stable under base-change. -/ instance pullback_snd_of_left : LocallyRingedSpace.is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin delta pullback.snd, rw ← limit.iso_limit_cone_hom_π ⟨_, pullback_cone_of_left_is_limit f g⟩ walking_cospan.right, apply_instance end /-- Open immersions are stable under base-change. -/ instance pullback_fst_of_right : LocallyRingedSpace.is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_one_is_open_immersion [LocallyRingedSpace.is_open_immersion g] : LocallyRingedSpace.is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw [←limit.w (cospan f g) walking_cospan.hom.inl, cospan_map_inl], apply_instance end instance forget_preserves_pullback_of_left : preserves_limit (cospan f g) LocallyRingedSpace.forget_to_SheafedSpace := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit_map_cone_pullback_cone_equiv _ _).symm.to_fun, apply is_limit_of_is_limit_pullback_cone_map SheafedSpace.forget_to_PresheafedSpace, exact PresheafedSpace.is_open_immersion.pullback_cone_of_left_is_limit f.1 g.1 end instance forget_to_PresheafedSpace_preserves_pullback_of_left : preserves_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := preserves_limit_of_preserves_limit_cone (pullback_cone_of_left_is_limit f g) begin apply (is_limit_map_cone_pullback_cone_equiv _ _).symm.to_fun, exact PresheafedSpace.is_open_immersion.pullback_cone_of_left_is_limit f.1 g.1 end instance forget_to_PresheafedSpace_preserves_open_immersion : PresheafedSpace.is_open_immersion ((LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace).map f) := H instance forget_to_Top_preserves_pullback_of_left : preserves_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _) := begin change preserves_limit _ ((LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) ⋙ PresheafedSpace.forget _), apply_with limits.comp_preserves_limit { instances := ff }, apply_instance, apply preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{u} _).symm, dsimp [SheafedSpace.forget_to_PresheafedSpace, -subtype.val_eq_coe], apply_instance, end instance forget_reflects_pullback_of_left : reflects_limit (cospan f g) LocallyRingedSpace.forget_to_SheafedSpace := reflects_limit_of_reflects_isomorphisms _ _ instance forget_preserves_pullback_of_right : preserves_limit (cospan g f) LocallyRingedSpace.forget_to_SheafedSpace := preserves_pullback_symmetry _ _ _ instance forget_to_PresheafedSpace_preserves_pullback_of_right : preserves_limit (cospan g f) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := preserves_pullback_symmetry _ _ _ instance forget_reflects_pullback_of_right : reflects_limit (cospan g f) LocallyRingedSpace.forget_to_SheafedSpace := reflects_limit_of_reflects_isomorphisms _ _ instance forget_to_PresheafedSpace_reflects_pullback_of_left : reflects_limit (cospan f g) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := reflects_limit_of_reflects_isomorphisms _ _ instance forget_to_PresheafedSpace_reflects_pullback_of_right : reflects_limit (cospan g f) (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) := reflects_limit_of_reflects_isomorphisms _ _ lemma pullback_snd_is_iso_of_range_subset (H' : set.range g.1.base ⊆ set.range f.1.base) : is_iso (pullback.snd : pullback f g ⟶ _) := begin apply_with (reflects_isomorphisms.reflects LocallyRingedSpace.forget_to_SheafedSpace) { instances := ff }, apply_with (reflects_isomorphisms.reflects SheafedSpace.forget_to_PresheafedSpace) { instances := ff }, erw ← preserves_pullback.iso_hom_snd (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace) f g, haveI := PresheafedSpace.is_open_immersion.pullback_snd_is_iso_of_range_subset _ _ H', apply_instance, apply_instance end /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H' : set.range g.1.base ⊆ set.range f.1.base) : Y ⟶ X := begin haveI := pullback_snd_is_iso_of_range_subset f g H', exact inv (pullback.snd : pullback f g ⟶ _) ≫ pullback.fst, end @[simp, reassoc] lemma lift_fac (H' : set.range g.1.base ⊆ set.range f.1.base) : lift f g H' ≫ f = g := by { erw category.assoc, rw is_iso.inv_comp_eq, exact pullback.condition } lemma lift_uniq (H' : set.range g.1.base ⊆ set.range f.1.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H' := by rw [← cancel_mono f, hl, lift_fac] lemma lift_range (H' : set.range g.1.base ⊆ set.range f.1.base) : set.range (lift f g H').1.base = f.1.base ⁻¹' (set.range g.1.base) := begin haveI := pullback_snd_is_iso_of_range_subset f g H', dsimp only [lift], have : _ = (pullback.fst : pullback f g ⟶ _).val.base := preserves_pullback.iso_hom_fst (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _) f g, rw [LocallyRingedSpace.comp_val, SheafedSpace.comp_base, ← this, ← category.assoc, coe_comp], rw [set.range_comp, set.range_iff_surjective.mpr, set.image_univ, Top.pullback_fst_range], ext, split, { rintros ⟨y, eq⟩, exact ⟨y, eq.symm⟩ }, { rintros ⟨y, eq⟩, exact ⟨y, eq.symm⟩ }, { rw ← Top.epi_iff_surjective, rw (show (inv (pullback.snd : pullback f g ⟶ _)).val.base = _, from (LocallyRingedSpace.forget_to_SheafedSpace ⋙ SheafedSpace.forget _).map_inv _), apply_instance } end end pullback /-- An open immersion is isomorphic to the induced open subscheme on its image. -/ def iso_restrict {X Y : LocallyRingedSpace} {f : X ⟶ Y} (H : LocallyRingedSpace.is_open_immersion f) : X ≅ Y.restrict H.base_open := begin apply LocallyRingedSpace.iso_of_SheafedSpace_iso, refine SheafedSpace.forget_to_PresheafedSpace.preimage_iso _, exact H.iso_restrict end /-- To show that a locally ringed space is a scheme, it suffices to show that it has a jointly surjective family of open immersions from affine schemes. -/ protected def Scheme (X : LocallyRingedSpace) (h : ∀ (x : X), ∃ (R : CommRing) (f : Spec.to_LocallyRingedSpace.obj (op R) ⟶ X), (x ∈ set.range f.1.base : _) ∧ LocallyRingedSpace.is_open_immersion f) : Scheme := { to_LocallyRingedSpace := X, local_affine := begin intro x, obtain ⟨R, f, h₁, h₂⟩ := h x, refine ⟨⟨⟨_, h₂.base_open.open_range⟩, h₁⟩, R, ⟨_⟩⟩, apply LocallyRingedSpace.iso_of_SheafedSpace_iso, refine SheafedSpace.forget_to_PresheafedSpace.preimage_iso _, resetI, apply PresheafedSpace.is_open_immersion.iso_of_range_eq (PresheafedSpace.of_restrict _ _) f.1, { exact subtype.range_coe_subtype }, { apply_instance } end } end LocallyRingedSpace.is_open_immersion lemma is_open_immersion.open_range {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] : is_open (set.range f.1.base) := H.base_open.open_range section open_cover namespace Scheme /-- An open cover of `X` consists of a family of open immersions into `X`, and for each `x : X` an open immersion (indexed by `f x`) that covers `x`. This is merely a coverage in the Zariski pretopology, and it would be optimal if we could reuse the existing API about pretopologies, However, the definitions of sieves and grothendieck topologies uses `Prop`s, so that the actual open sets and immersions are hard to obtain. Also, since such a coverage in the pretopology usually contains a proper class of immersions, it is quite hard to glue them, reason about finite covers, etc. -/ -- TODO: provide API to and from a presieve. structure open_cover (X : Scheme.{u}) := (J : Type v) (obj : Π (j : J), Scheme) (map : Π (j : J), obj j ⟶ X) (f : X.carrier → J) (covers : ∀ x, x ∈ set.range ((map (f x)).1.base)) (is_open : ∀ x, is_open_immersion (map x) . tactic.apply_instance) attribute [instance] open_cover.is_open variables {X Y Z : Scheme.{u}} (𝒰 : open_cover X) (f : X ⟶ Z) (g : Y ⟶ Z) variables [∀ x, has_pullback (𝒰.map x ≫ f) g] /-- The affine cover of a scheme. -/ def affine_cover (X : Scheme) : open_cover X := { J := X.carrier, obj := λ x, Spec.obj $ opposite.op (X.local_affine x).some_spec.some, map := λ x, ((X.local_affine x).some_spec.some_spec.some.inv ≫ X.to_LocallyRingedSpace.of_restrict _ : _), f := λ x, x, is_open := λ x, begin apply_with PresheafedSpace.is_open_immersion.comp { instances := ff }, apply_instance, apply PresheafedSpace.is_open_immersion.of_restrict, end, covers := begin intro x, erw coe_comp, rw [set.range_comp, set.range_iff_surjective.mpr, set.image_univ], erw subtype.range_coe_subtype, exact (X.local_affine x).some.2, rw ← Top.epi_iff_surjective, change epi ((SheafedSpace.forget _).map (LocallyRingedSpace.forget_to_SheafedSpace.map _)), apply_instance end } instance : inhabited X.open_cover := ⟨X.affine_cover⟩ /-- Given an open cover `{ Uᵢ }` of `X`, and for each `Uᵢ` an open cover, we may combine these open covers to form an open cover of `X`. -/ @[simps J obj map] def open_cover.bind (f : Π (x : 𝒰.J), open_cover (𝒰.obj x)) : open_cover X := { J := Σ (i : 𝒰.J), (f i).J, obj := λ x, (f x.1).obj x.2, map := λ x, (f x.1).map x.2 ≫ 𝒰.map x.1, f := λ x, ⟨_, (f _).f (𝒰.covers x).some⟩, covers := λ x, begin let y := (𝒰.covers x).some, have hy : (𝒰.map (𝒰.f x)).val.base y = x := (𝒰.covers x).some_spec, rcases (f (𝒰.f x)).covers y with ⟨z, hz⟩, change x ∈ set.range (((f (𝒰.f x)).map ((f (𝒰.f x)).f y) ≫ 𝒰.map (𝒰.f x)).1.base), use z, erw comp_apply, rw [hz, hy], end } /-- An isomorphism `X ⟶ Y` is an open cover of `Y`. -/ @[simps J obj map] def open_cover_of_is_iso {X Y : Scheme.{u}} (f : X ⟶ Y) [is_iso f] : open_cover Y := { J := punit.{v+1}, obj := λ _, X, map := λ _, f, f := λ _, punit.star, covers := λ x, by { rw set.range_iff_surjective.mpr, { trivial }, rw ← Top.epi_iff_surjective, apply_instance } } /-- We construct an open cover from another, by providing the needed fields and showing that the provided fields are isomorphic with the original open cover. -/ @[simps J obj map] def open_cover.copy {X : Scheme} (𝒰 : open_cover X) (J : Type*) (obj : J → Scheme) (map : ∀ i, obj i ⟶ X) (e₁ : J ≃ 𝒰.J) (e₂ : ∀ i, obj i ≅ 𝒰.obj (e₁ i)) (e₂ : ∀ i, map i = (e₂ i).hom ≫ 𝒰.map (e₁ i)) : open_cover X := { J := J, obj := obj, map := map, f := λ x, e₁.symm (𝒰.f x), covers := λ x, begin rw [e₂, Scheme.comp_val_base, coe_comp, set.range_comp, set.range_iff_surjective.mpr, set.image_univ, e₁.right_inverse_symm], { exact 𝒰.covers x }, { rw ← Top.epi_iff_surjective, apply_instance } end, is_open := λ i, by { rw e₂, apply_instance } } /-- The pushforward of an open cover along an isomorphism. -/ @[simps J obj map] def open_cover.pushforward_iso {X Y : Scheme} (𝒰 : open_cover X) (f : X ⟶ Y) [is_iso f] : open_cover Y := ((open_cover_of_is_iso f).bind (λ _, 𝒰)).copy 𝒰.J _ _ ((equiv.punit_prod _).symm.trans (equiv.sigma_equiv_prod punit 𝒰.J).symm) (λ _, iso.refl _) (λ _, (category.id_comp _).symm) -- Related result : `open_cover.pullback_cover`, where we pullback an open cover on `X` along a -- morphism `W ⟶ X`. This is provided at the end of the file since it needs some more results -- about open immersion (which in turn needs the open cover API). local attribute [reducible] CommRing.of CommRing.of_hom instance val_base_is_iso {X Y : Scheme} (f : X ⟶ Y) [is_iso f] : is_iso f.1.base := Scheme.forget_to_Top.map_is_iso f instance basic_open_is_open_immersion {R : CommRing} (f : R) : algebraic_geometry.is_open_immersion (Scheme.Spec.map (CommRing.of_hom (algebra_map R (localization.away f))).op) := begin apply_with SheafedSpace.is_open_immersion.of_stalk_iso { instances := ff }, any_goals { apply_instance }, any_goals { apply_instance }, exact (prime_spectrum.localization_away_open_embedding (localization.away f) f : _), intro x, exact Spec_map_localization_is_iso R (submonoid.powers f) x, end /-- The basic open sets form an affine open cover of `Spec R`. -/ def affine_basis_cover_of_affine (R : CommRing) : open_cover (Spec.obj (opposite.op R)) := { J := R, obj := λ r, Spec.obj (opposite.op $ CommRing.of $ localization.away r), map := λ r, Spec.map (quiver.hom.op (algebra_map R (localization.away r) : _)), f := λ x, 1, covers := λ r, begin rw set.range_iff_surjective.mpr ((Top.epi_iff_surjective _).mp _), { exact trivial }, { apply_instance } end, is_open := λ x, algebraic_geometry.Scheme.basic_open_is_open_immersion x } /-- We may bind the basic open sets of an open affine cover to form a affine cover that is also a basis. -/ def affine_basis_cover (X : Scheme) : open_cover X := X.affine_cover.bind (λ x, affine_basis_cover_of_affine _) /-- The coordinate ring of a component in the `affine_basis_cover`. -/ def affine_basis_cover_ring (X : Scheme) (i : X.affine_basis_cover.J) : CommRing := CommRing.of $ @localization.away (X.local_affine i.1).some_spec.some _ i.2 lemma affine_basis_cover_obj (X : Scheme) (i : X.affine_basis_cover.J) : X.affine_basis_cover.obj i = Spec.obj (op $ X.affine_basis_cover_ring i) := rfl lemma affine_basis_cover_map_range (X : Scheme) (x : X.carrier) (r : (X.local_affine x).some_spec.some) : set.range (X.affine_basis_cover.map ⟨x, r⟩).1.base = (X.affine_cover.map x).1.base '' (prime_spectrum.basic_open r).1 := begin erw [coe_comp, set.range_comp], congr, exact (prime_spectrum.localization_away_comap_range (localization.away r) r : _) end lemma affine_basis_cover_is_basis (X : Scheme) : topological_space.is_topological_basis { x : set X.carrier | ∃ a : X.affine_basis_cover.J, x = set.range ((X.affine_basis_cover.map a).1.base) } := begin apply topological_space.is_topological_basis_of_open_of_nhds, { rintros _ ⟨a, rfl⟩, exact is_open_immersion.open_range (X.affine_basis_cover.map a) }, { rintros a U haU hU, rcases X.affine_cover.covers a with ⟨x, e⟩, let U' := (X.affine_cover.map (X.affine_cover.f a)).1.base ⁻¹' U, have hxU' : x ∈ U' := by { rw ← e at haU, exact haU }, rcases prime_spectrum.is_basis_basic_opens.exists_subset_of_mem_open hxU' ((X.affine_cover.map (X.affine_cover.f a)).1.base.continuous_to_fun.is_open_preimage _ hU) with ⟨_,⟨_,⟨s,rfl⟩,rfl⟩,hxV,hVU⟩, refine ⟨_,⟨⟨_,s⟩,rfl⟩,_,_⟩; erw affine_basis_cover_map_range, { exact ⟨x,hxV,e⟩ }, { rw set.image_subset_iff, exact hVU } } end /-- Every open cover of a quasi-compact scheme can be refined into a finite subcover. -/ @[simps obj map] def open_cover.finite_subcover {X : Scheme} (𝒰 : open_cover X) [H : compact_space X.carrier] : open_cover X := begin have := @@compact_space.elim_nhds_subcover _ H (λ (x : X.carrier), set.range ((𝒰.map (𝒰.f x)).1.base)) (λ x, (is_open_immersion.open_range (𝒰.map (𝒰.f x))).mem_nhds (𝒰.covers x)), let t := this.some, have h : ∀ (x : X.carrier), ∃ (y : t), x ∈ set.range ((𝒰.map (𝒰.f y)).1.base), { intro x, have h' : x ∈ (⊤ : set X.carrier) := trivial, rw [← classical.some_spec this, set.mem_Union] at h', rcases h' with ⟨y,_,⟨hy,rfl⟩,hy'⟩, exact ⟨⟨y,hy⟩,hy'⟩ }, exact { J := t, obj := λ x, 𝒰.obj (𝒰.f x.1), map := λ x, 𝒰.map (𝒰.f x.1), f := λ x, (h x).some, covers := λ x, (h x).some_spec } end instance [H : compact_space X.carrier] : fintype 𝒰.finite_subcover.J := by { delta open_cover.finite_subcover, apply_instance } end Scheme end open_cover namespace PresheafedSpace.is_open_immersion section to_Scheme variables {X : PresheafedSpace CommRing.{u}} (Y : Scheme.{u}) variables (f : X ⟶ Y.to_PresheafedSpace) [H : PresheafedSpace.is_open_immersion f] include H /-- If `X ⟶ Y` is an open immersion, and `Y` is a scheme, then so is `X`. -/ def to_Scheme : Scheme := begin apply LocallyRingedSpace.is_open_immersion.Scheme (to_LocallyRingedSpace _ f), intro x, obtain ⟨_,⟨i,rfl⟩,hx,hi⟩ := Y.affine_basis_cover_is_basis.exists_subset_of_mem_open (set.mem_range_self x) H.base_open.open_range, use Y.affine_basis_cover_ring i, use LocallyRingedSpace.is_open_immersion.lift (to_LocallyRingedSpace_hom _ f) _ hi, split, { rw LocallyRingedSpace.is_open_immersion.lift_range, exact hx }, { delta LocallyRingedSpace.is_open_immersion.lift, apply_instance } end @[simp] lemma to_Scheme_to_LocallyRingedSpace : (to_Scheme Y f).to_LocallyRingedSpace = (to_LocallyRingedSpace Y.1 f) := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a Scheme, we can upgrade it into a morphism of Schemes. -/ def to_Scheme_hom : to_Scheme Y f ⟶ Y := to_LocallyRingedSpace_hom _ f @[simp] lemma to_Scheme_hom_val : (to_Scheme_hom Y f).val = f := rfl instance to_Scheme_hom_is_open_immersion : is_open_immersion (to_Scheme_hom Y f) := H omit H lemma Scheme_eq_of_LocallyRingedSpace_eq {X Y : Scheme} (H : X.to_LocallyRingedSpace = Y.to_LocallyRingedSpace) : X = Y := by { cases X, cases Y, congr, exact H } lemma Scheme_to_Scheme {X Y : Scheme} (f : X ⟶ Y) [is_open_immersion f] : to_Scheme Y f.1 = X := begin apply Scheme_eq_of_LocallyRingedSpace_eq, exact LocallyRingedSpace_to_LocallyRingedSpace f end end to_Scheme end PresheafedSpace.is_open_immersion /-- The restriction of a Scheme along an open embedding. -/ @[simps] def Scheme.restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : Scheme := { to_PresheafedSpace := X.to_PresheafedSpace.restrict h, ..(PresheafedSpace.is_open_immersion.to_Scheme X (X.to_PresheafedSpace.of_restrict h)) } /-- The canonical map from the restriction to the supspace. -/ @[simps] def Scheme.of_restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : X.restrict h ⟶ X := X.to_LocallyRingedSpace.of_restrict h instance is_open_immersion.of_restrict {U : Top} (X : Scheme) {f : U ⟶ Top.of X.carrier} (h : open_embedding f) : is_open_immersion (X.of_restrict h) := show PresheafedSpace.is_open_immersion (X.to_PresheafedSpace.of_restrict h), by apply_instance namespace is_open_immersion variables {X Y Z : Scheme.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : is_open_immersion f] @[priority 100] instance of_is_iso [is_iso g] : is_open_immersion g := @@LocallyRingedSpace.is_open_immersion.of_is_iso _ (show is_iso ((induced_functor _).map g), by apply_instance) /-- A open immersion induces an isomorphism from the domain onto the image -/ def iso_restrict : X ≅ (Z.restrict H.base_open : _) := ⟨H.iso_restrict.hom, H.iso_restrict.inv, H.iso_restrict.hom_inv_id, H.iso_restrict.inv_hom_id⟩ include H local notation `forget` := Scheme.forget_to_LocallyRingedSpace instance mono : mono f := faithful_reflects_mono (induced_functor _) (show @mono LocallyRingedSpace _ _ _ f, by apply_instance) instance forget_map_is_open_immersion : LocallyRingedSpace.is_open_immersion (forget .map f) := ⟨H.base_open, H.c_iso⟩ instance has_limit_cospan_forget_of_left : has_limit (cospan f g ⋙ Scheme.forget_to_LocallyRingedSpace) := begin apply has_limit_of_iso (diagram_iso_cospan.{u} _).symm, change has_limit (cospan (forget .map f) (forget .map g)), apply_instance end open category_theory.limits.walking_cospan instance has_limit_cospan_forget_of_left' : has_limit (cospan ((cospan f g ⋙ forget).map hom.inl) ((cospan f g ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map f) (forget .map g)), from infer_instance instance has_limit_cospan_forget_of_right : has_limit (cospan g f ⋙ forget) := begin apply has_limit_of_iso (diagram_iso_cospan.{u} _).symm, change has_limit (cospan (forget .map g) (forget .map f)), apply_instance end instance has_limit_cospan_forget_of_right' : has_limit (cospan ((cospan g f ⋙ forget).map hom.inl) ((cospan g f ⋙ forget).map hom.inr)) := show has_limit (cospan (forget .map g) (forget .map f)), from infer_instance instance forget_creates_pullback_of_left : creates_limit (cospan f g) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_Scheme Y (@pullback.snd LocallyRingedSpace _ _ _ _ f g _).1) (eq_to_iso (by simp) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_creates_pullback_of_right : creates_limit (cospan g f) forget := creates_limit_of_fully_faithful_of_iso (PresheafedSpace.is_open_immersion.to_Scheme Y (@pullback.fst LocallyRingedSpace _ _ _ _ g f _).1) (eq_to_iso (by simp) ≪≫ has_limit.iso_of_nat_iso (diagram_iso_cospan _).symm) instance forget_preserves_of_left : preserves_limit (cospan f g) forget := category_theory.preserves_limit_of_creates_limit_and_has_limit _ _ instance forget_preserves_of_right : preserves_limit (cospan g f) forget := preserves_pullback_symmetry _ _ _ instance has_pullback_of_left : has_pullback f g := has_limit_of_created (cospan f g) forget instance has_pullback_of_right : has_pullback g f := has_limit_of_created (cospan g f) forget instance pullback_snd_of_left : is_open_immersion (pullback.snd : pullback f g ⟶ _) := begin have := preserves_pullback.iso_hom_snd forget f g, dsimp only [Scheme.forget_to_LocallyRingedSpace, induced_functor_map] at this, rw ← this, change LocallyRingedSpace.is_open_immersion _, apply_instance end instance pullback_fst_of_right : is_open_immersion (pullback.fst : pullback g f ⟶ _) := begin rw ← pullback_symmetry_hom_comp_snd, apply_instance end instance pullback_one [is_open_immersion g] : is_open_immersion (limit.π (cospan f g) walking_cospan.one) := begin rw ← limit.w (cospan f g) walking_cospan.hom.inl, change is_open_immersion (_ ≫ f), apply_instance end instance forget_to_Top_preserves_of_left : preserves_limit (cospan f g) Scheme.forget_to_Top := begin apply_with limits.comp_preserves_limit { instances := ff }, apply_instance, apply preserves_limit_of_iso_diagram _ (diagram_iso_cospan.{u} _).symm, dsimp [LocallyRingedSpace.forget_to_Top], apply_instance end instance forget_to_Top_preserves_of_right : preserves_limit (cospan g f) Scheme.forget_to_Top := preserves_pullback_symmetry _ _ _ /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H' : set.range g.1.base ⊆ set.range f.1.base) : Y ⟶ X := LocallyRingedSpace.is_open_immersion.lift f g H' @[simp, reassoc] lemma lift_fac (H' : set.range g.1.base ⊆ set.range f.1.base) : lift f g H' ≫ f = g := LocallyRingedSpace.is_open_immersion.lift_fac f g H' lemma lift_uniq (H' : set.range g.1.base ⊆ set.range f.1.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H' := LocallyRingedSpace.is_open_immersion.lift_uniq f g H' l hl /-- Two open immersions with equal range is isomorphic. -/ @[simps] def iso_of_range_eq [is_open_immersion g] (e : set.range f.1.base = set.range g.1.base) : X ≅ Y := { hom := lift g f (le_of_eq e), inv := lift f g (le_of_eq e.symm), hom_inv_id' := by { rw ← cancel_mono f, simp }, inv_hom_id' := by { rw ← cancel_mono g, simp } } end is_open_immersion /-- Given an open cover on `X`, we may pull them back along a morphism `W ⟶ X` to obtain an open cover of `W`. -/ @[simps] def Scheme.open_cover.pullback_cover {X : Scheme} (𝒰 : X.open_cover) {W : Scheme} (f : W ⟶ X) : W.open_cover := { J := 𝒰.J, obj := λ x, pullback f (𝒰.map x), map := λ x, pullback.fst, f := λ x, 𝒰.f (f.1.base x), covers := λ x, begin rw ← (show _ = (pullback.fst : pullback f (𝒰.map (𝒰.f (f.1.base x))) ⟶ _).1.base, from preserves_pullback.iso_hom_fst Scheme.forget_to_Top f (𝒰.map (𝒰.f (f.1.base x)))), rw [coe_comp, set.range_comp, set.range_iff_surjective.mpr, set.image_univ, Top.pullback_fst_range], obtain ⟨y, h⟩ := 𝒰.covers (f.1.base x), exact ⟨y, h.symm⟩, { rw ← Top.epi_iff_surjective, apply_instance } end } end algebraic_geometry
d5168bf354a3f1307a883c676086452336f55bcc
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/linear_algebra/basic.lean
12943f21f841a664cb0b6eb2ba4172e9f4302c0c
[ "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
62,572
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, Kevin Buzzard Basics of linear algebra. This sets up the "categorical/lattice structure" of modules, submodules, and linear maps. -/ import algebra.pi_instances data.finsupp data.equiv.algebra order.order_iso open function lattice reserve infix ` ≃ₗ `:25 universes u v w x y z variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ε : Type z} {ι : Type x} namespace finset lemma smul_sum [ring γ] [add_comm_group β] [module γ β] {s : finset α} {a : γ} {f : α → β} : a • (s.sum f) = s.sum (λc, a • f c) := (finset.sum_hom ((•) a)).symm end finset namespace finsupp lemma smul_sum [has_zero β] [ring γ] [add_comm_group δ] [module γ δ] {v : α →₀ β} {c : γ} {h : α → β → δ} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp namespace linear_map section variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [add_comm_group ε] variables [module α β] [module α γ] [module α δ] [module α ε] variables (f g : β →ₗ[α] γ) include α @[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 theorem comp_assoc (g : γ →ₗ[α] δ) (h : δ →ₗ[α] ε) : (h.comp g).comp f = h.comp (g.comp f) := rfl def cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h : ∀c, f c ∈ p) : γ →ₗ[α] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule α β) (f : γ →ₗ[α] β) {h} (x : γ) : (cod_restrict p f h x : β) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) (g : δ →ₗ[α] β) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl def inverse (g : γ → β) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : γ →ₗ[α] β := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ instance : has_zero (β →ₗ[α] γ) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ @[simp] lemma zero_apply (x : β) : (0 : β →ₗ[α] γ) x = 0 := rfl instance : has_neg (β →ₗ[α] γ) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩ @[simp] lemma neg_apply (x : β) : (- f) x = - f x := rfl instance : has_add (β →ₗ[α] γ) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : β) : (f + g) x = f x + g x := rfl instance : add_comm_group (β →ₗ[α] γ) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp instance linear_map.is_add_group_hom : is_add_group_hom f := { map_add := f.add } instance linear_map_apply_is_add_group_hom (a : β) : is_add_group_hom (λ f : β →ₗ[α] γ, f a) := { map_add := λ f g, linear_map.add_apply f g a } lemma sum_apply [decidable_eq δ] (t : finset δ) (f : δ → β →ₗ[α] γ) (b : β) : t.sum f b = t.sum (λd, f d b) := (@finset.sum_hom _ _ _ t f _ _ (λ g : β →ₗ[α] γ, g b) _).symm @[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl def smul_right (f : γ →ₗ[α] α) (x : β) : γ →ₗ[α] β := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : γ →ₗ[α] α) (x : β) (c : γ) : (smul_right f x : γ → β) c = f c • x := rfl instance : has_one (β →ₗ[α] β) := ⟨linear_map.id⟩ instance : has_mul (β →ₗ[α] β) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : β) : (1 : β →ₗ[α] β) x = x := rfl @[simp] lemma mul_app (A B : β →ₗ[α] β) (x : β) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : δ →ₗ[α] β) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : γ →ₗ[α] δ).comp f = 0 := rfl section variables (α β) include β instance endomorphism_ring : ring (β →ₗ[α] β) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp } end section variables (α β γ) def fst : β × γ →ₗ[α] β := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ def snd : β × γ →ₗ[α] γ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : β × γ) : fst α β γ x = x.1 := rfl @[simp] theorem snd_apply (x : β × γ) : snd α β γ x = x.2 := rfl def pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : β →ₗ[α] γ × δ := ⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩ @[simp] theorem pair_apply (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (x : β) : pair f g x = (f x, g x) := rfl @[simp] theorem fst_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (fst α γ δ).comp (pair f g) = f := by ext; refl @[simp] theorem snd_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (snd α γ δ).comp (pair f g) = g := by ext; refl @[simp] theorem pair_fst_snd : pair (fst α β γ) (snd α β γ) = linear_map.id := by ext; refl section variables (α β γ) def inl : β →ₗ[α] β × γ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] def inr : γ →ₗ[α] β × γ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : β) : inl α β γ x = (x, 0) := rfl @[simp] theorem inr_apply (x : γ) : inr α β γ x = (0, x) := rfl def copair (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : β × γ →ₗ[α] δ := ⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩ @[simp] theorem copair_apply (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (x : β) (y : γ) : copair f g (x, y) = f x + g y := rfl @[simp] theorem copair_inl (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inl α β γ) = f := by ext; simp @[simp] theorem copair_inr (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inr α β γ) = g := by ext; simp @[simp] theorem copair_inl_inr : copair (inl α β γ) (inr α β γ) = linear_map.id := by ext ⟨x, y⟩; simp theorem fst_eq_copair : fst α β γ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_copair : snd α β γ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_pair : inl α β γ = pair linear_map.id 0 := rfl theorem inr_eq_pair : inr α β γ = pair 0 linear_map.id := rfl end section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α instance : has_scalar α (β →ₗ[α] γ) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : α) (x : β) : (a • f) x = a • f x := rfl instance : module α (β →ₗ[α] γ) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] def congr_right (f : γ →ₗ[α] δ) : (β →ₗ[α] γ) →ₗ[α] (β →ₗ[α] δ) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ theorem smul_comp (g : γ →ₗ[α] δ) (a : α) : (a • g).comp f = a • (g.comp f) := rfl theorem comp_smul (g : γ →ₗ[α] δ) (a : α) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl end comm_ring end linear_map namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (p p' : submodule α β) (q q' : submodule α γ) variables {r : α} {x y : β} open set lattice instance : partial_order (submodule α β) := partial_order.lift (coe : submodule α β → set β) (λ a b, ext') (by apply_instance) lemma le_def {p p' : submodule α β} : p ≤ p' ↔ (p : set β) ⊆ p' := iff.rfl lemma le_def' {p p' : submodule α β} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl def of_le {p p' : submodule α β} (h : p ≤ p') : p →ₗ[α] p' := linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx @[simp] theorem of_le_apply {p p' : submodule α β} (h : p ≤ p') (x : p) : (of_le h x : β) = x := rfl lemma subtype_comp_of_le (p q : submodule α β) (h : p ≤ q) : (submodule.subtype q).comp (of_le h) = submodule.subtype p := by ext ⟨b, hb⟩; simp instance : has_bot (submodule α β) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ @[simp] lemma bot_coe : ((⊥ : submodule α β) : set β) = {0} := rfl section variables (α) @[simp] lemma mem_bot : x ∈ (⊥ : submodule α β) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule α β) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } instance : has_top (submodule α β) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule α β) : set β) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule α β) := trivial lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : α) = 1) : p = ⊥ := by ext x; simp [semimodule.eq_zero_of_zero_eq_one _ x zero_eq_one] instance : order_top (submodule α β) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule α β) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule α β)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule α β)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule α β) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule α β) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.lattice.order_top, ..submodule.lattice.order_bot } instance : add_comm_monoid (submodule α β) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (M N : submodule α β) : M + N = M ⊔ N := rfl @[simp] lemma zero_eq_bot : (0 : submodule α β) = ⊥ := rfl lemma eq_top_iff' {p : submodule α β} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set β) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule α β} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule α β)) : (↑(Inf P) : set β) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule α β) : (↑⨅ i, p i : set β) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule α β) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule α β} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:β) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set β)) ↔ _, by simp /-- The pushforward -/ def map (f : β →ₗ[α] γ) (p : submodule α β) : submodule α γ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } lemma map_coe (f : β →ₗ[α] γ) (p : submodule α β) : (map f p : set γ) = f '' p := rfl @[simp] lemma mem_map {f : β →ₗ[α] γ} {p : submodule α β} {x : γ} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : β →ₗ[α] γ} {p : submodule α β} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α β) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : β →ₗ[α] γ} {p p' : submodule α β} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : β →ₗ[α] γ) p = ⊥ := have ∃ (x : β), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback -/ def comap (f : β →ₗ[α] γ) (p : submodule α γ) : submodule α β := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : β →ₗ[α] γ) (p : submodule α γ) : (comap f p : set β) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : β →ₗ[α] γ} {p : submodule α γ} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α δ) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : β →ₗ[α] γ} {q q' : submodule α γ} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : β →ₗ[α] γ} {p : submodule α β} {q : submodule α γ} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : β →ₗ[α] γ) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : β →ₗ[α] γ) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : β →ₗ[α] γ) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α β) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : β →ₗ[α] γ) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : β →ₗ[α] γ) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α γ) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : β →ₗ[α] γ) q = ⊤ := ext $ by simp lemma map_comap_le (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : β →ₗ[α] γ) (p : submodule α β) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : β →ₗ[α] γ} {p : submodule α β} {p' : submodule α γ} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule α β)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot α).1 hb section variables (α) def span (s : set β) : submodule α β := Inf {p | s ⊆ p} end variables {s t : set β} lemma mem_span : x ∈ span α s ↔ ∀ p : submodule α β, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span α s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span α s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span α s ≤ span α t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span α s) : span α s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span α (p : set β) = p := span_eq_of_le _ (subset.refl _) subset_span @[elab_as_eliminator] lemma span_induction {p : β → Prop} (h : x ∈ span α s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:α) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (α β) protected def gi : galois_insertion (@span α β _ _ _) coe := { choice := λ s _, span α s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span α (∅ : set β) = ⊥ := (submodule.gi α β).gc.l_bot @[simp] lemma span_univ : span α (univ : set β) = ⊤ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set β) : span α (s ∪ t) = span α s ⊔ span α t := (submodule.gi α β).gc.l_sup lemma span_Union {ι} (s : ι → set β) : span α (⋃ i, s i) = ⨆ i, span α (s i) := (submodule.gi α β).gc.l_supr @[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) : ((supr S : submodule α β) : set β) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), rw [show supr S = ⨆ i, span α (S i), by simp, ← span_Union], unfreezeI, refine λ x hx, span_induction hx (λ _, id) _ _ _, { cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ }, { simp, intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_supr_of_mem {ι : Sort*} {b : β} (p : ι → submodule α β) (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h @[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl theorem mem_Sup_of_directed {s : set (submodule α β)} {z} (hzs : z ∈ Sup s) (x ∈ s) (hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) : ∃ y ∈ s, z ∈ y := begin haveI := classical.dec, rw Sup_eq_supr at hzs, have : ∃ (i : submodule α β), z ∈ ⨆ (H : i ∈ s), i, { refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs, by_cases his : i ∈ s; by_cases hjs : j ∈ s, { rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩, exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik), le_supr_of_le hks (supr_le $ λ _, hjk)⟩ }, { exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ }, { exact ⟨j, supr_le $ his.elim, le_refl _⟩ }, { exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } }, cases this with N hzn, by_cases hns : N ∈ s, { have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _), exact ⟨N, hns, this hzn⟩ }, { have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim, cases (mem_bot α).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ } end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ end lemma mem_span_singleton {y : β} : x ∈ span α ({y} : set β) ↔ ∃ a:α, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : β) : (span α ({y} : set β) : set β) = range ((• y) : α → β) := set.ext $ λ x, mem_span_singleton lemma mem_span_insert {y} : x ∈ span α (insert y s) ↔ ∃ (a:α) (z ∈ span α s), x = a • y + z := begin rw [← union_singleton, span_union, mem_sup], simp [mem_span_singleton], split, { rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ }, { rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ } end lemma mem_span_insert' {y} : x ∈ span α (insert y s) ↔ ∃(a:α), x + a • y ∈ span α s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ } end lemma span_insert_eq_span (h : x ∈ span α s) : span α (insert x s) = span α s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span α (span α s : set β) = span α s := span_eq _ lemma span_eq_bot : span α (s : set β) = ⊥ ↔ ∀ x ∈ s, (x:β) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot α).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot α).2 $ H x h)⟩ lemma span_singleton_eq_bot : span α ({x} : set β) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : β →ₗ[α] γ) : span α (f '' s) = map f (span α s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma linear_eq_on (s : set β) {f g : β →ₗ[α] γ} (H : ∀x∈s, f x = g x) {x} (h : x ∈ span α s) : f x = g x := by apply span_induction h H; simp {contextual := tt} def prod : submodule α (β × γ) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (β × γ)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule α β} {q : submodule α γ} {x : β × γ} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set β) (t : set γ) : span α (set.prod s t) ≤ prod (span α s) (span α t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule α (β × γ)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule α (β × γ)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule α β} {q q' : submodule α γ} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup def quotient_rel : setoid β := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩ def quotient : Type* := quotient (quotient_rel p) namespace quotient def mk {p : submodule α β} : β → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule α β} (x : β) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule α β} (x : β) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule α β} (x : β) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : β} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm] instance : has_scalar α (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module α (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] instance {α β} {R:discrete_field α} [add_comm_group β] [vector_space α β] (p : submodule α β) : vector_space α (quotient p) := {} end quotient end submodule namespace submodule variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma comap_smul (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : β →ₗ[α] γ) (p : submodule α β) (a : α) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end set_option class.instance_max_depth 40 lemma comap_smul' (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : β →ₗ[α] γ) (p : submodule α β) (a : α) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by by_cases a = 0; simp [h, map_smul] end submodule namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open submodule @[simp] lemma finsupp_sum {α β γ δ} [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] [has_zero δ] (f : β →ₗ[α] γ) {t : ι →₀ δ} {g : ι → δ → β} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ def range (f : β →ₗ[α] γ) : submodule α γ := map f ⊤ theorem range_coe (f : β →ₗ[α] γ) : (range f : set γ) = set.range f := set.image_univ @[simp] theorem mem_range {f : β →ₗ[α] γ} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := (set.ext_iff _ _).1 (range_coe f). @[simp] theorem range_id : range (linear_map.id : β →ₗ[α] β) = ⊤ := map_id _ theorem range_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : β →ₗ[α] γ} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : β →ₗ[α] γ} {p : submodule α γ} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : β →ₗ[α] γ} {p : submodule α β} : map f p ≤ range f := map_mono le_top lemma sup_range_inl_inr : (inl α β γ).range ⊔ (inr α β γ).range = ⊤ := begin refine eq_top_iff'.2 (λ x, mem_sup.2 _), rcases x with ⟨x₁, x₂⟩ , have h₁ : prod.mk x₁ (0 : γ) ∈ (inl α β γ).range, by simp, have h₂ : prod.mk (0 : β) x₂ ∈ (inr α β γ).range, by simp, use [⟨x₁, 0⟩, h₁, ⟨0, x₂⟩, h₂], simp end def ker (f : β →ₗ[α] γ) : submodule α β := comap f ⊥ @[simp] theorem mem_ker {f : β →ₗ[α] γ} {y} : y ∈ ker f ↔ f y = 0 := mem_bot α @[simp] theorem ker_id : ker (linear_map.id : β →ₗ[α] β) = ⊥ := rfl theorem ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : β →ₗ[α] γ} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} {s : set β} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) lemma disjoint_inl_inr : disjoint (inl α β γ).range (inr α β γ).range := by simp [disjoint_def, @eq_comm β 0, @eq_comm γ 0] {contextual := tt}; intros; refl theorem ker_eq_bot {f : β →ₗ[α] γ} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot' {f : β →ₗ[α] γ} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := have h : (∀ m ∈ (⊤ : submodule α β), f m = 0 → m = 0) ↔ (∀ m, f m = 0 → m = 0), from ⟨λ h m, h m mem_top, λ h m _, h m⟩, by simpa [h, disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : β →ₗ[α] γ} {p : submodule α β} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : β →ₗ[α] γ} {q : submodule α γ} (h : q ≤ range f) : map f (comap f q) = q := by rw [map_comap_eq, inf_of_le_right h] lemma comap_map_eq (f : β →ₗ[α] γ) (p : submodule α β) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : β →ₗ[α] γ} {p : submodule α β} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] @[simp] theorem ker_zero : ker (0 : β →ₗ[α] γ) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : β →ₗ[α] γ) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : β →ₗ[α] γ} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : β →ₗ[α] γ) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem map_le_map_iff {f : β →ₗ[α] γ} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : β →ₗ[α] γ} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : β →ₗ[α] γ} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : β →ₗ[α] γ} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_copair_prod (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (p : submodule α β) (q : submodule α γ) : map (copair f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_pair_prod (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (p : submodule α γ) (q : submodule α δ) : comap (pair f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule α β) (q : submodule α γ) : p.prod q = p.comap (linear_map.fst α β γ) ⊓ q.comap (linear_map.snd α β γ) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule α β) (q : submodule α γ) : p.prod q = p.map (linear_map.inl α β γ) ⊔ q.map (linear_map.inr α β γ) := by rw [← map_copair_prod, copair_inl_inr, map_id] lemma span_inl_union_inr {s : set β} {t : set γ} : span α (prod.inl '' s ∪ prod.inr '' t) = (span α s).prod (span α t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : ker (pair f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_pair_prod]; refl end linear_map namespace linear_map variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma ker_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : β →ₗ[α] γ) (a : α) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : β →ₗ[α] γ) (a : α) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end linear_map namespace is_linear_map lemma is_linear_map_add {α β : Type*} [ring α] [add_comm_group β] [module α β]: is_linear_map α (λ (x : β × β), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {α β : Type*} [ring α] [add_comm_group β] [module α β]: is_linear_map α (λ (x : β × β), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp }, { intros x y, simp [smul_add] } end end is_linear_map namespace submodule variables {R:ring α} [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] variables (p p' : submodule α β) (q : submodule α γ) include R open linear_map @[simp] theorem map_top (f : β →ₗ[α] γ) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : β →ₗ[α] γ) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule α p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) @[simp] theorem ker_of_le (p p' : submodule α β) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule α β) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] lemma disjoint_iff_comap_eq_bot (p q : submodule α β) : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff p.ker_subtype, map_bot, map_comap_subtype]; refl /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule α p → submodule α p → Prop) ≃o ((≤) : {p' : submodule α β // p' ≤ p} → {p' : submodule α β // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } def map_subtype.le_order_embedding : ((≤) : submodule α p → submodule α p → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule α p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl def map_subtype.lt_order_embedding : ((<) : submodule α p → submodule α p → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl α β γ) = prod p ⊥ := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem map_inr : q.map (inr α β γ) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst α β γ) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd α β γ) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl α β γ) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr α β γ) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst α β γ) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd α β γ) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] def mkq : β →ₗ[α] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : β) : p.mkq x = quotient.mk x := rfl def liftq (f : β →ₗ[α] γ) (h : p ≤ f.ker) : p.quotient →ₗ[α] γ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : β →ₗ[α] γ) {h} (x : β) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : β →ₗ[α] γ) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule α p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] def mapq (f : β →ₗ[α] γ) (h : p ≤ comap f q) : p.quotient →ₗ[α] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : β →ₗ[α] γ) {h} (x : β) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : β →ₗ[α] γ) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : β →ₗ[α] γ) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : β →ₗ[α] γ) (h) (q : submodule α (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : β →ₗ[α] γ) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : β →ₗ[α] γ) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : β →ₗ[α] γ) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- Correspondence Theorem -/ def comap_mkq.order_iso : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≃o ((≤) : {p' : submodule α β // p ≤ p'} → {p' : submodule α β // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq], ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } def comap_mkq.le_order_embedding : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule α p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl def comap_mkq.lt_order_embedding : ((<) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true structure linear_equiv (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] extends β →ₗ[α] γ, β ≃ γ end infix ` ≃ₗ ` := linear_equiv _ notation β ` ≃ₗ[`:50 α `] ` γ := linear_equiv α β γ namespace linear_equiv section ring variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α instance : has_coe (β ≃ₗ[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩ @[simp] theorem coe_apply (e : β ≃ₗ[α] γ) (b : β) : (e : β →ₗ[α] γ) b = e b := rfl lemma to_equiv_injective : function.injective (to_equiv : (β ≃ₗ[α] γ) → β ≃ γ) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[extensionality] lemma ext {f g : β ≃ₗ[α] γ} (h : (f : β → γ) = g) : f = g := to_equiv_injective (equiv.eq_of_to_fun_eq h) section variable (β) def refl : β ≃ₗ[α] β := { .. linear_map.id, .. equiv.refl β } end def symm (e : β ≃ₗ[α] γ) : γ ≃ₗ[α] β := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } def trans (e₁ : β ≃ₗ[α] γ) (e₂ : γ ≃ₗ[α] δ) : β ≃ₗ[α] δ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } @[simp] theorem apply_symm_apply (e : β ≃ₗ[α] γ) (c : γ) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (e : β ≃ₗ[α] γ) (b : β) : e.symm (e b) = b := e.5 b noncomputable def of_bijective (f : β →ₗ[α] γ) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : β ≃ₗ[α] γ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply (f : β →ₗ[α] γ) {hf₁ hf₂} (x : β) : of_bijective f hf₁ hf₂ x = f x := rfl def of_linear (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : β ≃ₗ[α] γ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : β) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : γ) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem ker (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).ker = ⊥ := linear_map.ker_eq_bot.2 f.to_equiv.injective @[simp] protected theorem range (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).range = ⊤ := linear_map.range_eq_top.2 f.to_equiv.surjective def of_top (p : submodule α β) (h : p = ⊤) : p ≃ₗ[α] β := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply (p : submodule α β) {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply (p : submodule α β) {h} (x : β) : ↑((of_top p h).symm x) = x := rfl lemma eq_bot_of_equiv (p : submodule α β) (e : p ≃ₗ[α] (⊥ : submodule α γ)) : p = ⊥ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot α).2 _), have := e.symm_apply_apply ⟨b, hb⟩, rw [← e.coe_apply, submodule.eq_zero_of_bot_submodule ((e : p →ₗ[α] (⊥ : submodule α γ)) ⟨b, hb⟩), ← e.symm.coe_apply, linear_map.map_zero] at this, exact congr_arg (coe : p → β) this.symm end end ring section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open linear_map set_option class.instance_max_depth 39 def smul_of_unit (a : units α) : β ≃ₗ[α] β := of_linear ((a:α) • 1 : β →ₗ β) (((a⁻¹ : units α) : α) • 1 : β →ₗ β) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) def congr_right (f : γ ≃ₗ[α] δ) : (β →ₗ[α] γ) ≃ₗ (β →ₗ δ) := of_linear f.to_linear_map.congr_right f.symm.to_linear_map.congr_right (linear_map.ext $ λ _, linear_map.ext $ λ _, f.6 _) (linear_map.ext $ λ _, linear_map.ext $ λ _, f.5 _) end comm_ring section field variables [field α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variable (β) open linear_map def smul_of_ne_zero (a : α) (ha : a ≠ 0) : β ≃ₗ[α] β := smul_of_unit $ units.mk0 a ha end field end linear_equiv namespace equiv variables [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] def to_linear_equiv (e : β ≃ γ) (h : is_linear_map α (e : β → γ)) : β ≃ₗ[α] γ := { add := h.add, smul := h.smul, .. e} end equiv namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f : β →ₗ[α] γ) /-- First Isomorphism Law -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[α] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[α] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule def sup_quotient_to_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient →ₗ[α] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end set_option class.instance_max_depth 41 /-- Second Isomorphism Law -/ noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[α] (comap (p ⊔ p').subtype p').quotient := { .. sup_quotient_to_quotient_inf p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin constructor, { rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } section prod def prod {α β γ δ : Type*} [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [module α β] [module α γ] [module α δ] (f₁ : β →ₗ[α] γ) (f₂ : β →ₗ[α] δ) : β →ₗ[α] (γ × δ) := { to_fun := λx, (f₁ x, f₂ x), add := λx y, begin change (f₁ (x + y), f₂ (x+y)) = (f₁ x, f₂ x) + (f₁ y, f₂ y), simp only [linear_map.map_add], refl end, smul := λc x, by simp only [linear_map.map_smul] } lemma is_linear_map_prod_iso {α β γ δ : Type*} [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [module α β] [module α γ] [module α δ] : is_linear_map α (λ(p : (β →ₗ[α] γ) × (β →ₗ[α] δ)), (linear_map.prod p.1 p.2 : (β →ₗ[α] (γ × δ)))) := ⟨λu v, rfl, λc u, rfl⟩ def scalar_prod_space_iso {α β γ : Type*} [comm_ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] (c : β →ₗ[α] α) (f : γ) : β →ₗ[α] γ := { to_fun := λx, (c x) • f, add := λx y, begin change c (x + y) • f = (c x) • f + (c y) • f, simp [add_smul], end, smul := λa x, by simp [smul_smul] } end prod section pi universe i variables {φ : ι → Type i} variables [∀i, add_comm_group (φ i)] [∀i, module α (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, γ →ₗ[α] φ i) : γ →ₗ[α] (Πi, φ i) := ⟨λc i, f i c, assume c d, funext $ assume i, (f i).add _ _, assume c d, funext $ assume i, (f i).smul _ _⟩ @[simp] lemma pi_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, γ →ₗ[α] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, γ →ₗ[α] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, γ →ₗ[α] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, γ →ₗ[α] φ i) (g : δ →ₗ[α] γ) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- Linear projection -/ def proj (i : ι) : (Πi, φ i) →ₗ[α] φ i := ⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[α] φ i) b = b i := rfl lemma proj_pi (f : Πi, γ →ₗ[α] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule α (Πi, φ i)) = ⊥ := bot_unique $ submodule.le_def'.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end section variables (α φ) def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule α (Πi, φ i)) ≃ₗ[α] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.val_prop'], ext b ⟨j, hj⟩, refl }, { ext ⟨b, hb⟩, apply subtype.coe_ext.2, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { rw [dif_pos h], refl }, { rw [dif_neg h], exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j` otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[α] φ j := @function.update ι (λj, φ i →ₗ[α] φ j) _ 0 i id j lemma update_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i j : ι) (b : γ →ₗ[α] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end section variable [decidable_eq ι] variables (α φ) /-- Standard basis -/ def std_basis (i : ι) : φ i →ₗ[α] (Πi, φ i) := pi (diag i) lemma std_basis_apply (i : ι) (b : φ i) : std_basis α φ i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis α φ i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis α φ i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ι) : ker (std_basis α φ i) = ⊥ := ker_eq_bot.2 $ assume f g hfg, have std_basis α φ i f i = std_basis α φ i g i := hfg ▸ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis α φ j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis α φ i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis α φ j) = 0 := by ext b; simp [std_basis_ne α φ _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis α φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩, rw [proj_std_basis_ne α φ j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis α φ i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show I.sum (λi, std_basis α φ i (b i)) = b, { ext i, rw [pi.finset_sum_apply, ← std_basis_same α φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem _ i $ mem_supr_of_mem _ hiI $ linear_map.mem_range.2 ⟨_, rfl⟩) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis α φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [finset.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis α φ this) (supr_le_supr $ assume i, _), rw [← finset.mem_coe, finset.coe_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis α φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis α φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis α φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis α φ i)) (⨆i∈J, range (std_basis α φ i)) := begin refine disjoint_mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end end end pi variables (α β) instance automorphism_group : group (β ≃ₗ[α] β) := { mul := λ f g, g.trans f, one := linear_equiv.refl β, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } instance automorphism_group.to_linear_map_is_monoid_hom : is_monoid_hom (linear_equiv.to_linear_map : (β ≃ₗ[α] β) → (β →ₗ[α] β)) := { map_one := rfl, map_mul := λ f g, rfl } /-- The group of invertible linear maps from `β` to itself -/ def general_linear_group := units (β →ₗ[α] β) namespace general_linear_group variables {α β} instance : group (general_linear_group α β) := by delta general_linear_group; apply_instance def to_linear_equiv (f : general_linear_group α β) : (β ≃ₗ[α] β) := { inv_fun := f.inv.to_fun, left_inv := λ m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := λ m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } def of_linear_equiv (f : (β ≃ₗ[α] β)) : general_linear_group α β := { val := f, inv := f.symm, val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (α β) def general_linear_equiv : general_linear_group α β ≃* (β ≃ₗ[α] β) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, begin delta to_linear_equiv of_linear_equiv, cases f with f f_inv, cases f, cases f_inv, congr end, right_inv := λ f, begin delta to_linear_equiv of_linear_equiv, cases f, congr end, hom := ⟨λ x y, by {ext, refl}⟩ } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group α β) : ((general_linear_equiv α β).to_equiv f).to_linear_map = f.val := by {ext, refl} end general_linear_group end linear_map
f133f976eb8db5bb73e96b1480d2fefc178313f4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/liouville/basic.lean
2f52313ceaa0867941d8b3124693a802915e79fa
[ "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
12,516
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Jujian Zhang -/ import analysis.calculus.mean_value import data.polynomial.denoms_clearable import data.real.irrational /-! # Liouville's theorem > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains a proof of Liouville's theorem stating that all Liouville numbers are transcendental. To obtain this result, there is first a proof that Liouville numbers are irrational and two technical lemmas. These lemmas exploit the fact that a polynomial with integer coefficients takes integer values at integers. When evaluating at a rational number, we can clear denominators and obtain precise inequalities that ultimately allow us to prove transcendence of Liouville numbers. -/ /-- A Liouville number is a real number `x` such that for every natural number `n`, there exist `a, b ∈ ℤ` with `1 < b` such that `0 < |x - a/b| < 1/bⁿ`. In the implementation, the condition `x ≠ a/b` replaces the traditional equivalent `0 < |x - a/b|`. -/ def liouville (x : ℝ) := ∀ n : ℕ, ∃ a b : ℤ, 1 < b ∧ x ≠ a / b ∧ |x - a / b| < 1 / b ^ n namespace liouville protected lemma irrational {x : ℝ} (h : liouville x) : irrational x := begin -- By contradiction, `x = a / b`, with `a ∈ ℤ`, `0 < b ∈ ℕ` is a Liouville number, rintros ⟨⟨a, b, bN0, cop⟩, rfl⟩, -- clear up the mess of constructions of rationals rw [rat.cast_mk', ←div_eq_mul_inv] at h, -- Since `a / b` is a Liouville number, there are `p, q ∈ ℤ`, with `q1 : 1 < q`, -- `a0 : a / b ≠ p / q` and `a1 : |a / b - p / q| < 1 / q ^ (b + 1)` rcases h (b + 1) with ⟨p, q, q1, a0, a1⟩, -- A few useful inequalities have qR0 : (0 : ℝ) < q := int.cast_pos.mpr (zero_lt_one.trans q1), have b0 : (b : ℝ) ≠ 0 := ne_of_gt (nat.cast_pos.mpr bN0), have bq0 : (0 : ℝ) < b * q := mul_pos (nat.cast_pos.mpr bN0) qR0, -- At a1, clear denominators... replace a1 : |a * q - b * p| * q ^ (b + 1) < b * q, by rwa [div_sub_div _ _ b0 (ne_of_gt qR0), abs_div, div_lt_div_iff (abs_pos.mpr (ne_of_gt bq0)) (pow_pos qR0 _), abs_of_pos bq0, one_mul, -- ... and revert to integers ← int.cast_pow, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, ← int.cast_abs, ← int.cast_mul, int.cast_lt] at a1, -- At a0, clear denominators... replace a0 : ¬a * q - ↑b * p = 0, by rwa [ne.def, div_eq_div_iff b0 (ne_of_gt qR0), mul_comm ↑p, ← sub_eq_zero, -- ... and revert to integers ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, int.cast_eq_zero] at a0, -- Actually, `q` is a natural number lift q to ℕ using (zero_lt_one.trans q1).le, -- Looks innocuous, but we now have an integer with non-zero absolute value: this is at -- least one away from zero. The gain here is what gets the proof going. have ap : 0 < |a * ↑q - ↑b * p| := abs_pos.mpr a0, -- Actually, the absolute value of an integer is a natural number lift (|a * ↑q - ↑b * p|) to ℕ using (abs_nonneg (a * ↑q - ↑b * p)), -- At a1, revert to natural numbers rw [← int.coe_nat_mul, ← int.coe_nat_pow, ← int.coe_nat_mul, int.coe_nat_lt] at a1, -- Recall this is by contradiction: we obtained the inequality `b * q ≤ x * q ^ (b + 1)`, so -- we are done. exact not_le.mpr a1 (nat.mul_lt_mul_pow_succ (int.coe_nat_pos.mp ap) (int.coe_nat_lt.mp q1)).le, end open polynomial metric set real ring_hom open_locale polynomial /-- Let `Z, N` be types, let `R` be a metric space, let `α : R` be a point and let `j : Z → N → R` be a function. We aim to estimate how close we can get to `α`, while staying in the image of `j`. The points `j z a` of `R` in the image of `j` come with a "cost" equal to `d a`. As we get closer to `α` while staying in the image of `j`, we are interested in bounding the quantity `d a * dist α (j z a)` from below by a strictly positive amount `1 / A`: the intuition is that approximating well `α` with the points in the image of `j` should come at a high cost. The hypotheses on the function `f : R → R` provide us with sufficient conditions to ensure our goal. The first hypothesis is that `f` is Lipschitz at `α`: this yields a bound on the distance. The second hypothesis is specific to the Liouville argument and provides the missing bound involving the cost function `d`. This lemma collects the properties used in the proof of `exists_pos_real_of_irrational_root`. It is stated in more general form than needed: in the intended application, `Z = ℤ`, `N = ℕ`, `R = ℝ`, `d a = (a + 1) ^ f.nat_degree`, `j z a = z / (a + 1)`, `f ∈ ℤ[x]`, `α` is an irrational root of `f`, `ε` is small, `M` is a bound on the Lipschitz constant of `f` near `α`, `n` is the degree of the polynomial `f`. -/ lemma exists_one_le_pow_mul_dist {Z N R : Type*} [pseudo_metric_space R] {d : N → ℝ} {j : Z → N → R} {f : R → R} {α : R} {ε M : ℝ} -- denominators are positive (d0 : ∀ (a : N), 1 ≤ d a) (e0 : 0 < ε) -- function is Lipschitz at α (B : ∀ ⦃y : R⦄, y ∈ closed_ball α ε → dist (f α) (f y) ≤ (dist α y) * M) -- clear denominators (L : ∀ ⦃z : Z⦄, ∀ ⦃a : N⦄, j z a ∈ closed_ball α ε → 1 ≤ (d a) * dist (f α) (f (j z a))) : ∃ A : ℝ, 0 < A ∧ ∀ (z : Z), ∀ (a : N), 1 ≤ (d a) * (dist α (j z a) * A) := begin -- A useful inequality to keep at hand have me0 : 0 < max (1 / ε) M := lt_max_iff.mpr (or.inl (one_div_pos.mpr e0)), -- The maximum between `1 / ε` and `M` works refine ⟨max (1 / ε) M, me0, λ z a, _⟩, -- First, let's deal with the easy case in which we are far away from `α` by_cases dm1 : 1 ≤ (dist α (j z a) * max (1 / ε) M), { exact one_le_mul_of_one_le_of_one_le (d0 a) dm1 }, { -- `j z a = z / (a + 1)`: we prove that this ratio is close to `α` have : j z a ∈ closed_ball α ε, { refine mem_closed_ball'.mp (le_trans _ ((one_div_le me0 e0).mpr (le_max_left _ _))), exact ((le_div_iff me0).mpr (not_le.mp dm1).le) }, -- use the "separation from `1`" (assumption `L`) for numerators, refine (L this).trans _, -- remove a common factor and use the Lipschitz assumption `B` refine mul_le_mul_of_nonneg_left ((B this).trans _) (zero_le_one.trans (d0 a)), exact mul_le_mul_of_nonneg_left (le_max_right _ M) dist_nonneg } end lemma exists_pos_real_of_irrational_root {α : ℝ} (ha : irrational α) {f : ℤ[X]} (f0 : f ≠ 0) (fa : eval α (map (algebra_map ℤ ℝ) f) = 0): ∃ A : ℝ, 0 < A ∧ ∀ (a : ℤ), ∀ (b : ℕ), (1 : ℝ) ≤ (b + 1) ^ f.nat_degree * (|α - (a / (b + 1))| * A) := begin -- `fR` is `f` viewed as a polynomial with `ℝ` coefficients. set fR : ℝ[X] := map (algebra_map ℤ ℝ) f, -- `fR` is non-zero, since `f` is non-zero. obtain fR0 : fR ≠ 0 := λ fR0, (map_injective (algebra_map ℤ ℝ) (λ _ _ A, int.cast_inj.mp A)).ne f0 (fR0.trans (polynomial.map_zero _).symm), -- reformulating assumption `fa`: `α` is a root of `fR`. have ar : α ∈ (fR.roots.to_finset : set ℝ) := finset.mem_coe.mpr (multiset.mem_to_finset.mpr ((mem_roots fR0).mpr (is_root.def.mpr fa))), -- Since the polynomial `fR` has finitely many roots, there is a closed interval centered at `α` -- such that `α` is the only root of `fR` in the interval. obtain ⟨ζ, z0, U⟩ : ∃ ζ > 0, closed_ball α ζ ∩ (fR.roots.to_finset) = {α} := @exists_closed_ball_inter_eq_singleton_of_discrete _ _ _ discrete_of_t1_of_finite _ ar, -- Since `fR` is continuous, it is bounded on the interval above. obtain ⟨xm, -, hM⟩ : ∃ (xm : ℝ) (H : xm ∈ Icc (α - ζ) (α + ζ)), ∀ (y : ℝ), y ∈ Icc (α - ζ) (α + ζ) → |fR.derivative.eval y| ≤ |fR.derivative.eval xm| := is_compact.exists_forall_ge is_compact_Icc ⟨α, (sub_lt_self α z0).le, (lt_add_of_pos_right α z0).le⟩ (continuous_abs.comp fR.derivative.continuous_aeval).continuous_on, -- Use the key lemma `exists_one_le_pow_mul_dist`: we are left to show that ... refine @exists_one_le_pow_mul_dist ℤ ℕ ℝ _ _ _ (λ y, fR.eval y) α ζ (|fR.derivative.eval xm|) _ z0 (λ y hy, _) (λ z a hq, _), -- 1: the denominators are positive -- essentially by definition; { exact λ a, one_le_pow_of_one_le ((le_add_iff_nonneg_left 1).mpr a.cast_nonneg) _ }, -- 2: the polynomial `fR` is Lipschitz at `α` -- as its derivative continuous; { rw mul_comm, rw real.closed_ball_eq_Icc at hy, -- apply the Mean Value Theorem: the bound on the derivative comes from differentiability. refine convex.norm_image_sub_le_of_norm_deriv_le (λ _ _, fR.differentiable_at) (λ y h, by { rw fR.deriv, exact hM _ h }) (convex_Icc _ _) hy (mem_Icc_iff_abs_le.mp _), exact @mem_closed_ball_self ℝ _ α ζ (le_of_lt z0) }, -- 3: the weird inequality of Liouville type with powers of the denominators. { show 1 ≤ (a + 1 : ℝ) ^ f.nat_degree * |eval α fR - eval (z / (a + 1)) fR|, rw [fa, zero_sub, abs_neg], rw [show (a + 1 : ℝ) = ((a + 1 : ℕ) : ℤ), by norm_cast] at hq ⊢, -- key observation: the right-hand side of the inequality is an *integer*. Therefore, -- if its absolute value is not at least one, then it vanishes. Proceed by contradiction refine one_le_pow_mul_abs_eval_div (int.coe_nat_succ_pos a) (λ hy, _), -- As the evaluation of the polynomial vanishes, we found a root of `fR` that is rational. -- We know that `α` is the only root of `fR` in our interval, and `α` is irrational: -- follow your nose. refine (irrational_iff_ne_rational α).mp ha z (a + 1) ((mem_singleton_iff.mp _).symm), refine U.subset _, refine ⟨hq, finset.mem_coe.mp (multiset.mem_to_finset.mpr _)⟩, exact (mem_roots fR0).mpr (is_root.def.mpr hy) } end /-- **Liouville's Theorem** -/ protected theorem transcendental {x : ℝ} (lx : liouville x) : transcendental ℤ x := begin -- Proceed by contradiction: if `x` is algebraic, then `x` is the root (`ef0`) of a -- non-zero (`f0`) polynomial `f` rintros ⟨f : ℤ[X], f0, ef0⟩, -- Change `aeval x f = 0` to `eval (map _ f) = 0`, who knew. replace ef0 : (f.map (algebra_map ℤ ℝ)).eval x = 0, { rwa [aeval_def, ← eval_map] at ef0 }, -- There is a "large" real number `A` such that `(b + 1) ^ (deg f) * |f (x - a / (b + 1))| * A` -- is at least one. This is obtained from lemma `exists_pos_real_of_irrational_root`. obtain ⟨A, hA, h⟩ : ∃ (A : ℝ), 0 < A ∧ ∀ (a : ℤ) (b : ℕ), (1 : ℝ) ≤ (b + 1) ^ f.nat_degree * (|x - a / (b + 1)| * A) := exists_pos_real_of_irrational_root lx.irrational f0 ef0, -- Since the real numbers are Archimedean, a power of `2` exceeds `A`: `hn : A < 2 ^ r`. rcases pow_unbounded_of_one_lt A (lt_add_one 1) with ⟨r, hn⟩, -- Use the Liouville property, with exponent `r + deg f`. obtain ⟨a, b, b1, -, a1⟩ : ∃ (a b : ℤ), 1 < b ∧ x ≠ a / b ∧ |x - a / b| < 1 / b ^ (r + f.nat_degree) := lx (r + f.nat_degree), have b0 : (0 : ℝ) < b := zero_lt_one.trans (by { rw ← int.cast_one, exact int.cast_lt.mpr b1 }), -- Prove that `b ^ f.nat_degree * abs (x - a / b)` is strictly smaller than itself -- recall, this is a proof by contradiction! refine lt_irrefl ((b : ℝ) ^ f.nat_degree * |x - ↑a / ↑b|) _, -- clear denominators at `a1` rw [lt_div_iff' (pow_pos b0 _), pow_add, mul_assoc] at a1, -- split the inequality via `1 / A`. refine ((_ : (b : ℝ) ^ f.nat_degree * |x - a / b| < 1 / A).trans_le _), -- This branch of the proof uses the Liouville condition and the Archimedean property { refine (lt_div_iff' hA).mpr _, refine lt_of_le_of_lt _ a1, refine mul_le_mul_of_nonneg_right _ (mul_nonneg (pow_nonneg b0.le _) (abs_nonneg _)), refine hn.le.trans _, refine pow_le_pow_of_le_left zero_le_two _ _, exact int.cast_two.symm.le.trans (int.cast_le.mpr (int.add_one_le_iff.mpr b1)) }, -- this branch of the proof exploits the "integrality" of evaluations of polynomials -- at ratios of integers. { lift b to ℕ using zero_le_one.trans b1.le, specialize h a b.pred, rwa [← nat.cast_succ, nat.succ_pred_eq_of_pos (zero_lt_one.trans _), ← mul_assoc, ← (div_le_iff hA)] at h, exact int.coe_nat_lt.mp b1 } end end liouville
43060686cd65bde095131e042e22e063765133e9
35960c5b117752aca7e3e7767c0b393e4dbd72a7
/src/env.lean
57d2635e2f0f2efb06764e970a7c2523a32dd952
[ "Apache-2.0" ]
permissive
spl/tts
461dc76b83df8db47e4660d0941dc97e6d4fd7d1
b65298fea68ce47c8ed3ba3dbce71c1a20dd3481
refs/heads/master
1,541,049,198,347
1,537,967,023,000
1,537,967,029,000
119,653,145
1
0
null
null
null
null
UTF-8
Lean
false
false
920
lean
import data.finmap sch namespace tts ------------------------------------------------------------------ /-- Binding type -/ abbreviation bty (V : Type) (x : tagged V) : Type := sch V /-- A (non-dependent) pair of a variable and a type scheme -/ abbreviation binding (V : Type) : Type := sigma (bty V) /-- Construct a binding -/ abbreviation binding.mk {V : Type} : tagged V → sch V → binding V := sigma.mk /- Notation for a binding -/ infix ` :~ `:80 := binding.mk /-- Free variables of a binding -/ abbreviation binding.fv {V : Type} [decidable_eq V] (b : binding V) : finset (tagged V) := sch.fv b.2 /-- Environment mapping variables to bindings -/ abbreviation env (V : Type) : Type := finmap (tagged V) (bty V) /-- Domain of an environment -/ abbreviation dom {V : Type} (Γ : env V) : finset (tagged V) := finmap.keys Γ end /- namespace -/ tts --------------------------------------------------------
ac851fc34e0f077c0203c87ae0cc28d32b3daa5f
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/power_basis.lean
44c64888a9e202c3743e4131effc6d7a1feac79b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
15,126
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.minpoly /-! # Power basis This file defines a structure `power_basis R S`, giving a basis of the `R`-algebra `S` as a finite list of powers `1, x, ..., x^n`. For example, if `x` is algebraic over a ring/field, adjoining `x` gives a `power_basis` structure generated by `x`. ## Definitions * `power_basis R A`: a structure containing an `x` and an `n` such that `1, x, ..., x^n` is a basis for the `R`-algebra `A` (viewed as an `R`-module). * `findim (hf : f ≠ 0) : finite_dimensional.findim K (adjoin_root f) = f.nat_degree`, the dimension of `adjoin_root f` equals the degree of `f` * `power_basis.lift (pb : power_basis R S)`: if `y : S'` satisfies the same equations as `pb.gen`, this is the map `S →ₐ[R] S'` sending `pb.gen` to `y` * `power_basis.equiv`: if two power bases satisfy the same equations, they are equivalent as algebras ## Implementation notes Throughout this file, `R`, `S`, ... are `comm_ring`s, `A`, `B`, ... are `integral_domain`s and `K`, `L`, ... are `field`s. `S` is an `R`-algebra, `B` is an `A`-algebra, `L` is a `K`-algebra. ## Tags power basis, powerbasis -/ open polynomial variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T] variables {A B : Type*} [integral_domain A] [integral_domain B] [algebra A B] variables {K L : Type*} [field K] [field L] [algebra K L] /-- `pb : power_basis R S` states that `1, pb.gen, ..., pb.gen ^ (pb.dim - 1)` is a basis for the `R`-algebra `S` (viewed as `R`-module). This is a structure, not a class, since the same algebra can have many power bases. For the common case where `S` is defined by adjoining an integral element to `R`, the canonical power basis is given by `{algebra,intermediate_field}.adjoin.power_basis`. -/ @[nolint has_inhabited_instance] structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] := (gen : S) (dim : ℕ) (is_basis : is_basis R (λ (i : fin dim), gen ^ (i : ℕ))) namespace power_basis /-- Cannot be an instance because `power_basis` cannot be a class. -/ lemma finite_dimensional [algebra K S] (pb : power_basis K S) : finite_dimensional K S := finite_dimensional.of_fintype_basis pb.is_basis lemma findim [algebra K S] (pb : power_basis K S) : finite_dimensional.findim K S = pb.dim := by rw [finite_dimensional.findim_eq_card_basis pb.is_basis, fintype.card_fin] /-- TODO: this mixes `polynomial` and `finsupp`, we should hide this behind a new function `polynomial.of_finsupp`. -/ lemma polynomial.mem_supported_range {f : polynomial R} {d : ℕ} : (f : finsupp ℕ R) ∈ finsupp.supported R R (↑(finset.range d) : set ℕ) ↔ f.degree < d := by { simp_rw [finsupp.mem_supported', finset.mem_coe, finset.mem_range, not_lt, degree_lt_iff_coeff_zero], refl } lemma mem_span_pow' {x y : S} {d : ℕ} : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.degree < d ∧ y = aeval x f := begin have : set.range (λ (i : fin d), x ^ (i : ℕ)) = (λ (i : ℕ), x ^ i) '' ↑(finset.range d), { ext n, simp_rw [set.mem_range, set.mem_image, finset.mem_coe, finset.mem_range], exact ⟨λ ⟨⟨i, hi⟩, hy⟩, ⟨i, hi, hy⟩, λ ⟨i, hi, hy⟩, ⟨⟨i, hi⟩, hy⟩⟩ }, rw [this, finsupp.mem_span_iff_total], -- In the next line we use that `polynomial R := finsupp ℕ R`. -- It would be nice to have a function `polynomial.of_finsupp`. apply exists_congr, rintro (f : polynomial R), simp only [exists_prop, polynomial.mem_supported_range, eq_comm], apply and_congr iff.rfl, split; { rintro rfl; rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, eq_comm], apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def] } end lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.nat_degree < d ∧ y = aeval x f := begin rw mem_span_pow', split; { rintros ⟨f, h, hy⟩, refine ⟨f, _, hy⟩, by_cases hf : f = 0, { simp only [hf, nat_degree_zero, degree_zero] at h ⊢, exact lt_of_le_of_ne (nat.zero_le d) hd.symm <|> exact with_bot.bot_lt_some d }, simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h }, end lemma dim_ne_zero [nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 := λ h, one_ne_zero $ show (1 : S) = 0, by { rw [← pb.is_basis.total_repr 1, finsupp.total_apply, finsupp.sum_fintype], { refine finset.sum_eq_zero (λ x hx, _), cases x with x x_lt, rw h at x_lt, cases x_lt }, { simp } } lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) : ∃ f : polynomial R, f.nat_degree < pb.dim ∧ y = aeval pb.gen f := (mem_span_pow pb.dim_ne_zero).mp (pb.is_basis.mem_span y) section minpoly open_locale big_operators variable [algebra A S] /-- `pb.minpoly_gen` is a minimal polynomial for `pb.gen`. If `A` is not a field, it might not necessarily be *the* minimal polynomial, however `nat_degree_minpoly` shows its degree is indeed minimal. -/ noncomputable def minpoly_gen (pb : power_basis A S) : polynomial A := X ^ pb.dim - ∑ (i : fin pb.dim), C (pb.is_basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ) @[simp] lemma nat_degree_minpoly_gen (pb : power_basis A S) : nat_degree (minpoly_gen pb) = pb.dim := begin unfold minpoly_gen, apply nat_degree_eq_of_degree_eq_some, rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow, apply degree_sum_fin_lt end lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) := begin apply monic_sub_of_left (monic_pow (monic_X) _), rw degree_X_pow, exact degree_sum_fin_lt _ end @[simp] lemma aeval_minpoly_gen (pb : power_basis A S) : aeval pb.gen (minpoly_gen pb) = 0 := begin simp_rw [minpoly_gen, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_mul, alg_hom.map_pow, aeval_C, ← algebra.smul_def, aeval_X], refine sub_eq_zero.mpr ((pb.is_basis.total_repr (pb.gen ^ pb.dim)).symm.trans _), rw [finsupp.total_apply, finsupp.sum_fintype], intro i, rw zero_smul end lemma is_integral_gen (pb : power_basis A S) : is_integral A pb.gen := ⟨minpoly_gen pb, minpoly_gen_monic pb, aeval_minpoly_gen pb⟩ lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : polynomial A} (ne_zero : p ≠ 0) (root : aeval h.gen p = 0) : h.dim ≤ p.nat_degree := begin refine le_of_not_lt (λ hlt, ne_zero _), let p_coeff : fin (h.dim) → A := λ i, p.coeff i, suffices : ∀ i, p_coeff i = 0, { ext i, by_cases hi : i < h.dim, { exact this ⟨i, hi⟩ }, exact coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)) }, intro i, refine linear_independent_iff'.mp h.is_basis.1 finset.univ _ _ i (finset.mem_univ _), rw aeval_eq_sum_range' hlt at root, rw finset.sum_fin_eq_sum_range, convert root, ext i, split_ifs with hi, { refl }, { rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)), zero_smul] } end @[simp] lemma nat_degree_minpoly (pb : power_basis A S) : (minpoly A pb.gen).nat_degree = pb.dim := begin refine le_antisymm _ (dim_le_nat_degree_of_root pb (minpoly.ne_zero pb.is_integral_gen) (minpoly.aeval _ _)), rw ← nat_degree_minpoly_gen, apply nat_degree_le_of_degree_le, rw ← degree_eq_nat_degree (minpoly_gen_monic pb).ne_zero, exact minpoly.min _ _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb) end end minpoly section equiv variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S'] lemma nat_degree_lt_nat_degree {p q : polynomial R} (hp : p ≠ 0) (hpq : p.degree < q.degree) : p.nat_degree < q.nat_degree := begin by_cases hq : q = 0, { rw [hq, degree_zero] at hpq, have := not_lt_bot hpq, contradiction }, rwa [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe] at hpq end lemma constr_pow_aeval (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f := begin rw [← aeval_mod_by_monic_eq_self_of_root (minpoly.monic pb.is_integral_gen) (minpoly.aeval _ _), ← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _ (minpoly.monic pb.is_integral_gen) y hy], by_cases hf : f %ₘ minpoly A pb.gen = 0, { simp only [hf, alg_hom.map_zero, linear_map.map_zero] }, have : (f %ₘ minpoly A pb.gen).nat_degree < pb.dim, { rw ← pb.nat_degree_minpoly, apply nat_degree_lt_nat_degree hf, exact degree_mod_by_monic_lt _ (minpoly.monic pb.is_integral_gen) (minpoly.ne_zero pb.is_integral_gen) }, rw [aeval_eq_sum_range' this, aeval_eq_sum_range' this, linear_map.map_sum], refine finset.sum_congr rfl (λ i (hi : i ∈ finset.range pb.dim), _), rw finset.mem_range at hi, rw linear_map.map_smul, congr, exact @constr_basis _ _ _ _ _ _ _ _ _ _ _ (⟨i, hi⟩ : fin pb.dim) pb.is_basis, end lemma constr_pow_gen (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) pb.gen = y := by { convert pb.constr_pow_aeval hy X; rw aeval_X } lemma constr_pow_algebra_map (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (x : A) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (algebra_map A S x) = algebra_map A S' x := by { convert pb.constr_pow_aeval hy (C x); rw aeval_C } lemma constr_pow_mul [nontrivial S] (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (x x' : S) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (x * x') = pb.is_basis.constr (λ i, y ^ (i : ℕ)) x * pb.is_basis.constr (λ i, y ^ (i : ℕ)) x' := begin obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, obtain ⟨g, hg, rfl⟩ := pb.exists_eq_aeval x', simp only [← aeval_mul, pb.constr_pow_aeval hy] end /-- `pb.lift y hy` is the algebra map sending `pb.gen` to `y`, where `hy` states the higher powers of `y` are the same as the higher powers of `pb.gen`. -/ noncomputable def lift [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) : S →ₐ[A] S' := { map_one' := by { convert pb.constr_pow_algebra_map hy 1 using 2; rw ring_hom.map_one }, map_zero' := by { convert pb.constr_pow_algebra_map hy 0 using 2; rw ring_hom.map_zero }, map_mul' := pb.constr_pow_mul hy, commutes' := pb.constr_pow_algebra_map hy, .. pb.is_basis.constr (λ i, y ^ (i : ℕ)) } @[simp] lemma lift_gen [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) : pb.lift y hy pb.gen = y := pb.constr_pow_gen hy @[simp] lemma lift_aeval [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) : pb.lift y hy (aeval pb.gen f) = aeval y f := pb.constr_pow_aeval hy f /-- `pb.equiv pb' h` is an equivalence of algebras with the same power basis. -/ noncomputable def equiv [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : S ≃ₐ[A] S' := alg_equiv.of_alg_hom (pb.lift pb'.gen (h.symm ▸ minpoly.aeval A pb'.gen)) (pb'.lift pb.gen (h ▸ minpoly.aeval A pb.gen)) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb'.exists_eq_aeval x, simp }) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, simp }) @[simp] lemma equiv_aeval [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) (f : polynomial A) : pb.equiv pb' h (aeval pb.gen f) = aeval pb'.gen f := pb.lift_aeval _ (h.symm ▸ minpoly.aeval A _) _ @[simp] lemma equiv_gen [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : pb.equiv pb' h pb.gen = pb'.gen := pb.lift_gen _ (h.symm ▸ minpoly.aeval A _) local attribute [irreducible] power_basis.lift @[simp] lemma equiv_symm [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : (pb.equiv pb' h).symm = pb'.equiv pb h.symm := rfl end equiv end power_basis open power_basis /-- Useful lemma to show `x` generates a power basis: the powers of `x` less than the degree of `x`'s minimal polynomial are linear independent. -/ lemma is_integral.linear_independent_pow [algebra K S] {x : S} (hx : is_integral K x) : linear_independent K (λ (i : fin (minpoly K x).nat_degree), x ^ (i : ℕ)) := begin rw linear_independent_iff, intros p hp, let f : polynomial K := p.sum (λ i, monomial i), have f_def : ∀ (i : fin _), f.coeff i = p i, { intro i, -- TODO: how can we avoid unfolding here? change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = p i, simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum], rw [finset.sum_eq_single, if_pos rfl], { intros b _ hb, rw if_neg (mt (λ h, _) hb), exact fin.coe_injective h }, { intro hi, split_ifs; { exact finsupp.not_mem_support_iff.mp hi } } }, have f_def' : ∀ i, f.coeff i = if hi : i < _ then p ⟨i, hi⟩ else 0, { intro i, split_ifs with hi, { exact f_def ⟨i, hi⟩ }, -- TODO: how can we avoid unfolding here? change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = 0, simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum], apply finset.sum_eq_zero, rintro ⟨j, hj⟩ -, apply if_neg (mt _ hi), rintro rfl, exact hj }, suffices : f = 0, { ext i, rw [← f_def, this, coeff_zero, finsupp.zero_apply] }, contrapose hp with hf, intro h, have : (minpoly K x).degree ≤ f.degree, { apply minpoly.degree_le_of_ne_zero K x hf, convert h, rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, finsupp.sum_sum_index], { apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def, monomial, finsupp.lsingle_apply, zero_mul, ring_hom.map_zero, finsupp.sum_single_index] }, { intro, simp only [ring_hom.map_zero, zero_mul] }, { intros, simp only [ring_hom.map_add, add_mul] } }, have : ¬ (minpoly K x).degree ≤ f.degree, { apply not_le_of_lt, rw [degree_eq_nat_degree (minpoly.ne_zero hx), degree_lt_iff_coeff_zero], intros i hi, rw [f_def' i, dif_neg], exact hi.not_lt }, contradiction end lemma is_integral.mem_span_pow [nontrivial R] {x y : S} (hx : is_integral R x) (hy : ∃ f : polynomial R, y = aeval x f) : y ∈ submodule.span R (set.range (λ (i : fin (minpoly R x).nat_degree), x ^ (i : ℕ))) := begin obtain ⟨f, rfl⟩ := hy, apply mem_span_pow'.mpr _, have := minpoly.monic hx, refine ⟨f.mod_by_monic (minpoly R x), lt_of_lt_of_le (degree_mod_by_monic_lt _ this (ne_zero_of_monic this)) degree_le_nat_degree, _⟩, conv_lhs { rw ← mod_by_monic_add_div f this }, simp only [add_zero, zero_mul, minpoly.aeval, aeval_add, alg_hom.map_mul] end
03a5b2422790ae43aaa17ab345e2ae931ffb0394
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/category/CompHaus/default.lean
8c58caf89086c531dfcda47df48133083d655f2b
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
9,619
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Bhavik Mehta -/ import category_theory.adjunction.reflective import topology.category.Top import topology.stone_cech import category_theory.monad.limits import topology.urysohns_lemma /-! # The category of Compact Hausdorff Spaces We construct the category of compact Hausdorff spaces. The type of compact Hausdorff spaces is denoted `CompHaus`, and it is endowed with a category instance making it a full subcategory of `Top`. The fully faithful functor `CompHaus ⥤ Top` is denoted `CompHaus_to_Top`. **Note:** The file `topology/category/Compactum.lean` provides the equivalence between `Compactum`, which is defined as the category of algebras for the ultrafilter monad, and `CompHaus`. `Compactum_to_CompHaus` is the functor from `Compactum` to `CompHaus` which is proven to be an equivalence of categories in `Compactum_to_CompHaus.is_equivalence`. See `topology/category/Compactum.lean` for a more detailed discussion where these definitions are introduced. -/ universes v u open category_theory /-- The type of Compact Hausdorff topological spaces. -/ structure CompHaus := (to_Top : Top) [is_compact : compact_space to_Top] [is_hausdorff : t2_space to_Top] namespace CompHaus instance : inhabited CompHaus := ⟨{to_Top := { α := pempty }}⟩ instance : has_coe_to_sort CompHaus Type* := ⟨λ X, X.to_Top⟩ instance {X : CompHaus} : compact_space X := X.is_compact instance {X : CompHaus} : t2_space X := X.is_hausdorff instance category : category CompHaus := induced_category.category to_Top instance concrete_category : concrete_category CompHaus := induced_category.concrete_category _ @[simp] lemma coe_to_Top {X : CompHaus} : (X.to_Top : Type*) = X := rfl variables (X : Type*) [topological_space X] [compact_space X] [t2_space X] /-- A constructor for objects of the category `CompHaus`, taking a type, and bundling the compact Hausdorff topology found by typeclass inference. -/ def of : CompHaus := { to_Top := Top.of X, is_compact := ‹_›, is_hausdorff := ‹_› } @[simp] lemma coe_of : (CompHaus.of X : Type _) = X := rfl /-- Any continuous function on compact Hausdorff spaces is a closed map. -/ lemma is_closed_map {X Y : CompHaus.{u}} (f : X ⟶ Y) : is_closed_map f := λ C hC, (hC.is_compact.image f.continuous).is_closed /-- Any continuous bijection of compact Hausdorff spaces is an isomorphism. -/ lemma is_iso_of_bijective {X Y : CompHaus.{u}} (f : X ⟶ Y) (bij : function.bijective f) : is_iso f := begin let E := equiv.of_bijective _ bij, have hE : continuous E.symm, { rw continuous_iff_is_closed, intros S hS, rw ← E.image_eq_preimage, exact is_closed_map f S hS }, refine ⟨⟨⟨E.symm, hE⟩, _, _⟩⟩, { ext x, apply E.symm_apply_apply }, { ext x, apply E.apply_symm_apply } end /-- Any continuous bijection of compact Hausdorff spaces induces an isomorphism. -/ noncomputable def iso_of_bijective {X Y : CompHaus.{u}} (f : X ⟶ Y) (bij : function.bijective f) : X ≅ Y := by letI := is_iso_of_bijective _ bij; exact as_iso f end CompHaus /-- The fully faithful embedding of `CompHaus` in `Top`. -/ @[simps {rhs_md := semireducible}, derive [full, faithful]] def CompHaus_to_Top : CompHaus.{u} ⥤ Top.{u} := induced_functor _ instance CompHaus.forget_reflects_isomorphisms : reflects_isomorphisms (forget CompHaus.{u}) := ⟨by introsI A B f hf; exact CompHaus.is_iso_of_bijective _ ((is_iso_iff_bijective f).mp hf)⟩ /-- (Implementation) The object part of the compactification functor from topological spaces to compact Hausdorff spaces. -/ @[simps] def StoneCech_obj (X : Top) : CompHaus := CompHaus.of (stone_cech X) /-- (Implementation) The bijection of homsets to establish the reflective adjunction of compact Hausdorff spaces in topological spaces. -/ noncomputable def stone_cech_equivalence (X : Top.{u}) (Y : CompHaus.{u}) : (StoneCech_obj X ⟶ Y) ≃ (X ⟶ CompHaus_to_Top.obj Y) := { to_fun := λ f, { to_fun := f ∘ stone_cech_unit, continuous_to_fun := f.2.comp (@continuous_stone_cech_unit X _) }, inv_fun := λ f, { to_fun := stone_cech_extend f.2, continuous_to_fun := continuous_stone_cech_extend f.2 }, left_inv := begin rintro ⟨f : stone_cech X ⟶ Y, hf : continuous f⟩, ext (x : stone_cech X), refine congr_fun _ x, apply continuous.ext_on dense_range_stone_cech_unit (continuous_stone_cech_extend _) hf, rintro _ ⟨y, rfl⟩, apply congr_fun (stone_cech_extend_extends (hf.comp _)) y, end, right_inv := begin rintro ⟨f : (X : Type*) ⟶ Y, hf : continuous f⟩, ext, exact congr_fun (stone_cech_extend_extends hf) _, end } /-- The Stone-Cech compactification functor from topological spaces to compact Hausdorff spaces, left adjoint to the inclusion functor. -/ noncomputable def Top_to_CompHaus : Top.{u} ⥤ CompHaus.{u} := adjunction.left_adjoint_of_equiv stone_cech_equivalence.{u} (λ _ _ _ _ _, rfl) lemma Top_to_CompHaus_obj (X : Top) : ↥(Top_to_CompHaus.obj X) = stone_cech X := rfl /-- The category of compact Hausdorff spaces is reflective in the category of topological spaces. -/ noncomputable instance CompHaus_to_Top.reflective : reflective CompHaus_to_Top := { to_is_right_adjoint := ⟨Top_to_CompHaus, adjunction.adjunction_of_equiv_left _ _⟩ } noncomputable instance CompHaus_to_Top.creates_limits : creates_limits CompHaus_to_Top := monadic_creates_limits _ instance CompHaus.has_limits : limits.has_limits CompHaus := has_limits_of_has_limits_creates_limits CompHaus_to_Top instance CompHaus.has_colimits : limits.has_colimits CompHaus := has_colimits_of_reflective CompHaus_to_Top namespace CompHaus /-- An explicit limit cone for a functor `F : J ⥤ CompHaus`, defined in terms of `Top.limit_cone`. -/ def limit_cone {J : Type v} [small_category J] (F : J ⥤ CompHaus.{max v u}) : limits.cone F := { X := { to_Top := (Top.limit_cone (F ⋙ CompHaus_to_Top)).X, is_compact := begin show compact_space ↥{u : Π j, (F.obj j) | ∀ {i j : J} (f : i ⟶ j), (F.map f) (u i) = u j}, rw ← is_compact_iff_compact_space, apply is_closed.is_compact, have : {u : Π j, F.obj j | ∀ {i j : J} (f : i ⟶ j), F.map f (u i) = u j} = ⋂ (i j : J) (f : i ⟶ j), {u | F.map f (u i) = u j}, { ext1, simp only [set.mem_Inter, set.mem_set_of_eq], }, rw this, apply is_closed_Inter, intros i, apply is_closed_Inter, intros j, apply is_closed_Inter, intros f, apply is_closed_eq, { exact (continuous_map.continuous (F.map f)).comp (continuous_apply i), }, { exact continuous_apply j, } end, is_hausdorff := show t2_space ↥{u : Π j, (F.obj j) | ∀ {i j : J} (f : i ⟶ j), (F.map f) (u i) = u j}, from infer_instance }, π := { app := λ j, (Top.limit_cone (F ⋙ CompHaus_to_Top)).π.app j, naturality' := by { intros _ _ _, ext ⟨x, hx⟩, simp only [comp_apply, functor.const_obj_map, id_apply], exact (hx f).symm, } } } /-- The limit cone `CompHaus.limit_cone F` is indeed a limit cone. -/ def limit_cone_is_limit {J : Type v} [small_category J] (F : J ⥤ CompHaus.{max v u}) : limits.is_limit (limit_cone F) := { lift := λ S, (Top.limit_cone_is_limit (F ⋙ CompHaus_to_Top)).lift (CompHaus_to_Top.map_cone S), uniq' := λ S m h, (Top.limit_cone_is_limit _).uniq (CompHaus_to_Top.map_cone S) _ h } lemma epi_iff_surjective {X Y : CompHaus.{u}} (f : X ⟶ Y) : epi f ↔ function.surjective f := begin split, { contrapose!, rintros ⟨y, hy⟩ hf, let C := set.range f, have hC : is_closed C := (is_compact_range f.continuous).is_closed, let D := {y}, have hD : is_closed D := is_closed_singleton, have hCD : disjoint C D, { rw set.disjoint_singleton_right, rintro ⟨y', hy'⟩, exact hy y' hy' }, haveI : normal_space ↥(Y.to_Top) := normal_of_compact_t2, obtain ⟨φ, hφ0, hφ1, hφ01⟩ := exists_continuous_zero_one_of_closed hC hD hCD, haveI : compact_space (ulift.{u} $ set.Icc (0:ℝ) 1) := homeomorph.ulift.symm.compact_space, haveI : t2_space (ulift.{u} $ set.Icc (0:ℝ) 1) := homeomorph.ulift.symm.t2_space, let Z := of (ulift.{u} $ set.Icc (0:ℝ) 1), let g : Y ⟶ Z := ⟨λ y', ⟨⟨φ y', hφ01 y'⟩⟩, continuous_ulift_up.comp (continuous_subtype_mk (λ y', hφ01 y') φ.continuous)⟩, let h : Y ⟶ Z := ⟨λ _, ⟨⟨0, set.left_mem_Icc.mpr zero_le_one⟩⟩, continuous_const⟩, have H : h = g, { rw ← cancel_epi f, ext x, dsimp, simp only [comp_apply, continuous_map.coe_mk, subtype.coe_mk, hφ0 (set.mem_range_self x), pi.zero_apply], }, apply_fun (λ e, (e y).down) at H, dsimp at H, simp only [subtype.mk_eq_mk, hφ1 (set.mem_singleton y), pi.one_apply] at H, exact zero_ne_one H, }, { rw ← category_theory.epi_iff_surjective, apply (forget CompHaus).epi_of_epi_map } end lemma mono_iff_injective {X Y : CompHaus.{u}} (f : X ⟶ Y) : mono f ↔ function.injective f := begin split, { introsI hf x₁ x₂ h, let g₁ : of punit ⟶ X := ⟨λ _, x₁, continuous_of_discrete_topology⟩, let g₂ : of punit ⟶ X := ⟨λ _, x₂, continuous_of_discrete_topology⟩, have : g₁ ≫ f = g₂ ≫ f, by { ext, exact h }, rw cancel_mono at this, apply_fun (λ e, e punit.star) at this, exact this }, { rw ← category_theory.mono_iff_injective, apply (forget CompHaus).mono_of_mono_map } end end CompHaus
7d2378c57bbd28bf6287bed88c5733f3c8c24806
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/analysis/normed_space/bounded_linear_maps.lean
e479d65060f50461eba494efd958aa77d168925f
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
14,537
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Continuous linear functions -- functions between normed vector spaces which are bounded and linear. -/ import algebra.field import analysis.normed_space.operator_norm noncomputable theory open_locale classical filter open filter (tendsto) open metric 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] set_option class.instance_max_depth 70 /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥ f x ∥ ≤ M * ∥ x ∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, assume x, le_trans (h x) $ mul_le_mul_of_nonneg_right (le_trans this zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in linear_map.continuous_of_bound _ C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_right _ _ end variables { f g : E → F } lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, c • f e) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ assume x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ assume x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := let ⟨hlg, Mg, hMgp, hMg⟩ := hg in let ⟨hlf, Mf, hMfp, hMf⟩ := hf in ((hlg.mk' _).comp (hlf.mk' _)).is_linear.with_bound (Mg * Mf) $ assume x, calc ∥g (f x)∥ ≤ Mg * ∥f x∥ : hMg _ ... ≤ Mg * (Mf * ∥x∥) : mul_le_mul_of_nonneg_left (hMf _) (le_of_lt hMgp) ... = Mg * Mf * ∥x∥ : (mul_assoc _ _ _).symm lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : f →_{x} (f x) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (assume e, norm_nonneg _) (assume e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices (λ (e : E), M * ∥e - x∥) →_{x} (M * 0), by simpa, tendsto_const_nhds.mul (lim_norm _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : (f →_{0} 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := h.bound in ⟨M, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : is_O (λ x', g (f x')) f l := (hg.is_O_id ⊤).comp_tendsto lattice.le_top theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp h l end end is_bounded_linear_map section set_option class.instance_max_depth 240 lemma is_bounded_linear_map_prod_iso : is_bounded_linear_map 𝕜 (λ(p : (E →L[𝕜] F) × (E →L[𝕜] G)), (continuous_linear_map.prod p.1 p.2 : (E →L[𝕜] (F × G)))) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ 1 (λp, _), simp only [norm, one_mul], refine continuous_linear_map.op_norm_le_bound _ (le_trans (norm_nonneg _) (le_max_left _ _)) (λu, _), simp only [norm, continuous_linear_map.prod, max_le_iff], split, { calc ∥p.1 u∥ ≤ ∥p.1∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) }, { calc ∥p.2 u∥ ≤ ∥p.2∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _) } end lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : continuous_linear_map 𝕜 F G) : is_bounded_linear_map 𝕜 (λ(f : E →L[𝕜] F), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, _, λc u, _⟩ (∥g∥) (λu, continuous_linear_map.op_norm_comp_le _ _), { ext x, change g ((u+v) x) = g (u x) + g (v x), have : (u+v) x = u x + v x := rfl, rw [this, g.map_add] }, { ext x, change g ((c • u) x) = c • g (u x), have : (c • u) x = c • u x := rfl, rw [this, continuous_linear_map.map_smul] } end lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : continuous_linear_map 𝕜 E F) : is_bounded_linear_map 𝕜 (λ(g : F →L[𝕜] G), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ (∥f∥) (λg, _), rw mul_comm, exact continuous_linear_map.op_norm_comp_le _ _ end end section bilinear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀(x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀(c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x,y)) (add_right : ∀(x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀(c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃C>0, ∀(x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp lemma is_bounded_bilinear_map_smul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × E), p.1 • p.2) := { add_left := add_smul, smul_left := λc x y, by simp [smul_smul], add_right := smul_add, smul_right := λc x y, by simp [smul_smul, mul_comm], bound := ⟨1, zero_lt_one, λx y, by simp [norm_smul]⟩ } lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ(p : (E →L[𝕜] F) × (F →L[𝕜] G)), p.2.comp p.1) := { add_left := λx₁ x₂ y, begin ext z, change y (x₁ z + x₂ z) = y (x₁ z) + y (x₂ z), rw y.map_add end, smul_left := λc x y, begin ext z, change y (c • (x z)) = c • y (x z), rw continuous_linear_map.map_smul end, add_right := λx y₁ y₂, rfl, smul_right := λc x y, rfl, bound := ⟨1, zero_lt_one, λx y, calc ∥continuous_linear_map.comp ((x, y).snd) ((x, y).fst)∥ ≤ ∥y∥ * ∥x∥ : continuous_linear_map.op_norm_comp_le _ _ ... = 1 * ∥x∥ * ∥ y∥ : by ring ⟩ } lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λp : (E →L[𝕜] F) × E, p.1 p.2) := { add_left := by simp, smul_left := by simp, add_right := by simp, smul_right := by simp, bound := ⟨1, zero_lt_one, by simp [continuous_linear_map.le_op_norm]⟩ } /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λp, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := { add_left := λm₁ m₂ f, by { ext z, simp [add_smul] }, smul_left := λc m f, by { ext z, simp [mul_smul] }, add_right := λm f₁ f₂, by { ext z, simp [smul_add] }, smul_right := λc m f, by { ext z, simp [smul_smul, mul_comm] }, bound := ⟨1, zero_lt_one, λm f, by simp⟩ } /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here a bounded linear map from `E × F` to `G`. The fact that this is indeed the derivative of `f` is proved in `is_bounded_bilinear_map.has_fderiv_at` in `fderiv.lean`-/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →ₗ[𝕜] G := { to_fun := λq, f (p.1, q.2) + f (q.1, p.2), add := λq₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right] end, smul := λc q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →L[𝕜] G := (h.linear_deriv p).with_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λq, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl set_option class.instance_max_depth 100 /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λp : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos, hC⟩, refine is_linear_map.with_bound ⟨λp₁ p₂, _, λc p, _⟩ (C + C) (λp, _), { ext q, simp [h.add_left, h.add_right] }, { ext q, simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg (le_of_lt Cpos) (le_of_lt Cpos)) (norm_nonneg _)) (λq, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, le_of_lt Cpos, le_refl, le_max_left, le_max_right, mul_nonneg, norm_nonneg, norm_nonneg, norm_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map
f139464fc69bce33746203fc93b07645fcdee5b6
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/adjunction/evaluation.lean
0f5f80e8df6d5d2e6315658a576670a8ad2dbe4c
[ "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,794
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.limits.shapes.products import category_theory.epi_mono /-! # Adjunctions involving evaluation We show that evaluation of functors have adjoints, given the existence of (co)products. -/ namespace category_theory open category_theory.limits universes v₁ v₂ u₁ u₂ variables {C : Type u₁} [category.{v₁} C] (D : Type u₂) [category.{v₂} D] noncomputable theory section variables [∀ (a b : C), has_coproducts_of_shape (a ⟶ b) D] /-- The left adjoint of evaluation. -/ @[simps] def evaluation_left_adjoint (c : C) : D ⥤ C ⥤ D := { obj := λ d, { obj := λ t, ∐ (λ i : c ⟶ t, d), map := λ u v f, sigma.desc $ λ g, sigma.ι (λ _, d) $ g ≫ f, map_id' := begin intros, ext, simp only [cofan.mk_ι_app, colimit.ι_desc, category.comp_id], congr' 1, rw category.comp_id, end, map_comp' := begin intros, ext, simp only [cofan.mk_ι_app, colimit.ι_desc_assoc, colimit.ι_desc], congr' 1, rw category.assoc, end }, map := λ d₁ d₂ f, { app := λ e, sigma.desc $ λ h, f ≫ sigma.ι (λ _, d₂) h, naturality' := by { intros, ext, dsimp, simp } }, map_id' := by { intros, ext, dsimp, simp }, map_comp' := by { intros, ext, dsimp, simp } } /-- The adjunction showing that evaluation is a right adjoint. -/ @[simps unit_app counit_app_app] def evaluation_adjunction_right (c : C) : evaluation_left_adjoint D c ⊣ (evaluation _ _).obj c := adjunction.mk_of_hom_equiv { hom_equiv := λ d F, { to_fun := λ f, sigma.ι (λ _, d) (𝟙 _) ≫ f.app c, inv_fun := λ f, { app := λ e, sigma.desc $ λ h, f ≫ F.map h, naturality' := by { intros, ext, dsimp, simp } }, left_inv := begin intros f, ext x g, dsimp, simp only [colimit.ι_desc, limits.cofan.mk_ι_app, category.assoc, ← f.naturality, evaluation_left_adjoint_obj_map, colimit.ι_desc_assoc, cofan.mk_ι_app], congr' 2, rw category.id_comp end, right_inv := λ f, by { dsimp, simp } }, hom_equiv_naturality_left_symm' := by { intros, ext, dsimp, simp }, hom_equiv_naturality_right' := by { intros, dsimp, simp } } instance evaluation_is_right_adjoint (c : C) : is_right_adjoint ((evaluation _ D).obj c) := ⟨_, evaluation_adjunction_right _ _⟩ lemma nat_trans.mono_iff_app_mono {F G : C ⥤ D} (η : F ⟶ G) : mono η ↔ (∀ c, mono (η.app c)) := begin split, { intros h c, exact right_adjoint_preserves_mono (evaluation_adjunction_right D c) h }, { introsI _, apply nat_trans.mono_app_of_mono } end end section variables [∀ (a b : C), has_products_of_shape (a ⟶ b) D] /-- The right adjoint of evaluation. -/ @[simps] def evaluation_right_adjoint (c : C) : D ⥤ C ⥤ D := { obj := λ d, { obj := λ t, ∏ (λ i : t ⟶ c, d), map := λ u v f, pi.lift $ λ g, pi.π _ $ f ≫ g, map_id' := begin intros, ext, dsimp, simp only [limit.lift_π, category.id_comp, fan.mk_π_app], congr, simp, end, map_comp' := begin intros, ext, dsimp, simp only [limit.lift_π, fan.mk_π_app, category.assoc], congr' 1, simp, end }, map := λ d₁ d₂ f, { app := λ t, pi.lift $ λ g, pi.π _ g ≫ f, naturality' := by { intros, ext, dsimp, simp } }, map_id' := by { intros, ext, dsimp, simp }, map_comp' := by { intros, ext, dsimp, simp } } /-- The adjunction showing that evaluation is a left adjoint. -/ @[simps unit_app_app counit_app] def evaluation_adjunction_left (c : C) : (evaluation _ _).obj c ⊣ evaluation_right_adjoint D c := adjunction.mk_of_hom_equiv { hom_equiv := λ F d, { to_fun := λ f, { app := λ t, pi.lift $ λ g, F.map g ≫ f, naturality' := by { intros, ext, dsimp, simp } }, inv_fun := λ f, f.app _ ≫ pi.π _ (𝟙 _), left_inv := λ f, by { dsimp, simp }, right_inv := begin intros f, ext x g, dsimp, simp only [limit.lift_π, evaluation_right_adjoint_obj_map, nat_trans.naturality_assoc, fan.mk_π_app], congr, rw category.comp_id end }, hom_equiv_naturality_left_symm' := by { intros, dsimp, simp }, hom_equiv_naturality_right' := by { intros, ext, dsimp, simp } } instance evaluation_is_left_adjoint (c : C) : is_left_adjoint ((evaluation _ D).obj c) := ⟨_, evaluation_adjunction_left _ _⟩ lemma nat_trans.epi_iff_app_epi {F G : C ⥤ D} (η : F ⟶ G) : epi η ↔ (∀ c, epi (η.app c)) := begin split, { intros h c, exact left_adjoint_preserves_epi (evaluation_adjunction_left D c) h }, { introsI, apply nat_trans.epi_app_of_epi } end end end category_theory
e23577db3699300f0b40edbd03e205ffcccf047e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/metric_space/hausdorff_distance.lean
1e59651bbb2e9f6b0e42859b91419bdc6012c10e
[ "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
59,790
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.specific_limits.basic import topology.metric_space.isometry import topology.instances.ennreal /-! # Hausdorff distance The Hausdorff distance on subsets of a metric (or emetric) space. Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d` such that any point `s` is within `d` of a point in `t`, and conversely. This quantity is often infinite (think of `s` bounded and `t` unbounded), and therefore better expressed in the setting of emetric spaces. ## Main definitions This files introduces: * `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space * `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space * Versions of these notions on metric spaces, called respectively `inf_dist` and `Hausdorff_dist` * `thickening δ s`, the open thickening by radius `δ` of a set `s` in a pseudo emetric space. * `cthickening δ s`, the closed thickening by radius `δ` of a set `s` in a pseudo emetric space. -/ noncomputable theory open_locale classical nnreal ennreal topological_space universes u v w open classical set function topological_space filter variables {ι : Sort*} {α : Type u} {β : Type v} namespace emetric section inf_edist variables [pseudo_emetric_space α] [pseudo_emetric_space β] {x y : α} {s t : set α} {Φ : α → β} /-! ### Distance of a point to a set as a function into `ℝ≥0∞`. -/ /-- The minimal edistance of a point to a set -/ def inf_edist (x : α) (s : set α) : ℝ≥0∞ := ⨅ y ∈ s, edist x y @[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ := infi_emptyset lemma le_inf_edist {d} : d ≤ inf_edist x s ↔ ∀ y ∈ s, d ≤ edist x y := by simp only [inf_edist, le_infi_iff] /-- The edist to a union is the minimum of the edists -/ @[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t := infi_union @[simp] lemma inf_edist_Union (f : ι → set α) (x : α) : inf_edist x (⋃ i, f i) = ⨅ i, inf_edist x (f i) := infi_Union f _ /-- The edist to a singleton is the edistance to the single point of this singleton -/ @[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y := infi_singleton /-- The edist to a set is bounded above by the edist to any of its points -/ lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y := infi₂_le _ h /-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/ lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 := nonpos_iff_eq_zero.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h /-- The edist is antitone with respect to inclusion. -/ lemma inf_edist_anti (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s := infi_le_infi_of_subset h /-- The edist to a set is `< r` iff there exists a point in the set at edistance `< r` -/ lemma inf_edist_lt_iff {r : ℝ≥0∞} : inf_edist x s < r ↔ ∃ y ∈ s, edist x y < r := by simp_rw [inf_edist, infi_lt_iff] /-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and the edist from `x` to `y` -/ lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y := calc (⨅ z ∈ s, edist x z) ≤ ⨅ z ∈ s, edist y z + edist x y : infi₂_mono $ λ z hz, (edist_triangle _ _ _).trans_eq (add_comm _ _) ... = (⨅ z ∈ s, edist y z) + edist x y : by simp only [ennreal.infi_add] lemma inf_edist_le_edist_add_inf_edist : inf_edist x s ≤ edist x y + inf_edist y s := by { rw add_comm, exact inf_edist_le_inf_edist_add_edist } /-- The edist to a set depends continuously on the point -/ @[continuity] lemma continuous_inf_edist : continuous (λx, inf_edist x s) := continuous_of_le_add_edist 1 (by simp) $ by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff] /-- The edist to a set and to its closure coincide -/ lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s := begin refine le_antisymm (inf_edist_anti subset_closure) _, refine ennreal.le_of_forall_pos_le_add (λε εpos h, _), have ε0 : 0 < (ε / 2 : ℝ≥0∞) := by simpa [pos_iff_ne_zero] using εpos, have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2, from ennreal.lt_add_right h.ne ε0.ne', rcases inf_edist_lt_iff.mp this with ⟨y, ycs, hy⟩, -- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2 rcases emetric.mem_closure_iff.1 ycs (ε/2) ε0 with ⟨z, zs, dyz⟩, -- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2 calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add (le_of_lt hy) (le_of_lt dyz) ... = inf_edist x (closure s) + ↑ε : by rw [add_assoc, ennreal.add_halves] end /-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/ lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 := ⟨λ h, by { rw ← inf_edist_closure, exact inf_edist_zero_of_mem h }, λ h, emetric.mem_closure_iff.2 $ λ ε εpos, inf_edist_lt_iff.mp $ by rwa h⟩ /-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/ lemma mem_iff_inf_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 := begin convert ← mem_closure_iff_inf_edist_zero, exact h.closure_eq end lemma disjoint_closed_ball_of_lt_inf_edist {r : ℝ≥0∞} (h : r < inf_edist x s) : disjoint (closed_ball x r) s := begin rw disjoint_left, assume y hy h'y, apply lt_irrefl (inf_edist x s), calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem h'y ... ≤ r : by rwa [mem_closed_ball, edist_comm] at hy ... < inf_edist x s : h end /-- The infimum edistance is invariant under isometries -/ lemma inf_edist_image (hΦ : isometry Φ) : inf_edist (Φ x) (Φ '' t) = inf_edist x t := by simp only [inf_edist, infi_image, hΦ.edist_eq] lemma _root_.is_open.exists_Union_is_closed {U : set α} (hU : is_open U) : ∃ F : ℕ → set α, (∀ n, is_closed (F n)) ∧ (∀ n, F n ⊆ U) ∧ ((⋃ n, F n) = U) ∧ monotone F := begin obtain ⟨a, a_pos, a_lt_one⟩ : ∃ (a : ℝ≥0∞), 0 < a ∧ a < 1 := exists_between (ennreal.zero_lt_one), let F := λ (n : ℕ), (λ x, inf_edist x Uᶜ) ⁻¹' (Ici (a^n)), have F_subset : ∀ n, F n ⊆ U, { assume n x hx, have : inf_edist x Uᶜ ≠ 0 := ((ennreal.pow_pos a_pos _).trans_le hx).ne', contrapose! this, exact inf_edist_zero_of_mem this }, refine ⟨F, λ n, is_closed.preimage continuous_inf_edist is_closed_Ici, F_subset, _, _⟩, show monotone F, { assume m n hmn x hx, simp only [mem_Ici, mem_preimage] at hx ⊢, apply le_trans (ennreal.pow_le_pow_of_le_one a_lt_one.le hmn) hx }, show (⋃ n, F n) = U, { refine subset.antisymm (by simp only [Union_subset_iff, F_subset, forall_const]) (λ x hx, _), have : ¬(x ∈ Uᶜ), by simpa using hx, rw mem_iff_inf_edist_zero_of_closed hU.is_closed_compl at this, have B : 0 < inf_edist x Uᶜ, by simpa [pos_iff_ne_zero] using this, have : filter.tendsto (λ n, a^n) at_top (𝓝 0) := ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 a_lt_one, rcases ((tendsto_order.1 this).2 _ B).exists with ⟨n, hn⟩, simp only [mem_Union, mem_Ici, mem_preimage], exact ⟨n, hn.le⟩ }, end lemma _root_.is_compact.exists_inf_edist_eq_edist (hs : is_compact s) (hne : s.nonempty) (x : α) : ∃ y ∈ s, inf_edist x s = edist x y := begin have A : continuous (λ y, edist x y) := continuous_const.edist continuous_id, obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, ∀ z, z ∈ s → edist x y ≤ edist x z := hs.exists_forall_le hne A.continuous_on, exact ⟨y, ys, le_antisymm (inf_edist_le_edist_of_mem ys) (by rwa le_inf_edist)⟩ end lemma exists_pos_forall_lt_edist (hs : is_compact s) (ht : is_closed t) (hst : disjoint s t) : ∃ r : ℝ≥0, 0 < r ∧ ∀ (x ∈ s) (y ∈ t), (r : ℝ≥0∞) < edist x y := begin rcases s.eq_empty_or_nonempty with rfl|hne, { use 1, simp }, obtain ⟨x, hx, h⟩ : ∃ x ∈ s, ∀ y ∈ s, inf_edist x t ≤ inf_edist y t := hs.exists_forall_le hne continuous_inf_edist.continuous_on, have : 0 < inf_edist x t, from pos_iff_ne_zero.2 (λ H, hst ⟨hx, (mem_iff_inf_edist_zero_of_closed ht).mpr H⟩), rcases ennreal.lt_iff_exists_nnreal_btwn.1 this with ⟨r, h₀, hr⟩, exact ⟨r, ennreal.coe_pos.mp h₀, λ y hy z hz, hr.trans_le $ le_inf_edist.1 (h y hy) z hz⟩ end end inf_edist --section /-! ### The Hausdorff distance as a function into `ℝ≥0∞`. -/ /-- The Hausdorff edistance between two sets is the smallest `r` such that each set is contained in the `r`-neighborhood of the other one -/ @[irreducible] def Hausdorff_edist {α : Type u} [pseudo_emetric_space α] (s t : set α) : ℝ≥0∞ := (⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s) lemma Hausdorff_edist_def {α : Type u} [pseudo_emetric_space α] (s t : set α) : Hausdorff_edist s t = (⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s) := by rw Hausdorff_edist section Hausdorff_edist variables [pseudo_emetric_space α] [pseudo_emetric_space β] {x y : α} {s t u : set α} {Φ : α → β} /-- The Hausdorff edistance of a set to itself vanishes -/ @[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 := begin simp only [Hausdorff_edist_def, sup_idem, ennreal.supr_eq_zero], exact λ x hx, inf_edist_zero_of_mem hx end /-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/ lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s := by unfold Hausdorff_edist; apply sup_comm /-- Bounding the Hausdorff edistance by bounding the edistance of any point in each set to the other set -/ lemma Hausdorff_edist_le_of_inf_edist {r : ℝ≥0∞} (H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) : Hausdorff_edist s t ≤ r := begin simp only [Hausdorff_edist, sup_le_iff, supr_le_iff], exact ⟨H1, H2⟩ end /-- Bounding the Hausdorff edistance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_edist_le_of_mem_edist {r : ℝ≥0∞} (H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) : Hausdorff_edist s t ≤ r := begin refine Hausdorff_edist_le_of_inf_edist _ _, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_edist_le_edist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_edist_le_edist_of_mem ys) hy } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t := begin rw Hausdorff_edist_def, refine le_trans _ le_sup_left, exact le_supr₂ x h end /-- If the Hausdorff distance is `<r`, then any point in one of the sets has a corresponding point at distance `<r` in the other set -/ lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ℝ≥0∞} (h : x ∈ s) (H : Hausdorff_edist s t < r) : ∃ y ∈ t, edist x y < r := inf_edist_lt_iff.mp $ calc inf_edist x t ≤ Hausdorff_edist s t : inf_edist_le_Hausdorff_edist_of_mem h ... < r : H /-- The distance from `x` to `s` or `t` is controlled in terms of the Hausdorff distance between `s` and `t` -/ lemma inf_edist_le_inf_edist_add_Hausdorff_edist : inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t := ennreal.le_of_forall_pos_le_add $ λε εpos h, begin have ε0 : (ε / 2 : ℝ≥0∞) ≠ 0 := by simpa [pos_iff_ne_zero] using εpos, have : inf_edist x s < inf_edist x s + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).1.ne ε0, rcases inf_edist_lt_iff.mp this with ⟨y, ys, dxy⟩, -- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2 have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).2.ne ε0, rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩, -- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2 calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add dxy.le dyz.le ... = inf_edist x s + Hausdorff_edist s t + ε : by simp [ennreal.add_halves, add_comm, add_left_comm] end /-- The Hausdorff edistance is invariant under eisometries -/ lemma Hausdorff_edist_image (h : isometry Φ) : Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t := by simp only [Hausdorff_edist_def, supr_image, inf_edist_image h] /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_edist_le_ediam (hs : s.nonempty) (ht : t.nonempty) : Hausdorff_edist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_edist_le_of_mem_edist _ _, { intros z hz, exact ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { intros z hz, exact ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u := begin rw Hausdorff_edist_def, simp only [sup_le_iff, supr_le_iff], split, show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist s t + Hausdorff_edist t u : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xs) _, show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist u t + Hausdorff_edist t s : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xu) _ ... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm] end /-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/ lemma Hausdorff_edist_zero_iff_closure_eq_closure : Hausdorff_edist s t = 0 ↔ closure s = closure t := calc Hausdorff_edist s t = 0 ↔ s ⊆ closure t ∧ t ⊆ closure s : by simp only [Hausdorff_edist_def, ennreal.sup_eq_zero, ennreal.supr_eq_zero, ← mem_closure_iff_inf_edist_zero, subset_def] ... ↔ closure s = closure t : ⟨λ h, subset.antisymm (closure_minimal h.1 is_closed_closure) (closure_minimal h.2 is_closed_closure), λ h, ⟨h ▸ subset_closure, h.symm ▸ subset_closure⟩⟩ /-- The Hausdorff edistance between a set and its closure vanishes -/ @[simp, priority 1100] lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, closure_closure] /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t := begin refine le_antisymm _ _, { calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle ... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] }, { calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t : Hausdorff_edist_triangle ... = Hausdorff_edist (closure s) t : by simp } end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t := by simp [@Hausdorff_edist_comm _ _ s _] /-- The Hausdorff edistance between sets or their closures is the same -/ @[simp] lemma Hausdorff_edist_closure : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t := by simp /-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/ lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) : Hausdorff_edist s t = 0 ↔ s = t := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, hs.closure_eq, ht.closure_eq] /-- The Haudorff edistance to the empty set is infinite -/ lemma Hausdorff_edist_empty (ne : s.nonempty) : Hausdorff_edist s ∅ = ∞ := begin rcases ne with ⟨x, xs⟩, have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs, simpa using this, end /-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/ lemma nonempty_of_Hausdorff_edist_ne_top (hs : s.nonempty) (fin : Hausdorff_edist s t ≠ ⊤) : t.nonempty := t.eq_empty_or_nonempty.elim (λ ht, (fin $ ht.symm ▸ Hausdorff_edist_empty hs).elim) id lemma empty_or_nonempty_of_Hausdorff_edist_ne_top (fin : Hausdorff_edist s t ≠ ⊤) : s = ∅ ∧ t = ∅ ∨ s.nonempty ∧ t.nonempty := begin cases s.eq_empty_or_nonempty with hs hs, { cases t.eq_empty_or_nonempty with ht ht, { exact or.inl ⟨hs, ht⟩ }, { rw Hausdorff_edist_comm at fin, exact or.inr ⟨nonempty_of_Hausdorff_edist_ne_top ht fin, ht⟩ } }, { exact or.inr ⟨hs, nonempty_of_Hausdorff_edist_ne_top hs fin⟩ } end end Hausdorff_edist -- section end emetric --namespace /-! Now, we turn to the same notions in metric spaces. To avoid the difficulties related to `Inf` and `Sup` on `ℝ` (which is only conditionally complete), we use the notions in `ℝ≥0∞` formulated in terms of the edistance, and coerce them to `ℝ`. Then their properties follow readily from the corresponding properties in `ℝ≥0∞`, modulo some tedious rewriting of inequalities from one to the other. -/ namespace metric section variables [pseudo_metric_space α] [pseudo_metric_space β] {s t u : set α} {x y : α} {Φ : α → β} open emetric /-! ### Distance of a point to a set as a function into `ℝ`. -/ /-- The minimal distance of a point to a set -/ def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s) /-- the minimal distance is always nonnegative -/ lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist] /-- the minimal distance to the empty set is 0 (if you want to have the more reasonable value ∞ instead, use `inf_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 := by simp [inf_dist] /-- In a metric space, the minimal edistance to a nonempty set is finite -/ lemma inf_edist_ne_top (h : s.nonempty) : inf_edist x s ≠ ⊤ := begin rcases h with ⟨y, hy⟩, apply lt_top_iff_ne_top.1, calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy ... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _) end /-- The minimal distance of a point to a set containing it vanishes -/ lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 := by simp [inf_edist_zero_of_mem h, inf_dist] /-- The minimal distance to a singleton is the distance to the unique point in this singleton -/ @[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y := by simp [inf_dist, inf_edist, dist_edist] /-- The minimal distance to a set is bounded by the distance to any point in this set -/ lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y := begin rw [dist_edist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ⟨_, h⟩) (edist_ne_top _ _)], exact inf_edist_le_edist_of_mem h end /-- The minimal distance is monotonous with respect to inclusion -/ lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s.nonempty) : inf_dist x t ≤ inf_dist x s := begin have ht : t.nonempty := hs.mono h, rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)], exact inf_edist_anti h end /-- The minimal distance to a set is `< r` iff there exists a point in this set at distance `< r` -/ lemma inf_dist_lt_iff {r : ℝ} (hs : s.nonempty) : inf_dist x s < r ↔ ∃ y ∈ s, dist x y < r := by simp_rw [inf_dist, ← ennreal.lt_of_real_iff_to_real_lt (inf_edist_ne_top hs), inf_edist_lt_iff, ennreal.lt_of_real_iff_to_real_lt (edist_ne_top _ _), ← dist_edist] /-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo the distance between `x` and `y` -/ lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y := begin cases s.eq_empty_or_nonempty with hs hs, { simp [hs, dist_nonneg] }, { rw [inf_dist, inf_dist, dist_edist, ← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _), ennreal.to_real_le_to_real (inf_edist_ne_top hs)], { exact inf_edist_le_inf_edist_add_edist }, { simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }} end lemma not_mem_of_dist_lt_inf_dist (h : dist x y < inf_dist x s) : y ∉ s := λ hy, h.not_le $ inf_dist_le_dist_of_mem hy lemma disjoint_ball_inf_dist : disjoint (ball x (inf_dist x s)) s := disjoint_left.2 $ λ y hy, not_mem_of_dist_lt_inf_dist $ calc dist x y = dist y x : dist_comm _ _ ... < inf_dist x s : hy lemma ball_inf_dist_subset_compl : ball x (inf_dist x s) ⊆ sᶜ := disjoint_ball_inf_dist.subset_compl_right lemma ball_inf_dist_compl_subset : ball x (inf_dist x sᶜ) ⊆ s := ball_inf_dist_subset_compl.trans (compl_compl s).subset lemma disjoint_closed_ball_of_lt_inf_dist {r : ℝ} (h : r < inf_dist x s) : disjoint (closed_ball x r) s := disjoint_ball_inf_dist.mono_left $ closed_ball_subset_ball h variable (s) /-- The minimal distance to a set is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_dist_pt : lipschitz_with 1 (λx, inf_dist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set is uniformly continuous in point -/ lemma uniform_continuous_inf_dist_pt : uniform_continuous (λx, inf_dist x s) := (lipschitz_inf_dist_pt s).uniform_continuous /-- The minimal distance to a set is continuous in point -/ @[continuity] lemma continuous_inf_dist_pt : continuous (λx, inf_dist x s) := (uniform_continuous_inf_dist_pt s).continuous variable {s} /-- The minimal distance to a set and its closure coincide -/ lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s := by simp [inf_dist, inf_edist_closure] /-- If a point belongs to the closure of `s`, then its infimum distance to `s` equals zero. The converse is true provided that `s` is nonempty, see `mem_closure_iff_inf_dist_zero`. -/ lemma inf_dist_zero_of_mem_closure (hx : x ∈ closure s) : inf_dist x s = 0 := by { rw ← inf_dist_eq_closure, exact inf_dist_zero_of_mem hx } /-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/ lemma mem_closure_iff_inf_dist_zero (h : s.nonempty) : x ∈ closure s ↔ inf_dist x s = 0 := by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h] /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ lemma _root_.is_closed.mem_iff_inf_dist_zero (h : is_closed s) (hs : s.nonempty) : x ∈ s ↔ inf_dist x s = 0 := by rw [←mem_closure_iff_inf_dist_zero hs, h.closure_eq] /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ lemma _root_.is_closed.not_mem_iff_inf_dist_pos (h : is_closed s) (hs : s.nonempty) : x ∉ s ↔ 0 < inf_dist x s := begin rw ← not_iff_not, push_neg, simp [h.mem_iff_inf_dist_zero hs, le_antisymm_iff, inf_dist_nonneg], end /-- The infimum distance is invariant under isometries -/ lemma inf_dist_image (hΦ : isometry Φ) : inf_dist (Φ x) (Φ '' t) = inf_dist x t := by simp [inf_dist, inf_edist_image hΦ] lemma inf_dist_inter_closed_ball_of_mem (h : y ∈ s) : inf_dist x (s ∩ closed_ball x (dist y x)) = inf_dist x s := begin replace h : y ∈ s ∩ closed_ball x (dist y x) := ⟨h, mem_closed_ball.2 le_rfl⟩, refine le_antisymm _ (inf_dist_le_inf_dist_of_subset (inter_subset_left _ _) ⟨y, h⟩), refine not_lt.1 (λ hlt, _), rcases (inf_dist_lt_iff ⟨y, h.1⟩).mp hlt with ⟨z, hzs, hz⟩, cases le_or_lt (dist z x) (dist y x) with hle hlt, { exact hz.not_le (inf_dist_le_dist_of_mem ⟨hzs, hle⟩) }, { rw [dist_comm z, dist_comm y] at hlt, exact (hlt.trans hz).not_le (inf_dist_le_dist_of_mem h) } end lemma _root_.is_compact.exists_inf_dist_eq_dist (h : is_compact s) (hne : s.nonempty) (x : α) : ∃ y ∈ s, inf_dist x s = dist x y := let ⟨y, hys, hy⟩ := h.exists_inf_edist_eq_edist hne x in ⟨y, hys, by rw [inf_dist, dist_edist, hy]⟩ lemma _root_.is_closed.exists_inf_dist_eq_dist [proper_space α] (h : is_closed s) (hne : s.nonempty) (x : α) : ∃ y ∈ s, inf_dist x s = dist x y := begin rcases hne with ⟨z, hz⟩, rw ← inf_dist_inter_closed_ball_of_mem hz, set t := s ∩ closed_ball x (dist z x), have htc : is_compact t := (is_compact_closed_ball x (dist z x)).inter_left h, have htne : t.nonempty := ⟨z, hz, mem_closed_ball.2 le_rfl⟩, obtain ⟨y, ⟨hys, hyx⟩, hyd⟩ : ∃ y ∈ t, inf_dist x t = dist x y := htc.exists_inf_dist_eq_dist htne x, exact ⟨y, hys, hyd⟩ end lemma exists_mem_closure_inf_dist_eq_dist [proper_space α] (hne : s.nonempty) (x : α) : ∃ y ∈ closure s, inf_dist x s = dist x y := by simpa only [inf_dist_eq_closure] using is_closed_closure.exists_inf_dist_eq_dist hne.closure x /-! ### Distance of a point to a set as a function into `ℝ≥0`. -/ /-- The minimal distance of a point to a set as a `ℝ≥0` -/ def inf_nndist (x : α) (s : set α) : ℝ≥0 := ennreal.to_nnreal (inf_edist x s) @[simp] lemma coe_inf_nndist : (inf_nndist x s : ℝ) = inf_dist x s := rfl /-- The minimal distance to a set (as `ℝ≥0`) is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_nndist_pt (s : set α) : lipschitz_with 1 (λx, inf_nndist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set (as `ℝ≥0`) is uniformly continuous in point -/ lemma uniform_continuous_inf_nndist_pt (s : set α) : uniform_continuous (λx, inf_nndist x s) := (lipschitz_inf_nndist_pt s).uniform_continuous /-- The minimal distance to a set (as `ℝ≥0`) is continuous in point -/ lemma continuous_inf_nndist_pt (s : set α) : continuous (λx, inf_nndist x s) := (uniform_continuous_inf_nndist_pt s).continuous /-! ### The Hausdorff distance as a function into `ℝ`. -/ /-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to be `0`, arbitrarily -/ def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t) /-- The Hausdorff distance is nonnegative -/ lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t := by simp [Hausdorff_dist] /-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance. -/ lemma Hausdorff_edist_ne_top_of_nonempty_of_bounded (hs : s.nonempty) (ht : t.nonempty) (bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ := begin rcases hs with ⟨cs, hcs⟩, rcases ht with ⟨ct, hct⟩, rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩, rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩, have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt), { apply Hausdorff_edist_le_of_mem_edist, { assume x xs, existsi [ct, hct], have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }, { assume x xt, existsi [cs, hcs], have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }}, exact ne_top_of_le_ne_top ennreal.of_real_ne_top this end /-- The Hausdorff distance between a set and itself is zero -/ @[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 := by simp [Hausdorff_dist] /-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/ lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s := by simp [Hausdorff_dist, Hausdorff_edist_comm] /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 := begin cases s.eq_empty_or_nonempty with h h, { simp [h] }, { simp [Hausdorff_dist, Hausdorff_edist_empty h] } end /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 := by simp [Hausdorff_dist_comm] /-- Bounding the Hausdorff distance by bounding the distance of any point in each set to the other set -/ lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) : Hausdorff_dist s t ≤ r := begin by_cases h1 : Hausdorff_edist s t = ⊤, { rwa [Hausdorff_dist, h1, ennreal.top_to_real] }, cases s.eq_empty_or_nonempty with hs hs, { rwa [hs, Hausdorff_dist_empty'] }, cases t.eq_empty_or_nonempty with ht ht, { rwa [ht, Hausdorff_dist_empty] }, have : Hausdorff_edist s t ≤ ennreal.of_real r, { apply Hausdorff_edist_le_of_inf_edist _ _, { assume x hx, have I := H1 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top ht) ennreal.of_real_ne_top] at I }, { assume x hx, have I := H2 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top hs) ennreal.of_real_ne_top] at I }}, rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real h1 ennreal.of_real_ne_top] end /-- Bounding the Hausdorff distance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) : Hausdorff_dist s t ≤ r := begin apply Hausdorff_dist_le_of_inf_dist hr, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_dist_le_dist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_dist_le_dist_of_mem ys) hy } end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_dist_le_diam (hs : s.nonempty) (bs : bounded s) (ht : t.nonempty) (bt : bounded t) : Hausdorff_dist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _, { exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ Hausdorff_dist s t := begin have ht : t.nonempty := nonempty_of_Hausdorff_edist_ne_top ⟨x, hx⟩ fin, rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin], exact inf_edist_le_Hausdorff_edist_of_mem hx end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r := begin have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H, have : Hausdorff_edist s t < ennreal.of_real r, { rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0), ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H }, rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr, exact ⟨y, hy, yr⟩ end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r := begin rw Hausdorff_dist_comm at H, rw Hausdorff_edist_comm at fin, simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin end /-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance between `s` and `t` -/ lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t := begin rcases empty_or_nonempty_of_Hausdorff_edist_ne_top fin with ⟨hs,ht⟩|⟨hs,ht⟩, { simp only [hs, ht, Hausdorff_dist_empty, inf_dist_empty, zero_add] }, rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top hs) fin, ennreal.to_real_le_to_real (inf_edist_ne_top ht)], { exact inf_edist_le_inf_edist_add_Hausdorff_edist }, { exact ennreal.add_ne_top.2 ⟨inf_edist_ne_top hs, fin⟩ } end /-- The Hausdorff distance is invariant under isometries -/ lemma Hausdorff_dist_image (h : isometry Φ) : Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t := by simp [Hausdorff_dist, Hausdorff_edist_image h] /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin by_cases Hausdorff_edist s u = ⊤, { calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h] ... ≤ Hausdorff_dist s t + Hausdorff_dist t u : add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) }, { have Dtu : Hausdorff_edist t u < ⊤ := calc Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle ... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm] ... < ⊤ : lt_top_iff_ne_top.mpr $ ennreal.add_ne_top.mpr ⟨fin, h⟩, rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist, ← ennreal.to_real_add fin Dtu.ne, ennreal.to_real_le_to_real h], { exact Hausdorff_edist_triangle }, { simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }} end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin rw Hausdorff_edist_comm at fin, have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s := Hausdorff_dist_triangle fin, simpa [add_comm, Hausdorff_dist_comm] using I end /-- The Hausdorff distance between a set and its closure vanish -/ @[simp, priority 1100] lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- The Hausdorff distance between two sets and their closures coincide -/ @[simp] lemma Hausdorff_dist_closure : Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/ lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ closure s = closure t := by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] /-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/ lemma _root_.is_closed.Hausdorff_dist_zero_iff_eq (hs : is_closed s) (ht : is_closed t) (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t := by simp [←Hausdorff_edist_zero_iff_eq_of_closed hs ht, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] end --section section thickening variables [pseudo_emetric_space α] {δ : ℝ} {s : set α} {x : α} open emetric /-- The (open) `δ`-thickening `thickening δ E` of a subset `E` in a pseudo emetric space consists of those points that are at distance less than `δ` from some point of `E`. -/ def thickening (δ : ℝ) (E : set α) : set α := {x : α | inf_edist x E < ennreal.of_real δ} lemma mem_thickening_iff_inf_edist_lt : x ∈ thickening δ s ↔ inf_edist x s < ennreal.of_real δ := iff.rfl /-- The (open) thickening equals the preimage of an open interval under `inf_edist`. -/ lemma thickening_eq_preimage_inf_edist (δ : ℝ) (E : set α) : thickening δ E = (λ x, inf_edist x E) ⁻¹' (Iio (ennreal.of_real δ)) := rfl /-- The (open) thickening is an open set. -/ lemma is_open_thickening {δ : ℝ} {E : set α} : is_open (thickening δ E) := continuous.is_open_preimage continuous_inf_edist _ is_open_Iio /-- The (open) thickening of the empty set is empty. -/ @[simp] lemma thickening_empty (δ : ℝ) : thickening δ (∅ : set α) = ∅ := by simp only [thickening, set_of_false, inf_edist_empty, not_top_lt] lemma thickening_of_nonpos (hδ : δ ≤ 0) (s : set α) : thickening δ s = ∅ := eq_empty_of_forall_not_mem $ λ x, ((ennreal.of_real_of_nonpos hδ).trans_le bot_le).not_lt /-- The (open) thickening `thickening δ E` of a fixed subset `E` is an increasing function of the thickening radius `δ`. -/ lemma thickening_mono {δ₁ δ₂ : ℝ} (hle : δ₁ ≤ δ₂) (E : set α) : thickening δ₁ E ⊆ thickening δ₂ E := preimage_mono (Iio_subset_Iio (ennreal.of_real_le_of_real hle)) /-- The (open) thickening `thickening δ E` with a fixed thickening radius `δ` is an increasing function of the subset `E`. -/ lemma thickening_subset_of_subset (δ : ℝ) {E₁ E₂ : set α} (h : E₁ ⊆ E₂) : thickening δ E₁ ⊆ thickening δ E₂ := λ _ hx, lt_of_le_of_lt (inf_edist_anti h) hx lemma mem_thickening_iff_exists_edist_lt {δ : ℝ} (E : set α) (x : α) : x ∈ thickening δ E ↔ ∃ z ∈ E, edist x z < ennreal.of_real δ := inf_edist_lt_iff variables {X : Type u} [pseudo_metric_space X] /-- A point in a metric space belongs to the (open) `δ`-thickening of a subset `E` if and only if it is at distance less than `δ` from some point of `E`. -/ lemma mem_thickening_iff {E : set X} {x : X} : x ∈ thickening δ E ↔ (∃ z ∈ E, dist x z < δ) := begin have key_iff : ∀ (z : X), edist x z < ennreal.of_real δ ↔ dist x z < δ, { intros z, rw dist_edist, have d_lt_top : edist x z < ∞, by simp only [edist_dist, ennreal.of_real_lt_top], have key := (@ennreal.of_real_lt_of_real_iff_of_nonneg ((edist x z).to_real) δ (ennreal.to_real_nonneg)), rwa ennreal.of_real_to_real d_lt_top.ne at key, }, simp_rw [mem_thickening_iff_exists_edist_lt, key_iff], end @[simp] lemma thickening_singleton (δ : ℝ) (x : X) : thickening δ ({x} : set X) = ball x δ := by { ext, simp [mem_thickening_iff] } /-- The (open) `δ`-thickening `thickening δ E` of a subset `E` in a metric space equals the union of balls of radius `δ` centered at points of `E`. -/ lemma thickening_eq_bUnion_ball {δ : ℝ} {E : set X} : thickening δ E = ⋃ x ∈ E, ball x δ := by { ext x, rw mem_Union₂, exact mem_thickening_iff } lemma bounded.thickening {δ : ℝ} {E : set X} (h : bounded E) : bounded (thickening δ E) := begin refine bounded_iff_mem_bounded.2 (λ x hx, _), rcases h.subset_ball x with ⟨R, hR⟩, refine (bounded_iff_subset_ball x).2 ⟨R + δ, _⟩, assume y hy, rcases mem_thickening_iff.1 hy with ⟨z, zE, hz⟩, calc dist y x ≤ dist z x + dist y z : by { rw add_comm, exact dist_triangle _ _ _ } ... ≤ R + δ : add_le_add (hR zE) hz.le end end thickening --section section cthickening variables [pseudo_emetric_space α] {δ ε : ℝ} {s t : set α} {x : α} open emetric /-- The closed `δ`-thickening `cthickening δ E` of a subset `E` in a pseudo emetric space consists of those points that are at infimum distance at most `δ` from `E`. -/ def cthickening (δ : ℝ) (E : set α) : set α := {x : α | inf_edist x E ≤ ennreal.of_real δ} @[simp] lemma mem_cthickening_iff : x ∈ cthickening δ s ↔ inf_edist x s ≤ ennreal.of_real δ := iff.rfl lemma mem_cthickening_of_edist_le (x y : α) (δ : ℝ) (E : set α) (h : y ∈ E) (h' : edist x y ≤ ennreal.of_real δ) : x ∈ cthickening δ E := (inf_edist_le_edist_of_mem h).trans h' lemma mem_cthickening_of_dist_le {α : Type*} [pseudo_metric_space α] (x y : α) (δ : ℝ) (E : set α) (h : y ∈ E) (h' : dist x y ≤ δ) : x ∈ cthickening δ E := begin apply mem_cthickening_of_edist_le x y δ E h, rw edist_dist, exact ennreal.of_real_le_of_real h', end lemma cthickening_eq_preimage_inf_edist (δ : ℝ) (E : set α) : cthickening δ E = (λ x, inf_edist x E) ⁻¹' (Iic (ennreal.of_real δ)) := rfl /-- The closed thickening is a closed set. -/ lemma is_closed_cthickening {δ : ℝ} {E : set α} : is_closed (cthickening δ E) := is_closed.preimage continuous_inf_edist is_closed_Iic /-- The closed thickening of the empty set is empty. -/ @[simp] lemma cthickening_empty (δ : ℝ) : cthickening δ (∅ : set α) = ∅ := by simp only [cthickening, ennreal.of_real_ne_top, set_of_false, inf_edist_empty, top_le_iff] lemma cthickening_of_nonpos {δ : ℝ} (hδ : δ ≤ 0) (E : set α) : cthickening δ E = closure E := by { ext x, simp [mem_closure_iff_inf_edist_zero, cthickening, ennreal.of_real_eq_zero.2 hδ] } /-- The closed thickening with radius zero is the closure of the set. -/ @[simp] lemma cthickening_zero (E : set α) : cthickening 0 E = closure E := cthickening_of_nonpos le_rfl E /-- The closed thickening `cthickening δ E` of a fixed subset `E` is an increasing function of the thickening radius `δ`. -/ lemma cthickening_mono {δ₁ δ₂ : ℝ} (hle : δ₁ ≤ δ₂) (E : set α) : cthickening δ₁ E ⊆ cthickening δ₂ E := preimage_mono (Iic_subset_Iic.mpr (ennreal.of_real_le_of_real hle)) @[simp] lemma cthickening_singleton {α : Type*} [pseudo_metric_space α] (x : α) {δ : ℝ} (hδ : 0 ≤ δ) : cthickening δ ({x} : set α) = closed_ball x δ := by { ext y, simp [cthickening, edist_dist, ennreal.of_real_le_of_real_iff hδ] } lemma closed_ball_subset_cthickening_singleton {α : Type*} [pseudo_metric_space α] (x : α) (δ : ℝ) : closed_ball x δ ⊆ cthickening δ ({x} : set α) := begin rcases lt_or_le δ 0 with hδ|hδ, { simp only [closed_ball_eq_empty.mpr hδ, empty_subset] }, { simp only [cthickening_singleton x hδ] } end /-- The closed thickening `cthickening δ E` with a fixed thickening radius `δ` is an increasing function of the subset `E`. -/ lemma cthickening_subset_of_subset (δ : ℝ) {E₁ E₂ : set α} (h : E₁ ⊆ E₂) : cthickening δ E₁ ⊆ cthickening δ E₂ := λ _ hx, le_trans (inf_edist_anti h) hx lemma cthickening_subset_thickening {δ₁ : ℝ≥0} {δ₂ : ℝ} (hlt : (δ₁ : ℝ) < δ₂) (E : set α) : cthickening δ₁ E ⊆ thickening δ₂ E := λ _ hx, lt_of_le_of_lt hx ((ennreal.of_real_lt_of_real_iff (lt_of_le_of_lt δ₁.prop hlt)).mpr hlt) /-- The closed thickening `cthickening δ₁ E` is contained in the open thickening `thickening δ₂ E` if the radius of the latter is positive and larger. -/ lemma cthickening_subset_thickening' {δ₁ δ₂ : ℝ} (δ₂_pos : 0 < δ₂) (hlt : δ₁ < δ₂) (E : set α) : cthickening δ₁ E ⊆ thickening δ₂ E := λ _ hx, lt_of_le_of_lt hx ((ennreal.of_real_lt_of_real_iff δ₂_pos).mpr hlt) /-- The open thickening `thickening δ E` is contained in the closed thickening `cthickening δ E` with the same radius. -/ lemma thickening_subset_cthickening (δ : ℝ) (E : set α) : thickening δ E ⊆ cthickening δ E := by { intros x hx, rw [thickening, mem_set_of_eq] at hx, exact hx.le, } lemma thickening_subset_cthickening_of_le {δ₁ δ₂ : ℝ} (hle : δ₁ ≤ δ₂) (E : set α) : thickening δ₁ E ⊆ cthickening δ₂ E := (thickening_subset_cthickening δ₁ E).trans (cthickening_mono hle E) lemma bounded.cthickening {α : Type*} [pseudo_metric_space α] {δ : ℝ} {E : set α} (h : bounded E) : bounded (cthickening δ E) := begin have : bounded (thickening (max (δ + 1) 1) E) := h.thickening, apply bounded.mono _ this, exact cthickening_subset_thickening' (zero_lt_one.trans_le (le_max_right _ _)) ((lt_add_one _).trans_le (le_max_left _ _)) _ end lemma thickening_subset_interior_cthickening (δ : ℝ) (E : set α) : thickening δ E ⊆ interior (cthickening δ E) := (subset_interior_iff_open.mpr (is_open_thickening)).trans (interior_mono (thickening_subset_cthickening δ E)) lemma closure_thickening_subset_cthickening (δ : ℝ) (E : set α) : closure (thickening δ E) ⊆ cthickening δ E := (closure_mono (thickening_subset_cthickening δ E)).trans is_closed_cthickening.closure_subset /-- The closed thickening of a set contains the closure of the set. -/ lemma closure_subset_cthickening (δ : ℝ) (E : set α) : closure E ⊆ cthickening δ E := by { rw ← cthickening_of_nonpos (min_le_right δ 0), exact cthickening_mono (min_le_left δ 0) E, } /-- The (open) thickening of a set contains the closure of the set. -/ lemma closure_subset_thickening {δ : ℝ} (δ_pos : 0 < δ) (E : set α) : closure E ⊆ thickening δ E := by { rw ← cthickening_zero, exact cthickening_subset_thickening' δ_pos δ_pos E, } /-- A set is contained in its own (open) thickening. -/ lemma self_subset_thickening {δ : ℝ} (δ_pos : 0 < δ) (E : set α) : E ⊆ thickening δ E := (@subset_closure _ _ E).trans (closure_subset_thickening δ_pos E) /-- A set is contained in its own closed thickening. -/ lemma self_subset_cthickening {δ : ℝ} (E : set α) : E ⊆ cthickening δ E := subset_closure.trans (closure_subset_cthickening δ E) lemma thickening_mem_nhds_set (E : set α) {δ : ℝ} (hδ : 0 < δ) : thickening δ E ∈ 𝓝ˢ E := is_open_thickening.mem_nhds_set.2 $ self_subset_thickening hδ E lemma cthickening_mem_nhds_set (E : set α) {δ : ℝ} (hδ : 0 < δ) : cthickening δ E ∈ 𝓝ˢ E := mem_of_superset (thickening_mem_nhds_set E hδ) (thickening_subset_cthickening _ _) @[simp] lemma thickening_union (δ : ℝ) (s t : set α) : thickening δ (s ∪ t) = thickening δ s ∪ thickening δ t := by simp_rw [thickening, inf_edist_union, inf_eq_min, min_lt_iff, set_of_or] @[simp] lemma cthickening_union (δ : ℝ) (s t : set α) : cthickening δ (s ∪ t) = cthickening δ s ∪ cthickening δ t := by simp_rw [cthickening, inf_edist_union, inf_eq_min, min_le_iff, set_of_or] @[simp] lemma thickening_Union (δ : ℝ) (f : ι → set α) : thickening δ (⋃ i, f i) = ⋃ i, thickening δ (f i) := by simp_rw [thickening, inf_edist_Union, infi_lt_iff, set_of_exists] @[simp] lemma thickening_closure : thickening δ (closure s) = thickening δ s := by simp_rw [thickening, inf_edist_closure] @[simp] lemma cthickening_closure : cthickening δ (closure s) = cthickening δ s := by simp_rw [cthickening, inf_edist_closure] open ennreal lemma _root_.disjoint.exists_thickenings (hst : disjoint s t) (hs : is_compact s) (ht : is_closed t) : ∃ δ, 0 < δ ∧ disjoint (thickening δ s) (thickening δ t) := begin obtain ⟨r, hr, h⟩ := exists_pos_forall_lt_edist hs ht hst, refine ⟨r / 2, half_pos (nnreal.coe_pos.2 hr), _⟩, rintro z ⟨hzs, hzt⟩, rw mem_thickening_iff_exists_edist_lt at hzs hzt, rw [← nnreal.coe_two, ← nnreal.coe_div, ennreal.of_real_coe_nnreal] at hzs hzt, obtain ⟨x, hx, hzx⟩ := hzs, obtain ⟨y, hy, hzy⟩ := hzt, refine (h x hx y hy).not_le _, calc edist x y ≤ edist z x + edist z y : edist_triangle_left _ _ _ ... ≤ ↑(r / 2) + ↑(r / 2) : add_le_add hzx.le hzy.le ... = r : by rw [← ennreal.coe_add, nnreal.add_halves] end lemma _root_.disjoint.exists_cthickenings (hst : disjoint s t) (hs : is_compact s) (ht : is_closed t) : ∃ δ, 0 < δ ∧ disjoint (cthickening δ s) (cthickening δ t) := begin obtain ⟨δ, hδ, h⟩ := hst.exists_thickenings hs ht, refine ⟨δ / 2, half_pos hδ, h.mono _ _⟩; exact (cthickening_subset_thickening' hδ (half_lt_self hδ) _), end lemma _root_.is_compact.exists_cthickening_subset_open (hs : is_compact s) (ht : is_open t) (hst : s ⊆ t) : ∃ δ, 0 < δ ∧ cthickening δ s ⊆ t := (hst.disjoint_compl_right.exists_cthickenings hs ht.is_closed_compl).imp $ λ δ h, ⟨h.1, disjoint_compl_right_iff_subset.1 $ h.2.mono_right $ self_subset_cthickening _⟩ lemma _root_.is_compact.exists_thickening_subset_open (hs : is_compact s) (ht : is_open t) (hst : s ⊆ t) : ∃ δ, 0 < δ ∧ thickening δ s ⊆ t := let ⟨δ, h₀, hδ⟩ := hs.exists_cthickening_subset_open ht hst in ⟨δ, h₀, (thickening_subset_cthickening _ _).trans hδ⟩ lemma has_basis_nhds_set_thickening {K : set α} (hK : is_compact K) : (𝓝ˢ K).has_basis (λ δ : ℝ, 0 < δ) (λ δ, thickening δ K) := (has_basis_nhds_set K).to_has_basis' (λ U hU, hK.exists_thickening_subset_open hU.1 hU.2) $ λ _, thickening_mem_nhds_set K lemma has_basis_nhds_set_cthickening {K : set α} (hK : is_compact K) : (𝓝ˢ K).has_basis (λ δ : ℝ, 0 < δ) (λ δ, cthickening δ K) := (has_basis_nhds_set K).to_has_basis' (λ U hU, hK.exists_cthickening_subset_open hU.1 hU.2) $ λ _, cthickening_mem_nhds_set K lemma cthickening_eq_Inter_cthickening' {δ : ℝ} (s : set ℝ) (hsδ : s ⊆ Ioi δ) (hs : ∀ ε, δ < ε → (s ∩ (Ioc δ ε)).nonempty) (E : set α) : cthickening δ E = ⋂ ε ∈ s, cthickening ε E := begin apply subset.antisymm, { exact subset_Inter₂ (λ _ hε, cthickening_mono (le_of_lt (hsδ hε)) E), }, { unfold thickening cthickening, intros x hx, simp only [mem_Inter, mem_set_of_eq] at *, apply ennreal.le_of_forall_pos_le_add, intros η η_pos _, rcases hs (δ + η) (lt_add_of_pos_right _ (nnreal.coe_pos.mpr η_pos)) with ⟨ε, ⟨hsε, hε⟩⟩, apply ((hx ε hsε).trans (ennreal.of_real_le_of_real hε.2)).trans, rw ennreal.coe_nnreal_eq η, exact ennreal.of_real_add_le, }, end lemma cthickening_eq_Inter_cthickening {δ : ℝ} (E : set α) : cthickening δ E = ⋂ (ε : ℝ) (h : δ < ε), cthickening ε E := begin apply cthickening_eq_Inter_cthickening' (Ioi δ) rfl.subset, simp_rw inter_eq_right_iff_subset.mpr Ioc_subset_Ioi_self, exact λ _ hε, nonempty_Ioc.mpr hε, end lemma cthickening_eq_Inter_thickening' {δ : ℝ} (δ_nn : 0 ≤ δ) (s : set ℝ) (hsδ : s ⊆ Ioi δ) (hs : ∀ ε, δ < ε → (s ∩ (Ioc δ ε)).nonempty) (E : set α) : cthickening δ E = ⋂ ε ∈ s, thickening ε E := begin refine (subset_Inter₂ $ λ ε hε, _).antisymm _, { obtain ⟨ε', hsε', hε'⟩ := hs ε (hsδ hε), have ss := cthickening_subset_thickening' (lt_of_le_of_lt δ_nn hε'.1) hε'.1 E, exact ss.trans (thickening_mono hε'.2 E), }, { rw cthickening_eq_Inter_cthickening' s hsδ hs E, exact Inter₂_mono (λ ε hε, thickening_subset_cthickening ε E) } end lemma cthickening_eq_Inter_thickening {δ : ℝ} (δ_nn : 0 ≤ δ) (E : set α) : cthickening δ E = ⋂ (ε : ℝ) (h : δ < ε), thickening ε E := begin apply cthickening_eq_Inter_thickening' δ_nn (Ioi δ) rfl.subset, simp_rw inter_eq_right_iff_subset.mpr Ioc_subset_Ioi_self, exact λ _ hε, nonempty_Ioc.mpr hε, end /-- The closure of a set equals the intersection of its closed thickenings of positive radii accumulating at zero. -/ lemma closure_eq_Inter_cthickening' (E : set α) (s : set ℝ) (hs : ∀ ε, 0 < ε → (s ∩ (Ioc 0 ε)).nonempty) : closure E = ⋂ δ ∈ s, cthickening δ E := begin by_cases hs₀ : s ⊆ Ioi 0, { rw ← cthickening_zero, apply cthickening_eq_Inter_cthickening' _ hs₀ hs, }, obtain ⟨δ, hδs, δ_nonpos⟩ := not_subset.mp hs₀, rw [set.mem_Ioi, not_lt] at δ_nonpos, apply subset.antisymm, { exact subset_Inter₂ (λ ε _, closure_subset_cthickening ε E), }, { rw ← cthickening_of_nonpos δ_nonpos E, exact bInter_subset_of_mem hδs, }, end /-- The closure of a set equals the intersection of its closed thickenings of positive radii. -/ lemma closure_eq_Inter_cthickening (E : set α) : closure E = ⋂ (δ : ℝ) (h : 0 < δ), cthickening δ E := by { rw ← cthickening_zero, exact cthickening_eq_Inter_cthickening E, } /-- The closure of a set equals the intersection of its open thickenings of positive radii accumulating at zero. -/ lemma closure_eq_Inter_thickening' (E : set α) (s : set ℝ) (hs₀ : s ⊆ Ioi 0) (hs : ∀ ε, 0 < ε → (s ∩ (Ioc 0 ε)).nonempty) : closure E = ⋂ δ ∈ s, thickening δ E := by { rw ← cthickening_zero, apply cthickening_eq_Inter_thickening' le_rfl _ hs₀ hs, } /-- The closure of a set equals the intersection of its (open) thickenings of positive radii. -/ lemma closure_eq_Inter_thickening (E : set α) : closure E = ⋂ (δ : ℝ) (h : 0 < δ), thickening δ E := by { rw ← cthickening_zero, exact cthickening_eq_Inter_thickening rfl.ge E, } /-- The frontier of the (open) thickening of a set is contained in an `inf_edist` level set. -/ lemma frontier_thickening_subset (E : set α) {δ : ℝ} (δ_pos : 0 < δ) : frontier (thickening δ E) ⊆ {x : α | inf_edist x E = ennreal.of_real δ} := begin have singleton_preim : {x : α | inf_edist x E = ennreal.of_real δ } = (λ x , inf_edist x E) ⁻¹' {ennreal.of_real δ}, { simp only [preimage, mem_singleton_iff] }, rw [thickening_eq_preimage_inf_edist, singleton_preim, ← (frontier_Iio' ⟨(0 : ℝ≥0∞), ennreal.of_real_pos.mpr δ_pos⟩)], exact continuous_inf_edist.frontier_preimage_subset (Iio (ennreal.of_real δ)), end /-- The frontier of the closed thickening of a set is contained in an `inf_edist` level set. -/ lemma frontier_cthickening_subset (E : set α) {δ : ℝ} : frontier (cthickening δ E) ⊆ {x : α | inf_edist x E = ennreal.of_real δ} := begin have singleton_preim : {x : α | inf_edist x E = ennreal.of_real δ } = (λ x , inf_edist x E) ⁻¹' {ennreal.of_real δ}, { simp only [preimage, mem_singleton_iff] }, rw [cthickening_eq_preimage_inf_edist, singleton_preim, ← frontier_Iic' ⟨∞, ennreal.of_real_lt_top⟩], exact continuous_inf_edist.frontier_preimage_subset (Iic (ennreal.of_real δ)), end /-- The closed ball of radius `δ` centered at a point of `E` is included in the closed thickening of `E`. -/ lemma closed_ball_subset_cthickening {α : Type*} [pseudo_metric_space α] {x : α} {E : set α} (hx : x ∈ E) (δ : ℝ) : closed_ball x δ ⊆ cthickening δ E := begin refine (closed_ball_subset_cthickening_singleton _ _).trans (cthickening_subset_of_subset _ _), simpa using hx, end /-- The closed thickening of a compact set `E` is the union of the balls `closed_ball x δ` over `x ∈ E`. -/ lemma _root_.is_compact.cthickening_eq_bUnion_closed_ball {α : Type*} [pseudo_metric_space α] {δ : ℝ} {E : set α} (hE : is_compact E) (hδ : 0 ≤ δ) : cthickening δ E = ⋃ x ∈ E, closed_ball x δ := begin rcases eq_empty_or_nonempty E with rfl|hne, { simp only [cthickening_empty, Union_false, Union_empty] }, refine subset.antisymm (λ x hx, _) (Union₂_subset $ λ x hx, closed_ball_subset_cthickening hx _), obtain ⟨y, yE, hy⟩ : ∃ y ∈ E, emetric.inf_edist x E = edist x y := hE.exists_inf_edist_eq_edist hne _, have D1 : edist x y ≤ ennreal.of_real δ := (le_of_eq hy.symm).trans hx, have D2 : dist x y ≤ δ, { rw edist_dist at D1, exact (ennreal.of_real_le_of_real_iff hδ).1 D1 }, exact mem_bUnion yE D2, end /-- For the equality, see `inf_edist_cthickening`. -/ lemma inf_edist_le_inf_edist_cthickening_add : inf_edist x s ≤ inf_edist x (cthickening δ s) + ennreal.of_real δ := begin refine le_of_forall_lt' (λ r h, _), simp_rw [←lt_tsub_iff_right, inf_edist_lt_iff, mem_cthickening_iff] at h, obtain ⟨y, hy, hxy⟩ := h, exact inf_edist_le_edist_add_inf_edist.trans_lt ((ennreal.add_lt_add_of_lt_of_le (hy.trans_lt ennreal.of_real_lt_top).ne hxy hy).trans_le (tsub_add_cancel_of_le $ le_self_add.trans (lt_tsub_iff_left.1 hxy).le).le), end /-- For the equality, see `inf_edist_thickening`. -/ lemma inf_edist_le_inf_edist_thickening_add : inf_edist x s ≤ inf_edist x (thickening δ s) + ennreal.of_real δ := inf_edist_le_inf_edist_cthickening_add.trans $ add_le_add_right (inf_edist_anti $ thickening_subset_cthickening _ _) _ /-- For the equality, see `thickening_thickening`. -/ @[simp] lemma thickening_thickening_subset (ε δ : ℝ) (s : set α) : thickening ε (thickening δ s) ⊆ thickening (ε + δ) s := begin obtain hε | hε := le_total ε 0, { simp only [thickening_of_nonpos hε, empty_subset] }, obtain hδ | hδ := le_total δ 0, { simp only [thickening_of_nonpos hδ, thickening_empty, empty_subset] }, intros x, simp_rw [mem_thickening_iff_exists_edist_lt, ennreal.of_real_add hε hδ], exact λ ⟨y, ⟨z, hz, hy⟩, hx⟩, ⟨z, hz, (edist_triangle _ _ _).trans_lt $ ennreal.add_lt_add hx hy⟩, end /-- For the equality, see `thickening_cthickening`. -/ @[simp] lemma thickening_cthickening_subset (ε : ℝ) (hδ : 0 ≤ δ) (s : set α) : thickening ε (cthickening δ s) ⊆ thickening (ε + δ) s := begin obtain hε | hε := le_total ε 0, { simp only [thickening_of_nonpos hε, empty_subset] }, intro x, simp_rw [mem_thickening_iff_exists_edist_lt, mem_cthickening_iff, ←inf_edist_lt_iff, ennreal.of_real_add hε hδ], rintro ⟨y, hy, hxy⟩, exact inf_edist_le_edist_add_inf_edist.trans_lt (ennreal.add_lt_add_of_lt_of_le (hy.trans_lt ennreal.of_real_lt_top).ne hxy hy), end /-- For the equality, see `cthickening_thickening`. -/ @[simp] lemma cthickening_thickening_subset (hε : 0 ≤ ε) (δ : ℝ) (s : set α) : cthickening ε (thickening δ s) ⊆ cthickening (ε + δ) s := begin obtain hδ | hδ := le_total δ 0, { simp only [thickening_of_nonpos hδ, cthickening_empty, empty_subset] }, intro x, simp_rw [mem_cthickening_iff, ennreal.of_real_add hε hδ], exact λ hx, inf_edist_le_inf_edist_thickening_add.trans (add_le_add_right hx _), end /-- For the equality, see `cthickening_cthickening`. -/ @[simp] lemma cthickening_cthickening_subset (hε : 0 ≤ ε) (hδ : 0 ≤ δ) (s : set α) : cthickening ε (cthickening δ s) ⊆ cthickening (ε + δ) s := begin intro x, simp_rw [mem_cthickening_iff, ennreal.of_real_add hε hδ], exact λ hx, inf_edist_le_inf_edist_cthickening_add.trans (add_le_add_right hx _), end end cthickening --section end metric --namespace
de789a4c17aa95c0d3818bf70cbba0e05fdd145d
54ce0561cebde424526f41d45f490ed56be2bd0c
/src/game/ch3_Set_Theory/3_Functions.lean
81dea77c283dffc76e6848d30121e2b766b31426
[]
no_license
melembroucarlitos/Tao_Analysis-LEAN
cf7b3298d317891a09e4bf21cfe7c7ffcb57b9a9
3f4fc7e090d96b6cef64896492fba4bef124794b
refs/heads/master
1,692,952,385,694
1,636,287,522,000
1,636,287,522,000
400,630,166
3
0
null
1,635,910,807,000
1,630,096,823,000
Lean
UTF-8
Lean
false
false
160
lean
-- Level name : Functions /- # Hey yall ## This is just to a placeholder to make sure all is working these are some words and these are some other words -/
a65716f4fc375e185ca2790c79a2eb669aa7c233
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0307.lean
b640432e5c4883043369ada7fe5cd6f601d9948a
[]
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
315
lean
universes u v def swap_pair {α : Type u} {β : Type v} : α × β → β × α := begin intro p, cases p with ha hb, constructor, exact hb, exact ha end def swap_sum {α : Type u} {β : Type v} : α ⊕ β → β ⊕ α := begin intro p, cases p with ha hb, right, exact ha, left, exact hb end
92dc9e2b32c15f5f5fbd95461d0da992e95343f6
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/finset/intervals.lean
9c2ed00c5f1bc6d39198bc7fbf68fd4072cbb403
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,086
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 data.finset.basic import data.multiset.intervals /-! # Intervals in ℕ as finsets For now this only covers `Ico n m`, the "closed-open" interval containing `[n, ..., m-1]`. -/ namespace finset open multiset nat /-! ### intervals -/ /- Ico (a closed open interval) -/ variables {n m l : ℕ} /-- `Ico n m` is the set of natural numbers `n ≤ k < m`. -/ def Ico (n m : ℕ) : finset ℕ := ⟨_, Ico.nodup n m⟩ namespace Ico @[simp] theorem val (n m : ℕ) : (Ico n m).1 = multiset.Ico n m := rfl @[simp] theorem to_finset (n m : ℕ) : (multiset.Ico n m).to_finset = Ico n m := (multiset.to_finset_eq _).symm theorem image_add (n m k : ℕ) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] theorem image_sub (n m k : ℕ) (h : k ≤ n) : (Ico n m).image (λ x, x - k) = Ico (n - k) (m - k) := begin dsimp [image], rw [multiset.Ico.map_sub _ _ _ h, ←multiset.to_finset_eq], refl, end theorem zero_bot (n : ℕ) : Ico 0 n = range n := eq_of_veq $ multiset.Ico.zero_bot _ @[simp] theorem card (n m : ℕ) : (Ico n m).card = m - n := multiset.Ico.card _ _ @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := multiset.Ico.mem @[simp, norm_cast] lemma coe_eq_Ico {n m : ℕ} : ↑(Ico n m) = set.Ico n m := begin ext, rw [set.mem_Ico, mem_coe, finset.Ico.mem], end theorem eq_empty_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = ∅ := eq_of_veq $ multiset.Ico.eq_zero_of_le h @[simp] theorem self_eq_empty (n : ℕ) : Ico n n = ∅ := eq_empty_of_le $ le_refl n @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = ∅ ↔ m ≤ n := iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff theorem subset_iff {m₁ n₁ m₂ n₂ : ℕ} (hmn : m₁ < n₁) : Ico m₁ n₁ ⊆ Ico m₂ n₂ ↔ (m₂ ≤ m₁ ∧ n₁ ≤ n₂) := begin simp only [subset_iff, mem], refine ⟨λ h, ⟨_, _⟩, _⟩, { exact (h ⟨le_refl _, hmn⟩).1 }, { refine le_of_pred_lt (@h (pred n₁) ⟨le_pred_of_lt hmn, pred_lt _⟩).2, exact ne_of_gt (lt_of_le_of_lt (nat.zero_le m₁) hmn) }, { rintros ⟨hm, hn⟩ k ⟨hmk, hkn⟩, exact ⟨le_trans hm hmk, lt_of_lt_of_le hkn hn⟩ } end protected theorem subset {m₁ n₁ m₂ n₂ : ℕ} (hmm : m₂ ≤ m₁) (hnn : n₁ ≤ n₂) : Ico m₁ n₁ ⊆ Ico m₂ n₂ := begin simp only [finset.subset_iff, Ico.mem], assume x hx, exact ⟨le_trans hmm hx.1, lt_of_lt_of_le hx.2 hnn⟩ end lemma union_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ∪ Ico m l = Ico n l := by rw [← to_finset, ← to_finset, ← multiset.to_finset_add, multiset.Ico.add_consecutive hnm hml, to_finset] lemma union' {n m l k : ℕ} (hlm : l ≤ m) (hnk : n ≤ k) : Ico n m ∪ Ico l k = Ico (min n l) (max m k) := by simp [←coe_inj, set.Ico_union_Ico' hlm hnk] lemma union {n m l k : ℕ} (h₁ : min n m ≤ max l k) (h₂ : min l k ≤ max n m) : Ico n m ∪ Ico l k = Ico (min n l) (max m k) := by simp [←coe_inj, set.Ico_union_Ico h₁ h₂] @[simp] lemma inter_consecutive (n m l : ℕ) : Ico n m ∩ Ico m l = ∅ := begin rw [← to_finset, ← to_finset, ← multiset.to_finset_inter, multiset.Ico.inter_consecutive], simp, end lemma inter {n m l k : ℕ} : Ico n m ∩ Ico l k = Ico (max n l) (min m k) := by simp [←coe_inj, ←inf_eq_min, ←sup_eq_max, set.Ico_inter_Ico] lemma disjoint_consecutive (n m l : ℕ) : disjoint (Ico n m) (Ico m l) := le_of_eq $ inter_consecutive n m l @[simp] theorem succ_singleton (n : ℕ) : Ico n (n+1) = {n} := eq_of_veq $ multiset.Ico.succ_singleton theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = insert m (Ico n m) := by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset] theorem succ_top' {n m : ℕ} (h : n < m) : Ico n m = insert (m - 1) (Ico n (m - 1)) := begin have w : m = m - 1 + 1 := (nat.sub_add_cancel (nat.one_le_of_lt h)).symm, conv { to_lhs, rw w }, rw succ_top, exact nat.le_pred_of_lt h end theorem insert_succ_bot {n m : ℕ} (h : n < m) : insert n (Ico (n + 1) m) = Ico n m := by rw [eq_comm, ← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : ℕ} (h : 0 < m) : Ico (m - 1) m = {m - 1} := eq_of_veq $ multiset.Ico.pred_singleton h @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := multiset.Ico.not_mem_top lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = ∅ := eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln lemma filter_Ico_bot {n m : ℕ} (hnm : n < m) : (Ico n m).filter (λ x, x ≤ n) = {n} := eq_of_veq $ multiset.Ico.filter_le_of_bot hnm lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := eq_of_veq $ multiset.Ico.filter_lt n m l lemma filter_le_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, l ≤ x) = Ico n m := eq_of_veq $ multiset.Ico.filter_le_of_le_bot hln lemma filter_le_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, l ≤ x) = ∅ := eq_of_veq $ multiset.Ico.filter_le_of_top_le hml lemma filter_le_of_le {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, l ≤ x) = Ico l m := eq_of_veq $ multiset.Ico.filter_le_of_le hnl @[simp] lemma filter_le (n m l : ℕ) : (Ico n m).filter (λ x, l ≤ x) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_le n m l @[simp] lemma diff_left (l n m : ℕ) : (Ico n m) \ (Ico n l) = Ico (max n l) m := by ext k; by_cases n ≤ k; simp [h, and_comm] @[simp] lemma diff_right (l n m : ℕ) : (Ico n m) \ (Ico l m) = Ico n (min m l) := have ∀k, (k < m ∧ (l ≤ k → m ≤ k)) ↔ (k < m ∧ k < l) := assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk], by ext k; by_cases n ≤ k; simp [h, this] lemma image_const_sub {k m n : ℕ} (hkn : k ≤ n) : (Ico k m).image (λ j, n - j) = Ico (n + 1 - m) (n + 1 - k) := begin rw [nat.sub_add_comm hkn], ext j, simp only [mem, mem_image, exists_prop, nat.lt_iff_add_one_le, add_le_add_iff_right], split, { rintros ⟨j, ⟨hjk, hjm⟩, rfl⟩, split, { simp only [← nat.add_sub_add_right n 1 j, nat.sub_le_sub_left, hjm] }, { exact nat.sub_le_sub_left _ hjk } }, { rintros ⟨hm, hk⟩, have hj : j ≤ n := le_trans hk (nat.sub_le_self _ _), refine ⟨n - j, ⟨_, _⟩, _⟩, { apply nat.le_sub_right_of_add_le, rwa nat.le_sub_left_iff_add_le hkn at hk }, { rwa [← nat.sub_add_comm hj, nat.sub_le_iff] }, { exact nat.sub_sub_self hj } } end end Ico lemma range_eq_Ico (n : ℕ) : finset.range n = finset.Ico 0 n := by { ext i, simp } lemma range_image_pred_top_sub (n : ℕ) : (finset.range n).image (λ j, n - 1 - j) = finset.range n := begin cases n, { simp }, { simp [range_eq_Ico, Ico.image_const_sub] } end -- TODO We don't yet attempt to reproduce the entire interface for `Ico` for `Ico_ℤ`. /-- `Ico_ℤ l u` is the set of integers `l ≤ k < u`. -/ def Ico_ℤ (l u : ℤ) : finset ℤ := (finset.range (u - l).to_nat).map { to_fun := λ n, n + l, inj' := λ n m h, by simpa using h } @[simp] lemma Ico_ℤ.mem {n m l : ℤ} : l ∈ Ico_ℤ n m ↔ n ≤ l ∧ l < m := begin dsimp [Ico_ℤ], simp only [int.lt_to_nat, exists_prop, mem_range, add_comm, function.embedding.coe_fn_mk, mem_map], split, { rintro ⟨a, ⟨h, rfl⟩⟩, exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ }, { rintro ⟨h₁, h₂⟩, use (l - n).to_nat, split; simp [h₁, h₂], } end @[simp] lemma Ico_ℤ.card (l u : ℤ) : (Ico_ℤ l u).card = (u - l).to_nat := by simp [Ico_ℤ] end finset
bf9d93051cae22d15db4f963d7668467b0f68e07
1e561612e7479c100cd9302e3fe08cbd2914aa25
/mathlib4_experiments/Data/List/Defs.lean
dfeca6413d0f9eb96b64807edabac05debe14a77
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib4_experiments
8de8ed7193f70748a7529e05d831203a7c64eedb
87cb879b4d602c8ecfd9283b7c0b06015abdbab1
refs/heads/master
1,687,971,389,316
1,620,336,942,000
1,620,336,942,000
353,994,588
7
4
Apache-2.0
1,622,410,748,000
1,617,361,732,000
Lean
UTF-8
Lean
false
false
35,013
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ --import data.option.defs import mathlib4_experiments.Logic.Basic import mathlib4_experiments.CoreExt import mathlib4_experiments.Commands.print_prefix --import tactic.cache /-! ## Definitions on lists This file contains various definitions on lists. It does not contain proofs about these definitions, those are contained in other files in `data/list` -/ namespace List open Function Nat -- open native (rb_map mk_rb_map rb_map.of_list) variable (R : α → α → Prop) /-- `pairwise R l` means that all the elements with earlier indexes are `R`-related to all the elements with later indexes. pairwise R [1, 2, 3] ↔ R 1 2 ∧ R 1 3 ∧ R 2 3 For example if `R = (≠)` then it asserts `l` has no duplicates, and if `R = (<)` then it asserts that `l` is (strictly) sorted. -/ inductive pairwise : List α → Prop | nil : pairwise [] | cons : ∀ {a : α} {l : List α}, (∀ a', a' ∈ l → R a a') → pairwise l → pairwise (a::l) /-- `nodup l` means that `l` has no duplicates, that is, any element appears at most once in the list. It is defined as `pairwise (≠)`. -/ def nodup : List α → Prop := pairwise (· ≠ ·) end List /- TO BE PORTED universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} instance [decidable_eq α] : has_sdiff (list α) := ⟨ list.diff ⟩ /-- Split a list at an index. split_at 2 [a, b, c] = ([a, b], [c]) -/ def split_at : ℕ → list α → list α × list α | 0 a := ([], a) | (succ n) [] := ([], []) | (succ n) (x :: xs) := let (l, r) := split_at n xs in (x :: l, r) /-- An auxiliary function for `split_on_p`. -/ def split_on_p_aux {α : Type u} (P : α → Prop) [decidable_pred P] : list α → (list α → list α) → list (list α) | [] f := [f []] | (h :: t) f := if P h then f [] :: split_on_p_aux t id else split_on_p_aux t (λ l, f (h :: l)) /-- Split a list at every element satisfying a predicate. -/ def split_on_p {α : Type u} (P : α → Prop) [decidable_pred P] (l : list α) : list (list α) := split_on_p_aux P l id /-- Split a list at every occurrence of an element. [1,1,2,3,2,4,4].split_on 2 = [[1,1],[3],[4,4]] -/ def split_on {α : Type u} [decidable_eq α] (a : α) (as : list α) : list (list α) := as.split_on_p (=a) /-- Concatenate an element at the end of a list. concat [a, b] c = [a, b, c] -/ @[simp] def concat : list α → α → list α | [] a := [a] | (b::l) a := b :: concat l a /-- `head' xs` returns the first element of `xs` if `xs` is non-empty; it returns `none` otherwise -/ @[simp] def head' : list α → option α | [] := none | (a :: l) := some a /-- Convert a list into an array (whose length is the length of `l`). -/ def to_array (l : list α) : array l.length α := {data := λ v, l.nth_le v.1 v.2} /-- "inhabited" `nth` function: returns `default` instead of `none` in the case that the index is out of bounds. -/ @[simp] def inth [h : inhabited α] (l : list α) (n : nat) : α := (nth l n).iget /-- Apply a function to the nth tail of `l`. Returns the input without using `f` if the index is larger than the length of the list. modify_nth_tail f 2 [a, b, c] = [a, b] ++ f [c] -/ @[simp] def modify_nth_tail (f : list α → list α) : ℕ → list α → list α | 0 l := f l | (n+1) [] := [] | (n+1) (a::l) := a :: modify_nth_tail n l /-- Apply `f` to the head of the list, if it exists. -/ @[simp] def modify_head (f : α → α) : list α → list α | [] := [] | (a::l) := f a :: l /-- Apply `f` to the nth element of the list, if it exists. -/ def modify_nth (f : α → α) : ℕ → list α → list α := modify_nth_tail (modify_head f) /-- Apply `f` to the last element of `l`, if it exists. -/ @[simp] def modify_last (f : α → α) : list α → list α | [] := [] | [x] := [f x] | (x :: xs) := x :: modify_last xs /-- `insert_nth n a l` inserts `a` into the list `l` after the first `n` elements of `l` `insert_nth 2 1 [1, 2, 3, 4] = [1, 2, 1, 3, 4]`-/ def insert_nth (n : ℕ) (a : α) : list α → list α := modify_nth_tail (list.cons a) n section take' variable [inhabited α] /-- Take `n` elements from a list `l`. If `l` has less than `n` elements, append `n - length l` elements `default α`. -/ def take' : ∀ n, list α → list α | 0 l := [] | (n+1) l := l.head :: take' n l.tail end take' /-- Get the longest initial segment of the list whose members all satisfy `p`. take_while (λ x, x < 3) [0, 2, 5, 1] = [0, 2] -/ def take_while (p : α → Prop) [decidable_pred p] : list α → list α | [] := [] | (a::l) := if p a then a :: take_while l else [] /-- Fold a function `f` over the list from the left, returning the list of partial results. scanl (+) 0 [1, 2, 3] = [0, 1, 3, 6] -/ def scanl (f : α → β → α) : α → list β → list α | a [] := [a] | a (b::l) := a :: scanl (f a b) l /-- Auxiliary definition used to define `scanr`. If `scanr_aux f b l = (b', l')` then `scanr f b l = b' :: l'` -/ def scanr_aux (f : α → β → β) (b : β) : list α → β × list β | [] := (b, []) | (a::l) := let (b', l') := scanr_aux l in (f a b', b' :: l') /-- Fold a function `f` over the list from the right, returning the list of partial results. scanr (+) 0 [1, 2, 3] = [6, 5, 3, 0] -/ def scanr (f : α → β → β) (b : β) (l : list α) : list β := let (b', l') := scanr_aux f b l in b' :: l' /-- Product of a list. prod [a, b, c] = ((1 * a) * b) * c -/ def prod [has_mul α] [has_one α] : list α → α := foldl (*) 1 /-- Sum of a list. sum [a, b, c] = ((0 + a) + b) + c -/ -- Later this will be tagged with `to_additive`, but this can't be done yet because of import -- dependencies. def sum [has_add α] [has_zero α] : list α → α := foldl (+) 0 /-- The alternating sum of a list. -/ def alternating_sum {G : Type*} [has_zero G] [has_add G] [has_neg G] : list G → G | [] := 0 | (g :: []) := g | (g :: h :: t) := g + -h + alternating_sum t /-- The alternating product of a list. -/ def alternating_prod {G : Type*} [has_one G] [has_mul G] [has_inv G] : list G → G | [] := 1 | (g :: []) := g | (g :: h :: t) := g * h⁻¹ * alternating_prod t /-- Given a function `f : α → β ⊕ γ`, `partition_map f l` maps the list by `f` whilst partitioning the result it into a pair of lists, `list β × list γ`, partitioning the `sum.inl _` into the left list, and the `sum.inr _` into the right list. `partition_map (id : ℕ ⊕ ℕ → ℕ ⊕ ℕ) [inl 0, inr 1, inl 2] = ([0,2], [1])` -/ def partition_map (f : α → β ⊕ γ) : list α → list β × list γ | [] := ([],[]) | (x::xs) := match f x with | (sum.inr r) := prod.map id (cons r) $ partition_map xs | (sum.inl l) := prod.map (cons l) id $ partition_map xs end /-- `find p l` is the first element of `l` satisfying `p`, or `none` if no such element exists. -/ def find (p : α → Prop) [decidable_pred p] : list α → option α | [] := none | (a::l) := if p a then some a else find l /-- `mfind tac l` returns the first element of `l` on which `tac` succeeds, and fails otherwise. -/ def mfind {α} {m : Type u → Type v} [monad m] [alternative m] (tac : α → m punit) : list α → m α := list.mfirst $ λ a, tac a $> a /-- `mbfind' p l` returns the first element `a` of `l` for which `p a` returns true. `mbfind'` short-circuits, so `p` is not necessarily run on every `a` in `l`. This is a monadic version of `list.find`. -/ def mbfind' {m : Type u → Type v} [monad m] {α : Type u} (p : α → m (ulift bool)) : list α → m (option α) | [] := pure none | (x :: xs) := do ⟨px⟩ ← p x, if px then pure (some x) else mbfind' xs section variables {m : Type → Type v} [monad m] /-- A variant of `mbfind'` with more restrictive universe levels. -/ def mbfind {α} (p : α → m bool) (xs : list α) : m (option α) := xs.mbfind' (functor.map ulift.up ∘ p) /-- `many p as` returns true iff `p` returns true for any element of `l`. `many` short-circuits, so if `p` returns true for any element of `l`, later elements are not checked. This is a monadic version of `list.any`. -/ -- Implementing this via `mbfind` would give us less universe polymorphism. def many {α : Type u} (p : α → m bool) : list α → m bool | [] := pure false | (x :: xs) := do px ← p x, if px then pure tt else many xs /-- `mall p as` returns true iff `p` returns true for all elements of `l`. `mall` short-circuits, so if `p` returns false for any element of `l`, later elements are not checked. This is a monadic version of `list.all`. -/ def mall {α : Type u} (p : α → m bool) (as : list α) : m bool := bnot <$> many (λ a, bnot <$> p a) as /-- `mbor xs` runs the actions in `xs`, returning true if any of them returns true. `mbor` short-circuits, so if an action returns true, later actions are not run. This is a monadic version of `list.bor`. -/ def mbor : list (m bool) → m bool := many id /-- `mband xs` runs the actions in `xs`, returning true if all of them return true. `mband` short-circuits, so if an action returns false, later actions are not run. This is a monadic version of `list.band`. -/ def mband : list (m bool) → m bool := mall id end /-- Auxiliary definition for `foldl_with_index`. -/ def foldl_with_index_aux (f : ℕ → α → β → α) : ℕ → α → list β → α | _ a [] := a | i a (b :: l) := foldl_with_index_aux (i + 1) (f i a b) l /-- Fold a list from left to right as with `foldl`, but the combining function also receives each element's index. -/ def foldl_with_index (f : ℕ → α → β → α) (a : α) (l : list β) : α := foldl_with_index_aux f 0 a l /-- Auxiliary definition for `foldr_with_index`. -/ def foldr_with_index_aux (f : ℕ → α → β → β) : ℕ → β → list α → β | _ b [] := b | i b (a :: l) := f i a (foldr_with_index_aux (i + 1) b l) /-- Fold a list from right to left as with `foldr`, but the combining function also receives each element's index. -/ def foldr_with_index (f : ℕ → α → β → β) (b : β) (l : list α) : β := foldr_with_index_aux f 0 b l /-- `find_indexes p l` is the list of indexes of elements of `l` that satisfy `p`. -/ def find_indexes (p : α → Prop) [decidable_pred p] (l : list α) : list nat := foldr_with_index (λ i a is, if p a then i :: is else is) [] l /-- Returns the elements of `l` that satisfy `p` together with their indexes in `l`. The returned list is ordered by index. -/ def indexes_values (p : α → Prop) [decidable_pred p] (l : list α) : list (ℕ × α) := foldr_with_index (λ i a l, if p a then (i , a) :: l else l) [] l /-- `indexes_of a l` is the list of all indexes of `a` in `l`. For example: ``` indexes_of a [a, b, a, a] = [0, 2, 3] ``` -/ def indexes_of [decidable_eq α] (a : α) : list α → list nat := find_indexes (eq a) section mfold_with_index variables {m : Type v → Type w} [monad m] /-- Monadic variant of `foldl_with_index`. -/ def mfoldl_with_index {α β} (f : ℕ → β → α → m β) (b : β) (as : list α) : m β := as.foldl_with_index (λ i ma b, do a ← ma, f i a b) (pure b) /-- Monadic variant of `foldr_with_index`. -/ def mfoldr_with_index {α β} (f : ℕ → α → β → m β) (b : β) (as : list α) : m β := as.foldr_with_index (λ i a mb, do b ← mb, f i a b) (pure b) end mfold_with_index section mmap_with_index variables {m : Type v → Type w} [applicative m] /-- Auxiliary definition for `mmap_with_index`. -/ def mmap_with_index_aux {α β} (f : ℕ → α → m β) : ℕ → list α → m (list β) | _ [] := pure [] | i (a :: as) := list.cons <$> f i a <*> mmap_with_index_aux (i + 1) as /-- Applicative variant of `map_with_index`. -/ def mmap_with_index {α β} (f : ℕ → α → m β) (as : list α) : m (list β) := mmap_with_index_aux f 0 as /-- Auxiliary definition for `mmap_with_index'`. -/ def mmap_with_index'_aux {α} (f : ℕ → α → m punit) : ℕ → list α → m punit | _ [] := pure ⟨⟩ | i (a :: as) := f i a *> mmap_with_index'_aux (i + 1) as /-- A variant of `mmap_with_index` specialised to applicative actions which return `unit`. -/ def mmap_with_index' {α} (f : ℕ → α → m punit) (as : list α) : m punit := mmap_with_index'_aux f 0 as end mmap_with_index /-- `lookmap` is a combination of `lookup` and `filter_map`. `lookmap f l` will apply `f : α → option α` to each element of the list, replacing `a → b` at the first value `a` in the list such that `f a = some b`. -/ def lookmap (f : α → option α) : list α → list α | [] := [] | (a::l) := match f a with | some b := b :: l | none := a :: lookmap l end /-- `countp p l` is the number of elements of `l` that satisfy `p`. -/ def countp (p : α → Prop) [decidable_pred p] : list α → nat | [] := 0 | (x::xs) := if p x then succ (countp xs) else countp xs /-- `count a l` is the number of occurrences of `a` in `l`. -/ def count [decidable_eq α] (a : α) : list α → nat := countp (eq a) /-- `is_prefix l₁ l₂`, or `l₁ <+: l₂`, means that `l₁` is a prefix of `l₂`, that is, `l₂` has the form `l₁ ++ t` for some `t`. -/ def is_prefix (l₁ : list α) (l₂ : list α) : Prop := ∃ t, l₁ ++ t = l₂ /-- `is_suffix l₁ l₂`, or `l₁ <:+ l₂`, means that `l₁` is a suffix of `l₂`, that is, `l₂` has the form `t ++ l₁` for some `t`. -/ def is_suffix (l₁ : list α) (l₂ : list α) : Prop := ∃ t, t ++ l₁ = l₂ /-- `is_infix l₁ l₂`, or `l₁ <:+: l₂`, means that `l₁` is a contiguous substring of `l₂`, that is, `l₂` has the form `s ++ l₁ ++ t` for some `s, t`. -/ def is_infix (l₁ : list α) (l₂ : list α) : Prop := ∃ s t, s ++ l₁ ++ t = l₂ infix ` <+: `:50 := is_prefix infix ` <:+ `:50 := is_suffix infix ` <:+: `:50 := is_infix /-- `inits l` is the list of initial segments of `l`. inits [1, 2, 3] = [[], [1], [1, 2], [1, 2, 3]] -/ @[simp] def inits : list α → list (list α) | [] := [[]] | (a::l) := [] :: map (λt, a::t) (inits l) /-- `tails l` is the list of terminal segments of `l`. tails [1, 2, 3] = [[1, 2, 3], [2, 3], [3], []] -/ @[simp] def tails : list α → list (list α) | [] := [[]] | (a::l) := (a::l) :: tails l def sublists'_aux : list α → (list α → list β) → list (list β) → list (list β) | [] f r := f [] :: r | (a::l) f r := sublists'_aux l f (sublists'_aux l (f ∘ cons a) r) /-- `sublists' l` is the list of all (non-contiguous) sublists of `l`. It differs from `sublists` only in the order of appearance of the sublists; `sublists'` uses the first element of the list as the MSB, `sublists` uses the first element of the list as the LSB. sublists' [1, 2, 3] = [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]] -/ def sublists' (l : list α) : list (list α) := sublists'_aux l id [] def sublists_aux : list α → (list α → list β → list β) → list β | [] f := [] | (a::l) f := f [a] (sublists_aux l (λys r, f ys (f (a :: ys) r))) /-- `sublists l` is the list of all (non-contiguous) sublists of `l`; cf. `sublists'` for a different ordering. sublists [1, 2, 3] = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] -/ def sublists (l : list α) : list (list α) := [] :: sublists_aux l cons def sublists_aux₁ : list α → (list α → list β) → list β | [] f := [] | (a::l) f := f [a] ++ sublists_aux₁ l (λys, f ys ++ f (a :: ys)) section forall₂ variables {r : α → β → Prop} {p : γ → δ → Prop} /-- `forall₂ R l₁ l₂` means that `l₁` and `l₂` have the same length, and whenever `a` is the nth element of `l₁`, and `b` is the nth element of `l₂`, then `R a b` is satisfied. -/ inductive forall₂ (R : α → β → Prop) : list α → list β → Prop | nil : forall₂ [] [] | cons {a b l₁ l₂} : R a b → forall₂ l₁ l₂ → forall₂ (a::l₁) (b::l₂) attribute [simp] forall₂.nil end forall₂ /-- Auxiliary definition used to define `transpose`. `transpose_aux l L` takes each element of `l` and appends it to the start of each element of `L`. `transpose_aux [a, b, c] [l₁, l₂, l₃] = [a::l₁, b::l₂, c::l₃]` -/ def transpose_aux : list α → list (list α) → list (list α) | [] ls := ls | (a::i) [] := [a] :: transpose_aux i [] | (a::i) (l::ls) := (a::l) :: transpose_aux i ls /-- transpose of a list of lists, treated as a matrix. transpose [[1, 2], [3, 4], [5, 6]] = [[1, 3, 5], [2, 4, 6]] -/ def transpose : list (list α) → list (list α) | [] := [] | (l::ls) := transpose_aux l (transpose ls) /-- List of all sections through a list of lists. A section of `[L₁, L₂, ..., Lₙ]` is a list whose first element comes from `L₁`, whose second element comes from `L₂`, and so on. -/ def sections : list (list α) → list (list α) | [] := [[]] | (l::L) := bind (sections L) $ λ s, map (λ a, a::s) l section permutations def permutations_aux2 (t : α) (ts : list α) (r : list β) : list α → (list α → β) → list α × list β | [] f := (ts, r) | (y::ys) f := let (us, zs) := permutations_aux2 ys (λx : list α, f (y::x)) in (y :: us, f (t :: y :: us) :: zs) private def meas : (Σ'_:list α, list α) → ℕ × ℕ | ⟨l, i⟩ := (length l + length i, length l) local infix ` ≺ `:50 := inv_image (prod.lex (<) (<)) meas @[elab_as_eliminator] def permutations_aux.rec {C : list α → list α → Sort v} (H0 : ∀ is, C [] is) (H1 : ∀ t ts is, C ts (t::is) → C is [] → C (t::ts) is) : ∀ l₁ l₂, C l₁ l₂ | [] is := H0 is | (t::ts) is := have h1 : ⟨ts, t :: is⟩ ≺ ⟨t :: ts, is⟩, from show prod.lex _ _ (succ (length ts + length is), length ts) (succ (length ts) + length is, length (t :: ts)), by rw nat.succ_add; exact prod.lex.right _ (lt_succ_self _), have h2 : ⟨is, []⟩ ≺ ⟨t :: ts, is⟩, from prod.lex.left _ _ (nat.lt_add_of_pos_left (succ_pos _)), H1 t ts is (permutations_aux.rec ts (t::is)) (permutations_aux.rec is []) using_well_founded { dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨(≺), @inv_image.wf _ _ _ meas (prod.lex_wf lt_wf lt_wf)⟩] } def permutations_aux : list α → list α → list (list α) := @@permutations_aux.rec (λ _ _, list (list α)) (λ is, []) (λ t ts is IH1 IH2, foldr (λy r, (permutations_aux2 t ts r y id).2) IH1 (is :: IH2)) /-- List of all permutations of `l`. permutations [1, 2, 3] = [[1, 2, 3], [2, 1, 3], [3, 2, 1], [2, 3, 1], [3, 1, 2], [1, 3, 2]] -/ def permutations (l : list α) : list (list α) := l :: permutations_aux l [] end permutations /-- `erasep p l` removes the first element of `l` satisfying the predicate `p`. -/ def erasep (p : α → Prop) [decidable_pred p] : list α → list α | [] := [] | (a::l) := if p a then l else a :: erasep l /-- `extractp p l` returns a pair of an element `a` of `l` satisfying the predicate `p`, and `l`, with `a` removed. If there is no such element `a` it returns `(none, l)`. -/ def extractp (p : α → Prop) [decidable_pred p] : list α → option α × list α | [] := (none, []) | (a::l) := if p a then (some a, l) else let (a', l') := extractp l in (a', a :: l') /-- `revzip l` returns a list of pairs of the elements of `l` paired with the elements of `l` in reverse order. `revzip [1,2,3,4,5] = [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]` -/ def revzip (l : list α) : list (α × α) := zip l l.reverse /-- `product l₁ l₂` is the list of pairs `(a, b)` where `a ∈ l₁` and `b ∈ l₂`. product [1, 2] [5, 6] = [(1, 5), (1, 6), (2, 5), (2, 6)] -/ def product (l₁ : list α) (l₂ : list β) : list (α × β) := l₁.bind $ λ a, l₂.map $ prod.mk a /-- `sigma l₁ l₂` is the list of dependent pairs `(a, b)` where `a ∈ l₁` and `b ∈ l₂ a`. sigma [1, 2] (λ_, [(5 : ℕ), 6]) = [(1, 5), (1, 6), (2, 5), (2, 6)] -/ protected def sigma {σ : α → Type*} (l₁ : list α) (l₂ : Π a, list (σ a)) : list (Σ a, σ a) := l₁.bind $ λ a, (l₂ a).map $ sigma.mk a /-- Auxliary definition used to define `of_fn`. `of_fn_aux f m h l` returns the first `m` elements of `of_fn f` appended to `l` -/ def of_fn_aux {n} (f : fin n → α) : ∀ m, m ≤ n → list α → list α | 0 h l := l | (succ m) h l := of_fn_aux m (le_of_lt h) (f ⟨m, h⟩ :: l) /-- `of_fn f` with `f : fin n → α` returns the list whose ith element is `f i` `of_fun f = [f 0, f 1, ... , f(n - 1)]` -/ def of_fn {n} (f : fin n → α) : list α := of_fn_aux f n (le_refl _) [] /-- `of_fn_nth_val f i` returns `some (f i)` if `i < n` and `none` otherwise. -/ def of_fn_nth_val {n} (f : fin n → α) (i : ℕ) : option α := if h : i < n then some (f ⟨i, h⟩) else none /-- `disjoint l₁ l₂` means that `l₁` and `l₂` have no elements in common. -/ def disjoint (l₁ l₂ : list α) : Prop := ∀ ⦃a⦄, a ∈ l₁ → a ∈ l₂ → false section pairwise -- kmb ported the definition variables (R : α → α → Prop) variables {R} @[simp] theorem pairwise_cons {a : α} {l : list α} : pairwise R (a::l) ↔ (∀ a' ∈ l, R a a') ∧ pairwise R l := ⟨λ p, by cases p with a l n p; exact ⟨n, p⟩, λ ⟨n, p⟩, p.cons n⟩ attribute [simp] pairwise.nil instance decidable_pairwise [decidable_rel R] (l : list α) : decidable (pairwise R l) := by induction l with hd tl ih; [exact is_true pairwise.nil, exactI decidable_of_iff' _ pairwise_cons] end pairwise /-- `pw_filter R l` is a maximal sublist of `l` which is `pairwise R`. `pw_filter (≠)` is the erase duplicates function (cf. `erase_dup`), and `pw_filter (<)` finds a maximal increasing subsequence in `l`. For example, pw_filter (<) [0, 1, 5, 2, 6, 3, 4] = [0, 1, 2, 3, 4] -/ def pw_filter (R : α → α → Prop) [decidable_rel R] : list α → list α | [] := [] | (x :: xs) := let IH := pw_filter xs in if ∀ y ∈ IH, R x y then x :: IH else IH section chain variable (R : α → α → Prop) /-- `chain R a l` means that `R` holds between adjacent elements of `a::l`. chain R a [b, c, d] ↔ R a b ∧ R b c ∧ R c d -/ inductive chain : α → list α → Prop | nil {a : α} : chain a [] | cons : ∀ {a b : α} {l : list α}, R a b → chain b l → chain a (b::l) /-- `chain' R l` means that `R` holds between adjacent elements of `l`. chain' R [a, b, c, d] ↔ R a b ∧ R b c ∧ R c d -/ def chain' : list α → Prop | [] := true | (a :: l) := chain R a l variable {R} @[simp] theorem chain_cons {a b : α} {l : list α} : chain R a (b::l) ↔ R a b ∧ chain R b l := ⟨λ p, by cases p with _ a b l n p; exact ⟨n, p⟩, λ ⟨n, p⟩, p.cons n⟩ attribute [simp] chain.nil instance decidable_chain [decidable_rel R] (a : α) (l : list α) : decidable (chain R a l) := by induction l generalizing a; simp only [chain.nil, chain_cons]; resetI; apply_instance instance decidable_chain' [decidable_rel R] (l : list α) : decidable (chain' R l) := by cases l; dunfold chain'; apply_instance end chain -- kmb : I ported def of `nodup` instance nodup_decidable [decidable_eq α] : ∀ l : list α, decidable (nodup l) := list.decidable_pairwise /-- `erase_dup l` removes duplicates from `l` (taking only the first occurrence). Defined as `pw_filter (≠)`. erase_dup [1, 0, 2, 2, 1] = [0, 2, 1] -/ def erase_dup [decidable_eq α] : list α → list α := pw_filter (≠) /-- `range' s n` is the list of numbers `[s, s+1, ..., s+n-1]`. It is intended mainly for proving properties of `range` and `iota`. -/ @[simp] def range' : ℕ → ℕ → list ℕ | s 0 := [] | s (n+1) := s :: range' (s+1) n /-- Drop `none`s from a list, and replace each remaining `some a` with `a`. -/ def reduce_option {α} : list (option α) → list α := list.filter_map id /-- `ilast' x xs` returns the last element of `xs` if `xs` is non-empty; it returns `x` otherwise -/ @[simp] def ilast' {α} : α → list α → α | a [] := a | a (b::l) := ilast' b l /-- `last' xs` returns the last element of `xs` if `xs` is non-empty; it returns `none` otherwise -/ @[simp] def last' {α} : list α → option α | [] := none | [a] := some a | (b::l) := last' l /-- `rotate l n` rotates the elements of `l` to the left by `n` rotate [0, 1, 2, 3, 4, 5] 2 = [2, 3, 4, 5, 0, 1] -/ def rotate (l : list α) (n : ℕ) : list α := let (l₁, l₂) := list.split_at (n % l.length) l in l₂ ++ l₁ /-- rotate' is the same as `rotate`, but slower. Used for proofs about `rotate`-/ def rotate' : list α → ℕ → list α | [] n := [] | l 0 := l | (a::l) (n+1) := rotate' (l ++ [a]) n section choose variables (p : α → Prop) [decidable_pred p] (l : list α) /-- Given a decidable predicate `p` and a proof of existence of `a ∈ l` such that `p a`, choose the first element with this property. This version returns both `a` and proofs of `a ∈ l` and `p a`. -/ def choose_x : Π l : list α, Π hp : (∃ a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } | [] hp := false.elim (exists.elim hp (assume a h, not_mem_nil a h.left)) | (l :: ls) hp := if pl : p l then ⟨l, ⟨or.inl rfl, pl⟩⟩ else let ⟨a, ⟨a_mem_ls, pa⟩⟩ := choose_x ls (hp.imp (λ b ⟨o, h₂⟩, ⟨o.resolve_left (λ e, pl $ e ▸ h₂), h₂⟩)) in ⟨a, ⟨or.inr a_mem_ls, pa⟩⟩ /-- Given a decidable predicate `p` and a proof of existence of `a ∈ l` such that `p a`, choose the first element with this property. This version returns `a : α`, and properties are given by `choose_mem` and `choose_property`. -/ def choose (hp : ∃ a, a ∈ l ∧ p a) : α := choose_x p l hp end choose /-- Filters and maps elements of a list -/ def mmap_filter {m : Type → Type v} [monad m] {α β} (f : α → m (option β)) : list α → m (list β) | [] := return [] | (h :: t) := do b ← f h, t' ← t.mmap_filter, return $ match b with none := t' | (some x) := x::t' end /-- `mmap_upper_triangle f l` calls `f` on all elements in the upper triangular part of `l × l`. That is, for each `e ∈ l`, it will run `f e e` and then `f e e'` for each `e'` that appears after `e` in `l`. Example: suppose `l = [1, 2, 3]`. `mmap_upper_triangle f l` will produce the list `[f 1 1, f 1 2, f 1 3, f 2 2, f 2 3, f 3 3]`. -/ def mmap_upper_triangle {m} [monad m] {α β : Type u} (f : α → α → m β) : list α → m (list β) | [] := return [] | (h::t) := do v ← f h h, l ← t.mmap (f h), t ← t.mmap_upper_triangle, return $ (v::l) ++ t /-- `mmap'_diag f l` calls `f` on all elements in the upper triangular part of `l × l`. That is, for each `e ∈ l`, it will run `f e e` and then `f e e'` for each `e'` that appears after `e` in `l`. Example: suppose `l = [1, 2, 3]`. `mmap'_diag f l` will evaluate, in this order, `f 1 1`, `f 1 2`, `f 1 3`, `f 2 2`, `f 2 3`, `f 3 3`. -/ def mmap'_diag {m} [monad m] {α} (f : α → α → m unit) : list α → m unit | [] := return () | (h::t) := f h h >> t.mmap' (f h) >> t.mmap'_diag protected def traverse {F : Type u → Type v} [applicative F] {α β : Type*} (f : α → F β) : list α → F (list β) | [] := pure [] | (x :: xs) := list.cons <$> f x <*> traverse xs /-- `get_rest l l₁` returns `some l₂` if `l = l₁ ++ l₂`. If `l₁` is not a prefix of `l`, returns `none` -/ def get_rest [decidable_eq α] : list α → list α → option (list α) | l [] := some l | [] _ := none | (x::l) (y::l₁) := if x = y then get_rest l l₁ else none /-- `list.slice n m xs` removes a slice of length `m` at index `n` in list `xs`. -/ def slice {α} : ℕ → ℕ → list α → list α | 0 n xs := xs.drop n | (succ n) m [] := [] | (succ n) m (x :: xs) := x :: slice n m xs /-- Left-biased version of `list.map₂`. `map₂_left' f as bs` applies `f` to each pair of elements `aᵢ ∈ as` and `bᵢ ∈ bs`. If `bs` is shorter than `as`, `f` is applied to `none` for the remaining `aᵢ`. Returns the results of the `f` applications and the remaining `bs`. ``` map₂_left' prod.mk [1, 2] ['a'] = ([(1, some 'a'), (2, none)], []) map₂_left' prod.mk [1] ['a', 'b'] = ([(1, some 'a')], ['b']) ``` -/ @[simp] def map₂_left' (f : α → option β → γ) : list α → list β → (list γ × list β) | [] bs := ([], bs) | (a :: as) [] := ((a :: as).map (λ a, f a none), []) | (a :: as) (b :: bs) := let rec := map₂_left' as bs in (f a (some b) :: rec.fst, rec.snd) /-- Right-biased version of `list.map₂`. `map₂_right' f as bs` applies `f` to each pair of elements `aᵢ ∈ as` and `bᵢ ∈ bs`. If `as` is shorter than `bs`, `f` is applied to `none` for the remaining `bᵢ`. Returns the results of the `f` applications and the remaining `as`. ``` map₂_right' prod.mk [1] ['a', 'b'] = ([(some 1, 'a'), (none, 'b')], []) map₂_right' prod.mk [1, 2] ['a'] = ([(some 1, 'a')], [2]) ``` -/ def map₂_right' (f : option α → β → γ) (as : list α) (bs : list β) : (list γ × list α) := map₂_left' (flip f) bs as /-- Left-biased version of `list.zip`. `zip_left' as bs` returns the list of pairs `(aᵢ, bᵢ)` for `aᵢ ∈ as` and `bᵢ ∈ bs`. If `bs` is shorter than `as`, the remaining `aᵢ` are paired with `none`. Also returns the remaining `bs`. ``` zip_left' [1, 2] ['a'] = ([(1, some 'a'), (2, none)], []) zip_left' [1] ['a', 'b'] = ([(1, some 'a')], ['b']) zip_left' = map₂_left' prod.mk ``` -/ def zip_left' : list α → list β → list (α × option β) × list β := map₂_left' prod.mk /-- Right-biased version of `list.zip`. `zip_right' as bs` returns the list of pairs `(aᵢ, bᵢ)` for `aᵢ ∈ as` and `bᵢ ∈ bs`. If `as` is shorter than `bs`, the remaining `bᵢ` are paired with `none`. Also returns the remaining `as`. ``` zip_right' [1] ['a', 'b'] = ([(some 1, 'a'), (none, 'b')], []) zip_right' [1, 2] ['a'] = ([(some 1, 'a')], [2]) zip_right' = map₂_right' prod.mk ``` -/ def zip_right' : list α → list β → list (option α × β) × list α := map₂_right' prod.mk /-- Left-biased version of `list.map₂`. `map₂_left f as bs` applies `f` to each pair `aᵢ ∈ as` and `bᵢ ‌∈ bs`. If `bs` is shorter than `as`, `f` is applied to `none` for the remaining `aᵢ`. ``` map₂_left prod.mk [1, 2] ['a'] = [(1, some 'a'), (2, none)] map₂_left prod.mk [1] ['a', 'b'] = [(1, some 'a')] map₂_left f as bs = (map₂_left' f as bs).fst ``` -/ @[simp] def map₂_left (f : α → option β → γ) : list α → list β → list γ | [] _ := [] | (a :: as) [] := (a :: as).map (λ a, f a none) | (a :: as) (b :: bs) := f a (some b) :: map₂_left as bs /-- Right-biased version of `list.map₂`. `map₂_right f as bs` applies `f` to each pair `aᵢ ∈ as` and `bᵢ ‌∈ bs`. If `as` is shorter than `bs`, `f` is applied to `none` for the remaining `bᵢ`. ``` map₂_right prod.mk [1, 2] ['a'] = [(some 1, 'a')] map₂_right prod.mk [1] ['a', 'b'] = [(some 1, 'a'), (none, 'b')] map₂_right f as bs = (map₂_right' f as bs).fst ``` -/ def map₂_right (f : option α → β → γ) (as : list α) (bs : list β) : list γ := map₂_left (flip f) bs as /-- Left-biased version of `list.zip`. `zip_left as bs` returns the list of pairs `(aᵢ, bᵢ)` for `aᵢ ∈ as` and `bᵢ ∈ bs`. If `bs` is shorter than `as`, the remaining `aᵢ` are paired with `none`. ``` zip_left [1, 2] ['a'] = [(1, some 'a'), (2, none)] zip_left [1] ['a', 'b'] = [(1, some 'a')] zip_left = map₂_left prod.mk ``` -/ def zip_left : list α → list β → list (α × option β) := map₂_left prod.mk /-- Right-biased version of `list.zip`. `zip_right as bs` returns the list of pairs `(aᵢ, bᵢ)` for `aᵢ ∈ as` and `bᵢ ∈ bs`. If `as` is shorter than `bs`, the remaining `bᵢ` are paired with `none`. ``` zip_right [1, 2] ['a'] = [(some 1, 'a')] zip_right [1] ['a', 'b'] = [(some 1, 'a'), (none, 'b')] zip_right = map₂_right prod.mk ``` -/ def zip_right : list α → list β → list (option α × β) := map₂_right prod.mk /-- If all elements of `xs` are `some xᵢ`, `all_some xs` returns the `xᵢ`. Otherwise it returns `none`. ``` all_some [some 1, some 2] = some [1, 2] all_some [some 1, none ] = none ``` -/ def all_some : list (option α) → option (list α) | [] := some [] | (some a :: as) := cons a <$> all_some as | (none :: as) := none /-- `fill_nones xs ys` replaces the `none`s in `xs` with elements of `ys`. If there are not enough `ys` to replace all the `none`s, the remaining `none`s are dropped from `xs`. ``` fill_nones [none, some 1, none, none] [2, 3] = [2, 1, 3] ``` -/ def fill_nones {α} : list (option α) → list α → list α | [] _ := [] | (some a :: as) as' := a :: fill_nones as as' | (none :: as) [] := as.reduce_option | (none :: as) (a :: as') := a :: fill_nones as as' /-- `take_list as ns` extracts successive sublists from `as`. For `ns = n₁ ... nₘ`, it first takes the `n₁` initial elements from `as`, then the next `n₂` ones, etc. It returns the sublists of `as` -- one for each `nᵢ` -- and the remaining elements of `as`. If `as` does not have at least as many elements as the sum of the `nᵢ`, the corresponding sublists will have less than `nᵢ` elements. ``` take_list ['a', 'b', 'c', 'd', 'e'] [2, 1, 1] = ([['a', 'b'], ['c'], ['d']], ['e']) take_list ['a', 'b'] [3, 1] = ([['a', 'b'], []], []) ``` -/ def take_list {α} : list α → list ℕ → list (list α) × list α | xs [] := ([], xs) | xs (n :: ns) := let ⟨xs₁, xs₂⟩ := xs.split_at n in let ⟨xss, rest⟩ := take_list xs₂ ns in (xs₁ :: xss, rest) /-- `to_rbmap as` is the map that associates each index `i` of `as` with the corresponding element of `as`. ``` to_rbmap ['a', 'b', 'c'] = rbmap_of [(0, 'a'), (1, 'b'), (2, 'c')] ``` -/ def to_rbmap : list α → rbmap ℕ α := foldl_with_index (λ i mapp a, mapp.insert i a) (mk_rbmap ℕ α) /-- `to_rb_map as` is the map that associates each index `i` of `as` with the corresponding element of `as`. ``` to_rb_map ['a', 'b', 'c'] = rb_map.of_list [(0, 'a'), (1, 'b'), (2, 'c')] ``` -/ meta def to_rb_map {α : Type} : list α → rb_map ℕ α := foldl_with_index (λ i mapp a, mapp.insert i a) mk_rb_map /-- `xs.to_chunks n` splits the list into sublists of size at most `n`, such that `(xs.to_chunks n).join = xs`. TODO: make non-meta; currently doesn't terminate, e.g. ``` #eval [0].to_chunks 0 ``` -/ meta def to_chunks {α} (n : ℕ) : list α → list (list α) | [] := [] | xs := xs.take n :: (xs.drop n).to_chunks /-- Asynchronous version of `list.map`. -/ meta def map_async_chunked {α β} (f : α → β) (xs : list α) (chunk_size := 1024) : list β := ((xs.to_chunks chunk_size).map (λ xs, task.delay (λ _, list.map f xs))).bind task.get end list -/
319c1c5f38317281924a8a903dc8053a20dbe2fb
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/ring_theory/free_comm_ring.lean
f1bb721f29fc0247b0f563d241f8718f382e0076
[ "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
16,517
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Johan Commelin -/ import data.equiv.functor import data.mv_polynomial import ring_theory.free_ring /-! # Free commutative rings The theory of the free commutative ring generated by a type `α`. It is isomorphic to the polynomial ring over ℤ with variables in `α` ## Main definitions * `free_comm_ring α` : the free commutative ring on a type α * `lift_hom (f : α → R)` : the ring hom `free_comm_ring α →+* R` induced by functoriality from `f`. * `map (f : α → β)` : the ring hom `free_comm_ring α →*+ free_comm_ring β` induced by functoriality from f. ## Main results `free_comm_ring` has functorial properties (it is an adjoint to the forgetful functor). In this file we have: * `of : α → free_comm_ring α` * `lift_hom (f : α → R) : free_comm_ring α →+* R` * `map (f : α → β) : free_comm_ring α →+* free_comm_ring β` * `free_comm_ring_equiv_mv_polynomial_int : free_comm_ring α ≃+* mv_polynomial α ℤ` : `free_comm_ring α` is isomorphic to a polynomial ring. ## Implementation notes `free_comm_ring α` is implemented not using `mv_polynomial` but directly as the free abelian group on `multiset α`, the type of monomials in this free commutative ring. ## Tags free commutative ring, free ring -/ noncomputable theory open_locale classical universes u v variables (α : Type u) /-- `free_comm_ring α` is the free commutative ring on the type `α`. -/ def free_comm_ring (α : Type u) : Type u := free_abelian_group $ multiplicative $ multiset α namespace free_comm_ring /-- The structure of a commutative ring on `free_comm_ring α`. -/ instance : comm_ring (free_comm_ring α) := free_abelian_group.comm_ring _ instance : inhabited (free_comm_ring α) := ⟨0⟩ variables {α} /-- The canonical map from `α` to the free commutative ring on `α`. -/ def of (x : α) : free_comm_ring α := free_abelian_group.of ([x] : multiset α) @[elab_as_eliminator] protected lemma induction_on {C : free_comm_ring α → Prop} (z : free_comm_ring α) (hn1 : C (-1)) (hb : ∀ b, C (of b)) (ha : ∀ x y, C x → C y → C (x + y)) (hm : ∀ x y, C x → C y → C (x * y)) : C z := have hn : ∀ x, C x → C (-x), from λ x ih, neg_one_mul x ▸ hm _ _ hn1 ih, have h1 : C 1, from neg_neg (1 : free_comm_ring α) ▸ hn _ hn1, free_abelian_group.induction_on z (add_left_neg (1 : free_comm_ring α) ▸ ha _ _ hn1 h1) (λ m, multiset.induction_on m h1 $ λ a m ih, hm _ _ (hb a) ih) (λ m ih, hn _ ih) ha section lift variables {R : Type v} [comm_ring R] (f : α → R) /-- Lift a map `α → R` to a additive group homomorphism `free_comm_ring α → R`. For a version producing a bundled homomorphism, see `lift_hom`. -/ def lift : free_comm_ring α →+* R := { map_one' := free_abelian_group.lift.of _ _, map_mul' := λ x y, begin refine free_abelian_group.induction_on y (mul_zero _).symm _ _ _, { intros s2, conv_lhs { dsimp only [free_abelian_group.mul_def] }, simp only [free_abelian_group.lift.of, add_monoid_hom.to_fun_eq_coe], refine free_abelian_group.induction_on x (zero_mul _).symm _ _ _, { intros s1, iterate 3 { rw free_abelian_group.lift.of }, calc _ = multiset.prod ((multiset.map f s1) + (multiset.map f s2)) : by {congr' 1, exact multiset.map_add _ _ _} ... = _ : multiset.prod_add _ _ }, { intros s1 ih, iterate 3 { rw free_abelian_group.lift.neg }, rw [ih, neg_mul_eq_neg_mul] }, { intros x1 x2 ih1 ih2, iterate 3 { rw free_abelian_group.lift.add }, rw [ih1, ih2, add_mul] } }, { intros s2 ih, simp only [add_monoid_hom.to_fun_eq_coe] at ih ⊢, rw [mul_neg_eq_neg_mul_symm, add_monoid_hom.map_neg, add_monoid_hom.map_neg, mul_neg_eq_neg_mul_symm, ih] }, { intros y1 y2 ih1 ih2, simp only [add_monoid_hom.to_fun_eq_coe] at ih1 ih2 ⊢, rw [mul_add, add_monoid_hom.map_add, add_monoid_hom.map_add, mul_add, ih1, ih2] }, end, ..free_abelian_group.lift $ λ s : multiplicative (multiset α), (s.to_add.map f).prod } @[simp] lemma lift_of (x : α) : lift f (of x) = f x := (free_abelian_group.lift.of _ _).trans $ mul_one _ @[simp] lemma lift_comp_of (f : free_comm_ring α →+* R) : lift (f ∘ of) = f := ring_hom.ext $ λ x, free_comm_ring.induction_on x (by rw [ring_hom.map_neg, ring_hom.map_one, f.map_neg, f.map_one]) (lift_of _) (λ x y ihx ihy, by rw [ring_hom.map_add, f.map_add, ihx, ihy]) (λ x y ihx ihy, by rw [ring_hom.map_mul, f.map_mul, ihx, ihy]) end lift variables {β : Type v} (f : α → β) /-- A map `f : α → β` produces a ring homomorphism `free_comm_ring α →+* free_comm_ring β`. -/ def map : free_comm_ring α →+* free_comm_ring β := lift $ of ∘ f @[simp] lemma map_of (x : α) : map f (of x) = of (f x) := lift_of _ _ /-- `is_supported x s` means that all monomials showing up in `x` have variables in `s`. -/ def is_supported (x : free_comm_ring α) (s : set α) : Prop := x ∈ ring.closure (of '' s) section is_supported variables {x y : free_comm_ring α} {s t : set α} theorem is_supported_upwards (hs : is_supported x s) (hst : s ⊆ t) : is_supported x t := ring.closure_mono (set.monotone_image hst) hs theorem is_supported_add (hxs : is_supported x s) (hys : is_supported y s) : is_supported (x + y) s := is_add_submonoid.add_mem hxs hys theorem is_supported_neg (hxs : is_supported x s) : is_supported (-x) s := is_add_subgroup.neg_mem hxs theorem is_supported_sub (hxs : is_supported x s) (hys : is_supported y s) : is_supported (x - y) s := is_add_subgroup.sub_mem _ _ _ hxs hys theorem is_supported_mul (hxs : is_supported x s) (hys : is_supported y s) : is_supported (x * y) s := is_submonoid.mul_mem hxs hys theorem is_supported_zero : is_supported 0 s := is_add_submonoid.zero_mem theorem is_supported_one : is_supported 1 s := is_submonoid.one_mem theorem is_supported_int {i : ℤ} {s : set α} : is_supported ↑i s := int.induction_on i is_supported_zero (λ i hi, by rw [int.cast_add, int.cast_one]; exact is_supported_add hi is_supported_one) (λ i hi, by rw [int.cast_sub, int.cast_one]; exact is_supported_sub hi is_supported_one) end is_supported /-- The restriction map from `free_comm_ring α` to `free_comm_ring s` where `s : set α`, defined by sending all variables not in `s` to zero. -/ def restriction (s : set α) [decidable_pred s] : free_comm_ring α →+* free_comm_ring s := lift (λ p, if H : p ∈ s then of ⟨p, H⟩ else 0) section restriction variables (s : set α) [decidable_pred s] (x y : free_comm_ring α) @[simp] lemma restriction_of (p) : restriction s (of p) = if H : p ∈ s then of ⟨p, H⟩ else 0 := lift_of _ _ end restriction theorem is_supported_of {p} {s : set α} : is_supported (of p) s ↔ p ∈ s := suffices is_supported (of p) s → p ∈ s, from ⟨this, λ hps, ring.subset_closure ⟨p, hps, rfl⟩⟩, assume hps : is_supported (of p) s, begin haveI := classical.dec_pred s, have : ∀ x, is_supported x s → ∃ (n : ℤ), lift (λ a, if a ∈ s then (0 : polynomial ℤ) else polynomial.X) x = n, { intros x hx, refine ring.in_closure.rec_on hx _ _ _ _, { use 1, rw [ring_hom.map_one], norm_cast }, { use -1, rw [ring_hom.map_neg, ring_hom.map_one], norm_cast }, { rintros _ ⟨z, hzs, rfl⟩ _ _, use 0, rw [ring_hom.map_mul, lift_of, if_pos hzs, zero_mul], norm_cast }, { rintros x y ⟨q, hq⟩ ⟨r, hr⟩, refine ⟨q+r, _⟩, rw [ring_hom.map_add, hq, hr], norm_cast } }, specialize this (of p) hps, rw [lift_of] at this, split_ifs at this, { exact h }, exfalso, apply ne.symm int.zero_ne_one, rcases this with ⟨w, H⟩, rw ←polynomial.C_eq_int_cast at H, have : polynomial.X.coeff 1 = (polynomial.C ↑w).coeff 1, by rw H, rwa [polynomial.coeff_C, if_neg (one_ne_zero : 1 ≠ 0), polynomial.coeff_X, if_pos rfl] at this end theorem map_subtype_val_restriction {x} (s : set α) [decidable_pred s] (hxs : is_supported x s) : map (subtype.val : s → α) (restriction s x) = x := begin refine ring.in_closure.rec_on hxs _ _ _ _, { rw ring_hom.map_one, refl }, { rw [ring_hom.map_neg, ring_hom.map_neg, ring_hom.map_one], refl }, { rintros _ ⟨p, hps, rfl⟩ n ih, rw [ring_hom.map_mul, restriction_of, dif_pos hps, ring_hom.map_mul, map_of, ih] }, { intros x y ihx ihy, rw [ring_hom.map_add, ring_hom.map_add, ihx, ihy] } end theorem exists_finite_support (x : free_comm_ring α) : ∃ s : set α, set.finite s ∧ is_supported x s := free_comm_ring.induction_on x ⟨∅, set.finite_empty, is_supported_neg is_supported_one⟩ (λ p, ⟨{p}, set.finite_singleton p, is_supported_of.2 $ set.mem_singleton _⟩) (λ x y ⟨s, hfs, hxs⟩ ⟨t, hft, hxt⟩, ⟨s ∪ t, hfs.union hft, is_supported_add (is_supported_upwards hxs $ set.subset_union_left s t) (is_supported_upwards hxt $ set.subset_union_right s t)⟩) (λ x y ⟨s, hfs, hxs⟩ ⟨t, hft, hxt⟩, ⟨s ∪ t, hfs.union hft, is_supported_mul (is_supported_upwards hxs $ set.subset_union_left s t) (is_supported_upwards hxt $ set.subset_union_right s t)⟩) theorem exists_finset_support (x : free_comm_ring α) : ∃ s : finset α, is_supported x ↑s := let ⟨s, hfs, hxs⟩ := exists_finite_support x in ⟨hfs.to_finset, by rwa set.finite.coe_to_finset⟩ end free_comm_ring namespace free_ring open function variable (α) /-- The canonical ring homomorphism from the free ring generated by `α` to the free commutative ring generated by `α`. -/ def to_free_comm_ring {α} : free_ring α →+* free_comm_ring α := free_ring.lift free_comm_ring.of instance : has_coe (free_ring α) (free_comm_ring α) := ⟨to_free_comm_ring⟩ instance coe.is_ring_hom : is_ring_hom (coe : free_ring α → free_comm_ring α) := free_ring.to_free_comm_ring.is_ring_hom @[simp, norm_cast] protected lemma coe_zero : ↑(0 : free_ring α) = (0 : free_comm_ring α) := rfl @[simp, norm_cast] protected lemma coe_one : ↑(1 : free_ring α) = (1 : free_comm_ring α) := rfl variable {α} @[simp] protected lemma coe_of (a : α) : ↑(free_ring.of a) = free_comm_ring.of a := free_ring.lift_of _ _ @[simp, norm_cast] protected lemma coe_neg (x : free_ring α) : ↑(-x) = -(x : free_comm_ring α) := (free_ring.lift _).map_neg _ @[simp, norm_cast] protected lemma coe_add (x y : free_ring α) : ↑(x + y) = (x : free_comm_ring α) + y := (free_ring.lift _).map_add _ _ @[simp, norm_cast] protected lemma coe_sub (x y : free_ring α) : ↑(x - y) = (x : free_comm_ring α) - y := (free_ring.lift _).map_sub _ _ @[simp, norm_cast] protected lemma coe_mul (x y : free_ring α) : ↑(x * y) = (x : free_comm_ring α) * y := (free_ring.lift _).map_mul _ _ variable (α) protected lemma coe_surjective : surjective (coe : free_ring α → free_comm_ring α) := λ x, begin apply free_comm_ring.induction_on x, { use -1, refl }, { intro x, use free_ring.of x, refl }, { rintros _ _ ⟨x, rfl⟩ ⟨y, rfl⟩, use x + y, exact (free_ring.lift _).map_add _ _ }, { rintros _ _ ⟨x, rfl⟩ ⟨y, rfl⟩, use x * y, exact (free_ring.lift _).map_mul _ _ } end lemma coe_eq : (coe : free_ring α → free_comm_ring α) = @functor.map free_abelian_group _ _ _ (λ (l : list α), (l : multiset α)) := funext $ λ x, free_abelian_group.lift.unique _ _ $ λ L, by { simp_rw [free_abelian_group.lift.of, (∘)], exact list.rec_on L rfl (λ hd tl ih, by { rw [list.map_cons, list.prod_cons, ih], refl }) } -- FIXME This was in `deprecated.ring`, but only used here. -- It would be good to inline it into the next construction. /-- Interpret an equivalence `f : R ≃ S` as a ring equivalence `R ≃+* S`. -/ def of' {R S : Type*} [ring R] [ring S] (e : R ≃ S) [is_ring_hom e] : R ≃+* S := { .. e, .. monoid_hom.of e, .. add_monoid_hom.of e } /-- If α has size at most 1 then the natural map from the free ring on `α` to the free commutative ring on `α` is an isomorphism of rings. -/ def subsingleton_equiv_free_comm_ring [subsingleton α] : free_ring α ≃+* free_comm_ring α := @of' (free_ring α) (free_comm_ring α) _ _ (functor.map_equiv free_abelian_group (multiset.subsingleton_equiv α)) $ begin delta functor.map_equiv, rw congr_arg is_ring_hom _, work_on_goal 2 { symmetry, exact coe_eq α }, apply_instance end instance [subsingleton α] : comm_ring (free_ring α) := { mul_comm := λ x y, by { rw [← (subsingleton_equiv_free_comm_ring α).symm_apply_apply (y * x), ((subsingleton_equiv_free_comm_ring α)).map_mul, mul_comm, ← ((subsingleton_equiv_free_comm_ring α)).map_mul, (subsingleton_equiv_free_comm_ring α).symm_apply_apply], }, .. free_ring.ring α } end free_ring /-- The free commutative ring on `α` is isomorphic to the polynomial ring over ℤ with variables in `α` -/ def free_comm_ring_equiv_mv_polynomial_int : free_comm_ring α ≃+* mv_polynomial α ℤ := { to_fun := free_comm_ring.lift $ λ a, mv_polynomial.X a, inv_fun := mv_polynomial.eval₂ (int.cast_ring_hom (free_comm_ring α)) free_comm_ring.of, left_inv := begin intro x, haveI : is_semiring_hom (coe : int → free_comm_ring α) := (int.cast_ring_hom _).is_semiring_hom, refine free_abelian_group.induction_on x rfl _ _ _, { intro s, refine multiset.induction_on s _ _, { unfold free_comm_ring.lift, simp only [free_abelian_group.lift.of, ring_hom.coe_mk, add_monoid_hom.to_fun_eq_coe], exact mv_polynomial.eval₂_one _ _ }, { intros hd tl ih, show mv_polynomial.eval₂ (int.cast_ring_hom (free_comm_ring α)) free_comm_ring.of (free_comm_ring.lift (λ a, mv_polynomial.X a) (free_comm_ring.of hd * free_abelian_group.of tl)) = free_comm_ring.of hd * free_abelian_group.of tl, rw [ring_hom.map_mul, free_comm_ring.lift_of, mv_polynomial.eval₂_mul, mv_polynomial.eval₂_X, ih] } }, { intros s ih, rw [ring_hom.map_neg, ← neg_one_mul, mv_polynomial.eval₂_mul, ← mv_polynomial.C_1, ← mv_polynomial.C_neg, mv_polynomial.eval₂_C, ring_hom.map_neg, ring_hom.map_one, neg_one_mul, ih] }, { intros x₁ x₂ ih₁ ih₂, rw [ring_hom.map_add, mv_polynomial.eval₂_add, ih₁, ih₂] } end, right_inv := begin intro x, haveI : is_semiring_hom (coe : int → free_comm_ring α) := (int.cast_ring_hom _).is_semiring_hom, have : ∀ i : ℤ, free_comm_ring.lift (λ (a : α), mv_polynomial.X a) (int.cast_ring_hom _ i) = mv_polynomial.C i, { exact λ i, int.induction_on i (by rw [ring_hom.map_zero, ring_hom.map_zero, mv_polynomial.C_0]) (λ i ih, by rw [ring_hom.map_add, ring_hom.map_one, ring_hom.map_add, ring_hom.map_one, ih, mv_polynomial.C_add, mv_polynomial.C_1]) (λ i ih, by rw [ring_hom.map_sub, ring_hom.map_one, ring_hom.map_sub, ring_hom.map_one, ih, mv_polynomial.C_sub, mv_polynomial.C_1]) }, apply mv_polynomial.induction_on x, { intro i, rw [mv_polynomial.eval₂_C, this] }, { intros p q ihp ihq, rw [mv_polynomial.eval₂_add, ring_hom.map_add, ihp, ihq] }, { intros p a ih, rw [mv_polynomial.eval₂_mul, mv_polynomial.eval₂_X, ring_hom.map_mul, free_comm_ring.lift_of, ih] } end, .. free_comm_ring.lift $ λ a, mv_polynomial.X a } /-- The free commutative ring on the empty type is isomorphic to `ℤ`. -/ def free_comm_ring_pempty_equiv_int : free_comm_ring pempty.{u+1} ≃+* ℤ := ring_equiv.trans (free_comm_ring_equiv_mv_polynomial_int _) (mv_polynomial.pempty_ring_equiv _) /-- The free commutative ring on a type with one term is isomorphic to `ℤ[X]`. -/ def free_comm_ring_punit_equiv_polynomial_int : free_comm_ring punit.{u+1} ≃+* polynomial ℤ := ring_equiv.trans (free_comm_ring_equiv_mv_polynomial_int _) (mv_polynomial.punit_ring_equiv _) open free_ring /-- The free ring on the empty type is isomorphic to `ℤ`. -/ def free_ring_pempty_equiv_int : free_ring pempty.{u+1} ≃+* ℤ := ring_equiv.trans (subsingleton_equiv_free_comm_ring _) free_comm_ring_pempty_equiv_int /-- The free ring on a type with one term is isomorphic to `ℤ[X]`. -/ def free_ring_punit_equiv_polynomial_int : free_ring punit.{u+1} ≃+* polynomial ℤ := ring_equiv.trans (subsingleton_equiv_free_comm_ring _) free_comm_ring_punit_equiv_polynomial_int
df4d630d64a0f9286f2fc44952cc9d382ea2e53b
aa101d73b1a3173c7ec56de02b96baa8ca64c42e
/src/my_exercises/07bis_abstract_negations.lean
036f7113f78b168d6bea78939925bf5ad8a0f7b7
[ "Apache-2.0" ]
permissive
gihanmarasingha/tutorials
b554d4d53866c493c4341dc13e914b01444e95a6
56617114ef0f9f7b808476faffd11e22e4380918
refs/heads/master
1,671,141,758,153
1,599,173,318,000
1,599,173,318,000
282,405,870
0
0
Apache-2.0
1,595,666,751,000
1,595,666,750,000
null
UTF-8
Lean
false
false
2,899
lean
import data.real.basic open_locale classical /- Theoretical negations. This file is for people interested in logic who want to fully understand negations. Here we don't use `contrapose` or `push_neg`. The goal is to prove lemmas that are used by those tactics. Of course we can use `exfalso`, `by_contradiction` and `by_cases`. If this doesn't sound like fun then skip ahead to the next file. -/ section negation_prop variables P Q : Prop -- 0055 example : (P → Q) ↔ (¬ Q → ¬ P) := begin split, { intros hpq hnq hp, exact hnq (hpq hp), }, { intros hnqnp hp, by_contradiction hnq, exact (hnqnp hnq) hp, } end -- 0056 /- The model solutions handle the first case more efficiently bu using a double contradiction. -/ lemma non_imp (P Q : Prop) : ¬ (P → Q) ↔ P ∧ ¬ Q := begin split, { intro hnpq, by_cases hp : P, { by_cases hq : Q, exact ⟨hp, λ q, hnpq (λ p, q) ⟩, exact ⟨hp, hq⟩, }, { exfalso, have hpq : P → Q, intro p, exfalso, exact hp p, exact hnpq hpq, } }, { rintros ⟨hp,hnq⟩ hnpq, exact hnq (hnpq hp), } end -- In the next one, let's use the axiom -- propext {P Q : Prop} : (P ↔ Q) → P = Q /- My comments: What wasn't obvious to me before starting this question is that = has higher order of precedence that → or ↔. So, propext : (A ↔ B) → (A = B) and the question asks us to prove ¬P ↔ (P = false). -/ -- 0057 example (P : Prop) : ¬ P ↔ P = false := begin split, { intro hnp, suffices h : P ↔ false, exact propext h, split; contradiction, }, { rintro ⟨h,rfl⟩, contradiction, } end end negation_prop section negation_quantifiers variables (X : Type) (P : X → Prop) -- 0058 example : ¬ (∀ x, P x) ↔ ∃ x, ¬ P x := begin split, { intro hna, by_contradiction hnex, apply hna, intro x, by_contradiction h, apply hnex, use x, }, { rintros ⟨x,hn⟩ hax, exact hn (hax x), } end -- 0059 example : ¬ (∃ x, P x) ↔ ∀ x, ¬ P x := begin split, { intros hnex x hnp, apply hnex, use x, exact hnp, }, { rintros hax ⟨x,hx⟩, exact (hax x) hx, } end -- 0060 example (P : ℝ → Prop) : ¬ (∃ ε > 0, P ε) ↔ ∀ ε > 0, ¬ P ε := begin split, { intros hnex ε ε_pos hnp, apply hnex, use ε, exact ⟨ε_pos, hnp⟩, }, { rintros ha ⟨ε,⟨ε_pos,hp⟩⟩, exact (ha ε ε_pos) hp, } end -- 0061 example (P : ℝ → Prop) : ¬ (∀ x > 0, P x) ↔ ∃ x > 0, ¬ P x := begin split, { intro hna, by_contradiction hex, apply hna, intros x x_pos, by_contradiction hp, apply hex, use x, exact ⟨x_pos, hp⟩, }, { rintros ⟨x,x_pos,hnp⟩ hax, exact hnp (hax x x_pos), } end end negation_quantifiers
bae78f129339cf8f13eaa14452ea6c4e21e8eed5
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/algebra/ordered_group.lean
31c5eb25478ac29cb1bc663f7f39ba443ec940c3
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,256
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.ordered_monoid /-! # Ordered groups This file develops the basics of ordered groups. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true universe u variable {α : Type u} /-- An ordered additive commutative group is an additive commutative group with a partial order in which addition is strictly monotone. -/ @[protect_proj, ancestor add_comm_group partial_order] class ordered_add_comm_group (α : Type u) extends add_comm_group α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) /-- An ordered commutative group is an commutative group with a partial order in which multiplication is strictly monotone. -/ @[protect_proj, ancestor comm_group partial_order] class ordered_comm_group (α : Type u) extends comm_group α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) attribute [to_additive] ordered_comm_group /--The units of an ordered commutative monoid form an ordered commutative group. -/ @[to_additive] instance units.ordered_comm_group [ordered_comm_monoid α] : ordered_comm_group (units α) := { mul_le_mul_left := λ a b h c, mul_le_mul_left' h _, .. units.partial_order, .. (infer_instance : comm_group (units α)) } section ordered_comm_group variables [ordered_comm_group α] {a b c d : α} @[to_additive ordered_add_comm_group.add_lt_add_left] lemma ordered_comm_group.mul_lt_mul_left' (a b : α) (h : a < b) (c : α) : c * a < c * b := begin rw lt_iff_le_not_le at h ⊢, split, { apply ordered_comm_group.mul_le_mul_left _ _ h.1 }, { intro w, replace w : c⁻¹ * (c * b) ≤ c⁻¹ * (c * a) := ordered_comm_group.mul_le_mul_left _ _ w _, simp only [mul_one, mul_comm, mul_left_inv, mul_left_comm] at w, exact h.2 w }, end @[to_additive ordered_add_comm_group.le_of_add_le_add_left] lemma ordered_comm_group.le_of_mul_le_mul_left (h : a * b ≤ a * c) : b ≤ c := have a⁻¹ * (a * b) ≤ a⁻¹ * (a * c), from ordered_comm_group.mul_le_mul_left _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] lemma ordered_comm_group.lt_of_mul_lt_mul_left (h : a * b < a * c) : b < c := have a⁻¹ * (a * b) < a⁻¹ * (a * c), from ordered_comm_group.mul_lt_mul_left' _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_comm_group.to_ordered_cancel_comm_monoid (α : Type u) [s : ordered_comm_group α] : ordered_cancel_comm_monoid α := { mul_left_cancel := @mul_left_cancel α _, mul_right_cancel := @mul_right_cancel α _, le_of_mul_le_mul_left := @ordered_comm_group.le_of_mul_le_mul_left α _, ..s } @[priority 100, to_additive] instance ordered_comm_group.has_exists_mul_of_le (α : Type u) [ordered_comm_group α] : has_exists_mul_of_le α := ⟨λ a b hab, ⟨b * a⁻¹, (mul_inv_cancel_comm_assoc a b).symm⟩⟩ @[to_additive neg_le_neg] lemma inv_le_inv' (h : a ≤ b) : b⁻¹ ≤ a⁻¹ := have 1 ≤ a⁻¹ * b, from mul_left_inv a ▸ mul_le_mul_left' h _, have 1 * b⁻¹ ≤ a⁻¹ * b * b⁻¹, from mul_le_mul_right' this _, by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma le_of_inv_le_inv (h : b⁻¹ ≤ a⁻¹) : a ≤ b := suffices (a⁻¹)⁻¹ ≤ (b⁻¹)⁻¹, from begin simp [inv_inv] at this, assumption end, inv_le_inv' h @[to_additive] lemma one_le_of_inv_le_one (h : a⁻¹ ≤ 1) : 1 ≤ a := have a⁻¹ ≤ 1⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive] lemma inv_le_one_of_one_le (h : 1 ≤ a) : a⁻¹ ≤ 1 := have a⁻¹ ≤ 1⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive nonpos_of_neg_nonneg] lemma le_one_of_one_le_inv (h : 1 ≤ a⁻¹) : a ≤ 1 := have 1⁻¹ ≤ a⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive neg_nonneg_of_nonpos] lemma one_le_inv_of_le_one (h : a ≤ 1) : 1 ≤ a⁻¹ := have 1⁻¹ ≤ a⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive neg_lt_neg] lemma inv_lt_inv' (h : a < b) : b⁻¹ < a⁻¹ := have 1 < a⁻¹ * b, from mul_left_inv a ▸ mul_lt_mul_left' h (a⁻¹), have 1 * b⁻¹ < a⁻¹ * b * b⁻¹, from mul_lt_mul_right' this (b⁻¹), by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma lt_of_inv_lt_inv (h : b⁻¹ < a⁻¹) : a < b := inv_inv a ▸ inv_inv b ▸ inv_lt_inv' h @[to_additive] lemma one_lt_of_inv_inv (h : a⁻¹ < 1) : 1 < a := have a⁻¹ < 1⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive] lemma inv_inv_of_one_lt (h : 1 < a) : a⁻¹ < 1 := have a⁻¹ < 1⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive neg_of_neg_pos] lemma inv_of_one_lt_inv (h : 1 < a⁻¹) : a < 1 := have 1⁻¹ < a⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive neg_pos_of_neg] lemma one_lt_inv_of_inv (h : a < 1) : 1 < a⁻¹ := have 1⁻¹ < a⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive] lemma le_inv_of_le_inv (h : a ≤ b⁻¹) : b ≤ a⁻¹ := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_le_of_inv_le (h : a⁻¹ ≤ b) : b⁻¹ ≤ a := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma lt_inv_of_lt_inv (h : a < b⁻¹) : b < a⁻¹ := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_lt_of_inv_lt (h : a⁻¹ < b) : b⁻¹ < a := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma mul_le_of_le_inv_mul (h : b ≤ a⁻¹ * c) : a * b ≤ c := begin have h := mul_le_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma le_inv_mul_of_mul_le (h : a * b ≤ c) : b ≤ a⁻¹ * c := begin have h := mul_le_mul_left' h a⁻¹, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le (h : b⁻¹ * a ≤ c) : a ≤ b * c := begin have h := mul_le_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_le_of_le_mul (h : a ≤ b * c) : b⁻¹ * a ≤ c := begin have h := mul_le_mul_left' h b⁻¹, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le_left (h : b⁻¹ * a ≤ c) : a ≤ b * c := le_mul_of_inv_mul_le h @[to_additive] lemma inv_mul_le_left_of_le_mul (h : a ≤ b * c) : b⁻¹ * a ≤ c := inv_mul_le_of_le_mul h @[to_additive] lemma le_mul_of_inv_mul_le_right (h : c⁻¹ * a ≤ b) : a ≤ b * c := by { rw mul_comm, exact le_mul_of_inv_mul_le h } @[to_additive] lemma inv_mul_le_right_of_le_mul (h : a ≤ b * c) : c⁻¹ * a ≤ b := by { rw mul_comm at h, apply inv_mul_le_left_of_le_mul h } @[to_additive] lemma mul_lt_of_lt_inv_mul (h : b < a⁻¹ * c) : a * b < c := begin have h := mul_lt_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma lt_inv_mul_of_mul_lt (h : a * b < c) : b < a⁻¹ * c := begin have h := mul_lt_mul_left' h (a⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt (h : b⁻¹ * a < c) : a < b * c := begin have h := mul_lt_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_lt_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := begin have h := mul_lt_mul_left' h (b⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt_left (h : b⁻¹ * a < c) : a < b * c := lt_mul_of_inv_mul_lt h @[to_additive] lemma inv_mul_lt_left_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := inv_mul_lt_of_lt_mul h @[to_additive] lemma lt_mul_of_inv_mul_lt_right (h : c⁻¹ * a < b) : a < b * c := by { rw mul_comm, exact lt_mul_of_inv_mul_lt h } @[to_additive] lemma inv_mul_lt_right_of_lt_mul (h : a < b * c) : c⁻¹ * a < b := by { rw mul_comm at h, exact inv_mul_lt_of_lt_mul h } @[simp, to_additive] lemma inv_lt_one_iff_one_lt : a⁻¹ < 1 ↔ 1 < a := ⟨ one_lt_of_inv_inv, inv_inv_of_one_lt ⟩ @[simp, to_additive] lemma inv_le_inv_iff : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := have a * b * a⁻¹ ≤ a * b * b⁻¹ ↔ a⁻¹ ≤ b⁻¹, from mul_le_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_le] lemma inv_le' : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := have a⁻¹ ≤ (b⁻¹)⁻¹ ↔ b⁻¹ ≤ a, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive le_neg] lemma le_inv' : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := have (a⁻¹)⁻¹ ≤ b⁻¹ ↔ b ≤ a⁻¹, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive neg_le_iff_add_nonneg] lemma inv_le_iff_one_le_mul : a⁻¹ ≤ b ↔ 1 ≤ b * a := (mul_le_mul_iff_right a).symm.trans $ by rw inv_mul_self @[to_additive neg_le_iff_add_nonneg'] lemma inv_le_iff_one_le_mul' : a⁻¹ ≤ b ↔ 1 ≤ a * b := (mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma inv_lt_iff_one_lt_mul : a⁻¹ < b ↔ 1 < b * a := (mul_lt_mul_iff_right a).symm.trans $ by rw inv_mul_self @[to_additive] lemma inv_lt_iff_one_lt_mul' : a⁻¹ < b ↔ 1 < a * b := (mul_lt_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_iff_mul_le_one : a ≤ b⁻¹ ↔ a * b ≤ 1 := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self @[to_additive] lemma le_inv_iff_mul_le_one' : a ≤ b⁻¹ ↔ b * a ≤ 1 := (mul_le_mul_iff_left b).symm.trans $ by rw mul_inv_self @[to_additive] lemma lt_inv_iff_mul_lt_one : a < b⁻¹ ↔ a * b < 1 := (mul_lt_mul_iff_right b).symm.trans $ by rw inv_mul_self @[to_additive] lemma lt_inv_iff_mul_lt_one' : a < b⁻¹ ↔ b * a < 1 := (mul_lt_mul_iff_left b).symm.trans $ by rw mul_inv_self @[simp, to_additive neg_nonpos] lemma inv_le_one' : a⁻¹ ≤ 1 ↔ 1 ≤ a := have a⁻¹ ≤ 1⁻¹ ↔ 1 ≤ a, from inv_le_inv_iff, by rwa one_inv at this @[simp, to_additive neg_nonneg] lemma one_le_inv' : 1 ≤ a⁻¹ ↔ a ≤ 1 := have 1⁻¹ ≤ a⁻¹ ↔ a ≤ 1, from inv_le_inv_iff, by rwa one_inv at this @[to_additive] lemma inv_le_self (h : 1 ≤ a) : a⁻¹ ≤ a := le_trans (inv_le_one'.2 h) h @[to_additive] lemma self_le_inv (h : a ≤ 1) : a ≤ a⁻¹ := le_trans h (one_le_inv'.2 h) @[simp, to_additive] lemma inv_lt_inv_iff : a⁻¹ < b⁻¹ ↔ b < a := have a * b * a⁻¹ < a * b * b⁻¹ ↔ a⁻¹ < b⁻¹, from mul_lt_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_lt_zero] lemma inv_lt_one' : a⁻¹ < 1 ↔ 1 < a := have a⁻¹ < 1⁻¹ ↔ 1 < a, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_pos] lemma one_lt_inv' : 1 < a⁻¹ ↔ a < 1 := have 1⁻¹ < a⁻¹ ↔ a < 1, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_lt] lemma inv_lt' : a⁻¹ < b ↔ b⁻¹ < a := have a⁻¹ < (b⁻¹)⁻¹ ↔ b⁻¹ < a, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive lt_neg] lemma lt_inv' : a < b⁻¹ ↔ b < a⁻¹ := have (a⁻¹)⁻¹ < b⁻¹ ↔ b < a⁻¹, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive] lemma inv_lt_self (h : 1 < a) : a⁻¹ < a := (inv_lt_one'.2 h).trans h @[to_additive] lemma le_inv_mul_iff_mul_le : b ≤ a⁻¹ * c ↔ a * b ≤ c := have a⁻¹ * (a * b) ≤ a⁻¹ * c ↔ a * b ≤ c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_le_iff_le_mul : b⁻¹ * a ≤ c ↔ a ≤ b * c := have b⁻¹ * a ≤ b⁻¹ * (b * c) ↔ a ≤ b * c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma mul_inv_le_iff_le_mul : a * c⁻¹ ≤ b ↔ a ≤ b * c := by rw [mul_comm a, mul_comm b, inv_mul_le_iff_le_mul] @[simp, to_additive] lemma mul_inv_le_iff_le_mul' : a * b⁻¹ ≤ c ↔ a ≤ b * c := by rw [← inv_mul_le_iff_le_mul, mul_comm] @[to_additive] lemma inv_mul_le_iff_le_mul' : c⁻¹ * a ≤ b ↔ a ≤ b * c := by rw [inv_mul_le_iff_le_mul, mul_comm] @[simp, to_additive] lemma lt_inv_mul_iff_mul_lt : b < a⁻¹ * c ↔ a * b < c := have a⁻¹ * (a * b) < a⁻¹ * c ↔ a * b < c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_lt_iff_lt_mul : b⁻¹ * a < c ↔ a < b * c := have b⁻¹ * a < b⁻¹ * (b * c) ↔ a < b * c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma inv_mul_lt_iff_lt_mul_right : c⁻¹ * a < b ↔ a < b * c := by rw [inv_mul_lt_iff_lt_mul, mul_comm] @[to_additive add_neg_le_add_neg_iff] lemma div_le_div_iff' : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := begin split ; intro h, have := mul_le_mul_right' (mul_le_mul_right' h b) d, rwa [inv_mul_cancel_right, mul_assoc _ _ b, mul_comm _ b, ← mul_assoc, inv_mul_cancel_right] at this, have := mul_le_mul_right' (mul_le_mul_right' h d⁻¹) b⁻¹, rwa [mul_inv_cancel_right, _root_.mul_assoc, _root_.mul_comm d⁻¹ b⁻¹, ← mul_assoc, mul_inv_cancel_right] at this, end @[simp, to_additive] lemma div_le_self_iff (a : α) {b : α} : a / b ≤ a ↔ 1 ≤ b := by simp [div_eq_mul_inv] @[simp, to_additive] lemma div_lt_self_iff (a : α) {b : α} : a / b < a ↔ 1 < b := by simp [div_eq_mul_inv] /-- Pullback an `ordered_comm_group` under an injective map. -/ @[to_additive function.injective.ordered_add_comm_group "Pullback an `ordered_add_comm_group` under an injective map."] def function.injective.ordered_comm_group {β : Type*} [has_one β] [has_mul β] [has_inv β] [has_div β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : ordered_comm_group β := { ..partial_order.lift f hf, ..hf.ordered_comm_monoid f one mul, ..hf.comm_group_div f one mul inv div } end ordered_comm_group section ordered_add_comm_group variables [ordered_add_comm_group α] {a b c d : α} lemma sub_le_sub (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := by simpa only [sub_eq_add_neg] using add_le_add hab (neg_le_neg hcd) lemma sub_lt_sub (hab : a < b) (hcd : c < d) : a - d < b - c := by simpa only [sub_eq_add_neg] using add_lt_add hab (neg_lt_neg hcd) alias sub_le_self_iff ↔ _ sub_le_self alias sub_lt_self_iff ↔ _ sub_lt_self lemma sub_le_sub_iff : a - b ≤ c - d ↔ a + d ≤ c + b := by simpa only [sub_eq_add_neg] using add_neg_le_add_neg_iff @[simp] lemma sub_le_sub_iff_left (a : α) {b c : α} : a - b ≤ a - c ↔ c ≤ b := by rw [sub_eq_add_neg, sub_eq_add_neg, add_le_add_iff_left, neg_le_neg_iff] lemma sub_le_sub_left (h : a ≤ b) (c : α) : c - b ≤ c - a := (sub_le_sub_iff_left c).2 h @[simp] lemma sub_le_sub_iff_right (c : α) : a - c ≤ b - c ↔ a ≤ b := by simpa only [sub_eq_add_neg] using add_le_add_iff_right _ lemma sub_le_sub_right (h : a ≤ b) (c : α) : a - c ≤ b - c := (sub_le_sub_iff_right c).2 h @[simp] lemma sub_lt_sub_iff_left (a : α) {b c : α} : a - b < a - c ↔ c < b := by rw [sub_eq_add_neg, sub_eq_add_neg, add_lt_add_iff_left, neg_lt_neg_iff] lemma sub_lt_sub_left (h : a < b) (c : α) : c - b < c - a := (sub_lt_sub_iff_left c).2 h @[simp] lemma sub_lt_sub_iff_right (c : α) : a - c < b - c ↔ a < b := by simpa only [sub_eq_add_neg] using add_lt_add_iff_right _ lemma sub_lt_sub_right (h : a < b) (c : α) : a - c < b - c := (sub_lt_sub_iff_right c).2 h @[simp] lemma sub_nonneg : 0 ≤ a - b ↔ b ≤ a := by rw [← sub_self a, sub_le_sub_iff_left] alias sub_nonneg ↔ le_of_sub_nonneg sub_nonneg_of_le @[simp] lemma sub_nonpos : a - b ≤ 0 ↔ a ≤ b := by rw [← sub_self b, sub_le_sub_iff_right] alias sub_nonpos ↔ le_of_sub_nonpos sub_nonpos_of_le @[simp] lemma sub_pos : 0 < a - b ↔ b < a := by rw [← sub_self a, sub_lt_sub_iff_left] alias sub_pos ↔ lt_of_sub_pos sub_pos_of_lt @[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b := by rw [← sub_self b, sub_lt_sub_iff_right] alias sub_lt_zero ↔ lt_of_sub_neg sub_neg_of_lt lemma le_sub_iff_add_le' : b ≤ c - a ↔ a + b ≤ c := by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le] lemma le_sub_iff_add_le : a ≤ c - b ↔ a + b ≤ c := by rw [le_sub_iff_add_le', add_comm] alias le_sub_iff_add_le ↔ add_le_of_le_sub_right le_sub_right_of_add_le lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c := by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add] alias le_sub_iff_add_le' ↔ add_le_of_le_sub_left le_sub_left_of_add_le lemma sub_le_iff_le_add : a - c ≤ b ↔ a ≤ b + c := by rw [sub_le_iff_le_add', add_comm] @[simp] lemma neg_le_sub_iff_le_add : -b ≤ a - c ↔ c ≤ a + b := le_sub_iff_add_le.trans neg_add_le_iff_le_add' lemma neg_le_sub_iff_le_add' : -a ≤ b - c ↔ c ≤ a + b := by rw [neg_le_sub_iff_le_add, add_comm] lemma sub_le : a - b ≤ c ↔ a - c ≤ b := sub_le_iff_le_add'.trans sub_le_iff_le_add.symm theorem le_sub : a ≤ b - c ↔ c ≤ b - a := le_sub_iff_add_le'.trans le_sub_iff_add_le.symm lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c := by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt] alias lt_sub_iff_add_lt' ↔ add_lt_of_lt_sub_left lt_sub_left_of_add_lt lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c := by rw [lt_sub_iff_add_lt', add_comm] alias lt_sub_iff_add_lt ↔ add_lt_of_lt_sub_right lt_sub_right_of_add_lt lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c := by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add] alias sub_lt_iff_lt_add' ↔ lt_add_of_sub_left_lt sub_left_lt_of_lt_add lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c := by rw [sub_lt_iff_lt_add', add_comm] alias sub_lt_iff_lt_add ↔ lt_add_of_sub_right_lt sub_right_lt_of_lt_add @[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b := lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b := by rw [neg_lt_sub_iff_lt_add, add_comm] lemma sub_lt : a - b < c ↔ a - c < b := sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm theorem lt_sub : a < b - c ↔ c < b - a := lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm end ordered_add_comm_group /-! ### Linearly ordered commutative groups -/ /-- A linearly ordered additive commutative group is an additive commutative group with a linear order in which addition is monotone. -/ @[protect_proj, ancestor add_comm_group linear_order] class linear_ordered_add_comm_group (α : Type u) extends add_comm_group α, linear_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) /-- A linearly ordered commutative group is a commutative group with a linear order in which multiplication is monotone. -/ @[protect_proj, ancestor comm_group linear_order, to_additive] class linear_ordered_comm_group (α : Type u) extends comm_group α, linear_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) section linear_ordered_comm_group variables [linear_ordered_comm_group α] {a b c : α} @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_ordered_comm_group : ordered_comm_group α := { ..‹linear_ordered_comm_group α› } @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_linear_ordered_cancel_comm_monoid : linear_ordered_cancel_comm_monoid α := { le_of_mul_le_mul_left := λ x y z, le_of_mul_le_mul_left', mul_left_cancel := λ x y z, mul_left_cancel, mul_right_cancel := λ x y z, mul_right_cancel, ..‹linear_ordered_comm_group α› } /-- Pullback a `linear_ordered_comm_group` under an injective map. -/ @[to_additive function.injective.linear_ordered_add_comm_group "Pullback a `linear_ordered_add_comm_group` under an injective map."] def function.injective.linear_ordered_comm_group {β : Type*} [has_one β] [has_mul β] [has_inv β] [has_div β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : linear_ordered_comm_group β := { ..linear_order.lift f hf, ..hf.ordered_comm_group f one mul inv div } @[to_additive linear_ordered_add_comm_group.add_lt_add_left] lemma linear_ordered_comm_group.mul_lt_mul_left' (a b : α) (h : a < b) (c : α) : c * a < c * b := ordered_comm_group.mul_lt_mul_left' a b h c @[to_additive min_neg_neg] lemma min_inv_inv' (a b : α) : min (a⁻¹) (b⁻¹) = (max a b)⁻¹ := eq.symm $ @monotone.map_max α (order_dual α) _ _ has_inv.inv a b $ λ a b, inv_le_inv' @[to_additive max_neg_neg] lemma max_inv_inv' (a b : α) : max (a⁻¹) (b⁻¹) = (min a b)⁻¹ := eq.symm $ @monotone.map_min α (order_dual α) _ _ has_inv.inv a b $ λ a b, inv_le_inv' @[to_additive min_sub_sub_right] lemma min_div_div_right' (a b c : α) : min (a / c) (b / c) = min a b / c := by simpa only [div_eq_mul_inv] using min_mul_mul_right a b (c⁻¹) @[to_additive max_sub_sub_right] lemma max_div_div_right' (a b c : α) : max (a / c) (b / c) = max a b / c := by simpa only [div_eq_mul_inv] using max_mul_mul_right a b (c⁻¹) @[to_additive min_sub_sub_left] lemma min_div_div_left' (a b c : α) : min (a / b) (a / c) = a / max b c := by simp only [div_eq_mul_inv, min_mul_mul_left, min_inv_inv'] @[to_additive max_sub_sub_left] lemma max_div_div_left' (a b c : α) : max (a / b) (a / c) = a / min b c := by simp only [div_eq_mul_inv, max_mul_mul_left, max_inv_inv'] @[to_additive max_zero_sub_eq_self] lemma max_one_div_eq_self' (a : α) : max a 1 / max (a⁻¹) 1 = a := begin rcases le_total a 1, { rw [max_eq_right h, max_eq_left, one_div, inv_inv], { rwa [le_inv', one_inv] } }, { rw [max_eq_left, max_eq_right, div_eq_mul_inv, one_inv, mul_one], { rwa [inv_le', one_inv] }, exact h } end @[to_additive eq_zero_of_neg_eq] lemma eq_one_of_inv_eq' (h : a⁻¹ = a) : a = 1 := match lt_trichotomy a 1 with | or.inl h₁ := have 1 < a, from h ▸ one_lt_inv_of_inv h₁, absurd h₁ this.asymm | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 1, from h ▸ inv_inv_of_one_lt h₁, absurd h₁ this.asymm end @[to_additive exists_zero_lt] lemma exists_one_lt' [nontrivial α] : ∃ (a:α), 1 < a := begin obtain ⟨y, hy⟩ := exists_ne (1 : α), cases hy.lt_or_lt, { exact ⟨y⁻¹, one_lt_inv'.mpr h⟩ }, { exact ⟨y, h⟩ } end @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_no_top_order [nontrivial α] : no_top_order α := ⟨ begin obtain ⟨y, hy⟩ : ∃ (a:α), 1 < a := exists_one_lt', exact λ a, ⟨a * y, lt_mul_of_one_lt_right' a hy⟩ end ⟩ @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_no_bot_order [nontrivial α] : no_bot_order α := ⟨ begin obtain ⟨y, hy⟩ : ∃ (a:α), 1 < a := exists_one_lt', exact λ a, ⟨a / y, (div_lt_self_iff a).mpr hy⟩ end ⟩ end linear_ordered_comm_group section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {a b c : α} lemma le_of_forall_pos_le_add [densely_ordered α] (h : ∀ ε : α, 0 < ε → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ λ c hc, calc a ≤ b + (c - b) : h _ (sub_pos_of_lt hc) ... = c : add_sub_cancel'_right _ _ /-- `abs a` is the absolute value of `a`. -/ def abs (a : α) : α := max a (-a) lemma abs_of_nonneg (h : 0 ≤ a) : abs a = a := max_eq_left $ (neg_nonpos.2 h).trans h lemma abs_of_pos (h : 0 < a) : abs a = a := abs_of_nonneg h.le lemma abs_of_nonpos (h : a ≤ 0) : abs a = -a := max_eq_right $ h.trans (neg_nonneg.2 h) lemma abs_of_neg (h : a < 0) : abs a = -a := abs_of_nonpos h.le @[simp] lemma abs_zero : abs 0 = (0:α) := abs_of_nonneg le_rfl @[simp] lemma abs_neg (a : α) : abs (-a) = abs a := begin unfold abs, rw [max_comm, neg_neg] end @[simp] lemma abs_pos : 0 < abs a ↔ a ≠ 0 := begin rcases lt_trichotomy a 0 with (ha|rfl|ha), { simp [abs_of_neg ha, neg_pos, ha.ne, ha] }, { simp }, { simp [abs_of_pos ha, ha, ha.ne.symm] } end lemma abs_pos_of_pos (h : 0 < a) : 0 < abs a := abs_pos.2 h.ne.symm lemma abs_pos_of_neg (h : a < 0) : 0 < abs a := abs_pos.2 h.ne lemma abs_sub (a b : α) : abs (a - b) = abs (b - a) := by rw [← neg_sub, abs_neg] lemma abs_le' : abs a ≤ b ↔ a ≤ b ∧ -a ≤ b := max_le_iff lemma abs_le : abs a ≤ b ↔ - b ≤ a ∧ a ≤ b := by rw [abs_le', and.comm, neg_le] lemma neg_le_of_abs_le (h : abs a ≤ b) : -b ≤ a := (abs_le.mp h).1 lemma le_of_abs_le (h : abs a ≤ b) : a ≤ b := (abs_le.mp h).2 lemma le_abs : a ≤ abs b ↔ a ≤ b ∨ a ≤ -b := le_max_iff lemma le_abs_self (a : α) : a ≤ abs a := le_max_left _ _ lemma neg_le_abs_self (a : α) : -a ≤ abs a := le_max_right _ _ lemma abs_nonneg (a : α) : 0 ≤ abs a := (le_total 0 a).elim (λ h, h.trans (le_abs_self a)) (λ h, (neg_nonneg.2 h).trans $ neg_le_abs_self a) @[simp] lemma abs_abs (a : α) : abs (abs a) = abs a := abs_of_nonneg $ abs_nonneg a @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := not_iff_not.1 $ ne_comm.trans $ (abs_nonneg a).lt_iff_ne.symm.trans abs_pos @[simp] lemma abs_nonpos_iff {a : α} : abs a ≤ 0 ↔ a = 0 := (abs_nonneg a).le_iff_eq.trans abs_eq_zero lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := max_lt_iff.trans $ and.comm.trans $ by rw [neg_lt] lemma neg_lt_of_abs_lt (h : abs a < b) : -b < a := (abs_lt.mp h).1 lemma lt_of_abs_lt (h : abs a < b) : a < b := (abs_lt.mp h).2 lemma lt_abs : a < abs b ↔ a < b ∨ a < -b := lt_max_iff lemma max_sub_min_eq_abs' (a b : α) : max a b - min a b = abs (a - b) := begin cases le_total a b with ab ba, { rw [max_eq_right ab, min_eq_left ab, abs_of_nonpos, neg_sub], rwa sub_nonpos }, { rw [max_eq_left ba, min_eq_right ba, abs_of_nonneg], rwa sub_nonneg } end lemma max_sub_min_eq_abs (a b : α) : max a b - min a b = abs (b - a) := by { rw [abs_sub], exact max_sub_min_eq_abs' _ _ } lemma abs_add (a b : α) : abs (a + b) ≤ abs a + abs b := abs_le.2 ⟨(neg_add (abs a) (abs b)).symm ▸ add_le_add (neg_le.2 $ neg_le_abs_self _) (neg_le.2 $ neg_le_abs_self _), add_le_add (le_abs_self _) (le_abs_self _)⟩ lemma abs_sub_le_iff : abs (a - b) ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] lemma sub_le_of_abs_sub_le_left (h : abs (a - b) ≤ c) : b - c ≤ a := sub_le.1 $ (abs_sub_le_iff.1 h).2 lemma sub_le_of_abs_sub_le_right (h : abs (a - b) ≤ c) : a - c ≤ b := sub_le_of_abs_sub_le_left (abs_sub a b ▸ h) lemma sub_lt_of_abs_sub_lt_left (h : abs (a - b) < c) : b - c < a := sub_lt.1 $ (abs_sub_lt_iff.1 h).2 lemma sub_lt_of_abs_sub_lt_right (h : abs (a - b) < c) : a - c < b := sub_lt_of_abs_sub_lt_left (abs_sub a b ▸ h) lemma abs_sub_abs_le_abs_sub (a b : α) : abs a - abs b ≤ abs (a - b) := sub_le_iff_le_add.2 $ calc abs a = abs (a - b + b) : by rw [sub_add_cancel] ... ≤ abs (a - b) + abs b : abs_add _ _ lemma abs_abs_sub_abs_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := abs_sub_le_iff.2 ⟨abs_sub_abs_le_abs_sub _ _, by rw abs_sub; apply abs_sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≤ b) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : abs b ≤ max (abs a) (abs c) := abs_le'.2 ⟨by simp [hbc.trans (le_abs_self c)], by simp [(neg_le_neg hab).trans (neg_le_abs_self a)]⟩ theorem abs_le_abs (h₀ : a ≤ b) (h₁ : -a ≤ b) : abs a ≤ abs b := (abs_le'.2 ⟨h₀, h₁⟩).trans (le_abs_self b) lemma abs_max_sub_max_le_abs (a b c : α) : abs (max a c - max b c) ≤ abs (a - b) := begin simp_rw [abs_le, le_sub_iff_add_le, sub_le_iff_le_add, ← max_add_add_left], split; apply max_le_max; simp only [← le_sub_iff_add_le, ← sub_le_iff_le_add, sub_self, neg_le, neg_le_abs_self, neg_zero, abs_nonneg, le_abs_self] end lemma eq_of_abs_sub_eq_zero {a b : α} (h : abs (a - b) = 0) : a = b := sub_eq_zero.1 $ abs_eq_zero.1 h lemma abs_by_cases (P : α → Prop) {a : α} (h1 : P a) (h2 : P (-a)) : P (abs a) := sup_ind _ _ h1 h2 lemma abs_sub_le (a b c : α) : abs (a - c) ≤ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rw [sub_add_sub_cancel] ... ≤ abs (a - b) + abs (b - c) : abs_add _ _ lemma abs_add_three (a b c : α) : abs (a + b + c) ≤ abs a + abs b + abs c := (abs_add _ _).trans (add_le_add_right (abs_add _ _) _) lemma dist_bdd_within_interval {a b lb ub : α} (hal : lb ≤ a) (hau : a ≤ ub) (hbl : lb ≤ b) (hbu : b ≤ ub) : abs (a - b) ≤ ub - lb := abs_sub_le_iff.2 ⟨sub_le_sub hau hbl, sub_le_sub hbu hal⟩ lemma eq_of_abs_sub_nonpos (h : abs (a - b) ≤ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) end linear_ordered_add_comm_group /-- This is not so much a new structure as a construction mechanism for ordered groups, by specifying only the "positive cone" of the group. -/ class nonneg_add_comm_group (α : Type*) extends add_comm_group α := (nonneg : α → Prop) (pos : α → Prop := λ a, nonneg a ∧ ¬ nonneg (neg a)) (pos_iff : ∀ a, pos a ↔ nonneg a ∧ ¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a + b)) (nonneg_antisymm : ∀ {a}, nonneg a → nonneg (-a) → a = 0) namespace nonneg_add_comm_group variable [s : nonneg_add_comm_group α] include s @[reducible, priority 100] -- see Note [lower instance priority] instance to_ordered_add_comm_group : ordered_add_comm_group α := { le := λ a b, nonneg (b - a), lt := λ a b, pos (b - a), lt_iff_le_not_le := λ a b, by simp; rw [pos_iff]; simp, le_refl := λ a, by simp [zero_nonneg], le_trans := λ a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact add_nonneg nbc nab, le_antisymm := λ a b nab nba, eq_of_sub_eq_zero $ nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := λ a b nab c, by simpa [(≤), preorder.le] using nab, ..s } theorem nonneg_def {a : α} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem pos_def {a : α} : pos a ↔ 0 < a := show _ ↔ pos _, by simp theorem not_zero_pos : ¬ pos (0 : α) := mt pos_def.1 (lt_irrefl _) theorem zero_lt_iff_nonneg_nonneg {a : α} : 0 < a ↔ nonneg a ∧ ¬ nonneg (-a) := pos_def.symm.trans (pos_iff _) theorem nonneg_total_iff : (∀ a : α, nonneg a ∨ nonneg (-a)) ↔ (∀ a b : α, a ≤ b ∨ b ≤ a) := ⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this, λ h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩ /-- A `nonneg_add_comm_group` is a `linear_ordered_add_comm_group` if `nonneg` is total and decidable. -/ def to_linear_ordered_add_comm_group [decidable_pred (@nonneg α _)] (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : linear_ordered_add_comm_group α := { le := (≤), lt := (<), le_total := nonneg_total_iff.1 nonneg_total, decidable_le := by apply_instance, decidable_lt := by apply_instance, ..@nonneg_add_comm_group.to_ordered_add_comm_group _ s } end nonneg_add_comm_group namespace order_dual instance [ordered_add_comm_group α] : ordered_add_comm_group (order_dual α) := { add_left_neg := λ a : α, add_left_neg a, sub := λ a b, (a - b : α), ..order_dual.ordered_add_comm_monoid, ..show add_comm_group α, by apply_instance } instance [linear_ordered_add_comm_group α] : linear_ordered_add_comm_group (order_dual α) := { add_le_add_left := λ a b h c, @add_le_add_left α _ b a h _, ..order_dual.linear_order α, ..show add_comm_group α, by apply_instance } end order_dual namespace prod variables {G H : Type*} @[to_additive] instance [ordered_comm_group G] [ordered_comm_group H] : ordered_comm_group (G × H) := { .. prod.comm_group, .. prod.partial_order G H, .. prod.ordered_cancel_comm_monoid } end prod section type_tags instance [ordered_add_comm_group α] : ordered_comm_group (multiplicative α) := { ..multiplicative.comm_group, ..multiplicative.ordered_comm_monoid } instance [ordered_comm_group α] : ordered_add_comm_group (additive α) := { ..additive.add_comm_group, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_group α] : linear_ordered_comm_group (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_group } instance [linear_ordered_comm_group α] : linear_ordered_add_comm_group (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_group } end type_tags
11285024ffe45c268e463f2f708012d245f0cf26
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/polynomial/erase_lead.lean
3456ab2949a85dec313f387cfa7f8ff8cfb184de
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,979
lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.polynomial.degree.trailing_degree import data.polynomial.inductions /-! # Erase the leading term of a univariate polynomial ## Definition * `erase_lead f`: the polynomial `f - leading term of f` `erase_lead` serves as reduction step in an induction, shaving off one monomial from a polynomial. The definition is set up so that it does not mention subtraction in the definition, and thus works for polynomials over semirings as well as rings. -/ noncomputable theory open_locale classical open polynomial finset namespace polynomial variables {R : Type*} [semiring R] {f : polynomial R} /-- `erase_lead f` for a polynomial `f` is the polynomial obtained by subtracting from `f` the leading term of `f`. -/ def erase_lead (f : polynomial R) : polynomial R := polynomial.erase f.nat_degree f section erase_lead lemma erase_lead_support (f : polynomial R) : f.erase_lead.support = f.support.erase f.nat_degree := by simp only [erase_lead, support_erase] lemma erase_lead_coeff (i : ℕ) : f.erase_lead.coeff i = if i = f.nat_degree then 0 else f.coeff i := by simp only [erase_lead, coeff_erase] @[simp] lemma erase_lead_coeff_nat_degree : f.erase_lead.coeff f.nat_degree = 0 := by simp [erase_lead_coeff] lemma erase_lead_coeff_of_ne (i : ℕ) (hi : i ≠ f.nat_degree) : f.erase_lead.coeff i = f.coeff i := by simp [erase_lead_coeff, hi] @[simp] lemma erase_lead_zero : erase_lead (0 : polynomial R) = 0 := by simp only [erase_lead, erase_zero] @[simp] lemma erase_lead_add_monomial_nat_degree_leading_coeff (f : polynomial R) : f.erase_lead + monomial f.nat_degree f.leading_coeff = f := begin ext i, simp only [erase_lead_coeff, coeff_monomial, coeff_add, @eq_comm _ _ i], split_ifs with h, { subst i, simp only [leading_coeff, zero_add] }, { exact add_zero _ } end @[simp] lemma erase_lead_add_C_mul_X_pow (f : polynomial R) : f.erase_lead + (C f.leading_coeff) * X ^ f.nat_degree = f := by rw [C_mul_X_pow_eq_monomial, erase_lead_add_monomial_nat_degree_leading_coeff] @[simp] lemma self_sub_monomial_nat_degree_leading_coeff {R : Type*} [ring R] (f : polynomial R) : f - monomial f.nat_degree f.leading_coeff = f.erase_lead := (eq_sub_iff_add_eq.mpr (erase_lead_add_monomial_nat_degree_leading_coeff f)).symm @[simp] lemma self_sub_C_mul_X_pow {R : Type*} [ring R] (f : polynomial R) : f - (C f.leading_coeff) * X ^ f.nat_degree = f.erase_lead := by rw [C_mul_X_pow_eq_monomial, self_sub_monomial_nat_degree_leading_coeff] lemma erase_lead_ne_zero (f0 : 2 ≤ f.support.card) : erase_lead f ≠ 0 := begin rw [ne.def, ← card_support_eq_zero, erase_lead_support], exact (zero_lt_one.trans_le $ (nat.sub_le_sub_right f0 1).trans finset.pred_card_le_card_erase).ne.symm end @[simp] lemma nat_degree_not_mem_erase_lead_support : f.nat_degree ∉ (erase_lead f).support := by simp [not_mem_support_iff] lemma ne_nat_degree_of_mem_erase_lead_support {a : ℕ} (h : a ∈ (erase_lead f).support) : a ≠ f.nat_degree := by { rintro rfl, exact nat_degree_not_mem_erase_lead_support h } lemma erase_lead_support_card_lt (h : f ≠ 0) : (erase_lead f).support.card < f.support.card := begin rw erase_lead_support, exact card_lt_card (erase_ssubset $ nat_degree_mem_support_of_nonzero h) end lemma erase_lead_card_support {c : ℕ} (fc : f.support.card = c) : f.erase_lead.support.card = c - 1 := begin by_cases f0 : f = 0, { rw [← fc, f0, erase_lead_zero, support_zero, card_empty] }, { rw [erase_lead_support, card_erase_of_mem (nat_degree_mem_support_of_nonzero f0), fc], exact c.pred_eq_sub_one }, end lemma erase_lead_card_support' {c : ℕ} (fc : f.support.card = c + 1) : f.erase_lead.support.card = c := erase_lead_card_support fc @[simp] lemma erase_lead_monomial (i : ℕ) (r : R) : erase_lead (monomial i r) = 0 := begin by_cases hr : r = 0, { subst r, simp only [monomial_zero_right, erase_lead_zero] }, { rw [erase_lead, nat_degree_monomial _ _ hr, erase_monomial] } end @[simp] lemma erase_lead_C (r : R) : erase_lead (C r) = 0 := erase_lead_monomial _ _ @[simp] lemma erase_lead_X : erase_lead (X : polynomial R) = 0 := erase_lead_monomial _ _ @[simp] lemma erase_lead_X_pow (n : ℕ) : erase_lead (X ^ n : polynomial R) = 0 := by rw [X_pow_eq_monomial, erase_lead_monomial] @[simp] lemma erase_lead_C_mul_X_pow (r : R) (n : ℕ) : erase_lead (C r * X ^ n) = 0 := by rw [C_mul_X_pow_eq_monomial, erase_lead_monomial] lemma erase_lead_degree_le : (erase_lead f).degree ≤ f.degree := begin rw degree_le_iff_coeff_zero, intros i hi, rw erase_lead_coeff, split_ifs with h, { refl }, apply coeff_eq_zero_of_degree_lt hi end lemma erase_lead_nat_degree_le : (erase_lead f).nat_degree ≤ f.nat_degree := nat_degree_le_nat_degree erase_lead_degree_le lemma erase_lead_nat_degree_lt (f0 : 2 ≤ f.support.card) : (erase_lead f).nat_degree < f.nat_degree := lt_of_le_of_ne erase_lead_nat_degree_le $ ne_nat_degree_of_mem_erase_lead_support $ nat_degree_mem_support_of_nonzero $ erase_lead_ne_zero f0 lemma erase_lead_nat_degree_lt_or_erase_lead_eq_zero (f : polynomial R) : (erase_lead f).nat_degree < f.nat_degree ∨ f.erase_lead = 0 := begin by_cases h : f.support.card ≤ 1, { right, rw ← C_mul_X_pow_eq_self h, simp }, { left, apply erase_lead_nat_degree_lt (lt_of_not_ge h) } end end erase_lead /-- An induction lemma for polynomials. It takes a natural number `N` as a parameter, that is required to be at least as big as the `nat_degree` of the polynomial. This is useful to prove results where you want to change each term in a polynomial to something else depending on the `nat_degree` of the polynomial itself and not on the specific `nat_degree` of each term. -/ lemma induction_with_nat_degree_le {R : Type*} [semiring R] {P : polynomial R → Prop} (N : ℕ) (P_0 : P 0) (P_C_mul_pow : ∀ n : ℕ, ∀ r : R, r ≠ 0 → n ≤ N → P (C r * X ^ n)) (P_C_add : ∀ f g : polynomial R, f.nat_degree ≤ N → g.nat_degree ≤ N → P f → P g → P (f + g)) : ∀ f : polynomial R, f.nat_degree ≤ N → P f := begin intros f df, generalize' hd : card f.support = c, revert f, induction c with c hc, { assume f df f0, convert P_0, simpa only [support_eq_empty, card_eq_zero] using f0}, -- exact λ f df f0, by rwa (finsupp.support_eq_empty.mp (card_eq_zero.mp f0)) }, { intros f df f0, rw ← erase_lead_add_C_mul_X_pow f, refine P_C_add f.erase_lead _ (erase_lead_nat_degree_le.trans df) _ _ _, { exact (nat_degree_C_mul_X_pow_le f.leading_coeff f.nat_degree).trans df }, { exact hc _ (erase_lead_nat_degree_le.trans df) (erase_lead_card_support f0) }, { refine P_C_mul_pow _ _ _ df, rw [ne.def, leading_coeff_eq_zero], rintro rfl, exact not_le.mpr c.succ_pos f0.ge } } end end polynomial
859fe578e660d55196722daca29f7c4206283721
218e2afdf9209a5f82db7ac0089f79064e52bd80
/lean/src/chapter_2.lean
698d068bfca12328f2cc9bb185e212ea186c1f98
[ "MIT", "LicenseRef-scancode-unknown-license-reference" ]
permissive
mk12/analysis-i
1f022e81cdc5cae5b25360f93000186874e1d02b
e85deae5441f705d9284eeb204c9c3ecb450c93a
refs/heads/main
1,637,808,582,073
1,636,857,479,000
1,636,857,479,000
67,387,570
8
1
null
null
null
null
UTF-8
Lean
false
false
27,764
lean
-- Copyright 2017 Mitchell Kember. Subject to the MIT License. -- Formalization of Analysis I: Chapter 2 import .common open classical (by_cases by_contradiction) open eq (symm) namespace chapter_2 -- Definition 2.1.1: The natural numbers inductive N : Type | zero : N | succ : N → N namespace N -- Type class instances instance : has_zero N := ⟨zero⟩ instance : has_one N := ⟨succ zero⟩ -- Axiom 2.1: 0 is a natural number example : N := 0 -- Axiom 2.2: Every natural number has a successor example (n : N) : N := succ n -- Definition 2.1.3: Arabic numerals are defined as natural numbers def num : nat → N | 0 := 0 | (nat.succ n) := succ (num n) -- Proposition 2.1.4: 3 is a natural number example : N := num 3 -- Axiom 2.3: 0 is not a successor of any natural number theorem succ_ne_zero {n : N} : succ n ≠ 0 := suppose succ n = 0, N.no_confusion this -- Proposition 2.1.6: 4 is not equal to 0 example : num 4 ≠ 0 := succ_ne_zero -- Axiom 2.4: Different natural numbers have different successors theorem succ_inj {n m : N} : succ n = succ m → n = m := succ.inj -- Proposition 2.1.8: 6 is not equal to 2 example : num 6 ≠ num 2 := have num 4 ≠ num 0, from succ_ne_zero, have num 5 ≠ num 1, from mt succ_inj this, show num 6 ≠ num 2, from mt succ_inj this -- Axiom 2.5: Principle of mathematical induction theorem induction {p : N → Prop} : p 0 → (∀ n : N, p n → p (succ n)) → ∀ n : N, p n := N.rec -- More convenient form of Axiom 2.5 open N (renaming rec_on → induction_on) -- Proposition 2.1.16: Recursive definitions section recursive_def parameters (f : N → N → N) (c : N) private def a (n : N) : N := N.rec c f n example : a 0 = c := rfl example (n : N) : a (succ n) = f n (a n) := rfl end recursive_def -- Definition 2.2.1: Addition of natural numbers def add : N → N → N | 0 m := m | (succ n) m := succ (add n m) -- Type class instance instance : has_add N := ⟨add⟩ -- Lemma 2.2.2 lemma add_zero_right {n : N} : n + 0 = n := induction_on n (show zero + 0 = 0, from rfl) -- can't use 0 for some reason (assume (n : N), assume (IH : n + 0 = n), show succ n + 0 = succ n, from calc succ n + 0 = succ (n + 0) : rfl ... = succ n : by rw IH) -- Lemma 2.2.3 lemma add_succ_right {n m : N} : n + succ m = succ (n + m) := induction_on n (show 0 + succ m = succ (0 + m), from calc 0 + succ m = succ m : rfl ... = succ (0 + m) : rfl) (assume (n : N) (IH : n + succ m = succ (n + m)), show succ n + succ m = succ (succ n + m), from calc succ n + succ m = succ (n + succ m) : rfl ... = succ (succ (n + m)) : by rw IH ... = succ (succ n + m) : rfl) -- Proposition 2.2.4: Addition is commutative theorem add_comm {n m : N} : n + m = m + n := induction_on n (show 0 + m = m + 0, from calc 0 + m = m : rfl ... = m + 0 : add_zero_right.symm) (assume (n : N) (IH : n + m = m + n), show succ n + m = m + succ n, from calc succ n + m = succ (n + m) : rfl ... = succ (m + n) : by rw IH ... = m + succ n : add_succ_right.symm) -- Proposition 2.2.5: Addition is associative theorem add_assoc {a b c : N} : (a + b) + c = a + (b + c) := induction_on a (show (0 + b) + c = 0 + (b + c), from calc (0 + b) + c = b + c : rfl ... = 0 + (b + c) : rfl) (assume (a : N) (IH : (a + b) + c = a + (b + c)), show (succ a + b) + c = succ a + (b + c), from calc (succ a + b) + c = succ (a + b) + c : rfl ... = succ ((a + b) + c) : rfl ... = succ (a + (b + c)) : by rw IH ... = succ a + (b + c) : rfl) -- Proposition 2.2.6: Cancellation law theorem add_cancel {a b c : N} : a + b = a + c → b = c := induction_on a (suppose 0 + b = 0 + c, show b = c, from calc b = 0 + b : rfl ... = 0 + c : this ... = c : rfl) (assume (a : N) (IH : a + b = a + c → b = c), suppose succ a + b = succ a + c, have succ (a + b) = succ (a + c), from calc succ (a + b) = succ a + b : rfl ... = succ a + c : this ... = succ (a + c) : rfl, have a + b = a + c, from succ_inj this, show b = c, from IH this) -- Definition 2.2.7: Positive natural numbers def pos (n : N) : Prop := n ≠ 0 -- Proposition 2.2.8 theorem add_pos {a b : N} (H : pos a) : pos (a + b) := induction_on b (show pos (a + 0), by rw add_zero_right; exact H) (assume (b : N) (IH : pos (a + b)), have pos (succ (a + b)), from succ_ne_zero, show pos (a + succ b), by rw add_succ_right; exact this) -- Corollary 2.2.9 theorem add_eq_zero {a b : N} (H : a + b = 0) : a = 0 ∧ b = 0 := by_cases (assume Ha : a = 0, have b = 0, by rw Ha at H; exact H, show a = 0 ∧ b = 0, from ⟨Ha, this⟩) (suppose a ≠ 0, have pos (a + b), from add_pos this, absurd H this) -- Lemma 2.2.10 lemma pos_pred {a : N} : pos a → ∃ b : N, succ b = a := induction_on a (suppose pos 0, absurd rfl this) (assume (a : N) (IH : pos a → ∃ b : N, succ b = a) (H : pos (succ a)), show ∃ b : N, succ b = succ a, from ⟨a, rfl⟩) -- Definition 2.2.11: Ordering of the natural numbers def le (n m : N) : Prop := ∃ a : N, m = n + a instance : has_le N := ⟨le⟩ def lt (n m : N) : Prop := n ≤ m ∧ n ≠ m instance : has_lt N := ⟨lt⟩ -- Proposition 2.2.12: Basic properties of order for natural numbers section order_properties variables {a b c : N} theorem order_refl : a ≥ a := ⟨0, add_zero_right.symm⟩ theorem order_trans : a ≥ b → b ≥ c → a ≥ c | ⟨n, (Hn : a = b + n)⟩ ⟨m, (Hm : b = c + m)⟩ := have a = c + (m + n), from calc a = b + n : Hn ... = c + m + n : by rw Hm ... = c + (m + n) : add_assoc, show a ≥ c, from ⟨m + n, this⟩ theorem order_antisymm : a ≥ b → b ≥ a → a = b | ⟨n, (Hn : a = b + n)⟩ ⟨m, (Hm : b = a + m)⟩ := have a + 0 = a + (m + n), from calc a + 0 = a : add_zero_right ... = b + n : Hn ... = a + m + n : by rw Hm ... = a + (m + n) : add_assoc, have m + n = 0, from add_cancel this.symm, have m = 0 ∧ n = 0, from add_eq_zero this, show a = b, from calc a = b + n : Hn ... = b + 0 : by rw this.right ... = b : add_zero_right theorem ge_iff_add_ge : a ≥ b ↔ a + c ≥ b + c := iff.intro (suppose a ≥ b, let ⟨n, (H : a = b + n)⟩ := this in have a + c = b + c + n, from calc a + c = b + n + c : by rw H ... = b + (n + c) : add_assoc ... = b + (c + n) : by rw @add_comm n c ... = b + c + n : add_assoc.symm, show a + c ≥ b + c, from ⟨n, this⟩) (suppose a + c ≥ b + c, let ⟨n, (H : a + c = b + c + n)⟩ := this in have c + a = c + (b + n), from calc c + a = a + c : add_comm ... = b + c + n : H ... = c + b + n : by rw @add_comm b c ... = c + (b + n) : add_assoc, have a = b + n, from add_cancel this, show a ≥ b, from ⟨n, this⟩) theorem lt_iff_pos : a < b ↔ ∃ d : N, b = a + d ∧ pos d := iff.intro (suppose a < b, have H : a ≤ b ∧ a ≠ b, from this, let ⟨d, (Hd : b = a + d)⟩ := H.left in have pos d, from suppose d = 0, have b = a, from calc b = a + d : Hd ... = a + 0 : by rw this ... = a : add_zero_right, absurd this.symm H.right, show ∃ d : N, b = a + d ∧ pos d, from ⟨d, ⟨Hd, this⟩⟩) (suppose ∃ d : N, b = a + d ∧ pos d, let ⟨d, (H : b = a + d ∧ pos d)⟩ := this in have H₁ : a ≤ b, from ⟨d, H.left⟩, have H₂ : a ≠ b, from suppose a = b, have b + 0 = b + d, from calc b + 0 = b : add_zero_right ... = a + d : H.left ... = b + d : by rw this, have 0 = d, from add_cancel this, absurd this.symm H.right, show a < b, from ⟨H₁, H₂⟩) theorem lt_iff_succ_le : a < b ↔ succ a ≤ b := iff.intro (suppose a < b, let ⟨d, (H₁ : b = a + d ∧ pos d)⟩ := lt_iff_pos.mp this, ⟨d', (H₂ : succ d' = d)⟩ := pos_pred H₁.right in have b = succ a + d', from calc b = a + d : H₁.left ... = a + succ d' : by rw H₂ ... = succ (a + d') : add_succ_right ... = succ a + d' : rfl, show succ a ≤ b, from ⟨d', this⟩) (suppose succ a ≤ b, let ⟨n, (H : b = succ a + n)⟩ := this in have b = a + succ n, from calc b = succ a + n : H ... = succ (a + n) : rfl ... = a + succ n : add_succ_right.symm, have H₁ : a ≤ b, from ⟨succ n, this⟩, have H₂ : a ≠ b, from suppose a = b, have a + 0 = a + succ n, from calc a + 0 = a : add_zero_right ... = b : this ... = succ a + n : H ... = succ (a + n) : rfl ... = a + succ n : add_succ_right.symm, have 0 = succ n, from add_cancel this, absurd this.symm succ_ne_zero, show a < b, from ⟨H₁, H₂⟩) -- Extra properties theorem lt_succ_iff_le : a < succ b ↔ a ≤ b := iff.intro (suppose a < succ b, let ⟨d, (H₁ : succ b = a + d ∧ pos d)⟩ := lt_iff_pos.mp this, ⟨d', (H₂ : succ d' = d)⟩ := pos_pred H₁.right in have succ b = succ (a + d'), from calc succ b = a + d : H₁.left ... = a + succ d' : by rw H₂ ... = succ (a + d') : add_succ_right, have b = a + d', from succ_inj this, show a ≤ b, from ⟨d', this⟩) (suppose a ≤ b, let ⟨n, (H : b = a + n)⟩ := this in have succ b = a + succ n, from calc succ b = succ (a + n) : by rw H ... = a + succ n : add_succ_right.symm, have H₁ : a ≤ succ b, from ⟨succ n, this⟩, have H₂ : a ≠ succ b, from suppose a = succ b, have b + 0 = b + succ n, from calc b + 0 = b : add_zero_right ... = a + n : H ... = succ b + n : by rw this ... = succ (b + n) : rfl ... = b + succ n : add_succ_right.symm, have 0 = succ n, from add_cancel this, absurd this.symm succ_ne_zero, show a < succ b, from ⟨H₁, H₂⟩) theorem not_lt_and_ge : ¬ (a < b ∧ a ≥ b) := assume H : a < b ∧ a ≥ b, let ⟨n, (Hn : b = a + n)⟩ := H.left.left, ⟨m, (Hm : a = b + m)⟩ := H.right in have H₁ : a ≠ b, from H.left.right, have a + 0 = a + (n + m), from calc a + 0 = a : add_zero_right ... = b + m : Hm ... = a + n + m : by rw Hn ... = a + (n + m) : add_assoc, have 0 = n + m, from add_cancel this, have m = 0, from (add_eq_zero this.symm).right, have a = b, from calc a = b + m : Hm ... = b + 0 : by rw this ... = b : add_zero_right, absurd this H₁ theorem not_le_and_gt : ¬ (a ≤ b ∧ a > b) := suppose a ≤ b ∧ a > b, have b < a ∧ b ≥ a, from this.swap, absurd this (@not_lt_and_ge b a) theorem not_lt_zero : ¬ (a < 0) := suppose a < 0, let ⟨n, (H : 0 = a + n)⟩ := this.left in have H₁ : a ≠ 0, from this.right, have H₂ : a = 0, from (add_eq_zero H.symm).left, absurd H₂ H₁ end order_properties -- Proposition 2.2.13: Trichotomy of order for natural numbers section trichotomy variables {a b : N} theorem trichotomy : a < b ∨ a = b ∨ a > b := induction_on a (show 0 < b ∨ 0 = b ∨ 0 > b, from have H : 0 ≤ b, from ⟨b, rfl⟩, by_cases (suppose 0 = b, or.inr (or.inl this)) (suppose 0 ≠ b, or.inl ⟨H, this⟩)) (assume (a : N) (IH : a < b ∨ a = b ∨ a > b), show succ a < b ∨ succ a = b ∨ succ a > b, from IH.elim3 (suppose a < b, have H : succ a ≤ b, from lt_iff_succ_le.mp this, by_cases (suppose succ a = b, or.inr (or.inl this)) (suppose succ a ≠ b, or.inl ⟨H, this⟩)) (suppose a = b, have H₁ : succ a = b + 1, from calc succ a = succ b : by rw this ... = succ b + 0 : add_zero_right.symm ... = succ (b + 0) : rfl ... = b + 1 : add_succ_right.symm, have H₂ : b ≠ succ a, from suppose b = succ a, have b + 0 = b + 1, from calc b + 0 = b : add_zero_right ... = succ a : this ... = b + 1 : H₁, have 0 = succ 0, from add_cancel this, absurd this.symm succ_ne_zero, have succ a ≥ b, from ⟨1, H₁⟩, have succ a > b, from ⟨this, H₂⟩, show succ a < b ∨ succ a = b ∨ succ a > b, from or.inr (or.inr this)) (suppose a > b, let ⟨n, (Hn : a = b + n)⟩ := this.left in have b ≠ a, from this.right, have H₁ : succ a = b + succ n, from calc succ a = succ (b + n) : by rw Hn ... = b + succ n : add_succ_right.symm, have H₂ : b ≠ succ a, from suppose b = succ a, have succ a + 0 = succ a + succ n, from calc succ a + 0 = b + succ n + 0 : by rw H₁ ... = b + succ n : add_zero_right ... = succ a + succ n : by rw this, have 0 = succ n, from add_cancel this, absurd this.symm succ_ne_zero, have succ a ≥ b, from ⟨succ n, H₁⟩, have succ a > b, from ⟨this, H₂⟩, show succ a < b ∨ succ a = b ∨ succ a > b, from or.inr (or.inr this))) theorem not_eq_and_lt : ¬ (a = b ∧ a < b) := assume H : a = b ∧ a < b, have a ≠ b, from H.right.right, absurd H.left this theorem not_eq_and_gt : ¬ (a = b ∧ a > b) := assume H : a = b ∧ a > b, have a ≠ b, from H.right.right.symm, absurd H.left this theorem not_lt_and_gt : ¬ (a < b ∧ a > b) := assume H : a < b ∧ a > b, have a ≤ b ∧ a > b, from ⟨H.left.left, H.right⟩, absurd this not_le_and_gt end trichotomy -- Some more order properties section order_equivalence variables {a b : N} theorem le_iff_not_gt : a ≤ b ↔ ¬ a > b := iff.intro (assume (H₁ : a ≤ b) (H₂ : a > b), have a ≤ b ∧ a > b, from ⟨H₁, H₂⟩, absurd this not_le_and_gt) (assume H : ¬ a > b, show a ≤ b, from trichotomy.elim3 (suppose a < b, this.left) (suppose a = b, ⟨0, this.symm ▸ add_zero_right.symm⟩) (suppose a > b, absurd this H)) theorem gt_iff_not_le : a > b ↔ ¬ a ≤ b := iff.intro (suppose a > b, have ¬ ¬ a > b, from not_not_intro this, show ¬ a ≤ b, from mt le_iff_not_gt.mp this) (suppose ¬ a ≤ b, have ¬ ¬ a > b, from mt le_iff_not_gt.mpr this, show a > b, from not_not_elim this) theorem ge_iff_not_lt : a ≥ b ↔ ¬ a < b := @le_iff_not_gt b a theorem lt_iff_not_ge : a < b ↔ ¬ a ≥ b := @gt_iff_not_le b a end order_equivalence -- Proposition 2.2.14: Strong principle of induction section strong_induction parameters (p : N → Prop) (n₀ : N) private def q (n : N) : Prop := ∀ m : N, m ≥ n₀ ∧ m < n → p m theorem strong_induction (SI : ∀ {n : N}, n ≥ n₀ → q n → p n) : ∀ n : N, n ≥ n₀ → p n := assume n : N, suffices H : n ≥ n₀ → q n, from suppose n ≥ n₀, show p n, from SI this (H this), show n ≥ n₀ → q n, from induction_on n (show 0 ≥ n₀ → q 0, from assume (H₁ : 0 ≥ n₀) (m : N) (H₂ : m ≥ n₀ ∧ m < 0), absurd H₂.right not_lt_zero) (assume (n : N) (IH : n ≥ n₀ → q n), show succ n ≥ n₀ → q (succ n), from assume (H₁ : succ n ≥ n₀) (m : N) (H₂ : m ≥ n₀ ∧ m < succ n), show p m, from by_cases (suppose n₀ = succ n, have m ≥ succ n, from this ▸ H₂.left, have m < succ n ∧ m ≥ succ n, from ⟨H₂.right, this⟩, absurd this not_lt_and_ge) (suppose n₀ ≠ succ n, have succ n > n₀, from ⟨H₁, this⟩, have n ≥ n₀, from lt_succ_iff_le.mp this, have Hqn : q n, from IH this, have Hpn : p n, from SI this Hqn, show p m, from by_cases (suppose n = m, this ▸ Hpn) (assume H : n ≠ m, have m ≤ n, from lt_succ_iff_le.mp H₂.right, have m < n, from ⟨this, H.symm⟩, have m ≥ n₀ ∧ m < n, from ⟨H₂.left, this⟩, show p m, from Hqn m this))) end strong_induction -- Exercise 2.2.6: Backward principle of induction example (n : N) (p : N → Prop) (BI : ∀ m : N, p (succ m) → p m) (Hp : p n) : ∀ m : N, m ≤ n → p m := by_contradiction (suppose ¬ ∀ m : N, m ≤ n → p m, let ⟨m, (H₁ : ¬ (m ≤ n → p m))⟩ := dm_exists_not this in have m ≤ n ∧ ¬ p m, from not_imp_iff_and_not.mp H₁, let ⟨d, (H₂ : n = m + d)⟩ := this.left in have H₃ : ¬ p m, from this.right, have ¬ p (m + d), from induction_on d (show ¬ p (m + 0), by rw add_zero_right; exact H₃) (assume (d : N) (IH : ¬ p (m + d)), have p (succ (m + d)) → p (m + d), from BI (m + d), have ¬ p (succ (m + d)), from mt this IH, show ¬ p (m + succ d), by rw add_succ_right; exact this), have ¬ p n, from H₂.symm ▸ this, absurd Hp this) -- Definition 2.3.1: Multiplication of natural numbers def mul : N → N → N | 0 m := 0 | (succ n) m := mul n m + m -- Type class instance instance : has_mul N := ⟨mul⟩ -- Part of Exercise 2.3.1 lemma mul_zero_right {n : N} : n * 0 = 0 := induction_on n (show zero * 0 = 0, from rfl) -- can't use 0 for some reason (assume (n : N) (IH : n * 0 = 0), show succ n * 0 = 0, from calc succ n * 0 = n * 0 + 0 : rfl ... = 0 + 0 : by rw IH ... = 0 : rfl) -- Part of Exercise 2.3.1 lemma mul_succ_right {n m : N} : n * succ m = n * m + n := induction_on n (show 0 * succ m = 0 * m + 0, from calc 0 * succ m = 0 : rfl ... = 0 + 0 : rfl ... = 0 * m + 0 : rfl) (assume (n : N) (IH : n * succ m = n * m + n), show succ n * succ m = succ n * m + succ n, from calc succ n * succ m = n * succ m + succ m : rfl ... = n * m + n + succ m : by rw IH ... = n * m + (n + succ m) : add_assoc ... = n * m + (succ m + n) : by rw @add_comm (succ m) n ... = n * m + succ (m + n) : rfl ... = n * m + (m + succ n) : by rw @add_succ_right m n ... = n * m + m + succ n : add_assoc.symm ... = succ n * m + succ n : rfl) -- Lemma 2.3.2: Multiplication is commutative lemma mul_comm {n m : N} : n * m = m * n := induction_on n (show 0 * m = m * 0, from calc 0 * m = 0 : rfl ... = m * 0 : mul_zero_right.symm) (assume (n : N) (IH : n * m = m * n), show succ n * m = m * succ n, from calc succ n * m = n * m + m : rfl ... = m * n + m : by rw IH ... = m * succ n : mul_succ_right.symm) -- Lemma 2.3.3: Natural numbers have no zero divisors lemma mul_eq_zero {n m : N} : n * m = 0 ↔ n = 0 ∨ m = 0 := iff.intro (assume H₁ : n * m = 0, show n = 0 ∨ m = 0, from by_cases (suppose n = 0, or.inl this) (suppose n ≠ 0, let ⟨n', (H₂ : succ n' = n)⟩ := pos_pred this in have 0 = n' * m + m, from calc 0 = n * m : H₁.symm ... = succ n' * m : by rw H₂ ... = n' * m + m : rfl, have m = 0, from (add_eq_zero this.symm).right, show n = 0 ∨ m = 0, from or.inr this)) (suppose n = 0 ∨ m = 0, show n * m = 0, from this.elim (assume Hn : n = 0, have 0 * m = 0, from rfl, show n * m = 0, from Hn.symm ▸ this) (assume Hm : m = 0, have n * 0 = 0, from mul_zero_right, show n * m = 0, from Hm.symm ▸ this)) -- Alternative form of Lemma 2.3.3 lemma mul_pos {n m : N} : pos (n * m) ↔ pos n ∧ pos m := iff.intro (suppose pos (n * m), have ¬ (n = 0 ∨ m = 0), from mt mul_eq_zero.mpr this, show pos n ∧ pos m, from dm_not_and_not this) (suppose pos n ∧ pos m, have ¬ (n = 0 ∨ m = 0), from dm_not_or this, show pos (n * m), from mt mul_eq_zero.mp this) -- Proposition 2.3.4: Distributive law section distributive_law variables {a b c : N} theorem left_distrib : a * (b + c) = a * b + a * c := induction_on c (show a * (b + 0) = a * b + a * 0, from calc a * (b + 0) = a * b : by rw @add_zero_right b ... = a * b + 0 : add_zero_right.symm ... = a * b + a * 0 : by rw @mul_zero_right a) (assume (c : N) (IH : a * (b + c) = a * b + a * c), show a * (b + succ c) = a * b + a * succ c, from calc a * (b + succ c) = a * succ (b + c) : by rw @add_succ_right b c ... = a * (b + c) + a : mul_succ_right ... = a * b + a * c + a : by rw IH ... = a * b + (a * c + a) : add_assoc ... = a * b + a * succ c : by rw @mul_succ_right a c) theorem right_distrib : (b + c) * a = b * a + c * a := calc (b + c) * a = a * (b + c) : mul_comm ... = a * b + a * c : left_distrib ... = b * a + a * c : by rw @mul_comm b a ... = b * a + c * a : by rw @mul_comm c a end distributive_law -- Proposition 2.3.5: Multiplication is associative theorem mul_assoc {a b c : N} : (a * b) * c = a * (b * c) := induction_on a (show (0 * b) * c = 0 * (b * c), from calc (0 * b) * c = 0 * c : rfl ... = 0 : rfl ... = 0 * (b * c) : rfl) (assume (a : N) (IH : (a * b) * c = a * (b * c)), show (succ a * b) * c = succ a * (b * c), from calc (succ a * b) * c = (a * b + b) * c : rfl ... = (a * b) * c + b * c : right_distrib ... = a * (b * c) + b * c : by rw IH ... = succ a * (b * c) : rfl) -- Proposition 2.3.6: Multiplication preserves order theorem mul_lt_mul {a b c : N} (H₁ : a < b) (H₂ : pos c) : a * c < b * c := let ⟨d, (Hd : b = a + d ∧ pos d)⟩ := lt_iff_pos.mp H₁ in have H₃ : b * c = a * c + d * c, from calc b * c = (a + d) * c : by rw Hd.left ... = a * c + d * c : right_distrib, have pos d ∧ pos c, from ⟨Hd.right, H₂⟩, have pos (d * c), from mul_pos.mpr this, show a * c < b * c, from lt_iff_pos.mpr ⟨d * c, ⟨H₃, this⟩⟩ -- Corollary 2.3.7: Cancellation law theorem mul_cancel {a b c : N} (H₁ : a * c = b * c) (H₂ : pos c) : a = b := trichotomy.elim3 (suppose a < b, have a * c < b * c, from mul_lt_mul this H₂, absurd H₁ this.right) (suppose a = b, this) (suppose a > b, have a * c > b * c, from mul_lt_mul this H₂, absurd H₁ this.right.symm) -- Proposition 2.3.9: Euclidean algorithm theorem euclid_alg {n q : N} (H : pos q) : ∃ m r : N, r < q ∧ n = m * q + r := have 0 ≤ q, from ⟨q, rfl⟩, have H₀ : 0 < q, from ⟨this, H.symm⟩, induction_on n (show ∃ m r : N, r < q ∧ 0 = m * q + r, from have 0 = 0 * q + 0, from calc 0 = 0 * q : rfl ... = 0 * q + 0 : add_zero_right.symm, have 0 < q ∧ 0 = 0 * q + 0, from ⟨H₀, this⟩, ⟨0, ⟨0, this⟩⟩) (assume (n : N) (IH : ∃ m r : N, r < q ∧ n = m * q + r), let ⟨m, ⟨r, (IH : r < q ∧ n = m * q + r)⟩⟩ := IH in show ∃ m r : N, r < q ∧ succ n = m * q + r, from by_cases (suppose succ r = q, have succ n = succ m * q + 0, from calc succ n = succ (m * q + r) : by rw IH.right ... = m * q + succ r : add_succ_right.symm ... = m * q + q : by rw this ... = q * m + q : by rw @mul_comm q m ... = q * succ m : mul_succ_right.symm ... = succ m * q : mul_comm ... = succ m * q + 0 : add_zero_right.symm, have 0 < q ∧ succ n = succ m * q + 0, from ⟨H₀, this⟩, ⟨succ m, ⟨0, this⟩⟩) (suppose succ r ≠ q, have LT : succ r < q, from ⟨lt_iff_succ_le.mp IH.left, this⟩, have succ n = m * q + succ r, from calc succ n = succ (m * q + r) : by rw IH.right ... = m * q + succ r : add_succ_right.symm, have succ r < q ∧ succ n = m * q + succ r, from ⟨LT, this⟩, ⟨m, ⟨succ r, this⟩⟩)) -- Definition 2.3.11: Exponentiation for natural numbers def pow : N → N → N | m 0 := 1 | m (succ n) := pow m n * m -- Can't use ^ because has_pow_nat requires nat infixr `**`:80 := pow -- Exercise 2.3.4: Square of binomial example (a b : N) : (a + b)**2 = a**2 + 2 * a * b + b**2 := have b * a + a * b = 2 * a * b, from calc b * a + a * b = a * b + a * b : by rw @mul_comm a b ... = 0 + a * b + a * b : rfl ... = a * b * 0 + a * b + a * b : by rw @mul_zero_right (a * b) ... = a * b * succ 0 + a * b : by rw @mul_succ_right (a * b) 0 ... = a * b * succ (succ 0) : mul_succ_right.symm ... = a * b * 2 : rfl ... = 2 * (a * b) : mul_comm ... = 2 * a * b : mul_assoc.symm, show (a + b)**2 = a**2 + 2 * a * b + b**2, from calc (a + b)**2 = (a + b) * (a + b) : rfl ... = (a + b) * a + (a + b) * b : left_distrib ... = a * a + b * a + (a + b) * b : by rw @right_distrib a a b ... = a * a + b * a + (a * b + b * b) : by rw @right_distrib b a b ... = a * a + b * a + a * b + b * b : by rw @add_assoc (a * a + b * a) (a * b) (b * b) ... = a**2 + b * a + a * b + b * b : rfl ... = a**2 + b * a + a * b + b**2 : rfl ... = a**2 + (b * a + a * b) + b**2 : by rw @add_assoc (a**2) (b * a) (a * b) ... = a**2 + 2 * a * b + b**2 : by rw this end N end chapter_2
cac57f485dfbd8b9bb3f0711698787d660078616
cbb1957fc3e28e502582c54cbce826d666350eda
/fabstract/Wiles_A_and_Taylor_R_FermatLast/fabstract.lean
a560562e055cc61cd55f0f5a67b46086a2f62ecb
[ "CC-BY-4.0" ]
permissive
andrejbauer/formalabstracts
9040b172da080406448ad1b0260d550122dcad74
a3b84fd90901ccf4b63eb9f95d4286a8775864d0
refs/heads/master
1,609,476,417,918
1,501,541,742,000
1,501,541,760,000
97,241,872
1
0
null
1,500,042,191,000
1,500,042,191,000
null
UTF-8
Lean
false
false
672
lean
import meta_data namespace Wiles_A_and_Taylor_R_FermatLast -- the statement of Fermat's Last Theorem axiom fermat_last_theorem : ∀ (x y z n : nat), x > 0 → y > 0 → n > 2 → x ^ n + y ^ n ≠ z ^ n definition fabstract : meta_data := { description := "A result in number theory conjectured by Pierre de Fermat and proved by Andrew Wiles and Richard Taylor. Colloquially referred to as Fermat's Last Theorem.", authors := [ {name := "Andrew Wiles"}, {name := "Richard Tylor"} ], primary := cite.DOI "10.2307/2118559", secondary := [cite.DOI "10.2307/2118560"], results := [result.Proof fermat_last_theorem] } end Wiles_A_and_Taylor_R_FermatLast
ad9e15736b86ff98d2266a062351ad8f831e6b8a
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Lean/Syntax.lean
e9e6b688e65e3495cb6de05d3209d64b608b167a
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,751
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich, Leonardo de Moura -/ prelude import Init.Data.Array import Init.Lean.Data.Name import Init.Lean.Data.Format namespace Lean namespace SourceInfo def updateTrailing (info : SourceInfo) (trailing : Option Substring) : SourceInfo := { info with trailing := trailing } end SourceInfo /- Syntax AST -/ def Syntax.isMissing : Syntax → Bool | Syntax.missing => true | _ => false inductive IsNode : Syntax → Prop | mk (kind : SyntaxNodeKind) (args : Array Syntax) : IsNode (Syntax.node kind args) def SyntaxNode : Type := {s : Syntax // IsNode s } def unreachIsNodeMissing {β} (h : IsNode Syntax.missing) : β := False.elim (nomatch h) def unreachIsNodeAtom {β} {info val} (h : IsNode (Syntax.atom info val)) : β := False.elim (nomatch h) def unreachIsNodeIdent {β info rawVal val preresolved} (h : IsNode (Syntax.ident info rawVal val preresolved)) : β := False.elim (nomatch h) namespace SyntaxNode @[inline] def getKind (n : SyntaxNode) : SyntaxNodeKind := match n with | ⟨Syntax.node k args, _⟩ => k | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h @[inline] def withArgs {β} (n : SyntaxNode) (fn : Array Syntax → β) : β := match n with | ⟨Syntax.node _ args, _⟩ => fn args | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h @[inline] def getNumArgs (n : SyntaxNode) : Nat := withArgs n $ fun args => args.size @[inline] def getArg (n : SyntaxNode) (i : Nat) : Syntax := withArgs n $ fun args => args.get! i @[inline] def getArgs (n : SyntaxNode) : Array Syntax := withArgs n $ fun args => args @[inline] def modifyArgs (n : SyntaxNode) (fn : Array Syntax → Array Syntax) : Syntax := match n with | ⟨Syntax.node kind args, _⟩ => Syntax.node kind (fn args) | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h end SyntaxNode namespace Syntax def setAtomVal : Syntax → String → Syntax | atom info _, v => (atom info v) | stx, _ => stx @[inline] def ifNode {β} (stx : Syntax) (hyes : SyntaxNode → β) (hno : Unit → β) : β := match stx with | Syntax.node k args => hyes ⟨Syntax.node k args, IsNode.mk k args⟩ | _ => hno () @[inline] def ifNodeKind {β} (stx : Syntax) (kind : SyntaxNodeKind) (hyes : SyntaxNode → β) (hno : Unit → β) : β := match stx with | Syntax.node k args => if k == kind then hyes ⟨Syntax.node k args, IsNode.mk k args⟩ else hno () | _ => hno () def asNode : Syntax → SyntaxNode | Syntax.node kind args => ⟨Syntax.node kind args, IsNode.mk kind args⟩ | _ => ⟨Syntax.node nullKind #[], IsNode.mk nullKind #[]⟩ def getNumArgs (stx : Syntax) : Nat := stx.asNode.getNumArgs def setArgs (stx : Syntax) (args : Array Syntax) : Syntax := match stx with | node k _ => node k args | stx => stx @[inline] def modifyArgs (stx : Syntax) (fn : Array Syntax → Array Syntax) : Syntax := match stx with | node k args => node k (fn args) | stx => stx def setArg (stx : Syntax) (i : Nat) (arg : Syntax) : Syntax := match stx with | node k args => node k (args.set! i arg) | stx => stx @[inline] def modifyArg (stx : Syntax) (i : Nat) (fn : Syntax → Syntax) : Syntax := match stx with | node k args => node k (args.modify i fn) | stx => stx def getIdAt (stx : Syntax) (i : Nat) : Name := (stx.getArg i).getId @[specialize] partial def mreplace {m : Type → Type} [Monad m] (fn : Syntax → m (Option Syntax)) : Syntax → m (Syntax) | stx@(node kind args) => do o ← fn stx; match o with | some stx => pure stx | none => do args ← args.mapM mreplace; pure (node kind args) | stx => do o ← fn stx; pure $ o.getD stx @[specialize] partial def mrewriteBottomUp {m : Type → Type} [Monad m] (fn : Syntax → m (Syntax)) : Syntax → m (Syntax) | node kind args => do args ← args.mapM mrewriteBottomUp; fn (node kind args) | stx => fn stx @[inline] def rewriteBottomUp (fn : Syntax → Syntax) (stx : Syntax) : Syntax := Id.run $ stx.mrewriteBottomUp fn private def updateInfo : SourceInfo → String.Pos → SourceInfo | {leading := some {str := s, startPos := _, stopPos := _}, pos := some pos, trailing := trailing}, last => {leading := some {str := s, startPos := last, stopPos := pos}, pos := some pos, trailing := trailing} | info, _ => info /- Remark: the State `String.Pos` is the `SourceInfo.trailing.stopPos` of the previous token, or the beginning of the String. -/ @[inline] private def updateLeadingAux : Syntax → StateM String.Pos (Option Syntax) | atom info@{trailing := some trail, ..} val => do last ← get; set trail.stopPos; let newInfo := updateInfo info last; pure $ some (atom newInfo val) | ident info@{trailing := some trail, ..} rawVal val pre => do last ← get; set trail.stopPos; let newInfo := updateInfo info last; pure $ some (ident newInfo rawVal val pre) | _ => pure none /-- Set `SourceInfo.leading` according to the trailing stop of the preceding token. The Result is a round-tripping Syntax tree IF, in the input Syntax tree, * all leading stops, atom contents, and trailing starts are correct * trailing stops are between the trailing start and the next leading stop. Remark: after parsing all `SourceInfo.leading` fields are Empty. The Syntax argument is the output produced by the Parser for `source`. This Function "fixes" the `source.leanding` field. Note that, the `SourceInfo.trailing` fields are correct. The implementation of this Function relies on this property. -/ def updateLeading : Syntax → Syntax := fun stx => (mreplace updateLeadingAux stx).run' 0 partial def updateTrailing (trailing : Option Substring) : Syntax → Syntax | Syntax.atom info val => Syntax.atom (info.updateTrailing trailing) val | Syntax.ident info rawVal val pre => Syntax.ident (info.updateTrailing trailing) rawVal val pre | n@(Syntax.node k args) => if args.size == 0 then n else let i := args.size - 1; let last := updateTrailing (args.get! i); let args := args.set! i last; Syntax.node k args | s => s def getPos (stx : Syntax) : Option String.Pos := stx.getHeadInfo >>= SourceInfo.pos partial def getTailWithPos : Syntax → Option Syntax | stx@(atom { pos := some _, .. } _) => some stx | stx@(ident { pos := some _, .. } _ _ _) => some stx | node _ args => args.findSomeRev? getTailWithPos | _ => none partial def getTailInfo : Syntax → Option SourceInfo | atom info _ => info | ident info _ _ _ => info | node _ args => args.findSomeRev? getTailInfo | _ => none @[specialize] private partial def updateLast {α} [Inhabited α] (a : Array α) (f : α → Option α) : Nat → Option (Array α) | i => if i == 0 then none else let i := i - 1; let v := a.get! i; match f v with | some v => some $ a.set! i v | none => updateLast i partial def setTailInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some $ atom info val | ident _ rawVal val pre => some $ ident info rawVal val pre | node k args => match updateLast args setTailInfoAux args.size with | some args => some $ node k args | none => none | stx => none def setTailInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setTailInfoAux info stx with | some stx => stx | none => stx def unsetTrailing (stx : Syntax) : Syntax := match stx.getTailInfo with | none => stx | some info => stx.setTailInfo { info with trailing := none } @[specialize] private partial def updateFirst {α} [Inhabited α] (a : Array α) (f : α → Option α) : Nat → Option (Array α) | i => if h : i < a.size then let v := a.get ⟨i, h⟩; match f v with | some v => some $ a.set ⟨i, h⟩ v | none => updateFirst (i+1) else none partial def setHeadInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some $ atom info val | ident _ rawVal val pre => some $ ident info rawVal val pre | node k args => match updateFirst args setHeadInfoAux args.size with | some args => some $ node k args | noxne => none | stx => none def setHeadInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setHeadInfoAux info stx with | some stx => stx | none => stx def setInfo (info : SourceInfo) : Syntax → Syntax | atom _ val => atom info val | ident _ rawVal val pre => ident info rawVal val pre | stx => stx partial def replaceInfo (info : SourceInfo) : Syntax → Syntax | node k args => node k $ args.map replaceInfo | stx => setInfo info stx private def reprintLeaf (info : SourceInfo) (val : String) : String := -- no source info => add gracious amounts of whitespace to definitely separate tokens -- Note that the proper pretty printer does not use this function. -- The parser as well always produces source info, so round-tripping is still -- guaranteed. (Substring.toString <$> info.leading).getD " " ++ val ++ (Substring.toString <$> info.trailing).getD " " partial def reprint : Syntax → Option String | atom info val => reprintLeaf info val | ident info rawVal _ _ => reprintLeaf info rawVal.toString | node kind args => if kind == choiceKind then if args.size == 0 then failure else do s ← reprint (args.get! 0); args.foldlFromM (fun s stx => do s' ← reprint stx; guard (s == s'); pure s) s 1 else args.foldlM (fun r stx => do s ← reprint stx; pure $ r ++ s) "" | _ => "" open Lean.Format private def formatInfo (showPos : Bool) (info : Option SourceInfo) : Format := match info, showPos with | some info, true => ":" ++ toString info.pos | _, _ => "" partial def formatStxAux (maxDepth : Option Nat) (showPos : Bool) : Nat → Syntax → Format | _, atom info val => format (repr val) ++ formatInfo showPos info | _, ident info _ val pre => format "`" ++ format val ++ formatInfo showPos info | _, missing => "<missing>" | depth, node kind args => let depth := depth + 1; if kind == `Lean.Parser.noKind then sbracket $ if depth > maxDepth.getD depth then ".." else joinSep (args.toList.map (formatStxAux depth)) line else let shorterName := kind.replacePrefix `Lean.Parser Name.anonymous; let header := format shorterName; let body : List Format := if depth > maxDepth.getD depth then [".."] else args.toList.map (formatStxAux depth); paren $ joinSep (header :: body) line def formatStx (stx : Syntax) (maxDepth : Option Nat := none) (showPos := false) : Format := formatStxAux maxDepth showPos 0 stx instance : HasFormat (Syntax) := ⟨formatStx⟩ instance : HasToString (Syntax) := ⟨toString ∘ format⟩ end Syntax namespace SyntaxNode @[inline] def getIdAt (n : SyntaxNode) (i : Nat) : Name := (n.getArg i).getId end SyntaxNode /- Helper functions for creating Syntax objects using C++ -/ @[export lean_mk_syntax_atom] def mkSimpleAtom (val : String) : Syntax := Syntax.atom {} val @[export lean_mk_syntax_list] def mkListNode (args : Array Syntax) : Syntax := Syntax.node nullKind args def mkAtom (val : String) : Syntax := Syntax.atom {} val @[inline] def mkNode (k : SyntaxNodeKind) (args : Array Syntax) : Syntax := Syntax.node k args @[export lean_mk_syntax_str_lit] def mkStxStrLitAux (val : String) : Syntax := mkStxStrLit val @[export lean_mk_syntax_num_lit] def mkStxNumLitAux (val : Nat) : Syntax := mkStxNumLit (toString val) /-- Reflect a runtime datum back to surface syntax (best-effort). -/ class HasQuote (α : Type) := (quote : α → Syntax) export HasQuote (quote) instance Syntax.HasQuote : HasQuote Syntax := ⟨id⟩ instance String.HasQuote : HasQuote String := ⟨fun s => Syntax.node `Lean.Parser.Term.str #[mkStxStrLit s]⟩ instance Nat.HasQuote : HasQuote Nat := ⟨fun n => Syntax.node `Lean.Parser.Term.num #[mkStxNumLit $ toString n]⟩ instance Substring.HasQuote : HasQuote Substring := ⟨fun s => mkCAppStx `String.toSubstring #[quote s.toString]⟩ private def quoteName : Name → Syntax | Name.anonymous => mkCTermId `Lean.Name.anonymous | Name.str n s _ => mkCAppStx `Lean.mkNameStr #[quoteName n, quote s] | Name.num n i _ => mkCAppStx `Lean.mkNameNum #[quoteName n, quote i] instance Name.hasQuote : HasQuote Name := ⟨quoteName⟩ instance Prod.hasQuote {α β : Type} [HasQuote α] [HasQuote β] : HasQuote (α × β) := ⟨fun ⟨a, b⟩ => mkCAppStx `Prod.mk #[quote a, quote b]⟩ private def quoteList {α : Type} [HasQuote α] : List α → Syntax | [] => mkCTermId `List.nil | (x::xs) => mkCAppStx `List.cons #[quote x, quoteList xs] instance List.hasQuote {α : Type} [HasQuote α] : HasQuote (List α) := ⟨quoteList⟩ instance Array.hasQuote {α : Type} [HasQuote α] : HasQuote (Array α) := ⟨fun xs => mkCAppStx `List.toArray #[quote xs.toList]⟩ private def quoteOption {α : Type} [HasQuote α] : Option α → Syntax | none => mkTermId `Option.none | (some x) => mkCAppStx `Option.some #[quote x] instance Option.hasQuote {α : Type} [HasQuote α] : HasQuote (Option α) := ⟨quoteOption⟩ end Lean