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
29d9973098aa1b2f62114e3d3424e31179608b2b
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Oneshot/lean4-in/lakefile.lean
6e5443a93a85b55ef8fe0bfd9867ac6b45a3e993
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
434
lean
import Lake open Lake DSL package extra @[default_target] lean_lib Extra require Qq from ".." / ".." / "lake-packages" / "Qq" require std from ".." / ".." / "lake-packages" / "std" require aesop from ".." / ".." / "lake-packages" / "aesop" require proofwidgets from ".." / ".." / "lake-packages" / "proofwidgets" require Cli from ".." / ".." / "lake-packages" / "Cli" require mathlib from ".." / ".." / "lake-packages" / "mathlib"
580f095c213029ed680a8aa6aff32ef2babdb57a
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Init/Control/Foldable.lean
ca43ea69dff5dd030d9f1b4198121446bbcb443d
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
716
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 -/ prelude import Init.Core /- Typeclass for the polymorphic `foldlM` operation described in the paper. Remark: - `γ` is a "container" type of elements of type `α`. - `α` is treated as an output parameter by the typeclass resolution procedure. That is, it tries to find an instance using only `m` and `γ`. -/ class Foldable (m : Type u → Type v) (γ : Type w₁) (α : outParam (Type w₂)) where foldlM [Monad m] : (δ → α → m δ) → δ → γ → m δ -- Add the alias `foldlM` for `Foldable.foldlM` export Foldable (foldlM)
35082067d394d5e8946fc34dd561e3c64f377132
76c77df8a58af24dbf1d75c7012076a42244d728
/src/ch8.lean
2ae3224973f56754e560f47976d8733f3b9a5e04
[]
no_license
kris-brown/theorem_proving_in_lean
7a7a584ba2c657a35335dc895d49d991c997a0c9
774460c21bf857daff158210741bd88d1c8323cd
refs/heads/master
1,668,278,123,743
1,593,445,161,000
1,593,445,161,000
265,748,924
0
1
null
null
null
null
UTF-8
Lean
false
false
890
lean
-- Section 8.1: pattern matching namespace s81 open nat def sub1 : ℕ → ℕ | zero := zero | (succ x) := x def is_zero : ℕ → Prop | zero := true | (succ x) := false example : sub1 0 = 0 := rfl example (x : ℕ) : sub1 (succ x) = x := rfl example : is_zero 0 = true := rfl example (x : ℕ) : is_zero (succ x) = false := rfl example : sub1 7 = 6 := rfl example (x : ℕ) : ¬ is_zero (x + 3) := not_false end s81 -- Section 8.2: wildcards/overlapping patterns namespace s82 end s82 -- Section 8.3: structural recursion namespace s83 end s83 -- Section 8.4: Well-founded recursion namespace s84 end s84 -- Section 8.5: Mutual recursion namespace s85 end s85 -- Section 8.6: Dependent pattern matching namespace s86 end s86 -- Section 8.7: Inaccessible terms namespace s87 end s87 -- Section 8.8: Match expressions namespace s88 end s88 -- Exercises namespace s8x end s8x
39e52427ccbc6bae8da38f2f4d2aa3d875aa1447
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/basic.lean
a79e1aa7f925d3755102c21b384938d69306dd56
[ "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
71,974
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, Jeremy Avigad -/ import order.filter.ultrafilter import order.filter.partial import algebra.support /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. A filter `F` on `α` has `x` as a cluster point if `cluster_pt x F : 𝓝 x ⊓ F ≠ ⊥`. A map `f : ι → α` clusters at `x` along `F : filter ι` if `map_cluster_pt x F f : cluster_pt x (map f F)`. In particular the notion of cluster point of a sequence `u` is `map_cluster_pt x at_top u`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Notation * `𝓝 x`: the filter `nhds x` of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; * `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`; * `𝓝[≤] x`: the filter `nhds_within x (set.Iic x)` of left-neighborhoods of `x`; * `𝓝[≥] x`: the filter `nhds_within x (set.Ici x)` of right-neighborhoods of `x`; * `𝓝[<] x`: the filter `nhds_within x (set.Iio x)` of punctured left-neighborhoods of `x`; * `𝓝[>] x`: the filter `nhds_within x (set.Ioi x)` of punctured right-neighborhoods of `x`; * `𝓝[≠] x`: the filter `nhds_within x {x}ᶜ` of punctured neighborhoods of `x`. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ noncomputable theory open set filter classical open_locale classical filter universes u v w /-! ### Topological spaces -/ /-- A topology on `α`. -/ @[protect_proj] structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def topological_space.of_closed {α : Type u} (T : set (set α)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) : topological_space α := { is_open := λ X, Xᶜ ∈ T, is_open_univ := by simp [empty_mem], is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem sᶜ hs tᶜ ht, is_open_sUnion := λ s hs, by rw set.compl_sUnion; exact sInter_mem (set.compl '' s) (λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) } section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open.inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma topological_space_eq_iff {t t' : topological_space α} : t = t' ↔ ∀ s, @is_open α t s ↔ @is_open α t' s := ⟨λ h s, h ▸ iff.rfl, λ h, by { ext, exact h _ }⟩ lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open.union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open.inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open.inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open.and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open.inter /-- A set is closed if its complement is open -/ class is_closed (s : set α) : Prop := (is_open_compl : is_open sᶜ) @[simp] lemma is_open_compl_iff {s : set α} : is_open sᶜ ↔ is_closed s := ⟨λ h, ⟨h⟩, λ h, h.is_open_compl⟩ @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by { rw [← is_open_compl_iff, compl_empty], exact is_open_univ } @[simp] lemma is_closed_univ : is_closed (univ : set α) := by { rw [← is_open_compl_iff, compl_univ], exact is_open_empty } lemma is_closed.union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by { rw [← is_open_compl_iff] at *, rw compl_union, exact is_open.inter h₁ h₂ } lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simpa only [← is_open_compl_iff, compl_sInter, sUnion_image] using is_open_bUnion lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i lemma is_closed_bInter {s : set β} {f : β → set α} (h : ∀ i ∈ s, is_closed (f i)) : is_closed (⋂ i ∈ s, f i) := is_closed_Inter $ λ i, is_closed_Inter $ h i @[simp] lemma is_closed_compl_iff {s : set α} : is_closed sᶜ ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open.is_closed_compl {s : set α} (hs : is_open s) : is_closed sᶜ := is_closed_compl_iff.2 hs lemma is_open.sdiff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open.inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed.inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by { rw [← is_open_compl_iff] at *, rw compl_inter, exact is_open.union h₁ h₂ } lemma is_closed.sdiff {s t : set α} (h₁ : is_closed s) (h₂ : is_open t) : is_closed (s \ t) := is_closed.inter h₁ (is_closed_compl_iff.mpr h₂) lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed.union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = {x | p x}ᶜ ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed.union (is_closed_compl_iff.mpr hp) hq lemma is_closed.not : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-! ### Interior of a set -/ /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma is_open.interior_eq {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, is_open.interior_eq⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma subset_interior_iff {s t : set α} : t ⊆ interior s ↔ ∃ U, is_open U ∧ t ⊆ U ∧ U ⊆ s := ⟨λ h, ⟨interior s, is_open_interior, h, interior_subset⟩, λ ⟨U, hU, htU, hUs⟩, htU.trans (interior_maximal hUs hU)⟩ @[mono] lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := is_open_empty.interior_eq @[simp] lemma interior_univ : interior (univ : set α) = univ := is_open_univ.interior_eq @[simp] lemma interior_eq_univ {s : set α} : interior s = univ ↔ s = univ := ⟨λ h, univ_subset_iff.mp $ h.symm.trans_le interior_subset, λ h, h.symm ▸ interior_univ⟩ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := is_open_interior.interior_eq @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open.inter is_open_interior is_open_interior) @[simp] lemma finset.interior_Inter {ι : Type*} (s : finset ι) (f : ι → set α) : interior (⋂ i ∈ s, f i) = ⋂ i ∈ s, interior (f i) := begin classical, refine s.induction_on (by simp) _, intros i s h₁ h₂, simp [h₂], end @[simp] lemma interior_Inter_of_fintype {ι : Type*} [fintype ι] (f : ι → set α) : interior (⋂ i, f i) = ⋂ i, interior (f i) := by { convert finset.univ.interior_Inter f; simp, } lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open.sdiff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] lemma interior_Inter_subset (s : ι → set α) : interior (⋂ i, s i) ⊆ ⋂ i, interior (s i) := subset_Inter $ λ i, interior_mono $ Inter_subset _ _ lemma interior_Inter₂_subset (p : ι → Sort*) (s : Π i, p i → set α) : interior (⋂ i j, s i j) ⊆ ⋂ i j, interior (s i j) := (interior_Inter_subset _).trans $ Inter_mono $ λ i, interior_Inter_subset _ lemma interior_sInter_subset (S : set (set α)) : interior (⋂₀ S) ⊆ ⋂ s ∈ S, interior s := calc interior (⋂₀ S) = interior (⋂ s ∈ S, s) : by rw sInter_eq_bInter ... ⊆ ⋂ s ∈ S, interior s : interior_Inter₂_subset _ _ /-! ### Closure of a set -/ /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma not_mem_of_not_mem_closure {s : set α} {P : α} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma disjoint.closure_left {s t : set α} (hd : disjoint s t) (ht : is_open t) : disjoint (closure s) t := disjoint_compl_left.mono_left $ closure_minimal (disjoint_iff_subset_compl_right.1 hd) ht.is_closed_compl lemma disjoint.closure_right {s t : set α} (hd : disjoint s t) (hs : is_open s) : disjoint s (closure t) := (hd.symm.closure_left hs).symm lemma is_closed.closure_eq {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma is_closed.closure_subset {s : set α} (hs : is_closed s) : closure s ⊆ s := closure_minimal (subset.refl _) hs lemma is_closed.closure_subset_iff {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma is_closed.mem_iff_closure_subset {α : Type*} [topological_space α] {U : set α} (hU : is_closed U) {x : α} : x ∈ U ↔ closure ({x} : set α) ⊆ U := (hU.closure_subset_iff.trans set.singleton_subset_iff).symm @[mono] lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma diff_subset_closure_iff {s t : set α} : s \ t ⊆ closure t ↔ s ⊆ closure t := by rw [diff_subset_iff, union_eq_self_of_subset_left subset_closure] lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, is_closed.closure_eq⟩ lemma closure_subset_iff_is_closed {s : set α} : closure s ⊆ s ↔ is_closed s := ⟨is_closed_of_closure_subset, is_closed.closure_subset⟩ @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := is_closed_empty.closure_eq @[simp] lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ @[simp] lemma closure_nonempty_iff {s : set α} : (closure s).nonempty ↔ s.nonempty := by simp only [← ne_empty_iff_nonempty, ne.def, closure_empty_iff] alias closure_nonempty_iff ↔ set.nonempty.of_closure set.nonempty.closure @[simp] lemma closure_univ : closure (univ : set α) = univ := is_closed_univ.closure_eq @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := is_closed_closure.closure_eq @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed.union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) @[simp] lemma finset.closure_bUnion {ι : Type*} (s : finset ι) (f : ι → set α) : closure (⋃ i ∈ s, f i) = ⋃ i ∈ s, closure (f i) := begin classical, refine s.induction_on (by simp) _, intros i s h₁ h₂, simp [h₂], end @[simp] lemma closure_Union_of_fintype {ι : Type*} [fintype ι] (f : ι → set α) : closure (⋃ i, f i) = ⋃ i, closure (f i) := by { convert finset.univ.closure_bUnion f; simp, } lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = (interior sᶜ)ᶜ := begin rw [interior, closure, compl_sUnion, compl_image_set_of], simp only [compl_subset_compl, is_open_compl_iff], end @[simp] lemma interior_compl {s : set α} : interior sᶜ = (closure s)ᶜ := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure sᶜ = (interior s)ᶜ := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ oᶜ, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁.is_open_compl nc) in hc (h₂ hs)⟩ /-- A set is dense in a topological space if every point belongs to its closure. -/ def dense (s : set α) : Prop := ∀ x, x ∈ closure s lemma dense_iff_closure_eq {s : set α} : dense s ↔ closure s = univ := eq_univ_iff_forall.symm lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ := dense_iff_closure_eq.mp h lemma interior_eq_empty_iff_dense_compl {s : set α} : interior s = ∅ ↔ dense sᶜ := by rw [dense_iff_closure_eq, closure_compl, compl_univ_iff] lemma dense.interior_compl {s : set α} (h : dense s) : interior sᶜ = ∅ := interior_eq_empty_iff_dense_compl.2 $ by rwa compl_compl /-- The closure of a set `s` is dense if and only if `s` is dense. -/ @[simp] lemma dense_closure {s : set α} : dense (closure s) ↔ dense s := by rw [dense, dense, closure_closure] alias dense_closure ↔ dense.of_closure dense.closure @[simp] lemma dense_univ : dense (univ : set α) := λ x, subset_closure trivial /-- A set is dense if and only if it has a nonempty intersection with each nonempty open set. -/ lemma dense_iff_inter_open {s : set α} : dense s ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h.closure_eq]) U U_op x_in }, { intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end alias dense_iff_inter_open ↔ dense.inter_open_nonempty _ lemma dense.exists_mem_open {s : set α} (hs : dense s) {U : set α} (ho : is_open U) (hne : U.nonempty) : ∃ x ∈ s, x ∈ U := let ⟨x, hx⟩ := hs.inter_open_nonempty U ho hne in ⟨x, hx.2, hx.1⟩ lemma dense.nonempty_iff {s : set α} (hs : dense s) : s.nonempty ↔ nonempty α := ⟨λ ⟨x, hx⟩, ⟨x⟩, λ ⟨x⟩, let ⟨y, hy⟩ := hs.inter_open_nonempty _ is_open_univ ⟨x, trivial⟩ in ⟨y, hy.2⟩⟩ lemma dense.nonempty [h : nonempty α] {s : set α} (hs : dense s) : s.nonempty := hs.nonempty_iff.2 h @[mono] lemma dense.mono {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : dense s₁) : dense s₂ := λ x, closure_mono h (hd x) /-- Complement to a singleton is dense if and only if the singleton is not an open set. -/ lemma dense_compl_singleton_iff_not_open {x : α} : dense ({x}ᶜ : set α) ↔ ¬is_open ({x} : set α) := begin fsplit, { intros hd ho, exact (hd.inter_open_nonempty _ ho (singleton_nonempty _)).ne_empty (inter_compl_self _) }, { refine λ ho, dense_iff_inter_open.2 (λ U hU hne, inter_compl_nonempty_iff.2 $ λ hUx, _), obtain rfl : U = {x}, from eq_singleton_iff_nonempty_unique_mem.2 ⟨hne, hUx⟩, exact ho hU } end /-! ### Frontier of a set -/ /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure sᶜ := by rw [closure_compl, frontier, diff_eq] lemma frontier_subset_closure {s : set α} : frontier s ⊆ closure s := diff_subset _ _ lemma is_closed.frontier_subset (hs : is_closed s) : frontier s ⊆ s := frontier_subset_closure.trans hs.closure_eq.subset lemma frontier_closure_subset {s : set α} : frontier (closure s) ⊆ frontier s := diff_subset_diff closure_closure.subset $ interior_mono subset_closure lemma frontier_interior_subset {s : set α} : frontier (interior s) ⊆ frontier s := diff_subset_diff (closure_mono interior_subset) interior_interior.symm.subset /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier sᶜ = frontier s := by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm] @[simp] lemma frontier_univ : frontier (univ : set α) = ∅ := by simp [frontier] @[simp] lemma frontier_empty : frontier (∅ : set α) = ∅ := by simp [frontier] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure tᶜ) ∪ (closure sᶜ ∩ frontier t) := by simpa only [frontier_compl, ← compl_union] using frontier_inter_subset sᶜ tᶜ lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, hs.closure_eq] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, hs.interior_eq] lemma is_open.inter_frontier_eq {s : set α} (hs : is_open s) : s ∩ frontier s = ∅ := by rw [hs.frontier_eq, inter_diff_self] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed.inter is_closed_closure is_closed_closure /-- The frontier of a closed set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end lemma closure_eq_interior_union_frontier (s : set α) : closure s = interior s ∪ frontier s := (union_diff_cancel interior_subset_closure).symm lemma closure_eq_self_union_frontier (s : set α) : closure s = s ∪ frontier s := (union_diff_cancel' interior_subset subset_closure).symm lemma is_open.inter_frontier_eq_empty_of_disjoint {s t : set α} (ht : is_open t) (hd : disjoint s t) : t ∩ frontier s = ∅ := begin rw [inter_comm, ← subset_compl_iff_disjoint], exact subset.trans frontier_subset_closure (closure_minimal (λ _, disjoint_left.1 hd) (is_closed_compl_iff.2 ht)) end lemma frontier_eq_inter_compl_interior {s : set α} : frontier s = (interior s)ᶜ ∩ (interior (sᶜ))ᶜ := by { rw [←frontier_compl, ←closure_compl], refl } lemma compl_frontier_eq_union_interior {s : set α} : (frontier s)ᶜ = interior s ∪ interior sᶜ := begin rw frontier_eq_inter_compl_interior, simp only [compl_inter, compl_compl], end /-! ### Neighborhoods -/ /-- A set is called a neighborhood of `a` if it contains an open set around `a`. The set of all neighborhoods of `a` forms a filter, the neighborhood filter at `a`, is here defined as the infimum over the principal filters of all open sets containing `a`. -/ @[irreducible] def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) localized "notation `𝓝` := nhds" in topological_space /-- The "neighborhood within" filter. Elements of `𝓝[s] a` are sets containing the intersection of `s` and a neighborhood of `a`. -/ def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ 𝓟 s localized "notation `𝓝[` s `] ` x:100 := nhds_within x s" in topological_space localized "notation `𝓝[≠] ` x:100 := nhds_within x {x}ᶜ" in topological_space localized "notation `𝓝[≥] ` x:100 := nhds_within x (set.Ici x)" in topological_space localized "notation `𝓝[≤] ` x:100 := nhds_within x (set.Iic x)" in topological_space localized "notation `𝓝[>] ` x:100 := nhds_within x (set.Ioi x)" in topological_space localized "notation `𝓝[<] ` x:100 := nhds_within x (set.Iio x)" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := by rw nhds /-- The open sets containing `a` are a basis for the neighborhood filter. See `nhds_basis_opens'` for a variant using open neighborhoods instead. -/ lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) := begin rw nhds_def, exact has_basis_binfi_principal (λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open.inter hs ht⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩) ⟨univ, ⟨mem_univ a, is_open_univ⟩⟩ end /-- A filter lies below the neighborhood filter at `a` iff it contains every open set around `a`. -/ lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] /-- To show a filter is above the neighborhood filter at `a`, it suffices to show that it is above the principal filter of some open set `s` containing `a`. -/ lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : 𝓟 s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma mem_nhds_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃ t ⊆ s, is_open t ∧ a ∈ t := (nhds_basis_opens a).mem_iff.trans ⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩ /-- A predicate is true in a neighborhood of `a` iff it is true for all the points in an open set containing `a`. -/ lemma eventually_nhds_iff {a : α} {p : α → Prop} : (∀ᶠ x in 𝓝 a, p x) ↔ ∃ (t : set α), (∀ x ∈ t, p x) ∧ is_open t ∧ a ∈ t := mem_nhds_iff.trans $ by simp only [subset_def, exists_prop, mem_set_of_eq] lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 (image f s)) := ((nhds_basis_opens a).map f).eq_binfi lemma mem_of_mem_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_iff.1 H in ht hs /-- If a predicate is true in a neighborhood of `a`, then it is true for `a`. -/ lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : p a := mem_of_mem_nhds h lemma is_open.mem_nhds {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_iff.2 ⟨s, subset.refl _, hs, ha⟩ lemma is_open.mem_nhds_iff {a : α} {s : set α} (hs : is_open s) : s ∈ 𝓝 a ↔ a ∈ s := ⟨mem_of_mem_nhds, λ ha, mem_nhds_iff.2 ⟨s, subset.refl _, hs, ha⟩⟩ lemma is_closed.compl_mem_nhds {a : α} {s : set α} (hs : is_closed s) (ha : a ∉ s) : sᶜ ∈ 𝓝 a := hs.is_open_compl.mem_nhds (mem_compl ha) lemma is_open.eventually_mem {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : ∀ᶠ x in 𝓝 a, x ∈ s := is_open.mem_nhds hs ha /-- The open neighborhoods of `a` are a basis for the neighborhood filter. See `nhds_basis_opens` for a variant using open sets around `a` instead. -/ lemma nhds_basis_opens' (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_open s) (λ x, x) := begin convert nhds_basis_opens a, ext s, split, { rintros ⟨s_in, s_op⟩, exact ⟨mem_of_mem_nhds s_in, s_op⟩ }, { rintros ⟨a_in, s_op⟩, exact ⟨is_open.mem_nhds s_op a_in, s_op⟩ }, end /-- If `U` is a neighborhood of each point of a set `s` then it is a neighborhood of `s`: it contains an open set containing `s`. -/ lemma exists_open_set_nhds {s U : set α} (h : ∀ x ∈ s, U ∈ 𝓝 x) : ∃ V : set α, s ⊆ V ∧ is_open V ∧ V ⊆ U := begin have := λ x hx, (nhds_basis_opens x).mem_iff.1 (h x hx), choose! Z hZ hZ' using this, refine ⟨⋃ x ∈ s, Z x, λ x hx, mem_bUnion hx (hZ x hx).1, is_open_Union _, Union₂_subset hZ'⟩, intro x, by_cases hx : x ∈ s ; simp [hx], exact (hZ x hx).2, end /-- If `U` is a neighborhood of each point of a set `s` then it is a neighborhood of s: it contains an open set containing `s`. -/ lemma exists_open_set_nhds' {s U : set α} (h : U ∈ ⨆ x ∈ s, 𝓝 x) : ∃ V : set α, s ⊆ V ∧ is_open V ∧ V ⊆ U := exists_open_set_nhds (by simpa using h) /-- If a predicate is true in a neighbourhood of `a`, then for `y` sufficiently close to `a` this predicate is true in a neighbourhood of `y`. -/ lemma filter.eventually.eventually_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : ∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x := let ⟨t, htp, hto, ha⟩ := eventually_nhds_iff.1 h in eventually_nhds_iff.2 ⟨t, λ x hx, eventually_nhds_iff.2 ⟨t, htp, hto, hx⟩, hto, ha⟩ @[simp] lemma eventually_eventually_nhds {p : α → Prop} {a : α} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x) ↔ ∀ᶠ x in 𝓝 a, p x := ⟨λ h, h.self_of_nhds, λ h, h.eventually_nhds⟩ @[simp] lemma eventually_mem_nhds {s : set α} {a : α} : (∀ᶠ x in 𝓝 a, s ∈ 𝓝 x) ↔ s ∈ 𝓝 a := eventually_eventually_nhds @[simp] lemma nhds_bind_nhds : (𝓝 a).bind 𝓝 = 𝓝 a := filter.ext $ λ s, eventually_eventually_nhds @[simp] lemma eventually_eventually_eq_nhds {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g) ↔ f =ᶠ[𝓝 a] g := eventually_eventually_nhds lemma filter.eventually_eq.eq_of_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : f a = g a := h.self_of_nhds @[simp] lemma eventually_eventually_le_nhds [has_le β] {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g) ↔ f ≤ᶠ[𝓝 a] g := eventually_eventually_nhds /-- If two functions are equal in a neighbourhood of `a`, then for `y` sufficiently close to `a` these functions are equal in a neighbourhood of `y`. -/ lemma filter.eventually_eq.eventually_eq_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g := h.eventually_nhds /-- If `f x ≤ g x` in a neighbourhood of `a`, then for `y` sufficiently close to `a` we have `f x ≤ g x` in a neighbourhood of `y`. -/ lemma filter.eventually_le.eventually_le_nhds [has_le β] {f g : α → β} {a : α} (h : f ≤ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g := h.eventually_nhds theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := ((nhds_basis_opens x).forall_iff hP).trans $ by simp only [and_comm (x ∈ _), and_imp] theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem' $ assume _, ha lemma tendsto_at_top_of_eventually_const {ι : Type*} [semilattice_sup ι] [nonempty ι] {x : α} {u : ι → α} {i₀ : ι} (h : ∀ i ≥ i₀, u i = x) : tendsto u at_top (𝓝 x) := tendsto.congr' (eventually_eq.symm (eventually_at_top.mpr ⟨i₀, h⟩)) tendsto_const_nhds lemma tendsto_at_bot_of_eventually_const {ι : Type*} [semilattice_inf ι] [nonempty ι] {x : α} {u : ι → α} {i₀ : ι} (h : ∀ i ≤ i₀, u i = x) : tendsto u at_bot (𝓝 x) := tendsto.congr' (eventually_eq.symm (eventually_at_bot.mpr ⟨i₀, h⟩)) tendsto_const_nhds lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure.2 $ mem_of_mem_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := (tendsto_pure_pure f a).mono_right (pure_le_nhds _) lemma order_top.tendsto_at_top_nhds {α : Type*} [partial_order α] [order_top α] [topological_space β] (f : α → β) : tendsto f at_top (𝓝 $ f ⊤) := (tendsto_at_top_pure f).mono_right (pure_le_nhds _) @[simp] instance nhds_ne_bot {a : α} : ne_bot (𝓝 a) := ne_bot_of_le (pure_le_nhds a) /-! ### Cluster points In this section we define [cluster points](https://en.wikipedia.org/wiki/Limit_point) (also known as limit points and accumulation points) of a filter and of a sequence. -/ /-- A point `x` is a cluster point of a filter `F` if 𝓝 x ⊓ F ≠ ⊥. Also known as an accumulation point or a limit point. -/ def cluster_pt (x : α) (F : filter α) : Prop := ne_bot (𝓝 x ⊓ F) lemma cluster_pt.ne_bot {x : α} {F : filter α} (h : cluster_pt x F) : ne_bot (𝓝 x ⊓ F) := h lemma filter.has_basis.cluster_pt_iff {ιa ιF} {pa : ιa → Prop} {sa : ιa → set α} {pF : ιF → Prop} {sF : ιF → set α} {F : filter α} (ha : (𝓝 a).has_basis pa sa) (hF : F.has_basis pF sF) : cluster_pt a F ↔ ∀ ⦃i⦄ (hi : pa i) ⦃j⦄ (hj : pF j), (sa i ∩ sF j).nonempty := ha.inf_basis_ne_bot_iff hF lemma cluster_pt_iff {x : α} {F : filter α} : cluster_pt x F ↔ ∀ ⦃U : set α⦄ (hU : U ∈ 𝓝 x) ⦃V⦄ (hV : V ∈ F), (U ∩ V).nonempty := inf_ne_bot_iff /-- `x` is a cluster point of a set `s` if every neighbourhood of `x` meets `s` on a nonempty set. -/ lemma cluster_pt_principal_iff {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∀ U ∈ 𝓝 x, (U ∩ s).nonempty := inf_principal_ne_bot_iff lemma cluster_pt_principal_iff_frequently {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∃ᶠ y in 𝓝 x, y ∈ s := by simp only [cluster_pt_principal_iff, frequently_iff, set.nonempty, exists_prop, mem_inter_iff] lemma cluster_pt.of_le_nhds {x : α} {f : filter α} (H : f ≤ 𝓝 x) [ne_bot f] : cluster_pt x f := by rwa [cluster_pt, inf_eq_right.mpr H] lemma cluster_pt.of_le_nhds' {x : α} {f : filter α} (H : f ≤ 𝓝 x) (hf : ne_bot f) : cluster_pt x f := cluster_pt.of_le_nhds H lemma cluster_pt.of_nhds_le {x : α} {f : filter α} (H : 𝓝 x ≤ f) : cluster_pt x f := by simp only [cluster_pt, inf_eq_left.mpr H, nhds_ne_bot] lemma cluster_pt.mono {x : α} {f g : filter α} (H : cluster_pt x f) (h : f ≤ g) : cluster_pt x g := ⟨ne_bot_of_le_ne_bot H.ne $ inf_le_inf_left _ h⟩ lemma cluster_pt.of_inf_left {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x f := H.mono inf_le_left lemma cluster_pt.of_inf_right {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x g := H.mono inf_le_right lemma ultrafilter.cluster_pt_iff {x : α} {f : ultrafilter α} : cluster_pt x f ↔ ↑f ≤ 𝓝 x := ⟨f.le_of_inf_ne_bot', λ h, cluster_pt.of_le_nhds h⟩ /-- A point `x` is a cluster point of a sequence `u` along a filter `F` if it is a cluster point of `map u F`. -/ def map_cluster_pt {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : Prop := cluster_pt x (map u F) lemma map_cluster_pt_iff {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : map_cluster_pt x F u ↔ ∀ s ∈ 𝓝 x, ∃ᶠ a in F, u a ∈ s := by { simp_rw [map_cluster_pt, cluster_pt, inf_ne_bot_iff_frequently_left, frequently_map], refl } lemma map_cluster_pt_of_comp {ι δ :Type*} {F : filter ι} {φ : δ → ι} {p : filter δ} {x : α} {u : ι → α} [ne_bot p] (h : tendsto φ p F) (H : tendsto (u ∘ φ) p (𝓝 x)) : map_cluster_pt x F u := begin have := calc map (u ∘ φ) p = map u (map φ p) : map_map ... ≤ map u F : map_mono h, have : map (u ∘ φ) p ≤ 𝓝 x ⊓ map u F, from le_inf H this, exact ne_bot_of_le this end /-! ### Interior, closure and frontier in terms of neighborhoods -/ lemma interior_eq_nhds' {s : set α} : interior s = {a | s ∈ 𝓝 a} := set.ext $ λ x, by simp only [mem_interior, mem_nhds_iff, mem_set_of_eq] lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ 𝓟 s} := interior_eq_nhds'.trans $ by simp only [le_principal_iff] lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by rw [interior_eq_nhds', mem_set_of_eq] @[simp] lemma interior_mem_nhds {s : set α} {a : α} : interior s ∈ 𝓝 a ↔ s ∈ 𝓝 a := ⟨λ h, mem_of_superset h interior_subset, λ h, is_open.mem_nhds is_open_interior (mem_interior_iff_mem_nhds.2 h)⟩ lemma interior_set_of_eq {p : α → Prop} : interior {x | p x} = {x | ∀ᶠ y in 𝓝 x, p y} := interior_eq_nhds' lemma is_open_set_of_eventually_nhds {p : α → Prop} : is_open {x | ∀ᶠ y in 𝓝 x, p y} := by simp only [← interior_set_of_eq, is_open_interior] lemma subset_interior_iff_nhds {s V : set α} : s ⊆ interior V ↔ ∀ x ∈ s, V ∈ 𝓝 x := show (∀ x, x ∈ s → x ∈ _) ↔ _, by simp_rw mem_interior_iff_mem_nhds lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ 𝓟 s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ 𝓟 s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff theorem is_open_iff_ultrafilter {s : set α} : is_open s ↔ (∀ (x ∈ s) (l : ultrafilter α), ↑l ≤ 𝓝 x → s ∈ l) := by simp_rw [is_open_iff_mem_nhds, ← mem_iff_ultrafilter] lemma is_open_singleton_iff_nhds_eq_pure {α : Type*} [topological_space α] (a : α) : is_open ({a} : set α) ↔ 𝓝 a = pure a := begin split, { intros h, apply le_antisymm _ (pure_le_nhds a), rw le_pure_iff, exact h.mem_nhds (mem_singleton a) }, { intros h, simp [is_open_iff_nhds, h] } end lemma mem_closure_iff_frequently {s : set α} {a : α} : a ∈ closure s ↔ ∃ᶠ x in 𝓝 a, x ∈ s := by rw [filter.frequently, filter.eventually, ← mem_interior_iff_mem_nhds, closure_eq_compl_interior_compl]; refl alias mem_closure_iff_frequently ↔ _ filter.frequently.mem_closure /-- The set of cluster points of a filter is closed. In particular, the set of limit points of a sequence is closed. -/ lemma is_closed_set_of_cluster_pt {f : filter α} : is_closed {x | cluster_pt x f} := begin simp only [cluster_pt, inf_ne_bot_iff_frequently_left, set_of_forall, imp_iff_not_or], refine is_closed_Inter (λ p, is_closed.union _ _); apply is_closed_compl_iff.2, exacts [is_open_set_of_eventually_nhds, is_open_const] end theorem mem_closure_iff_cluster_pt {s : set α} {a : α} : a ∈ closure s ↔ cluster_pt a (𝓟 s) := mem_closure_iff_frequently.trans cluster_pt_principal_iff_frequently.symm lemma mem_closure_iff_nhds_ne_bot {s : set α} : a ∈ closure s ↔ 𝓝 a ⊓ 𝓟 s ≠ ⊥ := mem_closure_iff_cluster_pt.trans ne_bot_iff lemma mem_closure_iff_nhds_within_ne_bot {s : set α} {x : α} : x ∈ closure s ↔ ne_bot (𝓝[s] x) := mem_closure_iff_cluster_pt /-- If `x` is not an isolated point of a topological space, then `{x}ᶜ` is dense in the whole space. -/ lemma dense_compl_singleton (x : α) [ne_bot (𝓝[≠] x)] : dense ({x}ᶜ : set α) := begin intro y, unfreezingI { rcases eq_or_ne y x with rfl|hne }, { rwa mem_closure_iff_nhds_within_ne_bot }, { exact subset_closure hne } end /-- If `x` is not an isolated point of a topological space, then the closure of `{x}ᶜ` is the whole space. -/ @[simp] lemma closure_compl_singleton (x : α) [ne_bot (𝓝[≠] x)] : closure {x}ᶜ = (univ : set α) := (dense_compl_singleton x).closure_eq /-- If `x` is not an isolated point of a topological space, then the interior of `{x}` is empty. -/ @[simp] lemma interior_singleton (x : α) [ne_bot (𝓝[≠] x)] : interior {x} = (∅ : set α) := interior_eq_empty_iff_dense_compl.2 (dense_compl_singleton x) lemma closure_eq_cluster_pts {s : set α} : closure s = {a | cluster_pt a (𝓟 s)} := set.ext $ λ x, mem_closure_iff_cluster_pt theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff_cluster_pt.trans cluster_pt_principal_iff theorem mem_closure_iff_nhds' {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, ∃ y : s, ↑y ∈ t := by simp only [mem_closure_iff_nhds, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_comap_ne_bot {A : set α} {x : α} : x ∈ closure A ↔ ne_bot (comap (coe : A → α) (𝓝 x)) := by simp_rw [mem_closure_iff_nhds, comap_ne_bot_iff, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_nhds_basis' {a : α} {p : ι → Prop} {s : ι → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → (s i ∩ t).nonempty := mem_closure_iff_cluster_pt.trans $ (h.cluster_pt_iff (has_basis_principal _)).trans $ by simp only [exists_prop, forall_const] theorem mem_closure_iff_nhds_basis {a : α} {p : ι → Prop} {s : ι → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i := (mem_closure_iff_nhds_basis' h).trans $ by simp only [set.nonempty, mem_inter_eq, exists_prop, and_comm] /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u ∧ ↑u ≤ 𝓝 x := by simp [closure_eq_cluster_pts, cluster_pt, ← exists_ultrafilter_iff, and.comm] lemma is_closed_iff_cluster_pt {s : set α} : is_closed s ↔ ∀a, cluster_pt a (𝓟 s) → a ∈ s := calc is_closed s ↔ closure s ⊆ s : closure_subset_iff_is_closed.symm ... ↔ (∀a, cluster_pt a (𝓟 s) → a ∈ s) : by simp only [subset_def, mem_closure_iff_cluster_pt] lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀ x, (∀ U ∈ 𝓝 x, (U ∩ s).nonempty) → x ∈ s := by simp_rw [is_closed_iff_cluster_pt, cluster_pt, inf_principal_ne_bot_iff] lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := begin rintro a ⟨hs, ht⟩, have : s ∈ 𝓝 a := is_open.mem_nhds h hs, rw mem_closure_iff_nhds_ne_bot at ht ⊢, rwa [← inf_principal, ← inf_assoc, inf_eq_left.2 (le_principal_iff.2 this)], end lemma closure_inter_open' {s t : set α} (h : is_open t) : closure s ∩ t ⊆ closure (s ∩ t) := by simpa only [inter_comm] using closure_inter_open h lemma dense.open_subset_closure_inter {s t : set α} (hs : dense s) (ht : is_open t) : t ⊆ closure (t ∩ s) := calc t = t ∩ closure s : by rw [hs.closure_eq, inter_univ] ... ⊆ closure (t ∩ s) : closure_inter_open ht lemma mem_closure_of_mem_closure_union {s₁ s₂ : set α} {x : α} (h : x ∈ closure (s₁ ∪ s₂)) (h₁ : s₁ᶜ ∈ 𝓝 x) : x ∈ closure s₂ := begin rw mem_closure_iff_nhds_ne_bot at *, rwa ← calc 𝓝 x ⊓ principal (s₁ ∪ s₂) = 𝓝 x ⊓ (principal s₁ ⊔ principal s₂) : by rw sup_principal ... = (𝓝 x ⊓ principal s₁) ⊔ (𝓝 x ⊓ principal s₂) : inf_sup_left ... = ⊥ ⊔ 𝓝 x ⊓ principal s₂ : by rw inf_principal_eq_bot.mpr h₁ ... = 𝓝 x ⊓ principal s₂ : bot_sup_eq end /-- The intersection of an open dense set with a dense set is a dense set. -/ lemma dense.inter_of_open_left {s t : set α} (hs : dense s) (ht : dense t) (hso : is_open s) : dense (s ∩ t) := λ x, (closure_minimal (closure_inter_open hso) is_closed_closure) $ by simp [hs.closure_eq, ht.closure_eq] /-- The intersection of a dense set with an open dense set is a dense set. -/ lemma dense.inter_of_open_right {s t : set α} (hs : dense s) (ht : dense t) (hto : is_open t) : dense (s ∩ t) := inter_comm t s ▸ ht.inter_of_open_left hs hto lemma dense.inter_nhds_nonempty {s t : set α} (hs : dense s) {x : α} (ht : t ∈ 𝓝 x) : (s ∩ t).nonempty := let ⟨U, hsub, ho, hx⟩ := mem_nhds_iff.1 ht in (hs.inter_open_nonempty U ho ⟨x, hx⟩).mono $ λ y hy, ⟨hy.2, hsub hy.1⟩ lemma closure_diff {s t : set α} : closure s \ closure t ⊆ closure (s \ t) := calc closure s \ closure t = (closure t)ᶜ ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure ((closure t)ᶜ ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma filter.frequently.mem_of_closed {a : α} {s : set α} (h : ∃ᶠ x in 𝓝 a, x ∈ s) (hs : is_closed s) : a ∈ s := hs.closure_subset h.mem_closure lemma is_closed.mem_of_frequently_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hs : is_closed s) (h : ∃ᶠ x in b, f x ∈ s) (hf : tendsto f b (𝓝 a)) : a ∈ s := (hf.frequently $ show ∃ᶠ x in b, (λ y, y ∈ s) (f x), from h).mem_of_closed hs lemma is_closed.mem_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hs : is_closed s) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ s := hs.mem_of_frequently_of_tendsto h.frequently hf lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s := is_closed_closure.mem_of_tendsto hf $ h.mono (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only if it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ 𝓟 s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : 𝓟 sᶜ ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_mem_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end /-! ### Limits of filters in topological spaces -/ section lim /-- If `f` is a filter, then `Lim f` is a limit of the filter, if it exists. -/ noncomputable def Lim [nonempty α] (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a /-- If `f` is a filter satisfying `ne_bot f`, then `Lim' f` is a limit of the filter, if it exists. -/ def Lim' (f : filter α) [ne_bot f] : α := @Lim _ _ (nonempty_of_ne_bot f) f /-- If `F` is an ultrafilter, then `filter.ultrafilter.Lim F` is a limit of the filter, if it exists. Note that dot notation `F.Lim` can be used for `F : ultrafilter α`. -/ def ultrafilter.Lim : ultrafilter α → α := λ F, Lim' F /-- If `f` is a filter in `β` and `g : β → α` is a function, then `lim f` is a limit of `g` at `f`, if it exists. -/ noncomputable def lim [nonempty α] (f : filter β) (g : β → α) : α := Lim (f.map g) /-- If a filter `f` is majorated by some `𝓝 a`, then it is majorated by `𝓝 (Lim f)`. We formulate this lemma with a `[nonempty α]` argument of `Lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma le_nhds_Lim {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (@Lim _ _ (nonempty_of_exists h) f) := epsilon_spec h /-- If `g` tends to some `𝓝 a` along `f`, then it tends to `𝓝 (lim f g)`. We formulate this lemma with a `[nonempty α]` argument of `lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma tendsto_nhds_lim {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) : tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) := le_nhds_Lim h end lim /-! ### Locally finite families -/ /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite.point_finite {f : β → set α} (hf : locally_finite f) (x : α) : finite {b | x ∈ f b} := let ⟨t, hxt, ht⟩ := hf x in ht.subset $ λ b hb, ⟨x, hb, mem_of_mem_nhds hxt⟩ lemma locally_finite_of_fintype [fintype β] (f : β → set α) : locally_finite f := assume x, ⟨univ, univ_mem, finite.of_fintype _⟩ lemma locally_finite.subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma locally_finite.comp_injective {ι} {f : β → set α} {g : ι → β} (hf : locally_finite f) (hg : function.injective g) : locally_finite (f ∘ g) := λ x, let ⟨t, htx, htf⟩ := hf x in ⟨t, htx, htf.preimage (hg.inj_on _)⟩ lemma locally_finite.closure {f : β → set α} (hf : locally_finite f) : locally_finite (λ i, closure (f i)) := begin intro x, rcases hf x with ⟨s, hsx, hsf⟩, refine ⟨interior s, interior_mem_nhds.2 hsx, hsf.subset $ λ i hi, _⟩, exact (hi.mono (closure_inter_open' is_open_interior)).of_closure.mono (inter_subset_inter_right _ interior_subset) end lemma locally_finite.is_closed_Union {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := begin simp only [← is_open_compl_iff, compl_Union, is_open_iff_mem_nhds, mem_Inter], intros a ha, replace ha : ∀ i, (f i)ᶜ ∈ 𝓝 a := λ i, (h₂ i).is_open_compl.mem_nhds (ha i), rcases h₁ a with ⟨t, h_nhds, h_fin⟩, have : t ∩ (⋂ i ∈ {i | (f i ∩ t).nonempty}, (f i)ᶜ) ∈ 𝓝 a, from inter_mem h_nhds ((bInter_mem h_fin).2 (λ i _, ha i)), filter_upwards [this], simp only [mem_inter_eq, mem_Inter], rintros b ⟨hbt, hn⟩ i hfb, exact hn i ⟨b, hfb, hbt⟩ hfb, end lemma locally_finite.closure_Union {f : β → set α} (h : locally_finite f) : closure (⋃ i, f i) = ⋃ i, closure (f i) := subset.antisymm (closure_minimal (Union_mono $ λ _, subset_closure) $ h.closure.is_closed_Union $ λ _, is_closed_closure) (Union_subset $ λ i, closure_mono $ subset_Union _ _) end locally_finite end topological_space /-! ### Continuity -/ section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. Registered as a structure to make sure it is not unfolded by Lean. -/ structure continuous (f : α → β) : Prop := (is_open_preimage : ∀s, is_open s → is_open (f ⁻¹' s)) lemma continuous_def {f : α → β} : continuous f ↔ (∀s, is_open s → is_open (f ⁻¹' s)) := ⟨λ hf s hs, hf.is_open_preimage s hs, λ h, ⟨h⟩⟩ lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) : is_open (f ⁻¹' s) := hf.is_open_preimage s h lemma continuous.congr {f g : α → β} (h : continuous f) (h' : ∀ x, f x = g x) : continuous g := by { convert h, ext, rw h' } /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.tendsto {f : α → β} {x : α} (h : continuous_at f x) : tendsto f (𝓝 x) (𝓝 (f x)) := h lemma continuous_at_def {f : α → β} {x : α} : continuous_at f x ↔ ∀ A ∈ 𝓝 (f x), f ⁻¹' A ∈ 𝓝 x := iff.rfl lemma continuous_at_congr {f g : α → β} {x : α} (h : f =ᶠ[𝓝 x] g) : continuous_at f x ↔ continuous_at g x := by simp only [continuous_at, tendsto_congr' h, h.eq_of_nhds] lemma continuous_at.congr {f g : α → β} {x : α} (hf : continuous_at f x) (h : f =ᶠ[𝓝 x] g) : continuous_at g x := (continuous_at_congr h).1 hf lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma eventually_eq_zero_nhds {M₀} [has_zero M₀] {a : α} {f : α → M₀} : f =ᶠ[𝓝 a] 0 ↔ a ∉ closure (function.support f) := by rw [← mem_compl_eq, ← interior_compl, mem_interior_iff_mem_nhds, function.compl_support]; refl lemma cluster_pt.map {x : α} {la : filter α} {lb : filter β} (H : cluster_pt x la) {f : α → β} (hfc : continuous_at f x) (hf : tendsto f la lb) : cluster_pt (f x) lb := ⟨ne_bot_of_le_ne_bot ((map_ne_bot_iff f).2 H).ne $ hfc.tendsto.inf hf⟩ /-- See also `interior_preimage_subset_preimage_interior`. -/ lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β} (hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) := interior_maximal (preimage_mono interior_subset) (is_open_interior.preimage hf) lemma continuous_id : continuous (id : α → α) := continuous_def.2 $ assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := continuous_def.2 $ assume s h, (h.preimage hg).preimage hf lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) := nat.rec_on n continuous_id (λ n ihn, ihn.comp h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) := ((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $ λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, ht.preimage hf⟩, subset.refl _⟩ /-- A version of `continuous.tendsto` that allows one to specify a simpler form of the limit. E.g., one can write `continuous_exp.tendsto' 0 1 exp_zero`. -/ lemma continuous.tendsto' {f : α → β} (hf : continuous f) (x : α) (y : β) (h : f x = y) : tendsto f (𝓝 x) (𝓝 y) := h ▸ hf.tendsto x lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), continuous_def.2 $ assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), from λ a ha, is_open.mem_nhds hs ha, show is_open (f ⁻¹' s), from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩ lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := tendsto_const_nhds lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, continuous_at_const lemma filter.eventually_eq.continuous_at {x : α} {f : α → β} {y : β} (h : f =ᶠ[𝓝 x] (λ _, y)) : continuous_at f x := (continuous_at_congr h).2 tendsto_const_nhds lemma continuous_of_const {f : α → β} (h : ∀ x y, f x = f y) : continuous f := continuous_iff_continuous_at.mpr $ λ x, filter.eventually_eq.continuous_at $ eventually_of_forall (λ y, h y x) lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) : continuous_at (f^[n]) x := nat.rec_on n continuous_at_id $ λ n ihn, show continuous_at (f^[n] ∘ f) x, from continuous_at.comp (hx.symm ▸ ihn) hf lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, by simpa using (continuous_def.1 hf sᶜ hs.is_open_compl).is_closed_compl, assume hf, continuous_def.2 $ assume s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma is_closed.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_closed s) : is_closed (f ⁻¹' s) := continuous_iff_is_closed.mp hf s h lemma mem_closure_image {f : α → β} {x : α} {s : set α} (hf : continuous_at f x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := begin rw [mem_closure_iff_nhds_ne_bot] at hx ⊢, rw ← bot_lt_iff_ne_bot, haveI : ne_bot _ := ⟨hx⟩, calc ⊥ < map f (𝓝 x ⊓ principal s) : bot_lt_iff_ne_bot.mpr ne_bot.ne' ... ≤ (map f $ 𝓝 x) ⊓ (map f $ principal s) : map_inf_le ... = (map f $ 𝓝 x) ⊓ (principal $ f '' s) : by rw map_principal ... ≤ 𝓝 (f x) ⊓ (principal $ f '' s) : inf_le_inf hf le_rfl end lemma continuous_at_iff_ultrafilter {f : α → β} {x} : continuous_at f x ↔ ∀ g : ultrafilter α, ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x (g : ultrafilter α), ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] lemma continuous.closure_preimage_subset {f : α → β} (hf : continuous f) (t : set β) : closure (f ⁻¹' t) ⊆ f ⁻¹' (closure t) := begin rw ← (is_closed_closure.preimage hf).closure_eq, exact closure_mono (preimage_mono subset_closure), end lemma continuous.frontier_preimage_subset {f : α → β} (hf : continuous f) (t : set β) : frontier (f ⁻¹' t) ⊆ f ⁻¹' (frontier t) := diff_subset_diff (hf.closure_preimage_subset t) (preimage_interior_subset_interior_preimage hf) /-! ### Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', simp only [ptendsto'_def, mem_nhds_iff], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end /-- If a continuous map `f` maps `s` to `t`, then it maps `closure s` to `closure t`. -/ lemma set.maps_to.closure {s : set α} {t : set β} {f : α → β} (h : maps_to f s t) (hc : continuous f) : maps_to f (closure s) (closure t) := begin simp only [maps_to, mem_closure_iff_cluster_pt], exact λ x hx, hx.map hc.continuous_at (tendsto_principal_principal.2 h) end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := ((maps_to_image f s).closure h).image_subset lemma closure_subset_preimage_closure_image {f : α → β} {s : set α} (h : continuous f) : closure s ⊆ f ⁻¹' (closure (f '' s)) := by { rw ← set.image_subset_iff, exact image_closure_subset_closure_image h } lemma map_mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := set.maps_to.closure ht hf ha /-! ### Function with dense range -/ section dense_range variables {κ ι : Type*} (f : κ → β) (g : β → γ) /-- `f : ι → β` has dense range if its range (image) is a dense subset of β. -/ def dense_range := dense (range f) variables {f} /-- A surjective map has dense range. -/ lemma function.surjective.dense_range (hf : function.surjective f) : dense_range f := λ x, by simp [hf.range_eq] lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ := dense_iff_closure_eq lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ := h.closure_eq lemma dense.dense_range_coe {s : set α} (h : dense s) : dense_range (coe : s → α) := by simpa only [dense_range, subtype.range_coe_subtype] lemma continuous.range_subset_closure_image_dense {f : α → β} (hf : continuous f) {s : set α} (hs : dense s) : range f ⊆ closure (f '' s) := by { rw [← image_univ, ← hs.closure_eq], exact image_closure_subset_closure_image hf } /-- The image of a dense set under a continuous map with dense range is a dense set. -/ lemma dense_range.dense_image {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) : dense (f '' s) := (hf'.mono $ hf.range_subset_closure_image_dense hs).of_closure /-- If `f` has dense range and `s` is an open set in the codomain of `f`, then the image of the preimage of `s` under `f` is dense in `s`. -/ lemma dense_range.subset_closure_image_preimage_of_is_open (hf : dense_range f) {s : set β} (hs : is_open s) : s ⊆ closure (f '' (f ⁻¹' s)) := by { rw image_preimage_eq_inter_range, exact hf.open_subset_closure_inter hs } /-- If a continuous map with dense range maps a dense set to a subset of `t`, then `t` is a dense set. -/ lemma dense_range.dense_of_maps_to {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) {t : set β} (ht : maps_to f s t) : dense t := (hf'.dense_image hf hs).mono ht.image_subset /-- Composition of a continuous map with dense range and a function with dense range has dense range. -/ lemma dense_range.comp {g : β → γ} {f : κ → β} (hg : dense_range g) (hf : dense_range f) (cg : continuous g) : dense_range (g ∘ f) := by { rw [dense_range, range_comp], exact hg.dense_image cg hf } lemma dense_range.nonempty_iff (hf : dense_range f) : nonempty κ ↔ nonempty β := range_nonempty_iff_nonempty.symm.trans hf.nonempty_iff lemma dense_range.nonempty [h : nonempty β] (hf : dense_range f) : nonempty κ := hf.nonempty_iff.mpr h /-- Given a function `f : α → β` with dense range and `b : β`, returns some `a : α`. -/ def dense_range.some (hf : dense_range f) (b : β) : κ := classical.choice $ hf.nonempty_iff.mpr ⟨b⟩ lemma dense_range.exists_mem_open (hf : dense_range f) {s : set β} (ho : is_open s) (hs : s.nonempty) : ∃ a, f a ∈ s := exists_range_iff.1 $ hf.exists_mem_open ho hs lemma dense_range.mem_nhds {f : κ → β} (h : dense_range f) {b : β} {U : set β} (U_in : U ∈ nhds b) : ∃ a, f a ∈ U := begin rcases (mem_closure_iff_nhds.mp ((dense_range_iff_closure_range.mp h).symm ▸ mem_univ b : b ∈ closure (range f)) U U_in) with ⟨_, h, a, rfl⟩, exact ⟨a, h⟩ end end dense_range end continuous /-- The library contains many lemmas stating that functions/operations are continuous. There are many ways to formulate the continuity of operations. Some are more convenient than others. Note: for the most part this note also applies to other properties (`measurable`, `differentiable`, `continuous_on`, ...). ### The traditional way As an example, let's look at addition `(+) : M → M → M`. We can state that this is continuous in different definitionally equal ways (omitting some typing information) * `continuous (λ p, p.1 + p.2)`; * `continuous (function.uncurry (+))`; * `continuous ↿(+)`. (`↿` is notation for recursively uncurrying a function) However, lemmas with this conclusion are not nice to use in practice because 1. They confuse the elaborator. The following two examples fail, because of limitations in the elaboration process. ``` variables {M : Type*} [has_mul M] [topological_space M] [has_continuous_mul M] example : continuous (λ x : M, x + x) := continuous_add.comp _ example : continuous (λ x : M, x + x) := continuous_add.comp (continuous_id.prod_mk continuous_id) ``` The second is a valid proof, which is accepted if you write it as `continuous_add.comp (continuous_id.prod_mk continuous_id : _)` 2. If the operation has more than 2 arguments, they are impractical to use, because in your application the arguments in the domain might be in a different order or associated differently. ### The convenient way A much more convenient way to write continuity lemmas is like `continuous.add`: ``` continuous.add {f g : X → M} (hf : continuous f) (hg : continuous g) : continuous (λ x, f x + g x) ``` The conclusion can be `continuous (f + g)`, which is definitionally equal. This has the following advantages * It supports projection notation, so is shorter to write. * `continuous.add _ _` is recognized correctly by the elaborator and gives useful new goals. * It works generally, since the domain is a variable. As an example for an unary operation, we have `continuous.neg`. ``` continuous.neg {f : α → G} (hf : continuous f) : continuous (λ x, -f x) ``` For unary functions, the elaborator is not confused when applying the traditional lemma (like `continuous_neg`), but it's still convenient to have the short version available (compare `hf.neg.neg.neg` with `continuous_neg.comp $ continuous_neg.comp $ continuous_neg.comp hf`). As a harder example, consider an operation of the following type: ``` def strans {x : F} (γ γ' : path x x) (t₀ : I) : path x x ``` The precise definition is not important, only its type. The correct continuity principle for this operation is something like this: ``` {f : X → F} {γ γ' : ∀ x, path (f x) (f x)} {t₀ s : X → I} (hγ : continuous ↿γ) (hγ' : continuous ↿γ') (ht : continuous t₀) (hs : continuous s) : continuous (λ x, strans (γ x) (γ' x) (t x) (s x)) ``` Note that *all* arguments of `strans` are indexed over `X`, even the basepoint `x`, and the last argument `s` that arises since `path x x` has a coercion to `I → F`. The paths `γ` and `γ'` (which are unary functions from `I`) become binary functions in the continuity lemma. ### Summary * Make sure that your continuity lemmas are stated in the most general way, and in a convenient form. That means that: - The conclusion has a variable `X` as domain (not something like `Y × Z`); - Wherever possible, all point arguments `c : Y` are replaced by functions `c : X → Y`; - All `n`-ary function arguments are replaced by `n+1`-ary functions (`f : Y → Z` becomes `f : X → Y → Z`); - All (relevant) arguments have continuity assumptions, and perhaps there are additional assumptions needed to make the operation continuous; - The function in the conclusion is fully applied. * These remarks are mostly about the format of the *conclusion* of a continuity lemma. In assumptions it's fine to state that a function with more than 1 argument is continuous using `↿` or `function.uncurry`. ### Functions with discontinuities In some cases, you want to work with discontinuous functions, and in certain expressions they are still continuous. For example, consider the fractional part of a number, `fract : ℝ → ℝ`. In this case, you want to add conditions to when a function involving `fract` is continuous, so you get something like this: (assumption `hf` could be weakened, but the important thing is the shape of the conclusion) ``` lemma continuous_on.comp_fract {X Y : Type*} [topological_space X] [topological_space Y] {f : X → ℝ → Y} {g : X → ℝ} (hf : continuous ↿f) (hg : continuous g) (h : ∀ s, f s 0 = f s 1) : continuous (λ x, f x (fract (g x))) ``` With `continuous_at` you can be even more precise about what to prove in case of discontinuities, see e.g. `continuous_at.comp_div_cases`. -/ library_note "continuity lemma statement"
dc3b29ec4333f876f1fc4cc2109a7b5fa874113b
6e41ee3ac9b96e8980a16295cc21f131e731884f
/tests/lean/run/fibrant_class1.lean
d78435a3504b838a80f6ff0a840932939f8e204f
[ "Apache-2.0" ]
permissive
EgbertRijke/lean
3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3
4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183
refs/heads/master
1,610,834,871,476
1,422,159,801,000
1,422,159,801,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
558
lean
inductive fibrant [class] (T : Type) : Type := fibrant_mk : fibrant T inductive path {A : Type'} [fA : fibrant A] (a : A) : A → Type := idpath : path a a notation a ≈ b := path a b axiom path_fibrant {A : Type'} [fA : fibrant A] (a b : A) : fibrant (path a b) persistent attribute path_fibrant [instance] axiom imp_fibrant {A : Type'} {B : Type'} [C1 : fibrant A] [C2 : fibrant B] : fibrant (A → B) attribute imp_fibrant [instance] definition test {A : Type} [fA : fibrant A] {x y : A} : Π (z : A), y ≈ z → fibrant (x ≈ y → x ≈ z) := _
f90086a59c8c9f174523df530207fb271e1b9d9f
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/data/array/lemmas.lean
faa0656877d7a3e6971d5824f3cb73c9c6ea783a
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
8,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, Mario Carneiro -/ import control.traversable.equiv import data.vector2 universes u v w namespace d_array variables {n : ℕ} {α : fin n → Type u} instance [∀ i, inhabited (α i)] : inhabited (d_array n α) := ⟨⟨λ _, default _⟩⟩ end d_array namespace array instance {n α} [inhabited α] : inhabited (array n α) := d_array.inhabited theorem to_list_of_heq {n₁ n₂ α} {a₁ : array n₁ α} {a₂ : array n₂ α} (hn : n₁ = n₂) (ha : a₁ == a₂) : a₁.to_list = a₂.to_list := by congr; assumption /- rev_list -/ section rev_list variables {n : ℕ} {α : Type u} {a : array n α} theorem rev_list_reverse_aux : ∀ i (h : i ≤ n) (t : list α), (a.iterate_aux (λ _, (::)) i h []).reverse_core t = a.rev_iterate_aux (λ _, (::)) i h t | 0 h t := rfl | (i+1) h t := rev_list_reverse_aux i _ _ @[simp] theorem rev_list_reverse : a.rev_list.reverse = a.to_list := rev_list_reverse_aux _ _ _ @[simp] theorem to_list_reverse : a.to_list.reverse = a.rev_list := by rw [←rev_list_reverse, list.reverse_reverse] end rev_list /- mem -/ section mem variables {n : ℕ} {α : Type u} {v : α} {a : array n α} theorem mem.def : v ∈ a ↔ ∃ i, a.read i = v := iff.rfl theorem mem_rev_list_aux : ∀ {i} (h : i ≤ n), (∃ (j : fin n), j.1 < i ∧ read a j = v) ↔ v ∈ a.iterate_aux (λ _, (::)) i h [] | 0 _ := ⟨λ ⟨i, n, _⟩, absurd n i.val.not_lt_zero, false.elim⟩ | (i+1) h := let IH := mem_rev_list_aux (le_of_lt h) in ⟨λ ⟨j, ji1, e⟩, or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ ji1) (λ ji, list.mem_cons_of_mem _ $ IH.1 ⟨j, ji, e⟩) (λ je, by simp [d_array.iterate_aux]; apply or.inl; unfold read at e; have H : j = ⟨i, h⟩ := fin.eq_of_veq je; rwa [←H, e]), λ m, begin simp [d_array.iterate_aux, list.mem] at m, cases m with e m', exact ⟨⟨i, h⟩, nat.lt_succ_self _, eq.symm e⟩, exact let ⟨j, ji, e⟩ := IH.2 m' in ⟨j, nat.le_succ_of_le ji, e⟩ end⟩ @[simp] theorem mem_rev_list : v ∈ a.rev_list ↔ v ∈ a := iff.symm $ iff.trans (exists_congr $ λ j, iff.symm $ show j.1 < n ∧ read a j = v ↔ read a j = v, from and_iff_right j.2) (mem_rev_list_aux _) @[simp] theorem mem_to_list : v ∈ a.to_list ↔ v ∈ a := by rw ←rev_list_reverse; exact list.mem_reverse.trans mem_rev_list end mem /- foldr -/ section foldr variables {n : ℕ} {α : Type u} {β : Type w} {b : β} {f : α → β → β} {a : array n α} theorem rev_list_foldr_aux : ∀ {i} (h : i ≤ n), (d_array.iterate_aux a (λ _, (::)) i h []).foldr f b = d_array.iterate_aux a (λ _, f) i h b | 0 h := rfl | (j+1) h := congr_arg (f (read a ⟨j, h⟩)) (rev_list_foldr_aux _) theorem rev_list_foldr : a.rev_list.foldr f b = a.foldl b f := rev_list_foldr_aux _ end foldr /- foldl -/ section foldl variables {n : ℕ} {α : Type u} {β : Type w} {b : β} {f : β → α → β} {a : array n α} theorem to_list_foldl : a.to_list.foldl f b = a.foldl b (function.swap f) := by rw [←rev_list_reverse, list.foldl_reverse, rev_list_foldr] end foldl /- length -/ section length variables {n : ℕ} {α : Type u} theorem rev_list_length_aux (a : array n α) (i h) : (a.iterate_aux (λ _, (::)) i h []).length = i := by induction i; simp [*, d_array.iterate_aux] @[simp] theorem rev_list_length (a : array n α) : a.rev_list.length = n := rev_list_length_aux a _ _ @[simp] theorem to_list_length (a : array n α) : a.to_list.length = n := by rw[←rev_list_reverse, list.length_reverse, rev_list_length] end length /- nth -/ section nth variables {n : ℕ} {α : Type u} {a : array n α} theorem to_list_nth_le_aux (i : ℕ) (ih : i < n) : ∀ j {jh t h'}, (∀ k tl, j + k = i → list.nth_le t k tl = a.read ⟨i, ih⟩) → (a.rev_iterate_aux (λ _, (::)) j jh t).nth_le i h' = a.read ⟨i, ih⟩ | 0 _ _ _ al := al i _ $ zero_add _ | (j+1) jh t h' al := to_list_nth_le_aux j $ λ k tl hjk, show list.nth_le (a.read ⟨j, jh⟩ :: t) k tl = a.read ⟨i, ih⟩, from match k, hjk, tl with | 0, e, tl := match i, e, ih with ._, rfl, _ := rfl end | k'+1, _, tl := by simp[list.nth_le]; exact al _ _ (by simp [*]; cc) end theorem to_list_nth_le (i : ℕ) (h h') : list.nth_le a.to_list i h' = a.read ⟨i, h⟩ := to_list_nth_le_aux _ _ _ (λ k tl, absurd tl k.not_lt_zero) @[simp] theorem to_list_nth_le' (a : array n α) (i : fin n) (h') : list.nth_le a.to_list i.1 h' = a.read i := by cases i; apply to_list_nth_le theorem to_list_nth {i v} : list.nth a.to_list i = some v ↔ ∃ h, a.read ⟨i, h⟩ = v := begin rw list.nth_eq_some, have ll := to_list_length a, split; intro h; cases h with h e; subst v, { exact ⟨ll ▸ h, (to_list_nth_le _ _ _).symm⟩ }, { exact ⟨ll.symm ▸ h, to_list_nth_le _ _ _⟩ } end theorem write_to_list {i v} : (a.write i v).to_list = a.to_list.update_nth i.1 v := list.ext_le (by simp) $ λ j h₁ h₂, begin have h₃ : j < n, {simpa using h₁}, rw [to_list_nth_le _ h₃], refine let ⟨_, e⟩ := list.nth_eq_some.1 _ in e.symm, by_cases ij : i.1 = j, { subst j, rw [show fin.mk i.val h₃ = i, from fin.eq_of_veq rfl, array.read_write, list.nth_update_nth_of_lt], simp [h₃] }, { rw [list.nth_update_nth_ne _ _ ij, a.read_write_of_ne, to_list_nth.2 ⟨h₃, rfl⟩], exact fin.ne_of_vne ij } end end nth /- enum -/ section enum variables {n : ℕ} {α : Type u} {a : array n α} theorem mem_to_list_enum {i v} : (i, v) ∈ a.to_list.enum ↔ ∃ h, a.read ⟨i, h⟩ = v := by simp [list.mem_iff_nth, to_list_nth, and.comm, and.assoc, and.left_comm] end enum /- to_array -/ section to_array variables {n : ℕ} {α : Type u} @[simp] theorem to_list_to_array (a : array n α) : a.to_list.to_array == a := heq_of_heq_of_eq (@@eq.drec_on (λ m (e : a.to_list.length = m), (d_array.mk (λ v, a.to_list.nth_le v.1 v.2)) == (@d_array.mk m (λ _, α) $ λ v, a.to_list.nth_le v.1 $ e.symm ▸ v.2)) a.to_list_length heq.rfl) $ d_array.ext $ λ ⟨i, h⟩, to_list_nth_le i h _ @[simp] theorem to_array_to_list (l : list α) : l.to_array.to_list = l := list.ext_le (to_list_length _) $ λ n h1 h2, to_list_nth_le _ h2 _ end to_array /- push_back -/ section push_back variables {n : ℕ} {α : Type u} {v : α} {a : array n α} lemma push_back_rev_list_aux : ∀ i h h', d_array.iterate_aux (a.push_back v) (λ _, (::)) i h [] = d_array.iterate_aux a (λ _, (::)) i h' [] | 0 h h' := rfl | (i+1) h h' := begin simp [d_array.iterate_aux], refine ⟨_, push_back_rev_list_aux _ _ _⟩, dsimp [read, d_array.read, push_back], rw [dif_neg], refl, exact ne_of_lt h', end @[simp] theorem push_back_rev_list : (a.push_back v).rev_list = v :: a.rev_list := begin unfold push_back rev_list foldl iterate d_array.iterate, dsimp [d_array.iterate_aux, read, d_array.read, push_back], rw [dif_pos (eq.refl n)], apply congr_arg, apply push_back_rev_list_aux end @[simp] theorem push_back_to_list : (a.push_back v).to_list = a.to_list ++ [v] := by rw [←rev_list_reverse, ←rev_list_reverse, push_back_rev_list, list.reverse_cons] end push_back /- foreach -/ section foreach variables {n : ℕ} {α : Type u} {β : Type v} {i : fin n} {f : fin n → α → β} {a : array n α} @[simp] theorem read_foreach : (foreach a f).read i = f i (a.read i) := rfl end foreach /- map -/ section map variables {n : ℕ} {α : Type u} {β : Type v} {i : fin n} {f : α → β} {a : array n α} theorem read_map : (a.map f).read i = f (a.read i) := read_foreach end map /- map₂ -/ section map₂ variables {n : ℕ} {α : Type u} {i : fin n} {f : α → α → α} {a₁ a₂ : array n α} @[simp] theorem read_map₂ : (map₂ f a₁ a₂).read i = f (a₁.read i) (a₂.read i) := read_foreach end map₂ end array namespace equiv def d_array_equiv_fin {n : ℕ} (α : fin n → Type*) : d_array n α ≃ (∀ i, α i) := ⟨d_array.read, d_array.mk, λ ⟨f⟩, rfl, λ f, rfl⟩ def array_equiv_fin (n : ℕ) (α : Type*) : array n α ≃ (fin n → α) := d_array_equiv_fin _ def vector_equiv_fin (α : Type*) (n : ℕ) : vector α n ≃ (fin n → α) := ⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩ def vector_equiv_array (α : Type*) (n : ℕ) : vector α n ≃ array n α := (vector_equiv_fin _ _).trans (array_equiv_fin _ _).symm end equiv namespace array open function variable {n : ℕ} instance : traversable (array n) := @equiv.traversable (flip vector n) _ (λ α, equiv.vector_equiv_array α n) _ instance : is_lawful_traversable (array n) := @equiv.is_lawful_traversable (flip vector n) _ (λ α, equiv.vector_equiv_array α n) _ _ end array
f8cf9de5c6756f3086f5b661fa3d5d33c7b37809
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/tacticTests.lean
be035acb77d794ce81c8aac76a47dddcc6d5956e
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,472
lean
inductive Le (m : Nat) : Nat → Prop | base : Le m m | succ : (n : Nat) → Le m n → Le m n.succ theorem ex1 (m : Nat) : Le m 0 → m = 0 := by intro h cases h rfl theorem ex2 (m n : Nat) : Le m n → Le m.succ n.succ := by intro h induction h with | base n => apply Le.base | succ n m ih => apply Le.succ apply ih theorem ex3 (m : Nat) : Le 0 m := by induction m with | zero => apply Le.base | succ m ih => apply Le.succ apply ih theorem ex4 (m : Nat) : ¬ Le m.succ 0 := by intro h cases h theorem ex5 {m n : Nat} : Le m n.succ → m = n.succ ∨ Le m n := by intro h cases h with | base => apply Or.inl; rfl | succ => apply Or.inr; assumption theorem ex6 {m n : Nat} : Le m.succ n.succ → Le m n := by revert m induction n with | zero => intros m h; cases h with | base => apply Le.base | succ n h => exact absurd h (ex4 _) | succ n ih => intros m h have aux := ih (m := m) cases ex5 h with | inl h => injection h with h subst h apply Le.base | inr h => apply Le.succ exact ih h theorem ex7 {m n o : Nat} : Le m n → Le n o → Le m o := by intro h induction h with | base => intros; assumption | succ n s ih => intro h₂ apply ih apply ex6 apply Le.succ assumption theorem ex8 {m n : Nat} : Le m.succ n → Le m n := by intro h apply ex6 apply Le.succ assumption theorem ex9 {m n : Nat} : Le m n → m = n ∨ Le m.succ n := by intro h cases h with | base => apply Or.inl; rfl | succ n s => apply Or.inr apply ex2 assumption /- theorem ex10 (n : Nat) : ¬ Le n.succ n := by intro h cases h -- TODO: improve cases tactic done -/ theorem ex10 (n : Nat) : n.succ ≠ n := by induction n with | zero => intro h; injection h; done | succ n ih => intro h; injection h with h; apply ih h theorem ex11 (n : Nat) : ¬ Le n.succ n := by induction n with | zero => intro h; cases h; done | succ n ih => intro h have aux := ex6 h exact absurd aux ih done theorem ex12 (m n : Nat) : Le m n → Le n m → m = n := by revert m induction n with | zero => intro m h1 h2; apply ex1; assumption; done | succ n ih => intro m h1 h2 have ih := ih m cases ex5 h1 with | inl h => assumption | inr h => have ih := ih h have h3 := ex8 h2 have ih := ih h3 subst ih apply absurd h2 (ex11 _) done
1b4f9e8f0b8a937dd870d9914894d356507e3938
4c630d016e43ace8c5f476a5070a471130c8a411
/set_theory/cardinal.lean
f3e1a1d46464e5e9f2ac461f087fc500b594815d
[ "Apache-2.0" ]
permissive
ngamt/mathlib
9a510c391694dc43eec969914e2a0e20b272d172
58909bd424209739a2214961eefaa012fb8a18d2
refs/heads/master
1,585,942,993,674
1,540,739,585,000
1,540,916,815,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
30,595
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Mario Carneiro Cardinal arithmetic. Cardinals are represented as quotient over equinumerous types. -/ import data.set.finite data.quot logic.schroeder_bernstein logic.function open function lattice set local attribute [instance] classical.prop_decidable universes u v w x instance cardinal.is_equivalent : setoid (Type u) := { r := λα β, nonempty (α ≃ β), iseqv := ⟨λα, ⟨equiv.refl α⟩, λα β ⟨e⟩, ⟨e.symm⟩, λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `cardinal.{u}` is the type of cardinal numbers in `Type u`, defined as the quotient of `Type u` by existence of an equivalence (a bijection with explicit inverse). -/ def cardinal : Type (u + 1) := quotient cardinal.is_equivalent namespace cardinal /-- The cardinal of a type -/ def mk : Type u → cardinal := quotient.mk @[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (mk α) := rfl @[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _ instance : has_le cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩ theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} : c ≤ mk α ↔ ∃ p : set α, mk p = c := ⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩, ⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩, λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩ instance : linear_order cardinal.{u} := { le := (≤), le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩, le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.trans e₂⟩, le_antisymm := by rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩; exact quotient.sound (e₁.antisymm e₂), le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total } noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _ noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance instance : has_zero cardinal.{u} := ⟨⟦pempty⟧⟩ instance : inhabited cardinal.{u} := ⟨0⟩ theorem ne_zero_iff_nonempty {α : Type u} : mk α ≠ 0 ↔ nonempty α := not_iff_comm.1 ⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pempty⟩, λ e, let ⟨h⟩ := quotient.exact e in λ ⟨a⟩, (h a).elim⟩ instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩ instance : zero_ne_one_class cardinal.{u} := { zero := 0, one := 1, zero_ne_one := ne.symm $ ne_zero_iff_nonempty.2 ⟨punit.star⟩ } theorem le_one_iff_subsingleton {α : Type u} : mk α ≤ 1 ↔ subsingleton α := ⟨λ ⟨f⟩, ⟨λ a b, f.inj (subsingleton.elim _ _)⟩, λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a b _, h _ _⟩⟩⟩ instance : has_add cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩ @[simp] theorem add_def (α β) : mk α + mk β = mk (α ⊕ β) := rfl instance : has_mul cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩ @[simp] theorem mul_def (α β) : mk α * mk β = mk (α × β) := rfl private theorem add_comm (a b : cardinal.{u}) : a + b = b + a := quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩ private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a := quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩ private theorem zero_add (a : cardinal.{u}) : 0 + a = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_sum α⟩ private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_prod α⟩ private theorem one_mul (a : cardinal.{u}) : 1 * a = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_prod α⟩ private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩ instance : comm_semiring cardinal.{u} := { zero := 0, one := 1, add := (+), mul := (*), zero_add := zero_add, add_zero := assume a, by rw [add_comm a 0, zero_add a], add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.sum_assoc α β γ⟩, add_comm := add_comm, zero_mul := zero_mul, mul_zero := assume a, by rw [mul_comm a 0, zero_mul a], one_mul := one_mul, mul_one := assume a, by rw [mul_comm a 1, one_mul a], mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_assoc α β γ⟩, mul_comm := mul_comm, left_distrib := left_distrib, right_distrib := assume a b c, by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] } /-- The cardinal exponential. `mk α ^ mk β` is the cardinal of `β → α`. -/ protected def power (a b : cardinal.{u}) : cardinal.{u} := quotient.lift_on₂ a b (λα β, mk (β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩ instance : has_pow cardinal cardinal := ⟨cardinal.power⟩ local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow @[simp] theorem power_def (α β) : mk α ^ mk β = mk (β → α) := rfl @[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_arrow_equiv_punit α⟩ @[simp] theorem power_one {a : cardinal} : a ^ 1 = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_arrow_equiv α⟩ @[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.arrow_punit_equiv_punit α⟩ @[simp] theorem prop_eq_two : mk (ulift Prop) = 2 := quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 := quotient.induction_on a $ assume α heq, nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a, quotient.sound ⟨equiv.equiv_pempty $ assume f, pempty.rec (λ _, false) (f a)⟩ theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 := quotient.induction_on₂ a b $ λ α β h, let ⟨a⟩ := ne_zero_iff_nonempty.1 h in ne_zero_iff_nonempty.2 ⟨λ _, a⟩ theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩ theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩ theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) := by rw [_root_.mul_comm b c]; from (quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow γ β α⟩) section order_properties open sum theorem zero_le : ∀(a : cardinal), 0 ≤ a := by rintro ⟨α⟩; exact ⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim⟩ theorem le_zero (a : cardinal) : a ≤ 0 ↔ a = 0 := by simp [le_antisymm_iff, zero_le] theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o ≠ 0 := by simp [lt_iff_le_and_ne, eq_comm, zero_le] theorem zero_lt_one : (0 : cardinal) < 1 := lt_of_le_of_ne (zero_le _) zero_ne_one theorem add_le_add : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨embedding.sum_congr e₁ e₂⟩ theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c := add_le_add (le_refl _) theorem add_le_add_right {a b : cardinal} (c) (h : a ≤ b) : a + c ≤ b + c := add_le_add h (le_refl _) theorem le_add_right (a b : cardinal) : a ≤ a + b := by simpa using add_le_add_left a (zero_le b) theorem le_add_left (a b : cardinal) : a ≤ b + a := by simpa using add_le_add_right a (zero_le b) theorem mul_le_mul : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a * c ≤ b * d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨embedding.prod_congr e₁ e₂⟩ theorem mul_le_mul_left (a) {b c : cardinal} : b ≤ c → a * b ≤ a * c := mul_le_mul (le_refl _) theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≤ b) : a * c ≤ b * c := mul_le_mul h (le_refl _) theorem power_le_power_left : ∀{a b c : cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩; exact let ⟨a⟩ := ne_zero_iff_nonempty.1 hα in ⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩ theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c := quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩ theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c := ⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩, have (α ⊕ ↥-range f) ≃ β, from (equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $ (equiv.set.sum_compl (range f)), ⟨⟦(-range f : set β)⟧, quotient.sound ⟨this.symm⟩⟩, λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ add_le_add_left _ (zero_le _)⟩ end order_properties instance : canonically_ordered_monoid cardinal.{u} := { add_le_add_left := λ a b h c, add_le_add_left _ h, lt_of_add_lt_add_left := λ a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _), le_iff_exists_add := @le_iff_exists_add, ..cardinal.comm_semiring, ..cardinal.linear_order } instance : order_bot cardinal.{u} := { bot := 0, bot_le := zero_le, ..cardinal.linear_order } theorem cantor : ∀(a : cardinal.{u}), a < 2 ^ a := by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨ ⟨⟨λ a b, ⟨a = b⟩, λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩, λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $ λ s t h, by funext a; injection congr_fun (hf h) a⟩ instance : no_top_order cardinal.{u} := { no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order } /-- The minimum cardinal in a family of cardinals (the existence of which is provided by `injective_min`). -/ noncomputable def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal := f $ classical.some $ @embedding.injective_min _ (λ i, (f i).out) I theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i := ⟨_, rfl⟩ theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i := by rw [← mk_out (min I f), ← mk_out (f i)]; exact let ⟨g⟩ := classical.some_spec (@embedding.injective_min _ (λ i, (f i).out) I) in ⟨g i⟩ theorem le_min {ι I} {f : ι → cardinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i := ⟨λ h i, le_trans h (min_le _ _), λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩ protected theorem wf : @well_founded cardinal.{u} (<) := ⟨λ a, classical.by_contradiction $ λ h, let ι := {c :cardinal // ¬ acc (<) c}, f : ι → cardinal := subtype.val, ⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in hc (acc.intro _ (λ j ⟨_, h'⟩, classical.by_contradiction $ λ hj, h' $ by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩ instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩ instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩ /-- The successor cardinal - the smallest cardinal greater than `c`. This is not the same as `c + 1` except in the case of finite `c`. -/ noncomputable def succ (c : cardinal) : cardinal := @min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val theorem lt_succ_self (c : cardinal) : c < succ c := by cases min_eq _ _ with s e; rw [succ, e]; exact s.2 theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b := ⟨lt_of_lt_of_le (lt_succ_self _), λ h, by exact min_le _ (subtype.mk b h)⟩ theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b := by rw [← not_le, succ_le, not_lt] theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c := begin refine quot.induction_on c (λ α, _) (lt_succ_self c), refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _), cases h.left with f, have : ¬ surjective f := λ hn, ne_of_lt h (quotient.sound ⟨equiv.of_bijective ⟨f.inj, hn⟩⟩), cases classical.not_forall.1 this with b nex, refine ⟨⟨sum.rec (by exact f) _, _⟩⟩, { exact λ _, b }, { intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩, { rw f.inj h }, { exact nex.elim ⟨_, h⟩ }, { exact nex.elim ⟨_, h.symm⟩ }, { refl } } end /-- The indexed sum of cardinals is the cardinality of the indexed disjoint union, i.e. sigma type. -/ def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f := by rw ← quotient.out_eq (f i); exact ⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩ @[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, mk (f i)) = mk (Σ i, f i) := quot.sound ⟨equiv.sigma_congr_right $ λ i, classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩ theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = mk ι * a := quotient.induction_on a $ λ α, by simp; exact quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩ theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g := ⟨embedding.sigma_congr_right $ λ i, classical.choice $ by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩ /-- The indexed supremum of cardinals is the smallest cardinal above everything in the family. -/ noncomputable def sup {ι} (f : ι → cardinal) : cardinal := @min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1) theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f := by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i theorem sup_le {ι} {f : ι → cardinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a := ⟨λ h i, le_trans (le_sup _ _) h, λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩ theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g := sup_le.2 $ λ i, le_trans (H i) (le_sup _ _) theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f := sup_le.2 $ le_sum _ theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ mk ι * sup.{u u} f := by rw ← sum_const; exact sum_le_sum _ _ (le_sup _) /-- The indexed product of cardinals is the cardinality of the Pi type (dependent product). -/ def prod {ι : Type u} (f : ι → cardinal) : cardinal := mk (Π i, (f i).out) @[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, mk (f i)) = mk (Π i, f i) := quot.sound ⟨equiv.Pi_congr_right $ λ i, classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩ theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ mk ι := quotient.induction_on a $ by simp theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g := ⟨embedding.Pi_congr_right $ λ i, classical.choice $ by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩ theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 := begin conv in (f _) {rw ← mk_out (f i)}, simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def], exact ⟨λ ⟨F⟩ i, ⟨F i⟩, λ h, ⟨λ i, classical.choice (h i)⟩⟩, end theorem prod_eq_zero {ι} (f : ι → cardinal) : prod f = 0 ↔ ∃ i, f i = 0 := not_iff_not.1 $ by simpa using prod_ne_zero f /-- The universe lift operation on cardinals -/ def lift (c : cardinal.{u}) : cardinal.{max u v} := quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩, quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩ theorem lift_mk (α) : lift.{u v} (mk α) = mk (ulift.{v u} α) := rfl theorem lift_umax : lift.{u (max u v)} = lift.{u v} := funext $ λ a, quot.induction_on a $ λ α, quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_id' (a : cardinal) : lift a = a := quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} @[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a := quot.induction_on a $ λ α, quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_mk_le {α : Type u} {β : Type v} : lift.{u (max v w)} (mk α) ≤ lift.{v (max u w)} (mk β) ↔ nonempty (α ↪ β) := ⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩, λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩ theorem lift_mk_eq {α : Type u} {β : Type v} : lift.{u (max v w)} (mk α) = lift.{v (max u w)} (mk β) ↔ nonempty (α ≃ β) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩, λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩ @[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b := quotient.induction_on₂ a b $ λ α β, by rw ← lift_umax; exact lift_mk_le @[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b := by simp [le_antisymm_iff] @[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b := by simp [lt_iff_le_not_le, -not_le] @[simp] theorem lift_zero : lift 0 = 0 := quotient.sound ⟨equiv.ulift.trans equiv.pempty_equiv_pempty⟩ @[simp] theorem lift_one : lift 1 = 1 := quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩ @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a := by simp [bit0] @[simp] theorem lift_min {ι I} (f : ι → cardinal) : lift (min I f) = min I (lift ∘ f) := le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $ let ⟨i, e⟩ := min_eq I (lift ∘ f) in by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $ by have := min_le (lift ∘ f) j; rwa e at this) theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} : b ≤ lift a → ∃ a', lift a' = b := quotient.induction_on₂ a b $ λ α β, by dsimp; rw [← lift_id (mk β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact λ ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2 ⟨embedding.equiv_of_surjective (embedding.cod_restrict _ f set.mem_range_self) $ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩ theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} : b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩ theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} : b < lift a ↔ ∃ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := le_antisymm (le_of_not_gt $ λ h, begin rcases lt_lift_iff.1 h with ⟨b, e, h⟩, rw [lt_succ, ← lift_le, e] at h, exact not_lt_of_le h (lt_succ_self _) end) (succ_le.2 $ lift_lt.2 $ lt_succ_self _) /-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/ def omega : cardinal.{u} := lift (mk ℕ) theorem omega_ne_zero : omega ≠ 0 := ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩ theorem omega_pos : 0 < omega := pos_iff_ne_zero.2 omega_ne_zero @[simp] theorem lift_omega : lift omega = omega := lift_lift _ @[simp] theorem mk_fin : ∀ (n : ℕ), mk (fin n) = n | 0 := quotient.sound ⟨(equiv.pempty_of_not_nonempty $ λ ⟨h⟩, h.elim0)⟩ | (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact quotient.sound (fintype.card_eq.1 $ by simp) @[simp] theorem lift_nat_cast (n : ℕ) : lift n = n := by induction n; simp * theorem lift_mk_fin (n : ℕ) : lift (mk (fin n)) = n := by simp theorem fintype_card (α : Type u) [fintype α] : mk α = fintype.card α := by rw [← lift_mk_fin.{u}, ← lift_id (mk α), lift_mk_eq.{u 0 u}]; exact fintype.card_eq.1 (by simp) theorem card_le_of_finset {α} (s : finset α) : (s.card : cardinal) ≤ cardinal.mk α := begin rw (_ : (s.card : cardinal) = cardinal.mk (↑s : set α)), { exact ⟨function.embedding.subtype _⟩ }, rw [cardinal.fintype_card, fintype.card_coe] end @[simp] theorem nat_cast_pow {m n : ℕ} : (↑(pow m n) : cardinal) = m ^ n := by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *] @[simp] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n := by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact ⟨λ ⟨⟨f, hf⟩⟩, begin have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf, simp at this, rw [← fintype.card_fin n, ← this], exact finset.card_le_of_subset (finset.subset_univ _) end, λ h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, λ a b h, have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩ @[simp] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n := by simp [lt_iff_le_not_le, -not_le] @[simp] theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n := by simp [le_antisymm_iff] @[simp] theorem nat_succ (n : ℕ) : succ n = n.succ := le_antisymm (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _) (add_one_le_succ _) @[simp] theorem succ_zero : succ 0 = 1 := by simpa using nat_succ 0 theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a := by rw [← succ_le, (by simpa using nat_succ 1 : succ 1 = 2)] at hb; exact lt_of_lt_of_le (cantor _) (power_le_power_right hb) theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c := by rw [← succ_zero, succ_le] theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < omega := succ_le.1 $ by rw [nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact ⟨⟨fin.val, λ a b, fin.eq_of_veq⟩⟩ theorem one_lt_omega : 1 < omega := by simpa using nat_lt_omega 1 theorem lt_omega {c : cardinal.{u}} : c < omega ↔ ∃ n : ℕ, c = n := ⟨λ h, begin rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩, rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩, suffices : finite S, { cases this, resetI, existsi fintype.card S, rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] }, by_contra nf, have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a := λ n IH, let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in classical.not_forall.1 (λ h, nf ⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩), let F : ℕ → S := nat.lt_wf.fix (λ n IH, classical.some (P n IH)), refine not_le_of_lt h' ⟨⟨F, _⟩⟩, suffices : ∀ (n : ℕ) (m < n), F m ≠ F n, { refine λ m n, not_imp_not.1 (λ ne, _), rcases lt_trichotomy m n with h|h|h, { exact this n m h }, { contradiction }, { exact (this m n h).symm } }, intros n m h, have := classical.some_spec (P n (λ y _, F y)), rw [← show F n = classical.some (P n (λ y _, F y)), from nat.lt_wf.fix_eq (λ n IH, classical.some (P n IH)) n] at this, exact λ e, this ⟨m, h, e⟩, end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩ theorem omega_le {c : cardinal.{u}} : omega ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c := ⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h, λ h, le_of_not_lt $ λ hn, begin rcases lt_omega.1 hn with ⟨n, rfl⟩, exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1))) end⟩ theorem lt_omega_iff_fintype {α : Type u} : mk α < omega ↔ nonempty (fintype α) := lt_omega.trans ⟨λ ⟨n, e⟩, begin rw [← lift_mk_fin n] at e, cases quotient.exact e with f, exact ⟨fintype.of_equiv _ f.symm⟩ end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩ theorem lt_omega_iff_finite {α} {S : set α} : mk S < omega ↔ finite S := lt_omega_iff_fintype theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega end theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega end theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega end /-- König's theorem -/ theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g := lt_of_not_ge $ λ ⟨F⟩, begin have : inhabited (Π (i : ι), (g i).out), { refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩, rw mk_out, exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI, let G := inv_fun F, have sG : surjective G := inv_fun_surjective F.2, choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b, { assume i, simp only [- not_exists, not_exists.symm, classical.not_forall.symm], refine λ h, not_le_of_lt (H i) _, rw [← mk_out (f i), ← mk_out (g i)], exact ⟨embedding.of_surjective h⟩ }, exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _)) end @[simp] theorem mk_empty : mk empty = 0 := fintype_card empty @[simp] theorem mk_pempty : mk pempty = 0 := fintype_card pempty @[simp] theorem mk_empty' (α : Type u) : mk (∅ : set α) = 0 := quotient.sound ⟨equiv.set.pempty α⟩ @[simp] theorem mk_plift_false : mk (plift false) = 0 := quotient.sound ⟨equiv.plift.trans $ equiv.false_equiv_pempty⟩ @[simp] theorem mk_unit : mk unit = 1 := (fintype_card unit).trans nat.cast_one @[simp] theorem mk_punit : mk punit = 1 := (fintype_card punit).trans nat.cast_one @[simp] theorem mk_singleton {α : Type u} (x : α) : mk ({x} : set α) = 1 := quotient.sound ⟨equiv.set.singleton x⟩ @[simp] theorem mk_plift_true : mk (plift true) = 1 := quotient.sound ⟨equiv.plift.trans equiv.true_equiv_punit⟩ @[simp] theorem mk_bool : mk bool = 2 := quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem mk_Prop : mk Prop = 2 := (quotient.sound ⟨equiv.Prop_equiv_bool⟩ : mk Prop = mk bool).trans mk_bool @[simp] theorem mk_option {α : Type u} : mk (option α) = mk α + 1 := quotient.sound ⟨equiv.option_equiv_sum_punit α⟩ theorem mk_eq_of_injective {α β : Type u} {f : α → β} {s : set α} (hf : injective f) : mk (f '' s) = mk s := quotient.sound ⟨(equiv.set.image f s hf).symm⟩ theorem mk_list_eq_sum_pow (α : Type u) : mk (list α) = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) := calc mk (list α) = mk (Σ n, vector α n) : quotient.sound ⟨equiv.equiv_sigma_subtype list.length⟩ ... = mk (Σ n, fin n → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n, ⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩⟩ ... = mk (Σ n : ℕ, ulift.{u} (fin n) → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n, equiv.arrow_congr equiv.ulift.symm (equiv.refl α)⟩ ... = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk] theorem mk_Union_le_sum_mk {α ι : Type u} {f : ι → set α} : mk (⋃ i, f i) ≤ sum (λ i, mk (f i)) := calc mk (⋃ i, f i) ≤ mk (Σ i, f i) : show nonempty ((⋃ i, f i) ↪ (Σ i, f i)), from ⟨⟨λ x, ⟨classical.some (mem_Union.1 x.2), x.1, classical.some_spec (mem_Union.1 x.2)⟩, λ x y H, subtype.eq $ begin cases sigma.mk.inj H with H1 H2, clear H, generalize_hyp : classical.some_spec _ = H4 at H1 H2, generalize_hyp : classical.some _ = i₀ at H1 H2 H4, subst H1, exact subtype.mk.inj (eq_of_heq H2) end⟩⟩ ... = sum (λ i, mk (f i)) : (sum_mk _).symm @[simp] lemma finset_card {α : Type u} {s : finset α} : ↑(finset.card s) = mk (↑s : set α) := by rw [fintype_card, nat_cast_inj, fintype.card_coe] theorem mk_union_add_mk_inter {α : Type u} {S T : set α} : mk (S ∪ T : set α) + mk (S ∩ T : set α) = mk S + mk T := quotient.sound $ nonempty.intro $ { to_fun := λ x, sum.rec_on x (λ x, if h : x.1 ∈ S then sum.inl ⟨x.1, h⟩ else sum.inr ⟨x.1, x.2.resolve_left h⟩) (λ x, sum.inr ⟨x.1, x.2.2⟩), inv_fun := λ x, sum.rec_on x (λ x, sum.inl ⟨x.1, or.inl x.2⟩) (λ x, if h : x.1 ∈ S then sum.inr ⟨x.1, h, x.2⟩ else sum.inl ⟨x.1, or.inr x.2⟩), left_inv := λ x, sum.rec_on x (λ ⟨x, hx⟩, if h : x ∈ S then by dsimp only; rw [dif_pos h]; refl else by dsimp only; rw [dif_neg h]; dsimp only; rw [dif_neg h]; refl) (λ ⟨x, hx1, hx2⟩, by dsimp only; rw [dif_pos hx1]), right_inv := λ x, sum.rec_on x (λ ⟨x, hx⟩, by dsimp only; rw [dif_pos hx]) (λ ⟨x, hx⟩, if h : x ∈ S then by dsimp only; rw [dif_pos h] else by dsimp only; rw [dif_neg h]; dsimp only; rw [dif_neg h]) } theorem mk_union_of_disjiont {α : Type u} {S T : set α} (H : disjoint S T) : mk (S ∪ T : set α) = mk S + mk T := eq.trans (by simp only [(eq_empty_of_subset_empty H : S ∩ T = ∅), mk_empty', add_zero]) mk_union_add_mk_inter end cardinal
bd17b7975cbe1b6b03f76469daa48e18a6c967f2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/data/vector_auto.lean
d9445fbd22b6b7918f7ae1eb7c08114b6cd23883
[]
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,978
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 Tuples are lists of a fixed size. It is implemented as a subtype. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.default import Mathlib.Lean3Lib.init.data.subtype.default import Mathlib.Lean3Lib.init.meta.interactive import Mathlib.Lean3Lib.init.data.fin.default universes u u_1 v w namespace Mathlib def vector (α : Type u) (n : ℕ) := Subtype fun (l : List α) => list.length l = n namespace vector protected instance decidable_eq {α : Type u} {n : ℕ} [DecidableEq α] : DecidableEq (vector α n) := eq.mpr sorry fun (a b : Subtype fun (l : List α) => list.length l = n) => subtype.decidable_eq a b def nil {α : Type u} : vector α 0 := { val := [], property := sorry } def cons {α : Type u} {n : ℕ} : α → vector α n → vector α (Nat.succ n) := sorry def length {α : Type u} {n : ℕ} (v : vector α n) : ℕ := n def head {α : Type u} {n : ℕ} : vector α (Nat.succ n) → α := sorry theorem head_cons {α : Type u} {n : ℕ} (a : α) (v : vector α n) : head (cons a v) = a := sorry def tail {α : Type u} {n : ℕ} : vector α n → vector α (n - 1) := sorry theorem tail_cons {α : Type u} {n : ℕ} (a : α) (v : vector α n) : tail (cons a v) = v := sorry @[simp] theorem cons_head_tail {α : Type u} {n : ℕ} (v : vector α (Nat.succ n)) : cons (head v) (tail v) = v := sorry def to_list {α : Type u} {n : ℕ} (v : vector α n) : List α := subtype.val v def nth {α : Type u} {n : ℕ} (v : vector α n) : fin n → α := sorry def append {α : Type u} {n : ℕ} {m : ℕ} : vector α n → vector α m → vector α (n + m) := sorry def elim {α : Type u_1} {C : {n : ℕ} → vector α n → Sort u} (H : (l : List α) → C { val := l, property := elim._proof_1 l }) {n : ℕ} (v : vector α n) : C v := sorry /- map -/ def map {α : Type u} {β : Type v} {n : ℕ} (f : α → β) : vector α n → vector β n := sorry @[simp] theorem map_nil {α : Type u} {β : Type v} (f : α → β) : map f nil = nil := rfl theorem map_cons {α : Type u} {β : Type v} {n : ℕ} (f : α → β) (a : α) (v : vector α n) : map f (cons a v) = cons (f a) (map f v) := sorry def map₂ {α : Type u} {β : Type v} {φ : Type w} {n : ℕ} (f : α → β → φ) : vector α n → vector β n → vector φ n := sorry def repeat {α : Type u} (a : α) (n : ℕ) : vector α n := { val := list.repeat a n, property := list.length_repeat a n } def drop {α : Type u} {n : ℕ} (i : ℕ) : vector α n → vector α (n - i) := sorry def take {α : Type u} {n : ℕ} (i : ℕ) : vector α n → vector α (min i n) := sorry def remove_nth {α : Type u} {n : ℕ} (i : fin n) : vector α n → vector α (n - 1) := sorry def of_fn {α : Type u} {n : ℕ} : (fin n → α) → vector α n := sorry def map_accumr {α : Type u} {β : Type v} {n : ℕ} {σ : Type} (f : α → σ → σ × β) : vector α n → σ → σ × vector β n := sorry def map_accumr₂ {n : ℕ} {α : Type} {β : Type} {σ : Type} {φ : Type} (f : α → β → σ → σ × φ) : vector α n → vector β n → σ → σ × vector φ n := sorry protected theorem eq {α : Type u} {n : ℕ} (a1 : vector α n) (a2 : vector α n) : to_list a1 = to_list a2 → a1 = a2 := sorry protected theorem eq_nil {α : Type u} (v : vector α 0) : v = nil := vector.eq v nil (list.eq_nil_of_length_eq_zero (subtype.property v)) @[simp] theorem to_list_mk {α : Type u} {n : ℕ} (v : List α) (P : list.length v = n) : to_list { val := v, property := P } = v := rfl @[simp] theorem to_list_nil {α : Type u} : to_list nil = [] := rfl @[simp] theorem to_list_length {α : Type u} {n : ℕ} (v : vector α n) : list.length (to_list v) = n := subtype.property v @[simp] theorem to_list_cons {α : Type u} {n : ℕ} (a : α) (v : vector α n) : to_list (cons a v) = a :: to_list v := subtype.cases_on v fun (v_val : List α) (v_property : list.length v_val = n) => Eq.refl (to_list (cons a { val := v_val, property := v_property })) @[simp] theorem to_list_append {α : Type u} {n : ℕ} {m : ℕ} (v : vector α n) (w : vector α m) : to_list (append v w) = to_list v ++ to_list w := sorry @[simp] theorem to_list_drop {α : Type u} {n : ℕ} {m : ℕ} (v : vector α m) : to_list (drop n v) = list.drop n (to_list v) := subtype.cases_on v fun (v_val : List α) (v_property : list.length v_val = m) => Eq.refl (to_list (drop n { val := v_val, property := v_property })) @[simp] theorem to_list_take {α : Type u} {n : ℕ} {m : ℕ} (v : vector α m) : to_list (take n v) = list.take n (to_list v) := subtype.cases_on v fun (v_val : List α) (v_property : list.length v_val = m) => Eq.refl (to_list (take n { val := v_val, property := v_property })) end Mathlib
24f8901527ae06567eeeaec40b4bdba9b32bf89f
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/measure_theory/vitali_caratheodory.lean
fbe03b776fba3ae863b6dc28f706802e2c1dc130
[ "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
29,698
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.regular import topology.semicontinuous import measure_theory.bochner_integration import topology.instances.ereal /-! # Vitali-Carathéodory theorem Vitali-Carathéodory theorem asserts the following. Consider an integrable function `f : α → ℝ` on a space with a regular measure. Then there exists a function `g : α → ereal` such that `f x < g x` everywhere, `g` is lower semicontinuous, and the integral of `g` is arbitrarily close to that of `f`. This theorem is proved in this file, as `exists_lt_lower_semicontinuous_integral_lt`. Symmetrically, there exists `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. It follows from the previous statement applied to `-f`. It is formalized under the name `exists_upper_semicontinuous_lt_integral_gt`. The most classical version of Vitali-Carathéodory theorem only ensures a large inequality `f x ≤ g x`. For applications to the fundamental theorem of calculus, though, the strict inequality `f x < g x` is important. Therefore, we prove the stronger version with strict inequalities in this file. There is a price to pay: we require that the measure is `σ`-finite, which is not necessary for the classical Vitali-Carathéodory theorem. Since this is satisfied in all applications, this is not a real problem. ## Sketch of proof Decomposing `f` as the difference of its positive and negative parts, it suffices to show that a positive function can be bounded from above by a lower semicontinuous function, and from below by an upper semicontinuous function, with integrals close to that of `f`. For the bound from above, write `f` as a series `∑' n, cₙ * indicator (sₙ)` of simple functions. Then, approximate `sₙ` by a larger open set `uₙ` with measure very close to that of `sₙ` (this is possible by regularity of the measure), and set `g = ∑' n, cₙ * indicator (uₙ)`. It is lower semicontinuous as a series of lower semicontinuous functions, and its integral is arbitrarily close to that of `f`. For the bound from below, use finitely many terms in the series, and approximate `sₙ` from inside by a closed set `Fₙ`. Then `∑ n < N, cₙ * indicator (Fₙ)` is bounded from above by `f`, it is upper semicontinuous as a finite sum of upper semicontinuous functions, and its integral is arbitrarily close to that of `f`. The main pain point in the implementation is that one needs to jump between the spaces `ℝ`, `ℝ≥0`, `ℝ≥0∞` and `ereal` (and be careful that addition is not well behaved on `ereal`), and between `lintegral` and `integral`. We first show the bound from above for simple functions and the nonnegative integral (this is the main nontrivial mathematical point), then deduce it for general nonnegative functions, first for the nonnegative integral and then for the Bochner integral. Then we follow the same steps for the lower bound. Finally, we glue them together to obtain the main statement `exists_lt_lower_semicontinuous_integral_lt`. ## References [Rudin, *Real and Complex Analysis* (Theorem 2.24)][rudin2006real] -/ open_locale ennreal nnreal open measure_theory measure_theory.measure variables {α : Type*} [topological_space α] [measurable_space α] [borel_space α] (μ : measure α) [weakly_regular μ] namespace measure_theory local infixr ` →ₛ `:25 := simple_func /-! ### Lower semicontinuous upper bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_le_lower_semicontinuous_lintegral_ge : ∀ (f : α →ₛ ℝ≥0) {ε : ℝ≥0∞} (εpos : 0 < ε), ∃ g : α → ℝ≥0, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin refine simple_func.induction _ _, { assume c s hs ε εpos, let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases h : ∫⁻ x, f x ∂μ = ⊤, { refine ⟨λ x, c, λ x, _, lower_semicontinuous_const, by simp only [ennreal.top_add, le_top, h]⟩, simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_self _ _ _ }, by_cases hc : c = 0, { refine ⟨λ x, 0, _, lower_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [lintegral_const, zero_mul, zero_le, ennreal.coe_zero] } }, have : μ s < μ s + ε / c, { have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨εpos.ne', ennreal.coe_ne_top⟩, simpa using (ennreal.add_lt_add_iff_left _).2 this, simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, or_false, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and, restrict_apply] using h }, obtain ⟨u, u_open, su, μu⟩ : ∃ u, is_open u ∧ s ⊆ u ∧ μ u < μ s + ε / c := hs.exists_is_open_lt_of_lt _ this, refine ⟨set.indicator u (λ x, c), λ x, _, u_open.lower_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset su (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ u ≤ c * μ s + ε, by simpa only [hs, u_open.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ u ≤ c * (μ s + ε / c) : ennreal.mul_le_mul (le_refl _) μu.le ... = c * μ s + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { assume f₁ f₂ H h₁ h₂ ε εpos, rcases h₁ (ennreal.half_pos εpos) with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.half_pos εpos) with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end open simple_func (eapprox_diff tsum_eapprox_diff) /-- Given a measurable function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_le_lower_semicontinuous_lintegral_ge (f : α → ℝ≥0∞) (hf : measurable f) {ε : ℝ≥0∞} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases ennreal.exists_pos_sum_of_encodable' εpos ℕ with ⟨δ, δpos, hδ⟩, have : ∀ n, ∃ g : α → ℝ≥0, (∀ x, simple_func.eapprox_diff f n x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, simple_func.eapprox_diff f n x ∂μ + δ n) := λ n, simple_func.exists_le_lower_semicontinuous_lintegral_ge μ (simple_func.eapprox_diff f n) (δpos n), choose g f_le_g gcont hg using this, refine ⟨λ x, (∑' n, g n x), λ x, _, _, _⟩, { rw ← tsum_eapprox_diff f hf, exact ennreal.tsum_le_tsum (λ n, ennreal.coe_le_coe.2 (f_le_g n x)) }, { apply lower_semicontinuous_tsum (λ n, _), exact ennreal.continuous_coe.comp_lower_semicontinuous (gcont n) (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { calc ∫⁻ x, ∑' (n : ℕ), g n x ∂μ = ∑' n, ∫⁻ x, g n x ∂μ : by rw lintegral_tsum (λ n, (gcont n).measurable.coe_nnreal_ennreal) ... ≤ ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ + δ n) : ennreal.tsum_le_tsum hg ... = ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ) + ∑' n, δ n : ennreal.tsum_add ... ≤ ∫⁻ (x : α), f x ∂μ + ε : begin refine add_le_add _ hδ.le, rw [← lintegral_tsum], { simp_rw [tsum_eapprox_diff f hf, le_refl] }, { assume n, exact (simple_func.measurable _).coe_nnreal_ennreal } end } end /-- Given a measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge [sigma_finite μ] (f : α → ℝ≥0) (fmeas : measurable f) {ε : ℝ≥0} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases exists_integrable_pos_of_sigma_finite μ (nnreal.half_pos εpos) with ⟨w, wpos, wmeas, wint⟩, let f' := λ x, ((f x + w x : ℝ≥0) : ℝ≥0∞), rcases exists_le_lower_semicontinuous_lintegral_ge μ f' (fmeas.add wmeas).coe_nnreal_ennreal (ennreal.coe_pos.2 (nnreal.half_pos εpos)) with ⟨g, le_g, gcont, gint⟩, refine ⟨g, λ x, _, gcont, _⟩, { calc (f x : ℝ≥0∞) < f' x : by simpa [← ennreal.coe_lt_coe] using add_lt_add_left (wpos x) (f x) ... ≤ g x : le_g x }, { calc ∫⁻ (x : α), g x ∂μ ≤ ∫⁻ (x : α), f x + w x ∂μ + (ε / 2 : ℝ≥0) : gint ... = ∫⁻ (x : α), f x ∂ μ + ∫⁻ (x : α), w x ∂ μ + (ε / 2 : ℝ≥0) : by rw lintegral_add fmeas.coe_nnreal_ennreal wmeas.coe_nnreal_ennreal ... ≤ ∫⁻ (x : α), f x ∂ μ + (ε / 2 : ℝ≥0) + (ε / 2 : ℝ≥0) : add_le_add_right (add_le_add_left wint.le _) _ ... = ∫⁻ (x : α), f x ∂μ + ε : by rw [add_assoc, ← ennreal.coe_add, nnreal.add_halves] }, end /-- Given an almost everywhere measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable [sigma_finite μ] (f : α → ℝ≥0) (fmeas : ae_measurable f μ) {ε : ℝ≥0} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_lintegral_ge μ (fmeas.mk f) fmeas.measurable_mk (nnreal.half_pos εpos) with ⟨g0, f_lt_g0, g0_cont, g0_int⟩, rcases exists_measurable_superset_of_null fmeas.ae_eq_mk with ⟨s, hs, smeas, μs⟩, rcases exists_le_lower_semicontinuous_lintegral_ge μ (s.indicator (λ x, ∞)) (measurable_const.indicator smeas) (ennreal.half_pos (ennreal.coe_pos.2 εpos)) with ⟨g1, le_g1, g1_cont, g1_int⟩, refine ⟨λ x, g0 x + g1 x, λ x, _, g0_cont.add g1_cont, _⟩, { by_cases h : x ∈ s, { have := le_g1 x, simp only [h, set.indicator_of_mem, top_le_iff] at this, simp [this] }, { have : f x = fmeas.mk f x, by { rw set.compl_subset_comm at hs, exact hs h }, rw this, exact (f_lt_g0 x).trans_le le_self_add } }, { calc ∫⁻ x, g0 x + g1 x ∂μ = ∫⁻ x, g0 x ∂μ + ∫⁻ x, g1 x ∂μ : lintegral_add g0_cont.measurable g1_cont.measurable ... ≤ (∫⁻ x, f x ∂μ + ε / 2) + (0 + ε / 2) : begin refine add_le_add _ _, { convert g0_int using 2, { exact lintegral_congr_ae (fmeas.ae_eq_mk.fun_comp _) }, { simp only [ennreal.coe_div, ennreal.coe_one, ennreal.coe_bit0, ne.def, not_false_iff, bit0_eq_zero, one_ne_zero], } }, { convert g1_int, simp only [smeas, μs, lintegral_const, set.univ_inter, measurable_set.univ, lintegral_indicator, mul_zero, restrict_apply] } end ... = ∫⁻ x, f x ∂μ + ε : by simp only [add_assoc, ennreal.add_halves, zero_add] } end variable {μ} /-- Given an integrable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_integral_gt_nnreal [sigma_finite μ] (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (integrable (λ x, (g x).to_real) μ) ∧ (∫ x, (g x).to_real ∂μ < ∫ x, f x ∂μ + ε) := begin have fmeas : ae_measurable f μ, by { convert fint.ae_measurable.real_to_nnreal, ext1 x, simp only [real.to_nnreal_coe] }, let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, have int_f_lt_top : ∫⁻ (a : α), (f a) ∂μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable μ f fmeas δpos with ⟨g, f_lt_g, gcont, gint⟩, have gint_lt : ∫⁻ (x : α), g x ∂μ < ∞ := gint.trans_lt (by simpa using int_f_lt_top), have g_lt_top : ∀ᵐ (x : α) ∂μ, g x < ∞ := ae_lt_top gcont.measurable gint_lt, have Ig : ∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ = ∫⁻ (a : α), g a ∂μ, { apply lintegral_congr_ae, filter_upwards [g_lt_top], assume x hx, simp only [hx.ne, ennreal.of_real_to_real, ne.def, not_false_iff] }, refine ⟨g, f_lt_g, gcont, g_lt_top, _, _⟩, { refine ⟨gcont.measurable.ennreal_to_real.ae_measurable, _⟩, simp [has_finite_integral_iff_norm, real.norm_eq_abs, abs_of_nonneg], convert gint_lt using 1 }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { calc ennreal.to_real (∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ) = ennreal.to_real (∫⁻ (a : α), g a ∂μ) : by congr' 1 ... ≤ ennreal.to_real (∫⁻ (a : α), f a ∂μ + δ) : begin apply ennreal.to_real_mono _ gint, simpa using int_f_lt_top.ne, end ... = ennreal.to_real (∫⁻ (a : α), f a ∂μ) + δ : by rw [ennreal.to_real_add int_f_lt_top.ne ennreal.coe_ne_top, ennreal.coe_to_real] ... < ennreal.to_real (∫⁻ (a : α), f a ∂μ) + ε : add_lt_add_left (by simp [δ, half_lt_self εpos]) _ ... = (∫⁻ (a : α), ennreal.of_real ↑(f a) ∂μ).to_real + ε : by simp }, { apply filter.eventually_of_forall (λ x, _), simp }, { exact fmeas.coe_nnreal_real, }, { apply filter.eventually_of_forall (λ x, _), simp }, { apply gcont.measurable.ennreal_to_real.ae_measurable } } end /-! ### Upper semicontinuous lower bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_upper_semicontinuous_le_lintegral_le : ∀ (f : α →ₛ ℝ≥0) (int_f : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (εpos : 0 < ε), ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin refine simple_func.induction _ _, { assume c s hs int_f ε εpos, let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases hc : c = 0, { refine ⟨λ x, 0, _, upper_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [hc, set.indicator_zero', lintegral_const, zero_mul, pi.zero_apply, simple_func.const_zero, zero_add, zero_le', simple_func.coe_zero, set.piecewise_eq_indicator, ennreal.coe_zero, simple_func.coe_piecewise, εpos.le] } }, have μs_lt_top : μ s < ∞, by simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, or_false, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, restrict_apply measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, function.const_apply, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and] using int_f, have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨εpos.ne', ennreal.coe_ne_top⟩, obtain ⟨F, F_closed, Fs, μF⟩ : ∃ F, is_closed F ∧ F ⊆ s ∧ μ s < μ F + ε / c := hs.exists_lt_is_closed_of_lt_top_of_pos μs_lt_top this, refine ⟨set.indicator F (λ x, c), λ x, _, F_closed.upper_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset Fs (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ s ≤ c * μ F + ε, by simpa only [hs, F_closed.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ s ≤ c * (μ F + ε / c) : ennreal.mul_le_mul (le_refl _) μF.le ... = c * μ F + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { assume f₁ f₂ H h₁ h₂ f_int ε εpos, have A : ∫⁻ (x : α), f₁ x ∂μ + ∫⁻ (x : α), f₂ x ∂μ < ⊤, { rw ← lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, simpa only [simple_func.coe_add, ennreal.coe_add, pi.add_apply] using f_int }, rcases h₁ (ennreal.add_lt_top.1 A).1 (ennreal.half_pos εpos) with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.add_lt_top.1 A).2 (ennreal.half_pos εpos) with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_lintegral_le (f : α → ℝ≥0) (int_f : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin obtain ⟨fs, fs_le_f, int_fs⟩ : ∃ (fs : α →ₛ ℝ≥0), (∀ x, fs x ≤ f x) ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε/2) := begin have := ennreal.lt_add_right int_f (ennreal.half_pos εpos), conv_rhs at this { rw lintegral_eq_nnreal (λ x, (f x : ℝ≥0∞)) μ }, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp only [lt_supr_iff] at this, rcases this with ⟨fs, fs_le_f, int_fs⟩, refine ⟨fs, λ x, by simpa only [ennreal.coe_le_coe] using fs_le_f x, _⟩, convert int_fs.le, rw ← simple_func.lintegral_eq_lintegral, refl end, have int_fs_lt_top : ∫⁻ x, fs x ∂μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) int_f, simpa only [ennreal.coe_le_coe] using fs_le_f x }, obtain ⟨g, g_le_fs, gcont, gint⟩ : ∃ g : α → ℝ≥0, (∀ x, g x ≤ fs x) ∧ upper_semicontinuous g ∧ (∫⁻ x, fs x ∂μ ≤ ∫⁻ x, g x ∂μ + ε/2) := fs.exists_upper_semicontinuous_le_lintegral_le int_fs_lt_top (ennreal.half_pos εpos), refine ⟨g, λ x, (g_le_fs x).trans (fs_le_f x), gcont, _⟩, calc ∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε / 2 : int_fs ... ≤ (∫⁻ x, g x ∂μ + ε / 2) + ε / 2 : add_le_add gint (le_refl _) ... = ∫⁻ x, g x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_integral_le (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, (g x : ℝ)) μ) ∧ (∫ x, (f x : ℝ) ∂μ - ε ≤ ∫ x, g x ∂μ) := begin let δ : ℝ≥0 := ⟨ε, εpos.le⟩, have δpos : (0 : ℝ≥0∞) < δ := ennreal.coe_lt_coe.2 εpos, have If : ∫⁻ x, f x ∂ μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_upper_semicontinuous_le_lintegral_le f If δpos with ⟨g, gf, gcont, gint⟩, have Ig : ∫⁻ x, g x ∂ μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) If, simpa using gf x }, refine ⟨g, gf, gcont, _, _⟩, { refine integrable.mono fint gcont.measurable.coe_nnreal_real.ae_measurable _, exact filter.eventually_of_forall (λ x, by simp [gf x]) }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { rw sub_le_iff_le_add, convert ennreal.to_real_mono _ gint, { simp, }, { rw ennreal.to_real_add Ig.ne ennreal.coe_ne_top, simp }, { simpa using Ig.ne } }, { apply filter.eventually_of_forall, simp }, { exact gcont.measurable.coe_nnreal_real.ae_measurable }, { apply filter.eventually_of_forall, simp }, { exact fint.ae_measurable } } end /-! ### Vitali-Carathéodory theorem -/ /-- Vitali-Carathéodory theorem: given an integrable real function `f`, there exists an integrable function `g > f` which is lower semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_lt_lower_semicontinuous_integral_lt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (f x : ereal) < g x) ∧ lower_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (∫ x, ereal.to_real (g x) ∂μ < ∫ x, f x ∂μ + ε) := begin let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, let fp : α → ℝ≥0 := λ x, real.to_nnreal (f x), have int_fp : integrable (λ x, (fp x : ℝ)) μ := hf.real_to_nnreal, rcases exists_lt_lower_semicontinuous_integral_gt_nnreal fp int_fp δpos with ⟨gp, fp_lt_gp, gpcont, gp_lt_top, gp_integrable, gpint⟩, let fm : α → ℝ≥0 := λ x, real.to_nnreal (-f x), have int_fm : integrable (λ x, (fm x : ℝ)) μ := hf.neg.real_to_nnreal, rcases exists_upper_semicontinuous_le_integral_le fm int_fm δpos with ⟨gm, gm_le_fm, gmcont, gm_integrable, gmint⟩, let g : α → ereal := λ x, (gp x : ereal) - (gm x), have ae_g : ∀ᵐ x ∂ μ, (g x).to_real = (gp x : ereal).to_real - (gm x : ereal).to_real, { filter_upwards [gp_lt_top], assume x hx, rw ereal.to_real_sub; simp [hx.ne] }, refine ⟨g, _, _, _, _, _⟩, show integrable (λ x, ereal.to_real (g x)) μ, { rw integrable_congr ae_g, convert gp_integrable.sub gm_integrable, ext x, simp }, show ∫ (x : α), (g x).to_real ∂μ < ∫ (x : α), f x ∂μ + ε, from calc ∫ (x : α), (g x).to_real ∂μ = ∫ (x : α), ereal.to_real (gp x) - ereal.to_real (gm x) ∂μ : integral_congr_ae ae_g ... = ∫ (x : α), ereal.to_real (gp x) ∂ μ - ∫ (x : α), gm x ∂μ : begin simp only [ereal.to_real_coe_ennreal, ennreal.coe_to_real, coe_coe], exact integral_sub gp_integrable gm_integrable, end ... < ∫ (x : α), ↑(fp x) ∂μ + ↑δ - ∫ (x : α), gm x ∂μ : begin apply sub_lt_sub_right, convert gpint, simp only [ereal.to_real_coe_ennreal], end ... ≤ ∫ (x : α), ↑(fp x) ∂μ + ↑δ - (∫ (x : α), fm x ∂μ - δ) : sub_le_sub_left gmint _ ... = ∫ (x : α), f x ∂μ + 2 * δ : by { simp_rw [integral_eq_integral_pos_part_sub_integral_neg_part hf, fp, fm], ring } ... = ∫ (x : α), f x ∂μ + ε : by { congr' 1, field_simp [δ, mul_comm] }, show ∀ᵐ (x : α) ∂μ, g x < ⊤, { filter_upwards [gp_lt_top], assume x hx, simp [g, ereal.sub_eq_add_neg, lt_top_iff_ne_top, lt_top_iff_ne_top.1 hx] }, show ∀ x, (f x : ereal) < g x, { assume x, rw ereal.coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (f x), refine ereal.sub_lt_sub_of_lt_of_le _ _ _ _, { simp only [ereal.coe_ennreal_lt_coe_ennreal_iff, coe_coe], exact (fp_lt_gp x) }, { simp only [ennreal.coe_le_coe, ereal.coe_ennreal_le_coe_ennreal_iff, coe_coe], exact (gm_le_fm x) }, { simp only [ereal.coe_ennreal_ne_bot, ne.def, not_false_iff, coe_coe] }, { simp only [ereal.coe_nnreal_ne_top, ne.def, not_false_iff, coe_coe] } }, show lower_semicontinuous g, { apply lower_semicontinuous.add', { exact continuous_coe_ennreal_ereal.comp_lower_semicontinuous gpcont (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy) }, { apply ereal.continuous_neg.comp_upper_semicontinuous_antimono _ (λ x y hxy, ereal.neg_le_neg_iff.2 hxy), dsimp, apply continuous_coe_ennreal_ereal.comp_upper_semicontinuous _ (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy), exact ennreal.continuous_coe.comp_upper_semicontinuous gmcont (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { assume x, exact ereal.continuous_at_add (by simp) (by simp) } } end /-- Vitali-Carathéodory theorem: given an integrable real function `f`, there exists an integrable function `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_upper_semicontinuous_lt_integral_gt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (g x : ereal) < f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂μ, ⊥ < g x) ∧ (∫ x, f x ∂μ < ∫ x, ereal.to_real (g x) ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_integral_lt (λ x, - f x) hf.neg εpos with ⟨g, g_lt_f, gcont, g_integrable, g_lt_top, gint⟩, refine ⟨λ x, - g x, _, _, _, _, _⟩, { exact λ x, ereal.neg_lt_iff_neg_lt.1 (by simpa only [ereal.coe_neg] using g_lt_f x) }, { exact ereal.continuous_neg.comp_lower_semicontinuous_antimono gcont (λ x y hxy, ereal.neg_le_neg_iff.2 hxy) }, { convert g_integrable.neg, ext x, simp }, { simpa [bot_lt_iff_ne_bot, lt_top_iff_ne_top] using g_lt_top }, { simp_rw [integral_neg, lt_neg_add_iff_add_lt] at gint, rw add_comm at gint, simpa [integral_neg] using gint } end end measure_theory
0d88d1f57a52524ef90166daa2e32eac382ecc2b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group_power/lemmas.lean
78ba286c992136462877e5581f5d9cd9b64f9e94
[ "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
34,887
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.invertible import data.int.cast /-! # Lemmas about power operations on monoids and groups This file contains lemmas about `monoid.pow`, `group.pow`, `nsmul`, `zsmul` which require additional imports besides those available in `algebra.group_power.basic`. -/ universes u v w x y z u₁ u₂ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} /-! ### (Additive) monoid -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem nsmul_one [has_one A] : ∀ n : ℕ, n • (1 : A) = n := begin refine eq_nat_cast' (⟨_, _, _⟩ : ℕ →+ A) _, { simp [zero_nsmul] }, { simp [add_nsmul] }, { simp } end @[simp, norm_cast, to_additive] lemma units.coe_pow (u : Mˣ) (n : ℕ) : ((u ^ n : Mˣ) : M) = u ^ n := (units.coe_hom M).map_pow u n instance invertible_pow (m : M) [invertible m] (n : ℕ) : invertible (m ^ n) := { inv_of := ⅟ m ^ n, inv_of_mul_self := by rw [← (commute_inv_of m).symm.mul_pow, inv_of_mul_self, one_pow], mul_inv_of_self := by rw [← (commute_inv_of m).mul_pow, mul_inv_of_self, one_pow] } lemma inv_of_pow (m : M) [invertible m] (n : ℕ) [invertible (m ^ n)] : ⅟(m ^ n) = ⅟m ^ n := @invertible_unique M _ (m ^ n) (m ^ n) rfl ‹_› (invertible_pow m n) lemma is_unit.pow {m : M} (n : ℕ) : is_unit m → is_unit (m ^ n) := λ ⟨u, hu⟩, ⟨u ^ n, by simp *⟩ @[simp] lemma is_unit_pow_succ_iff {m : M} {n : ℕ} : is_unit (m ^ (n + 1)) ↔ is_unit m := begin refine ⟨_, λ h, h.pow _⟩, rw [pow_succ, ((commute.refl _).pow_right _).is_unit_mul_iff], exact and.left end lemma is_unit_pos_pow_iff {m : M} : ∀ {n : ℕ} (h : 0 < n), is_unit (m ^ n) ↔ is_unit m | (n + 1) _ := is_unit_pow_succ_iff /-- If `x ^ n.succ = 1` then `x` has an inverse, `x^n`. -/ def invertible_of_pow_succ_eq_one (x : M) (n : ℕ) (hx : x ^ n.succ = 1) : invertible x := ⟨x ^ n, (pow_succ' x n).symm.trans hx, (pow_succ x n).symm.trans hx⟩ /-- If `x ^ n = 1` then `x` has an inverse, `x^(n - 1)`. -/ def invertible_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) : invertible x := begin apply invertible_of_pow_succ_eq_one x (n - 1), convert hx, exact tsub_add_cancel_of_le (nat.succ_le_of_lt hn), end lemma is_unit_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) : is_unit x := begin haveI := invertible_of_pow_eq_one x n hx hn, exact is_unit_of_invertible x end lemma smul_pow [mul_action M N] [is_scalar_tower M N N] [smul_comm_class M N N] (k : M) (x : N) (p : ℕ) : (k • x) ^ p = k ^ p • x ^ p := begin induction p with p IH, { simp }, { rw [pow_succ', IH, smul_mul_smul, ←pow_succ', ←pow_succ'] } end @[simp] lemma smul_pow' [mul_distrib_mul_action M N] (x : M) (m : N) (n : ℕ) : x • m ^ n = (x • m) ^ n := begin induction n with n ih, { rw [pow_zero, pow_zero], exact smul_one x }, { rw [pow_succ, pow_succ], exact (smul_mul' x m (m ^ n)).trans (congr_arg _ ih) } end end monoid section group variables [group G] [group H] [add_group A] [add_group B] open int local attribute [ematch] le_of_lt open nat theorem zsmul_one [has_one A] (n : ℤ) : n • (1 : A) = n := by cases n; simp @[to_additive add_one_zsmul] lemma zpow_add_one (a : G) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a | (of_nat n) := by simp [← int.coe_nat_succ, pow_succ'] | -[1+0] := by simp [int.neg_succ_of_nat_eq] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, zpow_neg, neg_add, neg_add_cancel_right, zpow_neg, ← int.coe_nat_succ, zpow_coe_nat, zpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev, inv_mul_cancel_right] @[to_additive zsmul_sub_one] lemma zpow_sub_one (a : G) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : (mul_inv_cancel_right _ _).symm ... = a^n * a⁻¹ : by rw [← zpow_add_one, sub_add_cancel] @[to_additive add_zsmul] lemma zpow_add (a : G) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, zpow_add_one, ihn, mul_assoc] }, { rw [zpow_sub_one, ← mul_assoc, ← ihn, ← zpow_sub_one, add_sub_assoc] } end @[to_additive add_zsmul_self] lemma mul_self_zpow (b : G) (m : ℤ) : b*b^m = b^(m+1) := by { conv_lhs {congr, rw ← zpow_one b }, rw [← zpow_add, add_comm] } @[to_additive add_self_zsmul] lemma mul_zpow_self (b : G) (m : ℤ) : b^m*b = b^(m+1) := by { conv_lhs {congr, skip, rw ← zpow_one b }, rw [← zpow_add, add_comm] } @[to_additive sub_zsmul] lemma zpow_sub (a : G) (m n : ℤ) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := by rw [sub_eq_add_neg, zpow_add, zpow_neg] @[to_additive one_add_zsmul] theorem zpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [zpow_add, zpow_one] @[to_additive] theorem zpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← zpow_add, ← zpow_add, add_comm] -- note that `mul_zsmul` and `zpow_mul` have the primes swapped since their argument order -- and therefore the more "natural" choice of lemma is reversed. @[to_additive mul_zsmul'] theorem zpow_mul (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ m) ^ n := int.induction_on n (by simp) (λ n ihn, by simp [mul_add, zpow_add, ihn]) (λ n ihn, by simp only [mul_sub, zpow_sub, ihn, mul_one, zpow_one]) @[to_additive mul_zsmul] theorem zpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, zpow_mul] @[to_additive bit0_zsmul] theorem zpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := zpow_add _ _ _ @[to_additive bit1_zsmul] theorem zpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, zpow_add, zpow_bit0, zpow_one] @[simp, norm_cast, to_additive] lemma units.coe_zpow (u : Gˣ) (n : ℤ) : ((u ^ n : Gˣ) : G) = u ^ n := (units.coe_hom G).map_zpow u n end group section ordered_add_comm_group variables [ordered_add_comm_group A] /-! Lemmas about `zsmul` under ordering, placed here (rather than in `algebra.group_power.order` with their friends) because they require facts from `data.int.basic`-/ open int lemma zsmul_pos {a : A} (ha : 0 < a) {k : ℤ} (hk : (0:ℤ) < k) : 0 < k • a := begin lift k to ℕ using int.le_of_lt hk, rw coe_nat_zsmul, apply nsmul_pos ha, exact (coe_nat_pos.mp hk).ne', end theorem zsmul_strict_mono_left {a : A} (ha : 0 < a) : strict_mono (λ n : ℤ, n • a) := λ n m h, calc n • a = n • a + 0 : (add_zero _).symm ... < n • a + (m - n) • a : add_lt_add_left (zsmul_pos ha (sub_pos.mpr h)) _ ... = m • a : by { rw [← add_zsmul], simp } theorem zsmul_mono_left {a : A} (ha : 0 ≤ a) : monotone (λ n : ℤ, n • a) := λ n m h, calc n • a = n • a + 0 : (add_zero _).symm ... ≤ n • a + (m - n) • a : add_le_add_left (zsmul_nonneg ha (sub_nonneg.mpr h)) _ ... = m • a : by { rw [← add_zsmul], simp } theorem zsmul_le_zsmul {a : A} {n m : ℤ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a := zsmul_mono_left ha h theorem zsmul_lt_zsmul {a : A} {n m : ℤ} (ha : 0 < a) (h : n < m) : n • a < m • a := zsmul_strict_mono_left ha h theorem zsmul_le_zsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n • a ≤ m • a ↔ n ≤ m := (zsmul_strict_mono_left ha).le_iff_le theorem zsmul_lt_zsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n • a < m • a ↔ n < m := (zsmul_strict_mono_left ha).lt_iff_lt variables (A) lemma zsmul_strict_mono_right {n : ℤ} (hn : 0 < n) : strict_mono ((•) n : A → A) := λ a b hab, begin rw ← sub_pos at hab, rw [← sub_pos, ← zsmul_sub], exact zsmul_pos hab hn, end lemma zsmul_mono_right {n : ℤ} (hn : 0 ≤ n) : monotone ((•) n : A → A) := λ a b hab, begin rw ← sub_nonneg at hab, rw [← sub_nonneg, ← zsmul_sub], exact zsmul_nonneg hab hn, end variables {A} theorem zsmul_le_zsmul' {n : ℤ} (hn : 0 ≤ n) {a₁ a₂ : A} (h : a₁ ≤ a₂) : n • a₁ ≤ n • a₂ := zsmul_mono_right A hn h theorem zsmul_lt_zsmul' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} (h : a₁ < a₂) : n • a₁ < n • a₂ := zsmul_strict_mono_right A hn h lemma abs_nsmul {α : Type*} [linear_ordered_add_comm_group α] (n : ℕ) (a : α) : |n • a| = n • |a| := begin cases le_total a 0 with hneg hpos, { rw [abs_of_nonpos hneg, ← abs_neg, ← neg_nsmul, abs_of_nonneg], exact nsmul_nonneg (neg_nonneg.mpr hneg) n }, { rw [abs_of_nonneg hpos, abs_of_nonneg], exact nsmul_nonneg hpos n } end lemma abs_zsmul {α : Type*} [linear_ordered_add_comm_group α] (n : ℤ) (a : α) : |n • a| = |n| • |a| := begin by_cases n0 : 0 ≤ n, { lift n to ℕ using n0, simp only [abs_nsmul, coe_nat_abs, coe_nat_zsmul] }, { lift (- n) to ℕ using int.le_of_lt (neg_pos.mpr (not_le.mp n0)) with m h, rw [← abs_neg (n • a), ← neg_zsmul, ← abs_neg n, ← h, coe_nat_zsmul, coe_nat_abs, coe_nat_zsmul], exact abs_nsmul m _ }, end lemma abs_add_eq_add_abs_le {α : Type*} [linear_ordered_add_comm_group α] {a b : α} (hle : a ≤ b) : |a + b| = |a| + |b| ↔ (0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0) := begin by_cases a0 : 0 ≤ a; by_cases b0 : 0 ≤ b, { simp [a0, b0, abs_of_nonneg, add_nonneg a0 b0] }, { exact (lt_irrefl (0 : α) (a0.trans_lt (hle.trans_lt (not_le.mp b0)))).elim }, any_goals { simp [(not_le.mp a0).le, (not_le.mp b0).le, abs_of_nonpos, add_nonpos, add_comm] }, obtain F := (not_le.mp a0), have : (|a + b| = -a + b ↔ b ≤ 0) ↔ (|a + b| = |a| + |b| ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0), { simp [a0, b0, abs_of_neg, abs_of_nonneg, F, F.le] }, refine this.mp ⟨λ h, _, λ h, by simp only [le_antisymm h b0, abs_of_neg F, add_zero]⟩, by_cases ba : a + b ≤ 0, { refine le_of_eq (eq_zero_of_neg_eq _), rwa [abs_of_nonpos ba, neg_add_rev, add_comm, add_right_inj] at h }, { refine (lt_irrefl (0 : α) _).elim, rw [abs_of_pos (not_le.mp ba), add_left_inj] at h, rwa eq_zero_of_neg_eq h.symm at F } end lemma abs_add_eq_add_abs_iff {α : Type*} [linear_ordered_add_comm_group α] (a b : α) : |a + b| = |a| + |b| ↔ (0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0) := begin by_cases ab : a ≤ b, { exact abs_add_eq_add_abs_le ab }, { rw [add_comm a, add_comm (abs _), abs_add_eq_add_abs_le ((not_le.mp ab).le), and.comm, @and.comm (b ≤ 0 ) _] } end end ordered_add_comm_group section linear_ordered_add_comm_group variable [linear_ordered_add_comm_group A] theorem zsmul_le_zsmul_iff' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} : n • a₁ ≤ n • a₂ ↔ a₁ ≤ a₂ := (zsmul_strict_mono_right A hn).le_iff_le theorem zsmul_lt_zsmul_iff' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} : n • a₁ < n • a₂ ↔ a₁ < a₂ := (zsmul_strict_mono_right A hn).lt_iff_lt theorem nsmul_le_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n • a ≤ m • a ↔ n ≤ m := begin refine ⟨λ h, _, nsmul_le_nsmul $ le_of_lt ha⟩, by_contra H, exact lt_irrefl _ (lt_of_lt_of_le (nsmul_lt_nsmul ha (not_le.mp H)) h) end theorem nsmul_lt_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n • a < m • a ↔ n < m := begin refine ⟨λ h, _, nsmul_lt_nsmul ha⟩, by_contra H, exact lt_irrefl _ (lt_of_le_of_lt (nsmul_le_nsmul (le_of_lt ha) $ not_lt.mp H) h) end /-- See also `smul_right_injective`. TODO: provide a `no_zero_smul_divisors` instance. We can't do that here because importing that definition would create import cycles. -/ lemma zsmul_right_injective {m : ℤ} (hm : m ≠ 0) : function.injective ((•) m : A → A) := begin cases hm.symm.lt_or_lt, { exact (zsmul_strict_mono_right A h).injective, }, { intros a b hab, refine (zsmul_strict_mono_right A (neg_pos.mpr h)).injective _, rw [neg_zsmul, neg_zsmul, hab], }, end lemma zsmul_right_inj {a b : A} {m : ℤ} (hm : m ≠ 0) : m • a = m • b ↔ a = b := (zsmul_right_injective hm).eq_iff /-- Alias of `zsmul_right_inj`, for ease of discovery alongside `zsmul_le_zsmul_iff'` and `zsmul_lt_zsmul_iff'`. -/ lemma zsmul_eq_zsmul_iff' {a b : A} {m : ℤ} (hm : m ≠ 0) : m • a = m • b ↔ a = b := zsmul_right_inj hm end linear_ordered_add_comm_group @[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : ℕ) : ((n • a : A) : with_bot A) = n • a := add_monoid_hom.map_nsmul ⟨(coe : A → with_bot A), with_bot.coe_zero, with_bot.coe_add⟩ a n theorem nsmul_eq_mul' [semiring R] (a : R) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero], rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]] @[simp] theorem nsmul_eq_mul [semiring R] (n : ℕ) (a : R) : n • a = n * a := by rw [nsmul_eq_mul', (n.cast_commute a).eq] theorem mul_nsmul_left [semiring R] (a b : R) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc] theorem mul_nsmul_assoc [semiring R] (a b : R) (n : ℕ) : n • (a * b) = n • a * b := by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc] @[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := begin induction m with m ih, { rw [pow_zero, pow_zero], exact nat.cast_one }, { rw [pow_succ', pow_succ', nat.cast_mul, ih] } end @[simp, norm_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [pow_succ', pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, pow_succ', ih]] -- The next four lemmas allow us to replace multiplication by a numeral with a `zsmul` expression. -- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`. lemma bit0_mul [ring R] {n r : R} : bit0 n * r = (2 : ℤ) • (n * r) := by { dsimp [bit0], rw [add_mul, add_zsmul, one_zsmul], } lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = (2 : ℤ) • (r * n) := by { dsimp [bit0], rw [mul_add, add_zsmul, one_zsmul], } lemma bit1_mul [ring R] {n r : R} : bit1 n * r = (2 : ℤ) • (n * r) + r := by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], } lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = (2 : ℤ) • (r * n) + r := by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], } @[simp] theorem zsmul_eq_mul [ring R] (a : R) : ∀ (n : ℤ), n • a = n * a | (n : ℕ) := by { rw [coe_nat_zsmul, nsmul_eq_mul], refl } | -[1+ n] := by simp [nat.cast_succ, neg_add_rev, int.cast_neg_succ_of_nat, add_mul] theorem zsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n • a = a * n := by rw [zsmul_eq_mul, (n.cast_commute a).eq] theorem mul_zsmul_left [ring R] (a b : R) (n : ℤ) : n • (a * b) = a * (n • b) := by rw [zsmul_eq_mul', zsmul_eq_mul', mul_assoc] theorem mul_zsmul_assoc [ring R] (a b : R) (n : ℤ) : n • (a * b) = n • a * b := by rw [zsmul_eq_mul, zsmul_eq_mul, mul_assoc] lemma zsmul_int_int (a b : ℤ) : a • b = a * b := by simp lemma zsmul_int_one (n : ℤ) : n • 1 = n := by simp @[simp, norm_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := begin induction m with m ih, { rw [pow_zero, pow_zero, int.cast_one] }, { rw [pow_succ, pow_succ, int.cast_mul, ih] } end lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = (-1) ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [sq] section ordered_semiring variables [ordered_semiring R] {a : R} /-- Bernoulli's inequality. This version works for semirings but requires additional hypotheses `0 ≤ a * a` and `0 ≤ (1 + a) * (1 + a)`. -/ theorem one_add_mul_le_pow' (Hsq : 0 ≤ a * a) (Hsq' : 0 ≤ (1 + a) * (1 + a)) (H : 0 ≤ 2 + a) : ∀ (n : ℕ), 1 + (n : R) * a ≤ (1 + a) ^ n | 0 := by simp | 1 := by simp | (n+2) := have 0 ≤ (n : R) * (a * a * (2 + a)) + a * a, from add_nonneg (mul_nonneg n.cast_nonneg (mul_nonneg Hsq H)) Hsq, calc 1 + (↑(n + 2) : R) * a ≤ 1 + ↑(n + 2) * a + (n * (a * a * (2 + a)) + a * a) : (le_add_iff_nonneg_right _).2 this ... = (1 + a) * (1 + a) * (1 + n * a) : by { simp [add_mul, mul_add, bit0, mul_assoc, (n.cast_commute (_ : R)).left_comm], ac_refl } ... ≤ (1 + a) * (1 + a) * (1 + a)^n : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) Hsq' ... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc] private lemma pow_le_pow_of_le_one_aux (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by { rw [←add_assoc, ←one_mul (a^i), pow_succ], exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one } lemma pow_le_pow_of_le_one (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_of_le_one (h₀ : 0 ≤ a) (h₁ : a ≤ 1) {n : ℕ} (hn : n ≠ 0) : a ^ n ≤ a := (pow_one a).subst (pow_le_pow_of_le_one h₀ h₁ (nat.pos_of_ne_zero hn)) lemma sq_le (h₀ : 0 ≤ a) (h₁ : a ≤ 1) : a ^ 2 ≤ a := pow_le_of_le_one h₀ h₁ two_ne_zero end ordered_semiring section linear_ordered_semiring variables [linear_ordered_semiring R] lemma sign_cases_of_C_mul_pow_nonneg {C r : R} (h : ∀ n : ℕ, 0 ≤ C * r ^ n) : C = 0 ∨ (0 < C ∧ 0 ≤ r) := begin have : 0 ≤ C, by simpa only [pow_zero, mul_one] using h 0, refine this.eq_or_lt.elim (λ h, or.inl h.symm) (λ hC, or.inr ⟨hC, _⟩), refine nonneg_of_mul_nonneg_left _ hC, simpa only [pow_one] using h 1 end end linear_ordered_semiring section linear_ordered_ring variables [linear_ordered_ring R] {a : R} {n : ℕ} @[simp] lemma abs_pow (a : R) (n : ℕ) : |a ^ n| = |a| ^ n := (pow_abs a n).symm @[simp] theorem pow_bit1_neg_iff : a ^ bit1 n < 0 ↔ a < 0 := ⟨λ h, not_le.1 $ λ h', not_le.2 h $ pow_nonneg h' _, λ ha, pow_bit1_neg ha n⟩ @[simp] theorem pow_bit1_nonneg_iff : 0 ≤ a ^ bit1 n ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 pow_bit1_neg_iff @[simp] theorem pow_bit1_nonpos_iff : a ^ bit1 n ≤ 0 ↔ a ≤ 0 := by simp only [le_iff_lt_or_eq, pow_bit1_neg_iff, pow_eq_zero_iff (bit1_pos (zero_le n))] @[simp] theorem pow_bit1_pos_iff : 0 < a ^ bit1 n ↔ 0 < a := lt_iff_lt_of_le_iff_le pow_bit1_nonpos_iff lemma even.pow_nonneg (hn : even n) (a : R) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_nonneg a k lemma even.pow_pos (hn : even n) (ha : a ≠ 0) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_pos ha k lemma odd.pow_nonpos (hn : odd n) (ha : a ≤ 0) : a ^ n ≤ 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_nonpos_iff.mpr ha lemma odd.pow_neg (hn : odd n) (ha : a < 0) : a ^ n < 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_neg_iff.mpr ha lemma odd.pow_nonneg_iff (hn : odd n) : 0 ≤ a ^ n ↔ 0 ≤ a := ⟨λ h, le_of_not_lt (λ ha, h.not_lt $ hn.pow_neg ha), λ ha, pow_nonneg ha n⟩ lemma odd.pow_nonpos_iff (hn : odd n) : a ^ n ≤ 0 ↔ a ≤ 0 := ⟨λ h, le_of_not_lt (λ ha, h.not_lt $ pow_pos ha _), hn.pow_nonpos⟩ lemma odd.pow_pos_iff (hn : odd n) : 0 < a ^ n ↔ 0 < a := ⟨λ h, lt_of_not_ge' (λ ha, h.not_le $ hn.pow_nonpos ha), λ ha, pow_pos ha n⟩ lemma odd.pow_neg_iff (hn : odd n) : a ^ n < 0 ↔ a < 0 := ⟨λ h, lt_of_not_ge' (λ ha, h.not_le $ pow_nonneg ha _), hn.pow_neg⟩ lemma even.pow_pos_iff (hn : even n) (h₀ : 0 < n) : 0 < a ^ n ↔ a ≠ 0 := ⟨λ h ha, by { rw [ha, zero_pow h₀] at h, exact lt_irrefl 0 h }, hn.pow_pos⟩ lemma even.pow_abs {p : ℕ} (hp : even p) (a : R) : |a| ^ p = a ^ p := begin rw [←abs_pow, abs_eq_self], exact hp.pow_nonneg _ end @[simp] lemma pow_bit0_abs (a : R) (p : ℕ) : |a| ^ bit0 p = a ^ bit0 p := (even_bit0 _).pow_abs _ lemma strict_mono_pow_bit1 (n : ℕ) : strict_mono (λ a : R, a ^ bit1 n) := begin intros a b hab, cases le_total a 0 with ha ha, { cases le_or_lt b 0 with hb hb, { rw [← neg_lt_neg_iff, ← neg_pow_bit1, ← neg_pow_bit1], exact pow_lt_pow_of_lt_left (neg_lt_neg hab) (neg_nonneg.2 hb) (bit1_pos (zero_le n)) }, { exact (pow_bit1_nonpos_iff.2 ha).trans_lt (pow_bit1_pos_iff.2 hb) } }, { exact pow_lt_pow_of_lt_left hab ha (bit1_pos (zero_le n)) } end lemma odd.strict_mono_pow (hn : odd n) : strict_mono (λ a : R, a ^ n) := by cases hn with k hk; simpa only [hk, two_mul] using strict_mono_pow_bit1 _ /-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/ theorem one_add_mul_le_pow (H : -2 ≤ a) (n : ℕ) : 1 + (n : R) * a ≤ (1 + a) ^ n := one_add_mul_le_pow' (mul_self_nonneg _) (mul_self_nonneg _) (neg_le_iff_add_nonneg'.1 H) _ /-- Bernoulli's inequality reformulated to estimate `a^n`. -/ theorem one_add_mul_sub_le_pow (H : -1 ≤ a) (n : ℕ) : 1 + (n : R) * (a - 1) ≤ a ^ n := have -2 ≤ a - 1, by rwa [bit0, neg_add, ← sub_eq_add_neg, sub_le_sub_iff_right], by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n end linear_ordered_ring /-- Bernoulli's inequality reformulated to estimate `(n : K)`. -/ theorem nat.cast_le_pow_sub_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ (a ^ n - 1) / (a - 1) := (le_div_iff (sub_pos.2 H)).2 $ le_sub_left_of_add_le $ one_add_mul_sub_le_pow ((neg_le_self $ @zero_le_one K _).trans H.le) _ /-- For any `a > 1` and a natural `n` we have `n ≤ a ^ n / (a - 1)`. See also `nat.cast_le_pow_sub_div_sub` for a stronger inequality with `a ^ n - 1` in the numerator. -/ theorem nat.cast_le_pow_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ a ^ n / (a - 1) := (n.cast_le_pow_sub_div_sub H).trans $ div_le_div_of_le (sub_nonneg.2 H.le) (sub_le_self _ zero_le_one) namespace int lemma units_sq (u : ℤˣ) : u ^ 2 = 1 := (sq u).symm ▸ units_mul_self u alias int.units_sq ← int.units_pow_two lemma units_pow_eq_pow_mod_two (u : ℤˣ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_sq, one_pow, mul_one] @[simp] lemma nat_abs_sq (x : ℤ) : (x.nat_abs ^ 2 : ℤ) = x ^ 2 := by rw [sq, int.nat_abs_mul_self', sq] alias int.nat_abs_sq ← int.nat_abs_pow_two lemma abs_le_self_sq (a : ℤ) : (int.nat_abs a : ℤ) ≤ a ^ 2 := by { rw [← int.nat_abs_sq a, sq], norm_cast, apply nat.le_mul_self } alias int.abs_le_self_sq ← int.abs_le_self_pow_two lemma le_self_sq (b : ℤ) : b ≤ b ^ 2 := le_trans (le_nat_abs) (abs_le_self_sq _) alias int.le_self_sq ← int.le_self_pow_two lemma pow_right_injective {x : ℤ} (h : 1 < x.nat_abs) : function.injective ((^) x : ℕ → ℤ) := begin suffices : function.injective (nat_abs ∘ ((^) x : ℕ → ℤ)), { exact function.injective.of_comp this }, convert nat.pow_right_injective h, ext n, rw [function.comp_app, nat_abs_pow] end end int variables (M G A) /-- Monoid homomorphisms from `multiplicative ℕ` are defined by the image of `multiplicative.of_add 1`. -/ def powers_hom [monoid M] : M ≃ (multiplicative ℕ →* M) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, by { convert pow_zero x, exact to_add_one }, λ m n, pow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := pow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_pow, ← of_add_nsmul] } } /-- Monoid homomorphisms from `multiplicative ℤ` are defined by the image of `multiplicative.of_add 1`. -/ def zpowers_hom [group G] : G ≃ (multiplicative ℤ →* G) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, zpow_zero x, λ m n, zpow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := zpow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_zpow, ← of_add_zsmul ] } } /-- Additive homomorphisms from `ℕ` are defined by the image of `1`. -/ def multiples_hom [add_monoid A] : A ≃ (ℕ →+ A) := { to_fun := λ x, ⟨λ n, n • x, zero_nsmul x, λ m n, add_nsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_nsmul, right_inv := λ f, add_monoid_hom.ext_nat $ one_nsmul (f 1) } /-- Additive homomorphisms from `ℤ` are defined by the image of `1`. -/ def zmultiples_hom [add_group A] : A ≃ (ℤ →+ A) := { to_fun := λ x, ⟨λ n, n • x, zero_zsmul x, λ m n, add_zsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_zsmul, right_inv := λ f, add_monoid_hom.ext_int $ one_zsmul (f 1) } attribute [to_additive multiples_hom] powers_hom attribute [to_additive zmultiples_hom] zpowers_hom variables {M G A} @[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative ℕ) : powers_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative ℕ →* M) : (powers_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma zpowers_hom_apply [group G] (x : G) (n : multiplicative ℤ) : zpowers_hom G x n = x ^ n.to_add := rfl @[simp] lemma zpowers_hom_symm_apply [group G] (f : multiplicative ℤ →* G) : (zpowers_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_hom_apply [add_monoid A] (x : A) (n : ℕ) : multiples_hom A x n = n • x := rfl attribute [to_additive multiples_hom_apply] powers_hom_apply @[simp] lemma multiples_hom_symm_apply [add_monoid A] (f : ℕ →+ A) : (multiples_hom A).symm f = f 1 := rfl attribute [to_additive multiples_hom_symm_apply] powers_hom_symm_apply @[simp] lemma zmultiples_hom_apply [add_group A] (x : A) (n : ℤ) : zmultiples_hom A x n = n • x := rfl attribute [to_additive zmultiples_hom_apply] zpowers_hom_apply @[simp] lemma zmultiples_hom_symm_apply [add_group A] (f : ℤ →+ A) : (zmultiples_hom A).symm f = f 1 := rfl attribute [to_additive zmultiples_hom_symm_apply] zpowers_hom_symm_apply -- TODO use to_additive in the rest of this file lemma monoid_hom.apply_mnat [monoid M] (f : multiplicative ℕ →* M) (n : multiplicative ℕ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← powers_hom_symm_apply, ← powers_hom_apply, equiv.apply_symm_apply] @[ext] lemma monoid_hom.ext_mnat [monoid M] ⦃f g : multiplicative ℕ →* M⦄ (h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g := monoid_hom.ext $ λ n, by rw [f.apply_mnat, g.apply_mnat, h] lemma monoid_hom.apply_mint [group M] (f : multiplicative ℤ →* M) (n : multiplicative ℤ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← zpowers_hom_symm_apply, ← zpowers_hom_apply, equiv.apply_symm_apply] /-! `monoid_hom.ext_mint` is defined in `data.int.cast` -/ lemma add_monoid_hom.apply_nat [add_monoid M] (f : ℕ →+ M) (n : ℕ) : f n = n • (f 1) := by rw [← multiples_hom_symm_apply, ← multiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_nat` is defined in `data.nat.cast` -/ lemma add_monoid_hom.apply_int [add_group M] (f : ℤ →+ M) (n : ℤ) : f n = n • (f 1) := by rw [← zmultiples_hom_symm_apply, ← zmultiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_int` is defined in `data.int.cast` -/ variables (M G A) /-- If `M` is commutative, `powers_hom` is a multiplicative equivalence. -/ def powers_mul_hom [comm_monoid M] : M ≃* (multiplicative ℕ →* M) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_pow], ..powers_hom M} /-- If `M` is commutative, `zpowers_hom` is a multiplicative equivalence. -/ def zpowers_mul_hom [comm_group G] : G ≃* (multiplicative ℤ →* G) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_zpow], ..zpowers_hom G} /-- If `M` is commutative, `multiples_hom` is an additive equivalence. -/ def multiples_add_hom [add_comm_monoid A] : A ≃+ (ℕ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [nsmul_add], ..multiples_hom A} /-- If `M` is commutative, `zmultiples_hom` is an additive equivalence. -/ def zmultiples_add_hom [add_comm_group A] : A ≃+ (ℤ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [zsmul_add], ..zmultiples_hom A} variables {M G A} @[simp] lemma powers_mul_hom_apply [comm_monoid M] (x : M) (n : multiplicative ℕ) : powers_mul_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_mul_hom_symm_apply [comm_monoid M] (f : multiplicative ℕ →* M) : (powers_mul_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma zpowers_mul_hom_apply [comm_group G] (x : G) (n : multiplicative ℤ) : zpowers_mul_hom G x n = x ^ n.to_add := rfl @[simp] lemma zpowers_mul_hom_symm_apply [comm_group G] (f : multiplicative ℤ →* G) : (zpowers_mul_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_add_hom_apply [add_comm_monoid A] (x : A) (n : ℕ) : multiples_add_hom A x n = n • x := rfl @[simp] lemma multiples_add_hom_symm_apply [add_comm_monoid A] (f : ℕ →+ A) : (multiples_add_hom A).symm f = f 1 := rfl @[simp] lemma zmultiples_add_hom_apply [add_comm_group A] (x : A) (n : ℤ) : zmultiples_add_hom A x n = n • x := rfl @[simp] lemma zmultiples_add_hom_symm_apply [add_comm_group A] (f : ℤ →+ A) : (zmultiples_add_hom A).symm f = f 1 := rfl /-! ### Commutativity (again) Facts about `semiconj_by` and `commute` that require `zpow` or `zsmul`, or the fact that integer multiplication equals semiring multiplication. -/ namespace semiconj_by section variables [semiring R] {a x y : R} @[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : ℕ) : semiconj_by a ((n : R) * x) (n * y) := semiconj_by.mul_right (nat.commute_cast _ _) h @[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : ℕ) : semiconj_by ((n : R) * a) x y := semiconj_by.mul_left (nat.cast_commute _ _) h @[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : ℕ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_nat_mul_left m).cast_nat_mul_right n end variables [monoid M] [group G] [ring R] @[simp, to_additive] lemma units_zpow_right {a : M} {x y : Mˣ} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (↑(x^m)) (↑(y^m)) | (n : ℕ) := by simp only [zpow_coe_nat, units.coe_pow, h, pow_right] | -[1+n] := by simp only [zpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right] variables {a b x y x' y' : R} @[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : ℤ) : semiconj_by a ((m : ℤ) * x) (m * y) := semiconj_by.mul_right (int.commute_cast _ _) h @[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : ℤ) : semiconj_by ((m : R) * a) x y := semiconj_by.mul_left (int.cast_commute _ _) h @[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : ℤ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_int_mul_left m).cast_int_mul_right n end semiconj_by namespace commute section variables [semiring R] {a b : R} @[simp] theorem cast_nat_mul_right (h : commute a b) (n : ℕ) : commute a ((n : R) * b) := h.cast_nat_mul_right n @[simp] theorem cast_nat_mul_left (h : commute a b) (n : ℕ) : commute ((n : R) * a) b := h.cast_nat_mul_left n @[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : ℕ) : commute ((m : R) * a) (n * b) := h.cast_nat_mul_cast_nat_mul m n @[simp] theorem self_cast_nat_mul (n : ℕ) : commute a (n * a) := (commute.refl a).cast_nat_mul_right n @[simp] theorem cast_nat_mul_self (n : ℕ) : commute ((n : R) * a) a := (commute.refl a).cast_nat_mul_left n @[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : ℕ) : commute ((m : R) * a) (n * a) := (commute.refl a).cast_nat_mul_cast_nat_mul m n end variables [monoid M] [group G] [ring R] @[simp, to_additive] lemma units_zpow_right {a : M} {u : Mˣ} (h : commute a u) (m : ℤ) : commute a (↑(u^m)) := h.units_zpow_right m @[simp, to_additive] lemma units_zpow_left {u : Mˣ} {a : M} (h : commute ↑u a) (m : ℤ) : commute (↑(u^m)) a := (h.symm.units_zpow_right m).symm variables {a b : R} @[simp] lemma cast_int_mul_right (h : commute a b) (m : ℤ) : commute a (m * b) := h.cast_int_mul_right m @[simp] lemma cast_int_mul_left (h : commute a b) (m : ℤ) : commute ((m : R) * a) b := h.cast_int_mul_left m lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : ℤ) : commute ((m : R) * a) (n * b) := h.cast_int_mul_cast_int_mul m n variables (a) (m n : ℤ) @[simp] lemma cast_int_left : commute (m : R) a := by { rw [← mul_one (m : R)], exact (one_left a).cast_int_mul_left m } @[simp] lemma cast_int_right : commute a m := by { rw [← mul_one (m : R)], exact (one_right a).cast_int_mul_right m } @[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n @[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) := (commute.refl a).cast_int_mul_cast_int_mul m n end commute section multiplicative open multiplicative @[simp] lemma nat.to_add_pow (a : multiplicative ℕ) (b : ℕ) : to_add (a ^ b) = to_add a * b := begin induction b with b ih, { erw [pow_zero, to_add_one, mul_zero] }, { simp [*, pow_succ, add_comm, nat.mul_succ] } end @[simp] lemma nat.of_add_mul (a b : ℕ) : of_add (a * b) = of_add a ^ b := (nat.to_add_pow _ _).symm @[simp] lemma int.to_add_pow (a : multiplicative ℤ) (b : ℕ) : to_add (a ^ b) = to_add a * b := by induction b; simp [*, mul_add, pow_succ, add_comm] @[simp] lemma int.to_add_zpow (a : multiplicative ℤ) (b : ℤ) : to_add (a ^ b) = to_add a * b := int.induction_on b (by simp) (by simp [zpow_add, mul_add] {contextual := tt}) (by simp [zpow_add, mul_add, sub_eq_add_neg, -int.add_neg_one] {contextual := tt}) @[simp] lemma int.of_add_mul (a b : ℤ) : of_add (a * b) = of_add a ^ b := (int.to_add_zpow _ _).symm end multiplicative namespace units variables [monoid M] lemma conj_pow (u : Mˣ) (x : M) (n : ℕ) : (↑u * x * ↑(u⁻¹))^n = u * x^n * ↑(u⁻¹) := (divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm lemma conj_pow' (u : Mˣ) (x : M) (n : ℕ) : (↑(u⁻¹) * x * u)^n = ↑(u⁻¹) * x^n * u:= (u⁻¹).conj_pow x n end units namespace mul_opposite /-- Moving to the opposite monoid commutes with taking powers. -/ @[simp] lemma op_pow [monoid M] (x : M) (n : ℕ) : op (x ^ n) = (op x) ^ n := rfl @[simp] lemma unop_pow [monoid M] (x : Mᵐᵒᵖ) (n : ℕ) : unop (x ^ n) = (unop x) ^ n := rfl /-- Moving to the opposite group or group_with_zero commutes with taking powers. -/ @[simp] lemma op_zpow [div_inv_monoid M] (x : M) (z : ℤ) : op (x ^ z) = (op x) ^ z := rfl @[simp] lemma unop_zpow [div_inv_monoid M] (x : Mᵐᵒᵖ) (z : ℤ) : unop (x ^ z) = (unop x) ^ z := rfl end mul_opposite
d7975a08fa87e4b6bfc999810e1131e91b38e71d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/uniform_space/uniform_convergence.lean
b351b64278cb5ca4610ca202ecafb4e5a920c4e0
[ "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
44,535
lean
/- Copyright (c) 2020 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 topology.uniform_space.basic /-! # Uniform convergence A sequence of functions `Fₙ` (with values in a metric space) converges uniformly on a set `s` to a function `f` if, for all `ε > 0`, for all large enough `n`, one has for all `y ∈ s` the inequality `dist (f y, Fₙ y) < ε`. Under uniform convergence, many properties of the `Fₙ` pass to the limit, most notably continuity. We prove this in the file, defining the notion of uniform convergence in the more general setting of uniform spaces, and with respect to an arbitrary indexing set endowed with a filter (instead of just `ℕ` with `at_top`). ## Main results Let `α` be a topological space, `β` a uniform space, `Fₙ` and `f` be functions from `α`to `β` (where the index `n` belongs to an indexing type `ι` endowed with a filter `p`). * `tendsto_uniformly_on F f p s`: the fact that `Fₙ` converges uniformly to `f` on `s`. This means that, for any entourage `u` of the diagonal, for large enough `n` (with respect to `p`), one has `(f y, Fₙ y) ∈ u` for all `y ∈ s`. * `tendsto_uniformly F f p`: same notion with `s = univ`. * `tendsto_uniformly_on.continuous_on`: a uniform limit on a set of functions which are continuous on this set is itself continuous on this set. * `tendsto_uniformly.continuous`: a uniform limit of continuous functions is continuous. * `tendsto_uniformly_on.tendsto_comp`: If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s`. * `tendsto_uniformly.tendsto_comp`: If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. We also define notions where the convergence is locally uniform, called `tendsto_locally_uniformly_on F f p s` and `tendsto_locally_uniformly F f p`. The previous theorems all have corresponding versions under locally uniform convergence. Finally, we introduce the notion of a uniform Cauchy sequence, which is to uniform convergence what a Cauchy sequence is to the usual notion of convergence. ## Implementation notes We derive most of our initial results from an auxiliary definition `tendsto_uniformly_on_filter`. This definition in and of itself can sometimes be useful, e.g., when studying the local behavior of the `Fₙ` near a point, which would typically look like `tendsto_uniformly_on_filter F f p (𝓝 x)`. Still, while this may be the "correct" definition (see `tendsto_uniformly_on_iff_tendsto_uniformly_on_filter`), it is somewhat unwieldy to work with in practice. Thus, we provide the more traditional definition in `tendsto_uniformly_on`. Most results hold under weaker assumptions of locally uniform approximation. In a first section, we prove the results under these weaker assumptions. Then, we derive the results on uniform convergence from them. ## Tags Uniform limit, uniform convergence, tends uniformly to -/ noncomputable theory open_locale topological_space classical uniformity filter open set filter universes u v w variables {α β γ ι : Type*} [uniform_space β] variables {F : ι → α → β} {f : α → β} {s s' : set α} {x : α} {p : filter ι} {p' : filter α} {g : ι → α} /-! ### Different notions of uniform convergence We define uniform convergence and locally uniform convergence, on a set or in the whole space. -/ /-- A sequence of functions `Fₙ` converges uniformly on a filter `p'` to a limiting function `f` with respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p ×ᶠ p'`-eventually `(f x, Fₙ x) ∈ u`. -/ def tendsto_uniformly_on_filter (F : ι → α → β) (f : α → β) (p : filter ι) (p' : filter α) := ∀ u ∈ 𝓤 β, ∀ᶠ (n : ι × α) in (p ×ᶠ p'), (f n.snd, F n.fst n.snd) ∈ u /-- A sequence of functions `Fₙ` converges uniformly on a filter `p'` to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ᶠ p'` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit besides it being in `p'`. -/ lemma tendsto_uniformly_on_filter_iff_tendsto : tendsto_uniformly_on_filter F f p p' ↔ tendsto (λ q : ι × α, (f q.2, F q.1 q.2)) (p ×ᶠ p') (𝓤 β) := forall₂_congr $ λ u u_in, by simp [mem_map, filter.eventually, mem_prod_iff, preimage] /-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` with respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually `(f x, Fₙ x) ∈ u` for all `x ∈ s`. -/ def tendsto_uniformly_on (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) := ∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ (x : α), x ∈ s → (f x, F n x) ∈ u lemma tendsto_uniformly_on_iff_tendsto_uniformly_on_filter : tendsto_uniformly_on F f p s ↔ tendsto_uniformly_on_filter F f p (𝓟 s) := begin simp only [tendsto_uniformly_on, tendsto_uniformly_on_filter], apply forall₂_congr, simp_rw [eventually_prod_principal_iff], simp, end alias tendsto_uniformly_on_iff_tendsto_uniformly_on_filter ↔ tendsto_uniformly_on.tendsto_uniformly_on_filter tendsto_uniformly_on_filter.tendsto_uniformly_on /-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ᶠ 𝓟 s` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit besides it being in `s`. -/ lemma tendsto_uniformly_on_iff_tendsto {F : ι → α → β} {f : α → β} {p : filter ι} {s : set α} : tendsto_uniformly_on F f p s ↔ tendsto (λ q : ι × α, (f q.2, F q.1 q.2)) (p ×ᶠ 𝓟 s) (𝓤 β) := by simp [tendsto_uniformly_on_iff_tendsto_uniformly_on_filter, tendsto_uniformly_on_filter_iff_tendsto] /-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` with respect to a filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually `(f x, Fₙ x) ∈ u` for all `x`. -/ def tendsto_uniformly (F : ι → α → β) (f : α → β) (p : filter ι) := ∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ (x : α), (f x, F n x) ∈ u lemma tendsto_uniformly_iff_tendsto_uniformly_on_filter : tendsto_uniformly F f p ↔ tendsto_uniformly_on_filter F f p ⊤ := begin simp only [tendsto_uniformly, tendsto_uniformly_on_filter], apply forall₂_congr, simp_rw [← principal_univ, eventually_prod_principal_iff], simp, end lemma tendsto_uniformly.tendsto_uniformly_on_filter (h : tendsto_uniformly F f p) : tendsto_uniformly_on_filter F f p ⊤ := by rwa ← tendsto_uniformly_iff_tendsto_uniformly_on_filter lemma tendsto_uniformly_on_iff_tendsto_uniformly_comp_coe : tendsto_uniformly_on F f p s ↔ tendsto_uniformly (λ i (x : s), F i x) (f ∘ coe) p := begin apply forall₂_congr, intros u hu, simp, end /-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ᶠ ⊤` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit. -/ lemma tendsto_uniformly_iff_tendsto {F : ι → α → β} {f : α → β} {p : filter ι} : tendsto_uniformly F f p ↔ tendsto (λ q : ι × α, (f q.2, F q.1 q.2)) (p ×ᶠ ⊤) (𝓤 β) := by simp [tendsto_uniformly_iff_tendsto_uniformly_on_filter, tendsto_uniformly_on_filter_iff_tendsto] /-- Uniform converence implies pointwise convergence. -/ lemma tendsto_uniformly_on_filter.tendsto_at (h : tendsto_uniformly_on_filter F f p p') (hx : 𝓟 {x} ≤ p') : tendsto (λ n, F n x) p $ 𝓝 (f x) := begin refine uniform.tendsto_nhds_right.mpr (λ u hu, mem_map.mpr _), filter_upwards [(h u hu).curry], intros i h, simpa using (h.filter_mono hx), end /-- Uniform converence implies pointwise convergence. -/ lemma tendsto_uniformly_on.tendsto_at (h : tendsto_uniformly_on F f p s) {x : α} (hx : x ∈ s) : tendsto (λ n, F n x) p $ 𝓝 (f x) := h.tendsto_uniformly_on_filter.tendsto_at (le_principal_iff.mpr $ mem_principal.mpr $ singleton_subset_iff.mpr $ hx) /-- Uniform converence implies pointwise convergence. -/ lemma tendsto_uniformly.tendsto_at (h : tendsto_uniformly F f p) (x : α) : tendsto (λ n, F n x) p $ 𝓝 (f x) := h.tendsto_uniformly_on_filter.tendsto_at le_top lemma tendsto_uniformly_on_univ : tendsto_uniformly_on F f p univ ↔ tendsto_uniformly F f p := by simp [tendsto_uniformly_on, tendsto_uniformly] lemma tendsto_uniformly_on_filter.mono_left {p'' : filter ι} (h : tendsto_uniformly_on_filter F f p p') (hp : p'' ≤ p) : tendsto_uniformly_on_filter F f p'' p' := λ u hu, (h u hu).filter_mono (p'.prod_mono_left hp) lemma tendsto_uniformly_on_filter.mono_right {p'' : filter α} (h : tendsto_uniformly_on_filter F f p p') (hp : p'' ≤ p') : tendsto_uniformly_on_filter F f p p'' := λ u hu, (h u hu).filter_mono (p.prod_mono_right hp) lemma tendsto_uniformly_on.mono {s' : set α} (h : tendsto_uniformly_on F f p s) (h' : s' ⊆ s) : tendsto_uniformly_on F f p s' := tendsto_uniformly_on_iff_tendsto_uniformly_on_filter.mpr (h.tendsto_uniformly_on_filter.mono_right (le_principal_iff.mpr $ mem_principal.mpr h')) lemma tendsto_uniformly_on_filter.congr {F' : ι → α → β} (hf : tendsto_uniformly_on_filter F f p p') (hff' : ∀ᶠ (n : ι × α) in (p ×ᶠ p'), F n.fst n.snd = F' n.fst n.snd) : tendsto_uniformly_on_filter F' f p p' := begin refine (λ u hu, ((hf u hu).and hff').mono (λ n h, _)), rw ← h.right, exact h.left, end lemma tendsto_uniformly_on.congr {F' : ι → α → β} (hf : tendsto_uniformly_on F f p s) (hff' : ∀ᶠ n in p, set.eq_on (F n) (F' n) s) : tendsto_uniformly_on F' f p s := begin rw tendsto_uniformly_on_iff_tendsto_uniformly_on_filter at hf ⊢, refine hf.congr _, rw eventually_iff at hff' ⊢, simp only [set.eq_on] at hff', simp only [mem_prod_principal, hff', mem_set_of_eq], end protected lemma tendsto_uniformly.tendsto_uniformly_on (h : tendsto_uniformly F f p) : tendsto_uniformly_on F f p s := (tendsto_uniformly_on_univ.2 h).mono (subset_univ s) /-- Composing on the right by a function preserves uniform convergence on a filter -/ lemma tendsto_uniformly_on_filter.comp (h : tendsto_uniformly_on_filter F f p p') (g : γ → α) : tendsto_uniformly_on_filter (λ n, F n ∘ g) (f ∘ g) p (p'.comap g) := begin intros u hu, obtain ⟨pa, hpa, pb, hpb, hpapb⟩ := eventually_prod_iff.mp (h u hu), rw eventually_prod_iff, simp_rw eventually_comap, exact ⟨pa, hpa, pb ∘ g, ⟨hpb.mono (λ x hx y hy, by simp only [hx, hy, function.comp_app]), λ x hx y hy, hpapb hx hy⟩⟩, end /-- Composing on the right by a function preserves uniform convergence on a set -/ lemma tendsto_uniformly_on.comp (h : tendsto_uniformly_on F f p s) (g : γ → α) : tendsto_uniformly_on (λ n, F n ∘ g) (f ∘ g) p (g ⁻¹' s) := begin rw tendsto_uniformly_on_iff_tendsto_uniformly_on_filter at h ⊢, simpa [tendsto_uniformly_on, comap_principal] using (tendsto_uniformly_on_filter.comp h g), end /-- Composing on the right by a function preserves uniform convergence -/ lemma tendsto_uniformly.comp (h : tendsto_uniformly F f p) (g : γ → α) : tendsto_uniformly (λ n, F n ∘ g) (f ∘ g) p := begin rw tendsto_uniformly_iff_tendsto_uniformly_on_filter at h ⊢, simpa [principal_univ, comap_principal] using (h.comp g), end /-- Composing on the left by a uniformly continuous function preserves uniform convergence on a filter -/ lemma uniform_continuous.comp_tendsto_uniformly_on_filter [uniform_space γ] {g : β → γ} (hg : uniform_continuous g) (h : tendsto_uniformly_on_filter F f p p') : tendsto_uniformly_on_filter (λ i, g ∘ (F i)) (g ∘ f) p p' := λ u hu, h _ (hg hu) /-- Composing on the left by a uniformly continuous function preserves uniform convergence on a set -/ lemma uniform_continuous.comp_tendsto_uniformly_on [uniform_space γ] {g : β → γ} (hg : uniform_continuous g) (h : tendsto_uniformly_on F f p s) : tendsto_uniformly_on (λ i, g ∘ (F i)) (g ∘ f) p s := λ u hu, h _ (hg hu) /-- Composing on the left by a uniformly continuous function preserves uniform convergence -/ lemma uniform_continuous.comp_tendsto_uniformly [uniform_space γ] {g : β → γ} (hg : uniform_continuous g) (h : tendsto_uniformly F f p) : tendsto_uniformly (λ i, g ∘ (F i)) (g ∘ f) p := λ u hu, h _ (hg hu) lemma tendsto_uniformly_on_filter.prod_map {ι' α' β' : Type*} [uniform_space β'] {F' : ι' → α' → β'} {f' : α' → β'} {q : filter ι'} {q' : filter α'} (h : tendsto_uniformly_on_filter F f p p') (h' : tendsto_uniformly_on_filter F' f' q q') : tendsto_uniformly_on_filter (λ (i : ι × ι'), prod.map (F i.1) (F' i.2)) (prod.map f f') (p.prod q) (p'.prod q') := begin intros u hu, rw [uniformity_prod_eq_prod, mem_map, mem_prod_iff] at hu, obtain ⟨v, hv, w, hw, hvw⟩ := hu, apply (tendsto_swap4_prod.eventually ((h v hv).prod_mk (h' w hw))).mono, simp only [prod_map, and_imp, prod.forall], intros n n' x hxv hxw, have hout : ((f x.fst, F n x.fst), (f' x.snd, F' n' x.snd)) ∈ {x : (β × β) × β' × β' | ((x.fst.fst, x.snd.fst), x.fst.snd, x.snd.snd) ∈ u}, { exact mem_of_mem_of_subset (set.mem_prod.mpr ⟨hxv, hxw⟩) hvw, }, exact hout, end lemma tendsto_uniformly_on.prod_map {ι' α' β' : Type*} [uniform_space β'] {F' : ι' → α' → β'} {f' : α' → β'} {p' : filter ι'} {s' : set α'} (h : tendsto_uniformly_on F f p s) (h' : tendsto_uniformly_on F' f' p' s') : tendsto_uniformly_on (λ (i : ι × ι'), prod.map (F i.1) (F' i.2)) (prod.map f f') (p.prod p') (s ×ˢ s') := begin rw tendsto_uniformly_on_iff_tendsto_uniformly_on_filter at h h' ⊢, simpa only [prod_principal_principal] using (h.prod_map h'), end lemma tendsto_uniformly.prod_map {ι' α' β' : Type*} [uniform_space β'] {F' : ι' → α' → β'} {f' : α' → β'} {p' : filter ι'} (h : tendsto_uniformly F f p) (h' : tendsto_uniformly F' f' p') : tendsto_uniformly (λ (i : ι × ι'), prod.map (F i.1) (F' i.2)) (prod.map f f') (p.prod p') := begin rw [←tendsto_uniformly_on_univ, ←univ_prod_univ] at *, exact h.prod_map h', end lemma tendsto_uniformly_on_filter.prod {ι' β' : Type*} [uniform_space β'] {F' : ι' → α → β'} {f' : α → β'} {q : filter ι'} (h : tendsto_uniformly_on_filter F f p p') (h' : tendsto_uniformly_on_filter F' f' q p') : tendsto_uniformly_on_filter (λ (i : ι × ι') a, (F i.1 a, F' i.2 a)) (λ a, (f a, f' a)) (p.prod q) p' := λ u hu, ((h.prod_map h') u hu).diag_of_prod_right lemma tendsto_uniformly_on.prod {ι' β' : Type*} [uniform_space β'] {F' : ι' → α → β'} {f' : α → β'} {p' : filter ι'} (h : tendsto_uniformly_on F f p s) (h' : tendsto_uniformly_on F' f' p' s) : tendsto_uniformly_on (λ (i : ι × ι') a, (F i.1 a, F' i.2 a)) (λ a, (f a, f' a)) (p.prod p') s := (congr_arg _ s.inter_self).mp ((h.prod_map h').comp (λ a, (a, a))) lemma tendsto_uniformly.prod {ι' β' : Type*} [uniform_space β'] {F' : ι' → α → β'} {f' : α → β'} {p' : filter ι'} (h : tendsto_uniformly F f p) (h' : tendsto_uniformly F' f' p') : tendsto_uniformly (λ (i : ι × ι') a, (F i.1 a, F' i.2 a)) (λ a, (f a, f' a)) (p.prod p') := (h.prod_map h').comp (λ a, (a, a)) /-- Uniform convergence on a filter `p'` to a constant function is equivalent to convergence in `p ×ᶠ p'`. -/ lemma tendsto_prod_filter_iff {c : β} : tendsto ↿F (p ×ᶠ p') (𝓝 c) ↔ tendsto_uniformly_on_filter F (λ _, c) p p' := begin simp_rw [tendsto, nhds_eq_comap_uniformity, map_le_iff_le_comap.symm, map_map, le_def, mem_map], exact forall₂_congr (λ u hu, by simpa [eventually_iff]), end /-- Uniform convergence on a set `s` to a constant function is equivalent to convergence in `p ×ᶠ 𝓟 s`. -/ lemma tendsto_prod_principal_iff {c : β} : tendsto ↿F (p ×ᶠ 𝓟 s) (𝓝 c) ↔ tendsto_uniformly_on F (λ _, c) p s := begin rw tendsto_uniformly_on_iff_tendsto_uniformly_on_filter, exact tendsto_prod_filter_iff, end /-- Uniform convergence to a constant function is equivalent to convergence in `p ×ᶠ ⊤`. -/ lemma tendsto_prod_top_iff {c : β} : tendsto ↿F (p ×ᶠ ⊤) (𝓝 c) ↔ tendsto_uniformly F (λ _, c) p := begin rw tendsto_uniformly_iff_tendsto_uniformly_on_filter, exact tendsto_prod_filter_iff, end /-- Uniform convergence on the empty set is vacuously true -/ lemma tendsto_uniformly_on_empty : tendsto_uniformly_on F f p ∅ := λ u hu, by simp /-- Uniform convergence on a singleton is equivalent to regular convergence -/ lemma tendsto_uniformly_on_singleton_iff_tendsto : tendsto_uniformly_on F f p {x} ↔ tendsto (λ n : ι, F n x) p (𝓝 (f x)) := begin simp_rw [tendsto_uniformly_on_iff_tendsto, uniform.tendsto_nhds_right, tendsto_def], exact forall₂_congr (λ u hu, by simp [mem_prod_principal, preimage]), end /-- If a sequence `g` converges to some `b`, then the sequence of constant functions `λ n, λ a, g n` converges to the constant function `λ a, b` on any set `s` -/ lemma filter.tendsto.tendsto_uniformly_on_filter_const {g : ι → β} {b : β} (hg : tendsto g p (𝓝 b)) (p' : filter α) : tendsto_uniformly_on_filter (λ n : ι, λ a : α, g n) (λ a : α, b) p p' := begin rw tendsto_uniformly_on_filter_iff_tendsto, rw uniform.tendsto_nhds_right at hg, exact (hg.comp (tendsto_fst.comp ((@tendsto_id ι p).prod_map (@tendsto_id α p')))).congr (λ x, by simp), end /-- If a sequence `g` converges to some `b`, then the sequence of constant functions `λ n, λ a, g n` converges to the constant function `λ a, b` on any set `s` -/ lemma filter.tendsto.tendsto_uniformly_on_const {g : ι → β} {b : β} (hg : tendsto g p (𝓝 b)) (s : set α) : tendsto_uniformly_on (λ n : ι, λ a : α, g n) (λ a : α, b) p s := tendsto_uniformly_on_iff_tendsto_uniformly_on_filter.mpr (hg.tendsto_uniformly_on_filter_const (𝓟 s)) lemma uniform_continuous_on.tendsto_uniformly [uniform_space α] [uniform_space γ] {x : α} {U : set α} (hU : U ∈ 𝓝 x) {F : α → β → γ} (hF : uniform_continuous_on ↿F (U ×ˢ (univ : set β))) : tendsto_uniformly F (F x) (𝓝 x) := begin let φ := (λ q : α × β, ((x, q.2), q)), rw [tendsto_uniformly_iff_tendsto, show (λ q : α × β, (F x q.2, F q.1 q.2)) = prod.map ↿F ↿F ∘ φ, by { ext ; simpa }], apply hF.comp (tendsto_inf.mpr ⟨_, _⟩), { rw [uniformity_prod, tendsto_inf, tendsto_comap_iff, tendsto_comap_iff, show (λp : (α × β) × α × β, (p.1.1, p.2.1)) ∘ φ = (λa, (x, a)) ∘ prod.fst, by { ext, simp }, show (λp : (α × β) × α × β, (p.1.2, p.2.2)) ∘ φ = (λb, (b, b)) ∘ prod.snd, by { ext, simp }], exact ⟨tendsto_left_nhds_uniformity.comp tendsto_fst, (tendsto_diag_uniformity id ⊤).comp tendsto_top⟩ }, { rw tendsto_principal, apply mem_of_superset (prod_mem_prod hU (mem_top.mpr rfl)) (λ q h, _), simp [h.1, mem_of_mem_nhds hU] } end lemma uniform_continuous₂.tendsto_uniformly [uniform_space α] [uniform_space γ] {f : α → β → γ} (h : uniform_continuous₂ f) {x : α} : tendsto_uniformly f (f x) (𝓝 x) := uniform_continuous_on.tendsto_uniformly univ_mem $ by rwa [univ_prod_univ, uniform_continuous_on_univ] /-- A sequence is uniformly Cauchy if eventually all of its pairwise differences are uniformly bounded -/ def uniform_cauchy_seq_on_filter (F : ι → α → β) (p : filter ι) (p' : filter α) : Prop := ∀ u : set (β × β), u ∈ 𝓤 β → ∀ᶠ (m : (ι × ι) × α) in ((p ×ᶠ p) ×ᶠ p'), (F m.fst.fst m.snd, F m.fst.snd m.snd) ∈ u /-- A sequence is uniformly Cauchy if eventually all of its pairwise differences are uniformly bounded -/ def uniform_cauchy_seq_on (F : ι → α → β) (p : filter ι) (s : set α) : Prop := ∀ u : set (β × β), u ∈ 𝓤 β → ∀ᶠ (m : ι × ι) in (p ×ᶠ p), ∀ (x : α), x ∈ s → (F m.fst x, F m.snd x) ∈ u lemma uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter : uniform_cauchy_seq_on F p s ↔ uniform_cauchy_seq_on_filter F p (𝓟 s) := begin simp only [uniform_cauchy_seq_on, uniform_cauchy_seq_on_filter], refine forall₂_congr (λ u hu, _), rw eventually_prod_principal_iff, end lemma uniform_cauchy_seq_on.uniform_cauchy_seq_on_filter (hF : uniform_cauchy_seq_on F p s) : uniform_cauchy_seq_on_filter F p (𝓟 s) := by rwa ←uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter /-- A sequence that converges uniformly is also uniformly Cauchy -/ lemma tendsto_uniformly_on_filter.uniform_cauchy_seq_on_filter (hF : tendsto_uniformly_on_filter F f p p') : uniform_cauchy_seq_on_filter F p p' := begin intros u hu, rcases comp_symm_of_uniformity hu with ⟨t, ht, htsymm, htmem⟩, have := tendsto_swap4_prod.eventually ((hF t ht).prod_mk (hF t ht)), apply this.diag_of_prod_right.mono, simp only [and_imp, prod.forall], intros n1 n2 x hl hr, exact set.mem_of_mem_of_subset (prod_mk_mem_comp_rel (htsymm hl) hr) htmem, end /-- A sequence that converges uniformly is also uniformly Cauchy -/ lemma tendsto_uniformly_on.uniform_cauchy_seq_on (hF : tendsto_uniformly_on F f p s) : uniform_cauchy_seq_on F p s := uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter.mpr hF.tendsto_uniformly_on_filter.uniform_cauchy_seq_on_filter /-- A uniformly Cauchy sequence converges uniformly to its limit -/ lemma uniform_cauchy_seq_on_filter.tendsto_uniformly_on_filter_of_tendsto [ne_bot p] (hF : uniform_cauchy_seq_on_filter F p p') (hF' : ∀ᶠ (x : α) in p', tendsto (λ n, F n x) p (𝓝 (f x))) : tendsto_uniformly_on_filter F f p p' := begin -- Proof idea: |f_n(x) - f(x)| ≤ |f_n(x) - f_m(x)| + |f_m(x) - f(x)|. We choose `n` -- so that |f_n(x) - f_m(x)| is uniformly small across `s` whenever `m ≥ n`. Then for -- a fixed `x`, we choose `m` sufficiently large such that |f_m(x) - f(x)| is small. intros u hu, rcases comp_symm_of_uniformity hu with ⟨t, ht, htsymm, htmem⟩, -- We will choose n, x, and m simultaneously. n and x come from hF. m comes from hF' -- But we need to promote hF' to the full product filter to use it have hmc : ∀ᶠ (x : (ι × ι) × α) in p ×ᶠ p ×ᶠ p', tendsto (λ (n : ι), F n x.snd) p (𝓝 (f x.snd)), { rw eventually_prod_iff, refine ⟨(λ x, true), by simp, _, hF', by simp⟩, }, -- To apply filter operations we'll need to do some order manipulation rw filter.eventually_swap_iff, have := tendsto_prod_assoc.eventually (tendsto_prod_swap.eventually ((hF t ht).and hmc)), apply this.curry.mono, simp only [equiv.prod_assoc_apply, eventually_and, eventually_const, prod.snd_swap, prod.fst_swap, and_imp, prod.forall], -- Complete the proof intros x n hx hm', refine set.mem_of_mem_of_subset (mem_comp_rel.mpr _) htmem, rw uniform.tendsto_nhds_right at hm', have := hx.and (hm' ht), obtain ⟨m, hm⟩ := this.exists, exact ⟨F m x, ⟨hm.2, htsymm hm.1⟩⟩, end /-- A uniformly Cauchy sequence converges uniformly to its limit -/ lemma uniform_cauchy_seq_on.tendsto_uniformly_on_of_tendsto [ne_bot p] (hF : uniform_cauchy_seq_on F p s) (hF' : ∀ x : α, x ∈ s → tendsto (λ n, F n x) p (𝓝 (f x))) : tendsto_uniformly_on F f p s := tendsto_uniformly_on_iff_tendsto_uniformly_on_filter.mpr (hF.uniform_cauchy_seq_on_filter.tendsto_uniformly_on_filter_of_tendsto hF') lemma uniform_cauchy_seq_on_filter.mono_left {p'' : filter ι} (hf : uniform_cauchy_seq_on_filter F p p') (hp : p'' ≤ p) : uniform_cauchy_seq_on_filter F p'' p' := begin intros u hu, have := (hf u hu).filter_mono (p'.prod_mono_left (filter.prod_mono hp hp)), exact this.mono (by simp), end lemma uniform_cauchy_seq_on_filter.mono_right {p'' : filter α} (hf : uniform_cauchy_seq_on_filter F p p') (hp : p'' ≤ p') : uniform_cauchy_seq_on_filter F p p'' := begin intros u hu, have := (hf u hu).filter_mono ((p ×ᶠ p).prod_mono_right hp), exact this.mono (by simp), end lemma uniform_cauchy_seq_on.mono {s' : set α} (hf : uniform_cauchy_seq_on F p s) (hss' : s' ⊆ s) : uniform_cauchy_seq_on F p s' := begin rw uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter at hf ⊢, exact hf.mono_right (le_principal_iff.mpr $mem_principal.mpr hss'), end /-- Composing on the right by a function preserves uniform Cauchy sequences -/ lemma uniform_cauchy_seq_on_filter.comp {γ : Type*} (hf : uniform_cauchy_seq_on_filter F p p') (g : γ → α) : uniform_cauchy_seq_on_filter (λ n, F n ∘ g) p (p'.comap g) := begin intros u hu, obtain ⟨pa, hpa, pb, hpb, hpapb⟩ := eventually_prod_iff.mp (hf u hu), rw eventually_prod_iff, refine ⟨pa, hpa, pb ∘ g, _, λ x hx y hy, hpapb hx hy⟩, exact eventually_comap.mpr (hpb.mono (λ x hx y hy, by simp only [hx, hy, function.comp_app])), end /-- Composing on the right by a function preserves uniform Cauchy sequences -/ lemma uniform_cauchy_seq_on.comp {γ : Type*} (hf : uniform_cauchy_seq_on F p s) (g : γ → α) : uniform_cauchy_seq_on (λ n, F n ∘ g) p (g ⁻¹' s) := begin rw uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter at hf ⊢, simpa only [uniform_cauchy_seq_on, comap_principal] using (hf.comp g), end /-- Composing on the left by a uniformly continuous function preserves uniform Cauchy sequences -/ lemma uniform_continuous.comp_uniform_cauchy_seq_on [uniform_space γ] {g : β → γ} (hg : uniform_continuous g) (hf : uniform_cauchy_seq_on F p s) : uniform_cauchy_seq_on (λ n, g ∘ (F n)) p s := λ u hu, hf _ (hg hu) lemma uniform_cauchy_seq_on.prod_map {ι' α' β' : Type*} [uniform_space β'] {F' : ι' → α' → β'} {p' : filter ι'} {s' : set α'} (h : uniform_cauchy_seq_on F p s) (h' : uniform_cauchy_seq_on F' p' s') : uniform_cauchy_seq_on (λ (i : ι × ι'), prod.map (F i.1) (F' i.2)) (p.prod p') (s ×ˢ s') := begin intros u hu, rw [uniformity_prod_eq_prod, mem_map, mem_prod_iff] at hu, obtain ⟨v, hv, w, hw, hvw⟩ := hu, simp_rw [mem_prod, prod_map, and_imp, prod.forall], rw [← set.image_subset_iff] at hvw, apply (tendsto_swap4_prod.eventually ((h v hv).prod_mk (h' w hw))).mono, intros x hx a b ha hb, refine hvw ⟨_, mk_mem_prod (hx.1 a ha) (hx.2 b hb), rfl⟩, end lemma uniform_cauchy_seq_on.prod {ι' β' : Type*} [uniform_space β'] {F' : ι' → α → β'} {p' : filter ι'} (h : uniform_cauchy_seq_on F p s) (h' : uniform_cauchy_seq_on F' p' s) : uniform_cauchy_seq_on (λ (i : ι × ι') a, (F i.fst a, F' i.snd a)) (p ×ᶠ p') s := (congr_arg _ s.inter_self).mp ((h.prod_map h').comp (λ a, (a, a))) lemma uniform_cauchy_seq_on.prod' {β' : Type*} [uniform_space β'] {F' : ι → α → β'} (h : uniform_cauchy_seq_on F p s) (h' : uniform_cauchy_seq_on F' p s) : uniform_cauchy_seq_on (λ (i : ι) a, (F i a, F' i a)) p s := begin intros u hu, have hh : tendsto (λ x : ι, (x, x)) p (p ×ᶠ p), { exact tendsto_diag, }, exact (hh.prod_map hh).eventually ((h.prod h') u hu), end section seq_tendsto lemma tendsto_uniformly_on_of_seq_tendsto_uniformly_on {l : filter ι} [l.is_countably_generated] (h : ∀ u : ℕ → ι, tendsto u at_top l → tendsto_uniformly_on (λ n, F (u n)) f at_top s) : tendsto_uniformly_on F f l s := begin rw [tendsto_uniformly_on_iff_tendsto, tendsto_iff_seq_tendsto], intros u hu, rw tendsto_prod_iff' at hu, specialize h (λ n, (u n).fst) hu.1, rw tendsto_uniformly_on_iff_tendsto at h, have : ((λ (q : ι × α), (f q.snd, F q.fst q.snd)) ∘ u) = (λ (q : ℕ × α), (f q.snd, F ((λ (n : ℕ), (u n).fst) q.fst) q.snd)) ∘ (λ n, (n, (u n).snd)), { ext1 n, simp, }, rw this, refine tendsto.comp h _, rw tendsto_prod_iff', exact ⟨tendsto_id, hu.2⟩, end lemma tendsto_uniformly_on.seq_tendsto_uniformly_on {l : filter ι} (h : tendsto_uniformly_on F f l s) (u : ℕ → ι) (hu : tendsto u at_top l) : tendsto_uniformly_on (λ n, F (u n)) f at_top s := begin rw tendsto_uniformly_on_iff_tendsto at h ⊢, have : (λ (q : ℕ × α), (f q.snd, F (u q.fst) q.snd)) = (λ (q : ι × α), (f q.snd, F q.fst q.snd)) ∘ (λ p : ℕ × α, (u p.fst, p.snd)), { ext1 x, simp, }, rw this, refine h.comp _, rw tendsto_prod_iff', exact ⟨hu.comp tendsto_fst, tendsto_snd⟩, end lemma tendsto_uniformly_on_iff_seq_tendsto_uniformly_on {l : filter ι} [l.is_countably_generated] : tendsto_uniformly_on F f l s ↔ ∀ u : ℕ → ι, tendsto u at_top l → tendsto_uniformly_on (λ n, F (u n)) f at_top s := ⟨tendsto_uniformly_on.seq_tendsto_uniformly_on, tendsto_uniformly_on_of_seq_tendsto_uniformly_on⟩ lemma tendsto_uniformly_iff_seq_tendsto_uniformly {l : filter ι} [l.is_countably_generated] : tendsto_uniformly F f l ↔ ∀ u : ℕ → ι, tendsto u at_top l → tendsto_uniformly (λ n, F (u n)) f at_top := begin simp_rw ← tendsto_uniformly_on_univ, exact tendsto_uniformly_on_iff_seq_tendsto_uniformly_on, end end seq_tendsto variable [topological_space α] /-- A sequence of functions `Fₙ` converges locally uniformly on a set `s` to a limiting function `f` with respect to a filter `p` if, for any entourage of the diagonal `u`, for any `x ∈ s`, one has `p`-eventually `(f y, Fₙ y) ∈ u` for all `y` in a neighborhood of `x` in `s`. -/ def tendsto_locally_uniformly_on (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) := ∀ u ∈ 𝓤 β, ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u /-- A sequence of functions `Fₙ` converges locally uniformly to a limiting function `f` with respect to a filter `p` if, for any entourage of the diagonal `u`, for any `x`, one has `p`-eventually `(f y, Fₙ y) ∈ u` for all `y` in a neighborhood of `x`. -/ def tendsto_locally_uniformly (F : ι → α → β) (f : α → β) (p : filter ι) := ∀ u ∈ 𝓤 β, ∀ (x : α), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u lemma tendsto_locally_uniformly_on_iff_tendsto_locally_uniformly_comp_coe : tendsto_locally_uniformly_on F f p s ↔ tendsto_locally_uniformly (λ i (x : s), F i x) (f ∘ coe) p := begin refine forall₂_congr (λ V hV, _), simp only [exists_prop, function.comp_app, set_coe.forall, subtype.coe_mk], refine forall₂_congr (λ x hx, ⟨_, _⟩), { rintro ⟨t, ht₁, ht₂⟩, obtain ⟨u, hu₁, hu₂⟩ := mem_nhds_within_iff_exists_mem_nhds_inter.mp ht₁, exact ⟨coe⁻¹' u, (mem_nhds_subtype _ _ _).mpr ⟨u, hu₁, rfl.subset⟩, ht₂.mono (λ i hi y hy₁ hy₂, hi y (hu₂ ⟨hy₂, hy₁⟩))⟩, }, { rintro ⟨t, ht₁, ht₂⟩, obtain ⟨u, hu₁, hu₂⟩ := (mem_nhds_subtype _ _ _).mp ht₁, exact ⟨u ∩ s, mem_nhds_within_iff_exists_mem_nhds_inter.mpr ⟨u, hu₁, rfl.subset⟩, ht₂.mono (λ i hi y hy, hi y hy.2 (hu₂ (by simp [hy.1])))⟩, }, end lemma tendsto_locally_uniformly_iff_forall_tendsto : tendsto_locally_uniformly F f p ↔ ∀ x, tendsto (λ (y : ι × α), (f y.2, F y.1 y.2)) (p ×ᶠ (𝓝 x)) (𝓤 β) := begin simp only [tendsto_locally_uniformly, filter.forall_in_swap, tendsto_def, mem_prod_iff, set.prod_subset_iff], refine forall₃_congr (λ x u hu, ⟨_, _⟩), { rintros ⟨n, hn, hp⟩, exact ⟨_, hp, n, hn, λ i hi a ha, hi a ha⟩, }, { rintros ⟨I, hI, n, hn, hu⟩, exact ⟨n, hn, by filter_upwards [hI] using hu⟩, }, end protected lemma tendsto_uniformly_on.tendsto_locally_uniformly_on (h : tendsto_uniformly_on F f p s) : tendsto_locally_uniformly_on F f p s := λ u hu x hx,⟨s, self_mem_nhds_within, by simpa using (h u hu)⟩ protected lemma tendsto_uniformly.tendsto_locally_uniformly (h : tendsto_uniformly F f p) : tendsto_locally_uniformly F f p := λ u hu x, ⟨univ, univ_mem, by simpa using (h u hu)⟩ lemma tendsto_locally_uniformly_on.mono (h : tendsto_locally_uniformly_on F f p s) (h' : s' ⊆ s) : tendsto_locally_uniformly_on F f p s' := begin assume u hu x hx, rcases h u hu x (h' hx) with ⟨t, ht, H⟩, exact ⟨t, nhds_within_mono x h' ht, H.mono (λ n, id)⟩ end lemma tendsto_locally_uniformly_on_univ : tendsto_locally_uniformly_on F f p univ ↔ tendsto_locally_uniformly F f p := by simp [tendsto_locally_uniformly_on, tendsto_locally_uniformly, nhds_within_univ] protected lemma tendsto_locally_uniformly.tendsto_locally_uniformly_on (h : tendsto_locally_uniformly F f p) : tendsto_locally_uniformly_on F f p s := (tendsto_locally_uniformly_on_univ.mpr h).mono (subset_univ _) /-- On a compact space, locally uniform convergence is just uniform convergence. -/ lemma tendsto_locally_uniformly_iff_tendsto_uniformly_of_compact_space [compact_space α] : tendsto_locally_uniformly F f p ↔ tendsto_uniformly F f p := begin refine ⟨λ h V hV, _, tendsto_uniformly.tendsto_locally_uniformly⟩, choose U hU using h V hV, obtain ⟨t, ht⟩ := compact_univ.elim_nhds_subcover' (λ k hk, U k) (λ k hk, (hU k).1), replace hU := λ (x : t), (hU x).2, rw ← eventually_all at hU, refine hU.mono (λ i hi x, _), specialize ht (mem_univ x), simp only [exists_prop, mem_Union, set_coe.exists, exists_and_distrib_right,subtype.coe_mk] at ht, obtain ⟨y, ⟨hy₁, hy₂⟩, hy₃⟩ := ht, exact hi ⟨⟨y, hy₁⟩, hy₂⟩ x hy₃, end /-- For a compact set `s`, locally uniform convergence on `s` is just uniform convergence on `s`. -/ lemma tendsto_locally_uniformly_on_iff_tendsto_uniformly_on_of_compact (hs : is_compact s) : tendsto_locally_uniformly_on F f p s ↔ tendsto_uniformly_on F f p s := begin haveI : compact_space s := is_compact_iff_compact_space.mp hs, refine ⟨λ h, _, tendsto_uniformly_on.tendsto_locally_uniformly_on⟩, rwa [tendsto_locally_uniformly_on_iff_tendsto_locally_uniformly_comp_coe, tendsto_locally_uniformly_iff_tendsto_uniformly_of_compact_space, ← tendsto_uniformly_on_iff_tendsto_uniformly_comp_coe] at h, end lemma tendsto_locally_uniformly_on.comp [topological_space γ] {t : set γ} (h : tendsto_locally_uniformly_on F f p s) (g : γ → α) (hg : maps_to g t s) (cg : continuous_on g t) : tendsto_locally_uniformly_on (λ n, (F n) ∘ g) (f ∘ g) p t := begin assume u hu x hx, rcases h u hu (g x) (hg hx) with ⟨a, ha, H⟩, have : g⁻¹' a ∈ 𝓝[t] x := ((cg x hx).preimage_mem_nhds_within' (nhds_within_mono (g x) hg.image_subset ha)), exact ⟨g ⁻¹' a, this, H.mono (λ n hn y hy, hn _ hy)⟩ end lemma tendsto_locally_uniformly.comp [topological_space γ] (h : tendsto_locally_uniformly F f p) (g : γ → α) (cg : continuous g) : tendsto_locally_uniformly (λ n, (F n) ∘ g) (f ∘ g) p := begin rw ← tendsto_locally_uniformly_on_univ at h ⊢, rw continuous_iff_continuous_on_univ at cg, exact h.comp _ (maps_to_univ _ _) cg end /-! ### Uniform approximation In this section, we give lemmas ensuring that a function is continuous if it can be approximated uniformly by continuous functions. We give various versions, within a set or the whole space, at a single point or at all points, with locally uniform approximation or uniform approximation. All the statements are derived from a statement about locally uniform approximation within a set at a point, called `continuous_within_at_of_locally_uniform_approx_of_continuous_within_at`. -/ /-- A function which can be locally uniformly approximated by functions which are continuous within a set at a point is continuous within this set at this point. -/ lemma continuous_within_at_of_locally_uniform_approx_of_continuous_within_at (hx : x ∈ s) (L : ∀ u ∈ 𝓤 β, ∃ (t ∈ 𝓝[s] x) (F : α → β), continuous_within_at F s x ∧ ∀ y ∈ t, (f y, F y) ∈ u) : continuous_within_at f s x := begin apply uniform.continuous_within_at_iff'_left.2 (λ u₀ hu₀, _), obtain ⟨u₁, h₁, u₁₀⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β), comp_rel u u ⊆ u₀ := comp_mem_uniformity_sets hu₀, obtain ⟨u₂, h₂, hsymm, u₂₁⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β), (∀{a b}, (a, b) ∈ u → (b, a) ∈ u) ∧ comp_rel u u ⊆ u₁ := comp_symm_of_uniformity h₁, rcases L u₂ h₂ with ⟨t, tx, F, hFc, hF⟩, have A : ∀ᶠ y in 𝓝[s] x, (f y, F y) ∈ u₂ := eventually.mono tx hF, have B : ∀ᶠ y in 𝓝[s] x, (F y, F x) ∈ u₂ := uniform.continuous_within_at_iff'_left.1 hFc h₂, have C : ∀ᶠ y in 𝓝[s] x, (f y, F x) ∈ u₁ := (A.and B).mono (λ y hy, u₂₁ (prod_mk_mem_comp_rel hy.1 hy.2)), have : (F x, f x) ∈ u₁ := u₂₁ (prod_mk_mem_comp_rel (refl_mem_uniformity h₂) (hsymm (A.self_of_nhds_within hx))), exact C.mono (λ y hy, u₁₀ (prod_mk_mem_comp_rel hy this)) end /-- A function which can be locally uniformly approximated by functions which are continuous at a point is continuous at this point. -/ lemma continuous_at_of_locally_uniform_approx_of_continuous_at (L : ∀ u ∈ 𝓤 β, ∃ (t ∈ 𝓝 x) F, continuous_at F x ∧ ∀ y ∈ t, (f y, F y) ∈ u) : continuous_at f x := begin rw ← continuous_within_at_univ, apply continuous_within_at_of_locally_uniform_approx_of_continuous_within_at (mem_univ _) _, simpa only [exists_prop, nhds_within_univ, continuous_within_at_univ] using L end /-- A function which can be locally uniformly approximated by functions which are continuous on a set is continuous on this set. -/ lemma continuous_on_of_locally_uniform_approx_of_continuous_within_at (L : ∀ (x ∈ s) (u ∈ 𝓤 β), ∃ (t ∈ 𝓝[s] x) F, continuous_within_at F s x ∧ ∀ y ∈ t, (f y, F y) ∈ u) : continuous_on f s := λ x hx, continuous_within_at_of_locally_uniform_approx_of_continuous_within_at hx (L x hx) /-- A function which can be uniformly approximated by functions which are continuous on a set is continuous on this set. -/ lemma continuous_on_of_uniform_approx_of_continuous_on (L : ∀ u ∈ 𝓤 β, ∃ F, continuous_on F s ∧ ∀ y ∈ s, (f y, F y) ∈ u) : continuous_on f s := continuous_on_of_locally_uniform_approx_of_continuous_within_at $ λ x hx u hu, ⟨s, self_mem_nhds_within, (L u hu).imp $ λ F hF, ⟨hF.1.continuous_within_at hx, hF.2⟩⟩ /-- A function which can be locally uniformly approximated by continuous functions is continuous. -/ lemma continuous_of_locally_uniform_approx_of_continuous_at (L : ∀ (x : α), ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝 x, ∃ F, continuous_at F x ∧ ∀ y ∈ t, (f y, F y) ∈ u) : continuous f := continuous_iff_continuous_at.2 $ λ x, continuous_at_of_locally_uniform_approx_of_continuous_at (L x) /-- A function which can be uniformly approximated by continuous functions is continuous. -/ lemma continuous_of_uniform_approx_of_continuous (L : ∀ u ∈ 𝓤 β, ∃ F, continuous F ∧ ∀ y, (f y, F y) ∈ u) : continuous f := continuous_iff_continuous_on_univ.mpr $ continuous_on_of_uniform_approx_of_continuous_on $ by simpa [continuous_iff_continuous_on_univ] using L /-! ### Uniform limits From the previous statements on uniform approximation, we deduce continuity results for uniform limits. -/ /-- A locally uniform limit on a set of functions which are continuous on this set is itself continuous on this set. -/ protected lemma tendsto_locally_uniformly_on.continuous_on (h : tendsto_locally_uniformly_on F f p s) (hc : ∀ᶠ n in p, continuous_on (F n) s) [ne_bot p] : continuous_on f s := begin apply continuous_on_of_locally_uniform_approx_of_continuous_within_at (λ x hx u hu, _), rcases h u hu x hx with ⟨t, ht, H⟩, rcases (hc.and H).exists with ⟨n, hFc, hF⟩, exact ⟨t, ht, ⟨F n, hFc.continuous_within_at hx, hF⟩⟩ end /-- A uniform limit on a set of functions which are continuous on this set is itself continuous on this set. -/ protected lemma tendsto_uniformly_on.continuous_on (h : tendsto_uniformly_on F f p s) (hc : ∀ᶠ n in p, continuous_on (F n) s) [ne_bot p] : continuous_on f s := h.tendsto_locally_uniformly_on.continuous_on hc /-- A locally uniform limit of continuous functions is continuous. -/ protected lemma tendsto_locally_uniformly.continuous (h : tendsto_locally_uniformly F f p) (hc : ∀ᶠ n in p, continuous (F n)) [ne_bot p] : continuous f := continuous_iff_continuous_on_univ.mpr $ h.tendsto_locally_uniformly_on.continuous_on $ hc.mono $ λ n hn, hn.continuous_on /-- A uniform limit of continuous functions is continuous. -/ protected lemma tendsto_uniformly.continuous (h : tendsto_uniformly F f p) (hc : ∀ᶠ n in p, continuous (F n)) [ne_bot p] : continuous f := h.tendsto_locally_uniformly.continuous hc /-! ### Composing limits under uniform convergence In general, if `Fₙ` converges pointwise to a function `f`, and `gₙ` tends to `x`, it is not true that `Fₙ gₙ` tends to `f x`. It is true however if the convergence of `Fₙ` to `f` is uniform. In this paragraph, we prove variations around this statement. -/ /-- If `Fₙ` converges locally uniformly on a neighborhood of `x` within a set `s` to a function `f` which is continuous at `x` within `s `, and `gₙ` tends to `x` within `s`, then `Fₙ (gₙ)` tends to `f x`. -/ lemma tendsto_comp_of_locally_uniform_limit_within (h : continuous_within_at f s x) (hg : tendsto g p (𝓝[s] x)) (hunif : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := begin apply uniform.tendsto_nhds_right.2 (λ u₀ hu₀, _), obtain ⟨u₁, h₁, u₁₀⟩ : ∃ (u : set (β × β)) (H : u ∈ 𝓤 β), comp_rel u u ⊆ u₀ := comp_mem_uniformity_sets hu₀, rcases hunif u₁ h₁ with ⟨s, sx, hs⟩, have A : ∀ᶠ n in p, g n ∈ s := hg sx, have B : ∀ᶠ n in p, (f x, f (g n)) ∈ u₁ := hg (uniform.continuous_within_at_iff'_right.1 h h₁), refine ((hs.and A).and B).mono (λ y hy, _), rcases hy with ⟨⟨H1, H2⟩, H3⟩, exact u₁₀ (prod_mk_mem_comp_rel H3 (H1 _ H2)) end /-- If `Fₙ` converges locally uniformly on a neighborhood of `x` to a function `f` which is continuous at `x`, and `gₙ` tends to `x`, then `Fₙ (gₙ)` tends to `f x`. -/ lemma tendsto_comp_of_locally_uniform_limit (h : continuous_at f x) (hg : tendsto g p (𝓝 x)) (hunif : ∀ u ∈ 𝓤 β, ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, (f y, F n y) ∈ u) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := begin rw ← continuous_within_at_univ at h, rw ← nhds_within_univ at hunif hg, exact tendsto_comp_of_locally_uniform_limit_within h hg hunif end /-- If `Fₙ` tends locally uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s` and `x ∈ s`. -/ lemma tendsto_locally_uniformly_on.tendsto_comp (h : tendsto_locally_uniformly_on F f p s) (hf : continuous_within_at f s x) (hx : x ∈ s) (hg : tendsto g p (𝓝[s] x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := tendsto_comp_of_locally_uniform_limit_within hf hg (λ u hu, h u hu x hx) /-- If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s`. -/ lemma tendsto_uniformly_on.tendsto_comp (h : tendsto_uniformly_on F f p s) (hf : continuous_within_at f s x) (hg : tendsto g p (𝓝[s] x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := tendsto_comp_of_locally_uniform_limit_within hf hg (λ u hu, ⟨s, self_mem_nhds_within, h u hu⟩) /-- If `Fₙ` tends locally uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/ lemma tendsto_locally_uniformly.tendsto_comp (h : tendsto_locally_uniformly F f p) (hf : continuous_at f x) (hg : tendsto g p (𝓝 x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := tendsto_comp_of_locally_uniform_limit hf hg (λ u hu, h u hu x) /-- If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/ lemma tendsto_uniformly.tendsto_comp (h : tendsto_uniformly F f p) (hf : continuous_at f x) (hg : tendsto g p (𝓝 x)) : tendsto (λ n, F n (g n)) p (𝓝 (f x)) := h.tendsto_locally_uniformly.tendsto_comp hf hg
6266a6c4015d5de697f9de36e269c0bcd1298cd6
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world06/level05.lean
23d74b91205f9e5afb00454616f98739c27d3103
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
79
lean
example (P Q : Prop) : P → (Q → P) := begin intro p, intro q, exact p, end
908b12bf3e8c686df18dc5b257afcdcde52956a1
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/field_theory/adjoin.lean
5b69a28fc9249a851691d225d863d27382648b8f
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
5,441
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning and Patrick Lutz -/ import deprecated.subfield import linear_algebra.finite_dimensional /-! # Adjoining Elements to Fields In this file we introduce the notion of adjoining elements to fields. This isn't quite the same as adjoining elements to rings. For example, `algebra.adjoin K {x}` might not include `x⁻¹`. ## Main results (This is just a start; we've got more to add, including a proof of the Primitive Element Theorem.) - `adjoin_adjoin_left`: adjoining S and then T is the same as adjoining S ∪ T. ## Notation - `F⟮α⟯`: adjoin a single element `α` to `F`. -/ namespace field variables (F : Type*) [field F] {E : Type*} [field E] [algebra F E] (S : set E) /-- `adjoin F S` extends a field `F` by adjoining a set `S ⊆ E`. -/ def adjoin : subalgebra F E := { carrier := { carrier := field.closure (set.range (algebra_map F E) ∪ S), one_mem' := is_submonoid.one_mem, mul_mem' := λ x y, is_submonoid.mul_mem, zero_mem' := is_add_submonoid.zero_mem, add_mem' := λ x y, is_add_submonoid.add_mem }, algebra_map_mem' := λ x, field.mem_closure (or.inl (set.mem_range.mpr ⟨x,rfl⟩)) } lemma adjoin.algebra_map_mem (x : F) : algebra_map F E x ∈ adjoin F S := field.mem_closure (or.inl (set.mem_range_self x)) lemma subset_adjoin_of_subset_left {F : set E} {HF : is_subfield F} {T : set E} (HT : T ⊆ F) : T ⊆ adjoin F S := λ x hx, adjoin.algebra_map_mem F S ⟨x, HT hx⟩ lemma adjoin.range_algebra_map_subset : set.range (algebra_map F E) ⊆ adjoin F S := begin intros x hx, cases hx with f hf, rw ← hf, exact adjoin.algebra_map_mem F S f, end instance adjoin.field_coe : has_coe_t F (adjoin F S) := {coe := λ x, ⟨algebra_map F E x, adjoin.algebra_map_mem F S x⟩} lemma subset_adjoin : S ⊆ adjoin F S := λ x hx, field.mem_closure (or.inr hx) instance adjoin.set_coe : has_coe_t S (adjoin F S) := {coe := λ x, ⟨x,subset_adjoin F S (subtype.mem x)⟩} lemma adjoin.mono (T : set E) (h : S ⊆ T) : (adjoin F S : set E) ⊆ adjoin F T := field.closure_mono (set.union_subset (set.subset_union_left _ _) (set.subset_union_of_subset_right h _)) instance adjoin.is_subfield : is_subfield (adjoin F S : set E) := field.closure.is_subfield --Lean has trouble figuring this out on its own instance adjoin.is_field : field (adjoin F S) := @is_subfield.field E _ ((adjoin F S) : set E) _ lemma adjoin_contains_field_as_subfield (F : set E) {HF : is_subfield F} : F ⊆ adjoin F S := λ x hx, adjoin.algebra_map_mem F S ⟨x, hx⟩ lemma subset_adjoin_of_subset_right {T : set E} (H : T ⊆ S) : T ⊆ adjoin F S := begin intros x hx, exact subset_adjoin F S (H hx), end /-- If `K` is a field with `F ⊆ K` and `S ⊆ K` then `adjoin F S ⊆ K`. -/ lemma adjoin_subset_subfield {K : set E} [is_subfield K] (HF : set.range (algebra_map F E) ⊆ K) (HS : S ⊆ K) : (adjoin F S : set E) ⊆ K := begin apply field.closure_subset, rw set.union_subset_iff, exact ⟨HF, HS⟩, end /-- `S ⊆ adjoin F T` if and only if `adjoin F S ⊆ adjoin F T`. -/ lemma adjoin_subset_iff {T : set E} : S ⊆ adjoin F T ↔ (adjoin F S : set E) ⊆ adjoin F T := ⟨λ h, adjoin_subset_subfield F S (adjoin.range_algebra_map_subset F T) h, λ h, set.subset.trans (subset_adjoin F S) h⟩ lemma subfield_subset_adjoin_self {F : set E} {HF : is_subfield F} {T : set E} {HT : T ⊆ F} : T ⊆ adjoin F S := λ x hx, adjoin.algebra_map_mem F S ⟨x,HT hx⟩ lemma adjoin_subset_adjoin_iff {F' : Type*} [field F'] [algebra F' E] {S S' : set E} : (adjoin F S : set E) ⊆ adjoin F' S' ↔ set.range (algebra_map F E) ⊆ adjoin F' S' ∧ S ⊆ adjoin F' S' := ⟨λ h, ⟨trans (adjoin.range_algebra_map_subset _ _) h, trans (subset_adjoin _ _) h⟩, λ ⟨hF, hS⟩, field.closure_subset (set.union_subset hF hS)⟩ /-- `F[S][T] = F[S ∪ T]` -/ lemma adjoin_adjoin_left (T : set E) : (adjoin (adjoin F S : set E) T : set E) = adjoin F (S ∪ T) := begin apply set.eq_of_subset_of_subset; rw adjoin_subset_adjoin_iff; split, { exact algebra.set_range_subset (adjoin.mono _ _ _ (set.subset_union_left _ _)) }, { exact subset_adjoin_of_subset_right _ _ (set.subset_union_right _ _) }, { exact subset_adjoin_of_subset_left _ (adjoin.range_algebra_map_subset _ _) }, { exact set.union_subset (subset_adjoin_of_subset_left _ (subset_adjoin _ _)) (subset_adjoin _ _) }, end variables (α : E) notation K`⟮`:std.prec.max_plus l:(foldr `, ` (h t, set.insert h t) ∅) `⟯` := adjoin K l --unfortunately this lemma is not definitionally true lemma adjoin_singleton : F⟮α⟯ = adjoin F {α} := begin change adjoin F (insert α ∅) = adjoin F {α}, rw insert_emptyc_eq α, exact set.is_lawful_singleton, end lemma mem_adjoin_simple_self : α ∈ F⟮α⟯ := begin rw adjoin_singleton, exact subset_adjoin F {α} (set.mem_singleton α), end /-- generator of `F⟮α⟯` -/ def adjoin_simple.gen : F⟮α⟯ := ⟨α, mem_adjoin_simple_self F α⟩ lemma adjoin_simple.algebra_map_gen : algebra_map F⟮α⟯ E (adjoin_simple.gen F α) = α := rfl lemma adjoin_simple_adjoin_simple (β : E) : F⟮α,β⟯ = adjoin F {α,β} := begin change adjoin F (insert α (insert β ∅)) = adjoin F _, simp only [insert_emptyc_eq], end end field
6e8b382704b74cf1137607da3c270cf3765acb9a
367134ba5a65885e863bdc4507601606690974c1
/src/data/complex/exponential.lean
fdd84a942aa6c221adafaf4422870c1efa29a43a
[ "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
61,477
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 -/ import algebra.geom_sum import data.nat.choose.sum import data.complex.basic /-! # Exponential, trigonometric and hyperbolic trigonometric functions This file contains the definitions of the real and complex exponential, sine, cosine, tangent, hyperbolic sine, hyperbolic cosine, and hyperbolic tangent functions. -/ local notation `abs'` := _root_.abs open is_absolute_value open_locale classical big_operators nat section open real is_absolute_value finset lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end section variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - l •ℕ ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - (k + (k + 1)) •ℕ ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_nsmul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - l •ℕ ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - (nat.pred l) •ℕ ε : hi.2 ... = a - l •ℕ ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, ∑ i in range n, g i) → is_cau_seq abv (λ n, ∑ i in range n, f i) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le (∑ k in range j, g k) (∑ k in range i, g k) (∑ k in range (max n i), g k), have := add_lt_add hi₁ hi₂, rw [abs_sub (∑ k in range (max n i), g k), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, simp only [nat.succ_add, sum_range_succ, sub_eq_add_neg, add_assoc], refine le_trans (abv_add _ _ _) _, simp only [sub_eq_add_neg] at hi, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, ∑ n in range m, abv (f n)) → is_cau_seq abv (λ m, ∑ n in range m, f n) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) end no_archimedean section variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, ∑ m in range n, x ^ m) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv] {eta := ff}, have : (λ (m : ℕ), ∑ n in range m, (abv x) ^ n) = λ m, geom_series (abv x) m := rfl, simp only [this, geom_sum hx1'] {eta := ff}, conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] }, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)), refine div_nonneg (sub_nonneg.2 _) (sub_nonneg.2 $ le_of_lt hx1), clear hn, induction n with n ih, { simp }, { rw [pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_sub_left _ _), rw [← one_mul (_ ^ n), pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, ∑ n in range m, a * x ^ n) := have is_cau_seq abs (λ m, a * ∑ n in range m, x ^ n) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, ∑ n in range m, f n) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : ∑ m in range n, ∑ k in range (m + 1), f k (m - k) = ∑ m in range n, ∑ k in range (n - m), f m k := by rw [sum_sigma', sum_sigma']; exact sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : ∑ k in range m, f k - ∑ k in range n, f k = ∑ k in (range m).filter (λ k, n ≤ k), f k := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (∑ k in s, f k) ≤ ∑ k in s, abv (f k) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, ∑ n in range m, abv (a n))) (hb : is_cau_seq abv (λ m, ∑ n in range m, b n)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((∑ k in range j, a k) * (∑ k in range j, b k) - ∑ n in range j, ∑ m in range (n + 1), a m * b (n - m)) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : ∑ m in range K, ∑ k in range (m + 1), a k * b (m - k) = ∑ m in range K, ∑ n in range (K - m), a m * b n, by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, ∑ k in range (K - i), a i * b k) = (λ i, a i * ∑ k in range (K - i), b k), by simp [finset.mul_sum], have h₃ : ∑ i in range K, a i * ∑ k in range (K - i), b k = ∑ i in range K, a i * (∑ k in range (K - i), b k - ∑ k in range K, b k) + ∑ i in range K, a i * ∑ k in range K, b k, by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in range (max N M + 1), abv (a i) * (ε / (2 * P)), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : ∑ n in range (max N M + 1), abv (a n) < P := calc ∑ n in range (max N M + 1), abv (a n) = abs (∑ n in range (max N M + 1), abv (a n)) : eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) + (∑ i in range K, abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) - ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * (2 * Q) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end no_archimedean end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, ∑ m in range n, abs (z ^ m / m!)) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg (complex.abs_nonneg _) (le_of_lt hn0)) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.factorial_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, ∑ m in range n, z ^ m / m!) := is_cau_series_of_abv_cau (is_cau_abs_exp z) /-- The Cauchy sequence consisting of partial sums of the Taylor series of the complex exponential function -/ @[pp_nodot] def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, ∑ m in range n, z ^ m / m!, is_cau_exp z⟩ /-- The complex exponential function, defined via its Taylor series -/ @[pp_nodot] def exp (z : ℂ) : ℂ := lim (exp' z) /-- The complex sine function, defined via `exp` -/ @[pp_nodot] def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 /-- The complex cosine function, defined via `exp` -/ @[pp_nodot] def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 /-- The complex tangent function, defined as `sin z / cos z` -/ @[pp_nodot] def tan (z : ℂ) : ℂ := sin z / cos z /-- The complex hyperbolic sine function, defined via `exp` -/ @[pp_nodot] def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 /-- The complex hyperbolic cosine function, defined via `exp` -/ @[pp_nodot] def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 /-- The complex hyperbolic tangent function, defined as `sinh z / cosh z` -/ @[pp_nodot] def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex /-- The real exponential function, defined as the real part of the complex exponential -/ @[pp_nodot] def exp (x : ℝ) : ℝ := (exp x).re /-- The real sine function, defined as the real part of the complex sine -/ @[pp_nodot] def sin (x : ℝ) : ℝ := (sin x).re /-- The real cosine function, defined as the real part of the complex cosine -/ @[pp_nodot] def cos (x : ℝ) : ℝ := (cos x).re /-- The real tangent function, defined as the real part of the complex tangent -/ @[pp_nodot] def tan (x : ℝ) : ℝ := (tan x).re /-- The real hypebolic sine function, defined as the real part of the complex hyperbolic sine -/ @[pp_nodot] def sinh (x : ℝ) : ℝ := (sinh x).re /-- The real hypebolic cosine function, defined as the real part of the complex hyperbolic cosine -/ @[pp_nodot] def cosh (x : ℝ) : ℝ := (cosh x).re /-- The real hypebolic tangent function, defined as the real part of the complex hyperbolic tangent -/ @[pp_nodot] def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, ∑ m in range j, (x + y) ^ m / m! = ∑ i in range j, ∑ k in range (i + 1), x ^ k / k! * (y ^ (i - k) / (i - k)!), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (m.choose i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := nat.choose_mul_factorial_mul_factorial (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (m.choose i : ℂ), mul_assoc, mul_left_comm (m.choose i : ℂ)⁻¹, mul_comm (m.choose i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) attribute [irreducible] complex.exp lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod α (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← mul_right_inj' (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw conj.map_sum, refine sum_congr rfl (λ n hn, _), rw [conj.map_div, conj.map_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real] @[simp, norm_cast] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := of_real_exp_of_real_re _ @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := by rw [← of_real_exp_of_real_re, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl lemma two_sinh : 2 * sinh x = exp x - exp (-x) := mul_div_cancel' _ two_ne_zero' lemma two_cosh : 2 * cosh x = exp x + exp (-x) := mul_div_cancel' _ two_ne_zero' @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] private lemma sinh_add_aux {a b c d : ℂ} : (a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, ← mul_assoc, two_cosh], exact sinh_add_aux end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [add_comm, cosh, exp_neg] private lemma cosh_add_aux {a b c d : ℂ} : (a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, mul_left_comm, two_sinh], exact cosh_add_aux end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_sub, sinh, conj.map_div, conj_bit0, conj.map_one] @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := of_real_sinh_of_real_re _ @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := by rw [← of_real_sinh_of_real_re, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl lemma cosh_conj : cosh (conj x) = conj (cosh x) := begin rw [cosh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_add, cosh, conj.map_div, conj_bit0, conj.map_one] end @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := of_real_cosh_of_real_re _ @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := by rw [← of_real_cosh_of_real_re, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj.map_div, tanh] @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := of_real_tanh_of_real_re _ @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := by rw [← of_real_tanh_of_real_re, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, two_cosh, two_sinh, add_add_sub_cancel, two_mul] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw [add_comm, cosh_add_sinh] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_sub, two_cosh, two_sinh, add_sub_sub_cancel, two_mul] lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero] lemma cosh_square : cosh x ^ 2 = sinh x ^ 2 + 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma sinh_square : sinh x ^ 2 = cosh x ^ 2 - 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw [two_mul, cosh_add, pow_two, pow_two] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := begin rw [two_mul, sinh_add], ring end lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cosh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : sinh x * (2 * sinh x * cosh x) = 2 * cosh x * sinh x ^ 2, by ring, rw [h2, sinh_square], ring end lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sinh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : cosh x * (2 * sinh x * cosh x) = 2 * sinh x * cosh x ^ 2, by ring, rw [h2, cosh_square], ring, end @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul] lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I := mul_div_cancel' _ two_ne_zero' lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) := mul_div_cancel' _ two_ne_zero' lemma sinh_mul_I : sinh (x * I) = sin x * I := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, ← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one, neg_sub, neg_mul_eq_neg_mul] lemma cosh_mul_I : cosh (x * I) = cos x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, two_cos, neg_mul_eq_neg_mul] lemma tanh_mul_I : tanh (x * I) = tan x * I := by rw [tanh_eq_sinh_div_cosh, cosh_mul_I, sinh_mul_I, mul_div_right_comm, tan] lemma cos_mul_I : cos (x * I) = cosh x := by rw ← cosh_mul_I; ring; simp lemma sin_mul_I : sin (x * I) = sinh x * I := have h : I * sin (x * I) = -sinh x := by { rw [mul_comm, ← sinh_mul_I], ring, simp }, by simpa only [neg_mul_eq_neg_mul_symm, div_I, neg_neg] using cancel_factors.cancel_factors_eq_div h I_ne_zero lemma tan_mul_I : tan (x * I) = tanh x * I := by rw [tan, sin_mul_I, cos_mul_I, mul_div_right_comm, tanh_eq_sinh_div_cosh] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, add_mul, add_mul, mul_right_comm, ← sinh_mul_I, mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, sub_eq_add_neg, exp_neg, add_comm] private lemma cos_add_aux {a b c d : ℂ} : (a + b) * (c + d) - (b - a) * (d - c) * (-1) = 2 * (a * c + b * d) := by ring lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I, sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I, mul_neg_one, sub_eq_add_neg] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_add_mul_I (x y : ℂ) : sin (x + y*I) = sin x * cosh y + cos x * sinh y * I := by rw [sin_add, cos_mul_I, sin_mul_I, mul_assoc] lemma sin_eq (z : ℂ) : sin z = sin z.re * cosh z.im + cos z.re * sinh z.im * I := by convert sin_add_mul_I z.re z.im; exact (re_add_im z).symm lemma cos_add_mul_I (x y : ℂ) : cos (x + y*I) = cos x * cosh y - sin x * sinh y * I := by rw [cos_add, cos_mul_I, sin_mul_I, mul_assoc] lemma cos_eq (z : ℂ) : cos z = cos z.re * cosh z.im - sin z.re * sinh z.im * I := by convert cos_add_mul_I z.re z.im; exact (re_add_im z).symm theorem sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) := begin have s1 := sin_add ((x + y) / 2) ((x - y) / 2), have s2 := sin_sub ((x + y) / 2) ((x - y) / 2), rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1, rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2, rw [s1, s2], ring end theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) := begin have s1 := cos_add ((x + y) / 2) ((x - y) / 2), have s2 := cos_sub ((x + y) / 2) ((x - y) / 2), rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1, rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2, rw [s1, s2], ring, end lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) := begin have h2 : (2:ℂ) ≠ 0 := by norm_num, calc cos x + cos y = cos ((x + y) / 2 + (x - y) / 2) + cos ((x + y) / 2 - (x - y) / 2) : _ ... = (cos ((x + y) / 2) * cos ((x - y) / 2) - sin ((x + y) / 2) * sin ((x - y) / 2)) + (cos ((x + y) / 2) * cos ((x - y) / 2) + sin ((x + y) / 2) * sin ((x - y) / 2)) : _ ... = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) : _, { congr; field_simp [h2]; ring }, { rw [cos_add, cos_sub] }, ring, end lemma sin_conj : sin (conj x) = conj (sin x) := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, ← conj_neg_I, ← conj.map_mul, ← conj.map_mul, sinh_conj, mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm] @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := of_real_sin_of_real_re _ @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := by rw [← of_real_sin_of_real_re, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl lemma cos_conj : cos (conj x) = conj (cos x) := by rw [← cosh_mul_I, ← conj_neg_I, ← conj.map_mul, ← cosh_mul_I, cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg] @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := of_real_cos_of_real_re _ @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := by rw [← of_real_cos_of_real_re, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl lemma tan_mul_cos {x : ℂ} (hx : cos x ≠ 0) : tan x * cos x = sin x := by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj.map_div, tan] @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := of_real_tan_of_real_re _ @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := by rw [← of_real_tan_of_real_re, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) := by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I] lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) := by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I] @[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := eq.trans (by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm]) (cosh_sq_sub_sinh_sq (x * I)) @[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 := by rw [add_comm, sin_sq_add_cos_sq] lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw [two_mul, cos_add, ← pow_two, ← pow_two] lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x), ← sub_add, sub_add_eq_add_sub, two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div] lemma cos_square' : cos x ^ 2 = 1 - sin x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel'] lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel] lemma inv_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 := have cos x ^ 2 ≠ 0, from pow_ne_zero 2 hx, by { rw [tan_eq_sin_div_cos, div_pow], field_simp [this] } lemma tan_sq_div_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 := by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul] lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cos_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, mul_add, mul_sub, mul_one, pow_two], have h2 : 4 * cos x ^ 3 = 2 * cos x * cos x * cos x + 2 * cos x * cos x ^ 2, by ring, rw [h2, cos_square'], ring end lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sin_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, cos_square'], have h2 : cos x * (2 * sin x * cos x) = 2 * sin x * cos x ^ 2, by ring, rw [h2, cos_square'], ring end lemma exp_mul_I : exp (x * I) = cos x + sin x * I := (cos_add_sin_I _).symm lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] lemma exp_re : (exp x).re = real.exp x.re * real.cos x.im := by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, cos_of_real_re] } lemma exp_im : (exp x).im = real.exp x.re * real.sin x.im := by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, sin_of_real_re] } /-- De Moivre's formula -/ theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I := begin rw [← exp_mul_I, ← exp_mul_I], induction n with n ih, { rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] }, { rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] } end end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod α (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) := begin rw ← of_real_inj, simp only [sin, cos, of_real_sin_of_real_re, of_real_sub, of_real_add, of_real_div, of_real_mul, of_real_one, of_real_bit0], convert sin_sub_sin _ _; norm_cast end theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) := begin rw ← of_real_inj, simp only [cos, neg_mul_eq_neg_mul_symm, of_real_sin, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_neg, of_real_bit0], convert cos_sub_cos _ _, ring, end lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) := begin rw ← of_real_inj, simp only [cos, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_bit0], convert cos_add_cos _ _; norm_cast, end lemma tan_eq_sin_div_cos : tan x = sin x / cos x := by rw [← of_real_inj, of_real_tan, tan_eq_sin_div_cos, of_real_div, of_real_sin, of_real_cos] lemma tan_mul_cos {x : ℝ} (hx : cos x ≠ 0) : tan x * cos x = sin x := by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx] @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] @[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := of_real_inj.1 $ by simp @[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 := by rw [add_comm, sin_sq_add_cos_sq] lemma sin_sq_le_one : sin x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right (pow_two_nonneg _) lemma cos_sq_le_one : cos x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left (pow_two_nonneg _) lemma abs_sin_le_one : abs' (sin x) ≤ 1 := abs_le_one_iff_mul_self_le_one.2 $ by simp only [← pow_two, sin_sq_le_one] lemma abs_cos_le_one : abs' (cos x) ≤ 1 := abs_le_one_iff_mul_self_le_one.2 $ by simp only [← pow_two, cos_sq_le_one] lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul] lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw ← of_real_inj; simp [cos_two_mul'] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := of_real_inj.1 $ by simpa using cos_square x lemma cos_square' : cos x ^ 2 = 1 - sin x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel'] lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _ lemma inv_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 := have complex.cos x ≠ 0, from mt (congr_arg re) hx, of_real_inj.1 $ by simpa using complex.inv_one_add_tan_sq this lemma tan_sq_div_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 := by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul] lemma inv_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) : (sqrt (1 + tan x ^ 2))⁻¹ = cos x := by rw [← sqrt_sqr hx.le, ← sqrt_inv, inv_one_add_tan_sq hx.ne'] lemma tan_div_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) : tan x / sqrt (1 + tan x ^ 2) = sin x := by rw [← tan_mul_cos hx.ne', ← inv_sqrt_one_add_tan_sq hx, div_eq_mul_inv] lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := by rw ← of_real_inj; simp [cos_three_mul] lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := by rw ← of_real_inj; simp [sin_three_mul] /-- The definition of `sinh` in terms of `exp`. -/ lemma sinh_eq (x : ℝ) : sinh x = (exp x - exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [sinh, exp, exp, complex.of_real_neg, complex.sinh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.sub_re] @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh_add] /-- The definition of `cosh` in terms of `exp`. -/ lemma cosh_eq (x : ℝ) : cosh x = (exp x + exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [cosh, exp, exp, complex.of_real_neg, complex.cosh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.add_re] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh] @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw ← of_real_inj; simp [cosh_add_sinh] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw ← of_real_inj; simp [sinh_add_cosh] lemma cosh_sq_sub_sinh_sq (x : ℝ) : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw ← of_real_inj; simp [cosh_sq_sub_sinh_sq] lemma cosh_square : cosh x ^ 2 = sinh x ^ 2 + 1 := by rw ← of_real_inj; simp [cosh_square] lemma sinh_square : sinh x ^ 2 = cosh x ^ 2 - 1 := by rw ← of_real_inj; simp [sinh_square] lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw ← of_real_inj; simp [cosh_two_mul] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := by rw ← of_real_inj; simp [sinh_two_mul] lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := by rw ← of_real_inj; simp [cosh_three_mul] lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := by rw ← of_real_inj; simp [sinh_three_mul] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ (∑ m in range j, (x ^ m / m! : ℂ)).re, from have h₁ : (((λ m : ℕ, (x ^ m / m! : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / 0!).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_nonneg _), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_pos (exp_pos _) lemma exp_strict_mono : strict_mono exp := λ x y h, by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) @[mono] lemma exp_monotone : ∀ {x y : ℝ}, x ≤ y → exp x ≤ exp y := exp_strict_mono.monotone @[simp] lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt @[simp] lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le lemma exp_injective : function.injective exp := exp_strict_mono.injective @[simp] lemma exp_eq_exp {x y : ℝ} : exp x = exp y ↔ x = y := exp_injective.eq_iff @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := by rw [← exp_zero, exp_injective.eq_iff] @[simp] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_le_one_iff {x : ℝ} : exp x ≤ 1 ↔ x ≤ 0 := exp_zero ▸ exp_le_exp @[simp] lemma one_le_exp_iff {x : ℝ} : 1 ≤ exp x ↔ 0 ≤ x := exp_zero ▸ exp_le_exp /-- `real.cosh` is always positive -/ lemma cosh_pos (x : ℝ) : 0 < real.cosh x := (cosh_eq x).symm ▸ half_pos (add_pos (exp_pos x) (exp_pos (-x))) end real namespace complex lemma sum_div_factorial_le {α : Type*} [linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) ≤ n.succ * (n! * n)⁻¹ := calc ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) = ∑ m in range (j - n), 1 / (m + n)! : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_left_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ ∑ m in range (j - n), (n! * n.succ ^ m)⁻¹ : begin refine sum_le_sum (assume m n, _), rw [one_div, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.factorial_mul_pow_le_factorial }, { exact nat.cast_pos.2 (nat.factorial_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.factorial_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = n!⁻¹ * ∑ m in range (j - n), n.succ⁻¹ ^ m : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.factorial_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n! * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ.inj (pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n! * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.factorial_pos _))) (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) = n, by simp, by rw [← geom_series_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq h₃, mul_comm _ (n! * n : α), ← mul_assoc (n!⁻¹ : α), ← mul_inv_rev', h₄, ← mul_assoc (n! * n : α), mul_comm (n : α) n!, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_assoc, mul_comm] ... ≤ n.succ / (n! * n) : begin refine iff.mpr (div_le_div_right (mul_pos _ _)) _, exact nat.cast_pos.2 (nat.factorial_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) := begin rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), simp_rw ← sub_eq_add_neg, show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ m / m! : ℂ)) = abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ n * (x ^ (m - n) / m!) : ℂ)) : congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto)) ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs (x ^ n * (_ / m!)) : abv_sum_le_sum_abv _ _ ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs x ^ n * (1 / m!) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.factorial_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (∑ m in (range j).filter (λ k, n ≤ k), (1 / m! : ℝ)) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_factorial_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - ∑ m in range 1, x ^ m / m!) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (1! * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1 - x) ≤ (abs x)^2 := calc abs (exp x - 1 - x) = abs (exp x - ∑ m in range 2, x ^ m / m!) : by simp [sub_eq_add_neg, sum_range_succ, add_assoc] ... ≤ (abs x)^2 * (nat.succ 2 * (2! * (2 : ℕ))⁻¹) : exp_bound hx dec_trivial ... ≤ (abs x)^2 * 1 : mul_le_mul_of_nonneg_left (by norm_num) (pow_two_nonneg (abs x)) ... = (abs x)^2 : by rw [mul_one] end complex namespace real open complex finset lemma exp_bound {x : ℝ} (hx : abs' x ≤ 1) {n : ℕ} (hn : 0 < n) : abs' (exp x - ∑ m in range n, x ^ m / m!) ≤ abs' x ^ n * (n.succ / (n! * n)) := begin have hxc : complex.abs x ≤ 1, by exact_mod_cast hx, convert exp_bound hxc hn; norm_cast end /-- A finite initial segment of the exponential series, followed by an arbitrary tail. For fixed `n` this is just a linear map wrt `r`, and each map is a simple linear function of the previous (see `exp_near_succ`), with `exp_near n x r ⟶ exp x` as `n ⟶ ∞`, for any `r`. -/ def exp_near (n : ℕ) (x r : ℝ) : ℝ := ∑ m in range n, x ^ m / m! + x ^ n / n! * r @[simp] theorem exp_near_zero (x r) : exp_near 0 x r = r := by simp [exp_near] @[simp] theorem exp_near_succ (n x r) : exp_near (n + 1) x r = exp_near n x (1 + x / (n+1) * r) := by simp [exp_near, range_succ, mul_add, add_left_comm, add_assoc, pow_succ, div_eq_mul_inv, mul_inv']; ac_refl theorem exp_near_sub (n x r₁ r₂) : exp_near n x r₁ - exp_near n x r₂ = x ^ n / n! * (r₁ - r₂) := by simp [exp_near, mul_sub] lemma exp_approx_end (n m : ℕ) (x : ℝ) (e₁ : n + 1 = m) (h : abs' x ≤ 1) : abs' (exp x - exp_near m x 0) ≤ abs' x ^ m / m! * ((m+1)/m) := by { simp [exp_near], convert exp_bound h _ using 1, field_simp [mul_comm], linarith } lemma exp_approx_succ {n} {x a₁ b₁ : ℝ} (m : ℕ) (e₁ : n + 1 = m) (a₂ b₂ : ℝ) (e : abs' (1 + x / m * a₂ - a₁) ≤ b₁ - abs' x / m * b₂) (h : abs' (exp x - exp_near m x a₂) ≤ abs' x ^ m / m! * b₂) : abs' (exp x - exp_near n x a₁) ≤ abs' x ^ n / n! * b₁ := begin refine (_root_.abs_sub_le _ _ _).trans ((add_le_add_right h _).trans _), subst e₁, rw [exp_near_succ, exp_near_sub, _root_.abs_mul], convert mul_le_mul_of_nonneg_left (le_sub_iff_add_le'.1 e) _, { simp [mul_add, pow_succ', div_eq_mul_inv, _root_.abs_mul, _root_.abs_inv, ← pow_abs, mul_inv'], ac_refl }, { simp [_root_.div_nonneg, _root_.abs_nonneg] } end lemma exp_approx_end' {n} {x a b : ℝ} (m : ℕ) (e₁ : n + 1 = m) (rm : ℝ) (er : ↑m = rm) (h : abs' x ≤ 1) (e : abs' (1 - a) ≤ b - abs' x / rm * ((rm+1)/rm)) : abs' (exp x - exp_near n x a) ≤ abs' x ^ n / n! * b := by subst er; exact exp_approx_succ _ e₁ _ _ (by simpa using e) (exp_approx_end _ _ _ e₁ h) lemma exp_1_approx_succ_eq {n} {a₁ b₁ : ℝ} {m : ℕ} (en : n + 1 = m) {rm : ℝ} (er : ↑m = rm) (h : abs' (exp 1 - exp_near m 1 ((a₁ - 1) * rm)) ≤ abs' 1 ^ m / m! * (b₁ * rm)) : abs' (exp 1 - exp_near n 1 a₁) ≤ abs' 1 ^ n / n! * b₁ := begin subst er, refine exp_approx_succ _ en _ _ _ h, field_simp [show (m : ℝ) ≠ 0, by norm_cast; linarith], end lemma exp_approx_start (x a b : ℝ) (h : abs' (exp x - exp_near 0 x a) ≤ abs' x ^ 0 / 0! * b) : abs' (exp x - a) ≤ b := by simpa using h lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) + ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) / 2) + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) - (complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) * I / 2) + abs (-((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [add_comm, complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from (div_le_iff (by norm_num)).mpr (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by rw [pow_two, pow_two]; exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le) (by norm_num)) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_sq_add_cos_sq x, by simp [add_comm, abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
2707b760f4b2583d148098c1eb690b2f6ca31aef
32317185abf7e7c963f4c67c190aec61af6b3628
/library/data/rat/basic.lean
837ab5afecaa765f45b622a696298fa22e260b18
[ "Apache-2.0" ]
permissive
Andrew-Zipperer-unorganized/lean
198a2317f21198cd8d26e7085e484b86277f17f7
dcb35008e1474a0abebe632b1dced120e5f8c009
refs/heads/master
1,622,526,520,945
1,453,576,559,000
1,454,612,842,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,178
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The rational numbers as a field generated by the integers, defined as the usual quotient. -/ import data.int algebra.field open int quot eq.ops record prerat : Type := (num : ℤ) (denom : ℤ) (denom_pos : denom > 0) /- prerat: the representations of the rationals as integers num, denom, with denom > 0. note: names are not protected, because it is not expected that users will open prerat. -/ namespace prerat /- the equivalence relation -/ definition equiv (a b : prerat) : Prop := num a * denom b = num b * denom a infix ≡ := equiv theorem equiv.refl [refl] (a : prerat) : a ≡ a := rfl theorem equiv.symm [symm] {a b : prerat} (H : a ≡ b) : b ≡ a := !eq.symm H theorem num_eq_zero_of_equiv {a b : prerat} (H : a ≡ b) (na_zero : num a = 0) : num b = 0 := have num a * denom b = 0, from !zero_mul ▸ na_zero ▸ rfl, have num b * denom a = 0, from H ▸ this, show num b = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero this) (ne_of_gt (denom_pos a)) theorem num_pos_of_equiv {a b : prerat} (H : a ≡ b) (na_pos : num a > 0) : num b > 0 := have num a * denom b > 0, from mul_pos na_pos (denom_pos b), have num b * denom a > 0, from H ▸ this, show num b > 0, from pos_of_mul_pos_right this (le_of_lt (denom_pos a)) theorem num_neg_of_equiv {a b : prerat} (H : a ≡ b) (na_neg : num a < 0) : num b < 0 := assert H₁ : num a * denom b = num b * denom a, from H, assert num a * denom b < 0, from mul_neg_of_neg_of_pos na_neg (denom_pos b), have -(-num b * denom a) < 0, begin rewrite [neg_mul_eq_neg_mul, neg_neg, -H₁], exact this end, have -num b > 0, from pos_of_mul_pos_right (pos_of_neg_neg this) (le_of_lt (denom_pos a)), neg_of_neg_pos this theorem equiv_of_num_eq_zero {a b : prerat} (H1 : num a = 0) (H2 : num b = 0) : a ≡ b := by rewrite [↑equiv, H1, H2, *zero_mul] theorem equiv.trans [trans] {a b c : prerat} (H1 : a ≡ b) (H2 : b ≡ c) : a ≡ c := decidable.by_cases (suppose num b = 0, have num a = 0, from num_eq_zero_of_equiv (equiv.symm H1) `num b = 0`, have num c = 0, from num_eq_zero_of_equiv H2 `num b = 0`, equiv_of_num_eq_zero `num a = 0` `num c = 0`) (suppose num b ≠ 0, have H3 : num b * denom b ≠ 0, from mul_ne_zero this (ne_of_gt (denom_pos b)), have H4 : (num b * denom b) * (num a * denom c) = (num b * denom b) * (num c * denom a), from calc (num b * denom b) * (num a * denom c) = (num a * denom b) * (num b * denom c) : by rewrite [*mul.assoc, *mul.left_comm (num a), *mul.left_comm (num b)] ... = (num b * denom a) * (num b * denom c) : {H1} ... = (num b * denom a) * (num c * denom b) : {H2} ... = (num b * denom b) * (num c * denom a) : by rewrite [*mul.assoc, *mul.left_comm (denom a), *mul.left_comm (denom b), mul.comm (denom a)], eq_of_mul_eq_mul_left H3 H4) theorem equiv.is_equivalence : equivalence equiv := mk_equivalence equiv equiv.refl @equiv.symm @equiv.trans definition setoid : setoid prerat := setoid.mk equiv equiv.is_equivalence /- field operations -/ definition of_int (i : int) : prerat := prerat.mk i 1 !of_nat_succ_pos definition zero : prerat := of_int 0 definition one : prerat := of_int 1 private theorem mul_denom_pos (a b : prerat) : denom a * denom b > 0 := mul_pos (denom_pos a) (denom_pos b) definition add (a b : prerat) : prerat := prerat.mk (num a * denom b + num b * denom a) (denom a * denom b) (mul_denom_pos a b) definition mul (a b : prerat) : prerat := prerat.mk (num a * num b) (denom a * denom b) (mul_denom_pos a b) definition neg (a : prerat) : prerat := prerat.mk (- num a) (denom a) (denom_pos a) definition smul (a : ℤ) (b : prerat) (H : a > 0) : prerat := prerat.mk (a * num b) (a * denom b) (mul_pos H (denom_pos b)) theorem of_int_add (a b : ℤ) : of_int (a + b) ≡ add (of_int a) (of_int b) := by esimp [equiv, num, denom, one, add, of_int]; rewrite [*int.mul_one] theorem of_int_mul (a b : ℤ) : of_int (a * b) ≡ mul (of_int a) (of_int b) := !equiv.refl theorem of_int_neg (a : ℤ) : of_int (-a) ≡ neg (of_int a) := !equiv.refl theorem of_int.inj {a b : ℤ} : of_int a ≡ of_int b → a = b := by rewrite [↑of_int, ↑equiv, *mul_one]; intros; assumption definition inv : prerat → prerat | inv (prerat.mk nat.zero d dp) := zero | inv (prerat.mk (nat.succ n) d dp) := prerat.mk d (nat.succ n) !of_nat_succ_pos | inv (prerat.mk -[1+n] d dp) := prerat.mk (-d) (nat.succ n) !of_nat_succ_pos theorem equiv_zero_of_num_eq_zero {a : prerat} (H : num a = 0) : a ≡ zero := by rewrite [↑equiv, H, ↑zero, ↑num, ↑of_int, *zero_mul] theorem num_eq_zero_of_equiv_zero {a : prerat} : a ≡ zero → num a = 0 := by rewrite [↑equiv, ↑zero, ↑of_int, mul_one, zero_mul]; intro H; exact H theorem inv_zero {d : int} (dp : d > 0) : inv (mk nat.zero d dp) = zero := begin rewrite [↑inv, ▸*] end theorem inv_zero' : inv zero = zero := inv_zero (of_nat_succ_pos nat.zero) open nat theorem inv_of_pos {n d : int} (np : n > 0) (dp : d > 0) : inv (mk n d dp) ≡ mk d n np := obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np), have (n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np), obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt this, have d * n = d * nat.succ k, by rewrite [Hn', Hk], Hn'⁻¹ ▸ (Hk⁻¹ ▸ this) theorem inv_neg {n d : int} (np : n > 0) (dp : d > 0) : inv (mk (-n) d dp) ≡ mk (-d) n np := obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np), have (n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np), obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt this, have -d * n = -d * nat.succ k, by rewrite [Hn', Hk], have H3 : inv (mk -[1+k] d dp) ≡ mk (-d) n np, from this, have H4 : -[1+k] = -n, from calc -[1+k] = -(nat.succ k) : rfl ... = -n : by rewrite [Hk⁻¹, Hn'], H4 ▸ H3 theorem inv_of_neg {n d : int} (nn : n < 0) (dp : d > 0) : inv (mk n d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn) := have inv (mk (-(-n)) d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn), from inv_neg (neg_pos_of_neg nn) dp, !neg_neg ▸ this /- operations respect equiv -/ theorem add_equiv_add {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) : add a1 b1 ≡ add a2 b2 := calc (num a1 * denom b1 + num b1 * denom a1) * (denom a2 * denom b2) = num a1 * denom a2 * denom b1 * denom b2 + num b1 * denom b2 * denom a1 * denom a2 : by rewrite [right_distrib, *mul.assoc, mul.left_comm (denom b1), mul.comm (denom b2), *mul.assoc] ... = num a2 * denom a1 * denom b1 * denom b2 + num b2 * denom b1 * denom a1 * denom a2 : by rewrite [↑equiv at *, eqv1, eqv2] ... = (num a2 * denom b2 + num b2 * denom a2) * (denom a1 * denom b1) : by rewrite [right_distrib, *mul.assoc, *mul.left_comm (denom b2), *mul.comm (denom b1), *mul.assoc, mul.left_comm (denom a2)] theorem mul_equiv_mul {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) : mul a1 b1 ≡ mul a2 b2 := calc (num a1 * num b1) * (denom a2 * denom b2) = (num a1 * denom a2) * (num b1 * denom b2) : by rewrite [*mul.assoc, mul.left_comm (num b1)] ... = (num a2 * denom a1) * (num b2 * denom b1) : by rewrite [↑equiv at *, eqv1, eqv2] ... = (num a2 * num b2) * (denom a1 * denom b1) : by rewrite [*mul.assoc, mul.left_comm (num b2)] theorem neg_equiv_neg {a b : prerat} (eqv : a ≡ b) : neg a ≡ neg b := calc -num a * denom b = -(num a * denom b) : neg_mul_eq_neg_mul ... = -(num b * denom a) : {eqv} ... = -num b * denom a : neg_mul_eq_neg_mul theorem inv_equiv_inv : ∀{a b : prerat}, a ≡ b → inv a ≡ inv b | (mk an ad adp) (mk bn bd bdp) := assume H, lt.by_cases (assume an_neg : an < 0, have bn_neg : bn < 0, from num_neg_of_equiv H an_neg, calc inv (mk an ad adp) ≡ mk (-ad) (-an) (neg_pos_of_neg an_neg) : inv_of_neg an_neg adp ... ≡ mk (-bd) (-bn) (neg_pos_of_neg bn_neg) : by rewrite [↑equiv at *, ▸*, *neg_mul_neg, mul.comm ad, mul.comm bd, H] ... ≡ inv (mk bn bd bdp) : (inv_of_neg bn_neg bdp)⁻¹) (assume an_zero : an = 0, have bn_zero : bn = 0, from num_eq_zero_of_equiv H an_zero, eq.subst (calc inv (mk an ad adp) = inv (mk 0 ad adp) : {an_zero} ... = zero : inv_zero adp ... = inv (mk 0 bd bdp) : inv_zero bdp ... = inv (mk bn bd bdp) : bn_zero) !equiv.refl) (assume an_pos : an > 0, have bn_pos : bn > 0, from num_pos_of_equiv H an_pos, calc inv (mk an ad adp) ≡ mk ad an an_pos : inv_of_pos an_pos adp ... ≡ mk bd bn bn_pos : by rewrite [↑equiv at *, ▸*, mul.comm ad, mul.comm bd, H] ... ≡ inv (mk bn bd bdp) : (inv_of_pos bn_pos bdp)⁻¹) theorem smul_equiv {a : ℤ} {b : prerat} (H : a > 0) : smul a b H ≡ b := by esimp[equiv, smul]; rewrite[mul.assoc, mul.left_comm] /- properties -/ protected theorem add.comm (a b : prerat) : add a b ≡ add b a := by rewrite [↑add, ↑equiv, ▸*, add.comm, mul.comm (denom a)] protected theorem add.assoc (a b c : prerat) : add (add a b) c ≡ add a (add b c) := by rewrite [↑add, ↑equiv, ▸*, *(mul.comm (num c)), *(λy, mul.comm y (denom a)), *left_distrib, *right_distrib, *mul.assoc, *add.assoc] protected theorem add_zero (a : prerat) : add a zero ≡ a := by rewrite [↑add, ↑equiv, ↑zero, ↑of_int, ▸*, *mul_one, zero_mul, add_zero] protected theorem add_left_inv (a : prerat) : add (neg a) a ≡ zero := by rewrite [↑add, ↑equiv, ↑neg, ↑zero, ↑of_int, ▸*, -neg_mul_eq_neg_mul, add.left_inv, *zero_mul] protected theorem mul_comm (a b : prerat) : mul a b ≡ mul b a := by rewrite [↑mul, ↑equiv, mul.comm (num a), mul.comm (denom a)] protected theorem mul_assoc (a b c : prerat) : mul (mul a b) c ≡ mul a (mul b c) := by rewrite [↑mul, ↑equiv, *mul.assoc] protected theorem mul_one (a : prerat) : mul a one ≡ a := by rewrite [↑mul, ↑one, ↑of_int, ↑equiv, ▸*, *mul_one] protected theorem mul_left_distrib (a b c : prerat) : mul a (add b c) ≡ add (mul a b) (mul a c) := have H : smul (denom a) (mul a (add b c)) (denom_pos a) = add (mul a b) (mul a c), from begin rewrite[↑smul, ↑mul, ↑add], congruence, rewrite[*left_distrib, *right_distrib, -+(int.mul_assoc)], have T : ∀ {x y z w : ℤ}, x*y*z*w=y*z*x*w, from λx y z w, (!int.mul_assoc ⬝ !int.mul_comm) ▸ rfl, exact !congr_arg2 T T, rewrite [mul.left_comm (denom a) (denom b) (denom c)], rewrite int.mul_assoc end, equiv.symm (H ▸ smul_equiv (denom_pos a)) theorem mul_inv_cancel : ∀{a : prerat}, ¬ a ≡ zero → mul a (inv a) ≡ one | (mk an ad adp) := assume H, let a := mk an ad adp in lt.by_cases (assume an_neg : an < 0, let ia := mk (-ad) (-an) (neg_pos_of_neg an_neg) in calc mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_neg an_neg adp) ... ≡ one : begin esimp [equiv, num, denom, one, mul, of_int], rewrite [*int.mul_one, *int.one_mul, mul.comm, neg_mul_comm] end) (assume an_zero : an = 0, absurd (equiv_zero_of_num_eq_zero an_zero) H) (assume an_pos : an > 0, let ia := mk ad an an_pos in calc mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_pos an_pos adp) ... ≡ one : begin esimp [equiv, num, denom, one, mul, of_int], rewrite [*int.mul_one, *int.one_mul, mul.comm] end) theorem zero_not_equiv_one : ¬ zero ≡ one := begin esimp [equiv, zero, one, of_int], rewrite [zero_mul, int.mul_one], exact zero_ne_one end theorem mul_denom_equiv (a : prerat) : mul a (of_int (denom a)) ≡ of_int (num a) := by esimp [mul, of_int, equiv]; rewrite [*int.mul_one] /- Reducing a fraction to lowest terms. Needed to choose a canonical representative of rat, and define numerator and denominator. -/ definition reduce : prerat → prerat | (mk an ad adpos) := have pos : ad / gcd an ad > 0, from div_pos_of_pos_of_dvd adpos !gcd_nonneg !gcd_dvd_right, if an = 0 then prerat.zero else mk (an / gcd an ad) (ad / gcd an ad) pos protected theorem eq {a b : prerat} (Hn : num a = num b) (Hd : denom a = denom b) : a = b := begin cases a with [an, ad, adpos], cases b with [bn, bd, bdpos], generalize adpos, generalize bdpos, esimp at *, rewrite [Hn, Hd], intros, apply rfl end theorem reduce_equiv : ∀ a : prerat, reduce a ≡ a | (mk an ad adpos) := decidable.by_cases (assume anz : an = 0, begin rewrite [↑reduce, if_pos anz, ↑equiv, anz], krewrite zero_mul end) (assume annz : an ≠ 0, by rewrite [↑reduce, if_neg annz, ↑equiv, mul.comm, -!int.mul_div_assoc !gcd_dvd_left, -!int.mul_div_assoc !gcd_dvd_right, mul.comm]) theorem reduce_eq_reduce : ∀ {a b : prerat}, a ≡ b → reduce a = reduce b | (mk an ad adpos) (mk bn bd bdpos) := assume H : an * bd = bn * ad, decidable.by_cases (assume anz : an = 0, have H' : bn * ad = 0, by rewrite [-H, anz, zero_mul], assert bnz : bn = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt adpos), by rewrite [↑reduce, if_pos anz, if_pos bnz]) (assume annz : an ≠ 0, assert bnnz : bn ≠ 0, from assume bnz, have H' : an * bd = 0, by rewrite [H, bnz, zero_mul], have anz : an = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt bdpos), show false, from annz anz, begin rewrite [↑reduce, if_neg annz, if_neg bnnz], apply prerat.eq, {apply div_gcd_eq_div_gcd H adpos bdpos}, {esimp, rewrite [gcd.comm, gcd.comm bn], apply div_gcd_eq_div_gcd_of_nonneg, rewrite [mul.comm, -H, mul.comm], apply annz, apply bnnz, apply le_of_lt adpos, apply le_of_lt bdpos}, end) end prerat /- the rationals -/ definition rat : Type.{1} := quot prerat.setoid notation `ℚ` := rat local attribute prerat.setoid [instance] namespace rat /- operations -/ definition of_int [coercion] (i : ℤ) : ℚ := ⟦prerat.of_int i⟧ definition of_nat [coercion] (n : ℕ) : ℚ := nat.to.rat n definition of_num [coercion] [reducible] (n : num) : ℚ := num.to.rat n protected definition prio := num.pred int.prio definition rat_has_zero [reducible] [instance] [priority rat.prio] : has_zero rat := has_zero.mk (of_int 0) definition rat_has_one [reducible] [instance] [priority rat.prio] : has_one rat := has_one.mk (of_int 1) theorem of_int_zero : of_int (0:int) = (0:rat) := rfl theorem of_int_one : of_int (1:int) = (1:rat) := rfl protected definition add : ℚ → ℚ → ℚ := quot.lift₂ (λ a b : prerat, ⟦prerat.add a b⟧) (take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.add_equiv_add H1 H2)) protected definition mul : ℚ → ℚ → ℚ := quot.lift₂ (λ a b : prerat, ⟦prerat.mul a b⟧) (take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.mul_equiv_mul H1 H2)) protected definition neg : ℚ → ℚ := quot.lift (λ a : prerat, ⟦prerat.neg a⟧) (take a1 a2, assume H, quot.sound (prerat.neg_equiv_neg H)) protected definition inv : ℚ → ℚ := quot.lift (λ a : prerat, ⟦prerat.inv a⟧) (take a1 a2, assume H, quot.sound (prerat.inv_equiv_inv H)) definition reduce : ℚ → prerat := quot.lift (λ a : prerat, prerat.reduce a) @prerat.reduce_eq_reduce definition num (a : ℚ) : ℤ := prerat.num (reduce a) definition denom (a : ℚ) : ℤ := prerat.denom (reduce a) theorem denom_pos (a : ℚ): denom a > 0 := prerat.denom_pos (reduce a) definition rat_has_add [reducible] [instance] [priority rat.prio] : has_add rat := has_add.mk rat.add definition rat_has_mul [reducible] [instance] [priority rat.prio] : has_mul rat := has_mul.mk rat.mul definition rat_has_neg [reducible] [instance] [priority rat.prio] : has_neg rat := has_neg.mk rat.neg definition rat_has_inv [reducible] [instance] [priority rat.prio] : has_inv rat := has_inv.mk rat.inv protected definition sub [reducible] (a b : ℚ) : rat := a + (-b) definition rat_has_sub [reducible] [instance] [priority rat.prio] : has_sub rat := has_sub.mk rat.sub lemma sub.def (a b : ℚ) : a - b = a + (-b) := rfl /- properties -/ theorem of_int_add (a b : ℤ) : of_int (a + b) = of_int a + of_int b := quot.sound (prerat.of_int_add a b) theorem of_int_mul (a b : ℤ) : of_int (a * b) = of_int a * of_int b := quot.sound (prerat.of_int_mul a b) theorem of_int_neg (a : ℤ) : of_int (-a) = -(of_int a) := quot.sound (prerat.of_int_neg a) theorem of_int_sub (a b : ℤ) : of_int (a - b) = of_int a - of_int b := calc of_int (a - b) = of_int a + of_int (-b) : of_int_add ... = of_int a - of_int b : {of_int_neg b} theorem of_int.inj {a b : ℤ} (H : of_int a = of_int b) : a = b := prerat.of_int.inj (quot.exact H) theorem eq_of_of_int_eq_of_int {a b : ℤ} (H : of_int a = of_int b) : a = b := of_int.inj H theorem of_int_eq_of_int_iff (a b : ℤ) : of_int a = of_int b ↔ a = b := iff.intro eq_of_of_int_eq_of_int !congr_arg theorem of_nat_eq (a : ℕ) : of_nat a = of_int (int.of_nat a) := rfl open nat theorem of_nat_add (a b : ℕ) : of_nat (a + b) = of_nat a + of_nat b := by rewrite [of_nat_eq, int.of_nat_add, rat.of_int_add] theorem of_nat_mul (a b : ℕ) : of_nat (a * b) = of_nat a * of_nat b := by rewrite [of_nat_eq, int.of_nat_mul, rat.of_int_mul] theorem of_nat_sub {a b : ℕ} (H : a ≥ b) : of_nat (a - b) = of_nat a - of_nat b := begin rewrite of_nat_eq, rewrite [int.of_nat_sub H], rewrite [rat.of_int_sub] end theorem of_nat.inj {a b : ℕ} (H : of_nat a = of_nat b) : a = b := int.of_nat.inj (of_int.inj H) theorem eq_of_of_nat_eq_of_nat {a b : ℕ} (H : of_nat a = of_nat b) : a = b := of_nat.inj H theorem of_nat_eq_of_nat_iff (a b : ℕ) : of_nat a = of_nat b ↔ a = b := iff.intro of_nat.inj !congr_arg protected theorem add_comm (a b : ℚ) : a + b = b + a := quot.induction_on₂ a b (take u v, quot.sound !prerat.add.comm) protected theorem add_assoc (a b c : ℚ) : a + b + c = a + (b + c) := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.add.assoc) protected theorem add_zero (a : ℚ) : a + 0 = a := quot.induction_on a (take u, quot.sound !prerat.add_zero) protected theorem zero_add (a : ℚ) : 0 + a = a := !rat.add_comm ▸ !rat.add_zero protected theorem add_left_inv (a : ℚ) : -a + a = 0 := quot.induction_on a (take u, quot.sound !prerat.add_left_inv) protected theorem mul_comm (a b : ℚ) : a * b = b * a := quot.induction_on₂ a b (take u v, quot.sound !prerat.mul_comm) protected theorem mul_assoc (a b c : ℚ) : a * b * c = a * (b * c) := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul_assoc) protected theorem mul_one (a : ℚ) : a * 1 = a := quot.induction_on a (take u, quot.sound !prerat.mul_one) protected theorem one_mul (a : ℚ) : 1 * a = a := !rat.mul_comm ▸ !rat.mul_one protected theorem left_distrib (a b c : ℚ) : a * (b + c) = a * b + a * c := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul_left_distrib) protected theorem right_distrib (a b c : ℚ) : (a + b) * c = a * c + b * c := by rewrite [rat.mul_comm, rat.left_distrib, *rat.mul_comm c] protected theorem mul_inv_cancel {a : ℚ} : a ≠ 0 → a * a⁻¹ = 1 := quot.induction_on a (take u, assume H, quot.sound (!prerat.mul_inv_cancel (assume H1, H (quot.sound H1)))) protected theorem inv_mul_cancel {a : ℚ} (H : a ≠ 0) : a⁻¹ * a = 1 := !rat.mul_comm ▸ rat.mul_inv_cancel H protected theorem zero_ne_one : (0 : ℚ) ≠ 1 := assume H, prerat.zero_not_equiv_one (quot.exact H) definition has_decidable_eq [instance] : decidable_eq ℚ := take a b, quot.rec_on_subsingleton₂ a b (take u v, if H : prerat.num u * prerat.denom v = prerat.num v * prerat.denom u then decidable.inl (quot.sound H) else decidable.inr (assume H1, H (quot.exact H1))) protected theorem inv_zero : inv 0 = (0 : ℚ) := quot.sound (prerat.inv_zero' ▸ !prerat.equiv.refl) theorem quot_reduce (a : ℚ) : ⟦reduce a⟧ = a := quot.induction_on a (take u, quot.sound !prerat.reduce_equiv) section local attribute rat [reducible] theorem mul_denom (a : ℚ) : a * denom a = num a := have H : ⟦reduce a⟧ * of_int (denom a) = of_int (num a), from quot.sound (!prerat.mul_denom_equiv), quot_reduce a ▸ H end theorem coprime_num_denom (a : ℚ) : coprime (num a) (denom a) := decidable.by_cases (suppose a = 0, by substvars) (quot.induction_on a (take u H, assert H' : prerat.num u ≠ 0, from take H'', H (quot.sound (prerat.equiv_zero_of_num_eq_zero H'')), begin cases u with un ud udpos, rewrite [▸*, ↑num, ↑denom, ↑reduce, ↑prerat.reduce, if_neg H', ▸*], have gcd un ud ≠ 0, from ne_of_gt (!gcd_pos_of_ne_zero_left H'), apply coprime_div_gcd_div_gcd this end)) protected definition discrete_field [reducible] [trans_instance] : discrete_field rat := ⦃discrete_field, add := rat.add, add_assoc := rat.add_assoc, zero := 0, zero_add := rat.zero_add, add_zero := rat.add_zero, neg := rat.neg, add_left_inv := rat.add_left_inv, add_comm := rat.add_comm, mul := rat.mul, mul_assoc := rat.mul_assoc, one := 1, one_mul := rat.one_mul, mul_one := rat.mul_one, left_distrib := rat.left_distrib, right_distrib := rat.right_distrib, mul_comm := rat.mul_comm, mul_inv_cancel := @rat.mul_inv_cancel, inv_mul_cancel := @rat.inv_mul_cancel, zero_ne_one := rat.zero_ne_one, inv_zero := rat.inv_zero, has_decidable_eq := has_decidable_eq⦄ definition rat_has_div [instance] [reducible] [priority rat.prio] : has_div rat := has_div.mk has_div.div definition rat_has_pow_nat [instance] [reducible] [priority rat.prio] : has_pow_nat rat := has_pow_nat.mk has_pow_nat.pow_nat theorem eq_num_div_denom (a : ℚ) : a = num a / denom a := have H : of_int (denom a) ≠ 0, from assume H', ne_of_gt (denom_pos a) (of_int.inj H'), iff.mpr (!eq_div_iff_mul_eq H) (mul_denom a) theorem of_int_div {a b : ℤ} (H : b ∣ a) : of_int (a / b) = of_int a / of_int b := decidable.by_cases (assume bz : b = 0, by rewrite [bz, int.div_zero, of_int_zero, div_zero]) (assume bnz : b ≠ 0, have bnz' : of_int b ≠ 0, from assume oibz, bnz (of_int.inj oibz), have H' : of_int (a / b) * of_int b = of_int a, from dvd.elim H (take c, assume Hc : a = b * c, by rewrite [Hc, !int.mul_div_cancel_left bnz, mul.comm]), iff.mpr (!eq_div_iff_mul_eq bnz') H') theorem of_nat_div {a b : ℕ} (H : b ∣ a) : of_nat (a / b) = of_nat a / of_nat b := have H' : (int.of_nat b ∣ int.of_nat a), by rewrite [int.of_nat_dvd_of_nat_iff]; exact H, by+ rewrite [of_nat_eq, int.of_nat_div, of_int_div H'] theorem of_int_pow (a : ℤ) (n : ℕ) : of_int (a^n) = (of_int a)^n := begin induction n with n ih, apply eq.refl, rewrite [pow_succ, pow_succ, of_int_mul, ih] end theorem of_nat_pow (a : ℕ) (n : ℕ) : of_nat (a^n) = (of_nat a)^n := by rewrite [of_nat_eq, int.of_nat_pow, of_int_pow] end rat
8b14a3cf253735e2a14be064bac32625314c1203
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/archimedean.lean
10d2da995855812393b6f73b53675354ed888e9c
[ "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
13,593
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.field_power import data.rat /-! # Archimedean groups and fields. This file defines the archimedean property for ordered groups and proves several results connected to this notion. Being archimedean means that for all elements `x` and `y>0` there exists a natural number `n` such that `x ≤ n •ℕ y`. ## Main definitions * `archimedean` is a typeclass for an ordered additive commutative monoid to have the archimedean property. * `archimedean.floor_ring` defines a floor function on an archimedean linearly ordered ring making it into a `floor_ring`. * `round` defines a function rounding to the nearest integer for a linearly ordered field which is also a floor ring. ## Main statements * `ℕ`, `ℤ`, and `ℚ` are archimedean. -/ variables {α : Type*} /-- An ordered additive commutative monoid is called `archimedean` if for any two elements `x`, `y` such that `0 < y` there exists a natural number `n` such that `x ≤ n •ℕ y`. -/ class archimedean (α) [ordered_add_comm_monoid α] : Prop := (arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n •ℕ y) namespace linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] [archimedean α] /-- An archimedean decidable linearly ordered `add_comm_group` has a version of the floor: for `a > 0`, any `g` in the group lies between some two consecutive multiples of `a`. -/ lemma exists_int_smul_near_of_pos {a : α} (ha : 0 < a) (g : α) : ∃ k, k •ℤ a ≤ g ∧ g < (k + 1) •ℤ a := begin let s : set ℤ := {n : ℤ | n •ℤ a ≤ g}, obtain ⟨k, hk : -g ≤ k •ℕ a⟩ := archimedean.arch (-g) ha, have h_ne : s.nonempty := ⟨-k, by simpa using neg_le_neg hk⟩, obtain ⟨k, hk⟩ := archimedean.arch g ha, have h_bdd : ∀ n ∈ s, n ≤ (k : ℤ) := λ n hn, (gsmul_le_gsmul_iff ha).mp (le_trans hn hk : n •ℤ a ≤ k •ℤ a), obtain ⟨m, hm, hm'⟩ := int.exists_greatest_of_bdd ⟨k, h_bdd⟩ h_ne, refine ⟨m, hm, _⟩, by_contra H, linarith [hm' _ $ not_lt.mp H] end lemma exists_int_smul_near_of_pos' {a : α} (ha : 0 < a) (g : α) : ∃ k, 0 ≤ g - k •ℤ a ∧ g - k •ℤ a < a := begin obtain ⟨k, h1, h2⟩ := exists_int_smul_near_of_pos ha g, refine ⟨k, sub_nonneg.mpr h1, _⟩, have : g < k •ℤ a + 1 •ℤ a, by rwa ← add_gsmul a k 1, simpa [sub_lt_iff_lt_add'] end end linear_ordered_add_comm_group theorem exists_nat_gt [ordered_semiring α] [nontrivial α] [archimedean α] (x : α) : ∃ n : ℕ, x < n := let ⟨n, h⟩ := archimedean.arch x zero_lt_one in ⟨n+1, lt_of_le_of_lt (by rwa ← nsmul_one) (nat.cast_lt.2 (nat.lt_succ_self _))⟩ theorem exists_nat_ge [ordered_semiring α] [archimedean α] (x : α) : ∃ n : ℕ, x ≤ n := begin nontriviality α, exact (exists_nat_gt x).imp (λ n, le_of_lt) end lemma add_one_pow_unbounded_of_pos [ordered_semiring α] [nontrivial α] [archimedean α] (x : α) {y : α} (hy : 0 < y) : ∃ n : ℕ, x < (y + 1) ^ n := have 0 ≤ 1 + y, from add_nonneg zero_le_one hy.le, let ⟨n, h⟩ := archimedean.arch x hy in ⟨n, calc x ≤ n •ℕ y : h ... = n * y : nsmul_eq_mul _ _ ... < 1 + n * y : lt_one_add _ ... ≤ (1 + y) ^ n : one_add_mul_le_pow' (mul_nonneg hy.le hy.le) (mul_nonneg this this) (add_nonneg zero_le_two hy.le) _ ... = (y + 1) ^ n : by rw [add_comm]⟩ section linear_ordered_ring variables [linear_ordered_ring α] [archimedean α] lemma pow_unbounded_of_one_lt (x : α) {y : α} (hy1 : 1 < y) : ∃ n : ℕ, x < y ^ n := sub_add_cancel y 1 ▸ add_one_pow_unbounded_of_pos _ (sub_pos.2 hy1) /-- Every x greater than or equal to 1 is between two successive natural-number powers of every y greater than one. -/ lemma exists_nat_pow_near {x : α} {y : α} (hx : 1 ≤ x) (hy : 1 < y) : ∃ n : ℕ, y ^ n ≤ x ∧ x < y ^ (n + 1) := have h : ∃ n : ℕ, x < y ^ n, from pow_unbounded_of_one_lt _ hy, by classical; exact let n := nat.find h in have hn : x < y ^ n, from nat.find_spec h, have hnp : 0 < n, from pos_iff_ne_zero.2 (λ hn0, by rw [hn0, pow_zero] at hn; exact (not_le_of_gt hn hx)), have hnsp : nat.pred n + 1 = n, from nat.succ_pred_eq_of_pos hnp, have hltn : nat.pred n < n, from nat.pred_lt (ne_of_gt hnp), ⟨nat.pred n, le_of_not_lt (nat.find_min h hltn), by rwa hnsp⟩ theorem exists_int_gt (x : α) : ∃ n : ℤ, x < n := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa ← coe_coe⟩ theorem exists_int_lt (x : α) : ∃ n : ℤ, (n : α) < x := let ⟨n, h⟩ := exists_int_gt (-x) in ⟨-n, by rw int.cast_neg; exact neg_lt.1 h⟩ theorem exists_floor (x : α) : ∃ (fl : ℤ), ∀ (z : ℤ), z ≤ fl ↔ (z : α) ≤ x := begin haveI := classical.prop_decidable, have : ∃ (ub : ℤ), (ub:α) ≤ x ∧ ∀ (z : ℤ), (z:α) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩), refine this.imp (λ fl h z, _), cases h with h₁ h₂, exact ⟨λ h, le_trans (int.cast_le.2 h) h₁, h₂ z⟩, end end linear_ordered_ring section linear_ordered_field variables [linear_ordered_field α] /-- Every positive `x` is between two successive integer powers of another `y` greater than one. This is the same as `exists_int_pow_near'`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1) := by classical; exact let ⟨N, hN⟩ := pow_unbounded_of_one_lt x⁻¹ hy in have he: ∃ m : ℤ, y ^ m ≤ x, from ⟨-N, le_of_lt (by rw [(fpow_neg y (↑N))]; exact (inv_lt hx (lt_trans (inv_pos.2 hx) hN)).1 hN)⟩, let ⟨M, hM⟩ := pow_unbounded_of_one_lt x hy in have hb: ∃ b : ℤ, ∀ m, y ^ m ≤ x → m ≤ b, from ⟨M, λ m hm, le_of_not_lt (λ hlt, not_lt_of_ge (fpow_le_of_le (le_of_lt hy) (le_of_lt hlt)) (lt_of_le_of_lt hm hM))⟩, let ⟨n, hn₁, hn₂⟩ := int.exists_greatest_of_bdd hb he in ⟨n, hn₁, lt_of_not_ge (λ hge, not_le_of_gt (int.lt_succ _) (hn₂ _ hge))⟩ /-- Every positive `x` is between two successive integer powers of another `y` greater than one. This is the same as `exists_int_pow_near`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near' [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1) := let ⟨m, hle, hlt⟩ := exists_int_pow_near (inv_pos.2 hx) hy in have hyp : 0 < y, from lt_trans zero_lt_one hy, ⟨-(m+1), by rwa [fpow_neg, inv_lt (fpow_pos_of_pos hyp _) hx], by rwa [neg_add, neg_add_cancel_right, fpow_neg, le_inv hx (fpow_pos_of_pos hyp _)]⟩ /-- For any `y < 1` and any positive `x`, there exists `n : ℕ` with `y ^ n < x`. -/ lemma exists_pow_lt_of_lt_one [archimedean α] {x y : α} (hx : 0 < x) (hy : y < 1) : ∃ n : ℕ, y ^ n < x := begin by_cases y_pos : y ≤ 0, { use 1, simp only [pow_one], linarith, }, rw [not_le] at y_pos, rcases pow_unbounded_of_one_lt (x⁻¹) (one_lt_inv y_pos hy) with ⟨q, hq⟩, exact ⟨q, by rwa [inv_pow', inv_lt_inv hx (pow_pos y_pos _)] at hq⟩ end /-- Given `x` and `y` between `0` and `1`, `x` is between two successive powers of `y`. This is the same as `exists_nat_pow_near`, but for elements between `0` and `1` -/ lemma exists_nat_pow_near_of_lt_one [archimedean α] {x : α} {y : α} (xpos : 0 < x) (hx : x ≤ 1) (ypos : 0 < y) (hy : y < 1) : ∃ n : ℕ, y ^ (n + 1) < x ∧ x ≤ y ^ n := begin rcases exists_nat_pow_near (one_le_inv_iff.2 ⟨xpos, hx⟩) (one_lt_inv_iff.2 ⟨ypos, hy⟩) with ⟨n, hn, h'n⟩, refine ⟨n, _, _⟩, { rwa [inv_pow', inv_lt_inv xpos (pow_pos ypos _)] at h'n }, { rwa [inv_pow', inv_le_inv (pow_pos ypos _) xpos] at hn } end variables [floor_ring α] lemma sub_floor_div_mul_nonneg (x : α) {y : α} (hy : 0 < y) : 0 ≤ x - ⌊x / y⌋ * y := begin conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← sub_mul, exact mul_nonneg (sub_nonneg.2 (floor_le _)) (le_of_lt hy) end lemma sub_floor_div_mul_lt (x : α) {y : α} (hy : 0 < y) : x - ⌊x / y⌋ * y < y := sub_lt_iff_lt_add.2 begin conv in y {rw ← one_mul y}, conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← add_mul, exact (mul_lt_mul_right hy).2 (by rw add_comm; exact lt_floor_add_one _), end end linear_ordered_field instance : archimedean ℕ := ⟨λ n m m0, ⟨n, by simpa only [mul_one, nat.nsmul_eq_mul] using nat.mul_le_mul_left n m0⟩⟩ instance : archimedean ℤ := ⟨λ n m m0, ⟨n.to_nat, le_trans (int.le_to_nat _) $ by simpa only [nsmul_eq_mul, int.nat_cast_eq_coe_nat, zero_add, mul_one] using mul_le_mul_of_nonneg_left (int.add_one_le_iff.2 m0) (int.coe_zero_le n.to_nat)⟩⟩ /-- A linear ordered archimedean ring is a floor ring. This is not an `instance` because in some cases we have a computable `floor` function. -/ noncomputable def archimedean.floor_ring (α) [linear_ordered_ring α] [archimedean α] : floor_ring α := { floor := λ x, classical.some (exists_floor x), le_floor := λ z x, classical.some_spec (exists_floor x) z } section linear_ordered_field variables [linear_ordered_field α] theorem archimedean_iff_nat_lt : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x < n := ⟨@exists_nat_gt α _ _, λ H, ⟨λ x y y0, (H (x / y)).imp $ λ n h, le_of_lt $ by rwa [div_lt_iff y0, ← nsmul_eq_mul] at h⟩⟩ theorem archimedean_iff_nat_le : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x ≤ n := archimedean_iff_nat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (nat.cast_lt.2 (lt_add_one _))⟩⟩ theorem exists_rat_gt [archimedean α] (x : α) : ∃ q : ℚ, x < q := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa rat.cast_coe_nat⟩ theorem archimedean_iff_rat_lt : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x < q := ⟨@exists_rat_gt α _, λ H, archimedean_iff_nat_lt.2 $ λ x, let ⟨q, h⟩ := H x in ⟨nat_ceil q, lt_of_lt_of_le h $ by simpa only [rat.cast_coe_nat] using (@rat.cast_le α _ _ _).2 (le_nat_ceil _)⟩⟩ theorem archimedean_iff_rat_le : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x ≤ q := archimedean_iff_rat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (rat.cast_lt.2 (lt_add_one _))⟩⟩ variable [archimedean α] theorem exists_rat_lt (x : α) : ∃ q : ℚ, (q : α) < x := let ⟨n, h⟩ := exists_int_lt x in ⟨n, by rwa rat.cast_coe_int⟩ theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q:α) < y := begin cases exists_nat_gt (y - x)⁻¹ with n nh, cases exists_floor (x * n) with z zh, refine ⟨(z + 1 : ℤ) / n, _⟩, have n0' := (inv_pos.2 (sub_pos.2 h)).trans nh, have n0 := nat.cast_pos.1 n0', rw [rat.cast_div_of_ne_zero, rat.cast_coe_nat, rat.cast_coe_int, div_lt_iff n0'], refine ⟨(lt_div_iff n0').2 $ (lt_iff_lt_of_le_iff_le (zh _)).1 (lt_add_one _), _⟩, rw [int.cast_add, int.cast_one], refine lt_of_le_of_lt (add_le_add_right ((zh _).1 (le_refl _)) _) _, rwa [← lt_sub_iff_add_lt', ← sub_mul, ← div_lt_iff' (sub_pos.2 h), one_div], { rw [rat.coe_int_denom, nat.cast_one], exact one_ne_zero }, { intro H, rw [rat.coe_nat_num, ← coe_coe, nat.cast_eq_zero] at H, subst H, cases n0 }, { rw [rat.coe_nat_denom, nat.cast_one], exact one_ne_zero } end theorem exists_nat_one_div_lt {ε : α} (hε : 0 < ε) : ∃ n : ℕ, 1 / (n + 1: α) < ε := begin cases exists_nat_gt (1/ε) with n hn, use n, rw [div_lt_iff, ← div_lt_iff' hε], { apply hn.trans, simp [zero_lt_one] }, { exact n.cast_add_one_pos } end theorem exists_pos_rat_lt {x : α} (x0 : 0 < x) : ∃ q : ℚ, 0 < q ∧ (q : α) < x := by simpa only [rat.cast_pos] using exists_rat_btwn x0 include α @[simp] theorem rat.cast_floor (x : ℚ) : by haveI := archimedean.floor_ring α; exact ⌊(x:α)⌋ = ⌊x⌋ := begin haveI := archimedean.floor_ring α, apply le_antisymm, { rw [le_floor, ← @rat.cast_le α, rat.cast_coe_int], apply floor_le }, { rw [le_floor, ← rat.cast_coe_int, rat.cast_le], apply floor_le } end end linear_ordered_field section variables [linear_ordered_field α] /-- `round` rounds a number to the nearest integer. `round (1 / 2) = 1` -/ def round [floor_ring α] (x : α) : ℤ := ⌊x + 1 / 2⌋ lemma abs_sub_round [floor_ring α] (x : α) : abs (x - round x) ≤ 1 / 2 := begin rw [round, abs_sub_le_iff], have := floor_le (x + 1 / 2), have := lt_floor_add_one (x + 1 / 2), split; linarith end variable [archimedean α] theorem exists_rat_near (x : α) {ε : α} (ε0 : 0 < ε) : ∃ q : ℚ, abs (x - q) < ε := let ⟨q, h₁, h₂⟩ := exists_rat_btwn $ lt_trans ((sub_lt_self_iff x).2 ε0) ((lt_add_iff_pos_left x).2 ε0) in ⟨q, abs_sub_lt_iff.2 ⟨sub_lt.1 h₁, sub_lt_iff_lt_add.2 h₂⟩⟩ instance : archimedean ℚ := archimedean_iff_rat_le.2 $ λ q, ⟨q, by rw rat.cast_id⟩ @[simp] theorem rat.cast_round (x : ℚ) : by haveI := archimedean.floor_ring α; exact round (x:α) = round x := have ((x + (1 : ℚ) / (2 : ℚ) : ℚ) : α) = x + 1 / 2, by simp, by rw [round, round, ← this, rat.cast_floor] end
4195422833c90094a4b40c51962c70c6438a1a91
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/basic.lean
3521a3f4d114ad541bdbd9b707c56e10cd906f66
[ "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
98,426
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, Yury Kudryashov -/ import algebra.big_operators.pi import algebra.module.hom import algebra.module.pi import algebra.module.prod import algebra.module.submodule import algebra.module.submodule_lattice import algebra.group.prod import data.finsupp.basic import data.dfinsupp import algebra.pointwise import order.compactly_generated /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module`. ## Main definitions * Many constructors for linear maps * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * If `p` is a submodule of `M`, `submodule.quotient p` is the quotient of `M` with respect to `p`: that is, elements of `M` are identified if their difference is in `p`. This is itself a module. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first and second isomorphism laws for modules are proved as `quot_ker_equiv_range` and `quotient_inf_equiv_sup_quotient`. ## Notations * We continue to use the notation `M →ₗ[R] M₂` for the type of linear maps from `M` to `M₂` over the ring `R`. * We introduce the notations `M ≃ₗ M₂` and `M ≃ₗ[R] M₂` for `linear_equiv M M₂`. In the first, the ring `R` is implicit. * We introduce the notation `R ∙ v` for the span of a singleton, `submodule.span R {v}`. This is `\.`, not the same as the scalar multiplication `•`/`\bub`. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`linear_map.prod`, `linear_map.coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function open_locale big_operators universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} namespace finsupp lemma smul_sum {α : Type u} {β : Type v} {R : Type w} {M : Type y} [has_zero β] [semiring R] [add_comm_monoid M] [semimodule R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum @[simp] lemma sum_smul_index_linear_map' {α : Type u} {R : Type v} {M : Type w} {M₂ : Type x} [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule R M₂] {v : α →₀ M} {c : R} {h : α → M →ₗ[R] M₂} : (c • v).sum (λ a, h a) = c • (v.sum (λ a, h a)) := begin rw [finsupp.sum_smul_index', finsupp.smul_sum], { simp only [linear_map.map_smul], }, { intro i, exact (h i).map_zero }, end end finsupp section open_locale classical /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type u} [fintype ι] {R : Type v} [semiring R] (x : ι → R) : x = ∑ i, x i • (λj, if i = j then 1 else 0) := by { ext, simp } end /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] variables (f g : M →ₗ[R] M₂) include R theorem comp_assoc (g : M₂ →ₗ[R] M₃) (h : M₃ →ₗ[R] M₄) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- The restriction of a linear map `f : M → M₂` to a submodule `p ⊆ M` gives a linear map `p → M₂`. -/ def dom_restrict (f : M →ₗ[R] M₂) (p : submodule R M) : p →ₗ[R] M₂ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h : ∀c, f c ∈ p) : M₂ →ₗ[R] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R M) (f : M₂ →ₗ[R] M) {h} (x : M₂) : (cod_restrict p f h x : M) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) (g : M₃ →ₗ[R] M) : (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 R M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- Restrict domain and codomain of an endomorphism. -/ def restrict (f : M →ₗ[R] M) {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p →ₗ[R] p := (f.dom_restrict p).cod_restrict p $ set_like.forall.2 hf lemma restrict_apply {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) (x : p) : f.restrict hf x = ⟨f x, hf x.1 x.2⟩ := rfl lemma subtype_comp_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p.subtype.comp (f.restrict hf) = f.dom_restrict p := rfl lemma restrict_eq_cod_restrict_dom_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : f.restrict hf = (f.dom_restrict p).cod_restrict p (λ x, hf x.1 x.2) := rfl lemma restrict_eq_dom_restrict_cod_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x, f x ∈ p) : f.restrict (λ x _, hf x) = (f.cod_restrict p hf).dom_restrict p := rfl /-- The constant 0 map is linear. -/ instance : has_zero (M →ₗ[R] M₂) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ instance : inhabited (M →ₗ[R] M₂) := ⟨0⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₗ[R] M₂) x = 0 := rfl @[simp] lemma default_def : default (M →ₗ[R] M₂) = 0 := rfl instance unique_of_left [subsingleton M] : unique (M →ₗ[R] M₂) := { uniq := λ f, ext $ λ x, by rw [subsingleton.elim x 0, map_zero, map_zero], .. linear_map.inhabited } instance unique_of_right [subsingleton M₂] : unique (M →ₗ[R] M₂) := coe_injective.unique /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₗ[R] M₂) := ⟨λ f g, ⟨λ b, f b + g b, by simp [add_comm, add_left_comm], by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : M) : (f + g) x = f x + g x := rfl /-- The type of linear maps is an additive monoid. -/ instance : add_comm_monoid (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_monoid_hom (a : M) : is_add_monoid_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a, map_zero := rfl } lemma add_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (h + g).comp f = h.comp f + g.comp f := rfl lemma comp_add (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (f + g) = h.comp f + h.comp g := by { ext, simp } lemma sum_apply (t : finset ι) (f : ι → M →ₗ[R] M₂) (b : M) : (∑ d in t, f d) b = ∑ d in t, f d b := (t.sum_hom (λ g : M →ₗ[R] M₂, g b)).symm section smul_right variables {S : Type*} [semiring S] [semimodule R S] [semimodule S M] [is_scalar_tower R S M] /-- When `f` is an `R`-linear map taking values in `S`, then `λb, f b • x` is an `R`-linear map. -/ def smul_right (f : M₂ →ₗ[R] S) (x : M) : M₂ →ₗ[R] M := { to_fun := λb, f b • x, map_add' := λ x y, by rw [f.map_add, add_smul], map_smul' := λ b y, by rw [f.map_smul, smul_assoc] } @[simp] theorem smul_right_apply (f : M₂ →ₗ[R] S) (x : M) (c : M₂) : smul_right f x c = f c • x := rfl end smul_right instance : has_one (M →ₗ[R] M) := ⟨linear_map.id⟩ instance : has_mul (M →ₗ[R] M) := ⟨linear_map.comp⟩ lemma mul_eq_comp (f g : M →ₗ[R] M) : f * g = f.comp g := rfl @[simp] lemma one_apply (x : M) : (1 : M →ₗ[R] M) x = x := rfl @[simp] lemma mul_apply (f g : M →ₗ[R] M) (x : M) : (f * g) x = f (g x) := rfl lemma coe_one : ⇑(1 : M →ₗ[R] M) = _root_.id := rfl lemma coe_mul (f g : M →ₗ[R] M) : ⇑(f * g) = f ∘ g := rfl @[simp] theorem comp_zero : f.comp (0 : M₃ →ₗ[R] M) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : M₂ →ₗ[R] M₃).comp f = 0 := rfl @[simp, norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₗ[R] M₂) : ⇑(∑ i in t, f i) = ∑ i in t, (f i : M → M₂) := add_monoid_hom.map_sum ⟨@to_fun R M M₂ _ _ _ _ _, rfl, λ x y, rfl⟩ _ _ instance : monoid (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..}; { intros, apply linear_map.ext, simp {proj := ff} } @[simp] lemma pow_apply (f : M →ₗ[R] M) (n : ℕ) (m : M) : (f^n) m = (f^[n] m) := begin induction n with n ih, { refl, }, { simp only [function.comp_app, function.iterate_succ, linear_map.mul_apply, pow_succ, ih], exact (function.commute.iterate_self _ _ m).symm, }, end lemma coe_pow (f : M →ₗ[R] M) (n : ℕ) : ⇑(f^n) = (f^[n]) := by { ext m, apply pow_apply, } section variables {f' : M →ₗ[R] M} lemma iterate_succ (n : ℕ) : (f' ^ (n + 1)) = comp (f' ^ n) f' := by rw [pow_succ', mul_eq_comp] lemma iterate_surjective (h : surjective f') : ∀ n : ℕ, surjective ⇑(f' ^ n) | 0 := surjective_id | (n + 1) := by { rw [iterate_succ], exact surjective.comp (iterate_surjective n) h, } lemma iterate_injective (h : injective f') : ∀ n : ℕ, injective ⇑(f' ^ n) | 0 := injective_id | (n + 1) := by { rw [iterate_succ], exact injective.comp (iterate_injective n) h, } lemma iterate_bijective (h : bijective f') : ∀ n : ℕ, bijective ⇑(f' ^ n) | 0 := bijective_id | (n + 1) := by { rw [iterate_succ], exact bijective.comp (iterate_bijective n) h, } lemma injective_of_iterate_injective {n : ℕ} (hn : n ≠ 0) (h : injective ⇑(f' ^ n)) : injective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), iterate_succ, coe_comp] at h, exact injective.of_comp h, end end section open_locale classical /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = ∑ i, x i • (f (λj, if i = j then 1 else 0)) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw f.map_smul end end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_monoid M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] (f g : M →ₗ[R] M₂) /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₗ[R] M₂) := ⟨λ f, ⟨λ b, - f b, by simp [add_comm], by simp⟩⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl @[simp] lemma comp_neg (g : M₂ →ₗ[R] M₃) : g.comp (- f) = - g.comp f := by { ext, simp } /-- The negation of a linear map is linear. -/ instance : has_sub (M →ₗ[R] M₂) := ⟨λ f g, ⟨λ b, f b - g b, by { simp only [map_add, sub_eq_add_neg, neg_add], cc }, by { intros, simp only [map_smul, smul_sub] }⟩⟩ @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl lemma sub_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (g - h).comp f = g.comp f - h.comp f := rfl lemma comp_sub (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (g - f) = h.comp g - h.comp f := by { ext, simp } /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, ..}; intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg] instance linear_map_apply_is_add_group_hom (a : M) : is_add_group_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a } end add_comm_group section has_scalar variables {S : Type*} [semiring R] [monoid S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [distrib_mul_action S M₂] [smul_comm_class R S M₂] (f : M →ₗ[R] M₂) instance : has_scalar S (M →ₗ[R] M₂) := ⟨λ a f, ⟨λ b, a • f b, λ x y, by rw [f.map_add, smul_add], λ c x, by simp only [f.map_smul, smul_comm c]⟩⟩ @[simp] lemma smul_apply (a : S) (x : M) : (a • f) x = a • f x := rfl instance {T : Type*} [monoid T] [distrib_mul_action T M₂] [smul_comm_class R T M₂] [smul_comm_class S T M₂] : smul_comm_class S T (M →ₗ[R] M₂) := ⟨λ a b f, ext $ λ x, smul_comm _ _ _⟩ -- example application of this instance: if S -> T -> R are homomorphisms of commutative rings and -- M and M₂ are R-modules then the S-module and T-module structures on Hom_R(M,M₂) are compatible. instance {T : Type*} [monoid T] [has_scalar S T] [distrib_mul_action T M₂] [smul_comm_class R T M₂] [is_scalar_tower S T M₂] : is_scalar_tower S T (M →ₗ[R] M₂) := { smul_assoc := λ _ _ _, ext $ λ _, smul_assoc _ _ _ } instance : distrib_mul_action S (M →ₗ[R] M₂) := { one_smul := λ f, ext $ λ _, one_smul _ _, mul_smul := λ c c' f, ext $ λ _, mul_smul _ _ _, smul_add := λ c f g, ext $ λ x, smul_add _ _ _, smul_zero := λ c, ext $ λ x, smul_zero _ } theorem smul_comp (a : S) (g : M₃ →ₗ[R] M₂) (f : M →ₗ[R] M₃) : (a • g).comp f = a • (g.comp f) := rfl end has_scalar section semimodule variables {S : Type*} [semiring R] [semiring S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule S M₂] [semimodule S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] (f : M →ₗ[R] M₂) instance : semimodule S (M →ₗ[R] M₂) := { add_smul := λ a b f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } variable (S) /-- Applying a linear map at `v : M`, seen as `S`-linear map from `M →ₗ[R] M₂` to `M₂`. See `linear_map.applyₗ` for a version where `S = R`. -/ @[simps] def applyₗ' : M →+ (M →ₗ[R] M₂) →ₗ[S] M₂ := { to_fun := λ v, { to_fun := λ f, f v, map_add' := λ f g, f.add_apply g v, map_smul' := λ x f, f.smul_apply x v }, map_zero' := linear_map.ext $ λ f, f.map_zero, map_add' := λ x y, linear_map.ext $ λ f, f.map_add _ _ } section variables (R M) /-- The equivalence between R-linear maps from `R` to `M`, and points of `M` itself. This says that the forgetful functor from `R`-modules to types is representable, by `R`. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ @[simps] def ring_lmap_equiv_self [semimodule S M] [smul_comm_class R S M] : (R →ₗ[R] M) ≃ₗ[S] M := { to_fun := λ f, f 1, inv_fun := smul_right (1 : R →ₗ[R] R), left_inv := λ f, by { ext, simp }, right_inv := λ x, by simp, .. applyₗ' S (1 : R) } end end semimodule section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (f g : M →ₗ[R] M₂) include R theorem comp_smul (g : M₂ →ₗ[R] M₃) (a : R) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def comp_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := ⟨f.comp, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ /-- Applying a linear map at `v : M`, seen as a linear map from `M →ₗ[R] M₂` to `M₂`. See also `linear_map.applyₗ'` for a version that works with two different semirings. This is the `linear_map` version of `add_monoid_hom.eval`. -/ @[simps] def applyₗ : M →ₗ[R] (M →ₗ[R] M₂) →ₗ[R] M₂ := { to_fun := λ v, { to_fun := λ f, f v, ..applyₗ' R v }, map_smul' := λ x y, linear_map.ext $ λ f, f.map_smul _ _, ..applyₗ' R } /-- Alternative version of `dom_restrict` as a linear map. -/ def dom_restrict' (p : submodule R M) : (M →ₗ[R] M₂) →ₗ[R] (p →ₗ[R] M₂) := { to_fun := λ φ, φ.dom_restrict p, map_add' := by simp [linear_map.ext_iff], map_smul' := by simp [linear_map.ext_iff] } @[simp] lemma dom_restrict'_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : dom_restrict' p f x = f x := rfl end comm_semiring section semiring variables [semiring R] [add_comm_monoid M] [semimodule R M] instance endomorphism_semiring : semiring (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_monoid, ..}; { intros, apply linear_map.ext, simp {proj := ff} } end semiring section ring variables [ring R] [add_comm_group M] [semimodule R M] instance endomorphism_ring : ring (M →ₗ[R] M) := { ..linear_map.endomorphism_semiring, ..linear_map.add_comm_group } end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] /-- The family of linear maps `M₂ → M` parameterised by `f ∈ M₂ → R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M := { to_fun := λ f, { to_fun := linear_map.smul_right f, map_add' := λ m m', by { ext, apply smul_add, }, map_smul' := λ c m, by { ext, apply smul_comm, } }, map_add' := λ f f', by { ext, apply add_smul, }, map_smul' := λ c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightₗ_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_rightₗ : (M₂ →ₗ R) →ₗ M →ₗ M₂ →ₗ M) f x c = (f c) • x := rfl end comm_ring end linear_map /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set variables {p p'} /-- If two submodules `p` and `p'` satisfy `p ⊆ p'`, then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≤ p') : p →ₗ[R] p' := p.subtype.cod_restrict p' $ λ ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≤ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } instance add_comm_monoid_submodule : add_comm_monoid (submodule R M) := { 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 (p q : submodule R M) : p + q = p ⊔ q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl variables (R) lemma subsingleton_iff : subsingleton M ↔ subsingleton (submodule R M) := add_submonoid.subsingleton_iff.trans $ begin rw [←subsingleton_iff_bot_eq_top, ←subsingleton_iff_bot_eq_top], convert to_add_submonoid_eq; refl end lemma nontrivial_iff : nontrivial M ↔ nontrivial (submodule R M) := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R).trans not_nontrivial_iff_subsingleton.symm) variables {R} instance [subsingleton M] : unique (submodule R M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ ((subsingleton_iff R).mp ‹_›) a _⟩ instance unique' [subsingleton R] : unique (submodule R M) := by haveI := semimodule.subsingleton R M; apply_instance instance [nontrivial M] : nontrivial (submodule R M) := (nontrivial_iff R).mp ‹_› theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:M) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set M)) ↔ _, by simp theorem disjoint_def' {p p' : submodule R M} : disjoint p p' ↔ ∀ (x ∈ p) (y ∈ p'), x = y → x = (0:M) := disjoint_def.trans ⟨λ h x hx y hy hxy, h x hx $ hxy.symm ▸ hy, λ h x hx hx', h _ hx x hx' rfl⟩ theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : M →ₗ[R] M₂) (p : submodule R M) : submodule R M₂ := { carrier := f '' p, smul_mem' := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩, .. p.to_add_submonoid.map f.to_add_monoid_hom } @[simp] lemma map_coe (f : M →ₗ[R] M₂) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl @[simp] lemma mem_map {f : M →ₗ[R] M₂} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M →ₗ[R] M₂} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h @[simp] lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M) : map (g.comp f) p = map g (map f p) := set_like.coe_injective $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M →ₗ[R] M₂} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M →ₗ[R] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] lemma range_map_nonempty (N : submodule R M) : (set.range (λ ϕ, submodule.map ϕ N : (M →ₗ[R] M₂) → submodule R M₂)).nonempty := ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩ /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : M →ₗ[R] M₂) (p : submodule R M₂) : submodule R M := { carrier := f ⁻¹' p, smul_mem' := λ a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f.to_add_monoid_hom } @[simp] lemma comap_coe (f : M →ₗ[R] M₂) (p : submodule R M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := set_like.coe_injective rfl lemma comap_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M₃) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : M →ₗ[R] M₂} {q q' : submodule R M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : M →ₗ[R] M₂} {p : submodule R M} {q : submodule R M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : M →ₗ[R] M₂) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M →ₗ[R] M₂) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M →ₗ[R] M₂) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : M →ₗ[R] M₂) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : M →ₗ[R] M₂) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M →ₗ[R] M₂) q = ⊤ := ext $ by simp lemma map_comap_le (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : M →ₗ[R] M₂) (p : submodule R M) : 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 : M →ₗ[R] M₂} {p : submodule R M} {p' : submodule R M₂} : 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 R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span lemma map_span (f : M →ₗ[R] M₂) (s : set M) : (span R s).map f = span R (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ λ x hx, subset_span ⟨x, hx, rfl⟩ /- See also `span_preimage_eq` below. -/ lemma span_preimage_le (f : M →ₗ[R] M₂) (s : set M₂) : span R (f ⁻¹' s) ≤ (span R s).comap f := by { rw [span_le, comap_coe], exact preimage_mono (subset_span), } /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ set_like.le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr lemma span_eq_supr_of_singleton_spans (s : set M) : span R s = ⨆ x ∈ s, span R {x} := by simp only [←span_Union, set.bUnion_of_singleton s] @[simp] theorem coe_supr_of_directed {ι} [hι : nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) ⊆ ⋃ (i : ι), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (λ x hx, span_induction hx (λ _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hι.elim (λ i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma sum_mem_bsupr {ι : Type*} {s : finset ι} {f : ι → M} {p : ι → submodule R M} (h : ∀ i ∈ s, f i ∈ p i) : ∑ i in s, f i ∈ ⨆ i ∈ s, p i := sum_mem _ $ λ i hi, mem_supr_of_mem i $ mem_supr_of_mem hi (h i hi) lemma sum_mem_supr {ι : Type*} [fintype ι] {f : ι → M} {p : ι → submodule R M} (h : ∀ i, f i ∈ p i) : ∑ i, f i ∈ ⨆ i, p i := sum_mem _ $ λ i hi, mem_supr_of_mem i (h i) @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← set_like.mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≤) s) : z ∈ Sup s ↔ ∃ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] 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 [add_assoc]; cc⟩ }, { 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)⟩ lemma mem_sup' : x ∈ p ⊔ p' ↔ ∃ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] end /- This is the character `∙`, with escape sequence `\.`, and is thus different from the scalar multiplication character `•`, with escape sequence `\bub`. -/ notation R`∙`:1000 x := span R (@singleton _ _ set.has_singleton x) lemma mem_span_singleton_self (x : M) : x ∈ R ∙ x := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x ≠ 0) : nontrivial (R ∙ x) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ (R ∙ y) ↔ ∃ a:R, 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 le_span_singleton_iff {s : submodule R M} {v₀ : M} : s ≤ (R ∙ v₀) ↔ ∀ v ∈ s, ∃ r : R, r • v₀ = v := by simp_rw [set_like.le_def, mem_span_singleton] @[simp] lemma span_zero_singleton : (R ∙ (0:M)) = ⊥ := by { ext, simp [mem_span_singleton, eq_comm] } lemma span_singleton_eq_range (y : M) : ↑(R ∙ y) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma span_singleton_smul_le (r : R) (x : M) : (R ∙ (r • x)) ≤ R ∙ x := begin rw [span_le, set.singleton_subset_iff, set_like.mem_coe], exact smul_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_smul_eq {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {r : K} (x : E) (hr : r ≠ 0) : (K ∙ (r • x)) = K ∙ x := begin refine le_antisymm (span_singleton_smul_le r x) _, convert span_singleton_smul_le r⁻¹ (r • x), exact (inv_smul_smul' hr _).symm end lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (K ∙ x) ↔ (x ∈ s → x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, hc⟩ := mem_span_singleton.1 hyx, subst y, classical, by_cases hc : c = 0, by simp only [hc, zero_smul], rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] end lemma disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x ≠ 0) : disjoint p (K ∙ x) ↔ x ∉ p := disjoint_span_singleton.trans ⟨λ h₁ h₂, x0 (h₁ h₂), λ h₁ h₂, (h₁ h₂).elim⟩ lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : (R ∙ x) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = ⊥ := by rw [←singleton_zero, span_singleton_eq_bot] @[simp] lemma span_image (f : M →ₗ[R] M₂) : span R (f '' s) = map f (span R 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 apply_mem_span_image_of_mem_span (f : M →ₗ[R] M₂) {x : M} {s : set M} (h : x ∈ submodule.span R s) : f x ∈ submodule.span R (f '' s) := begin rw submodule.span_image, exact submodule.mem_map_of_mem h end /-- `f` is an explicit argument so we can `apply` this theorem and obtain `h` as a new goal. -/ lemma not_mem_span_of_apply_not_mem_span_image (f : M →ₗ[R] M₂) {x : M} {s : set M} (h : f x ∉ submodule.span R (f '' s)) : x ∉ submodule.span R s := not.imp h (apply_mem_span_image_of_mem_span f) lemma supr_eq_span {ι : Sort w} (p : ι → submodule R M) : (⨆ (i : ι), p i) = submodule.span R (⋃ (i : ι), ↑(p i)) := le_antisymm (supr_le $ assume i, subset.trans (assume m hm, set.mem_Union.mpr ⟨i, hm⟩) subset_span) (span_le.mpr $ Union_subset_iff.mpr $ assume i m hm, mem_supr_of_mem i hm) lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : (R ∙ m) ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, set_like.mem_coe] lemma singleton_span_is_compact_element (x : M) : complete_lattice.is_compact_element (span R {x} : submodule R M) := begin rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, have : x ∈ Sup d, from (set_like.le_def.mp hsup) (mem_span_singleton_self x), obtain ⟨y, ⟨hyd, hxy⟩⟩ := (mem_Sup_of_directed hemp hdir).mp this, exact ⟨y, ⟨hyd, by simpa only [span_le, singleton_subset_iff]⟩⟩, end instance : is_compactly_generated (submodule R M) := ⟨λ s, ⟨(λ x, span R {x}) '' s, ⟨λ t ht, begin rcases (set.mem_image _ _ _).1 ht with ⟨x, hx, rfl⟩, apply singleton_span_is_compact_element, end, by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, span_eq]⟩⟩⟩ lemma lt_add_iff_not_mem {I : submodule R M} {a : M} : I < I + (R ∙ a) ↔ a ∉ I := begin split, { intro h, by_contra akey, have h1 : I + (R ∙ a) ≤ I, { simp only [add_eq_sup, sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply set_like.lt_iff_le_and_exists.mpr, split, simp only [add_eq_sup, le_sup_left], use a, split, swap, { assumption, }, { have : (R ∙ a) ≤ I + (R ∙ a) := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ι : Sort w} (p : ι → submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end section open_locale classical /-- For every element in the span of a set, there exists a finite subset of the set such that the element is contained in the span of the subset. -/ lemma mem_span_finite_of_mem_span {S : set M} {x : M} (hx : x ∈ span R S) : ∃ T : finset M, ↑T ⊆ S ∧ x ∈ span R (T : set M) := begin refine span_induction hx (λ x hx, _) _ _ _, { refine ⟨{x}, _, _⟩, { rwa [finset.coe_singleton, set.singleton_subset_iff] }, { rw finset.coe_singleton, exact submodule.mem_span_singleton_self x } }, { use ∅, simp }, { rintros x y ⟨X, hX, hxX⟩ ⟨Y, hY, hyY⟩, refine ⟨X ∪ Y, _, _⟩, { rw finset.coe_union, exact set.union_subset hX hY }, rw [finset.coe_union, span_union, mem_sup], exact ⟨x, hxX, y, hyY, rfl⟩, }, { rintros a x ⟨T, hT, h2⟩, exact ⟨T, hT, smul_mem _ _ h2⟩ } end end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M₂) := { carrier := set.prod p q, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q.to_add_submonoid } @[simp] lemma prod_coe : (prod p q : set (M × M₂)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M₂} {x : M × M₂} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M₂) : span R (set.prod s t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M₂)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M₂)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M₂} : 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') := set_like.coe_injective 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 [set_like.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 end add_comm_monoid variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set @[simp] lemma neg_coe : -(p : set M) = p := set.ext $ λ x, p.neg_mem_iff @[simp] protected lemma map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext $ λ y, ⟨λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, f.map_neg x⟩, λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, ((-f).map_neg _).trans (neg_neg (f x))⟩⟩ @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M →ₗ[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : (map_span _ _).symm ... = span R s : by simp lemma mem_span_insert' {y} {s : set M} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end -- TODO(Mario): Factor through add_subgroup /-- The equivalence relation associated to a submodule `p`, defined by `x ≈ y` iff `y - x ∈ p`. -/ def quotient_rel : setoid M := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa [sub_eq_add_neg, add_left_comm, add_assoc] using add_mem _ h₁ h₂⟩ /-- The quotient of a module `M` by a submodule `p ⊆ M`. -/ def quotient : Type* := quotient (quotient_rel p) namespace quotient /-- Map associating to an element of `M` the corresponding element of `M/p`, when `p` is a submodule of `M`. -/ def mk {p : submodule R M} : M → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule R M} (x : M) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule R M} (x : M) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule R M} (x : M) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : M} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ instance : inhabited (quotient p) := ⟨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 [sub_eq_add_neg, add_left_comm, add_comm] 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 : has_sub (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 [sub_eq_add_neg, add_left_comm, add_comm] using add_mem p h₁ (neg_mem p h₂)⟩ @[simp] theorem mk_sub : (mk (x - y) : quotient p) = mk x - mk y := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, ← mk_zero p, -mk_add, ← mk_add p, -mk_neg, ← mk_neg p, -mk_sub, ← mk_sub p, sub_eq_add_neg]; cc instance : has_scalar R (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : semimodule R (quotient p) := semimodule.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] lemma mk_surjective : function.surjective (@mk _ _ _ _ _ p) := by { rintros ⟨x⟩, exact ⟨x, rfl⟩ } lemma nontrivial_of_lt_top (h : p < ⊤) : nontrivial (p.quotient) := begin obtain ⟨x, _, not_mem_s⟩ := set_like.exists_of_lt h, refine ⟨⟨mk x, 0, _⟩⟩, simpa using not_mem_s end end quotient lemma quot_hom_ext ⦃f g : quotient p →ₗ[R] M₂⦄ (h : ∀ x, f (quotient.mk x) = g (quotient.mk x)) : f = g := linear_map.ext $ λ x, quotient.induction_on' x h end submodule namespace submodule variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (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 : V →ₗ[K] V₂) (p : submodule K V) (a : K) (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 lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole semimodule and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M →ₗ[R] M₂} (hv : span R s = ⊤) (h : set.eq_on f g s) : f = g := linear_map.ext (λ x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ι → M` generates the whole semimodule and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {v : ι → M} {f g : M →ₗ[R] M₂} (hv : span R (set.range v) = ⊤) (h : ∀i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) section finsupp variables {γ : Type*} [has_zero γ] @[simp] lemma map_finsupp_sum (f : M →ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_finsupp_sum (t : ι →₀ γ) (g : ι → γ → M →ₗ[R] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma finsupp_sum_apply (t : ι →₀ γ) (g : ι → γ → M →ₗ[R] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end finsupp section dfinsupp variables {γ : ι → Type*} [decidable_eq ι] [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] @[simp] lemma map_dfinsupp_sum (f : M →ₗ[R] M₂) {t : Π₀ i, γ i} {g : Π i, γ i → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_dfinsupp_sum (t : Π₀ i, γ i) (g : Π i, γ i → M →ₗ[R] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma dfinsupp_sum_apply (t : Π₀ i, γ i) (g : Π i, γ i → M →ₗ[R] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end dfinsupp theorem map_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.ext_iff_val] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (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⟩ /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. -/ def range (f : M →ₗ[R] M₂) : submodule R M₂ := map f ⊤ theorem range_coe (f : M →ₗ[R] M₂) : (range f : set M₂) = set.range f := set.image_univ @[simp] theorem mem_range {f : M →ₗ[R] M₂} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := set.ext_iff.1 (range_coe f) theorem mem_range_self (f : M →ₗ[R] M₂) (x : M) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := map_id _ theorem range_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : M →ₗ[R] M₂} : range f = ⊤ ↔ surjective f := by rw [set_like.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M →ₗ[R] M₂} {p : submodule R M} : map f p ≤ range f := map_mono le_top /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : M →ₗ[R] M₂) : M →ₗ[R] f.range := f.cod_restrict f.range f.mem_range_self section variables (R) (M) /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ def to_span_singleton (x : M) : R →ₗ[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : (R ∙ x) = (to_span_singleton R M x).range := submodule.ext $ λ y, by {refine iff.trans _ mem_range.symm, exact mem_span_singleton } lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ end /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M →ₗ[R] M₂) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : M →ₗ[R] M₂} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl @[simp] theorem map_coe_ker (f : M →ₗ[R] M₂) (x : ker f) : f x = 0 := mem_ker.1 x.2 lemma comp_ker_subtype (f : M →ₗ[R] M₂) : f.comp f.ker.subtype = 0 := linear_map.ext $ λ x, suffices f x = 0, by simp [this], mem_ker.1 x.2 theorem ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem ker_eq_bot' {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := by simpa [disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot_of_inverse {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M} (h : g.comp f = id) : ker f = ⊥ := ker_eq_bot'.2 $ λ m hm, by rw [← id_apply m, ← h, comp_apply, hm, g.map_zero] lemma le_ker_iff_map {f : M →ₗ[R] M₂} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma ker_restrict {p : submodule R M} {f : M →ₗ[R] M} (hf : ∀ x : M, x ∈ p → f x ∈ p) : ker (f.restrict hf) = (f.dom_restrict p).ker := by rw [restrict_eq_cod_restrict_dom_restrict, ker_cod_restrict] lemma map_comap_eq (f : M →ₗ[R] M₂) (q : submodule R M₂) : 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 : M →ₗ[R] M₂} {q : submodule R M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [map_comap_eq, inf_eq_right] @[simp] theorem ker_zero : ker (0 : M →ₗ[R] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : M →ₗ[R] M₂) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : M →ₗ[R] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : M →ₗ[R] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem range_eq_bot {f : M →ₗ[R] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] lemma range_le_ker_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : range f ≤ ker g ↔ g.comp f = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ λ x, h $ mem_map_of_mem trivial, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y ⟨_, hy⟩, by rw [←hy, ←comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : M →ₗ[R] M₂} (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 : M →ₗ[R] M₂} (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 ker_eq_bot_of_injective {f : M →ₗ[R] M₂} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ f.ker, by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint] end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule lemma comap_map_eq (f : M →ₗ[R] M₂) (p : submodule R M) : 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 : M →ₗ[R] M₂} {p : submodule R M} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M →ₗ[R] M₂) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, comap_map_eq] theorem map_le_map_iff' {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : M →ₗ[R] M₂} (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 map_eq_top_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ f.ker = ⊤ := by simp_rw [← top_le_iff, ← hf, range, map_le_map_iff] end add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables {f : M →ₗ[R] M₂} include R open submodule theorem sub_mem_ker_iff {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {p : submodule R M} : 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 {p : submodule R M} {s : set M} (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) theorem ker_eq_bot : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ lemma ker_le_iff {p : submodule R M} : ker f ≤ p ↔ ∃ (y ∈ range f), f ⁻¹' {y} ⊆ p := begin split, { intros h, use 0, rw [← set_like.mem_coe, f.range_coe], exact ⟨⟨0, map_zero f⟩, h⟩, }, { rintros ⟨y, h₁, h₂⟩, rw set_like.le_def, intros z hz, simp only [mem_ker, set_like.mem_coe] at hz, rw [← set_like.mem_coe, f.range_coe, set.mem_range] at h₁, obtain ⟨x, hx⟩ := h₁, have hx' : x ∈ p, { exact h₂ hx, }, have hxz : z + x ∈ p, { apply h₂, simp [hx, hz], }, suffices : z + x - x ∈ p, { simpa only [this, add_sub_cancel], }, exact p.sub_mem hxz hx', }, end end ring section field variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a lemma span_singleton_sup_ker_eq_top (f : V →ₗ[K] K) {x : V} (hx : f x ≠ 0) : (K ∙ x) ⊔ f.ker = ⊤ := eq_top_iff.2 (λ y hy, submodule.mem_sup.2 ⟨(f y * (f x)⁻¹) • x, submodule.mem_span_singleton.2 ⟨f y * (f x)⁻¹, rfl⟩, ⟨y - (f y * (f x)⁻¹) • x, by rw [linear_map.mem_ker, f.map_sub, f.map_smul, smul_eq_mul, mul_assoc, inv_mul_cancel hx, mul_one, sub_self], by simp only [add_sub_cancel'_right]⟩⟩) end field end linear_map namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [semimodule R M] : is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp, cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [semiring R] [add_comm_group M] [semimodule R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule section add_comm_monoid variables {T : semiring R} [add_comm_monoid M] [add_comm_monoid M₂] variables [semimodule R M] [semimodule R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map @[simp] theorem map_top (f : M →ₗ[R] M₂) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : M →ₗ[R] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot_of_injective $ λ x y, subtype.ext_val @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 (le_refl _) @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (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] end add_comm_monoid section ring variables {T : ring R} [add_comm_group M] [add_comm_group M₂] [semimodule R M] [semimodule R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff' p.ker_subtype, map_bot, map_comap_subtype, disjoint] /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≤ p} := { 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.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := λ p₁ p₂, map_le_map_iff' (ker_subtype p) } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p ↪o submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl /-- The map from a module `M` to the quotient of `M` by a submodule `p` as a linear map. -/ def mkq : M →ₗ[R] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- The map from the quotient of `M` by a submodule `p` to `M₂` induced by a linear map `f : M → M₂` vanishing on `p`, as a linear map. -/ def liftq (f : M →ₗ[R] M₂) (h : p ≤ f.ker) : p.quotient →ₗ[R] M₂ := ⟨λ 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 : M →ₗ[R] M₂) {h} (x : M) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : M →ₗ[R] M₂) (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 R 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] @[simp] theorem map_mkq_eq_top : map p.mkq p' = ⊤ ↔ p ⊔ p' = ⊤ := by simp only [map_eq_top_iff p.range_mkq, sup_comm, ker_mkq] /-- The map from the quotient of `M` by submodule `p` to the quotient of `M₂` by submodule `q` along `f : M → M₂` is linear. -/ def mapq (f : M →ₗ[R] M₂) (h : p ≤ comap f q) : p.quotient →ₗ[R] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : M →ₗ[R] M₂) {h} (x : M) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : M →ₗ[R] M₂) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : M →ₗ[R] M₂) (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 : M →ₗ[R] M₂) (h) (q : submodule R (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 : M →ₗ[R] M₂) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : M →ₗ[R] M₂) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : M →ₗ[R] M₂) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- The correspondence theorem for modules: there is an order isomorphism between submodules of the quotient of `M` by `p`, and submodules of `M` larger than `p`. -/ def comap_mkq.rel_iso : submodule R p.quotient ≃o {p' : submodule R M // p ≤ p'} := { 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.ext_val $ by simpa [comap_map_mkq p], map_rel_iff' := λ p₁ p₂, comap_le_comap_iff $ range_mkq _ } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.order_embedding : submodule R p.quotient ↪o submodule R M := (rel_iso.to_rel_embedding $ comap_mkq.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.order_embedding p p' = comap p.mkq p' := rfl lemma span_preimage_eq {f : M →ₗ[R] M₂} {s : set M₂} (h₀ : s.nonempty) (h₁ : s ⊆ range f) : span R (f ⁻¹' s) = (span R s).comap f := begin suffices : (span R s).comap f ≤ span R (f ⁻¹' s), { exact le_antisymm (span_preimage_le f s) this, }, have hk : ker f ≤ span R (f ⁻¹' s), { let y := classical.some h₀, have hy : y ∈ s, { exact classical.some_spec h₀, }, rw ker_le_iff, use [y, h₁ hy], rw ← set.singleton_subset_iff at hy, exact set.subset.trans subset_span (span_mono (set.preimage_mono hy)), }, rw ← left_eq_sup at hk, rw f.range_coe at h₁, rw [hk, ← map_le_map_iff, map_span, map_comap_eq, set.image_preimage_eq_of_subset h₁], exact inf_le_right, end end ring end submodule namespace linear_map section semiring variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : f.ker →ₗ[R] M), f.comp u = f.comp v → u = v) : f.ker = ⊥ := begin have h₁ : f.comp (0 : f.ker →ₗ[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end lemma range_comp_of_range_eq_top {f : M →ₗ[R] M₂} (g : M₂ →ₗ[R] M₃) (hf : range f = ⊤) : range (g.comp f) = range g := by rw [range_comp, hf, submodule.map_top] lemma ker_comp_of_ker_eq_bot (f : M →ₗ[R] M₂) {g : M₂ →ₗ[R] M₃} (hg : ker g = ⊥) : ker (g.comp f) = ker f := by rw [ker_comp, hg, submodule.comap_bot] end semiring section ring variables [ring R] [add_comm_monoid M] [add_comm_group M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] lemma range_mkq_comp (f : M →ₗ[R] M₂) : f.range.mkq.comp f = 0 := linear_map.ext $ λ x, by simp lemma ker_le_range_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : g.ker ≤ f.range ↔ f.range.mkq.comp g.ker.subtype = 0 := by rw [←range_le_ker_iff, submodule.ker_mkq, submodule.range_subtype] /-- An epimorphism is surjective. -/ lemma range_eq_top_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : M₂ →ₗ[R] f.range.quotient), u.comp f = v.comp f → u = v) : f.range = ⊤ := begin have h₁ : (0 : M₂ →ₗ[R] f.range.quotient).comp f = 0 := zero_comp _, rw [←submodule.ker_mkq f.range, ←h 0 f.range.mkq (eq.trans h₁ (range_mkq_comp _).symm)], exact ker_zero end end ring end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ namespace linear_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (e e' : M ≃ₗ[R] M₂) lemma map_eq_comap {p : submodule R M} : (p.map e : submodule R M₂) = p.comap e.symm := set_like.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule `p` of the domain onto the image of that submodule. This is `linear_equiv.of_submodule'` but with `map` on the right instead of `comap` on the left. -/ def of_submodule (p : submodule R M) : p ≃ₗ[R] ↥(p.map ↑e : submodule R M₂) := { inv_fun := λ y, ⟨e.symm y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := λ x, by simp, right_inv := λ y, by { apply set_coe.ext, simp, }, ..((e : M →ₗ[R] M₂).dom_restrict p).cod_restrict (p.map ↑e) (λ x, ⟨x, by simp⟩) } @[simp] lemma of_submodule_apply (p : submodule R M) (x : p) : ↑(e.of_submodule p x) = e x := rfl @[simp] lemma of_submodule_symm_apply (p : submodule R M) (x : (p.map ↑e : submodule R M₂)) : ↑((e.of_submodule p).symm x) = e.symm x := rfl end section uncurry variables (V V₂ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def uncurry : (V → V₂ → R) ≃ₗ[R] (V × V₂ → R) := { map_add' := λ _ _, by { ext ⟨⟩, refl }, map_smul' := λ _ _, by { ext ⟨⟩, refl }, .. equiv.arrow_arrow_equiv_prod_arrow _ _ _} @[simp] lemma coe_uncurry : ⇑(linear_equiv.uncurry R V V₂) = uncurry := rfl @[simp] lemma coe_uncurry_symm : ⇑(linear_equiv.uncurry R V V₂).symm = curry := rfl end uncurry section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} {semimodule_M₃ : semimodule R M₃} variables (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) (e : M ≃ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) (l : M₃ →ₗ[R] M) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := λ _ _, rfl, map_add' := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R M₂) (h : p.map ↑e = q) : p ≃ₗ[R] q := (e.of_submodule p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl /-- A linear equivalence of two modules restricts to a linear equivalence from the preimage of any submodule to that submodule. This is `linear_equiv.of_submodule` but with `comap` on the left instead of `map` on the right. -/ def of_submodule' [semimodule R M] [semimodule R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) : U.comap (f : M →ₗ[R] M₂) ≃ₗ[R] U := (f.symm.of_submodules _ _ f.symm.map_eq_comap).symm lemma of_submodule'_to_linear_map [semimodule R M] [semimodule R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) : (f.of_submodule' U).to_linear_map = (f.to_linear_map.dom_restrict _).cod_restrict _ subtype.prop := by { ext, refl } @[simp] lemma of_submodule'_apply [semimodule R M] [semimodule R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) (x : U.comap (f : M →ₗ[R] M₂)) : (f.of_submodule' U x : M₂) = f (x : M) := rfl @[simp] lemma of_submodule'_symm_apply [semimodule R M] [semimodule R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) (x : U) : ((f.of_submodule' U).symm x : M) = f.symm (x : M₂) := rfl variable (p) /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₗ[R] M₂ := { 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 {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem range : (e : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective lemma eq_bot_of_equiv [semimodule R M₂] (e : p ≃ₗ[R] (⊥ : submodule R M₂)) : p = ⊥ := begin refine bot_unique (set_like.le_def.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end @[simp] protected theorem ker : (e : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective @[simp] theorem range_comp : (h.comp (e : M →ₗ[R] M₂)).range = h.range := linear_map.range_comp_of_range_eq_top _ e.range @[simp] theorem ker_comp : ((e : M →ₗ[R] M₂).comp l).ker = l.ker := linear_map.ker_comp_of_ker_eq_bot _ e.ker variables {f g} /-- An linear map `f : M →ₗ[R] M₂` with a left-inverse `g : M₂ →ₗ[R] M` defines a linear equivalence between `M` and `f.range`. This is a computable alternative to `linear_equiv.of_injective`, and a bidirectional version of `linear_map.range_restrict`. -/ def of_left_inverse {g : M₂ → M} (h : function.left_inverse g f) : M ≃ₗ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := linear_map.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.range_restrict } @[simp] lemma of_left_inverse_apply (h : function.left_inverse g f) (x : M) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl end end add_comm_monoid section add_comm_group variables [semiring R] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b end add_comm_group section neg variables (R) [semiring R] [add_comm_group M] [semimodule R M] /-- `x ↦ -x` as a `linear_equiv` -/ def neg : M ≃ₗ[R] M := { .. equiv.neg M, .. (-linear_map.id : M →ₗ[R] M) } variable {R} @[simp] lemma coe_neg : ⇑(neg R : M ≃ₗ[R] M) = -id := rfl lemma neg_apply (x : M) : neg R x = -x := by simp @[simp] lemma symm_neg : (neg R : M ≃ₗ[R] M).symm = neg R := rfl end neg section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) (e : M ≃ₗ[R] M₂) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. See also `linear_map.of_left_inverse`. -/ noncomputable def of_injective (h : f.ker = ⊥) : M ≃ₗ[R] f.range := of_left_inverse $ classical.some_spec (linear_map.ker_eq_bot.1 h).has_left_inverse @[simp] theorem of_injective_apply {h : f.ker = ⊥} (x : M) : ↑(of_injective f h x) = f x := rfl /-- A bijective linear map is a linear equivalence. Here, bijectivity is described by saying that the kernel of `f` is `{0}` and the range is the universal set. -/ noncomputable def of_bijective (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : M ≃ₗ[R] M₂ := (of_injective f hf₁).trans (of_top _ hf₂) @[simp] theorem of_bijective_apply {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] open linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) • 1 : M →ₗ M) (((a⁻¹ : units R) : R) • 1 : M →ₗ M) (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) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f, (e₂ : M₂₁ →ₗ[R] M₂₂).comp $ f.comp e₁.symm, inv_fun := λ f, (e₂.symm : M₂₂ →ₗ[R] M₂₁).comp $ f.comp e₁, left_inv := λ f, by { ext x, simp }, right_inv := λ f, by { ext x, simp }, map_add' := λ f g, by { ext x, simp }, map_smul' := λ c f, by { ext x, simp } } @[simp] lemma arrow_congr_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₁ →ₗ[R] M₂₁) (x : M₂) : arrow_congr e₁ e₂ f x = e₂ (f (e₁.symm x)) := rfl @[simp] lemma arrow_congr_symm_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₂ →ₗ[R] M₂₂) (x : M₁) : (arrow_congr e₁ e₂).symm f x = e₂.symm (f (e₁ x)) := rfl lemma arrow_congr_comp {N N₂ N₃ : Sort*} [add_comm_group N] [add_comm_group N₂] [add_comm_group N₃] [module R N] [module R N₂] [module R N₃] (e₁ : M ≃ₗ[R] N) (e₂ : M₂ ≃ₗ[R] N₂) (e₃ : M₃ ≃ₗ[R] N₃) (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ M₂ M₃ N₁ N₂ N₃ : Sort*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] [add_comm_group M₃] [module R M₃] [add_comm_group N₁] [module R N₁] [add_comm_group N₂] [module R N₂] [add_comm_group N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : N₁ ≃ₗ[R] N₂) (e₃ : M₂ ≃ₗ[R] M₃) (e₄ : N₂ ≃ₗ[R] N₃) : (arrow_congr e₁ e₂).trans (arrow_congr e₃ e₄) = arrow_congr (e₁.trans e₃) (e₂.trans e₄) := rfl /-- If `M₂` and `M₃` are linearly isomorphic then the two spaces of linear maps from `M` into `M₂` and `M` into `M₃` are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ (M →ₗ M₃) := arrow_congr (linear_equiv.refl R M) f /-- If `M` and `M₂` are linearly isomorphic then the two spaces of linear maps from `M` and `M₂` to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (module.End R M) ≃ₗ[R] (module.End R M₂) := arrow_congr e e lemma conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) : e.conj f = ((↑e : M →ₗ[R] M₂).comp f).comp e.symm := rfl lemma symm_conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M₂) : e.symm.conj f = ((↑e.symm : M₂ →ₗ[R] M).comp f).comp e := rfl lemma conj_comp (e : M ≃ₗ[R] M₂) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : e₁.conj.trans e₂.conj = (e₁.trans e₂).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] M₂) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } end comm_ring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variables (K) (M) open linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha section noncomputable theory open_locale classical lemma ker_to_span_singleton {x : M} (h : x ≠ 0) : (to_span_singleton K M x).ker = ⊥ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ • (c • x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ • ((to_span_singleton K M x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : M) (h : x ≠ 0) : K ≃ₗ[K] (K ∙ x) := linear_equiv.trans (linear_equiv.of_injective (to_span_singleton K M x) (ker_to_span_singleton K M h)) (of_eq (to_span_singleton K M x).range (K ∙ x) (span_singleton_eq_range K M x).symm) lemma to_span_nonzero_singleton_one (x : M) (h : x ≠ 0) : to_span_nonzero_singleton K M x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) := begin apply set_like.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K M x h 1) = to_span_singleton K M x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : M) (h : x ≠ 0) : (K ∙ x) ≃ₗ[K] K := (to_span_nonzero_singleton K M x h).symm lemma coord_self (x : M) (h : x ≠ 0) : (coord K M x h) (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) = 1 := by rw [← to_span_nonzero_singleton_one K M x h, symm_apply_apply] end end field end linear_equiv namespace submodule section semimodule variables [semiring R] [add_comm_monoid M] [semimodule R M] /-- Given `p` a submodule of the module `M` and `q` a submodule of `p`, `p.equiv_subtype_map q` is the natural `linear_equiv` between `q` and `q.map p.subtype`. -/ def equiv_subtype_map (p : submodule R M) (q : submodule R p) : q ≃ₗ[R] q.map p.subtype := { inv_fun := begin rintro ⟨x, hx⟩, refine ⟨⟨x, _⟩, _⟩; rcases hx with ⟨⟨_, h⟩, _, rfl⟩; assumption end, left_inv := λ ⟨⟨_, _⟩, _⟩, rfl, right_inv := λ ⟨x, ⟨_, h⟩, _, rfl⟩, rfl, .. (p.subtype.dom_restrict q).cod_restrict _ begin rintro ⟨x, hx⟩, refine ⟨x, hx, rfl⟩, end } @[simp] lemma equiv_subtype_map_apply {p : submodule R M} {q : submodule R p} (x : q) : (p.equiv_subtype_map q x : M) = p.subtype.dom_restrict q x := rfl @[simp] lemma equiv_subtype_map_symm_apply {p : submodule R M} {q : submodule R p} (x : q.map p.subtype) : ((p.equiv_subtype_map q).symm x : M) = x := by { cases x, refl } /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := λ x, ⟨x, x.2⟩, inv_fun := λ x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := λ x, by simp only [coe_mk, set_like.eta, coe_coe], right_inv := λ x, by simp only [subtype.coe_mk, set_like.eta, coe_coe], map_add' := λ x y, rfl, map_smul' := λ c x, rfl } end semimodule variables [ring R] [add_comm_group M] [module R M] variables (p : submodule R M) open linear_map /-- If `p = ⊥`, then `M / p ≃ₗ[R] M`. -/ def quot_equiv_of_eq_bot (hp : p = ⊥) : p.quotient ≃ₗ[R] M := linear_equiv.of_linear (p.liftq id $ hp.symm ▸ bot_le) p.mkq (liftq_mkq _ _ _) $ p.quot_hom_ext $ λ x, rfl @[simp] lemma quot_equiv_of_eq_bot_apply_mk (hp : p = ⊥) (x : M) : p.quot_equiv_of_eq_bot hp (quotient.mk x) = x := rfl @[simp] lemma quot_equiv_of_eq_bot_symm_apply (hp : p = ⊥) (x : M) : (p.quot_equiv_of_eq_bot hp).symm x = quotient.mk x := rfl @[simp] lemma coe_quot_equiv_of_eq_bot_symm (hp : p = ⊥) : ((p.quot_equiv_of_eq_bot hp).symm : M →ₗ[R] p.quotient) = p.mkq := rfl variables (q : submodule R M) /-- Quotienting by equal submodules gives linearly equivalent quotients. -/ def quot_equiv_of_eq (h : p = q) : p.quotient ≃ₗ[R] q.quotient := { map_add' := by { rintros ⟨x⟩ ⟨y⟩, refl }, map_smul' := by { rintros x ⟨y⟩, refl }, ..@quotient.congr _ _ (quotient_rel p) (quotient_rel q) (equiv.refl _) $ λ a b, by { subst h, refl } } end submodule namespace submodule variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] variables (p : submodule R M) (q : submodule R M₂) @[simp] lemma mem_map_equiv {e : M ≃ₗ[R] M₂} {x : M₂} : x ∈ p.map (e : M →ₗ[R] M₂) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end lemma comap_le_comap_smul (f : M →ₗ[R] M₂) (c : R) : comap f q ≤ comap (c • f) q := begin rw set_like.le_def, intros m h, change c • (f m) ∈ q, change f m ∈ q at h, apply q.smul_mem _ h, end lemma inf_comap_le_comap_add (f₁ f₂ : M →ₗ[R] M₂) : comap f₁ q ⊓ comap f₂ q ≤ comap (f₁ + f₂) q := begin rw set_like.le_def, intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply q.add_mem h.1 h.2, end /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the set of maps $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \}$ is a submodule of `Hom(M, M₂)`. -/ def compatible_maps : submodule R (M →ₗ[R] M₂) := { carrier := {f | p ≤ comap f q}, zero_mem' := by { change p ≤ comap 0 q, rw comap_zero, refine le_top, }, add_mem' := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add q f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul_mem' := λ c f h, le_trans h (comap_le_comap_smul q f c), } /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the natural map $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \} \to Hom(M/p, M₂/q)$ is linear. -/ def mapq_linear : compatible_maps p q →ₗ[R] p.quotient →ₗ[R] q.quotient := { to_fun := λ f, mapq _ _ f.val f.property, map_add' := λ x y, by { ext m', apply quotient.induction_on' m', intros m, refl, }, map_smul' := λ c f, by { ext m', apply quotient.induction_on' m', intros m, refl, } } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { .. e, .. h.mk' e} end equiv namespace add_equiv variables [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule R M₂] /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl end add_equiv namespace linear_map open submodule section isomorphism_laws variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f : M →ₗ[R] M₂) /-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly equivalent to the range of `f`. -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[R] f.range := (linear_equiv.of_injective (f.ker.liftq f $ le_refl _) $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans (linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _) /-- The first isomorphism theorem for surjective linear maps. -/ noncomputable def quot_ker_equiv_of_surjective (f : M →ₗ[R] M₂) (hf : function.surjective f) : f.ker.quotient ≃ₗ[R] M₂ := f.quot_ker_equiv_range.trans (linear_equiv.of_top f.range (linear_map.range_eq_top.2 hf)) @[simp] lemma quot_ker_equiv_range_apply_mk (x : M) : (f.quot_ker_equiv_range (submodule.quotient.mk x) : M₂) = f x := rfl @[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) : f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x := f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x) /-- Canonical linear map from the quotient `p/(p ∩ p')` to `(p+p')/p'`, mapping `x + (p ∩ p')` to `x + p'`, where `p` and `p'` are submodules of an ambient module. -/ def quotient_inf_to_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient →ₗ[R] (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_right _ le_sup_left) end /-- Second Isomorphism Law : the canonical map from `p/(p ∩ p')` to `(p+p')/p'` as a linear isomorphism. -/ noncomputable def quotient_inf_equiv_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[R] (comap (p ⊔ p').subtype p').quotient := linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p') begin rw [quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], exact λ ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩ end begin rw [quotient_inf_to_sup_quotient, 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 @[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) : ⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl @[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) : quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) = submodule.quotient.mk (of_le (le_sup_left : p ≤ p ⊔ p') x) := rfl lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M) (x : p ⊔ p') (hx : (x:M) ∈ p) : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = submodule.quotient.mk ⟨x, hx⟩ := (linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply] @[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M} {x : p ⊔ p'} : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' := (linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply] lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p ⊔ p'} (hx : (x:M) ∈ p') : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 := quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx end isomorphism_laws section fun_left variables (R M) [semiring R] [add_comm_monoid M] [semimodule R M] variables {m n p : Type*} /-- Given an `R`-module `M` and a function `m → n` between arbitrary types, construct a linear map `(n → M) →ₗ[R] (m → M)` -/ def fun_left (f : m → n) : (n → M) →ₗ[R] (m → M) := mk (∘f) (λ _ _, rfl) (λ _ _, rfl) @[simp] theorem fun_left_apply (f : m → n) (g : n → M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n → M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n → p) (f₂ : m → n) : fun_left R M (f₁ ∘ f₂) = (fun_left R M f₂).comp (fun_left R M f₁) := rfl /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n → M) ≃ₗ[R] (m → M)` -/ def fun_congr_left (e : m ≃ n) : (n → M) ≃ₗ[R] (m → M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n → M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n → M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (e₂ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ e₂) = linear_equiv.trans (fun_congr_left R M e₂) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end fun_left universe i variables [semiring R] [add_comm_monoid M] [semimodule R M] variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, 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 : (M ≃ₗ[R] M) → (M →ₗ[R] M)) := { map_one := rfl, map_mul := λ f g, rfl } /-- The group of invertible linear maps from `M` to itself -/ @[reducible] def general_linear_group := units (M →ₗ[R] M) namespace general_linear_group variables {R M} instance : has_coe_to_fun (general_linear_group R M) := by apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { 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 } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := f.symm, val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl }, map_mul' := λ x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : (general_linear_equiv R M f : M →ₗ[R] M) = f := by {ext, refl} end general_linear_group end linear_map namespace submodule variables [ring R] [add_comm_group M] [module R M] instance : is_modular_lattice (submodule R M) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw [← add_sub_cancel c b, add_comm], apply z.sub_mem haz (xz hb), end⟩ end submodule
61ad50b9a7f228d98265d594ef2fd569b90ce028
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/hash_map.lean
e51219833c6c81b6fac1e1249b2e3bf13a755f9c
[ "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
29,534
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, Mario Carneiro -/ import data.array.lemmas import data.list.join import data.list.range import data.pnat.defs /-! # Hash maps Defines a hash map data structure, representing a finite key-value map with a value type that may depend on the key type. The structure requires a `nat`-valued hash function to associate keys to buckets. ## Main definitions * `hash_map`: constructed with `mk_hash_map`. ## Implementation details A hash map with key type `α` and (dependent) value type `β : α → Type*` consists of an array of *buckets*, which are lists containing key/value pairs for that bucket. The hash function is taken modulo `n` to assign keys to their respective bucket. Because of this, some care should be put into the hash function to ensure it evenly distributes keys. The bucket array is an `array`. These have special VM support for in-place modification if there is only ever one reference to them. If one takes special care to never keep references to old versions of a hash map alive after updating it, then the hash map will be modified in-place. In this documentation, when we say a hash map is modified in-place, we are assuming the API is being used in this manner. When inserting (`hash_map.insert`), if the number of stored pairs (the *size*) is going to exceed the number of buckets, then a new hash map is first created with double the number of buckets and everything in the old hash map is reinserted along with the new key/value pair. Otherwise, the bucket array is modified in-place. The amortized running time of inserting $$n$$ elements into a hash map is $$O(n)$$. When removing (`hash_map.erase`), the hash map is modified in-place. The implementation does not reduce the number of buckets in the hash map if the size gets too low. ## Tags hash map -/ universes u v w /-- `bucket_array α β` is the underlying data type for `hash_map α β`, an array of linked lists of key-value pairs. -/ def bucket_array (α : Type u) (β : α → Type v) (n : ℕ+) := array n (list Σ a, β a) /-- Make a hash_map index from a `nat` hash value and a (positive) buffer size -/ def hash_map.mk_idx (n : ℕ+) (i : nat) : fin n := ⟨i % n, nat.mod_lt _ n.2⟩ namespace bucket_array section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) variables {n : ℕ+} (data : bucket_array α β n) instance : inhabited (bucket_array α β n) := ⟨mk_array _ []⟩ /-- Read the bucket corresponding to an element -/ def read (a : α) : list Σ a, β a := let bidx := hash_map.mk_idx n (hash_fn a) in data.read bidx /-- Write the bucket corresponding to an element -/ def write (a : α) (l : list Σ a, β a) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in data.write bidx l /-- Modify (read, apply `f`, and write) the bucket corresponding to an element -/ def modify (a : α) (f : list (Σ a, β a) → list (Σ a, β a)) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in array.write data bidx (f (array.read data bidx)) /-- The list of all key-value pairs in the bucket list -/ def as_list : list Σ a, β a := data.to_list.join theorem mem_as_list {a : Σ a, β a} : a ∈ data.as_list ↔ ∃i, a ∈ array.read data i := have (∃ (l : list (Σ (a : α), β a)) (i : fin (n.val)), a ∈ l ∧ array.read data i = l) ↔ ∃ (i : fin (n.val)), a ∈ array.read data i, by rw exists_swap; exact exists_congr (λ i, by simp), by simp [as_list]; simpa [array.mem.def, and_comm] /-- Fold a function `f` over the key-value pairs in the bucket list -/ def foldl {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : δ := data.foldl d (λ b d, b.foldl (λ r a, f r a.1 a.2) d) theorem foldl_eq {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : data.foldl d f = data.as_list.foldl (λ r a, f r a.1 a.2) d := by rw [foldl, as_list, list.foldl_join, ← array.to_list_foldl] end end bucket_array namespace hash_map section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) /-- Insert the pair `⟨a, b⟩` into the correct location in the bucket array (without checking for duplication) -/ def reinsert_aux {n} (data : bucket_array α β n) (a : α) (b : β a) : bucket_array α β n := data.modify hash_fn a (λl, ⟨a, b⟩ :: l) theorem mk_as_list (n : ℕ+) : bucket_array.as_list (mk_array n [] : bucket_array α β n) = [] := list.eq_nil_iff_forall_not_mem.mpr $ λ x m, let ⟨i, h⟩ := (bucket_array.mem_as_list _).1 m in h parameter [decidable_eq α] /-- Search a bucket for a key `a` and return the value -/ def find_aux (a : α) : list (Σ a, β a) → option (β a) | [] := none | (⟨a',b⟩::t) := if h : a' = a then some (eq.rec_on h b) else find_aux t theorem find_aux_iff {a : α} {b : β a} : Π {l : list Σ a, β a}, (l.map sigma.fst).nodup → (find_aux a l = some b ↔ sigma.mk a b ∈ l) | [] nd := ⟨λn, by injection n, false.elim⟩ | (⟨a',b'⟩::t) nd := begin by_cases a' = a, { clear find_aux_iff, subst h, suffices : b' = b ↔ b' = b ∨ sigma.mk a' b ∈ t, {simpa [find_aux, eq_comm]}, refine (or_iff_left_of_imp (λ m, _)).symm, have : a' ∉ t.map sigma.fst, from nd.not_mem, exact this.elim (list.mem_map_of_mem sigma.fst m) }, { have : sigma.mk a b ≠ ⟨a', b'⟩, { intro e, injection e with e, exact h e.symm }, simp at nd, simp [find_aux, h, ne.symm h, find_aux_iff, nd] } end /-- Returns `tt` if the bucket `l` contains the key `a` -/ def contains_aux (a : α) (l : list Σ a, β a) : bool := (find_aux a l).is_some theorem contains_aux_iff {a : α} {l : list Σ a, β a} (nd : (l.map sigma.fst).nodup) : contains_aux a l ↔ a ∈ l.map sigma.fst := begin unfold contains_aux, cases h : find_aux a l with b; simp, { assume (b : β a) (m : sigma.mk a b ∈ l), rw (find_aux_iff nd).2 m at h, contradiction }, { show ∃ (b : β a), sigma.mk a b ∈ l, exact ⟨_, (find_aux_iff nd).1 h⟩ }, end /-- Modify a bucket to replace a value in the list. Leaves the list unchanged if the key is not found. -/ def replace_aux (a : α) (b : β a) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then ⟨a, b⟩::t else ⟨a', b'⟩ :: replace_aux t /-- Modify a bucket to remove a key, if it exists. -/ def erase_aux (a : α) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then t else ⟨a', b'⟩ :: erase_aux t /-- The predicate `valid bkts sz` means that `bkts` satisfies the `hash_map` invariants: There are exactly `sz` elements in it, every pair is in the bucket determined by its key and the hash function, and no key appears multiple times in the list. -/ structure valid {n} (bkts : bucket_array α β n) (sz : nat) : Prop := (len : bkts.as_list.length = sz) (idx : ∀ {i} {a : Σ a, β a}, a ∈ array.read bkts i → mk_idx n (hash_fn a.1) = i) (nodup : ∀i, ((array.read bkts i).map sigma.fst).nodup) theorem valid.idx_enum {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : ∃ h, mk_idx n (hash_fn a) = ⟨i, h⟩ := (array.mem_to_list_enum.mp he).imp (λ h e, by subst e; exact v.idx hl) theorem valid.idx_enum_1 {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : (mk_idx n (hash_fn a)).1 = i := let ⟨h, e⟩ := v.idx_enum _ he hl in by rw e; refl theorem valid.as_list_nodup {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) : (bkts.as_list.map sigma.fst).nodup := begin suffices : (bkts.to_list.map (list.map sigma.fst)).pairwise list.disjoint, { suffices : ∀ l, array.mem l bkts → (l.map sigma.fst).nodup, by simpa [bucket_array.as_list, list.nodup_join, *], rintros l ⟨i, rfl⟩, apply v.nodup }, rw [← list.enum_map_snd bkts.to_list, list.pairwise_map, list.pairwise_map], have : (bkts.to_list.enum.map prod.fst).nodup := by simp [list.nodup_range], refine list.pairwise.imp_of_mem _ ((list.pairwise_map _).1 this), rw prod.forall, intros i l₁, rw prod.forall, intros j l₂ me₁ me₂ ij, simp [list.disjoint], intros a b ml₁ b' ml₂, apply ij, rwa [← v.idx_enum_1 _ me₁ ml₁, ← v.idx_enum_1 _ me₂ ml₂] end theorem mk_valid (n : ℕ+) : @valid n (mk_array n []) 0 := ⟨by simp [mk_as_list], λ i a h, by cases h, λ i, list.nodup_nil⟩ theorem valid.find_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {a : α} {b : β a} : find_aux a (bkts.read hash_fn a) = some b ↔ sigma.mk a b ∈ bkts.as_list := (find_aux_iff (v.nodup _)).trans $ by rw bkts.mem_as_list; exact ⟨λ h, ⟨_, h⟩, λ ⟨i, h⟩, (v.idx h).symm ▸ h⟩ theorem valid.contains_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) (a : α) : contains_aux a (bkts.read hash_fn a) ↔ a ∈ bkts.as_list.map sigma.fst := by simp [contains_aux, option.is_some_iff_exists, v.find_aux_iff hash_fn] section parameters {n : ℕ+} {bkts : bucket_array α β n} {bidx : fin n} {f : list (Σ a, β a) → list (Σ a, β a)} (u v1 v2 w : list Σ a, β a) local notation `L` := array.read bkts bidx private def bkts' : bucket_array α β n := array.write bkts bidx (f L) variables (hl : L = u ++ v1 ++ w) (hfl : f L = u ++ v2 ++ w) include hl hfl theorem append_of_modify : ∃ u' w', bkts.as_list = u' ++ v1 ++ w' ∧ bkts'.as_list = u' ++ v2 ++ w' := begin unfold bucket_array.as_list, have h : (bidx : ℕ) < bkts.to_list.length, { simp only [bidx.is_lt, array.to_list_length] }, refine ⟨(bkts.to_list.take bidx).join ++ u, w ++ (bkts.to_list.drop (bidx+1)).join, _, _⟩, { conv { to_lhs, rw [← list.take_append_drop bidx bkts.to_list, list.drop_eq_nth_le_cons h], simp [hl] }, simp }, { conv { to_lhs, rw [bkts', array.write_to_list, list.update_nth_eq_take_cons_drop _ h], simp [hfl] }, simp } end variables (hvnd : (v2.map sigma.fst).nodup) (hal : ∀ (a : Σ a, β a), a ∈ v2 → mk_idx n (hash_fn a.1) = bidx) (djuv : (u.map sigma.fst).disjoint (v2.map sigma.fst)) (djwv : (w.map sigma.fst).disjoint (v2.map sigma.fst)) include hvnd hal djuv djwv theorem valid.modify {sz : ℕ} (v : valid bkts sz) : v1.length ≤ sz + v2.length ∧ valid bkts' (sz + v2.length - v1.length) := begin rcases append_of_modify u v1 v2 w hl hfl with ⟨u', w', e₁, e₂⟩, rw [← v.len, e₁], suffices : valid bkts' (u' ++ v2 ++ w').length, { simpa [ge, add_comm, add_left_comm, nat.le_add_right, add_tsub_cancel_left] }, refine ⟨congr_arg _ e₂, λ i a, _, λ i, _⟩, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.idx _ _ _ v bidx a, simp only [hl, list.mem_append, or_imp_distrib] at this ⊢, exact ⟨⟨this.1.1, hal _⟩, this.2⟩ }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.idx } }, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.nodup _ _ _ v bidx, simp [hl, list.nodup_append] at this, simp [list.nodup_append, this, hvnd, djuv, djwv.symm] }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.nodup } } end end theorem valid.replace_aux (a : α) (b : β a) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b', l = u ++ [⟨a, b'⟩] ++ w ∧ replace_aux a b l = u ++ [⟨a, b⟩] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', suffices : ∃ (u w : list Σ a, β a) (b'' : β a), (sigma.mk a b') :: t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b (⟨a, b'⟩ :: t) = u ++ ⟨a, b⟩ :: w, {simpa}, refine ⟨[], t, b', _⟩, simp [replace_aux] }, { suffices : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), (sigma.mk a' b') :: t = u ++ ⟨a, b''⟩ :: w ∧ (sigma.mk a' b') :: (replace_aux a b t) = u ++ ⟨a, b⟩ :: w, { simpa [replace_aux, ne.symm e, e] }, intros x m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b t = u ++ ⟨a, b⟩ :: w, { simpa using valid.replace_aux t }, rcases IH x m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm, ne.symm e]⟩ } end theorem valid.replace {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (replace_aux a b)) sz := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b', hl, hfl⟩, simp [hl, list.nodup_append] at nd, refine (v.modify hash_fn u [⟨a, b'⟩] [⟨a, b⟩] w hl hfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa' e1 e2, _) (λa' e1 e2, _)).2; { revert e1, simp [-sigma.exists] at e2, subst a', simp [nd] } end theorem valid.insert {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hnc : ¬ contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (reinsert_aux bkts a b) (sz+1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), refine (v.modify hash_fn [] [] [⟨a, b⟩] (bkts.read hash_fn a) rfl rfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa', false.elim) (λa' e1 e2, _)).2, simp [-sigma.exists] at e2, subst a', exact Hnc ((contains_aux_iff nd).2 e1) end theorem valid.erase_aux (a : α) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b, l = u ++ [⟨a, b⟩] ++ w ∧ erase_aux a l = u ++ [] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', simpa [erase_aux, and_comm] using show ∃ u w (x : β a), t = u ++ w ∧ (sigma.mk a b') :: t = u ++ ⟨a, x⟩ :: w, from ⟨[], t, b', by simp⟩ }, { simp [erase_aux, e, ne.symm e], suffices : ∀ (b : β a) (_ : sigma.mk a b ∈ t), ∃ u w (x : β a), (sigma.mk a' b') :: t = u ++ ⟨a, x⟩ :: w ∧ (sigma.mk a' b') :: (erase_aux a t) = u ++ w, { simpa [replace_aux, ne.symm e, e] }, intros b m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (x : β a), t = u ++ ⟨a, x⟩ :: w ∧ erase_aux a t = u ++ w, { simpa using valid.erase_aux t }, rcases IH b m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm]⟩ } end theorem valid.erase {n} {bkts : bucket_array α β n} {sz} (a : α) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (erase_aux a)) (sz-1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.erase_aux a (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b, hl, hfl⟩, refine (v.modify hash_fn u [⟨a, b⟩] [] w hl hfl list.nodup_nil _ _ _).2; simp end end end hash_map /-- A hash map data structure, representing a finite key-value map with key type `α` and value type `β` (which may depend on `α`). -/ structure hash_map (α : Type u) [decidable_eq α] (β : α → Type v) := (hash_fn : α → nat) (size : ℕ) (nbuckets : ℕ+) (buckets : bucket_array α β nbuckets) (is_valid : hash_map.valid hash_fn buckets size) /-- Construct an empty hash map with buffer size `nbuckets` (default 8). -/ def mk_hash_map {α : Type u} [decidable_eq α] {β : α → Type v} (hash_fn : α → nat) (nbuckets := 8) : hash_map α β := let n := if nbuckets = 0 then 8 else nbuckets in let nz : n > 0 := by abstract { cases nbuckets; simp [if_pos, nat.succ_ne_zero] } in { hash_fn := hash_fn, size := 0, nbuckets := ⟨n, nz⟩, buckets := mk_array n [], is_valid := hash_map.mk_valid _ _ } namespace hash_map variables {α : Type u} {β : α → Type v} [decidable_eq α] /-- Return the value corresponding to a key, or `none` if not found -/ def find (m : hash_map α β) (a : α) : option (β a) := find_aux a (m.buckets.read m.hash_fn a) /-- Return `tt` if the key exists in the map -/ def contains (m : hash_map α β) (a : α) : bool := (m.find a).is_some instance : has_mem α (hash_map α β) := ⟨λa m, m.contains a⟩ /-- Fold a function over the key-value pairs in the map -/ def fold {δ : Type w} (m : hash_map α β) (d : δ) (f : δ → Π a, β a → δ) : δ := m.buckets.foldl d f /-- The list of key-value pairs in the map -/ def entries (m : hash_map α β) : list Σ a, β a := m.buckets.as_list /-- The list of keys in the map -/ def keys (m : hash_map α β) : list α := m.entries.map sigma.fst theorem find_iff (m : hash_map α β) (a : α) (b : β a) : m.find a = some b ↔ sigma.mk a b ∈ m.entries := m.is_valid.find_aux_iff _ theorem contains_iff (m : hash_map α β) (a : α) : m.contains a ↔ a ∈ m.keys := m.is_valid.contains_aux_iff _ _ theorem entries_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).entries = [] := mk_as_list _ theorem keys_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).keys = [] := by dsimp [keys]; rw entries_empty; refl theorem find_empty (hash_fn : α → nat) (n a) : (@mk_hash_map α _ β hash_fn n).find a = none := by induction h : (@mk_hash_map α _ β hash_fn n).find a; [refl, { have := (find_iff _ _ _).1 h, rw entries_empty at this, contradiction }] theorem not_contains_empty (hash_fn : α → nat) (n a) : ¬ (@mk_hash_map α _ β hash_fn n).contains a := by apply bool_iff_false.2; dsimp [contains]; rw [find_empty]; refl theorem insert_lemma (hash_fn : α → nat) {n n'} {bkts : bucket_array α β n} {sz} (v : valid hash_fn bkts sz) : valid hash_fn (bkts.foldl (mk_array _ [] : bucket_array α β n') (reinsert_aux hash_fn)) sz := begin suffices : ∀ (l : list Σ a, β a) (t : bucket_array α β n') sz, valid hash_fn t sz → ((l ++ t.as_list).map sigma.fst).nodup → valid hash_fn (l.foldl (λr (a : Σ a, β a), reinsert_aux hash_fn r a.1 a.2) t) (sz + l.length), { have p := this bkts.as_list _ _ (mk_valid _ _), rw [mk_as_list, list.append_nil, zero_add, v.len] at p, rw bucket_array.foldl_eq, exact p (v.as_list_nodup _) }, intro l, induction l with c l IH; intros t sz v nd, {exact v}, rw show sz + (c :: l).length = sz + 1 + l.length, by simp [add_comm, add_assoc], rcases (show (l.map sigma.fst).nodup ∧ ((bucket_array.as_list t).map sigma.fst).nodup ∧ c.fst ∉ l.map sigma.fst ∧ c.fst ∉ (bucket_array.as_list t).map sigma.fst ∧ (l.map sigma.fst).disjoint ((bucket_array.as_list t).map sigma.fst), by simpa [list.nodup_append, not_or_distrib, and_comm, and.left_comm] using nd) with ⟨nd1, nd2, nm1, nm2, dj⟩, have v' := v.insert _ _ c.2 (λHc, nm2 $ (v.contains_aux_iff _ c.1).1 Hc), apply IH _ _ v', suffices : ∀ ⦃a : α⦄ (b : β a), sigma.mk a b ∈ l → ∀ (b' : β a), sigma.mk a b' ∈ (reinsert_aux hash_fn t c.1 c.2).as_list → false, { simpa [list.nodup_append, nd1, v'.as_list_nodup _, list.disjoint] }, intros a b m1 b' m2, rcases (reinsert_aux hash_fn t c.1 c.2).mem_as_list.1 m2 with ⟨i, im⟩, have : sigma.mk a b' ∉ array.read t i, { intro m3, have : a ∈ list.map sigma.fst t.as_list := list.mem_map_of_mem sigma.fst (t.mem_as_list.2 ⟨_, m3⟩), exact dj (list.mem_map_of_mem sigma.fst m1) this }, by_cases h : mk_idx n' (hash_fn c.1) = i, { subst h, have e : sigma.mk a b' = ⟨c.1, c.2⟩, { simpa [reinsert_aux, bucket_array.modify, array.read_write, this] using im }, injection e with e, subst a, exact nm1.elim (@list.mem_map_of_mem _ _ sigma.fst _ _ m1) }, { apply this, simpa [reinsert_aux, bucket_array.modify, array.read_write_of_ne _ _ h] using im } end /-- Insert a key-value pair into the map. (Modifies `m` in-place when applicable) -/ def insert : Π (m : hash_map α β) (a : α) (b : β a), hash_map α β | ⟨hash_fn, size, n, buckets, v⟩ a b := let bkt := buckets.read hash_fn a in if hc : contains_aux a bkt then { hash_fn := hash_fn, size := size, nbuckets := n, buckets := buckets.modify hash_fn a (replace_aux a b), is_valid := v.replace _ a b hc } else let size' := size + 1, buckets' := buckets.modify hash_fn a (λl, ⟨a, b⟩::l), valid' := v.insert _ a b hc in if size' ≤ n then { hash_fn := hash_fn, size := size', nbuckets := n, buckets := buckets', is_valid := valid' } else let n' : ℕ+ := ⟨n * 2, mul_pos n.2 dec_trivial⟩, buckets'' : bucket_array α β n' := buckets'.foldl (mk_array _ []) (reinsert_aux hash_fn) in { hash_fn := hash_fn, size := size', nbuckets := n', buckets := buckets'', is_valid := insert_lemma _ valid' } theorem mem_insert : Π (m : hash_map α β) (a b a' b'), (sigma.mk a' b' : sigma β) ∈ (m.insert a b).entries ↔ if a = a' then b == b' else sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a b a' b' := begin let bkt := bkts.read hash_fn a, have nd : (bkt.map sigma.fst).nodup := v.nodup (mk_idx n (hash_fn a)), have lem : Π (bkts' : bucket_array α β n) (v1 u w) (hl : bucket_array.as_list bkts = u ++ v1 ++ w) (hfl : bucket_array.as_list bkts' = u ++ [⟨a, b⟩] ++ w) (veq : (v1 = [] ∧ ¬ contains_aux a bkt) ∨ ∃b'', v1 = [⟨a, b''⟩]), sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list, { intros bkts' v1 u w hl hfl veq, rw [hl, hfl], by_cases h : a = a', { subst a', suffices : b = b' ∨ sigma.mk a b' ∈ u ∨ sigma.mk a b' ∈ w ↔ b = b', { simpa [eq_comm, or.left_comm] }, refine or_iff_left_of_imp (not.elim $ not_or_distrib.2 _), rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩, { have na := (not_iff_not_of_iff $ v.contains_aux_iff _ _).1 Hnc, simp [hl, not_or_distrib] at na, simp [na] }, { have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] } }, { suffices : sigma.mk a' b' ∉ v1, {simp [h, ne.symm h, this]}, rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩; simp [ne.symm h] } }, by_cases Hc : (contains_aux a bkt : Prop), { rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u', w', b'', hl', hfl'⟩, rcases (append_of_modify u' [⟨a, b''⟩] [⟨a, b⟩] w' hl' hfl') with ⟨u, w, hl, hfl⟩, simpa [insert, @dif_pos (contains_aux a bkt) _ Hc] using lem _ _ u w hl hfl (or.inr ⟨b'', rfl⟩) }, { let size' := size + 1, let bkts' := bkts.modify hash_fn a (λl, ⟨a, b⟩::l), have mi : sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list := let ⟨u, w, hl, hfl⟩ := append_of_modify [] [] [⟨a, b⟩] _ rfl rfl in lem bkts' _ u w hl hfl $ or.inl ⟨rfl, Hc⟩, simp [insert, @dif_neg (contains_aux a bkt) _ Hc], by_cases h : size' ≤ n, { simpa [show size' ≤ n, from h] using mi }, { let n' : ℕ+ := ⟨n * 2, mul_pos n.2 dec_trivial⟩, let bkts'' : bucket_array α β n' := bkts'.foldl (mk_array _ []) (reinsert_aux hash_fn), suffices : sigma.mk a' b' ∈ bkts''.as_list ↔ sigma.mk a' b' ∈ bkts'.as_list.reverse, { simpa [show ¬ size' ≤ n, from h, mi] }, rw [show bkts'' = bkts'.as_list.foldl _ _, from bkts'.foldl_eq _ _, ← list.foldr_reverse], induction bkts'.as_list.reverse with a l IH, { simp [mk_as_list] }, { cases a with a'' b'', let B := l.foldr (λ (y : sigma β) (x : bucket_array α β n'), reinsert_aux hash_fn x y.1 y.2) (mk_array n' []), rcases append_of_modify [] [] [⟨a'', b''⟩] _ rfl rfl with ⟨u, w, hl, hfl⟩, simp [IH.symm, or.left_comm, show B.as_list = _, from hl, show (reinsert_aux hash_fn B a'' b'').as_list = _, from hfl] } } } end theorem find_insert_eq (m : hash_map α β) (a : α) (b : β a) : (m.insert a b).find a = some b := (find_iff (m.insert a b) a b).2 $ (mem_insert m a b a b).2 $ by rw if_pos rfl theorem find_insert_ne (m : hash_map α β) (a a' : α) (b : β a) (h : a ≠ a') : (m.insert a b).find a' = m.find a' := option.eq_of_eq_some $ λb', let t := mem_insert m a b a' b' in (find_iff _ _ _).trans $ iff.trans (by rwa if_neg h at t) (find_iff _ _ _).symm theorem find_insert (m : hash_map α β) (a' a : α) (b : β a) : (m.insert a b).find a' = if h : a = a' then some (eq.rec_on h b) else m.find a' := if h : a = a' then by rw dif_pos h; exact match a', h with ._, rfl := find_insert_eq m a b end else by rw dif_neg h; exact find_insert_ne m a a' b h /-- Insert a list of key-value pairs into the map. (Modifies `m` in-place when applicable) -/ def insert_all (l : list (Σ a, β a)) (m : hash_map α β) : hash_map α β := l.foldl (λ m ⟨a, b⟩, insert m a b) m /-- Construct a hash map from a list of key-value pairs. -/ def of_list (l : list (Σ a, β a)) (hash_fn) : hash_map α β := insert_all l (mk_hash_map hash_fn (2 * l.length)) /-- Remove a key from the map. (Modifies `m` in-place when applicable) -/ def erase (m : hash_map α β) (a : α) : hash_map α β := match m with ⟨hash_fn, size, n, buckets, v⟩ := if hc : contains_aux a (buckets.read hash_fn a) then { hash_fn := hash_fn, size := size - 1, nbuckets := n, buckets := buckets.modify hash_fn a (erase_aux a), is_valid := v.erase _ a hc } else m end theorem mem_erase : Π (m : hash_map α β) (a a' b'), (sigma.mk a' b' : sigma β) ∈ (m.erase a).entries ↔ a ≠ a' ∧ sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a a' b' := begin let bkt := bkts.read hash_fn a, by_cases Hc : (contains_aux a bkt : Prop), { let bkts' := bkts.modify hash_fn a (erase_aux a), suffices : sigma.mk a' b' ∈ bkts'.as_list ↔ a ≠ a' ∧ sigma.mk a' b' ∈ bkts.as_list, { simpa [erase, @dif_pos (contains_aux a bkt) _ Hc] }, have nd := v.nodup (mk_idx n (hash_fn a)), rcases valid.erase_aux a bkt ((contains_aux_iff nd).1 Hc) with ⟨u', w', b, hl', hfl'⟩, rcases append_of_modify u' [⟨a, b⟩] [] _ hl' hfl' with ⟨u, w, hl, hfl⟩, suffices : ∀_:sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w, a ≠ a', { have : sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w ↔ (¬a = a' ∧ a' = a) ∧ b' == b ∨ ¬a = a' ∧ (sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w), { simp [eq_comm, not_and_self_iff, and_iff_right_of_imp this] }, simpa [hl, show bkts'.as_list = _, from hfl, and_or_distrib_left, and_comm, and.left_comm, or.left_comm] }, rintro m rfl, revert m, apply not_or_distrib.2, have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] }, { suffices : ∀_:sigma.mk a' b' ∈ bucket_array.as_list bkts, a ≠ a', { simp [erase, @dif_neg (contains_aux a bkt) _ Hc, entries, and_iff_right_of_imp this] }, rintro m rfl, exact Hc ((v.contains_aux_iff _ _).2 (list.mem_map_of_mem sigma.fst m)) } end theorem find_erase_eq (m : hash_map α β) (a : α) : (m.erase a).find a = none := begin cases h : (m.erase a).find a with b, {refl}, exact absurd rfl ((mem_erase m a a b).1 ((find_iff (m.erase a) a b).1 h)).left end theorem find_erase_ne (m : hash_map α β) (a a' : α) (h : a ≠ a') : (m.erase a).find a' = m.find a' := option.eq_of_eq_some $ λb', (find_iff _ _ _).trans $ (mem_erase m a a' b').trans $ (and_iff_right h).trans (find_iff _ _ _).symm theorem find_erase (m : hash_map α β) (a' a : α) : (m.erase a).find a' = if a = a' then none else m.find a' := if h : a = a' then by subst a'; simp [find_erase_eq m a] else by rw if_neg h; exact find_erase_ne m a a' h section string variables [has_to_string α] [∀ a, has_to_string (β a)] open prod private def key_data_to_string (a : α) (b : β a) (first : bool) : string := (if first then "" else ", ") ++ sformat!"{a} ← {b}" private def to_string (m : hash_map α β) : string := "⟨" ++ (fst (fold m ("", tt) (λ p a b, (fst p ++ key_data_to_string a b (snd p), ff)))) ++ "⟩" instance : has_to_string (hash_map α β) := ⟨to_string⟩ end string section format open format prod variables [has_to_format α] [∀ a, has_to_format (β a)] private meta def format_key_data (a : α) (b : β a) (first : bool) : format := (if first then to_fmt "" else to_fmt "," ++ line) ++ to_fmt a ++ space ++ to_fmt "←" ++ space ++ to_fmt b private meta def to_format (m : hash_map α β) : format := group $ to_fmt "⟨" ++ nest 1 (fst (fold m (to_fmt "", tt) (λ p a b, (fst p ++ format_key_data a b (snd p), ff)))) ++ to_fmt "⟩" meta instance : has_to_format (hash_map α β) := ⟨to_format⟩ end format /-- `hash_map` with key type `nat` and value type that may vary. -/ instance {β : ℕ → Type*} : inhabited (hash_map ℕ β) := ⟨mk_hash_map id⟩ end hash_map
23dee1f43a38372ae9231fb1f6fa55fa185cce1a
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/free_module/pid.lean
af76def8e465faa8515de38fad57f9e76343367e
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
29,103
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 linear_algebra.dimension import linear_algebra.free_module.basic import ring_theory.principal_ideal_domain import ring_theory.finiteness /-! # Free modules over PID > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A free `R`-module `M` is a module with a basis over `R`, equivalently it is an `R`-module linearly equivalent to `ι →₀ R` for some `ι`. This file proves a submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain (PID), i.e. we have instances `[is_domain R] [is_principal_ideal_ring R]`. We express "free `R`-module of finite rank" as a module `M` which has a basis `b : ι → R`, where `ι` is a `fintype`. We call the cardinality of `ι` the rank of `M` in this file; it would be equal to `finrank R M` if `R` is a field and `M` is a vector space. ## Main results In this section, `M` is a free and finitely generated `R`-module, and `N` is a submodule of `M`. - `submodule.induction_on_rank`: if `P` holds for `⊥ : submodule R M` and if `P N` follows from `P N'` for all `N'` that are of lower rank, then `P` holds on all submodules - `submodule.exists_basis_of_pid`: if `R` is a PID, then `N : submodule R M` is free and finitely generated. This is the first part of the structure theorem for modules. - `submodule.smith_normal_form`: if `R` is a PID, then `M` has a basis `bM` and `N` has a basis `bN` such that `bN i = a i • bM i`. Equivalently, a linear map `f : M →ₗ M` with `range f = N` can be written as a matrix in Smith normal form, a diagonal matrix with the coefficients `a i` along the diagonal. ## Tags free module, finitely generated module, rank, structure theorem -/ open_locale big_operators universes u v section ring variables {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M] variables {ι : Type*} (b : basis ι R M) open submodule.is_principal submodule lemma eq_bot_of_generator_maximal_map_eq_zero (b : basis ι R M) {N : submodule R M} {ϕ : M →ₗ[R] R} (hϕ : ∀ (ψ : M →ₗ[R] R), ¬ N.map ϕ < N.map ψ) [(N.map ϕ).is_principal] (hgen : generator (N.map ϕ) = (0 : R)) : N = ⊥ := begin rw submodule.eq_bot_iff, intros x hx, refine b.ext_elem (λ i, _), rw (eq_bot_iff_generator_eq_zero _).mpr hgen at hϕ, rw [linear_equiv.map_zero, finsupp.zero_apply], exact (submodule.eq_bot_iff _).mp (not_bot_lt_iff.1 $ hϕ ((finsupp.lapply i) ∘ₗ ↑b.repr)) _ ⟨x, hx, rfl⟩ end lemma eq_bot_of_generator_maximal_submodule_image_eq_zero {N O : submodule R M} (b : basis ι R O) (hNO : N ≤ O) {ϕ : O →ₗ[R] R} (hϕ : ∀ (ψ : O →ₗ[R] R), ¬ ϕ.submodule_image N < ψ.submodule_image N) [(ϕ.submodule_image N).is_principal] (hgen : generator (ϕ.submodule_image N) = 0) : N = ⊥ := begin rw submodule.eq_bot_iff, intros x hx, refine congr_arg coe (show (⟨x, hNO hx⟩ : O) = 0, from b.ext_elem (λ i, _)), rw (eq_bot_iff_generator_eq_zero _).mpr hgen at hϕ, rw [linear_equiv.map_zero, finsupp.zero_apply], refine (submodule.eq_bot_iff _).mp (not_bot_lt_iff.1 $ hϕ ((finsupp.lapply i) ∘ₗ ↑b.repr)) _ _, exact (linear_map.mem_submodule_image_of_le hNO).mpr ⟨x, hx, rfl⟩ end end ring section is_domain variables {ι : Type*} {R : Type*} [comm_ring R] [is_domain R] variables {M : Type*} [add_comm_group M] [module R M] {b : ι → M} open submodule.is_principal set submodule lemma dvd_generator_iff {I : ideal R} [I.is_principal] {x : R} (hx : x ∈ I) : x ∣ generator I ↔ I = ideal.span {x} := begin conv_rhs { rw [← span_singleton_generator I] }, erw [ideal.span_singleton_eq_span_singleton, ← dvd_dvd_iff_associated, ← mem_iff_generator_dvd], exact ⟨λ h, ⟨hx, h⟩, λ h, h.2⟩ end end is_domain section principal_ideal_domain open submodule.is_principal set submodule variables {ι : Type*} {R : Type*} [comm_ring R] [is_domain R] [is_principal_ideal_ring R] variables {M : Type*} [add_comm_group M] [module R M] {b : ι → M} open submodule.is_principal lemma generator_maximal_submodule_image_dvd {N O : submodule R M} (hNO : N ≤ O) {ϕ : O →ₗ[R] R} (hϕ : ∀ (ψ : O →ₗ[R] R), ¬ ϕ.submodule_image N < ψ.submodule_image N) [(ϕ.submodule_image N).is_principal] (y : M) (yN : y ∈ N) (ϕy_eq : ϕ ⟨y, hNO yN⟩ = generator (ϕ.submodule_image N)) (ψ : O →ₗ[R] R) : generator (ϕ.submodule_image N) ∣ ψ ⟨y, hNO yN⟩ := begin let a : R := generator (ϕ.submodule_image N), let d : R := is_principal.generator (submodule.span R {a, ψ ⟨y, hNO yN⟩}), have d_dvd_left : d ∣ a := (mem_iff_generator_dvd _).mp (subset_span (mem_insert _ _)), have d_dvd_right : d ∣ ψ ⟨y, hNO yN⟩ := (mem_iff_generator_dvd _).mp (subset_span (mem_insert_of_mem _ (mem_singleton _))), refine dvd_trans _ d_dvd_right, rw [dvd_generator_iff, ideal.span, ← span_singleton_generator (submodule.span R {a, ψ ⟨y, hNO yN⟩})], obtain ⟨r₁, r₂, d_eq⟩ : ∃ r₁ r₂ : R, d = r₁ * a + r₂ * ψ ⟨y, hNO yN⟩, { obtain ⟨r₁, r₂', hr₂', hr₁⟩ := mem_span_insert.mp (is_principal.generator_mem (submodule.span R {a, ψ ⟨y, hNO yN⟩})), obtain ⟨r₂, rfl⟩ := mem_span_singleton.mp hr₂', exact ⟨r₁, r₂, hr₁⟩ }, let ψ' : O →ₗ[R] R := r₁ • ϕ + r₂ • ψ, have : span R {d} ≤ ψ'.submodule_image N, { rw [span_le, singleton_subset_iff, set_like.mem_coe, linear_map.mem_submodule_image_of_le hNO], refine ⟨y, yN, _⟩, change r₁ * ϕ ⟨y, hNO yN⟩ + r₂ * ψ ⟨y, hNO yN⟩ = d, rw [d_eq, ϕy_eq] }, refine le_antisymm (this.trans (le_of_eq _)) (ideal.span_singleton_le_span_singleton.mpr d_dvd_left), rw span_singleton_generator, apply (le_trans _ this).eq_of_not_gt (hϕ ψ'), rw [← span_singleton_generator (ϕ.submodule_image N)], exact ideal.span_singleton_le_span_singleton.mpr d_dvd_left, { exact subset_span (mem_insert _ _) } end /-- The induction hypothesis of `submodule.basis_of_pid` and `submodule.smith_normal_form`. Basically, it says: let `N ≤ M` be a pair of submodules, then we can find a pair of submodules `N' ≤ M'` of strictly smaller rank, whose basis we can extend to get a basis of `N` and `M`. Moreover, if the basis for `M'` is up to scalars a basis for `N'`, then the basis we find for `M` is up to scalars a basis for `N`. For `basis_of_pid` we only need the first half and can fix `M = ⊤`, for `smith_normal_form` we need the full statement, but must also feed in a basis for `M` using `basis_of_pid` to keep the induction going. -/ lemma submodule.basis_of_pid_aux [finite ι] {O : Type*} [add_comm_group O] [module R O] (M N : submodule R O) (b'M : basis ι R M) (N_bot : N ≠ ⊥) (N_le_M : N ≤ M) : ∃ (y ∈ M) (a : R) (hay : a • y ∈ N) (M' ≤ M) (N' ≤ N) (N'_le_M' : N' ≤ M') (y_ortho_M' : ∀ (c : R) (z : O), z ∈ M' → c • y + z = 0 → c = 0) (ay_ortho_N' : ∀ (c : R) (z : O), z ∈ N' → c • a • y + z = 0 → c = 0), ∀ (n') (bN' : basis (fin n') R N'), ∃ (bN : basis (fin (n' + 1)) R N), ∀ (m') (hn'm' : n' ≤ m') (bM' : basis (fin m') R M'), ∃ (hnm : (n' + 1) ≤ (m' + 1)) (bM : basis (fin (m' + 1)) R M), ∀ (as : fin n' → R) (h : ∀ (i : fin n'), (bN' i : O) = as i • (bM' (fin.cast_le hn'm' i) : O)), ∃ (as' : fin (n' + 1) → R), ∀ (i : fin (n' + 1)), (bN i : O) = as' i • (bM (fin.cast_le hnm i) : O) := begin -- Let `ϕ` be a maximal projection of `M` onto `R`, in the sense that there is -- no `ψ` whose image of `N` is larger than `ϕ`'s image of `N`. have : ∃ ϕ : M →ₗ[R] R, ∀ (ψ : M →ₗ[R] R), ¬ ϕ.submodule_image N < ψ.submodule_image N, { obtain ⟨P, P_eq, P_max⟩ := set_has_maximal_iff_noetherian.mpr (infer_instance : is_noetherian R R) _ (show (set.range (λ ψ : M →ₗ[R] R, ψ.submodule_image N)).nonempty, from ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩), obtain ⟨ϕ, rfl⟩ := set.mem_range.mp P_eq, exact ⟨ϕ, λ ψ hψ, P_max _ ⟨_, rfl⟩ hψ⟩ }, let ϕ := this.some, have ϕ_max := this.some_spec, -- Since `ϕ(N)` is a `R`-submodule of the PID `R`, -- it is principal and generated by some `a`. let a := generator (ϕ.submodule_image N), have a_mem : a ∈ ϕ.submodule_image N := generator_mem _, -- If `a` is zero, then the submodule is trivial. So let's assume `a ≠ 0`, `N ≠ ⊥`. by_cases a_zero : a = 0, { have := eq_bot_of_generator_maximal_submodule_image_eq_zero b'M N_le_M ϕ_max a_zero, contradiction }, -- We claim that `ϕ⁻¹ a = y` can be taken as basis element of `N`. obtain ⟨y, yN, ϕy_eq⟩ := (linear_map.mem_submodule_image_of_le N_le_M).mp a_mem, have ϕy_ne_zero : ϕ ⟨y, N_le_M yN⟩ ≠ 0 := λ h, a_zero (ϕy_eq.symm.trans h), -- Write `y` as `a • y'` for some `y'`. have hdvd : ∀ i, a ∣ b'M.coord i ⟨y, N_le_M yN⟩ := λ i, generator_maximal_submodule_image_dvd N_le_M ϕ_max y yN ϕy_eq (b'M.coord i), choose c hc using hdvd, casesI nonempty_fintype ι, let y' : O := ∑ i, c i • b'M i, have y'M : y' ∈ M := M.sum_mem (λ i _, M.smul_mem (c i) (b'M i).2), have mk_y' : (⟨y', y'M⟩ : M) = ∑ i, c i • b'M i := subtype.ext (show y' = M.subtype _, by { simp only [linear_map.map_sum, linear_map.map_smul], refl }), have a_smul_y' : a • y' = y, { refine congr_arg coe (show (a • ⟨y', y'M⟩ : M) = ⟨y, N_le_M yN⟩, from _), rw [← b'M.sum_repr ⟨y, N_le_M yN⟩, mk_y', finset.smul_sum], refine finset.sum_congr rfl (λ i _, _), rw [← mul_smul, ← hc], refl }, -- We found an `y` and an `a`! refine ⟨y', y'M, a, a_smul_y'.symm ▸ yN, _⟩, have ϕy'_eq : ϕ ⟨y', y'M⟩ = 1 := mul_left_cancel₀ a_zero (calc a • ϕ ⟨y', y'M⟩ = ϕ ⟨a • y', _⟩ : (ϕ.map_smul a ⟨y', y'M⟩).symm ... = ϕ ⟨y, N_le_M yN⟩ : by simp only [a_smul_y'] ... = a : ϕy_eq ... = a * 1 : (mul_one a).symm), have ϕy'_ne_zero : ϕ ⟨y', y'M⟩ ≠ 0 := by simpa only [ϕy'_eq] using one_ne_zero, -- `M' := ker (ϕ : M → R)` is smaller than `M` and `N' := ker (ϕ : N → R)` is smaller than `N`. let M' : submodule R O := ϕ.ker.map M.subtype, let N' : submodule R O := (ϕ.comp (of_le N_le_M)).ker.map N.subtype, have M'_le_M : M' ≤ M := M.map_subtype_le ϕ.ker, have N'_le_M' : N' ≤ M', { intros x hx, simp only [mem_map, linear_map.mem_ker] at hx ⊢, obtain ⟨⟨x, xN⟩, hx, rfl⟩ := hx, exact ⟨⟨x, N_le_M xN⟩, hx, rfl⟩ }, have N'_le_N : N' ≤ N := N.map_subtype_le (ϕ.comp (of_le N_le_M)).ker, -- So fill in those results as well. refine ⟨M', M'_le_M, N', N'_le_N, N'_le_M', _⟩, -- Note that `y'` is orthogonal to `M'`. have y'_ortho_M' : ∀ (c : R) z ∈ M', c • y' + z = 0 → c = 0, { intros c x xM' hc, obtain ⟨⟨x, xM⟩, hx', rfl⟩ := submodule.mem_map.mp xM', rw linear_map.mem_ker at hx', have hc' : (c • ⟨y', y'M⟩ + ⟨x, xM⟩ : M) = 0 := subtype.coe_injective hc, simpa only [linear_map.map_add, linear_map.map_zero, linear_map.map_smul, smul_eq_mul, add_zero, mul_eq_zero, ϕy'_ne_zero, hx', or_false] using congr_arg ϕ hc' }, -- And `a • y'` is orthogonal to `N'`. have ay'_ortho_N' : ∀ (c : R) z ∈ N', c • a • y' + z = 0 → c = 0, { intros c z zN' hc, refine (mul_eq_zero.mp (y'_ortho_M' (a * c) z (N'_le_M' zN') _)).resolve_left a_zero, rw [mul_comm, mul_smul, hc] }, -- So we can extend a basis for `N'` with `y` refine ⟨y'_ortho_M', ay'_ortho_N', λ n' bN', ⟨_, _⟩⟩, { refine basis.mk_fin_cons_of_le y yN bN' N'_le_N _ _, { intros c z zN' hc, refine ay'_ortho_N' c z zN' _, rwa ← a_smul_y' at hc }, { intros z zN, obtain ⟨b, hb⟩ : _ ∣ ϕ ⟨z, N_le_M zN⟩ := generator_submodule_image_dvd_of_mem N_le_M ϕ zN, refine ⟨-b, submodule.mem_map.mpr ⟨⟨_, N.sub_mem zN (N.smul_mem b yN)⟩, _, _⟩⟩, { refine linear_map.mem_ker.mpr (show ϕ (⟨z, N_le_M zN⟩ - b • ⟨y, N_le_M yN⟩) = 0, from _), rw [linear_map.map_sub, linear_map.map_smul, hb, ϕy_eq, smul_eq_mul, mul_comm, sub_self] }, { simp only [sub_eq_add_neg, neg_smul], refl } } }, -- And extend a basis for `M'` with `y'` intros m' hn'm' bM', refine ⟨nat.succ_le_succ hn'm', _, _⟩, { refine basis.mk_fin_cons_of_le y' y'M bM' M'_le_M y'_ortho_M' _, intros z zM, refine ⟨-ϕ ⟨z, zM⟩, ⟨⟨z, zM⟩ - (ϕ ⟨z, zM⟩) • ⟨y', y'M⟩, linear_map.mem_ker.mpr _, _⟩⟩, { rw [linear_map.map_sub, linear_map.map_smul, ϕy'_eq, smul_eq_mul, mul_one, sub_self] }, { rw [linear_map.map_sub, linear_map.map_smul, sub_eq_add_neg, neg_smul], refl } }, -- It remains to show the extended bases are compatible with each other. intros as h, refine ⟨fin.cons a as, _⟩, intro i, rw [basis.coe_mk_fin_cons_of_le, basis.coe_mk_fin_cons_of_le], refine fin.cases _ (λ i, _) i, { simp only [fin.cons_zero, fin.cast_le_zero], exact a_smul_y'.symm }, { rw fin.cast_le_succ, simp only [fin.cons_succ, coe_of_le, h i] } end /-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain. This is a `lemma` to make the induction a bit easier. To actually access the basis, see `submodule.basis_of_pid`. See also the stronger version `submodule.smith_normal_form`. -/ lemma submodule.nonempty_basis_of_pid {ι : Type*} [finite ι] (b : basis ι R M) (N : submodule R M) : ∃ (n : ℕ), nonempty (basis (fin n) R N) := begin haveI := classical.dec_eq M, casesI nonempty_fintype ι, refine N.induction_on_rank b _ _, intros N ih, let b' := (b.reindex (fintype.equiv_fin ι)).map (linear_equiv.of_top _ rfl).symm, by_cases N_bot : N = ⊥, { subst N_bot, exact ⟨0, ⟨basis.empty _⟩⟩ }, obtain ⟨y, -, a, hay, M', -, N', N'_le_N, -, -, ay_ortho, h'⟩ := submodule.basis_of_pid_aux ⊤ N b' N_bot le_top, obtain ⟨n', ⟨bN'⟩⟩ := ih N' N'_le_N _ hay ay_ortho, obtain ⟨bN, hbN⟩ := h' n' bN', exact ⟨n' + 1, ⟨bN⟩⟩ end /-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain. See also the stronger version `submodule.smith_normal_form`. -/ noncomputable def submodule.basis_of_pid {ι : Type*} [finite ι] (b : basis ι R M) (N : submodule R M) : Σ (n : ℕ), (basis (fin n) R N) := ⟨_, (N.nonempty_basis_of_pid b).some_spec.some⟩ lemma submodule.basis_of_pid_bot {ι : Type*} [finite ι] (b : basis ι R M) : submodule.basis_of_pid b ⊥ = ⟨0, basis.empty _⟩ := begin obtain ⟨n, b'⟩ := submodule.basis_of_pid b ⊥, let e : fin n ≃ fin 0 := b'.index_equiv (basis.empty _ : basis (fin 0) R (⊥ : submodule R M)), obtain rfl : n = 0 := by simpa using fintype.card_eq.mpr ⟨e⟩, exact sigma.eq rfl (basis.eq_of_apply_eq $ fin_zero_elim) end /-- A submodule inside a free `R`-submodule of finite rank is also a free `R`-module of finite rank, if `R` is a principal ideal domain. See also the stronger version `submodule.smith_normal_form_of_le`. -/ noncomputable def submodule.basis_of_pid_of_le {ι : Type*} [finite ι] {N O : submodule R M} (hNO : N ≤ O) (b : basis ι R O) : Σ (n : ℕ), basis (fin n) R N := let ⟨n, bN'⟩ := submodule.basis_of_pid b (N.comap O.subtype) in ⟨n, bN'.map (submodule.comap_subtype_equiv_of_le hNO)⟩ /-- A submodule inside the span of a linear independent family is a free `R`-module of finite rank, if `R` is a principal ideal domain. -/ noncomputable def submodule.basis_of_pid_of_le_span {ι : Type*} [finite ι] {b : ι → M} (hb : linear_independent R b) {N : submodule R M} (le : N ≤ submodule.span R (set.range b)) : Σ (n : ℕ), basis (fin n) R N := submodule.basis_of_pid_of_le le (basis.span hb) variable {M} /-- A finite type torsion free module over a PID admits a basis. -/ noncomputable def module.basis_of_finite_type_torsion_free [fintype ι] {s : ι → M} (hs : span R (range s) = ⊤) [no_zero_smul_divisors R M] : Σ (n : ℕ), basis (fin n) R M := begin classical, -- We define `N` as the submodule spanned by a maximal linear independent subfamily of `s` have := exists_maximal_independent R s, let I : set ι := this.some, obtain ⟨indepI : linear_independent R (s ∘ coe : I → M), hI : ∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I)⟩ := this.some_spec, let N := span R (range $ (s ∘ coe : I → M)), -- same as `span R (s '' I)` but more convenient let sI : I → N := λ i, ⟨s i.1, subset_span (mem_range_self i)⟩, -- `s` restricted to `I` let sI_basis : basis I R N, -- `s` restricted to `I` is a basis of `N` from basis.span indepI, -- Our first goal is to build `A ≠ 0` such that `A • M ⊆ N` have exists_a : ∀ i : ι, ∃ a : R, a ≠ 0 ∧ a • s i ∈ N, { intro i, by_cases hi : i ∈ I, { use [1, zero_ne_one.symm], rw one_smul, exact subset_span (mem_range_self (⟨i, hi⟩ : I)) }, { simpa [image_eq_range s I] using hI i hi } }, choose a ha ha' using exists_a, let A := ∏ i, a i, have hA : A ≠ 0, { rw finset.prod_ne_zero_iff, simpa using ha }, -- `M ≃ A • M` because `M` is torsion free and `A ≠ 0` let φ : M →ₗ[R] M := linear_map.lsmul R M A, have : φ.ker = ⊥, from linear_map.ker_lsmul hA, let ψ : M ≃ₗ[R] φ.range := linear_equiv.of_injective φ (linear_map.ker_eq_bot.mp this), have : φ.range ≤ N, -- as announced, `A • M ⊆ N` { suffices : ∀ i, φ (s i) ∈ N, { rw [linear_map.range_eq_map, ← hs, φ.map_span_le], rintros _ ⟨i, rfl⟩, apply this }, intro i, calc (∏ j, a j) • s i = (∏ j in {i}ᶜ, a j) • a i • s i : by rw [fintype.prod_eq_prod_compl_mul i, mul_smul] ... ∈ N : N.smul_mem _ (ha' i) }, -- Since a submodule of a free `R`-module is free, we get that `A • M` is free obtain ⟨n, b : basis (fin n) R φ.range⟩ := submodule.basis_of_pid_of_le this sI_basis, -- hence `M` is free. exact ⟨n, b.map ψ.symm⟩ end lemma module.free_of_finite_type_torsion_free [finite ι] {s : ι → M} (hs : span R (range s) = ⊤) [no_zero_smul_divisors R M] : module.free R M := begin casesI nonempty_fintype ι, obtain ⟨n, b⟩ : Σ n, basis (fin n) R M := module.basis_of_finite_type_torsion_free hs, exact module.free.of_basis b, end /-- A finite type torsion free module over a PID admits a basis. -/ noncomputable def module.basis_of_finite_type_torsion_free' [module.finite R M] [no_zero_smul_divisors R M] : Σ (n : ℕ), basis (fin n) R M := module.basis_of_finite_type_torsion_free module.finite.exists_fin.some_spec.some_spec lemma module.free_of_finite_type_torsion_free' [module.finite R M] [no_zero_smul_divisors R M] : module.free R M := begin obtain ⟨n, b⟩ : Σ n, basis (fin n) R M := module.basis_of_finite_type_torsion_free', exact module.free.of_basis b, end section smith_normal /-- A Smith normal form basis for a submodule `N` of a module `M` consists of bases for `M` and `N` such that the inclusion map `N → M` can be written as a (rectangular) matrix with `a` along the diagonal: in Smith normal form. -/ @[nolint has_nonempty_instance] structure basis.smith_normal_form (N : submodule R M) (ι : Type*) (n : ℕ) := (bM : basis ι R M) (bN : basis (fin n) R N) (f : fin n ↪ ι) (a : fin n → R) (snf : ∀ i, (bN i : M) = a i • bM (f i)) /-- If `M` is finite free over a PID `R`, then any submodule `N` is free and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix in Smith normal form. See `submodule.smith_normal_form_of_le` for a version of this theorem that returns a `basis.smith_normal_form`. This is a strengthening of `submodule.basis_of_pid_of_le`. -/ theorem submodule.exists_smith_normal_form_of_le [finite ι] (b : basis ι R M) (N O : submodule R M) (N_le_O : N ≤ O) : ∃ (n o : ℕ) (hno : n ≤ o) (bO : basis (fin o) R O) (bN : basis (fin n) R N) (a : fin n → R), ∀ i, (bN i : M) = a i • bO (fin.cast_le hno i) := begin casesI nonempty_fintype ι, revert N, refine induction_on_rank b _ _ O, intros M ih N N_le_M, obtain ⟨m, b'M⟩ := M.basis_of_pid b, by_cases N_bot : N = ⊥, { subst N_bot, exact ⟨0, m, nat.zero_le _, b'M, basis.empty _, fin_zero_elim, fin_zero_elim⟩ }, obtain ⟨y, hy, a, hay, M', M'_le_M, N', N'_le_N, N'_le_M', y_ortho, ay_ortho, h⟩ := submodule.basis_of_pid_aux M N b'M N_bot N_le_M, obtain ⟨n', m', hn'm', bM', bN', as', has'⟩ := ih M' M'_le_M y hy y_ortho N' N'_le_M', obtain ⟨bN, h'⟩ := h n' bN', obtain ⟨hmn, bM, h''⟩ := h' m' hn'm' bM', obtain ⟨as, has⟩ := h'' as' has', exact ⟨_, _, hmn, bM, bN, as, has⟩ end /-- If `M` is finite free over a PID `R`, then any submodule `N` is free and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix in Smith normal form. See `submodule.exists_smith_normal_form_of_le` for a version of this theorem that doesn't need to map `N` into a submodule of `O`. This is a strengthening of `submodule.basis_of_pid_of_le`. -/ noncomputable def submodule.smith_normal_form_of_le [finite ι] (b : basis ι R M) (N O : submodule R M) (N_le_O : N ≤ O) : Σ (o n : ℕ), basis.smith_normal_form (N.comap O.subtype) (fin o) n := begin choose n o hno bO bN a snf using N.exists_smith_normal_form_of_le b O N_le_O, refine ⟨o, n, bO, bN.map (comap_subtype_equiv_of_le N_le_O).symm, (fin.cast_le hno).to_embedding, a, λ i, _⟩, ext, simp only [snf, basis.map_apply, submodule.comap_subtype_equiv_of_le_symm_apply_coe_coe, submodule.coe_smul_of_tower, rel_embedding.coe_fn_to_embedding] end /-- If `M` is finite free over a PID `R`, then any submodule `N` is free and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix in Smith normal form. This is a strengthening of `submodule.basis_of_pid`. See also `ideal.smith_normal_form`, which moreover proves that the dimension of an ideal is the same as the dimension of the whole ring. -/ noncomputable def submodule.smith_normal_form [finite ι] (b : basis ι R M) (N : submodule R M) : Σ (n : ℕ), basis.smith_normal_form N ι n := let ⟨m, n, bM, bN, f, a, snf⟩ := N.smith_normal_form_of_le b ⊤ le_top, bM' := bM.map (linear_equiv.of_top _ rfl), e := bM'.index_equiv b in ⟨n, bM'.reindex e, bN.map (comap_subtype_equiv_of_le le_top), f.trans e.to_embedding, a, λ i, by simp only [snf, basis.map_apply, linear_equiv.of_top_apply, submodule.coe_smul_of_tower, submodule.comap_subtype_equiv_of_le_apply_coe, coe_coe, basis.reindex_apply, equiv.to_embedding_apply, function.embedding.trans_apply, equiv.symm_apply_apply]⟩ section ideal variables {S : Type*} [comm_ring S] [is_domain S] [algebra R S] /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix. See `ideal.exists_smith_normal_form` for a version of this theorem that doesn't need to map `I` into a submodule of `R`. This is a strengthening of `submodule.basis_of_pid`. -/ noncomputable def ideal.smith_normal_form [fintype ι] (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : basis.smith_normal_form (I.restrict_scalars R) ι (fintype.card ι) := let ⟨n, bS, bI, f, a, snf⟩ := (I.restrict_scalars R).smith_normal_form b in have eq : _ := ideal.rank_eq bS hI (bI.map ((restrict_scalars_equiv R S S I).restrict_scalars _)), let e : fin n ≃ fin (fintype.card ι) := fintype.equiv_of_card_eq (by rw [eq, fintype.card_fin]) in ⟨bS, bI.reindex e, e.symm.to_embedding.trans f, a ∘ e.symm, λ i, by simp only [snf, basis.coe_reindex, function.embedding.trans_apply, equiv.to_embedding_apply]⟩ variables [finite ι] /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix. See also `ideal.smith_normal_form` for a version of this theorem that returns a `basis.smith_normal_form`. The definitions `ideal.ring_basis`, `ideal.self_basis`, `ideal.smith_coeffs` are (noncomputable) choices of values for this existential quantifier. -/ theorem ideal.exists_smith_normal_form (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : ∃ (b' : basis ι R S) (a : ι → R) (ab' : basis ι R I), ∀ i, (ab' i : S) = a i • b' i := by casesI nonempty_fintype ι; exact let ⟨bS, bI, f, a, snf⟩ := I.smith_normal_form b hI, e : fin (fintype.card ι) ≃ ι := equiv.of_bijective f ((fintype.bijective_iff_injective_and_card f).mpr ⟨f.injective, fintype.card_fin _⟩) in have fe : ∀ i, f (e.symm i) = i := e.apply_symm_apply, ⟨bS, a ∘ e.symm, (bI.reindex e).map ((restrict_scalars_equiv _ _ _ _).restrict_scalars R), λ i, by simp only [snf, fe, basis.map_apply, linear_equiv.restrict_scalars_apply, submodule.restrict_scalars_equiv_apply, basis.coe_reindex]⟩ /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix; this is the basis for `S`. See `ideal.self_basis` for the basis on `I`, see `ideal.smith_coeffs` for the entries of the diagonal matrix and `ideal.self_basis_def` for the proof that the inclusion map forms a square diagonal matrix. -/ noncomputable def ideal.ring_basis (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : basis ι R S := (ideal.exists_smith_normal_form b I hI).some /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix; this is the basis for `I`. See `ideal.ring_basis` for the basis on `S`, see `ideal.smith_coeffs` for the entries of the diagonal matrix and `ideal.self_basis_def` for the proof that the inclusion map forms a square diagonal matrix. -/ noncomputable def ideal.self_basis (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : basis ι R I := (ideal.exists_smith_normal_form b I hI).some_spec.some_spec.some /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix; these are the entries of the diagonal matrix. See `ideal.ring_basis` for the basis on `S`, see `ideal.self_basis` for the basis on `I`, and `ideal.self_basis_def` for the proof that the inclusion map forms a square diagonal matrix. -/ noncomputable def ideal.smith_coeffs (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : ι → R := (ideal.exists_smith_normal_form b I hI).some_spec.some /-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module, then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can find a basis for `S` and `I` such that the inclusion map is a square diagonal matrix. -/ @[simp] lemma ideal.self_basis_def (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) : ∀ i, (ideal.self_basis b I hI i : S) = ideal.smith_coeffs b I hI i • ideal.ring_basis b I hI i := (ideal.exists_smith_normal_form b I hI).some_spec.some_spec.some_spec @[simp] lemma ideal.smith_coeffs_ne_zero (b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) (i) : ideal.smith_coeffs b I hI i ≠ 0 := begin intro hi, apply basis.ne_zero (ideal.self_basis b I hI) i, refine subtype.coe_injective _, simp [hi] end end ideal end smith_normal end principal_ideal_domain /-- A set of linearly independent vectors in a module `M` over a semiring `S` is also linearly independent over a subring `R` of `K`. -/ lemma linear_independent.restrict_scalars_algebras {R S M ι : Type*} [comm_semiring R] [semiring S] [add_comm_monoid M] [algebra R S] [module R M] [module S M] [is_scalar_tower R S M] (hinj : function.injective (algebra_map R S)) {v : ι → M} (li : linear_independent S v) : linear_independent R v := linear_independent.restrict_scalars (by rwa algebra.algebra_map_eq_smul_one' at hinj) li
15ba1ac97f9ac80c9ade3d15bd34e2b51962ed52
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/category/Profinite/as_limit.lean
f7d2da181c805faa94ad629c8ad9fc027d5adb14
[ "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,301
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Calle Sönne, Adam Topaz -/ import topology.category.Profinite import topology.discrete_quotient /-! # Profinite sets as limits of finite sets. We show that any profinite set is isomorphic to the limit of its discrete (hence finite) quotients. ## Definitions There are a handful of definitions in this file, given `X : Profinite`: 1. `X.fintype_diagram` is the functor `discrete_quotient X ⥤ Fintype` whose limit is isomorphic to `X` (the limit taking place in `Profinite` via `Fintype_to_Profinite`, see 2). 2. `X.diagram` is an abbreviation for `X.fintype_diagram ⋙ Fintype_to_Profinite`. 3. `X.as_limit_cone` is the cone over `X.diagram` whose cone point is `X`. 4. `X.iso_as_limit_cone_lift` is the isomorphism `X ≅ (Profinite.limit_cone X.diagram).X` induced by lifting `X.as_limit_cone`. 5. `X.as_limit_cone_iso` is the isomorphism `X.as_limit_cone ≅ (Profinite.limit_cone X.diagram)` induced by `X.iso_as_limit_cone_lift`. 6. `X.as_limit` is a term of type `is_limit X.as_limit_cone`. 7. `X.lim : category_theory.limits.limit_cone X.as_limit_cone` is a bundled combination of 3 and 6. -/ noncomputable theory open category_theory namespace Profinite universe u variables (X : Profinite.{u}) /-- The functor `discrete_quotient X ⥤ Fintype` whose limit is isomorphic to `X`. -/ def fintype_diagram : discrete_quotient X ⥤ Fintype := { obj := λ S, Fintype.of S, map := λ S T f, discrete_quotient.of_le f.le } /-- An abbreviation for `X.fintype_diagram ⋙ Fintype_to_Profinite`. -/ abbreviation diagram : discrete_quotient X ⥤ Profinite := X.fintype_diagram ⋙ Fintype.to_Profinite /-- A cone over `X.diagram` whose cone point is `X`. -/ def as_limit_cone : category_theory.limits.cone X.diagram := { X := X, π := { app := λ S, ⟨S.proj, S.proj_is_locally_constant.continuous⟩ } } instance is_iso_as_limit_cone_lift : is_iso ((limit_cone_is_limit X.diagram).lift X.as_limit_cone) := is_iso_of_bijective _ begin refine ⟨λ a b, _, λ a, _⟩, { intro h, refine discrete_quotient.eq_of_proj_eq (λ S, _), apply_fun (λ f : (limit_cone X.diagram).X, f.val S) at h, exact h }, { obtain ⟨b, hb⟩ := discrete_quotient.exists_of_compat (λ S, a.val S) (λ _ _ h, a.prop (hom_of_le h)), refine ⟨b, _⟩, ext S : 3, apply hb }, end /-- The isomorphism between `X` and the explicit limit of `X.diagram`, induced by lifting `X.as_limit_cone`. -/ def iso_as_limit_cone_lift : X ≅ (limit_cone X.diagram).X := as_iso $ (limit_cone_is_limit _).lift X.as_limit_cone /-- The isomorphism of cones `X.as_limit_cone` and `Profinite.limit_cone X.diagram`. The underlying isomorphism is defeq to `X.iso_as_limit_cone_lift`. -/ def as_limit_cone_iso : X.as_limit_cone ≅ limit_cone _ := limits.cones.ext (iso_as_limit_cone_lift _) (λ _, rfl) /-- `X.as_limit_cone` is indeed a limit cone. -/ def as_limit : category_theory.limits.is_limit X.as_limit_cone := limits.is_limit.of_iso_limit (limit_cone_is_limit _) X.as_limit_cone_iso.symm /-- A bundled version of `X.as_limit_cone` and `X.as_limit`. -/ def lim : limits.limit_cone X.diagram := ⟨X.as_limit_cone, X.as_limit⟩ end Profinite
f4031c95bcafeba28d25ebad3a4549a73b2b09e4
dac4e6671410f506c880986cbb2212dd7f5b3dfd
/folklore/banach.lean
7f1291317c680441daa928ae9a92d4013d5c7814
[ "CC-BY-4.0" ]
permissive
thalesant/formalabstracts-2018
e6ddfe8b3ce5c6f055ddcccf345bf55c41f850c1
d206dfa32a6b4a84aacc3a5500a144757e6d3454
refs/heads/master
1,642,678,879,231
1,561,648,713,000
1,561,648,713,000
97,608,420
1
0
null
1,564,063,995,000
1,500,388,250,000
Lean
UTF-8
Lean
false
false
2,404
lean
/- Frechet derivatives on Banach space -/ import topology.metric_space algebra.module .complex noncomputable theory open classical local attribute [instance] prop_decidable universes u v variables (α : Type u) (β : Type v) section topology variables [topological_space α] variables [topological_space β] class topological_field [field α] extends topological_ring α : Prop := (inv_cont : continuous (λ (x : { x // (x : α) ≠ 0 }), (x : α)⁻¹)) class topological_module [ring α] [module α β] [topological_ring α] extends topological_add_group β : Prop := (continuous_mul : continuous (λp:α×β, p.1 • p.2)) class t1_topological_vector_space [field α] [topological_field α] [t1_space β] [vector_space α β] extends topological_module α β : Prop class has_norm (α : Type u) := (norm : α → ℝ) class multiplicative_norm (α : Type u) [has_norm α] [monoid α] : Prop := (mult : ∀ (x y : α), has_norm.norm (x * y) = has_norm.norm x * has_norm.norm y) #print fields t1_topological_vector_space --def topological_vector_space.to_vector_space [field α] [topological_field α] [t1_space β] [m : module α β] --(tvs : t1_topological_vector_space α β) : vector_space α β := m end topology section normed_ring open has_norm variables [ring α] [metric_space α] [has_norm α] class normed_ring : Prop := (submult : ∀ (x y : α), norm(x * y) ≤ norm x * norm y) (norm_dist : ∀ (x y : α), dist x y = norm (x - y)) class multiplicative_normed_ring (α : Type u) [ring α] [metric_space α] [has_norm α] extends normed_ring α, multiplicative_norm α : Prop end normed_ring constant normed_ring.to_topological_field (α : Type*) [field α] [metric_space α] [has_norm α] [normed_ring α] : topological_field α attribute [instance] normed_ring.to_topological_field section variables [field α] [metric_space α] [has_norm α] [normed_ring α] [vector_space α β] [has_norm β] [metric_space β] [t1_topological_vector_space α β] class normed_space : Prop := (scalar_norm : ∀ (x : α) (y : β), has_norm.norm (x • y) = has_norm.norm x * has_norm.norm y) (dist_norm: ∀ (x y : β), dist x y = has_norm.norm (x - y)) (triangle : ∀ (x y : β), has_norm.norm(x + y) ≤ has_norm.norm x + has_norm.norm y) structure banach_space [complete_space α] [complete_space β] extends normed_space α β : Prop end
177c14319edf3c9135759f2246319c7dc211e964
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/data/padics/hensel.lean
d2744f110d1082b258c3e71ec895dae0bbe40355
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
21,571
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.padics.padic_integers data.polynomial topology.metric_space.cau_seq_filter import analysis.specific_limits topology.algebra.polynomial /-! # Hensel's lemma on ℤ_p This file proves Hensel's lemma on ℤ_p, roughly following Keith Conrad's writeup: http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf Hensel's lemma gives a simple condition for the existence of a root of a polynomial. The proof and motivation are described in the paper [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]. ## References * http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * https://en.wikipedia.org/wiki/Hensel%27s_lemma ## Tags p-adic, p adic, padic, p-adic integer -/ noncomputable theory open_locale classical topological_space -- We begin with some general lemmas that are used below in the computation. lemma padic_polynomial_dist {p : ℕ} [p.prime] (F : polynomial ℤ_[p]) (x y : ℤ_[p]) : ∥F.eval x - F.eval y∥ ≤ ∥x - y∥ := let ⟨z, hz⟩ := F.eval_sub_factor x y in calc ∥F.eval x - F.eval y∥ = ∥z∥ * ∥x - y∥ : by simp [hz] ... ≤ 1 * ∥x - y∥ : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... = ∥x - y∥ : by simp open filter metric private lemma comp_tendsto_lim {p : ℕ} [p.prime] {F : polynomial ℤ_[p]} (ncs : cau_seq ℤ_[p] norm) : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 (F.eval ncs.lim)) := @tendsto.comp _ _ _ ncs (λ k, F.eval k) _ _ _ (continuous_iff_continuous_at.1 F.continuous_eval _) (tendsto_limit ncs) section parameters {p : ℕ} [nat.prime p] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} {a : ℤ_[p]} (ncs_der_val : ∀ n, ∥F.derivative.eval (ncs n)∥ = ∥F.derivative.eval a∥) include ncs_der_val private lemma ncs_tendsto_const : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 ∥F.derivative.eval a∥) := by convert tendsto_const_nhds; ext; rw ncs_der_val private lemma ncs_tendsto_lim : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 (∥F.derivative.eval ncs.lim∥)) := tendsto.comp (continuous_iff_continuous_at.1 continuous_norm _) (comp_tendsto_lim _) private lemma norm_deriv_eq : ∥F.derivative.eval ncs.lim∥ = ∥F.derivative.eval a∥ := tendsto_nhds_unique at_top_ne_bot ncs_tendsto_lim ncs_tendsto_const end section parameters {p : ℕ} [nat.prime p] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} (hnorm : tendsto (λ i, ∥F.eval (ncs i)∥) at_top (𝓝 0)) include hnorm private lemma tendsto_zero_of_norm_tendsto_zero : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 0) := tendsto_iff_norm_tendsto_zero.2 (by simpa using hnorm) lemma limit_zero_of_norm_tendsto_zero : F.eval ncs.lim = 0 := tendsto_nhds_unique at_top_ne_bot (comp_tendsto_lim _) tendsto_zero_of_norm_tendsto_zero end section hensel open nat parameters {p : ℕ} [nat.prime p] {F : polynomial ℤ_[p]} {a : ℤ_[p]} (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) (hnsol : F.eval a ≠ 0) include hnorm /-- `T` is an auxiliary value that is used to control the behavior of the polynomial `F`. -/ private def T : ℝ := ∥(F.eval a).val / ((F.derivative.eval a).val)^2∥ private lemma deriv_sq_norm_pos : 0 < ∥F.derivative.eval a∥ ^ 2 := lt_of_le_of_lt (norm_nonneg _) hnorm private lemma deriv_sq_norm_ne_zero : ∥F.derivative.eval a∥^2 ≠ 0 := ne_of_gt deriv_sq_norm_pos private lemma deriv_norm_ne_zero : ∥F.derivative.eval a∥ ≠ 0 := λ h, deriv_sq_norm_ne_zero (by simp [*, _root_.pow_two]) private lemma deriv_norm_pos : 0 < ∥F.derivative.eval a∥ := lt_of_le_of_ne (norm_nonneg _) (ne.symm deriv_norm_ne_zero) private lemma deriv_ne_zero : F.derivative.eval a ≠ 0 := mt (norm_eq_zero _).2 deriv_norm_ne_zero private lemma T_def : T = ∥F.eval a∥ / ∥F.derivative.eval a∥^2 := calc T = ∥(F.eval a).val∥ / ∥((F.derivative.eval a).val)^2∥ : normed_field.norm_div _ _ ... = ∥F.eval a∥ / ∥(F.derivative.eval a)^2∥ : by simp [norm, padic_norm_z] ... = ∥F.eval a∥ / ∥(F.derivative.eval a)∥^2 : by simp [pow, monoid.pow] private lemma T_lt_one : T < 1 := let h := (div_lt_one_iff_lt deriv_sq_norm_pos).2 hnorm in by rw T_def; apply h private lemma T_pow {n : ℕ} (hn : n > 0) : T ^ n < 1 := have T ^ n ≤ T ^ 1, from pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) (succ_le_of_lt hn), lt_of_le_of_lt (by simpa) T_lt_one private lemma T_pow' (n : ℕ) : T ^ (2 ^ n) < 1 := (T_pow (nat.pow_pos (by norm_num) _)) private lemma T_pow_nonneg (n : ℕ) : T ^ n ≥ 0 := pow_nonneg (norm_nonneg _) _ /-- We will construct a sequence of elements of ℤ_p satisfying successive values of `ih`. -/ private def ih (n : ℕ) (z : ℤ_[p]) : Prop := ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∥F.eval z∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) private lemma ih_0 : ih 0 a := ⟨ rfl, by simp [T_def, mul_div_cancel' _ (ne_of_gt (deriv_sq_norm_pos hnorm))] ⟩ private lemma calc_norm_le_one {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1 := calc ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ = ∥(↑(F.eval z) : ℚ_[p])∥ / ∥(↑(F.derivative.eval z) : ℚ_[p])∥ : normed_field.norm_div _ _ ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : by simp [hz.1] ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ ... ≤ 1 : mul_le_one (padic_norm_z.le_one _) (T_pow_nonneg _) (le_of_lt (T_pow' _)) private lemma calc_deriv_dist {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) (hz1 : ∥z1∥ = ∥F.eval z∥ / ∥F.derivative.eval a∥) {n} (hz : ih n z) : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥ := calc ∥F.derivative.eval z' - F.derivative.eval z∥ ≤ ∥z' - z∥ : padic_polynomial_dist _ _ _ ... = ∥z1∥ : by simp [hz'] ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : hz1 ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel deriv_norm_ne_zero _ ... < ∥F.derivative.eval a∥ : (mul_lt_iff_lt_one_right deriv_norm_pos).2 (T_pow (pow_pos (by norm_num) _)) private def calc_eval_z' {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) {n} (hz : ih n z) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : {q : ℤ_[p] // F.eval z' = q * z1^2} := have hdzne' : (↑(F.derivative.eval z) : ℚ_[p]) ≠ 0, from have hdzne : F.derivative.eval z ≠ 0, from mt (norm_eq_zero _).2 (by rw hz.1; apply deriv_norm_ne_zero; assumption), λ h, hdzne $ subtype.ext.2 h, let ⟨q, hq⟩ := F.binom_expansion z (-z1) in have ∥(↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)) : ℚ_[p])∥ ≤ 1, by {rw padic_norm_e.mul, apply mul_le_one, apply padic_norm_z.le_one, apply norm_nonneg, apply h1}, have F.derivative.eval z * (-z1) = -F.eval z, from calc F.derivative.eval z * (-z1) = (F.derivative.eval z) * -⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩ : by rw [hzeq] ... = -((F.derivative.eval z) * ⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩) : by simp ... = -(⟨↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)), this⟩) : subtype.ext.2 $ by simp ... = -(F.eval z) : by simp [mul_div_cancel' _ hdzne'], have heq : F.eval z' = q * z1^2, by simpa [this, hz'] using hq, ⟨q, heq⟩ private def calc_eval_z'_norm {z z' z1 : ℤ_[p]} {n} (hz : ih n z) {q} (heq : F.eval z' = q * z1^2) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)) := calc ∥F.eval z'∥ = ∥q∥ * ∥z1∥^2 : by simp [heq] ... ≤ 1 * ∥z1∥^2 : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (pow_nonneg (norm_nonneg _) _) ... = ∥F.eval z∥^2 / ∥F.derivative.eval a∥^2 : by simp [hzeq, hz.1, div_pow _ (deriv_norm_ne_zero hnorm)] ... ≤ (∥F.derivative.eval a∥^2 * T^(2^n))^2 / ∥F.derivative.eval a∥^2 : (div_le_div_right deriv_sq_norm_pos).2 (pow_le_pow_of_le_left (norm_nonneg _) hz.2 _) ... = (∥F.derivative.eval a∥^2)^2 * (T^(2^n))^2 / ∥F.derivative.eval a∥^2 : by simp only [_root_.mul_pow] ... = ∥F.derivative.eval a∥^2 * (T^(2^n))^2 : div_sq_cancel deriv_sq_norm_ne_zero _ ... = ∥F.derivative.eval a∥^2 * T^(2^(n + 1)) : by rw [←pow_mul]; refl set_option eqn_compiler.zeta true /-- Given `z : ℤ_[p]` satisfying `ih n z`, construct `z' : ℤ_[p]` satisfying `ih (n+1) z'`. We need the hypothesis `ih n z`, since otherwise `z'` is not necessarily an integer. -/ private def ih_n {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : {z' : ℤ_[p] // ih (n+1) z'} := have h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1, from calc_norm_le_one hz, let z1 : ℤ_[p] := ⟨_, h1⟩, z' : ℤ_[p] := z - z1 in ⟨ z', have hdist : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥, from calc_deriv_dist rfl (by simp [z1, hz.1]) hz, have hfeq : ∥F.derivative.eval z'∥ = ∥F.derivative.eval a∥, begin rw [sub_eq_add_neg, ← hz.1, ←norm_neg (F.derivative.eval z)] at hdist, have := padic_norm_z.eq_of_norm_add_lt_right hdist, rwa [norm_neg, hz.1] at this end, let ⟨q, heq⟩ := calc_eval_z' rfl hz h1 rfl in have hnle : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)), from calc_eval_z'_norm hz heq h1 rfl, ⟨hfeq, hnle⟩⟩ set_option eqn_compiler.zeta false -- why doesn't "noncomputable theory" stick here? private noncomputable def newton_seq_aux : Π n : ℕ, {z : ℤ_[p] // ih n z} | 0 := ⟨a, ih_0⟩ | (k+1) := ih_n (newton_seq_aux k).2 private def newton_seq (n : ℕ) : ℤ_[p] := (newton_seq_aux n).1 private lemma newton_seq_deriv_norm (n : ℕ) : ∥F.derivative.eval (newton_seq n)∥ = ∥F.derivative.eval a∥ := (newton_seq_aux n).2.1 private lemma newton_seq_norm_le (n : ℕ) : ∥F.eval (newton_seq n)∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) := (newton_seq_aux n).2.2 private lemma newton_seq_norm_eq (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ := by induction n; simp [newton_seq, newton_seq_aux, ih_n] private lemma newton_seq_succ_dist (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := calc ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ : newton_seq_norm_eq _ ... = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval a∥ : by rw newton_seq_deriv_norm ... ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 (newton_seq_norm_le _) ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ include hnsol private lemma T_pos : T > 0 := begin rw T_def, apply div_pos_of_pos_of_pos, { apply (norm_pos_iff _).2, apply hnsol }, { exact deriv_sq_norm_pos hnorm } end private lemma newton_seq_succ_dist_weak (n : ℕ) : ∥newton_seq (n+2) - newton_seq (n+1)∥ < ∥F.eval a∥ / ∥F.derivative.eval a∥ := have 2 ≤ 2^(n+1), from have _, from pow_le_pow (by norm_num : 1 ≤ 2) (nat.le_add_left _ _ : 1 ≤ n + 1), by simpa using this, calc ∥newton_seq (n+2) - newton_seq (n+1)∥ ≤ ∥F.derivative.eval a∥ * T^(2^(n+1)) : newton_seq_succ_dist _ ... ≤ ∥F.derivative.eval a∥ * T^2 : mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) ... < ∥F.derivative.eval a∥ * T^1 : mul_lt_mul_of_pos_left (pow_lt_pow_of_lt_one T_pos T_lt_one (by norm_num)) deriv_norm_pos ... = ∥F.eval a∥ / ∥F.derivative.eval a∥ : begin rw [T, _root_.pow_two, _root_.pow_one, normed_field.norm_div, ←mul_div_assoc, padic_norm_e.mul], apply mul_div_mul_left', apply deriv_norm_ne_zero; assumption end private lemma newton_seq_dist_aux (n : ℕ) : ∀ k : ℕ, ∥newton_seq (n + k) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) | 0 := begin simp, apply mul_nonneg, {apply norm_nonneg}, {apply T_pow_nonneg} end | (k+1) := have 2^n ≤ 2^(n+k), by {rw [←nat.pow_eq_pow, ←nat.pow_eq_pow], apply pow_le_pow, norm_num, apply nat.le_add_right}, calc ∥newton_seq (n + (k + 1)) - newton_seq n∥ = ∥newton_seq ((n + k) + 1) - newton_seq n∥ : by simp ... = ∥(newton_seq ((n + k) + 1) - newton_seq (n+k)) + (newton_seq (n+k) - newton_seq n)∥ : by rw ←sub_add_sub_cancel ... ≤ max (∥newton_seq ((n + k) + 1) - newton_seq (n+k)∥) (∥newton_seq (n+k) - newton_seq n∥) : padic_norm_z.nonarchimedean _ _ ... ≤ max (∥F.derivative.eval a∥ * T^(2^((n + k)))) (∥F.derivative.eval a∥ * T^(2^n)) : max_le_max (newton_seq_succ_dist _) (newton_seq_dist_aux _) ... = ∥F.derivative.eval a∥ * T^(2^n) : max_eq_right $ mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) private lemma newton_seq_dist {n k : ℕ} (hnk : n ≤ k) : ∥newton_seq k - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := have hex : ∃ m, k = n + m, from exists_eq_add_of_le hnk, let ⟨_, hex'⟩ := hex in by rw hex'; apply newton_seq_dist_aux; assumption private lemma newton_seq_dist_to_a : ∀ n : ℕ, 0 < n → ∥newton_seq n - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ | 1 h := by simp [newton_seq, newton_seq_aux, ih_n]; apply norm_div | (k+2) h := have hlt : ∥newton_seq (k+2) - newton_seq (k+1)∥ < ∥newton_seq (k+1) - a∥, by rw newton_seq_dist_to_a (k+1) (succ_pos _); apply newton_seq_succ_dist_weak; assumption, have hne' : ∥newton_seq (k + 2) - newton_seq (k+1)∥ ≠ ∥newton_seq (k+1) - a∥, from ne_of_lt hlt, calc ∥newton_seq (k + 2) - a∥ = ∥(newton_seq (k + 2) - newton_seq (k+1)) + (newton_seq (k+1) - a)∥ : by rw ←sub_add_sub_cancel ... = max (∥newton_seq (k + 2) - newton_seq (k+1)∥) (∥newton_seq (k+1) - a∥) : padic_norm_z.add_eq_max_of_ne hne' ... = ∥newton_seq (k+1) - a∥ : max_eq_right_of_lt hlt ... = ∥polynomial.eval a F∥ / ∥polynomial.eval a (polynomial.derivative F)∥ : newton_seq_dist_to_a (k+1) (succ_pos _) private lemma bound' : tendsto (λ n : ℕ, ∥F.derivative.eval a∥ * T^(2^n)) at_top (𝓝 0) := begin rw ←mul_zero (∥F.derivative.eval a∥), exact tendsto_mul (tendsto_const_nhds) (tendsto.comp (tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) (T_lt_one hnorm)) (tendsto_pow_at_top_at_top_of_gt_1_nat (by norm_num))) end private lemma bound : ∀ {ε}, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → ∥F.derivative.eval a∥ * T^(2^n) < ε := have mtn : ∀ n : ℕ, ∥polynomial.eval a (polynomial.derivative F)∥ * T ^ (2 ^ n) ≥ 0, from λ n, mul_nonneg (norm_nonneg _) (T_pow_nonneg _), begin have := bound' hnorm hnsol, simp [tendsto, nhds] at this, intros ε hε, cases this (ball 0 ε) (mem_ball_self hε) (is_open_ball) with N hN, existsi N, intros n hn, simpa [normed_field.norm_mul, real.norm_eq_abs, abs_of_nonneg (mtn n)] using hN _ hn end private lemma bound'_sq : tendsto (λ n : ℕ, ∥F.derivative.eval a∥^2 * T^(2^n)) at_top (𝓝 0) := begin rw [←mul_zero (∥F.derivative.eval a∥), _root_.pow_two], simp only [mul_assoc], apply tendsto_mul, { apply tendsto_const_nhds }, { apply bound', assumption } end private theorem newton_seq_is_cauchy : is_cau_seq norm newton_seq := begin intros ε hε, cases bound hnorm hnsol hε with N hN, existsi N, intros j hj, apply lt_of_le_of_lt, { apply newton_seq_dist _ _ hj, assumption }, { apply hN, apply le_refl } end private def newton_cau_seq : cau_seq ℤ_[p] norm := ⟨_, newton_seq_is_cauchy⟩ private def soln : ℤ_[p] := newton_cau_seq.lim private lemma soln_spec {ε : ℝ} (hε : ε > 0) : ∃ (N : ℕ), ∀ {i : ℕ}, i ≥ N → ∥soln - newton_cau_seq i∥ < ε := setoid.symm (cau_seq.equiv_lim newton_cau_seq) _ hε private lemma soln_deriv_norm : ∥F.derivative.eval soln∥ = ∥F.derivative.eval a∥ := norm_deriv_eq newton_seq_deriv_norm private lemma newton_seq_norm_tendsto_zero : tendsto (λ i, ∥F.eval (newton_cau_seq i)∥) at_top (𝓝 0) := squeeze_zero (λ _, norm_nonneg _) newton_seq_norm_le bound'_sq private lemma newton_seq_dist_tendsto : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 (∥F.eval a∥ / ∥F.derivative.eval a∥)) := tendsto.congr' (suffices ∃ k, ∀ n ≥ k, ∥F.eval a∥ / ∥F.derivative.eval a∥ = ∥newton_cau_seq n - a∥, by simpa, ⟨1, λ _ hx, (newton_seq_dist_to_a _ hx).symm⟩) (tendsto_const_nhds) private lemma newton_seq_dist_tendsto' : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 ∥soln - a∥) := tendsto.comp (continuous_iff_continuous_at.1 continuous_norm _) (tendsto_sub (tendsto_limit _) tendsto_const_nhds) private lemma soln_dist_to_a : ∥soln - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ := tendsto_nhds_unique at_top_ne_bot newton_seq_dist_tendsto' newton_seq_dist_tendsto private lemma soln_dist_to_a_lt_deriv : ∥soln - a∥ < ∥F.derivative.eval a∥ := begin rw soln_dist_to_a, apply div_lt_of_mul_lt_of_pos, { apply deriv_norm_pos; assumption }, { rwa _root_.pow_two at hnorm } end private lemma eval_soln : F.eval soln = 0 := limit_zero_of_norm_tendsto_zero newton_seq_norm_tendsto_zero private lemma soln_unique (z : ℤ_[p]) (hev : F.eval z = 0) (hnlt : ∥z - a∥ < ∥F.derivative.eval a∥) : z = soln := have soln_dist : ∥z - soln∥ < ∥F.derivative.eval a∥, from calc ∥z - soln∥ = ∥(z - a) + (a - soln)∥ : by rw sub_add_sub_cancel ... ≤ max (∥z - a∥) (∥a - soln∥) : padic_norm_z.nonarchimedean _ _ ... < ∥F.derivative.eval a∥ : max_lt hnlt (by rw norm_sub_rev; apply soln_dist_to_a_lt_deriv), let h := z - soln, ⟨q, hq⟩ := F.binom_expansion soln h in have (F.derivative.eval soln + q * h) * h = 0, from eq.symm (calc 0 = F.eval (soln + h) : by simp [hev, h] ... = F.derivative.eval soln * h + q * h^2 : by rw [hq, eval_soln, zero_add] ... = (F.derivative.eval soln + q * h) * h : by rw [_root_.pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval soln + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval soln = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval soln∥ (calc ∥F.derivative.eval soln∥ = ∥(-q) * h∥ : by rw this ... ≤ 1 * ∥h∥ : by rw [padic_norm_z.mul]; exact mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... = ∥z - soln∥ : by simp [h] ... < ∥F.derivative.eval soln∥ : by rw soln_deriv_norm; apply soln_dist), eq_of_sub_eq_zero (by rw ←this; refl) end hensel variables {p : ℕ} [nat.prime p] {F : polynomial ℤ_[p]} {a : ℤ_[p]} private lemma a_soln_is_unique (ha : F.eval a = 0) (z' : ℤ_[p]) (hz' : F.eval z' = 0) (hnormz' : ∥z' - a∥ < ∥F.derivative.eval a∥) : z' = a := let h := z' - a, ⟨q, hq⟩ := F.binom_expansion a h in have (F.derivative.eval a + q * h) * h = 0, from eq.symm (calc 0 = F.eval (a + h) : show 0 = F.eval (a + (z' - a)), by rw add_comm; simp [hz'] ... = F.derivative.eval a * h + q * h^2 : by rw [hq, ha, zero_add] ... = (F.derivative.eval a + q * h) * h : by rw [_root_.pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval a + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval a = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval a∥ (calc ∥F.derivative.eval a∥ = ∥q∥*∥h∥ : by simp [this] ... ≤ 1*∥h∥ : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... < ∥F.derivative.eval a∥ : by simpa [h]), eq_of_sub_eq_zero (by rw ←this; refl) variable (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) include hnorm private lemma a_is_soln (ha : F.eval a = 0) : F.eval a = 0 ∧ ∥a - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval a∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = a := ⟨ha, by simp; apply deriv_norm_pos; apply hnorm, rfl, a_soln_is_unique ha⟩ lemma hensels_lemma : ∃ z : ℤ_[p], F.eval z = 0 ∧ ∥z - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = z := if ha : F.eval a = 0 then ⟨a, a_is_soln hnorm ha⟩ else by refine ⟨soln _ _, eval_soln _ _, soln_dist_to_a_lt_deriv _ _, soln_deriv_norm _ _, soln_unique _ _⟩; assumption
2df702f66c263e0f4faeb3da70832bd823e0a98c
d7189ea2ef694124821b033e533f18905b5e87ef
/galois/logic.lean
f6aa1f8439ad2dfae9bfa4db499a1758228d384d
[ "Apache-2.0" ]
permissive
digama0/lean-protocol-support
eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59
cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda
refs/heads/master
1,625,421,450,627
1,506,035,462,000
1,506,035,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
420
lean
/- Lemmas for basic logic -/ lemma contrapos {p q : Prop} (h : p → q) (g : ¬ q) : ¬ p := g ∘ h lemma ite_iff {P Q : Prop} [decP : decidable P] [decQ : decidable Q] (H : P ↔ Q) {A} : @ite P _ A = ite Q := begin apply funext, intros x, apply funext, intros y, have decP' := decP, cases decP' with HP HP, rw (if_neg HP), rw if_neg, rw ← H, assumption, rw (if_pos HP), rw if_pos, rw ← H, assumption, end
177f5aed8849cc3db9a63408579cfcda3fd636f6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/complex/unit_disc/basic.lean
dfcdf4eb3fed66880d9a560fd6ddfe6ddf5bd27d
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,242
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 analysis.complex.circle import analysis.normed_space.ball_action /-! # Poincaré disc In this file we define `complex.unit_disc` to be the unit disc in the complex plane. We also introduce some basic operations on this disc. -/ open set function metric open_locale big_operators noncomputable theory local notation `conj'` := star_ring_end ℂ namespace complex /-- Complex unit disc. -/ @[derive [comm_semigroup, has_distrib_neg, λ α, has_coe α ℂ, topological_space]] def unit_disc : Type := ball (0 : ℂ) 1 localized "notation `𝔻` := complex.unit_disc" in unit_disc namespace unit_disc lemma coe_injective : injective (coe : 𝔻 → ℂ) := subtype.coe_injective lemma abs_lt_one (z : 𝔻) : abs (z : ℂ) < 1 := mem_ball_zero_iff.1 z.2 lemma abs_ne_one (z : 𝔻) : abs (z : ℂ) ≠ 1 := z.abs_lt_one.ne lemma norm_sq_lt_one (z : 𝔻) : norm_sq z < 1 := @one_pow ℝ _ 2 ▸ (real.sqrt_lt' one_pos).1 z.abs_lt_one lemma coe_ne_one (z : 𝔻) : (z : ℂ) ≠ 1 := ne_of_apply_ne abs $ (map_one abs).symm ▸ z.abs_ne_one lemma coe_ne_neg_one (z : 𝔻) : (z : ℂ) ≠ -1 := ne_of_apply_ne abs $ by { rw [abs.map_neg, map_one], exact z.abs_ne_one } lemma one_add_coe_ne_zero (z : 𝔻) : (1 + z : ℂ) ≠ 0 := mt neg_eq_iff_add_eq_zero.2 z.coe_ne_neg_one.symm @[simp, norm_cast] lemma coe_mul (z w : 𝔻) : ↑(z * w) = (z * w : ℂ) := rfl /-- A constructor that assumes `abs z < 1` instead of `dist z 0 < 1` and returns an element of `𝔻` instead of `↥metric.ball (0 : ℂ) 1`. -/ def mk (z : ℂ) (hz : abs z < 1) : 𝔻 := ⟨z, mem_ball_zero_iff.2 hz⟩ @[simp] lemma coe_mk (z : ℂ) (hz : abs z < 1) : (mk z hz : ℂ) = z := rfl @[simp] lemma mk_coe (z : 𝔻) (hz : abs (z : ℂ) < 1 := z.abs_lt_one) : mk z hz = z := subtype.eta _ _ @[simp] lemma mk_neg (z : ℂ) (hz : abs (-z) < 1) : mk (-z) hz = -mk z (abs.map_neg z ▸ hz) := rfl instance : semigroup_with_zero 𝔻 := { zero := mk 0 $ (map_zero _).trans_lt one_pos, zero_mul := λ z, coe_injective $ zero_mul _, mul_zero := λ z, coe_injective $ mul_zero _, .. unit_disc.comm_semigroup} @[simp] lemma coe_zero : ((0 : 𝔻) : ℂ) = 0 := rfl @[simp] lemma coe_eq_zero {z : 𝔻} : (z : ℂ) = 0 ↔ z = 0 := coe_injective.eq_iff' coe_zero instance : inhabited 𝔻 := ⟨0⟩ instance circle_action : mul_action circle 𝔻 := mul_action_sphere_ball instance is_scalar_tower_circle_circle : is_scalar_tower circle circle 𝔻 := is_scalar_tower_sphere_sphere_ball instance is_scalar_tower_circle : is_scalar_tower circle 𝔻 𝔻 := is_scalar_tower_sphere_ball_ball instance smul_comm_class_circle : smul_comm_class circle 𝔻 𝔻 := smul_comm_class_sphere_ball_ball instance smul_comm_class_circle' : smul_comm_class 𝔻 circle 𝔻 := smul_comm_class.symm _ _ _ @[simp, norm_cast] lemma coe_smul_circle (z : circle) (w : 𝔻) : ↑(z • w) = (z * w : ℂ) := rfl instance closed_ball_action : mul_action (closed_ball (0 : ℂ) 1) 𝔻 := mul_action_closed_ball_ball instance is_scalar_tower_closed_ball_closed_ball : is_scalar_tower (closed_ball (0 : ℂ) 1) (closed_ball (0 : ℂ) 1) 𝔻 := is_scalar_tower_closed_ball_closed_ball_ball instance is_scalar_tower_closed_ball : is_scalar_tower (closed_ball (0 : ℂ) 1) 𝔻 𝔻 := is_scalar_tower_closed_ball_ball_ball instance smul_comm_class_closed_ball : smul_comm_class (closed_ball (0 : ℂ) 1) 𝔻 𝔻 := ⟨λ a b c, subtype.ext $ mul_left_comm _ _ _⟩ instance smul_comm_class_closed_ball' : smul_comm_class 𝔻 (closed_ball (0 : ℂ) 1) 𝔻 := smul_comm_class.symm _ _ _ instance smul_comm_class_circle_closed_ball : smul_comm_class circle (closed_ball (0 : ℂ) 1) 𝔻 := smul_comm_class_sphere_closed_ball_ball instance smul_comm_class_closed_ball_circle : smul_comm_class (closed_ball (0 : ℂ) 1) circle 𝔻 := smul_comm_class.symm _ _ _ @[simp, norm_cast] lemma coe_smul_closed_ball (z : closed_ball (0 : ℂ) 1) (w : 𝔻) : ↑(z • w) = (z * w : ℂ) := rfl /-- Real part of a point of the unit disc. -/ def re (z : 𝔻) : ℝ := re z /-- Imaginary part of a point of the unit disc. -/ def im (z : 𝔻) : ℝ := im z @[simp, norm_cast] lemma re_coe (z : 𝔻) : (z : ℂ).re = z.re := rfl @[simp, norm_cast] lemma im_coe (z : 𝔻) : (z : ℂ).im = z.im := rfl @[simp] lemma re_neg (z : 𝔻) : (-z).re = -z.re := rfl @[simp] lemma im_neg (z : 𝔻) : (-z).im = -z.im := rfl /-- Conjugate point of the unit disc. -/ def conj (z : 𝔻) : 𝔻 := mk (conj' ↑z) $ (abs_conj z).symm ▸ z.abs_lt_one @[simp, norm_cast] lemma coe_conj (z : 𝔻) : (z.conj : ℂ) = conj' ↑z := rfl @[simp] lemma conj_zero : conj 0 = 0 := coe_injective (map_zero conj') @[simp] lemma conj_conj (z : 𝔻) : conj (conj z) = z := coe_injective $ complex.conj_conj z @[simp] lemma conj_neg (z : 𝔻) : (-z).conj = -z.conj := rfl @[simp] lemma re_conj (z : 𝔻) : z.conj.re = z.re := rfl @[simp] lemma im_conj (z : 𝔻) : z.conj.im = -z.im := rfl @[simp] lemma conj_mul (z w : 𝔻) : (z * w).conj = z.conj * w.conj := subtype.ext $ map_mul _ _ _ end unit_disc end complex
7c4fe994935184c5f3ced77f8860aa5863aa83e0
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Meta/Tactic/Simp/SimpCongrTheorems.lean
3ecee3b311f6772056765f3488ecf6eaa6c19f4b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
5,428
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ScopedEnvExtension import Lean.Util.Recognizers import Lean.Util.CollectMVars import Lean.Meta.Basic namespace Lean.Meta /-- Data for user-defined theorems marked with the `congr` attribute. This type should be confused with `CongrTheorem` which reprents different kinds of automatically generated congruence theorems. The `simp` tactic also uses some of them. -/ structure SimpCongrTheorem where theoremName : Name funName : Name hypothesesPos : Array Nat priority : Nat deriving Inhabited, Repr structure SimpCongrTheorems where lemmas : SMap Name (List SimpCongrTheorem) := {} deriving Inhabited, Repr def SimpCongrTheorems.get (d : SimpCongrTheorems) (declName : Name) : List SimpCongrTheorem := match d.lemmas.find? declName with | none => [] | some cs => cs def addSimpCongrTheoremEntry (d : SimpCongrTheorems) (e : SimpCongrTheorem) : SimpCongrTheorems := { d with lemmas := match d.lemmas.find? e.funName with | none => d.lemmas.insert e.funName [e] | some es => d.lemmas.insert e.funName <| insert es } where insert : List SimpCongrTheorem → List SimpCongrTheorem | [] => [e] | e'::es => if e.priority ≥ e'.priority then e::e'::es else e' :: insert es builtin_initialize congrExtension : SimpleScopedEnvExtension SimpCongrTheorem SimpCongrTheorems ← registerSimpleScopedEnvExtension { name := `congrExt initial := {} addEntry := addSimpCongrTheoremEntry finalizeImport := fun s => { s with lemmas := s.lemmas.switch } } def mkSimpCongrTheorem (declName : Name) (prio : Nat) : MetaM SimpCongrTheorem := withReducible do let c ← mkConstWithLevelParams declName let (xs, bis, type) ← forallMetaTelescopeReducing (← inferType c) match type.eq? with | none => throwError "invalid 'congr' theorem, equality expected{indentExpr type}" | some (_, lhs, rhs) => lhs.withApp fun lhsFn lhsArgs => rhs.withApp fun rhsFn rhsArgs => do unless lhsFn.isConst && rhsFn.isConst && lhsFn.constName! == rhsFn.constName! && lhsArgs.size == rhsArgs.size do throwError "invalid 'congr' theorem, equality left/right-hand sides must be applications of the same function{indentExpr type}" let mut foundMVars : MVarIdSet := {} for lhsArg in lhsArgs do for mvarId in (lhsArg.collectMVars {}).result do foundMVars := foundMVars.insert mvarId let mut i := 0 let mut hypothesesPos := #[] for x in xs, bi in bis do if bi.isExplicit && !foundMVars.contains x.mvarId! then let rhsFn? ← forallTelescopeReducing (← inferType x) fun ys xType => do match xType.eq? with | none => pure none -- skip | some (_, xLhs, xRhs) => let mut j := 0 for y in ys do let yType ← inferType y unless onlyMVarsAt yType foundMVars do throwError "invalid 'congr' theorem, argument #{j+1} of parameter #{i+1} contains unresolved parameter{indentExpr yType}" j := j + 1 unless onlyMVarsAt xLhs foundMVars do throwError "invalid 'congr' theorem, parameter #{i+1} is not a valid hypothesis, the left-hand-side contains unresolved parameters{indentExpr xLhs}" let xRhsFn := xRhs.getAppFn unless xRhsFn.isMVar do throwError "invalid 'congr' theorem, parameter #{i+1} is not a valid hypothesis, the right-hand-side head is not a metavariable{indentExpr xRhs}" unless !foundMVars.contains xRhsFn.mvarId! do throwError "invalid 'congr' theorem, parameter #{i+1} is not a valid hypothesis, the right-hand-side head was already resolved{indentExpr xRhs}" for arg in xRhs.getAppArgs do unless arg.isFVar do throwError "invalid 'congr' theorem, parameter #{i+1} is not a valid hypothesis, the right-hand-side argument is not local variable{indentExpr xRhs}" pure (some xRhsFn) match rhsFn? with | none => pure () | some rhsFn => foundMVars := foundMVars.insert x.mvarId! |>.insert rhsFn.mvarId! hypothesesPos := hypothesesPos.push i i := i + 1 return { theoremName := declName funName := lhsFn.constName! hypothesesPos := hypothesesPos priority := prio } where /-- Return `true` if `t` contains a metavariable that is not in `mvarSet` -/ onlyMVarsAt (t : Expr) (mvarSet : MVarIdSet) : Bool := Option.isNone <| t.find? fun e => e.isMVar && !mvarSet.contains e.mvarId! def addSimpCongrTheorem (declName : Name) (attrKind : AttributeKind) (prio : Nat) : MetaM Unit := do let lemma ← mkSimpCongrTheorem declName prio congrExtension.add lemma attrKind builtin_initialize registerBuiltinAttribute { name := `congr descr := "congruence theorem" add := fun declName stx attrKind => do let prio ← getAttrParamOptPrio stx[1] discard <| addSimpCongrTheorem declName attrKind prio |>.run {} {} } def getSimpCongrTheorems : MetaM SimpCongrTheorems := return congrExtension.getState (← getEnv) end Lean.Meta
d218a472fa7e3c9a25faf263aaefdcd0800406b2
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/measure_theory/content.lean
601150efd8780d5a56695d098881efe8ec25df86
[ "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
12,117
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.measure_space import measure_theory.borel_space import topology.opens import topology.compacts /-! # Contents In this file we work with *contents*. A content `λ` is a function from a certain class of subsets (such as the the compact subsets) to `ennreal` (or `nnreal`) that is * additive: If `K₁` and `K₂` are disjoint sets in the domain of `λ`, then `λ(K₁ ∪ K₂) = λ(K₁) + λ(K₂)`; * subadditive: If `K₁` and `K₂` are in the domain of `λ`, then `λ(K₁ ∪ K₂) ≤ λ(K₁) + λ(K₂)`; * monotone: If `K₁ ⊆ K₂` are in the domain of `λ`, then `λ(K₁) ≤ λ(K₂)`. We show that: * Given a content `λ` on compact sets, we get a countably subadditive map that vanishes at `∅`. In Halmos (1950) this is called the *inner content* `λ*` of `λ`. * Given an inner content, we define an outer measure. We don't explicitly define the type of contents. In this file we only work on contents on compact sets, and inner contents on open sets, and both contents and inner contents map into the extended nonnegative reals. However, in other applications other choices can be made, and it is not a priori clear what the best interface should be. ## Main definitions * `measure_theory.inner_content`: define an inner content from an content * `measure_theory.outer_measure.of_content`: construct an outer measure from a content ## References * Paul Halmos (1950), Measure Theory, §53 * https://en.wikipedia.org/wiki/Content_(measure_theory) -/ universe variables u v w noncomputable theory open set topological_space namespace measure_theory variables {G : Type w} [topological_space G] /-- Constructing the inner content of a content. From a content defined on the compact sets, we obtain a function defined on all open sets, by taking the supremum of the content of all compact subsets. -/ def inner_content (μ : compacts G → ennreal) (U : opens G) : ennreal := ⨆ (K : compacts G) (h : K.1 ⊆ U), μ K lemma le_inner_content {μ : compacts G → ennreal} (K : compacts G) (U : opens G) (h2 : K.1 ⊆ U) : μ K ≤ inner_content μ U := le_supr_of_le K $ le_supr _ h2 lemma inner_content_le {μ : compacts G → ennreal} (h : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂) (U : opens G) (K : compacts G) (h2 : (U : set G) ⊆ K.1) : inner_content μ U ≤ μ K := bsupr_le $ λ K' hK', h _ _ (subset.trans hK' h2) lemma inner_content_of_is_compact {μ : compacts G → ennreal} (h : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂) {K : set G} (h1K : is_compact K) (h2K : is_open K) : inner_content μ ⟨K, h2K⟩ = μ ⟨K, h1K⟩ := le_antisymm (bsupr_le $ λ K' hK', h _ ⟨K, h1K⟩ hK') (le_inner_content _ _ subset.rfl) lemma inner_content_empty {μ : compacts G → ennreal} (h : μ ⊥ = 0) : inner_content μ ∅ = 0 := begin refine le_antisymm _ (zero_le _), rw ←h, refine bsupr_le (λ K hK, _), have : K = ⊥, { ext1, rw [subset_empty_iff.mp hK, compacts.bot_val] }, rw this, refl' end /-- This is "unbundled", because that it required for the API of `induced_outer_measure`. -/ lemma inner_content_mono {μ : compacts G → ennreal} ⦃U V : set G⦄ (hU : is_open U) (hV : is_open V) (h2 : U ⊆ V) : inner_content μ ⟨U, hU⟩ ≤ inner_content μ ⟨V, hV⟩ := supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2 lemma inner_content_exists_compact {μ : compacts G → ennreal} {U : opens G} (hU : inner_content μ U < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ K : compacts G, K.1 ⊆ U ∧ inner_content μ U ≤ μ K + ε := begin have h'ε := ennreal.zero_lt_coe_iff.2 hε, cases le_or_lt (inner_content μ U) ε, { exact ⟨⊥, empty_subset _, le_trans h (le_add_of_nonneg_left (zero_le _))⟩ }, have := ennreal.sub_lt_sub_self (ne_of_lt hU) (ne_of_gt $ lt_trans h'ε h) h'ε, conv at this {to_rhs, rw inner_content }, simp only [lt_supr_iff] at this, rcases this with ⟨U, h1U, h2U⟩, refine ⟨U, h1U, _⟩, rw [← ennreal.sub_le_iff_le_add], exact le_of_lt h2U end /-- The inner content of a surpremum of opens is at most the sum of the individual inner contents. -/ lemma inner_content_Sup_nat [t2_space G] {μ : compacts G → ennreal} (h1 : μ ⊥ = 0) (h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂) (U : ℕ → opens G) : inner_content μ (⨆ (i : ℕ), U i) ≤ ∑' (i : ℕ), inner_content μ (U i) := begin have h3 : ∀ (t : finset ℕ) (K : ℕ → compacts G), μ (t.sup K) ≤ t.sum (λ i, μ (K i)), { intros t K, refine finset.induction_on t _ _, { simp only [h1, le_zero_iff_eq, finset.sum_empty, finset.sup_empty] }, { intros n s hn ih, rw [finset.sup_insert, finset.sum_insert hn], exact le_trans (h2 _ _) (add_le_add_left ih _) }}, refine bsupr_le (λ K hK, _), rcases is_compact.elim_finite_subcover K.2 _ (λ i, (U i).prop) _ with ⟨t, ht⟩, swap, { convert hK, rw [opens.supr_def, subtype.coe_mk] }, rcases K.2.finite_compact_cover t (coe ∘ U) (λ i _, (U _).prop) (by simp only [ht]) with ⟨K', h1K', h2K', h3K'⟩, let L : ℕ → compacts G := λ n, ⟨K' n, h1K' n⟩, convert le_trans (h3 t L) _, { ext1, rw [h3K', compacts.finset_sup_val, finset.sup_eq_supr] }, refine le_trans (finset.sum_le_sum _) (ennreal.sum_le_tsum t), intros i hi, refine le_trans _ (le_supr _ (L i)), refine le_trans _ (le_supr _ (h2K' i)), refl' end /-- The inner content of a union of sets is at most the sum of the individual inner contents. This is the "unbundled" version of `inner_content_Sup_nat`. It required for the API of `induced_outer_measure`. -/ lemma inner_content_Union_nat [t2_space G] {μ : compacts G → ennreal} (h1 : μ ⊥ = 0) (h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂) ⦃U : ℕ → set G⦄ (hU : ∀ (i : ℕ), is_open (U i)) : inner_content μ ⟨⋃ (i : ℕ), U i, is_open_Union hU⟩ ≤ ∑' (i : ℕ), inner_content μ ⟨U i, hU i⟩ := by { have := inner_content_Sup_nat h1 h2 (λ i, ⟨U i, hU i⟩), rwa [opens.supr_def] at this } lemma inner_content_comap {μ : compacts G → ennreal} (f : G ≃ₜ G) (h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K) (U : opens G) : inner_content μ (U.comap f.continuous) = inner_content μ U := begin refine supr_congr _ ((compacts.equiv f).surjective) _, intro K, refine supr_congr_Prop image_subset_iff _, intro hK, simp only [equiv.coe_fn_mk, subtype.mk_eq_mk, ennreal.coe_eq_coe, compacts.equiv], apply h, end lemma is_left_invariant_inner_content [group G] [topological_group G] {μ : compacts G → ennreal} (h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G) (U : opens G) : inner_content μ (U.comap $ continuous_mul_left g) = inner_content μ U := by convert inner_content_comap (homeomorph.mul_left g) (λ K, h g) U lemma inner_content_pos [t2_space G] [group G] [topological_group G] {μ : compacts G → ennreal} (h1 : μ ⊥ = 0) (h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂) (h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (K : compacts G) (hK : 0 < μ K) (U : opens G) (hU : (U : set G).nonempty) : 0 < inner_content μ U := begin have : (interior (U : set G)).nonempty, rwa [U.prop.interior_eq], rcases compact_covered_by_mul_left_translates K.2 this with ⟨s, hs⟩, suffices : μ K ≤ s.card * inner_content μ U, { exact (ennreal.mul_pos.mp $ lt_of_lt_of_le hK this).2 }, have : K.1 ⊆ ↑⨆ (g ∈ s), U.comap $ continuous_mul_left g, { simpa only [opens.supr_def, opens.coe_comap, subtype.coe_mk] }, refine le_trans (le_inner_content _ _ this) _, refine le_trans (rel_supr_sum (inner_content μ) (inner_content_empty h1) (≤) (inner_content_Sup_nat h1 h2) _ _) _, simp only [is_left_invariant_inner_content h3, finset.sum_const, nsmul_eq_mul, le_refl] end lemma inner_content_mono' {μ : compacts G → ennreal} ⦃U V : set G⦄ (hU : is_open U) (hV : is_open V) (h2 : U ⊆ V) : inner_content μ ⟨U, hU⟩ ≤ inner_content μ ⟨V, hV⟩ := supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2 namespace outer_measure /-- Extending a content on compact sets to an outer measure on all sets. -/ def of_content [t2_space G] (μ : compacts G → ennreal) (h1 : μ ⊥ = 0) : outer_measure G := induced_outer_measure (λ U hU, inner_content μ ⟨U, hU⟩) is_open_empty (inner_content_empty h1) variables [t2_space G] {μ : compacts G → ennreal} {h1 : μ ⊥ = 0} (h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂) include h2 lemma of_content_opens (U : opens G) : of_content μ h1 U = inner_content μ U := induced_outer_measure_eq' (λ _, is_open_Union) (inner_content_Union_nat h1 h2) inner_content_mono U.2 lemma le_of_content_compacts (K : compacts G) : μ K ≤ of_content μ h1 K.1 := begin rw [of_content, induced_outer_measure_eq_infi], { exact le_infi (λ U, le_infi $ λ hU, le_infi $ le_inner_content K ⟨U, hU⟩) }, { exact inner_content_Union_nat h1 h2 }, { exact inner_content_mono } end lemma of_content_eq_infi (A : set G) : of_content μ h1 A = ⨅ (U : set G) (hU : is_open U) (h : A ⊆ U), inner_content μ ⟨U, hU⟩ := induced_outer_measure_eq_infi _ (inner_content_Union_nat h1 h2) inner_content_mono A lemma of_content_interior_compacts (h3 : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂) (K : compacts G) : of_content μ h1 (interior K.1) ≤ μ K := le_trans (le_of_eq $ of_content_opens h2 (opens.interior K.1)) (inner_content_le h3 _ _ interior_subset) lemma of_content_exists_compact {U : opens G} (hU : of_content μ h1 U < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ K : compacts G, K.1 ⊆ U ∧ of_content μ h1 U ≤ of_content μ h1 K.1 + ε := begin rw [of_content_opens h2] at hU ⊢, rcases inner_content_exists_compact hU hε with ⟨K, h1K, h2K⟩, exact ⟨K, h1K, le_trans h2K $ add_le_add_right (le_of_content_compacts h2 K) _⟩, end lemma of_content_exists_open {A : set G} (hA : of_content μ h1 A < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ U : opens G, A ⊆ U ∧ of_content μ h1 U ≤ of_content μ h1 A + ε := begin rcases induced_outer_measure_exists_set _ _ inner_content_mono hA hε with ⟨U, hU, h2U, h3U⟩, exact ⟨⟨U, hU⟩, h2U, h3U⟩, swap, exact inner_content_Union_nat h1 h2 end lemma of_content_preimage (f : G ≃ₜ G) (h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K) (A : set G) : of_content μ h1 (f ⁻¹' A) = of_content μ h1 A := begin refine induced_outer_measure_preimage _ (inner_content_Union_nat h1 h2) inner_content_mono _ (λ s, f.is_open_preimage) _, intros s hs, convert inner_content_comap f h ⟨s, hs⟩ end lemma is_left_invariant_of_content [group G] [topological_group G] (h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G) (A : set G) : of_content μ h1 ((λ h, g * h) ⁻¹' A) = of_content μ h1 A := by convert of_content_preimage h2 (homeomorph.mul_left g) (λ K, h g) A lemma of_content_caratheodory (A : set G) : (of_content μ h1).caratheodory.is_measurable A ↔ ∀ (U : opens G), of_content μ h1 (U ∩ A) + of_content μ h1 (U \ A) ≤ of_content μ h1 U := begin dsimp [opens], rw subtype.forall, apply induced_outer_measure_caratheodory, apply inner_content_Union_nat h1 h2, apply inner_content_mono' end lemma of_content_pos_of_is_open [group G] [topological_group G] (h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (K : compacts G) (hK : 0 < μ K) {U : set G} (h1U : is_open U) (h2U : U.nonempty) : 0 < of_content μ h1 U := by { convert inner_content_pos h1 h2 h3 K hK ⟨U, h1U⟩ h2U, exact of_content_opens h2 ⟨U, h1U⟩ } end outer_measure end measure_theory
7d27a6bfd5440455ff070d0c15bdbb3e4945d248
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/instances.lean
be36d14555a63e873dd0b2b02ff611f9ab4b7c24
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
938
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.meta.mk_dec_eq_instance init.subtype init.meta.occurrences init.sum open tactic subtype universe variables u v attribute [instance] definition subtype_decidable_eq {A : Type u} {P : A → Prop} [decidable_eq A] : decidable_eq {x \ P x} := by mk_dec_eq_instance attribute [instance] definition list_decidable_eq {A : Type u} [decidable_eq A] : decidable_eq (list A) := by mk_dec_eq_instance attribute [instance] definition occurrences_decidable_eq : decidable_eq occurrences := by mk_dec_eq_instance attribute [instance] definition unit_decidable_eq : decidable_eq unit := by mk_dec_eq_instance attribute [instance] definition sum_decidable {A : Type u} {B : Type v} [decidable_eq A] [decidable_eq B] : decidable_eq (A ⊕ B) := by mk_dec_eq_instance
10faa61e502085670d0950d44a400b95c2889477
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/584c.lean
abddd52a2641b6f1560d9c362b9d06bbb0aea9da
[ "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
499
lean
section universe variables u parameter (A : Type u) section parameters (a b : A) parameter (H : a = b) definition tst₁ := a parameter {A} definition tst₂ := a lemma symm₂ : b = a := eq.symm H parameters {a b} lemma foo (c : A) (h₂ : c = b) : c = a := eq.trans h₂ symm₂ end parameter (a : A) definition tst₃ := a end #check @tst₁ #check @tst₂ -- A is implicit #check @symm₂ #check @tst₃ -- A is explicit again #check @foo
41b02bec6dda323b2c39ea679f49f1f2981749fc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/KeyedDeclsAttribute.lean
0e62849edb7599242085defa3263045c9a0ed4a0
[ "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,263
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, Sebastian Ullrich -/ import Lean.Compiler.InitAttr import Lean.ScopedEnvExtension import Lean.Compiler.IR.CompilerM /-! A builder for attributes that are applied to declarations of a common type and group them by the given attribute argument (an arbitrary `Name`, currently). Also creates a second "builtin" attribute used for bootstrapping, which saves the applied declarations in an `IO.Ref` instead of an environment extension. Used to register elaborators, macros, tactics, and delaborators. -/ namespace Lean namespace KeyedDeclsAttribute -- could be a parameter as well, but right now it's all names abbrev Key := Name /-- `KeyedDeclsAttribute` definition. Important: `mkConst valueTypeName` and `γ` must be definitionally equal. -/ structure Def (γ : Type) where /-- Builtin attribute name, if any (e.g., `builtin_term_elab) -/ builtinName : Name := Name.anonymous /-- Attribute name (e.g., `term_elab) -/ name : Name /-- Attribute description -/ descr : String valueTypeName : Name /-- Convert `Syntax` into a `Key`, the default implementation expects an identifier. -/ evalKey (builtin : Bool) (stx : Syntax) : AttrM Key := do let stx ← Attribute.Builtin.getIdent stx let kind := stx.getId if (← getEnv).contains kind && (← Elab.getInfoState).enabled then Elab.addConstInfo stx kind none pure kind onAdded (builtin : Bool) (declName : Name) : AttrM Unit := pure () deriving Inhabited structure OLeanEntry where key : Key /-- Name of a declaration stored in the environment which has type `mkConst Def.valueTypeName`. -/ declName : Name deriving Inhabited structure AttributeEntry (γ : Type) extends OLeanEntry where /-- Recall that we cannot store `γ` into .olean files because it is a closure. Given `OLeanEntry.declName`, we convert it into a `γ` by using the unsafe function `evalConstCheck`. -/ value : γ abbrev Table (γ : Type) := SMap Key (List (AttributeEntry γ)) structure ExtensionState (γ : Type) where newEntries : List OLeanEntry := [] table : Table γ := {} declNames : PHashSet Name := {} erased : PHashSet Name := {} deriving Inhabited abbrev Extension (γ : Type) := ScopedEnvExtension OLeanEntry (AttributeEntry γ) (ExtensionState γ) end KeyedDeclsAttribute structure KeyedDeclsAttribute (γ : Type) where defn : KeyedDeclsAttribute.Def γ -- imported/builtin instances tableRef : IO.Ref (KeyedDeclsAttribute.Table γ) -- instances from current module ext : KeyedDeclsAttribute.Extension γ deriving Nonempty namespace KeyedDeclsAttribute private def Table.insert (table : Table γ) (v : AttributeEntry γ) : Table γ := match table.find? v.key with | some vs => SMap.insert table v.key (v::vs) | none => SMap.insert table v.key [v] def ExtensionState.insert (s : ExtensionState γ) (v : AttributeEntry γ) : ExtensionState γ := { table := s.table.insert v newEntries := v.toOLeanEntry :: s.newEntries declNames := s.declNames.insert v.declName erased := s.erased.erase v.declName } def addBuiltin (attr : KeyedDeclsAttribute γ) (key : Key) (declName : Name) (value : γ) : IO Unit := attr.tableRef.modify fun m => m.insert { key, declName, value } def mkStateOfTable (table : Table γ) : ExtensionState γ := { table declNames := table.fold (init := {}) fun s _ es => es.foldl (init := s) fun s e => s.insert e.declName } def ExtensionState.erase (s : ExtensionState γ) (attrName : Name) (declName : Name) : CoreM (ExtensionState γ) := do unless s.declNames.contains declName do throwError "'{declName}' does not have [{attrName}] attribute" return { s with erased := s.erased.insert declName, declNames := s.declNames.erase declName } protected unsafe def init {γ} (df : Def γ) (attrDeclName : Name := by exact decl_name%) : IO (KeyedDeclsAttribute γ) := do let tableRef ← IO.mkRef ({} : Table γ) let ext : Extension γ ← registerScopedEnvExtension { name := attrDeclName mkInitial := return mkStateOfTable (← tableRef.get) ofOLeanEntry := fun _ entry => do let ctx ← read match ctx.env.evalConstCheck γ ctx.opts df.valueTypeName entry.declName with | Except.ok f => return { toOLeanEntry := entry, value := f } | Except.error ex => throw (IO.userError ex) addEntry := fun s e => s.insert e toOLeanEntry := (·.toOLeanEntry) } unless df.builtinName.isAnonymous do registerBuiltinAttribute { ref := attrDeclName name := df.builtinName descr := "(builtin) " ++ df.descr add := fun declName stx kind => do unless kind == AttributeKind.global do throwError "invalid attribute '{df.builtinName}', must be global" let key ← df.evalKey true stx let decl ← getConstInfo declName match decl.type with | Expr.const c _ => if c != df.valueTypeName then throwError "unexpected type at '{declName}', '{df.valueTypeName}' expected" else /- builtin_initialize @addBuiltin $(mkConst valueTypeName) $(mkConst attrDeclName) $(key) $(declName) $(mkConst declName) -/ let val := mkAppN (mkConst ``addBuiltin) #[mkConst df.valueTypeName, mkConst attrDeclName, toExpr key, toExpr declName, mkConst declName] declareBuiltin declName val df.onAdded true declName | _ => throwError "unexpected type at '{declName}', '{df.valueTypeName}' expected" applicationTime := AttributeApplicationTime.afterCompilation } registerBuiltinAttribute { ref := attrDeclName name := df.name descr := df.descr erase := fun declName => do let s := ext.getState (← getEnv) let s ← s.erase df.name declName modifyEnv fun env => ext.modifyState env fun _ => s add := fun declName stx attrKind => do let key ← df.evalKey false stx match IR.getSorryDep (← getEnv) declName with | none => let val ← evalConstCheck γ df.valueTypeName declName ext.add { key := key, declName := declName, value := val } attrKind df.onAdded false declName | _ => -- If the declaration contains `sorry`, we skip `evalConstCheck` to avoid unnecessary bizarre error message pure () applicationTime := AttributeApplicationTime.afterCompilation } pure { defn := df, tableRef := tableRef, ext := ext } /-- Retrieve entries tagged with `[attr key]` or `[builtinAttr key]`. -/ def getEntries {γ} (attr : KeyedDeclsAttribute γ) (env : Environment) (key : Name) : List (AttributeEntry γ) := let s := attr.ext.getState env let attrs := s.table.findD key [] if s.erased.isEmpty then attrs else attrs.filter fun attr => !s.erased.contains attr.declName /-- Retrieve values tagged with `[attr key]` or `[builtinAttr key]`. -/ def getValues {γ} (attr : KeyedDeclsAttribute γ) (env : Environment) (key : Name) : List γ := (getEntries attr env key).map AttributeEntry.value end KeyedDeclsAttribute end Lean
c40f2371b0edad6a7f61f7a74f98bc8650f7622a
c17c60327eee1622a8d7defa5af340e08619107f
/src/snippets/definitions/definitions.lean
1215fe09fb0a94b23f9d63eee4cfb263a50f1915
[]
no_license
FredericLeRoux/dEAduction-lean2
277e3aad5102ff155fb04b188dbd01568ceea005
bf7d7d88c2511ecfda5a98ed96e4ca3bc7ae1151
refs/heads/master
1,667,931,697,036
1,593,174,880,000
1,593,174,880,000
275,150,842
0
0
null
null
null
null
UTF-8
Lean
false
false
5,437
lean
import tactic import data.real.basic import data.set import data.set.lattice import tactic import logics namespace tactic.interactive open lean.parser tactic interactive open tactic expr local postfix *:9001 := many /- Tente de réécrire le but ou une hypothèse avec un lemme du type "definitions.***" dans le sens direct, puis dans le sens réciproque -/ -- Ammélioration : utilisation parse location, cf mathlib doc -- Amélioration vitale : cibler une occurence de la définition -- Amélioration : pouvoir passer deux hypothèses (ou plus) qui seront combinées en P ∧ Q ; problème = -- comment les combiner dans le bon ordre ? -- Amélioration : essayer successivement toutes les versions d'un lemme, -- terminant par un numéro, e g intersection_2, intersection_ensemble meta def defi (name : parse ident) (at_hypo : parse (optional (tk "at" *> ident))) : tactic unit := do let name := "definitions" <.> to_string name, trace ("J'appelle le lemme " ++ to_string name ++ ","), expr ← mk_const name, -- expr ← get_local name, -- expr ← (to_expr ``(%%name)), match at_hypo with | none := do {rewrite_target expr, trace "sur le but, sens direct"} <|> do {rewrite_target expr {symm := tt}, trace "sur le but, sens réciproque"} -- | `(tk "at" %%hypo) := skip, | some hypo := do e ← get_local hypo, do {rewrite_hyp expr e, trace ("sur l'hypothèse " ++ (to_string hypo) ++", sens direct")} <|> do {rewrite_hyp expr e {symm := tt}, trace ("sur l'hypothèse " ++ (to_string hypo) ++", sens réciproque")} end -- à AMELIORER : essayer simp only si ça rate meta def applique (names : parse ident*) : tactic unit := match names with | [H1,H2] := do nom_hyp ← get_unused_name `H, n1 ← get_local H1, n2 ← get_local H2, «have» nom_hyp none ``(%%n1 %%n2) | _ := fail "Il faut deux paramètres exactement" end -- à AMELIORER meta def appliquetheo (names : parse ident*) : tactic unit := match names with | [name,H2] := do nom_hyp ← get_unused_name `H, let name := "theoremes" <.> to_string name, n1 ← mk_const name, n2 ← get_local H2, «have» nom_hyp none ``(%%n1 %%n2) | _ := fail "Il faut deux paramètres exactement" end end tactic.interactive ------------- Lemmes définitionnels --------------- namespace definitions ------------ Théorie des ensembles -------------- section theorie_des_ensembles variables {X : Type} {Y : Type} -- mem_compl_iff --lemma complement {A : set X} {x : X} : x ∈ - A ↔ ¬ x ∈ A := --iff.rfl lemma complement {A : set X} {x : X} : x ∈ set.univ \ A ↔ x ∉ A := by finish lemma complement_1 {A : set X} {x : X} : x ∈ set.compl A ↔ x ∉ A := by finish lemma complement_2 {A B : set X} {x : X} : x ∈ B \ A ↔ (x ∈ B ∧ x ∉ A) := iff.rfl lemma inclusion (A B : set X) : A ⊆ B ↔ ∀ {{x:X}}, x ∈ A → x ∈ B := iff.rfl lemma intersection_deux (A B : set X) (x : X) : x ∈ A ∩ B ↔ ( x ∈ A ∧ x ∈ B) := iff.rfl -- bof : ce n'est pas une définition, mais une caractérisation lemma intersection_ensemble (A B C : set X) : C ⊆ A ∩ B ↔ C ⊆ A ∧ C ⊆ B := begin exact ball_and_distrib end lemma intersection_quelconque (I : Type) (O : I → set X) (x : X) : (x ∈ set.Inter O) ↔ (∀ i:I, x ∈ O i) := set.mem_Inter -- Les deux lemmes suivants seront à regroupé au sein d'une même tactique : essayer le premier, -- en cas d'échec essayer le second. Un seul bouton dans l'interface graphique lemma union (A : set X) (B : set X) (x : X) : x ∈ A ∪ B ↔ ( x ∈ A ∨ x ∈ B) := iff.rfl lemma union_quelconque (I : Type) (O : I → set X) (x : X) : (x ∈ set.Union O) ↔ (∃ i:I, x ∈ O i) := set.mem_Union -- mem_image_iff_bex lemma image (A : set X) (f : X → Y) (b : Y) : b ∈ f '' A ↔ ∃ a, a ∈ A ∧ f(a) = b := begin tidy, end lemma image_reciproque {B : set Y} {f : X → Y} {a : X} : a ∈ f ⁻¹' B ↔ f a ∈ B := iff.rfl lemma ensemble_egal {A A' : set X} : (A = A') ↔ ( ∀ x, x ∈ A ↔ x ∈ A' ) := by exact set.ext_iff lemma double_inclusion {A A' : set X} : (A = A') ↔ (A ⊆ A' ∧ A' ⊆ A) := begin exact le_antisymm_iff end end theorie_des_ensembles -------------------- LOGIQUE ----------------------- section logique lemma double_implication (P Q : Prop) : (P ↔ Q) ↔ (P → Q) ∧ (Q → P) := by tautology end logique set_option trace.simplify.rewrite true -- set_option pp.all true ------------------ Nombres ------------------- section nombres lemma minimum (a b m :ℝ) : m = min a b ↔ (m=a ∨ m=b) ∧ m ≤ a ∧ m ≤ b := begin by_cases a ≤ b, simp only [h, min_eq_left], split, intro H, rw H, finish, finish, push_neg at h, have H : min a b = b, by exact min_eq_right_of_lt h, rw H, split, intro H', split, finish, rw H', split, linarith only [h], exact le_refl b, rintro ⟨ H1, H2, H3 ⟩, cases H1 with Ha Hb, exfalso, rw Ha at H3, linarith only [h, H3], assumption end end nombres ------------------ Topologie ------------------- ------------------------------------------------ end definitions
1aa8c70149d5c482e89edcc99025d5cfc5424f59
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/tests/lean/autoBoundImplicits1.lean
7f57885ed13903ad9ab71e2073b31025ffbdc597
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,328
lean
def myid (a : α) := a -- works #check myid 10 #check myid true theorem ex1 (a : α) : myid a = a := rfl def cnst (b : β) : α → β := fun _ => b -- works theorem ex2 (b : β) (a : α) : cnst b a = b := rfl def Vec (α : Type) (n : Nat) := { a : Array α // a.size = n } def mkVec : Vec α 0 := ⟨ #[], rfl ⟩ def Vec.map (xs : Vec α n) (f : α → β) : Vec β n := ⟨ xs.val.map f, sorry ⟩ /- unbound implicit locals must be greek or lower case letters followed by numerical digits -/ def Vec.map2 (xs : Vec α size /- error: unknown identifier size -/) (f : α → β) : Vec β n := ⟨ xs.val.map f, sorry ⟩ set_option autoBoundImplicitLocal false in def Vec.map3 (xs : Vec α n) (f : α → β) : Vec β n := -- Errors, unknown identifiers 'α', 'n', 'β' ⟨ xs.val.map f, sorry ⟩ def double [Add α] (a : α) := a + a variables (xs : Vec α n) -- works def f := xs #check @f #check f mkVec #check f (α := Nat) mkVec def g (a : α) := xs.val.push a theorem ex3 : g ⟨#[0], rfl⟩ 1 = #[0, 1] := rfl inductive Tree (α β : Type) := | leaf1 : α → Tree α β | leaf2 : β → Tree α β | node : Tree α β → Tree α β → Tree α β inductive TreeElem1 : α → Tree α β → Prop | leaf1 : (a : α) → TreeElem1 a (Tree.leaf1 (β := β) a) | nodeLeft : (a : α) → (left : Tree α β) → (right : Tree α β) → TreeElem1 a left → TreeElem1 a (Tree.node left right) | nodeRight : (a : α) → (left : Tree α β) → (right : Tree α β) → TreeElem1 a right → TreeElem1 a (Tree.node left right) inductive TreeElem2 : β → Tree α β → Prop | leaf2 : (b : β) → TreeElem2 b (Tree.leaf2 (α := α) b) | nodeLeft : (b : β) → (left : Tree α β) → (right : Tree α β) → TreeElem2 b left → TreeElem2 b (Tree.node left right) | nodeRight : (b : β) → (left : Tree α β) → (right : Tree α β) → TreeElem2 b right → TreeElem2 b (Tree.node left right) namespace Ex1 def findSomeRevM? [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := pure none def findSomeRev? (as : Array α) (f : α → Option β) : Option β := Id.run <| findSomeRevM? as f end Ex1 def apply {α : Type u₁} {β : α → Type u₂} (f : (a : α) → β a) (a : α) : β a := f a def pair (a : α₁) := (a, a)
a6de5cde3df3bd5efa69b6d011aa2a3ae4969814
ac89c256db07448984849346288e0eeffe8b20d0
/src/Lean/Elab/SyntheticMVars.lean
fc96c7c58e4e02cd4130c2b25797304d9ebcb5c6
[ "Apache-2.0" ]
permissive
chepinzhang/lean4
002cc667f35417a418f0ebc9cb4a44559bb0ccac
24fe2875c68549b5481f07c57eab4ad4a0ae5305
refs/heads/master
1,688,942,838,326
1,628,801,942,000
1,628,801,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,547
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, Sebastian Ullrich -/ import Lean.Util.ForEachExpr import Lean.Elab.Term import Lean.Elab.Tactic.Basic namespace Lean.Elab.Term open Tactic (TacticM evalTactic getUnsolvedGoals withTacticInfoContext) open Meta /-- Auxiliary function used to implement `synthesizeSyntheticMVars`. -/ private def resumeElabTerm (stx : Syntax) (expectedType? : Option Expr) (errToSorry := true) : TermElabM Expr := -- Remark: if `ctx.errToSorry` is already false, then we don't enable it. Recall tactics disable `errToSorry` withReader (fun ctx => { ctx with errToSorry := ctx.errToSorry && errToSorry }) do elabTerm stx expectedType? false /-- Try to elaborate `stx` that was postponed by an elaboration method using `Expection.postpone`. It returns `true` if it succeeded, and `false` otherwise. It is used to implement `synthesizeSyntheticMVars`. -/ private def resumePostponed (savedContext : SavedContext) (stx : Syntax) (mvarId : MVarId) (postponeOnError : Bool) : TermElabM Bool := withRef stx <| withMVarContext mvarId do let s ← get try withSavedContext savedContext do let mvarDecl ← getMVarDecl mvarId let expectedType ← instantiateMVars mvarDecl.type withInfoHole mvarId do let result ← resumeElabTerm stx expectedType (!postponeOnError) /- We must ensure `result` has the expected type because it is the one expected by the method that postponed stx. That is, the method does not have an opportunity to check whether `result` has the expected type or not. -/ let result ← withRef stx <| ensureHasType expectedType result /- We must perform `occursCheck` here since `result` may contain `mvarId` when it has synthetic `sorry`s. -/ if (← occursCheck mvarId result) then assignExprMVar mvarId result return true else return false catch | ex@(Exception.internal id _) => if id == postponeExceptionId then set s return false else throw ex | ex@(Exception.error _ _) => if postponeOnError then set s return false else logException ex return true /-- Similar to `synthesizeInstMVarCore`, but makes sure that `instMVar` local context and instances are used. It also logs any error message produced. -/ private def synthesizePendingInstMVar (instMVar : MVarId) : TermElabM Bool := withMVarContext instMVar do try synthesizeInstMVarCore instMVar catch | ex@(Exception.error _ _) => logException ex; return true | _ => unreachable! /-- Similar to `synthesizePendingInstMVar`, but generates type mismatch error message. Remark: `eNew` is of the form `@coe ... mvar`, where `mvar` is the metavariable for the `CoeT ...` instance. If `mvar` can be synthesized, then assign `auxMVarId := (expandCoe eNew)`. -/ private def synthesizePendingCoeInstMVar (auxMVarId : MVarId) (errorMsgHeader? : Option String) (eNew : Expr) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Bool := do let instMVarId := eNew.appArg!.mvarId! withMVarContext instMVarId do if (← isDefEq expectedType eType) then /- This case may seem counterintuitive since we created the coercion because the `isDefEq expectedType eType` test failed before. However, it may succeed here because we have more information, for example, metavariables occurring at `expectedType` and `eType` may have been assigned. -/ if (← occursCheck auxMVarId e) then assignExprMVar auxMVarId e return true else return false try if (← synthesizeCoeInstMVarCore instMVarId) then let eNew ← expandCoe eNew if (← occursCheck auxMVarId eNew) then assignExprMVar auxMVarId eNew return true return false catch | Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg | _ => unreachable! /-- Try to synthesize a value for `mvarId` using the given default instance. Return `some (val, mvarDecls)` if successful, where `val` is the value assigned to `mvarId`, and `mvarDecls` is a list of new type class instances that need to be synthesized. -/ private def tryToSynthesizeUsingDefaultInstance (mvarId : MVarId) (defaultInstance : Name) : TermElabM (Option (Expr × List SyntheticMVarDecl)) := commitWhenSome? do let candidate ← mkConstWithFreshMVarLevels defaultInstance let (mvars, bis, _) ← forallMetaTelescopeReducing (← inferType candidate) let candidate := mkAppN candidate mvars trace[Elab.resume] "trying default instance for {mkMVar mvarId} := {candidate}" if (← isDefEqGuarded (mkMVar mvarId) candidate) then -- Succeeded. Collect new TC problems let mut result := [] for i in [:bis.size] do if bis[i] == BinderInfo.instImplicit then result := { mvarId := mvars[i].mvarId!, stx := (← getRef), kind := SyntheticMVarKind.typeClass } :: result trace[Elab.resume] "worked" return some (candidate, result) else return none private def tryToSynthesizeUsingDefaultInstances (mvarId : MVarId) (prio : Nat) : TermElabM (Option (Expr × List SyntheticMVarDecl)) := withMVarContext mvarId do let mvarType := (← Meta.getMVarDecl mvarId).type match (← isClass? mvarType) with | none => return none | some className => match (← getDefaultInstances className) with | [] => return none | defaultInstances => for (defaultInstance, instPrio) in defaultInstances do if instPrio == prio then match (← tryToSynthesizeUsingDefaultInstance mvarId defaultInstance) with | some result => return some result | none => continue return none /- Used to implement `synthesizeUsingDefault`. This method only consider default instances with the given priority. -/ private def synthesizeUsingDefaultPrio (prio : Nat) : TermElabM Bool := do let rec visit (syntheticMVars : List SyntheticMVarDecl) (syntheticMVarsNew : List SyntheticMVarDecl) : TermElabM Bool := do match syntheticMVars with | [] => return false | mvarDecl :: mvarDecls => match mvarDecl.kind with | SyntheticMVarKind.typeClass => match (← withRef mvarDecl.stx <| tryToSynthesizeUsingDefaultInstances mvarDecl.mvarId prio) with | none => visit mvarDecls (mvarDecl :: syntheticMVarsNew) | some (val, newMVarDecls) => for newMVarDecl in newMVarDecls do -- Register that `newMVarDecl.mvarId`s are implicit arguments of the value assigned to `mvarDecl.mvarId` registerMVarErrorImplicitArgInfo newMVarDecl.mvarId (← getRef) val let syntheticMVarsNew := newMVarDecls ++ syntheticMVarsNew let syntheticMVarsNew := mvarDecls.reverse ++ syntheticMVarsNew modify fun s => { s with syntheticMVars := syntheticMVarsNew } return true | _ => visit mvarDecls (mvarDecl :: syntheticMVarsNew) /- Recall that s.syntheticMVars is essentially a stack. The first metavariable was the last one created. We want to apply the default instance in reverse creation order. Otherwise, `toString 0` will produce a `OfNat String _` cannot be synthesized error. -/ visit (← get).syntheticMVars.reverse [] /-- Apply default value to any pending synthetic metavariable of kind `SyntheticMVarKind.withDefault` Return true if something was synthesized. -/ private def synthesizeUsingDefault : TermElabM Bool := do let prioSet ← getDefaultInstancesPriorities /- Recall that `prioSet` is stored in descending order -/ for prio in prioSet do if (← synthesizeUsingDefaultPrio prio) then return true return false /-- Report an error for each synthetic metavariable that could not be resolved. Remark: we set `ignoreStuckTC := true` when elaborating `simp` arguments. -/ private def reportStuckSyntheticMVars (ignoreStuckTC := false) : TermElabM Unit := do let syntheticMVars ← modifyGet fun s => (s.syntheticMVars, { s with syntheticMVars := [] }) for mvarSyntheticDecl in syntheticMVars do withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => unless ignoreStuckTC do withMVarContext mvarSyntheticDecl.mvarId do let mvarDecl ← getMVarDecl mvarSyntheticDecl.mvarId unless (← get).messages.hasErrors do throwError "typeclass instance problem is stuck, it is often due to metavariables{indentExpr mvarDecl.type}" | SyntheticMVarKind.coe header eNew expectedType eType e f? => let mvarId := eNew.appArg!.mvarId! withMVarContext mvarId do let mvarDecl ← getMVarDecl mvarId throwTypeMismatchError header expectedType eType e f? (some ("failed to create type class instance for " ++ indentExpr mvarDecl.type)) | _ => unreachable! -- TODO handle other cases. private def getSomeSynthethicMVarsRef : TermElabM Syntax := do let s ← get match s.syntheticMVars.find? fun (mvarDecl : SyntheticMVarDecl) => !mvarDecl.stx.getPos?.isNone with | some mvarDecl => return mvarDecl.stx | none => return Syntax.missing mutual partial def runTactic (mvarId : MVarId) (tacticCode : Syntax) : TermElabM Unit := do /- Recall, `tacticCode` is the whole `by ...` expression. -/ let byTk := tacticCode[0] let code := tacticCode[1] modifyThe Meta.State fun s => { s with mctx := s.mctx.instantiateMVarDeclMVars mvarId } let remainingGoals ← withInfoHole mvarId <| Tactic.run mvarId do withTacticInfoContext tacticCode (evalTactic code) synthesizeSyntheticMVars (mayPostpone := false) unless remainingGoals.isEmpty do reportUnsolvedGoals remainingGoals /-- Try to synthesize the given pending synthetic metavariable. -/ private partial def synthesizeSyntheticMVar (mvarSyntheticDecl : SyntheticMVarDecl) (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => synthesizePendingInstMVar mvarSyntheticDecl.mvarId | SyntheticMVarKind.coe header? eNew expectedType eType e f? => synthesizePendingCoeInstMVar mvarSyntheticDecl.mvarId header? eNew expectedType eType e f? -- NOTE: actual processing at `synthesizeSyntheticMVarsAux` | SyntheticMVarKind.postponed savedContext => resumePostponed savedContext mvarSyntheticDecl.stx mvarSyntheticDecl.mvarId postponeOnError | SyntheticMVarKind.tactic tacticCode savedContext => withSavedContext savedContext do if runTactics then runTactic mvarSyntheticDecl.mvarId tacticCode return true else return false /-- Try to synthesize the current list of pending synthetic metavariables. Return `true` if at least one of them was synthesized. -/ private partial def synthesizeSyntheticMVarsStep (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := do let ctx ← read traceAtCmdPos `Elab.resuming fun _ => m!"resuming synthetic metavariables, mayPostpone: {ctx.mayPostpone}, postponeOnError: {postponeOnError}" let syntheticMVars := (← get).syntheticMVars let numSyntheticMVars := syntheticMVars.length -- We reset `syntheticMVars` because new synthetic metavariables may be created by `synthesizeSyntheticMVar`. modify fun s => { s with syntheticMVars := [] } -- Recall that `syntheticMVars` is a list where head is the most recent pending synthetic metavariable. -- We use `filterRevM` instead of `filterM` to make sure we process the synthetic metavariables using the order they were created. -- It would not be incorrect to use `filterM`. let remainingSyntheticMVars ← syntheticMVars.filterRevM fun mvarDecl => do -- We use `traceM` because we want to make sure the metavar local context is used to trace the message traceM `Elab.postpone (withMVarContext mvarDecl.mvarId do addMessageContext m!"resuming {mkMVar mvarDecl.mvarId}") let succeeded ← synthesizeSyntheticMVar mvarDecl postponeOnError runTactics trace[Elab.postpone] if succeeded then format "succeeded" else format "not ready yet" pure !succeeded -- Merge new synthetic metavariables with `remainingSyntheticMVars`, i.e., metavariables that still couldn't be synthesized modify fun s => { s with syntheticMVars := s.syntheticMVars ++ remainingSyntheticMVars } return numSyntheticMVars != remainingSyntheticMVars.length /-- Try to process pending synthetic metavariables. If `mayPostpone == false`, then `syntheticMVars` is `[]` after executing this method. It keeps executing `synthesizeSyntheticMVarsStep` while progress is being made. If `mayPostpone == false`, then it applies default instances to `SyntheticMVarKind.typeClass` (if available) metavariables that are still unresolved, and then tries to resolve metavariables with `mayPostpone == false`. That is, we force them to produce error messages and/or commit to a "best option". If, after that, we still haven't made progress, we report "stuck" errors. Remark: we set `ignoreStuckTC := true` when elaborating `simp` arguments. Then, pending TC problems become implicit parameters for the simp theorem. -/ partial def synthesizeSyntheticMVars (mayPostpone := true) (ignoreStuckTC := false) : TermElabM Unit := let rec loop (u : Unit) : TermElabM Unit := do withRef (← getSomeSynthethicMVarsRef) <| withIncRecDepth do unless (← get).syntheticMVars.isEmpty do if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then loop () else if !mayPostpone then /- Resume pending metavariables with "elaboration postponement" disabled. We postpone elaboration errors in this step by setting `postponeOnError := true`. Example: ``` #check let x := ⟨1, 2⟩; Prod.fst x ``` The term `⟨1, 2⟩` can't be elaborated because the expected type is not know. The `x` at `Prod.fst x` is not elaborated because the type of `x` is not known. When we execute the following step with "elaboration postponement" disabled, the elaborator fails at `⟨1, 2⟩` and postpones it, and succeeds at `x` and learns that its type must be of the form `Prod ?α ?β`. Recall that we postponed `x` at `Prod.fst x` because its type it is not known. We the type of `x` may learn later its type and it may contain implicit and/or auto arguments. By disabling postponement, we are essentially giving up the opportunity of learning `x`s type and assume it does not have implict and/or auto arguments. -/ if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := true) (runTactics := false) then loop () else if ← synthesizeUsingDefault then loop () else if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then loop () else if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := true) then loop () else reportStuckSyntheticMVars ignoreStuckTC loop () end def synthesizeSyntheticMVarsNoPostponing : TermElabM Unit := synthesizeSyntheticMVars (mayPostpone := false) /- Keep invoking `synthesizeUsingDefault` until it returns false. -/ private partial def synthesizeUsingDefaultLoop : TermElabM Unit := do if (← synthesizeUsingDefault) then synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop def synthesizeSyntheticMVarsUsingDefault : TermElabM Unit := do synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop private partial def withSynthesizeImp {α} (k : TermElabM α) (mayPostpone : Bool) : TermElabM α := do let syntheticMVarsSaved := (← get).syntheticMVars modify fun s => { s with syntheticMVars := [] } try let a ← k synthesizeSyntheticMVars mayPostpone if mayPostpone then synthesizeUsingDefaultLoop return a finally modify fun s => { s with syntheticMVars := s.syntheticMVars ++ syntheticMVarsSaved } /-- Execute `k`, and synthesize pending synthetic metavariables created while executing `k` are solved. If `mayPostpone == false`, then all of them must be synthesized. Remark: even if `mayPostpone == true`, the method still uses `synthesizeUsingDefault` -/ @[inline] def withSynthesize [MonadFunctorT TermElabM m] [Monad m] (k : m α) (mayPostpone := false) : m α := monadMap (m := TermElabM) (withSynthesizeImp . mayPostpone) k /-- Elaborate `stx`, and make sure all pending synthetic metavariables created while elaborating `stx` are solved. -/ def elabTermAndSynthesize (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := withRef stx do instantiateMVars (← withSynthesize <| elabTerm stx expectedType?) end Lean.Elab.Term
8d53fcb7515059a2cbe5e2b287138f0334c1161d
367134ba5a65885e863bdc4507601606690974c1
/src/data/nat/pairing.lean
0386401596632518323a04f4dc62255d8e746fc3
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,986
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro Elegant pairing function. -/ import data.nat.sqrt import data.set.lattice open prod decidable function namespace nat /-- Pairing function for the natural numbers. -/ def mkpair (a b : ℕ) : ℕ := if a < b then b*b + a else a*a + a + b /-- Unpairing function for the natural numbers. -/ def unpair (n : ℕ) : ℕ × ℕ := let s := sqrt n in if n - s*s < s then (n - s*s, s) else (s, n - s*s - s) @[simp] theorem mkpair_unpair (n : ℕ) : mkpair (unpair n).1 (unpair n).2 = n := let s := sqrt n in begin dsimp [unpair], change sqrt n with s, have sm : s * s + (n - s * s) = n := nat.add_sub_cancel' (sqrt_le _), by_cases h : n - s * s < s; simp [h, mkpair], { exact sm }, { have hl : n - s*s - s ≤ s := nat.sub_le_left_of_le_add (nat.sub_le_left_of_le_add $ by rw ← add_assoc; apply sqrt_le_add), suffices : s * s + (s + (n - s * s - s)) = n, {simpa [not_lt_of_ge hl, add_assoc]}, rwa [nat.add_sub_cancel' (le_of_not_gt h)] } end theorem mkpair_unpair' {n a b} (H : unpair n = (a, b)) : mkpair a b = n := by simpa [H] using mkpair_unpair n @[simp] theorem unpair_mkpair (a b : ℕ) : unpair (mkpair a b) = (a, b) := begin by_cases a < b; simp [h, mkpair], { show unpair (b * b + a) = (a, b), have be : sqrt (b * b + a) = b, { rw sqrt_add_eq, exact le_trans (le_of_lt h) (le_add_left _ _) }, simp [unpair, be, nat.add_sub_cancel, h] }, { show unpair (a * a + a + b) = (a, b), have ae : sqrt (a * a + (a + b)) = a, { rw sqrt_add_eq, exact add_le_add_left (le_of_not_gt h) _ }, simp [unpair, ae, not_lt_zero, add_assoc] } end lemma surjective_unpair : surjective unpair := λ ⟨m, n⟩, ⟨mkpair m n, unpair_mkpair m n⟩ theorem unpair_lt {n : ℕ} (n1 : 1 ≤ n) : (unpair n).1 < n := let s := sqrt n in begin simp [unpair], change sqrt n with s, by_cases h : n - s * s < s; simp [h], { exact lt_of_lt_of_le h (sqrt_le_self _) }, { simp at h, have s0 : 0 < s := sqrt_pos.2 n1, exact lt_of_le_of_lt h (nat.sub_lt_self n1 (mul_pos s0 s0)) } end theorem unpair_le_left : ∀ (n : ℕ), (unpair n).1 ≤ n | 0 := dec_trivial | (n+1) := le_of_lt (unpair_lt (nat.succ_pos _)) theorem le_mkpair_left (a b : ℕ) : a ≤ mkpair a b := by simpa using unpair_le_left (mkpair a b) theorem le_mkpair_right (a b : ℕ) : b ≤ mkpair a b := begin by_cases h : a < b; simp [mkpair, h], exact le_trans (le_mul_self _) (le_add_right _ _) end theorem unpair_le_right (n : ℕ) : (unpair n).2 ≤ n := by simpa using le_mkpair_right n.unpair.1 n.unpair.2 theorem mkpair_lt_mkpair_left {a₁ a₂} (b) (h : a₁ < a₂) : mkpair a₁ b < mkpair a₂ b := begin by_cases h₁ : a₁ < b; simp [mkpair, h₁, add_assoc], { by_cases h₂ : a₂ < b; simp [mkpair, h₂, h], simp at h₂, apply add_lt_add_of_le_of_lt, exact mul_self_le_mul_self h₂, exact lt_add_right _ _ _ h }, { simp at h₁, simp [not_lt_of_gt (lt_of_le_of_lt h₁ h)], apply add_lt_add, exact mul_self_lt_mul_self h, apply add_lt_add_right; assumption } end theorem mkpair_lt_mkpair_right (a) {b₁ b₂} (h : b₁ < b₂) : mkpair a b₁ < mkpair a b₂ := begin by_cases h₁ : a < b₁; simp [mkpair, h₁, add_assoc], { simp [mkpair, lt_trans h₁ h, h], exact mul_self_lt_mul_self h }, { by_cases h₂ : a < b₂; simp [mkpair, h₂, h], simp at h₁, rw [add_comm, add_comm _ a, add_assoc, add_lt_add_iff_left], rwa [add_comm, ← sqrt_lt, sqrt_add_eq], exact le_trans h₁ (le_add_left _ _) } end end nat open nat namespace set lemma Union_unpair_prod {α β} {s : ℕ → set α} {t : ℕ → set β} : (⋃ n : ℕ, (s n.unpair.fst).prod (t n.unpair.snd)) = (⋃ n, s n).prod (⋃ n, t n) := by { rw [← Union_prod], convert surjective_unpair.Union_comp _, refl } end set
e8cdcdbd1c1e3bd64e7c408e8e4f5c0bd05cf38c
e09201d437062e1f95e6e5360aab0c9f947901aa
/src/all.lean
55d660d3d1fa91c99dc58a1981cadeef50f56c13
[]
no_license
VArtem/lean-regular-languages
34f4b093f28ef2f09ba7e684e642a0f97c901560
e877243188253d0ac17ccf0ae2da7bf608686ff0
refs/heads/master
1,683,590,111,306
1,622,307,234,000
1,622,307,234,000
284,232,653
7
0
null
null
null
null
UTF-8
Lean
false
false
107
lean
import automata.nfa import automata.dfa import automata.epsnfa import languages.basic import regular.regex
73b402ad7115fac10623f11e3ac4b28bb673b9a9
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/analysis/mean_inequalities.lean
b7a9a1be62897510c8a209cd0bd88a8365115f22
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
49,579
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.specific_functions import analysis.special_functions.pow import data.real.conjugate_exponents import tactic.nth_rewrite import measure_theory.integration /-! # Mean value inequalities In this file we prove several inequalities, including AM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. ## 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. ### Generalized mean inequality The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$ and $p ≤ q$ we have $$ \sqrt[p]{\sum_{i\in s} w_i z_i^p} ≤ \sqrt[q]{\sum_{i\in s} w_i z_i^q}. $$ Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents (`fpow_arith_mean_le_arith_mean_fpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and `arith_mean_le_rpow_mean`). In the first two cases we prove $$ \left(\sum_{i\in s} w_i z_i\right)^n ≤ \sum_{i\in s} w_i z_i^n $$ in order to avoid using real exponents. For real exponents we prove both this and standard versions. ### 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 can be used to prove Hölder's inequality (see below) but we use a different proof. ### 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 `real`, `nnreal` and `ennreal`. There are at least two short proofs of this inequality. In one proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. We use a different proof deducing this inequality from the generalized mean inequality for well-chosen vectors and weights. Hölder's inequality for the Lebesgue integral of ennreal and nnreal functions: we prove `∫ (f * g) ∂μ ≤ (∫ f^p ∂μ) ^ (1/p) * (∫ g^q ∂μ) ^ (1/q)` for `p`, `q` conjugate real exponents and `α→(e)nnreal` functions in two cases, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ennreal functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : nnreal functions. ### 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`, `nnreal` and `ennreal`. 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. Minkowski's inequality for the Lebesgue integral of measurable functions with `ennreal` values: we prove `(∫ (f + g)^p ∂μ) ^ (1/p) ≤ (∫ f^p ∂μ) ^ (1/p) + (∫ g^p ∂μ) ^ (1/p)` for `1 ≤ p`. ## 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. - prove integral versions of these inequalities. -/ universes u v open finset open_locale classical nnreal big_operators noncomputable theory variables {ι : Type u} (s : finset ι) 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 pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (n : ℕ) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := (convex_on_pow n).map_sum_le hw hw' hz theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) {n : ℕ} (hn : even n) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := (convex_on_pow_of_even hn).map_sum_le hw hw' (λ _ _, trivial) theorem fpow_arith_mean_le_arith_mean_fpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 < z i) (m : ℤ) : (∑ i in s, w i * z i) ^ m ≤ ∑ i in s, (w i * z i ^ m) := (convex_on_fpow m).map_sum_le hw hw' hz theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := (convex_on_rpow hp).map_sum_le hw hw' hz theorem arith_mean_le_rpow_mean (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) : ∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) := begin have : 0 < p := lt_of_lt_of_le zero_lt_one hp, rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one], exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp, all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg], intros i hi, apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] }, end 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, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin_zero_elim) 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, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 3)) (fin.cons w₁ $ fin.cons w₂ $ fin.cons w₃ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin.cons p₃ fin_zero_elim) 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, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 4)) (fin.cons w₁ $ fin.cons w₂ $ fin.cons w₃ $ fin.cons w₄ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin.cons p₃ $ fin.cons p₄ fin_zero_elim) /-- Weighted generalized mean inequality, version sums over finite sets, with `ℝ≥0`-valued functions and natural exponent. -/ theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) (n : ℕ) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) n /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp /-- Weighted generalized mean inequality, version for two elements of `ℝ≥0` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0) (hw' : w₁ + w₂ = 1) {p : ℝ} (hp : 1 ≤ p) : (w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons z₁ $ fin.cons z₂ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued functions and real exponents. -/ theorem arith_mean_le_rpow_mean (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : ∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) := by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp end nnreal namespace ennreal /-- Weighted generalized mean inequality, version for sums over finite sets, with `ennreal`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ennreal) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp, have hp_nonneg : 0 ≤ p, from le_of_lt hp_pos, have hp_not_nonpos : ¬ p ≤ 0, by simp [hp_pos], have hp_not_neg : ¬ p < 0, by simp [hp_nonneg], have h_top_iff_rpow_top : ∀ (i : ι) (hi : i ∈ s), w i * z i = ⊤ ↔ w i * (z i) ^ p = ⊤, by simp [hp_pos, hp_nonneg, hp_not_nonpos, hp_not_neg], refine le_of_top_imp_top_of_to_nnreal_le _ _, { -- first, prove `(∑ i in s, w i * z i) ^ p = ⊤ → ∑ i in s, (w i * z i ^ p) = ⊤` rw [rpow_eq_top_iff, sum_eq_top_iff, sum_eq_top_iff], intro h, simp only [and_false, hp_not_neg, false_or] at h, rcases h.left with ⟨a, H, ha⟩, use [a, H], rwa ←h_top_iff_rpow_top a H, }, { -- second, suppose both `(∑ i in s, w i * z i) ^ p ≠ ⊤` and `∑ i in s, (w i * z i ^ p) ≠ ⊤`, -- and prove `((∑ i in s, w i * z i) ^ p).to_nnreal ≤ (∑ i in s, (w i * z i ^ p)).to_nnreal`, -- by using `nnreal.rpow_arith_mean_le_arith_mean_rpow`. intros h_top_rpow_sum _, -- show hypotheses needed to put the `.to_nnreal` inside the sums. have h_top : ∀ (a : ι), a ∈ s → w a * z a < ⊤, { have h_top_sum : ∑ (i : ι) in s, w i * z i < ⊤, { by_contra h, rw [lt_top_iff_ne_top, not_not] at h, rw [h, top_rpow_of_pos hp_pos] at h_top_rpow_sum, exact h_top_rpow_sum rfl, }, rwa sum_lt_top_iff at h_top_sum, }, have h_top_rpow : ∀ (a : ι), a ∈ s → w a * z a ^ p < ⊤, { intros i hi, specialize h_top i hi, rw lt_top_iff_ne_top at h_top ⊢, rwa [ne.def, ←h_top_iff_rpow_top i hi], }, -- put the `.to_nnreal` inside the sums. simp_rw [to_nnreal_sum h_top_rpow, ←to_nnreal_rpow, to_nnreal_sum h_top, to_nnreal_mul, ←to_nnreal_rpow], -- use corresponding nnreal result refine nnreal.rpow_arith_mean_le_arith_mean_rpow s (λ i, (w i).to_nnreal) (λ i, (z i).to_nnreal) _ hp, -- verify the hypothesis `∑ i in s, (w i).to_nnreal = 1`, using `∑ i in s, w i = 1` . have h_sum_nnreal : (∑ i in s, w i) = ↑(∑ i in s, (w i).to_nnreal), { have hw_top : ∑ i in s, w i < ⊤, by { rw hw', exact one_lt_top, }, rw ←to_nnreal_sum, { rw coe_to_nnreal, rwa ←lt_top_iff_ne_top, }, { rwa sum_lt_top_iff at hw_top, }, }, rwa [←coe_eq_coe, ←h_sum_nnreal], }, end /-- Weighted generalized mean inequality, version for two elements of `ennreal` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ennreal) (hw' : w₁ + w₂ = 1) {p : ℝ} (hp : 1 ≤ p) : (w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons z₁ $ fin.cons z₂ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end end ennreal 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 $ by assumption 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 /-- 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 ≤ (abs a)^p / p + (abs b)^q / q := calc a * b ≤ abs (a * b) : le_abs_self (a * b) ... = abs a * abs b : abs_mul a b ... ≤ (abs a)^p / p + (abs 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 / nnreal.of_real p + b ^ q / nnreal.of_real q := begin nth_rewrite 0 ←coe_of_real p hpq.nonneg, nth_rewrite 0 ←coe_of_real q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, 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 -- Let `G=∥g∥_q` be the `L_q`-norm of `g`. set G := (∑ i in s, (g i) ^ q) ^ (1 / q), have hGq : G ^ q = ∑ i in s, (g i) ^ q, { rw [← rpow_mul, one_div_mul_cancel hpq.symm.ne_zero, rpow_one], }, -- First consider the trivial case `∥g∥_q=0` by_cases hG : G = 0, { rw [hG, sum_eq_zero, mul_zero], intros i hi, simp only [rpow_eq_zero_iff, sum_eq_zero_iff] at hG, simp [(hG.1 i hi).1] }, { -- Move power from right to left rw [← div_le_iff hG, sum_div], -- Now the inequality follows from the weighted generalized mean inequality -- with weights `w_i` and numbers `z_i` given by the following formulas. set w : ι → ℝ≥0 := λ i, (g i) ^ q / G ^ q, set z : ι → ℝ≥0 := λ i, f i * (G / g i) ^ (q / p), -- Show that the sum of weights equals one have A : ∑ i in s, w i = 1, { rw [← sum_div, hGq, div_self], simpa [rpow_eq_zero_iff, hpq.symm.ne_zero] using hG }, -- LHS of the goal equals LHS of the weighted generalized mean inequality calc (∑ i in s, f i * g i / G) = (∑ i in s, w i * z i) : begin refine sum_congr rfl (λ i hi, _), have : q - q / p = 1, by field_simp [hpq.ne_zero, hpq.symm.mul_eq_add], dsimp only [w, z], rw [← div_rpow, mul_left_comm, mul_div_assoc, ← @inv_div _ _ _ G, inv_rpow, ← div_eq_mul_inv, ← rpow_sub']; simp [this] end -- Apply the generalized mean inequality ... ≤ (∑ i in s, w i * (z i) ^ p) ^ (1 / p) : nnreal.arith_mean_le_rpow_mean s w z A (le_of_lt hpq.one_lt) -- Simplify the right hand side. Terms with `g i ≠ 0` are equal to `(f i) ^ p`, -- the others are zeros. ... ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) : begin refine rpow_le_rpow (sum_le_sum (λ i hi, _)) hpq.one_div_nonneg, dsimp only [w, z], rw [mul_rpow, mul_left_comm, ← rpow_mul _ _ p, div_mul_cancel _ hpq.ne_zero, div_rpow, div_mul_div, mul_comm (G ^ q), mul_div_mul_right], { nth_rewrite 1 [← mul_one ((f i) ^ p)], exact canonically_ordered_semiring.mul_le_mul (le_refl _) (div_self_le _) }, { simpa [hpq.symm.ne_zero] using hG } end } 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 canonically_ordered_semiring.mul_le_mul (le_refl _) (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 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, (abs $ f i)^p) ^ (1 / p) * (∑ i in s, (abs $ 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 /-- 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, (abs $ f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (abs $ f i) ^ p) ^ (1 / p) + (∑ i in s, (abs $ 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] /-- 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 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] end real namespace ennreal /-- Young's inequality, `ennreal` version with real conjugate exponents. -/ theorem young_inequality (a b : ennreal) {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 ennreal.div_def}, 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 (nnreal.of_real p) _ (by simp [hpq.pos]), ←@coe_div (nnreal.of_real q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end variables (f g : ι → ennreal) {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 `ennreal`-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 /-- 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 `ennreal` 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 section lintegral /-! ### Hölder's inequality for the Lebesgue integral of ennreal and nnreal functions We prove `∫ (f * g) ∂μ ≤ (∫ f^p ∂μ) ^ (1/p) * (∫ g^q ∂μ) ^ (1/q)` for `p`, `q` conjugate real exponents and `α→(e)nnreal` functions in several cases, the first two being useful only to prove the more general results: * `ennreal.lintegral_mul_le_one_of_lintegral_rpow_eq_one` : ennreal functions for which the integrals on the right are equal to 1, * `ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top` : ennreal functions for which the integrals on the right are neither ⊤ nor 0, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ennreal functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : nnreal functions. -/ open measure_theory variables {α : Type*} [measurable_space α] {μ : measure α} namespace ennreal lemma lintegral_mul_le_one_of_lintegral_rpow_eq_one {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_norm : ∫⁻ a, (f a)^p ∂μ = 1) (hg_norm : ∫⁻ a, (g a)^q ∂μ = 1) : ∫⁻ a, (f * g) a ∂μ ≤ 1 := begin calc ∫⁻ (a : α), ((f * g) a) ∂μ ≤ ∫⁻ (a : α), ((f a)^p / ennreal.of_real p + (g a)^q / ennreal.of_real q) ∂μ : lintegral_mono (λ a, young_inequality (f a) (g a) hpq) ... = 1 : begin simp_rw [div_def], rw lintegral_add', { rw [lintegral_mul_const'' _ hf.ennreal_rpow_const, lintegral_mul_const'' _ hg.ennreal_rpow_const, hf_norm, hg_norm, ←ennreal.div_def, ←ennreal.div_def, hpq.inv_add_inv_conj_ennreal], }, { exact hf.ennreal_rpow_const.ennreal_mul ae_measurable_const, }, { exact hg.ennreal_rpow_const.ennreal_mul ae_measurable_const, }, end end /-- Function multiplied by the inverse of its p-seminorm `(∫⁻ f^p ∂μ) ^ 1/p`-/ def fun_mul_inv_snorm (f : α → ennreal) (p : ℝ) (μ : measure α) : α → ennreal := λ a, (f a) * ((∫⁻ c, (f c) ^ p ∂μ) ^ (1 / p))⁻¹ lemma fun_eq_fun_mul_inv_snorm_mul_snorm {p : ℝ} (f : α → ennreal) (hf_nonzero : ∫⁻ a, (f a) ^ p ∂μ ≠ 0) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) {a : α} : f a = (fun_mul_inv_snorm f p μ a) * (∫⁻ c, (f c)^p ∂μ)^(1/p) := by simp [fun_mul_inv_snorm, mul_assoc, inv_mul_cancel, hf_nonzero, hf_top] lemma fun_mul_inv_snorm_rpow {p : ℝ} (hp0 : 0 < p) {f : α → ennreal} {a : α} : (fun_mul_inv_snorm f p μ a) ^ p = (f a)^p * (∫⁻ c, (f c) ^ p ∂μ)⁻¹ := begin rw [fun_mul_inv_snorm, mul_rpow_of_nonneg _ _ (le_of_lt hp0)], suffices h_inv_rpow : ((∫⁻ (c : α), f c ^ p ∂μ) ^ (1 / p))⁻¹ ^ p = (∫⁻ (c : α), f c ^ p ∂μ)⁻¹, by rw h_inv_rpow, rw [inv_rpow_of_pos hp0, ←rpow_mul, div_eq_mul_inv, one_mul, _root_.inv_mul_cancel (ne_of_lt hp0).symm, rpow_one], end lemma lintegral_rpow_fun_mul_inv_snorm_eq_one {p : ℝ} (hp0_lt : 0 < p) {f : α → ennreal} (hf : ae_measurable f μ) (hf_nonzero : ∫⁻ a, (f a)^p ∂μ ≠ 0) (hf_top : ∫⁻ a, (f a)^p ∂μ ≠ ⊤) : ∫⁻ c, (fun_mul_inv_snorm f p μ c)^p ∂μ = 1 := begin simp_rw fun_mul_inv_snorm_rpow hp0_lt, rw [lintegral_mul_const'' _ hf.ennreal_rpow_const, mul_inv_cancel hf_nonzero hf_top], end /-- Hölder's inequality in case of finite non-zero integrals -/ lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_nontop : ∫⁻ a, (f a)^p ∂μ ≠ ⊤) (hg_nontop : ∫⁻ a, (g a)^q ∂μ ≠ ⊤) (hf_nonzero : ∫⁻ a, (f a)^p ∂μ ≠ 0) (hg_nonzero : ∫⁻ a, (g a)^q ∂μ ≠ 0) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ)^(1/p) * (∫⁻ a, (g a)^q ∂μ)^(1/q) := begin let npf := (∫⁻ (c : α), (f c) ^ p ∂μ) ^ (1/p), let nqg := (∫⁻ (c : α), (g c) ^ q ∂μ) ^ (1/q), calc ∫⁻ (a : α), (f * g) a ∂μ = ∫⁻ (a : α), ((fun_mul_inv_snorm f p μ * fun_mul_inv_snorm g q μ) a) * (npf * nqg) ∂μ : begin refine lintegral_congr (λ a, _), rw [pi.mul_apply, fun_eq_fun_mul_inv_snorm_mul_snorm f hf_nonzero hf_nontop, fun_eq_fun_mul_inv_snorm_mul_snorm g hg_nonzero hg_nontop, pi.mul_apply], ring, end ... ≤ npf * nqg : begin rw lintegral_mul_const' (npf * nqg) _ (by simp [hf_nontop, hg_nontop, hf_nonzero, hg_nonzero]), nth_rewrite 1 ←one_mul (npf * nqg), refine mul_le_mul _ (le_refl (npf * nqg)), have hf1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.pos hf hf_nonzero hf_nontop, have hg1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.symm.pos hg hg_nonzero hg_nontop, exact lintegral_mul_le_one_of_lintegral_rpow_eq_one hpq (hf.ennreal_mul ae_measurable_const) (hg.ennreal_mul ae_measurable_const) hf1 hg1, end end lemma ae_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f : α → ennreal} (hf : ae_measurable f μ) (hf_zero : ∫⁻ a, (f a)^p ∂μ = 0) : f =ᵐ[μ] 0 := begin rw lintegral_eq_zero_iff' hf.ennreal_rpow_const at hf_zero, refine filter.eventually.mp hf_zero (filter.eventually_of_forall (λ x, _)), dsimp only, rw [pi.zero_apply, rpow_eq_zero_iff], intro hx, cases hx, { exact hx.left, }, { exfalso, linarith, }, end lemma lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f g : α → ennreal} (hf : ae_measurable f μ) (hf_zero : ∫⁻ a, (f a)^p ∂μ = 0) : ∫⁻ a, (f * g) a ∂μ = 0 := begin rw ←@lintegral_zero_fun α _ μ, refine lintegral_congr_ae _, suffices h_mul_zero : f * g =ᵐ[μ] 0 * g , by rwa zero_mul at h_mul_zero, have hf_eq_zero : f =ᵐ[μ] 0, from ae_eq_zero_of_lintegral_rpow_eq_zero hp0_lt hf hf_zero, exact filter.eventually_eq.mul hf_eq_zero (ae_eq_refl g), end lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top {p q : ℝ} (hp0_lt : 0 < p) (hq0 : 0 ≤ q) {f g : α → ennreal} (hf_top : ∫⁻ a, (f a)^p ∂μ = ⊤) (hg_nonzero : ∫⁻ a, (g a)^q ∂μ ≠ 0) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^q ∂μ) ^ (1/q) := begin refine le_trans le_top (le_of_eq _), have hp0_inv_lt : 0 < 1/p, by simp [hp0_lt], rw [hf_top, ennreal.top_rpow_of_pos hp0_inv_lt], simp [hq0, hg_nonzero], end /-- Hölder's inequality for functions `α → ennreal`. The integral of the product of two functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem lintegral_mul_le_Lp_mul_Lq (μ : measure α) {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^q ∂μ) ^ (1/q) := begin by_cases hf_zero : ∫⁻ a, (f a) ^ p ∂μ = 0, { refine le_trans (le_of_eq _) (zero_le _), exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.pos hf hf_zero, }, by_cases hg_zero : ∫⁻ a, (g a) ^ q ∂μ = 0, { refine le_trans (le_of_eq _) (zero_le _), rw mul_comm, exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.symm.pos hg hg_zero, }, by_cases hf_top : ∫⁻ a, (f a) ^ p ∂μ = ⊤, { exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.pos hpq.symm.nonneg hf_top hg_zero, }, by_cases hg_top : ∫⁻ a, (g a) ^ q ∂μ = ⊤, { rw [mul_comm, mul_comm ((∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p))], exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.symm.pos hpq.nonneg hg_top hf_zero, }, -- non-⊤ non-zero case exact ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top hpq hf hg hf_top hg_top hf_zero hg_zero, end lemma lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top {p : ℝ} {f g : α → ennreal} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ < ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ < ⊤) (hp1 : 1 ≤ p) : ∫⁻ a, ((f + g) a) ^ p ∂μ < ⊤ := begin have hp0_lt : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, have hp0 : 0 ≤ p, from le_of_lt hp0_lt, calc ∫⁻ (a : α), (f a + g a) ^ p ∂μ ≤ ∫⁻ a, ((2:ennreal)^(p-1) * (f a) ^ p + (2:ennreal)^(p-1) * (g a) ^ p) ∂ μ : begin refine lintegral_mono (λ a, _), dsimp only, have h_zero_lt_half_rpow : (0 : ennreal) < (1 / 2) ^ p, { rw [←ennreal.zero_rpow_of_pos hp0_lt], exact ennreal.rpow_lt_rpow (by simp [zero_lt_one]) hp0_lt, }, have h_rw : (1 / 2) ^ p * (2:ennreal) ^ (p - 1) = 1 / 2, { rw [sub_eq_add_neg, ennreal.rpow_add _ _ ennreal.two_ne_zero ennreal.coe_ne_top, ←mul_assoc, ←ennreal.mul_rpow_of_nonneg _ _ hp0, ennreal.div_def, one_mul, ennreal.inv_mul_cancel ennreal.two_ne_zero ennreal.coe_ne_top, ennreal.one_rpow, one_mul, ennreal.rpow_neg_one], }, rw ←ennreal.mul_le_mul_left (ne_of_lt h_zero_lt_half_rpow).symm _, { rw [mul_add, ← mul_assoc, ← mul_assoc, h_rw, ←ennreal.mul_rpow_of_nonneg _ _ hp0, mul_add], refine ennreal.rpow_arith_mean_le_arith_mean2_rpow (1/2 : ennreal) (1/2 : ennreal) (f a) (g a) _ hp1, rw [ennreal.div_add_div_same, one_add_one_eq_two, ennreal.div_self ennreal.two_ne_zero ennreal.coe_ne_top], }, { rw ←ennreal.lt_top_iff_ne_top, refine ennreal.rpow_lt_top_of_nonneg hp0 _, rw [ennreal.div_def, one_mul, ennreal.inv_ne_top], exact ennreal.two_ne_zero, }, end ... < ⊤ : begin rw [lintegral_add', lintegral_const_mul'' _ hf.ennreal_rpow_const, lintegral_const_mul'' _ hg.ennreal_rpow_const, ennreal.add_lt_top], { have h_two : (2 : ennreal) ^ (p - 1) < ⊤, from ennreal.rpow_lt_top_of_nonneg (by simp [hp1]) ennreal.coe_ne_top, repeat {rw ennreal.mul_lt_top_iff}, simp [hf_top, hg_top, h_two], }, { exact (ennreal.continuous_const_mul (by simp)).measurable.comp_ae_measurable hf.ennreal_rpow_const, }, { exact (ennreal.continuous_const_mul (by simp)).measurable.comp_ae_measurable hg.ennreal_rpow_const }, end end lemma lintegral_Lp_mul_le_Lq_mul_Lr {α} [measurable_space α] {p q r : ℝ} (hp0_lt : 0 < p) (hpq : p < q) (hpqr : 1/p = 1/q + 1/r) (μ : measure α) {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : (∫⁻ a, ((f * g) a)^p ∂μ) ^ (1/p) ≤ (∫⁻ a, (f a)^q ∂μ) ^ (1/q) * (∫⁻ a, (g a)^r ∂μ) ^ (1/r) := begin have hp0_ne : p ≠ 0, from (ne_of_lt hp0_lt).symm, have hp0 : 0 ≤ p, from le_of_lt hp0_lt, have hq0_lt : 0 < q, from lt_of_le_of_lt hp0 hpq, have hq0_ne : q ≠ 0, from (ne_of_lt hq0_lt).symm, have h_one_div_r : 1/r = 1/p - 1/q, by simp [hpqr], have hr0_ne : r ≠ 0, { have hr_inv_pos : 0 < 1/r, by rwa [h_one_div_r, sub_pos, one_div_lt_one_div hq0_lt hp0_lt], rw [one_div, _root_.inv_pos] at hr_inv_pos, exact (ne_of_lt hr_inv_pos).symm, }, let p2 := q/p, let q2 := p2.conjugate_exponent, have hp2q2 : p2.is_conjugate_exponent q2, from real.is_conjugate_exponent_conjugate_exponent (by simp [lt_div_iff, hpq, hp0_lt]), calc (∫⁻ (a : α), ((f * g) a) ^ p ∂μ) ^ (1 / p) = (∫⁻ (a : α), (f a)^p * (g a)^p ∂μ) ^ (1 / p) : by simp_rw [pi.mul_apply, ennreal.mul_rpow_of_nonneg _ _ hp0] ... ≤ ((∫⁻ a, (f a)^(p * p2) ∂ μ)^(1/p2) * (∫⁻ a, (g a)^(p * q2) ∂ μ)^(1/q2)) ^ (1/p) : begin refine ennreal.rpow_le_rpow _ (by simp [hp0]), simp_rw ennreal.rpow_mul, exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hp2q2 hf.ennreal_rpow_const hg.ennreal_rpow_const, end ... = (∫⁻ (a : α), (f a) ^ q ∂μ) ^ (1 / q) * (∫⁻ (a : α), (g a) ^ r ∂μ) ^ (1 / r) : begin rw [@ennreal.mul_rpow_of_nonneg _ _ (1/p) (by simp [hp0]), ←ennreal.rpow_mul, ←ennreal.rpow_mul], have hpp2 : p * p2 = q, { symmetry, rw [mul_comm, ←div_eq_iff hp0_ne], }, have hpq2 : p * q2 = r, { rw [← inv_inv' r, ← one_div, ← one_div, h_one_div_r], field_simp [q2, real.conjugate_exponent, p2, hp0_ne, hq0_ne] }, simp_rw [div_mul_div, mul_one, mul_comm p2, mul_comm q2, hpp2, hpq2], end end lemma lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) : ∫⁻ a, (f a) * (g a) ^ (p - 1) ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^p ∂μ) ^ (1/q) := begin refine le_trans (ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf hg.ennreal_rpow_const) _, by_cases hf_zero_rpow : (∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p) = 0, { rw [hf_zero_rpow, zero_mul], exact zero_le _, }, have hf_top_rpow : (∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p) ≠ ⊤, { by_contra h, push_neg at h, refine hf_top _, have hp_not_neg : ¬ p < 0, by simp [hpq.nonneg], simpa [hpq.pos, hp_not_neg] using h, }, refine (ennreal.mul_le_mul_left hf_zero_rpow hf_top_rpow).mpr (le_of_eq _), congr, ext1 a, rw [←ennreal.rpow_mul, hpq.sub_one_mul_conj], end lemma lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ ≠ ⊤) : ∫⁻ a, ((f + g) a)^p ∂ μ ≤ ((∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p ∂μ) ^ (1/q) := begin calc ∫⁻ a, ((f+g) a) ^ p ∂μ ≤ ∫⁻ a, ((f + g) a) * ((f + g) a) ^ (p - 1) ∂μ : begin refine lintegral_mono (λ a, _), dsimp only, by_cases h_zero : (f + g) a = 0, { rw [h_zero, ennreal.zero_rpow_of_pos hpq.pos], exact zero_le _, }, by_cases h_top : (f + g) a = ⊤, { rw [h_top, ennreal.top_rpow_of_pos hpq.sub_one_pos, ennreal.top_mul_top], exact le_top, }, refine le_of_eq _, nth_rewrite 1 ←ennreal.rpow_one ((f + g) a), rw [←ennreal.rpow_add _ _ h_zero h_top, add_sub_cancel'_right], end ... = ∫⁻ (a : α), f a * (f + g) a ^ (p - 1) ∂μ + ∫⁻ (a : α), g a * (f + g) a ^ (p - 1) ∂μ : begin have h_add_m : ae_measurable (λ (a : α), ((f + g) a) ^ (p-1)) μ, from (hf.add hg).ennreal_rpow_const, have h_add_apply : ∫⁻ (a : α), (f + g) a * (f + g) a ^ (p - 1) ∂μ = ∫⁻ (a : α), (f a + g a) * (f + g) a ^ (p - 1) ∂μ, from rfl, simp_rw [h_add_apply, add_mul], rw lintegral_add' (hf.ennreal_mul h_add_m) (hg.ennreal_mul h_add_m), end ... ≤ ((∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p ∂μ) ^ (1/q) : begin rw add_mul, exact add_le_add (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hf (hf.add hg) hf_top) (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hg (hf.add hg) hg_top), end end private lemma lintegral_Lp_add_le_aux {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ennreal} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ ≠ ⊤) (h_add_zero : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ 0) (h_add_top : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ ⊤) : (∫⁻ a, ((f + g) a)^p ∂ μ) ^ (1/p) ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p) := begin have hp_not_nonpos : ¬ p ≤ 0, by simp [hpq.pos], have htop_rpow : (∫⁻ a, ((f+g) a) ^ p ∂μ)^(1/p) ≠ ⊤, { by_contra h, push_neg at h, exact h_add_top (@ennreal.rpow_eq_top_of_nonneg _ (1/p) (by simp [hpq.nonneg]) h), }, have h0_rpow : (∫⁻ a, ((f+g) a) ^ p ∂ μ) ^ (1/p) ≠ 0, by simp [h_add_zero, h_add_top, hpq.nonneg, hp_not_nonpos, -pi.add_apply], suffices h : 1 ≤ (∫⁻ (a : α), ((f+g) a)^p ∂μ) ^ -(1/p) * ((∫⁻ (a : α), (f a)^p ∂μ) ^ (1/p) + (∫⁻ (a : α), (g a)^p ∂μ) ^ (1/p)), by rwa [←mul_le_mul_left h0_rpow htop_rpow, ←mul_assoc, ←rpow_add _ _ h_add_zero h_add_top, ←sub_eq_add_neg, _root_.sub_self, rpow_zero, one_mul, mul_one] at h, have h : ∫⁻ (a : α), ((f+g) a)^p ∂μ ≤ ((∫⁻ (a : α), (f a)^p ∂μ) ^ (1/p) + (∫⁻ (a : α), (g a)^p ∂μ) ^ (1/p)) * (∫⁻ (a : α), ((f+g) a)^p ∂μ) ^ (1/q), from lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add hpq hf hf_top hg hg_top, have h_one_div_q : 1/q = 1 - 1/p, by { nth_rewrite 1 ←hpq.inv_add_inv_conj, ring, }, simp_rw [h_one_div_q, sub_eq_add_neg 1 (1/p), ennreal.rpow_add _ _ h_add_zero h_add_top, rpow_one] at h, nth_rewrite 1 mul_comm at h, nth_rewrite 0 ←one_mul (∫⁻ (a : α), ((f+g) a) ^ p ∂μ) at h, rwa [←mul_assoc, ennreal.mul_le_mul_right h_add_zero h_add_top, mul_comm] at h, end /-- Minkowski's inequality for functions `α → ennreal`: the `ℒp` seminorm of the sum of two functions is bounded by the sum of their `ℒp` seminorms. -/ theorem lintegral_Lp_add_le {p : ℝ} {f g : α → ennreal} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hp1 : 1 ≤ p) : (∫⁻ a, ((f + g) a)^p ∂ μ) ^ (1/p) ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, by_cases hf_top : ∫⁻ a, (f a) ^ p ∂μ = ⊤, { simp [hf_top, hp_pos], }, by_cases hg_top : ∫⁻ a, (g a) ^ p ∂μ = ⊤, { simp [hg_top, hp_pos], }, by_cases h1 : p = 1, { refine le_of_eq _, simp_rw [h1, one_div_one, ennreal.rpow_one], exact lintegral_add' hf hg, }, have hp1_lt : 1 < p, by { refine lt_of_le_of_ne hp1 _, symmetry, exact h1, }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp1_lt, by_cases h0 : ∫⁻ a, ((f+g) a) ^ p ∂ μ = 0, { rw [h0, @ennreal.zero_rpow_of_pos (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1])], exact zero_le _, }, have htop : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ ⊤, { rw ←ne.def at hf_top hg_top, rw ←ennreal.lt_top_iff_ne_top at hf_top hg_top ⊢, exact lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top hf hf_top hg hg_top hp1, }, exact lintegral_Lp_add_le_aux hpq hf hf_top hg hg_top h0 htop, end end ennreal /-- Hölder's inequality for functions `α → ℝ≥0`. The integral of the product of two functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem nnreal.lintegral_mul_le_Lp_mul_Lq {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ)^(1/p) * (∫⁻ a, (g a)^q ∂μ)^(1/q) := begin simp_rw [pi.mul_apply, ennreal.coe_mul], exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf.ennreal_coe hg.ennreal_coe, end end lintegral
abf71909462274bd67de105e265796740a4e152e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/group/prod.lean
3b56ea83810f1fc5223095542e4f8fadba896ba6
[]
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
12,990
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group.hom import Mathlib.data.equiv.mul_add import Mathlib.data.prod import Mathlib.PostPort universes u_5 u_6 u_3 u_4 u_1 u_2 u_7 u_8 u_9 namespace Mathlib /-! # Monoid, group etc structures on `M × N` In this file we define one-binop (`monoid`, `group` etc) structures on `M × N`. We also prove trivial `simp` lemmas, and define the following operations on `monoid_hom`s: * `fst M N : M × N →* M`, `snd M N : M × N →* N`: projections `prod.fst` and `prod.snd` as `monoid_hom`s; * `inl M N : M →* M × N`, `inr M N : N →* M × N`: inclusions of first/second monoid into the product; * `f.prod g : `M →* N × P`: sends `x` to `(f x, g x)`; * `f.coprod g : M × N →* P`: sends `(x, y)` to `f x * g y`; * `f.prod_map g : M × N → M' × N'`: `prod.map f g` as a `monoid_hom`, sends `(x, y)` to `(f x, g y)`. -/ namespace prod protected instance has_add {M : Type u_5} {N : Type u_6} [Add M] [Add N] : Add (M × N) := { add := fun (p q : M × N) => (fst p + fst q, snd p + snd q) } @[simp] theorem fst_add {M : Type u_5} {N : Type u_6} [Add M] [Add N] (p : M × N) (q : M × N) : fst (p + q) = fst p + fst q := rfl @[simp] theorem snd_add {M : Type u_5} {N : Type u_6} [Add M] [Add N] (p : M × N) (q : M × N) : snd (p + q) = snd p + snd q := rfl @[simp] theorem mk_add_mk {M : Type u_5} {N : Type u_6} [Add M] [Add N] (a₁ : M) (a₂ : M) (b₁ : N) (b₂ : N) : (a₁, b₁) + (a₂, b₂) = (a₁ + a₂, b₁ + b₂) := rfl protected instance has_one {M : Type u_5} {N : Type u_6} [HasOne M] [HasOne N] : HasOne (M × N) := { one := (1, 1) } @[simp] theorem fst_zero {M : Type u_5} {N : Type u_6} [HasZero M] [HasZero N] : fst 0 = 0 := rfl @[simp] theorem snd_one {M : Type u_5} {N : Type u_6} [HasOne M] [HasOne N] : snd 1 = 1 := rfl theorem zero_eq_mk {M : Type u_5} {N : Type u_6} [HasZero M] [HasZero N] : 0 = (0, 0) := rfl @[simp] theorem mk_eq_zero {M : Type u_5} {N : Type u_6} [HasZero M] [HasZero N] {x : M} {y : N} : (x, y) = 0 ↔ x = 0 ∧ y = 0 := mk.inj_iff theorem fst_mul_snd {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] (p : M × N) : (fst p, 1) * (1, snd p) = p := ext (mul_one (fst p)) (one_mul (snd p)) protected instance has_inv {M : Type u_5} {N : Type u_6} [has_inv M] [has_inv N] : has_inv (M × N) := has_inv.mk fun (p : M × N) => (fst p⁻¹, snd p⁻¹) @[simp] theorem fst_inv {G : Type u_3} {H : Type u_4} [has_inv G] [has_inv H] (p : G × H) : fst (p⁻¹) = (fst p⁻¹) := rfl @[simp] theorem snd_neg {G : Type u_3} {H : Type u_4} [Neg G] [Neg H] (p : G × H) : snd (-p) = -snd p := rfl @[simp] theorem inv_mk {G : Type u_3} {H : Type u_4} [has_inv G] [has_inv H] (a : G) (b : H) : (a, b)⁻¹ = (a⁻¹, b⁻¹) := rfl protected instance has_sub {M : Type u_5} {N : Type u_6} [Sub M] [Sub N] : Sub (M × N) := { sub := fun (p q : M × N) => (fst p - fst q, snd p - snd q) } @[simp] theorem fst_sub {A : Type u_1} {B : Type u_2} [add_group A] [add_group B] (a : A × B) (b : A × B) : fst (a - b) = fst a - fst b := rfl @[simp] theorem snd_sub {A : Type u_1} {B : Type u_2} [add_group A] [add_group B] (a : A × B) (b : A × B) : snd (a - b) = snd a - snd b := rfl @[simp] theorem mk_sub_mk {A : Type u_1} {B : Type u_2} [add_group A] [add_group B] (x₁ : A) (x₂ : A) (y₁ : B) (y₂ : B) : (x₁, y₁) - (x₂, y₂) = (x₁ - x₂, y₁ - y₂) := rfl protected instance semigroup {M : Type u_5} {N : Type u_6} [semigroup M] [semigroup N] : semigroup (M × N) := semigroup.mk Mul.mul sorry protected instance add_monoid {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : add_monoid (M × N) := add_monoid.mk add_semigroup.add sorry 0 sorry sorry protected instance group {G : Type u_3} {H : Type u_4} [group G] [group H] : group (G × H) := group.mk monoid.mul sorry monoid.one sorry sorry has_inv.inv Div.div sorry protected instance add_comm_semigroup {G : Type u_3} {H : Type u_4} [add_comm_semigroup G] [add_comm_semigroup H] : add_comm_semigroup (G × H) := add_comm_semigroup.mk add_semigroup.add sorry sorry protected instance left_cancel_semigroup {G : Type u_3} {H : Type u_4} [left_cancel_semigroup G] [left_cancel_semigroup H] : left_cancel_semigroup (G × H) := left_cancel_semigroup.mk semigroup.mul sorry sorry protected instance right_cancel_semigroup {G : Type u_3} {H : Type u_4} [right_cancel_semigroup G] [right_cancel_semigroup H] : right_cancel_semigroup (G × H) := right_cancel_semigroup.mk semigroup.mul sorry sorry protected instance add_comm_monoid {M : Type u_5} {N : Type u_6} [add_comm_monoid M] [add_comm_monoid N] : add_comm_monoid (M × N) := add_comm_monoid.mk add_comm_semigroup.add sorry add_monoid.zero sorry sorry sorry protected instance comm_group {G : Type u_3} {H : Type u_4} [comm_group G] [comm_group H] : comm_group (G × H) := comm_group.mk comm_semigroup.mul sorry group.one sorry sorry group.inv group.div sorry sorry end prod namespace monoid_hom /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `M`.-/ def fst (M : Type u_5) (N : Type u_6) [monoid M] [monoid N] : M × N →* M := mk prod.fst sorry sorry /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `N`.-/ def Mathlib.add_monoid_hom.snd (M : Type u_5) (N : Type u_6) [add_monoid M] [add_monoid N] : M × N →+ N := add_monoid_hom.mk prod.snd sorry sorry /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `M` to `M × N`. -/ def Mathlib.add_monoid_hom.inl (M : Type u_5) (N : Type u_6) [add_monoid M] [add_monoid N] : M →+ M × N := add_monoid_hom.mk (fun (x : M) => (x, 0)) sorry sorry /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `N` to `M × N`. -/ def Mathlib.add_monoid_hom.inr (M : Type u_5) (N : Type u_6) [add_monoid M] [add_monoid N] : N →+ M × N := add_monoid_hom.mk (fun (y : N) => (0, y)) sorry sorry @[simp] theorem Mathlib.add_monoid_hom.coe_fst {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : ⇑(add_monoid_hom.fst M N) = prod.fst := rfl @[simp] theorem Mathlib.add_monoid_hom.coe_snd {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : ⇑(add_monoid_hom.snd M N) = prod.snd := rfl @[simp] theorem inl_apply {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] (x : M) : coe_fn (inl M N) x = (x, 1) := rfl @[simp] theorem inr_apply {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] (y : N) : coe_fn (inr M N) y = (1, y) := rfl @[simp] theorem Mathlib.add_monoid_hom.fst_comp_inl {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : add_monoid_hom.comp (add_monoid_hom.fst M N) (add_monoid_hom.inl M N) = add_monoid_hom.id M := rfl @[simp] theorem Mathlib.add_monoid_hom.snd_comp_inl {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : add_monoid_hom.comp (add_monoid_hom.snd M N) (add_monoid_hom.inl M N) = 0 := rfl @[simp] theorem Mathlib.add_monoid_hom.fst_comp_inr {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : add_monoid_hom.comp (add_monoid_hom.fst M N) (add_monoid_hom.inr M N) = 0 := rfl @[simp] theorem Mathlib.add_monoid_hom.snd_comp_inr {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] : add_monoid_hom.comp (add_monoid_hom.snd M N) (add_monoid_hom.inr M N) = add_monoid_hom.id N := rfl /-- Combine two `monoid_hom`s `f : M →* N`, `g : M →* P` into `f.prod g : M →* N × P` given by `(f.prod g) x = (f x, g x)` -/ protected def prod {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [monoid P] (f : M →* N) (g : M →* P) : M →* N × P := mk (fun (x : M) => (coe_fn f x, coe_fn g x)) sorry sorry @[simp] theorem prod_apply {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [monoid P] (f : M →* N) (g : M →* P) (x : M) : coe_fn (monoid_hom.prod f g) x = (coe_fn f x, coe_fn g x) := rfl @[simp] theorem fst_comp_prod {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [monoid P] (f : M →* N) (g : M →* P) : comp (fst N P) (monoid_hom.prod f g) = f := ext fun (x : M) => rfl @[simp] theorem snd_comp_prod {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [monoid P] (f : M →* N) (g : M →* P) : comp (snd N P) (monoid_hom.prod f g) = g := ext fun (x : M) => rfl @[simp] theorem Mathlib.add_monoid_hom.prod_unique {M : Type u_5} {N : Type u_6} {P : Type u_7} [add_monoid M] [add_monoid N] [add_monoid P] (f : M →+ N × P) : add_monoid_hom.prod (add_monoid_hom.comp (add_monoid_hom.fst N P) f) (add_monoid_hom.comp (add_monoid_hom.snd N P) f) = f := sorry /-- `prod.map` as a `monoid_hom`. -/ def Mathlib.add_monoid_hom.prod_map {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] {M' : Type u_8} {N' : Type u_9} [add_monoid M'] [add_monoid N'] (f : M →+ M') (g : N →+ N') : M × N →+ M' × N' := add_monoid_hom.prod (add_monoid_hom.comp f (add_monoid_hom.fst M N)) (add_monoid_hom.comp g (add_monoid_hom.snd M N)) theorem Mathlib.add_monoid_hom.prod_map_def {M : Type u_5} {N : Type u_6} [add_monoid M] [add_monoid N] {M' : Type u_8} {N' : Type u_9} [add_monoid M'] [add_monoid N'] (f : M →+ M') (g : N →+ N') : add_monoid_hom.prod_map f g = add_monoid_hom.prod (add_monoid_hom.comp f (add_monoid_hom.fst M N)) (add_monoid_hom.comp g (add_monoid_hom.snd M N)) := rfl @[simp] theorem coe_prod_map {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] {M' : Type u_8} {N' : Type u_9} [monoid M'] [monoid N'] (f : M →* M') (g : N →* N') : ⇑(prod_map f g) = prod.map ⇑f ⇑g := rfl theorem prod_comp_prod_map {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] {M' : Type u_8} {N' : Type u_9} [monoid M'] [monoid N'] [monoid P] (f : P →* M) (g : P →* N) (f' : M →* M') (g' : N →* N') : comp (prod_map f' g') (monoid_hom.prod f g) = monoid_hom.prod (comp f' f) (comp g' g) := rfl /-- Coproduct of two `monoid_hom`s with the same codomain: `f.coprod g (p : M × N) = f p.1 * g p.2`. -/ def coprod {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [comm_monoid P] (f : M →* P) (g : N →* P) : M × N →* P := comp f (fst M N) * comp g (snd M N) @[simp] theorem coprod_apply {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [comm_monoid P] (f : M →* P) (g : N →* P) (p : M × N) : coe_fn (coprod f g) p = coe_fn f (prod.fst p) * coe_fn g (prod.snd p) := rfl @[simp] theorem Mathlib.add_monoid_hom.coprod_comp_inl {M : Type u_5} {N : Type u_6} {P : Type u_7} [add_monoid M] [add_monoid N] [add_comm_monoid P] (f : M →+ P) (g : N →+ P) : add_monoid_hom.comp (add_monoid_hom.coprod f g) (add_monoid_hom.inl M N) = f := sorry @[simp] theorem Mathlib.add_monoid_hom.coprod_comp_inr {M : Type u_5} {N : Type u_6} {P : Type u_7} [add_monoid M] [add_monoid N] [add_comm_monoid P] (f : M →+ P) (g : N →+ P) : add_monoid_hom.comp (add_monoid_hom.coprod f g) (add_monoid_hom.inr M N) = g := sorry @[simp] theorem Mathlib.add_monoid_hom.coprod_unique {M : Type u_5} {N : Type u_6} {P : Type u_7} [add_monoid M] [add_monoid N] [add_comm_monoid P] (f : M × N →+ P) : add_monoid_hom.coprod (add_monoid_hom.comp f (add_monoid_hom.inl M N)) (add_monoid_hom.comp f (add_monoid_hom.inr M N)) = f := sorry @[simp] theorem coprod_inl_inr {M : Type u_1} {N : Type u_2} [comm_monoid M] [comm_monoid N] : coprod (inl M N) (inr M N) = id (M × N) := coprod_unique (id (M × N)) theorem comp_coprod {M : Type u_5} {N : Type u_6} {P : Type u_7} [monoid M] [monoid N] [comm_monoid P] {Q : Type u_1} [comm_monoid Q] (h : P →* Q) (f : M →* P) (g : N →* P) : comp h (coprod f g) = coprod (comp h f) (comp h g) := sorry end monoid_hom namespace mul_equiv /-- The equivalence between `M × N` and `N × M` given by swapping the components is multiplicative. -/ def prod_comm {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] : M × N ≃* N × M := mk (equiv.to_fun (equiv.prod_comm M N)) (equiv.inv_fun (equiv.prod_comm M N)) sorry sorry sorry @[simp] theorem coe_prod_comm {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] : ⇑prod_comm = prod.swap := rfl @[simp] theorem coe_prod_comm_symm {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] : ⇑(symm prod_comm) = prod.swap := rfl /-- The monoid equivalence between units of a product of two monoids, and the product of the units of each monoid. -/ def prod_units {M : Type u_5} {N : Type u_6} [monoid M] [monoid N] : units (M × N) ≃* units M × units N := mk (⇑(monoid_hom.prod (units.map (monoid_hom.fst M N)) (units.map (monoid_hom.snd M N)))) (fun (u : units M × units N) => units.mk (↑(prod.fst u), ↑(prod.snd u)) (↑(prod.fst u⁻¹), ↑(prod.snd u⁻¹)) sorry sorry) sorry sorry sorry
21a3f721be5ad1ec2bd001c35fc290bedf2f0162
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/computability/primrec.lean
98f60dbe6fa77ab826c72dc8d04309229a127c08
[ "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
52,604
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import logic.equiv.array import logic.equiv.list import logic.function.iterate /-! # The primitive recursive functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The primitive recursive functions are the least collection of functions `nat → nat` which are closed under projections (using the mkpair pairing function), composition, zero, successor, and primitive recursion (i.e. nat.rec where the motive is C n := nat). We can extend this definition to a large class of basic types by using canonical encodings of types as natural numbers (Gödel numbering), which we implement through the type class `encodable`. (More precisely, we need that the composition of encode with decode yields a primitive recursive function, so we have the `primcodable` type class for this.) ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open denumerable encodable function namespace nat /-- The non-dependent recursor on naturals. -/ def elim {C : Sort*} : C → (ℕ → C → C) → ℕ → C := @nat.rec (λ _, C) @[simp] theorem elim_zero {C} (a f) : @nat.elim C a f 0 = a := rfl @[simp] theorem elim_succ {C} (a f n) : @nat.elim C a f (succ n) = f n (nat.elim a f n) := rfl /-- Cases on whether the input is 0 or a successor. -/ def cases {C : Sort*} (a : C) (f : ℕ → C) : ℕ → C := nat.elim a (λ n _, f n) @[simp] theorem cases_zero {C} (a f) : @nat.cases C a f 0 = a := rfl @[simp] theorem cases_succ {C} (a f n) : @nat.cases C a f (succ n) = f n := rfl /-- Calls the given function on a pair of entries `n`, encoded via the pairing function. -/ @[simp, reducible] def unpaired {α} (f : ℕ → ℕ → α) (n : ℕ) : α := f n.unpair.1 n.unpair.2 /-- The primitive recursive functions `ℕ → ℕ`. -/ inductive primrec : (ℕ → ℕ) → Prop | zero : primrec (λ n, 0) | succ : primrec succ | left : primrec (λ n, n.unpair.1) | right : primrec (λ n, n.unpair.2) | pair {f g} : primrec f → primrec g → primrec (λ n, mkpair (f n) (g n)) | comp {f g} : primrec f → primrec g → primrec (λ n, f (g n)) | prec {f g} : primrec f → primrec g → primrec (unpaired (λ z n, n.elim (f z) (λ y IH, g $ mkpair z $ mkpair y IH))) namespace primrec theorem of_eq {f g : ℕ → ℕ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const : ∀ (n : ℕ), primrec (λ _, n) | 0 := zero | (n+1) := succ.comp (const n) protected theorem id : primrec id := (left.pair right).of_eq $ λ n, by simp theorem prec1 {f} (m : ℕ) (hf : primrec f) : primrec (λ n, n.elim m (λ y IH, f $ mkpair y IH)) := ((prec (const m) (hf.comp right)).comp (zero.pair primrec.id)).of_eq $ λ n, by simp theorem cases1 {f} (m : ℕ) (hf : primrec f) : primrec (nat.cases m f) := (prec1 m (hf.comp left)).of_eq $ by simp [cases] theorem cases {f g} (hf : primrec f) (hg : primrec g) : primrec (unpaired (λ z n, n.cases (f z) (λ y, g $ mkpair z y))) := (prec hf (hg.comp (pair left (left.comp right)))).of_eq $ by simp [cases] protected theorem swap : primrec (unpaired (swap mkpair)) := (pair right left).of_eq $ λ n, by simp theorem swap' {f} (hf : primrec (unpaired f)) : primrec (unpaired (swap f)) := (hf.comp primrec.swap).of_eq $ λ n, by simp theorem pred : primrec pred := (cases1 0 primrec.id).of_eq $ λ n, by cases n; simp * theorem add : primrec (unpaired (+)) := (prec primrec.id ((succ.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, add_succ] theorem sub : primrec (unpaired has_sub.sub) := (prec primrec.id ((pred.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, sub_succ] theorem mul : primrec (unpaired (*)) := (prec zero (add.comp (pair left (right.comp right)))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, mul_succ, add_comm] theorem pow : primrec (unpaired (^)) := (prec (const 1) (mul.comp (pair (right.comp right) left))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, pow_succ'] end primrec end nat /-- A `primcodable` type is an `encodable` type for which the encode/decode functions are primitive recursive. -/ class primcodable (α : Type*) extends encodable α := (prim [] : nat.primrec (λ n, encodable.encode (decode n))) namespace primcodable open nat.primrec @[priority 10] instance of_denumerable (α) [denumerable α] : primcodable α := ⟨succ.of_eq $ by simp⟩ /-- Builds a `primcodable` instance from an equivalence to a `primcodable` type. -/ def of_equiv (α) {β} [primcodable α] (e : β ≃ α) : primcodable β := { prim := (primcodable.prim α).of_eq $ λ n, show encode (decode α n) = (option.cases_on (option.map e.symm (decode α n)) 0 (λ a, nat.succ (encode (e a))) : ℕ), by cases decode α n; dsimp; simp, ..encodable.of_equiv α e } instance empty : primcodable empty := ⟨zero⟩ instance unit : primcodable punit := ⟨(cases1 1 zero).of_eq $ λ n, by cases n; simp⟩ instance option {α : Type*} [h : primcodable α] : primcodable (option α) := ⟨(cases1 1 ((cases1 0 (succ.comp succ)).comp (primcodable.prim α))).of_eq $ λ n, by cases n; simp; cases decode α n; refl⟩ instance bool : primcodable bool := ⟨(cases1 1 (cases1 2 zero)).of_eq $ λ n, begin cases n, {refl}, cases n, {refl}, rw decode_ge_two, {refl}, exact dec_trivial end⟩ end primcodable /-- `primrec f` means `f` is primitive recursive (after encoding its input and output as natural numbers). -/ def primrec {α β} [primcodable α] [primcodable β] (f : α → β) : Prop := nat.primrec (λ n, encode ((decode α n).map f)) namespace primrec variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec protected theorem encode : primrec (@encode α _) := (primcodable.prim α).of_eq $ λ n, by cases decode α n; refl protected theorem decode : primrec (decode α) := succ.comp (primcodable.prim α) theorem dom_denumerable {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ nat.primrec (λ n, encode (f (of_nat α n))) := ⟨λ h, (pred.comp h).of_eq $ λ n, by simp; refl, λ h, (succ.comp h).of_eq $ λ n, by simp; refl⟩ theorem nat_iff {f : ℕ → ℕ} : primrec f ↔ nat.primrec f := dom_denumerable theorem encdec : primrec (λ n, encode (decode α n)) := nat_iff.2 (primcodable.prim α) theorem option_some : primrec (@some α) := ((cases1 0 (succ.comp succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp theorem of_eq {f g : α → σ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const (x : σ) : primrec (λ a : α, x) := ((cases1 0 (const (encode x).succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; refl protected theorem id : primrec (@id α) := (primcodable.prim α).of_eq $ by simp theorem comp {f : β → σ} {g : α → β} (hf : primrec f) (hg : primrec g) : primrec (λ a, f (g a)) := ((cases1 0 (hf.comp $ pred.comp hg)).comp (primcodable.prim α)).of_eq $ λ n, begin cases decode α n, {refl}, simp [encodek] end theorem succ : primrec nat.succ := nat_iff.2 nat.primrec.succ theorem pred : primrec nat.pred := nat_iff.2 nat.primrec.pred theorem encode_iff {f : α → σ} : primrec (λ a, encode (f a)) ↔ primrec f := ⟨λ h, nat.primrec.of_eq h $ λ n, by cases decode α n; refl, primrec.encode.comp⟩ theorem of_nat_iff {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ primrec (λ n, f (of_nat α n)) := dom_denumerable.trans $ nat_iff.symm.trans encode_iff protected theorem of_nat (α) [denumerable α] : primrec (of_nat α) := of_nat_iff.1 primrec.id theorem option_some_iff {f : α → σ} : primrec (λ a, some (f a)) ↔ primrec f := ⟨λ h, encode_iff.1 $ pred.comp $ encode_iff.2 h, option_some.comp⟩ theorem of_equiv {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e := by letI : primcodable β := primcodable.of_equiv α e; exact encode_iff.1 primrec.encode theorem of_equiv_symm {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e.symm := by letI := primcodable.of_equiv α e; exact encode_iff.1 (show primrec (λ a, encode (e (e.symm a))), by simp [primrec.encode]) theorem of_equiv_iff {β} (e : β ≃ α) {f : σ → β} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv_symm.comp h).of_eq (λ a, by simp), of_equiv.comp⟩ theorem of_equiv_symm_iff {β} (e : β ≃ α) {f : σ → α} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e.symm (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv.comp h).of_eq (λ a, by simp), of_equiv_symm.comp⟩ end primrec namespace primcodable open nat.primrec instance prod {α β} [primcodable α] [primcodable β] : primcodable (α × β) := ⟨((cases zero ((cases zero succ).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp [nat.unpaired], cases decode α n.unpair.1, { simp }, cases decode β n.unpair.2; simp end⟩ end primcodable namespace primrec variables {α : Type*} {σ : Type*} [primcodable α] [primcodable σ] open nat.primrec theorem fst {α β} [primcodable α] [primcodable β] : primrec (@prod.fst α β) := ((cases zero ((cases zero (nat.primrec.succ.comp left)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem snd {α β} [primcodable α] [primcodable β] : primrec (@prod.snd α β) := ((cases zero ((cases zero (nat.primrec.succ.comp right)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem pair {α β γ} [primcodable α] [primcodable β] [primcodable γ] {f : α → β} {g : α → γ} (hf : primrec f) (hg : primrec g) : primrec (λ a, (f a, g a)) := ((cases1 0 (nat.primrec.succ.comp $ pair (nat.primrec.pred.comp hf) (nat.primrec.pred.comp hg))).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp [encodek]; refl theorem unpair : primrec nat.unpair := (pair (nat_iff.2 nat.primrec.left) (nat_iff.2 nat.primrec.right)).of_eq $ λ n, by simp theorem list_nth₁ : ∀ (l : list α), primrec l.nth | [] := dom_denumerable.2 zero | (a::l) := dom_denumerable.2 $ (cases1 (encode a).succ $ dom_denumerable.1 $ list_nth₁ l).of_eq $ λ n, by cases n; simp end primrec /-- `primrec₂ f` means `f` is a binary primitive recursive function. This is technically unnecessary since we can always curry all the arguments together, but there are enough natural two-arg functions that it is convenient to express this directly. -/ def primrec₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β → σ) := primrec (λ p : α × β, f p.1 p.2) /-- `primrec_pred p` means `p : α → Prop` is a (decidable) primitive recursive predicate, which is to say that `to_bool ∘ p : α → bool` is primitive recursive. -/ def primrec_pred {α} [primcodable α] (p : α → Prop) [decidable_pred p] := primrec (λ a, to_bool (p a)) /-- `primrec_rel p` means `p : α → β → Prop` is a (decidable) primitive recursive relation, which is to say that `to_bool ∘ p : α → β → bool` is primitive recursive. -/ def primrec_rel {α β} [primcodable α] [primcodable β] (s : α → β → Prop) [∀ a b, decidable (s a b)] := primrec₂ (λ a b, to_bool (s a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] theorem of_eq {f g : α → β → σ} (hg : primrec₂ f) (H : ∀ a b, f a b = g a b) : primrec₂ g := (by funext a b; apply H : f = g) ▸ hg theorem const (x : σ) : primrec₂ (λ (a : α) (b : β), x) := primrec.const _ protected theorem pair : primrec₂ (@prod.mk α β) := primrec.pair primrec.fst primrec.snd theorem left : primrec₂ (λ (a : α) (b : β), a) := primrec.fst theorem right : primrec₂ (λ (a : α) (b : β), b) := primrec.snd theorem mkpair : primrec₂ nat.mkpair := by simp [primrec₂, primrec]; constructor theorem unpaired {f : ℕ → ℕ → α} : primrec (nat.unpaired f) ↔ primrec₂ f := ⟨λ h, by simpa using h.comp mkpair, λ h, h.comp primrec.unpair⟩ theorem unpaired' {f : ℕ → ℕ → ℕ} : nat.primrec (nat.unpaired f) ↔ primrec₂ f := primrec.nat_iff.symm.trans unpaired theorem encode_iff {f : α → β → σ} : primrec₂ (λ a b, encode (f a b)) ↔ primrec₂ f := primrec.encode_iff theorem option_some_iff {f : α → β → σ} : primrec₂ (λ a b, some (f a b)) ↔ primrec₂ f := primrec.option_some_iff theorem of_nat_iff {α β σ} [denumerable α] [denumerable β] [primcodable σ] {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, f (of_nat α m) (of_nat β n)) := (primrec.of_nat_iff.trans $ by simp).trans unpaired theorem uncurry {f : α → β → σ} : primrec (function.uncurry f) ↔ primrec₂ f := by rw [show function.uncurry f = λ (p : α × β), f p.1 p.2, from funext $ λ ⟨a, b⟩, rfl]; refl theorem curry {f : α × β → σ} : primrec₂ (function.curry f) ↔ primrec f := by rw [← uncurry, function.uncurry_curry] end primrec₂ section comp variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem primrec.comp₂ {f : γ → σ} {g : α → β → γ} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a b, f (g a b)) := hf.comp hg theorem primrec₂.comp {f : β → γ → σ} {g : α → β} {h : α → γ} (hf : primrec₂ f) (hg : primrec g) (hh : primrec h) : primrec (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem primrec₂.comp₂ {f : γ → δ → σ} {g : α → β → γ} {h : α → β → δ} (hf : primrec₂ f) (hg : primrec₂ g) (hh : primrec₂ h) : primrec₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh theorem primrec_pred.comp {p : β → Prop} [decidable_pred p] {f : α → β} : primrec_pred p → primrec f → primrec_pred (λ a, p (f a)) := primrec.comp theorem primrec_rel.comp {R : β → γ → Prop} [∀ a b, decidable (R a b)] {f : α → β} {g : α → γ} : primrec_rel R → primrec f → primrec g → primrec_pred (λ a, R (f a) (g a)) := primrec₂.comp theorem primrec_rel.comp₂ {R : γ → δ → Prop} [∀ a b, decidable (R a b)] {f : α → β → γ} {g : α → β → δ} : primrec_rel R → primrec₂ f → primrec₂ g → primrec_rel (λ a b, R (f a b) (g a b)) := primrec_rel.comp end comp theorem primrec_pred.of_eq {α} [primcodable α] {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (H : ∀ a, p a ↔ q a) : primrec_pred q := primrec.of_eq hp (λ a, to_bool_congr (H a)) theorem primrec_rel.of_eq {α β} [primcodable α] [primcodable β] {r s : α → β → Prop} [∀ a b, decidable (r a b)] [∀ a b, decidable (s a b)] (hr : primrec_rel r) (H : ∀ a b, r a b ↔ s a b) : primrec_rel s := primrec₂.of_eq hr (λ a b, to_bool_congr (H a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec theorem swap {f : α → β → σ} (h : primrec₂ f) : primrec₂ (swap f) := h.comp₂ primrec₂.right primrec₂.left theorem nat_iff {f : α → β → σ} : primrec₂ f ↔ nat.primrec (nat.unpaired $ λ m n : ℕ, encode $ (decode α m).bind $ λ a, (decode β n).map (f a)) := have ∀ (a : option α) (b : option β), option.map (λ (p : α × β), f p.1 p.2) (option.bind a (λ (a : α), option.map (prod.mk a) b)) = option.bind a (λ a, option.map (f a) b), by intros; cases a; [refl, {cases b; refl}], by simp [primrec₂, primrec, this] theorem nat_iff' {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, option.bind (decode α m) (λ a, option.map (f a) (decode β n))) := nat_iff.trans $ unpaired'.trans encode_iff end primrec₂ namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem to₂ {f : α × β → σ} (hf : primrec f) : primrec₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl theorem nat_elim {f : α → β} {g : α → ℕ × β → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a (n : ℕ), n.elim (f a) (λ n IH, g a (n, IH))) := primrec₂.nat_iff.2 $ ((nat.primrec.cases nat.primrec.zero $ (nat.primrec.prec hf $ nat.primrec.comp hg $ nat.primrec.left.pair $ (nat.primrec.left.comp nat.primrec.right).pair $ nat.primrec.pred.comp $ nat.primrec.right.comp nat.primrec.right).comp $ nat.primrec.right.pair $ nat.primrec.right.comp nat.primrec.left).comp $ nat.primrec.id.pair $ (primcodable.prim α).comp nat.primrec.left).of_eq $ λ n, begin simp, cases decode α n.unpair.1 with a, {refl}, simp [encodek], induction n.unpair.2 with m; simp [encodek], simp [ih, encodek] end theorem nat_elim' {f : α → ℕ} {g : α → β} {h : α → ℕ × β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).elim (g a) (λ n IH, h a (n, IH))) := (nat_elim hg hh).comp primrec.id hf theorem nat_elim₁ {f : ℕ → α → α} (a : α) (hf : primrec₂ f) : primrec (nat.elim a f) := nat_elim' primrec.id (const a) $ comp₂ hf primrec₂.right theorem nat_cases' {f : α → β} {g : α → ℕ → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a, nat.cases (f a) (g a)) := nat_elim hf $ hg.comp₂ primrec₂.left $ comp₂ fst primrec₂.right theorem nat_cases {f : α → ℕ} {g : α → β} {h : α → ℕ → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).cases (g a) (h a)) := (nat_cases' hg hh).comp primrec.id hf theorem nat_cases₁ {f : ℕ → α} (a : α) (hf : primrec f) : primrec (nat.cases a f) := nat_cases primrec.id (const a) (comp₂ hf primrec₂.right) theorem nat_iterate {f : α → ℕ} {g : α → β} {h : α → β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (h a)^[f a] (g a)) := (nat_elim' hf hg (hh.comp₂ primrec₂.left $ snd.comp₂ primrec₂.right)).of_eq $ λ a, by induction f a; simp [*, function.iterate_succ'] theorem option_cases {o : α → option β} {f : α → σ} {g : α → β → σ} (ho : primrec o) (hf : primrec f) (hg : primrec₂ g) : @primrec _ σ _ _ (λ a, option.cases_on (o a) (f a) (g a)) := encode_iff.1 $ (nat_cases (encode_iff.2 ho) (encode_iff.2 hf) $ pred.comp₂ $ primrec₂.encode_iff.2 $ (primrec₂.nat_iff'.1 hg).comp₂ ((@primrec.encode α _).comp fst).to₂ primrec₂.right).of_eq $ λ a, by cases o a with b; simp [encodek]; refl theorem option_bind {f : α → option β} {g : α → β → option σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).bind (g a)) := (option_cases hf (const none) hg).of_eq $ λ a, by cases f a; refl theorem option_bind₁ {f : α → option σ} (hf : primrec f) : primrec (λ o, option.bind o f) := option_bind primrec.id (hf.comp snd).to₂ theorem option_map {f : α → option β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := option_bind hf (option_some.comp₂ hg) theorem option_map₁ {f : α → σ} (hf : primrec f) : primrec (option.map f) := option_map primrec.id (hf.comp snd).to₂ theorem option_iget [inhabited α] : primrec (@option.iget α _) := (option_cases primrec.id (const $ @default α _) primrec₂.right).of_eq $ λ o, by cases o; refl theorem option_is_some : primrec (@option.is_some α) := (option_cases primrec.id (const ff) (const tt).to₂).of_eq $ λ o, by cases o; refl theorem option_get_or_else : primrec₂ (@option.get_or_else α) := primrec.of_eq (option_cases primrec₂.left primrec₂.right primrec₂.right) $ λ ⟨o, a⟩, by cases o; refl theorem bind_decode_iff {f : α → β → option σ} : primrec₂ (λ a n, (decode β n).bind (f a)) ↔ primrec₂ f := ⟨λ h, by simpa [encodek] using h.comp fst ((@primrec.encode β _).comp snd), λ h, option_bind (primrec.decode.comp snd) $ h.comp (fst.comp fst) snd⟩ theorem map_decode_iff {f : α → β → σ} : primrec₂ (λ a n, (decode β n).map (f a)) ↔ primrec₂ f := bind_decode_iff.trans primrec₂.option_some_iff theorem nat_add : primrec₂ ((+) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.add theorem nat_sub : primrec₂ (has_sub.sub : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.sub theorem nat_mul : primrec₂ ((*) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.mul theorem cond {c : α → bool} {f : α → σ} {g : α → σ} (hc : primrec c) (hf : primrec f) (hg : primrec g) : primrec (λ a, cond (c a) (f a) (g a)) := (nat_cases (encode_iff.2 hc) hg (hf.comp fst).to₂).of_eq $ λ a, by cases c a; refl theorem ite {c : α → Prop} [decidable_pred c] {f : α → σ} {g : α → σ} (hc : primrec_pred c) (hf : primrec f) (hg : primrec g) : primrec (λ a, if c a then f a else g a) := by simpa using cond hc hf hg theorem nat_le : primrec_rel ((≤) : ℕ → ℕ → Prop) := (nat_cases nat_sub (const tt) (const ff).to₂).of_eq $ λ p, begin dsimp [swap], cases e : p.1 - p.2 with n, { simp [tsub_eq_zero_iff_le.1 e] }, { simp [not_le.2 (nat.lt_of_sub_eq_succ e)] } end theorem nat_min : primrec₂ (@min ℕ _) := ite nat_le fst snd theorem nat_max : primrec₂ (@max ℕ _) := ite (nat_le.comp primrec.fst primrec.snd) snd fst theorem dom_bool (f : bool → α) : primrec f := (cond primrec.id (const (f tt)) (const (f ff))).of_eq $ λ b, by cases b; refl theorem dom_bool₂ (f : bool → bool → α) : primrec₂ f := (cond fst ((dom_bool (f tt)).comp snd) ((dom_bool (f ff)).comp snd)).of_eq $ λ ⟨a, b⟩, by cases a; refl protected theorem bnot : primrec bnot := dom_bool _ protected theorem band : primrec₂ band := dom_bool₂ _ protected theorem bor : primrec₂ bor := dom_bool₂ _ protected theorem not {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primrec_pred (λ a, ¬ p a) := (primrec.bnot.comp hp).of_eq $ λ n, by simp protected theorem and {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∧ q a) := (primrec.band.comp hp hq).of_eq $ λ n, by simp protected theorem or {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∨ q a) := (primrec.bor.comp hp hq).of_eq $ λ n, by simp protected theorem eq [decidable_eq α] : primrec_rel (@eq α) := have primrec_rel (λ a b : ℕ, a = b), from (primrec.and nat_le nat_le.swap).of_eq $ λ a, by simp [le_antisymm_iff], (this.comp₂ (primrec.encode.comp₂ primrec₂.left) (primrec.encode.comp₂ primrec₂.right)).of_eq $ λ a b, encode_injective.eq_iff theorem nat_lt : primrec_rel ((<) : ℕ → ℕ → Prop) := (nat_le.comp snd fst).not.of_eq $ λ p, by simp theorem option_guard {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) {f : α → β} (hf : primrec f) : primrec (λ a, option.guard (p a) (f a)) := ite (hp.comp primrec.id hf) (option_some_iff.2 hf) (const none) theorem option_orelse : primrec₂ ((<|>) : option α → option α → option α) := (option_cases fst snd (fst.comp fst).to₂).of_eq $ λ ⟨o₁, o₂⟩, by cases o₁; cases o₂; refl protected theorem decode₂ : primrec (decode₂ α) := option_bind primrec.decode $ option_guard ((@primrec.eq _ _ nat.decidable_eq).comp (encode_iff.2 snd) (fst.comp fst)) snd theorem list_find_index₁ {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) : ∀ (l : list β), primrec (λ a, l.find_index (p a)) | [] := const 0 | (a::l) := ite (hp.comp primrec.id (const a)) (const 0) (succ.comp (list_find_index₁ l)) theorem list_index_of₁ [decidable_eq α] (l : list α) : primrec (λ a, l.index_of a) := list_find_index₁ primrec.eq l theorem dom_fintype [fintype α] (f : α → σ) : primrec f := let ⟨l, nd, m⟩ := finite.exists_univ_list α in option_some_iff.1 $ begin haveI := decidable_eq_of_encodable α, refine ((list_nth₁ (l.map f)).comp (list_index_of₁ l)).of_eq (λ a, _), rw [list.nth_map, list.nth_le_nth (list.index_of_lt_length.2 (m _)), list.index_of_nth_le]; refl end theorem nat_bodd_div2 : primrec nat.bodd_div2 := (nat_elim' primrec.id (const (ff, 0)) (((cond fst (pair (const ff) (succ.comp snd)) (pair (const tt) snd)).comp snd).comp snd).to₂).of_eq $ λ n, begin simp [-nat.bodd_div2_eq], induction n with n IH, {refl}, simp [-nat.bodd_div2_eq, nat.bodd_div2, *], rcases nat.bodd_div2 n with ⟨_|_, m⟩; simp [nat.bodd_div2] end theorem nat_bodd : primrec nat.bodd := fst.comp nat_bodd_div2 theorem nat_div2 : primrec nat.div2 := snd.comp nat_bodd_div2 theorem nat_bit0 : primrec (@bit0 ℕ _) := nat_add.comp primrec.id primrec.id theorem nat_bit1 : primrec (@bit1 ℕ _ _) := nat_add.comp nat_bit0 (const 1) theorem nat_bit : primrec₂ nat.bit := (cond primrec.fst (nat_bit1.comp primrec.snd) (nat_bit0.comp primrec.snd)).of_eq $ λ n, by cases n.1; refl theorem nat_div_mod : primrec₂ (λ n k : ℕ, (n / k, n % k)) := let f (a : ℕ × ℕ) : ℕ × ℕ := a.1.elim (0, 0) (λ _ IH, if nat.succ IH.2 = a.2 then (nat.succ IH.1, 0) else (IH.1, nat.succ IH.2)) in have hf : primrec f, from nat_elim' fst (const (0, 0)) $ ((ite ((@primrec.eq ℕ _ _).comp (succ.comp $ snd.comp snd) fst) (pair (succ.comp $ fst.comp snd) (const 0)) (pair (fst.comp snd) (succ.comp $ snd.comp snd))) .comp (pair (snd.comp fst) (snd.comp snd))).to₂, suffices ∀ k n, (n / k, n % k) = f (n, k), from hf.of_eq $ λ ⟨m, n⟩, by simp [this], λ k n, begin have : (f (n, k)).2 + k * (f (n, k)).1 = n ∧ (0 < k → (f (n, k)).2 < k) ∧ (k = 0 → (f (n, k)).1 = 0), { induction n with n IH, {exact ⟨rfl, id, λ _, rfl⟩}, rw [λ n:ℕ, show f (n.succ, k) = _root_.ite ((f (n, k)).2.succ = k) (nat.succ (f (n, k)).1, 0) ((f (n, k)).1, (f (n, k)).2.succ), from rfl], by_cases h : (f (n, k)).2.succ = k; simp [h], { have := congr_arg nat.succ IH.1, refine ⟨_, λ k0, nat.no_confusion (h.trans k0)⟩, rwa [← nat.succ_add, h, add_comm, ← nat.mul_succ] at this }, { exact ⟨by rw [nat.succ_add, IH.1], λ k0, lt_of_le_of_ne (IH.2.1 k0) h, IH.2.2⟩ } }, revert this, cases f (n, k) with D M, simp, intros h₁ h₂ h₃, cases nat.eq_zero_or_pos k, { simp [h, h₃ h] at h₁ ⊢, simp [h₁] }, { exact (nat.div_mod_unique h).2 ⟨h₁, h₂ h⟩ } end theorem nat_div : primrec₂ ((/) : ℕ → ℕ → ℕ) := fst.comp₂ nat_div_mod theorem nat_mod : primrec₂ ((%) : ℕ → ℕ → ℕ) := snd.comp₂ nat_div_mod end primrec section variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] variable (H : nat.primrec (λ n, encodable.encode (decode (list β) n))) include H open primrec private def prim : primcodable (list β) := ⟨H⟩ private lemma list_cases' {f : α → list β} {g : α → σ} {h : α → β × list β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := by letI := prim H; exact have @primrec _ (option σ) _ _ (λ a, (decode (option (β × list β)) (encode (f a))).map (λ o, option.cases_on o (g a) (h a))), from ((@map_decode_iff _ (option (β × list β)) _ _ _ _ _).2 $ to₂ $ option_cases snd (hg.comp fst) (hh.comp₂ (fst.comp₂ primrec₂.left) primrec₂.right)) .comp primrec.id (encode_iff.2 hf), option_some_iff.1 $ this.of_eq $ λ a, by cases f a with b l; simp [encodek]; refl private lemma list_foldl' {f : α → list β} {g : α → σ} {h : α → σ × β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := by letI := prim H; exact let G (a : α) (IH : σ × list β) : σ × list β := list.cases_on IH.2 IH (λ b l, (h a (IH.1, b), l)) in let F (a : α) (n : ℕ) := (G a)^[n] (g a, f a) in have primrec (λ a, (F a (encode (f a))).1), from fst.comp $ nat_iterate (encode_iff.2 hf) (pair hg hf) $ list_cases' H (snd.comp snd) snd $ to₂ $ pair (hh.comp (fst.comp fst) $ pair ((fst.comp snd).comp fst) (fst.comp snd)) (snd.comp snd), this.of_eq $ λ a, begin have : ∀ n, F a n = ((list.take n (f a)).foldl (λ s b, h a (s, b)) (g a), list.drop n (f a)), { intro, simp [F], generalize : f a = l, generalize : g a = x, induction n with n IH generalizing l x, {refl}, simp, cases l with b l; simp [IH] }, rw [this, list.take_all_of_le (length_le_encode _)] end private lemma list_cons' : by haveI := prim H; exact primrec₂ (@list.cons β) := by letI := prim H; exact encode_iff.1 (succ.comp $ primrec₂.mkpair.comp (encode_iff.2 fst) (encode_iff.2 snd)) private lemma list_reverse' : by haveI := prim H; exact primrec (@list.reverse β) := by letI := prim H; exact (list_foldl' H primrec.id (const []) $ to₂ $ ((list_cons' H).comp snd fst).comp snd).of_eq (suffices ∀ l r, list.foldl (λ (s : list β) (b : β), b :: s) r l = list.reverse_core l r, from λ l, this l [], λ l, by induction l; simp [*, list.reverse_core]) end namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec instance sum : primcodable (α ⊕ β) := ⟨primrec.nat_iff.1 $ (encode_iff.2 (cond nat_bodd (((@primrec.decode β _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const tt) (primrec.encode.comp snd)) (((@primrec.decode α _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const ff) (primrec.encode.comp snd)))).of_eq $ λ n, show _ = encode (decode_sum n), begin simp [decode_sum], cases nat.bodd n; simp [decode_sum], { cases decode α n.div2; refl }, { cases decode β n.div2; refl } end⟩ instance list : primcodable (list α) := ⟨ by letI H := primcodable.prim (list ℕ); exact have primrec₂ (λ (a : α) (o : option (list ℕ)), o.map (list.cons (encode a))), from option_map snd $ (list_cons' H).comp ((@primrec.encode α _).comp (fst.comp fst)) snd, have primrec (λ n, (of_nat (list ℕ) n).reverse.foldl (λ o m, (decode α m).bind (λ a, o.map (list.cons (encode a)))) (some [])), from list_foldl' H ((list_reverse' H).comp (primrec.of_nat (list ℕ))) (const (some [])) (primrec.comp₂ (bind_decode_iff.2 $ primrec₂.swap this) primrec₂.right), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, begin rw list.foldl_reverse, apply nat.case_strong_induction_on n, { simp }, intros n IH, simp, cases decode α n.unpair.1 with a, {refl}, simp, suffices : ∀ (o : option (list ℕ)) p (_ : encode o = encode p), encode (option.map (list.cons (encode a)) o) = encode (option.map (list.cons a) p), from this _ _ (IH _ (nat.unpair_right_le n)), intros o p IH, cases o; cases p; injection IH with h, exact congr_arg (λ k, (nat.mkpair (encode a) k).succ.succ) h end⟩ end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem sum_inl : primrec (@sum.inl α β) := encode_iff.1 $ nat_bit0.comp primrec.encode theorem sum_inr : primrec (@sum.inr α β) := encode_iff.1 $ nat_bit1.comp primrec.encode theorem sum_cases {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ → σ} (hf : primrec f) (hg : primrec₂ g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (nat_bodd.comp $ encode_iff.2 hf) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hh) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hg)).of_eq $ λ a, by cases f a with b c; simp [nat.div2_bit, nat.bodd_bit, encodek]; refl theorem list_cons : primrec₂ (@list.cons α) := list_cons' (primcodable.prim _) theorem list_cases {f : α → list β} {g : α → σ} {h : α → β × list β → σ} : primrec f → primrec g → primrec₂ h → @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := list_cases' (primcodable.prim _) theorem list_foldl {f : α → list β} {g : α → σ} {h : α → σ × β → σ} : primrec f → primrec g → primrec₂ h → primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := list_foldl' (primcodable.prim _) theorem list_reverse : primrec (@list.reverse α) := list_reverse' (primcodable.prim _) theorem list_foldr {f : α → list β} {g : α → σ} {h : α → β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).foldr (λ b s, h a (b, s)) (g a)) := (list_foldl (list_reverse.comp hf) hg $ to₂ $ hh.comp fst $ (pair snd fst).comp snd).of_eq $ λ a, by simp [list.foldl_reverse] theorem list_head' : primrec (@list.head' α) := (list_cases primrec.id (const none) (option_some_iff.2 $ (fst.comp snd)).to₂).of_eq $ λ l, by cases l; refl theorem list_head [inhabited α] : primrec (@list.head α _) := (option_iget.comp list_head').of_eq $ λ l, l.head_eq_head'.symm theorem list_tail : primrec (@list.tail α) := (list_cases primrec.id (const []) (snd.comp snd).to₂).of_eq $ λ l, by cases l; refl theorem list_rec {f : α → list β} {g : α → σ} {h : α → β × list β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))) := let F (a : α) := (f a).foldr (λ (b : β) (s : list β × σ), (b :: s.1, h a (b, s))) ([], g a) in have primrec F, from list_foldr hf (pair (const []) hg) $ to₂ $ pair ((list_cons.comp fst (fst.comp snd)).comp snd) hh, (snd.comp this).of_eq $ λ a, begin suffices : F a = (f a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))), {rw this}, simp [F], induction f a with b l IH; simp * end theorem list_nth : primrec₂ (@list.nth α) := let F (l : list α) (n : ℕ) := l.foldl (λ (s : ℕ ⊕ α) (a : α), sum.cases_on s (@nat.cases (ℕ ⊕ α) (sum.inr a) sum.inl) sum.inr) (sum.inl n) in have hF : primrec₂ F, from list_foldl fst (sum_inl.comp snd) ((sum_cases fst (nat_cases snd (sum_inr.comp $ snd.comp fst) (sum_inl.comp snd).to₂).to₂ (sum_inr.comp snd).to₂).comp snd).to₂, have @primrec _ (option α) _ _ (λ p : list α × ℕ, sum.cases_on (F p.1 p.2) (λ _, none) some), from sum_cases hF (const none).to₂ (option_some.comp snd).to₂, this.to₂.of_eq $ λ l n, begin dsimp, symmetry, induction l with a l IH generalizing n, {refl}, cases n with n, { rw [(_ : F (a :: l) 0 = sum.inr a)], {refl}, clear IH, dsimp [F], induction l with b l IH; simp * }, { apply IH } end theorem list_nthd (d : α) : primrec₂ (λ l n, list.nthd l n d) := begin simp only [list.nthd_eq_get_or_else_nth], exact option_get_or_else.comp₂ list_nth (const _) end theorem list_inth [inhabited α] : primrec₂ (@list.inth α _) := list_nthd _ theorem list_append : primrec₂ ((++) : list α → list α → list α) := (list_foldr fst snd $ to₂ $ comp (@list_cons α _) snd).to₂.of_eq $ λ l₁ l₂, by induction l₁; simp * theorem list_concat : primrec₂ (λ l (a:α), l ++ [a]) := list_append.comp fst (list_cons.comp snd (const [])) theorem list_map {f : α → list β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := (list_foldr hf (const []) $ to₂ $ list_cons.comp (hg.comp fst (fst.comp snd)) (snd.comp snd)).of_eq $ λ a, by induction f a; simp * theorem list_range : primrec list.range := (nat_elim' primrec.id (const []) ((list_concat.comp snd fst).comp snd).to₂).of_eq $ λ n, by simp; induction n; simp [*, list.range_succ]; refl theorem list_join : primrec (@list.join α) := (list_foldr primrec.id (const []) $ to₂ $ comp (@list_append α _) snd).of_eq $ λ l, by dsimp; induction l; simp * theorem list_length : primrec (@list.length α) := (list_foldr (@primrec.id (list α) _) (const 0) $ to₂ $ (succ.comp $ snd.comp snd).to₂).of_eq $ λ l, by dsimp; induction l; simp [*, -add_comm] theorem list_find_index {f : α → list β} {p : α → β → Prop} [∀ a b, decidable (p a b)] (hf : primrec f) (hp : primrec_rel p) : primrec (λ a, (f a).find_index (p a)) := (list_foldr hf (const 0) $ to₂ $ ite (hp.comp fst $ fst.comp snd) (const 0) (succ.comp $ snd.comp snd)).of_eq $ λ a, eq.symm $ by dsimp; induction f a with b l; [refl, simp [*, list.find_index]] theorem list_index_of [decidable_eq α] : primrec₂ (@list.index_of α _) := to₂ $ list_find_index snd $ primrec.eq.comp₂ (fst.comp fst).to₂ snd.to₂ theorem nat_strong_rec (f : α → ℕ → σ) {g : α → list σ → option σ} (hg : primrec₂ g) (H : ∀ a n, g a ((list.range n).map (f a)) = some (f a n)) : primrec₂ f := suffices primrec₂ (λ a n, (list.range n).map (f a)), from primrec₂.option_some_iff.1 $ (list_nth.comp (this.comp fst (succ.comp snd)) snd).to₂.of_eq $ λ a n, by simp [list.nth_range (nat.lt_succ_self n)]; refl, primrec₂.option_some_iff.1 $ (nat_elim (const (some [])) (to₂ $ option_bind (snd.comp snd) $ to₂ $ option_map (hg.comp (fst.comp fst) snd) (to₂ $ list_concat.comp (snd.comp fst) snd))).of_eq $ λ a n, begin simp, induction n with n IH, {refl}, simp [IH, H, list.range_succ] end end primrec namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec /-- A subtype of a primitive recursive predicate is `primcodable`. -/ def subtype {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primcodable (subtype p) := ⟨have primrec (λ n, (decode α n).bind (λ a, option.guard p a)), from option_bind primrec.decode (option_guard (hp.comp snd) snd), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, show _ = encode ((decode α n).bind (λ a, _)), begin cases decode α n with a, {refl}, dsimp [option.guard], by_cases h : p a; simp [h]; refl end⟩ instance fin {n} : primcodable (fin n) := @of_equiv _ _ (subtype $ nat_lt.comp primrec.id (const n)) fin.equiv_subtype instance vector {n} : primcodable (vector α n) := subtype ((@primrec.eq _ _ nat.decidable_eq).comp list_length (const _)) instance fin_arrow {n} : primcodable (fin n → α) := of_equiv _ (equiv.vector_equiv_fin _ _).symm instance array {n} : primcodable (array n α) := of_equiv _ (equiv.array_equiv_fin _ _) section ulower local attribute [instance, priority 100] encodable.decidable_range_encode encodable.decidable_eq_of_encodable instance ulower : primcodable (ulower α) := have primrec_pred (λ n, encodable.decode₂ α n ≠ none), from primrec.not (primrec.eq.comp (primrec.option_bind primrec.decode (primrec.ite (primrec.eq.comp (primrec.encode.comp primrec.snd) primrec.fst) (primrec.option_some.comp primrec.snd) (primrec.const _))) (primrec.const _)), primcodable.subtype $ primrec_pred.of_eq this (λ n, decode₂_ne_none_iff) end ulower end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem subtype_val {p : α → Prop} [decidable_pred p] {hp : primrec_pred p} : by haveI := primcodable.subtype hp; exact primrec (@subtype.val α p) := begin letI := primcodable.subtype hp, refine (primcodable.prim (subtype p)).of_eq (λ n, _), rcases decode (subtype p) n with _|⟨a,h⟩; refl end theorem subtype_val_iff {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → subtype p} : by haveI := primcodable.subtype hp; exact primrec (λ a, (f a).1) ↔ primrec f := begin letI := primcodable.subtype hp, refine ⟨λ h, _, λ hf, subtype_val.comp hf⟩, refine nat.primrec.of_eq h (λ n, _), cases decode α n with a, {refl}, simp, cases f a; refl end theorem subtype_mk {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → β} {h : ∀ a, p (f a)} (hf : primrec f) : by haveI := primcodable.subtype hp; exact primrec (λ a, @subtype.mk β p (f a) (h a)) := subtype_val_iff.1 hf theorem option_get {f : α → option β} {h : ∀ a, (f a).is_some} : primrec f → primrec (λ a, option.get (h a)) := begin intro hf, refine (nat.primrec.pred.comp hf).of_eq (λ n, _), generalize hx : decode α n = x, cases x; simp end theorem ulower_down : primrec (ulower.down : α → ulower α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact subtype_mk primrec.encode theorem ulower_up : primrec (ulower.up : ulower α → α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact option_get (primrec.decode₂.comp subtype_val) theorem fin_val_iff {n} {f : α → fin n} : primrec (λ a, (f a).1) ↔ primrec f := begin let : primcodable {a//id a<n}, swap, exactI (iff.trans (by refl) subtype_val_iff).trans (of_equiv_iff _) end theorem fin_val {n} : primrec (coe : fin n → ℕ) := fin_val_iff.2 primrec.id theorem fin_succ {n} : primrec (@fin.succ n) := fin_val_iff.1 $ by simp [succ.comp fin_val] theorem vector_to_list {n} : primrec (@vector.to_list α n) := subtype_val theorem vector_to_list_iff {n} {f : α → vector β n} : primrec (λ a, (f a).to_list) ↔ primrec f := subtype_val_iff theorem vector_cons {n} : primrec₂ (@vector.cons α n) := vector_to_list_iff.1 $ by simp; exact list_cons.comp fst (vector_to_list_iff.2 snd) theorem vector_length {n} : primrec (@vector.length α n) := const _ theorem vector_head {n} : primrec (@vector.head α n) := option_some_iff.1 $ (list_head'.comp vector_to_list).of_eq $ λ ⟨a::l, h⟩, rfl theorem vector_tail {n} : primrec (@vector.tail α n) := vector_to_list_iff.1 $ (list_tail.comp vector_to_list).of_eq $ λ ⟨l, h⟩, by cases l; refl theorem vector_nth {n} : primrec₂ (@vector.nth α n) := option_some_iff.1 $ (list_nth.comp (vector_to_list.comp fst) (fin_val.comp snd)).of_eq $ λ a, by simp [vector.nth_eq_nth_le]; rw [← list.nth_le_nth] theorem list_of_fn : ∀ {n} {f : fin n → α → σ}, (∀ i, primrec (f i)) → primrec (λ a, list.of_fn (λ i, f i a)) | 0 f hf := const [] | (n+1) f hf := by simp [list.of_fn_succ]; exact list_cons.comp (hf 0) (list_of_fn (λ i, hf i.succ)) theorem vector_of_fn {n} {f : fin n → α → σ} (hf : ∀ i, primrec (f i)) : primrec (λ a, vector.of_fn (λ i, f i a)) := vector_to_list_iff.1 $ by simp [list_of_fn hf] theorem vector_nth' {n} : primrec (@vector.nth α n) := of_equiv_symm theorem vector_of_fn' {n} : primrec (@vector.of_fn α n) := of_equiv theorem fin_app {n} : primrec₂ (@id (fin n → σ)) := (vector_nth.comp (vector_of_fn'.comp fst) snd).of_eq $ λ ⟨v, i⟩, by simp theorem fin_curry₁ {n} {f : fin n → α → σ} : primrec₂ f ↔ ∀ i, primrec (f i) := ⟨λ h i, h.comp (const i) primrec.id, λ h, (vector_nth.comp ((vector_of_fn h).comp snd) fst).of_eq $ λ a, by simp⟩ theorem fin_curry {n} {f : α → fin n → σ} : primrec f ↔ primrec₂ f := ⟨λ h, fin_app.comp (h.comp fst) snd, λ h, (vector_nth'.comp (vector_of_fn (λ i, show primrec (λ a, f a i), from h.comp primrec.id (const i)))).of_eq $ λ a, by funext i; simp⟩ end primrec namespace nat open vector /-- An alternative inductive definition of `primrec` which does not use the pairing function on ℕ, and so has to work with n-ary functions on ℕ instead of unary functions. We prove that this is equivalent to the regular notion in `to_prim` and `of_prim`. -/ inductive primrec' : ∀ {n}, (vector ℕ n → ℕ) → Prop | zero : @primrec' 0 (λ _, 0) | succ : @primrec' 1 (λ v, succ v.head) | nth {n} (i : fin n) : primrec' (λ v, v.nth i) | comp {m n f} (g : fin n → vector ℕ m → ℕ) : primrec' f → (∀ i, primrec' (g i)) → primrec' (λ a, f (of_fn (λ i, g i a))) | prec {n f g} : @primrec' n f → @primrec' (n+2) g → primrec' (λ v : vector ℕ (n+1), v.head.elim (f v.tail) (λ y IH, g (y ::ᵥ IH ::ᵥ v.tail))) end nat namespace nat.primrec' open vector primrec nat (primrec') nat.primrec' hide ite theorem to_prim {n f} (pf : @primrec' n f) : primrec f := begin induction pf, case nat.primrec'.zero { exact const 0 }, case nat.primrec'.succ { exact primrec.succ.comp vector_head }, case nat.primrec'.nth : n i { exact vector_nth.comp primrec.id (const i) }, case nat.primrec'.comp : m n f g _ _ hf hg { exact hf.comp (vector_of_fn (λ i, hg i)) }, case nat.primrec'.prec : n f g _ _ hf hg { exact nat_elim' vector_head (hf.comp vector_tail) (hg.comp $ vector_cons.comp (fst.comp snd) $ vector_cons.comp (snd.comp snd) $ (@vector_tail _ _ (n+1)).comp fst).to₂ }, end theorem of_eq {n} {f g : vector ℕ n → ℕ} (hf : primrec' f) (H : ∀ i, f i = g i) : primrec' g := (funext H : f = g) ▸ hf theorem const {n} : ∀ m, @primrec' n (λ v, m) | 0 := zero.comp fin.elim0 (λ i, i.elim0) | (m+1) := succ.comp _ (λ i, const m) theorem head {n : ℕ} : @primrec' n.succ head := (nth 0).of_eq $ λ v, by simp [nth_zero] theorem tail {n f} (hf : @primrec' n f) : @primrec' n.succ (λ v, f v.tail) := (hf.comp _ (λ i, @nth _ i.succ)).of_eq $ λ v, by rw [← of_fn_nth v.tail]; congr; funext i; simp /-- A function from vectors to vectors is primitive recursive when all of its projections are. -/ def vec {n m} (f : vector ℕ n → vector ℕ m) : Prop := ∀ i, primrec' (λ v, (f v).nth i) protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0 protected theorem cons {n m f g} (hf : @primrec' n f) (hg : @vec n m g) : vec (λ v, (f v ::ᵥ g v)) := λ i, fin.cases (by simp *) (λ i, by simp [hg i]) i theorem idv {n} : @vec n n id := nth theorem comp' {n m f g} (hf : @primrec' m f) (hg : @vec n m g) : primrec' (λ v, f (g v)) := (hf.comp _ hg).of_eq $ λ v, by simp theorem comp₁ (f : ℕ → ℕ) (hf : @primrec' 1 (λ v, f v.head)) {n g} (hg : @primrec' n g) : primrec' (λ v, f (g v)) := hf.comp _ (λ i, hg) theorem comp₂ (f : ℕ → ℕ → ℕ) (hf : @primrec' 2 (λ v, f v.head v.tail.head)) {n g h} (hg : @primrec' n g) (hh : @primrec' n h) : primrec' (λ v, f (g v) (h v)) := by simpa using hf.comp' (hg.cons $ hh.cons primrec'.nil) theorem prec' {n f g h} (hf : @primrec' n f) (hg : @primrec' n g) (hh : @primrec' (n+2) h) : @primrec' n (λ v, (f v).elim (g v) (λ (y IH : ℕ), h (y ::ᵥ IH ::ᵥ v))) := by simpa using comp' (prec hg hh) (hf.cons idv) theorem pred : @primrec' 1 (λ v, v.head.pred) := (prec' head (const 0) head).of_eq $ λ v, by simp; cases v.head; refl theorem add : @primrec' 2 (λ v, v.head + v.tail.head) := (prec head (succ.comp₁ _ (tail head))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_add] theorem sub : @primrec' 2 (λ v, v.head - v.tail.head) := begin suffices, simpa using comp₂ (λ a b, b - a) this (tail head) head, refine (prec head (pred.comp₁ _ (tail head))).of_eq (λ v, _), simp, induction v.head; simp [*, nat.sub_succ] end theorem mul : @primrec' 2 (λ v, v.head * v.tail.head) := (prec (const 0) (tail (add.comp₂ _ (tail head) (head)))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_mul]; rw add_comm theorem if_lt {n a b f g} (ha : @primrec' n a) (hb : @primrec' n b) (hf : @primrec' n f) (hg : @primrec' n g) : @primrec' n (λ v, if a v < b v then f v else g v) := (prec' (sub.comp₂ _ hb ha) hg (tail $ tail hf)).of_eq $ λ v, begin cases e : b v - a v, { simp [not_lt.2 (tsub_eq_zero_iff_le.mp e)] }, { simp [nat.lt_of_sub_eq_succ e] } end theorem mkpair : @primrec' 2 (λ v, v.head.mkpair v.tail.head) := if_lt head (tail head) (add.comp₂ _ (tail $ mul.comp₂ _ head head) head) (add.comp₂ _ (add.comp₂ _ (mul.comp₂ _ head head) head) (tail head)) protected theorem encode : ∀ {n}, @primrec' n encode | 0 := (const 0).of_eq (λ v, by rw v.eq_nil; refl) | (n+1) := (succ.comp₁ _ (mkpair.comp₂ _ head (tail encode))) .of_eq $ λ ⟨a::l, e⟩, rfl theorem sqrt : @primrec' 1 (λ v, v.head.sqrt) := begin suffices H : ∀ n : ℕ, n.sqrt = n.elim 0 (λ x y, if x.succ < y.succ*y.succ then y else y.succ), { simp [H], have := @prec' 1 _ _ (λ v, by have x := v.head; have y := v.tail.head; from if x.succ < y.succ*y.succ then y else y.succ) head (const 0) _, { convert this, funext, congr, funext x y, congr; simp }, have x1 := succ.comp₁ _ head, have y1 := succ.comp₁ _ (tail head), exact if_lt x1 (mul.comp₂ _ y1 y1) (tail head) y1 }, intro, symmetry, induction n with n IH, {simp}, dsimp, rw IH, split_ifs, { exact le_antisymm (nat.sqrt_le_sqrt (nat.le_succ _)) (nat.lt_succ_iff.1 $ nat.sqrt_lt.2 h) }, { exact nat.eq_sqrt.2 ⟨not_lt.1 h, nat.sqrt_lt.1 $ nat.lt_succ_iff.2 $ nat.sqrt_succ_le_succ_sqrt _⟩ }, end theorem unpair₁ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.1) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s fss s).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem unpair₂ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.2) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s s (sub.comp₂ _ fss s)).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem of_prim : ∀ {n f}, primrec f → @primrec' n f := suffices ∀ f, nat.primrec f → @primrec' 1 (λ v, f v.head), from λ n f hf, (pred.comp₁ _ $ (this _ hf).comp₁ (λ m, encodable.encode $ (decode (vector ℕ n) m).map f) primrec'.encode).of_eq (λ i, by simp [encodek]), λ f hf, begin induction hf, case nat.primrec.zero { exact const 0 }, case nat.primrec.succ { exact succ }, case nat.primrec.left { exact unpair₁ head }, case nat.primrec.right { exact unpair₂ head }, case nat.primrec.pair : f g _ _ hf hg { exact mkpair.comp₂ _ hf hg }, case nat.primrec.comp : f g _ _ hf hg { exact hf.comp₁ _ hg }, case nat.primrec.prec : f g _ _ hf hg { simpa using prec' (unpair₂ head) (hf.comp₁ _ (unpair₁ head)) (hg.comp₁ _ $ mkpair.comp₂ _ (unpair₁ $ tail $ tail head) (mkpair.comp₂ _ head (tail head))) }, end theorem prim_iff {n f} : @primrec' n f ↔ primrec f := ⟨to_prim, of_prim⟩ theorem prim_iff₁ {f : ℕ → ℕ} : @primrec' 1 (λ v, f v.head) ↔ primrec f := prim_iff.trans ⟨ λ h, (h.comp $ vector_of_fn $ λ i, primrec.id).of_eq (λ v, by simp), λ h, h.comp vector_head⟩ theorem prim_iff₂ {f : ℕ → ℕ → ℕ} : @primrec' 2 (λ v, f v.head v.tail.head) ↔ primrec₂ f := prim_iff.trans ⟨ λ h, (h.comp $ vector_cons.comp fst $ vector_cons.comp snd (primrec.const nil)).of_eq (λ v, by simp), λ h, h.comp vector_head (vector_head.comp vector_tail)⟩ theorem vec_iff {m n f} : @vec m n f ↔ primrec f := ⟨λ h, by simpa using vector_of_fn (λ i, to_prim (h i)), λ h i, of_prim $ vector_nth.comp h (primrec.const i)⟩ end nat.primrec' theorem primrec.nat_sqrt : primrec nat.sqrt := nat.primrec'.prim_iff₁.1 nat.primrec'.sqrt
a5b2feb8451f7cf2ee4e34397dd3e432690e99c8
bdd56e6eb0f467437e368d613de75299495d4054
/src/group_theory/affine_variety.lean
cfc2f7075a5f48694fe5f1dad453f32d8bc22609
[]
no_license
truong111000/formalabstracts
49a04c268ccee136e48e24e9d5dcb6fedea4b53e
93a89a5c05c6fbc23eb9b914b60dcc353e609cd2
refs/heads/master
1,589,551,767,824
1,555,708,723,000
1,555,708,723,000
182,326,292
0
0
null
1,555,708,332,000
1,555,708,331,000
null
UTF-8
Lean
false
false
6,940
lean
import ..basic ring_theory.basic topology.basic category_theory.opposites category_theory.limits.limits ..category_theory.group_object tactic.omitted open category_theory ideal set topological_space noncomputable theory universes u v w def has_coe_ideal {α : Type u} [comm_ring α] : has_coe (ideal α) (set α) := by apply_instance local attribute [instance] has_coe_ideal /-- An algebraically closed field is a field where every polynomial with positive degree has a root -/ class algebraically_closed_field (α : Type u) extends discrete_field α := (closed : is_algebraically_closed α) local attribute [instance, priority 0] classical.prop_decidable /-- A finitely generated reduced algebra -/ class finitely_generated_reduced_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] extends algebra R A := (finitely_generated : is_finitely_generated R A) (reduced : is_reduced A) variables (K : Type u) [discrete_field K] (R : Type v) [comm_ring R] [finitely_generated_reduced_algebra K R] {σ : Type w} [decidable_eq σ] namespace algebraic_geometry /-- The spectrum `Specm(R)` of a `K`-algebra `R` is the set of homomorphisms from `R` to `K`. -/ @[reducible] def spectrum : Type* := R →ₐ[K] K variables {R K} /-- The quotient of R by a maximal ideal is isomorphic to K -/ def quotient_maximal_ideal (I : maximal_ideal R) : { f : I.val.quotient ≃ K // is_ring_hom f.to_fun } := classical.choice omitted /-- The spectrum of R is equivalent to the set of maximal ideals on R -/ def spectrum_equiv_maximal_ideal : spectrum K R ≃ maximal_ideal R := let f : maximal_ideal R → R → K := λ I, (quotient_maximal_ideal I).val.to_fun ∘ ideal.quotient.mk I.val in by { haveI h : ∀ I : maximal_ideal R, is_ring_hom (f I) := omitted, exact ⟨λ ϕ, ⟨ideal.ker ⇑ϕ, omitted⟩, λ I, ⟨f I, omitted⟩, omitted, omitted⟩ } variables (K) {R} /-- `Z(S)` is the set of homomorphisms in `Spec(R)` that vanish on `S`. -/ def Z (S : set R) : set (spectrum K R) := { f | ∀ x ∈ S, f.to_fun x = 0 } /--`I(X)` consists of all points in `R` that are send to `0` by all `ϕ ∈ X`-/ def I (X : set (spectrum K R)) : set R := { x : R | ∀ϕ : X, ϕ.val x = 0 } /-- `Z(S)` is equal to `Z` of the radical of the span of `S`. -/ lemma Z_radical_span (S : set R) : Z K ((ideal.span S).radical.carrier) = Z K S := omitted variables (K R) /-- The Zariski topology is the topology where the closed sets are of the form `Z(S)` for some `S ⊆ R` -/ instance Zariski_topology : topological_space (spectrum K R) := ⟨set.range (λ S : set R, - Z K S), omitted, omitted, omitted⟩ variables {K R} /-- A radical ideal gives rise to a closed set in the Zariski topology -/ def closed_set_of_radical_ideal (I : radical_ideal R) : closed_set (spectrum K R) := ⟨Z K I.val, mem_range_self I.val⟩ /-- A closed set in the Zariski topology gives rise to a radical ideal -/ def radical_ideal_of_closed_set (X : closed_set (spectrum K R)) : radical_ideal R := ⟨⟨ I K X, omitted, omitted, omitted⟩, omitted⟩ /-- Hilbert's Nullstellensatz: there is a correspondence between radical ideals in R and closed sets in the spectrum of R. -/ def Nullstellensatz : radical_ideal R ≃ closed_set (spectrum K R) := ⟨closed_set_of_radical_ideal, radical_ideal_of_closed_set, omitted, omitted⟩ instance quotient.finitely_generated_reduced_algebra (I : radical_ideal R) : finitely_generated_reduced_algebra K I.1.quotient := { finitely_generated := is_finitely_generated_quotient (finitely_generated_reduced_algebra.finitely_generated K R) I.1, reduced := is_reduced_quotient I.2, ..quotient.algebra I.1 } /-- The type of finitely generated reduced algebras over a fixed commutative ring. -/ structure FRAlgebra (R : Type u) [comm_ring R] : Type max u (v+1) := (β : Type v) [ring : comm_ring β] [algebra : finitely_generated_reduced_algebra R β] attribute [instance] FRAlgebra.ring FRAlgebra.algebra instance (R : Type u) [comm_ring R] : has_coe_to_sort (FRAlgebra.{u v} R) := { S := Type v, coe := FRAlgebra.β } open category_theory /-- The category of finitely generated reduced algebras over a fixed commutative ring. -/ instance Algebra.category (R : Type u) [comm_ring R] : category (FRAlgebra.{u v} R) := { hom := λ a b, a.β →ₐ[R] b.β, id := λ a, alg_hom.id R a, comp := λ a b c f g, alg_hom.comp g f } def FRAlgebra.quotient (R : FRAlgebra K) (Z : closed_set (spectrum K R)) : FRAlgebra K := ⟨K, (radical_ideal_of_closed_set Z).1.quotient⟩ variables (K) /-- In algebraic geometry, the categories of algebra's over K and affine varieties are opposite of each other. In this development we take a shortcut, and *define* affine varieties as the opposite of algebra's over K. -/ @[reducible] def affine_variety : Type* := opposite (FRAlgebra K) @[instance]def affine_variety.category : category (affine_variety K) := by apply_instance def affine_variety.subobject (R : affine_variety K) (Z : closed_set (spectrum K ↥(unop R))) : FRAlgebra K := FRAlgebra.quotient (unop R) Z @[instance] lemma affine_variety.complete : limits.has_limits (affine_variety K) := begin intros F 𝒥 X, haveI := F, cases classical.indefinite_description _ (omitted : ∃ t : limits.cone X, nonempty (limits.is_limit t)) with w h, exact ⟨w, classical.choice h⟩ end def FRAlgebra_self : (FRAlgebra K) := { β := K, ring := (by apply_instance), algebra := by {split, split, omit_proofs, exact {1}}} lemma FRAlgebra_self_hom (R : FRAlgebra K) : (R ⟶ (FRAlgebra_self K)) = (R →ₐ[K] K) := by refl lemma FRAlgebra_self_hom' (R : FRAlgebra K) : (by exact (R ⟶ (FRAlgebra_self K))) = (spectrum K R) := by refl /- The underlying type of an affine variety G = Rᵒᵖ is Spec(R), equivalently the global points of G in the category of affine varieties. It is easy to show that the global points functor in a category with finite limits is left-exact. -/ def algebraic_variety.type : (affine_variety K) ⥤ Type* := { obj := λ X, (unop X) ⟶ (FRAlgebra_self K), map := λ X Y f ϕ, f.unop ≫ ϕ, map_id' := by tidy, map_comp' := by tidy} variables {K R} /- to do: * quotient algebras, * if Z is closed in Spec R, then R / I(Z) is an algebra over K * The spectrum of this algebra is Z * affine_variety has a terminal object and binary products -/ end algebraic_geometry section algebraic_group open algebraic_geometry variables (K) [discrete_field K] /- For our purposes, an algebraic group is a group object in the category of affine varieties -/ include K def algebraic_group : Type* := @group_object (affine_variety K) (by apply_instance) (by apply_instance) (by apply_instance) /- to do: * group instance on underlying type of algebraic group * statement that algebraic_variety.type preserves finite products (is just left-exact) -/ end algebraic_group
9303f121cc2f429bf5f8adcc5d461a3907e475eb
7541ac8517945d0f903ff5397e13e2ccd7c10573
/src/category_theory/Grothendieck_topology.lean
a7a90d54478c4b07c08c2bd39222d585877b355c
[]
no_license
ramonfmir/lean-category-theory
29b6bad9f62c2cdf7517a3135e5a12b340b4ed90
be516bcbc2dc21b99df2bcb8dde0d1e8de79c9ad
refs/heads/master
1,586,110,684,637
1,541,927,184,000
1,541,927,184,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,690
lean
import category_theory.limits import category_theory.over open category_theory open category_theory.limits universes u₁ v₁ variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] include 𝒞 variables {X Y : C} structure cover (X : C) := (I : Type) (U : I → C) (π : Π i : I, U i ⟶ X) def pullback_cover [has_pullbacks.{u₁ v₁} C] {X Y : C} (c : cover.{u₁ v₁} X) (f : Y ⟶ X) : cover.{u₁ v₁} Y := { I := c.I, U := λ i, (pullback (c.π i) f), π := λ i, (pullback.square (c.π i) f).π₂ } def covers_of_cover {X : C} (c : cover.{u₁ v₁} X) (d : Π i : c.I, cover.{u₁ v₁} (c.U i)) : cover.{u₁ v₁} X := { I := Σ i : c.I, (d i).I, U := λ i, (d i.1).U i.2, π := λ i, ((d i.1).π i.2) ≫ (c.π i.1) } def singleton_cover {Y X : C} (f : Y ⟶ X) : cover.{u₁ v₁} X := { I := unit, U := λ i, Y, π := λ i, f } structure Grothendieck_topology [has_pullbacks.{u₁ v₁} C] := (covers (X : C) : set (cover.{u₁ v₁} X)) (pullback {X : C} (c ∈ covers X) {Y : C} (f : Y ⟶ X) : pullback_cover c f ∈ covers Y) (cover_of_covers {X : C} (c ∈ covers X) (d : Π (i : cover.I.{u₁ v₁} c), {P | covers (c.U i) P}) : covers_of_cover c (λ i, (d i).1) ∈ covers X) (isomorphism_cover {Y X : C} (f : Y ≅ X) : singleton_cover (f.hom) ∈ covers X) -- structure site (C : Type u₁) -- Or, we could do this in terms of sieves: structure sieve (X : C) := (S : set (over.{u₁ v₁} X)) (closed (f : { f // S f }) {Z : C} (g : Z ⟶ f.1.1) : (⟨ Z, g ≫ f.val.2 ⟩ : over X) ∈ S) -- example : a topology is a Grothendieck topology -- example : etale maps over a scheme X, with covers jointly surjective (U_i ⟶ X)_i
dd6db3f7128784ccaa19fd311fdef38daa4d0409
46125763b4dbf50619e8846a1371029346f4c3db
/src/topology/algebra/monoid.lean
57e411531ec8916e66a8cda998deb29ab7f4e972
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
5,710
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Theory of topological monoids. TODO: generalize `topological_monoid` and `topological_add_monoid` to semigroups, or add a type class `topological_operator α (*)`. -/ import topology.constructions topology.continuous_on import algebra.pi_instances open classical set lattice filter topological_space open_locale classical topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section topological_monoid /-- A topological monoid is a monoid in which the multiplication is continuous as a function `α × α → α`. -/ class topological_monoid (α : Type u) [topological_space α] [monoid α] : Prop := (continuous_mul : continuous (λp:α×α, p.1 * p.2)) /-- A topological (additive) monoid is a monoid in which the addition is continuous as a function `α × α → α`. -/ class topological_add_monoid (α : Type u) [topological_space α] [add_monoid α] : Prop := (continuous_add : continuous (λp:α×α, p.1 + p.2)) attribute [to_additive topological_add_monoid] topological_monoid section variables [topological_space α] [monoid α] [topological_monoid α] @[to_additive] lemma continuous_mul : continuous (λp:α×α, p.1 * p.2) := topological_monoid.continuous_mul α @[to_additive] lemma continuous.mul [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λx, f x * g x) := continuous_mul.comp (hf.prod_mk hg) @[to_additive] lemma continuous_mul_left (a : α) : continuous (λ b:α, a * b) := continuous_const.mul continuous_id @[to_additive] lemma continuous_mul_right (a : α) : continuous (λ b:α, b * a) := continuous_id.mul continuous_const @[to_additive] lemma continuous_on.mul [topological_space β] {f : β → α} {g : β → α} {s : set β} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, f x * g x) s := (continuous_mul.comp_continuous_on (hf.prod hg) : _) -- @[to_additive continuous_smul] lemma continuous_pow : ∀ n : ℕ, continuous (λ a : α, a ^ n) | 0 := by simpa using continuous_const | (k+1) := show continuous (λ (a : α), a * a ^ k), from continuous_id.mul (continuous_pow _) @[to_additive] lemma tendsto_mul {a b : α} : tendsto (λp:α×α, p.fst * p.snd) (𝓝 (a, b)) (𝓝 (a * b)) := continuous_iff_continuous_at.mp (topological_monoid.continuous_mul α) (a, b) @[to_additive] lemma filter.tendsto.mul {f : β → α} {g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, f x * g x) x (𝓝 (a * b)) := tendsto_mul.comp (hf.prod_mk_nhds hg) @[to_additive] lemma continuous_at.mul [topological_space β] {f : β → α} {g : β → α} {x : β} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λx, f x * g x) x := hf.mul hg @[to_additive] lemma continuous_within_at.mul [topological_space β] {f : β → α} {g : β → α} {s : set β} {x : β} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (λx, f x * g x) s x := hf.mul hg @[to_additive] lemma tendsto_list_prod {f : γ → β → α} {x : filter β} {a : γ → α} : ∀l:list γ, (∀c∈l, tendsto (f c) x (𝓝 (a c))) → tendsto (λb, (l.map (λc, f c b)).prod) x (𝓝 ((l.map a).prod)) | [] _ := by simp [tendsto_const_nhds] | (f :: l) h := begin simp only [list.map_cons, list.prod_cons], exact (h f (list.mem_cons_self _ _)).mul (tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc))) end @[to_additive] lemma continuous_list_prod [topological_space β] {f : γ → β → α} (l : list γ) (h : ∀c∈l, continuous (f c)) : continuous (λa, (l.map (λc, f c a)).prod) := continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc, continuous_iff_continuous_at.1 (h c hc) x @[to_additive topological_add_monoid] instance [topological_space β] [monoid β] [topological_monoid β] : topological_monoid (α × β) := ⟨((continuous_fst.comp continuous_fst).mul (continuous_fst.comp continuous_snd)).prod_mk ((continuous_snd.comp continuous_fst).mul (continuous_snd.comp continuous_snd))⟩ attribute [instance] prod.topological_add_monoid end section variables [topological_space α] [comm_monoid α] @[to_additive] lemma is_submonoid.mem_nhds_one (β : set α) [is_submonoid β] (oβ : is_open β) : β ∈ 𝓝 (1 : α) := mem_nhds_sets_iff.2 ⟨β, (by refl), oβ, is_submonoid.one_mem _⟩ variable [topological_monoid α] @[to_additive] lemma tendsto_multiset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : multiset γ) : (∀c∈s, tendsto (f c) x (𝓝 (a c))) → tendsto (λb, (s.map (λc, f c b)).prod) x (𝓝 ((s.map a).prod)) := by { rcases s with ⟨l⟩, simp, exact tendsto_list_prod l } @[to_additive] lemma tendsto_finset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : finset γ) : (∀c∈s, tendsto (f c) x (𝓝 (a c))) → tendsto (λb, s.prod (λc, f c b)) x (𝓝 (s.prod a)) := tendsto_multiset_prod _ @[to_additive] lemma continuous_multiset_prod [topological_space β] {f : γ → β → α} (s : multiset γ) : (∀c∈s, continuous (f c)) → continuous (λa, (s.map (λc, f c a)).prod) := by { rcases s with ⟨l⟩, simp, exact continuous_list_prod l } @[to_additive] lemma continuous_finset_prod [topological_space β] {f : γ → β → α} (s : finset γ) : (∀c∈s, continuous (f c)) → continuous (λa, s.prod (λc, f c a)) := continuous_multiset_prod _ end end topological_monoid
c6b76ef91f4e8123189e8fd887ae909adb6f300b
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/meta/match_tactic.lean
a7089ab0c38b65eb94c862c7801d37bde1965239
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
4,455
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.interactive_base init.function namespace tactic meta structure pattern := /- Term to match. -/ (target : expr) /- Set of universes that is instantiated for each successful match. -/ (uoutput : list level) /- Set of terms that is instantiated for each successful match. -/ (moutput : list expr) /- Number of (temporary) universe meta-variables in this pattern. -/ (nuvars : nat) /- Number of (temporary) meta-variables in this pattern. -/ (nmvars : nat) /- (mk_pattern ls es t u o) creates a new pattern with (length ls) universe meta-variables and (length es) meta-variables. In the produced pattern p, we have that - (pattern.target p) is the term t where the universes ls and expressions es have been replaced with temporary meta-variables. - (pattern.uoutput p) is the list u where the universes ls have been replaced with temporary meta-variables. - (pattern.moutput p) is the list o where the universes ls and expressions es have been replaced with temporary meta-variables. - (pattern.nuvars p) = length ls - (pattern.nmvars p) = length es The tactic fails if o and the types of es do not contain all universes ls and expressions es. -/ meta constant mk_pattern : list level → list expr → expr → list level → list expr → tactic pattern /- (mk_pattern_core m p e) matches (pattern.target p) and e using transparency m. If the matching is successful, then return the instantiation of (pattern.output p). The tactic fails if not all (temporary) meta-variables are assigned. -/ meta constant match_pattern_core : transparency → pattern → expr → tactic (list level × list expr) meta def match_pattern (p : pattern) (e : expr) : tactic (list expr) := prod.snd <$> (match_pattern_core semireducible p e) open expr /- Helper function for converting a term (λ x_1 ... x_n, t) into a pattern where x_1 ... x_n are metavariables -/ private meta def to_pattern_core : expr → tactic (expr × list expr) | (lam n bi d b) := do id ← mk_fresh_name, let x := local_const id n bi d, let new_b := instantiate_var b x, (p, xs) ← to_pattern_core new_b, return (p, x::xs) | e := return (e, []) /- Given a pre-term of the form (λ x_1 ... x_n, t[x_1, ..., x_n]), converts it into the pattern t[?x_1, ..., ?x_n] -/ meta def pexpr_to_pattern (p : pexpr) : tactic pattern := do e ← to_expr p, (new_p, xs) ← to_pattern_core e, mk_pattern [] xs new_p [] xs /- Convert pre-term into a pattern and try to match e. Given p of the form (λ x_1 ... x_n, t[x_1, ..., x_n]), a successful match will produce a list of length n. -/ meta def match_expr (p : pexpr) (e : expr) : tactic (list expr) := do new_p ← pexpr_to_pattern p, match_pattern new_p e private meta def match_subexpr_core : pattern → list expr → tactic (list expr) | p [] := failed | p (e::es) := match_pattern p e <|> match_subexpr_core p es <|> if is_app e then match_subexpr_core p (get_app_args e) else failed /- Similar to match_expr, but it tries to match a subexpression of e. Remark: the procedure does not go inside binders. -/ meta def match_subexpr (p : pexpr) (e : expr) : tactic (list expr) := do new_p ← pexpr_to_pattern p, match_subexpr_core new_p [e] /- Match the main goal target. -/ meta def match_target (p : pexpr) : tactic (list expr) := target >>= match_expr p /- Match a subterm in the main goal target. -/ meta def match_target_subexpr (p : pexpr) : tactic (list expr) := target >>= match_subexpr p private meta def match_hypothesis_core : pattern → list expr → tactic (expr × list expr) | p [] := failed | p (h::hs) := do h_type ← infer_type h, (do r ← match_pattern p h_type, return (h, r)) <|> match_hypothesis_core p hs /- Match hypothesis in the main goal target. The result is pair (hypothesis, substitution). -/ meta def match_hypothesis (p : pexpr) : tactic (expr × list expr) := do ctx ← local_context, new_p ← pexpr_to_pattern p, match_hypothesis_core new_p ctx meta instance : has_to_tactic_format pattern := ⟨λp, do t ← pp p.target, mo ← pp p.moutput, uo ← pp p.uoutput, u ← pp p.nuvars, m ← pp p.nmvars, return format!"pattern.mk ({t}) {uo} {mo} {u} {m}" ⟩ end tactic
35a049558fb735d3b16007ff664f20bc9f1320cc
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/584a.lean
a16e38e71fb1882f08fd0041333dccad346f99c2
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
328
lean
universe variables u variables (A : Type u) [H : inhabited A] (x : A) include H definition foo := x check foo -- A and x are explicit variables {A x} definition foo' := x check @foo' -- A is explicit, x is implicit open nat check foo nat 10 definition test : foo' = (10:nat) := rfl set_option pp.implicit true print test
fdee2e05128b6d3d2d3ee01c35d48eda86f99194
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Simp/DiscrM.lean
81389b76848898c380c55d14a0edcf336ff6e5c2
[ "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
4,398
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.LCNF.CompilerM import Lean.Compiler.LCNF.Types import Lean.Compiler.LCNF.InferType import Lean.Compiler.LCNF.Simp.Basic namespace Lean.Compiler.LCNF namespace Simp inductive CtorInfo where | ctor (val : ConstructorVal) (args : Array Arg) | /-- Natural numbers are morally constructor applications -/ natVal (n : Nat) def CtorInfo.getName : CtorInfo → Name | .ctor val _ => val.name | .natVal 0 => ``Nat.zero | .natVal _ => ``Nat.succ def CtorInfo.getNumParams : CtorInfo → Nat | .ctor val _ => val.numParams | .natVal _ => 0 def CtorInfo.getNumFields : CtorInfo → Nat | .ctor val _ => val.numFields | .natVal 0 => 0 | .natVal _ => 1 structure DiscrM.Context where /-- A mapping from discriminant to constructor application it is equal to in the current context. -/ discrCtorMap : FVarIdMap CtorInfo := {} /-- A mapping from constructor application to discriminant it is equal to in the current context. -/ ctorDiscrMap : PersistentExprMap FVarId := {} /-- Helper monad for tracking mappings from discriminant to constructor applications and back. The combinator `withDiscrCtor` should be used when visiting `cases` alternatives. -/ abbrev DiscrM := ReaderT DiscrM.Context CompilerM /-- If `fvarId` is a constructor application, returns constructor information. Remark: we use the map `discrCtorMap`. Remark: We use this method when simplifying projections and cases-constructor. -/ def findCtor? (fvarId : FVarId) : DiscrM (Option CtorInfo) := do match (← findLetDecl? fvarId) with | some { value := .value (.natVal n), .. } => return some <| .natVal n | some { value := .const declName _ args, .. } => let some (.ctorInfo val) := (← getEnv).find? declName | return none return some <| .ctor val args | some _ => return none | none => return (← read).discrCtorMap.find? fvarId def findCtorName? (fvarId : FVarId) : DiscrM (Option Name) := do let some ctorInfo ← findCtor? fvarId | return none return ctorInfo.getName /-- If `type` is an inductive datatype, return its universe levels and parameters. -/ def getIndInfo? (type : Expr) : CoreM (Option (List Level × Array Arg)) := do let type := type.headBeta let .const declName us := type.getAppFn | return none let .inductInfo info ← getConstInfo declName | return none unless type.getAppNumArgs >= info.numParams do return none let args := type.getAppArgs[:info.numParams].toArray.map fun | .fvar fvarId => .fvar fvarId | e => if e.isErased then .erased else .type e return some (us, args) /-- Execute `x` with the information that `discr = ctorName ctorFields`. We use this information to simplify nested cases on the same discriminant. -/ @[inline] def withDiscrCtorImp (discr : FVarId) (ctorName : Name) (ctorFields : Array Param) (x : DiscrM α) : DiscrM α := do let ctx ← updateCtx withReader (fun _ => ctx) x where updateCtx : DiscrM DiscrM.Context := do let ctorVal ← getConstInfoCtor ctorName let fieldArgs := ctorFields.map (Arg.fvar ·.fvarId) let ctx ← read if let some (us, params) ← getIndInfo? (← getType discr) then let ctorArgs := params ++ fieldArgs let ctorInfo := .ctor ctorVal ctorArgs let ctor := LetValue.const ctorVal.name us ctorArgs return { ctx with discrCtorMap := ctx.discrCtorMap.insert discr ctorInfo, ctorDiscrMap := ctx.ctorDiscrMap.insert ctor.toExpr discr } else -- For the discrCtor map, the constructor parameters are irrelevant for optimizations that use this information let ctorInfo := .ctor ctorVal (mkArray ctorVal.numParams Arg.erased ++ fieldArgs) return { ctx with discrCtorMap := ctx.discrCtorMap.insert discr ctorInfo } @[inline, inherit_doc withDiscrCtorImp] def withDiscrCtor [MonadFunctorT DiscrM m] (discr : FVarId) (ctorName : Name) (ctorFields : Array Param) : m α → m α := monadMap (m := DiscrM) <| withDiscrCtorImp discr ctorName ctorFields def simpCtorDiscrCore? (e : Expr) : DiscrM (Option FVarId) := do let some discr := (← read).ctorDiscrMap.find? e | return none unless eqvTypes (← getType discr) (← inferType e) do return none return some <| discr end Simp end Lean.Compiler.LCNF
b7cbf3a55c19ca08f67461d8e74368b3f25ced31
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/modular_forms/basic.lean
08a2663bcdfffda5d4875cd900d6ea72d4126476
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
11,168
lean
/- Copyright (c) 2022 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import analysis.complex.upper_half_plane.functions_bounded_at_infty import analysis.complex.upper_half_plane.topology import number_theory.modular_forms.slash_invariant_forms /-! # Modular forms This file defines modular forms and proves some basic properties about them. We begin by defining modular forms and cusp forms as extension of `slash_invariant_forms` then we define the space of modular forms, cusp forms and prove that the product of two modular forms is a modular form. -/ open complex upper_half_plane open_locale topology manifold upper_half_plane noncomputable theory local prefix `↑ₘ`:1024 := @coe _ (matrix (fin 2) (fin 2) _) _ local notation `GL(` n `, ` R `)`⁺ := matrix.GL_pos (fin n) R local notation `SL(` n `, ` R `)` := matrix.special_linear_group (fin n) R section modular_form open modular_form variables (F : Type*) (Γ : subgroup SL(2, ℤ)) (k : ℤ) open_locale modular_form set_option old_structure_cmd true /--These are `slash_invariant_form`'s that are holomophic and bounded at infinity. -/ structure modular_form extends slash_invariant_form Γ k := (holo' : mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (to_fun : ℍ → ℂ)) (bdd_at_infty' : ∀ (A : SL(2, ℤ)), is_bounded_at_im_infty (to_fun ∣[k] A)) /-- The `slash_invariant_form` associated to a `modular_form`. -/ add_decl_doc modular_form.to_slash_invariant_form /--These are `slash_invariant_form`s that are holomophic and zero at infinity. -/ structure cusp_form extends slash_invariant_form Γ k := (holo' : mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (to_fun : ℍ → ℂ)) (zero_at_infty' : ∀ (A : SL(2, ℤ)), is_zero_at_im_infty (to_fun ∣[k] A)) /-- The `slash_invariant_form` associated to a `cusp_form`. -/ add_decl_doc cusp_form.to_slash_invariant_form /--`modular_form_class F Γ k` says that `F` is a type of bundled functions that extend `slash_invariant_forms_class` by requiring that the functions be holomorphic and bounded at infinity. -/ class modular_form_class extends slash_invariant_form_class F Γ k := (holo: ∀ f : F, mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (f : ℍ → ℂ)) (bdd_at_infty : ∀ (f : F) (A : SL(2, ℤ)), is_bounded_at_im_infty (f ∣[k] A)) /--`cusp_form_class F Γ k` says that `F` is a type of bundled functions that extend `slash_invariant_forms_class` by requiring that the functions be holomorphic and zero at infinity. -/ class cusp_form_class extends slash_invariant_form_class F Γ k := (holo: ∀ f : F, mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (f : ℍ → ℂ)) (zero_at_infty : ∀ (f : F) (A : SL(2, ℤ)), is_zero_at_im_infty (f ∣[k] A)) @[priority 100] instance modular_form_class.modular_form : modular_form_class (modular_form Γ k) Γ k := { coe := modular_form.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', slash_action_eq := modular_form.slash_action_eq', holo:= modular_form.holo', bdd_at_infty := modular_form.bdd_at_infty' } @[priority 100] instance cusp_form_class.cusp_form : cusp_form_class (cusp_form Γ k) Γ k := { coe := cusp_form.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', slash_action_eq := cusp_form.slash_action_eq', holo:= cusp_form.holo', zero_at_infty := cusp_form.zero_at_infty' } variables {F Γ k} @[simp] lemma modular_form_to_fun_eq_coe {f : modular_form Γ k} : f.to_fun = (f : ℍ → ℂ) := rfl @[simp] lemma cusp_form_to_fun_eq_coe {f : cusp_form Γ k} : f.to_fun = (f : ℍ → ℂ) := rfl @[ext] theorem modular_form.ext {f g : modular_form Γ k} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h @[ext] theorem cusp_form.ext {f g : cusp_form Γ k} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h /-- Copy of a `modular_form` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def modular_form.copy (f : modular_form Γ k) (f' : ℍ → ℂ) (h : f' = ⇑f) : modular_form Γ k := { to_fun := f', slash_action_eq' := h.symm ▸ f.slash_action_eq', holo' := h.symm ▸ f.holo', bdd_at_infty' := λ A, h.symm ▸ f.bdd_at_infty' A } /-- Copy of a `cusp_form` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def cusp_form.copy (f : cusp_form Γ k) (f' : ℍ → ℂ) (h : f' = ⇑f) : cusp_form Γ k := { to_fun := f', slash_action_eq' := h.symm ▸ f.slash_action_eq', holo' := h.symm ▸ f.holo', zero_at_infty' := λ A, h.symm ▸ f.zero_at_infty' A } end modular_form namespace modular_form open slash_invariant_form variables {F : Type*} {Γ : subgroup SL(2, ℤ)} {k : ℤ} instance has_add : has_add (modular_form Γ k) := ⟨ λ f g, { holo' := f.holo'.add g.holo', bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).add (g.bdd_at_infty' A), .. (f : slash_invariant_form Γ k) + g }⟩ @[simp] lemma coe_add (f g : modular_form Γ k) : ⇑(f + g) = f + g := rfl @[simp] lemma add_apply (f g : modular_form Γ k) (z : ℍ) : (f + g) z = f z + g z := rfl instance has_zero : has_zero (modular_form Γ k) := ⟨ { holo' := (λ _, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ)), bdd_at_infty' := λ A, by simpa using zero_form_is_bounded_at_im_infty, .. (0 : slash_invariant_form Γ k) } ⟩ @[simp] lemma coe_zero : ⇑(0 : modular_form Γ k) = (0 : ℍ → ℂ) := rfl @[simp] lemma zero_apply (z : ℍ) : (0 : modular_form Γ k) z = 0 := rfl section variables {α : Type*} [has_smul α ℂ] [is_scalar_tower α ℂ ℂ] instance has_smul : has_smul α (modular_form Γ k) := ⟨ λ c f, { to_fun := c • f, holo' := by simpa using f.holo'.const_smul (c • (1 : ℂ)), bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).const_smul_left (c • (1 : ℂ)), .. c • (f : slash_invariant_form Γ k)}⟩ @[simp] lemma coe_smul (f : (modular_form Γ k)) (n : α) : ⇑(n • f) = n • f := rfl @[simp] lemma smul_apply (f : (modular_form Γ k)) (n : α) (z : ℍ) : (n • f) z = n • (f z) := rfl end instance has_neg : has_neg (modular_form Γ k) := ⟨λ f, { to_fun := -f, holo' := f.holo'.neg, bdd_at_infty':= λ A, by simpa using (f.bdd_at_infty' A).neg, .. -(f : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_neg (f : modular_form Γ k) : ⇑(-f) = -f := rfl @[simp] lemma neg_apply (f : modular_form Γ k) (z : ℍ) : (-f) z = - (f z) := rfl instance has_sub : has_sub (modular_form Γ k) := ⟨ λ f g, f + -g ⟩ @[simp] lemma coe_sub (f g : (modular_form Γ k)) : ⇑(f - g) = f - g := rfl @[simp] lemma sub_apply (f g : modular_form Γ k) (z : ℍ) : (f - g) z = f z - g z := rfl instance : add_comm_group (modular_form Γ k) := fun_like.coe_injective.add_comm_group _ rfl coe_add coe_neg coe_sub coe_smul coe_smul /--Additive coercion from `modular_form` to `ℍ → ℂ`. -/ @[simps] def coe_hom : (modular_form Γ k) →+ (ℍ → ℂ) := { to_fun := λ f, f, map_zero' := coe_zero, map_add' := λ _ _, rfl } instance : module ℂ (modular_form Γ k) := function.injective.module ℂ coe_hom fun_like.coe_injective (λ _ _, rfl) instance : inhabited (modular_form Γ k) := ⟨0⟩ /--The modular form of weight `k_1 + k_2` given by the product of two modular forms of weights `k_1` and `k_2`. -/ def mul {k_1 k_2 : ℤ} {Γ : subgroup SL(2, ℤ)} (f : (modular_form Γ k_1)) (g : (modular_form Γ k_2)) : (modular_form Γ (k_1 + k_2)) := { to_fun := f * g, slash_action_eq' := λ A, by simp_rw [mul_slash_subgroup, modular_form_class.slash_action_eq], holo' := f.holo'.mul g.holo', bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).mul (g.bdd_at_infty' A) } @[simp] lemma mul_coe {k_1 k_2 : ℤ} {Γ : subgroup SL(2, ℤ)} (f : (modular_form Γ k_1)) (g : (modular_form Γ k_2)) : ((f.mul g) : ℍ → ℂ) = f * g := rfl instance : has_one (modular_form Γ 0) := ⟨{ holo' := λ x, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ), bdd_at_infty' := λ A, by simpa using at_im_infty.const_bounded_at_filter (1:ℂ), .. (1 : slash_invariant_form Γ 0) }⟩ @[simp] lemma one_coe_eq_one : ((1 : modular_form Γ 0) : ℍ → ℂ) = 1 := rfl end modular_form namespace cusp_form open modular_form variables {F : Type*} {Γ : subgroup SL(2, ℤ)} {k : ℤ} instance has_add : has_add (cusp_form Γ k) := ⟨ λ f g, { to_fun := f + g, holo' := f.holo'.add g.holo', zero_at_infty' := λ A, by simpa using (f.zero_at_infty' A).add (g.zero_at_infty' A), .. (f : slash_invariant_form Γ k) + g }⟩ @[simp] lemma coe_add (f g : cusp_form Γ k) : ⇑(f + g) = f + g := rfl @[simp] lemma add_apply (f g : cusp_form Γ k) (z : ℍ) : (f + g) z = f z + g z := rfl instance has_zero : has_zero (cusp_form Γ k) := ⟨ { to_fun := 0, holo' := (λ _, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ)), zero_at_infty' := by simpa using filter.zero_zero_at_filter _, .. (0 : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_zero : ⇑(0 : cusp_form Γ k) = (0 : ℍ → ℂ) := rfl @[simp] lemma zero_apply (z : ℍ) : (0 : cusp_form Γ k) z = 0 := rfl section variables {α : Type*} [has_smul α ℂ] [is_scalar_tower α ℂ ℂ] instance has_smul : has_smul α (cusp_form Γ k) := ⟨ λ c f, { to_fun := c • f, holo' := by simpa using f.holo'.const_smul (c • (1 : ℂ)), zero_at_infty' := λ A, by simpa using (f.zero_at_infty' A).smul (c • (1 : ℂ)), .. c • (f : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_smul (f : (cusp_form Γ k)) (n : α) : ⇑(n • f) = n • f := rfl @[simp] lemma smul_apply (f : (cusp_form Γ k)) (n : α) {z : ℍ} : (n • f) z = n • (f z) := rfl end instance has_neg : has_neg (cusp_form Γ k) := ⟨λ f, { to_fun := -f, holo' := f.holo'.neg, zero_at_infty':= λ A, by simpa using (f.zero_at_infty' A).neg, .. -(f : slash_invariant_form Γ k)} ⟩ @[simp] lemma coe_neg (f : cusp_form Γ k) : ⇑(-f) = -f := rfl @[simp] lemma neg_apply (f : cusp_form Γ k) (z : ℍ) : (-f) z = -(f z) := rfl instance has_sub : has_sub (cusp_form Γ k) := ⟨ λ f g, f + -g ⟩ @[simp] lemma coe_sub (f g : cusp_form Γ k) : ⇑(f - g) = f - g := rfl @[simp] lemma sub_apply (f g : cusp_form Γ k) (z : ℍ) : (f - g) z = f z - g z := rfl instance : add_comm_group (cusp_form Γ k) := fun_like.coe_injective.add_comm_group _ rfl coe_add coe_neg coe_sub coe_smul coe_smul /--Additive coercion from `cusp_form` to `ℍ → ℂ`. -/ @[simps] def coe_hom : (cusp_form Γ k) →+ (ℍ → ℂ) := { to_fun := λ f, f, map_zero' := cusp_form.coe_zero, map_add' := λ _ _, rfl } instance : module ℂ (cusp_form Γ k) := function.injective.module ℂ coe_hom fun_like.coe_injective (λ _ _, rfl) instance : inhabited (cusp_form Γ k) := ⟨0⟩ @[priority 99] instance [cusp_form_class F Γ k] : modular_form_class F Γ k := { coe := fun_like.coe, coe_injective' := fun_like.coe_injective', slash_action_eq := cusp_form_class.slash_action_eq, holo:= cusp_form_class.holo, bdd_at_infty := λ _ _, (cusp_form_class.zero_at_infty _ _).bounded_at_filter} end cusp_form
56fce14e2d66399ecf4340cf9fea64b9ddfdb95a
01f6b345a06ece970e589d4bbc68ee8b9b2cf58a
/src/seminorm_from_const.lean
b03db2e0b61512f018731975fd438f8df75c7887
[]
no_license
mariainesdff/norm_extensions_journal_submission
6077acb98a7200de4553e653d81d54fb5d2314c8
d396130660935464fbc683f9aaf37fff8a890baa
refs/heads/master
1,686,685,693,347
1,684,065,115,000
1,684,065,115,000
603,823,641
0
0
null
null
null
null
UTF-8
Lean
false
false
14,954
lean
/- Copyright (c) 2023 María Inés de Frutos-Fernández. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: María Inés de Frutos-Fernández -/ import filter import ring_seminorm /-! # seminorm_from_const In this file, we prove [BGR, Proposition 1.3.2/2] : starting from a power-multiplicative seminorm on a commutative ring `R` and a nonzero `c : R`, we create a new power-multiplicative seminorm for which `c` is multiplicative. ## Main Definitions * `seminorm_from_const'` : the real-valued function sending `x ∈ R` to the limit of `(f (x * c^n))/((f c)^n)`. * `seminorm_from_const` : the function `seminorm_from_const'` is a `ring_seminorm` on `R`. ## Main Results * `seminorm_from_const_is_nonarchimedean` : the function `seminorm_from_const' hf1 hc hpm` is nonarchimedean when f is nonarchimedean. * `seminorm_from_const_is_pow_mul` : the function `seminorm_from_const' hf1 hc hpm` is power-multiplicative. * `seminorm_from_const_c_is_mul` : for every `x : R`, `seminorm_from_const' hf1 hc hpm (c * x)` equals the product `seminorm_from_const' hf1 hc hpm c * seminorm_from_const' hf1 hc hpm x`. ## References * [S. Bosch, U. Güntzer, R. Remmert, *Non-Archimedean Analysis*][bosch-guntzer-remmert] ## Tags seminorm_from_const, seminorm, nonarchimedean -/ noncomputable theory open_locale topological_space section ring variables {R : Type*} [comm_ring R] (c : R) (f : ring_seminorm R) (hf1 : f 1 ≤ 1) (hc : 0 ≠ f c) (hpm : is_pow_mul f) /-- For a ring seminorm `f` on `R` and `c ∈ R`, the sequence given by `(f (x * c^n))/((f c)^n)`. -/ def seminorm_from_const_seq (x : R) : ℕ → ℝ := λ n, (f (x * c^n))/((f c)^n) /-- The terms in the sequence `seminorm_from_const_seq c f x` are nonnegative. -/ lemma seminorm_from_const_seq_nonneg (x : R) (n : ℕ) : 0 ≤ seminorm_from_const_seq c f x n := div_nonneg (map_nonneg f (x * c ^ n)) (pow_nonneg (map_nonneg f c) n) /-- The image of `seminorm_from_const_seq c f x` is bounded below by zero. -/ lemma seminorm_from_const_is_bounded (x : R) : bdd_below (set.range (seminorm_from_const_seq c f x)) := begin use 0, rw mem_lower_bounds, intros r hr, obtain ⟨n, hn⟩ := hr, rw ← hn, exact seminorm_from_const_seq_nonneg c f x n, end variable {f} /-- `seminorm_from_const_seq c f 0` is the constant sequence zero. -/ lemma seminorm_from_const_seq_zero (hf : f 0 = 0) : seminorm_from_const_seq c f 0 = 0 := begin simp only [seminorm_from_const_seq], ext n, rw [zero_mul, hf, zero_div], refl, end variable {c} include hc hpm /-- If `1 ≤ n`, then `seminorm_from_const_seq c f 1 n = 1`. -/ lemma seminorm_from_const_seq_one (n : ℕ) (hn : 1 ≤ n) : seminorm_from_const_seq c f 1 n = 1 := begin simp only [seminorm_from_const_seq], rw [one_mul, hpm _ hn, div_self (pow_ne_zero n (ne.symm hc))], end include hf1 /-- `seminorm_from_const_seq c f x` is antitone. -/ lemma seminorm_from_const_seq_antitone (x : R) : antitone (seminorm_from_const_seq c f x) := begin intros m n hmn, simp only [seminorm_from_const_seq], nth_rewrite 0 ← nat.add_sub_of_le hmn, rw [pow_add, ← mul_assoc], have hc_pos : 0 < f c := lt_of_le_of_ne (map_nonneg f _) hc, apply le_trans ((div_le_div_right (pow_pos hc_pos _)).mpr (map_mul_le_mul f _ _)), by_cases heq : m = n, { have : n - m = 0, { rw heq, exact nat.sub_self n, }, rw [this, heq, div_le_div_right (pow_pos hc_pos _), pow_zero], conv_rhs{rw ← mul_one (f (x * c ^ n))}, exact mul_le_mul_of_nonneg_left hf1 (map_nonneg f _) }, { have h1 : 1 ≤ n - m, { rw [nat.one_le_iff_ne_zero, ne.def, nat.sub_eq_zero_iff_le, not_le], exact lt_of_le_of_ne hmn heq,}, rw [hpm c h1, mul_div_assoc, div_eq_mul_inv, pow_sub₀ _ (ne.symm hc) hmn, mul_assoc, mul_comm (f c ^ m)⁻¹, ← mul_assoc (f c ^ n), mul_inv_cancel (pow_ne_zero n (ne.symm hc)), one_mul, div_eq_mul_inv] } end /-- The real-valued function sending `x ∈ R` to the limit of `(f (x * c^n))/((f c)^n)`. -/ def seminorm_from_const' (x : R) : ℝ := classical.some (real.tendsto_of_is_bounded_antitone (seminorm_from_const_is_bounded c f x) (seminorm_from_const_seq_antitone hf1 hc hpm x)) /-- We prove that `seminorm_from_const' hf1 hc hpm x` is the limit of the sequence `seminorm_from_const_seq c f x` as `n` tends to infinity. -/ lemma seminorm_from_const_is_limit (x : R) : filter.tendsto ((seminorm_from_const_seq c f x)) filter.at_top (𝓝 (seminorm_from_const' hf1 hc hpm x)) := classical.some_spec (real.tendsto_of_is_bounded_antitone (seminorm_from_const_is_bounded c f x) (seminorm_from_const_seq_antitone hf1 hc hpm x)) /-- `seminorm_from_const' hf1 hc hpm 0 = 0`. -/ lemma seminorm_from_const_zero : seminorm_from_const' hf1 hc hpm 0 = 0 := tendsto_nhds_unique (seminorm_from_const_is_limit hf1 hc hpm 0) (by simpa [seminorm_from_const_seq_zero c (map_zero _)] using tendsto_const_nhds) /-- `seminorm_from_const' hf1 hc hpm 1 = 1`. -/ lemma seminorm_from_const_is_norm_one_class : seminorm_from_const' hf1 hc hpm 1 = 1 := begin apply tendsto_nhds_unique_of_eventually_eq (seminorm_from_const_is_limit hf1 hc hpm 1) tendsto_const_nhds, simp only [filter.eventually_eq, filter.eventually_at_top, ge_iff_le], exact ⟨1, seminorm_from_const_seq_one hc hpm⟩, end /-- `seminorm_from_const' hf1 hc hpm` is submultiplicative. -/ lemma seminorm_from_const_mul (x y : R) : seminorm_from_const' hf1 hc hpm (x * y) ≤ seminorm_from_const' hf1 hc hpm x * seminorm_from_const' hf1 hc hpm y := begin have hlim : filter.tendsto (λ n, seminorm_from_const_seq c f (x * y) (2 *n)) filter.at_top (𝓝 (seminorm_from_const' hf1 hc hpm (x * y) )), { refine filter.tendsto.comp (seminorm_from_const_is_limit hf1 hc hpm (x * y)) _, apply filter.tendsto_at_top_at_top_of_monotone, { intros n m hnm, simp only [mul_le_mul_left, nat.succ_pos', hnm], }, { rintro n, use n, linarith, }}, apply le_of_tendsto_of_tendsto' hlim (filter.tendsto.mul (seminorm_from_const_is_limit hf1 hc hpm x) (seminorm_from_const_is_limit hf1 hc hpm y)), intro n, simp only [seminorm_from_const_seq], rw [div_mul_div_comm, ← pow_add, two_mul, div_le_div_right (pow_pos (lt_of_le_of_ne (map_nonneg f _) hc) _), pow_add, ← mul_assoc, mul_comm (x * y), ← mul_assoc, mul_assoc, mul_comm (c^n)], exact map_mul_le_mul f (x * c ^ n) (y * c ^ n), end /-- `seminorm_from_const' hf1 hc hpm` is invariant under negation of `x`. -/ lemma seminorm_from_const_neg (x : R) : seminorm_from_const' hf1 hc hpm (-x) = seminorm_from_const' hf1 hc hpm x := begin apply tendsto_nhds_unique_of_eventually_eq (seminorm_from_const_is_limit hf1 hc hpm (-x)) (seminorm_from_const_is_limit hf1 hc hpm x), simp only [filter.eventually_eq, filter.eventually_at_top], use 0, intros n hn, simp only [seminorm_from_const_seq], rw [neg_mul, map_neg_eq_map], end /-- `seminorm_from_const' hf1 hc hpm` satisfies the triangle inequality. -/ lemma seminorm_from_const_add (x y : R) : seminorm_from_const' hf1 hc hpm (x + y) ≤ seminorm_from_const' hf1 hc hpm x + seminorm_from_const' hf1 hc hpm y := begin apply le_of_tendsto_of_tendsto' (seminorm_from_const_is_limit hf1 hc hpm (x + y)) (filter.tendsto.add (seminorm_from_const_is_limit hf1 hc hpm x) (seminorm_from_const_is_limit hf1 hc hpm y)), intro n, have h_add : f ((x + y) * c ^ n) ≤ (f (x * c ^ n)) + (f (y * c ^ n)), { rw add_mul, exact map_add_le_add f _ _ }, simp only [seminorm_from_const_seq], rw div_add_div_same, exact (div_le_div_right (pow_pos (lt_of_le_of_ne (map_nonneg f _) hc) _)).mpr h_add, end /-- The function `seminorm_from_const` is a `ring_seminorm` on `R`. -/ def seminorm_from_const : ring_seminorm R := { to_fun := seminorm_from_const' hf1 hc hpm, map_zero' := seminorm_from_const_zero hf1 hc hpm, add_le' := seminorm_from_const_add hf1 hc hpm, neg' := seminorm_from_const_neg hf1 hc hpm, mul_le' := seminorm_from_const_mul hf1 hc hpm } lemma seminorm_from_const_def (x : R) : seminorm_from_const hf1 hc hpm x = seminorm_from_const' hf1 hc hpm x := rfl /-- `seminorm_from_const' hf1 hc hpm 1 ≤ 1`. -/ lemma seminorm_from_const_is_norm_le_one_class : seminorm_from_const' hf1 hc hpm 1 ≤ 1 := le_of_eq (seminorm_from_const_is_norm_one_class hf1 hc hpm) /-- The function `seminorm_from_const' hf1 hc hpm` is nonarchimedean. -/ lemma seminorm_from_const_is_nonarchimedean (hna : is_nonarchimedean f) : is_nonarchimedean (seminorm_from_const' hf1 hc hpm) := begin intros x y, apply le_of_tendsto_of_tendsto' (seminorm_from_const_is_limit hf1 hc hpm (x + y)) (filter.tendsto.max (seminorm_from_const_is_limit hf1 hc hpm x) (seminorm_from_const_is_limit hf1 hc hpm y)), intro n, have hmax : f ((x + y) * c ^ n) ≤ max (f (x * c ^ n)) (f (y * c ^ n)), { rw add_mul, exact hna _ _ }, rw le_max_iff at hmax ⊢, cases hmax; [left, right]; exact (div_le_div_right (pow_pos (lt_of_le_of_ne (map_nonneg f c) hc) _)).mpr hmax, end /-- The function `seminorm_from_const' hf1 hc hpm` is power-multiplicative. -/ lemma seminorm_from_const_is_pow_mul : is_pow_mul (seminorm_from_const' hf1 hc hpm) := begin intros x m hm, simp only [seminorm_from_const'], have hpow := filter.tendsto.pow (seminorm_from_const_is_limit hf1 hc hpm x) m, have hlim : filter.tendsto (λ n, seminorm_from_const_seq c f (x^m) (m*n)) filter.at_top (𝓝 (seminorm_from_const' hf1 hc hpm (x^m) )), { refine filter.tendsto.comp (seminorm_from_const_is_limit hf1 hc hpm (x^m)) _, apply filter.tendsto_at_top_at_top_of_monotone, { intros n k hnk, exact mul_le_mul_left' hnk m, }, { rintro n, use n, exact le_mul_of_one_le_left' hm, }}, apply tendsto_nhds_unique hlim, convert filter.tendsto.pow (seminorm_from_const_is_limit hf1 hc hpm x) m, ext n, simp only [seminorm_from_const_seq], rw [div_pow, ← hpm _ hm, ← pow_mul, mul_pow, ← pow_mul, mul_comm m n], end /-- The function `seminorm_from_const' hf1 hc hpm` is bounded above by `x`. -/ lemma seminorm_from_const_le_seminorm (x : R) : seminorm_from_const' hf1 hc hpm x ≤ f x := begin apply le_of_tendsto (seminorm_from_const_is_limit hf1 hc hpm x), simp only [filter.eventually_at_top, ge_iff_le], use 1, rintros n hn, apply le_trans ((div_le_div_right (pow_pos (lt_of_le_of_ne (map_nonneg f c) hc) _)).mpr (map_mul_le_mul _ _ _)), rw [hpm c hn, mul_div_assoc, div_self (pow_ne_zero n hc.symm), mul_one], end /-- If `x : R` is multiplicative for `f`, then `seminorm_from_const' hf1 hc hpm x = f x`. -/ lemma seminorm_from_const_apply_of_is_mul {x : R} (hx : ∀ y : R, f (x * y) = f x * f y) : seminorm_from_const' hf1 hc hpm x = f x := begin have hlim : filter.tendsto (seminorm_from_const_seq c f x) filter.at_top (𝓝 (f x)), { have hseq : seminorm_from_const_seq c f x = λ n, f x, { ext n, by_cases hn : n = 0, { simp only [seminorm_from_const_seq], rw [hn, pow_zero, pow_zero, mul_one, div_one], }, { simp only [seminorm_from_const_seq], rw [hx (c ^n), hpm _ (nat.one_le_iff_ne_zero.mpr hn), mul_div_assoc, div_self (pow_ne_zero n hc.symm), mul_one], }}, simpa [hseq] using tendsto_const_nhds, }, exact tendsto_nhds_unique (seminorm_from_const_is_limit hf1 hc hpm x) hlim, end /-- If `x : R` is multiplicative for `f`, then it is multiplicative for `seminorm_from_const' hf1 hc hpm`. -/ lemma seminorm_from_const_is_mul_of_is_mul {x : R} (hx : ∀ y : R, f (x * y) = f x * f y) (y : R) : seminorm_from_const' hf1 hc hpm (x * y) = seminorm_from_const' hf1 hc hpm x * seminorm_from_const' hf1 hc hpm y := begin have hlim : filter.tendsto (seminorm_from_const_seq c f (x * y)) filter.at_top (𝓝 (seminorm_from_const' hf1 hc hpm x * seminorm_from_const' hf1 hc hpm y)), { rw seminorm_from_const_apply_of_is_mul hf1 hc hpm hx, have hseq : seminorm_from_const_seq c f (x * y) = λ n, f x * seminorm_from_const_seq c f y n, { ext n, simp only [seminorm_from_const_seq], rw [mul_assoc, hx, mul_div_assoc], }, simpa [hseq] using filter.tendsto.const_mul _(seminorm_from_const_is_limit hf1 hc hpm y) }, exact tendsto_nhds_unique (seminorm_from_const_is_limit hf1 hc hpm (x * y)) hlim, end /-- `seminorm_from_const' hf1 hc hpm c = f c`. -/ lemma seminorm_from_const_apply_c : seminorm_from_const' hf1 hc hpm c = f c := begin have hlim : filter.tendsto (seminorm_from_const_seq c f c) filter.at_top (𝓝 (f c)), { have hseq : seminorm_from_const_seq c f c = λ n, f c, { ext n, simp only [seminorm_from_const_seq], rw [← pow_succ, hpm _ le_add_self, pow_succ, mul_div_assoc, div_self (pow_ne_zero n hc.symm), mul_one], }, simpa [hseq] using tendsto_const_nhds }, exact tendsto_nhds_unique (seminorm_from_const_is_limit hf1 hc hpm c) hlim, end /-- For every `x : R`, `seminorm_from_const' hf1 hc hpm (c * x)` equals the product `seminorm_from_const' hf1 hc hpm c * seminorm_from_const' hf1 hc hpm x`. -/ lemma seminorm_from_const_c_is_mul (x : R) : seminorm_from_const' hf1 hc hpm (c * x) = seminorm_from_const' hf1 hc hpm c * seminorm_from_const' hf1 hc hpm x := begin have hlim : filter.tendsto (λ n, seminorm_from_const_seq c f x (n + 1)) filter.at_top (𝓝 (seminorm_from_const' hf1 hc hpm x)), { refine filter.tendsto.comp (seminorm_from_const_is_limit hf1 hc hpm x) _, apply filter.tendsto_at_top_at_top_of_monotone, { intros n m hnm, exact add_le_add_right hnm 1, }, { rintro n, use n, linarith, }}, rw seminorm_from_const_apply_c hf1 hc hpm, apply tendsto_nhds_unique (seminorm_from_const_is_limit hf1 hc hpm (c * x)), have hterm : seminorm_from_const_seq c f (c * x) = (λ n, f c * (seminorm_from_const_seq c f x (n + 1))), { simp only [seminorm_from_const_seq], ext n, rw [mul_comm c, pow_succ, pow_succ, mul_div, div_eq_mul_inv _ (f c * f c ^ n), mul_inv, ← mul_assoc, mul_comm (f c), mul_assoc _ (f c), mul_inv_cancel hc.symm, mul_one, mul_assoc, div_eq_mul_inv] }, simpa [hterm] using filter.tendsto.mul tendsto_const_nhds hlim, end end ring section field variables {K : Type*} [field K] /-- If `K` is a field, the function `seminorm_from_const` is a `ring_norm` on `K`. -/ def seminorm_from_const_ring_norm_of_field {k : K} {g : ring_seminorm K} (hg1 : g 1 ≤ 1) (hg_k : g k ≠ 0) (hg_pm : is_pow_mul g) : ring_norm K := (seminorm_from_const hg1 hg_k.symm hg_pm).to_ring_norm (ring_seminorm.ne_zero_iff.mpr⟨k, by simpa [seminorm_from_const_def, seminorm_from_const_apply_c] using hg_k⟩) lemma seminorm_from_const_ring_norm_of_field_def {k : K} {g : ring_seminorm K} (hg1 : g 1 ≤ 1) (hg_k : g k ≠ 0) (hg_pm : is_pow_mul g) (x : K) : seminorm_from_const_ring_norm_of_field hg1 hg_k hg_pm x = seminorm_from_const' hg1 hg_k.symm hg_pm x := rfl end field
89dcfb25c6ee387e9dd55a04da970c216969edf3
968e2f50b755d3048175f176376eff7139e9df70
/examples/pred_logic/unnamed_52.lean
75677ab2ff2a89dc92fc411d07ee620838305180
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
69
lean
def avg (x y : ℤ) : ℤ := (x + y)/2 -- BEGIN #eval avg 10 6 -- END
97b3fba1eea5dd4b0d69ce6be75dfe729f90af26
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/instanceWhere.lean
85a10d65e59aa88469520c0783e1956128780981
[ "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
534
lean
namespace Exp universes u v w def StateT' (σ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) := σ → m (α × σ) instance [Monad m] : Monad (StateT' σ m) where pure a := fun s => pure (a, s) bind x f := fun s => do let (a, s) ← x s f a s map f x := fun s => do let (a, s) ← x s pure (f a, s) instance [ToString α] [ToString β] : ToString (Sum α β) where toString : Sum α β → String | Sum.inr a => "inl" ++ toString a | Sum.inl b => "inr" ++ toString b end Exp
0d31f7297d1837530e4e5ac680f6f7e3ae5b5be7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/alias.lean
2e65ab64aa0553c7cd9776f6790820b4fe21dccb
[ "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
340
lean
import logic namespace N1 constant num : Type.{1} constant foo : num → num → num end N1 namespace N2 constant val : Type.{1} constant foo : val → val → val end N2 open N2 open N1 constants a b : num print raw foo a b open N2 print raw foo a b open N1 print raw foo a b open N1 print raw foo a b open N2 print raw foo a b
2d41f4322af602178a5cd5f2384d93d7dbf3924c
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/monoidal_categories/drinfeld_centre.lean
fa54598ddc42309ea21770df0ad880fce26ae6b5
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
3,113
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .braided_monoidal_category import .tensor_with_object --set_option pp.universes true open tqft.categories open tqft.categories.functor open tqft.categories.products open tqft.categories.natural_transformation open tqft.categories.monoidal_category namespace tqft.categories.drinfeld_centre structure {u v} HalfBraiding { C : Category.{u v} } ( m : MonoidalStructure C ) := (object : C.Obj) (commutor : NaturalIsomorphism (m.tensor_on_left object) (m.tensor_on_right object)) definition {u v} HalfBraiding_coercion_to_object { C : Category.{u v} } ( m : MonoidalStructure C ) : has_coe (HalfBraiding m) (C.Obj) := { coe := HalfBraiding.object } attribute [instance] HalfBraiding_coercion_to_object structure {u v} HalfBraidingMorphism { C : Category.{u v} } { m : MonoidalStructure C } ( X Y : HalfBraiding m ) := (morphism : C.Hom X Y) -- FIXME I've had to write out the statement gorily, so that it can match. -- (witness : ∀ Z : C.Obj, C.compose (X.commutor Z) (m.tensorMorphisms (C.identity Z) morphism) = C.compose (m.tensorMorphisms morphism (C.identity Z)) (Y.commutor Z)) (witness : ∀ Z : C.Obj, C.compose (X.commutor.morphism.components Z) (@Functor.onMorphisms _ _ m.tensor (Z, X) (Z, Y) (C.identity Z, morphism)) = C.compose (@Functor.onMorphisms _ _ m.tensor (X, Z) (Y, Z) (morphism, C.identity Z)) (Y.commutor Z)) attribute [ematch] HalfBraidingMorphism.witness @[pointwise] lemma HalfBraidingMorphism_equal { C : Category } { m : MonoidalStructure C } { X Y : HalfBraiding m } { f g : HalfBraidingMorphism X Y } ( w : f.morphism = g.morphism ) : f = g := begin induction f, induction g, blast end local attribute [ematch] MonoidalStructure.interchange_right_identity MonoidalStructure.interchange_left_identity -- set_option pp.implicit true -- set_option pp.all true local attribute [ematch] MonoidalStructure.interchange_right_identity local attribute [ematch] MonoidalStructure.interchange_left_identity definition {u v} DrinfeldCentre { C : Category.{u v} } ( m : MonoidalStructure C ) : Category := { Obj := HalfBraiding m, Hom := λ X Y, HalfBraidingMorphism X Y, identity := λ X, { morphism := C.identity X, witness := ♯ }, compose := λ P Q R f g, { morphism := C.compose f.morphism g.morphism, witness := begin intros, dsimp, rewrite m.interchange_right_identity, rewrite m.interchange_left_identity, erewrite - C.associativity, rewrite f.witness, rewrite C.associativity, -- blast, erewrite g.witness, -- blast, -- FIXME these blasts are unfolding too many implicit arguments. -- https://github.com/leanprover/lean/issues/1509 erewrite - C.associativity, trivial end }, left_identity := ♯, right_identity := ♯, associativity := ♯ } end tqft.categories.drinfeld_centre
11c888b690babba326058321e198aec773a44535
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/cyclotomic/primitive_roots.lean
3be3b8787f591f8ed7bce18b2675e0c85e0645ca
[ "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
25,042
lean
/- Copyright (c) 2022 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Best, Riccardo Brasca, Eric Rodriguez -/ import data.pnat.prime import algebra.is_prime_pow import number_theory.cyclotomic.basic import ring_theory.adjoin.power_basis import ring_theory.polynomial.cyclotomic.eval import ring_theory.norm /-! # Primitive roots in cyclotomic fields If `is_cyclotomic_extension {n} A B`, we define an element `zeta n A B : B` that is (under certain assumptions) a primitive `n`-root of unity in `B` and we study its properties. We also prove related theorems under the more general assumption of just being a primitive root, for reasons described in the implementation details section. ## Main definitions * `is_cyclotomic_extension.zeta n A B`: if `is_cyclotomic_extension {n} A B`, than `zeta n A B` is an element of `B` that plays the role of a primitive `n`-th root of unity. * `is_primitive_root.power_basis`: if `K` and `L` are fields such that `is_cyclotomic_extension {n} K L`, then `is_primitive_root.power_basis` gives a K-power basis for L given a primitive root `ζ`. * `is_primitive_root.embeddings_equiv_primitive_roots`: the equivalence between `L →ₐ[K] A` and `primitive_roots n A` given by the choice of `ζ`. ## Main results * `is_cyclotomic_extension.zeta_primitive_root`: `zeta n A B` is a primitive `n`-th root of unity. * `is_cyclotomic_extension.finrank`: if `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the `finrank` of a cyclotomic extension is `n.totient`. * `is_primitive_root.norm_eq_one`: if `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), the norm of a primitive root is `1` if `n ≠ 2`. * `is_primitive_root.sub_one_norm_eq_eval_cyclotomic`: if `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `eval 1 (cyclotomic n ℤ)`, for a primitive root ζ. We also prove the analogous of this result for `zeta`. * `is_primitive_root.pow_sub_one_norm_prime_pow_ne_two` : if `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` `p ^ (k - s + 1) ≠ 2`. See the following lemmas for similar results. We also prove the analogous of this result for `zeta`. * `is_primitive_root.sub_one_norm_prime_ne_two` : if `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. We also prove the analogous of this result for `zeta`. * `is_primitive_root.embeddings_equiv_primitive_roots`: the equivalence between `L →ₐ[K] A` and `primitive_roots n A` given by the choice of `ζ`. ## Implementation details `zeta n A B` is defined as any primitive root of unity in `B`, that exists by definition. It is not true in general that it is a root of `cyclotomic n B`, but this holds if `is_domain B` and `ne_zero (↑n : B)`. `zeta n A B` is defined using `exists.some`, which means we cannot control it. For example, in normal mathematics, we can demand that `(zeta p ℤ ℤ[ζₚ] : ℚ(ζₚ))` is equal to `zeta p ℚ ℚ(ζₚ)`, as we are just choosing "an arbitrary primitive root" and we can internally specify that our choices agree. This is not the case here, and it is indeed impossible to prove that these two are equal. Therefore, whenever possible, we prove our results for any primitive root, and only at the "final step", when we need to provide an "explicit" primitive root, we use `zeta`. -/ open polynomial algebra finset finite_dimensional is_cyclotomic_extension nat pnat set universes u v w z variables {p n : ℕ+} (A : Type w) (B : Type z) (K : Type u) {L : Type v} (C : Type w) variables [comm_ring A] [comm_ring B] [algebra A B] [is_cyclotomic_extension {n} A B] section zeta namespace is_cyclotomic_extension variables (n) /-- If `B` is a `n`-th cyclotomic extension of `A`, then `zeta n A B` is a primitive root of unity in `B`. -/ noncomputable def zeta : B := (exists_prim_root A $ set.mem_singleton n : ∃ r : B, is_primitive_root r n).some /-- `zeta n A B` is a primitive `n`-th root of unity. -/ @[simp] lemma zeta_spec : is_primitive_root (zeta n A B) n := classical.some_spec (exists_prim_root A (set.mem_singleton n) : ∃ r : B, is_primitive_root r n) lemma aeval_zeta [is_domain B] [ne_zero ((n : ℕ) : B)] : aeval (zeta n A B) (cyclotomic n A) = 0 := begin rw [aeval_def, ← eval_map, ← is_root.def, map_cyclotomic, is_root_cyclotomic_iff], exact zeta_spec n A B end lemma zeta_is_root [is_domain B] [ne_zero ((n : ℕ) : B)] : is_root (cyclotomic n B) (zeta n A B) := by { convert aeval_zeta n A B, rw [is_root.def, aeval_def, eval₂_eq_eval_map, map_cyclotomic] } lemma zeta_pow : (zeta n A B) ^ (n : ℕ) = 1 := (zeta_spec n A B).pow_eq_one end is_cyclotomic_extension end zeta section no_order variables [field K] [comm_ring L] [is_domain L] [algebra K L] [is_cyclotomic_extension {n} K L] {ζ : L} (hζ : is_primitive_root ζ n) namespace is_primitive_root variable {C} /-- The `power_basis` given by a primitive root `η`. -/ @[simps] protected noncomputable def power_basis : power_basis K L := power_basis.map (algebra.adjoin.power_basis $ integral {n} K L ζ) $ (subalgebra.equiv_of_eq _ _ (is_cyclotomic_extension.adjoin_primitive_root_eq_top hζ)).trans subalgebra.top_equiv lemma power_basis_gen_mem_adjoin_zeta_sub_one : (hζ.power_basis K).gen ∈ adjoin K ({ζ - 1} : set L) := begin rw [power_basis_gen, adjoin_singleton_eq_range_aeval, alg_hom.mem_range], exact ⟨X + 1, by simp⟩ end /-- The `power_basis` given by `η - 1`. -/ @[simps] noncomputable def sub_one_power_basis : power_basis K L := (hζ.power_basis K).of_gen_mem_adjoin (is_integral_sub (is_cyclotomic_extension.integral {n} K L ζ) is_integral_one) (hζ.power_basis_gen_mem_adjoin_zeta_sub_one _) variables {K} (C) /-- The equivalence between `L →ₐ[K] C` and `primitive_roots n C` given by a primitive root `ζ`. -/ @[simps] noncomputable def embeddings_equiv_primitive_roots (C : Type*) [comm_ring C] [is_domain C] [algebra K C] (hirr : irreducible (cyclotomic n K)) : (L →ₐ[K] C) ≃ primitive_roots n C := ((hζ.power_basis K).lift_equiv).trans { to_fun := λ x, begin haveI := is_cyclotomic_extension.ne_zero' n K L, haveI hn := ne_zero.of_no_zero_smul_divisors K C n, refine ⟨x.1, _⟩, cases x, rwa [mem_primitive_roots n.pos, ←is_root_cyclotomic_iff, is_root.def, ←map_cyclotomic _ (algebra_map K C), hζ.minpoly_eq_cyclotomic_of_irreducible hirr, ←eval₂_eq_eval_map, ←aeval_def] end, inv_fun := λ x, begin haveI := is_cyclotomic_extension.ne_zero' n K L, haveI hn := ne_zero.of_no_zero_smul_divisors K C n, refine ⟨x.1, _⟩, cases x, rwa [aeval_def, eval₂_eq_eval_map, hζ.power_basis_gen K, ←hζ.minpoly_eq_cyclotomic_of_irreducible hirr, map_cyclotomic, ←is_root.def, is_root_cyclotomic_iff, ← mem_primitive_roots n.pos] end, left_inv := λ x, subtype.ext rfl, right_inv := λ x, subtype.ext rfl } end is_primitive_root namespace is_cyclotomic_extension variables {K} (L) /-- If `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the `finrank` of a cyclotomic extension is `n.totient`. -/ lemma finrank (hirr : irreducible (cyclotomic n K)) : finrank K L = (n : ℕ).totient := begin haveI := is_cyclotomic_extension.ne_zero' n K L, rw [((zeta_spec n K L).power_basis K).finrank, is_primitive_root.power_basis_dim, ←(zeta_spec n K L).minpoly_eq_cyclotomic_of_irreducible hirr, nat_degree_cyclotomic] end end is_cyclotomic_extension end no_order section norm namespace is_primitive_root variables [field L] {ζ : L} (hζ : is_primitive_root ζ n) variables {K} [field K] [algebra K L] /-- This mathematically trivial result is complementary to `norm_eq_one` below. -/ lemma norm_eq_neg_one_pow (hζ : is_primitive_root ζ 2) : norm K ζ = (-1) ^ finrank K L := by rw [hζ.eq_neg_one_of_two_right , show -1 = algebra_map K L (-1), by simp, algebra.norm_algebra_map] include hζ /-- If `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), the norm of a primitive root is `1` if `n ≠ 2`. -/ lemma norm_eq_one [is_cyclotomic_extension {n} K L] (hn : n ≠ 2) (hirr : irreducible (cyclotomic n K)) : norm K ζ = 1 := begin haveI := is_cyclotomic_extension.ne_zero' n K L, by_cases h1 : n = 1, { rw [h1, one_coe, one_right_iff] at hζ, rw [hζ, show 1 = algebra_map K L 1, by simp, algebra.norm_algebra_map, one_pow] }, { replace h1 : 2 ≤ n, { by_contra' h, exact h1 (pnat.eq_one_of_lt_two h) }, rw [← hζ.power_basis_gen K, power_basis.norm_gen_eq_coeff_zero_minpoly, hζ.power_basis_gen K, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, cyclotomic_coeff_zero _ h1, mul_one, hζ.power_basis_dim K, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, nat_degree_cyclotomic], exact (totient_even $ h1.lt_of_ne hn.symm).neg_one_pow } end /-- If `K` is linearly ordered, the norm of a primitive root is `1` if `n` is odd. -/ lemma norm_eq_one_of_linearly_ordered {K : Type*} [linear_ordered_field K] [algebra K L] (hodd : odd (n : ℕ)) : norm K ζ = 1 := begin have hz := congr_arg (norm K) ((is_primitive_root.iff_def _ n).1 hζ).1, rw [←(algebra_map K L).map_one , algebra.norm_algebra_map, one_pow, map_pow, ←one_pow ↑n] at hz, exact strict_mono.injective hodd.strict_mono_pow hz end lemma norm_of_cyclotomic_irreducible [is_cyclotomic_extension {n} K L] (hirr : irreducible (cyclotomic n K)) : norm K ζ = ite (n = 2) (-1) 1 := begin split_ifs with hn, { unfreezingI {subst hn}, convert norm_eq_neg_one_pow hζ, erw [is_cyclotomic_extension.finrank _ hirr, totient_two, pow_one], all_goals { apply_instance } }, { exact hζ.norm_eq_one hn hirr } end /-- If `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `eval 1 (cyclotomic n ℤ)`. -/ lemma sub_one_norm_eq_eval_cyclotomic [is_cyclotomic_extension {n} K L] (h : 2 < (n : ℕ)) (hirr : irreducible (cyclotomic n K)) : norm K (ζ - 1) = ↑(eval 1 (cyclotomic n ℤ)) := begin haveI := is_cyclotomic_extension.ne_zero' n K L, let E := algebraic_closure L, obtain ⟨z, hz⟩ := is_alg_closed.exists_root _ (degree_cyclotomic_pos n E n.pos).ne.symm, apply (algebra_map K E).injective, letI := finite_dimensional {n} K L, letI := is_galois n K L, rw [norm_eq_prod_embeddings], conv_lhs { congr, skip, funext, rw [← neg_sub, alg_hom.map_neg, alg_hom.map_sub, alg_hom.map_one, neg_eq_neg_one_mul] }, rw [prod_mul_distrib, prod_const, card_univ, alg_hom.card, is_cyclotomic_extension.finrank L hirr, (totient_even h).neg_one_pow, one_mul], have : finset.univ.prod (λ (σ : L →ₐ[K] E), 1 - σ ζ) = eval 1 (cyclotomic' n E), { rw [cyclotomic', eval_prod, ← @finset.prod_attach E E, ← univ_eq_attach], refine fintype.prod_equiv (hζ.embeddings_equiv_primitive_roots E hirr) _ _ (λ σ, _), simp }, haveI : ne_zero ((n : ℕ) : E) := (ne_zero.of_no_zero_smul_divisors K _ (n : ℕ)), rw [this, cyclotomic', ← cyclotomic_eq_prod_X_sub_primitive_roots (is_root_cyclotomic_iff.1 hz), ← map_cyclotomic_int, (algebra_map K E).map_int_cast, ←int.cast_one, eval_int_cast_map, ring_hom.eq_int_cast, int.cast_id] end /-- If `is_prime_pow (n : ℕ)`, `n ≠ 2` and `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `(n : ℕ).min_fac`. -/ lemma sub_one_norm_is_prime_pow (hn : is_prime_pow (n : ℕ)) [is_cyclotomic_extension {n} K L] (hirr : irreducible (cyclotomic (n : ℕ) K)) (h : n ≠ 2) : norm K (ζ - 1) = (n : ℕ).min_fac := begin have := (coe_lt_coe 2 _).1 (lt_of_le_of_ne (succ_le_of_lt (is_prime_pow.one_lt hn)) (function.injective.ne pnat.coe_injective h).symm), letI hprime : fact ((n : ℕ).min_fac.prime) := ⟨min_fac_prime (is_prime_pow.ne_one hn)⟩, rw [sub_one_norm_eq_eval_cyclotomic hζ this hirr], nth_rewrite 0 [← is_prime_pow.min_fac_pow_factorization_eq hn], obtain ⟨k, hk⟩ : ∃ k, ((n : ℕ).factorization) (n : ℕ).min_fac = k + 1 := exists_eq_succ_of_ne_zero (((n : ℕ).factorization.mem_support_to_fun (n : ℕ).min_fac).1 $ factor_iff_mem_factorization.2 $ (mem_factors (is_prime_pow.ne_zero hn)).2 ⟨hprime.out, min_fac_dvd _⟩), simp [hk, sub_one_norm_eq_eval_cyclotomic hζ this hirr], end omit hζ variable {A} lemma minpoly_sub_one_eq_cyclotomic_comp [algebra K A] [is_domain A] {ζ : A} [is_cyclotomic_extension {n} K A] (hζ : is_primitive_root ζ n) (h : irreducible (polynomial.cyclotomic n K)) : minpoly K (ζ - 1) = (cyclotomic n K).comp (X + 1) := begin haveI := is_cyclotomic_extension.ne_zero' n K A, rw [show ζ - 1 = ζ + (algebra_map K A (-1)), by simp [sub_eq_add_neg], minpoly.add_algebra_map (is_cyclotomic_extension.integral {n} K A ζ), hζ.minpoly_eq_cyclotomic_of_irreducible h], simp end local attribute [instance] is_cyclotomic_extension.finite_dimensional local attribute [instance] is_cyclotomic_extension.is_galois /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `p ^ (k - s + 1) ≠ 2`. See the next lemmas for similar results. -/ lemma pow_sub_one_norm_prime_pow_ne_two {k s : ℕ} (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) [hpri : fact (p : ℕ).prime] [is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (hs : s ≤ k) (htwo : p ^ (k - s + 1) ≠ 2) : norm K (ζ ^ ((p : ℕ) ^ s) - 1) = p ^ ((p : ℕ) ^ s) := begin have hirr₁ : irreducible (cyclotomic (p ^ (k - s + 1)) K) := cyclotomic_irreducible_pow_of_irreducible_pow hpri.1 (by linarith) hirr, rw ←pnat.pow_coe at hirr₁, let η := ζ ^ ((p : ℕ) ^ s) - 1, let η₁ : K⟮η⟯ := intermediate_field.adjoin_simple.gen K η, have hη : is_primitive_root (η + 1) (p ^ (k + 1 - s)), { rw [sub_add_cancel], refine is_primitive_root.pow (p ^ (k + 1)).pos hζ _, rw [pnat.pow_coe, ← pow_add, add_comm s, nat.sub_add_cancel (le_trans hs (nat.le_succ k))] }, haveI : is_cyclotomic_extension {p ^ (k - s + 1)} K K⟮η⟯, { suffices : is_cyclotomic_extension {p ^ (k - s + 1)} K K⟮η + 1⟯.to_subalgebra, { have H : K⟮η + 1⟯.to_subalgebra = K⟮η⟯.to_subalgebra, { simp only [intermediate_field.adjoin_simple_to_subalgebra_of_integral _ _ (is_cyclotomic_extension.integral {p ^ (k + 1)} K L _)], refine subalgebra.ext (λ x, ⟨λ hx, adjoin_le _ hx, λ hx, adjoin_le _ hx⟩), { simp only [set.singleton_subset_iff, set_like.mem_coe], exact subalgebra.add_mem _ (subset_adjoin (mem_singleton η)) (subalgebra.one_mem _) }, { simp only [set.singleton_subset_iff, set_like.mem_coe], nth_rewrite 0 [← add_sub_cancel η 1], refine subalgebra.sub_mem _ (subset_adjoin (mem_singleton _)) (subalgebra.one_mem _) } }, rw [H] at this, exact this }, rw [intermediate_field.adjoin_simple_to_subalgebra_of_integral _ _ (is_cyclotomic_extension.integral {p ^ (k + 1)} K L _)], have hη' : is_primitive_root (η + 1) ↑(p ^ (k + 1 - s)) := by simpa using hη, convert hη'.adjoin_is_cyclotomic_extension K, rw [nat.sub_add_comm hs] }, replace hη : is_primitive_root (η₁ + 1) ↑(p ^ (k - s + 1)), { apply coe_submonoid_class_iff.1, convert hη, rw [nat.sub_add_comm hs, pow_coe] }, rw [norm_eq_norm_adjoin K], { have H := hη.sub_one_norm_is_prime_pow _ hirr₁ htwo, swap, { rw [pnat.pow_coe], exact hpri.1.is_prime_pow.pow (nat.succ_ne_zero _) }, rw [add_sub_cancel] at H, rw [H, coe_coe], congr, { rw [pnat.pow_coe, nat.pow_min_fac, hpri.1.min_fac_eq], exact nat.succ_ne_zero _ }, have := finite_dimensional.finrank_mul_finrank K K⟮η⟯ L, rw [is_cyclotomic_extension.finrank L hirr, is_cyclotomic_extension.finrank K⟮η⟯ hirr₁, pnat.pow_coe, pnat.pow_coe, nat.totient_prime_pow hpri.out (k - s).succ_pos, nat.totient_prime_pow hpri.out k.succ_pos, mul_comm _ (↑p - 1), mul_assoc, mul_comm (↑p ^ (k.succ - 1))] at this, replace this := nat.eq_of_mul_eq_mul_left (tsub_pos_iff_lt.2 (nat.prime.one_lt hpri.out)) this, have Hex : k.succ - 1 = (k - s).succ - 1 + s, { simp only [nat.succ_sub_succ_eq_sub, tsub_zero], exact (nat.sub_add_cancel hs).symm }, rw [Hex, pow_add] at this, exact nat.eq_of_mul_eq_mul_left (pow_pos hpri.out.pos _) this }, all_goals { apply_instance } end /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `p ≠ 2`. -/ lemma pow_sub_one_norm_prime_ne_two {k : ℕ} (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) [hpri : fact (p : ℕ).prime] [is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) {s : ℕ} (hs : s ≤ k) (hodd : p ≠ 2) : norm K (ζ ^ ((p : ℕ) ^ s) - 1) = p ^ ((p : ℕ) ^ s) := begin refine hζ.pow_sub_one_norm_prime_pow_ne_two hirr hs (λ h, _), rw [← pnat.coe_inj, pnat.coe_bit0, pnat.one_coe, pnat.pow_coe, ← pow_one 2] at h, replace h := eq_of_prime_pow_eq (prime_iff.1 hpri.out) (prime_iff.1 nat.prime_two) ((k - s).succ_pos) h, rw [← pnat.one_coe, ← pnat.coe_bit0, pnat.coe_inj] at h, exact hodd h end /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. -/ lemma sub_one_norm_prime_ne_two {k : ℕ} (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) [hpri : fact (p : ℕ).prime] [is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (h : p ≠ 2) : norm K (ζ - 1) = p := by simpa using hζ.pow_sub_one_norm_prime_ne_two hirr k.zero_le h /-- If `irreducible (cyclotomic p K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. -/ lemma sub_one_norm_prime [hpri : fact (p : ℕ).prime] [hcyc : is_cyclotomic_extension {p} K L] (hζ: is_primitive_root ζ p) (hirr : irreducible (cyclotomic p K)) (h : p ≠ 2) : norm K (ζ - 1) = p := begin replace hirr : irreducible (cyclotomic (↑(p ^ (0 + 1)) : ℕ) K) := by simp [hirr], replace hζ : is_primitive_root ζ (↑(p ^ (0 + 1)) : ℕ) := by simp [hζ], haveI : is_cyclotomic_extension {p ^ (0 + 1)} K L := by simp [hcyc], simpa using sub_one_norm_prime_ne_two hζ hirr h end /-- If `irreducible (cyclotomic (2 ^ (k + 1)) K)` (in particular for `K = ℚ`), then the norm of `ζ ^ (2 ^ k) - 1` is `(-2) ^ (2 ^ k)`. -/ lemma pow_sub_one_norm_two {k : ℕ} (hζ : is_primitive_root ζ (2 ^ (k + 1))) [is_cyclotomic_extension {2 ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (2 ^ (k + 1)) K)) : norm K (ζ ^ (2 ^ k) - 1) = (-2) ^ (2 ^ k) := begin have := hζ.pow_of_dvd (λ h, two_ne_zero (pow_eq_zero h)) (pow_dvd_pow 2 (le_succ k)), rw [nat.pow_div (le_succ k) zero_lt_two, nat.succ_sub (le_refl k), nat.sub_self, pow_one] at this, have H : (-1 : L) - (1 : L) = algebra_map K L (-2), { simp only [_root_.map_neg, map_bit0, _root_.map_one], ring }, replace hirr : irreducible (cyclotomic (2 ^ (k + 1) : ℕ+) K) := by simp [hirr], rw [this.eq_neg_one_of_two_right, H, algebra.norm_algebra_map, is_cyclotomic_extension.finrank L hirr, pow_coe, pnat.coe_bit0, one_coe, totient_prime_pow nat.prime_two (zero_lt_succ k), succ_sub_succ_eq_sub, tsub_zero, mul_one] end /-- If `irreducible (cyclotomic (2 ^ k) K)` (in particular for `K = ℚ`) and `k` is at least `2`, then the norm of `ζ - 1` is `2`. -/ lemma sub_one_norm_two {k : ℕ} (hζ : is_primitive_root ζ (2 ^ k)) (hk : 2 ≤ k) [H : is_cyclotomic_extension {2 ^ k} K L] (hirr : irreducible (cyclotomic (2 ^ k) K)) : norm K (ζ - 1) = 2 := begin have : 2 < (2 ^ k : ℕ+), { simp only [← coe_lt_coe, pnat.coe_bit0, one_coe, pow_coe], nth_rewrite 0 [← pow_one 2], exact pow_lt_pow one_lt_two (lt_of_lt_of_le one_lt_two hk) }, replace hirr : irreducible (cyclotomic (2 ^ k : ℕ+) K) := by simp [hirr], replace hζ : is_primitive_root ζ (2 ^ k : ℕ+) := by simp [hζ], obtain ⟨k₁, hk₁⟩ := exists_eq_succ_of_ne_zero ((lt_of_lt_of_le zero_lt_two hk).ne.symm), simpa [hk₁] using sub_one_norm_eq_eval_cyclotomic hζ this hirr, end /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `1 ≤ k`. -/ lemma pow_sub_one_norm_prime_pow_of_one_le {k s : ℕ} (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) [hpri : fact (p : ℕ).prime] [hcycl : is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (hs : s ≤ k) (hk : 1 ≤ k) : norm K (ζ ^ ((p : ℕ) ^ s) - 1) = p ^ ((p : ℕ) ^ s) := begin by_cases htwo : p ^ (k - s + 1) = 2, { have hp : p = 2, { rw [← pnat.coe_inj, pnat.coe_bit0, pnat.one_coe, pnat.pow_coe, ← pow_one 2] at htwo, replace htwo := eq_of_prime_pow_eq (prime_iff.1 hpri.out) (prime_iff.1 nat.prime_two) (succ_pos _) htwo, rwa [show 2 = ((2 : ℕ+) : ℕ), by simp, pnat.coe_inj] at htwo }, replace hs : s = k, { rw [hp, ← pnat.coe_inj, pnat.pow_coe, pnat.coe_bit0, pnat.one_coe] at htwo, nth_rewrite 1 [← pow_one 2] at htwo, replace htwo := nat.pow_right_injective rfl.le htwo, rw [add_left_eq_self, nat.sub_eq_zero_iff_le] at htwo, refine le_antisymm hs htwo }, simp only [hs, hp, pnat.coe_bit0, one_coe, coe_coe, cast_bit0, cast_one, pow_coe] at ⊢ hζ hirr hcycl, haveI := hcycl, obtain ⟨k₁, hk₁⟩ := nat.exists_eq_succ_of_ne_zero (one_le_iff_ne_zero.1 hk), rw [hζ.pow_sub_one_norm_two hirr], rw [hk₁, pow_succ, pow_mul, neg_eq_neg_one_mul, mul_pow, neg_one_sq, one_mul, ← pow_mul, ← pow_succ] }, { exact hζ.pow_sub_one_norm_prime_pow_ne_two hirr hs htwo } end end is_primitive_root namespace is_cyclotomic_extension open is_primitive_root variables {K} (L) [field K] [field L] [algebra K L] /-- If `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), the norm of `zeta n K L` is `1` if `n` is odd. -/ lemma norm_zeta_eq_one [is_cyclotomic_extension {n} K L] (hn : n ≠ 2) (hirr : irreducible (cyclotomic n K)) : norm K (zeta n K L) = 1 := (zeta_spec n K L).norm_eq_one hn hirr /-- If `is_prime_pow (n : ℕ)`, `n ≠ 2` and `irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `zeta n K L - 1` is `(n : ℕ).min_fac`. -/ lemma is_prime_pow_norm_zeta_sub_one (hn : is_prime_pow (n : ℕ)) [is_cyclotomic_extension {n} K L] (hirr : irreducible (cyclotomic (n : ℕ) K)) (h : n ≠ 2) : norm K (zeta n K L - 1) = (n : ℕ).min_fac := (zeta_spec n K L).sub_one_norm_is_prime_pow hn hirr h /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `(zeta (p ^ (k + 1)) K L) ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `p ^ (k - s + 1) ≠ 2`. -/ lemma prime_ne_two_pow_norm_zeta_pow_sub_one {k : ℕ} [hpri : fact (p : ℕ).prime] [is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) {s : ℕ} (hs : s ≤ k) (htwo : p ^ (k - s + 1) ≠ 2) : norm K ((zeta (p ^ (k + 1)) K L) ^ ((p : ℕ) ^ s) - 1) = p ^ ((p : ℕ) ^ s) := (zeta_spec _ K L).pow_sub_one_norm_prime_pow_ne_two hirr hs htwo /-- If `irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `zeta (p ^ (k + 1)) K L - 1` is `p`. -/ lemma prime_ne_two_pow_norm_zeta_sub_one {k : ℕ} [hpri : fact (p : ℕ).prime] [is_cyclotomic_extension {p ^ (k + 1)} K L] (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (h : p ≠ 2) : norm K (zeta (p ^ (k + 1)) K L - 1) = p := (zeta_spec _ K L).sub_one_norm_prime_ne_two hirr h /-- If `irreducible (cyclotomic p K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `zeta p K L - 1` is `p`. -/ lemma prime_ne_two_norm_zeta_sub_one [hpri : fact (p : ℕ).prime] [hcyc : is_cyclotomic_extension {p} K L] (hirr : irreducible (cyclotomic p K)) (h : p ≠ 2) : norm K (zeta p K L - 1) = p := (zeta_spec _ K L).sub_one_norm_prime hirr h /-- If `irreducible (cyclotomic (2 ^ k) K)` (in particular for `K = ℚ`) and `k` is at least `2`, then the norm of `zeta (2 ^ k) K L - 1` is `2`. -/ lemma two_pow_norm_zeta_sub_one {k : ℕ} (hk : 2 ≤ k) [is_cyclotomic_extension {2 ^ k} K L] (hirr : irreducible (cyclotomic (2 ^ k) K)) : norm K (zeta (2 ^ k) K L - 1) = 2 := sub_one_norm_two (zeta_spec (2 ^ k) K L) hk hirr end is_cyclotomic_extension end norm
14ff608ea29941b52a0ce84c8fd2246047d1654c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/sheaves/sheafify_auto.lean
c8accd222308a9f902d6e281f7bfb922326d9262
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,476
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.sheaves.local_predicate import Mathlib.topology.sheaves.stalks import Mathlib.PostPort universes v namespace Mathlib /-! # Sheafification of `Type` valued presheaves We construct the sheafification of a `Type` valued presheaf, as the subsheaf of dependent functions into the stalks consisting of functions which are locally germs. We show that the stalks of the sheafification are isomorphic to the original stalks, via `stalk_to_fiber` which evaluates a germ of a dependent function at a point. We construct a morphism `to_sheafify` from a presheaf to (the underlying presheaf of) its sheafification, given by sending a section to its collection of germs. ## Future work Show that the map induced on stalks by `to_sheafify` is the inverse of `stalk_to_fiber`. Show sheafification is a functor from presheaves to sheaves, and that it is the left adjoint of the forgetful functor, following https://stacks.math.columbia.edu/tag/007X. -/ namespace Top.presheaf namespace sheafify /-- The prelocal predicate on functions into the stalks, asserting that the function is equal to a germ. -/ def is_germ {X : Top} (F : presheaf (Type v) X) : prelocal_predicate fun (x : ↥X) => stalk F x := prelocal_predicate.mk (fun (U : topological_space.opens ↥X) (f : (x : ↥U) → stalk F ↑x) => ∃ (g : category_theory.functor.obj F (opposite.op U)), ∀ (x : ↥U), f x = germ F x g) sorry /-- The local predicate on functions into the stalks, asserting that the function is locally equal to a germ. -/ def is_locally_germ {X : Top} (F : presheaf (Type v) X) : local_predicate fun (x : ↥X) => stalk F x := prelocal_predicate.sheafify (is_germ F) end sheafify /-- The sheafification of a `Type` valued presheaf, defined as the functions into the stalks which are locally equal to germs. -/ def sheafify {X : Top} (F : presheaf (Type v) X) : sheaf (Type v) X := subsheaf_to_Types sorry /-- The morphism from a presheaf to its sheafification, sending each section to its germs. (This forms the unit of the adjunction.) -/ def to_sheafify {X : Top} (F : presheaf (Type v) X) : F ⟶ sheaf.presheaf (sheafify F) := category_theory.nat_trans.mk fun (U : topological_space.opens ↥Xᵒᵖ) (f : category_theory.functor.obj F U) => { val := fun (x : ↥(opposite.unop U)) => germ F x f, property := sorry } /-- The natural morphism from the stalk of the sheafification to the original stalk. In `sheafify_stalk_iso` we show this is an isomorphism. -/ def stalk_to_fiber {X : Top} (F : presheaf (Type v) X) (x : ↥X) : stalk (sheaf.presheaf (sheafify F)) x ⟶ stalk F x := stalk_to_fiber (sheafify.is_locally_germ F) x theorem stalk_to_fiber_surjective {X : Top} (F : presheaf (Type v) X) (x : ↥X) : function.surjective (stalk_to_fiber F x) := sorry theorem stalk_to_fiber_injective {X : Top} (F : presheaf (Type v) X) (x : ↥X) : function.injective (stalk_to_fiber F x) := sorry /-- The isomorphism betweeen a stalk of the sheafification and the original stalk. -/ def sheafify_stalk_iso {X : Top} (F : presheaf (Type v) X) (x : ↥X) : stalk (sheaf.presheaf (sheafify F)) x ≅ stalk F x := equiv.to_iso (equiv.of_bijective (stalk_to_fiber F x) sorry) end Mathlib
089a51002ebab2fa192eff5f02b5423e8c403d68
f57749ca63d6416f807b770f67559503fdb21001
/hott/init/equiv.hlean
9a9a39f9c1855dd3d89e94be9fa35a655e3a9cb3
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,251
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Jakob von Raumer Ported from Coq HoTT -/ prelude import .path .function open eq function lift /- Equivalences -/ -- This is our definition of equivalence. In the HoTT-book it's called -- ihae (half-adjoint equivalence). structure is_equiv [class] {A B : Type} (f : A → B) := mk' :: (inv : B → A) (right_inv : Πb, f (inv b) = b) (left_inv : Πa, inv (f a) = a) (adj : Πx, right_inv (f x) = ap f (left_inv x)) attribute is_equiv.inv [quasireducible] -- A more bundled version of equivalence structure equiv (A B : Type) := (to_fun : A → B) (to_is_equiv : is_equiv to_fun) namespace is_equiv /- Some instances and closure properties of equivalences -/ postfix `⁻¹` := inv /- a second notation for the inverse, which is not overloaded -/ postfix [parsing-only] `⁻¹ᶠ`:std.prec.max_plus := inv section variables {A B C : Type} (f : A → B) (g : B → C) {f' : A → B} -- The variant of mk' where f is explicit. protected abbreviation mk [constructor] := @is_equiv.mk' A B f -- The identity function is an equivalence. definition is_equiv_id (A : Type) : (is_equiv (id : A → A)) := is_equiv.mk id id (λa, idp) (λa, idp) (λa, idp) -- The composition of two equivalences is, again, an equivalence. definition is_equiv_compose [Hf : is_equiv f] [Hg : is_equiv g] : is_equiv (g ∘ f) := is_equiv.mk (g ∘ f) (f⁻¹ ∘ g⁻¹) (λc, ap g (right_inv f (g⁻¹ c)) ⬝ right_inv g c) (λa, ap (inv f) (left_inv g (f a)) ⬝ left_inv f a) (λa, (whisker_left _ (adj g (f a))) ⬝ (ap_con g _ _)⁻¹ ⬝ ap02 g ((ap_con_eq_con (right_inv f) (left_inv g (f a)))⁻¹ ⬝ (ap_compose f (inv f) _ ◾ adj f a) ⬝ (ap_con f _ _)⁻¹ ) ⬝ (ap_compose g f _)⁻¹ ) -- Any function equal to an equivalence is an equivlance as well. variable {f} definition is_equiv_eq_closed [Hf : is_equiv f] (Heq : f = f') : is_equiv f' := eq.rec_on Heq Hf end section parameters {A B : Type} (f : A → B) (g : B → A) (ret : Πb, f (g b) = b) (sec : Πa, g (f a) = a) private definition adjointify_left_inv' (a : A) : g (f a) = a := ap g (ap f (inverse (sec a))) ⬝ ap g (ret (f a)) ⬝ sec a private definition adjointify_adj' (a : A) : ret (f a) = ap f (adjointify_left_inv' a) := let fgretrfa := ap f (ap g (ret (f a))) in let fgfinvsect := ap f (ap g (ap f (sec a)⁻¹)) in let fgfa := f (g (f a)) in let retrfa := ret (f a) in have eq1 : ap f (sec a) = _, from calc ap f (sec a) = idp ⬝ ap f (sec a) : by rewrite idp_con ... = (ret (f a) ⬝ (ret (f a))⁻¹) ⬝ ap f (sec a) : by rewrite con.right_inv ... = ((ret fgfa)⁻¹ ⬝ ap (f ∘ g) (ret (f a))) ⬝ ap f (sec a) : by rewrite con_ap_eq_con ... = ((ret fgfa)⁻¹ ⬝ fgretrfa) ⬝ ap f (sec a) : by rewrite ap_compose ... = (ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a)) : by rewrite con.assoc, have eq2 : ap f (sec a) ⬝ idp = (ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a)), from !con_idp ⬝ eq1, have eq3 : idp = _, from calc idp = (ap f (sec a))⁻¹ ⬝ ((ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a))) : eq_inv_con_of_con_eq eq2 ... = ((ap f (sec a))⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ (fgretrfa ⬝ ap f (sec a)) : by rewrite con.assoc' ... = (ap f (sec a)⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ (fgretrfa ⬝ ap f (sec a)) : by rewrite ap_inv ... = ((ap f (sec a)⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ fgretrfa) ⬝ ap f (sec a) : by rewrite con.assoc' ... = ((retrfa⁻¹ ⬝ ap (f ∘ g) (ap f (sec a)⁻¹)) ⬝ fgretrfa) ⬝ ap f (sec a) : by rewrite con_ap_eq_con ... = ((retrfa⁻¹ ⬝ fgfinvsect) ⬝ fgretrfa) ⬝ ap f (sec a) : by rewrite ap_compose ... = (retrfa⁻¹ ⬝ (fgfinvsect ⬝ fgretrfa)) ⬝ ap f (sec a) : by rewrite con.assoc' ... = retrfa⁻¹ ⬝ ap f (ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ ap f (sec a) : by rewrite ap_con ... = retrfa⁻¹ ⬝ (ap f (ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ ap f (sec a)) : by rewrite con.assoc' ... = retrfa⁻¹ ⬝ ap f ((ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ sec a) : by rewrite -ap_con, have eq4 : ret (f a) = ap f ((ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ sec a), from eq_of_idp_eq_inv_con eq3, eq4 definition adjointify [constructor] : is_equiv f := is_equiv.mk f g ret adjointify_left_inv' adjointify_adj' end -- Any function pointwise equal to an equivalence is an equivalence as well. definition homotopy_closed [constructor] {A B : Type} {f f' : A → B} [Hf : is_equiv f] (Hty : f ~ f') : is_equiv f' := adjointify f' (inv f) (λ b, (Hty (inv f b))⁻¹ ⬝ right_inv f b) (λ a, (ap (inv f) (Hty a))⁻¹ ⬝ left_inv f a) definition inv_homotopy_closed [constructor] {A B : Type} {f : A → B} {f' : B → A} [Hf : is_equiv f] (Hty : f⁻¹ ~ f') : is_equiv f := adjointify f f' (λ b, ap f !Hty⁻¹ ⬝ right_inv f b) (λ a, !Hty⁻¹ ⬝ left_inv f a) definition is_equiv_up [instance] (A : Type) : is_equiv (up : A → lift A) := adjointify up down (λa, by induction a;reflexivity) (λa, idp) section variables {A B C : Type} (f : A → B) {f' : A → B} [Hf : is_equiv f] (g : B → C) include Hf --The inverse of an equivalence is, again, an equivalence. definition is_equiv_inv [instance] : is_equiv f⁻¹ := adjointify f⁻¹ f (left_inv f) (right_inv f) definition cancel_right (g : B → C) [Hgf : is_equiv (g ∘ f)] : (is_equiv g) := have Hfinv [visible] : is_equiv f⁻¹, from is_equiv_inv f, @homotopy_closed _ _ _ _ (is_equiv_compose f⁻¹ (g ∘ f)) (λb, ap g (@right_inv _ _ f _ b)) definition cancel_left (g : C → A) [Hgf : is_equiv (f ∘ g)] : (is_equiv g) := have Hfinv [visible] : is_equiv f⁻¹, from is_equiv_inv f, @homotopy_closed _ _ _ _ (is_equiv_compose (f ∘ g) f⁻¹) (λa, left_inv f (g a)) definition eq_of_fn_eq_fn' {x y : A} (q : f x = f y) : x = y := (left_inv f x)⁻¹ ⬝ ap f⁻¹ q ⬝ left_inv f y definition is_equiv_ap [instance] (x y : A) : is_equiv (ap f : x = y → f x = f y) := adjointify (ap f) (eq_of_fn_eq_fn' f) (λq, !ap_con ⬝ whisker_right !ap_con _ ⬝ ((!ap_inv ⬝ inverse2 (adj f _)⁻¹) ◾ (inverse (ap_compose f f⁻¹ _)) ◾ (adj f _)⁻¹) ⬝ con_ap_con_eq_con_con (right_inv f) _ _ ⬝ whisker_right !con.left_inv _ ⬝ !idp_con) (λp, whisker_right (whisker_left _ (ap_compose f⁻¹ f _)⁻¹) _ ⬝ con_ap_con_eq_con_con (left_inv f) _ _ ⬝ whisker_right !con.left_inv _ ⬝ !idp_con) -- The function equiv_rect says that given an equivalence f : A → B, -- and a hypothesis from B, one may always assume that the hypothesis -- is in the image of e. -- In fibrational terms, if we have a fibration over B which has a section -- once pulled back along an equivalence f : A → B, then it has a section -- over all of B. definition equiv_rect (P : B → Type) : (Πx, P (f x)) → (Πy, P y) := (λg y, eq.transport _ (right_inv f y) (g (f⁻¹ y))) definition equiv_rect_comp (P : B → Type) (df : Π (x : A), P (f x)) (x : A) : equiv_rect f P df (f x) = df x := calc equiv_rect f P df (f x) = right_inv f (f x) ▸ df (f⁻¹ (f x)) : by esimp ... = ap f (left_inv f x) ▸ df (f⁻¹ (f x)) : by rewrite -adj ... = left_inv f x ▸ df (f⁻¹ (f x)) : by rewrite -transport_compose ... = df x : by rewrite (apd df (left_inv f x)) end section variables {A B : Type} {f : A → B} [Hf : is_equiv f] {a : A} {b : B} include Hf --Rewrite rules definition eq_of_eq_inv (p : a = f⁻¹ b) : f a = b := ap f p ⬝ right_inv f b definition eq_of_inv_eq (p : f⁻¹ b = a) : b = f a := (eq_of_eq_inv p⁻¹)⁻¹ definition inv_eq_of_eq (p : b = f a) : f⁻¹ b = a := ap f⁻¹ p ⬝ left_inv f a definition eq_inv_of_eq (p : f a = b) : a = f⁻¹ b := (inv_eq_of_eq p⁻¹)⁻¹ end --Transporting is an equivalence definition is_equiv_tr [instance] {A : Type} (P : A → Type) {x y : A} (p : x = y) : (is_equiv (transport P p)) := is_equiv.mk _ (transport P p⁻¹) (tr_inv_tr p) (inv_tr_tr p) (tr_inv_tr_lemma p) end is_equiv open is_equiv namespace eq definition tr_inv_fn {A : Type} {B : A → Type} {a a' : A} (p : a = a') : transport B p⁻¹ = (transport B p)⁻¹ := idp definition tr_inv {A : Type} {B : A → Type} {a a' : A} (p : a = a') (b : B a') : p⁻¹ ▸ b = (transport B p)⁻¹ b := idp definition cast_inv_fn {A B : Type} (p : A = B) : cast p⁻¹ = (cast p)⁻¹ := idp definition cast_inv {A B : Type} (p : A = B) (b : B) : cast p⁻¹ b = (cast p)⁻¹ b := idp end eq namespace equiv namespace ops attribute to_fun [coercion] end ops open equiv.ops attribute to_is_equiv [instance] infix `≃`:25 := equiv variables {A B C : Type} protected definition MK [reducible] [constructor] (f : A → B) (g : B → A) (right_inv : Πb, f (g b) = b) (left_inv : Πa, g (f a) = a) : A ≃ B := equiv.mk f (adjointify f g right_inv left_inv) definition to_inv [reducible] [unfold 3] (f : A ≃ B) : B → A := f⁻¹ definition to_right_inv [reducible] [unfold 3] (f : A ≃ B) (b : B) : f (f⁻¹ b) = b := right_inv f b definition to_left_inv [reducible] [unfold 3] (f : A ≃ B) (a : A) : f⁻¹ (f a) = a := left_inv f a protected definition refl [refl] [constructor] : A ≃ A := equiv.mk id !is_equiv_id protected definition symm [symm] [unfold 3] (f : A ≃ B) : B ≃ A := equiv.mk f⁻¹ !is_equiv_inv protected definition trans [trans] (f : A ≃ B) (g: B ≃ C) : A ≃ C := equiv.mk (g ∘ f) !is_equiv_compose infixl `⬝e`:75 := equiv.trans postfix [parsing-only] `⁻¹ᵉ`:(max + 1) := equiv.symm -- notation for inverse which is not overloaded abbreviation erfl [constructor] := @equiv.refl definition equiv_change_fun [constructor] (f : A ≃ B) {f' : A → B} (Heq : f ~ f') : A ≃ B := equiv.mk f' (is_equiv.homotopy_closed Heq) definition equiv_change_inv [constructor] (f : A ≃ B) {f' : B → A} (Heq : f⁻¹ ~ f') : A ≃ B := equiv.mk f (inv_homotopy_closed Heq) --rename: eq_equiv_fn_eq_of_is_equiv definition eq_equiv_fn_eq [constructor] (f : A → B) [H : is_equiv f] (a b : A) : (a = b) ≃ (f a = f b) := equiv.mk (ap f) !is_equiv_ap --rename: eq_equiv_fn_eq definition eq_equiv_fn_eq_of_equiv [constructor] (f : A ≃ B) (a b : A) : (a = b) ≃ (f a = f b) := equiv.mk (ap f) !is_equiv_ap definition equiv_ap (P : A → Type) {a b : A} (p : a = b) : (P a) ≃ (P b) := equiv.mk (transport P p) !is_equiv_tr definition eq_of_fn_eq_fn (f : A ≃ B) {x y : A} (q : f x = f y) : x = y := (left_inv f x)⁻¹ ⬝ ap f⁻¹ q ⬝ left_inv f y definition eq_of_fn_eq_fn_inv (f : A ≃ B) {x y : B} (q : f⁻¹ x = f⁻¹ y) : x = y := (right_inv f x)⁻¹ ⬝ ap f q ⬝ right_inv f y --we need this theorem for the funext_of_ua proof theorem inv_eq {A B : Type} (eqf eqg : A ≃ B) (p : eqf = eqg) : (to_fun eqf)⁻¹ = (to_fun eqg)⁻¹ := eq.rec_on p idp definition equiv_of_equiv_of_eq [trans] {A B C : Type} (p : A = B) (q : B ≃ C) : A ≃ C := p⁻¹ ▸ q definition equiv_of_eq_of_equiv [trans] {A B C : Type} (p : A ≃ B) (q : B = C) : A ≃ C := q ▸ p definition equiv_lift (A : Type) : A ≃ lift A := equiv.mk up _ namespace ops postfix `⁻¹` := equiv.symm -- overloaded notation for inverse end ops end equiv export [unfold-hints] equiv [unfold-hints] is_equiv
be2e1e986e771510c398b0a6325a321e94212484
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Compiler/CSimpAttr.lean
93742e1772832368de0ed01b91647c4d512d19b7
[ "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
1,936
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ScopedEnvExtension import Lean.Util.Recognizers import Lean.Util.ReplaceExpr namespace Lean.Compiler namespace CSimp structure Entry where fromDeclName : Name toDeclName : Name deriving Inhabited abbrev State := SMap Name Name builtin_initialize ext : SimpleScopedEnvExtension Entry State ← registerSimpleScopedEnvExtension { name := `csimp initial := {} addEntry := fun s { fromDeclName, toDeclName } => s.insert fromDeclName toDeclName finalizeImport := fun s => s.switch } private def isConstantReplacement? (declName : Name) : CoreM (Option Entry) := do let info ← getConstInfo declName match info.type.eq? with | some (_, Expr.const fromDeclName us .., Expr.const toDeclName vs ..) => if us == vs then return some { fromDeclName, toDeclName } else return none | _ => return none def add (declName : Name) (kind : AttributeKind) : CoreM Unit := do if let some entry ← isConstantReplacement? declName then ext.add entry kind else throwError "invalid 'csimp' theorem, only constant replacement theorems (e.g., `@f = @g`) are currently supported." builtin_initialize registerBuiltinAttribute { name := `csimp descr := "simplification theorem for the compiler" add := fun declName stx attrKind => do Attribute.Builtin.ensureNoArgs stx discard <| add declName attrKind } @[export lean_csimp_replace_constants] def replaceConstants (env : Environment) (e : Expr) : Expr := let map := ext.getState env e.replace fun e => if e.isConst then match map.find? e.constName! with | some declNameNew => some (mkConst declNameNew e.constLevels!) | none => none else none end CSimp end Lean.Compiler
72fa20ec9762ee8e419a646681ebbb276f6be6df
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/implicit.lean
c4e55b07a5379fd182e21201867453db5558c787
[]
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
29,222
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.inverse import Mathlib.analysis.normed_space.complemented import Mathlib.PostPort universes u_1 u_2 u_3 u_4 l namespace Mathlib /-! # Implicit function theorem We prove three versions of the implicit function theorem. First we define a structure `implicit_function_data` that holds arguments for the most general version of the implicit function theorem, see `implicit_function_data.implicit_function` and `implicit_function_data.to_implicit_function`. This version allows a user to choose a specific implicit function but provides only a little convenience over the inverse function theorem. Then we define `implicit_function_of_complemented`: implicit function defined by `f (g z y) = z`, where `f : E → F` is a function strictly differentiable at `a` such that its derivative `f'` is surjective and has a `complemented` kernel. Finally, if the codomain of `f` is a finite dimensional space, then we can automatically prove that the kernel of `f'` is complemented, hence the only assumptions are `has_strict_fderiv_at` and `f'.range = ⊤`. This version is named `implicit_function`. ## TODO * Add a version for a function `f : E × F → G` such that $$\frac{\partial f}{\partial y}$$ is invertible. * Add a version for `f : 𝕜 × 𝕜 → 𝕜` proving `has_strict_deriv_at` and `deriv φ = ...`. * Prove that in a real vector space the implicit function has the same smoothness as the original one. * If the original function is differentiable in a neighborhood, then the implicit function is differentiable in a neighborhood as well. Current setup only proves differentiability at one point for the implicit function constructed in this file (as opposed to an unspecified implicit function). One of the ways to overcome this difficulty is to use uniqueness of the implicit function in the general version of the theorem. Another way is to prove that *any* implicit function satisfying some predicate is strictly differentiable. ## Tags implicit function, inverse function -/ /-! ### General version Consider two functions `f : E → F` and `g : E → G` and a point `a` such that * both functions are strictly differentiable at `a`; * the derivatives are surjective; * the kernels of the derivatives are complementary subspaces of `E`. Note that the map `x ↦ (f x, g x)` has a bijective derivative, hence it is a local homeomorphism between `E` and `F × G`. We use this fact to define a function `φ : F → G → E` (see `implicit_function_data.implicit_function`) such that for `(y, z)` close enough to `(f a, g a)` we have `f (φ y z) = y` and `g (φ y z) = z`. We also prove a formula for $$\frac{\partial\varphi}{\partial z}.$$ Though this statement is almost symmetric with respect to `F`, `G`, we interpret it in the following way. Consider a family of surfaces `{x | f x = y}`, `y ∈ 𝓝 (f a)`. Each of these surfaces is parametrized by `φ y`. There are many ways to choose a (differentiable) function `φ` such that `f (φ y z) = y` but the extra condition `g (φ y z) = z` allows a user to select one of these functions. If we imagine that the level surfaces `f = const` form a local horizontal foliation, then the choice of `g` fixes a transverse foliation `g = const`, and `φ` is the inverse function of the projection of `{x | f x = y}` along this transverse foliation. This version of the theorem is used to prove the other versions and can be used if a user needs to have a complete control over the choice of the implicit function. -/ /-- Data for the general version of the implicit function theorem. It holds two functions `f : E → F` and `g : E → G` (named `left_fun` and `right_fun`) and a point `a` (named `pt`) such that * both functions are strictly differentiable at `a`; * the derivatives are surjective; * the kernels of the derivatives are complementary subspaces of `E`. -/ structure implicit_function_data (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] [complete_space E] (F : Type u_3) [normed_group F] [normed_space 𝕜 F] [complete_space F] (G : Type u_4) [normed_group G] [normed_space 𝕜 G] [complete_space G] where left_fun : E → F left_deriv : continuous_linear_map 𝕜 E F right_fun : E → G right_deriv : continuous_linear_map 𝕜 E G pt : E left_has_deriv : has_strict_fderiv_at left_fun left_deriv pt right_has_deriv : has_strict_fderiv_at right_fun right_deriv pt left_range : continuous_linear_map.range left_deriv = ⊤ right_range : continuous_linear_map.range right_deriv = ⊤ is_compl_ker : is_compl (continuous_linear_map.ker left_deriv) (continuous_linear_map.ker right_deriv) namespace implicit_function_data /-- The function given by `x ↦ (left_fun x, right_fun x)`. -/ def prod_fun {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) (x : E) : F × G := (left_fun φ x, right_fun φ x) @[simp] theorem prod_fun_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) (x : E) : prod_fun φ x = (left_fun φ x, right_fun φ x) := rfl protected theorem has_strict_fderiv_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : has_strict_fderiv_at (prod_fun φ) (↑(continuous_linear_map.equiv_prod_of_surjective_of_is_compl (left_deriv φ) (right_deriv φ) (left_range φ) (right_range φ) (is_compl_ker φ))) (pt φ) := has_strict_fderiv_at.prod (left_has_deriv φ) (right_has_deriv φ) /-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are complementary subspaces of `E`, then `x ↦ (f x, g x)` defines a local homeomorphism between `E` and `F × G`. In particular, `{x | f x = f a}` is locally homeomorphic to `G`. -/ def to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : local_homeomorph E (F × G) := has_strict_fderiv_at.to_local_homeomorph (prod_fun φ) (implicit_function_data.has_strict_fderiv_at φ) /-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are complementary subspaces of `E`, then `implicit_function_of_is_compl_ker` is the unique (germ of a) map `φ : F → G → E` such that `f (φ y z) = y` and `g (φ y z) = z`. -/ def implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : F → G → E := function.curry ⇑(local_homeomorph.symm (to_local_homeomorph φ)) @[simp] theorem to_local_homeomorph_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : ⇑(to_local_homeomorph φ) = prod_fun φ := rfl theorem to_local_homeomorph_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) (x : E) : coe_fn (to_local_homeomorph φ) x = (left_fun φ x, right_fun φ x) := rfl theorem pt_mem_to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : pt φ ∈ local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph φ)) := has_strict_fderiv_at.mem_to_local_homeomorph_source (implicit_function_data.has_strict_fderiv_at φ) theorem map_pt_mem_to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : (left_fun φ (pt φ), right_fun φ (pt φ)) ∈ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph φ)) := local_homeomorph.map_source (to_local_homeomorph φ) (pt_mem_to_local_homeomorph_source φ) theorem prod_map_implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : filter.eventually (fun (p : F × G) => prod_fun φ (implicit_function φ (prod.fst p) (prod.snd p)) = p) (nhds (prod_fun φ (pt φ))) := sorry theorem left_map_implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : filter.eventually (fun (p : F × G) => left_fun φ (implicit_function φ (prod.fst p) (prod.snd p)) = prod.fst p) (nhds (prod_fun φ (pt φ))) := filter.eventually.mono (prod_map_implicit_function φ) fun (z : F × G) => congr_arg prod.fst theorem right_map_implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : filter.eventually (fun (p : F × G) => right_fun φ (implicit_function φ (prod.fst p) (prod.snd p)) = prod.snd p) (nhds (prod_fun φ (pt φ))) := filter.eventually.mono (prod_map_implicit_function φ) fun (z : F × G) => congr_arg prod.snd theorem implicit_function_apply_image {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) : filter.eventually (fun (x : E) => implicit_function φ (left_fun φ x) (right_fun φ x) = x) (nhds (pt φ)) := has_strict_fderiv_at.eventually_left_inverse (implicit_function_data.has_strict_fderiv_at φ) theorem implicit_function_has_strict_fderiv_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] [complete_space G] (φ : implicit_function_data 𝕜 E F G) (g'inv : continuous_linear_map 𝕜 G E) (hg'inv : continuous_linear_map.comp (right_deriv φ) g'inv = continuous_linear_map.id 𝕜 G) (hg'invf : continuous_linear_map.comp (left_deriv φ) g'inv = 0) : has_strict_fderiv_at (implicit_function φ (left_fun φ (pt φ))) g'inv (right_fun φ (pt φ)) := sorry end implicit_function_data namespace has_strict_fderiv_at /-! ### Case of a complemented kernel In this section we prove the following version of the implicit function theorem. Consider a map `f : E → F` and a point `a : E` such that `f` is strictly differentiable at `a`, its derivative `f'` is surjective and the kernel of `f'` is a complemented subspace of `E` (i.e., it has a closed complementary subspace). Then there exists a function `φ : F → ker f' → E` such that for `(y, z)` close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the embedding `ker f' → E`. Note that a map with these properties is not unique. E.g., different choices of a subspace complementary to `ker f'` lead to different maps `φ`. -/ /-- Data used to apply the generic implicit function theorem to the case of a strictly differentiable map such that its derivative is surjective and has a complemented kernel. -/ @[simp] def implicit_function_data_of_complemented {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : implicit_function_data 𝕜 E F ↥(continuous_linear_map.ker f') := implicit_function_data.mk f f' (fun (x : E) => coe_fn (classical.some hker) (x - a)) (classical.some hker) a hf sorry hf' sorry sorry /-- A local homeomorphism between `E` and `F × f'.ker` sending level surfaces of `f` to vertical subspaces. -/ def implicit_to_local_homeomorph_of_complemented {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : local_homeomorph E (F × ↥(continuous_linear_map.ker f')) := implicit_function_data.to_local_homeomorph (implicit_function_data_of_complemented f f' hf hf' hker) /-- Implicit function `g` defined by `f (g z y) = z`. -/ def implicit_function_of_complemented {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : F → ↥(continuous_linear_map.ker f') → E := implicit_function_data.implicit_function (implicit_function_data_of_complemented f f' hf hf' hker) @[simp] theorem implicit_to_local_homeomorph_of_complemented_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) (x : E) : prod.fst (coe_fn (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker) x) = f x := rfl theorem implicit_to_local_homeomorph_of_complemented_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) (y : E) : coe_fn (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker) y = (f y, coe_fn (classical.some hker) (y - a)) := rfl @[simp] theorem implicit_to_local_homeomorph_of_complemented_apply_ker {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) (y : ↥(continuous_linear_map.ker f')) : coe_fn (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker) (↑y + a) = (f (↑y + a), y) := sorry @[simp] theorem implicit_to_local_homeomorph_of_complemented_self {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : coe_fn (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker) a = (f a, 0) := sorry theorem mem_implicit_to_local_homeomorph_of_complemented_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker)) := mem_to_local_homeomorph_source (implicit_function_data.has_strict_fderiv_at (implicit_function_data_of_complemented f f' hf hf' hker)) theorem mem_implicit_to_local_homeomorph_of_complemented_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : (f a, 0) ∈ local_equiv.target (local_homeomorph.to_local_equiv (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker)) := sorry /-- `implicit_function_of_complemented` sends `(z, y)` to a point in `f ⁻¹' z`. -/ theorem map_implicit_function_of_complemented_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : filter.eventually (fun (p : F × ↥(continuous_linear_map.ker f')) => f (implicit_function_of_complemented f f' hf hf' hker (prod.fst p) (prod.snd p)) = prod.fst p) (nhds (f a, 0)) := sorry /-- Any point in some neighborhood of `a` can be represented as `implicit_function` of some point. -/ theorem eq_implicit_function_of_complemented {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : filter.eventually (fun (x : E) => implicit_function_of_complemented f f' hf hf' hker (f x) (prod.snd (coe_fn (implicit_to_local_homeomorph_of_complemented f f' hf hf' hker) x)) = x) (nhds a) := implicit_function_data.implicit_function_apply_image (implicit_function_data_of_complemented f f' hf hf' hker) theorem to_implicit_function_of_complemented {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (hker : submodule.closed_complemented (continuous_linear_map.ker f')) : has_strict_fderiv_at (implicit_function_of_complemented f f' hf hf' hker (f a)) (continuous_linear_map.subtype_val (continuous_linear_map.ker f')) 0 := sorry /-! ### Finite dimensional case In this section we prove the following version of the implicit function theorem. Consider a map `f : E → F` from a Banach normed space to a finite dimensional space. Take a point `a : E` such that `f` is strictly differentiable at `a` and its derivative `f'` is surjective. Then there exists a function `φ : F → ker f' → E` such that for `(y, z)` close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the embedding `ker f' → E`. This version deduces that `ker f'` is a complemented subspace from the fact that `F` is a finite dimensional space, then applies the previous version. Note that a map with these properties is not unique. E.g., different choices of a subspace complementary to `ker f'` lead to different maps `φ`. -/ /-- Given a map `f : E → F` to a finite dimensional space with a surjective derivative `f'`, returns a local homeomorphism between `E` and `F × ker f'`. -/ def implicit_to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : local_homeomorph E (F × ↥(continuous_linear_map.ker f')) := implicit_to_local_homeomorph_of_complemented f f' hf hf' sorry /-- Implicit function `g` defined by `f (g z y) = z`. -/ def implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : F → ↥(continuous_linear_map.ker f') → E := function.curry ⇑(local_homeomorph.symm (implicit_to_local_homeomorph f f' hf hf')) @[simp] theorem implicit_to_local_homeomorph_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (x : E) : prod.fst (coe_fn (implicit_to_local_homeomorph f f' hf hf') x) = f x := rfl @[simp] theorem implicit_to_local_homeomorph_apply_ker {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) (y : ↥(continuous_linear_map.ker f')) : coe_fn (implicit_to_local_homeomorph f f' hf hf') (↑y + a) = (f (↑y + a), y) := implicit_to_local_homeomorph_of_complemented_apply_ker hf hf' (implicit_to_local_homeomorph._proof_1 f') y @[simp] theorem implicit_to_local_homeomorph_self {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : coe_fn (implicit_to_local_homeomorph f f' hf hf') a = (f a, 0) := implicit_to_local_homeomorph_of_complemented_self hf hf' (implicit_to_local_homeomorph._proof_1 f') theorem mem_implicit_to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (implicit_to_local_homeomorph f f' hf hf')) := mem_to_local_homeomorph_source (implicit_function_data.has_strict_fderiv_at (implicit_function_data_of_complemented f f' hf hf' (implicit_to_local_homeomorph._proof_1 f'))) theorem mem_implicit_to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : (f a, 0) ∈ local_equiv.target (local_homeomorph.to_local_equiv (implicit_to_local_homeomorph f f' hf hf')) := mem_implicit_to_local_homeomorph_of_complemented_target hf hf' (implicit_to_local_homeomorph._proof_1 f') /-- `implicit_function` sends `(z, y)` to a point in `f ⁻¹' z`. -/ theorem map_implicit_function_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : filter.eventually (fun (p : F × ↥(continuous_linear_map.ker f')) => f (implicit_function f f' hf hf' (prod.fst p) (prod.snd p)) = prod.fst p) (nhds (f a, 0)) := map_implicit_function_of_complemented_eq hf hf' (implicit_to_local_homeomorph._proof_1 f') /-- Any point in some neighborhood of `a` can be represented as `implicit_function` of some point. -/ theorem eq_implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : filter.eventually (fun (x : E) => implicit_function f f' hf hf' (f x) (prod.snd (coe_fn (implicit_to_local_homeomorph f f' hf hf') x)) = x) (nhds a) := eq_implicit_function_of_complemented hf hf' (implicit_to_local_homeomorph._proof_1 f') theorem to_implicit_function {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) (hf' : continuous_linear_map.range f' = ⊤) : has_strict_fderiv_at (implicit_function f f' hf hf' (f a)) (continuous_linear_map.subtype_val (continuous_linear_map.ker f')) 0 := to_implicit_function_of_complemented hf hf' (implicit_to_local_homeomorph._proof_1 f')
5454a3b49f4b45fc86add7d192a6045acb46cd0f
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/library/init/data/fin/ops.lean
e5541a6fa2a82697dda1488cbcb68a6cfbf10ad0
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,397
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat init.data.fin.basic namespace fin open nat variable {n : nat} def of_nat {n : nat} (a : nat) : fin (succ n) := ⟨a % succ n, nat.mod_lt _ (nat.zero_lt_succ _)⟩ private lemma mlt {n b : nat} : ∀ {a}, n > a → b % n < n | 0 h := nat.mod_lt _ h | (a+1) h := have n > 0, from lt.trans (nat.zero_lt_succ _) h, nat.mod_lt _ this protected def add : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨(a + b) % n, mlt h⟩ protected def mul : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨(a * b) % n, mlt h⟩ private lemma sublt {a b n : nat} (h : a < n) : a - b < n := lt_of_le_of_lt (nat.sub_le a b) h protected def sub : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨a - b, sublt h⟩ private lemma modlt {a b n : nat} (h₁ : a < n) (h₂ : b < n) : a % b < n := begin cases b with b, {simp [mod_zero], assumption}, {assert h : a % (succ b) < succ b, apply nat.mod_lt _ (nat.zero_lt_succ _), exact lt.trans h h₂} end protected def mod : fin n → fin n → fin n | ⟨a, h₁⟩ ⟨b, h₂⟩ := ⟨a % b, modlt h₁ h₂⟩ private lemma divlt {a b n : nat} (h : a < n) : a / b < n := lt_of_le_of_lt (nat.div_le_self a b) h protected def div : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨a / b, divlt h⟩ protected def lt : fin n → fin n → Prop | ⟨a, _⟩ ⟨b, _⟩ := a < b protected def le : fin n → fin n → Prop | ⟨a, _⟩ ⟨b, _⟩ := a ≤ b instance : has_zero (fin (succ n)) := ⟨of_nat 0⟩ instance : has_one (fin (succ n)) := ⟨of_nat 1⟩ instance : has_add (fin n) := ⟨fin.add⟩ instance : has_sub (fin n) := ⟨fin.sub⟩ instance : has_mul (fin n) := ⟨fin.mul⟩ instance : has_mod (fin n) := ⟨fin.mod⟩ instance : has_div (fin n) := ⟨fin.div⟩ instance : has_lt (fin n) := ⟨fin.lt⟩ instance : has_le (fin n) := ⟨fin.le⟩ instance decidable_lt : ∀ (a b : fin n), decidable (a < b) | ⟨a, _⟩ ⟨b, _⟩ := by apply nat.decidable_lt instance decidable_le : ∀ (a b : fin n), decidable (a ≤ b) | ⟨a, _⟩ ⟨b, _⟩ := by apply nat.decidable_le lemma add_def (a b : fin n) : (a + b)^.val = (a^.val + b^.val) % n := show (fin.add a b)^.val = (a^.val + b^.val) % n, from by cases a; cases b; simp [fin.add] lemma mul_def (a b : fin n) : (a * b)^.val = (a^.val * b^.val) % n := show (fin.mul a b)^.val = (a^.val * b^.val) % n, from by cases a; cases b; simp [fin.mul] lemma sub_def (a b : fin n) : (a - b)^.val = a^.val - b^.val := show (fin.sub a b)^.val = a^.val - b^.val, from by cases a; cases b; simp [fin.sub] lemma mod_def (a b : fin n) : (a % b)^.val = a^.val % b^.val := show (fin.mod a b)^.val = a^.val % b^.val, from by cases a; cases b; simp [fin.mod] lemma div_def (a b : fin n) : (a / b)^.val = a^.val / b^.val := show (fin.div a b)^.val = a^.val / b^.val, from by cases a; cases b; simp [fin.div] lemma lt_def (a b : fin n) : (a < b) = (a^.val < b^.val) := show (fin.lt a b) = (a^.val < b^.val), from by cases a; cases b; simp [fin.lt] lemma le_def (a b : fin n) : (a ≤ b) = (a^.val ≤ b^.val) := show (fin.le a b) = (a^.val ≤ b^.val), from by cases a; cases b; simp [fin.le] end fin
5206931e56bdb07ae60ead2339bbb53047c9858e
28be2ab6091504b6ba250b367205fb94d50ab284
/src/game/world9/level4.lean
6ba82ef3900be2efaab396e19a7440fb76355db3
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
1,744
lean
import game.world9.level3 -- hide namespace mynat -- hide /- # Advanced Multiplication World ## Level 4: `mul_left_cancel` This is the last of the bonus multiplication levels. `mul_left_cancel` will be useful in inequality world. It might be worth noting that `revert` is the opposite of `intro`. `revert b` can be a convenient tactic just before the beginning of an induction proof. -/ /- Theorem If $a \neq 0$, $b$ and $c$ are natural numbers such that $ ab = ac, $ then $b = c$. -/ theorem mul_left_cancel (a b c : mynat) (ha : a ≠ 0) : a * b = a * c → b = c := begin [nat_num_game] revert b, induction c with d hd, { intro b, rw mul_zero, intro h, cases (eq_zero_or_eq_zero_of_mul_eq_zero _ _ h) with h1 h2, exfalso, apply ha, assumption, assumption }, { intros b hb, cases b with c, { rw mul_zero at hb, exfalso, apply ha, symmetry at hb, cases (eq_zero_or_eq_zero_of_mul_eq_zero _ _ hb) with h h, exact h, exfalso, exact succ_ne_zero _ h, }, { have h : c = d, apply hd, rw mul_succ at hb, rw mul_succ at hb, apply add_right_cancel _ _ _ hb, rw h, refl, } } end end mynat -- hide /- You should now be ready for inequality world. -/ /- Tactic : revert ## Summary `revert x` is the opposite to `intro x`. ## Details If the tactic state looks like this ``` P Q : Prop, h : P ⊢ Q ``` then `revert h` will change it to ``` P Q : Prop ⊢ P → Q ``` `revert` also works with things like natural numbers: if the tactic state looks like this ``` m : mynat ⊢ m + 1 = succ m ``` then `revert m` will turn it into ``` ⊢ ∀ (m : mynat), m + 1 = mynat.succ m ``` -/
8fa41f9db087b606a7f2b64f4143caf9cc8d712a
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/ring_theory/polynomial/basic.lean
f4597e195921e208c69f7f1d2e569d59ba0b013d
[ "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
19,914
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Ring-theoretic supplement of data.polynomial. Main result: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. -/ import algebra.char_p import data.mv_polynomial import data.polynomial.ring_division import ring_theory.noetherian noncomputable theory local attribute [instance, priority 100] classical.prop_decidable universes u v w namespace polynomial instance {R : Type u} [semiring R] (p : ℕ) [h : char_p R p] : char_p (polynomial R) p := let ⟨h⟩ := h in ⟨λ n, by rw [← C.map_nat_cast, ← C_0, C_inj, h]⟩ variables (R : Type u) [comm_ring R] /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degree_lt (n : ℕ) : submodule R (polynomial R) := ⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : polynomial R} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl @[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) : degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, X^n) : finset (polynomial R)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← finsupp.sum_single p, finsupp.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [single_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, apply le_trans (degree_X_pow_le _) (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end theorem mem_degree_lt {n : ℕ} {f : polynomial R} : f ∈ degree_lt R n ↔ degree f < n := by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), finsupp.mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, lt_iff_not_ge', ne, not_imp_not], refl } @[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) : degree_lt R m ≤ degree_lt R n := λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H) theorem degree_lt_eq_span_X_pow {n : ℕ} : degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset (polynomial R)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_lt.1 hp, rw [← finsupp.sum_single p, finsupp.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk), rw [single_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_lt.2, exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk) end /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : polynomial R) : polynomial (ring.closure (↑p.frange : set R)) := ⟨p.support, λ i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem else ring.subset_closure $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ @[simp] theorem coeff_restriction {p : polynomial R} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := rfl @[simp] theorem coeff_restriction' {p : polynomial R} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := rfl @[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := rfl @[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := rfl @[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) ↔ monic p := ⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩ @[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := rfl @[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables {S : Type v} [ring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : polynomial R} : eval₂ f x p = eval₂ (f.comp (is_subring.subtype _)) x p.restriction := rfl section to_subring variables (p : polynomial R) (T : set R) [is_subring T] /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T. -/ def to_subring (hp : ↑p.frange ⊆ T) : polynomial T := ⟨p.support, λ i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem else hp $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ variables (hp : ↑p.frange ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := rfl @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := rfl @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := rfl @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := rfl @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := ⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩ omit hp @[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (set.empty_subset _) = 0 := rfl @[simp] theorem to_subring_one : to_subring (1 : polynomial R) T (set.subset.trans (finset.coe_subset.2 finsupp.frange_single) (finset.singleton_subset_set_iff.2 is_submonoid.one_mem)) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl @[simp] theorem map_to_subring : (p.to_subring T hp).map (is_subring.subtype T) = p := ext $ λ n, coeff_map _ _ end to_subring variables (T : set R) [is_subring T] /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefificents are in the ambient ring. -/ def of_subring (p : polynomial T) : polynomial R := ⟨p.support, subtype.val ∘ p.to_fun, λ n, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ h, congr_arg subtype.val h, λ h, subtype.eq h⟩)⟩ @[simp] theorem frange_of_subring {p : polynomial T} : ↑(p.of_subring T).frange ⊆ T := λ y H, let ⟨hy, x, hx⟩ := finsupp.mem_frange.1 H in hx ▸ (p.to_fun x).2 end polynomial variables {R : Type u} {σ : Type v} [comm_ring R] namespace ideal open polynomial /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) := { carrier := I.carrier, zero_mem' := I.zero_mem, add_mem' := λ _ _, I.add_mem, smul_mem' := λ c x H, by rw [← C_mul']; exact submodule.smul_mem _ _ H } variables {I : ideal (polynomial R)} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ leading_coeff p = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_refl _ }, { rw [leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [submodule.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, nat.add_sub_cancel' H], exact le_refl _ end /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end theorem is_fg_degree_le [is_noetherian_ring R] (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end ideal /-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/ protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] : is_noetherian_ring (polynomial R) := ⟨assume I : ideal (polynomial R), let L := I.leading_coeff in let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set (polynomial R)), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin change I ≤ ideal.span ↑s, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, nat.add_sub_cancel', hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ attribute [instance] polynomial.is_noetherian_ring namespace polynomial theorem exists_irreducible_of_degree_pos {R : Type u} [integral_domain R] [is_noetherian_ring R] {f : polynomial R} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f := is_noetherian_ring.exists_irreducible_factor (λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf) (λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _) theorem exists_irreducible_of_nat_degree_pos {R : Type u} [integral_domain R] [is_noetherian_ring R] {f : polynomial R} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf } theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [integral_domain R] [is_noetherian_ring R] {f : polynomial R} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf end polynomial namespace mv_polynomial lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin 0) R) := is_noetherian_ring_of_ring_equiv R ((mv_polynomial.pempty_ring_equiv R).symm.trans (mv_polynomial.ring_equiv_of_equiv _ fin_zero_equiv'.symm)) theorem is_noetherian_ring_fin [is_noetherian_ring R] : ∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R) | 0 := is_noetherian_ring_fin_0 | (n+1) := @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _ (mv_polynomial.fin_succ_equiv _ n).symm (@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin)) /-- The multivariate polynomial ring in finitely many variables over a noetherian ring is itself a noetherian ring. -/ instance is_noetherian_ring [fintype σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := trunc.induction_on (fintype.equiv_fin σ) $ λ e, @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (mv_polynomial.ring_equiv_of_equiv _ e.symm) is_noetherian_ring_fin lemma is_integral_domain_fin_zero (R : Type u) [comm_ring R] (hR : is_integral_domain R) : is_integral_domain (mv_polynomial (fin 0) R) := ring_equiv.is_integral_domain R hR ((ring_equiv_of_equiv R fin_zero_equiv').trans (mv_polynomial.pempty_ring_equiv R)) /-- Auxilliary lemma: Multivariate polynomials over an integral domain with variables indexed by `fin n` form an integral domain. This fact is proven inductively, and then used to prove the general case without any finiteness hypotheses. See `mv_polynomial.integral_domain` for the general case. -/ lemma is_integral_domain_fin (R : Type u) [comm_ring R] (hR : is_integral_domain R) : ∀ (n : ℕ), is_integral_domain (mv_polynomial (fin n) R) | 0 := is_integral_domain_fin_zero R hR | (n+1) := ring_equiv.is_integral_domain (polynomial (mv_polynomial (fin n) R)) (is_integral_domain_fin n).polynomial (mv_polynomial.fin_succ_equiv _ n) lemma is_integral_domain_fintype (R : Type u) (σ : Type v) [comm_ring R] [fintype σ] (hR : is_integral_domain R) : is_integral_domain (mv_polynomial σ R) := trunc.induction_on (fintype.equiv_fin σ) $ λ e, @ring_equiv.is_integral_domain _ (mv_polynomial (fin $ fintype.card σ) R) _ _ (mv_polynomial.is_integral_domain_fin _ hR _) (ring_equiv_of_equiv R e) /-- Auxilliary definition: Multivariate polynomials in finitely many variables over an integral domain form an integral domain. This fact is proven by transport of structure from the `mv_polynomial.integral_domain_fin`, and then used to prove the general case without finiteness hypotheses. See `mv_polynomial.integral_domain` for the general case. -/ def integral_domain_fintype (R : Type u) (σ : Type v) [integral_domain R] [fintype σ] : integral_domain (mv_polynomial σ R) := @is_integral_domain.to_integral_domain _ _ $ mv_polynomial.is_integral_domain_fintype R σ $ integral_domain.to_is_integral_domain R protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {R : Type u} [integral_domain R] {σ : Type v} (p q : mv_polynomial σ R) (h : p * q = 0) : p = 0 ∨ q = 0 := begin obtain ⟨s, p, rfl⟩ := exists_finset_rename p, obtain ⟨t, q, rfl⟩ := exists_finset_rename q, have : p.rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) * q.rename (subtype.map id (finset.subset_union_right s t)) = 0, { apply rename_injective _ subtype.val_injective, simpa using h }, letI := mv_polynomial.integral_domain_fintype R {x // x ∈ (s ∪ t)}, rw mul_eq_zero at this, cases this; [left, right], all_goals { simpa using congr_arg (rename subtype.val) this } end /-- The multivariate polynomial ring over an integral domain is an integral domain. -/ instance {R : Type u} {σ : Type v} [integral_domain R] : integral_domain (mv_polynomial σ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := mv_polynomial.eq_zero_or_eq_zero_of_mul_eq_zero, exists_pair_ne := ⟨0, 1, λ H, begin have : eval₂ id (λ s, (0:R)) (0 : mv_polynomial σ R) = eval₂ id (λ s, (0:R)) (1 : mv_polynomial σ R), { congr, exact H }, simpa, end⟩, .. (by apply_instance : comm_ring (mv_polynomial σ R)) } end mv_polynomial
98a5dd5057b4fe5194bbedd409568b4da91aca44
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/finset/pointwise.lean
03063a1ed2bc68e87df32de82e02a6e60c55d191
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
56,458
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Yaël Dillies -/ import data.finset.n_ary import data.finset.preimage import data.set.pointwise.finite import data.set.pointwise.smul import data.set.pointwise.list_of_fn /-! # Pointwise operations of finsets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines pointwise algebraic operations on finsets. ## Main declarations For finsets `s` and `t`: * `0` (`finset.has_zero`): The singleton `{0}`. * `1` (`finset.has_one`): The singleton `{1}`. * `-s` (`finset.has_neg`): Negation, finset of all `-x` where `x ∈ s`. * `s⁻¹` (`finset.has_inv`): Inversion, finset of all `x⁻¹` where `x ∈ s`. * `s + t` (`finset.has_add`): Addition, finset of all `x + y` where `x ∈ s` and `y ∈ t`. * `s * t` (`finset.has_mul`): Multiplication, finset of all `x * y` where `x ∈ s` and `y ∈ t`. * `s - t` (`finset.has_sub`): Subtraction, finset of all `x - y` where `x ∈ s` and `y ∈ t`. * `s / t` (`finset.has_div`): Division, finset of all `x / y` where `x ∈ s` and `y ∈ t`. * `s +ᵥ t` (`finset.has_vadd`): Scalar addition, finset of all `x +ᵥ y` where `x ∈ s` and `y ∈ t`. * `s • t` (`finset.has_smul`): Scalar multiplication, finset of all `x • y` where `x ∈ s` and `y ∈ t`. * `s -ᵥ t` (`finset.has_vsub`): Scalar subtraction, finset of all `x -ᵥ y` where `x ∈ s` and `y ∈ t`. * `a • s` (`finset.has_smul_finset`): Scaling, finset of all `a • x` where `x ∈ s`. * `a +ᵥ s` (`finset.has_vadd_finset`): Translation, finset of all `a +ᵥ x` where `x ∈ s`. For `α` a semigroup/monoid, `finset α` is a semigroup/monoid. As an unfortunate side effect, this means that `n • s`, where `n : ℕ`, is ambiguous between pointwise scaling and repeated pointwise addition; the former has `(2 : ℕ) • {1, 2} = {2, 4}`, while the latter has `(2 : ℕ) • {1, 2} = {2, 3, 4}`. See note [pointwise nat action]. ## Implementation notes We put all instances in the locale `pointwise`, so that these instances are not available by default. Note that we do not mark them as reducible (as argued by note [reducible non-instances]) since we expect the locale to be open whenever the instances are actually used (and making the instances reducible changes the behavior of `simp`. ## Tags finset multiplication, finset addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ open function mul_opposite open_locale big_operators pointwise variables {F α β γ : Type*} namespace finset /-! ### `0`/`1` as finsets -/ section has_one variables [has_one α] {s : finset α} {a : α} /-- The finset `1 : finset α` is defined as `{1}` in locale `pointwise`. -/ @[to_additive "The finset `0 : finset α` is defined as `{0}` in locale `pointwise`."] protected def has_one : has_one (finset α) := ⟨{1}⟩ localized "attribute [instance] finset.has_one finset.has_zero" in pointwise @[simp, to_additive] lemma mem_one : a ∈ (1 : finset α) ↔ a = 1 := mem_singleton @[simp, norm_cast, to_additive] lemma coe_one : ↑(1 : finset α) = (1 : set α) := coe_singleton 1 @[simp, to_additive] lemma one_subset : (1 : finset α) ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff @[to_additive] lemma singleton_one : ({1} : finset α) = 1 := rfl @[to_additive] lemma one_mem_one : (1 : α) ∈ (1 : finset α) := mem_singleton_self _ @[to_additive] lemma one_nonempty : (1 : finset α).nonempty := ⟨1, one_mem_one⟩ @[simp, to_additive] protected lemma map_one {f : α ↪ β} : map f 1 = {f 1} := map_singleton f 1 @[simp, to_additive] lemma image_one [decidable_eq β] {f : α → β} : image f 1 = {f 1} := image_singleton _ _ @[to_additive] lemma subset_one_iff_eq : s ⊆ 1 ↔ s = ∅ ∨ s = 1 := subset_singleton_iff @[to_additive] lemma nonempty.subset_one_iff (h : s.nonempty) : s ⊆ 1 ↔ s = 1 := h.subset_singleton_iff @[simp, to_additive] lemma card_one : (1 : finset α).card = 1 := card_singleton _ /-- The singleton operation as a `one_hom`. -/ @[to_additive "The singleton operation as a `zero_hom`."] def singleton_one_hom : one_hom α (finset α) := ⟨singleton, singleton_one⟩ @[simp, to_additive] lemma coe_singleton_one_hom : (singleton_one_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_one_hom_apply (a : α) : singleton_one_hom a = {a} := rfl /-- Lift a `one_hom` to `finset` via `image`. -/ @[to_additive "Lift a `zero_hom` to `finset` via `image`", simps] def image_one_hom [decidable_eq β] [has_one β] [one_hom_class F α β] (f : F) : one_hom (finset α) (finset β) := { to_fun := finset.image f, map_one' := by rw [image_one, map_one, singleton_one] } end has_one /-! ### Finset negation/inversion -/ section has_inv variables [decidable_eq α] [has_inv α] {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise inversion of finset `s⁻¹` is defined as `{x⁻¹ | x ∈ s}` in locale `pointwise`. -/ @[to_additive "The pointwise negation of finset `-s` is defined as `{-x | x ∈ s}` in locale `pointwise`."] protected def has_inv : has_inv (finset α) := ⟨image has_inv.inv⟩ localized "attribute [instance] finset.has_inv finset.has_neg" in pointwise @[to_additive] lemma inv_def : s⁻¹ = s.image (λ x, x⁻¹) := rfl @[to_additive] lemma image_inv : s.image (λ x, x⁻¹) = s⁻¹ := rfl @[to_additive] lemma mem_inv {x : α} : x ∈ s⁻¹ ↔ ∃ y ∈ s, y⁻¹ = x := mem_image @[to_additive] lemma inv_mem_inv (ha : a ∈ s) : a⁻¹ ∈ s⁻¹ := mem_image_of_mem _ ha @[to_additive] lemma card_inv_le : s⁻¹.card ≤ s.card := card_image_le @[simp, to_additive] lemma inv_empty : (∅ : finset α)⁻¹ = ∅ := image_empty _ @[simp, to_additive] lemma inv_nonempty_iff : s⁻¹.nonempty ↔ s.nonempty := nonempty.image_iff _ alias inv_nonempty_iff ↔ nonempty.of_inv nonempty.inv attribute [to_additive] nonempty.inv nonempty.of_inv @[to_additive, mono] lemma inv_subset_inv (h : s ⊆ t) : s⁻¹ ⊆ t⁻¹ := image_subset_image h attribute [mono] neg_subset_neg @[simp, to_additive] lemma inv_singleton (a : α) : ({a} : finset α)⁻¹ = {a⁻¹} := image_singleton _ _ @[simp, to_additive] lemma inv_insert (a : α) (s : finset α) : (insert a s)⁻¹ = insert a⁻¹ s⁻¹ := image_insert _ _ _ end has_inv open_locale pointwise section has_involutive_inv variables [decidable_eq α] [has_involutive_inv α] (s : finset α) @[simp, norm_cast, to_additive] lemma coe_inv : ↑(s⁻¹) = (s : set α)⁻¹ := coe_image.trans set.image_inv @[simp, to_additive] lemma card_inv : s⁻¹.card = s.card := card_image_of_injective _ inv_injective @[simp, to_additive] lemma preimage_inv : s.preimage has_inv.inv (inv_injective.inj_on _) = s⁻¹ := coe_injective $ by rw [coe_preimage, set.inv_preimage, coe_inv] end has_involutive_inv /-! ### Finset addition/multiplication -/ section has_mul variables [decidable_eq α] [decidable_eq β] [has_mul α] [has_mul β] [mul_hom_class F α β] (f : F) {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise multiplication of finsets `s * t` and `t` is defined as `{x * y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive "The pointwise addition of finsets `s + t` is defined as `{x + y | x ∈ s, y ∈ t}` in locale `pointwise`."] protected def has_mul : has_mul (finset α) := ⟨image₂ (*)⟩ localized "attribute [instance] finset.has_mul finset.has_add" in pointwise @[to_additive] lemma mul_def : s * t = (s ×ˢ t).image (λ p : α × α, p.1 * p.2) := rfl @[to_additive] lemma image_mul_product : (s ×ˢ t).image (λ x : α × α, x.fst * x.snd) = s * t := rfl @[to_additive] lemma mem_mul {x : α} : x ∈ s * t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y * z = x := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_mul (s t : finset α) : (↑(s * t) : set α) = ↑s * ↑t := coe_image₂ _ _ _ @[to_additive] lemma mul_mem_mul : a ∈ s → b ∈ t → a * b ∈ s * t := mem_image₂_of_mem @[to_additive] lemma card_mul_le : (s * t).card ≤ s.card * t.card := card_image₂_le _ _ _ @[to_additive] lemma card_mul_iff : (s * t).card = s.card * t.card ↔ (s ×ˢ t : set (α × α)).inj_on (λ p, p.1 * p.2) := card_image₂_iff @[simp, to_additive] lemma empty_mul (s : finset α) : ∅ * s = ∅ := image₂_empty_left @[simp, to_additive] lemma mul_empty (s : finset α) : s * ∅ = ∅ := image₂_empty_right @[simp, to_additive] lemma mul_eq_empty : s * t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma mul_nonempty : (s * t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.mul : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_mul_left : (s * t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_mul_right : (s * t).nonempty → t.nonempty := nonempty.of_image₂_right @[to_additive] lemma mul_singleton (a : α) : s * {a} = s.image (* a) := image₂_singleton_right @[to_additive] lemma singleton_mul (a : α) : {a} * s = s.image ((*) a) := image₂_singleton_left @[simp, to_additive] lemma singleton_mul_singleton (a b : α) : ({a} : finset α) * {b} = {a * b} := image₂_singleton @[to_additive, mono] lemma mul_subset_mul : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ * t₁ ⊆ s₂ * t₂ := image₂_subset @[to_additive] lemma mul_subset_mul_left : t₁ ⊆ t₂ → s * t₁ ⊆ s * t₂ := image₂_subset_left @[to_additive] lemma mul_subset_mul_right : s₁ ⊆ s₂ → s₁ * t ⊆ s₂ * t := image₂_subset_right @[to_additive] lemma mul_subset_iff : s * t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x * y ∈ u := image₂_subset_iff attribute [mono] add_subset_add @[to_additive] lemma union_mul : (s₁ ∪ s₂) * t = s₁ * t ∪ s₂ * t := image₂_union_left @[to_additive] lemma mul_union : s * (t₁ ∪ t₂) = s * t₁ ∪ s * t₂ := image₂_union_right @[to_additive] lemma inter_mul_subset : (s₁ ∩ s₂) * t ⊆ s₁ * t ∩ (s₂ * t) := image₂_inter_subset_left @[to_additive] lemma mul_inter_subset : s * (t₁ ∩ t₂) ⊆ s * t₁ ∩ (s * t₂) := image₂_inter_subset_right @[to_additive] lemma inter_mul_union_subset_union : s₁ ∩ s₂ * (t₁ ∪ t₂) ⊆ (s₁ * t₁) ∪ (s₂ * t₂) := image₂_inter_union_subset_union @[to_additive] lemma union_mul_inter_subset_union : (s₁ ∪ s₂) * (t₁ ∩ t₂) ⊆ (s₁ * t₁) ∪ (s₂ * t₂) := image₂_union_inter_subset_union /-- If a finset `u` is contained in the product of two sets `s * t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' * t'`. -/ @[to_additive "If a finset `u` is contained in the sum of two sets `s + t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' + t'`."] lemma subset_mul {s t : set α} : ↑u ⊆ s * t → ∃ s' t' : finset α, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' * t' := subset_image₂ @[to_additive] lemma image_mul : (s * t).image (f : α → β) = s.image f * t.image f := image_image₂_distrib $ map_mul f /-- The singleton operation as a `mul_hom`. -/ @[to_additive "The singleton operation as an `add_hom`."] def singleton_mul_hom : α →ₙ* finset α := ⟨singleton, λ a b, (singleton_mul_singleton _ _).symm⟩ @[simp, to_additive] lemma coe_singleton_mul_hom : (singleton_mul_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_mul_hom_apply (a : α) : singleton_mul_hom a = {a} := rfl /-- Lift a `mul_hom` to `finset` via `image`. -/ @[to_additive "Lift an `add_hom` to `finset` via `image`", simps] def image_mul_hom : finset α →ₙ* finset β := { to_fun := finset.image f, map_mul' := λ s t, image_mul _ } end has_mul /-! ### Finset subtraction/division -/ section has_div variables [decidable_eq α] [has_div α] {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise division of sfinets `s / t` is defined as `{x / y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive "The pointwise subtraction of finsets `s - t` is defined as `{x - y | x ∈ s, y ∈ t}` in locale `pointwise`."] protected def has_div : has_div (finset α) := ⟨image₂ (/)⟩ localized "attribute [instance] finset.has_div finset.has_sub" in pointwise @[to_additive] lemma div_def : s / t = (s ×ˢ t).image (λ p : α × α, p.1 / p.2) := rfl @[to_additive add_image_prod] lemma image_div_prod : (s ×ˢ t).image (λ x : α × α, x.fst / x.snd) = s / t := rfl @[to_additive] lemma mem_div : a ∈ s / t ↔ ∃ b c, b ∈ s ∧ c ∈ t ∧ b / c = a := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_div (s t : finset α) : (↑(s / t) : set α) = ↑s / ↑t := coe_image₂ _ _ _ @[to_additive] lemma div_mem_div : a ∈ s → b ∈ t → a / b ∈ s / t := mem_image₂_of_mem @[to_additive] lemma div_card_le : (s / t).card ≤ s.card * t.card := card_image₂_le _ _ _ @[simp, to_additive] lemma empty_div (s : finset α) : ∅ / s = ∅ := image₂_empty_left @[simp, to_additive] lemma div_empty (s : finset α) : s / ∅ = ∅ := image₂_empty_right @[simp, to_additive] lemma div_eq_empty : s / t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma div_nonempty : (s / t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.div : s.nonempty → t.nonempty → (s / t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_div_left : (s / t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_div_right : (s / t).nonempty → t.nonempty := nonempty.of_image₂_right @[simp, to_additive] lemma div_singleton (a : α) : s / {a} = s.image (/ a) := image₂_singleton_right @[simp, to_additive] lemma singleton_div (a : α) : {a} / s = s.image ((/) a) := image₂_singleton_left @[simp, to_additive] lemma singleton_div_singleton (a b : α) : ({a} : finset α) / {b} = {a / b} := image₂_singleton @[to_additive, mono] lemma div_subset_div : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ / t₁ ⊆ s₂ / t₂ := image₂_subset @[to_additive] lemma div_subset_div_left : t₁ ⊆ t₂ → s / t₁ ⊆ s / t₂ := image₂_subset_left @[to_additive] lemma div_subset_div_right : s₁ ⊆ s₂ → s₁ / t ⊆ s₂ / t := image₂_subset_right @[to_additive] lemma div_subset_iff : s / t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x / y ∈ u := image₂_subset_iff attribute [mono] sub_subset_sub @[to_additive] lemma union_div : (s₁ ∪ s₂) / t = s₁ / t ∪ s₂ / t := image₂_union_left @[to_additive] lemma div_union : s / (t₁ ∪ t₂) = s / t₁ ∪ s / t₂ := image₂_union_right @[to_additive] lemma inter_div_subset : (s₁ ∩ s₂) / t ⊆ s₁ / t ∩ (s₂ / t) := image₂_inter_subset_left @[to_additive] lemma div_inter_subset : s / (t₁ ∩ t₂) ⊆ s / t₁ ∩ (s / t₂) := image₂_inter_subset_right @[to_additive] lemma inter_div_union_subset_union : (s₁ ∩ s₂) / (t₁ ∪ t₂) ⊆ (s₁ / t₁) ∪ (s₂ / t₂) := image₂_inter_union_subset_union @[to_additive] lemma union_div_inter_subset_union : (s₁ ∪ s₂) / (t₁ ∩ t₂) ⊆ (s₁ / t₁) ∪ (s₂ / t₂) := image₂_union_inter_subset_union /-- If a finset `u` is contained in the product of two sets `s / t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' / t'`. -/ @[to_additive "If a finset `u` is contained in the sum of two sets `s - t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' - t'`."] lemma subset_div {s t : set α} : ↑u ⊆ s / t → ∃ s' t' : finset α, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' / t' := subset_image₂ end has_div /-! ### Instances -/ open_locale pointwise section instances variables [decidable_eq α] [decidable_eq β] /-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `finset`. See note [pointwise nat action]. -/ protected def has_nsmul [has_zero α] [has_add α] : has_smul ℕ (finset α) := ⟨nsmul_rec⟩ /-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a `finset`. See note [pointwise nat action]. -/ @[to_additive] protected def has_npow [has_one α] [has_mul α] : has_pow (finset α) ℕ := ⟨λ s n, npow_rec n s⟩ /-- Repeated pointwise addition/subtraction (not the same as pointwise repeated addition/subtraction!) of a `finset`. See note [pointwise nat action]. -/ protected def has_zsmul [has_zero α] [has_add α] [has_neg α] : has_smul ℤ (finset α) := ⟨zsmul_rec⟩ /-- Repeated pointwise multiplication/division (not the same as pointwise repeated multiplication/division!) of a `finset`. See note [pointwise nat action]. -/ @[to_additive] protected def has_zpow [has_one α] [has_mul α] [has_inv α] : has_pow (finset α) ℤ := ⟨λ s n, zpow_rec n s⟩ localized "attribute [instance] finset.has_nsmul finset.has_npow finset.has_zsmul finset.has_zpow" in pointwise /-- `finset α` is a `semigroup` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_semigroup` under pointwise operations if `α` is. "] protected def semigroup [semigroup α] : semigroup (finset α) := coe_injective.semigroup _ coe_mul section comm_semigroup variables [comm_semigroup α] {s t : finset α} /-- `finset α` is a `comm_semigroup` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_comm_semigroup` under pointwise operations if `α` is. "] protected def comm_semigroup : comm_semigroup (finset α) := coe_injective.comm_semigroup _ coe_mul @[to_additive] lemma inter_mul_union_subset : s ∩ t * (s ∪ t) ⊆ s * t := image₂_inter_union_subset mul_comm @[to_additive] lemma union_mul_inter_subset : (s ∪ t) * (s ∩ t) ⊆ s * t := image₂_union_inter_subset mul_comm end comm_semigroup section mul_one_class variables [mul_one_class α] /-- `finset α` is a `mul_one_class` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_zero_class` under pointwise operations if `α` is."] protected def mul_one_class : mul_one_class (finset α) := coe_injective.mul_one_class _ (coe_singleton 1) coe_mul localized "attribute [instance] finset.semigroup finset.add_semigroup finset.comm_semigroup finset.add_comm_semigroup finset.mul_one_class finset.add_zero_class" in pointwise @[to_additive] lemma subset_mul_left (s : finset α) {t : finset α} (ht : (1 : α) ∈ t) : s ⊆ s * t := λ a ha, mem_mul.2 ⟨a, 1, ha, ht, mul_one _⟩ @[to_additive] lemma subset_mul_right {s : finset α} (t : finset α) (hs : (1 : α) ∈ s) : t ⊆ s * t := λ a ha, mem_mul.2 ⟨1, a, hs, ha, one_mul _⟩ /-- The singleton operation as a `monoid_hom`. -/ @[to_additive "The singleton operation as an `add_monoid_hom`."] def singleton_monoid_hom : α →* finset α := { ..singleton_mul_hom, ..singleton_one_hom } @[simp, to_additive] lemma coe_singleton_monoid_hom : (singleton_monoid_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_monoid_hom_apply (a : α) : singleton_monoid_hom a = {a} := rfl /-- The coercion from `finset` to `set` as a `monoid_hom`. -/ @[to_additive "The coercion from `finset` to `set` as an `add_monoid_hom`."] def coe_monoid_hom : finset α →* set α := { to_fun := coe, map_one' := coe_one, map_mul' := coe_mul } @[simp, to_additive] lemma coe_coe_monoid_hom : (coe_monoid_hom : finset α → set α) = coe := rfl @[simp, to_additive] lemma coe_monoid_hom_apply (s : finset α) : coe_monoid_hom s = s := rfl /-- Lift a `monoid_hom` to `finset` via `image`. -/ @[to_additive "Lift an `add_monoid_hom` to `finset` via `image`", simps] def image_monoid_hom [mul_one_class β] [monoid_hom_class F α β] (f : F) : finset α →* finset β := { ..image_mul_hom f, ..image_one_hom f } end mul_one_class section monoid variables [monoid α] {s t : finset α} {a : α} {m n : ℕ} @[simp, norm_cast, to_additive] lemma coe_pow (s : finset α) (n : ℕ) : ↑(s ^ n) = (s ^ n : set α) := begin change ↑(npow_rec n s) = _, induction n with n ih, { rw [npow_rec, pow_zero, coe_one] }, { rw [npow_rec, pow_succ, coe_mul, ih] } end /-- `finset α` is a `monoid` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_monoid` under pointwise operations if `α` is. "] protected def monoid : monoid (finset α) := coe_injective.monoid _ coe_one coe_mul coe_pow localized "attribute [instance] finset.monoid finset.add_monoid" in pointwise @[to_additive] lemma pow_mem_pow (ha : a ∈ s) : ∀ n : ℕ, a ^ n ∈ s ^ n | 0 := by { rw pow_zero, exact one_mem_one } | (n + 1) := by { rw pow_succ, exact mul_mem_mul ha (pow_mem_pow _) } @[to_additive] lemma pow_subset_pow (hst : s ⊆ t) : ∀ n : ℕ, s ^ n ⊆ t ^ n | 0 := by { rw pow_zero, exact subset.rfl } | (n + 1) := by { rw pow_succ, exact mul_subset_mul hst (pow_subset_pow _) } @[to_additive] lemma pow_subset_pow_of_one_mem (hs : (1 : α) ∈ s) : m ≤ n → s ^ m ⊆ s ^ n := begin refine nat.le_induction _ (λ n h ih, _) _, { exact subset.rfl }, { rw pow_succ, exact ih.trans (subset_mul_right _ hs) } end @[simp, norm_cast, to_additive] lemma coe_list_prod (s : list (finset α)) : (↑s.prod : set α) = (s.map coe).prod := map_list_prod (coe_monoid_hom : finset α →* set α) _ @[to_additive] lemma mem_prod_list_of_fn {a : α} {s : fin n → finset α} : a ∈ (list.of_fn s).prod ↔ ∃ f : (Π i : fin n, s i), (list.of_fn (λ i, (f i : α))).prod = a := by { rw [←mem_coe, coe_list_prod, list.map_of_fn, set.mem_prod_list_of_fn], refl } @[to_additive] lemma mem_pow {a : α} {n : ℕ} : a ∈ s ^ n ↔ ∃ f : fin n → s, (list.of_fn (λ i, ↑(f i))).prod = a := by { simp_rw [←mem_coe, coe_pow, set.mem_pow], refl } @[simp, to_additive] lemma empty_pow (hn : n ≠ 0) : (∅ : finset α) ^ n = ∅ := by rw [←tsub_add_cancel_of_le (nat.succ_le_of_lt $ nat.pos_of_ne_zero hn), pow_succ, empty_mul] @[to_additive] lemma mul_univ_of_one_mem [fintype α] (hs : (1 : α) ∈ s) : s * univ = univ := eq_univ_iff_forall.2 $ λ a, mem_mul.2 ⟨_, _, hs, mem_univ _, one_mul _⟩ @[to_additive] lemma univ_mul_of_one_mem [fintype α] (ht : (1 : α) ∈ t) : univ * t = univ := eq_univ_iff_forall.2 $ λ a, mem_mul.2 ⟨_, _, mem_univ _, ht, mul_one _⟩ @[simp, to_additive] lemma univ_mul_univ [fintype α] : (univ : finset α) * univ = univ := mul_univ_of_one_mem $ mem_univ _ @[simp, to_additive nsmul_univ] lemma univ_pow [fintype α] (hn : n ≠ 0) : (univ : finset α) ^ n = univ := coe_injective $ by rw [coe_pow, coe_univ, set.univ_pow hn] @[to_additive] protected lemma _root_.is_unit.finset : is_unit a → is_unit ({a} : finset α) := is_unit.map (singleton_monoid_hom : α →* finset α) end monoid section comm_monoid variables [comm_monoid α] /-- `finset α` is a `comm_monoid` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_comm_monoid` under pointwise operations if `α` is. "] protected def comm_monoid : comm_monoid (finset α) := coe_injective.comm_monoid _ coe_one coe_mul coe_pow localized "attribute [instance] finset.comm_monoid finset.add_comm_monoid" in pointwise @[simp, norm_cast, to_additive] lemma coe_prod {ι : Type*} (s : finset ι) (f : ι → finset α) : (↑(∏ i in s, f i) : set α) = ∏ i in s, f i := map_prod (coe_monoid_hom : finset α →* set α) _ _ end comm_monoid open_locale pointwise section division_monoid variables [division_monoid α] {s t : finset α} @[simp, to_additive] lemma coe_zpow (s : finset α) : ∀ n : ℤ, ↑(s ^ n) = (s ^ n : set α) | (int.of_nat n) := coe_pow _ _ | (int.neg_succ_of_nat n) := by { refine (coe_inv _).trans _, convert congr_arg has_inv.inv (coe_pow _ _) } @[to_additive] protected lemma mul_eq_one_iff : s * t = 1 ↔ ∃ a b, s = {a} ∧ t = {b} ∧ a * b = 1 := by simp_rw [←coe_inj, coe_mul, coe_one, set.mul_eq_one_iff, coe_singleton] /-- `finset α` is a division monoid under pointwise operations if `α` is. -/ @[to_additive "`finset α` is a subtraction monoid under pointwise operations if `α` is."] protected def division_monoid : division_monoid (finset α) := coe_injective.division_monoid _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow @[simp, to_additive] lemma is_unit_iff : is_unit s ↔ ∃ a, s = {a} ∧ is_unit a := begin split, { rintro ⟨u, rfl⟩, obtain ⟨a, b, ha, hb, h⟩ := finset.mul_eq_one_iff.1 u.mul_inv, refine ⟨a, ha, ⟨a, b, h, singleton_injective _⟩, rfl⟩, rw [←singleton_mul_singleton, ←ha, ←hb], exact u.inv_mul }, { rintro ⟨a, rfl, ha⟩, exact ha.finset } end @[simp, to_additive] lemma is_unit_coe : is_unit (s : set α) ↔ is_unit s := by simp_rw [is_unit_iff, set.is_unit_iff, coe_eq_singleton] end division_monoid /-- `finset α` is a commutative division monoid under pointwise operations if `α` is. -/ @[to_additive subtraction_comm_monoid "`finset α` is a commutative subtraction monoid under pointwise operations if `α` is."] protected def division_comm_monoid [division_comm_monoid α] : division_comm_monoid (finset α) := coe_injective.division_comm_monoid _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow /-- `finset α` has distributive negation if `α` has. -/ protected def has_distrib_neg [has_mul α] [has_distrib_neg α] : has_distrib_neg (finset α) := coe_injective.has_distrib_neg _ coe_neg coe_mul localized "attribute [instance] finset.division_monoid finset.subtraction_monoid finset.division_comm_monoid finset.subtraction_comm_monoid finset.has_distrib_neg" in pointwise section distrib variables [distrib α] (s t u : finset α) /-! Note that `finset α` is not a `distrib` because `s * t + s * u` has cross terms that `s * (t + u)` lacks. ```lean -- {10, 16, 18, 20, 8, 9} #eval {1, 2} * ({3, 4} + {5, 6} : finset ℕ) -- {10, 11, 12, 13, 14, 15, 16, 18, 20, 8, 9} #eval ({1, 2} : finset ℕ) * {3, 4} + {1, 2} * {5, 6} ``` -/ lemma mul_add_subset : s * (t + u) ⊆ s * t + s * u := image₂_distrib_subset_left mul_add lemma add_mul_subset : (s + t) * u ⊆ s * u + t * u := image₂_distrib_subset_right add_mul end distrib section mul_zero_class variables [mul_zero_class α] {s t : finset α} /-! Note that `finset` is not a `mul_zero_class` because `0 * ∅ ≠ 0`. -/ lemma mul_zero_subset (s : finset α) : s * 0 ⊆ 0 := by simp [subset_iff, mem_mul] lemma zero_mul_subset (s : finset α) : 0 * s ⊆ 0 := by simp [subset_iff, mem_mul] lemma nonempty.mul_zero (hs : s.nonempty) : s * 0 = 0 := s.mul_zero_subset.antisymm $ by simpa [mem_mul] using hs lemma nonempty.zero_mul (hs : s.nonempty) : 0 * s = 0 := s.zero_mul_subset.antisymm $ by simpa [mem_mul] using hs end mul_zero_class section group variables [group α] [division_monoid β] [monoid_hom_class F α β] (f : F) {s t : finset α} {a b : α} /-! Note that `finset` is not a `group` because `s / s ≠ 1` in general. -/ @[simp, to_additive] lemma one_mem_div_iff : (1 : α) ∈ s / t ↔ ¬ disjoint s t := by rw [←mem_coe, ←disjoint_coe, coe_div, set.one_mem_div_iff] @[to_additive] lemma not_one_mem_div_iff : (1 : α) ∉ s / t ↔ disjoint s t := one_mem_div_iff.not_left @[to_additive] lemma nonempty.one_mem_div (h : s.nonempty) : (1 : α) ∈ s / s := let ⟨a, ha⟩ := h in mem_div.2 ⟨a, a, ha, ha, div_self' _⟩ @[to_additive] lemma is_unit_singleton (a : α) : is_unit ({a} : finset α) := (group.is_unit a).finset @[simp] lemma is_unit_iff_singleton : is_unit s ↔ ∃ a, s = {a} := by simp only [is_unit_iff, group.is_unit, and_true] @[simp, to_additive] lemma image_mul_left : image (λ b, a * b) t = preimage t (λ b, a⁻¹ * b) ((mul_right_injective _).inj_on _) := coe_injective $ by simp @[simp, to_additive] lemma image_mul_right : image (* b) t = preimage t (* b⁻¹) ((mul_left_injective _).inj_on _) := coe_injective $ by simp @[to_additive] lemma image_mul_left' : image (λ b, a⁻¹ * b) t = preimage t (λ b, a * b) ((mul_right_injective _).inj_on _) := by simp @[to_additive] lemma image_mul_right' : image (* b⁻¹) t = preimage t (* b) ((mul_left_injective _).inj_on _) := by simp lemma image_div : (s / t).image (f : α → β) = s.image f / t.image f := image_image₂_distrib $ map_div f end group section group_with_zero variables [group_with_zero α] {s t : finset α} lemma div_zero_subset (s : finset α) : s / 0 ⊆ 0 := by simp [subset_iff, mem_div] lemma zero_div_subset (s : finset α) : 0 / s ⊆ 0 := by simp [subset_iff, mem_div] lemma nonempty.div_zero (hs : s.nonempty) : s / 0 = 0 := s.div_zero_subset.antisymm $ by simpa [mem_div] using hs lemma nonempty.zero_div (hs : s.nonempty) : 0 / s = 0 := s.zero_div_subset.antisymm $ by simpa [mem_div] using hs end group_with_zero end instances section group variables [group α] {s t : finset α} {a b : α} @[simp, to_additive] lemma preimage_mul_left_singleton : preimage {b} ((*) a) ((mul_right_injective _).inj_on _) = {a⁻¹ * b} := by { classical, rw [← image_mul_left', image_singleton] } @[simp, to_additive] lemma preimage_mul_right_singleton : preimage {b} (* a) ((mul_left_injective _).inj_on _) = {b * a⁻¹} := by { classical, rw [← image_mul_right', image_singleton] } @[simp, to_additive] lemma preimage_mul_left_one : preimage 1 ((*) a) ((mul_right_injective _).inj_on _) = {a⁻¹} := by { classical, rw [← image_mul_left', image_one, mul_one] } @[simp, to_additive] lemma preimage_mul_right_one : preimage 1 (* b) ((mul_left_injective _).inj_on _) = {b⁻¹} := by { classical, rw [← image_mul_right', image_one, one_mul] } @[to_additive] lemma preimage_mul_left_one' : preimage 1 ((*) a⁻¹) ((mul_right_injective _).inj_on _) = {a} := by rw [preimage_mul_left_one, inv_inv] @[to_additive] lemma preimage_mul_right_one' : preimage 1 (* b⁻¹) ((mul_left_injective _).inj_on _) = {b} := by rw [preimage_mul_right_one, inv_inv] end group /-! ### Scalar addition/multiplication of finsets -/ section has_smul variables [decidable_eq β] [has_smul α β] {s s₁ s₂ : finset α} {t t₁ t₂ u : finset β} {a : α} {b : β} /-- The pointwise product of two finsets `s` and `t`: `s • t = {x • y | x ∈ s, y ∈ t}`. -/ @[to_additive "The pointwise sum of two finsets `s` and `t`: `s +ᵥ t = {x +ᵥ y | x ∈ s, y ∈ t}`."] protected def has_smul : has_smul (finset α) (finset β) := ⟨image₂ (•)⟩ localized "attribute [instance] finset.has_smul finset.has_vadd" in pointwise @[to_additive] lemma smul_def : s • t = (s ×ˢ t).image (λ p : α × β, p.1 • p.2) := rfl @[to_additive] lemma image_smul_product : (s ×ˢ t).image (λ x : α × β, x.fst • x.snd) = s • t := rfl @[to_additive] lemma mem_smul {x : β} : x ∈ s • t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y • z = x := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_smul (s : finset α) (t : finset β) : (↑(s • t) : set β) = (s : set α) • t := coe_image₂ _ _ _ @[to_additive] lemma smul_mem_smul : a ∈ s → b ∈ t → a • b ∈ s • t := mem_image₂_of_mem @[to_additive] lemma smul_card_le : (s • t).card ≤ s.card • t.card := card_image₂_le _ _ _ @[simp, to_additive] lemma empty_smul (t : finset β) : (∅ : finset α) • t = ∅ := image₂_empty_left @[simp, to_additive] lemma smul_empty (s : finset α) : s • (∅ : finset β) = ∅ := image₂_empty_right @[simp, to_additive] lemma smul_eq_empty : s • t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma smul_nonempty_iff : (s • t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.smul : s.nonempty → t.nonempty → (s • t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_smul_left : (s • t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_smul_right : (s • t).nonempty → t.nonempty := nonempty.of_image₂_right @[to_additive] lemma smul_singleton (b : β) : s • ({b} : finset β) = s.image (• b) := image₂_singleton_right @[to_additive] lemma singleton_smul_singleton (a : α) (b : β) : ({a} : finset α) • ({b} : finset β) = {a • b} := image₂_singleton @[to_additive, mono] lemma smul_subset_smul : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ • t₁ ⊆ s₂ • t₂ := image₂_subset @[to_additive] lemma smul_subset_smul_left : t₁ ⊆ t₂ → s • t₁ ⊆ s • t₂ := image₂_subset_left @[to_additive] lemma smul_subset_smul_right : s₁ ⊆ s₂ → s₁ • t ⊆ s₂ • t := image₂_subset_right @[to_additive] lemma smul_subset_iff : s • t ⊆ u ↔ ∀ (a ∈ s) (b ∈ t), a • b ∈ u := image₂_subset_iff attribute [mono] vadd_subset_vadd @[to_additive] lemma union_smul [decidable_eq α] : (s₁ ∪ s₂) • t = s₁ • t ∪ s₂ • t := image₂_union_left @[to_additive] lemma smul_union : s • (t₁ ∪ t₂) = s • t₁ ∪ s • t₂ := image₂_union_right @[to_additive] lemma inter_smul_subset [decidable_eq α] : (s₁ ∩ s₂) • t ⊆ s₁ • t ∩ s₂ • t := image₂_inter_subset_left @[to_additive] lemma smul_inter_subset : s • (t₁ ∩ t₂) ⊆ s • t₁ ∩ s • t₂ := image₂_inter_subset_right @[to_additive] lemma inter_smul_union_subset_union [decidable_eq α] : (s₁ ∩ s₂) • (t₁ ∪ t₂) ⊆ (s₁ • t₁) ∪ (s₂ • t₂) := image₂_inter_union_subset_union @[to_additive] lemma union_smul_inter_subset_union [decidable_eq α] : (s₁ ∪ s₂) • (t₁ ∩ t₂) ⊆ (s₁ • t₁) ∪ (s₂ • t₂) := image₂_union_inter_subset_union /-- If a finset `u` is contained in the scalar product of two sets `s • t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' • t'`. -/ @[to_additive "If a finset `u` is contained in the scalar sum of two sets `s +ᵥ t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' +ᵥ t'`."] lemma subset_smul {s : set α} {t : set β} : ↑u ⊆ s • t → ∃ (s' : finset α) (t' : finset β), ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' • t' := subset_image₂ end has_smul /-! ### Scalar subtraction of finsets -/ section has_vsub variables [decidable_eq α] [has_vsub α β] {s s₁ s₂ t t₁ t₂ : finset β} {u : finset α} {a : α} {b c : β} include α /-- The pointwise product of two finsets `s` and `t`: `s -ᵥ t = {x -ᵥ y | x ∈ s, y ∈ t}`. -/ protected def has_vsub : has_vsub (finset α) (finset β) := ⟨image₂ (-ᵥ)⟩ localized "attribute [instance] finset.has_vsub" in pointwise lemma vsub_def : s -ᵥ t = image₂ (-ᵥ) s t := rfl @[simp] lemma image_vsub_product : image₂ (-ᵥ) s t = s -ᵥ t := rfl lemma mem_vsub : a ∈ s -ᵥ t ↔ ∃ b c, b ∈ s ∧ c ∈ t ∧ b -ᵥ c = a := mem_image₂ @[simp, norm_cast] lemma coe_vsub (s t : finset β) : (↑(s -ᵥ t) : set α) = (s : set β) -ᵥ t := coe_image₂ _ _ _ lemma vsub_mem_vsub : b ∈ s → c ∈ t → b -ᵥ c ∈ s -ᵥ t := mem_image₂_of_mem lemma vsub_card_le : (s -ᵥ t : finset α).card ≤ s.card * t.card := card_image₂_le _ _ _ @[simp] lemma empty_vsub (t : finset β) : (∅ : finset β) -ᵥ t = ∅ := image₂_empty_left @[simp] lemma vsub_empty (s : finset β) : s -ᵥ (∅ : finset β) = ∅ := image₂_empty_right @[simp] lemma vsub_eq_empty : s -ᵥ t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp] lemma vsub_nonempty : (s -ᵥ t : finset α).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff lemma nonempty.vsub : s.nonempty → t.nonempty → (s -ᵥ t : finset α).nonempty := nonempty.image₂ lemma nonempty.of_vsub_left : (s -ᵥ t : finset α).nonempty → s.nonempty := nonempty.of_image₂_left lemma nonempty.of_vsub_right : (s -ᵥ t : finset α).nonempty → t.nonempty := nonempty.of_image₂_right @[simp] lemma vsub_singleton (b : β) : s -ᵥ ({b} : finset β) = s.image (-ᵥ b) := image₂_singleton_right lemma singleton_vsub (a : β) : ({a} : finset β) -ᵥ t = t.image ((-ᵥ) a) := image₂_singleton_left @[simp] lemma singleton_vsub_singleton (a b : β) : ({a} : finset β) -ᵥ {b} = {a -ᵥ b} := image₂_singleton @[mono] lemma vsub_subset_vsub : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ -ᵥ t₁ ⊆ s₂ -ᵥ t₂ := image₂_subset lemma vsub_subset_vsub_left : t₁ ⊆ t₂ → s -ᵥ t₁ ⊆ s -ᵥ t₂ := image₂_subset_left lemma vsub_subset_vsub_right : s₁ ⊆ s₂ → s₁ -ᵥ t ⊆ s₂ -ᵥ t := image₂_subset_right lemma vsub_subset_iff : s -ᵥ t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x -ᵥ y ∈ u := image₂_subset_iff section variables [decidable_eq β] lemma union_vsub : (s₁ ∪ s₂) -ᵥ t = (s₁ -ᵥ t) ∪ (s₂ -ᵥ t) := image₂_union_left lemma vsub_union : s -ᵥ (t₁ ∪ t₂) = (s -ᵥ t₁) ∪ (s -ᵥ t₂) := image₂_union_right lemma inter_vsub_subset : (s₁ ∩ s₂) -ᵥ t ⊆ (s₁ -ᵥ t) ∩ (s₂ -ᵥ t) := image₂_inter_subset_left lemma vsub_inter_subset : s -ᵥ (t₁ ∩ t₂) ⊆ (s -ᵥ t₁) ∩ (s -ᵥ t₂) := image₂_inter_subset_right end /-- If a finset `u` is contained in the pointwise subtraction of two sets `s -ᵥ t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' -ᵥ t'`. -/ lemma subset_vsub {s t : set β} : ↑u ⊆ s -ᵥ t → ∃ s' t' : finset β, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' -ᵥ t' := subset_image₂ end has_vsub open_locale pointwise /-! ### Translation/scaling of finsets -/ section has_smul variables [decidable_eq β] [has_smul α β] {s s₁ s₂ t u : finset β} {a : α} {b : β} /-- The scaling of a finset `s` by a scalar `a`: `a • s = {a • x | x ∈ s}`. -/ @[to_additive "The translation of a finset `s` by a vector `a`: `a +ᵥ s = {a +ᵥ x | x ∈ s}`."] protected def has_smul_finset : has_smul α (finset β) := ⟨λ a, image $ (•) a⟩ localized "attribute [instance] finset.has_smul_finset finset.has_vadd_finset" in pointwise @[to_additive] lemma smul_finset_def : a • s = s.image ((•) a) := rfl @[to_additive] lemma image_smul : s.image (λ x, a • x) = a • s := rfl @[to_additive] lemma mem_smul_finset {x : β} : x ∈ a • s ↔ ∃ y, y ∈ s ∧ a • y = x := by simp only [finset.smul_finset_def, and.assoc, mem_image, exists_prop, prod.exists, mem_product] @[simp, norm_cast, to_additive] lemma coe_smul_finset (a : α) (s : finset β) : (↑(a • s) : set β) = a • s := coe_image @[to_additive] lemma smul_mem_smul_finset : b ∈ s → a • b ∈ a • s := mem_image_of_mem _ @[to_additive] lemma smul_finset_card_le : (a • s).card ≤ s.card := card_image_le @[simp, to_additive] lemma smul_finset_empty (a : α) : a • (∅ : finset β) = ∅ := image_empty _ @[simp, to_additive] lemma smul_finset_eq_empty : a • s = ∅ ↔ s = ∅ := image_eq_empty @[simp, to_additive] lemma smul_finset_nonempty : (a • s).nonempty ↔ s.nonempty := nonempty.image_iff _ @[to_additive] lemma nonempty.smul_finset (hs : s.nonempty) : (a • s).nonempty := hs.image _ @[simp, to_additive] lemma singleton_smul (a : α) : ({a} : finset α) • t = a • t := image₂_singleton_left @[to_additive, mono] lemma smul_finset_subset_smul_finset : s ⊆ t → a • s ⊆ a • t := image_subset_image attribute [mono] vadd_finset_subset_vadd_finset @[simp, to_additive] lemma smul_finset_singleton (b : β) : a • ({b} : finset β) = {a • b} := image_singleton _ _ @[to_additive] lemma smul_finset_union : a • (s₁ ∪ s₂) = a • s₁ ∪ a • s₂ := image_union _ _ @[to_additive] lemma smul_finset_inter_subset : a • (s₁ ∩ s₂) ⊆ a • s₁ ∩ (a • s₂) := image_inter_subset _ _ _ @[to_additive] lemma smul_finset_subset_smul {s : finset α} : a ∈ s → a • t ⊆ s • t := image_subset_image₂_right @[simp, to_additive] lemma bUnion_smul_finset (s : finset α) (t : finset β) : s.bUnion (• t) = s • t := bUnion_image_left end has_smul open_locale pointwise section instances variables [decidable_eq γ] @[to_additive] instance smul_comm_class_finset [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α β (finset γ) := ⟨λ _ _, commute.finset_image $ smul_comm _ _⟩ @[to_additive] instance smul_comm_class_finset' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_comm]⟩ @[to_additive] instance smul_comm_class_finset'' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (finset α) β (finset γ) := by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _ @[to_additive] instance smul_comm_class [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (finset α) (finset β) (finset γ) := ⟨λ s t u, coe_injective $ by simp_rw [coe_smul, smul_comm]⟩ @[to_additive] instance is_scalar_tower [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α β (finset γ) := ⟨λ a b s, by simp only [←image_smul, image_image, smul_assoc]⟩ variables [decidable_eq β] @[to_additive] instance is_scalar_tower' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_assoc]⟩ @[to_additive] instance is_scalar_tower'' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower (finset α) (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_assoc]⟩ @[to_additive] instance is_central_scalar [has_smul α β] [has_smul αᵐᵒᵖ β] [is_central_scalar α β] : is_central_scalar α (finset β) := ⟨λ a s, coe_injective $ by simp only [coe_smul_finset, coe_smul, op_smul_eq_smul]⟩ /-- A multiplicative action of a monoid `α` on a type `β` gives a multiplicative action of `finset α` on `finset β`. -/ @[to_additive "An additive action of an additive monoid `α` on a type `β` gives an additive action of `finset α` on `finset β`"] protected def mul_action [decidable_eq α] [monoid α] [mul_action α β] : mul_action (finset α) (finset β) := { mul_smul := λ _ _ _, image₂_assoc mul_smul, one_smul := λ s, image₂_singleton_left.trans $ by simp_rw [one_smul, image_id'] } /-- A multiplicative action of a monoid on a type `β` gives a multiplicative action on `finset β`. -/ @[to_additive "An additive action of an additive monoid on a type `β` gives an additive action on `finset β`."] protected def mul_action_finset [monoid α] [mul_action α β] : mul_action α (finset β) := coe_injective.mul_action _ coe_smul_finset localized "attribute [instance] finset.mul_action_finset finset.add_action_finset finset.mul_action finset.add_action" in pointwise /-- A distributive multiplicative action of a monoid on an additive monoid `β` gives a distributive multiplicative action on `finset β`. -/ protected def distrib_mul_action_finset [monoid α] [add_monoid β] [distrib_mul_action α β] : distrib_mul_action α (finset β) := function.injective.distrib_mul_action ⟨coe, coe_zero, coe_add⟩ coe_injective coe_smul_finset /-- A multiplicative action of a monoid on a monoid `β` gives a multiplicative action on `set β`. -/ protected def mul_distrib_mul_action_finset [monoid α] [monoid β] [mul_distrib_mul_action α β] : mul_distrib_mul_action α (finset β) := function.injective.mul_distrib_mul_action ⟨coe, coe_one, coe_mul⟩ coe_injective coe_smul_finset localized "attribute [instance] finset.distrib_mul_action_finset finset.mul_distrib_mul_action_finset" in pointwise instance [decidable_eq α] [has_zero α] [has_mul α] [no_zero_divisors α] : no_zero_divisors (finset α) := coe_injective.no_zero_divisors _ coe_zero coe_mul instance [has_zero α] [has_zero β] [has_smul α β] [no_zero_smul_divisors α β] : no_zero_smul_divisors (finset α) (finset β) := ⟨λ s t h, begin by_contra' H, have hst : (s • t).nonempty := h.symm.subst zero_nonempty, simp_rw [←hst.of_smul_left.subset_zero_iff, ←hst.of_smul_right.subset_zero_iff, not_subset, mem_zero] at H, obtain ⟨⟨a, hs, ha⟩, b, ht, hb⟩ := H, have := subset_of_eq h, exact (eq_zero_or_eq_zero_of_smul_eq_zero $ mem_zero.1 $ this $ smul_mem_smul hs ht).elim ha hb, end⟩ instance no_zero_smul_divisors_finset [has_zero α] [has_zero β] [has_smul α β] [no_zero_smul_divisors α β] : no_zero_smul_divisors α (finset β) := coe_injective.no_zero_smul_divisors _ coe_zero coe_smul_finset end instances section has_smul variables [decidable_eq β] [decidable_eq γ] [has_smul αᵐᵒᵖ β] [has_smul β γ] [has_smul α γ] -- TODO: replace hypothesis and conclusion with a typeclass @[to_additive] lemma op_smul_finset_smul_eq_smul_smul_finset (a : α) (s : finset β) (t : finset γ) (h : ∀ (a : α) (b : β) (c : γ), (op a • b) • c = b • a • c) : (op a • s) • t = s • a • t := by { ext, simp [mem_smul, mem_smul_finset, h] } end has_smul section has_mul variables [has_mul α] [decidable_eq α] {s t u : finset α} {a : α} @[to_additive] lemma op_smul_finset_subset_mul : a ∈ t → op a • s ⊆ s * t := image_subset_image₂_left @[simp, to_additive] lemma bUnion_op_smul_finset (s t : finset α) : t.bUnion (λ a, op a • s) = s * t := bUnion_image_right @[to_additive] lemma mul_subset_iff_left : s * t ⊆ u ↔ ∀ a ∈ s, a • t ⊆ u := image₂_subset_iff_left @[to_additive] lemma mul_subset_iff_right : s * t ⊆ u ↔ ∀ b ∈ t, op b • s ⊆ u := image₂_subset_iff_right end has_mul section semigroup variables [semigroup α] [decidable_eq α] @[to_additive] lemma op_smul_finset_mul_eq_mul_smul_finset (a : α) (s : finset α) (t : finset α) : (op a • s) * t = s * a • t := op_smul_finset_smul_eq_smul_smul_finset _ _ _ $ λ _ _ _, mul_assoc _ _ _ end semigroup section left_cancel_semigroup variables [left_cancel_semigroup α] [decidable_eq α] (s t : finset α) (a : α) @[to_additive] lemma pairwise_disjoint_smul_iff {s : set α} {t : finset α} : s.pairwise_disjoint (• t) ↔ (s ×ˢ t : set (α × α)).inj_on (λ p, p.1 * p.2) := by simp_rw [←pairwise_disjoint_coe, coe_smul_finset, set.pairwise_disjoint_smul_iff] @[simp, to_additive] lemma card_singleton_mul : ({a} * t).card = t.card := card_image₂_singleton_left _ $ mul_right_injective _ @[to_additive] lemma singleton_mul_inter : {a} * (s ∩ t) = ({a} * s) ∩ ({a} * t) := image₂_singleton_inter _ _ $ mul_right_injective _ @[to_additive] lemma card_le_card_mul_left {s : finset α} (hs : s.nonempty) : t.card ≤ (s * t).card := card_le_card_image₂_left _ hs mul_right_injective end left_cancel_semigroup section variables [right_cancel_semigroup α] [decidable_eq α] (s t : finset α) (a : α) @[simp, to_additive] lemma card_mul_singleton : (s * {a}).card = s.card := card_image₂_singleton_right _ $ mul_left_injective _ @[to_additive] lemma inter_mul_singleton : (s ∩ t) * {a} = (s * {a}) ∩ (t * {a}) := image₂_inter_singleton _ _ $ mul_left_injective _ @[to_additive] lemma card_le_card_mul_right {t : finset α} (ht : t.nonempty) : s.card ≤ (s * t).card := card_le_card_image₂_right _ ht mul_left_injective end open_locale pointwise @[to_additive] lemma image_smul_comm [decidable_eq β] [decidable_eq γ] [has_smul α β] [has_smul α γ] (f : β → γ) (a : α) (s : finset β) : (∀ b, f (a • b) = a • f b) → (a • s).image f = a • s.image f := image_comm @[to_additive] lemma image_smul_distrib [decidable_eq α] [decidable_eq β] [monoid α] [monoid β] [monoid_hom_class F α β] (f : F) (a : α) (s : finset α) : (a • s).image f = f a • s.image f := image_comm $ map_mul _ _ section group variables [decidable_eq β] [group α] [mul_action α β] {s t : finset β} {a : α} {b : β} @[simp, to_additive] lemma smul_mem_smul_finset_iff (a : α) : a • b ∈ a • s ↔ b ∈ s := (mul_action.injective _).mem_finset_image @[to_additive] lemma inv_smul_mem_iff : a⁻¹ • b ∈ s ↔ b ∈ a • s := by rw [←smul_mem_smul_finset_iff a, smul_inv_smul] @[to_additive] lemma mem_inv_smul_finset_iff : b ∈ a⁻¹ • s ↔ a • b ∈ s := by rw [←smul_mem_smul_finset_iff a, smul_inv_smul] @[simp, to_additive] lemma smul_finset_subset_smul_finset_iff : a • s ⊆ a • t ↔ s ⊆ t := image_subset_image_iff $ mul_action.injective _ @[to_additive] lemma smul_finset_subset_iff : a • s ⊆ t ↔ s ⊆ a⁻¹ • t := by { simp_rw ←coe_subset, push_cast, exact set.set_smul_subset_iff } @[to_additive] lemma subset_smul_finset_iff : s ⊆ a • t ↔ a⁻¹ • s ⊆ t := by { simp_rw ←coe_subset, push_cast, exact set.subset_set_smul_iff } @[to_additive] lemma smul_finset_inter : a • (s ∩ t) = a • s ∩ a • t := image_inter _ _ $ mul_action.injective a @[to_additive] lemma smul_finset_sdiff : a • (s \ t) = a • s \ a • t := image_sdiff _ _ $ mul_action.injective a @[to_additive] lemma smul_finset_symm_diff : a • (s ∆ t) = (a • s) ∆ (a • t) := image_symm_diff _ _ $ mul_action.injective a @[simp, to_additive] lemma smul_finset_univ [fintype β] : a • (univ : finset β) = univ := image_univ_of_surjective $ mul_action.surjective a @[simp, to_additive] lemma smul_univ [fintype β] {s : finset α} (hs : s.nonempty) : s • (univ : finset β) = univ := coe_injective $ by { push_cast, exact set.smul_univ hs } @[simp, to_additive] lemma card_smul_finset (a : α) (s : finset β) : (a • s).card = s.card := card_image_of_injective _ $ mul_action.injective _ end group section group_with_zero variables [decidable_eq β] [group_with_zero α] [mul_action α β] {s t : finset β} {a : α} {b : β} @[simp] lemma smul_mem_smul_finset_iff₀ (ha : a ≠ 0) : a • b ∈ a • s ↔ b ∈ s := smul_mem_smul_finset_iff (units.mk0 a ha) lemma inv_smul_mem_iff₀ (ha : a ≠ 0) : a⁻¹ • b ∈ s ↔ b ∈ a • s := show _ ↔ _ ∈ units.mk0 a ha • _, from inv_smul_mem_iff lemma mem_inv_smul_finset_iff₀ (ha : a ≠ 0) : b ∈ a⁻¹ • s ↔ a • b ∈ s := show _ ∈ (units.mk0 a ha)⁻¹ • _ ↔ _, from mem_inv_smul_finset_iff @[simp] lemma smul_finset_subset_smul_finset_iff₀ (ha : a ≠ 0) : a • s ⊆ a • t ↔ s ⊆ t := show units.mk0 a ha • _ ⊆ _ ↔ _, from smul_finset_subset_smul_finset_iff lemma smul_finset_subset_iff₀ (ha : a ≠ 0) : a • s ⊆ t ↔ s ⊆ a⁻¹ • t := show units.mk0 a ha • _ ⊆ _ ↔ _, from smul_finset_subset_iff lemma subset_smul_finset_iff₀ (ha : a ≠ 0) : s ⊆ a • t ↔ a⁻¹ • s ⊆ t := show _ ⊆ units.mk0 a ha • _ ↔ _, from subset_smul_finset_iff lemma smul_finset_inter₀ (ha : a ≠ 0) : a • (s ∩ t) = a • s ∩ a • t := image_inter _ _ $ mul_action.injective₀ ha lemma smul_finset_sdiff₀ (ha : a ≠ 0) : a • (s \ t) = a • s \ a • t := image_sdiff _ _ $ mul_action.injective₀ ha lemma smul_finset_symm_diff₀ (ha : a ≠ 0) : a • (s ∆ t) = (a • s) ∆ (a • t) := image_symm_diff _ _ $ mul_action.injective₀ ha lemma smul_univ₀ [fintype β] {s : finset α} (hs : ¬ s ⊆ 0) : s • (univ : finset β) = univ := coe_injective $ by { rw ←coe_subset at hs, push_cast at ⊢ hs, exact set.smul_univ₀ hs } lemma smul_finset_univ₀ [fintype β] (ha : a ≠ 0) : a • (univ : finset β) = univ := coe_injective $ by { push_cast, exact set.smul_set_univ₀ ha } end group_with_zero section smul_with_zero variables [has_zero α] [has_zero β] [smul_with_zero α β] [decidable_eq β] {s : finset α} {t : finset β} /-! Note that we have neither `smul_with_zero α (finset β)` nor `smul_with_zero (finset α) (finset β)` because `0 * ∅ ≠ 0`. -/ lemma smul_zero_subset (s : finset α) : s • (0 : finset β) ⊆ 0 := by simp [subset_iff, mem_smul] lemma zero_smul_subset (t : finset β) : (0 : finset α) • t ⊆ 0 := by simp [subset_iff, mem_smul] lemma nonempty.smul_zero (hs : s.nonempty) : s • (0 : finset β) = 0 := s.smul_zero_subset.antisymm $ by simpa [mem_smul] using hs lemma nonempty.zero_smul (ht : t.nonempty) : (0 : finset α) • t = 0 := t.zero_smul_subset.antisymm $ by simpa [mem_smul] using ht /-- A nonempty set is scaled by zero to the singleton set containing 0. -/ lemma zero_smul_finset {s : finset β} (h : s.nonempty) : (0 : α) • s = (0 : finset β) := coe_injective $ by simpa using @set.zero_smul_set α _ _ _ _ _ h lemma zero_smul_finset_subset (s : finset β) : (0 : α) • s ⊆ 0 := image_subset_iff.2 $ λ x _, mem_zero.2 $ zero_smul α x lemma zero_mem_smul_finset {t : finset β} {a : α} (h : (0 : β) ∈ t) : (0 : β) ∈ a • t := mem_smul_finset.2 ⟨0, h, smul_zero _⟩ variables [no_zero_smul_divisors α β] {a : α} lemma zero_mem_smul_iff : (0 : β) ∈ s • t ↔ (0 : α) ∈ s ∧ t.nonempty ∨ (0 : β) ∈ t ∧ s.nonempty := by { rw [←mem_coe, coe_smul, set.zero_mem_smul_iff], refl } lemma zero_mem_smul_finset_iff (ha : a ≠ 0) : (0 : β) ∈ a • t ↔ (0 : β) ∈ t := by { rw [←mem_coe, coe_smul_finset, set.zero_mem_smul_set_iff ha, mem_coe], apply_instance } end smul_with_zero section monoid variables [monoid α] [add_group β] [distrib_mul_action α β] [decidable_eq β] (a : α) (s : finset α) (t : finset β) @[simp] lemma smul_finset_neg : a • -t = -(a • t) := by simp only [←image_smul, ←image_neg, function.comp, image_image, smul_neg] @[simp] protected lemma smul_neg : s • -t = -(s • t) := by { simp_rw ←image_neg, exact image_image₂_right_comm smul_neg } end monoid section ring variables [ring α] [add_comm_group β] [module α β] [decidable_eq β] {s : finset α} {t : finset β} {a : α} @[simp] lemma neg_smul_finset : -a • t = -(a • t) := by simp only [←image_smul, ←image_neg, image_image, neg_smul] @[simp] protected lemma neg_smul [decidable_eq α] : -s • t = -(s • t) := by { simp_rw ←image_neg, exact image₂_image_left_comm neg_smul } end ring end finset open_locale pointwise namespace set section has_one variables [has_one α] @[simp, to_additive] lemma to_finset_one : (1 : set α).to_finset = 1 := rfl @[simp, to_additive] lemma finite.to_finset_one (h : (1 : set α).finite := finite_one) : h.to_finset = 1 := finite.to_finset_singleton _ end has_one section has_mul variables [decidable_eq α] [has_mul α] {s t : set α} @[simp, to_additive] lemma to_finset_mul (s t : set α) [fintype s] [fintype t] [fintype ↥(s * t)] : (s * t).to_finset = s.to_finset * t.to_finset := to_finset_image2 _ _ _ @[to_additive] lemma finite.to_finset_mul (hs : s.finite) (ht : t.finite) (hf := hs.mul ht) : hf.to_finset = hs.to_finset * ht.to_finset := finite.to_finset_image2 _ _ _ end has_mul section has_smul variables [has_smul α β] [decidable_eq β] {a : α} {s : set α} {t : set β} @[simp, to_additive] lemma to_finset_smul (s : set α) (t : set β) [fintype s] [fintype t] [fintype ↥(s • t)] : (s • t).to_finset = s.to_finset • t.to_finset := to_finset_image2 _ _ _ @[to_additive] lemma finite.to_finset_smul (hs : s.finite) (ht : t.finite) (hf := hs.smul ht) : hf.to_finset = hs.to_finset • ht.to_finset := finite.to_finset_image2 _ _ _ end has_smul section has_smul variables [decidable_eq β] [has_smul α β] {a : α} {s : set β} @[simp, to_additive] lemma to_finset_smul_set (a : α) (s : set β) [fintype s] [fintype ↥(a • s)] : (a • s).to_finset = a • s.to_finset := to_finset_image _ _ @[to_additive] lemma finite.to_finset_smul_set (hs : s.finite) (hf : (a • s).finite := hs.smul_set) : hf.to_finset = a • hs.to_finset := finite.to_finset_image _ _ _ end has_smul section has_vsub variables [decidable_eq α] [has_vsub α β] {s t : set β} include α @[simp] lemma to_finset_vsub (s t : set β) [fintype s] [fintype t] [fintype ↥(s -ᵥ t)] : (s -ᵥ t : set α).to_finset = s.to_finset -ᵥ t.to_finset := to_finset_image2 _ _ _ lemma finite.to_finset_vsub (hs : s.finite) (ht : t.finite) (hf := hs.vsub ht) : hf.to_finset = hs.to_finset -ᵥ ht.to_finset := finite.to_finset_image2 _ _ _ end has_vsub end set
6d9e07f3ffc729e46a9f7c3eb270b32dd371f304
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/init/util.hlean
4d5055af6b94c4017a4f7d6ecd9c213611eb8779
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
519
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Auxiliary definitions used by automation -/ prelude import init.trunc open is_trunc definition eq_rec_eq.{l₁ l₂} {A : Type.{l₁}} {B : A → Type.{l₂}} [h : is_hset A] {a : A} (b : B a) (p : a = a) : b = @eq.rec.{l₂ l₁} A a (λ (a' : A) (h : a = a'), B a') b a p := eq.rec_on (is_hset.elim (eq.refl a) p) (eq.refl (eq.rec_on (eq.refl a) b))
f4d9e26f2972426ae385be8d7f81aba0ae01b06d
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/AssociativeRightRingoid.lean
f168d27673c8d15d610f46721f3e6709e1cdc32b
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
9,974
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section AssociativeRightRingoid structure AssociativeRightRingoid (A : Type) : Type := (times : (A → (A → A))) (associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z)))) (plus : (A → (A → A))) (rightDistributive_times_plus : (∀ {x y z : A} , (times (plus y z) x) = (plus (times y x) (times z x)))) open AssociativeRightRingoid structure Sig (AS : Type) : Type := (timesS : (AS → (AS → AS))) (plusS : (AS → (AS → AS))) structure Product (A : Type) : Type := (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP)))) (rightDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP (plusP yP zP) xP) = (plusP (timesP yP xP) (timesP zP xP)))) structure Hom {A1 : Type} {A2 : Type} (As1 : (AssociativeRightRingoid A1)) (As2 : (AssociativeRightRingoid A2)) : Type := (hom : (A1 → A2)) (pres_times : (∀ {x1 x2 : A1} , (hom ((times As1) x1 x2)) = ((times As2) (hom x1) (hom x2)))) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus As1) x1 x2)) = ((plus As2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (As1 : (AssociativeRightRingoid A1)) (As2 : (AssociativeRightRingoid A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times As1) x1 x2) ((times As2) y1 y2)))))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus As1) x1 x2) ((plus As2) y1 y2)))))) inductive AssociativeRightRingoidTerm : Type | timesL : (AssociativeRightRingoidTerm → (AssociativeRightRingoidTerm → AssociativeRightRingoidTerm)) | plusL : (AssociativeRightRingoidTerm → (AssociativeRightRingoidTerm → AssociativeRightRingoidTerm)) open AssociativeRightRingoidTerm inductive ClAssociativeRightRingoidTerm (A : Type) : Type | sing : (A → ClAssociativeRightRingoidTerm) | timesCl : (ClAssociativeRightRingoidTerm → (ClAssociativeRightRingoidTerm → ClAssociativeRightRingoidTerm)) | plusCl : (ClAssociativeRightRingoidTerm → (ClAssociativeRightRingoidTerm → ClAssociativeRightRingoidTerm)) open ClAssociativeRightRingoidTerm inductive OpAssociativeRightRingoidTerm (n : ℕ) : Type | v : ((fin n) → OpAssociativeRightRingoidTerm) | timesOL : (OpAssociativeRightRingoidTerm → (OpAssociativeRightRingoidTerm → OpAssociativeRightRingoidTerm)) | plusOL : (OpAssociativeRightRingoidTerm → (OpAssociativeRightRingoidTerm → OpAssociativeRightRingoidTerm)) open OpAssociativeRightRingoidTerm inductive OpAssociativeRightRingoidTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpAssociativeRightRingoidTerm2) | sing2 : (A → OpAssociativeRightRingoidTerm2) | timesOL2 : (OpAssociativeRightRingoidTerm2 → (OpAssociativeRightRingoidTerm2 → OpAssociativeRightRingoidTerm2)) | plusOL2 : (OpAssociativeRightRingoidTerm2 → (OpAssociativeRightRingoidTerm2 → OpAssociativeRightRingoidTerm2)) open OpAssociativeRightRingoidTerm2 def simplifyCl {A : Type} : ((ClAssociativeRightRingoidTerm A) → (ClAssociativeRightRingoidTerm A)) | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpAssociativeRightRingoidTerm n) → (OpAssociativeRightRingoidTerm n)) | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpAssociativeRightRingoidTerm2 n A) → (OpAssociativeRightRingoidTerm2 n A)) | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((AssociativeRightRingoid A) → (AssociativeRightRingoidTerm → A)) | As (timesL x1 x2) := ((times As) (evalB As x1) (evalB As x2)) | As (plusL x1 x2) := ((plus As) (evalB As x1) (evalB As x2)) def evalCl {A : Type} : ((AssociativeRightRingoid A) → ((ClAssociativeRightRingoidTerm A) → A)) | As (sing x1) := x1 | As (timesCl x1 x2) := ((times As) (evalCl As x1) (evalCl As x2)) | As (plusCl x1 x2) := ((plus As) (evalCl As x1) (evalCl As x2)) def evalOpB {A : Type} {n : ℕ} : ((AssociativeRightRingoid A) → ((vector A n) → ((OpAssociativeRightRingoidTerm n) → A))) | As vars (v x1) := (nth vars x1) | As vars (timesOL x1 x2) := ((times As) (evalOpB As vars x1) (evalOpB As vars x2)) | As vars (plusOL x1 x2) := ((plus As) (evalOpB As vars x1) (evalOpB As vars x2)) def evalOp {A : Type} {n : ℕ} : ((AssociativeRightRingoid A) → ((vector A n) → ((OpAssociativeRightRingoidTerm2 n A) → A))) | As vars (v2 x1) := (nth vars x1) | As vars (sing2 x1) := x1 | As vars (timesOL2 x1 x2) := ((times As) (evalOp As vars x1) (evalOp As vars x2)) | As vars (plusOL2 x1 x2) := ((plus As) (evalOp As vars x1) (evalOp As vars x2)) def inductionB {P : (AssociativeRightRingoidTerm → Type)} : ((∀ (x1 x2 : AssociativeRightRingoidTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → ((∀ (x1 x2 : AssociativeRightRingoidTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → (∀ (x : AssociativeRightRingoidTerm) , (P x)))) | ptimesl pplusl (timesL x1 x2) := (ptimesl _ _ (inductionB ptimesl pplusl x1) (inductionB ptimesl pplusl x2)) | ptimesl pplusl (plusL x1 x2) := (pplusl _ _ (inductionB ptimesl pplusl x1) (inductionB ptimesl pplusl x2)) def inductionCl {A : Type} {P : ((ClAssociativeRightRingoidTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClAssociativeRightRingoidTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → ((∀ (x1 x2 : (ClAssociativeRightRingoidTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → (∀ (x : (ClAssociativeRightRingoidTerm A)) , (P x))))) | psing ptimescl ppluscl (sing x1) := (psing x1) | psing ptimescl ppluscl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ptimescl ppluscl x1) (inductionCl psing ptimescl ppluscl x2)) | psing ptimescl ppluscl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ptimescl ppluscl x1) (inductionCl psing ptimescl ppluscl x2)) def inductionOpB {n : ℕ} {P : ((OpAssociativeRightRingoidTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpAssociativeRightRingoidTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → ((∀ (x1 x2 : (OpAssociativeRightRingoidTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → (∀ (x : (OpAssociativeRightRingoidTerm n)) , (P x))))) | pv ptimesol pplusol (v x1) := (pv x1) | pv ptimesol pplusol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv ptimesol pplusol x1) (inductionOpB pv ptimesol pplusol x2)) | pv ptimesol pplusol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv ptimesol pplusol x1) (inductionOpB pv ptimesol pplusol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpAssociativeRightRingoidTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpAssociativeRightRingoidTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → ((∀ (x1 x2 : (OpAssociativeRightRingoidTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → (∀ (x : (OpAssociativeRightRingoidTerm2 n A)) , (P x)))))) | pv2 psing2 ptimesol2 pplusol2 (v2 x1) := (pv2 x1) | pv2 psing2 ptimesol2 pplusol2 (sing2 x1) := (psing2 x1) | pv2 psing2 ptimesol2 pplusol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 x2)) | pv2 psing2 ptimesol2 pplusol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 x2)) def stageB : (AssociativeRightRingoidTerm → (Staged AssociativeRightRingoidTerm)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClAssociativeRightRingoidTerm A) → (Staged (ClAssociativeRightRingoidTerm A))) | (sing x1) := (Now (sing x1)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpAssociativeRightRingoidTerm n) → (Staged (OpAssociativeRightRingoidTerm n))) | (v x1) := (const (code (v x1))) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpAssociativeRightRingoidTerm2 n A) → (Staged (OpAssociativeRightRingoidTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (timesT : ((Repr A) → ((Repr A) → (Repr A)))) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) end AssociativeRightRingoid
6c4b8b6e48cd005232f3e284b3d69a0c0b4e0ea6
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_theory/unnamed_1641.lean
5c8744ad1b047873fba4e61df8ab142f8749aeb5
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
371
lean
import logic.basic variables p q : Prop -- BEGIN example (k : ¬(p ∨ q)) : ¬q ∧ ¬p := begin rw not_or_distrib at k, -- Rewriting using De Morgan's law at `k`, we have `k : ¬p ∧ ¬q`. rw and_comm, -- Rewriting using commutativity of conjunction, the goal is `¬p ∧ ¬q`. exact k, -- This holds by reiteration on `k`. end -- END
a5e437f22da94d8c94268b7ff741ef067fdb0b0e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/metric_space/partition_of_unity.lean
5fa49bc4e0c8a23783116d84ac2c166b5bb68d86
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
8,199
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.emetric_paracompact import analysis.convex.partition_of_unity /-! # Lemmas about (e)metric spaces that need partition of unity The main lemma in this file (see `metric.exists_continuous_real_forall_closed_ball_subset`) says the following. Let `X` be a metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, → ℝ)` such that for any `i` and `x ∈ K i`, we have `metric.closed_ball x (δ x) ⊆ U i`. We also formulate versions of this lemma for extended metric spaces and for different codomains (`ℝ`, `ℝ≥0`, and `ℝ≥0∞`). We also prove a few auxiliary lemmas to be used later in a proof of the smooth version of this lemma. ## Tags metric space, partition of unity, locally finite -/ open_locale topological_space ennreal big_operators nnreal filter open set function filter topological_space variables {ι X : Type*} namespace emetric variables [emetric_space X] {K : ι → set X} {U : ι → set X} /-- Let `K : ι → set X` be a locally finitie family of closed sets in an emetric space. Let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then for any point `x : X`, for sufficiently small `r : ℝ≥0∞` and for `y` sufficiently close to `x`, for all `i`, if `y ∈ K i`, then `emetric.closed_ball y r ⊆ U i`. -/ lemma eventually_nhds_zero_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) (x : X) : ∀ᶠ p : ℝ≥0∞ × X in 𝓝 0 ×ᶠ 𝓝 x, ∀ i, p.2 ∈ K i → closed_ball p.2 p.1 ⊆ U i := begin suffices : ∀ i, x ∈ K i → ∀ᶠ p : ℝ≥0∞ × X in 𝓝 0 ×ᶠ 𝓝 x, closed_ball p.2 p.1 ⊆ U i, { filter_upwards [tendsto_snd (hfin.Inter_compl_mem_nhds hK x), (eventually_all_finite (hfin.point_finite x)).2 this], rintro ⟨r, y⟩ hxy hyU i hi, simp only [mem_Inter₂, mem_compl_iff, not_imp_not, mem_preimage] at hxy, exact hyU _ (hxy _ hi) }, intros i hi, rcases nhds_basis_closed_eball.mem_iff.1 ((hU i).mem_nhds $ hKU i hi) with ⟨R, hR₀, hR⟩, rcases ennreal.lt_iff_exists_nnreal_btwn.mp hR₀ with ⟨r, hr₀, hrR⟩, filter_upwards [prod_mem_prod (eventually_lt_nhds hr₀) (closed_ball_mem_nhds x (tsub_pos_iff_lt.2 hrR))] with p hp z hz, apply hR, calc edist z x ≤ edist z p.2 + edist p.2 x : edist_triangle _ _ _ ... ≤ p.1 + (R - p.1) : add_le_add hz $ le_trans hp.2 $ tsub_le_tsub_left hp.1.out.le _ ... = R : add_tsub_cancel_of_le (lt_trans hp.1 hrR).le, end lemma exists_forall_closed_ball_subset_aux₁ (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) (x : X) : ∃ r : ℝ, ∀ᶠ y in 𝓝 x, r ∈ Ioi (0 : ℝ) ∩ ennreal.of_real ⁻¹' ⋂ i (hi : y ∈ K i), {r | closed_ball y r ⊆ U i} := begin have := (ennreal.continuous_of_real.tendsto' 0 0 ennreal.of_real_zero).eventually (eventually_nhds_zero_forall_closed_ball_subset hK hU hKU hfin x).curry, rcases this.exists_gt with ⟨r, hr0, hr⟩, refine ⟨r, hr.mono (λ y hy, ⟨hr0, _⟩)⟩, rwa [mem_preimage, mem_Inter₂] end lemma exists_forall_closed_ball_subset_aux₂ (y : X) : convex ℝ (Ioi (0 : ℝ) ∩ ennreal.of_real ⁻¹' ⋂ i (hi : y ∈ K i), {r | closed_ball y r ⊆ U i}) := (convex_Ioi _).inter $ ord_connected.convex $ ord_connected.preimage_ennreal_of_real $ ord_connected_Inter $ λ i, ord_connected_Inter $ λ hi, ord_connected_set_of_closed_ball_subset y (U i) /-- Let `X` be an extended metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, ℝ)` such that for any `i` and `x ∈ K i`, we have `emetric.closed_ball x (ennreal.of_real (δ x)) ⊆ U i`. -/ lemma exists_continuous_real_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) : ∃ δ : C(X, ℝ), (∀ x, 0 < δ x) ∧ ∀ i (x ∈ K i), closed_ball x (ennreal.of_real $ δ x) ⊆ U i := by simpa only [mem_inter_eq, forall_and_distrib, mem_preimage, mem_Inter, @forall_swap ι X] using exists_continuous_forall_mem_convex_of_local_const exists_forall_closed_ball_subset_aux₂ (exists_forall_closed_ball_subset_aux₁ hK hU hKU hfin) /-- Let `X` be an extended metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, ℝ≥0)` such that for any `i` and `x ∈ K i`, we have `emetric.closed_ball x (δ x) ⊆ U i`. -/ lemma exists_continuous_nnreal_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) : ∃ δ : C(X, ℝ≥0), (∀ x, 0 < δ x) ∧ ∀ i (x ∈ K i), closed_ball x (δ x) ⊆ U i := begin rcases exists_continuous_real_forall_closed_ball_subset hK hU hKU hfin with ⟨δ, hδ₀, hδ⟩, lift δ to C(X, ℝ≥0) using λ x, (hδ₀ x).le, refine ⟨δ, hδ₀, λ i x hi, _⟩, simpa only [← ennreal.of_real_coe_nnreal] using hδ i x hi end /-- Let `X` be an extended metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, ℝ≥0∞)` such that for any `i` and `x ∈ K i`, we have `emetric.closed_ball x (δ x) ⊆ U i`. -/ lemma exists_continuous_ennreal_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) : ∃ δ : C(X, ℝ≥0∞), (∀ x, 0 < δ x) ∧ ∀ i (x ∈ K i), closed_ball x (δ x) ⊆ U i := let ⟨δ, hδ₀, hδ⟩ := exists_continuous_nnreal_forall_closed_ball_subset hK hU hKU hfin in ⟨continuous_map.comp ⟨coe, ennreal.continuous_coe⟩ δ, λ x, ennreal.coe_pos.2 (hδ₀ x), hδ⟩ end emetric namespace metric variables [metric_space X] {K : ι → set X} {U : ι → set X} /-- Let `X` be a metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, ℝ≥0)` such that for any `i` and `x ∈ K i`, we have `metric.closed_ball x (δ x) ⊆ U i`. -/ lemma exists_continuous_nnreal_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) : ∃ δ : C(X, ℝ≥0), (∀ x, 0 < δ x) ∧ ∀ i (x ∈ K i), closed_ball x (δ x) ⊆ U i := begin rcases emetric.exists_continuous_nnreal_forall_closed_ball_subset hK hU hKU hfin with ⟨δ, hδ0, hδ⟩, refine ⟨δ, hδ0, λ i x hx, _⟩, rw [← emetric_closed_ball_nnreal], exact hδ i x hx end /-- Let `X` be a metric space. Let `K : ι → set X` be a locally finite family of closed sets, let `U : ι → set X` be a family of open sets such that `K i ⊆ U i` for all `i`. Then there exists a positive continuous function `δ : C(X, ℝ)` such that for any `i` and `x ∈ K i`, we have `metric.closed_ball x (δ x) ⊆ U i`. -/ lemma exists_continuous_real_forall_closed_ball_subset (hK : ∀ i, is_closed (K i)) (hU : ∀ i, is_open (U i)) (hKU : ∀ i, K i ⊆ U i) (hfin : locally_finite K) : ∃ δ : C(X, ℝ), (∀ x, 0 < δ x) ∧ ∀ i (x ∈ K i), closed_ball x (δ x) ⊆ U i := let ⟨δ, hδ₀, hδ⟩ := exists_continuous_nnreal_forall_closed_ball_subset hK hU hKU hfin in ⟨continuous_map.comp ⟨coe, nnreal.continuous_coe⟩ δ, hδ₀, hδ⟩ end metric
cb1a648f0b1983006fc5b73c7f38237f488f34d5
5382d69a781e8d7e4f53e2358896eb7649c9b298
/lob.lean
4700c29b8a3588af24a852db2603d635c340ed14
[]
no_license
evhub/lean-math-examples
c30249747a21fba3bc8793eba4928db47cf28768
dec44bf581a1e9d5bf0b5261803a43fe8fd350e1
refs/heads/master
1,624,170,837,738
1,623,889,725,000
1,623,889,725,000
148,759,369
3
0
null
null
null
null
UTF-8
Lean
false
false
3,376
lean
import .lawvere namespace lob open function open util lawvere -- diagonal lemma: def S0: Type := Prop def S1: Type := S0 → S0 def up (ψ: S0): S1 := const S0 ψ theorem diag {f: S1 → S1 → S0} (sur_f: surjective f): ∀ φ: S1, ∃ ψ: S0, ψ = f φ (up ψ) := assume φ: S1, let h: S0 → S0 := f φ ∘ up in simple_lawvere.{0 0} sur_f h -- lob axioms: constant Bew: S0 → S0 constant godnum: S1 → S0 def box: S0 → S0 := Bew ∘ godnum ∘ up def proves: S0 → Prop := box namespace hilbert_bernay axiom a: ∀ {φ: S0}, proves φ → proves (box φ) axiom b: ∀ {φ: S0}, proves (box φ → box (box φ)) axiom c: ∀ {φ ψ: S0}, proves (box (φ → ψ) → box φ → box ψ) end hilbert_bernay def f (φ: S1) (ψ: S1): S0 := φ (godnum ψ) @[instance] axiom f.sur: surjective f axiom proves.mp: ∀ {φ ψ: S0}, proves (φ → ψ) → proves φ → proves ψ axiom proves.implies_trans: ∀ {a b c: S0}, proves (a → b) → proves (b → c) → proves (a → c) axiom proves.implies_middleman_elim: ∀ {a b c: S0}, proves (a → b → c) → proves (a → b) → proves (a → c) axiom proves.diag: ∀ φ: S1, ∃ ψ: S0, proves (ψ ↔ f φ (up ψ)) axiom proves.iff_mp: ∀ {a b: S0}, proves (a ↔ b) → proves (a → b) axiom proves.iff_mpr: ∀ {a b: S0}, proves (a ↔ b) → proves (b → a) -- lob's theorem: def h (ψ: S0) (x: S0): S0 := Bew x → ψ @[simp] theorem f_of_h: ∀ {ψ φ: S0}, f (h ψ) (up φ) = (box φ → ψ) := assume ψ φ: S0, rfl theorem lob {ψ: S0} (h0: proves (box ψ → ψ)): proves ψ := exists.elim (proves.diag (h ψ)) ( assume φ: S0, assume heq: proves (φ ↔ f (h ψ) (up φ)), have h1: proves (φ ↔ (box φ → ψ)), by {simp at heq, exact heq}, have h1_forward: proves (φ → (box φ → ψ)), from h1.iff_mp, have h1_reverse: proves ((box φ → ψ) → φ), from h1.iff_mpr, have h2: proves (box (φ → (box φ → ψ))), from hilbert_bernay.a h1_forward, have h3: proves (box φ → box (box φ → ψ)), from hilbert_bernay.c.mp h2, have h4: proves (box φ → box (box φ) → box ψ), from h3.implies_trans hilbert_bernay.c, have h5: proves (box φ → box (box φ)), from hilbert_bernay.b, have h6: proves (box φ → box ψ), from h4.implies_middleman_elim h5, have h7: proves (box φ → ψ), from h6.implies_trans h0, have h8: proves φ, from h1_reverse.mp h7, have h9: proves (box φ), from hilbert_bernay.a h8, show proves ψ, from h7.mp h9 ) -- godel's second incompleteness theorem: theorem godel: proves (¬box false) → proves false := lob end lob
259b68483fcb2cdb0d3204508f625319ee278f30
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/invertible.lean
1440b365ceb4634e5e43844693bfdea1d7fee47f
[ "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
9,600
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen A typeclass for the two-sided multiplicative inverse. -/ import algebra.char_zero import algebra.char_p.basic /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. This file also includes some instances of `invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertible_of_nonzero` is a useful definition. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- An `invertible` element is a unit. -/ def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } @[simp] lemma unit_of_invertible_val [monoid α] (a : α) [invertible a] : (unit_of_invertible a : α) = a := rfl @[simp] lemma unit_of_invertible_inv [monoid α] (a : α) [invertible a] : (↑(unit_of_invertible a)⁻¹ : α) = ⅟a := rfl lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨ 1, mul_one _, one_mul _ ⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm instance invertible_pow {M : Type*} [monoid M] (m : M) [invertible m] (n : ℕ) : invertible (m ^ n) := { inv_of := ⅟ m ^ n, inv_of_mul_self := by rw [← (commute_inv_of m).symm.mul_pow, inv_of_mul_self, one_pow], mul_inv_of_self := by rw [← (commute_inv_of m).mul_pow, mul_inv_of_self, one_pow] } section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] } section ring_char /-- A natural number `t` is invertible in a field `K` if the charactistic of `K` does not divide `t`. -/ def invertible_of_ring_char_not_dvd {K : Type*} [field K] {t : ℕ} (not_dvd : ¬(ring_char K ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((ring_char.spec K t).mp h)) end ring_char section char_p /-- A natural number `t` is invertible in a field `K` of charactistic `p` if `p` does not divide `t`. -/ def invertible_of_char_p_not_dvd {K : Type*} [field K] {p : ℕ} [char_p K p] {t : ℕ} (not_dvd : ¬(p ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((char_p.cast_eq_zero_iff K p t).mp h)) instance invertible_of_pos {K : Type*} [field K] [char_zero K] (n : ℕ) [h : fact (0 < n)] : invertible (n : K) := invertible_of_nonzero $ by simpa [pos_iff_ne_zero] using h end char_p section division_ring variable [division_ring α] instance invertible_succ [char_zero α] (n : ℕ) : invertible (n.succ : α) := invertible_of_nonzero (nat.cast_ne_zero.mpr (nat.succ_ne_zero _)) /-! A few `invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertible_two [char_zero α] : invertible (2 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 2 ≠ 0)) instance invertible_three [char_zero α] : invertible (3 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 3 ≠ 0)) end division_ring
ef611a5ca342465a22f2ce74ad0aba3fb5b3c740
f57749ca63d6416f807b770f67559503fdb21001
/hott/types/prod.hlean
b28a3b6aaa3410c92c14af5fa3988be247eccf5c
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,358
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about products -/ open eq equiv is_equiv is_trunc prod prod.ops unit variables {A A' B B' C D : Type} {a a' a'' : A} {b b₁ b₂ b' b'' : B} {u v w : A × B} namespace prod protected definition eta (u : A × B) : (pr₁ u, pr₂ u) = u := by cases u; apply idp definition pair_eq (pa : a = a') (pb : b = b') : (a, b) = (a', b') := by cases pa; cases pb; apply idp definition prod_eq (H₁ : pr₁ u = pr₁ v) (H₂ : pr₂ u = pr₂ v) : u = v := by cases u; cases v; exact pair_eq H₁ H₂ /- Symmetry -/ definition is_equiv_flip [instance] (A B : Type) : is_equiv (@flip A B) := adjointify flip flip (λu, destruct u (λb a, idp)) (λu, destruct u (λa b, idp)) definition prod_comm_equiv (A B : Type) : A × B ≃ B × A := equiv.mk flip _ definition prod_contr_equiv (A B : Type) [H : is_contr B] : A × B ≃ A := equiv.MK pr1 (λx, (x, !center)) (λx, idp) (λx, by cases x with a b; exact pair_eq idp !center_eq) definition prod_unit_equiv (A : Type) : A × unit ≃ A := !prod_contr_equiv -- is_trunc_prod is defined in sigma end prod
26ee1b6eb5a96e4bd3c0c2fcc84e6ff881e52299
130c49f47783503e462c16b2eff31933442be6ff
/stage0/src/Lean/Meta/Match/Match.lean
86c4418f43ea2cf775a01c2de239928841826f93
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,018
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.CollectLevelParams import Lean.Util.Recognizers import Lean.Compiler.ExternAttr import Lean.Meta.Check import Lean.Meta.Closure import Lean.Meta.Tactic.Cases import Lean.Meta.Tactic.Contradiction import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match.Basic import Lean.Meta.Match.MVarRenaming import Lean.Meta.Match.CaseValues namespace Lean.Meta.Match /- The number of patterns in each AltLHS must be equal to majors.length -/ private def checkNumPatterns (majors : Array Expr) (lhss : List AltLHS) : MetaM Unit := do let num := majors.size if lhss.any fun lhs => lhs.patterns.length != num then throwError "incorrect number of patterns" /- Given a list of `AltLHS`, create a minor premise for each one, convert them into `Alt`, and then execute `k` -/ private def withAlts {α} (motive : Expr) (lhss : List AltLHS) (k : List Alt → Array (Expr × Nat) → MetaM α) : MetaM α := loop lhss [] #[] where mkMinorType (xs : Array Expr) (lhs : AltLHS) : MetaM Expr := withExistingLocalDecls lhs.fvarDecls do let args ← lhs.patterns.toArray.mapM (Pattern.toExpr · (annotate := true)) let minorType := mkAppN motive args mkForallFVars xs minorType loop (lhss : List AltLHS) (alts : List Alt) (minors : Array (Expr × Nat)) : MetaM α := do match lhss with | [] => k alts.reverse minors | lhs::lhss => let xs := lhs.fvarDecls.toArray.map LocalDecl.toExpr let minorType ← mkMinorType xs lhs let (minorType, minorNumParams) := if !xs.isEmpty then (minorType, xs.size) else (mkSimpleThunkType minorType, 1) let idx := alts.length let minorName := (`h).appendIndexAfter (idx+1) trace[Meta.Match.debug] "minor premise {minorName} : {minorType}" withLocalDeclD minorName minorType fun minor => do let rhs := if xs.isEmpty then mkApp minor (mkConst `Unit.unit) else mkAppN minor xs let minors := minors.push (minor, minorNumParams) let fvarDecls ← lhs.fvarDecls.mapM instantiateLocalDeclMVars let alts := { ref := lhs.ref, idx := idx, rhs := rhs, fvarDecls := fvarDecls, patterns := lhs.patterns : Alt } :: alts loop lhss alts minors def assignGoalOf (p : Problem) (e : Expr) : MetaM Unit := withGoalOf p (assignExprMVar p.mvarId e) structure State where used : Std.HashSet Nat := {} -- used alternatives counterExamples : List (List Example) := [] /-- Return true if the given (sub-)problem has been solved. -/ private def isDone (p : Problem) : Bool := p.vars.isEmpty /-- Return true if the next element on the `p.vars` list is a variable. -/ private def isNextVar (p : Problem) : Bool := match p.vars with | Expr.fvar _ _ :: _ => true | _ => false private def hasAsPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.as _ _ :: _ => true | _ => false private def hasCtorPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | _ => false private def hasValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val _ :: _ => true | _ => false private def hasNatValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val v :: _ => v.isNatLit | _ => false private def hasVarPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def hasArrayLitPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | _ => false private def isVariableTransition (p : Problem) : Bool := p.alts.all fun alt => match alt.patterns with | Pattern.inaccessible _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isConstructorTransition (p : Problem) : Bool := (hasCtorPattern p || p.alts.isEmpty) && p.alts.all fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false private def isValueTransition (p : Problem) : Bool := hasVarPattern p && hasValPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.val _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isArrayLitTransition (p : Problem) : Bool := hasArrayLitPattern p && hasVarPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isNatValueTransition (p : Problem) : Bool := hasNatValPattern p && (!isNextVar p || p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false) private def processSkipInaccessible (p : Problem) : Problem := match p.vars with | [] => unreachable! | x :: xs => do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => { alt with patterns := ps } | _ => unreachable! { p with alts := alts, vars := xs } private def processLeaf (p : Problem) : StateRefT State MetaM Unit := match p.alts with | [] => do trace[Meta.Match.match] "missing alternative" admit p.mvarId modify fun s => { s with counterExamples := p.examples :: s.counterExamples } | alt :: _ => do -- TODO: check whether we have unassigned metavars in rhs liftM $ assignGoalOf p alt.rhs modify fun s => { s with used := s.used.insert alt.idx } private def processAsPattern (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.as fvarId p :: ps => /- We used to use `checkAndReplaceFVarId` here, but `x` and `fvarId` may have different types when dependent types are beind used. Let's consider the repro for issue #471 ``` inductive vec : Nat → Type | nil : vec 0 | cons : Int → vec n → vec n.succ def vec_len : vec n → Nat | vec.nil => 0 | x@(vec.cons h t) => vec_len t + 1 ``` we reach the state ``` [Meta.Match.match] remaining variables: [x✝:(vec n✝)] alternatives: [n:(Nat), x:(vec (Nat.succ n)), h:(Int), t:(vec n)] |- [x@(vec.cons n h t)] => h_1 n x h t [x✝:(vec n✝)] |- [x✝] => h_2 n✝ x✝ ``` The variables `x✝:(vec n✝)` and `x:(vec (Nat.succ n))` have different types, but we perform the substitution anyway, because we claim the "discrepancy" will be corrected after we process the pattern `(vec.cons n h t)`. The right-hand-side is temporarily type incorrect, but we claim this is fine because it will be type correct again after we the pattern `(vec.cons n h t)`. TODO: try to find a cleaner solution. -/ { alt with patterns := p :: ps }.replaceFVarId fvarId x | _ => pure alt pure { p with alts := alts } private def processVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => pure { alt with patterns := ps } | Pattern.var fvarId :: ps => { alt with patterns := ps }.checkAndReplaceFVarId fvarId x | _ => unreachable! pure { p with alts := alts, vars := xs } private def throwInductiveTypeExpected {α} (e : Expr) : MetaM α := do let t ← inferType e throwError "failed to compile pattern matching, inductive type expected{indentExpr e}\nhas type{indentExpr t}" private def inLocalDecls (localDecls : List LocalDecl) (fvarId : FVarId) : Bool := localDecls.any fun d => d.fvarId == fvarId namespace Unify structure Context where altFVarDecls : List LocalDecl structure State where fvarSubst : FVarSubst := {} abbrev M := ReaderT Context $ StateRefT State MetaM def isAltVar (fvarId : FVarId) : M Bool := do return inLocalDecls (← read).altFVarDecls fvarId def expandIfVar (e : Expr) : M Expr := do match e with | Expr.fvar _ _ => return (← get).fvarSubst.apply e | _ => return e def occurs (fvarId : FVarId) (v : Expr) : Bool := Option.isSome $ v.find? fun e => match e with | Expr.fvar fvarId' _ => fvarId == fvarId' | _=> false def assign (fvarId : FVarId) (v : Expr) : M Bool := do if occurs fvarId v then trace[Meta.Match.unify] "assign occurs check failed, {mkFVar fvarId} := {v}" pure false else let ctx ← read if (← isAltVar fvarId) then trace[Meta.Match.unify] "{mkFVar fvarId} := {v}" modify fun s => { s with fvarSubst := s.fvarSubst.insert fvarId v } pure true else trace[Meta.Match.unify] "assign failed variable is not local, {mkFVar fvarId} := {v}" pure false partial def unify (a : Expr) (b : Expr) : M Bool := do trace[Meta.Match.unify] "{a} =?= {b}" if (← isDefEq a b) then pure true else let a' ← whnfD (← expandIfVar a) let b' ← whnfD (← expandIfVar b) if a != a' || b != b' then unify a' b' else match a, b with | Expr.fvar aFvarId _, Expr.fvar bFVarId _ => assign aFvarId b <||> assign bFVarId a | Expr.fvar aFvarId _, b => assign aFvarId b | a, Expr.fvar bFVarId _ => assign bFVarId a | Expr.app aFn aArg _, Expr.app bFn bArg _ => unify aFn bFn <&&> unify aArg bArg | _, _ => pure false end Unify private def unify? (altFVarDecls : List LocalDecl) (a b : Expr) : MetaM (Option FVarSubst) := do let a ← instantiateMVars a let b ← instantiateMVars b let (b, s) ← Unify.unify a b { altFVarDecls := altFVarDecls} |>.run {} if b then pure s.fvarSubst else trace[Meta.Match.unify] "failed to unify {a} =?= {b}" pure none private def expandVarIntoCtor? (alt : Alt) (fvarId : FVarId) (ctorName : Name) : MetaM (Option Alt) := withExistingLocalDecls alt.fvarDecls do let env ← getEnv let ldecl ← getLocalDecl fvarId let expectedType ← inferType (mkFVar fvarId) let expectedType ← whnfD expectedType let (ctorLevels, ctorParams) ← getInductiveUniverseAndParams expectedType let ctor := mkAppN (mkConst ctorName ctorLevels) ctorParams let ctorType ← inferType ctor forallTelescopeReducing ctorType fun ctorFields resultType => do let ctor := mkAppN ctor ctorFields let alt := alt.replaceFVarId fvarId ctor let ctorFieldDecls ← ctorFields.mapM fun ctorField => getLocalDecl ctorField.fvarId! let newAltDecls := ctorFieldDecls.toList ++ alt.fvarDecls let subst? ← unify? newAltDecls resultType expectedType match subst? with | none => pure none | some subst => let newAltDecls := newAltDecls.filter fun d => !subst.contains d.fvarId -- remove declarations that were assigned let newAltDecls := newAltDecls.map fun d => d.applyFVarSubst subst -- apply substitution to remaining declaration types let patterns := alt.patterns.map fun p => p.applyFVarSubst subst let rhs := subst.apply alt.rhs let ctorFieldPatterns := ctorFields.toList.map fun ctorField => match subst.get ctorField.fvarId! with | e@(Expr.fvar fvarId _) => if inLocalDecls newAltDecls fvarId then Pattern.var fvarId else Pattern.inaccessible e | e => Pattern.inaccessible e pure $ some { alt with fvarDecls := newAltDecls, rhs := rhs, patterns := ctorFieldPatterns ++ patterns } private def getInductiveVal? (x : Expr) : MetaM (Option InductiveVal) := do let xType ← inferType x let xType ← whnfD xType match xType.getAppFn with | Expr.const constName _ _ => let cinfo ← getConstInfo constName match cinfo with | ConstantInfo.inductInfo val => pure (some val) | _ => pure none | _ => pure none private def hasRecursiveType (x : Expr) : MetaM Bool := do match (← getInductiveVal? x) with | some val => pure val.isRec | _ => pure false /- Given `alt` s.t. the next pattern is an inaccessible pattern `e`, try to normalize `e` into a constructor application. If it is not a constructor, throw an error. Otherwise, if it is a constructor application of `ctorName`, update the next patterns with the fields of the constructor. Otherwise, return none. -/ def processInaccessibleAsCtor (alt : Alt) (ctorName : Name) : MetaM (Option Alt) := do let env ← getEnv match alt.patterns with | p@(Pattern.inaccessible e) :: ps => trace[Meta.Match.match] "inaccessible in ctor step {e}" withExistingLocalDecls alt.fvarDecls do -- Try to push inaccessible annotations. let e ← whnfD e match e.constructorApp? env with | some (ctorVal, ctorArgs) => if ctorVal.name == ctorName then let fields := ctorArgs.extract ctorVal.numParams ctorArgs.size let fields := fields.toList.map Pattern.inaccessible pure $ some { alt with patterns := fields ++ ps } else pure none | _ => throwErrorAt alt.ref "dependent match elimination failed, inaccessible pattern found{indentD p.toMessageData}\nconstructor expected" | _ => unreachable! private def hasNonTrivialExample (p : Problem) : Bool := p.examples.any fun | Example.underscore => false | _ => true private def throwCasesException (p : Problem) (ex : Exception) : MetaM α := do match ex with | Exception.error ref msg => let exampleMsg := if hasNonTrivialExample p then m!" after processing{indentD <| examplesToMessageData p.examples}" else "" throw <| Exception.error ref <| m!"{msg}{exampleMsg}\n" ++ "the dependent pattern matcher can solve the following kinds of equations\n" ++ "- <var> = <term> and <term> = <var>\n" ++ "- <term> = <term> where the terms are definitionally equal\n" ++ "- <constructor> = <constructor>, examples: List.cons x xs = List.cons y ys, and List.cons x xs = List.nil" | _ => throw ex private def processConstructor (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "constructor step" let env ← getEnv match p.vars with | [] => unreachable! | x :: xs => do let subgoals? ← commitWhenSome? do let subgoals ← try cases p.mvarId x.fvarId! catch ex => if p.alts.isEmpty then /- If we have no alternatives and dependent pattern matching fails, then a "missing cases" error is bettern than a "stuck" error message. -/ return none else throwCasesException p ex if subgoals.isEmpty then /- Easy case: we have solved problem `p` since there are no subgoals -/ pure (some #[]) else if !p.alts.isEmpty then pure (some subgoals) else do let isRec ← withGoalOf p $ hasRecursiveType x /- If there are no alternatives and the type of the current variable is recursive, we do NOT consider a constructor-transition to avoid nontermination. TODO: implement a more general approach if this is not sufficient in practice -/ if isRec then pure none else pure (some subgoals) match subgoals? with | none => pure #[{ p with vars := xs }] | some subgoals => subgoals.mapM fun subgoal => withMVarContext subgoal.mvarId do let subst := subgoal.subst let fields := subgoal.fields.toList let newVars := fields ++ xs let newVars := newVars.map fun x => x.applyFVarSubst subst let subex := Example.ctor subgoal.ctorName $ fields.map fun field => match field with | Expr.fvar fvarId _ => Example.var fvarId | _ => Example.underscore -- This case can happen due to dependent elimination let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex let examples := examples.map $ Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.ctor n _ _ _ :: _ => n == subgoal.ctorName | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts ← newAlts.filterMapM fun alt => match alt.patterns with | Pattern.ctor _ _ _ fields :: ps => pure $ some { alt with patterns := fields ++ ps } | Pattern.var fvarId :: ps => expandVarIntoCtor? { alt with patterns := ps } fvarId subgoal.ctorName | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt subgoal.ctorName | _ => unreachable! pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } private def processNonVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let x ← whnfD x let env ← getEnv match x.constructorApp? env with | some (ctorVal, xArgs) => let alts ← p.alts.filterMapM fun alt => match alt.patterns with | Pattern.ctor n _ _ fields :: ps => if n != ctorVal.name then pure none else pure $ some { alt with patterns := fields ++ ps } | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt ctorVal.name | p :: _ => throwError "failed to compile pattern matching, inaccessible pattern or constructor expected{indentD p.toMessageData}" | _ => unreachable! let xFields := xArgs.extract ctorVal.numParams xArgs.size pure { p with alts := alts, vars := xFields.toList ++ xs } | none => let alts ← p.alts.filterMapM fun alt => match alt.patterns with | Pattern.inaccessible e :: ps => do if (← isDefEq x e) then pure $ some { alt with patterns := ps } else pure none | p :: _ => throwError "failed to compile pattern matching, unexpected pattern{indentD p.toMessageData}\ndiscriminant{indentExpr x}" | _ => unreachable! pure { p with alts := alts, vars := xs } private def collectValues (p : Problem) : Array Expr := p.alts.foldl (init := #[]) fun values alt => match alt.patterns with | Pattern.val v :: _ => if values.contains v then values else values.push v | _ => values private def isFirstPatternVar (alt : Alt) : Bool := match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def processValue (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "value step" match p.vars with | [] => unreachable! | x :: xs => do let values := collectValues p let subgoals ← caseValues p.mvarId x.fvarId! values subgoals.mapIdxM fun i subgoal => do if h : i.val < values.size then let value := values.get ⟨i, h⟩ -- (x = value) branch let subst := subgoal.subst let examples := p.examples.map $ Example.replaceFVarId x.fvarId! (Example.val value) let examples := examples.map $ Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.val v :: _ => v == value | Pattern.var _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts := newAlts.map fun alt => match alt.patterns with | Pattern.val _ :: ps => { alt with patterns := ps } | Pattern.var fvarId :: ps => let alt := { alt with patterns := ps } alt.replaceFVarId fvarId value | _ => unreachable! let newVars := xs.map fun x => x.applyFVarSubst subst pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else -- else branch for value let newAlts := p.alts.filter isFirstPatternVar pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def collectArraySizes (p : Problem) : Array Nat := p.alts.foldl (init := #[]) fun sizes alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => let sz := ps.length; if sizes.contains sz then sizes else sizes.push sz | _ => sizes private def expandVarIntoArrayLit (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (arraySize : Nat) : MetaM Alt := withExistingLocalDecls alt.fvarDecls do let fvarDecl ← getLocalDecl fvarId let varNamePrefix := fvarDecl.userName let rec loop | n+1, newVars => withLocalDeclD (varNamePrefix.appendIndexAfter (n+1)) arrayElemType fun x => loop n (newVars.push x) | 0, newVars => do let arrayLit ← mkArrayLit arrayElemType newVars.toList let alt := alt.replaceFVarId fvarId arrayLit let newDecls ← newVars.toList.mapM fun newVar => getLocalDecl newVar.fvarId! let newPatterns := newVars.toList.map fun newVar => Pattern.var newVar.fvarId! pure { alt with fvarDecls := newDecls ++ alt.fvarDecls, patterns := newPatterns ++ alt.patterns } loop arraySize #[] private def processArrayLit (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "array literal step" match p.vars with | [] => unreachable! | x :: xs => do let sizes := collectArraySizes p let subgoals ← caseArraySizes p.mvarId x.fvarId! sizes subgoals.mapIdxM fun i subgoal => do if h : i.val < sizes.size then let size := sizes.get! i let subst := subgoal.subst let elems := subgoal.elems.toList let newVars := elems.map mkFVar ++ xs let newVars := newVars.map fun x => x.applyFVarSubst subst let subex := Example.arrayLit <| elems.map Example.var let examples := p.examples.map <| Example.replaceFVarId x.fvarId! subex let examples := examples.map <| Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => ps.length == size | Pattern.var _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts ← newAlts.mapM fun alt => match alt.patterns with | Pattern.arrayLit _ pats :: ps => pure { alt with patterns := pats ++ ps } | Pattern.var fvarId :: ps => do let α ← getArrayArgType <| subst.apply x expandVarIntoArrayLit { alt with patterns := ps } fvarId α size | _ => unreachable! pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else do -- else branch let newAlts := p.alts.filter isFirstPatternVar pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def expandNatValuePattern (p : Problem) : Problem := do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.val (Expr.lit (Literal.natVal 0) _) :: ps => { alt with patterns := Pattern.ctor `Nat.zero [] [] [] :: ps } | Pattern.val (Expr.lit (Literal.natVal (n+1)) _) :: ps => { alt with patterns := Pattern.ctor `Nat.succ [] [] [Pattern.val (mkRawNatLit n)] :: ps } | _ => alt { p with alts := alts } private def traceStep (msg : String) : StateRefT State MetaM Unit := do trace[Meta.Match.match] "{msg} step" private def traceState (p : Problem) : MetaM Unit := withGoalOf p (traceM `Meta.Match.match p.toMessageData) private def throwNonSupported (p : Problem) : MetaM Unit := withGoalOf p do let msg ← p.toMessageData throwError "failed to compile pattern matching, stuck at{indentD msg}" def isCurrVarInductive (p : Problem) : MetaM Bool := do match p.vars with | [] => pure false | x::_ => withGoalOf p do let val? ← getInductiveVal? x pure val?.isSome private def checkNextPatternTypes (p : Problem) : MetaM Unit := do match p.vars with | [] => return () | x::_ => withGoalOf p do for alt in p.alts do withRef alt.ref do match alt.patterns with | [] => pure () | p::_ => let e ← p.toExpr let xType ← inferType x let eType ← inferType e unless (← isDefEq xType eType) do throwError "pattern{indentExpr e}\n{← mkHasTypeButIsExpectedMsg eType xType}" private def List.moveToFront [Inhabited α] (as : List α) (i : Nat) : List α := let rec loop : (as : List α) → (i : Nat) → α × List α | [], _ => unreachable! | a::as, 0 => (a, as) | a::as, i+1 => let (b, bs) := loop as i (b, a::bs) let (b, bs) := loop as i b :: bs /-- Move variable `#i` to the beginning of the to-do list `p.vars`. -/ private def moveToFront (p : Problem) (i : Nat) : Problem := do if i == 0 then p else if h : i < p.vars.length then let x := p.vars.get i h return { p with vars := List.moveToFront p.vars i alts := p.alts.map fun alt => { alt with patterns := List.moveToFront alt.patterns i } } else p private partial def process (p : Problem) : StateRefT State MetaM Unit := search 0 where /- If `p.vars` is empty, then we are done. Otherwise, we process `p.vars[0]`. -/ tryToProcess (p : Problem) : StateRefT State MetaM Unit := withIncRecDepth do traceState p let isInductive ← liftM $ isCurrVarInductive p if isDone p then processLeaf p else if hasAsPattern p then traceStep ("as-pattern") let p ← processAsPattern p process p else if isNatValueTransition p then traceStep ("nat value to constructor") process (expandNatValuePattern p) else if !isNextVar p then traceStep ("non variable") let p ← processNonVariable p process p else if isInductive && isConstructorTransition p then let ps ← processConstructor p ps.forM process else if isVariableTransition p then traceStep ("variable") let p ← processVariable p process p else if isValueTransition p then let ps ← processValue p ps.forM process else if isArrayLitTransition p then let ps ← processArrayLit p ps.forM process else if hasNatValPattern p then -- This branch is reachable when `p`, for example, is just values without an else-alternative. -- We added it just to get better error messages. traceStep ("nat value to constructor") process (expandNatValuePattern p) else checkNextPatternTypes p throwNonSupported p /- Return `true` if `type` does not depend on the first `i` elements in `xs` -/ checkVarDeps (xs : List Expr) (i : Nat) (type : Expr) : MetaM Bool := do match i, xs with | 0, _ => return true | _, [] => unreachable! | i+1, x::xs => if x.isFVar then if (← dependsOn type x.fvarId!) then return false checkVarDeps xs i type /-- Auxiliary method for `search`. Find next variable to "try". `i` is the position of the variable s.t. `tryToProcess` failed. We only consider variables that do not depend on other variables at `p.vars`. -/ findNext (i : Nat) : MetaM (Option Nat) := do if h : i < p.vars.length then let x := p.vars.get i h if (← checkVarDeps p.vars i (← inferType x)) then return i else findNext (i+1) else return none /-- Auxiliary method for trying the next variable to process. It moves variable `#i` to the front of the to-do list and invokes `tryToProcess`. Note that for non-dependent elimination, variable `#0` always work. If variable `#i` fails, we use `findNext` to select the next variable to try. Remark: the "missing cases" error is not considered a failure. -/ search (i : Nat) : StateRefT State MetaM Unit := do let s₁ ← getThe Meta.State let s₂ ← get let p' := moveToFront p i try tryToProcess p' catch ex => match (← withGoalOf p <| findNext (i+1)) with | none => throw ex | some j => trace[Meta.Match.match] "failed with #{i}, trying #{j}{indentD ex.toMessageData}" set s₁; set s₂; search j private def getUElimPos? (matcherLevels : List Level) (uElim : Level) : MetaM (Option Nat) := if uElim == levelZero then return none else match matcherLevels.toArray.indexOf? uElim with | none => throwError "dependent match elimination failed, universe level not found" | some pos => return some pos.val /- See comment at `mkMatcher` before `mkAuxDefinition` -/ register_builtin_option bootstrap.genMatcherCode : Bool := { defValue := true group := "bootstrap" descr := "disable code generation for auxiliary matcher function" } builtin_initialize matcherExt : EnvExtension (Std.PHashMap (Expr × Bool) Name) ← registerEnvExtension (pure {}) /- Similar to `mkAuxDefinition`, but uses the cache `matcherExt`. It also returns an Boolean that indicates whether a new matcher function was added to the environment or not. -/ def mkMatcherAuxDefinition (name : Name) (type : Expr) (value : Expr) : MetaM (Expr × (Option $ MatcherInfo → MetaM Unit)) := do trace[Meta.debug] "{name} : {type} := {value}" let compile := bootstrap.genMatcherCode.get (← getOptions) let result ← Closure.mkValueTypeClosure type value (zeta := false) let env ← getEnv let mkMatcherConst name := mkAppN (mkConst name result.levelArgs.toList) result.exprArgs match (matcherExt.getState env).find? (result.value, compile) with | some nameNew => (mkMatcherConst nameNew, none) | none => let decl := Declaration.defnDecl { name := name, levelParams := result.levelParams.toList, type := result.type, value := result.value, hints := ReducibilityHints.regular (getMaxHeight env result.value + 1), safety := if env.hasUnsafe result.type || env.hasUnsafe result.value then DefinitionSafety.unsafe else DefinitionSafety.safe } trace[Meta.debug] "{name} : {result.type} := {result.value}" let addMatcher : MatcherInfo → MetaM Unit := fun mi => do addDecl decl if compile then compileDecl decl modifyEnv fun env => matcherExt.modifyState env fun s => s.insert (result.value, compile) name addMatcherInfo name mi setInlineAttribute name (mkMatcherConst name, some addMatcher) structure MkMatcherInput where matcherName : Name matchType : Expr numDiscrs : Nat lhss : List Match.AltLHS /- Create a dependent matcher for `matchType` where `matchType` is of the form `(a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> B[a_1, ..., a_n]` where `n = numDiscrs`, and the `lhss` are the left-hand-sides of the `match`-expression alternatives. Each `AltLHS` has a list of local declarations and a list of patterns. The number of patterns must be the same in each `AltLHS`. The generated matcher has the structure described at `MatcherInfo`. The motive argument is of the form `(motive : (a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> Sort v)` where `v` is a universe parameter or 0 if `B[a_1, ..., a_n]` is a proposition. -/ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := let ⟨matcherName, matchType, numDiscrs, lhss⟩ := input forallBoundedTelescope matchType numDiscrs fun majors matchTypeBody => do checkNumPatterns majors lhss /- We generate an matcher that can eliminate using different motives with different universe levels. `uElim` is the universe level the caller wants to eliminate to. If it is not levelZero, we create a matcher that can eliminate in any universe level. This is useful for implementing `MatcherApp.addArg` because it may have to change the universe level. -/ let uElim ← getLevel matchTypeBody let uElimGen ← if uElim == levelZero then pure levelZero else mkFreshLevelMVar let motiveType ← mkForallFVars majors (mkSort uElimGen) withLocalDeclD `motive motiveType fun motive => do trace[Meta.Match.debug] "motiveType: {motiveType}" let mvarType := mkAppN motive majors trace[Meta.Match.debug] "target: {mvarType}" withAlts motive lhss fun alts minors => do let mvar ← mkFreshExprMVar mvarType let examples := majors.toList.map fun major => Example.var major.fvarId! let (_, s) ← (process { mvarId := mvar.mvarId!, vars := majors.toList, alts := alts, examples := examples }).run {} let args := #[motive] ++ majors ++ minors.map Prod.fst let type ← mkForallFVars args mvarType let val ← mkLambdaFVars args mvar trace[Meta.Match.debug] "matcher value: {val}\ntype: {type}" /- The option `bootstrap.gen_matcher_code` is a helper hack. It is useful, for example, for compiling `src/Init/Data/Int`. It is needed because the compiler uses `Int.decLt` for generating code for `Int.casesOn` applications, but `Int.casesOn` is used to give the reference implementation for ``` @[extern "lean_int_neg"] def neg (n : @& Int) : Int := match n with | ofNat n => negOfNat n | negSucc n => succ n ``` which is defined **before** `Int.decLt` -/ let (matcher, addMatcher) ← mkMatcherAuxDefinition matcherName type val trace[Meta.Match.debug] "matcher levels: {matcher.getAppFn.constLevels!}, uElim: {uElimGen}" let uElimPos? ← getUElimPos? matcher.getAppFn.constLevels! uElimGen discard <| isLevelDefEq uElimGen uElim let addMatcher := match addMatcher with | some addMatcher => { numParams := matcher.getAppNumArgs numDiscrs, altNumParams := minors.map Prod.snd, uElimPos? } |> addMatcher | none => () trace[Meta.Match.debug] "matcher: {matcher}" let unusedAltIdxs := lhss.length.fold (init := []) fun i r => if s.used.contains i then r else i::r pure { matcher, counterExamples := s.counterExamples, unusedAltIdxs := unusedAltIdxs.reverse, addMatcher } def getMkMatcherInputInContext (matcherApp : MatcherApp) : MetaM MkMatcherInput := do let matcherName := matcherApp.matcherName let some matcherInfo ← getMatcherInfo? matcherName | throwError "not a matcher: {matcherName}" let matcherConst ← getConstInfo matcherName let matcherType ← instantiateForall matcherConst.type $ matcherApp.params ++ #[matcherApp.motive] let matchType ← do let u := if let some idx := matcherInfo.uElimPos? then mkLevelParam matcherConst.levelParams.toArray[idx] else levelZero forallBoundedTelescope matcherType (some matcherInfo.numDiscrs) fun discrs t => do mkForallFVars discrs (mkConst ``PUnit [u]) let matcherType ← instantiateForall matcherType matcherApp.discrs let lhss := Array.toList $ ←forallBoundedTelescope matcherType (some matcherApp.alts.size) fun alts _ => alts.mapM fun alt => do let ty ← inferType alt forallTelescope ty fun xs body => do let xs ← xs.filterM fun x => dependsOn body x.fvarId! body.withApp fun f args => do let ctx ← getLCtx let localDecls := xs.map ctx.getFVar! let patterns ← args.mapM Match.toPattern return { ref := Syntax.missing fvarDecls := localDecls.toList patterns := patterns.toList : Match.AltLHS } return { matcherName, matchType, numDiscrs := matcherApp.discrs.size, lhss } def withMkMatcherInput (matcherName : Name) (k : MkMatcherInput → MetaM α) : MetaM α := do let some matcherInfo ← getMatcherInfo? matcherName | throwError "not a matcher: {matcherName}" let matcherConst ← getConstInfo matcherName forallBoundedTelescope matcherConst.type (some matcherInfo.arity) fun xs t => do let matcherApp ← mkConstWithLevelParams matcherConst.name let matcherApp := mkAppN matcherApp xs let some matcherApp ← matchMatcherApp? matcherApp | throwError "not a matcher app: {matcherApp}" let mkMatcherInput ← getMkMatcherInputInContext matcherApp k mkMatcherInput end Match /- Auxiliary function for MatcherApp.addArg -/ private partial def updateAlts (typeNew : Expr) (altNumParams : Array Nat) (alts : Array Expr) (i : Nat) : MetaM (Array Nat × Array Expr) := do if h : i < alts.size then let alt := alts.get ⟨i, h⟩ let numParams := altNumParams[i] let typeNew ← whnfD typeNew match typeNew with | Expr.forallE n d b _ => let alt ← forallBoundedTelescope d (some numParams) fun xs d => do let alt ← try instantiateLambda alt xs catch _ => throwError "unexpected matcher application, insufficient number of parameters in alternative" forallBoundedTelescope d (some 1) fun x d => do let alt ← mkLambdaFVars x alt -- x is the new argument we are adding to the alternative let alt ← mkLambdaFVars xs alt pure alt updateAlts (b.instantiate1 alt) (altNumParams.set! i (numParams+1)) (alts.set ⟨i, h⟩ alt) (i+1) | _ => throwError "unexpected type at MatcherApp.addArg" else pure (altNumParams, alts) /- Given - matcherApp `match_i As (fun xs => motive[xs]) discrs (fun ys_1 => (alt_1 : motive (C_1[ys_1])) ... (fun ys_n => (alt_n : motive (C_n[ys_n]) remaining`, and - expression `e : B[discrs]`, Construct the term `match_i As (fun xs => B[xs] -> motive[xs]) discrs (fun ys_1 (y : B[C_1[ys_1]]) => alt_1) ... (fun ys_n (y : B[C_n[ys_n]]) => alt_n) e remaining`, and We use `kabstract` to abstract the discriminants from `B[discrs]`. This method assumes - the `matcherApp.motive` is a lambda abstraction where `xs.size == discrs.size` - each alternative is a lambda abstraction where `ys_i.size == matcherApp.altNumParams[i]` -/ def MatcherApp.addArg (matcherApp : MatcherApp) (e : Expr) : MetaM MatcherApp := lambdaTelescope matcherApp.motive fun motiveArgs motiveBody => do unless motiveArgs.size == matcherApp.discrs.size do -- This error can only happen if someone implemented a transformation that rewrites the motive created by `mkMatcher`. throwError "unexpected matcher application, motive must be lambda expression with #{matcherApp.discrs.size} arguments" let eType ← inferType e let eTypeAbst ← matcherApp.discrs.size.foldRevM (init := eType) fun i eTypeAbst => do let motiveArg := motiveArgs[i] let discr := matcherApp.discrs[i] let eTypeAbst ← kabstract eTypeAbst discr pure $ eTypeAbst.instantiate1 motiveArg let motiveBody ← mkArrow eTypeAbst motiveBody let matcherLevels ← match matcherApp.uElimPos? with | none => pure matcherApp.matcherLevels | some pos => let uElim ← getLevel motiveBody pure $ matcherApp.matcherLevels.set! pos uElim let motive ← mkLambdaFVars motiveArgs motiveBody -- Construct `aux` `match_i As (fun xs => B[xs] → motive[xs]) discrs`, and infer its type `auxType`. -- We use `auxType` to infer the type `B[C_i[ys_i]]` of the new argument in each alternative. let aux := mkAppN (mkConst matcherApp.matcherName matcherLevels.toList) matcherApp.params let aux := mkApp aux motive let aux := mkAppN aux matcherApp.discrs check aux unless (← isTypeCorrect aux) do throwError "failed to add argument to matcher application, type error when constructing the new motive" let auxType ← inferType aux let (altNumParams, alts) ← updateAlts auxType matcherApp.altNumParams matcherApp.alts 0 pure { matcherApp with matcherLevels := matcherLevels, motive := motive, alts := alts, altNumParams := altNumParams, remaining := #[e] ++ matcherApp.remaining } builtin_initialize registerTraceClass `Meta.Match.match registerTraceClass `Meta.Match.debug registerTraceClass `Meta.Match.unify end Lean.Meta
10d4e9f94deb772125a2b8e1d74abf5aabbfcdd5
03577f7aaac416af2e8f734149669af300dc499c
/lean_exercises/q1.lean
17c7225fb0b43869f7c9c5c2363157089b2f0858
[]
no_license
nymarya/algorithms
b4dc73389de588e6934da15b0e0b67ae354907aa
1df987f82273a8ffe2b2042552bbb1428bf165bf
refs/heads/master
1,609,629,118,479
1,600,087,537,000
1,600,087,537,000
99,489,969
0
1
null
null
null
null
UTF-8
Lean
false
false
812
lean
-- constants variables x y z u v w :Prop -- relations variable B: Prop → Prop → Prop → Prop --betweeness variable C: Prop → Prop → Prop → Prop → Prop -- congruence -- axioms --- identity of betweness axiom bet_id : B x y x → x = y --- reflexivity of congruence axiom cong_refl : C x y y x --- identity of congruence axiom cong_id : C x y z z → x = y --- transitivity of congruence axiom cong_trans: (C x y z u ∧ C x y v w) → C z u v w. -- theorem: symmetry of betweeness theorem bet_sym: B x y z → B y z x := sorry example: (C x y z z ↔ x = y) ↔ (B x y x) := iff.intro( assume h1: C x y z z ↔ x = y, show B x y x, from sorry ) --end iff.intro ( assume h2: B x y x, show C x y z z ↔ x = y, from sorry ) --https://en.wikipedia.org/wiki/Tarski%27s_axioms
ec45d92be7eb978c89ca52657983a5855a367e2d
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/group_theory/group_action.lean
f806fc8c868107a7a0a6740b5fcb9f4ee94cd332
[ "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,697
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.set.finite group_theory.coset universes u v w variables {α : Type u} {β : Type v} {γ : Type w} class is_monoid_action [monoid α] (f : α → β → β) : Prop := (one : ∀ a : β, f (1 : α) a = a) (mul : ∀ (x y : α) (a : β), f (x * y) a = f x (f y a)) namespace is_monoid_action variables [monoid α] (f : α → β → β) [is_monoid_action f] def orbit (a : β) := set.range (λ x : α, f x a) @[simp] lemma mem_orbit_iff {f : α → β → β} [is_monoid_action f] {a b : β} : b ∈ orbit f a ↔ ∃ x : α, f x a = b := iff.rfl @[simp] lemma mem_orbit (a : β) (x : α) : f x a ∈ orbit f a := ⟨x, rfl⟩ @[simp] lemma mem_orbit_self (a : β) : a ∈ orbit f a := ⟨1, show f 1 a = a, by simp [is_monoid_action.one f]⟩ instance orbit_fintype (a : β) [fintype α] [decidable_eq β] : fintype (orbit f a) := set.fintype_range _ def stabilizer (a : β) : set α := {x : α | f x a = a} @[simp] lemma mem_stabilizer_iff {f : α → β → β} [is_monoid_action f] {a : β} {x : α} : x ∈ stabilizer f a ↔ f x a = a := iff.rfl def fixed_points : set β := {a : β | ∀ x, x ∈ stabilizer f a} @[simp] lemma mem_fixed_points {f : α → β → β} [is_monoid_action f] {a : β} : a ∈ fixed_points f ↔ ∀ x : α, f x a = a := iff.rfl lemma mem_fixed_points' {f : α → β → β} [is_monoid_action f] {a : β} : a ∈ fixed_points f ↔ (∀ b, b ∈ orbit f a → b = a) := ⟨λ h b h₁, let ⟨x, hx⟩ := mem_orbit_iff.1 h₁ in hx ▸ h x, λ h b, mem_stabilizer_iff.2 (h _ (mem_orbit _ _ _))⟩ lemma comp_hom [group γ] (f : α → β → β) [is_monoid_action f] (g : γ → α) [is_monoid_hom g] : is_monoid_action (f ∘ g) := { one := by simp [is_monoid_hom.map_one g, is_monoid_action.one f], mul := by simp [is_monoid_hom.map_mul g, is_monoid_action.mul f] } end is_monoid_action class is_group_action [group α] (f : α → β → β) extends is_monoid_action f : Prop namespace is_group_action variables [group α] section variables (f : α → β → β) [is_group_action f] open is_monoid_action quotient_group def to_perm (g : α) : equiv.perm β := { to_fun := f g, inv_fun := f g⁻¹, left_inv := λ a, by rw [← is_monoid_action.mul f, inv_mul_self, is_monoid_action.one f], right_inv := λ a, by rw [← is_monoid_action.mul f, mul_inv_self, is_monoid_action.one f] } instance : is_group_hom (to_perm f) := { mul := λ x y, equiv.ext _ _ (λ a, is_monoid_action.mul f x y a) } lemma bijective (g : α) : function.bijective (f g) := (to_perm f g).bijective lemma orbit_eq_iff {f : α → β → β} [is_monoid_action f] {a b : β} : orbit f a = orbit f b ↔ a ∈ orbit f b:= ⟨λ h, h ▸ mem_orbit_self _ _, λ ⟨x, (hx : f x b = a)⟩, set.ext (λ c, ⟨λ ⟨y, (hy : f y a = c)⟩, ⟨y * x, show f (y * x) b = c, by rwa [is_monoid_action.mul f, hx]⟩, λ ⟨y, (hy : f y b = c)⟩, ⟨y * x⁻¹, show f (y * x⁻¹) a = c, by conv {to_rhs, rw [← hy, ← mul_one y, ← inv_mul_self x, ← mul_assoc, is_monoid_action.mul f, hx]}⟩⟩)⟩ instance (a : β) : is_subgroup (stabilizer f a) := { one_mem := is_monoid_action.one _ _, mul_mem := λ x y (hx : f x a = a) (hy : f y a = a), show f (x * y) a = a, by rw is_monoid_action.mul f; simp *, inv_mem := λ x (hx : f x a = a), show f x⁻¹ a = a, by rw [← hx, ← is_monoid_action.mul f, inv_mul_self, is_monoid_action.one f, hx] } lemma comp_hom [group γ] (f : α → β → β) [is_group_action f] (g : γ → α) [is_group_hom g] : is_group_action (f ∘ g) := { ..is_monoid_action.comp_hom f g } def orbit_rel : setoid β := { r := λ a b, a ∈ orbit f b, iseqv := ⟨mem_orbit_self f, λ a b, by simp [orbit_eq_iff.symm, eq_comm], λ a b, by simp [orbit_eq_iff.symm, eq_comm] {contextual := tt}⟩ } open quotient_group noncomputable def orbit_equiv_quotient_stabilizer (a : β) : orbit f a ≃ quotient (stabilizer f a) := equiv.symm (@equiv.of_bijective _ _ (λ x : quotient (stabilizer f a), quotient.lift_on' x (λ x, (⟨f x a, mem_orbit _ _ _⟩ : orbit f a)) (λ g h (H : _ = _), subtype.eq $ (is_group_action.bijective f (g⁻¹)).1 $ show f g⁻¹ (f g a) = f g⁻¹ (f h a), by rw [← is_monoid_action.mul f, ← is_monoid_action.mul f, H, inv_mul_self, is_monoid_action.one f])) ⟨λ g h, quotient.induction_on₂' g h (λ g h H, quotient.sound' $ have H : f g a = f h a := subtype.mk.inj H, show f (g⁻¹ * h) a = a, by rw [is_monoid_action.mul f, ← H, ← is_monoid_action.mul f, inv_mul_self, is_monoid_action.one f]), λ ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩) end open quotient_group is_monoid_action is_subgroup def mul_left_cosets (H : set α) [is_subgroup H] (x : α) (y : quotient H) : quotient H := quotient.lift_on' y (λ y, quotient_group.mk ((x : α) * y)) (λ a b (hab : _ ∈ H), quotient_group.eq.2 (by rwa [mul_inv_rev, ← mul_assoc, mul_assoc (a⁻¹), inv_mul_self, mul_one])) instance (H : set α) [is_subgroup H] : is_group_action (mul_left_cosets H) := { one := λ a, quotient.induction_on' a (λ a, quotient_group.eq.2 (by simp [is_submonoid.one_mem])), mul := λ x y a, quotient.induction_on' a (λ a, quotient_group.eq.2 (by simp [mul_inv_rev, is_submonoid.one_mem, mul_assoc])) } instance mul_left_cosets_comp_subtype_val (H I : set α) [is_subgroup H] [is_subgroup I] : is_group_action (mul_left_cosets H ∘ (subtype.val : I → α)) := is_group_action.comp_hom _ _ end is_group_action
2b7328393a10d12533d505563d848ed45a701201
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/zmod/quadratic_reciprocity.lean
55e81fd3b894704c425b8a284a19d613f4ae4d9f
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
33,219
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.finite data.zmod.basic algebra.pi_instances open function finset nat finite_field zmodp namespace zmodp variables {p q : ℕ} (hp : nat.prime p) (hq : nat.prime q) @[simp] lemma card_units_zmodp : fintype.card (units (zmodp p hp)) = p - 1 := by rw [card_units, card_zmodp] theorem fermat_little {p : ℕ} (hp : nat.prime p) {a : zmodp p hp} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by rw [← units.mk0_val ha, ← @units.coe_one (zmodp p hp), ← units.coe_pow, ← units.ext_iff, ← card_units_zmodp hp, pow_card_eq_one] lemma euler_criterion_units {x : units (zmodp p hp)} : (∃ y : units (zmodp p hp), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := hp.eq_two_or_odd.elim (λ h, by subst h; revert x; exact dec_trivial) (λ hp1, let ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmodp p hp)) in let ⟨n, hn⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm ▸ hg x in ⟨λ ⟨y, hy⟩, by rw [← hy, ← pow_mul, two_mul_odd_div_two hp1, ← card_units_zmodp hp, pow_card_eq_one], λ hx, have 2 * (p / 2) ∣ n * (p / 2), by rw [two_mul_odd_div_two hp1, ← card_units_zmodp hp, ← order_of_eq_card_of_forall_mem_gpowers hg]; exact order_of_dvd_of_pow_eq_one (by rwa [pow_mul, hn]), let ⟨m, hm⟩ := dvd_of_mul_dvd_mul_right (nat.div_pos hp.ge_two dec_trivial) this in ⟨g ^ m, by rwa [← pow_mul, mul_comm, ← hm]⟩⟩) lemma euler_criterion {a : zmodp p hp} (ha : a ≠ 0) : (∃ y : zmodp p hp, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := ⟨λ ⟨y, hy⟩, have hy0 : y ≠ 0, from λ h, by simp [h, _root_.zero_pow (succ_pos 1)] at hy; cc, by simpa using (units.ext_iff.1 $ (euler_criterion_units hp).1 ⟨units.mk0 _ hy0, show _ = units.mk0 _ ha, by rw [units.ext_iff]; simpa⟩), λ h, let ⟨y, hy⟩ := (euler_criterion_units hp).2 (show units.mk0 _ ha ^ (p / 2) = 1, by simpa [units.ext_iff]) in ⟨y, by simpa [units.ext_iff] using hy⟩⟩ lemma exists_pow_two_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmodp p hp, y ^ 2 = -1) ↔ p % 4 ≠ 3 := have (-1 : zmodp p hp) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, hp.eq_two_or_odd.elim (λ hp, by subst hp; exact dec_trivial) (λ hp1, (mod_two_eq_zero_or_one (p / 2)).elim (λ hp2, begin rw [euler_criterion hp this, neg_one_pow_eq_pow_mod_two, hp2, _root_.pow_zero, eq_self_iff_true, true_iff], assume h, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, h] at hp2, exact absurd hp2 dec_trivial, end) (λ hp2, begin rw [euler_criterion hp this, neg_one_pow_eq_pow_mod_two, hp2, _root_.pow_one, iff_false_intro (zmodp.ne_neg_self hp hp1 one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at hp2, rw [← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp1, have hp4 : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp1 hp2, revert hp4, generalize : p % 4 = k, revert k, exact dec_trivial end)) lemma pow_div_two_eq_neg_one_or_one {a : zmodp p hp} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := hp.eq_two_or_odd.elim (λ h, by revert a ha; subst h; exact dec_trivial) (λ hp1, by rw [← mul_self_eq_one_iff, ← _root_.pow_add, ← two_mul, two_mul_odd_div_two hp1]; exact fermat_little hp ha) @[simp] lemma wilsons_lemma {p : ℕ} (hp : nat.prime p) : (fact (p - 1) : zmodp p hp) = -1 := begin rw [← finset.prod_range_id_eq_fact, ← @units.coe_one (zmodp p hp), ← units.coe_neg, ← @prod_univ_units_id_eq_neg_one (zmodp p hp), ← prod_hom (coe : units (zmodp p hp) → zmodp p hp), ← prod_hom (coe : ℕ → zmodp p hp)], exact eq.symm (prod_bij (λ a _, (a : zmodp p hp).1) (λ a ha, mem_erase.2 ⟨λ h, units.coe_ne_zero a $ fin.eq_of_veq h, by rw [mem_range, ← succ_sub hp.pos, succ_sub_one]; exact a.1.2⟩) (λ a _, by simp) (λ _ _ _ _, units.ext_iff.2 ∘ fin.eq_of_veq) (λ b hb, have b ≠ 0 ∧ b < p, by rwa [mem_erase, mem_range, ← succ_sub hp.pos, succ_sub_one] at hb, ⟨units.mk0 _ (show (b : zmodp p hp) ≠ 0, from fin.ne_of_vne $ by rw [zmod.val_cast_nat, ← @nat.cast_zero (zmodp p hp), zmod.val_cast_nat]; simp [mod_eq_of_lt this.2, this.1]), mem_univ _, by simp [val_cast_of_lt hp this.2]⟩)) end @[simp] lemma prod_range_prime_erase_zero {p : ℕ} (hp : nat.prime p) : ((range p).erase 0).prod (λ x, (x : zmodp p hp)) = -1 := by conv in (range p) { rw [← succ_sub_one p, succ_sub hp.pos] }; rw [prod_hom (coe : ℕ → zmodp p hp), finset.prod_range_id_eq_fact, wilsons_lemma] end zmodp namespace quadratic_reciprocity_aux variables {p q : ℕ} (hp : nat.prime p) (hq : nat.prime q) (hp1 : p % 2 = 1) (hq1 : q % 2 = 1) (hpq : p ≠ q) include hp hq hp1 hq1 hpq lemma filter_range_p_mul_q_div_two_eq : (range ((p * q) / 2).succ).filter (coprime p) = (range (q / 2)).bind (λ x, (erase (range p) 0).image (+ p * x)) ∪ (erase (range (succ (p / 2))) 0).image (+ q / 2 * p) := finset.ext.2 $ λ x, ⟨λ h, have hxp0 : x % p ≠ 0, by rw [ne.def, ← dvd_iff_mod_eq_zero, ← hp.coprime_iff_not_dvd]; exact (mem_filter.1 h).2, mem_union.2 $ or_iff_not_imp_right.2 (λ h₁, mem_bind.2 ⟨x / p, mem_range.2 $ nat.div_lt_of_lt_mul (by_contradiction (λ h₂, let ⟨c, hc⟩ := le_iff_exists_add.1 (le_of_not_gt h₂) in have hcp : c ≤ p / 2, from @nat.le_of_add_le_add_left (p * (q / 2)) _ _ (by rw [← hc, ← odd_mul_odd_div_two hp1 hq1]; exact le_of_lt_succ (mem_range.1 (mem_filter.1 h).1)), h₁ $ mem_image.2 ⟨c, mem_erase.2 ⟨λ h, hxp0 $ by simp [h, hc], mem_range.2 $ lt_succ_of_le $ hcp⟩, by rw hc; simp [mul_comm]⟩)), mem_image.2 ⟨x % p, mem_erase.2 $ by rw [ne.def, ← dvd_iff_mod_eq_zero, ← hp.coprime_iff_not_dvd, mem_range]; exact ⟨(mem_filter.1 h).2, mod_lt _ hp.pos⟩, nat.mod_add_div _ _⟩⟩), λ h, mem_filter.2 $ (mem_union.1 h).elim (λ h, let ⟨m, hm₁, hm₂⟩ := mem_bind.1 h in let ⟨k, hk₁, hk₂⟩ := mem_image.1 hm₂ in ⟨mem_range.2 $ hk₂ ▸ (mul_lt_mul_left (show 0 < 2, from dec_trivial)).1 begin rw [mul_succ, two_mul_odd_div_two (nat.odd_mul_odd hp1 hq1), mul_add], clear _let_match _let_match, exact calc 2 * k + 2 * (p * m) < 2 * p + 2 * (p * m) : add_lt_add_right ((mul_lt_mul_left dec_trivial).2 (by simp at hk₁; tauto)) _ ... = 2 * (p * (m + 1)) : by simp [mul_add, mul_assoc, mul_comm, mul_left_comm] ... ≤ 2 * (p * (q / 2)) : (mul_le_mul_left (show 0 < 2, from dec_trivial)).2 ((mul_le_mul_left hp.pos).2 $ succ_le_of_lt $ mem_range.1 hm₁) ... ≤ _ : by rw [mul_left_comm, two_mul_odd_div_two hq1, nat.mul_sub_left_distrib, ← nat.sub_add_comm (mul_pos hp.pos hq.pos), add_succ, succ_eq_add_one, nat.add_sub_cancel]; exact le_trans (nat.sub_le_self _ _) (nat.le_add_right _ _), end, by rw [nat.prime.coprime_iff_not_dvd hp, ← hk₂, ← nat.dvd_add_iff_left (dvd_mul_right _ _), dvd_iff_mod_eq_zero, mod_eq_of_lt]; clear _let_match _let_match; simp at hk₁; tauto⟩) (λ h, let ⟨m, hm₁, hm₂⟩ := mem_image.1 h in ⟨mem_range.2 $ hm₂ ▸ begin refine (mul_lt_mul_left (show 0 < 2, from dec_trivial)).1 _, rw [mul_succ, two_mul_odd_div_two (nat.odd_mul_odd hp1 hq1), mul_add, ← mul_assoc 2, two_mul_odd_div_two hq1], exact calc 2 * m + (q - 1) * p ≤ 2 * (p / 2) + (q - 1) * p : add_le_add_right ((mul_le_mul_left dec_trivial).2 (le_of_lt_succ (mem_range.1 (by simp * at *)))) _ ... < _ : begin rw [two_mul_odd_div_two hp1, nat.mul_sub_right_distrib, one_mul], rw [← nat.sub_add_comm hp.pos, nat.add_sub_cancel' (le_mul_of_ge_one_left' (nat.zero_le _) hq.pos), mul_comm], exact lt_add_of_pos_right _ dec_trivial end, end, by rw [hp.coprime_iff_not_dvd, dvd_iff_mod_eq_zero, ← hm₂, nat.add_mul_mod_self_right, mod_eq_of_lt (lt_of_lt_of_le _ (nat.div_lt_self hp.pos (show 1 < 2, from dec_trivial)))]; simp at hm₁; clear _let_match; tauto⟩)⟩ lemma prod_filter_range_p_mul_q_div_two_eq : (range (q / 2)).prod (λ n, ((range p).erase 0).prod (+ p * n)) * ((range (p / 2).succ).erase 0).prod (+ (q / 2) * p) = ((range ((p * q) / 2).succ).filter (coprime p)).prod (λ x, x) := calc (range (q / 2)).prod (λ n, ((range p).erase 0).prod (+ p * n)) * ((range (p / 2).succ).erase 0).prod (+ (q / 2) * p) = (range (q / 2)).prod (λ n, (((range p).erase 0).image (+ p * n)).prod (λ x, x)) * (((range (p / 2).succ).erase 0).image (+ (q / 2) * p)).prod (λ x, x) : by simp only [prod_image (λ _ _ _ _ h, add_right_cancel h)]; refl ... = ((range (q / 2)).bind (λ x, (erase (range p) 0).image (+ p * x)) ∪ (erase (range (succ (p / 2))) 0).image (+ q / 2 * p)).prod (λ x, x) : have h₁ : finset.bind (range (q / 2)) (λ x, ((range p).erase 0).image (+ p * x)) ∩ image (+ q / 2 * p) (erase (range (succ (p / 2))) 0) = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x, begin suffices : ∀ a, a ≠ 0 → a ≤ p / 2 → a + q / 2 * p = x → ∀ b, b < q / 2 → ∀ c, c ≠ 0 → c < p → ¬c + p * b = x, { simpa [lt_succ_iff] }, assume a ha0 hap ha b hbq c hc0 hcp hc, rw mul_comm at ha, rw [← ((nat.div_mod_unique hp.pos).2 ⟨hc, hcp⟩).1, ← ((nat.div_mod_unique hp.pos).2 ⟨ha, lt_of_le_of_lt hap (nat.div_lt_self hp.pos dec_trivial)⟩).1] at hbq, exact lt_irrefl _ hbq end, have h₂ : ∀ x, x ∈ range (q / 2) → ∀ y, y ∈ range (q / 2) → x ≠ y → (erase (range p) 0).image (+ p * x) ∩ image (+ p * y) (erase (range p) 0) = ∅ := λ x hx y hy hxy, begin suffices : ∀ z a, a ≠ 0 → a < p → a + p * x = z → ∀ b, b ≠ 0 → b < p → b + p * y ≠ z, { simpa [finset.ext] }, assume z a ha0 hap ha b hb0 hbp hb, have : (a + p * x) / p = (b + p * y) / p, { rw [ha, hb] }, rw [nat.add_mul_div_left _ _ hp.pos, nat.add_mul_div_left _ _ hp.pos, (nat.div_eq_zero_iff hp.pos).2 hap, (nat.div_eq_zero_iff hp.pos).2 hbp] at this, simpa [hxy] end, by rw [prod_union h₁, prod_bind h₂] ... = (((range ((p * q) / 2).succ)).filter (coprime p)).prod (λ x, x) : prod_congr (filter_range_p_mul_q_div_two_eq hp hq hp1 hq1 hpq).symm (λ _ _, rfl) lemma prod_filter_range_p_mul_q_div_two_mod_p_eq : ((((range ((p * q) / 2).succ).filter (coprime p)).prod (λ x, x) : ℕ) : zmodp p hp) = (-1) ^ (q / 2) * ((range (p / 2).succ).erase 0).prod (λ x, x) := begin rw [← prod_filter_range_p_mul_q_div_two_eq hp hq hp1 hq1 hpq, nat.cast_mul, ← prod_hom (coe : ℕ → zmodp p hp), ← prod_hom (coe : ℕ → zmodp p hp)], conv in ((finset.prod (erase (range p) 0) _ : ℕ) : zmodp p hp) { rw ← prod_hom (coe : ℕ → zmodp p hp) }, simp end lemma prod_filter_range_p_mul_q_not_coprime_eq : (((((range ((p * q) / 2).succ).filter (coprime p)).filter (λ x, ¬ coprime q x)).prod (λ x, x) : ℕ) : zmodp p hp) = q ^ (p / 2) * ((range (p / 2).succ).erase 0).prod (λ x, x) := have hcard : ((range (p / 2).succ).erase 0).card = p / 2 := by rw [card_erase_of_mem (mem_range.2 (succ_pos _)), card_range, pred_succ], begin conv in ((q : zmodp p hp) ^ (p / 2)) { rw ← hcard }, rw [← prod_const, ← prod_mul_distrib, ← prod_hom (coe : ℕ → zmodp p hp)], exact eq.symm (prod_bij (λ a _, a * q) (λ a ha, have ha' : a ≤ p / 2 ∧ a > 0, by simp [nat.pos_iff_ne_zero, lt_succ_iff] at *; tauto, mem_filter.2 ⟨mem_filter.2 ⟨mem_range.2 $ lt_succ_of_le $ (calc a * q ≤ q * (p / 2) : by rw mul_comm; exact mul_le_mul_left _ ha'.1 ... ≤ _ : by rw [mul_comm p, odd_mul_odd_div_two hq1 hp1]; exact nat.le_add_right _ _), by rw [hp.coprime_iff_not_dvd, hp.dvd_mul, not_or_distrib]; refine ⟨λ hpa, not_le_of_gt (show p / 2 < p, from nat.div_lt_self hp.pos dec_trivial) (le_trans (le_of_dvd ha'.2 hpa) ha'.1), by rwa [← hp.coprime_iff_not_dvd, coprime_primes hp hq]⟩⟩, by simp [hq.coprime_iff_not_dvd]⟩) (by simp [mul_comm]) (by simp [nat.mul_right_inj hq.pos]) (λ b hb, have hb' : (b ≤ p * q / 2 ∧ coprime p b) ∧ q ∣ b, by simpa [hq.coprime_iff_not_dvd, lt_succ_iff] using hb, have hb0 : b > 0, from nat.pos_of_ne_zero (λ hb0, by simpa [hb0, hp.coprime_iff_not_dvd] using hb'), ⟨b / q, mem_erase.2 ⟨nat.pos_iff_ne_zero.1 (nat.div_pos (le_of_dvd hb0 hb'.2) hq.pos), mem_range.2 $ lt_succ_of_le $ by rw [mul_comm, odd_mul_odd_div_two hq1 hp1] at hb'; have := @nat.div_le_div_right _ _ hb'.1.1 q; rwa [add_comm, nat.add_mul_div_left _ _ hq.pos, ((nat.div_eq_zero_iff hq.pos).2 (nat.div_lt_self hq.pos (lt_succ_self _))), zero_add] at this⟩, by rw nat.div_mul_cancel hb'.2⟩)) end lemma prod_range_p_mul_q_filter_coprime_mod_p (hq : nat.prime q) (hp1 : p % 2 = 1) (hq1 : q % 2 = 1) (hpq : p ≠ q) : ((((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, x) : ℕ) : zmodp p hp) = (-1) ^ (q / 2) * q ^ (p / 2) := have hq0 : (q : zmodp p hp) ≠ 0, by rwa [← nat.cast_zero, ne.def, zmodp.eq_iff_modeq_nat, nat.modeq.modeq_zero_iff, ← hp.coprime_iff_not_dvd, coprime_primes hp hq], (domain.mul_right_inj (show (q ^ (p / 2) * ((range (p / 2).succ).erase 0).prod (λ x, x) : zmodp p hp) ≠ 0, from mul_ne_zero (pow_ne_zero _ hq0) (suffices h : ∀ (x : ℕ), ¬x = 0 → x ≤ p / 2 → ¬(x : zmodp p hp) = 0, by simpa [prod_eq_zero_iff, lt_succ_iff], assume x hx0 hxp, by rwa [← @nat.cast_zero (zmodp p hp), zmodp.eq_iff_modeq_nat, nat.modeq, zero_mod, mod_eq_of_lt (lt_of_le_of_lt hxp (nat.div_lt_self hp.pos (lt_succ_self _)))]))).1 $ have h₁ : (range (succ (p * q / 2))).filter (coprime (p * q)) ∩ filter (λ x, ¬coprime q x) (filter (coprime p) (range (succ (p * q / 2)))) = ∅, by have := @coprime.coprime_mul_left p q; simp [finset.ext, *] at * {contextual := tt}, calc ((((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, x) : ℕ) : zmodp p hp) * (q ^ (p / 2) * ((range (p / 2).succ).erase 0).prod (λ x, x) : zmodp p hp) = (((range (succ (p * q / 2))).filter (coprime (p * q)) ∪ filter (λ x, ¬coprime q x) (filter (coprime p) (range (succ (p * q / 2))))).prod (λ x, x) : ℕ) : by rw [← prod_filter_range_p_mul_q_not_coprime_eq hp hq hp1 hq1 hpq, ← nat.cast_mul, ← prod_union h₁] ... = (((range ((p * q) / 2).succ).filter (coprime p)).prod (λ x, x) : ℕ) : congr_arg coe (prod_congr (by simp [finset.ext, coprime_mul_iff_left]; tauto) (λ _ _, rfl)) ... = _ : by rw [prod_filter_range_p_mul_q_div_two_mod_p_eq hp hq hp1 hq1 hpq]; cases zmodp.pow_div_two_eq_neg_one_or_one hp hq0; simp [h, _root_.pow_succ] lemma card_range_p_mul_q_filter_not_coprime : card (filter (λ x, ¬coprime p x) (range (succ (p * q / 2)))) = (q / 2).succ := calc card (filter (λ x, ¬coprime p x) (range (succ (p * q / 2)))) = card ((range (q / 2).succ).image (* p)) : congr_arg card $ finset.ext.2 $ λ x, begin rw [mem_filter, mem_range, hp.coprime_iff_not_dvd, not_not, mem_image], exact ⟨λ ⟨h, ⟨m, hm⟩⟩, ⟨m, mem_range.2 (lt_of_mul_lt_mul_left (by rw ← hm; exact lt_of_lt_of_le h (by rw [succ_le_iff, mul_succ, odd_mul_odd_div_two hp1 hq1]; exact add_lt_add_left (div_lt_self hp.pos (lt_succ_self 1)) _)) (nat.zero_le p)), hm.symm ▸ mul_comm m p⟩, λ ⟨m, hm₁, hm₂⟩, ⟨lt_succ_of_le (by rw [← hm₂, odd_mul_odd_div_two hp1 hq1]; exact le_trans (by rw mul_comm; exact mul_le_mul_left _ (le_of_lt_succ (mem_range.1 hm₁))) (le_add_right _ _)), by simp [hm₂.symm]⟩⟩ end ... = _ : by rw [card_image_of_injective _ (λ _ _ h, (nat.mul_right_inj hp.pos).1 h), card_range] lemma prod_filter_range_p_mul_q_div_two_eq_prod_product : ((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, if (x : zmodp q hq).1 ≤ q / 2 then ((x : zmodp p hp), (x : zmodp q hq)) else -((x : zmodp p hp), (x : zmodp q hq))) = (((range p).erase 0).product ((range (q / 2).succ).erase 0)).prod (λ x, ((x.1 : zmodp p hp), (x.2 : zmodp q hq))) := have hpqpnat : (((⟨p * q, mul_pos hp.pos hq.pos⟩ : ℕ+) : ℕ) : ℤ) = (p * q : ℤ), by simp, have hpqpnat' : ((⟨p * q, mul_pos hp.pos hq.pos⟩ : ℕ+) : ℕ) = p * q, by simp, have hpq1 : ((⟨p * q, mul_pos hp.pos hq.pos⟩ : ℕ+) : ℕ) % 2 = 1, from nat.odd_mul_odd hp1 hq1, have hpq1' : p * q > 1, from one_lt_mul hp.pos hq.gt_one, have hhq0 : ∀ a : ℕ, coprime q a → a ≠ 0, from λ a, imp_not_comm.1 $ by simp [hq.coprime_iff_not_dvd] {contextual := tt}, have hpq0 : 0 < p * q / 2, from nat.div_pos (succ_le_of_lt $ one_lt_mul hp.pos hq.gt_one) dec_trivial, have hinj : ∀ a₁ a₂ : ℕ, a₁ ∈ (range (p * q / 2).succ).filter (coprime (p * q)) → a₂ ∈ (range (p * q / 2).succ).filter (coprime (p * q)) → (if (a₁ : zmodp q hq).1 ≤ q / 2 then ((a₁ : zmodp p hp).1, (a₁ : zmodp q hq).1) else ((-a₁ : zmodp p hp).1, (-a₁ : zmodp q hq).1)) = (if (a₂ : zmodp q hq).1 ≤ q / 2 then ((a₂ : zmodp p hp).1, (a₂ : zmodp q hq).1) else ((-a₂ : zmodp p hp).1, (-a₂ : zmodp q hq).1)) → a₁ = a₂, from λ a b ha hb h, have ha' : a ≤ (p * q) / 2 ∧ coprime (p * q) a, by simpa [lt_succ_iff] using ha, have hapq' : a < ((⟨p * q, mul_pos hp.pos hq.pos⟩ : ℕ+) : ℕ) := lt_of_le_of_lt ha'.1 (div_lt_self (mul_pos hp.pos hq.pos) dec_trivial), have hb' : b ≤ (p * q) / 2 ∧ coprime (p * q) b, by simpa [lt_succ_iff, coprime_mul_iff_left] using hb, have hbpq' : b < ((⟨p * q, mul_pos hp.pos hq.pos⟩ : ℕ+) : ℕ) := lt_of_le_of_lt hb'.1 (div_lt_self (mul_pos hp.pos hq.pos) dec_trivial), have val_inj : ∀ {p : ℕ} (hp : nat.prime p) (x y : zmodp p hp), x.val = y.val ↔ x = y, from λ _ _ _ _, ⟨fin.eq_of_veq, fin.veq_of_eq⟩, have hbpq0 : (b : zmod (⟨p * q, mul_pos hp.pos hq.pos⟩)) ≠ 0, by rw [ne.def, zmod.eq_zero_iff_dvd_nat]; exact λ h, not_coprime_of_dvd_of_dvd hpq1' (dvd_refl (p * q)) h hb'.2, have habneg : ¬((a : zmodp p hp) = -b ∧ (a : zmodp q hq) = -b), begin rw [← int.cast_coe_nat a, ← int.cast_coe_nat b, ← int.cast_coe_nat a, ← int.cast_coe_nat b, ← int.cast_neg, ← int.cast_neg, zmodp.eq_iff_modeq_int, zmodp.eq_iff_modeq_int, @int.modeq.modeq_and_modeq_iff_modeq_mul _ _ p q ((coprime_primes hp hq).2 hpq), ← hpqpnat, ← zmod.eq_iff_modeq_int, int.cast_coe_nat, int.cast_neg, int.cast_coe_nat], assume h, rw [← hpqpnat', ← zmod.val_cast_of_lt hbpq', zmod.le_div_two_iff_lt_neg hpq1 hbpq0, ← h, zmod.val_cast_of_lt hapq', ← not_le] at hb', exact hb'.1 ha'.1, end, have habneg' : ¬((-a : zmodp p hp) = b ∧ (-a : zmodp q hq) = b), by rwa [← neg_inj', neg_neg, ← @neg_inj' _ _ (-a : zmodp q hq), neg_neg], suffices (a : zmodp p hp) = b ∧ (a : zmodp q hq) = b, by rw [← mod_eq_of_lt hapq', ← mod_eq_of_lt hbpq']; rwa [zmodp.eq_iff_modeq_nat, zmodp.eq_iff_modeq_nat, nat.modeq.modeq_and_modeq_iff_modeq_mul ((coprime_primes hp hq).2 hpq)] at this, by split_ifs at h; simp * at *, have hmem : ∀ a : ℕ, a ∈ (range (p * q / 2).succ).filter (coprime (p * q)) → (if (a : zmodp q hq).1 ≤ q / 2 then ((a : zmodp p hp).1, (a : zmodp q hq).1) else ((-a : zmodp p hp).1, (-a : zmodp q hq).1)) ∈ ((range p).erase 0).product ((range (succ (q / 2))).erase 0), from λ x, have hxp : ∀ {p : ℕ} (hp : nat.prime p), (x : zmodp p hp).val = 0 ↔ p ∣ x, from λ p hp, by rw [zmodp.val_cast_nat, nat.dvd_iff_mod_eq_zero], have hxpneg : ∀ {p : ℕ} (hp : nat.prime p), (-x : zmodp p hp).val = 0 ↔ p ∣ x, from λ p hp, by rw [← int.cast_coe_nat x, ← int.cast_neg, ← int.coe_nat_inj', zmodp.coe_val_cast_int, int.coe_nat_zero, ← int.dvd_iff_mod_eq_zero, dvd_neg, int.coe_nat_dvd], have hxplt : (x : zmodp p hp).val < p := (x : zmodp p hp).2, have hxpltneg : (-x : zmodp p hp).val < p := (-x : zmodp p hp).2, have hneglt : ¬(x : zmodp q hq).val ≤ q / 2 → (x : zmodp q hq) ≠ 0 → (-x : zmodp q hq).val ≤ q / 2, from λ hx₁ hx0, by rwa [zmodp.le_div_two_iff_lt_neg hq hq1 hx0, not_lt] at hx₁, by split_ifs; simp [zmodp.eq_zero_iff_dvd_nat hq, (x : zmodp p hp).2, coprime_mul_iff_left, lt_succ_iff, h, *, hp.coprime_iff_not_dvd, hq.coprime_iff_not_dvd, (x : zmodp p hp).2, (-x : zmodp p hp).2] {contextual := tt}, prod_bij (λ x _, if (x : zmodp q hq).1 ≤ (q / 2) then ((x : zmodp p hp).val, (x : zmodp q hq).val) else ((-x : zmodp p hp).val, (-x : zmodp q hq).val)) hmem (λ a ha, by split_ifs; simp [*, prod.ext_iff] at *) hinj (surj_on_of_inj_on_of_card_le _ hmem hinj (@nat.le_of_add_le_add_right (q / 2 + (p / 2).succ) _ _ (calc card (finset.product (erase (range p) 0) (erase (range (succ (q / 2))) 0)) + (q / 2 + (p / 2).succ) = (p * q) / 2 + 1 : by rw [card_product, card_erase_of_mem (mem_range.2 hp.pos), card_erase_of_mem (mem_range.2 (succ_pos _)), card_range, card_range, pred_succ, ← add_assoc, ← succ_mul, succ_pred_eq_of_pos hp.pos, odd_mul_odd_div_two hp1 hq1, add_succ] ... = card (range (p * q / 2).succ) : by rw card_range ... = card ((range (p * q / 2).succ).filter (coprime (p * q)) ∪ ((range (p * q / 2).succ).filter (λ x, ¬coprime p x)).erase 0 ∪ (range (p * q / 2).succ).filter (λ x, ¬coprime q x)) : congr_arg card (by simp [finset.ext, coprime_mul_iff_left]; tauto) ... ≤ card ((range (p * q / 2).succ).filter (coprime (p * q))) + card (((range (p * q / 2).succ).filter (λ x, ¬coprime p x)).erase 0) + card ((range (p * q / 2).succ).filter (λ x, ¬coprime q x)) : le_trans (card_union_le _ _) (add_le_add_right (card_union_le _ _) _) ... = _ : by rw [card_erase_of_mem, card_range_p_mul_q_filter_not_coprime hp hq hp1 hq1 hpq, mul_comm p q, card_range_p_mul_q_filter_not_coprime hq hp hq1 hp1 hpq.symm, pred_succ, add_assoc]; simp [range_succ, hp.coprime_iff_not_dvd, hpq0]))) lemma prod_range_div_two_erase_zero : ((range (p / 2).succ).erase 0).prod (λ x, (x : zmodp p hp)) ^ 2 * (-1) ^ (p / 2) = -1 := have hcard : card (erase (range (succ (p / 2))) 0) = p / 2, by rw [card_erase_of_mem (mem_range.2 (succ_pos _)), card_range, pred_succ], have hp2 : p / 2 < p, from div_lt_self hp.pos dec_trivial, have h₁ : (range (p / 2).succ).erase 0 = ((range p).erase 0).filter (λ x, (x : zmodp p hp).val ≤ p / 2) := finset.ext.2 (λ a, ⟨λ h, mem_filter.2 $ by rw [mem_erase, mem_range, lt_succ_iff] at h; exact ⟨mem_erase.2 ⟨h.1, mem_range.2 (lt_of_le_of_lt h.2 hp2)⟩, by rw zmodp.val_cast_of_lt hp (lt_of_le_of_lt h.2 hp2); exact h.2⟩, λ h, mem_erase.2 ⟨by simp at h; tauto, by rw [mem_filter, mem_erase, mem_range] at h; rw [mem_range, lt_succ_iff, ← zmodp.val_cast_of_lt hp h.1.2]; exact h.2⟩⟩), have hmem : ∀ x ∈ (range (p / 2).succ).erase 0, x ≠ 0 ∧ x ≤ p / 2, from λ x hx, by simpa [lt_succ_iff] using hx, have hmemv : ∀ x ∈ (range (p / 2).succ).erase 0, (x : zmodp p hp).val = x, from λ x hx, zmodp.val_cast_of_lt hp (lt_of_le_of_lt (hmem x hx).2 hp2), have hmem0 : ∀ x ∈ (range (p / 2).succ).erase 0, (x : zmodp p hp) ≠ 0, from λ x hx, fin.ne_of_vne $ by simp [hmemv x hx, (hmem x hx).1], have hmem0' : ∀ x ∈ (range (p / 2).succ).erase 0, (-x : zmodp p hp) ≠ 0, from λ x hx, neg_ne_zero.2 (hmem0 x hx), have h₂ : ((range (p / 2).succ).erase 0).prod (λ x : ℕ, (x : zmodp p hp) * -1) = (((range p).erase 0).filter (λ x : ℕ, ¬(x : zmodp p hp).val ≤ p / 2)).prod (λ x, (x : zmodp p hp)) := prod_bij (λ a _, (-a : zmodp p hp).1) (λ a ha, mem_filter.2 ⟨mem_erase.2 ⟨fin.vne_of_ne (hmem0' a ha), mem_range.2 (-a : zmodp p hp).2⟩, by simp [zmodp.le_div_two_iff_lt_neg hp hp1 (hmem0' a ha), hmemv a ha, (hmem a ha).2]; tauto⟩) (by simp) (λ a₁ a₂ ha₁ ha₂ h, by rw [← hmemv a₁ ha₁, ← hmemv a₂ ha₂]; exact fin.veq_of_eq (by rw neg_inj (fin.eq_of_veq h))) (λ b hb, have hb' : (b ≠ 0 ∧ b < p) ∧ (¬(b : zmodp p hp).1 ≤ p / 2), by simpa using hb, have hbv : (b : zmodp p hp).1 = b, from zmodp.val_cast_of_lt hp hb'.1.2, have hb0 : (b : zmodp p hp) ≠ 0, from fin.ne_of_vne $ by simp [hbv, hb'.1.1], ⟨(-b : zmodp p hp).1, mem_erase.2 ⟨fin.vne_of_ne (neg_ne_zero.2 hb0 : _), mem_range.2 $ lt_succ_of_le $ by rw [← not_lt, ← zmodp.le_div_two_iff_lt_neg hp hp1 hb0]; exact hb'.2⟩, by simp [hbv]⟩), calc ((((range (p / 2).succ).erase 0).prod (λ x, (x : zmodp p hp)) ^ 2)) * (-1) ^ (p / 2) = ((range (p / 2).succ).erase 0).prod (λ x, (x : zmodp p hp)) * ((range (p / 2).succ).erase 0).prod (λ x, (x : zmodp p hp) * -1) : by rw prod_mul_distrib; simp [_root_.pow_two, hcard, mul_assoc] ... = (((range p).erase 0).filter (λ x : ℕ, (x : zmodp p hp).val ≤ p / 2)).prod (λ x, (x : zmodp p hp)) * (((range p).erase 0).filter (λ x : ℕ, ¬(x : zmodp p hp).val ≤ p / 2)).prod (λ x, (x : zmodp p hp)) : by rw [h₂, h₁] ... = ((range p).erase 0).prod (λ x, (x : zmodp p hp)) : begin rw ← prod_union, { exact finset.prod_congr (by simp [finset.ext, -not_lt, -not_le]; tauto) (λ _ _, rfl) }, { simp [finset.ext, -not_lt, - not_le]; tauto } end ... = -1 : by simp lemma range_p_product_range_q_div_two_prod : (((range p).erase 0).product ((range (q / 2).succ).erase 0)).prod (λ x, ((x.1 : zmodp p hp), (x.2 : zmodp q hq))) = ((-1) ^ (q / 2), (-1) ^ (p / 2) * (-1) ^ (p / 2 * (q / 2))) := have hcard : card (erase (range (succ (q / 2))) 0) = q / 2, by rw [card_erase_of_mem (mem_range.2 (succ_pos _)), card_range, pred_succ], have finset.prod (erase (range (succ (q / 2))) 0) (λ x : ℕ, (x : zmodp q hq)) ^ 2 = -((-1 : zmodp q hq) ^ (q / 2)), from (domain.mul_right_inj (show (-1 : zmodp q hq) ^ (q / 2) ≠ 0, from pow_ne_zero _ (neg_ne_zero.2 zero_ne_one.symm))).1 $ by rw [prod_range_div_two_erase_zero hq hp hq1 hp1 hpq.symm, ← neg_mul_eq_neg_mul, ← _root_.pow_add, ← two_mul, pow_mul, _root_.pow_two]; simp, have finset.prod (erase (range (succ (q / 2))) 0) (λ x, (x : zmodp q hq)) ^ card (erase (range p) 0) = (- 1) ^ (p / 2) * ((-1) ^ (p / 2 * (q / 2))), by rw [card_erase_of_mem (mem_range.2 hp.pos), card_range, pred_eq_sub_one, ← two_mul_odd_div_two hp1, pow_mul, this, mul_comm (p / 2), pow_mul, ← _root_.mul_pow]; simp, by simp [prod_product, (prod_mk_prod _ _ _).symm, prod_pow, prod_nat_pow, prod_const, *, zmodp.prod_range_prime_erase_zero hp] lemma prod_range_p_mul_q_div_two_ite_eq : ((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, if (x : zmodp q hq).1 ≤ (q / 2) then ((x : zmodp p hp), (x : zmodp q hq)) else -((x : zmodp p hp), (x : zmodp q hq))) = ((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, if (x : zmodp q hq).1 ≤ q / 2 then 1 else -1) * ((-1) ^ (q / 2) * q ^ (p / 2), (-1) ^ (p / 2) * p ^ (q / 2)) := calc ((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, if (x : zmodp q hq).1 ≤ (q / 2) then ((x : zmodp p hp), (x : zmodp q hq)) else -((x : zmodp p hp), (x : zmodp q hq))) = ((range ((p * q) / 2).succ).filter (coprime (p * q))).prod (λ x, (if (x : zmodp q hq).1 ≤ (q / 2) then 1 else -1) * ((x : zmodp p hp), (x : zmodp q hq))) : prod_congr rfl (λ _ _, by split_ifs; simp) ... = _ : by rw [prod_mul_distrib, ← prod_mk_prod, prod_hom (coe : ℕ → zmodp p hp), prod_range_p_mul_q_filter_coprime_mod_p hp hq hp1 hq1 hpq, prod_hom (coe : ℕ → zmodp q hq), mul_comm p q, prod_range_p_mul_q_filter_coprime_mod_p hq hp hq1 hp1 hpq.symm] end quadratic_reciprocity_aux open quadratic_reciprocity_aux variables {p q : ℕ} (hp : nat.prime p) (hq : nat.prime q) namespace zmodp def legendre_sym (a p : ℕ) (hp : nat.prime p) : ℤ := if (a : zmodp p hp) = 0 then 0 else if ∃ b : zmodp p hp, b ^ 2 = a then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) (hp : nat.prime p) : (legendre_sym a p hp : zmodp p hp) = (a ^ (p / 2)) := if ha : (a : zmodp p hp) = 0 then by simp [*, legendre_sym, _root_.zero_pow (nat.div_pos hp.ge_two (succ_pos 1))] else (nat.prime.eq_two_or_odd hp).elim (λ hp2, begin subst hp2, suffices : ∀ a : zmodp 2 nat.prime_two, (((ite (a = 0) 0 (ite (∃ (b : zmodp 2 hp), b ^ 2 = a) 1 (-1))) : ℤ) : zmodp 2 nat.prime_two) = a ^ (2 / 2), { exact this a }, exact dec_trivial, end) (λ hp1, have _ := euler_criterion hp ha, have (-1 : zmodp p hp) ≠ 1, from (ne_neg_self hp hp1 zero_ne_one.symm).symm, by cases zmodp.pow_div_two_eq_neg_one_or_one hp ha; simp [legendre_sym, *] at *) lemma legendre_sym_eq_one_or_neg_one (a : ℕ) (hp : nat.prime p) (ha : (a : zmodp p hp) ≠ 0) : legendre_sym a p hp = -1 ∨ legendre_sym a p hp = 1 := by unfold legendre_sym; split_ifs; simp * at * theorem quadratic_reciprocity (hp : nat.prime p) (hq : nat.prime q) (hp1 : p % 2 = 1) (hq1 : q % 2 = 1) (hpq : p ≠ q) : legendre_sym p q hq * legendre_sym q p hp = (-1) ^ ((p / 2) * (q / 2)) := have hneg_one_or_one : ((range (p * q / 2).succ).filter (coprime (p * q))).prod (λ (x : ℕ), if (x : zmodp q hq).val ≤ q / 2 then (1 : zmodp p hp × zmodp q hq) else -1) = 1 ∨ ((range (p * q / 2).succ).filter (coprime (p * q))).prod (λ (x : ℕ), if (x : zmodp q hq).val ≤ q / 2 then (1 : zmodp p hp × zmodp q hq) else -1) = -1 := finset.induction_on ((range (p * q / 2).succ).filter (coprime (p * q))) (or.inl rfl) (λ a s h, by simp [prod_insert h]; split_ifs; finish), have h : (((-1) ^ (q / 2), (-1) ^ (p / 2) * (-1) ^ (p / 2 * (q / 2))) : zmodp p hp × zmodp q hq) = ((-1) ^ (q / 2) * q ^ (p / 2), (-1) ^ (p / 2) * p ^ (q / 2)) ∨ (((-1) ^ (q / 2), (-1) ^ (p / 2) * (-1) ^ (p / 2 * (q / 2))) : zmodp p hp × zmodp q hq) = - ((-1) ^ (q / 2) * q ^ (p / 2), (-1) ^ (p / 2) * p ^ (q / 2)) := begin have := prod_filter_range_p_mul_q_div_two_eq_prod_product hp hq hp1 hq1 hpq, rw [prod_range_p_mul_q_div_two_ite_eq hp hq hp1 hq1 hpq, range_p_product_range_q_div_two_prod hp hq hp1 hq1 hpq] at this, cases hneg_one_or_one with h h; simp * at * end, begin have := ne_neg_self hp hp1 one_ne_zero, have := ne_neg_self hq hq1 one_ne_zero, generalize hnp : (-1 : ℤ) ^ (p / 2) = np, have hnpp : (-1 : zmodp q hq) ^ (p / 2) = np, by simp [hnp.symm], generalize hnq : (-1 : ℤ) ^ (q / 2) = nq, have hnqp : (-1 : zmodp p hp) ^ (q / 2) = nq, by simp [hnq.symm], have hnqq : (-1 : zmodp q hq) ^ (q / 2) = nq, by simp [hnq.symm], cases legendre_sym_eq_one_or_neg_one q hp (zmodp.prime_ne_zero hp hq hpq); cases legendre_sym_eq_one_or_neg_one p hq (zmodp.prime_ne_zero hq hp hpq.symm); cases @neg_one_pow_eq_or ℤ _ (p / 2); cases @neg_one_pow_eq_or ℤ _ (q / 2); simp [*, pow_mul, (legendre_sym_eq_pow p q hq).symm, (legendre_sym_eq_pow q p hp).symm, prod.ext_iff] at *; cc end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) (hq1 : q % 2 = 1) : (∃ a : zmodp p hp, a ^ 2 = q) ↔ ∃ b : zmodp q hq, b ^ 2 = p := if hpq : p = q then by subst hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin have := quadratic_reciprocity hp hq (odd_of_mod_four_eq_one hp1) hq1 hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg (zmodp.prime_ne_zero hp hq hpq), if_neg (zmodp.prime_ne_zero hq hp (ne.symm hpq))] at this, split_ifs at this; simp *; contradiction end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmodp p hp, a ^ 2 = q) ↔ ¬∃ b : zmodp q hq, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin have := quadratic_reciprocity hp hq (odd_of_mod_four_eq_three hp3) (odd_of_mod_four_eq_three hq3) hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg (zmodp.prime_ne_zero hp hq hpq), if_neg (zmodp.prime_ne_zero hq hp hpq.symm)] at this, split_ifs at this; simp *; contradiction end end zmodp
0e0e83c6d11a47e1543892e7fdf6123e1d6de8fb
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/real/irrational.lean
8918a53d16df51eba3786eb4e4cf1e82f525df60
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,059
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Yury Kudryashov -/ import data.real.sqrt import data.rat.sqrt import ring_theory.int.basic import data.polynomial.eval import data.polynomial.degree import tactic.interval_cases import ring_theory.algebraic /-! # Irrational real numbers In this file we define a predicate `irrational` on `ℝ`, prove that the `n`-th root of an integer number is irrational if it is not integer, and that `sqrt q` is irrational if and only if `rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q`. We also provide dot-style constructors like `irrational.add_rat`, `irrational.rat_sub` etc. -/ open rat real multiplicity /-- A real number is irrational if it is not equal to any rational number. -/ def irrational (x : ℝ) := x ∉ set.range (coe : ℚ → ℝ) lemma irrational_iff_ne_rational (x : ℝ) : irrational x ↔ ∀ a b : ℤ, x ≠ a / b := by simp only [irrational, rat.forall, cast_mk, not_exists, set.mem_range, cast_coe_int, cast_div, eq_comm] /-- A transcendental real number is irrational. -/ lemma transcendental.irrational {r : ℝ} (tr : transcendental ℚ r) : irrational r := by { rintro ⟨a, rfl⟩, exact tr (is_algebraic_algebra_map a) } /-! ### Irrationality of roots of integer and rational numbers -/ /-- If `x^n`, `n > 0`, is integer and is not the `n`-th power of an integer, then `x` is irrational. -/ theorem irrational_nrt_of_notint_nrt {x : ℝ} (n : ℕ) (m : ℤ) (hxr : x ^ n = m) (hv : ¬ ∃ y : ℤ, x = y) (hnpos : 0 < n) : irrational x := begin rintros ⟨⟨N, D, P, C⟩, rfl⟩, rw [← cast_pow] at hxr, have c1 : ((D : ℤ) : ℝ) ≠ 0, { rw [int.cast_ne_zero, int.coe_nat_ne_zero], exact ne_of_gt P }, have c2 : ((D : ℤ) : ℝ) ^ n ≠ 0 := pow_ne_zero _ c1, rw [num_denom', cast_pow, cast_mk, div_pow, div_eq_iff_mul_eq c2, ← int.cast_pow, ← int.cast_pow, ← int.cast_mul, int.cast_inj] at hxr, have hdivn : ↑D ^ n ∣ N ^ n := dvd.intro_left m hxr, rw [← int.dvd_nat_abs, ← int.coe_nat_pow, int.coe_nat_dvd, int.nat_abs_pow, nat.pow_dvd_pow_iff hnpos] at hdivn, have hD : D = 1 := by rw [← nat.gcd_eq_right hdivn, C.gcd_eq_one], subst D, refine hv ⟨N, _⟩, rw [num_denom', int.coe_nat_one, mk_eq_div, int.cast_one, div_one, cast_coe_int] end /-- If `x^n = m` is an integer and `n` does not divide the `multiplicity p m`, then `x` is irrational. -/ theorem irrational_nrt_of_n_not_dvd_multiplicity {x : ℝ} (n : ℕ) {m : ℤ} (hm : m ≠ 0) (p : ℕ) [hp : fact p.prime] (hxr : x ^ n = m) (hv : (multiplicity (p : ℤ) m).get (finite_int_iff.2 ⟨hp.1.ne_one, hm⟩) % n ≠ 0) : irrational x := begin rcases nat.eq_zero_or_pos n with rfl | hnpos, { rw [eq_comm, pow_zero, ← int.cast_one, int.cast_inj] at hxr, simpa [hxr, multiplicity.one_right (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.1.not_dvd_one)), nat.zero_mod] using hv }, refine irrational_nrt_of_notint_nrt _ _ hxr _ hnpos, rintro ⟨y, rfl⟩, rw [← int.cast_pow, int.cast_inj] at hxr, subst m, have : y ≠ 0, { rintro rfl, rw zero_pow hnpos at hm, exact hm rfl }, erw [multiplicity.pow' (nat.prime_iff_prime_int.1 hp.1) (finite_int_iff.2 ⟨hp.1.ne_one, this⟩), nat.mul_mod_right] at hv, exact hv rfl end theorem irrational_sqrt_of_multiplicity_odd (m : ℤ) (hm : 0 < m) (p : ℕ) [hp : fact p.prime] (Hpv : (multiplicity (p : ℤ) m).get (finite_int_iff.2 ⟨hp.1.ne_one, (ne_of_lt hm).symm⟩) % 2 = 1) : irrational (sqrt m) := @irrational_nrt_of_n_not_dvd_multiplicity _ 2 _ (ne.symm (ne_of_lt hm)) p hp (sq_sqrt (int.cast_nonneg.2 $ le_of_lt hm)) (by rw Hpv; exact one_ne_zero) theorem nat.prime.irrational_sqrt {p : ℕ} (hp : nat.prime p) : irrational (sqrt p) := @irrational_sqrt_of_multiplicity_odd p (int.coe_nat_pos.2 hp.pos) p ⟨hp⟩ $ by simp [multiplicity_self (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.not_dvd_one) : _)]; refl /-- **Irrationality of the Square Root of 2** -/ theorem irrational_sqrt_two : irrational (sqrt 2) := by simpa using nat.prime_two.irrational_sqrt theorem irrational_sqrt_rat_iff (q : ℚ) : irrational (sqrt q) ↔ rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q := if H1 : rat.sqrt q * rat.sqrt q = q then iff_of_false (not_not_intro ⟨rat.sqrt q, by rw [← H1, cast_mul, sqrt_mul_self (cast_nonneg.2 $ rat.sqrt_nonneg q), sqrt_eq, abs_of_nonneg (rat.sqrt_nonneg q)]⟩) (λ h, h.1 H1) else if H2 : 0 ≤ q then iff_of_true (λ ⟨r, hr⟩, H1 $ (exists_mul_self _).1 ⟨r, by rwa [eq_comm, sqrt_eq_iff_mul_self_eq (cast_nonneg.2 H2), ← cast_mul, rat.cast_inj] at hr; rw [← hr]; exact real.sqrt_nonneg _⟩) ⟨H1, H2⟩ else iff_of_false (not_not_intro ⟨0, by rw cast_zero; exact (sqrt_eq_zero_of_nonpos (rat.cast_nonpos.2 $ le_of_not_le H2)).symm⟩) (λ h, H2 h.2) instance (q : ℚ) : decidable (irrational (sqrt q)) := decidable_of_iff' _ (irrational_sqrt_rat_iff q) /-! ### Adding/subtracting/multiplying by rational numbers -/ lemma rat.not_irrational (q : ℚ) : ¬irrational q := λ h, h ⟨q, rfl⟩ namespace irrational variables (q : ℚ) {x y : ℝ} open_locale classical theorem add_cases : irrational (x + y) → irrational x ∨ irrational y := begin delta irrational, contrapose!, rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩, exact ⟨rx + ry, cast_add rx ry⟩ end theorem of_rat_add (h : irrational (q + x)) : irrational x := h.add_cases.elim (λ h, absurd h q.not_irrational) id theorem rat_add (h : irrational x) : irrational (q + x) := of_rat_add (-q) $ by rwa [cast_neg, neg_add_cancel_left] theorem of_add_rat : irrational (x + q) → irrational x := add_comm ↑q x ▸ of_rat_add q theorem add_rat (h : irrational x) : irrational (x + q) := add_comm ↑q x ▸ h.rat_add q theorem of_neg (h : irrational (-x)) : irrational x := λ ⟨q, hx⟩, h ⟨-q, by rw [cast_neg, hx]⟩ protected theorem neg (h : irrational x) : irrational (-x) := of_neg $ by rwa neg_neg theorem sub_rat (h : irrational x) : irrational (x - q) := by simpa only [sub_eq_add_neg, cast_neg] using h.add_rat (-q) theorem rat_sub (h : irrational x) : irrational (q - x) := by simpa only [sub_eq_add_neg] using h.neg.rat_add q theorem of_sub_rat (h : irrational (x - q)) : irrational x := (of_add_rat (-q) $ by simpa only [cast_neg, sub_eq_add_neg] using h) theorem of_rat_sub (h : irrational (q - x)) : irrational x := of_neg (of_rat_add q (by simpa only [sub_eq_add_neg] using h)) theorem mul_cases : irrational (x * y) → irrational x ∨ irrational y := begin delta irrational, contrapose!, rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩, exact ⟨rx * ry, cast_mul rx ry⟩ end theorem of_mul_rat (h : irrational (x * q)) : irrational x := h.mul_cases.elim id (λ h, absurd h q.not_irrational) theorem mul_rat (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (x * q) := of_mul_rat q⁻¹ $ by rwa [mul_assoc, ← cast_mul, mul_inv_cancel hq, cast_one, mul_one] theorem of_rat_mul : irrational (q * x) → irrational x := mul_comm x q ▸ of_mul_rat q theorem rat_mul (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (q * x) := mul_comm x q ▸ h.mul_rat hq theorem of_mul_self (h : irrational (x * x)) : irrational x := h.mul_cases.elim id id theorem of_inv (h : irrational x⁻¹) : irrational x := λ ⟨q, hq⟩, h $ hq ▸ ⟨q⁻¹, q.cast_inv⟩ protected theorem inv (h : irrational x) : irrational x⁻¹ := of_inv $ by rwa inv_inv' theorem div_cases (h : irrational (x / y)) : irrational x ∨ irrational y := h.mul_cases.imp id of_inv theorem of_rat_div (h : irrational (q / x)) : irrational x := (h.of_rat_mul q).of_inv theorem of_one_div (h : irrational (1 / x)) : irrational x := of_rat_div 1 $ by rwa [cast_one] theorem of_pow : ∀ n : ℕ, irrational (x^n) → irrational x | 0 := λ h, by { rw pow_zero at h, exact (h ⟨1, cast_one⟩).elim } | (n+1) := λ h, by { rw pow_succ at h, exact h.mul_cases.elim id (of_pow n) } theorem of_fpow : ∀ m : ℤ, irrational (x^m) → irrational x | (n:ℕ) := of_pow n | -[1+n] := λ h, by { rw gpow_neg_succ_of_nat at h, exact h.of_inv.of_pow _ } end irrational section polynomial open polynomial variables (x : ℝ) (p : polynomial ℤ) lemma one_lt_nat_degree_of_irrational_root (hx : irrational x) (p_nonzero : p ≠ 0) (x_is_root : aeval x p = 0) : 1 < p.nat_degree := begin by_contra rid, rcases exists_eq_X_add_C_of_nat_degree_le_one (not_lt.1 rid) with ⟨a, b, rfl⟩, clear rid, have : (a : ℝ) * x = -b, by simpa [eq_neg_iff_add_eq_zero] using x_is_root, rcases em (a = 0) with (rfl|ha), { obtain rfl : b = 0, by simpa, simpa using p_nonzero }, { rw [mul_comm, ← eq_div_iff_mul_eq, eq_comm] at this, refine hx ⟨-b / a, _⟩, assumption_mod_cast, assumption_mod_cast } end end polynomial section variables {q : ℚ} {x : ℝ} open irrational @[simp] theorem irrational_rat_add_iff : irrational (q + x) ↔ irrational x := ⟨of_rat_add q, rat_add q⟩ @[simp] theorem irrational_add_rat_iff : irrational (x + q) ↔ irrational x := ⟨of_add_rat q, add_rat q⟩ @[simp] theorem irrational_rat_sub_iff : irrational (q - x) ↔ irrational x := ⟨of_rat_sub q, rat_sub q⟩ @[simp] theorem irrational_sub_rat_iff : irrational (x - q) ↔ irrational x := ⟨of_sub_rat q, sub_rat q⟩ @[simp] theorem irrational_neg_iff : irrational (-x) ↔ irrational x := ⟨of_neg, irrational.neg⟩ @[simp] theorem irrational_inv_iff : irrational x⁻¹ ↔ irrational x := ⟨of_inv, irrational.inv⟩ /-- There is an irrational number `r` between any two reals `x < r < y`. -/ theorem exists_irrational_btwn {x y : ℝ} (h : x < y) : ∃ r, irrational r ∧ x < r ∧ r < y := let ⟨q, ⟨hq1, hq2⟩⟩ := (exists_rat_btwn ((sub_lt_sub_iff_right (real.sqrt 2)).mpr h)) in ⟨q + real.sqrt 2, irrational_sqrt_two.rat_add _, sub_lt_iff_lt_add.mp hq1, lt_sub_iff_add_lt.mp hq2⟩ end
dcfda72e21ee9ff111a5d423a4528f992f1f1466
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/inequality/13.lean
2e254779297ad1f5fcb8cfc1468430217c73f46e
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
185
lean
theorem not_succ_le_self (a : mynat) : ¬ (succ a ≤ a) := begin intro h, have hsz := le_antisymm (succ a) a h (le_succ_self a), symmetry at hsz, exact ne_succ_self a hsz, end
18b714d1466514422f6cf76812fc23ffdbbd3bbe
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/multiset/erase_dup.lean
e7d03d0548afa768427444e54728de59a29fb2da
[ "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
3,697
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.multiset.nodup /-! # Erasing duplicates in a multiset. -/ namespace multiset open list variables {α β : Type*} [decidable_eq α] /-! ### erase_dup -/ /-- `erase_dup s` removes duplicates from `s`, yielding a `nodup` multiset. -/ def erase_dup (s : multiset α) : multiset α := quot.lift_on s (λ l, (l.erase_dup : multiset α)) (λ s t p, quot.sound p.erase_dup) @[simp] theorem coe_erase_dup (l : list α) : @erase_dup α _ l = l.erase_dup := rfl @[simp] theorem erase_dup_zero : @erase_dup α _ 0 = 0 := rfl @[simp] theorem mem_erase_dup {a : α} {s : multiset α} : a ∈ erase_dup s ↔ a ∈ s := quot.induction_on s $ λ l, mem_erase_dup @[simp] theorem erase_dup_cons_of_mem {a : α} {s : multiset α} : a ∈ s → erase_dup (a ::ₘ s) = erase_dup s := quot.induction_on s $ λ l m, @congr_arg _ _ _ _ coe $ erase_dup_cons_of_mem m @[simp] theorem erase_dup_cons_of_not_mem {a : α} {s : multiset α} : a ∉ s → erase_dup (a ::ₘ s) = a ::ₘ erase_dup s := quot.induction_on s $ λ l m, congr_arg coe $ erase_dup_cons_of_not_mem m theorem erase_dup_le (s : multiset α) : erase_dup s ≤ s := quot.induction_on s $ λ l, (erase_dup_sublist _).subperm theorem erase_dup_subset (s : multiset α) : erase_dup s ⊆ s := subset_of_le $ erase_dup_le _ theorem subset_erase_dup (s : multiset α) : s ⊆ erase_dup s := λ a, mem_erase_dup.2 @[simp] theorem erase_dup_subset' {s t : multiset α} : erase_dup s ⊆ t ↔ s ⊆ t := ⟨subset.trans (subset_erase_dup _), subset.trans (erase_dup_subset _)⟩ @[simp] theorem subset_erase_dup' {s t : multiset α} : s ⊆ erase_dup t ↔ s ⊆ t := ⟨λ h, subset.trans h (erase_dup_subset _), λ h, subset.trans h (subset_erase_dup _)⟩ @[simp] theorem nodup_erase_dup (s : multiset α) : nodup (erase_dup s) := quot.induction_on s nodup_erase_dup theorem erase_dup_eq_self {s : multiset α} : erase_dup s = s ↔ nodup s := ⟨λ e, e ▸ nodup_erase_dup s, quot.induction_on s $ λ l h, congr_arg coe h.erase_dup⟩ alias erase_dup_eq_self ↔ _ multiset.nodup.erase_dup alias erase_dup_eq_self ↔ _ multiset.nodup.erase_dup theorem erase_dup_eq_zero {s : multiset α} : erase_dup s = 0 ↔ s = 0 := ⟨λ h, eq_zero_of_subset_zero $ h ▸ subset_erase_dup _, λ h, h.symm ▸ erase_dup_zero⟩ @[simp] theorem erase_dup_singleton {a : α} : erase_dup ({a} : multiset α) = {a} := (nodup_singleton _).erase_dup theorem le_erase_dup {s t : multiset α} : s ≤ erase_dup t ↔ s ≤ t ∧ nodup s := ⟨λ h, ⟨le_trans h (erase_dup_le _), nodup_of_le h (nodup_erase_dup _)⟩, λ ⟨l, d⟩, (le_iff_subset d).2 $ subset.trans (subset_of_le l) (subset_erase_dup _)⟩ theorem erase_dup_ext {s t : multiset α} : erase_dup s = erase_dup t ↔ ∀ a, a ∈ s ↔ a ∈ t := by simp [nodup_ext] theorem erase_dup_map_erase_dup_eq [decidable_eq β] (f : α → β) (s : multiset α) : erase_dup (map f (erase_dup s)) = erase_dup (map f s) := by simp [erase_dup_ext] @[simp] lemma erase_dup_nsmul {s : multiset α} {n : ℕ} (h0 : n ≠ 0) : (n • s).erase_dup = s.erase_dup := begin ext a, by_cases h : a ∈ s; simp [h,h0] end lemma nodup.le_erase_dup_iff_le {s t : multiset α} (hno : s.nodup) : s ≤ t.erase_dup ↔ s ≤ t := by simp [le_erase_dup, hno] end multiset lemma multiset.nodup.le_nsmul_iff_le {α : Type*} {s t : multiset α} {n : ℕ} (h : s.nodup) (hn : n ≠ 0) : s ≤ n • t ↔ s ≤ t := begin classical, rw [← h.le_erase_dup_iff_le, iff.comm, ← h.le_erase_dup_iff_le], simp [hn] end
194ca5120bcc44380809caa8299cc39a11d8b318
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Elab/PreDefinition/WF/Eqns.lean
bc34de8a7ade950b4dabd7cb0bff98526649adf1
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
10,009
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Rewrite import Lean.Meta.Tactic.Split import Lean.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.Eqns namespace Lean.Elab.WF open Meta open Eqns structure EqnInfo extends EqnInfoCore where declNames : Array Name declNameNonRec : Name fixedPrefixSize : Nat deriving Inhabited private partial def deltaLHSUntilFix (mvarId : MVarId) : MetaM MVarId := mvarId.withContext do let target ← mvarId.getType' let some (_, lhs, _) := target.eq? | throwTacticEx `deltaLHSUntilFix mvarId "equality expected" if lhs.isAppOf ``WellFounded.fix then return mvarId else deltaLHSUntilFix (← deltaLHS mvarId) private def rwFixEq (mvarId : MVarId) : MetaM MVarId := mvarId.withContext do let target ← mvarId.getType' let some (_, lhs, rhs) := target.eq? | unreachable! let h := mkAppN (mkConst ``WellFounded.fix_eq lhs.getAppFn.constLevels!) lhs.getAppArgs let some (_, _, lhsNew) := (← inferType h).eq? | unreachable! let targetNew ← mkEq lhsNew rhs let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew mvarId.assign (← mkEqTrans h mvarNew) return mvarNew.mvarId! private def hasWellFoundedFix (e : Expr) : Bool := Option.isSome <| e.find? (·.isConstOf ``WellFounded.fix) /-- Helper function for decoding the packed argument for a `WellFounded.fix` application. Recall that we use `PSum` and `PSigma` for packing the arguments of mutually recursive nary functions. -/ private partial def decodePackedArg? (info : EqnInfo) (e : Expr) : Option (Name × Array Expr) := do if info.declNames.size == 1 then let args := decodePSigma e #[] return (info.declNames[0]!, args) else decodePSum? e 0 where decodePSum? (e : Expr) (i : Nat) : Option (Name × Array Expr) := do if e.isAppOfArity ``PSum.inl 3 then decodePSum? e.appArg! i else if e.isAppOfArity ``PSum.inr 3 then decodePSum? e.appArg! (i+1) else guard (i < info.declNames.size) return (info.declNames[i]!, decodePSigma e #[]) decodePSigma (e : Expr) (acc : Array Expr) : Array Expr := /- TODO: check arity of the given function. If it takes a PSigma as the last argument, this function will produce incorrect results. -/ if e.isAppOfArity ``PSigma.mk 4 then decodePSigma e.appArg! (acc.push e.appFn!.appArg!) else acc.push e /-- Try to fold `WellFounded.fix` applications that represent recursive applications of the functions in `info.declNames`. We need that to make sure `simpMatchWF?` succeeds at goals such as ```lean ... h : g x = 0 ... |- (match (WellFounded.fix ...) with | ...) = ... ``` where `WellFounded.fix ...` can be folded back to `g x`. -/ private def tryToFoldWellFoundedFix (info : EqnInfo) (us : List Level) (fixedPrefix : Array Expr) (e : Expr) : MetaM Expr := do if hasWellFoundedFix e then transform e (pre := pre) else return e where pre (e : Expr) : MetaM TransformStep := do let e' := e.headBeta if e'.isAppOf ``WellFounded.fix && e'.getAppNumArgs >= 6 then let args := e'.getAppArgs let packedArg := args[5]! let extraArgs := args[6:] if let some (declName, args) := decodePackedArg? info packedArg then let candidate := mkAppN (mkAppN (mkAppN (mkConst declName us) fixedPrefix) args) extraArgs trace[Elab.definition.wf] "found nested WF at discr {candidate}" if (← withDefault <| isDefEq candidate e) then return TransformStep.visit candidate return TransformStep.visit e /-- Simplify `match`-expressions when trying to prove equation theorems for a recursive declaration defined using well-founded recursion. It is similar to `simpMatch?`, but is also tries to fold `WellFounded.fix` applications occurring in discriminants. See comment at `tryToFoldWellFoundedFix`. -/ def simpMatchWF? (info : EqnInfo) (us : List Level) (fixedPrefix : Array Expr) (mvarId : MVarId) : MetaM (Option MVarId) := mvarId.withContext do let target ← instantiateMVars (← mvarId.getType) let targetNew ← Simp.main target (← Split.getSimpMatchContext) (methods := { pre }) let mvarIdNew ← applySimpResultToTarget mvarId target targetNew if mvarId != mvarIdNew then return some mvarIdNew else return none where pre (e : Expr) : SimpM Simp.Step := do let some app ← matchMatcherApp? e | return Simp.Step.visit { expr := e } if app.discrs.any hasWellFoundedFix then let discrsNew ← app.discrs.mapM (tryToFoldWellFoundedFix info us fixedPrefix ·) if discrsNew != app.discrs then let app := { app with discrs := discrsNew } let eNew := app.toExpr trace[Elab.definition.wf] "folded discriminants {indentExpr eNew}" return Simp.Step.visit { expr := app.toExpr } -- First try to reduce matcher match (← reduceRecMatcher? e) with | some e' => return Simp.Step.done { expr := e' } | none => match (← Simp.simpMatchCore? app e SplitIf.discharge?) with | some r => return r | none => return Simp.Step.visit { expr := e } private def tryToFoldLHS? (info : EqnInfo) (us : List Level) (fixedPrefix : Array Expr) (mvarId : MVarId) : MetaM (Option MVarId) := mvarId.withContext do let target ← mvarId.getType' let some (_, lhs, rhs) := target.eq? | unreachable! let lhsNew ← tryToFoldWellFoundedFix info us fixedPrefix lhs if lhs == lhsNew then return none let targetNew ← mkEq lhsNew rhs let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew mvarId.assign mvarNew return mvarNew.mvarId! /-- Given a goal of the form `|- f.{us} a_1 ... a_n b_1 ... b_m = ...`, return `(us, #[a_1, ..., a_n])` where `f` is a constant named `declName`, and `n = info.fixedPrefixSize`. -/ private def getFixedPrefix (declName : Name) (info : EqnInfo) (mvarId : MVarId) : MetaM (List Level × Array Expr) := mvarId.withContext do let target ← mvarId.getType' let some (_, lhs, _) := target.eq? | unreachable! let lhsArgs := lhs.getAppArgs if lhsArgs.size < info.fixedPrefixSize || !lhs.getAppFn matches .const .. then throwError "failed to generate equational theorem for '{declName}', unexpected number of arguments in the equation left-hand-side\n{mvarId}" let result := lhsArgs[:info.fixedPrefixSize] trace[Elab.definition.wf.eqns] "fixedPrefix: {result}" return (lhs.getAppFn.constLevels!, result) private partial def mkProof (declName : Name) (info : EqnInfo) (type : Expr) : MetaM Expr := do trace[Elab.definition.wf.eqns] "proving: {type}" withNewMCtxDepth do let main ← mkFreshExprSyntheticOpaqueMVar type let (_, mvarId) ← main.mvarId!.intros let (us, fixedPrefix) ← getFixedPrefix declName info mvarId let rec go (mvarId : MVarId) : MetaM Unit := do trace[Elab.definition.wf.eqns] "step\n{MessageData.ofGoal mvarId}" if (← tryURefl mvarId) then return () else if (← tryContradiction mvarId) then return () else if let some mvarId ← simpMatchWF? info us fixedPrefix mvarId then go mvarId else if let some mvarId ← simpIf? mvarId then go mvarId else if let some mvarId ← whnfReducibleLHS? mvarId then go mvarId else match (← simpTargetStar mvarId { config.dsimp := false }) with | TacticResultCNM.closed => return () | TacticResultCNM.modified mvarId => go mvarId | TacticResultCNM.noChange => if let some mvarIds ← casesOnStuckLHS? mvarId then mvarIds.forM go else if let some mvarIds ← splitTarget? mvarId then mvarIds.forM go else if let some mvarId ← tryToFoldLHS? info us fixedPrefix mvarId then go mvarId else throwError "failed to generate equational theorem for '{declName}'\n{MessageData.ofGoal mvarId}" go (← rwFixEq (← deltaLHSUntilFix mvarId)) instantiateMVars main def mkEqns (declName : Name) (info : EqnInfo) : MetaM (Array Name) := withOptions (tactic.hygienic.set · false) do let baseName := mkPrivateName (← getEnv) declName let eqnTypes ← withNewMCtxDepth <| lambdaTelescope info.value fun xs body => do let us := info.levelParams.map mkLevelParam let target ← mkEq (mkAppN (Lean.mkConst declName us) xs) body let goal ← mkFreshExprSyntheticOpaqueMVar target mkEqnTypes info.declNames goal.mvarId! let mut thmNames := #[] for i in [: eqnTypes.size] do let type := eqnTypes[i]! trace[Elab.definition.wf.eqns] "{eqnTypes[i]!}" let name := baseName ++ (`_eq).appendIndexAfter (i+1) thmNames := thmNames.push name let value ← mkProof declName info type let (type, value) ← removeUnusedEqnHypotheses type value addDecl <| Declaration.thmDecl { name, type, value levelParams := info.levelParams } return thmNames builtin_initialize eqnInfoExt : MapDeclarationExtension EqnInfo ← mkMapDeclarationExtension `wfEqInfo def registerEqnsInfo (preDefs : Array PreDefinition) (declNameNonRec : Name) (fixedPrefixSize : Nat) : CoreM Unit := do let declNames := preDefs.map (·.declName) modifyEnv fun env => preDefs.foldl (init := env) fun env preDef => eqnInfoExt.insert env preDef.declName { preDef with declNames, declNameNonRec, fixedPrefixSize } def getEqnsFor? (declName : Name) : MetaM (Option (Array Name)) := do if let some info := eqnInfoExt.find? (← getEnv) declName then mkEqns declName info else return none def getUnfoldFor? (declName : Name) : MetaM (Option Name) := do let env ← getEnv Eqns.getUnfoldFor? declName fun _ => eqnInfoExt.find? env declName |>.map (·.toEqnInfoCore) builtin_initialize registerGetEqnsFn getEqnsFor? registerGetUnfoldEqnFn getUnfoldFor? registerTraceClass `Elab.definition.wf.eqns end Lean.Elab.WF
cdfea34dcf315ca6dc84f685d35cc3b3def5f623
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/fiber_bundle.lean
24948499620b2f543e08c10f698ea2fcd8ffd33d
[ "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
58,805
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 topology.local_homeomorph import topology.algebra.ordered.basic import data.bundle /-! # Fiber bundles A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. We define a predicate `is_topological_fiber_bundle F p` saying that `p : Z → B` is a topological fiber bundle with fiber `F`. It is in general nontrivial to construct a fiber bundle. A way is to start from the knowledge of how changes of local trivializations act on the fiber. From this, one can construct the total space of the bundle and its topology by a suitable gluing construction. The main content of this file is an implementation of this construction: starting from an object of type `topological_fiber_bundle_core` registering the trivialization changes, one gets the corresponding fiber bundle and projection. Similarly we implement the object `topological_fiber_prebundle` which allows to define a topological fiber bundle from trivializations given as local equivalences with minimum additional properties. ## Main definitions ### Basic definitions * `trivialization F p` : structure extending local homeomorphisms, defining a local trivialization of a topological space `Z` with projection `p` and fiber `F`. * `is_topological_fiber_bundle F p` : Prop saying that the map `p` between topological spaces is a fiber bundle with fiber `F`. * `is_trivial_topological_fiber_bundle F p` : Prop saying that the map `p : Z → B` between topological spaces is a trivial topological fiber bundle, i.e., there exists a homeomorphism `h : Z ≃ₜ B × F` such that `proj x = (h x).1`. ### Operations on bundles We provide the following operations on `trivialization`s. * `trivialization.comap`: given a local trivialization `e` of a fiber bundle `p : Z → B`, a continuous map `f : B' → B` and a point `b' : B'` such that `f b' ∈ e.base_set`, `e.comap f hf b' hb'` is a trivialization of the pullback bundle. The pullback bundle (a.k.a., the induced bundle) has total space `{(x, y) : B' × Z | f x = p y}`, and is given by `λ ⟨(x, y), h⟩, x`. * `is_topological_fiber_bundle.comap`: if `p : Z → B` is a topological fiber bundle, then its pullback along a continuous map `f : B' → B` is a topological fiber bundle as well. * `trivialization.comp_homeomorph`: given a local trivialization `e` of a fiber bundle `p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle `p ∘ h`. * `is_topological_fiber_bundle.comp_homeomorph`: if `p : Z → B` is a topological fiber bundle and `h : Z' ≃ₜ Z` is a homeomorphism, then `p ∘ h : Z' → B` is a topological fiber bundle with the same fiber. ### Construction of a bundle from trivializations * `bundle.total_space E` is a type synonym for `Σ (x : B), E x`, that we can endow with a suitable topology. * `topological_fiber_bundle_core ι B F` : structure registering how changes of coordinates act on the fiber `F` above open subsets of `B`, where local trivializations are indexed by `ι`. Let `Z : topological_fiber_bundle_core ι B F`. Then we define * `Z.fiber x` : the fiber above `x`, homeomorphic to `F` (and defeq to `F` as a type). * `Z.total_space` : the total space of `Z`, defined as a `Type` as `Σ (b : B), F`, but with a twisted topology coming from the fiber bundle structure. It is (reducibly) the same as `bundle.total_space Z.fiber`. * `Z.proj` : projection from `Z.total_space` to `B`. It is continuous. * `Z.local_triv i`: for `i : ι`, bundle trivialization above the set `Z.base_set i`, which is an open set in `B`. * `pretrivialization F proj` : trivialization as a local equivalence, mainly used when the topology on the total space has not yet been defined. * `topological_fiber_prebundle F proj` : structure registering a cover of prebundle trivializations and requiring that the relative transition maps are local homeomorphisms. * `topological_fiber_prebundle.total_space_topology a` : natural topology of the total space, making the prebundle into a bundle. ## Implementation notes A topological fiber bundle with fiber `F` over a base `B` is a family of spaces isomorphic to `F`, indexed by `B`, which is locally trivial in the following sense: there is a covering of `B` by open sets such that, on each such open set `s`, the bundle is isomorphic to `s × F`. To construct a fiber bundle formally, the main data is what happens when one changes trivializations from `s × F` to `s' × F` on `s ∩ s'`: one should get a family of homeomorphisms of `F`, depending continuously on the base point, satisfying basic compatibility conditions (cocycle property). Useful classes of bundles can then be specified by requiring that these homeomorphisms of `F` belong to some subgroup, preserving some structure (the "structure group of the bundle"): then these structures are inherited by the fibers of the bundle. Given such trivialization change data (encoded below in a structure called `topological_fiber_bundle_core`), one can construct the fiber bundle. The intrinsic canonical mathematical construction is the following. The fiber above `x` is the disjoint union of `F` over all trivializations, modulo the gluing identifications: one gets a fiber which is isomorphic to `F`, but non-canonically (each choice of one of the trivializations around `x` gives such an isomorphism). Given a trivialization over a set `s`, one gets an isomorphism between `s × F` and `proj^{-1} s`, by using the identification corresponding to this trivialization. One chooses the topology on the bundle that makes all of these into homeomorphisms. For the practical implementation, it turns out to be more convenient to avoid completely the gluing and quotienting construction above, and to declare above each `x` that the fiber is `F`, but thinking that it corresponds to the `F` coming from the choice of one trivialization around `x`. This has several practical advantages: * without any work, one gets a topological space structure on the fiber. And if `F` has more structure it is inherited for free by the fiber. * In the case of the tangent bundle of manifolds, this implies that on vector spaces the derivative (from `F` to `F`) and the manifold derivative (from `tangent_space I x` to `tangent_space I' (f x)`) are equal. A drawback is that some silly constructions will typecheck: in the case of the tangent bundle, one can add two vectors in different tangent spaces (as they both are elements of `F` from the point of view of Lean). To solve this, one could mark the tangent space as irreducible, but then one would lose the identification of the tangent space to `F` with `F`. There is however a big advantage of this situation: even if Lean can not check that two basepoints are defeq, it will accept the fact that the tangent spaces are the same. For instance, if two maps `f` and `g` are locally inverse to each other, one can express that the composition of their derivatives is the identity of `tangent_space I x`. One could fear issues as this composition goes from `tangent_space I x` to `tangent_space I (g (f x))` (which should be the same, but should not be obvious to Lean as it does not know that `g (f x) = x`). As these types are the same to Lean (equal to `F`), there are in fact no dependent type difficulties here! For this construction of a fiber bundle from a `topological_fiber_bundle_core`, we should thus choose for each `x` one specific trivialization around it. We include this choice in the definition of the `topological_fiber_bundle_core`, as it makes some constructions more functorial and it is a nice way to say that the trivializations cover the whole space `B`. With this definition, the type of the fiber bundle space constructed from the core data is just `Σ (b : B), F `, but the topology is not the product one, in general. We also take the indexing type (indexing all the trivializations) as a parameter to the fiber bundle core: it could always be taken as a subtype of all the maps from open subsets of `B` to continuous maps of `F`, but in practice it will sometimes be something else. For instance, on a manifold, one will use the set of charts as a good parameterization for the trivializations of the tangent bundle. Or for the pullback of a `topological_fiber_bundle_core`, the indexing type will be the same as for the initial bundle. ## Tags Fiber bundle, topological bundle, local trivialization, structure group -/ variables {ι : Type*} {B : Type*} {F : Type*} open topological_space filter set open_locale topological_space classical /-! ### General definition of topological fiber bundles -/ section topological_fiber_bundle variables (F) {Z : Type*} [topological_space B] [topological_space F] {proj : Z → B} /-- This structure contains the information left for a local trivialization (which is implemented below as `trivialization F proj`) if the total space has not been given a topology, but we have a topology on both the fiber and the base space. Through the construction `topological_fiber_prebundle F proj` it will be possible to promote a `pretrivialization F proj` to a `trivialization F proj`. -/ @[nolint has_inhabited_instance] structure topological_fiber_bundle.pretrivialization (proj : Z → B) extends local_equiv Z (B × F) := (open_target : is_open target) (base_set : set B) (open_base_set : is_open base_set) (source_eq : source = proj ⁻¹' base_set) (target_eq : target = base_set ×ˢ (univ : set F)) (proj_to_fun : ∀ p ∈ source, (to_fun p).1 = proj p) open topological_fiber_bundle namespace topological_fiber_bundle.pretrivialization instance : has_coe_to_fun (pretrivialization F proj) (λ _, Z → (B × F)) := ⟨λ e, e.to_fun⟩ variables {F} (e : pretrivialization F proj) {x : Z} @[simp, mfld_simps] lemma coe_coe : ⇑e.to_local_equiv = e := rfl @[simp, mfld_simps] lemma coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex lemma mem_source : x ∈ e.source ↔ proj x ∈ e.base_set := by rw [e.source_eq, mem_preimage] lemma coe_fst' (ex : proj x ∈ e.base_set) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex) protected lemma eq_on : eq_on (prod.fst ∘ e) proj e.source := λ x hx, e.coe_fst hx lemma mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst ex).symm rfl lemma mk_proj_snd' (ex : proj x ∈ e.base_set) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst' ex).symm rfl /-- Composition of inverse and coercion from the subtype of the target. -/ def set_symm : e.target → Z := set.restrict e.to_local_equiv.symm e.target lemma mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set := by rw [e.target_eq, prod_univ, mem_preimage] lemma proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.to_local_equiv.symm x) = x.1 := begin have := (e.coe_fst (e.to_local_equiv.map_target hx)).symm, rwa [← e.coe_coe, e.to_local_equiv.right_inv hx] at this end lemma proj_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) : proj (e.to_local_equiv.symm (b, x)) = b := e.proj_symm_apply (e.mem_target.2 hx) lemma proj_surj_on_base_set [nonempty F] : set.surj_on proj e.source e.base_set := λ b hb, let ⟨y⟩ := ‹nonempty F› in ⟨e.to_local_equiv.symm (b, y), e.to_local_equiv.map_target $ e.mem_target.2 hb, e.proj_symm_apply' hb⟩ lemma apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.to_local_equiv.symm x) = x := e.to_local_equiv.right_inv hx lemma apply_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) : e (e.to_local_equiv.symm (b, x)) = (b, x) := e.apply_symm_apply (e.mem_target.2 hx) @[simp, mfld_simps] lemma symm_apply_mk_proj {x : Z} (ex : x ∈ e.source) : e.to_local_equiv.symm (proj x, (e x).2) = x := by rw [← e.coe_fst ex, prod.mk.eta, ← e.coe_coe, e.to_local_equiv.left_inv ex] @[simp, mfld_simps] lemma preimage_symm_proj_base_set : (e.to_local_equiv.symm ⁻¹' (proj ⁻¹' e.base_set)) ∩ e.target = e.target := begin refine inter_eq_right_iff_subset.mpr (λ x hx, _), simp only [mem_preimage, local_equiv.inv_fun_as_coe, e.proj_symm_apply hx], exact e.mem_target.mp hx, end @[simp, mfld_simps] lemma preimage_symm_proj_inter (s : set B) : (e.to_local_equiv.symm ⁻¹' (proj ⁻¹' s)) ∩ e.base_set ×ˢ (univ : set F) = (s ∩ e.base_set) ×ˢ (univ : set F) := begin ext ⟨x, y⟩, suffices : x ∈ e.base_set → (proj (e.to_local_equiv.symm (x, y)) ∈ s ↔ x ∈ s), by simpa only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ, and.congr_left_iff], intro h, rw [e.proj_symm_apply' h] end end topological_fiber_bundle.pretrivialization variable [topological_space Z] /-- A structure extending local homeomorphisms, defining a local trivialization of a projection `proj : Z → B` with fiber `F`, as a local homeomorphism between `Z` and `B × F` defined between two sets of the form `proj ⁻¹' base_set` and `base_set × F`, acting trivially on the first coordinate. -/ @[nolint has_inhabited_instance] structure topological_fiber_bundle.trivialization (proj : Z → B) extends local_homeomorph Z (B × F) := (base_set : set B) (open_base_set : is_open base_set) (source_eq : source = proj ⁻¹' base_set) (target_eq : target = base_set ×ˢ (univ : set F)) (proj_to_fun : ∀ p ∈ source, (to_local_homeomorph p).1 = proj p) open topological_fiber_bundle namespace topological_fiber_bundle.trivialization variables {F} (e : trivialization F proj) {x : Z} /-- Natural identification as a `pretrivialization`. -/ def to_pretrivialization : topological_fiber_bundle.pretrivialization F proj := { ..e } instance : has_coe_to_fun (trivialization F proj) (λ _, Z → B × F) := ⟨λ e, e.to_fun⟩ instance : has_coe (trivialization F proj) (pretrivialization F proj) := ⟨to_pretrivialization⟩ @[simp, mfld_simps] lemma coe_coe : ⇑e.to_local_homeomorph = e := rfl @[simp, mfld_simps] lemma coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex protected lemma eq_on : eq_on (prod.fst ∘ e) proj e.source := λ x hx, e.coe_fst hx lemma mem_source : x ∈ e.source ↔ proj x ∈ e.base_set := by rw [e.source_eq, mem_preimage] lemma coe_fst' (ex : proj x ∈ e.base_set) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex) lemma mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst ex).symm rfl lemma mk_proj_snd' (ex : proj x ∈ e.base_set) : (proj x, (e x).2) = e x := prod.ext (e.coe_fst' ex).symm rfl lemma source_inter_preimage_target_inter (s : set (B × F)) : e.source ∩ (e ⁻¹' (e.target ∩ s)) = e.source ∩ (e ⁻¹' s) := e.to_local_homeomorph.source_inter_preimage_target_inter s @[simp, mfld_simps] lemma coe_mk (e : local_homeomorph Z (B × F)) (i j k l m) (x : Z) : (trivialization.mk e i j k l m : trivialization F proj) x = e x := rfl lemma mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set := e.to_pretrivialization.mem_target lemma map_target {x : B × F} (hx : x ∈ e.target) : e.to_local_homeomorph.symm x ∈ e.source := e.to_local_homeomorph.map_target hx lemma proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.to_local_homeomorph.symm x) = x.1 := e.to_pretrivialization.proj_symm_apply hx lemma proj_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) : proj (e.to_local_homeomorph.symm (b, x)) = b := e.to_pretrivialization.proj_symm_apply' hx lemma proj_surj_on_base_set [nonempty F] : set.surj_on proj e.source e.base_set := e.to_pretrivialization.proj_surj_on_base_set lemma apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.to_local_homeomorph.symm x) = x := e.to_local_homeomorph.right_inv hx lemma apply_symm_apply' {b : B} {x : F} (hx : b ∈ e.base_set) : e (e.to_local_homeomorph.symm (b, x)) = (b, x) := e.to_pretrivialization.apply_symm_apply' hx @[simp, mfld_simps] lemma symm_apply_mk_proj (ex : x ∈ e.source) : e.to_local_homeomorph.symm (proj x, (e x).2) = x := e.to_pretrivialization.symm_apply_mk_proj ex lemma coe_fst_eventually_eq_proj (ex : x ∈ e.source) : prod.fst ∘ e =ᶠ[𝓝 x] proj := mem_nhds_iff.2 ⟨e.source, λ y hy, e.coe_fst hy, e.open_source, ex⟩ lemma coe_fst_eventually_eq_proj' (ex : proj x ∈ e.base_set) : prod.fst ∘ e =ᶠ[𝓝 x] proj := e.coe_fst_eventually_eq_proj (e.mem_source.2 ex) lemma map_proj_nhds (ex : x ∈ e.source) : map proj (𝓝 x) = 𝓝 (proj x) := by rw [← e.coe_fst ex, ← map_congr (e.coe_fst_eventually_eq_proj ex), ← map_map, ← e.coe_coe, e.to_local_homeomorph.map_nhds_eq ex, map_fst_nhds] /-- In the domain of a bundle trivialization, the projection is continuous-/ lemma continuous_at_proj (ex : x ∈ e.source) : continuous_at proj x := (e.map_proj_nhds ex).le /-- Composition of a `trivialization` and a `homeomorph`. -/ def comp_homeomorph {Z' : Type*} [topological_space Z'] (h : Z' ≃ₜ Z) : trivialization F (proj ∘ h) := { to_local_homeomorph := h.to_local_homeomorph.trans e.to_local_homeomorph, base_set := e.base_set, open_base_set := e.open_base_set, source_eq := by simp [e.source_eq, preimage_preimage], target_eq := by simp [e.target_eq], proj_to_fun := λ p hp, have hp : h p ∈ e.source, by simpa using hp, by simp [hp] } end topological_fiber_bundle.trivialization /-- A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. -/ def is_topological_fiber_bundle (proj : Z → B) : Prop := ∀ x : B, ∃e : trivialization F proj, x ∈ e.base_set /-- A trivial topological fiber bundle with fiber `F` over a base `B` is a space `Z` projecting on `B` for which there exists a homeomorphism to `B × F` that sends `proj` to `prod.fst`. -/ def is_trivial_topological_fiber_bundle (proj : Z → B) : Prop := ∃ e : Z ≃ₜ (B × F), ∀ x, (e x).1 = proj x variables {F} lemma is_trivial_topological_fiber_bundle.is_topological_fiber_bundle (h : is_trivial_topological_fiber_bundle F proj) : is_topological_fiber_bundle F proj := let ⟨e, he⟩ := h in λ x, ⟨⟨e.to_local_homeomorph, univ, is_open_univ, rfl, univ_prod_univ.symm, λ x _, he x⟩, mem_univ x⟩ lemma is_topological_fiber_bundle.map_proj_nhds (h : is_topological_fiber_bundle F proj) (x : Z) : map proj (𝓝 x) = 𝓝 (proj x) := let ⟨e, ex⟩ := h (proj x) in e.map_proj_nhds $ e.mem_source.2 ex /-- The projection from a topological fiber bundle to its base is continuous. -/ lemma is_topological_fiber_bundle.continuous_proj (h : is_topological_fiber_bundle F proj) : continuous proj := continuous_iff_continuous_at.2 $ λ x, (h.map_proj_nhds _).le /-- The projection from a topological fiber bundle to its base is an open map. -/ lemma is_topological_fiber_bundle.is_open_map_proj (h : is_topological_fiber_bundle F proj) : is_open_map proj := is_open_map.of_nhds_le $ λ x, (h.map_proj_nhds x).ge /-- The projection from a topological fiber bundle with a nonempty fiber to its base is a surjective map. -/ lemma is_topological_fiber_bundle.surjective_proj [nonempty F] (h : is_topological_fiber_bundle F proj) : function.surjective proj := λ b, let ⟨e, eb⟩ := h b, ⟨x, _, hx⟩ := e.proj_surj_on_base_set eb in ⟨x, hx⟩ /-- The projection from a topological fiber bundle with a nonempty fiber to its base is a quotient map. -/ lemma is_topological_fiber_bundle.quotient_map_proj [nonempty F] (h : is_topological_fiber_bundle F proj) : quotient_map proj := h.is_open_map_proj.to_quotient_map h.continuous_proj h.surjective_proj /-- The first projection in a product is a trivial topological fiber bundle. -/ lemma is_trivial_topological_fiber_bundle_fst : is_trivial_topological_fiber_bundle F (prod.fst : B × F → B) := ⟨homeomorph.refl _, λ x, rfl⟩ /-- The first projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_fst : is_topological_fiber_bundle F (prod.fst : B × F → B) := is_trivial_topological_fiber_bundle_fst.is_topological_fiber_bundle /-- The second projection in a product is a trivial topological fiber bundle. -/ lemma is_trivial_topological_fiber_bundle_snd : is_trivial_topological_fiber_bundle F (prod.snd : F × B → B) := ⟨homeomorph.prod_comm _ _, λ x, rfl⟩ /-- The second projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_snd : is_topological_fiber_bundle F (prod.snd : F × B → B) := is_trivial_topological_fiber_bundle_snd.is_topological_fiber_bundle lemma is_topological_fiber_bundle.comp_homeomorph {Z' : Type*} [topological_space Z'] (e : is_topological_fiber_bundle F proj) (h : Z' ≃ₜ Z) : is_topological_fiber_bundle F (proj ∘ h) := λ x, let ⟨e, he⟩ := e x in ⟨e.comp_homeomorph h, by simpa [topological_fiber_bundle.trivialization.comp_homeomorph] using he⟩ namespace topological_fiber_bundle.trivialization /-- If `e` is a `trivialization` of `proj : Z → B` with fiber `F` and `h` is a homeomorphism `F ≃ₜ F'`, then `e.trans_fiber_homeomorph h` is the trivialization of `proj` with the fiber `F'` that sends `p : Z` to `((e p).1, h (e p).2)`. -/ def trans_fiber_homeomorph {F' : Type*} [topological_space F'] (e : trivialization F proj) (h : F ≃ₜ F') : trivialization F' proj := { to_local_homeomorph := e.to_local_homeomorph.trans ((homeomorph.refl _).prod_congr h).to_local_homeomorph, base_set := e.base_set, open_base_set := e.open_base_set, source_eq := by simp [e.source_eq], target_eq := by { ext, simp [e.target_eq] }, proj_to_fun := λ p hp, have p ∈ e.source, by simpa using hp, by simp [this] } @[simp] lemma trans_fiber_homeomorph_apply {F' : Type*} [topological_space F'] (e : trivialization F proj) (h : F ≃ₜ F') (x : Z) : e.trans_fiber_homeomorph h x = ((e x).1, h (e x).2) := rfl /-- Coordinate transformation in the fiber induced by a pair of bundle trivializations. See also `trivialization.coord_change_homeomorph` for a version bundled as `F ≃ₜ F`. -/ def coord_change (e₁ e₂ : trivialization F proj) (b : B) (x : F) : F := (e₂ $ e₁.to_local_homeomorph.symm (b, x)).2 lemma mk_coord_change (e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) (x : F) : (b, e₁.coord_change e₂ b x) = e₂ (e₁.to_local_homeomorph.symm (b, x)) := begin refine prod.ext _ rfl, rw [e₂.coe_fst', ← e₁.coe_fst', e₁.apply_symm_apply' h₁], { rwa [e₁.proj_symm_apply' h₁] }, { rwa [e₁.proj_symm_apply' h₁] } end lemma coord_change_apply_snd (e₁ e₂ : trivialization F proj) {p : Z} (h : proj p ∈ e₁.base_set) : e₁.coord_change e₂ (proj p) (e₁ p).snd = (e₂ p).snd := by rw [coord_change, e₁.symm_apply_mk_proj (e₁.mem_source.2 h)] lemma coord_change_same_apply (e : trivialization F proj) {b : B} (h : b ∈ e.base_set) (x : F) : e.coord_change e b x = x := by rw [coord_change, e.apply_symm_apply' h] lemma coord_change_same (e : trivialization F proj) {b : B} (h : b ∈ e.base_set) : e.coord_change e b = id := funext $ e.coord_change_same_apply h lemma coord_change_coord_change (e₁ e₂ e₃ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) (x : F) : e₂.coord_change e₃ b (e₁.coord_change e₂ b x) = e₁.coord_change e₃ b x := begin rw [coord_change, e₁.mk_coord_change _ h₁ h₂, ← e₂.coe_coe, e₂.to_local_homeomorph.left_inv, coord_change], rwa [e₂.mem_source, e₁.proj_symm_apply' h₁] end lemma continuous_coord_change (e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) : continuous (e₁.coord_change e₂ b) := begin refine continuous_snd.comp (e₂.to_local_homeomorph.continuous_on.comp_continuous (e₁.to_local_homeomorph.continuous_on_symm.comp_continuous _ _) _), { exact continuous_const.prod_mk continuous_id }, { exact λ x, e₁.mem_target.2 h₁ }, { intro x, rwa [e₂.mem_source, e₁.proj_symm_apply' h₁] } end /-- Coordinate transformation in the fiber induced by a pair of bundle trivializations, as a homeomorphism. -/ def coord_change_homeomorph (e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) : F ≃ₜ F := { to_fun := e₁.coord_change e₂ b, inv_fun := e₂.coord_change e₁ b, left_inv := λ x, by simp only [*, coord_change_coord_change, coord_change_same_apply], right_inv := λ x, by simp only [*, coord_change_coord_change, coord_change_same_apply], continuous_to_fun := e₁.continuous_coord_change e₂ h₁ h₂, continuous_inv_fun := e₂.continuous_coord_change e₁ h₂ h₁ } @[simp] lemma coord_change_homeomorph_coe (e₁ e₂ : trivialization F proj) {b : B} (h₁ : b ∈ e₁.base_set) (h₂ : b ∈ e₂.base_set) : ⇑(e₁.coord_change_homeomorph e₂ h₁ h₂) = e₁.coord_change e₂ b := rfl end topological_fiber_bundle.trivialization section comap open_locale classical variables {B' : Type*} [topological_space B'] /-- Given a bundle trivialization of `proj : Z → B` and a continuous map `f : B' → B`, construct a bundle trivialization of `φ : {p : B' × Z | f p.1 = proj p.2} → B'` given by `φ x = (x : B' × Z).1`. -/ noncomputable def topological_fiber_bundle.trivialization.comap (e : trivialization F proj) (f : B' → B) (hf : continuous f) (b' : B') (hb' : f b' ∈ e.base_set) : trivialization F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) := { to_fun := λ p, ((p : B' × Z).1, (e (p : B' × Z).2).2), inv_fun := λ p, if h : f p.1 ∈ e.base_set then ⟨⟨p.1, e.to_local_homeomorph.symm (f p.1, p.2)⟩, by simp [e.proj_symm_apply' h]⟩ else ⟨⟨b', e.to_local_homeomorph.symm (f b', p.2)⟩, by simp [e.proj_symm_apply' hb']⟩, source := {p | f (p : B' × Z).1 ∈ e.base_set}, target := {p | f p.1 ∈ e.base_set}, map_source' := λ p hp, hp, map_target' := λ p (hp : f p.1 ∈ e.base_set), by simp [hp], left_inv' := begin rintro ⟨⟨b, x⟩, hbx⟩ hb, dsimp at *, have hx : x ∈ e.source, from e.mem_source.2 (hbx ▸ hb), ext; simp * end, right_inv' := λ p (hp : f p.1 ∈ e.base_set), by simp [*, e.apply_symm_apply'], open_source := e.open_base_set.preimage (hf.comp $ continuous_fst.comp continuous_subtype_coe), open_target := e.open_base_set.preimage (hf.comp continuous_fst), continuous_to_fun := ((continuous_fst.comp continuous_subtype_coe).continuous_on).prod $ continuous_snd.comp_continuous_on $ e.continuous_to_fun.comp (continuous_snd.comp continuous_subtype_coe).continuous_on $ by { rintro ⟨⟨b, x⟩, (hbx : f b = proj x)⟩ (hb : f b ∈ e.base_set), rw hbx at hb, exact e.mem_source.2 hb }, continuous_inv_fun := begin rw [embedding_subtype_coe.continuous_on_iff], suffices : continuous_on (λ p : B' × F, (p.1, e.to_local_homeomorph.symm (f p.1, p.2))) {p : B' × F | f p.1 ∈ e.base_set}, { refine this.congr (λ p (hp : f p.1 ∈ e.base_set), _), simp [hp] }, { refine continuous_on_fst.prod (e.to_local_homeomorph.symm.continuous_on.comp _ _), { exact ((hf.comp continuous_fst).prod_mk continuous_snd).continuous_on }, { exact λ p hp, e.mem_target.2 hp } } end, base_set := f ⁻¹' e.base_set, source_eq := rfl, target_eq := by { ext, simp }, open_base_set := e.open_base_set.preimage hf, proj_to_fun := λ _ _, rfl } /-- If `proj : Z → B` is a topological fiber bundle with fiber `F` and `f : B' → B` is a continuous map, then the pullback bundle (a.k.a. induced bundle) is the topological bundle with the total space `{(x, y) : B' × Z | f x = proj y}` given by `λ ⟨(x, y), h⟩, x`. -/ lemma is_topological_fiber_bundle.comap (h : is_topological_fiber_bundle F proj) {f : B' → B} (hf : continuous f) : is_topological_fiber_bundle F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) := λ x, let ⟨e, he⟩ := h (f x) in ⟨e.comap f hf x he, he⟩ end comap namespace topological_fiber_bundle.trivialization lemma is_image_preimage_prod (e : trivialization F proj) (s : set B) : e.to_local_homeomorph.is_image (proj ⁻¹' s) (s ×ˢ (univ : set F)) := λ x hx, by simp [e.coe_fst', hx] /-- Restrict a `trivialization` to an open set in the base. `-/ def restr_open (e : trivialization F proj) (s : set B) (hs : is_open s) : trivialization F proj := { to_local_homeomorph := ((e.is_image_preimage_prod s).symm.restr (is_open.inter e.open_target (hs.prod is_open_univ))).symm, base_set := e.base_set ∩ s, open_base_set := is_open.inter e.open_base_set hs, source_eq := by simp [e.source_eq], target_eq := by simp [e.target_eq, prod_univ], proj_to_fun := λ p hp, e.proj_to_fun p hp.1 } section piecewise lemma frontier_preimage (e : trivialization F proj) (s : set B) : e.source ∩ frontier (proj ⁻¹' s) = proj ⁻¹' (e.base_set ∩ frontier s) := by rw [← (e.is_image_preimage_prod s).frontier.preimage_eq, frontier_prod_univ_eq, (e.is_image_preimage_prod _).preimage_eq, e.source_eq, preimage_inter] /-- Given two bundle trivializations `e`, `e'` of `proj : Z → B` and a set `s : set B` such that the base sets of `e` and `e'` intersect `frontier s` on the same set and `e p = e' p` whenever `proj p ∈ e.base_set ∩ frontier s`, `e.piecewise e' s Hs Heq` is the bundle trivialization over `set.ite s e.base_set e'.base_set` that is equal to `e` on `proj ⁻¹ s` and is equal to `e'` otherwise. -/ noncomputable def piecewise (e e' : trivialization F proj) (s : set B) (Hs : e.base_set ∩ frontier s = e'.base_set ∩ frontier s) (Heq : eq_on e e' $ proj ⁻¹' (e.base_set ∩ frontier s)) : trivialization F proj := { to_local_homeomorph := e.to_local_homeomorph.piecewise e'.to_local_homeomorph (proj ⁻¹' s) (s ×ˢ (univ : set F)) (e.is_image_preimage_prod s) (e'.is_image_preimage_prod s) (by rw [e.frontier_preimage, e'.frontier_preimage, Hs]) (by rwa e.frontier_preimage), base_set := s.ite e.base_set e'.base_set, open_base_set := e.open_base_set.ite e'.open_base_set Hs, source_eq := by simp [e.source_eq, e'.source_eq], target_eq := by simp [e.target_eq, e'.target_eq, prod_univ], proj_to_fun := by rintro p (⟨he, hs⟩|⟨he, hs⟩); simp * } /-- Given two bundle trivializations `e`, `e'` of a topological fiber bundle `proj : Z → B` over a linearly ordered base `B` and a point `a ∈ e.base_set ∩ e'.base_set` such that `e` equals `e'` on `proj ⁻¹' {a}`, `e.piecewise_le_of_eq e' a He He' Heq` is the bundle trivialization over `set.ite (Iic a) e.base_set e'.base_set` that is equal to `e` on points `p` such that `proj p ≤ a` and is equal to `e'` otherwise. -/ noncomputable def piecewise_le_of_eq [linear_order B] [order_topology B] (e e' : trivialization F proj) (a : B) (He : a ∈ e.base_set) (He' : a ∈ e'.base_set) (Heq : ∀ p, proj p = a → e p = e' p) : trivialization F proj := e.piecewise e' (Iic a) (set.ext $ λ x, and.congr_left_iff.2 $ λ hx, by simp [He, He', mem_singleton_iff.1 (frontier_Iic_subset _ hx)]) (λ p hp, Heq p $ frontier_Iic_subset _ hp.2) /-- Given two bundle trivializations `e`, `e'` of a topological fiber bundle `proj : Z → B` over a linearly ordered base `B` and a point `a ∈ e.base_set ∩ e'.base_set`, `e.piecewise_le e' a He He'` is the bundle trivialization over `set.ite (Iic a) e.base_set e'.base_set` that is equal to `e` on points `p` such that `proj p ≤ a` and is equal to `((e' p).1, h (e' p).2)` otherwise, where `h = `e'.coord_change_homeomorph e _ _` is the homeomorphism of the fiber such that `h (e' p).2 = (e p).2` whenever `e p = a`. -/ noncomputable def piecewise_le [linear_order B] [order_topology B] (e e' : trivialization F proj) (a : B) (He : a ∈ e.base_set) (He' : a ∈ e'.base_set) : trivialization F proj := e.piecewise_le_of_eq (e'.trans_fiber_homeomorph (e'.coord_change_homeomorph e He' He)) a He He' $ by { unfreezingI {rintro p rfl }, ext1, { simp [e.coe_fst', e'.coe_fst', *] }, { simp [e'.coord_change_apply_snd, *] } } /-- Given two bundle trivializations `e`, `e'` over disjoint sets, `e.disjoint_union e' H` is the bundle trivialization over the union of the base sets that agrees with `e` and `e'` over their base sets. -/ noncomputable def disjoint_union (e e' : trivialization F proj) (H : disjoint e.base_set e'.base_set) : trivialization F proj := { to_local_homeomorph := e.to_local_homeomorph.disjoint_union e'.to_local_homeomorph (λ x hx, by { rw [e.source_eq, e'.source_eq] at hx, exact H hx }) (λ x hx, by { rw [e.target_eq, e'.target_eq] at hx, exact H ⟨hx.1.1, hx.2.1⟩ }), base_set := e.base_set ∪ e'.base_set, open_base_set := is_open.union e.open_base_set e'.open_base_set, source_eq := congr_arg2 (∪) e.source_eq e'.source_eq, target_eq := (congr_arg2 (∪) e.target_eq e'.target_eq).trans union_prod.symm, proj_to_fun := begin rintro p (hp|hp'), { show (e.source.piecewise e e' p).1 = proj p, rw [piecewise_eq_of_mem, e.coe_fst]; exact hp }, { show (e.source.piecewise e e' p).1 = proj p, rw [piecewise_eq_of_not_mem, e'.coe_fst hp'], simp only [e.source_eq, e'.source_eq] at hp' ⊢, exact λ h, H ⟨h, hp'⟩ } end } /-- If `h` is a topological fiber bundle over a conditionally complete linear order, then it is trivial over any closed interval. -/ lemma _root_.is_topological_fiber_bundle.exists_trivialization_Icc_subset [conditionally_complete_linear_order B] [order_topology B] (h : is_topological_fiber_bundle F proj) (a b : B) : ∃ e : trivialization F proj, Icc a b ⊆ e.base_set := begin classical, obtain ⟨ea, hea⟩ : ∃ ea : trivialization F proj, a ∈ ea.base_set := h a, -- If `a < b`, then `[a, b] = ∅`, and the statement is trivial cases le_or_lt a b with hab hab; [skip, exact ⟨ea, by simp *⟩], /- Let `s` be the set of points `x ∈ [a, b]` such that `proj` is trivializable over `[a, x]`. We need to show that `b ∈ s`. Let `c = Sup s`. We will show that `c ∈ s` and `c = b`. -/ set s : set B := {x ∈ Icc a b | ∃ e : trivialization F proj, Icc a x ⊆ e.base_set}, have ha : a ∈ s, from ⟨left_mem_Icc.2 hab, ea, by simp [hea]⟩, have sne : s.nonempty := ⟨a, ha⟩, have hsb : b ∈ upper_bounds s, from λ x hx, hx.1.2, have sbd : bdd_above s := ⟨b, hsb⟩, set c := Sup s, have hsc : is_lub s c, from is_lub_cSup sne sbd, have hc : c ∈ Icc a b, from ⟨hsc.1 ha, hsc.2 hsb⟩, obtain ⟨-, ec : trivialization F proj, hec : Icc a c ⊆ ec.base_set⟩ : c ∈ s, { cases hc.1.eq_or_lt with heq hlt, { rwa ← heq }, refine ⟨hc, _⟩, /- In order to show that `c ∈ s`, consider a trivialization `ec` of `proj` over a neighborhood of `c`. Its base set includes `(c', c]` for some `c' ∈ [a, c)`. -/ rcases h c with ⟨ec, hc⟩, obtain ⟨c', hc', hc'e⟩ : ∃ c' ∈ Ico a c, Ioc c' c ⊆ ec.base_set := (mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset hlt).1 (mem_nhds_within_of_mem_nhds $ is_open.mem_nhds ec.open_base_set hc), /- Since `c' < c = Sup s`, there exists `d ∈ s ∩ (c', c]`. Let `ead` be a trivialization of `proj` over `[a, d]`. Then we can glue `ead` and `ec` into a trivialization over `[a, c]`. -/ obtain ⟨d, ⟨hdab, ead, had⟩, hd⟩ : ∃ d ∈ s, d ∈ Ioc c' c := hsc.exists_between hc'.2, refine ⟨ead.piecewise_le ec d (had ⟨hdab.1, le_rfl⟩) (hc'e hd), subset_ite.2 _⟩, refine ⟨λ x hx, had ⟨hx.1.1, hx.2⟩, λ x hx, hc'e ⟨hd.1.trans (not_le.1 hx.2), hx.1.2⟩⟩ }, /- So, `c ∈ s`. Let `ec` be a trivialization of `proj` over `[a, c]`. If `c = b`, then we are done. Otherwise we show that `proj` can be trivialized over a larger interval `[a, d]`, `d ∈ (c, b]`, hence `c` is not an upper bound of `s`. -/ cases hc.2.eq_or_lt with heq hlt, { exact ⟨ec, heq ▸ hec⟩ }, suffices : ∃ (d ∈ Ioc c b) (e : trivialization F proj), Icc a d ⊆ e.base_set, { rcases this with ⟨d, hdcb, hd⟩, exact ((hsc.1 ⟨⟨hc.1.trans hdcb.1.le, hdcb.2⟩, hd⟩).not_lt hdcb.1).elim }, /- Since the base set of `ec` is open, it includes `[c, d)` (hence, `[a, d)`) for some `d ∈ (c, b]`. -/ obtain ⟨d, hdcb, hd⟩ : ∃ d ∈ Ioc c b, Ico c d ⊆ ec.base_set := (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hlt).1 (mem_nhds_within_of_mem_nhds $ is_open.mem_nhds ec.open_base_set (hec ⟨hc.1, le_rfl⟩)), have had : Ico a d ⊆ ec.base_set, from subset.trans Ico_subset_Icc_union_Ico (union_subset hec hd), by_cases he : disjoint (Iio d) (Ioi c), { /- If `(c, d) = ∅`, then let `ed` be a trivialization of `proj` over a neighborhood of `d`. Then the disjoint union of `ec` restricted to `(-∞, d)` and `ed` restricted to `(c, ∞)` is a trivialization over `[a, d]`. -/ rcases h d with ⟨ed, hed⟩, refine ⟨d, hdcb, (ec.restr_open (Iio d) is_open_Iio).disjoint_union (ed.restr_open (Ioi c) is_open_Ioi) (he.mono (inter_subset_right _ _) (inter_subset_right _ _)), λ x hx, _⟩, rcases hx.2.eq_or_lt with rfl|hxd, exacts [or.inr ⟨hed, hdcb.1⟩, or.inl ⟨had ⟨hx.1, hxd⟩, hxd⟩] }, { /- If `(c, d)` is nonempty, then take `d' ∈ (c, d)`. Since the base set of `ec` includes `[a, d)`, it includes `[a, d'] ⊆ [a, d)` as well. -/ rw [disjoint_left] at he, push_neg at he, rcases he with ⟨d', hdd' : d' < d, hd'c⟩, exact ⟨d', ⟨hd'c, hdd'.le.trans hdcb.2⟩, ec, subset.trans (Icc_subset_Ico_right hdd') had⟩ } end end piecewise end topological_fiber_bundle.trivialization end topological_fiber_bundle /-! ### Constructing topological fiber bundles -/ namespace bundle variable (E : B → Type*) attribute [mfld_simps] proj total_space_mk coe_fst coe_snd_map_apply coe_snd_map_smul instance [I : topological_space F] : ∀ x : B, topological_space (trivial B F x) := λ x, I instance [t₁ : topological_space B] [t₂ : topological_space F] : topological_space (total_space (trivial B F)) := topological_space.induced (proj (trivial B F)) t₁ ⊓ topological_space.induced (trivial.proj_snd B F) t₂ end bundle /-- Core data defining a locally trivial topological bundle with fiber `F` over a topological space `B`. Note that "bundle" is used in its mathematical sense. This is the (computer science) bundled version, i.e., all the relevant data is contained in the following structure. A family of local trivializations is indexed by a type `ι`, on open subsets `base_set i` for each `i : ι`. Trivialization changes from `i` to `j` are given by continuous maps `coord_change i j` from `base_set i ∩ base_set j` to the set of homeomorphisms of `F`, but we express them as maps `B → F → F` and require continuity on `(base_set i ∩ base_set j) × F` to avoid the topology on the space of continuous maps on `F`. -/ @[nolint has_inhabited_instance] structure topological_fiber_bundle_core (ι : Type*) (B : Type*) [topological_space B] (F : Type*) [topological_space F] := (base_set : ι → set B) (is_open_base_set : ∀ i, is_open (base_set i)) (index_at : B → ι) (mem_base_set_at : ∀ x, x ∈ base_set (index_at x)) (coord_change : ι → ι → B → F → F) (coord_change_self : ∀ i, ∀ x ∈ base_set i, ∀ v, coord_change i i x v = v) (coord_change_continuous : ∀ i j, continuous_on (λp : B × F, coord_change i j p.1 p.2) (((base_set i) ∩ (base_set j)) ×ˢ (univ : set F))) (coord_change_comp : ∀ i j k, ∀ x ∈ (base_set i) ∩ (base_set j) ∩ (base_set k), ∀ v, (coord_change j k x) (coord_change i j x v) = coord_change i k x v) namespace topological_fiber_bundle_core variables [topological_space B] [topological_space F] (Z : topological_fiber_bundle_core ι B F) include Z /-- The index set of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments has_inhabited_instance] def index := ι /-- The base space of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments, reducible] def base := B /-- The fiber of a topological fiber bundle core, as a convenience function for dot notation and typeclass inference -/ @[nolint unused_arguments has_inhabited_instance] def fiber (x : B) := F section fiber_instances local attribute [reducible] fiber instance topological_space_fiber (x : B) : topological_space (Z.fiber x) := by apply_instance end fiber_instances /-- The total space of the topological fiber bundle, as a convenience function for dot notation. It is by definition equal to `bundle.total_space Z.fiber`, a.k.a. `Σ x, Z.fiber x` but with a different name for typeclass inference. -/ @[nolint unused_arguments, reducible] def total_space := bundle.total_space Z.fiber /-- The projection from the total space of a topological fiber bundle core, on its base. -/ @[reducible, simp, mfld_simps] def proj : Z.total_space → B := bundle.proj Z.fiber /-- Local homeomorphism version of the trivialization change. -/ def triv_change (i j : ι) : local_homeomorph (B × F) (B × F) := { source := (Z.base_set i ∩ Z.base_set j) ×ˢ (univ : set F), target := (Z.base_set i ∩ Z.base_set j) ×ˢ (univ : set F), to_fun := λp, ⟨p.1, Z.coord_change i j p.1 p.2⟩, inv_fun := λp, ⟨p.1, Z.coord_change j i p.1 p.2⟩, map_source' := λp hp, by simpa using hp, map_target' := λp hp, by simpa using hp, left_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.1 }, { simp [hx] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.2 }, { simp [hx] }, end, open_source := (is_open.inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ, open_target := (is_open.inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ, continuous_to_fun := continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous i j), continuous_inv_fun := by simpa [inter_comm] using continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous j i) } @[simp, mfld_simps] lemma mem_triv_change_source (i j : ι) (p : B × F) : p ∈ (Z.triv_change i j).source ↔ p.1 ∈ Z.base_set i ∩ Z.base_set j := by { erw [mem_prod], simp } /-- Associate to a trivialization index `i : ι` the corresponding trivialization, i.e., a bijection between `proj ⁻¹ (base_set i)` and `base_set i × F`. As the fiber above `x` is `F` but read in the chart with index `index_at x`, the trivialization in the fiber above x is by definition the coordinate change from i to `index_at x`, so it depends on `x`. The local trivialization will ultimately be a local homeomorphism. For now, we only introduce the local equiv version, denoted with a prime. In further developments, avoid this auxiliary version, and use `Z.local_triv` instead. -/ def local_triv_as_local_equiv (i : ι) : local_equiv Z.total_space (B × F) := { source := Z.proj ⁻¹' (Z.base_set i), target := Z.base_set i ×ˢ (univ : set F), inv_fun := λp, ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩, to_fun := λp, ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩, map_source' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.prod_mk_mem_set_prod_eq] using hp, map_target' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.mem_prod] using hp, left_inv' := begin rintros ⟨x, v⟩ hx, change x ∈ Z.base_set i at hx, dsimp only, rw [Z.coord_change_comp, Z.coord_change_self], { exact Z.mem_base_set_at _ }, { simp only [hx, mem_inter_eq, and_self, mem_base_set_at] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx }, { simp only [hx, mem_inter_eq, and_self, mem_base_set_at] } end } variable (i : ι) lemma mem_local_triv_as_local_equiv_source (p : Z.total_space) : p ∈ (Z.local_triv_as_local_equiv i).source ↔ p.1 ∈ Z.base_set i := iff.rfl lemma mem_local_triv_as_local_equiv_target (p : B × F) : p ∈ (Z.local_triv_as_local_equiv i).target ↔ p.1 ∈ Z.base_set i := by { erw [mem_prod], simp only [and_true, mem_univ] } lemma local_triv_as_local_equiv_apply (p : Z.total_space) : (Z.local_triv_as_local_equiv i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl /-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/ lemma local_triv_as_local_equiv_trans (i j : ι) : (Z.local_triv_as_local_equiv i).symm.trans (Z.local_triv_as_local_equiv j) ≈ (Z.triv_change i j).to_local_equiv := begin split, { ext x, simp only [mem_local_triv_as_local_equiv_target] with mfld_simps, refl, }, { rintros ⟨x, v⟩ hx, simp only [triv_change, local_triv_as_local_equiv, local_equiv.symm, true_and, prod.mk.inj_iff, prod_mk_mem_set_prod_eq, local_equiv.trans_source, mem_inter_eq, and_true, mem_preimage, proj, mem_univ, local_equiv.coe_mk, eq_self_iff_true, local_equiv.coe_trans, bundle.proj] at hx ⊢, simp only [Z.coord_change_comp, hx, mem_inter_eq, and_self, mem_base_set_at], } end variable (ι) /-- Topological structure on the total space of a topological bundle created from core, designed so that all the local trivialization are continuous. -/ instance to_topological_space : topological_space (bundle.total_space Z.fiber) := topological_space.generate_from $ ⋃ (i : ι) (s : set (B × F)) (s_open : is_open s), {(Z.local_triv_as_local_equiv i).source ∩ (Z.local_triv_as_local_equiv i) ⁻¹' s} variable {ι} lemma open_source' (i : ι) : is_open (Z.local_triv_as_local_equiv i).source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨i, Z.base_set i ×ˢ (univ : set F), (Z.is_open_base_set i).prod is_open_univ, _⟩, ext p, simp only [local_triv_as_local_equiv_apply, prod_mk_mem_set_prod_eq, mem_inter_eq, and_self, mem_local_triv_as_local_equiv_source, and_true, mem_univ, mem_preimage], end open topological_fiber_bundle /-- Extended version of the local trivialization of a fiber bundle constructed from core, registering additionally in its type that it is a local bundle trivialization. -/ def local_triv (i : ι) : trivialization F Z.proj := { base_set := Z.base_set i, open_base_set := Z.is_open_base_set i, source_eq := rfl, target_eq := rfl, proj_to_fun := λ p hp, by { simp only with mfld_simps, refl }, open_source := Z.open_source' i, open_target := (Z.is_open_base_set i).prod is_open_univ, continuous_to_fun := begin rw continuous_on_open_iff (Z.open_source' i), assume s s_open, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨i, s, s_open, rfl⟩ end, continuous_inv_fun := begin apply continuous_on_open_of_generate_from ((Z.is_open_base_set i).prod is_open_univ), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, obtain ⟨j, s, s_open, ts⟩ : ∃ j s, is_open s ∧ t = (local_triv_as_local_equiv Z j).source ∩ (local_triv_as_local_equiv Z j) ⁻¹' s := ht, rw ts, simp only [local_equiv.right_inv, preimage_inter, local_equiv.left_inv], let e := Z.local_triv_as_local_equiv i, let e' := Z.local_triv_as_local_equiv j, let f := e.symm.trans e', have : is_open (f.source ∩ f ⁻¹' s), { rw [(Z.local_triv_as_local_equiv_trans i j).source_inter_preimage_eq], exact (continuous_on_open_iff (Z.triv_change i j).open_source).1 ((Z.triv_change i j).continuous_on) _ s_open }, convert this using 1, dsimp [local_equiv.trans_source], rw [← preimage_comp, inter_assoc], refl, end, to_local_equiv := Z.local_triv_as_local_equiv i } /-- A topological fiber bundle constructed from core is indeed a topological fiber bundle. -/ protected theorem is_topological_fiber_bundle : is_topological_fiber_bundle F Z.proj := λx, ⟨Z.local_triv (Z.index_at x), Z.mem_base_set_at x⟩ /-- The projection on the base of a topological bundle created from core is continuous -/ lemma continuous_proj : continuous Z.proj := Z.is_topological_fiber_bundle.continuous_proj /-- The projection on the base of a topological bundle created from core is an open map -/ lemma is_open_map_proj : is_open_map Z.proj := Z.is_topological_fiber_bundle.is_open_map_proj /-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as a bundle trivialization -/ def local_triv_at (b : B) : trivialization F Z.proj := Z.local_triv (Z.index_at b) @[simp, mfld_simps] lemma local_triv_at_def (b : B) : Z.local_triv (Z.index_at b) = Z.local_triv_at b := rfl /-- If an element of `F` is invariant under all coordinate changes, then one can define a corresponding section of the fiber bundle, which is continuous. This applies in particular to the zero section of a vector bundle. Another example (not yet defined) would be the identity section of the endomorphism bundle of a vector bundle. -/ lemma continuous_const_section (v : F) (h : ∀ i j, ∀ x ∈ (Z.base_set i) ∩ (Z.base_set j), Z.coord_change i j x v = v) : continuous (show B → Z.total_space, from λ x, ⟨x, v⟩) := begin apply continuous_iff_continuous_at.2 (λ x, _), have A : Z.base_set (Z.index_at x) ∈ 𝓝 x := is_open.mem_nhds (Z.is_open_base_set (Z.index_at x)) (Z.mem_base_set_at x), apply ((Z.local_triv_at x).to_local_homeomorph.continuous_at_iff_continuous_at_comp_left _).2, { simp only [(∘)] with mfld_simps, apply continuous_at_id.prod, have : continuous_on (λ (y : B), v) (Z.base_set (Z.index_at x)) := continuous_on_const, apply (this.congr _).continuous_at A, assume y hy, simp only [h, hy, mem_base_set_at] with mfld_simps }, { exact A } end @[simp, mfld_simps] lemma local_triv_as_local_equiv_coe : ⇑(Z.local_triv_as_local_equiv i) = Z.local_triv i := rfl @[simp, mfld_simps] lemma local_triv_as_local_equiv_source : (Z.local_triv_as_local_equiv i).source = (Z.local_triv i).source := rfl @[simp, mfld_simps] lemma local_triv_as_local_equiv_target : (Z.local_triv_as_local_equiv i).target = (Z.local_triv i).target := rfl @[simp, mfld_simps] lemma local_triv_as_local_equiv_symm : (Z.local_triv_as_local_equiv i).symm = (Z.local_triv i).to_local_equiv.symm := rfl @[simp, mfld_simps] lemma base_set_at : Z.base_set i = (Z.local_triv i).base_set := rfl @[simp, mfld_simps] lemma local_triv_apply (p : Z.total_space) : (Z.local_triv i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl @[simp, mfld_simps] lemma mem_local_triv_source (p : Z.total_space) : p ∈ (Z.local_triv i).source ↔ p.1 ∈ (Z.local_triv i).base_set := iff.rfl @[simp, mfld_simps] lemma mem_local_triv_target (p : B × F) : p ∈ (Z.local_triv i).target ↔ p.1 ∈ (Z.local_triv i).base_set := trivialization.mem_target _ @[simp, mfld_simps] lemma local_triv_symm_fst (p : B × F) : (Z.local_triv i).to_local_homeomorph.symm p = ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩ := rfl @[simp, mfld_simps] lemma local_triv_at_apply (b : B) (a : F) : ((Z.local_triv_at b) ⟨b, a⟩) = ⟨b, a⟩ := by { rw [local_triv_at, local_triv_apply, coord_change_self], exact Z.mem_base_set_at b } @[simp, mfld_simps] lemma mem_local_triv_at_base_set (b : B) : b ∈ (Z.local_triv_at b).base_set := by { rw [local_triv_at, ←base_set_at], exact Z.mem_base_set_at b, } open bundle /-- The inclusion of a fiber into the total space is a continuous map. -/ lemma continuous_total_space_mk (b : B) : continuous (λ a, total_space_mk Z.fiber b a) := begin rw [continuous_iff_le_induced, topological_fiber_bundle_core.to_topological_space], apply le_induced_generate_from, simp only [total_space_mk, mem_Union, mem_singleton_iff, local_triv_as_local_equiv_source, local_triv_as_local_equiv_coe], rintros s ⟨i, t, ht, rfl⟩, rw [←((Z.local_triv i).source_inter_preimage_target_inter t), preimage_inter, ←preimage_comp, trivialization.source_eq], apply is_open.inter, { simp only [bundle.proj, proj, ←preimage_comp], by_cases (b ∈ (Z.local_triv i).base_set), { rw preimage_const_of_mem h, exact is_open_univ, }, { rw preimage_const_of_not_mem h, exact is_open_empty, }}, { simp only [function.comp, local_triv_apply], rw [preimage_inter, preimage_comp], by_cases (b ∈ Z.base_set i), { have hc : continuous (λ (x : Z.fiber b), (Z.coord_change (Z.index_at b) i b) x) := begin rw continuous_iff_continuous_on_univ, refine ((Z.coord_change_continuous (Z.index_at b) i).comp ((continuous_const).prod_mk continuous_id).continuous_on) (by { convert (subset_univ univ), exact mk_preimage_prod_right (mem_inter (Z.mem_base_set_at b) h), }) end, exact hc.is_open_preimage _ ((continuous.prod.mk b).is_open_preimage _ ((Z.local_triv i).open_target.inter ht)), }, { rw [(Z.local_triv i).target_eq, ←base_set_at, mk_preimage_prod_right_eq_empty h, preimage_empty, empty_inter], exact is_open_empty, }} end end topological_fiber_bundle_core variables (F) {Z : Type*} [topological_space B] [topological_space F] {proj : Z → B} open topological_fiber_bundle /-- This structure permits to define a fiber bundle when trivializations are given as local equivalences but there is not yet a topology on the total space. The total space is hence given a topology in such a way that there is a fiber bundle structure for which the local equivalences are also local homeomorphism and hence local trivializations. -/ @[nolint has_inhabited_instance] structure topological_fiber_prebundle (proj : Z → B) := (pretrivialization_at : B → pretrivialization F proj) (mem_base_pretrivialization_at : ∀ x : B, x ∈ (pretrivialization_at x).base_set) (continuous_triv_change : ∀ x y : B, continuous_on ((pretrivialization_at x) ∘ (pretrivialization_at y).to_local_equiv.symm) ((pretrivialization_at y).target ∩ ((pretrivialization_at y).to_local_equiv.symm ⁻¹' (pretrivialization_at x).source))) namespace topological_fiber_prebundle variables {F} (a : topological_fiber_prebundle F proj) (x : B) /-- Topology on the total space that will make the prebundle into a bundle. -/ def total_space_topology (a : topological_fiber_prebundle F proj) : topological_space Z := ⨆ x : B, coinduced (a.pretrivialization_at x).set_symm (subtype.topological_space) lemma continuous_symm_pretrivialization_at : @continuous_on _ _ _ a.total_space_topology (a.pretrivialization_at x).to_local_equiv.symm (a.pretrivialization_at x).target := begin refine id (λ z H, id (λ U h, preimage_nhds_within_coinduced' H (a.pretrivialization_at x).open_target (le_def.1 (nhds_mono _) U h))), exact le_supr _ x, end lemma is_open_source_pretrivialization_at : @is_open _ a.total_space_topology (a.pretrivialization_at x).source := begin letI := a.total_space_topology, refine is_open_supr_iff.mpr (λ y, is_open_coinduced.mpr (is_open_induced_iff.mpr ⟨(a.pretrivialization_at x).target, (a.pretrivialization_at x).open_target, _⟩)), rw [pretrivialization.set_symm, restrict, (a.pretrivialization_at x).target_eq, (a.pretrivialization_at x).source_eq, preimage_comp, subtype.preimage_coe_eq_preimage_coe_iff, (a.pretrivialization_at y).target_eq, prod_inter_prod, inter_univ, pretrivialization.preimage_symm_proj_inter], end lemma is_open_target_pretrivialization_at_inter (x y : B) : is_open ((a.pretrivialization_at y).to_local_equiv.target ∩ (a.pretrivialization_at y).to_local_equiv.symm ⁻¹' (a.pretrivialization_at x).source) := begin letI := a.total_space_topology, obtain ⟨u, hu1, hu2⟩ := continuous_on_iff'.mp (a.continuous_symm_pretrivialization_at y) (a.pretrivialization_at x).source (a.is_open_source_pretrivialization_at x), rw [inter_comm, hu2], exact hu1.inter (a.pretrivialization_at y).open_target, end /-- Promotion from a `pretrivialization` to a `trivialization`. -/ def trivialization_at (a : topological_fiber_prebundle F proj) (x : B) : @trivialization B F Z _ _ a.total_space_topology proj := { open_source := a.is_open_source_pretrivialization_at x, continuous_to_fun := begin letI := a.total_space_topology, refine continuous_on_iff'.mpr (λ s hs, ⟨(a.pretrivialization_at x) ⁻¹' s ∩ (a.pretrivialization_at x).source, (is_open_supr_iff.mpr (λ y, _)), by { rw [inter_assoc, inter_self], refl }⟩), rw [is_open_coinduced, is_open_induced_iff], obtain ⟨u, hu1, hu2⟩ := continuous_on_iff'.mp (a.continuous_triv_change x y) s hs, have hu3 := congr_arg (λ s, (λ x : (a.pretrivialization_at y).target, (x : B × F)) ⁻¹' s) hu2, simp only [subtype.coe_preimage_self, preimage_inter, univ_inter] at hu3, refine ⟨u ∩ (a.pretrivialization_at y).to_local_equiv.target ∩ ((a.pretrivialization_at y).to_local_equiv.symm ⁻¹' (a.pretrivialization_at x).source), _, by { simp only [preimage_inter, inter_univ, subtype.coe_preimage_self, hu3.symm], refl }⟩, rw inter_assoc, exact hu1.inter (a.is_open_target_pretrivialization_at_inter x y), end, continuous_inv_fun := a.continuous_symm_pretrivialization_at x, ..(a.pretrivialization_at x) } lemma is_topological_fiber_bundle : @is_topological_fiber_bundle B F Z _ _ a.total_space_topology proj := λ x, ⟨a.trivialization_at x, a.mem_base_pretrivialization_at x ⟩ lemma continuous_proj : @continuous _ _ a.total_space_topology _ proj := by { letI := a.total_space_topology, exact a.is_topological_fiber_bundle.continuous_proj, } end topological_fiber_prebundle
7f140bbf3c9c5379a64d4051ffccefb3f8b8dd87
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/library/algebra/complete_lattice.lean
63313c587fb4afb7997c933767ca168a38360552
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,997
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 Complete lattices TODO: define dual complete lattice and simplify proof of dual theorems. -/ import algebra.lattice data.set.basic open set variable {A : Type} structure complete_lattice [class] (A : Type) extends lattice A := (Inf : set A → A) (Sup : set A → A) (Inf_le : ∀ {a : A} {s : set A}, a ∈ s → le (Inf s) a) (le_Inf : ∀ {b : A} {s : set A}, (∀ (a : A), a ∈ s → le b a) → le b (Inf s)) (le_Sup : ∀ {a : A} {s : set A}, a ∈ s → le a (Sup s)) (Sup_le : ∀ {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → le a b), le (Sup s) b) section variable [complete_lattice A] definition Inf (S : set A) : A := complete_lattice.Inf S prefix `⨅ `:70 := Inf definition Sup (S : set A) : A := complete_lattice.Sup S prefix `⨆ `:65 := Sup theorem Inf_le {a : A} {s : set A} (H : a ∈ s) : (Inf s) ≤ a := complete_lattice.Inf_le H theorem le_Inf {b : A} {s : set A} (H : ∀ (a : A), a ∈ s → b ≤ a) : b ≤ Inf s := complete_lattice.le_Inf H theorem le_Sup {a : A} {s : set A} (H : a ∈ s) : a ≤ Sup s := complete_lattice.le_Sup H theorem Sup_le {b : A} {s : set A} (H : ∀ (a : A), a ∈ s → a ≤ b) : Sup s ≤ b := complete_lattice.Sup_le H end -- Minimal complete_lattice definition based just on Inf. -- We later show that complete_lattice_Inf is a complete_lattice. structure complete_lattice_Inf [class] (A : Type) extends weak_order A := (Inf : set A → A) (Inf_le : ∀ {a : A} {s : set A}, a ∈ s → le (Inf s) a) (le_Inf : ∀ {b : A} {s : set A}, (∀ (a : A), a ∈ s → le b a) → le b (Inf s)) -- Minimal complete_lattice definition based just on Sup. -- We later show that complete_lattice_Sup is a complete_lattice. structure complete_lattice_Sup [class] (A : Type) extends weak_order A := (Sup : set A → A) (le_Sup : ∀ {a : A} {s : set A}, a ∈ s → le a (Sup s)) (Sup_le : ∀ {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → le a b), le (Sup s) b) namespace complete_lattice_Inf variable [C : complete_lattice_Inf A] include C definition Sup (s : set A) : A := Inf {b | ∀ a, a ∈ s → a ≤ b} local prefix `⨅`:70 := Inf local prefix `⨆`:65 := Sup lemma le_Sup {a : A} {s : set A} : a ∈ s → a ≤ ⨆ s := suppose a ∈ s, le_Inf (show ∀ (b : A), (∀ (a : A), a ∈ s → a ≤ b) → a ≤ b, from take b, assume h, h a `a ∈ s`) lemma Sup_le {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → a ≤ b) : ⨆ s ≤ b := Inf_le h definition inf (a b : A) := ⨅ '{a, b} definition sup (a b : A) := ⨆ '{a, b} local infix `⊓` := inf local infix `⊔` := sup lemma inf_le_left (a b : A) : a ⊓ b ≤ a := Inf_le !mem_insert lemma inf_le_right (a b : A) : a ⊓ b ≤ b := Inf_le (!mem_insert_of_mem !mem_insert) lemma le_inf {a b c : A} : c ≤ a → c ≤ b → c ≤ a ⊓ b := assume h₁ h₂, le_Inf (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, begin subst x, exact h₁ end) (suppose x ∈ '{b}, assert x = b, from !eq_of_mem_singleton this, begin subst x, exact h₂ end)) lemma le_sup_left (a b : A) : a ≤ a ⊔ b := le_Sup !mem_insert lemma le_sup_right (a b : A) : b ≤ a ⊔ b := le_Sup (!mem_insert_of_mem !mem_insert) lemma sup_le {a b c : A} : a ≤ c → b ≤ c → a ⊔ b ≤ c := assume h₁ h₂, Sup_le (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, by subst x; assumption) (suppose x ∈ '{b}, assert x = b, from !eq_of_mem_singleton this, by subst x; assumption)) end complete_lattice_Inf -- Every complete_lattice_Inf is a complete_lattice_Sup definition complete_lattice_Inf_to_complete_lattice_Sup [C : complete_lattice_Inf A] : complete_lattice_Sup A := ⦃ complete_lattice_Sup, C ⦄ -- Every complete_lattice_Inf is a complete_lattice definition complete_lattice_Inf_to_complete_lattice [trans_instance] [reducible] [C : complete_lattice_Inf A] : complete_lattice A := ⦃ complete_lattice, C ⦄ namespace complete_lattice_Sup variable [C : complete_lattice_Sup A] include C definition Inf (s : set A) : A := Sup {b | ∀ a, a ∈ s → b ≤ a} lemma Inf_le {a : A} {s : set A} : a ∈ s → Inf s ≤ a := suppose a ∈ s, Sup_le (show ∀ (b : A), (∀ (a : A), a ∈ s → b ≤ a) → b ≤ a, from take b, assume h, h a `a ∈ s`) lemma le_Inf {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → b ≤ a) : b ≤ Inf s := le_Sup h local prefix `⨅`:70 := Inf local prefix `⨆`:65 := Sup definition inf (a b : A) := ⨅ '{a, b} definition sup (a b : A) := ⨆ '{a, b} local infix `⊓` := inf local infix `⊔` := sup lemma inf_le_left (a b : A) : a ⊓ b ≤ a := Inf_le !mem_insert lemma inf_le_right (a b : A) : a ⊓ b ≤ b := Inf_le (!mem_insert_of_mem !mem_insert) lemma le_inf {a b c : A} : c ≤ a → c ≤ b → c ≤ a ⊓ b := assume h₁ h₂, le_Inf (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, begin subst x, exact h₁ end) (suppose x ∈ '{b}, assert x = b, from !eq_of_mem_singleton this, begin subst x, exact h₂ end)) lemma le_sup_left (a b : A) : a ≤ a ⊔ b := le_Sup !mem_insert lemma le_sup_right (a b : A) : b ≤ a ⊔ b := le_Sup (!mem_insert_of_mem !mem_insert) lemma sup_le {a b c : A} : a ≤ c → b ≤ c → a ⊔ b ≤ c := assume h₁ h₂, Sup_le (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (assume H : x = a, by subst x; exact h₁) (suppose x ∈ '{b}, assert x = b, from !eq_of_mem_singleton this, by subst x; exact h₂)) end complete_lattice_Sup -- Every complete_lattice_Sup is a complete_lattice_Inf definition complete_lattice_Sup_to_complete_lattice_Inf [C : complete_lattice_Sup A] : complete_lattice_Inf A := ⦃ complete_lattice_Inf, C ⦄ -- Every complete_lattice_Sup is a complete_lattice section definition complete_lattice_Sup_to_complete_lattice [trans_instance] [reducible] [C : complete_lattice_Sup A] : complete_lattice A := ⦃ complete_lattice, C ⦄ end section complete_lattice variable [C : complete_lattice A] include C variable {f : A → A} premise (mono : ∀ x y : A, x ≤ y → f x ≤ f y) theorem knaster_tarski : ∃ a, f a = a ∧ ∀ b, f b = b → a ≤ b := let a := ⨅ {u | f u ≤ u} in have h₁ : f a = a, from have ge : f a ≤ a, from have ∀ b, b ∈ {u | f u ≤ u} → f a ≤ b, from take b, suppose f b ≤ b, have a ≤ b, from Inf_le this, have f a ≤ f b, from !mono this, le.trans `f a ≤ f b` `f b ≤ b`, le_Inf this, have le : a ≤ f a, from have f (f a) ≤ f a, from !mono ge, have f a ∈ {u | f u ≤ u}, from this, Inf_le this, le.antisymm ge le, have h₂ : ∀ b, f b = b → a ≤ b, from take b, suppose f b = b, have b ∈ {u | f u ≤ u}, from show f b ≤ b, by rewrite this; apply le.refl, Inf_le this, exists.intro a (and.intro h₁ h₂) theorem knaster_tarski_dual : ∃ a, f a = a ∧ ∀ b, f b = b → b ≤ a := let a := ⨆ {u | u ≤ f u} in have h₁ : f a = a, from have le : a ≤ f a, from have ∀ b, b ∈ {u | u ≤ f u} → b ≤ f a, from take b, suppose b ≤ f b, have b ≤ a, from le_Sup this, have f b ≤ f a, from !mono this, le.trans `b ≤ f b` `f b ≤ f a`, Sup_le this, have ge : f a ≤ a, from have f a ≤ f (f a), from !mono le, have f a ∈ {u | u ≤ f u}, from this, le_Sup this, le.antisymm ge le, have h₂ : ∀ b, f b = b → b ≤ a, from take b, suppose f b = b, have b ≤ f b, by rewrite this; apply le.refl, le_Sup this, exists.intro a (and.intro h₁ h₂) /- top and bot -/ definition bot : A := ⨅ univ definition top : A := ⨆ univ notation `⊥` := bot notation `⊤` := top lemma bot_le (a : A) : ⊥ ≤ a := Inf_le !mem_univ lemma eq_bot {a : A} : (∀ b, a ≤ b) → a = ⊥ := assume h, have a ≤ ⊥, from le_Inf (take b bin, h b), le.antisymm this !bot_le lemma le_top (a : A) : a ≤ ⊤ := le_Sup !mem_univ lemma eq_top {a : A} : (∀ b, b ≤ a) → a = ⊤ := assume h, have ⊤ ≤ a, from Sup_le (take b bin, h b), le.antisymm !le_top this /- general facts about complete lattices -/ lemma Inf_singleton {a : A} : ⨅'{a} = a := have ⨅'{a} ≤ a, from Inf_le !mem_insert, have a ≤ ⨅'{a}, from le_Inf (take b, suppose b ∈ '{a}, assert b = a, from eq_of_mem_singleton this, by rewrite this; apply le.refl), le.antisymm `⨅'{a} ≤ a` `a ≤ ⨅'{a}` lemma Sup_singleton {a : A} : ⨆'{a} = a := have ⨆'{a} ≤ a, from Sup_le (take b, suppose b ∈ '{a}, assert b = a, from eq_of_mem_singleton this, by rewrite this; apply le.refl), have a ≤ ⨆'{a}, from le_Sup !mem_insert, le.antisymm `⨆'{a} ≤ a` `a ≤ ⨆'{a}` lemma Inf_antimono {s₁ s₂ : set A} : s₁ ⊆ s₂ → ⨅ s₂ ≤ ⨅ s₁ := suppose s₁ ⊆ s₂, le_Inf (take a : A, suppose a ∈ s₁, Inf_le (mem_of_subset_of_mem `s₁ ⊆ s₂` `a ∈ s₁`)) lemma Sup_mono {s₁ s₂ : set A} : s₁ ⊆ s₂ → ⨆ s₁ ≤ ⨆ s₂ := suppose s₁ ⊆ s₂, Sup_le (take a : A, suppose a ∈ s₁, le_Sup (mem_of_subset_of_mem `s₁ ⊆ s₂` `a ∈ s₁`)) lemma Inf_union (s₁ s₂ : set A) : ⨅ (s₁ ∪ s₂) = (⨅s₁) ⊓ (⨅s₂) := have le₁ : ⨅ (s₁ ∪ s₂) ≤ (⨅s₁) ⊓ (⨅s₂), from !le_inf (le_Inf (take a : A, suppose a ∈ s₁, Inf_le (mem_unionl `a ∈ s₁`))) (le_Inf (take a : A, suppose a ∈ s₂, Inf_le (mem_unionr `a ∈ s₂`))), have le₂ : (⨅s₁) ⊓ (⨅s₂) ≤ ⨅ (s₁ ∪ s₂), from le_Inf (take a : A, suppose a ∈ s₁ ∪ s₂, or.elim this (suppose a ∈ s₁, have (⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₁, from !inf_le_left, have ⨅s₁ ≤ a, from Inf_le `a ∈ s₁`, le.trans `(⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₁` `⨅s₁ ≤ a`) (suppose a ∈ s₂, have (⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₂, from !inf_le_right, have ⨅s₂ ≤ a, from Inf_le `a ∈ s₂`, le.trans `(⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₂` `⨅s₂ ≤ a`)), le.antisymm le₁ le₂ lemma Sup_union (s₁ s₂ : set A) : ⨆ (s₁ ∪ s₂) = (⨆s₁) ⊔ (⨆s₂) := have le₁ : ⨆ (s₁ ∪ s₂) ≤ (⨆s₁) ⊔ (⨆s₂), from Sup_le (take a : A, suppose a ∈ s₁ ∪ s₂, or.elim this (suppose a ∈ s₁, have a ≤ ⨆s₁, from le_Sup `a ∈ s₁`, have ⨆s₁ ≤ (⨆s₁) ⊔ (⨆s₂), from !le_sup_left, le.trans `a ≤ ⨆s₁` `⨆s₁ ≤ (⨆s₁) ⊔ (⨆s₂)`) (suppose a ∈ s₂, have a ≤ ⨆s₂, from le_Sup `a ∈ s₂`, have ⨆s₂ ≤ (⨆s₁) ⊔ (⨆s₂), from !le_sup_right, le.trans `a ≤ ⨆s₂` `⨆s₂ ≤ (⨆s₁) ⊔ (⨆s₂)`)), have le₂ : (⨆s₁) ⊔ (⨆s₂) ≤ ⨆ (s₁ ∪ s₂), from !sup_le (Sup_le (take a : A, suppose a ∈ s₁, le_Sup (mem_unionl `a ∈ s₁`))) (Sup_le (take a : A, suppose a ∈ s₂, le_Sup (mem_unionr `a ∈ s₂`))), le.antisymm le₁ le₂ lemma Inf_empty_eq_Sup_univ : ⨅ (∅ : set A) = ⨆ univ := have le₁ : ⨅ (∅ : set A) ≤ ⨆ univ, from le_Sup !mem_univ, have le₂ : ⨆ univ ≤ ⨅ ∅, from le_Inf (take a : A, suppose a ∈ ∅, absurd this !not_mem_empty), le.antisymm le₁ le₂ lemma Sup_empty_eq_Inf_univ : ⨆ (∅ : set A) = ⨅ univ := have le₁ : ⨆ (∅ : set A) ≤ ⨅ univ, from Sup_le (take a, suppose a ∈ ∅, absurd this !not_mem_empty), have le₂ : ⨅ univ ≤ ⨆ (∅ : set A), from Inf_le !mem_univ, le.antisymm le₁ le₂ lemma Sup_pair (a b : A) : Sup '{a, b} = sup a b := by rewrite [insert_eq, Sup_union, *Sup_singleton] lemma Inf_pair (a b : A) : Inf '{a, b} = inf a b := by rewrite [insert_eq, Inf_union, *Inf_singleton] end complete_lattice /- complete lattice instances -/ section open eq.ops complete_lattice definition complete_lattice_fun [instance] (A B : Type) [complete_lattice B] : complete_lattice (A → B) := ⦃ complete_lattice, lattice_fun A B, Inf := λS x, Inf ((λf, f x) ' S), le_Inf := take f S H x, le_Inf (take y Hy, obtain g `g ∈ S` `g x = y`, from Hy, `g x = y` ▸ H g `g ∈ S` x), Inf_le := take f S `f ∈ S` x, Inf_le (exists.intro f (and.intro `f ∈ S` rfl)), Sup := λS x, Sup ((λf, f x) ' S), le_Sup := take f S `f ∈ S` x, le_Sup (exists.intro f (and.intro `f ∈ S` rfl)), Sup_le := take f S H x, Sup_le (take y Hy, obtain g `g ∈ S` `g x = y`, from Hy, `g x = y` ▸ H g `g ∈ S` x) ⦄ section open classical -- Prop and set are only in the classical setting a complete lattice definition complete_lattice_Prop [instance] : complete_lattice Prop := ⦃ complete_lattice, lattice_Prop, Inf := λS, false ∉ S, le_Inf := take x S H Hx Hf, H _ Hf Hx, Inf_le := take x S Hx Hf, (classical.cases_on x (take x, true.intro) Hf) Hx, Sup := λS, true ∈ S, le_Sup := take x S Hx H, iff_subst (iff.intro (take H, true.intro) (take H', H)) Hx, Sup_le := take x S H Ht, H _ Ht true.intro ⦄ lemma sInter_eq_Inf_fun {A : Type} (S : set (set A)) : ⋂₀ S = @Inf (A → Prop) _ S := funext (take x, calc (⋂₀ S) x = ∀₀ P ∈ S, P x : rfl ... = ¬ (∃₀ P ∈ S, P x = false) : begin rewrite not_bounded_exists, apply bounded_forall_congr, intros, rewrite eq_false, rewrite not_not_iff end ... = @Inf (A → Prop) _ S x : rfl) lemma sUnion_eq_Sup_fun {A : Type} (S : set (set A)) : ⋃₀ S = @Sup (A → Prop) _ S := funext (take x, calc (⋃₀ S) x = ∃₀ P ∈ S, P x : rfl ... = (∃₀ P ∈ S, P x = true) : begin apply bounded_exists_congr, intros, rewrite eq_true end ... = @Sup (A → Prop) _ S x : rfl) definition complete_lattice_set [instance] (A : Type) : complete_lattice (set A) := ⦃ complete_lattice, le := subset, le_refl := @le_refl (A → Prop) _, le_trans := @le_trans (A → Prop) _, le_antisymm := @le_antisymm (A → Prop) _, inf := inter, sup := union, inf_le_left := @inf_le_left (A → Prop) _, inf_le_right := @inf_le_right (A → Prop) _, le_inf := @le_inf (A → Prop) _, le_sup_left := @le_sup_left (A → Prop) _, le_sup_right := @le_sup_right (A → Prop) _, sup_le := @sup_le (A → Prop) _, Inf := sInter, Sup := sUnion, le_Inf := begin intros X S H, rewrite sInter_eq_Inf_fun, apply (@le_Inf (A → Prop) _), exact H end, Inf_le := begin intros X S H, rewrite sInter_eq_Inf_fun, apply (@Inf_le (A → Prop) _), exact H end, le_Sup := begin intros X S H, rewrite sUnion_eq_Sup_fun, apply (@le_Sup (A → Prop) _), exact H end, Sup_le := begin intros X S H, rewrite sUnion_eq_Sup_fun, apply (@Sup_le (A → Prop) _), exact H end ⦄ end end
353d9efafe31691e5212475c53fb47c2269a81d2
46125763b4dbf50619e8846a1371029346f4c3db
/src/measure_theory/ae_eq_fun.lean
53542bd2fb640920efc068e1211db3e3a55a3bf1
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
22,578
lean
/- Copyright (c) 2019 Johannes Hölzl, Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Zhouhang Zhou -/ import measure_theory.integration /-! # Almost everywhere equal functions Two measurable functions are treated as identical if they are almost everywhere equal. We form the set of equivalence classes under the relation of being almost everywhere equal, which is sometimes known as the `L⁰` space. See `l1_space.lean` for `L¹` space. ## Notation * `α →ₘ β` is the type of `L⁰` space, where `α` is a measure space and `β` is a measurable space. `f : α →ₘ β` is a "function" in `L⁰`. In comments, `[f]` is also used to denote an `L⁰` function. `ₘ` can be typed as `\_m`. Sometimes it is shown as a box if font is missing. ## Main statements * The linear structure of `L⁰` : Addition and scalar multiplication are defined on `L⁰` in the natural way, i.e., `[f] + [g] := [f + g]`, `c • [f] := [c • f]`. So defined, `α →ₘ β` inherits the linear structure of `β`. For example, if `β` is a module, then `α →ₘ β` is a module over the same ring. See `mk_add_mk`, `neg_mk`, `mk_sub_mk`, `smul_mk`, `add_to_fun`, `neg_to_fun`, `sub_to_fun`, `smul_to_fun` * The order structure of `L⁰` : `≤` can be defined in a similar way: `[f] ≤ [g]` if `f a ≤ g a` for almost all `a` in domain. And `α →ₘ β` inherits the preorder and partial order of `β`. TODO: Define `sup` and `inf` on `L⁰` so that it forms a lattice. It seems that `β` must be a linear order, since otherwise `f ⊔ g` may not be a measurable function. * Emetric on `L⁰` : If `β` is an `emetric_space`, then `L⁰` can be made into an `emetric_space`, where `edist [f] [g]` is defined to be `∫⁻ a, edist (f a) (g a)`. The integral used here is `lintegral : (α → ennreal) → ennreal`, which is defined in the file `integration.lean`. See `edist_mk_mk` and `edist_to_fun`. ## Implementation notes * `f.to_fun` : To find a representative of `f : α →ₘ β`, use `f.to_fun`. For each operation `op` in `L⁰`, there is a lemma called `op_to_fun`, characterizing, say, `(f op g).to_fun`. * `ae_eq_fun.mk` : To constructs an `L⁰` function `α →ₘ β` from a measurable function `f : α → β`, use `ae_eq_fun.mk` * `comp` : Use `comp g f` to get `[g ∘ f]` from `g : β → γ` and `[f] : α →ₘ γ` * `comp₂` : Use `comp₂ g f₁ f₂ to get `[λa, g (f₁ a) (f₂ a)]`. For example, `[f + g]` is `comp₂ (+)` ## Tags function space, almost everywhere equal, `L⁰`, ae_eq_fun -/ noncomputable theory open_locale classical namespace measure_theory open set lattice filter topological_space universes u v variables {α : Type u} {β : Type v} [measure_space α] section measurable_space variables [measurable_space β] variables (α β) /-- The equivalence relation of being almost everywhere equal -/ instance ae_eq_fun.setoid : setoid { f : α → β // measurable f } := ⟨ λf g, ∀ₘ a, f.1 a = g.1 a, assume ⟨f, hf⟩, by filter_upwards [] assume a, rfl, assume ⟨f, hf⟩ ⟨g, hg⟩ hfg, by filter_upwards [hfg] assume a, eq.symm, assume ⟨f, hf⟩ ⟨g, hg⟩ ⟨h, hh⟩ hfg hgh, by filter_upwards [hfg, hgh] assume a, eq.trans ⟩ /-- The space of equivalence classes of measurable functions, where two measurable functions are equivalent if they agree almost everywhere, i.e., they differ on a set of measure `0`. -/ def ae_eq_fun : Type (max u v) := quotient (ae_eq_fun.setoid α β) variables {α β} infixr ` →ₘ `:25 := ae_eq_fun end measurable_space namespace ae_eq_fun variables [measurable_space β] /-- Construct the equivalence class `[f]` of a measurable function `f`, based on the equivalence relation of being almost everywhere equal. -/ def mk (f : α → β) (hf : measurable f) : α →ₘ β := quotient.mk ⟨f, hf⟩ /-- A representative of an `ae_eq_fun` [f] -/ protected def to_fun (f : α →ₘ β) : α → β := @quotient.out _ (ae_eq_fun.setoid α β) f protected lemma measurable (f : α →ₘ β) : measurable f.to_fun := (@quotient.out _ (ae_eq_fun.setoid α β) f).2 instance : has_coe (α →ₘ β) (α → β) := ⟨λf, f.to_fun⟩ @[simp] lemma quot_mk_eq_mk (f : {f : α → β // measurable f}) : quot.mk setoid.r f = mk f.1 f.2 := by cases f; refl @[simp] lemma mk_eq_mk (f g : α → β) (hf hg) : mk f hf = mk g hg ↔ (∀ₘ a, f a = g a) := ⟨quotient.exact, assume h, quotient.sound h⟩ @[ext] lemma ext (f g : α →ₘ β) (f' g' : α → β) (hf' hg') (hf : mk f' hf' = f) (hg : mk g' hg' = g) (h : ∀ₘ a, f' a = g' a) : f = g := by { rw [← hf, ← hg], rw mk_eq_mk, assumption } lemma self_eq_mk (f : α →ₘ β) : f = mk (f.to_fun) f.measurable := by simp [mk, ae_eq_fun.to_fun] lemma all_ae_mk_to_fun (f : α → β) (hf) : ∀ₘ a, (mk f hf).to_fun a = f a := by rw [← mk_eq_mk _ f _ hf, ← self_eq_mk (mk f hf)] /-- Given a measurable function `g : β → γ`, and an almost everywhere equal function `[f] : α →ₘ β`, return the equivalence class of `g ∘ f`, i.e., the almost everywhere equal function `[g ∘ f] : α →ₘ γ`. -/ def comp {γ : Type*} [measurable_space γ] (g : β → γ) (hg : measurable g) (f : α →ₘ β) : α →ₘ γ := quotient.lift_on f (λf, mk (g ∘ f.1) (measurable.comp hg f.2)) $ assume f₁ f₂ eq, by refine quotient.sound _; filter_upwards [eq] assume a, congr_arg g @[simp] lemma comp_mk {γ : Type*} [measurable_space γ] (g : β → γ) (hg : measurable g) (f : α → β) (hf) : comp g hg (mk f hf) = mk (g ∘ f) (measurable.comp hg hf) := rfl lemma comp_eq_mk_to_fun {γ : Type*} [measurable_space γ] (g : β → γ) (hg : measurable g) (f : α →ₘ β) : comp g hg f = mk (g ∘ f.to_fun) (hg.comp f.measurable) := by conv_lhs { rw [self_eq_mk f, comp_mk] } lemma comp_to_fun {γ : Type*} [measurable_space γ] (g : β → γ) (hg : measurable g) (f : α →ₘ β) : ∀ₘ a, (comp g hg f).to_fun a = (g ∘ f.to_fun) a := by { rw comp_eq_mk_to_fun, apply all_ae_mk_to_fun } /-- Given a measurable function `g : β → γ → δ`, and almost everywhere equal functions `[f₁] : α →ₘ β` and `[f₂] : α →ₘ γ`, return the equivalence class of the function `λa, g (f₁ a) (f₂ a)`, i.e., the almost everywhere equal function `[λa, g (f₁ a) (f₂ a)] : α →ₘ γ` -/ def comp₂ {γ δ : Type*} [measurable_space γ] [measurable_space δ] (g : β → γ → δ) (hg : measurable (λp:β×γ, g p.1 p.2)) (f₁ : α →ₘ β) (f₂ : α →ₘ γ) : α →ₘ δ := begin refine quotient.lift_on₂ f₁ f₂ (λf₁ f₂, mk (λa, g (f₁.1 a) (f₂.1 a)) $ _) _, { exact measurable.comp hg (measurable.prod_mk f₁.2 f₂.2) }, { rintros ⟨f₁, hf₁⟩ ⟨f₂, hf₂⟩ ⟨g₁, hg₁⟩ ⟨g₂, hg₂⟩ h₁ h₂, refine quotient.sound _, filter_upwards [h₁, h₂], simp {contextual := tt} } end @[simp] lemma comp₂_mk_mk {γ δ : Type*} [measurable_space γ] [measurable_space δ] (g : β → γ → δ) (hg : measurable (λp:β×γ, g p.1 p.2)) (f₁ : α → β) (f₂ : α → γ) (hf₁ hf₂) : comp₂ g hg (mk f₁ hf₁) (mk f₂ hf₂) = mk (λa, g (f₁ a) (f₂ a)) (measurable.comp hg (measurable.prod_mk hf₁ hf₂)) := rfl lemma comp₂_eq_mk_to_fun {γ δ : Type*} [measurable_space γ] [measurable_space δ] (g : β → γ → δ) (hg : measurable (λp:β×γ, g p.1 p.2)) (f₁ : α →ₘ β) (f₂ : α →ₘ γ) : comp₂ g hg f₁ f₂ = mk (λa, g (f₁.to_fun a) (f₂.to_fun a)) (hg.comp (measurable.prod_mk f₁.measurable f₂.measurable)) := by conv_lhs { rw [self_eq_mk f₁, self_eq_mk f₂, comp₂_mk_mk] } lemma comp₂_to_fun {γ δ : Type*} [measurable_space γ] [measurable_space δ] (g : β → γ → δ) (hg : measurable (λp:β×γ, g p.1 p.2)) (f₁ : α →ₘ β) (f₂ : α →ₘ γ) : ∀ₘ a, (comp₂ g hg f₁ f₂).to_fun a = g (f₁.to_fun a) (f₂.to_fun a) := by { rw comp₂_eq_mk_to_fun, apply all_ae_mk_to_fun } /-- Given a predicate `p` and an equivalence class `[f]`, return true if `p` holds of `f a` for almost all `a` -/ def lift_pred (p : β → Prop) (f : α →ₘ β) : Prop := quotient.lift_on f (λf, ∀ₘ a, p (f.1 a)) begin assume f g h, dsimp, refine propext (all_ae_congr _), filter_upwards [h], simp {contextual := tt} end /-- Given a relation `r` and equivalence class `[f]` and `[g]`, return true if `r` holds of `(f a, g a)` for almost all `a` -/ def lift_rel {γ : Type*} [measurable_space γ] (r : β → γ → Prop) (f : α →ₘ β) (g : α →ₘ γ) : Prop := lift_pred (λp:β×γ, r p.1 p.2) (comp₂ prod.mk (measurable.prod_mk (measurable.fst measurable_id) (measurable.snd measurable_id)) f g) lemma lift_rel_mk_mk {γ : Type*} [measurable_space γ] (r : β → γ → Prop) (f : α → β) (g : α → γ) (hf hg) : lift_rel r (mk f hf) (mk g hg) ↔ ∀ₘ a, r (f a) (g a) := iff.rfl lemma lift_rel_iff_to_fun {γ : Type*} [measurable_space γ] (r : β → γ → Prop) (f : α →ₘ β) (g : α →ₘ γ) : lift_rel r f g ↔ ∀ₘ a, r (f.to_fun a) (g.to_fun a) := by conv_lhs { rw [self_eq_mk f, self_eq_mk g, lift_rel_mk_mk] } section order instance [preorder β] : preorder (α →ₘ β) := { le := lift_rel (≤), le_refl := by rintros ⟨⟨f, hf⟩⟩; exact univ_mem_sets' (assume a, le_refl _), le_trans := begin rintros ⟨⟨f, hf⟩⟩ ⟨⟨g, hg⟩⟩ ⟨⟨h, hh⟩⟩ hfg hgh, filter_upwards [hfg, hgh] assume a, le_trans end } lemma mk_le_mk [preorder β] {f g : α → β} (hf hg) : mk f hf ≤ mk g hg ↔ ∀ₘ a, f a ≤ g a := iff.rfl lemma le_iff_to_fun_le [preorder β] {f g : α →ₘ β} : f ≤ g ↔ ∀ₘ a, f.to_fun a ≤ g.to_fun a := lift_rel_iff_to_fun _ _ _ instance [partial_order β] : partial_order (α →ₘ β) := { le_antisymm := begin rintros ⟨⟨f, hf⟩⟩ ⟨⟨g, hg⟩⟩ hfg hgf, refine quotient.sound _, filter_upwards [hfg, hgf] assume a, le_antisymm end, .. ae_eq_fun.preorder } /- TODO: Prove `L⁰` space is a lattice if β is linear order. What if β is only a lattice? -/ -- instance [linear_order β] : semilattice_sup (α →ₘ β) := -- { sup := comp₂ (⊔) (_), -- .. ae_eq_fun.partial_order } end order variable (α) /-- The equivalence class of a constant function: `[λa:α, b]`, based on the equivalence relation of being almost everywhere equal -/ def const (b : β) : α →ₘ β := mk (λa:α, b) measurable_const lemma const_to_fun (b : β) : ∀ₘ a, (const α b).to_fun a = b := all_ae_mk_to_fun _ _ variable {α} instance [inhabited β] : inhabited (α →ₘ β) := ⟨const _ (default _)⟩ instance [has_zero β] : has_zero (α →ₘ β) := ⟨const α 0⟩ lemma zero_def [has_zero β] : (0 : α →ₘ β) = mk (λa:α, 0) measurable_const := rfl lemma zero_to_fun [has_zero β] : ∀ₘ a, (0 : α →ₘ β).to_fun a = 0 := const_to_fun _ _ instance [has_one β] : has_one (α →ₘ β) := ⟨const α 1⟩ lemma one_def [has_one β] : (1 : α →ₘ β) = mk (λa:α, 1) measurable_const := rfl lemma one_to_fun [has_one β] : ∀ₘ a, (1 : α →ₘ β).to_fun a = 1 := const_to_fun _ _ section add_monoid variables {γ : Type*} [topological_space γ] [second_countable_topology γ] [add_monoid γ] [topological_add_monoid γ] protected def add : (α →ₘ γ) → (α →ₘ γ) → (α →ₘ γ) := comp₂ (+) (measurable.add (measurable.fst measurable_id) (measurable.snd measurable_id)) instance : has_add (α →ₘ γ) := ⟨ae_eq_fun.add⟩ @[simp] lemma mk_add_mk (f g : α → γ) (hf hg) : (mk f hf) + (mk g hg) = mk (f + g) (measurable.add hf hg) := rfl lemma add_to_fun (f g : α →ₘ γ) : ∀ₘ a, (f + g).to_fun a = f.to_fun a + g.to_fun a := comp₂_to_fun _ _ _ _ instance : add_monoid (α →ₘ γ) := { zero := 0, add := ae_eq_fun.add, add_zero := by rintros ⟨a⟩; exact quotient.sound (univ_mem_sets' $ assume a, add_zero _), zero_add := by rintros ⟨a⟩; exact quotient.sound (univ_mem_sets' $ assume a, zero_add _), add_assoc := by rintros ⟨a⟩ ⟨b⟩ ⟨c⟩; exact quotient.sound (univ_mem_sets' $ assume a, add_assoc _ _ _) } end add_monoid section add_comm_monoid variables {γ : Type*} [topological_space γ] [second_countable_topology γ] [add_comm_monoid γ] [topological_add_monoid γ] instance add_comm_monoid : add_comm_monoid (α →ₘ γ) := { add_comm := by rintros ⟨a⟩ ⟨b⟩; exact quotient.sound (univ_mem_sets' $ assume a, add_comm _ _), .. ae_eq_fun.add_monoid } end add_comm_monoid section add_group variables {γ : Type*} [topological_space γ] [add_group γ] [topological_add_group γ] protected def neg : (α →ₘ γ) → (α →ₘ γ) := comp has_neg.neg (measurable.neg measurable_id) instance : has_neg (α →ₘ γ) := ⟨ae_eq_fun.neg⟩ @[simp] lemma neg_mk (f : α → γ) (hf) : -(mk f hf) = mk (-f) (measurable.neg hf) := rfl lemma neg_to_fun (f : α →ₘ γ) : ∀ₘ a, (-f).to_fun a = - f.to_fun a := comp_to_fun _ _ _ variables [second_countable_topology γ] instance : add_group (α →ₘ γ) := { neg := ae_eq_fun.neg, add_left_neg := by rintros ⟨a⟩; exact quotient.sound (univ_mem_sets' $ assume a, add_left_neg _), .. ae_eq_fun.add_monoid } @[simp] lemma mk_sub_mk (f g : α → γ) (hf hg) : (mk f hf) - (mk g hg) = mk (λa, (f a) - (g a)) (measurable.sub hf hg) := rfl lemma sub_to_fun (f g : α →ₘ γ) : ∀ₘ a, (f - g).to_fun a = f.to_fun a - g.to_fun a := begin rw sub_eq_add_neg, filter_upwards [add_to_fun f (-g), neg_to_fun g], assume a, simp only [mem_set_of_eq], repeat {assume h, rw h}, refl end end add_group section add_comm_group variables {γ : Type*} [topological_space γ] [second_countable_topology γ] [add_comm_group γ] [topological_add_group γ] instance : add_comm_group (α →ₘ γ) := { add_comm := ae_eq_fun.add_comm_monoid.add_comm .. ae_eq_fun.add_group } end add_comm_group section semimodule variables {𝕜 : Type*} [semiring 𝕜] [topological_space 𝕜] variables {γ : Type*} [topological_space γ] [add_comm_monoid γ] [semimodule 𝕜 γ] [topological_semimodule 𝕜 γ] protected def smul : 𝕜 → (α →ₘ γ) → (α →ₘ γ) := λ c f, comp (has_scalar.smul c) (measurable.smul _ measurable_id) f instance : has_scalar 𝕜 (α →ₘ γ) := ⟨ae_eq_fun.smul⟩ @[simp] lemma smul_mk (c : 𝕜) (f : α → γ) (hf) : c • (mk f hf) = mk (c • f) (measurable.smul _ hf) := rfl lemma smul_to_fun (c : 𝕜) (f : α →ₘ γ) : ∀ₘ a, (c • f).to_fun a = c • f.to_fun a := comp_to_fun _ _ _ variables [second_countable_topology γ] [topological_add_monoid γ] instance : semimodule 𝕜 (α →ₘ γ) := { one_smul := by { rintros ⟨f, hf⟩, simp only [quot_mk_eq_mk, smul_mk, one_smul] }, mul_smul := by { rintros x y ⟨f, hf⟩, simp only [quot_mk_eq_mk, smul_mk, mul_action.mul_smul x y f], refl }, smul_add := begin rintros x ⟨f, hf⟩ ⟨g, hg⟩, simp only [quot_mk_eq_mk, smul_mk, mk_add_mk], congr, exact smul_add x f g end, smul_zero := by { intro x, simp only [zero_def, smul_mk], congr, exact smul_zero x }, add_smul := begin intros x y, rintro ⟨f, hf⟩, simp only [quot_mk_eq_mk, smul_mk, mk_add_mk], congr, exact add_smul x y f end, zero_smul := by { rintro ⟨f, hf⟩, simp only [quot_mk_eq_mk, smul_mk, zero_def], congr, exact zero_smul 𝕜 f }} instance : mul_action 𝕜 (α →ₘ γ) := by apply_instance end semimodule section module variables {𝕜 : Type*} [ring 𝕜] [topological_space 𝕜] variables {γ : Type*} [topological_space γ] [second_countable_topology γ] [add_comm_group γ] [topological_add_group γ] [module 𝕜 γ] [topological_semimodule 𝕜 γ] instance : module 𝕜 (α →ₘ γ) := { .. ae_eq_fun.semimodule } end module section vector_space variables {𝕜 : Type*} [field 𝕜] [topological_space 𝕜] variables {γ : Type*} [topological_space γ] [second_countable_topology γ] [add_comm_group γ] [topological_add_group γ] [vector_space 𝕜 γ] [topological_semimodule 𝕜 γ] instance : vector_space 𝕜 (α →ₘ γ) := { .. ae_eq_fun.semimodule } end vector_space /- TODO : Prove that `L⁰` is a complete space if the codomain is complete. -/ /- TODO : Multiplicative structure of `L⁰` if useful -/ open ennreal /-- For `f : α → ennreal`, Define `∫ [f]` to be `∫ f` -/ def eintegral (f : α →ₘ ennreal) : ennreal := quotient.lift_on f (λf, lintegral f.1) (assume ⟨f, hf⟩ ⟨g, hg⟩ eq, lintegral_congr_ae eq) @[simp] lemma eintegral_mk (f : α → ennreal) (hf) : eintegral (mk f hf) = lintegral f := rfl lemma eintegral_to_fun (f : α →ₘ ennreal) : eintegral f = lintegral (f.to_fun) := by conv_lhs { rw [self_eq_mk f, eintegral_mk] } @[simp] lemma eintegral_zero : eintegral (0 : α →ₘ ennreal) = 0 := lintegral_zero @[simp] lemma eintegral_eq_zero_iff (f : α →ₘ ennreal) : eintegral f = 0 ↔ f = 0 := begin rcases f with ⟨f, hf⟩, refine iff.trans (lintegral_eq_zero_iff hf) ⟨_, _⟩, { assume h, exact quotient.sound h }, { assume h, exact quotient.exact h } end lemma eintegral_add : ∀(f g : α →ₘ ennreal), eintegral (f + g) = eintegral f + eintegral g := by { rintros ⟨f⟩ ⟨g⟩, simp only [quot_mk_eq_mk, mk_add_mk, eintegral_mk], exact lintegral_add f.2 g.2 } lemma eintegral_le_eintegral {f g : α →ₘ ennreal} (h : f ≤ g) : eintegral f ≤ eintegral g := begin rcases f with ⟨f, hf⟩, rcases g with ⟨g, hg⟩, simp only [quot_mk_eq_mk, eintegral_mk, mk_le_mk] at *, refine lintegral_le_lintegral_ae _, filter_upwards [h], simp end section variables {γ : Type*} [emetric_space γ] [second_countable_topology γ] /-- `comp_edist [f] [g] a` will return `edist (f a) (g a) -/ def comp_edist (f g : α →ₘ γ) : α →ₘ ennreal := comp₂ edist measurable_edist f g lemma comp_edist_to_fun (f g : α →ₘ γ) : ∀ₘ a, (comp_edist f g).to_fun a = edist (f.to_fun a) (g.to_fun a) := comp₂_to_fun _ _ _ _ lemma comp_edist_self : ∀ (f : α →ₘ γ), comp_edist f f = 0 := by rintro ⟨f⟩; refine quotient.sound _; simp only [edist_self] /-- Almost everywhere equal functions form an `emetric_space`, with the emetric defined as `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (α →ₘ γ) := { edist := λf g, eintegral (comp_edist f g), edist_self := assume f, (eintegral_eq_zero_iff _).2 (comp_edist_self _), edist_comm := by rintros ⟨f⟩ ⟨g⟩; simp only [comp_edist, quot_mk_eq_mk, comp₂_mk_mk, edist_comm], edist_triangle := begin rintros ⟨f⟩ ⟨g⟩ ⟨h⟩, simp only [comp_edist, quot_mk_eq_mk, comp₂_mk_mk, (eintegral_add _ _).symm], exact lintegral_le_lintegral _ _ (assume a, edist_triangle _ _ _) end, eq_of_edist_eq_zero := begin rintros ⟨f⟩ ⟨g⟩, simp only [edist, comp_edist, quot_mk_eq_mk, comp₂_mk_mk, eintegral_eq_zero_iff], simp only [zero_def, mk_eq_mk, edist_eq_zero], assume h, assumption end } lemma edist_mk_mk {f g : α → γ} (hf hg) : edist (mk f hf) (mk g hg) = ∫⁻ x, edist (f x) (g x) := rfl lemma edist_to_fun (f g : α →ₘ γ) : edist f g = ∫⁻ x, edist (f.to_fun x) (g.to_fun x) := by conv_lhs { rw [self_eq_mk f, self_eq_mk g, edist_mk_mk] } lemma edist_zero_to_fun [has_zero γ] (f : α →ₘ γ) : edist f 0 = ∫⁻ x, edist (f.to_fun x) 0 := begin rw edist_to_fun, apply lintegral_congr_ae, have : ∀ₘ a:α, (0 : α →ₘ γ).to_fun a = 0 := zero_to_fun, filter_upwards [this], assume a h, simp only [mem_set_of_eq] at *, rw h end end section metric variables {γ : Type*} [metric_space γ] [second_countable_topology γ] lemma edist_mk_mk' {f g : α → γ} (hf hg) : edist (mk f hf) (mk g hg) = ∫⁻ x, nndist (f x) (g x) := show (∫⁻ x, edist (f x) (g x)) = ∫⁻ x, nndist (f x) (g x), from lintegral_congr_ae $all_ae_of_all $ assume a, edist_nndist _ _ lemma edist_to_fun' (f g : α →ₘ γ) : edist f g = ∫⁻ x, nndist (f.to_fun x) (g.to_fun x) := by conv_lhs { rw [self_eq_mk f, self_eq_mk g, edist_mk_mk'] } end metric section normed_group variables {γ : Type*} [normed_group γ] [second_countable_topology γ] lemma edist_eq_add_add : ∀ {f g h : α →ₘ γ}, edist f g = edist (f + h) (g + h) := begin rintros ⟨f⟩ ⟨g⟩ ⟨h⟩, simp only [quot_mk_eq_mk, mk_add_mk, edist_mk_mk'], apply lintegral_congr_ae, filter_upwards [], simp [nndist_eq_nnnorm] end end normed_group section normed_space set_option class.instance_max_depth 100 variables {𝕜 : Type*} [normed_field 𝕜] variables {γ : Type*} [normed_group γ] [second_countable_topology γ] [normed_space 𝕜 γ] lemma edist_smul (x : 𝕜) : ∀ f : α →ₘ γ, edist (x • f) 0 = (ennreal.of_real ∥x∥) * edist f 0 := begin rintros ⟨f, hf⟩, simp only [zero_def, edist_mk_mk', quot_mk_eq_mk, smul_mk], exact calc (∫⁻ (a : α), nndist (x • f a) 0) = (∫⁻ (a : α), (nnnorm x) * nnnorm (f a)) : lintegral_congr_ae $ by { filter_upwards [], assume a, simp [nndist_eq_nnnorm, nnnorm_smul] } ... = _ : lintegral_const_mul _ (measurable.coe_nnnorm hf) ... = _ : begin convert rfl, { rw ← coe_nnnorm, rw [ennreal.of_real], congr, exact nnreal.of_real_coe }, { funext, simp [nndist_eq_nnnorm] } end, end end normed_space section pos_part variables {γ : Type*} [topological_space γ] [decidable_linear_order γ] [order_closed_topology γ] [second_countable_topology γ] [has_zero γ] /-- Positive part of an `ae_eq_fun`. -/ def pos_part (f : α →ₘ γ) : α →ₘ γ := comp₂ max (measurable.max (measurable.fst measurable_id) (measurable.snd measurable_id)) f 0 lemma pos_part_to_fun (f : α →ₘ γ) : ∀ₘ a, (pos_part f).to_fun a = max (f.to_fun a) (0:γ) := begin filter_upwards [comp₂_to_fun max (measurable.max (measurable.fst measurable_id) (measurable.snd measurable_id)) f 0, @ae_eq_fun.zero_to_fun α γ], simp only [mem_set_of_eq], assume a h₁ h₂, rw [pos_part, h₁, h₂] end end pos_part end ae_eq_fun end measure_theory
134b7e22762cc265a4d793ca30f16a8ead072d7f
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Meta/Tactic/Simp/Main.lean
3aa63363d10ac8ebb554c634189fbff591f2732a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
42,094
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.Transform import Lean.Meta.Tactic.Replace import Lean.Meta.Tactic.UnifyEq import Lean.Meta.Tactic.Simp.Rewrite namespace Lean.Meta namespace Simp builtin_initialize congrHypothesisExceptionId : InternalExceptionId ← registerInternalExceptionId `congrHypothesisFailed def throwCongrHypothesisFailed : MetaM α := throw <| Exception.internal congrHypothesisExceptionId /-- Helper method for bootstrapping purposes. It disables `arith` if support theorems have not been defined yet. -/ def Config.updateArith (c : Config) : CoreM Config := do if c.arith then if (← getEnv).contains ``Nat.Linear.ExprCnstr.eq_of_toNormPoly_eq then return c else return { c with arith := false } else return c def Result.getProof (r : Result) : MetaM Expr := do match r.proof? with | some p => return p | none => mkEqRefl r.expr /-- Similar to `Result.getProof`, but adds a `mkExpectedTypeHint` if `proof?` is `none` (i.e., result is definitionally equal to input), but we cannot establish that `source` and `r.expr` are definitionally when using `TransparencyMode.reducible`. -/ def Result.getProof' (source : Expr) (r : Result) : MetaM Expr := do match r.proof? with | some p => return p | none => if (← isDefEq source r.expr) then mkEqRefl r.expr else /- `source` and `r.expr` must be definitionally equal, but are not definitionally equal at `TransparencyMode.reducible` -/ mkExpectedTypeHint (← mkEqRefl r.expr) (← mkEq source r.expr) def mkCongrFun (r : Result) (a : Expr) : MetaM Result := match r.proof? with | none => return { expr := mkApp r.expr a, proof? := none } | some h => return { expr := mkApp r.expr a, proof? := (← Meta.mkCongrFun h a) } def mkCongr (r₁ r₂ : Result) : MetaM Result := let e := mkApp r₁.expr r₂.expr match r₁.proof?, r₂.proof? with | none, none => return { expr := e, proof? := none } | some h, none => return { expr := e, proof? := (← Meta.mkCongrFun h r₂.expr) } | none, some h => return { expr := e, proof? := (← Meta.mkCongrArg r₁.expr h) } | some h₁, some h₂ => return { expr := e, proof? := (← Meta.mkCongr h₁ h₂) } private def mkImpCongr (src : Expr) (r₁ r₂ : Result) : MetaM Result := do let e := src.updateForallE! r₁.expr r₂.expr match r₁.proof?, r₂.proof? with | none, none => return { expr := e, proof? := none } | _, _ => return { expr := e, proof? := (← Meta.mkImpCongr (← r₁.getProof) (← r₂.getProof)) } -- TODO specialize if bootleneck /-- Return true if `e` is of the form `ofNat n` where `n` is a kernel Nat literal -/ def isOfNatNatLit (e : Expr) : Bool := e.isAppOfArity ``OfNat.ofNat 3 && e.appFn!.appArg!.isNatLit private def reduceProj (e : Expr) : MetaM Expr := do match (← reduceProj? e) with | some e => return e | _ => return e private def reduceProjFn? (e : Expr) : SimpM (Option Expr) := do matchConst e.getAppFn (fun _ => pure none) fun cinfo _ => do match (← getProjectionFnInfo? cinfo.name) with | none => return none | some projInfo => if projInfo.fromClass then if (← read).isDeclToUnfold cinfo.name then -- We only unfold class projections when the user explicitly requested them to be unfolded. -- Recall that `unfoldDefinition?` has support for unfolding this kind of projection. withReducibleAndInstances <| unfoldDefinition? e else return none else -- `structure` projection match (← unfoldDefinition? e) with | none => pure none | some e => match (← reduceProj? e.getAppFn) with | some f => return some (mkAppN f e.getAppArgs) | none => return none private def reduceFVar (cfg : Config) (e : Expr) : MetaM Expr := do if cfg.zeta then match (← getFVarLocalDecl e).value? with | some v => return v | none => return e else return e /-- Return true if `declName` is the name of a definition of the form ``` def declName ... := match ... with | ... ``` -/ private partial def isMatchDef (declName : Name) : CoreM Bool := do let .defnInfo info ← getConstInfo declName | return false return go (← getEnv) info.value where go (env : Environment) (e : Expr) : Bool := if e.isLambda then go env e.bindingBody! else let f := e.getAppFn f.isConst && isMatcherCore env f.constName! private def unfold? (e : Expr) : SimpM (Option Expr) := do let f := e.getAppFn if !f.isConst then return none let fName := f.constName! if (← isProjectionFn fName) then return none -- should be reduced by `reduceProjFn?` let ctx ← read if ctx.config.autoUnfold then if ctx.simpTheorems.isErased (.decl fName) then return none else if hasSmartUnfoldingDecl (← getEnv) fName then withDefault <| unfoldDefinition? e else if (← isMatchDef fName) then let some value ← withDefault <| unfoldDefinition? e | return none let .reduced value ← reduceMatcher? value | return none return some value else return none else if ctx.isDeclToUnfold fName then withDefault <| unfoldDefinition? e else return none private partial def reduce (e : Expr) : SimpM Expr := withIncRecDepth do let cfg := (← read).config if cfg.beta then let e' := e.headBeta if e' != e then return (← reduce e') -- TODO: eta reduction if cfg.proj then match (← reduceProjFn? e) with | some e => return (← reduce e) | none => pure () if cfg.iota then match (← reduceRecMatcher? e) with | some e => return (← reduce e) | none => pure () match (← unfold? e) with | some e' => trace[Meta.Tactic.simp.rewrite] "unfold {mkConst e.getAppFn.constName!}, {e} ==> {e'}" recordSimpTheorem (.decl e.getAppFn.constName!) reduce e' | none => return e private partial def dsimp (e : Expr) : M Expr := do let cfg ← getConfig unless cfg.dsimp do return e let pre (e : Expr) : M TransformStep := do if let Step.visit r ← rewritePre e (fun _ => pure none) (rflOnly := true) then if r.expr != e then return .visit r.expr return .continue let post (e : Expr) : M TransformStep := do if let Step.visit r ← rewritePost e (fun _ => pure none) (rflOnly := true) then if r.expr != e then return .visit r.expr let mut eNew ← reduce e if cfg.zeta && eNew.isFVar then eNew ← reduceFVar cfg eNew if eNew != e then return .visit eNew else return .done e transform (usedLetOnly := cfg.zeta) e (pre := pre) (post := post) instance : Inhabited (M α) where default := fun _ _ _ => default partial def lambdaTelescopeDSimp (e : Expr) (k : Array Expr → Expr → M α) : M α := do go #[] e where go (xs : Array Expr) (e : Expr) : M α := do match e with | .lam n d b c => withLocalDecl n c (← dsimp d) fun x => go (xs.push x) (b.instantiate1 x) | e => k xs e inductive SimpLetCase where | dep -- `let x := v; b` is not equivalent to `(fun x => b) v` | nondepDepVar -- `let x := v; b` is equivalent to `(fun x => b) v`, but result type depends on `x` | nondep -- `let x := v; b` is equivalent to `(fun x => b) v`, and result type does not depend on `x` def getSimpLetCase (n : Name) (t : Expr) (b : Expr) : MetaM SimpLetCase := do withLocalDeclD n t fun x => do let bx := b.instantiate1 x /- The following step is potentially very expensive when we have many nested let-decls. TODO: handle a block of nested let decls in a single pass if this becomes a performance problem. -/ if (← isTypeCorrect bx) then let bxType ← whnf (← inferType bx) if (← dependsOn bxType x.fvarId!) then return SimpLetCase.nondepDepVar else return SimpLetCase.nondep else return SimpLetCase.dep /-- Given the application `e`, remove unnecessary casts of the form `Eq.rec a rfl` and `Eq.ndrec a rfl`. -/ partial def removeUnnecessaryCasts (e : Expr) : MetaM Expr := do let mut args := e.getAppArgs let mut modified := false for i in [:args.size] do let arg := args[i]! if isDummyEqRec arg then args := args.set! i (elimDummyEqRec arg) modified := true if modified then return mkAppN e.getAppFn args else return e where isDummyEqRec (e : Expr) : Bool := (e.isAppOfArity ``Eq.rec 6 || e.isAppOfArity ``Eq.ndrec 6) && e.appArg!.isAppOf ``Eq.refl elimDummyEqRec (e : Expr) : Expr := if isDummyEqRec e then elimDummyEqRec e.appFn!.appFn!.appArg! else e partial def simp (e : Expr) : M Result := withIncRecDepth do checkMaxHeartbeats "simp" let cfg ← getConfig if (← isProof e) then return { expr := e } if cfg.memoize then if let some result := (← get).cache.find? e then /- If the result was cached at a dischargeDepth > the current one, it may not be valid. See issue #1234 -/ if result.dischargeDepth ≤ (← readThe Simp.Context).dischargeDepth then return result simpLoop { expr := e } where simpLoop (r : Result) : M Result := do let cfg ← getConfig if (← get).numSteps > cfg.maxSteps then throwError "simp failed, maximum number of steps exceeded" else let init := r.expr modify fun s => { s with numSteps := s.numSteps + 1 } match (← pre r.expr) with | Step.done r' => cacheResult cfg (← mkEqTrans r r') | Step.visit r' => let r ← mkEqTrans r r' let r ← mkEqTrans r (← simpStep r.expr) match (← post r.expr) with | Step.done r' => cacheResult cfg (← mkEqTrans r r') | Step.visit r' => let r ← mkEqTrans r r' if cfg.singlePass || init == r.expr then cacheResult cfg r else simpLoop r simpStep (e : Expr) : M Result := do match e with | Expr.mdata m e => let r ← simp e; return { r with expr := mkMData m r.expr } | Expr.proj .. => simpProj e | Expr.app .. => simpApp e | Expr.lam .. => simpLambda e | Expr.forallE .. => simpForall e | Expr.letE .. => simpLet e | Expr.const .. => simpConst e | Expr.bvar .. => unreachable! | Expr.sort .. => return { expr := e } | Expr.lit .. => simpLit e | Expr.mvar .. => return { expr := (← instantiateMVars e) } | Expr.fvar .. => return { expr := (← reduceFVar (← getConfig) e) } simpLit (e : Expr) : M Result := do match e.natLit? with | some n => /- If `OfNat.ofNat` is marked to be unfolded, we do not pack orphan nat literals as `OfNat.ofNat` applications to avoid non-termination. See issue #788. -/ if (← readThe Simp.Context).isDeclToUnfold ``OfNat.ofNat then return { expr := e } else return { expr := (← mkNumeral (mkConst ``Nat) n) } | none => return { expr := e } simpProj (e : Expr) : M Result := do match (← reduceProj? e) with | some e => return { expr := e } | none => let s := e.projExpr! let motive? ← withLocalDeclD `s (← inferType s) fun s => do let p := e.updateProj! s if (← dependsOn (← inferType p) s.fvarId!) then return none else let motive ← mkLambdaFVars #[s] (← mkEq e p) if !(← isTypeCorrect motive) then return none else return some motive if let some motive := motive? then let r ← simp s let eNew := e.updateProj! r.expr match r.proof? with | none => return { expr := eNew } | some h => let hNew ← mkEqNDRec motive (← mkEqRefl e) h return { expr := eNew, proof? := some hNew } else return { expr := (← dsimp e) } congrArgs (r : Result) (args : Array Expr) : M Result := do if args.isEmpty then return r else let infos := (← getFunInfoNArgs r.expr args.size).paramInfo let mut r := r let mut i := 0 for arg in args do trace[Debug.Meta.Tactic.simp] "app [{i}] {infos.size} {arg} hasFwdDeps: {infos[i]!.hasFwdDeps}" if i < infos.size && !infos[i]!.hasFwdDeps then r ← mkCongr r (← simp arg) else if (← whnfD (← inferType r.expr)).isArrow then r ← mkCongr r (← simp arg) else r ← mkCongrFun r (← dsimp arg) i := i + 1 return r visitFn (e : Expr) : M Result := do let f := e.getAppFn let fNew ← simp f if fNew.expr == f then return { expr := e } else let args := e.getAppArgs let eNew := mkAppN fNew.expr args if fNew.proof?.isNone then return { expr := eNew } let mut proof ← fNew.getProof for arg in args do proof ← Meta.mkCongrFun proof arg return { expr := eNew, proof? := proof } mkCongrSimp? (f : Expr) : M (Option CongrTheorem) := do if f.isConst then if (← isMatcher f.constName!) then -- We always use simple congruence theorems for auxiliary match applications return none let info ← getFunInfo f let kinds := getCongrSimpKinds info if kinds.all fun k => match k with | CongrArgKind.fixed => true | CongrArgKind.eq => true | _ => false then /- If all argument kinds are `fixed` or `eq`, then using simple congruence theorems `congr`, `congrArg`, and `congrFun` produces a more compact proof -/ return none match (← get).congrCache.find? f with | some thm? => return thm? | none => let thm? ← mkCongrSimpCore? f info kinds modify fun s => { s with congrCache := s.congrCache.insert f thm? } return thm? /-- Try to use automatically generated congruence theorems. See `mkCongrSimp?`. -/ tryAutoCongrTheorem? (e : Expr) : M (Option Result) := do let f := e.getAppFn -- TODO: cache let some cgrThm ← mkCongrSimp? f | return none if cgrThm.argKinds.size != e.getAppNumArgs then return none let mut simplified := false let mut hasProof := false let mut hasCast := false let mut argsNew := #[] let mut argResults := #[] let args := e.getAppArgs for arg in args, kind in cgrThm.argKinds do match kind with | CongrArgKind.fixed => argsNew := argsNew.push (← dsimp arg) | CongrArgKind.cast => hasCast := true; argsNew := argsNew.push arg | CongrArgKind.subsingletonInst => argsNew := argsNew.push arg | CongrArgKind.eq => let argResult ← simp arg argResults := argResults.push argResult argsNew := argsNew.push argResult.expr if argResult.proof?.isSome then hasProof := true if arg != argResult.expr then simplified := true | _ => unreachable! if !simplified then return some { expr := e } /- If `hasProof` is false, we used to return `mkAppN f argsNew` with `proof? := none`. However, this created a regression when we started using `proof? := none` for `rfl` theorems. Consider the following goal ``` m n : Nat a : Fin n h₁ : m < n h₂ : Nat.pred (Nat.succ m) < n ⊢ Fin.succ (Fin.mk m h₁) = Fin.succ (Fin.mk m.succ.pred h₂) ``` The term `m.succ.pred` is simplified to `m` using a `Nat.pred_succ` which is a `rfl` theorem. The auto generated theorem for `Fin.mk` has casts and if used here at `Fin.mk m.succ.pred h₂`, it produces the term `Fin.mk m (id (Eq.refl m) ▸ h₂)`. The key property here is that the proof `(id (Eq.refl m) ▸ h₂)` has type `m < n`. If we had just returned `mkAppN f argsNew`, the resulting term would be `Fin.mk m h₂` which is type correct, but later we would not be able to apply `eq_self` to ```lean Fin.succ (Fin.mk m h₁) = Fin.succ (Fin.mk m h₂) ``` because we would not be able to establish that `m < n` and `Nat.pred (Nat.succ m) < n` are definitionally equal using `TransparencyMode.reducible` (`Nat.pred` is not reducible). Thus, we decided to return here only if the auto generated congruence theorem does not introduce casts. -/ if !hasProof && !hasCast then return some { expr := mkAppN f argsNew } let mut proof := cgrThm.proof let mut type := cgrThm.type let mut j := 0 -- index at argResults let mut subst := #[] for arg in args, kind in cgrThm.argKinds do proof := mkApp proof arg subst := subst.push arg type := type.bindingBody! match kind with | CongrArgKind.fixed => pure () | CongrArgKind.cast => pure () | CongrArgKind.subsingletonInst => let clsNew := type.bindingDomain!.instantiateRev subst let instNew ← if (← isDefEq (← inferType arg) clsNew) then pure arg else match (← trySynthInstance clsNew) with | LOption.some val => pure val | _ => trace[Meta.Tactic.simp.congr] "failed to synthesize instance{indentExpr clsNew}" return none proof := mkApp proof instNew subst := subst.push instNew type := type.bindingBody! | CongrArgKind.eq => let argResult := argResults[j]! let argProof ← argResult.getProof' arg j := j + 1 proof := mkApp2 proof argResult.expr argProof subst := subst.push argResult.expr |>.push argProof type := type.bindingBody!.bindingBody! | _ => unreachable! let some (_, _, rhs) := type.instantiateRev subst |>.eq? | unreachable! let rhs ← if hasCast then removeUnnecessaryCasts rhs else pure rhs if hasProof then return some { expr := rhs, proof? := proof } else /- See comment above. This is reachable if `hasCast == true`. The `rhs` is not structurally equal to `mkAppN f argsNew` -/ return some { expr := rhs } congrDefault (e : Expr) : M Result := do if let some result ← tryAutoCongrTheorem? e then mkEqTrans result (← visitFn result.expr) else withParent e <| e.withApp fun f args => do congrArgs (← simp f) args /-- Process the given congruence theorem hypothesis. Return true if it made "progress". -/ processCongrHypothesis (h : Expr) : M Bool := do forallTelescopeReducing (← inferType h) fun xs hType => withNewLemmas xs do let lhs ← instantiateMVars hType.appFn!.appArg! let r ← simp lhs let rhs := hType.appArg! rhs.withApp fun m zs => do let val ← mkLambdaFVars zs r.expr unless (← isDefEq m val) do throwCongrHypothesisFailed unless (← isDefEq h (← mkLambdaFVars xs (← r.getProof))) do throwCongrHypothesisFailed /- We used to return `false` if `r.proof? = none` (i.e., an implicit `rfl` proof) because we assumed `dsimp` would also be able to simplify the term, but this is not true for non-trivial user-provided theorems. Example: ``` @[congr] theorem image_congr {f g : α → β} {s : Set α} (h : ∀ a, mem a s → f a = g a) : image f s = image g s := ... example {Γ: Set Nat}: (image (Nat.succ ∘ Nat.succ) Γ) = (image (fun a => a.succ.succ) Γ) := by simp only [Function.comp_apply] ``` `Function.comp_apply` is a `rfl` theorem, but `dsimp` will not apply it because the composition is not fully applied. See comment at issue #1113 Thus, we have an extra check now if `xs.size > 0`. TODO: refine this test. -/ return r.proof?.isSome || (xs.size > 0 && lhs != r.expr) /-- Try to rewrite `e` children using the given congruence theorem -/ trySimpCongrTheorem? (c : SimpCongrTheorem) (e : Expr) : M (Option Result) := withNewMCtxDepth do trace[Debug.Meta.Tactic.simp.congr] "{c.theoremName}, {e}" let thm ← mkConstWithFreshMVarLevels c.theoremName let (xs, bis, type) ← forallMetaTelescopeReducing (← inferType thm) if c.hypothesesPos.any (· ≥ xs.size) then return none let lhs := type.appFn!.appArg! let rhs := type.appArg! let numArgs := lhs.getAppNumArgs let mut e := e let mut extraArgs := #[] if e.getAppNumArgs > numArgs then let args := e.getAppArgs e := mkAppN e.getAppFn args[:numArgs] extraArgs := args[numArgs:].toArray if (← isDefEq lhs e) then let mut modified := false for i in c.hypothesesPos do let x := xs[i]! try if (← processCongrHypothesis x) then modified := true catch ex => trace[Meta.Tactic.simp.congr] "processCongrHypothesis {c.theoremName} failed {← inferType x}" if ex.isMaxRecDepth then -- Recall that `processCongrHypothesis` invokes `simp` recursively. throw ex else return none unless modified do trace[Meta.Tactic.simp.congr] "{c.theoremName} not modified" return none unless (← synthesizeArgs (.decl c.theoremName) xs bis (← read).discharge?) do trace[Meta.Tactic.simp.congr] "{c.theoremName} synthesizeArgs failed" return none let eNew ← instantiateMVars rhs let proof ← instantiateMVars (mkAppN thm xs) congrArgs { expr := eNew, proof? := proof } extraArgs else return none congr (e : Expr) : M Result := do let f := e.getAppFn if f.isConst then let congrThms ← getSimpCongrTheorems let cs := congrThms.get f.constName! for c in cs do match (← trySimpCongrTheorem? c e) with | none => pure () | some r => return r congrDefault e else congrDefault e simpApp (e : Expr) : M Result := do let e ← reduce e if !e.isApp then simp e else if isOfNatNatLit e then -- Recall that we expand "orphan" kernel nat literals `n` into `ofNat n` return { expr := e } else congr e simpConst (e : Expr) : M Result := return { expr := (← reduce e) } withNewLemmas {α} (xs : Array Expr) (f : M α) : M α := do if (← getConfig).contextual then let mut s ← getSimpTheorems let mut updated := false for x in xs do if (← isProof x) then s ← s.addTheorem (.fvar x.fvarId!) x updated := true if updated then withSimpTheorems s f else f else f simpLambda (e : Expr) : M Result := withParent e <| lambdaTelescopeDSimp e fun xs e => withNewLemmas xs do let r ← simp e let eNew ← mkLambdaFVars xs r.expr match r.proof? with | none => return { expr := eNew } | some h => let p ← xs.foldrM (init := h) fun x h => do mkFunExt (← mkLambdaFVars #[x] h) return { expr := eNew, proof? := p } simpArrow (e : Expr) : M Result := do trace[Debug.Meta.Tactic.simp] "arrow {e}" let p := e.bindingDomain! let q := e.bindingBody! let rp ← simp p trace[Debug.Meta.Tactic.simp] "arrow [{(← getConfig).contextual}] {p} [{← isProp p}] -> {q} [{← isProp q}]" if (← pure (← getConfig).contextual <&&> isProp p <&&> isProp q) then trace[Debug.Meta.Tactic.simp] "ctx arrow {rp.expr} -> {q}" withLocalDeclD e.bindingName! rp.expr fun h => do let s ← getSimpTheorems let s ← s.addTheorem (.fvar h.fvarId!) h withSimpTheorems s do let rq ← simp q match rq.proof? with | none => mkImpCongr e rp rq | some hq => let hq ← mkLambdaFVars #[h] hq /- We use the default reducibility setting at `mkImpDepCongrCtx` and `mkImpCongrCtx` because they use the theorems ```lean @implies_dep_congr_ctx : ∀ {p₁ p₂ q₁ : Prop}, p₁ = p₂ → ∀ {q₂ : p₂ → Prop}, (∀ (h : p₂), q₁ = q₂ h) → (p₁ → q₁) = ∀ (h : p₂), q₂ h @implies_congr_ctx : ∀ {p₁ p₂ q₁ q₂ : Prop}, p₁ = p₂ → (p₂ → q₁ = q₂) → (p₁ → q₁) = (p₂ → q₂) ``` And the proofs may be from `rfl` theorems which are now omitted. Moreover, we cannot establish that the two terms are definitionally equal using `withReducible`. TODO (better solution): provide the problematic implicit arguments explicitly. It is more efficient and avoids this problem. -/ if rq.expr.containsFVar h.fvarId! then return { expr := (← mkForallFVars #[h] rq.expr), proof? := (← withDefault <| mkImpDepCongrCtx (← rp.getProof) hq) } else return { expr := e.updateForallE! rp.expr rq.expr, proof? := (← withDefault <| mkImpCongrCtx (← rp.getProof) hq) } else mkImpCongr e rp (← simp q) simpForall (e : Expr) : M Result := withParent e do trace[Debug.Meta.Tactic.simp] "forall {e}" if e.isArrow then simpArrow e else if (← isProp e) then withLocalDecl e.bindingName! e.bindingInfo! e.bindingDomain! fun x => withNewLemmas #[x] do let b := e.bindingBody!.instantiate1 x let rb ← simp b let eNew ← mkForallFVars #[x] rb.expr match rb.proof? with | none => return { expr := eNew } | some h => return { expr := eNew, proof? := (← mkForallCongr (← mkLambdaFVars #[x] h)) } else return { expr := (← dsimp e) } simpLet (e : Expr) : M Result := do let Expr.letE n t v b _ := e | unreachable! if (← getConfig).zeta then return { expr := b.instantiate1 v } else match (← getSimpLetCase n t b) with | SimpLetCase.dep => return { expr := (← dsimp e) } | SimpLetCase.nondep => let rv ← simp v withLocalDeclD n t fun x => do let bx := b.instantiate1 x let rbx ← simp bx let hb? ← match rbx.proof? with | none => pure none | some h => pure (some (← mkLambdaFVars #[x] h)) let e' := mkLet n t rv.expr (← rbx.expr.abstractM #[x]) match rv.proof?, hb? with | none, none => return { expr := e' } | some h, none => return { expr := e', proof? := some (← mkLetValCongr (← mkLambdaFVars #[x] rbx.expr) h) } | _, some h => return { expr := e', proof? := some (← mkLetCongr (← rv.getProof) h) } | SimpLetCase.nondepDepVar => let v' ← dsimp v withLocalDeclD n t fun x => do let bx := b.instantiate1 x let rbx ← simp bx let e' := mkLet n t v' (← rbx.expr.abstractM #[x]) match rbx.proof? with | none => return { expr := e' } | some h => let h ← mkLambdaFVars #[x] h return { expr := e', proof? := some (← mkLetBodyCongr v' h) } cacheResult (cfg : Config) (r : Result) : M Result := do if cfg.memoize then let dischargeDepth := (← readThe Simp.Context).dischargeDepth modify fun s => { s with cache := s.cache.insert e { r with dischargeDepth } } return r def main (e : Expr) (ctx : Context) (usedSimps : UsedSimps := {}) (methods : Methods := {}) : MetaM (Result × UsedSimps) := do let ctx := { ctx with config := (← ctx.config.updateArith) } withConfig (fun c => { c with etaStruct := ctx.config.etaStruct }) <| withReducible do try let (r, s) ← simp e methods ctx |>.run { usedTheorems := usedSimps } pure (r, s.usedTheorems) catch ex => if ex.isMaxHeartbeat then throwNestedTacticEx `simp ex else throw ex def dsimpMain (e : Expr) (ctx : Context) (usedSimps : UsedSimps := {}) (methods : Methods := {}) : MetaM (Expr × UsedSimps) := do withConfig (fun c => { c with etaStruct := ctx.config.etaStruct }) <| withReducible do try let (r, s) ← dsimp e methods ctx |>.run { usedTheorems := usedSimps } pure (r, s.usedTheorems) catch ex => if ex.isMaxHeartbeat then throwNestedTacticEx `dsimp ex else throw ex /-- Return true if `e` is of the form `(x : α) → ... → s = t → ... → False` Recall that this kind of proposition is generated by Lean when creating equations for functions and match-expressions with overlapping cases. Example: the following `match`-expression has overlapping cases. ``` def f (x y : Nat) := match x, y with | Nat.succ n, Nat.succ m => ... | _, _ => 0 ``` The second equation is of the form ``` (x y : Nat) → ((n m : Nat) → x = Nat.succ n → y = Nat.succ m → False) → f x y = 0 ``` The hypothesis `(n m : Nat) → x = Nat.succ n → y = Nat.succ m → False` is essentially saying the first case is not applicable. -/ partial def isEqnThmHypothesis (e : Expr) : Bool := e.isForall && go e where go (e : Expr) : Bool := match e with | .forallE _ d b _ => (d.isEq || d.isHEq || b.hasLooseBVar 0) && go b | _ => e.isConstOf ``False abbrev Discharge := Expr → SimpM (Option Expr) def dischargeUsingAssumption? (e : Expr) : SimpM (Option Expr) := do (← getLCtx).findDeclRevM? fun localDecl => do if localDecl.isAuxDecl then return none else if (← isDefEq e localDecl.type) then return some localDecl.toExpr else return none /-- Tries to solve `e` using `unifyEq?`. It assumes that `isEqnThmHypothesis e` is `true`. -/ partial def dischargeEqnThmHypothesis? (e : Expr) : MetaM (Option Expr) := do assert! isEqnThmHypothesis e let mvar ← mkFreshExprSyntheticOpaqueMVar e withReader (fun ctx => { ctx with canUnfold? := canUnfoldAtMatcher }) do if let .none ← go? mvar.mvarId! then instantiateMVars mvar else return none where go? (mvarId : MVarId) : MetaM (Option MVarId) := try let (fvarId, mvarId) ← mvarId.intro1 mvarId.withContext do let localDecl ← fvarId.getDecl if localDecl.type.isEq || localDecl.type.isHEq then if let some { mvarId, .. } ← unifyEq? mvarId fvarId {} then go? mvarId else return none else go? mvarId catch _ => return some mvarId namespace DefaultMethods mutual partial def discharge? (e : Expr) : SimpM (Option Expr) := do if isEqnThmHypothesis e then if let some r ← dischargeUsingAssumption? e then return some r if let some r ← dischargeEqnThmHypothesis? e then return some r let ctx ← read trace[Meta.Tactic.simp.discharge] ">> discharge?: {e}" if ctx.dischargeDepth >= ctx.config.maxDischargeDepth then trace[Meta.Tactic.simp.discharge] "maximum discharge depth has been reached" return none else withReader (fun ctx => { ctx with dischargeDepth := ctx.dischargeDepth + 1 }) do let r ← simp e { pre := pre, post := post, discharge? := discharge? } if r.expr.isConstOf ``True then try return some (← mkOfEqTrue (← r.getProof)) catch _ => return none else return none partial def pre (e : Expr) : SimpM Step := preDefault e discharge? partial def post (e : Expr) : SimpM Step := postDefault e discharge? end def methods : Methods := { pre := pre, post := post, discharge? := discharge? } end DefaultMethods end Simp open Simp (UsedSimps) def simp (e : Expr) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (usedSimps : UsedSimps := {}) : MetaM (Simp.Result × UsedSimps) := do profileitM Exception "simp" (← getOptions) do match discharge? with | none => Simp.main e ctx usedSimps (methods := Simp.DefaultMethods.methods) | some d => Simp.main e ctx usedSimps (methods := { pre := (Simp.preDefault · d), post := (Simp.postDefault · d), discharge? := d }) def dsimp (e : Expr) (ctx : Simp.Context) (usedSimps : UsedSimps := {}) : MetaM (Expr × UsedSimps) := do profileitM Exception "dsimp" (← getOptions) do Simp.dsimpMain e ctx usedSimps (methods := Simp.DefaultMethods.methods) /-- Auxiliary method. Given the current `target` of `mvarId`, apply `r` which is a new target and proof that it is equaal to the current one. -/ def applySimpResultToTarget (mvarId : MVarId) (target : Expr) (r : Simp.Result) : MetaM MVarId := do match r.proof? with | some proof => mvarId.replaceTargetEq r.expr proof | none => if target != r.expr then mvarId.replaceTargetDefEq r.expr else return mvarId /-- See `simpTarget`. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def simpTargetCore (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (mayCloseGoal := true) (usedSimps : UsedSimps := {}) : MetaM (Option MVarId × UsedSimps) := do let target ← instantiateMVars (← mvarId.getType) let (r, usedSimps) ← simp target ctx discharge? usedSimps if mayCloseGoal && r.expr.isConstOf ``True then match r.proof? with | some proof => mvarId.assign (← mkOfEqTrue proof) | none => mvarId.assign (mkConst ``True.intro) return (none, usedSimps) else return (← applySimpResultToTarget mvarId target r, usedSimps) /-- Simplify the given goal target (aka type). Return `none` if the goal was closed. Return `some mvarId'` otherwise, where `mvarId'` is the simplified new goal. -/ def simpTarget (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (mayCloseGoal := true) (usedSimps : UsedSimps := {}) : MetaM (Option MVarId × UsedSimps) := mvarId.withContext do mvarId.checkNotAssigned `simp simpTargetCore mvarId ctx discharge? mayCloseGoal usedSimps /-- Apply the result `r` for `prop` (which is inhabited by `proof`). Return `none` if the goal was closed. Return `some (proof', prop')` otherwise, where `proof' : prop'` and `prop'` is the simplified `prop`. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def applySimpResultToProp (mvarId : MVarId) (proof : Expr) (prop : Expr) (r : Simp.Result) (mayCloseGoal := true) : MetaM (Option (Expr × Expr)) := do if mayCloseGoal && r.expr.isConstOf ``False then match r.proof? with | some eqProof => mvarId.assign (← mkFalseElim (← mvarId.getType) (← mkEqMP eqProof proof)) | none => mvarId.assign (← mkFalseElim (← mvarId.getType) proof) return none else match r.proof? with | some eqProof => return some ((← mkEqMP eqProof proof), r.expr) | none => if r.expr != prop then return some ((← mkExpectedTypeHint proof r.expr), r.expr) else return some (proof, r.expr) def applySimpResultToFVarId (mvarId : MVarId) (fvarId : FVarId) (r : Simp.Result) (mayCloseGoal : Bool) : MetaM (Option (Expr × Expr)) := do let localDecl ← fvarId.getDecl applySimpResultToProp mvarId (mkFVar fvarId) localDecl.type r mayCloseGoal /-- Simplify `prop` (which is inhabited by `proof`). Return `none` if the goal was closed. Return `some (proof', prop')` otherwise, where `proof' : prop'` and `prop'` is the simplified `prop`. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def simpStep (mvarId : MVarId) (proof : Expr) (prop : Expr) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (mayCloseGoal := true) (usedSimps : UsedSimps := {}) : MetaM (Option (Expr × Expr) × UsedSimps) := do let (r, usedSimps) ← simp prop ctx discharge? usedSimps return (← applySimpResultToProp mvarId proof prop r (mayCloseGoal := mayCloseGoal), usedSimps) def applySimpResultToLocalDeclCore (mvarId : MVarId) (fvarId : FVarId) (r : Option (Expr × Expr)) : MetaM (Option (FVarId × MVarId)) := do match r with | none => return none | some (value, type') => let localDecl ← fvarId.getDecl if localDecl.type != type' then let mvarId ← mvarId.assert localDecl.userName type' value let mvarId ← mvarId.tryClear localDecl.fvarId let (fvarId, mvarId) ← mvarId.intro1P return some (fvarId, mvarId) else return some (fvarId, mvarId) /-- Simplify `simp` result to the given local declaration. Return `none` if the goal was closed. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def applySimpResultToLocalDecl (mvarId : MVarId) (fvarId : FVarId) (r : Simp.Result) (mayCloseGoal : Bool) : MetaM (Option (FVarId × MVarId)) := do if r.proof?.isNone then -- New result is definitionally equal to input. Thus, we can avoid creating a new variable if there are dependencies let mvarId ← mvarId.replaceLocalDeclDefEq fvarId r.expr if mayCloseGoal && r.expr.isConstOf ``False then mvarId.assign (← mkFalseElim (← mvarId.getType) (mkFVar fvarId)) return none else return some (fvarId, mvarId) else applySimpResultToLocalDeclCore mvarId fvarId (← applySimpResultToFVarId mvarId fvarId r mayCloseGoal) def simpLocalDecl (mvarId : MVarId) (fvarId : FVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (mayCloseGoal := true) (usedSimps : UsedSimps := {}) : MetaM (Option (FVarId × MVarId) × UsedSimps) := do mvarId.withContext do mvarId.checkNotAssigned `simp let type ← instantiateMVars (← fvarId.getType) let (r, usedSimps) ← simpStep mvarId (mkFVar fvarId) type ctx discharge? mayCloseGoal usedSimps return (← applySimpResultToLocalDeclCore mvarId fvarId r, usedSimps) def simpGoal (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (simplifyTarget : Bool := true) (fvarIdsToSimp : Array FVarId := #[]) (usedSimps : UsedSimps := {}) : MetaM (Option (Array FVarId × MVarId) × UsedSimps) := do mvarId.withContext do mvarId.checkNotAssigned `simp let mut mvarId := mvarId let mut toAssert := #[] let mut replaced := #[] let mut usedSimps := usedSimps for fvarId in fvarIdsToSimp do let localDecl ← fvarId.getDecl let type ← instantiateMVars localDecl.type let ctx := { ctx with simpTheorems := ctx.simpTheorems.eraseTheorem (.fvar localDecl.fvarId) } let (r, usedSimps') ← simp type ctx discharge? usedSimps usedSimps := usedSimps' match r.proof? with | some _ => match (← applySimpResultToProp mvarId (mkFVar fvarId) type r) with | none => return (none, usedSimps) | some (value, type) => toAssert := toAssert.push { userName := localDecl.userName, type := type, value := value } | none => if r.expr.isConstOf ``False then mvarId.assign (← mkFalseElim (← mvarId.getType) (mkFVar fvarId)) return (none, usedSimps) -- TODO: if there are no forwards dependencies we may consider using the same approach we used when `r.proof?` is a `some ...` -- Reason: it introduces a `mkExpectedTypeHint` mvarId ← mvarId.replaceLocalDeclDefEq fvarId r.expr replaced := replaced.push fvarId if simplifyTarget then match (← simpTarget mvarId ctx discharge?) with | (none, usedSimps') => return (none, usedSimps') | (some mvarIdNew, usedSimps') => mvarId := mvarIdNew; usedSimps := usedSimps' let (fvarIdsNew, mvarIdNew) ← mvarId.assertHypotheses toAssert let toClear := fvarIdsToSimp.filter fun fvarId => !replaced.contains fvarId let mvarIdNew ← mvarIdNew.tryClearMany toClear return (some (fvarIdsNew, mvarIdNew), usedSimps) def simpTargetStar (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (usedSimps : UsedSimps := {}) : MetaM (TacticResultCNM × UsedSimps) := mvarId.withContext do let mut ctx := ctx for h in (← getPropHyps) do let localDecl ← h.getDecl let proof := localDecl.toExpr let simpTheorems ← ctx.simpTheorems.addTheorem (.fvar h) proof ctx := { ctx with simpTheorems } match (← simpTarget mvarId ctx discharge? (usedSimps := usedSimps)) with | (none, usedSimps) => return (TacticResultCNM.closed, usedSimps) | (some mvarId', usedSimps') => if (← mvarId.getType) == (← mvarId'.getType) then return (TacticResultCNM.noChange, usedSimps) else return (TacticResultCNM.modified mvarId', usedSimps') def dsimpGoal (mvarId : MVarId) (ctx : Simp.Context) (simplifyTarget : Bool := true) (fvarIdsToSimp : Array FVarId := #[]) (usedSimps : UsedSimps := {}) : MetaM (Option MVarId × UsedSimps) := do mvarId.withContext do mvarId.checkNotAssigned `simp let mut mvarId := mvarId let mut usedSimps : UsedSimps := usedSimps for fvarId in fvarIdsToSimp do let type ← instantiateMVars (← fvarId.getType) let (typeNew, usedSimps') ← dsimp type ctx usedSimps := usedSimps' if typeNew.isConstOf ``False then mvarId.assign (← mkFalseElim (← mvarId.getType) (mkFVar fvarId)) return (none, usedSimps) if typeNew != type then mvarId ← mvarId.replaceLocalDeclDefEq fvarId typeNew if simplifyTarget then let target ← mvarId.getType let (targetNew, usedSimps') ← dsimp target ctx usedSimps usedSimps := usedSimps' if targetNew.isConstOf ``True then mvarId.assign (mkConst ``True.intro) return (none, usedSimps) if let some (_, lhs, rhs) := targetNew.eq? then if (← withReducible <| isDefEq lhs rhs) then mvarId.assign (← mkEqRefl lhs) return (none, usedSimps) if target != targetNew then mvarId ← mvarId.replaceTargetDefEq targetNew pure () -- FIXME: bug in do notation if this is removed? return (some mvarId, usedSimps) end Lean.Meta
e97caf5cb729aa259a91c44c1e0f6327f6f0a5ec
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/ordered_field_auto.lean
2a29126794c25697e6afbb4fc2e1e1b38247ff4c
[]
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
38,631
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Mario Carneiro, Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.ordered_ring import Mathlib.algebra.field import Mathlib.tactic.monotonicity.basic import Mathlib.PostPort universes u_2 l u_1 namespace Mathlib /-! ### Linear ordered fields A linear ordered field is a field equipped with a linear order such that * addition respects the order: `a ≤ b → c + a ≤ c + b`; * multiplication of positives is positive: `0 < a → 0 < b → 0 < a * b`; * `0 < 1`. ### Main Definitions * `linear_ordered_field`: the class of linear ordered fields. -/ /-- A linear ordered field is a field with a linear order respecting the operations. -/ class linear_ordered_field (α : Type u_2) extends linear_ordered_comm_ring α, field α where /-! ### Lemmas about pos, nonneg, nonpos, neg -/ @[simp] theorem inv_pos {α : Type u_1} [linear_ordered_field α] {a : α} : 0 < (a⁻¹) ↔ 0 < a := sorry @[simp] theorem inv_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} : 0 ≤ (a⁻¹) ↔ 0 ≤ a := sorry @[simp] theorem inv_lt_zero {α : Type u_1} [linear_ordered_field α] {a : α} : a⁻¹ < 0 ↔ a < 0 := sorry @[simp] theorem inv_nonpos {α : Type u_1} [linear_ordered_field α] {a : α} : a⁻¹ ≤ 0 ↔ a ≤ 0 := sorry theorem one_div_pos {α : Type u_1} [linear_ordered_field α] {a : α} : 0 < 1 / a ↔ 0 < a := inv_eq_one_div a ▸ inv_pos theorem one_div_neg {α : Type u_1} [linear_ordered_field α] {a : α} : 1 / a < 0 ↔ a < 0 := inv_eq_one_div a ▸ inv_lt_zero theorem one_div_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} : 0 ≤ 1 / a ↔ 0 ≤ a := inv_eq_one_div a ▸ inv_nonneg theorem one_div_nonpos {α : Type u_1} [linear_ordered_field α] {a : α} : 1 / a ≤ 0 ↔ a ≤ 0 := inv_eq_one_div a ▸ inv_nonpos theorem div_pos_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : 0 < a / b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := sorry theorem div_neg_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : a / b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b := sorry theorem div_nonneg_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : 0 ≤ a / b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := sorry theorem div_nonpos_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : a / b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := sorry theorem div_pos {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : 0 < a / b := mul_pos ha (iff.mpr inv_pos hb) theorem div_pos_of_neg_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : 0 < a / b := mul_pos_of_neg_of_neg ha (iff.mpr inv_lt_zero hb) theorem div_neg_of_neg_of_pos {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : 0 < b) : a / b < 0 := mul_neg_of_neg_of_pos ha (iff.mpr inv_pos hb) theorem div_neg_of_pos_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : b < 0) : a / b < 0 := mul_neg_of_pos_of_neg ha (iff.mpr inv_lt_zero hb) theorem div_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b := mul_nonneg ha (iff.mpr inv_nonneg hb) theorem div_nonneg_of_nonpos {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a / b := mul_nonneg_of_nonpos_of_nonpos ha (iff.mpr inv_nonpos hb) theorem div_nonpos_of_nonpos_of_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a ≤ 0) (hb : 0 ≤ b) : a / b ≤ 0 := mul_nonpos_of_nonpos_of_nonneg ha (iff.mpr inv_nonneg hb) theorem div_nonpos_of_nonneg_of_nonpos {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 ≤ a) (hb : b ≤ 0) : a / b ≤ 0 := mul_nonpos_of_nonneg_of_nonpos ha (iff.mpr inv_nonpos hb) /-! ### Relating one division with another term. -/ theorem le_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a ≤ b / c ↔ a * c ≤ b := sorry theorem le_div_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a ≤ b / c ↔ c * a ≤ b := eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b / c ↔ c * a ≤ b)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b / c ↔ a * c ≤ b)) (propext (le_div_iff hc)))) (iff.refl (a * c ≤ b))) theorem div_le_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hb : 0 < b) : a / b ≤ c ↔ a ≤ c * b := sorry theorem div_le_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hb : 0 < b) : a / b ≤ c ↔ a ≤ b * c := eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ c ↔ a ≤ b * c)) (mul_comm b c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ c ↔ a ≤ c * b)) (propext (div_le_iff hb)))) (iff.refl (a ≤ c * b))) theorem lt_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a < b / c ↔ a * c < b := lt_iff_lt_of_le_iff_le (div_le_iff hc) theorem lt_div_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a < b / c ↔ c * a < b := eq.mpr (id (Eq._oldrec (Eq.refl (a < b / c ↔ c * a < b)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < b / c ↔ a * c < b)) (propext (lt_div_iff hc)))) (iff.refl (a * c < b))) theorem div_lt_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : b / c < a ↔ b < a * c := lt_iff_lt_of_le_iff_le (le_div_iff hc) theorem div_lt_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : b / c < a ↔ b < c * a := eq.mpr (id (Eq._oldrec (Eq.refl (b / c < a ↔ b < c * a)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / c < a ↔ b < a * c)) (propext (div_lt_iff hc)))) (iff.refl (b < a * c))) theorem inv_mul_le_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ b * c := sorry theorem inv_mul_le_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ c * b := eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a ≤ c ↔ a ≤ c * b)) (propext (inv_mul_le_iff h)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b * c ↔ a ≤ c * b)) (mul_comm b c))) (iff.refl (a ≤ c * b))) theorem mul_inv_le_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : a * (b⁻¹) ≤ c ↔ a ≤ b * c := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) ≤ c ↔ a ≤ b * c)) (mul_comm a (b⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a ≤ c ↔ a ≤ b * c)) (propext (inv_mul_le_iff h)))) (iff.refl (a ≤ b * c))) theorem mul_inv_le_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : a * (b⁻¹) ≤ c ↔ a ≤ c * b := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) ≤ c ↔ a ≤ c * b)) (mul_comm a (b⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a ≤ c ↔ a ≤ c * b)) (propext (inv_mul_le_iff' h)))) (iff.refl (a ≤ c * b))) theorem inv_mul_lt_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : b⁻¹ * a < c ↔ a < b * c := sorry theorem inv_mul_lt_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : b⁻¹ * a < c ↔ a < c * b := eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a < c ↔ a < c * b)) (propext (inv_mul_lt_iff h)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < b * c ↔ a < c * b)) (mul_comm b c))) (iff.refl (a < c * b))) theorem mul_inv_lt_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : a * (b⁻¹) < c ↔ a < b * c := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) < c ↔ a < b * c)) (mul_comm a (b⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a < c ↔ a < b * c)) (propext (inv_mul_lt_iff h)))) (iff.refl (a < b * c))) theorem mul_inv_lt_iff' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (h : 0 < b) : a * (b⁻¹) < c ↔ a < c * b := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) < c ↔ a < c * b)) (mul_comm a (b⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ * a < c ↔ a < c * b)) (propext (inv_mul_lt_iff' h)))) (iff.refl (a < c * b))) theorem inv_pos_le_iff_one_le_mul {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) : a⁻¹ ≤ b ↔ 1 ≤ b * a := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ ≤ b ↔ 1 ≤ b * a)) (inv_eq_one_div a))) (div_le_iff ha) theorem inv_pos_le_iff_one_le_mul' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) : a⁻¹ ≤ b ↔ 1 ≤ a * b := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ ≤ b ↔ 1 ≤ a * b)) (inv_eq_one_div a))) (div_le_iff' ha) theorem inv_pos_lt_iff_one_lt_mul {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) : a⁻¹ < b ↔ 1 < b * a := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ < b ↔ 1 < b * a)) (inv_eq_one_div a))) (div_lt_iff ha) theorem inv_pos_lt_iff_one_lt_mul' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) : a⁻¹ < b ↔ 1 < a * b := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ < b ↔ 1 < a * b)) (inv_eq_one_div a))) (div_lt_iff' ha) theorem div_le_iff_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : b / c ≤ a ↔ a * c ≤ b := sorry theorem div_le_iff_of_neg' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : b / c ≤ a ↔ c * a ≤ b := eq.mpr (id (Eq._oldrec (Eq.refl (b / c ≤ a ↔ c * a ≤ b)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / c ≤ a ↔ a * c ≤ b)) (propext (div_le_iff_of_neg hc)))) (iff.refl (a * c ≤ b))) theorem le_div_iff_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a ≤ b / c ↔ b ≤ a * c := sorry theorem le_div_iff_of_neg' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a ≤ b / c ↔ b ≤ c * a := eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b / c ↔ b ≤ c * a)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b / c ↔ b ≤ a * c)) (propext (le_div_iff_of_neg hc)))) (iff.refl (b ≤ a * c))) theorem div_lt_iff_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : b / c < a ↔ a * c < b := lt_iff_lt_of_le_iff_le (le_div_iff_of_neg hc) theorem div_lt_iff_of_neg' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : b / c < a ↔ c * a < b := eq.mpr (id (Eq._oldrec (Eq.refl (b / c < a ↔ c * a < b)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / c < a ↔ a * c < b)) (propext (div_lt_iff_of_neg hc)))) (iff.refl (a * c < b))) theorem lt_div_iff_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a < b / c ↔ b < a * c := lt_iff_lt_of_le_iff_le (div_le_iff_of_neg hc) theorem lt_div_iff_of_neg' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a < b / c ↔ b < c * a := eq.mpr (id (Eq._oldrec (Eq.refl (a < b / c ↔ b < c * a)) (mul_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < b / c ↔ b < a * c)) (propext (lt_div_iff_of_neg hc)))) (iff.refl (b < a * c))) /-- One direction of `div_le_iff` where `b` is allowed to be `0` (but `c` must be nonnegative) -/ theorem div_le_of_nonneg_of_le_mul {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hb : 0 ≤ b) (hc : 0 ≤ c) (h : a ≤ c * b) : a / b ≤ c := sorry theorem div_le_one_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (h : a ≤ b) (hb : 0 ≤ b) : a / b ≤ 1 := div_le_of_nonneg_of_le_mul hb zero_le_one (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ 1 * b)) (one_mul b))) h) /-! ### Bi-implications of inequalities using inversions -/ theorem inv_le_inv_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (h : a ≤ b) : b⁻¹ ≤ (a⁻¹) := sorry /-- See `inv_le_inv_of_le` for the implication from right-to-left with one fewer assumption. -/ theorem inv_le_inv {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ (b⁻¹) ↔ b ≤ a := sorry theorem inv_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ ≤ b ↔ b⁻¹ ≤ a)) (Eq.symm (propext (inv_le_inv hb (iff.mpr inv_pos ha)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ ≤ (a⁻¹⁻¹) ↔ b⁻¹ ≤ a)) (inv_inv' a))) (iff.refl (b⁻¹ ≤ a))) theorem le_inv {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a ≤ (b⁻¹) ↔ b ≤ (a⁻¹) := eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ (b⁻¹) ↔ b ≤ (a⁻¹))) (Eq.symm (propext (inv_le_inv (iff.mpr inv_pos hb) ha))))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹⁻¹ ≤ (a⁻¹) ↔ b ≤ (a⁻¹))) (inv_inv' b))) (iff.refl (b ≤ (a⁻¹)))) theorem inv_lt_inv {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a⁻¹ < (b⁻¹) ↔ b < a := lt_iff_lt_of_le_iff_le (inv_le_inv hb ha) theorem inv_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b ↔ b⁻¹ < a := lt_iff_lt_of_le_iff_le (le_inv hb ha) theorem lt_inv {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a < (b⁻¹) ↔ b < (a⁻¹) := lt_iff_lt_of_le_iff_le (inv_le hb ha) theorem inv_le_inv_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ (b⁻¹) ↔ b ≤ a := sorry theorem inv_le_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ ≤ b ↔ b⁻¹ ≤ a)) (Eq.symm (propext (inv_le_inv_of_neg hb (iff.mpr inv_lt_zero ha)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (b⁻¹ ≤ (a⁻¹⁻¹) ↔ b⁻¹ ≤ a)) (inv_inv' a))) (iff.refl (b⁻¹ ≤ a))) theorem le_inv_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a ≤ (b⁻¹) ↔ b ≤ (a⁻¹) := sorry theorem inv_lt_inv_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a⁻¹ < (b⁻¹) ↔ b < a := lt_iff_lt_of_le_iff_le (inv_le_inv_of_neg hb ha) theorem inv_lt_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a⁻¹ < b ↔ b⁻¹ < a := lt_iff_lt_of_le_iff_le (le_inv_of_neg hb ha) theorem lt_inv_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a < (b⁻¹) ↔ b < (a⁻¹) := lt_iff_lt_of_le_iff_le (inv_le_of_neg hb ha) theorem inv_lt_one {α : Type u_1} [linear_ordered_field α] {a : α} (ha : 1 < a) : a⁻¹ < 1 := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ < 1)) (propext (inv_lt (has_lt.lt.trans zero_lt_one ha) zero_lt_one)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1⁻¹ < a)) inv_one)) ha) theorem one_lt_inv {α : Type u_1} [linear_ordered_field α] {a : α} (h₁ : 0 < a) (h₂ : a < 1) : 1 < (a⁻¹) := eq.mpr (id (Eq._oldrec (Eq.refl (1 < (a⁻¹))) (propext (lt_inv zero_lt_one h₁)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < (1⁻¹))) inv_one)) h₂) theorem inv_le_one {α : Type u_1} [linear_ordered_field α] {a : α} (ha : 1 ≤ a) : a⁻¹ ≤ 1 := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ ≤ 1)) (propext (inv_le (has_lt.lt.trans_le zero_lt_one ha) zero_lt_one)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1⁻¹ ≤ a)) inv_one)) ha) theorem one_le_inv {α : Type u_1} [linear_ordered_field α] {a : α} (h₁ : 0 < a) (h₂ : a ≤ 1) : 1 ≤ (a⁻¹) := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ (a⁻¹))) (propext (le_inv zero_lt_one h₁)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ (1⁻¹))) inv_one)) h₂) theorem inv_lt_one_iff_of_pos {α : Type u_1} [linear_ordered_field α] {a : α} (h₀ : 0 < a) : a⁻¹ < 1 ↔ 1 < a := { mp := fun (h₁ : a⁻¹ < 1) => inv_inv' a ▸ one_lt_inv (iff.mpr inv_pos h₀) h₁, mpr := inv_lt_one } theorem inv_lt_one_iff {α : Type u_1} [linear_ordered_field α] {a : α} : a⁻¹ < 1 ↔ a ≤ 0 ∨ 1 < a := sorry theorem one_lt_inv_iff {α : Type u_1} [linear_ordered_field α] {a : α} : 1 < (a⁻¹) ↔ 0 < a ∧ a < 1 := sorry theorem inv_le_one_iff {α : Type u_1} [linear_ordered_field α] {a : α} : a⁻¹ ≤ 1 ↔ a ≤ 0 ∨ 1 ≤ a := sorry theorem one_le_inv_iff {α : Type u_1} [linear_ordered_field α] {a : α} : 1 ≤ (a⁻¹) ↔ 0 < a ∧ a ≤ 1 := sorry /-! ### Relating two divisions. -/ theorem div_le_div_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 ≤ c) (h : a ≤ b) : a / c ≤ b / c := eq.mpr (id (Eq._oldrec (Eq.refl (a / c ≤ b / c)) (div_eq_mul_one_div a c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1 / c) ≤ b / c)) (div_eq_mul_one_div b c))) (mul_le_mul_of_nonneg_right h (iff.mpr one_div_nonneg hc))) theorem div_le_div_of_le_left {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (ha : 0 ≤ a) (hc : 0 < c) (h : c ≤ b) : a / b ≤ a / c := eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ a / c)) (div_eq_mul_inv a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) ≤ a / c)) (div_eq_mul_inv a c))) (mul_le_mul_of_nonneg_left (iff.mpr (inv_le_inv (has_lt.lt.trans_le hc h) hc) h) ha)) theorem div_le_div_of_le_of_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hab : a ≤ b) (hc : 0 ≤ c) : a / c ≤ b / c := div_le_div_of_le hc hab theorem div_le_div_of_nonpos_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c ≤ 0) (h : b ≤ a) : a / c ≤ b / c := eq.mpr (id (Eq._oldrec (Eq.refl (a / c ≤ b / c)) (div_eq_mul_one_div a c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1 / c) ≤ b / c)) (div_eq_mul_one_div b c))) (mul_le_mul_of_nonpos_right h (iff.mpr one_div_nonpos hc))) theorem div_lt_div_of_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) (h : a < b) : a / c < b / c := eq.mpr (id (Eq._oldrec (Eq.refl (a / c < b / c)) (div_eq_mul_one_div a c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1 / c) < b / c)) (div_eq_mul_one_div b c))) (mul_lt_mul_of_pos_right h (iff.mpr one_div_pos hc))) theorem div_lt_div_of_neg_of_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) (h : b < a) : a / c < b / c := eq.mpr (id (Eq._oldrec (Eq.refl (a / c < b / c)) (div_eq_mul_one_div a c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1 / c) < b / c)) (div_eq_mul_one_div b c))) (mul_lt_mul_of_neg_right h (iff.mpr one_div_neg hc))) theorem div_le_div_right {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a / c ≤ b / c ↔ a ≤ b := { mp := le_imp_le_of_lt_imp_lt (div_lt_div_of_lt hc), mpr := div_le_div_of_le (has_lt.lt.le hc) } theorem div_le_div_right_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a / c ≤ b / c ↔ b ≤ a := { mp := le_imp_le_of_lt_imp_lt (div_lt_div_of_neg_of_lt hc), mpr := div_le_div_of_nonpos_of_le (has_lt.lt.le hc) } theorem div_lt_div_right {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : 0 < c) : a / c < b / c ↔ a < b := lt_iff_lt_of_le_iff_le (div_le_div_right hc) theorem div_lt_div_right_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hc : c < 0) : a / c < b / c ↔ b < a := lt_iff_lt_of_le_iff_le (div_le_div_right_of_neg hc) theorem div_lt_div_left {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b < a / c ↔ c < b := iff.trans (mul_lt_mul_left ha) (inv_lt_inv hb hc) theorem div_le_div_left {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b ≤ a / c ↔ c ≤ b := iff.mpr le_iff_le_iff_lt_iff_lt (div_lt_div_left ha hc hb) theorem div_lt_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (b0 : 0 < b) (d0 : 0 < d) : a / b < c / d ↔ a * d < c * b := sorry theorem div_le_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (b0 : 0 < b) (d0 : 0 < d) : a / b ≤ c / d ↔ a * d ≤ c * b := sorry theorem div_le_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hc : 0 ≤ c) (hac : a ≤ c) (hd : 0 < d) (hbd : d ≤ b) : a / b ≤ c / d := eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ c / d)) (propext (div_le_div_iff (has_lt.lt.trans_le hd hbd) hd)))) (mul_le_mul hac hbd (has_lt.lt.le hd) hc) theorem div_lt_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hac : a < c) (hbd : d ≤ b) (c0 : 0 ≤ c) (d0 : 0 < d) : a / b < c / d := iff.mpr (div_lt_div_iff (has_lt.lt.trans_le d0 hbd) d0) (mul_lt_mul hac hbd d0 c0) theorem div_lt_div' {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hac : a ≤ c) (hbd : d < b) (c0 : 0 < c) (d0 : 0 < d) : a / b < c / d := iff.mpr (div_lt_div_iff (has_lt.lt.trans d0 hbd) d0) (mul_lt_mul' hac hbd (has_lt.lt.le d0) c0) theorem div_lt_div_of_lt_left {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} (hb : 0 < b) (h : b < a) (hc : 0 < c) : c / a < c / b := iff.mpr (div_lt_div_left hc (has_lt.lt.trans hb h) hb) h /-! ### Relating one division and involving `1` -/ theorem one_le_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ a / b ↔ b ≤ a)) (propext (le_div_iff hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * b ≤ a ↔ b ≤ a)) (one_mul b))) (iff.refl (b ≤ a))) theorem div_le_one {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : 0 < b) : a / b ≤ 1 ↔ a ≤ b := eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ 1 ↔ a ≤ b)) (propext (div_le_iff hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ 1 * b ↔ a ≤ b)) (one_mul b))) (iff.refl (a ≤ b))) theorem one_lt_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : 0 < b) : 1 < a / b ↔ b < a := eq.mpr (id (Eq._oldrec (Eq.refl (1 < a / b ↔ b < a)) (propext (lt_div_iff hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * b < a ↔ b < a)) (one_mul b))) (iff.refl (b < a))) theorem div_lt_one {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : 0 < b) : a / b < 1 ↔ a < b := eq.mpr (id (Eq._oldrec (Eq.refl (a / b < 1 ↔ a < b)) (propext (div_lt_iff hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < 1 * b ↔ a < b)) (one_mul b))) (iff.refl (a < b))) theorem one_le_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) : 1 ≤ a / b ↔ a ≤ b := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ a / b ↔ a ≤ b)) (propext (le_div_iff_of_neg hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ 1 * b ↔ a ≤ b)) (one_mul b))) (iff.refl (a ≤ b))) theorem div_le_one_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) : a / b ≤ 1 ↔ b ≤ a := eq.mpr (id (Eq._oldrec (Eq.refl (a / b ≤ 1 ↔ b ≤ a)) (propext (div_le_iff_of_neg hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * b ≤ a ↔ b ≤ a)) (one_mul b))) (iff.refl (b ≤ a))) theorem one_lt_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) : 1 < a / b ↔ a < b := eq.mpr (id (Eq._oldrec (Eq.refl (1 < a / b ↔ a < b)) (propext (lt_div_iff_of_neg hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < 1 * b ↔ a < b)) (one_mul b))) (iff.refl (a < b))) theorem div_lt_one_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) : a / b < 1 ↔ b < a := eq.mpr (id (Eq._oldrec (Eq.refl (a / b < 1 ↔ b < a)) (propext (div_lt_iff_of_neg hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * b < a ↔ b < a)) (one_mul b))) (iff.refl (b < a))) theorem one_div_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ b ↔ 1 / b ≤ a := sorry theorem one_div_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : 1 / a < b ↔ 1 / b < a := sorry theorem le_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a ≤ 1 / b ↔ b ≤ 1 / a := sorry theorem lt_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : a < 1 / b ↔ b < 1 / a := sorry theorem one_div_le_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : 1 / a ≤ b ↔ 1 / b ≤ a := sorry theorem one_div_lt_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : 1 / a < b ↔ 1 / b < a := sorry theorem le_one_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a ≤ 1 / b ↔ b ≤ 1 / a := sorry theorem lt_one_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : a < 1 / b ↔ b < 1 / a := sorry theorem one_lt_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : 1 < a / b ↔ 0 < b ∧ b < a ∨ b < 0 ∧ a < b := sorry theorem one_le_div_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : 1 ≤ a / b ↔ 0 < b ∧ b ≤ a ∨ b < 0 ∧ a ≤ b := sorry theorem div_lt_one_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : a / b < 1 ↔ 0 < b ∧ a < b ∨ b = 0 ∨ b < 0 ∧ b < a := sorry theorem div_le_one_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} : a / b ≤ 1 ↔ 0 < b ∧ a ≤ b ∨ b = 0 ∨ b < 0 ∧ b ≤ a := sorry /-! ### Relating two divisions, involving `1` -/ theorem one_div_le_one_div_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (h : a ≤ b) : 1 / b ≤ 1 / a := sorry theorem one_div_lt_one_div_of_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (h : a < b) : 1 / b < 1 / a := eq.mpr (id (Eq._oldrec (Eq.refl (1 / b < 1 / a)) (propext (lt_div_iff' ha)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1 / b) < 1)) (Eq.symm (div_eq_mul_one_div a b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a / b < 1)) (propext (div_lt_one (has_lt.lt.trans ha h))))) h)) theorem one_div_le_one_div_of_neg_of_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) (h : a ≤ b) : 1 / b ≤ 1 / a := eq.mpr (id (Eq._oldrec (Eq.refl (1 / b ≤ 1 / a)) (propext (div_le_iff_of_neg' hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b * (1 / a) ≤ 1)) (Eq.symm (div_eq_mul_one_div b a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / a ≤ 1)) (propext (div_le_one_of_neg (has_le.le.trans_lt h hb))))) h)) theorem one_div_lt_one_div_of_neg_of_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) (h : a < b) : 1 / b < 1 / a := eq.mpr (id (Eq._oldrec (Eq.refl (1 / b < 1 / a)) (propext (div_lt_iff_of_neg' hb)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b * (1 / a) < 1)) (Eq.symm (div_eq_mul_one_div b a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / a < 1)) (propext (div_lt_one_of_neg (has_lt.lt.trans h hb))))) h)) theorem le_of_one_div_le_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (h : 1 / a ≤ 1 / b) : b ≤ a := le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_lt ha) h theorem lt_of_one_div_lt_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (h : 1 / a < 1 / b) : b < a := lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_le ha) h theorem le_of_neg_of_one_div_le_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) (h : 1 / a ≤ 1 / b) : b ≤ a := le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_neg_of_lt hb) h theorem lt_of_neg_of_one_div_lt_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (hb : b < 0) (h : 1 / a < 1 / b) : b < a := lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_neg_of_le hb) h /-- For the single implications with fewer assumptions, see `one_div_le_one_div_of_le` and `le_of_one_div_le_one_div` -/ theorem one_div_le_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ 1 / b ↔ b ≤ a := div_le_div_left zero_lt_one ha hb /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and `lt_of_one_div_lt_one_div` -/ theorem one_div_lt_one_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : 0 < a) (hb : 0 < b) : 1 / a < 1 / b ↔ b < a := div_lt_div_left zero_lt_one ha hb /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_neg_of_lt` and `lt_of_one_div_lt_one_div` -/ theorem one_div_le_one_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : 1 / a ≤ 1 / b ↔ b ≤ a := sorry /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and `lt_of_one_div_lt_one_div` -/ theorem one_div_lt_one_div_of_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (ha : a < 0) (hb : b < 0) : 1 / a < 1 / b ↔ b < a := lt_iff_lt_of_le_iff_le (one_div_le_one_div_of_neg hb ha) theorem one_lt_one_div {α : Type u_1} [linear_ordered_field α] {a : α} (h1 : 0 < a) (h2 : a < 1) : 1 < 1 / a := eq.mpr (id (Eq._oldrec (Eq.refl (1 < 1 / a)) (propext (lt_one_div zero_lt_one h1)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a < 1 / 1)) one_div_one)) h2) theorem one_le_one_div {α : Type u_1} [linear_ordered_field α] {a : α} (h1 : 0 < a) (h2 : a ≤ 1) : 1 ≤ 1 / a := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ 1 / a)) (propext (le_one_div zero_lt_one h1)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ 1 / 1)) one_div_one)) h2) theorem one_div_lt_neg_one {α : Type u_1} [linear_ordered_field α] {a : α} (h1 : a < 0) (h2 : -1 < a) : 1 / a < -1 := (fun (this : 1 / a < 1 / -1) => eq.mp (Eq._oldrec (Eq.refl (1 / a < 1 / -1)) one_div_neg_one_eq_neg_one) this) (one_div_lt_one_div_of_neg_of_lt h1 h2) theorem one_div_le_neg_one {α : Type u_1} [linear_ordered_field α] {a : α} (h1 : a < 0) (h2 : -1 ≤ a) : 1 / a ≤ -1 := (fun (this : 1 / a ≤ 1 / -1) => eq.mp (Eq._oldrec (Eq.refl (1 / a ≤ 1 / -1)) one_div_neg_one_eq_neg_one) this) (one_div_le_one_div_of_neg_of_le h1 h2) /-! ### Results about halving. The equalities also hold in fields of characteristic `0`. -/ theorem add_halves {α : Type u_1} [linear_ordered_field α] (a : α) : a / bit0 1 + a / bit0 1 = a := eq.mpr (id (Eq._oldrec (Eq.refl (a / bit0 1 + a / bit0 1 = a)) (div_add_div_same a a (bit0 1)))) (eq.mpr (id (Eq._oldrec (Eq.refl ((a + a) / bit0 1 = a)) (Eq.symm (two_mul a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (bit0 1 * a / bit0 1 = a)) (mul_div_cancel_left a two_ne_zero))) (Eq.refl a))) theorem sub_self_div_two {α : Type u_1} [linear_ordered_field α] (a : α) : a - a / bit0 1 = a / bit0 1 := sorry theorem div_two_sub_self {α : Type u_1} [linear_ordered_field α] (a : α) : a / bit0 1 - a = -(a / bit0 1) := sorry theorem add_self_div_two {α : Type u_1} [linear_ordered_field α] (a : α) : (a + a) / bit0 1 = a := eq.mpr (id (Eq._oldrec (Eq.refl ((a + a) / bit0 1 = a)) (Eq.symm (mul_two a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * bit0 1 / bit0 1 = a)) (mul_div_cancel a two_ne_zero))) (Eq.refl a)) theorem half_pos {α : Type u_1} [linear_ordered_field α] {a : α} (h : 0 < a) : 0 < a / bit0 1 := div_pos h zero_lt_two theorem one_half_pos {α : Type u_1} [linear_ordered_field α] : 0 < 1 / bit0 1 := half_pos zero_lt_one theorem div_two_lt_of_pos {α : Type u_1} [linear_ordered_field α] {a : α} (h : 0 < a) : a / bit0 1 < a := eq.mpr (id (Eq._oldrec (Eq.refl (a / bit0 1 < a)) (propext (div_lt_iff zero_lt_two)))) (lt_mul_of_one_lt_right h one_lt_two) theorem half_lt_self {α : Type u_1} [linear_ordered_field α] {a : α} : 0 < a → a / bit0 1 < a := div_two_lt_of_pos theorem one_half_lt_one {α : Type u_1} [linear_ordered_field α] : 1 / bit0 1 < 1 := half_lt_self zero_lt_one theorem add_sub_div_two_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (h : a < b) : a + (b - a) / bit0 1 < b := sorry /-! ### Miscellaneous lemmas -/ theorem mul_sub_mul_div_mul_neg_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hc : c ≠ 0) (hd : d ≠ 0) : (a * d - b * c) / (c * d) < 0 ↔ a / c < b / d := sorry theorem mul_sub_mul_div_mul_neg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hc : c ≠ 0) (hd : d ≠ 0) : a / c < b / d → (a * d - b * c) / (c * d) < 0 := iff.mpr (mul_sub_mul_div_mul_neg_iff hc hd) theorem mul_sub_mul_div_mul_nonpos_iff {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hc : c ≠ 0) (hd : d ≠ 0) : (a * d - b * c) / (c * d) ≤ 0 ↔ a / c ≤ b / d := sorry theorem mul_sub_mul_div_mul_nonpos {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (hc : c ≠ 0) (hd : d ≠ 0) : a / c ≤ b / d → (a * d - b * c) / (c * d) ≤ 0 := iff.mpr (mul_sub_mul_div_mul_nonpos_iff hc hd) theorem mul_le_mul_of_mul_div_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} (h : a * (b / c) ≤ d) (hc : 0 < c) : b * a ≤ d * c := eq.mpr (id (Eq._oldrec (Eq.refl (b * a ≤ d * c)) (mul_comm b a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * b ≤ d * c)) (Eq.symm (propext (div_le_iff hc))))) (eq.mp (Eq._oldrec (Eq.refl (a * (b / c) ≤ d)) (Eq.symm mul_div_assoc)) h)) theorem div_mul_le_div_mul_of_div_le_div {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} {c : α} {d : α} {e : α} (h : a / b ≤ c / d) (he : 0 ≤ e) : a / (b * e) ≤ c / (d * e) := eq.mpr (id (Eq._oldrec (Eq.refl (a / (b * e) ≤ c / (d * e))) (div_mul_eq_div_mul_one_div a b e))) (eq.mpr (id (Eq._oldrec (Eq.refl (a / b * (1 / e) ≤ c / (d * e))) (div_mul_eq_div_mul_one_div c d e))) (mul_le_mul_of_nonneg_right h (iff.mpr one_div_nonneg he))) theorem exists_add_lt_and_pos_of_lt {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (h : b < a) : ∃ (c : α), b + c < a ∧ 0 < c := Exists.intro ((a - b) / bit0 1) { left := add_sub_div_two_lt h, right := div_pos (sub_pos_of_lt h) zero_lt_two } theorem le_of_forall_sub_le {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (h : ∀ (ε : α), ε > 0 → b - ε ≤ a) : b ≤ a := sorry theorem monotone.div_const {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [preorder β] {f : β → α} (hf : monotone f) {c : α} (hc : 0 ≤ c) : monotone fun (x : β) => f x / c := monotone.mul_const hf (iff.mpr inv_nonneg hc) theorem strict_mono.div_const {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [preorder β] {f : β → α} (hf : strict_mono f) {c : α} (hc : 0 < c) : strict_mono fun (x : β) => f x / c := strict_mono.mul_const hf (iff.mpr inv_pos hc) protected instance linear_ordered_field.to_densely_ordered {α : Type u_1} [linear_ordered_field α] : densely_ordered α := densely_ordered.mk fun (a₁ a₂ : α) (h : a₁ < a₂) => Exists.intro ((a₁ + a₂) / bit0 1) { left := trans_rel_right Less (Eq.symm (add_self_div_two a₁)) (div_lt_div_of_lt zero_lt_two (add_lt_add_left h a₁)), right := trans_rel_left Less (div_lt_div_of_lt zero_lt_two (add_lt_add_right h a₂)) (add_self_div_two a₂) } theorem mul_self_inj_of_nonneg {α : Type u_1} [linear_ordered_field α] {a : α} {b : α} (a0 : 0 ≤ a) (b0 : 0 ≤ b) : a * a = b * b ↔ a = b := sorry theorem min_div_div_right {α : Type u_1} [linear_ordered_field α] {c : α} (hc : 0 ≤ c) (a : α) (b : α) : min (a / c) (b / c) = min a b / c := Eq.symm (monotone.map_min fun (x y : α) => div_le_div_of_le hc) theorem max_div_div_right {α : Type u_1} [linear_ordered_field α] {c : α} (hc : 0 ≤ c) (a : α) (b : α) : max (a / c) (b / c) = max a b / c := Eq.symm (monotone.map_max fun (x y : α) => div_le_div_of_le hc) theorem min_div_div_right_of_nonpos {α : Type u_1} [linear_ordered_field α] {c : α} (hc : c ≤ 0) (a : α) (b : α) : min (a / c) (b / c) = max a b / c := Eq.symm (monotone.map_max fun (x y : α) => div_le_div_of_nonpos_of_le hc) theorem max_div_div_right_of_nonpos {α : Type u_1} [linear_ordered_field α] {c : α} (hc : c ≤ 0) (a : α) (b : α) : max (a / c) (b / c) = min a b / c := Eq.symm (monotone.map_min fun (x y : α) => div_le_div_of_nonpos_of_le hc) theorem abs_div {α : Type u_1} [linear_ordered_field α] (a : α) (b : α) : abs (a / b) = abs a / abs b := monoid_with_zero_hom.map_div abs_hom a b theorem abs_one_div {α : Type u_1} [linear_ordered_field α] (a : α) : abs (1 / a) = 1 / abs a := eq.mpr (id (Eq._oldrec (Eq.refl (abs (1 / a) = 1 / abs a)) (abs_div 1 a))) (eq.mpr (id (Eq._oldrec (Eq.refl (abs 1 / abs a = 1 / abs a)) abs_one)) (Eq.refl (1 / abs a))) theorem abs_inv {α : Type u_1} [linear_ordered_field α] (a : α) : abs (a⁻¹) = (abs a⁻¹) := monoid_with_zero_hom.map_inv' abs_hom a end Mathlib
ce7d5a5e1cb931a97f9c7ba8d031a29feb84ca6c
731d2ed2a02a9177a189d50e20c2953aa6aae087
/src/problem_sheet_two.lean
e09e6f00c8402b4ccfa9a99bd58480fe1f379615
[]
no_license
DeeproChoudhury/Analysis
8175ae2a787135e7eb2ab070bfd6b77f43dbe2b6
1cfb1c9dd66e1ef316db5d3409262c6448e7e7e5
refs/heads/master
1,673,218,794,058
1,605,315,740,000
1,605,315,740,000
312,717,927
0
0
null
null
null
null
UTF-8
Lean
false
false
5,643
lean
import data.real.basic /-! # Q3 Take bounded, nonempty `S, T ⊆ ℝ`. Define `S + T := { s + t : s ∈ S, t ∈ T}`. Prove `sup(S + T) = sup(S) + sup(T)` -/ -- useful for rewriting theorem is_lub_def {S : set ℝ} {a : ℝ} : is_lub S a ↔ a ∈ upper_bounds S ∧ ∀ x, x ∈ upper_bounds S → a ≤ x := begin refl end #check mem_upper_bounds -- a ∈ upper_bounds S ↔ ∀ x, x ∈ S → x ≤ a /- Useful tactics for this one: push_neg, specialize, have -/ theorem useful_lemma {S : set ℝ} {a : ℝ} (haS : is_lub S a) (t : ℝ) (ht : t < a) : ∃ s, s ∈ S ∧ t < s := begin by_contradiction, push_neg at h, rw is_lub_def at haS, rw ← mem_upper_bounds at h, cases haS with h1 h2, specialize h2 t h, linarith, end /- Useful tactics for this one: `rcases h with ⟨s, t, hsS, htT, rfl⟩` if h : x ∈ S + T `linarith` `by_contra` `set ε := a + b - x with hε` -/ theorem Q3 (S T : set ℝ) (a b : ℝ) : is_lub S a → is_lub T b → is_lub (S + T) (a + b) := begin intros h1 h2, have h1':=h1, have h2':=h2, cases h1 with h3 h4, cases h2 with h5 h6, rw is_lub_def, rw mem_lower_bounds at *, rw mem_upper_bounds at *, split; intros x h8, rcases h8 with ⟨ s,t,hsS,htT,rfl⟩, specialize h5 t, specialize h3 s, have htb:= h5 htT, have hsa:= h3 hsS, linarith, rw mem_upper_bounds at *, by_contradiction, push_neg at h, set ε := a + b - x with hε, have h9: a - ε / 100 <a, linarith, have hb: b - ε / 100<b, linarith, have h10:= useful_lemma h1' (a-ε/100) _, rcases h10 with ⟨s, hs1, hs2⟩, have h11:= useful_lemma h2' (b-ε/100) _, rcases h11 with ⟨t, ht1, ht2⟩, specialize h5 t, specialize h3 s, have htb:= h5 ht1, have hsa:= h3 hs1, specialize h8 (s+t), have hst: s + t ∈ S + T, split, use t, split, assumption, split, assumption, refl, have h11:= h8 hst, linarith, linarith, linarith, end /-! # Q6 -/ -- We introduce the usual mathematical notation for absolute value local notation `|` x `|` := abs x /- Useful for this one: `unfold`, `split_ifs` if you want to prove from first principles, or guessing the name of the library function if you want to use the library. -/ theorem Q6a (x y : ℝ) : | x + y | ≤ | x | + | y | := begin exact abs_add x y, end -- all the rest you're supposed to do using Q6a somehow: -- `simp` and `linarith` are useful. theorem Q6b (x y : ℝ) : |x + y| ≥ |x| - |y| := begin have h:= Q6a, rw ge_iff_le, specialize h (x+y) (-y), simp at h, linarith, end theorem Q6c (x y : ℝ) : |x + y| ≥ |y| - |x| := begin have h:= Q6a, rw ge_iff_le, specialize h (-x) (x+y), simp at h, linarith, end theorem Q6d (x y : ℝ) : |x - y| ≥ | |x| - |y| | := begin rw ge_iff_le, have h: |x| -|y|<0 ∨ 0≤ |x| - |y|, exact lt_or_ge _ 0, cases h, rw abs_of_neg h, simp, have h2:= Q6c, specialize h2 x (-y), simp at h2, have h3: x+-y = x-y, ring, rwa h3 at h2, rw abs_of_nonneg h, have h2:= Q6c, specialize h2 y (-x), simp at h2, ring at h2, exact abs_sub_abs_le_abs_sub x y, end theorem Q6e (x y : ℝ) : |x| ≤ |y| + |x - y| := begin have h2:= Q6a, specialize h2 (x-y) (y), ring at h2, linarith, end theorem Q6f (x y : ℝ) : |x| ≥ |y| - |x - y| := begin have h2:= Q6a, rw ge_iff_le, specialize h2 x (y-x), ring at h2, have h3: abs(x-y) = abs(y-x), apply abs_sub x y, rw h3, linarith, end theorem Q6g (x y z : ℝ) : |x - y| ≤ |x - z| + |y - z| := begin have h2:=Q6a, specialize h2 (x-z) (z-y), ring at h2, have h3: abs(y-z) = abs(z-y), apply abs_sub y z, rwa h3, end /-! # Q4 NOTE: I have not done this one myself -- some lemmas could be wrong! I just copied directly from the problem sheet and you know how sloppy mathematicians are... Fix `a ∈ (0,∞)` and `n : ℕ`. We will prove `∃ x : ℝ, x^n = a`. -/ section Q4 noncomputable theory parameters {a : ℝ} (ha : 0 < a) {n : ℕ} (hn : 0 < n) /- 1) Set `Sₐ := {s ∈ [0,∞) : s^n < a}` and show `Sₐ` is nonempty and bounded above, so we may define `x := sup Sₐ`. -/ def S := {s : ℝ | 0 ≤ s ∧ s ^ n < a} include ha hn theorem part1 : (∃ s : ℝ, s ∈ S) ∧ (∃ B : ℝ, ∀ s ∈ S, s ≤ B ) := sorry def x := Sup S -- the sup is the least upper bound theorem is_lub_x : is_lub S x := begin cases part1 with nonempty bdd, cases nonempty with x hx, cases bdd with y hy, exact real.is_lub_Sup hx hy, end /- 2) For `ε ∈ (0,1)` show `(x+ε)ⁿ ≤ x^n + ε[(x + 1)ⁿ − xⁿ].` (Hint: multiply out.) -/ -- I'm pretty sure this is needed lemma x_nonneg : 0 ≤ x := begin rcases is_lub_x with ⟨h, -⟩, apply h, split, refl, convert ha, simp [hn], end theorem part2 (ε : ℝ) (hε0 : 0 < ε) (hε1 : ε < 1) : (x + ε)^n ≤ x^n + ε*((x+1)^n - x^n) := begin sorry end /- 3) Hence show that if `xⁿ < a` then `∃ ε ∈ (0,1)` such that `(x+ε)ⁿ < a.` (*) -/ theorem part3 (h : x ^ n < a) : ∃ ε : ℝ, 0 < ε ∧ ε < 1 ∧ (x+ε)^n < a := begin sorry end /- 4) If `xⁿ > a`, deduce from (∗) that `∃ ε ∈ (0,1)` such that `(1/x+ε)ⁿ < 1/a`. (∗∗) -/ -- part 4 doesn't quite make sense because we didn't show x ≠ 0 yet lemma easy (h : a < x^n) : x ≠ 0 := begin intro hx, rw hx at h, suffices : a < 0, linarith, convert h, symmetry, -- ?? simp [hn], end theorem part4 (h : a < x^n) : ∃ ε : ℝ, 0 < ε ∧ ε < 1 ∧ (1/x + ε)^n < 1/a := begin sorry end /- 5) Deduce contradictions from (∗) and (∗∗) to show that `xⁿ = a`. -/ theorem part5 : x^n = a := begin sorry end end Q4
5e35ce40a45a027931990739d69944701e4d6075
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/print_no_pattern.lean
ac4db0ff789da060d0aa045f7a3228ceefac78c5
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
19
lean
print [no_pattern]
a4597a518a92b5e834180af080d963b5cf6bdf21
46125763b4dbf50619e8846a1371029346f4c3db
/src/analysis/calculus/times_cont_diff.lean
d3512908479578e0ae84b72818c44d48289459bf
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
80,723
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.fderiv analysis.normed_space.multilinear /-! # Higher differentiability A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. Finally, it is `C^∞` if it is `C^n` for all n. We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain, as well as predicates `times_cont_diff 𝕜 n f` and `times_cont_diff_on 𝕜 n f s` saying that the function is `C^n`, respectively in the whole space or on the set `s`. To avoid the issue of choice when choosing a derivative in sets where the derivative is not necessarily unique, `times_cont_diff_on` is not defined directly in terms of the regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the existence of a nice sequence of derivatives, expressed with a predicate `has_ftaylor_series_up_to_on`. We prove basic properties of these notions. ## Main definitions and results Let `f : E → F` be a map between normed vector spaces over a nondiscrete normed field `𝕜`. * `formal_multilinear_series 𝕜 E F`: a family of `n`-multilinear maps for all `n`, designed to model the sequence of derivatives of a function. * `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`). * `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative is now taken inside `s`. In particular, derivatives don't have to be unique. * `times_cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to rank `n`. * `times_cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`. * `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise. * `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of `iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise. In sets of unique differentiability, `times_cont_diff_on 𝕜 n f s` can be expressed in terms of the properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space, `times_cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f` for `m ≤ n`. We also prove that the usual operations (addition, multiplication, difference, composition, and so on) preserve `C^n` functions. ## Implementation notes ### Definition of `C^n` functions in domains One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n` functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`. This definition still has the problem that a function which is locally `C^n` would not need to be `C^n`, as different choices of sequences of derivatives around different points might possibly not be glued together to give a globally defined sequence of derivatives. Also, there are locality problems in time: one could image a function which, for each `n`, has a nice sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore not be glued to give rise to an infinite sequence of derivatives. This would give a function which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions in space and time in our definition of `times_cont_diff_on`. The resulting definition is slightly more complicated to work with (in fact not so much), but it gives rise to completely satisfactory theorems. ### Side of the composition, and universe issues With a naïve direct definition, the `n`-th derivative of a function belongs to the space `E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space may also be seen as the space of continuous multilinear functions on `n` copies of `E` with values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks, and that we also use. This means that the definition and the first proofs are slightly involved, as one has to keep track of the uncurrying operation. The uncurrying can be done from the left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of the `n`-th derivative, or as the `n`-th derivative of the derivative. For proofs, it would be more convenient to use the latter approach (from the right), as it means to prove things at the `n+1`-th step we only need to understand well enough the derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know enough on the `n`-th derivative to deduce things on the `n+1`-th derivative). However, the definition from the right leads to a universe polymorphism problem: if we define `iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is only possible to generalize over all spaces in some fixed universe in an inductive definition. For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only work if `F` and `E →L[𝕜] F` are in the same universe. This issue does not appear with the definition from the left, where one does not need to generalize over all spaces. Therefore, we use the definition from the left. This means some proofs later on become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the inductive approach where one would prove smoothness statements without giving a formula for the derivative). In the end, this approach is still satisfactory as it is good to have formulas for the iterated derivatives in various constructions. One point where we depart from this explicit approach is in the proof of smoothness of a composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula), but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we give the inductive proof. As explained above, it works by generalizing over the target space, hence it only works well if all spaces belong to the same universe. To get the general version, we lift things to a common universe using a trick. ### Variables management The textbook definitions and proofs use various identifications and abuse of notations, for instance when saying that the natural space in which the derivative lives, i.e., `E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things formally, we need to provide explicit maps for these identifications, and chase some diagrams to see everything is compatible with the identifications. In particular, one needs to check that taking the derivative and then doing the identification, or first doing the identification and then taking the derivative, gives the same result. The key point for this is that taking the derivative commutes with continuous linear equivalences. Therefore, we need to implement all our identifications with continuous linear equivs. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable theory open_locale classical universes u v w open set fin open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] {s s₁ t u : set E} {f f₁ : E → F} {g : F → G} {x : E} {c : F} {b : E × F → G} set_option class.instance_max_depth 370 /-- A formal multilinear series over a field `𝕜`, from `E` to `F`, is given by a family of multilinear maps from `E^n` to `F` for all `n`. -/ @[derive add_comm_group] def formal_multilinear_series (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (F : Type*) [normed_group F] [normed_space 𝕜 F] := Π (n : ℕ), (E [×n]→L[𝕜] F) instance : inhabited (formal_multilinear_series 𝕜 E F) := ⟨0⟩ section module /- `derive` is not able to find the module structure, probably because Lean is confused by the dependent types. We register it explicitly. -/ local attribute [reducible] formal_multilinear_series instance : module 𝕜 (formal_multilinear_series 𝕜 E F) := begin letI : ∀ n, module 𝕜 (continuous_multilinear_map 𝕜 (λ (i : fin n), E) F) := λ n, by apply_instance, apply_instance end end module namespace formal_multilinear_series variables (p : formal_multilinear_series 𝕜 E F) /-- Forgetting the zeroth term in a formal multilinear series, and interpreting the following terms as multilinear maps into `E →L[𝕜] F`. If `p` corresponds to the Taylor series of a function, then `p.shift` is the Taylor series of the derivative of the function. -/ def shift : formal_multilinear_series 𝕜 E (E →L[𝕜] F) := λn, (p n.succ).curry_right /-- Adding a zeroth term to a formal multilinear series taking values in `E →L[𝕜] F`. This corresponds to starting from a Taylor series for the derivative of a function, and building a Taylor series for the function itself. -/ def unshift (q : formal_multilinear_series 𝕜 E (E →L[𝕜] F)) (z : F) : formal_multilinear_series 𝕜 E F | 0 := (continuous_multilinear_curry_fin0 𝕜 E F).symm z | (n + 1) := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin (n + 1)), E) F) (q n) end formal_multilinear_series variable {p : E → formal_multilinear_series 𝕜 E F} /-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_within_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to_on (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E) : Prop := (zero_eq : ∀ x ∈ s, (p x 0).uncurry0 = f x) (fderiv_within : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x ∈ s, has_fderiv_within_at (λ y, p y m) (p x m.succ).curry_left s x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous_on (λ x, p x m) s) lemma has_ftaylor_series_up_to_on.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } /-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a Taylor series for the second one. -/ lemma has_ftaylor_series_up_to_on.congr {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ x ∈ s, f₁ x = f x) : has_ftaylor_series_up_to_on n f₁ p s := begin refine ⟨λ x hx, _, h.fderiv_within, h.cont⟩, rw h₁ x hx, exact h.zero_eq x hx end lemma has_ftaylor_series_up_to_on.mono {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) : has_ftaylor_series_up_to_on n f p t := ⟨λ x hx, h.zero_eq x (hst hx), λ m hm x hx, (h.fderiv_within m hm x (hst hx)).mono hst, λ m hm, (h.cont m hm).mono hst⟩ lemma has_ftaylor_series_up_to_on.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) : has_ftaylor_series_up_to_on m f p s := ⟨h.zero_eq, λ k hk x hx, h.fderiv_within k (lt_of_lt_of_le hk hmn) x hx, λ k hk, h.cont k (le_trans hk hmn)⟩ lemma has_ftaylor_series_up_to_on.continuous_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s := begin have := (h.cont 0 lattice.bot_le).congr (λ x hx, (h.zero_eq' hx).symm), rwa continuous_linear_equiv.comp_continuous_on_iff at this end lemma has_ftaylor_series_up_to_on_zero_iff : has_ftaylor_series_up_to_on 0 f p s ↔ continuous_on f s ∧ (∀ x ∈ s, (p x 0).uncurry0 = f x) := begin refine ⟨λ H, ⟨H.continuous_on, H.zero_eq⟩, λ H, ⟨H.2, λ m hm, false.elim (not_le.2 hm lattice.bot_le), _⟩⟩, assume m hm, have : (m : with_top ℕ) = ((0 : ℕ) : with_bot ℕ) := le_antisymm hm lattice.bot_le, rw with_top.coe_eq_coe at this, rw this, have : ∀ x ∈ s, p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x), by { assume x hx, rw ← H.2 x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ }, rw [continuous_on_congr this, continuous_linear_equiv.comp_continuous_on_iff], exact H.1 end lemma has_ftaylor_series_up_to_on_top_iff : (has_ftaylor_series_up_to_on ⊤ f p s) ↔ (∀ (n : ℕ), has_ftaylor_series_up_to_on n f p s) := begin split, { assume H n, exact H.of_le lattice.le_top }, { assume H, split, { exact (H 0).zero_eq }, { assume m hm, apply (H m.succ).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) }, { assume m hm, apply (H m).cont m (le_refl _) } } end /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_fderiv_within_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) : has_fderiv_within_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x := begin have A : ∀ y ∈ s, f y = (continuous_multilinear_curry_fin0 𝕜 E F) (p y 0), { assume y hy, rw ← h.zero_eq y hy, refl }, suffices H : has_fderiv_within_at (λ y, continuous_multilinear_curry_fin0 𝕜 E F (p y 0)) (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x, by exact H.congr A (A x hx), rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', have : ((0 : ℕ) : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 zero_lt_one) hn, convert h.fderiv_within _ this x hx, ext y v, change (p x 1) (snoc 0 y) = (p x 1) (cons y v), unfold_coes, congr, ext i, have : i = 0 := subsingleton.elim i 0, rw this, refl end lemma has_ftaylor_series_up_to_on.differentiable_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := λ x hx, (h.has_fderiv_within_at hn hx).differentiable_within_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and `p (n + 1)` is a derivative of `p n`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_left {n : ℕ} : has_ftaylor_series_up_to_on (n + 1) f p s ↔ has_ftaylor_series_up_to_on n f p s ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y n) (p x n.succ).curry_left s x) ∧ continuous_on (λ x, p x (n + 1)) s := begin split, { assume h, exact ⟨h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)), h.fderiv_within _ (with_top.coe_lt_coe.2 (lt_add_one n)), h.cont (n + 1) (le_refl _)⟩ }, { assume h, split, { exact h.1.zero_eq }, { assume m hm, by_cases h' : m < n, { exact h.1.fderiv_within m (with_top.coe_lt_coe.2 h') }, { have : m = n := nat.eq_of_lt_succ_of_not_lt (with_top.coe_lt_coe.1 hm) h', rw this, exact h.2.1 } }, { assume m hm, by_cases h' : m ≤ n, { apply h.1.cont m (with_top.coe_le_coe.2 h') }, { have : m = (n + 1) := le_antisymm (with_top.coe_le_coe.1 hm) (not_le.1 h'), rw this, exact h.2.2 } } } end /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to_on ((n + 1) : ℕ) f p s ↔ (∀ x ∈ s, (p x 0).uncurry0 = f x) ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y 0) (p x 1).curry_left s x) ∧ has_ftaylor_series_up_to_on n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) s := begin split, { assume H, refine ⟨H.zero_eq, H.fderiv_within 0 (with_top.coe_lt_coe.2 (nat.succ_pos n)), _⟩, split, { assume x hx, refl }, { assume m (hm : (m : with_top ℕ) < n) x (hx : x ∈ s), have A : (m.succ : with_top ℕ) < n.succ, by { rw with_top.coe_lt_coe at ⊢ hm, exact nat.lt_succ_iff.mpr hm }, change has_fderiv_within_at ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) (p x m.succ.succ).curry_right.curry_left s x, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', convert H.fderiv_within _ A x hx, ext y v, change (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))) = (p x (nat.succ (nat.succ m))) (cons y v), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] }, { assume m (hm : (m : with_top ℕ) ≤ n), have A : (m.succ : with_top ℕ) ≤ n.succ, by { rw with_top.coe_le_coe at ⊢ hm, exact nat.pred_le_iff.mp hm }, change continuous_on ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) s, rw continuous_linear_equiv.comp_continuous_on_iff, exact H.cont _ A } }, { rintros ⟨Hzero_eq, Hfderiv_zero, Htaylor⟩, split, { exact Hzero_eq }, { assume m (hm : (m : with_top ℕ) < n.succ) x (hx : x ∈ s), cases m, { exact Hfderiv_zero x hx }, { have A : (m : with_top ℕ) < n, by { rw with_top.coe_lt_coe at hm ⊢, exact nat.lt_of_succ_lt_succ hm }, have : has_fderiv_within_at ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) ((p x).shift m.succ).curry_left s x := Htaylor.fderiv_within _ A x hx, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff' at this, convert this, ext y v, change (p x (nat.succ (nat.succ m))) (cons y v) = (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] } }, { assume m (hm : (m : with_top ℕ) ≤ n.succ), cases m, { have : differentiable_on 𝕜 (λ x, p x 0) s := λ x hx, (Hfderiv_zero x hx).differentiable_within_at, exact this.continuous_on }, { have A : (m : with_top ℕ) ≤ n, by { rw with_top.coe_le_coe at hm ⊢, exact nat.lt_succ_iff.mp hm }, have : continuous_on ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) s := Htaylor.cont _ A, rwa continuous_linear_equiv.comp_continuous_on_iff at this } } } end variable (𝕜) /-- A function is continuously differentiable up to `n` if it admits derivatives within `s` up to order `n`, which are continuous. There is a subtlety on sets where derivatives are not unique, that choices of derivatives around different points might not match. To ensure that being `C^n` is a local property, we therefore require it locally around each point. There is another subtlety that one might be able to find nice derivatives up to `n` for any finite `n`, but that they don't match so that one can not find them up to infinity. To get a good notion for `n = ∞`, we only require that for any finite `n` we may find such matching derivatives. -/ definition times_cont_diff_on (n : with_top ℕ) (f : E → F) (s : set E) := ∀ (m : ℕ), (m : with_top ℕ) ≤ n → ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on m f p u variable {𝕜} lemma times_cont_diff_on_nat {n : ℕ} : times_cont_diff_on 𝕜 n f s ↔ ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on n f p u := begin refine ⟨λ H, H n (le_refl _), λ H m hm x hx, _⟩, rcases H x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp.of_le hm⟩ end lemma times_cont_diff_on_top : times_cont_diff_on 𝕜 ⊤ f s ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 n f s := begin split, { assume H n m hm x hx, rcases H m lattice.le_top x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp⟩ }, { assume H m hm x hx, rcases H m m (le_refl _) x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp⟩ } end lemma times_cont_diff_on.continuous_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) : continuous_on f s := begin apply continuous_on_of_locally_continuous_on (λ x hx, _), rcases h 0 lattice.bot_le x hx with ⟨u, hu, p, H⟩, rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩, refine ⟨t, t_open, xt, _⟩, rw inter_comm at tu, exact (H.mono tu).continuous_on end lemma times_cont_diff_on.congr {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s := begin assume m hm x hx, rcases h m hm x hx with ⟨u, hu, p, H⟩, refine ⟨u ∩ s, filter.inter_mem_sets hu self_mem_nhds_within, p, _⟩, exact (H.mono (inter_subset_left u s)).congr (λ x hx, h₁ x hx.2) end lemma times_cont_diff_on_congr {n : with_top ℕ} (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s ↔ times_cont_diff_on 𝕜 n f s := ⟨λ H, H.congr (λ x hx, (h₁ x hx).symm), λ H, H.congr h₁⟩ lemma times_cont_diff_on.mono {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) {t : set E} (hst : t ⊆ s) : times_cont_diff_on 𝕜 n f t := begin assume m hm x hx, rcases h m hm x (hst hx) with ⟨u, hu, p, H⟩, exact ⟨u, nhds_within_mono x hst hu, p, H⟩ end lemma times_cont_diff_on.congr_mono {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s₁, f₁ x = f x) (hs : s₁ ⊆ s) : times_cont_diff_on 𝕜 n f₁ s₁ := (hf.mono hs).congr h₁ lemma times_cont_diff_on.of_le {m n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : m ≤ n) : times_cont_diff_on 𝕜 m f s := begin assume k hk x hx, rcases h k (le_trans hk hmn) x hx with ⟨u, hu, p, H⟩, exact ⟨u, hu, p, H⟩ end /-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/ lemma times_cont_diff_on.differentiable_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := begin apply differentiable_on_of_locally_differentiable_on (λ x hx, _), rcases h 1 hn x hx with ⟨u, hu, p, H⟩, rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩, rw inter_comm at tu, exact ⟨t, t_open, xt, (H.mono tu).differentiable_on (le_refl _)⟩ end /-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/ lemma times_cont_diff_on_of_locally_times_cont_diff_on {n : with_top ℕ} (h : ∀ x ∈ s, ∃u, is_open u ∧ x ∈ u ∧ times_cont_diff_on 𝕜 n f (s ∩ u)) : times_cont_diff_on 𝕜 n f s := begin assume m hm x hx, rcases h x hx with ⟨u, u_open, xu, Hu⟩, rcases Hu m hm x ⟨hx, xu⟩ with ⟨v, hv, p, H⟩, rw ← nhds_within_restrict s xu u_open at hv, exact ⟨v, hv, p, H⟩, end /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_on_succ_iff_has_fderiv_within_at {n : ℕ} : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ f' : E → (E →L[𝕜] F), (∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (times_cont_diff_on 𝕜 n f' u) := begin split, { assume h x hx, rcases h n.succ (le_refl _) x hx with ⟨u, hu, p, Hp⟩, refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1), λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩, rw has_ftaylor_series_up_to_on_succ_iff_right at Hp, assume m hm z hz, exact ⟨u, self_mem_nhds_within, λ (x : E), (p x).shift, Hp.2.2.of_le hm⟩ }, { assume h, rw times_cont_diff_on_nat, assume x hx, rcases h x hx with ⟨u, hu, f', f'_eq_deriv, Hf'⟩, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, rcases Hf' n (le_refl _) x xu with ⟨v, hv, p', Hp'⟩, refine ⟨v ∩ u, filter.inter_mem_sets (nhds_within_le_of_mem hu hv) hu, λ x, (p' x).unshift (f x), _⟩, rw has_ftaylor_series_up_to_on_succ_iff_right, refine ⟨λ y hy, rfl, λ y hy, _, _⟩, { change has_fderiv_within_at (λ (z : E), (continuous_multilinear_curry_fin0 𝕜 E F).symm (f z)) ((formal_multilinear_series.unshift (p' y) (f y) 1).curry_left) (v ∩ u) y, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', convert (f'_eq_deriv y hy.2).mono (inter_subset_right v u), rw ← Hp'.zero_eq y hy.1, ext z, change ((p' y 0) (init (@cons 0 (λ i, E) z 0))) (@cons 0 (λ i, E) z 0 (last 0)) = ((p' y 0) 0) z, unfold_coes, congr }, { convert (Hp'.mono (inter_subset_left v u)).congr (λ x hx, Hp'.zero_eq x hx.1), { ext x y, change p' x 0 (init (@snoc 0 (λ i : fin 1, E) 0 y)) y = p' x 0 0 y, rw init_snoc }, { ext x k v y, change p' x k (init (@snoc k (λ i : fin k.succ, E) v y)) (@snoc k (λ i : fin k.succ, E) v y (last k)) = p' x k v y, rw [snoc_last, init_snoc] } } } end /-! ### Iterated derivative within a set -/ variable (𝕜) /-- The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with an uncurrying step to see it as a multilinear map in `n+1` variables.. -/ noncomputable def iterated_fderiv_within (n : ℕ) (f : E → F) (s : set E) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series_within (f : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv_within 𝕜 n f s x variable {𝕜} @[simp] lemma iterated_fderiv_within_zero_apply (m : (fin 0) → E) : (iterated_fderiv_within 𝕜 0 f s x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_within_zero_eq_comp : iterated_fderiv_within 𝕜 0 f s = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_within_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_left {n : ℕ} : iterated_fderiv_within 𝕜 (n + 1) f s = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s) := rfl theorem iterated_fderiv_within_succ_apply_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin (n + 1) → E) : (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s x (init m) (m (last n)) := begin induction n with n IH generalizing x, { rw [iterated_fderiv_within_succ_eq_comp_left, iterated_fderiv_within_zero_eq_comp, iterated_fderiv_within_zero_apply, function.comp_apply, continuous_linear_equiv.comp_fderiv_within _ (hs x hx)], refl }, { let I := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin (n + 1)), E) F), have A : ∀ y ∈ s, iterated_fderiv_within 𝕜 n.succ f s y = (I ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) y, by { assume y hy, ext m, rw @IH m y hy, refl }, calc (iterated_fderiv_within 𝕜 (n+2) f s x : (fin (n+2) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n.succ f s) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : rfl ... = (fderiv_within 𝕜 (I ∘ (iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by rw fderiv_within_congr (hs x hx) A (A x hx) ... = (I ∘ fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by { rw continuous_linear_equiv.comp_fderiv_within _ (hs x hx), refl } ... = (fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (λ y, fderiv_within 𝕜 f s y) s)) s x : E → (E [×n]→L[𝕜] (E →L[𝕜] F))) (m 0) (init (tail m)) ((tail m) (last n)) : rfl ... = iterated_fderiv_within 𝕜 (nat.succ n) (λ y, fderiv_within 𝕜 f s y) s x (init m) (m (last (n + 1))) : by { rw [iterated_fderiv_within_succ_apply_left, tail_init_eq_init_tail], refl } } end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 (n + 1) f s x = ((continuous_multilinear_curry_right_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) x := by { ext m, rw iterated_fderiv_within_succ_apply_right hs hx, refl } @[simp] lemma iterated_fderiv_within_one_apply (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : (fin 1) → E) : (iterated_fderiv_within 𝕜 1 f s x : ((fin 1) → E) → F) m = (fderiv_within 𝕜 f s x : E → F) (m 0) := by { rw [iterated_fderiv_within_succ_apply_right hs hx, iterated_fderiv_within_zero_apply], refl } /-- If two functions coincide on a set `s` of unique differentiability, then their iterated differentials within this set coincide. -/ lemma iterated_fderiv_within_congr {n : ℕ} (hs : unique_diff_on 𝕜 s) (hL : ∀y∈s, f₁ y = f y) (hx : x ∈ s) : iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp [hL x hx] }, { have : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f₁ s y) s x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, this] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with an open set containing `x`. -/ lemma iterated_fderiv_within_inter_open {n : ℕ} (hu : is_open u) (hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp }, { have A : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f (s ∩ u) y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), have B : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_inter (mem_nhds_sets hu hx.2) ((unique_diff_within_at_inter (mem_nhds_sets hu hx.2)).1 (hs x hx)), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, A, B] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x` within `s`. -/ lemma iterated_fderiv_within_inter' {n : ℕ} (hu : u ∈ nhds_within x s) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin obtain ⟨v, v_open, xv, vu⟩ : ∃ v, is_open v ∧ x ∈ v ∧ v ∩ s ⊆ u := mem_nhds_within.1 hu, have A : (s ∩ u) ∩ v = s ∩ v, { apply subset.antisymm (inter_subset_inter (inter_subset_left _ _) (subset.refl _)), exact λ y ⟨ys, yv⟩, ⟨⟨ys, vu ⟨yv, ys⟩⟩, yv⟩ }, have : iterated_fderiv_within 𝕜 n f (s ∩ v) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter_open v_open (hs.inter v_open) ⟨xs, xv⟩, rw ← this, have : iterated_fderiv_within 𝕜 n f ((s ∩ u) ∩ v) x = iterated_fderiv_within 𝕜 n f (s ∩ u) x, { refine iterated_fderiv_within_inter_open v_open _ ⟨⟨xs, vu ⟨xv, xs⟩⟩, xv⟩, rw A, exact hs.inter v_open }, rw A at this, rw ← this end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x`. -/ lemma iterated_fderiv_within_inter {n : ℕ} (hu : u ∈ nhds x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs @[simp] lemma times_cont_diff_on_zero : times_cont_diff_on 𝕜 0 f s ↔ continuous_on f s := begin refine ⟨λ H, H.continuous_on, λ H, _⟩, assume m hm x hx, have : (m : with_top ℕ) = 0 := le_antisymm hm lattice.bot_le, rw this, refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, rw has_ftaylor_series_up_to_on_zero_iff, exact ⟨H, λ x hx, by simp [ftaylor_series_within]⟩ end /-- On a set with unique differentiability, any choice of iterated differential has to coincide with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/ theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {m : ℕ} (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : p x m = iterated_fderiv_within 𝕜 m f s x := begin induction m with m IH generalizing x, { rw [h.zero_eq' hx, iterated_fderiv_within_zero_eq_comp] }, { have A : (m : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 (lt_add_one m)) hmn, have : has_fderiv_within_at (λ (y : E), iterated_fderiv_within 𝕜 m f s y) (continuous_multilinear_map.curry_left (p x (nat.succ m))) s x := (h.fderiv_within m A x hx).congr (λ y hy, (IH (le_of_lt A) hy).symm) (IH (le_of_lt A) hx).symm, rw [iterated_fderiv_within_succ_eq_comp_left, function.comp_apply, this.fderiv_within (hs x hx)], exact (continuous_multilinear_map.uncurry_curry_left _).symm } end /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on.ftaylor_series_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) : has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s := begin split, { assume x hx, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume m hm x hx, rcases h m.succ (with_top.add_one_le_of_lt hm) x hx with ⟨u, hu, p, Hp⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, have : p x m.succ = ftaylor_series_within 𝕜 f s x m.succ, { change p x m.succ = iterated_fderiv_within 𝕜 m.succ f s x, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open xo) hs hx, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hx, xo⟩ }, rw [← this, ← has_fderiv_within_at_inter (mem_nhds_sets o_open xo)], have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (with_top.coe_le_coe.2 (nat.le_succ m)) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) x ⟨hx, xo⟩).congr (λ y hy, (A y hy).symm) (A x ⟨hx, xo⟩).symm }, { assume m hm, apply continuous_on_of_locally_continuous_on, assume x hx, rcases h m hm x hx with ⟨u, hu, p, Hp⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, refine ⟨o, o_open, xo, _⟩, have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).cont m (le_refl _)).congr (λ y hy, (A y hy).symm) } end lemma times_cont_diff_on_of_continuous_on_differentiable_on {n : with_top ℕ} (Hcont : ∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) (Hdiff : ∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) : times_cont_diff_on 𝕜 n f s := begin assume m hm x hx, refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, split, { assume x hx, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume k hk x hx, convert (Hdiff k (lt_of_lt_of_le hk hm) x hx).has_fderiv_within_at, simp only [ftaylor_series_within, iterated_fderiv_within_succ_eq_comp_left, continuous_linear_equiv.coe_apply, function.comp_app, coe_fn_coe_base], exact continuous_linear_map.curry_uncurry_left _ }, { assume k hk, exact Hcont k (le_trans hk hm) } end lemma times_cont_diff_on_of_differentiable_on {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) : times_cont_diff_on 𝕜 n f s := times_cont_diff_on_of_continuous_on_differentiable_on (λ m hm, (h m hm).continuous_on) (λ m hm, (h m (le_of_lt hm))) lemma times_cont_diff_on.continuous_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) : continuous_on (iterated_fderiv_within 𝕜 m f s) s := (h.ftaylor_series_within hs).cont m hmn lemma times_cont_diff_on.differentiable_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) < n) (hs : unique_diff_on 𝕜 s) : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s := λ x hx, ((h.ftaylor_series_within hs).fderiv_within m hmn x hx).differentiable_within_at lemma times_cont_diff_on_iff_continuous_on_differentiable_on {n : with_top ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 n f s ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) := begin split, { assume h, split, { assume m hm, exact h.continuous_on_iterated_fderiv_within hm hs }, { assume m hm, exact h.differentiable_on_iterated_fderiv_within hm hs } }, { assume h, exact times_cont_diff_on_of_continuous_on_differentiable_on h.1 h.2 } end /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_within {n : ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 n (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume H, refine ⟨H.differentiable_on (with_top.coe_le_coe.2 (nat.le_add_left 1 n)), _⟩, apply times_cont_diff_on_of_locally_times_cont_diff_on, assume x hx, rcases times_cont_diff_on_succ_iff_has_fderiv_within_at.1 H x hx with ⟨u, hu, f', hff', hf'⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, refine ⟨o, o_open, xo, _⟩, apply (hf'.mono ho).congr (λ y hy, _), have A : fderiv_within 𝕜 f (s ∩ o) y = f' y := ((hff' y (ho hy)).mono ho).fderiv_within (hs.inter o_open y hy), rwa fderiv_within_inter (mem_nhds_sets o_open hy.2) (hs y hy.1) at A }, { rw times_cont_diff_on_succ_iff_has_fderiv_within_at, rintros ⟨hdiff, h⟩ x hx, exact ⟨s, self_mem_nhds_within, fderiv_within 𝕜 f s, λ x hx, (hdiff x hx).has_fderiv_within_at, h⟩ } end /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_within (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ⊤ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume h, refine ⟨h.differentiable_on lattice.le_top, _⟩, apply times_cont_diff_on_top.2 (λ n, ((times_cont_diff_on_succ_iff_fderiv_within hs).1 _).2), exact h.of_le lattice.le_top }, { assume h, refine times_cont_diff_on_top.2 (λ n, _), have A : (n : with_top ℕ) ≤ ⊤ := lattice.le_top, apply ((times_cont_diff_on_succ_iff_fderiv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le, exact with_top.coe_le_coe.2 (nat.le_succ n) } end lemma times_cont_diff_on.fderiv_within {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λ y, fderiv_within 𝕜 f s y) s := begin cases m, { change ⊤ + 1 ≤ n at hmn, have : n = ⊤, by simpa using hmn, rw this at hf, exact ((times_cont_diff_on_top_iff_fderiv_within hs).1 hf).2 }, { change (m.succ : with_top ℕ) ≤ n at hmn, exact ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (hf.of_le hmn)).2 } end lemma times_cont_diff_on.continuous_on_fderiv_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λ x, fderiv_within 𝕜 f s x) s := ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (h.of_le hn)).2.continuous_on /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff_on.continuous_on_fderiv_within_apply {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1 : E → F) p.2) (set.prod s univ) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1, p.2)) (set.prod s univ), { apply continuous_on.prod _ continuous_snd.continuous_on, exact continuous_on.comp (h.continuous_on_fderiv_within hs hn) continuous_fst.continuous_on (prod_subset_preimage_fst _ _) }, exact A.comp_continuous_on B end /-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) : Prop := (zero_eq : ∀ x, (p x 0).uncurry0 = f x) (fderiv : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x, has_fderiv_at (λ y, p y m) (p x m.succ).curry_left x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous (λ x, p x m)) lemma has_ftaylor_series_up_to.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (x : E) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } lemma has_ftaylor_series_up_to_on_univ_iff {n : with_top ℕ} : has_ftaylor_series_up_to_on n f p univ ↔ has_ftaylor_series_up_to n f p := begin split, { assume H, split, { exact λ x, H.zero_eq x (mem_univ x) }, { assume m hm x, rw ← has_fderiv_within_at_univ, exact H.fderiv_within m hm x (mem_univ x) }, { assume m hm, rw continuous_iff_continuous_on_univ, exact H.cont m hm } }, { assume H, split, { exact λ x hx, H.zero_eq x }, { assume m hm x hx, rw has_fderiv_within_at_univ, exact H.fderiv m hm x }, { assume m hm, rw ← continuous_iff_continuous_on_univ, exact H.cont m hm } } end lemma has_ftaylor_series_up_to.has_ftaylor_series_up_to_on {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (s : set E) : has_ftaylor_series_up_to_on n f p s := (has_ftaylor_series_up_to_on_univ_iff.2 h).mono (subset_univ _) lemma has_ftaylor_series_up_to.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) : has_ftaylor_series_up_to m f p := by { rw ← has_ftaylor_series_up_to_on_univ_iff at h ⊢, exact h.of_le hmn } lemma has_ftaylor_series_up_to.continuous {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) : continuous f := begin rw ← has_ftaylor_series_up_to_on_univ_iff at h, rw continuous_iff_continuous_on_univ, exact h.continuous_on end lemma has_ftaylor_series_up_to_zero_iff : has_ftaylor_series_up_to 0 f p ↔ continuous f ∧ (∀ x, (p x 0).uncurry0 = f x) := by simp [has_ftaylor_series_up_to_on_univ_iff.symm, continuous_iff_continuous_on_univ, has_ftaylor_series_up_to_on_zero_iff] /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to.has_fderiv_at {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x := begin rw [← has_fderiv_within_at_univ], exact (has_ftaylor_series_up_to_on_univ_iff.2 h).has_fderiv_within_at hn (mem_univ _) end lemma has_ftaylor_series_up_to.differentiable {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f := λ x, (h.has_fderiv_at hn x).differentiable_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to ((n + 1) : ℕ) f p ↔ (∀ x, (p x 0).uncurry0 = f x) ∧ (∀ x, has_fderiv_at (λ y, p y 0) (p x 1).curry_left x) ∧ has_ftaylor_series_up_to n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) := by simp [has_ftaylor_series_up_to_on_succ_iff_right, has_ftaylor_series_up_to_on_univ_iff.symm, -add_comm, -with_bot.coe_add] variable (𝕜) /-- A function is continuously differentiable up to `n` if it admits derivatives up to order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives might not be unique) we do not need to localize the definition in space or time. -/ definition times_cont_diff (n : with_top ℕ) (f : E → F) := ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to n f p variable {𝕜} theorem times_cont_diff_on_univ {n : with_top ℕ} : times_cont_diff_on 𝕜 n f univ ↔ times_cont_diff 𝕜 n f := begin split, { assume H, use ftaylor_series_within 𝕜 f univ, rw ← has_ftaylor_series_up_to_on_univ_iff, exact H.ftaylor_series_within unique_diff_on_univ }, { rintros ⟨p, hp⟩ m hm x hx, exact ⟨univ, self_mem_nhds_within, p, (hp.has_ftaylor_series_up_to_on univ).of_le hm⟩ } end lemma times_cont_diff_top : times_cont_diff 𝕜 ⊤ f ↔ ∀ (n : ℕ), times_cont_diff 𝕜 n f := by simp [times_cont_diff_on_univ.symm, times_cont_diff_on_top] lemma times_cont_diff.times_cont_diff_on {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_on 𝕜 n f s := (times_cont_diff_on_univ.2 h).mono (subset_univ _) @[simp] lemma times_cont_diff_zero : times_cont_diff 𝕜 0 f ↔ continuous f := begin rw [← times_cont_diff_on_univ, continuous_iff_continuous_on_univ], exact times_cont_diff_on_zero end lemma times_cont_diff.of_le {m n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hmn : m ≤ n) : times_cont_diff 𝕜 m f := times_cont_diff_on_univ.1 $ (times_cont_diff_on_univ.2 h).of_le hmn lemma times_cont_diff.continuous {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : continuous f := times_cont_diff_zero.1 (h.of_le lattice.bot_le) /-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/ lemma times_cont_diff.differentiable {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f := differentiable_on_univ.1 $ (times_cont_diff_on_univ.2 h).differentiable_on hn variable (𝕜) /-! ### Iterated derivative -/ /-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/ noncomputable def iterated_fderiv (n : ℕ) (f : E → F) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv 𝕜 rec x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series (f : E → F) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv 𝕜 n f x variable {𝕜} @[simp] lemma iterated_fderiv_zero_apply (m : (fin 0) → E) : (iterated_fderiv 𝕜 0 f x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_zero_eq_comp : iterated_fderiv 𝕜 0 f = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_succ_eq_comp_left {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv 𝕜 (iterated_fderiv 𝕜 n f)) := rfl lemma iterated_fderiv_within_univ {n : ℕ} : iterated_fderiv_within 𝕜 n f univ = iterated_fderiv 𝕜 n f := begin induction n with n IH, { ext x, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, iterated_fderiv_within_succ_apply_left, IH, fderiv_within_univ] } end lemma ftaylor_series_within_univ : ftaylor_series_within 𝕜 f univ = ftaylor_series 𝕜 f := begin ext1 x, ext1 n, change iterated_fderiv_within 𝕜 n f univ x = iterated_fderiv 𝕜 n f x, rw iterated_fderiv_within_univ end theorem iterated_fderiv_succ_apply_right {n : ℕ} (m : fin (n + 1) → E) : (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y) x (init m) (m (last n)) := begin rw [← iterated_fderiv_within_univ, ← iterated_fderiv_within_univ, ← fderiv_within_univ], exact iterated_fderiv_within_succ_apply_right unique_diff_on_univ (mem_univ _) _ end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_succ_eq_comp_right {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f x = ((continuous_multilinear_curry_right_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y))) x := by { ext m, rw iterated_fderiv_succ_apply_right, refl } @[simp] lemma iterated_fderiv_one_apply (m : (fin 1) → E) : (iterated_fderiv 𝕜 1 f x : ((fin 1) → E) → F) m = (fderiv 𝕜 f x : E → F) (m 0) := by { rw [iterated_fderiv_succ_apply_right, iterated_fderiv_zero_apply], refl } /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on_iff_ftaylor_series {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) := begin split, { rw [← times_cont_diff_on_univ, ← has_ftaylor_series_up_to_on_univ_iff, ← ftaylor_series_within_univ], exact λ h, times_cont_diff_on.ftaylor_series_within h unique_diff_on_univ }, { assume h, exact ⟨ftaylor_series 𝕜 f, h⟩ } end lemma times_cont_diff_iff_continuous_differentiable {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous (λ x, iterated_fderiv 𝕜 m f x)) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable 𝕜 (λ x, iterated_fderiv 𝕜 m f x)) := by simp [times_cont_diff_on_univ.symm, continuous_iff_continuous_on_univ, differentiable_on_univ.symm, iterated_fderiv_within_univ, times_cont_diff_on_iff_continuous_on_differentiable_on unique_diff_on_univ] lemma times_cont_diff_of_differentiable_iterated_fderiv {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) : times_cont_diff 𝕜 n f := times_cont_diff_iff_continuous_differentiable.2 ⟨λ m hm, (h m hm).continuous, λ m hm, (h m (le_of_lt hm))⟩ /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_fderiv {n : ℕ} : times_cont_diff 𝕜 ((n + 1) : ℕ) f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 n (λ y, fderiv 𝕜 f y) := by simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ, times_cont_diff_on_succ_iff_fderiv_within unique_diff_on_univ, -with_bot.coe_add, -add_comm] /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_top_iff_fderiv : times_cont_diff 𝕜 ⊤ f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ⊤ (λ y, fderiv 𝕜 f y) := begin simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ], rw times_cont_diff_on_top_iff_fderiv_within unique_diff_on_univ, end lemma times_cont_diff.continuous_fderiv {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λ x, fderiv 𝕜 f x) := ((times_cont_diff_succ_iff_fderiv).1 (h.of_le hn)).2.continuous /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff.continuous_fderiv_apply {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λp : E × E, (fderiv 𝕜 f p.1 : E → F) p.2) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous (λp : E × E, (fderiv 𝕜 f p.1, p.2)), { apply continuous.prod_mk _ continuous_snd, exact continuous.comp (h.continuous_fderiv hn) continuous_fst }, exact A.comp B end /-! ### Constants -/ lemma iterated_fderiv_within_zero_fun {n : ℕ} : iterated_fderiv 𝕜 n (λ x : E, (0 : F)) = 0 := begin induction n with n IH, { ext m, simp, refl }, { ext x m, rw [iterated_fderiv_succ_apply_left, IH], change (fderiv 𝕜 (λ (x : E), (0 : (E [×n]→L[𝕜] F))) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) = _, rw fderiv_const, refl } end lemma times_cont_diff_zero_fun {n : with_top ℕ} : times_cont_diff 𝕜 n (λ x : E, (0 : F)) := begin apply times_cont_diff_of_differentiable_iterated_fderiv (λm hm, _), rw iterated_fderiv_within_zero_fun, apply differentiable_const (0 : (E [×m]→L[𝕜] F)) end /-- Constants are `C^∞`. -/ lemma times_cont_diff_const {n : with_top ℕ} {c : F} : times_cont_diff 𝕜 n (λx : E, c) := begin suffices h : times_cont_diff 𝕜 ⊤ (λx : E, c), by exact h.of_le lattice.le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨differentiable_const c, _⟩, rw fderiv_const, exact times_cont_diff_zero_fun end lemma times_cont_diff_on_const {n : with_top ℕ} {c : F} {s : set E} : times_cont_diff_on 𝕜 n (λx : E, c) s := times_cont_diff_const.times_cont_diff_on /-! ### Linear functions -/ /-- Unbundled bounded linear functions are `C^∞`. -/ lemma is_bounded_linear_map.times_cont_diff {n : with_top ℕ} (hf : is_bounded_linear_map 𝕜 f) : times_cont_diff 𝕜 n f := begin suffices h : times_cont_diff 𝕜 ⊤ f, by exact h.of_le lattice.le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hf.differentiable, _⟩, simp [hf.fderiv], exact times_cont_diff_const end lemma continuous_linear_map.times_cont_diff {n : with_top ℕ} (f : E →L[𝕜] F) : times_cont_diff 𝕜 n f := f.is_bounded_linear_map.times_cont_diff /-- The first projection in a product is `C^∞`. -/ lemma times_cont_diff_fst {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.fst : E × F → E) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst /-- The second projection in a product is `C^∞`. -/ lemma times_cont_diff_snd {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.snd : E × F → F) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd /-- The identity is `C^∞`. -/ lemma times_cont_diff_id {n : with_top ℕ} : times_cont_diff 𝕜 n (id : E → E) := is_bounded_linear_map.id.times_cont_diff /-- Bilinear functions are `C^∞`. -/ lemma is_bounded_bilinear_map.times_cont_diff {n : with_top ℕ} (hb : is_bounded_bilinear_map 𝕜 b) : times_cont_diff 𝕜 n b := begin suffices h : times_cont_diff 𝕜 ⊤ b, by exact h.of_le lattice.le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hb.differentiable, _⟩, simp [hb.fderiv], exact hb.is_bounded_linear_map_deriv.times_cont_diff end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ lemma has_ftaylor_series_up_to_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : has_ftaylor_series_up_to_on n f p s) : has_ftaylor_series_up_to_on n (g ∘ f) (λ x k, g.comp_continuous_multilinear_map (p x k)) s := begin split, { assume x hx, simp [(hf.zero_eq x hx).symm] }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) → (E [×m]→L[𝕜] G) := λ f, g.comp_continuous_multilinear_map f, have hA : is_bounded_linear_map 𝕜 A := is_bounded_bilinear_map_comp_multilinear.is_bounded_linear_map_right _, have := hf.fderiv_within m hm x hx, convert has_fderiv_at.comp_has_fderiv_within_at x (hA.has_fderiv_at) this }, { assume m hm, let A : (E [×m]→L[𝕜] F) → (E [×m]→L[𝕜] G) := λ f, g.comp_continuous_multilinear_map f, have hA : is_bounded_linear_map 𝕜 A := is_bounded_bilinear_map_comp_multilinear.is_bounded_linear_map_right _, exact hA.continuous.comp_continuous_on (hf.cont m hm) } end /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin assume m hm x hx, rcases hf m hm x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, _, hp.continuous_linear_map_comp g⟩, end /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ lemma times_cont_diff.continuous_linear_map_comp {n : with_top ℕ} {f : E → F} (g : F →L[𝕜] G) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, g (f x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.continuous_linear_map_comp _ (times_cont_diff_on_univ.2 hf) /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ lemma continuous_linear_equiv.comp_times_cont_diff_on_iff {n : with_top ℕ} (e : F ≃L[𝕜] G) : times_cont_diff_on 𝕜 n (e ∘ f) s ↔ times_cont_diff_on 𝕜 n f s := begin split, { assume H, have : f = e.symm ∘ (e ∘ f), by { ext y, simp only [function.comp_app], rw e.symm_apply_apply (f y) }, rw this, exact H.continuous_linear_map_comp _ }, { assume H, exact H.continuous_linear_map_comp _ } end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ lemma has_ftaylor_series_up_to_on.comp_continuous_linear_map {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (g : G →L[𝕜] E) : has_ftaylor_series_up_to_on n (f ∘ g) (λ x k, (p (g x) k).comp_continuous_linear_map 𝕜 E g) (g ⁻¹' s) := begin split, { assume x hx, simp only [(hf.zero_eq (g x) hx).symm, function.comp_app], change p (g x) 0 (λ (i : fin 0), g 0) = p (g x) 0 0, rw continuous_linear_map.map_zero, refl }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ h, h.comp_continuous_linear_map 𝕜 E g, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_continuous_multilinear_map_comp_linear g, convert (hA.has_fderiv_at).comp_has_fderiv_within_at x ((hf.fderiv_within m hm (g x) hx).comp x (g.has_fderiv_within_at) (subset.refl _)), ext y v, change p (g x) (nat.succ m) (g ∘ (cons y v)) = p (g x) m.succ (cons (g y) (g ∘ v)), rw comp_cons }, { assume m hm, let A : (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ h, h.comp_continuous_linear_map 𝕜 E g, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_continuous_multilinear_map_comp_linear g, exact hA.continuous.comp_continuous_on ((hf.cont m hm).comp g.continuous.continuous_on (subset.refl _)) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.comp_continuous_linear_map {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (g : G →L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ g) (g ⁻¹' s) := begin assume m hm x hx, rcases hf m hm (g x) hx with ⟨u, hu, p, hp⟩, refine ⟨g ⁻¹' u, _, _, hp.comp_continuous_linear_map g⟩, apply continuous_within_at.preimage_mem_nhds_within', { exact g.continuous.continuous_within_at }, { exact nhds_within_mono (g x) (image_preimage_subset g s) hu } end /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ lemma times_cont_diff.comp_continuous_linear_map {n : with_top ℕ} {f : E → F} {g : G →L[𝕜] E} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (f ∘ g) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp_continuous_linear_map (times_cont_diff_on_univ.2 hf) _ /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ lemma continuous_linear_equiv.times_cont_diff_on_comp_iff {n : with_top ℕ} (e : G ≃L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ times_cont_diff_on 𝕜 n f s := begin refine ⟨λ H, _, λ H, H.comp_continuous_linear_map _⟩, have A : f = (f ∘ e) ∘ e.symm, by { ext y, simp only [function.comp_app], rw e.apply_symm_apply y }, have B : e.symm ⁻¹' (e ⁻¹' s) = s, by { rw [← preimage_comp, e.self_comp_symm], refl }, rw [A, ← B], exact H.comp_continuous_linear_map _ end /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ lemma has_ftaylor_series_up_to_on.prod {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) : has_ftaylor_series_up_to_on n (λ y, (f y, g y)) (λ y k, (p y k).prod (q y k)) s := begin split, { assume x hx, rw [← hf.zero_eq x hx, ← hg.zero_eq x hx], refl }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) × (E [×m]→L[𝕜] G) → (E [×m]→L[𝕜] (F × G)) := λ p, p.1.prod p.2, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_prod_multilinear, convert hA.has_fderiv_at.comp_has_fderiv_within_at x ((hf.fderiv_within m hm x hx).prod (hg.fderiv_within m hm x hx)) }, { assume m hm, let A : (E [×m]→L[𝕜] F) × (E [×m]→L[𝕜] G) → (E [×m]→L[𝕜] (F × G)) := λ p, p.1.prod p.2, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_prod_multilinear, exact hA.continuous.comp_continuous_on ((hf.cont m hm).prod (hg.cont m hm)) } end /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.prod {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx:E, (f x, g x)) s := begin assume m hm x hx, rcases hf m hm x hx with ⟨u, hu, p, hp⟩, rcases hg m hm x hx with ⟨v, hv, q, hq⟩, exact ⟨u ∩ v, filter.inter_mem_sets hu hv, _, (hp.mono (inter_subset_left u v)).prod (hq.mono (inter_subset_right u v))⟩ end /-- The cartesian product of `C^n` functions is `C^n`. -/ lemma times_cont_diff.prod {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx:E, (f x, g x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.prod (times_cont_diff_on_univ.2 hf) (times_cont_diff_on_univ.2 hg) /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity, but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u` and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f` belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above proof would work fine, but this is not the case. One could still write the proof considering spaces in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same universe (where everything is fine), and then we deduce the general result by lifting all our spaces to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level, and then argue that composing with such a linear equiv does not change the fact of being `C^n`, which we have already proved previously. -/ /-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all spaces live in the same universe. Use instead `times_cont_diff_on.comp` which removes the universe assumption (but is deduced from this one). -/ private lemma times_cont_diff_on.comp_same_univ {Eu : Type u} [normed_group Eu] [normed_space 𝕜 Eu] {Fu : Type u} [normed_group Fu] [normed_space 𝕜 Fu] {Gu : Type u} [normed_group Gu] [normed_space 𝕜 Gu] {n : with_top ℕ} {s : set Eu} {t : set Fu} {g : Fu → Gu} {f : Eu → Fu} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin unfreezeI, induction n using with_top.nat_induction with n IH Itop generalizing Eu Fu Gu, { rw times_cont_diff_on_zero at hf hg ⊢, exact continuous_on.comp hg hf st }, { rw times_cont_diff_on_succ_iff_has_fderiv_within_at at hg ⊢, assume x hx, rcases (times_cont_diff_on_succ_iff_has_fderiv_within_at.1 hf) x hx with ⟨u, hu, f', hf', f'_diff⟩, rcases hg (f x) (st hx) with ⟨v, hv, g', hg', g'_diff⟩, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, let w := s ∩ (u ∩ f⁻¹' v), have wv : w ⊆ f ⁻¹' v := λ y hy, hy.2.2, have wu : w ⊆ u := λ y hy, hy.2.1, have ws : w ⊆ s := λ y hy, hy.1, refine ⟨w, _, λ y, (g' (f y)).comp (f' y), _, _⟩, show w ∈ nhds_within x s, { apply filter.inter_mem_sets self_mem_nhds_within, apply filter.inter_mem_sets hu, apply continuous_within_at.preimage_mem_nhds_within', { rw ← continuous_within_at_inter' hu, exact (hf' x xu).differentiable_within_at.continuous_within_at.mono (inter_subset_right _ _) }, { exact nhds_within_mono _ (image_subset_iff.2 st) hv } }, show ∀ y ∈ w, has_fderiv_within_at (g ∘ f) ((g' (f y)).comp (f' y)) w y, { rintros y ⟨ys, yu, yv⟩, exact (hg' (f y) yv).comp y ((hf' y yu).mono wu) wv }, show times_cont_diff_on 𝕜 n (λ y, (g' (f y)).comp (f' y)) w, { have A : times_cont_diff_on 𝕜 n (λ y, g' (f y)) w := IH g'_diff ((hf.of_le (with_top.coe_le_coe.2 (nat.le_succ n))).mono ws) wv, have B : times_cont_diff_on 𝕜 n f' w := f'_diff.mono wu, have C : times_cont_diff_on 𝕜 n (λ y, (f' y, g' (f y))) w := times_cont_diff_on.prod B A, have D : times_cont_diff_on 𝕜 n (λ(p : (Eu →L[𝕜] Fu) × (Fu →L[𝕜] Gu)), p.2.comp p.1) univ := is_bounded_bilinear_map_comp.times_cont_diff.times_cont_diff_on, exact IH D C (subset_univ _) } }, { rw times_cont_diff_on_top at hf hg ⊢, assume n, apply Itop n (hg n) (hf n) st } end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.comp {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin /- we lift all the spaces to a common universe, as we have already proved the result in this situation. For the lift, we use the trick that `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) H`, and continuous linear equivs respect smoothness classes. The instances are not found automatically by Lean, so we declare them by hand. TODO: fix. -/ let Eu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) E, letI : normed_group Eu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) E _ _ _ _ _ _ _, letI : normed_space 𝕜 Eu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) E _ _ _ _ _ _ _, let Fu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) F, letI : normed_group Fu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) F _ _ _ _ _ _ _, letI : normed_space 𝕜 Fu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) F _ _ _ _ _ _ _, let Gu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) G, letI : normed_group Gu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) G _ _ _ _ _ _ _, letI : normed_space 𝕜 Gu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) G _ _ _ _ _ _ _, -- declare the isomorphisms let isoE : Eu ≃L[𝕜] E := continuous_multilinear_curry_fin0 𝕜 (E × F × G) E, let isoF : Fu ≃L[𝕜] F := continuous_multilinear_curry_fin0 𝕜 (E × F × G) F, let isoG : Gu ≃L[𝕜] G := continuous_multilinear_curry_fin0 𝕜 (E × F × G) G, -- lift the functions to the new spaces, check smoothness there, and then go back. let fu : Eu → Fu := (isoF.symm ∘ f) ∘ isoE, have fu_diff : times_cont_diff_on 𝕜 n fu (isoE ⁻¹' s) := by rwa [isoE.times_cont_diff_on_comp_iff, isoF.symm.comp_times_cont_diff_on_iff], let gu : Fu → Gu := (isoG.symm ∘ g) ∘ isoF, have gu_diff : times_cont_diff_on 𝕜 n gu (isoF ⁻¹' t) := by rwa [isoF.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff], have main : times_cont_diff_on 𝕜 n (gu ∘ fu) (isoE ⁻¹' s), { apply times_cont_diff_on.comp_same_univ gu_diff fu_diff, assume y hy, simp only [fu, continuous_linear_equiv.coe_apply, function.comp_app, mem_preimage], rw isoF.apply_symm_apply (f (isoE y)), exact st hy }, have : gu ∘ fu = (isoG.symm ∘ (g ∘ f)) ∘ isoE, { ext y, simp only [function.comp_apply, gu, fu], rw isoF.apply_symm_apply (f (isoE y)) }, rwa [this, isoE.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff] at main end /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ lemma times_cont_diff.comp_times_cont_diff_on {n : with_top ℕ} {s : set E} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := (times_cont_diff_on_univ.2 hg).comp hf subset_preimage_univ /-- The composition of `C^n` functions is `C^n`. -/ lemma times_cont_diff.comp {n : with_top ℕ} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (g ∘ f) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp (times_cont_diff_on_univ.2 hg) (times_cont_diff_on_univ.2 hf) (subset_univ _) /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff_on_fderiv_within_apply {m n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λp : E × E, (fderiv_within 𝕜 f s p.1 : E →L[𝕜] F) p.2) (set.prod s (univ : set E)) := begin have A : times_cont_diff 𝕜 m (λp : (E →L[𝕜] F) × E, p.1 p.2), { apply is_bounded_bilinear_map.times_cont_diff, exact is_bounded_bilinear_map_apply }, have B : times_cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 f s p.fst), p.snd)) (set.prod s univ), { apply times_cont_diff_on.prod _ _, { have I : times_cont_diff_on 𝕜 m (λ (x : E), fderiv_within 𝕜 f s x) s := hf.fderiv_within hs hmn, have J : times_cont_diff_on 𝕜 m (λ (x : E × E), x.1) (set.prod s univ) := times_cont_diff_fst.times_cont_diff_on, exact times_cont_diff_on.comp I J (prod_subset_preimage_fst _ _) }, { apply times_cont_diff.times_cont_diff_on _ , apply is_bounded_linear_map.snd.times_cont_diff } }, exact A.comp_times_cont_diff_on B end /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff.times_cont_diff_fderiv_apply {n m : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) : times_cont_diff 𝕜 m (λp : E × E, (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2) := begin rw ← times_cont_diff_on_univ at ⊢ hf, rw [← fderiv_within_univ, ← univ_prod_univ], exact times_cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn end /-- The sum of two `C^n`functions on a domain is `C^n`. -/ lemma times_cont_diff_on.add {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x + g x) s := begin have : times_cont_diff 𝕜 n (λp : F × F, p.1 + p.2), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd }, exact this.comp_times_cont_diff_on (hf.prod hg) end /-- The sum of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.add {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x + g x) := begin have : times_cont_diff 𝕜 n (λp : F × F, p.1 + p.2), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd }, exact this.comp (hf.prod hg) end /-- The negative of a `C^n`function on a domain is `C^n`. -/ lemma times_cont_diff_on.neg {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (λx, -f x) s := begin have : times_cont_diff 𝕜 n (λp : F, -p), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.neg is_bounded_linear_map.id }, exact this.comp_times_cont_diff_on hf end /-- The negative of a `C^n`function is `C^n`. -/ lemma times_cont_diff.neg {n : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, -f x) := begin have : times_cont_diff 𝕜 n (λp : F, -p), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.neg is_bounded_linear_map.id }, exact this.comp hf end /-- The difference of two `C^n`functions on a domain is `C^n`. -/ lemma times_cont_diff_on.sub {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x - g x) s := hf.add (hg.neg) /-- The difference of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.sub {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x - g x) := hf.add hg.neg
6176242ce4e5d4b6188c8dc8b995694e73ae090e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/nested2.lean
7a9ea0369e9805d07b7bc4f59d282c8d8aac307e
[ "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
582
lean
import data.nat data.list open nat constant foo (a : nat) : a > 0 → nat definition bla (a : nat) := foo (succ (succ a)) abstract lt.step abstract zero_lt_succ a end end print bla print bla_1 print bla_2 definition test (a : nat) := foo (succ (succ a)) abstract [reducible] lt.step abstract [irreducible] zero_lt_succ a end end print test_1 print test_2 constant voo {A : Type} (a b : A) : a ≠ b → bool set_option pp.universes true open list definition tst {A : Type} (a : A) (l : list A) := voo (a::l) [] abstract by contradiction end print tst_1 print tst
231b6ca620e11e8aa250532d368888fd241ad036
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/category/Top/opens.lean
d4f29668aced56289fba44eff43643b9ef7fafbb
[ "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
3,945
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 topology.category.Top.basic import category_theory.natural_isomorphism import category_theory.opposites import category_theory.eq_to_hom import topology.opens open category_theory open topological_space open opposite universe u namespace topological_space.opens variables {X Y Z : Top.{u}} instance opens_category : category.{u} (opens X) := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ } def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.down.down x.2⟩, (embedding.continuous_iff embedding_subtype_val).2 continuous_induced_dom⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f.val ⁻¹' U.val, f.property _ U.property ⟩, map := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟶ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨ f.val ⁻¹' U, f.property _ p ⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp section variable (X) def map_id : map (𝟙 X) ≅ 𝟭 (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } @[simp] lemma map_id_hom_app (U) : (map_id X).hom.app U = eq_to_hom (map_id_obj U) := rfl @[simp] lemma map_id_inv_app (U) : (map_id X).inv.app U = eq_to_hom (map_id_obj U).symm := rfl end @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } @[simp] lemma map_comp_hom_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).hom.app U = eq_to_hom (map_comp_obj f g U) := rfl @[simp] lemma map_comp_inv_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).inv.app U = eq_to_hom (map_comp_obj f g U).symm := rfl -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens
a32341e546623b02dd4a7bdc987457cc41a101f7
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/sum_bug.lean
bccb6d5ca93fc51231e139e4af5765f527e39ba0
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,326
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura, Jeremy Avigad import logic.prop logic.inhabited logic.decidable open inhabited decidable -- TODO: take this outside the namespace when the inductive package handles it better inductive sum (A B : Type) : Type := inl : A → sum A B, inr : B → sum A B namespace sum reserve infixr `+`:25 infixr `+` := sum open eq theorem inl_inj {A B : Type} {a1 a2 : A} (H : inl B a1 = inl B a2) : a1 = a2 := let f := λs, rec_on s (λa, a1 = a) (λb, false) in have H1 : f (inl B a1), from rfl, have H2 : f (inl B a2), from subst H H1, H2 theorem inl_neq_inr {A B : Type} {a : A} {b : B} (H : inl B a = inr A b) : false := let f := λs, rec_on s (λa', a = a') (λb, false) in have H1 : f (inl B a), from rfl, have H2 : f (inr A b), from subst H H1, H2 theorem inr_inj {A B : Type} {b1 b2 : B} (H : inr A b1 = inr A b2) : b1 = b2 := let f := λs, rec_on s (λa, false) (λb, b1 = b) in have H1 : f (inr A b1), from rfl, have H2 : f (inr A b2), from subst H H1, H2 theorem sum_inhabited_left [instance] {A B : Type} (H : inhabited A) : inhabited (A + B) := inhabited.mk (inl B (default A)) theorem sum_inhabited_right [instance] {A B : Type} (H : inhabited B) : inhabited (A + B) := inhabited.mk (inr A (default B)) theorem sum_eq_decidable [instance] {A B : Type} (s1 s2 : A + B) (H1 : ∀a1 a2 : A, decidable (inl B a1 = inl B a2)) (H2 : ∀b1 b2 : B, decidable (inr A b1 = inr A b2)) : decidable (s1 = s2) := rec_on s1 (take a1, show decidable (inl B a1 = s2), from rec_on s2 (take a2, show decidable (inl B a1 = inl B a2), from H1 a1 a2) (take b2, have H3 : (inl B a1 = inr A b2) ↔ false, from iff.intro inl_neq_inr (assume H4, false_elim H4), show decidable (inl B a1 = inr A b2), from decidable_iff_equiv _ (iff.symm H3))) (take b1, show decidable (inr A b1 = s2), from rec_on s2 (take a2, have H3 : (inr A b1 = inl B a2) ↔ false, from iff.intro (assume H4, inl_neq_inr (symm H4)) (assume H4, false_elim H4), show decidable (inr A b1 = inl B a2), from decidable_iff_equiv _ (iff.symm H3)) (take b2, show decidable (inr A b1 = inr A b2), from H2 b1 b2)) end sum
baaa844def717b9862e8e59a32db22e86c72015b
4727251e0cd73359b15b664c3170e5d754078599
/src/data/fintype/list.lean
a679f2290cdd544d5eb4cd311f110ffab0e8c55b
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,897
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.fintype.basic import data.list.perm /-! # Fintype instance for nodup lists The subtype of `{l : list α // l.nodup}` over a `[fintype α]` admits a `fintype` instance. ## Implementation details To construct the `fintype` instance, a function lifting a `multiset α` to the `finset (list α)` that can construct it is provided. This function is applied to the `finset.powerset` of `finset.univ`. In general, a `decidable_eq` instance is not necessary to define this function, but a proof of `(list.permutations l).nodup` is required to avoid it, which is a TODO. -/ variables {α : Type*} [decidable_eq α] open list namespace multiset /-- The `finset` of `l : list α` that, given `m : multiset α`, have the property `⟦l⟧ = m`. -/ def lists : multiset α → finset (list α) := λ s, quotient.lift_on s (λ l, l.permutations.to_finset) (λ l l' (h : l ~ l'), begin ext sl, simp only [mem_permutations, list.mem_to_finset], exact ⟨λ hs, hs.trans h, λ hs, hs.trans h.symm⟩ end) @[simp] lemma lists_coe (l : list α) : lists (l : multiset α) = l.permutations.to_finset := rfl @[simp] lemma mem_lists_iff (s : multiset α) (l : list α) : l ∈ lists s ↔ s = ⟦l⟧ := begin induction s using quotient.induction_on, simpa using perm_comm end end multiset instance fintype_nodup_list [fintype α] : fintype {l : list α // l.nodup} := fintype.subtype ((finset.univ : finset α).powerset.bUnion (λ s, s.val.lists)) (λ l, begin suffices : (∃ (a : finset α), a.val = ↑l) ↔ l.nodup, { simpa }, split, { rintro ⟨s, hs⟩, simpa [←multiset.coe_nodup, ←hs] using s.nodup }, { intro hl, refine ⟨⟨↑l, hl⟩, _⟩, simp } end)
8fb9fbcfbec19c838a79bd51680d4f978d779558
80e164f6d4e98cd2476f5f4030ba16829658e71c
/src/definitions.lean
f143a5a73d785548551f60105c29e45bbec4e79f
[]
no_license
agusakov/matroids
416a7fab0a8a74abe87f6ff574cadc2eaf3bf13c
a95393f6321ccbdf12fafecc788c8bfb20928c3f
refs/heads/main
1,678,551,396,073
1,614,186,213,000
1,614,186,213,000
340,665,767
0
0
null
null
null
null
UTF-8
Lean
false
false
3,614
lean
import data.finset universes u open finset variables {α : Type u} [decidable_eq α] -- need decidability for set difference def independent (ℐ : finset (finset α)) (I₁ I₂ : finset α) (h₁ : I₁ ∈ ℐ) (h₂ : I₂ ∈ ℐ) : Prop := finset.card I₁ < finset.card I₂ → ∃ (e ∈ I₂ \ I₁), (insert e I₁ ∈ ℐ) -- how should i define this? def independent' (ℐ : finset α → Prop) : Prop := ∀ I₁ I₂, ℐ I₁ ∧ ℐ I₂ → finset.card I₁ < finset.card I₂ → ∃ (e ∈ I₂ \ I₁), (ℐ (insert e I₁)) def independent_collection (ℐ : finset (finset α)) : Prop := ∀ (I₁ I₂ : finset α) (h₁ : I₁ ∈ ℐ) (h₂ : I₂ ∈ ℐ), independent ℐ I₁ I₂ h₁ h₂ -- don't love that lemma subset_independent (ℐ ℐ': finset (finset α)) (hi : independent_collection ℐ) : ℐ' ⊆ ℐ → independent_collection ℐ' := begin intros h, rw independent_collection, intros I₁ I₂ h₁ h₂, rw independent_collection at hi, --rw subset_iff at h, have h1' := h h₁, have h2' := h h₂, specialize hi I₁ I₂ h1' h2', --exact hi, sorry, end /- A matroid M is an ordered pair `(E, ℐ)` consisting of a finite set `E` and a collection `ℐ` of subsets of `E` having the following three properties: (I1) `∅ ∈ ℐ`. (I2) If `I ∈ ℐ` and `I' ⊆ I`, then `I' ∈ ℐ`. (I3) If `I₁` and `I₂` are in `I` and `|I₁| < |I₂|`, then there is an element `e` of `I₂ − I₁` such that `I₁ ∪ {e} ∈ I`.-/ structure matroid (E : finset α) (ℐ : finset (finset α)) := (subsets : ∀ (I ∈ ℐ), I ⊆ E) (empty : ∅ ∈ ℐ) -- (I1) (hereditary : ∀ (I₁ ∈ ℐ), ∀ (I₂ : finset α), I₂ ⊆ I₁ → I₂ ∈ ℐ) -- (I2) (ind : independent_collection ℐ) -- (I3) /- A subset of `E` that is not in `ℐ` is called dependent. -/ -- is this something that merits a separate definition? def dependent_sets (E : finset α) (ℐ : finset (finset α)) : finset (finset α) := E.powerset \ ℐ variables {E : finset α} {ℐ : finset (finset α)} variables [decidable_pred (λ (D : finset α), independent_collection (erase D.powerset D))] -- figure out where this needs to go later namespace matroid def circuit (M : matroid E ℐ) : finset (finset α) := filter (λ (D : finset α), independent_collection (erase D.powerset D)) (dependent_sets E ℐ) -- we're not defining n-circuits lmao @[simp] lemma mem_circuit (M : matroid E ℐ) (C₁ : finset α) : C₁ ∈ M.circuit ↔ C₁ ∈ dependent_sets E ℐ ∧ independent_collection (erase C₁.powerset C₁) := begin rw circuit, rw mem_filter, end /- `(C1)` ∅ ∉ C -/ lemma empty_notmem_circuit (M : matroid E ℐ) : ∅ ∉ M.circuit := begin -- this is just due to the fact that ∅ ∈ ℐ lol unfold matroid.circuit, unfold dependent_sets, rw mem_filter, rw and_comm, push_neg, intros h, simp, exact M.empty, end /- `(C2)` if C₁ and C₂ are members of C and C₁ ⊆ C₂, then C₂ = C₂. In other words, C forms an antichain. -/ lemma circuit_antichain (M : matroid E ℐ) (C₁ C₂ : finset α) (h₁ : C₁ ∈ M.circuit) (h₂ : C₂ ∈ M.circuit) : C₁ ⊆ C₂ → C₁ = C₂ := begin -- this is because the proper subsets are independent and therefore not in M.circuit intros h, rw mem_circuit at h₂, by_contra h2, have h3 : C₁ ∈ powerset C₂, { exact finset.mem_powerset.2 h }, have h4 : C₁ ∈ C₂.powerset.erase C₂, { rw mem_erase, simp, exact ⟨h2, h⟩ }, have h5 : C₁ ⊂ C₂, { sorry }, sorry, end end matroid
5530afd2dc9745e791c765e62cddccb0c4ea8151
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Data/JsonRpc.lean
066e354f54f8230a8e22aca80bacfe1f92b29f9c
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
10,377
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.Control import Init.System.IO import Std.Data.RBTree import Lean.Data.Json /-! Implementation of JSON-RPC 2.0 (https://www.jsonrpc.org/specification) for use in the LSP server. -/ namespace Lean.JsonRpc open Json open Std (RBNode) inductive RequestID where | str (s : String) | num (n : JsonNumber) | null deriving Inhabited, BEq, Ord instance : OfNat RequestID n := ⟨RequestID.num n⟩ instance : ToString RequestID where toString | RequestID.str s => s!"\"{s}\"" | RequestID.num n => toString n | RequestID.null => "null" /-- Error codes defined by JSON-RPC and LSP. -/ inductive ErrorCode where | parseError | invalidRequest | methodNotFound | invalidParams | internalError | serverNotInitialized | unknownErrorCode -- LSP-specific codes below. | contentModified | requestCancelled -- Lean-specific codes below. | rpcNeedsReconnect | workerExited | workerCrashed deriving Inhabited, BEq instance : FromJson ErrorCode := ⟨fun | num (-32700 : Int) => return ErrorCode.parseError | num (-32600 : Int) => return ErrorCode.invalidRequest | num (-32601 : Int) => return ErrorCode.methodNotFound | num (-32602 : Int) => return ErrorCode.invalidParams | num (-32603 : Int) => return ErrorCode.internalError | num (-32002 : Int) => return ErrorCode.serverNotInitialized | num (-32001 : Int) => return ErrorCode.unknownErrorCode | num (-32801 : Int) => return ErrorCode.contentModified | num (-32800 : Int) => return ErrorCode.requestCancelled | num (-32900 : Int) => return ErrorCode.rpcNeedsReconnect | num (-32901 : Int) => return ErrorCode.workerExited | num (-32902 : Int) => return ErrorCode.workerCrashed | _ => throw "expected error code"⟩ instance : ToJson ErrorCode := ⟨fun | ErrorCode.parseError => (-32700 : Int) | ErrorCode.invalidRequest => (-32600 : Int) | ErrorCode.methodNotFound => (-32601 : Int) | ErrorCode.invalidParams => (-32602 : Int) | ErrorCode.internalError => (-32603 : Int) | ErrorCode.serverNotInitialized => (-32002 : Int) | ErrorCode.unknownErrorCode => (-32001 : Int) | ErrorCode.contentModified => (-32801 : Int) | ErrorCode.requestCancelled => (-32800 : Int) | ErrorCode.rpcNeedsReconnect => (-32900 : Int) | ErrorCode.workerExited => (-32901 : Int) | ErrorCode.workerCrashed => (-32902 : Int)⟩ /-- Uses separate constructors for notifications and errors because client and server behavior is expected to be wildly different for both. -/ inductive Message where | request (id : RequestID) (method : String) (params? : Option Structured) | notification (method : String) (params? : Option Structured) | response (id : RequestID) (result : Json) | responseError (id : RequestID) (code : ErrorCode) (message : String) (data? : Option Json) def Batch := Array Message -- Compound type with simplified APIs for passing around -- jsonrpc data structure Request (α : Type u) where id : RequestID method : String param : α deriving Inhabited, BEq instance [ToJson α] : Coe (Request α) Message := ⟨fun r => Message.request r.id r.method (toStructured? r.param).toOption⟩ structure Notification (α : Type u) where method : String param : α deriving Inhabited, BEq instance [ToJson α] : Coe (Notification α) Message := ⟨fun r => Message.notification r.method (toStructured? r.param).toOption⟩ structure Response (α : Type u) where id : RequestID result : α deriving Inhabited, BEq instance [ToJson α] : Coe (Response α) Message := ⟨fun r => Message.response r.id (toJson r.result)⟩ structure ResponseError (α : Type u) where id : RequestID code : ErrorCode message : String data? : Option α := none deriving Inhabited, BEq instance [ToJson α] : Coe (ResponseError α) Message := ⟨fun r => Message.responseError r.id r.code r.message (r.data?.map toJson)⟩ instance : Coe String RequestID := ⟨RequestID.str⟩ instance : Coe JsonNumber RequestID := ⟨RequestID.num⟩ private def RequestID.lt : RequestID → RequestID → Bool | RequestID.str a, RequestID.str b => a < b | RequestID.num a, RequestID.num b => a < b | RequestID.null, RequestID.num _ => true | RequestID.null, RequestID.str _ => true | RequestID.num _, RequestID.str _ => true | _, _ /- str < *, num < null, null < null -/ => false private def RequestID.ltProp : LT RequestID := ⟨fun a b => RequestID.lt a b = true⟩ instance : LT RequestID := RequestID.ltProp instance (a b : RequestID) : Decidable (a < b) := inferInstanceAs (Decidable (RequestID.lt a b = true)) instance : FromJson RequestID := ⟨fun j => match j with | str s => return RequestID.str s | num n => return RequestID.num n | _ => throw "a request id needs to be a number or a string"⟩ instance : ToJson RequestID := ⟨fun rid => match rid with | RequestID.str s => s | RequestID.num n => num n | RequestID.null => null⟩ instance : ToJson Message := ⟨fun m => mkObj $ ⟨"jsonrpc", "2.0"⟩ :: match m with | Message.request id method params? => [ ⟨"id", toJson id⟩, ⟨"method", method⟩ ] ++ opt "params" params? | Message.notification method params? => ⟨"method", method⟩ :: opt "params" params? | Message.response id result => [ ⟨"id", toJson id⟩, ⟨"result", result⟩] | Message.responseError id code message data? => [ ⟨"id", toJson id⟩, ⟨"error", mkObj $ [ ⟨"code", toJson code⟩, ⟨"message", message⟩ ] ++ opt "data" data?⟩ ]⟩ instance : FromJson Message where fromJson? j := do let "2.0" ← j.getObjVal? "jsonrpc" | throw "only version 2.0 of JSON RPC is supported" (do let id ← j.getObjValAs? RequestID "id" let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.request id method params?.toOption)) <|> (do let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.notification method params?.toOption)) <|> (do let id ← j.getObjValAs? RequestID "id" let result ← j.getObjVal? "result" pure (Message.response id result)) <|> (do let id ← j.getObjValAs? RequestID "id" let err ← j.getObjVal? "error" let code ← err.getObjValAs? ErrorCode "code" let message ← err.getObjValAs? String "message" let data? := err.getObjVal? "data" pure (Message.responseError id code message data?.toOption)) -- TODO(WN): temporary until we have deriving FromJson instance [FromJson α] : FromJson (Notification α) where fromJson? j := do let msg : Message ← fromJson? j if let Message.notification method params? := msg then let params := params? let param : α ← fromJson? (toJson params) pure $ ⟨method, param⟩ else throw "not a notfication" end Lean.JsonRpc namespace IO.FS.Stream open Lean open Lean.JsonRpc section def readMessage (h : FS.Stream) (nBytes : Nat) : IO Message := do let j ← h.readJson nBytes match fromJson? j with | Except.ok m => pure m | Except.error inner => throw $ userError s!"JSON '{j.compress}' did not have the format of a JSON-RPC message.\n{inner}" def readRequestAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Request α) := do let m ← h.readMessage nBytes match m with | Message.request id method params? => if method = expectedMethod then let j := toJson params? match fromJson? j with | Except.ok v => pure ⟨id, expectedMethod, v⟩ | Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}" else throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'" | _ => throw $ userError s!"Expected JSON-RPC request, got: '{(toJson m).compress}'" def readNotificationAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Notification α) := do let m ← h.readMessage nBytes match m with | Message.notification method params? => if method = expectedMethod then let j := toJson params? match fromJson? j with | Except.ok v => pure ⟨expectedMethod, v⟩ | Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}" else throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'" | _ => throw $ userError s!"Expected JSON-RPC notification, got: '{(toJson m).compress}'" partial def readResponseAs (h : FS.Stream) (nBytes : Nat) (expectedID : RequestID) (α) [FromJson α] : IO (Response α) := do let m ← h.readMessage nBytes match m with | Message.response id result => if id == expectedID then match fromJson? result with | Except.ok v => pure ⟨expectedID, v⟩ | Except.error inner => throw $ userError s!"Unexpected result '{result.compress}'\n{inner}" else throw $ userError s!"Expected id {expectedID}, got id {id}" | Message.notification .. => readResponseAs h nBytes expectedID α | _ => throw $ userError s!"Expected JSON-RPC response, got: '{(toJson m).compress}'" end section variable [ToJson α] def writeMessage (h : FS.Stream) (m : Message) : IO Unit := h.writeJson (toJson m) def writeRequest (h : FS.Stream) (r : Request α) : IO Unit := h.writeMessage r def writeNotification (h : FS.Stream) (n : Notification α) : IO Unit := h.writeMessage n def writeResponse (h : FS.Stream) (r : Response α) : IO Unit := h.writeMessage r def writeResponseError (h : FS.Stream) (e : ResponseError Unit) : IO Unit := h.writeMessage (Message.responseError e.id e.code e.message none) def writeResponseErrorWithData (h : FS.Stream) (e : ResponseError α) : IO Unit := h.writeMessage e end end IO.FS.Stream
e64dc8af2225da658d7aa19c0baea3d6a81b04a8
c37c89b934e368a90005cf2a72457da4d59a48a1
/Elgamal/src/Elgamal.lean
5672c871eaf46b0fdd7001badb88e38c5f965793
[ "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
6,284
lean
import data.fintype data.nat.basic data.zmod.basic algebra.group_power namespace Zeroknowledgeproof variables (p : ℕ) (q : ℕ) (r : ℕ+) (Hp : nat.prime p) (Hq : nat.prime q) (Hdiv : p = q * r + 1) (g : zmodp p Hp) /- g is a generator -/ (Hg : g^q = 1) /- of order q -/ (w : zmodp q Hq) /- private key-/ (h : zmodp p Hp) (Hh : h = g^w.val) /- A Schnorr group is a large prime-order subgroup of ℤ∗𝑝, the multiplicative group of integers modulo 𝑝. To generate such a group, we find 𝑝=𝑞𝑟+1 such that 𝑝 and 𝑞 are prime.Then, we choose any ℎ in the range 1<ℎ<𝑝 such that ℎ^r ≠ 1 (mod𝑝) The value 𝑔=ℎ^𝑟(mod𝑝) is a generator of a subgroup ℤ∗𝑝 of order 𝑞. By Fermat's little theorem g^q = h^(rq) = h^(p-1) = 1 (mod p) -/ def elgamal_enc (m : zmodp p Hp) (r : zmodp q Hq) := (g^r.val, g^m.val * h^r.val) def elgamal_dec (c : zmodp p Hp × zmodp p Hp) := c.2 * (c.1^w.val)⁻¹ def multiply_cipher (c₁ c₂ : zmodp p Hp × zmodp p Hp) := (c₁.1 * c₂.1, c₁.2 * c₂.2) #check elgamal_enc p q Hp Hq g h #check elgamal_dec p q Hp Hq w #check multiply_cipher p Hp lemma prime5: nat.prime 5 := begin unfold nat.prime, split, sorry, intros m Hm, sorry end /- It's pretty fase -/ #eval (13^1990 : zmodp 5 prime5) include Hh lemma elgama_enc_dec_identity : ∀ m r, elgamal_dec p q Hp Hq w (elgamal_enc p q Hp Hq g h m r) = g^m.val := begin intros m r, unfold elgamal_enc elgamal_dec, simp, rw Hh, sorry end end Zeroknowledgeproof namespace Interactivezkp variables (p : ℕ) (Hp : nat.prime p) (g : zmodp p Hp) universes u inductive communication : Type u | commitment (k : zmodp p Hp) : communication | challenge (k : zmodp p Hp) : communication | response (k s : zmodp p Hp) : communication /- zero knowledge proof of zkp {x | g^x = h} r is prover's randomness, c is challenger's randomness Can this be abstracted for some R : A → B → bool -/ open communication inductive zkp_transcript (x h : zmodp p Hp) (Hf : h = g^x.val) (r c : zmodp p Hp) : communication p Hp → Type u | commitment_step (k : zmodp p Hp) : k = g^r.val → zkp_transcript (commitment k) | challenge_step (k : zmodp p Hp) : zkp_transcript (commitment k) → zkp_transcript (challenge k) | response_step (k s : zmodp p Hp) : s = r + c * x → zkp_transcript (challenge k) → zkp_transcript (response k s) /- end of zero knowledge proof transcript -/ open zkp_transcript /- I don't care how the transcript is constructed. If it checks out according to defined rule then I will accept it. -/ def accept_transcript (k r c s x h : zmodp p Hp) (Hf : h = g^x.val) (Hzkp : zkp_transcript p Hp g x h Hf r c (response k s)) := g^s.val = k * h^c.val /- A transcript is not valid if it does not check out -/ def reject_transcript (k r c s x h : zmodp p Hp) (Hf : h = g^x.val) (Hzkp : zkp_transcript p Hp g x h Hf r c (response k s)) := g^s.val ≠ k * h^c.val /- for any given x h and proof Hf, randomness r c, I can always construct a valid certificate. I will prove this formally that this function always constructs a valid certificate which checks out -/ def construct_a_certificate (r c x h : zmodp p Hp) (Hf : h = g^x.val) : zkp_transcript p Hp g x h Hf r c (response (g^r.val) (r + c * x)) := response_step (g^r.val) (r + c * x) rfl (challenge_step (g^r.val) (commitment_step _ _ (g^r.val) rfl)) /- certificate checking is decidable-/ /- Proof that the construct_a_certificate function always constructs a valid certificate. Each valid certificate always checks out : Completeness -/ lemma proof_of_correctness : ∀ (r c x h : zmodp p Hp) (Hf : h = g^x.val) (cert = construct_a_certificate p Hp g r c x h Hf), accept_transcript p Hp g _ _ _ _ _ _ _ cert := begin intros, unfold accept_transcript, /- some basic math would solve it, but I don't know the tactics yet.-/ sorry end /- If you give me two valid ceritificate then I can extract a witness x : Soundenss -/ lemma extract_witness : ∀ (r₁ c₁ r₂ c₂ x h : zmodp p Hp) (Hf : h = g^x.val) (cert₁ = construct_a_certificate p Hp g r₁ c₁ x h Hf) (cert₂ = construct_a_certificate p Hp g r₂ c₂ x h Hf), accept_transcript p Hp g _ _ _ _ _ _ _ cert₁ → accept_transcript p Hp g _ _ _ _ _ _ _ cert₂ → true := begin intros, sorry end /- Zero knowledge Proof -/ end Interactivezkp namespace Elgamal /- define a group on finite type -/ universe u variables (A : Type u) (Hf : fintype A) (gop : A -> A -> A) (e : A) (inv : A -> A) class group := (associativity : ∀ x y z : A, gop x (gop y z) = gop (gop x y) z) (left_identity : ∀ x : A, gop e x = x) (right_identity : ∀ x : A, gop x e = x) (left_inverse : ∀ x : A, gop (inv x) x = e) (right_inverse : ∀ x : A, gop x (inv x) = e) def group_pow (x : A) : ℕ → A | 0 := e | (n + 1) := gop x (group_pow n) variable (G : group A gop e inv) include G lemma group_exp_identity : ∀ (n : ℕ), group_pow A gop e e n = e := begin intro n, induction n, /- simplification would do the job -/ /- simplify it and rewrite in Ih, follwed right_identity -/ {simp [group_pow]}, {dsimp [group_pow], rewrite n_ih, apply (group.left_identity gop e inv e)} end lemma group_exp_plus : ∀ (n m : ℕ) (x : A), group_pow A gop e x (n + m) = gop (group_pow A gop e x n) (group_pow A gop e x m) := begin intros n, induction n, {intros m x, simp [group_pow], rewrite group.left_identity gop e inv (group_pow A gOp e x m)}, {intros m x, simp [group_pow], rewrite nat.add_succ, simp [group_pow], rewrite n_ih, rewrite <- (group.associativity gOp e inv x)} end lemma group_exp_mult : ∀ (n m : ℕ) (x : A), group_pow A gop e x (n * m) = group_pow A gop e (group_pow A gop e x n) m := begin intros n, induction n, sorry, sorry end class abelian_group := (commutative : ∀ x y, gop x y = gop y x) #check (abelian_group A gop e inv G) class cyclic_group (g : A) (order : ℕ+) end Elgamal
386d2f9c729d98188c423a73595dd1edec806596
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/ll_infer_type_bug.lean
7e8019fbef15940e80546b96e11e80a7be855e4f
[ "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
520
lean
new_frontend partial def f : List Nat → Bool | [] => false | (a::as) => a > 0 && f as #check f._cstage2 #exit mutual def f1, f2, f3, f4, f5 with f1 : Nat → Bool | 0 := f3 0 | x := f2 x with f2 : Nat → Bool | 0 := f4 0 | x := f3 x with f3 : Nat → Bool | 0 := f5 0 | x := f4 x with f4 : Nat → Bool | 0 := f5 0 | (x+1) := f4 x with f5 : Nat → Bool | 0 := true | _ := false #check f1._main._cstage2 #check f2._main._cstage2 #check f3._main._cstage2 #check f4._main._cstage2 #check f5._main._cstage2
5d100f855f243a26d0796e2ebb51006ee6442db7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/category/Module/colimits.lean
33d2030132d883bb23286ad98f943b1d26f6b199
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
11,354
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 algebra.category.Module.basic import category_theory.concrete_category.elementwise /-! # The category of R-modules has all colimits. This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`. Note that finite colimits can already be obtained from the instance `abelian (Module R)`. TODO: In fact, in `Module R` there is a much nicer model of colimits as quotients of finitely supported functions, and we really should implement this as well (or instead). -/ universes u v w open category_theory open category_theory.limits variables {R : Type u} [ring R] -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. namespace Module.colimits /-! We build the colimit of a diagram in `Module` by constructing the free group on the disjoint union of all the abelian groups in the diagram, then taking the quotient by the abelian group laws within each abelian group, and the identifications given by the morphisms in the diagram. -/ variables {J : Type w} [category.{v} J] (F : J ⥤ Module.{max u v w} R) /-- An inductive type representing all module expressions (without relations) on a collection of types indexed by the objects of `J`. -/ inductive prequotient -- There's always `of` | of : Π (j : J) (x : F.obj j), prequotient -- Then one generator for each operation | zero : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient | smul : R → prequotient → prequotient instance : inhabited (prequotient F) := ⟨prequotient.zero⟩ open prequotient /-- The relation on `prequotient` saying when two expressions are equal because of the module laws, or because one element is mapped to another by a morphism in the diagram. -/ inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : F.obj j), relation (of j' (F.map f x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | neg : Π (j) (x : F.obj j), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : F.obj j), relation (of j (x + y)) (add (of j x) (of j y)) | smul : Π (j) (s) (x : F.obj j), relation (of j (s • x)) (smul s (of j x)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') | smul_1 : Π (s) (x x') (r : relation x x'), relation (smul s x) (smul s x') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) | one_smul : Π (x), relation (smul 1 x) x | mul_smul : Π (s t) (x), relation (smul (s * t) x) (smul s (smul t x)) | smul_add : Π (s) (x y), relation (smul s (add x y)) (add (smul s x) (smul s y)) | smul_zero : Π (s), relation (smul s zero) zero | add_smul : Π (s t) (x), relation (smul (s + t) x) (add (smul s x) (smul t x)) | zero_smul : Π (x), relation (smul 0 x) zero /-- The setoid corresponding to module expressions modulo module relations and identifications. -/ def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid /-- The underlying type of the colimit of a diagram in `Module R`. -/ @[derive inhabited] def colimit_type : Type (max u v w) := quotient (colimit_setoid F) instance : add_comm_group (colimit_type F) := { zero := begin exact quot.mk _ zero end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, } instance : module R (colimit_type F) := { smul := λ s, begin fapply @quot.lift, { intro x, exact quot.mk _ (smul s x) }, { intros x x' r, apply quot.sound, exact relation.smul_1 s _ _ r }, end, one_smul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_smul, refl, end, mul_smul := λ s t x, begin induction x, dsimp, apply quot.sound, apply relation.mul_smul, refl, end, smul_add := λ s x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.smul_add, refl, refl, end, smul_zero := λ s, begin apply quot.sound, apply relation.smul_zero, end, add_smul := λ s t x, begin induction x, dsimp, apply quot.sound, apply relation.add_smul, refl, end, zero_smul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_smul, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl @[simp] lemma quot_smul (s x) : quot.mk setoid.r (smul s x) = (s • (quot.mk setoid.r x) : colimit_type F) := rfl /-- The bundled module giving the colimit of a diagram. -/ def colimit : Module R := Module.of R (colimit_type F) /-- The function from a given module in the diagram to the colimit module. -/ def cocone_fun (j : J) (x : F.obj j) : colimit_type F := quot.mk _ (of j x) /-- The group homomorphism from a given module in the diagram to the colimit module. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, map_smul' := by { intros, apply quot.sound, apply relation.smul, }, map_add' := by intros; apply quot.sound; apply relation.add } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j) : (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } /-- The cocone over the proposed colimit module. -/ def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. /-- The function from the free module on the diagram to the cone point of any other cocone. -/ @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y | (smul s x) := s • (desc_fun_lift x) /-- The function from the colimit module to the cone point of any other cocone. -/ def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { simp, }, -- zero { simp, }, -- neg { simp, }, -- add { simp, }, -- smul, { simp, }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- smul_1 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- add_assoc { rw add_assoc, }, -- one_smul { rw one_smul, }, -- mul_smul { rw mul_smul, }, -- smul_add { rw smul_add, }, -- smul_zero { rw smul_zero, }, -- add_smul { rw add_smul, }, -- zero_smul { rw zero_smul, }, } end /-- The group homomorphism from the colimit module to the cone point of any other cocone. -/ def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_smul' := λ s x, by { induction x; refl, }, map_add' := λ x y, by { induction x; induction y; refl }, } /-- Evidence that the proposed colimit is the colimit. -/ def colimit_cocone_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp *, }, { simp *, }, { simp *, }, { simp *, }, refl end }. instance has_colimits_Module : has_colimits (Module.{max v u} R) := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } -- We manually add a `has_colimits` instance with universe parameters swapped, for otherwise -- the instance is not found by typeclass search. instance has_colimits_Module' (R : Type u) [ring R] : has_colimits (Module.{max u v} R) := Module.colimits.has_colimits_Module.{u v} -- We manually add a `has_colimits` instance with equal universe parameters, for otherwise -- the instance is not found by typeclass search. instance has_colimits_Module'' (R : Type u) [ring R] : has_colimits (Module.{u} R) := Module.colimits.has_colimits_Module.{u u} -- Sanity checks, just to make sure typeclass search can find the instances we want. example (R : Type u) [ring R] : has_colimits (Module.{max v u} R) := infer_instance example (R : Type u) [ring R] : has_colimits (Module.{max u v} R) := infer_instance example (R : Type u) [ring R] : has_colimits (Module.{u} R) := infer_instance end Module.colimits
90fff9be72ff0c49703660ddbc2545df1ded8a09
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/geometry/manifold/complex.lean
bcd39594ce8bc47022527bc64b08fc0d943e41e8
[ "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
6,108
lean
/- Copyright (c) 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.complex.abs_max import analysis.locally_convex.with_seminorms import geometry.manifold.mfderiv import topology.locally_constant.basic /-! # Holomorphic functions on complex manifolds > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Thanks to the rigidity of complex-differentiability compared to real-differentiability, there are many results about complex manifolds with no analogue for manifolds over a general normed field. For now, this file contains just two (closely related) such results: ## Main results * `mdifferentiable.is_locally_constant`: A complex-differentiable function on a compact complex manifold is locally constant. * `mdifferentiable.exists_eq_const_of_compact_space`: A complex-differentiable function on a compact preconnected complex manifold is constant. ## TODO There is a whole theory to develop here. Maybe a next step would be to develop a theory of holomorphic vector/line bundles, including: * the finite-dimensionality of the space of sections of a holomorphic vector bundle * Siegel's theorem: for any `n + 1` formal ratios `g 0 / h 0`, `g 1 / h 1`, .... `g n / h n` of sections of a fixed line bundle `L` over a complex `n`-manifold, there exists a polynomial relationship `P (g 0 / h 0, g 1 / h 1, .... g n / h n) = 0` Another direction would be to develop the relationship with sheaf theory, building the sheaves of holomorphic and meromorphic functions on a complex manifold and proving algebraic results about the stalks, such as the Weierstrass preparation theorem. -/ open_locale manifold topology open complex namespace mdifferentiable variables {E : Type*} [normed_add_comm_group E] [normed_space ℂ E] variables {F : Type*} [normed_add_comm_group F] [normed_space ℂ F] [strict_convex_space ℝ F] variables {M : Type*} [topological_space M] [compact_space M] [charted_space E M] [smooth_manifold_with_corners 𝓘(ℂ, E) M] /-- A holomorphic function on a compact complex manifold is locally constant. -/ protected lemma is_locally_constant {f : M → F} (hf : mdifferentiable 𝓘(ℂ, E) 𝓘(ℂ, F) f) : is_locally_constant f := begin haveI : locally_connected_space M := charted_space.locally_connected_space E M, apply is_locally_constant.of_constant_on_preconnected_clopens, intros s hs₂ hs₃ a ha b hb, have hs₁ : is_compact s := hs₃.2.is_compact, -- for an empty set this fact is trivial rcases s.eq_empty_or_nonempty with rfl | hs', { exact false.rec _ ha }, -- otherwise, let `p₀` be a point where the value of `f` has maximal norm obtain ⟨p₀, hp₀s, hp₀⟩ := hs₁.exists_forall_ge hs' hf.continuous.norm.continuous_on, -- we will show `f` agrees everywhere with `f p₀` suffices : s ⊆ {r : M | f r = f p₀} ∩ s, { exact (this hb).1.trans (this ha).1.symm }, clear ha hb a b, refine hs₂.subset_clopen _ ⟨p₀, hp₀s, ⟨rfl, hp₀s⟩⟩, -- closedness of the set of points sent to `f p₀` refine ⟨_, (is_closed_singleton.preimage hf.continuous).inter hs₃.2⟩, -- we will show this set is open by showing it is a neighbourhood of each of its members rw is_open_iff_mem_nhds, rintros p ⟨hp : f p = _, hps⟩, -- let `p` be in this set have hps' : s ∈ 𝓝 p := hs₃.1.mem_nhds hps, have key₁ : (chart_at E p).symm ⁻¹' s ∈ 𝓝 (chart_at E p p), { rw [← filter.mem_map, (chart_at E p).symm_map_nhds_eq (mem_chart_source E p)], exact hps' }, have key₂ : (chart_at E p).target ∈ 𝓝 (chart_at E p p) := (local_homeomorph.open_target _).mem_nhds (mem_chart_target E p), -- `f` pulled back by the chart at `p` is differentiable around `chart_at E p p` have hf' : ∀ᶠ (z : E) in 𝓝 (chart_at E p p), differentiable_at ℂ (f ∘ (chart_at E p).symm) z, { refine filter.eventually_of_mem key₂ (λ z hz, _), have H₁ : (chart_at E p).symm z ∈ (chart_at E p).source := (chart_at E p).map_target hz, have H₂ : f ((chart_at E p).symm z) ∈ (chart_at F (0:F)).source := trivial, have H := (mdifferentiable_at_iff_of_mem_source H₁ H₂).mp (hf ((chart_at E p).symm z)), simp only [differentiable_within_at_univ] with mfld_simps at H, simpa [local_homeomorph.right_inv _ hz] using H.2, }, -- `f` pulled back by the chart at `p` has a local max at `chart_at E p p` have hf'' : is_local_max (norm ∘ f ∘ (chart_at E p).symm) (chart_at E p p), { refine filter.eventually_of_mem key₁ (λ z hz, _), refine (hp₀ ((chart_at E p).symm z) hz).trans (_ : ‖f p₀‖ ≤ ‖f _‖), rw [← hp, local_homeomorph.left_inv _ (mem_chart_source E p)] }, -- so by the maximum principle `f` is equal to `f p` near `p` obtain ⟨U, hU, hUf⟩ := (complex.eventually_eq_of_is_local_max_norm hf' hf'').exists_mem, have H₁ : (chart_at E p) ⁻¹' U ∈ 𝓝 p := (chart_at E p).continuous_at (mem_chart_source E p) hU, have H₂ : (chart_at E p).source ∈ 𝓝 p := (local_homeomorph.open_source _).mem_nhds (mem_chart_source E p), apply filter.mem_of_superset (filter.inter_mem hps' (filter.inter_mem H₁ H₂)), rintros q ⟨hqs, hq : chart_at E p q ∈ _, hq'⟩, refine ⟨_, hqs⟩, simpa [local_homeomorph.left_inv _ hq', hp, -norm_eq_abs] using hUf (chart_at E p q) hq, end /-- A holomorphic function on a compact connected complex manifold is constant. -/ lemma apply_eq_of_compact_space [preconnected_space M] {f : M → F} (hf : mdifferentiable 𝓘(ℂ, E) 𝓘(ℂ, F) f) (a b : M) : f a = f b := hf.is_locally_constant.apply_eq_of_preconnected_space _ _ /-- A holomorphic function on a compact connected complex manifold is the constant function `f ≡ v`, for some value `v`. -/ lemma exists_eq_const_of_compact_space [preconnected_space M] {f : M → F} (hf : mdifferentiable 𝓘(ℂ, E) 𝓘(ℂ, F) f) : ∃ v : F, f = function.const M v := hf.is_locally_constant.exists_eq_const end mdifferentiable
5edbb00609662d398aa543749aa51aaf1d53923b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/specific_limits/basic.lean
aecf5fd432bf4228fc7fd374cd81f5311c55cca6
[ "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
26,044
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl, Yury G. Kudryashov, Patrick Massot -/ import algebra.geom_sum import order.filter.archimedean import order.iterate import topology.instances.ennreal import topology.algebra.algebra /-! # A collection of specific limit computations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file, by design, is independent of `normed_space` in the import hierarchy. It contains important specific limit computations in metric spaces, in ordered rings/fields, and in specific instances of these such as `ℝ`, `ℝ≥0` and `ℝ≥0∞`. -/ noncomputable theory open classical set function filter finset metric open_locale classical topology nat big_operators uniformity nnreal ennreal variables {α : Type*} {β : Type*} {ι : Type*} lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero.comp tendsto_coe_nat_at_top_at_top lemma tendsto_const_div_at_top_nhds_0_nat (C : ℝ) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa only [mul_zero] using tendsto_const_nhds.mul tendsto_inverse_at_top_nhds_0_nat lemma nnreal.tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ≥0)⁻¹) at_top (𝓝 0) := by { rw ← nnreal.tendsto_coe, exact tendsto_inverse_at_top_nhds_0_nat } lemma nnreal.tendsto_const_div_at_top_nhds_0_nat (C : ℝ≥0) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa using tendsto_const_nhds.mul nnreal.tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (𝓝 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (𝓝 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 (tendsto_const_div_at_top_nhds_0_nat 1) /-- The limit of `n / (n + x)` is 1, for any constant `x` (valid in `ℝ` or any topological division algebra over `ℝ`, e.g., `ℂ`). TODO: introduce a typeclass saying that `1 / n` tends to 0 at top, making it possible to get this statement simultaneously on `ℚ`, `ℝ` and `ℂ`. -/ lemma tendsto_coe_nat_div_add_at_top {𝕜 : Type*} [division_ring 𝕜] [topological_space 𝕜] [char_zero 𝕜] [algebra ℝ 𝕜] [has_continuous_smul ℝ 𝕜] [topological_division_ring 𝕜] (x : 𝕜) : tendsto (λ n:ℕ, (n:𝕜) / (n + x)) at_top (𝓝 1) := begin refine tendsto.congr' ((eventually_ne_at_top 0).mp (eventually_of_forall (λ n hn, _))) _, { exact λ n:ℕ, 1 / (1 + x / n) }, { field_simp [nat.cast_ne_zero.mpr hn] }, { have : 𝓝 (1:𝕜) = 𝓝 (1 / (1 + x * ↑(0:ℝ))), by rw [algebra_map.coe_zero, mul_zero, add_zero, div_one], rw this, refine tendsto_const_nhds.div (tendsto_const_nhds.add _) (by simp), simp_rw div_eq_mul_inv, refine (tendsto_const_nhds.mul _), have : (λ n : ℕ, (n : 𝕜)⁻¹) = (λ n : ℕ, ↑((n : ℝ)⁻¹)), { ext1 n, rw [←(map_nat_cast (algebra_map ℝ 𝕜) n), ←map_inv₀ (algebra_map ℝ 𝕜)], refl, }, rw this, exact ((continuous_algebra_map ℝ 𝕜).tendsto _).comp tendsto_inverse_at_top_nhds_0_nat } end /-! ### Powers -/ lemma tendsto_add_one_pow_at_top_at_top_of_pos [linear_ordered_semiring α] [archimedean α] {r : α} (h : 0 < r) : tendsto (λ n:ℕ, (r + 1)^n) at_top at_top := tendsto_at_top_at_top_of_monotone' (λ n m, pow_le_pow (le_add_of_nonneg_left (le_of_lt h))) $ not_bdd_above_iff.2 $ λ x, set.exists_range_iff.2 $ add_one_pow_unbounded_of_pos _ h lemma tendsto_pow_at_top_at_top_of_one_lt [linear_ordered_ring α] [archimedean α] {r : α} (h : 1 < r) : tendsto (λn:ℕ, r ^ n) at_top at_top := sub_add_cancel r 1 ▸ tendsto_add_one_pow_at_top_at_top_of_pos (sub_pos.2 h) lemma nat.tendsto_pow_at_top_at_top_of_one_lt {m : ℕ} (h : 1 < m) : tendsto (λn:ℕ, m ^ n) at_top at_top := tsub_add_cancel_of_le (le_of_lt h) ▸ tendsto_add_one_pow_at_top_at_top_of_pos (tsub_pos_of_lt h) lemma tendsto_pow_at_top_nhds_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := h₁.eq_or_lt.elim (assume : 0 = r, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, ← this, tendsto_const_nhds]) (assume : 0 < r, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (𝓝 0), from tendsto_inv_at_top_zero.comp (tendsto_pow_at_top_at_top_of_one_lt $ one_lt_inv this h₂), this.congr (λ n, by simp)) lemma tendsto_pow_at_top_nhds_within_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 < r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝[>] 0) := tendsto_inf.2 ⟨tendsto_pow_at_top_nhds_0_of_lt_1 h₁.le h₂, tendsto_principal.2 $ eventually_of_forall $ λ n, pow_pos h₁ _⟩ lemma uniformity_basis_dist_pow_of_lt_1 {α : Type*} [pseudo_metric_space α] {r : ℝ} (h₀ : 0 < r) (h₁ : r < 1) : (𝓤 α).has_basis (λ k : ℕ, true) (λ k, {p : α × α | dist p.1 p.2 < r ^ k}) := metric.mk_uniformity_basis (λ i _, pow_pos h₀ _) $ λ ε ε0, (exists_pow_lt_of_lt_one ε0 h₁).imp $ λ k hk, ⟨trivial, hk.le⟩ lemma geom_lt {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, c * u k < u (k + 1)) : c ^ n * u 0 < u n := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_le_of_lt hn _ _ h, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma geom_le {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, c * u k ≤ u (k + 1)) : c ^ n * u 0 ≤ u n := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ _ h; simp [pow_succ, mul_assoc, le_refl] lemma lt_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, u (k + 1) < c * u k) : u n < c ^ n * u 0 := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_lt_of_le hn _ h _, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma le_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, u (k + 1) ≤ c * u k) : u n ≤ (c ^ n) * u 0 := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ h _; simp [pow_succ, mul_assoc, le_refl] /-- If a sequence `v` of real numbers satisfies `k * v n ≤ v (n+1)` with `1 < k`, then it goes to +∞. -/ lemma tendsto_at_top_of_geom_le {v : ℕ → ℝ} {c : ℝ} (h₀ : 0 < v 0) (hc : 1 < c) (hu : ∀ n, c * v n ≤ v (n + 1)) : tendsto v at_top at_top := tendsto_at_top_mono (λ n, geom_le (zero_le_one.trans hc.le) n (λ k hk, hu k)) $ (tendsto_pow_at_top_at_top_of_one_lt hc).at_top_mul_const h₀ lemma nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := nnreal.tendsto_coe.1 $ by simp only [nnreal.coe_pow, nnreal.coe_zero, tendsto_pow_at_top_nhds_0_of_lt_1 r.coe_nonneg hr] lemma ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0∞} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := begin rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, rw [← ennreal.coe_zero], norm_cast at *, apply nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 hr end /-! ### Geometric series-/ section geometric lemma has_sum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := have r ≠ 1, from ne_of_lt h₂, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (𝓝 ((0 - 1) * (r - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂).sub tendsto_const_nhds).mul tendsto_const_nhds, (has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum_eq, div_eq_mul_inv, *] at * lemma summable_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, has_sum_geometric_of_lt_1 h₁ h₂⟩ lemma tsum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (has_sum_geometric_of_lt_1 h₁ h₂).tsum_eq lemma has_sum_geometric_two : has_sum (λn:ℕ, ((1:ℝ)/2) ^ n) 2 := by convert has_sum_geometric_of_lt_1 _ _; norm_num lemma summable_geometric_two : summable (λn:ℕ, ((1:ℝ)/2) ^ n) := ⟨_, has_sum_geometric_two⟩ lemma summable_geometric_two_encode {ι : Type*} [encodable ι] : summable (λ (i : ι), (1/2 : ℝ)^(encodable.encode i)) := summable_geometric_two.comp_injective encodable.encode_injective lemma tsum_geometric_two : ∑'n:ℕ, ((1:ℝ)/2) ^ n = 2 := has_sum_geometric_two.tsum_eq lemma sum_geometric_two_le (n : ℕ) : ∑ (i : ℕ) in range n, (1 / (2 : ℝ)) ^ i ≤ 2 := begin have : ∀ i, 0 ≤ (1 / (2 : ℝ)) ^ i, { intro i, apply pow_nonneg, norm_num }, convert sum_le_tsum (range n) (λ i _, this i) summable_geometric_two, exact tsum_geometric_two.symm end lemma tsum_geometric_inv_two : ∑' n : ℕ, (2 : ℝ)⁻¹ ^ n = 2 := (inv_eq_one_div (2 : ℝ)).symm ▸ tsum_geometric_two /-- The sum of `2⁻¹ ^ i` for `n ≤ i` equals `2 * 2⁻¹ ^ n`. -/ lemma tsum_geometric_inv_two_ge (n : ℕ) : ∑' i, ite (n ≤ i) ((2 : ℝ)⁻¹ ^ i) 0 = 2 * 2⁻¹ ^ n := begin have A : summable (λ (i : ℕ), ite (n ≤ i) ((2⁻¹ : ℝ) ^ i) 0), { apply summable_of_nonneg_of_le _ _ summable_geometric_two; { intro i, by_cases hi : n ≤ i; simp [hi] } }, have B : (finset.range n).sum (λ (i : ℕ), ite (n ≤ i) ((2⁻¹ : ℝ)^i) 0) = 0 := finset.sum_eq_zero (λ i hi, ite_eq_right_iff.2 $ λ h, (lt_irrefl _ ((finset.mem_range.1 hi).trans_le h)).elim), simp only [← sum_add_tsum_nat_add n A, B, if_true, zero_add, zero_le', le_add_iff_nonneg_left, pow_add, tsum_mul_right, tsum_geometric_inv_two], end lemma has_sum_geometric_two' (a : ℝ) : has_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert has_sum.mul_left (a / 2) (has_sum_geometric_of_lt_1 (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, refl, }, { norm_num } end lemma summable_geometric_two' (a : ℝ) : summable (λ n:ℕ, (a / 2) / 2 ^ n) := ⟨a, has_sum_geometric_two' a⟩ lemma tsum_geometric_two' (a : ℝ) : ∑' n:ℕ, (a / 2) / 2^n = a := (has_sum_geometric_two' a).tsum_eq /-- **Sum of a Geometric Series** -/ lemma nnreal.has_sum_geometric {r : ℝ≥0} (hr : r < 1) : has_sum (λ n : ℕ, r ^ n) (1 - r)⁻¹ := begin apply nnreal.has_sum_coe.1, push_cast, rw [nnreal.coe_sub (le_of_lt hr)], exact has_sum_geometric_of_lt_1 r.coe_nonneg hr end lemma nnreal.summable_geometric {r : ℝ≥0} (hr : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, nnreal.has_sum_geometric hr⟩ lemma tsum_geometric_nnreal {r : ℝ≥0} (hr : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (nnreal.has_sum_geometric hr).tsum_eq /-- The series `pow r` converges to `(1-r)⁻¹`. For `r < 1` the RHS is a finite number, and for `1 ≤ r` the RHS equals `∞`. -/ @[simp] lemma ennreal.tsum_geometric (r : ℝ≥0∞) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := begin cases lt_or_le r 1 with hr hr, { rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, norm_cast at *, convert ennreal.tsum_coe_eq (nnreal.has_sum_geometric hr), rw [ennreal.coe_inv $ ne_of_gt $ tsub_pos_iff_lt.2 hr] }, { rw [tsub_eq_zero_iff_le.mpr hr, ennreal.inv_zero, ennreal.tsum_eq_supr_nat, supr_eq_top], refine λ a ha, (ennreal.exists_nat_gt (lt_top_iff_ne_top.1 ha)).imp (λ n hn, lt_of_lt_of_le hn _), calc (n:ℝ≥0∞) = ∑ i in range n, 1 : by rw [sum_const, nsmul_one, card_range] ... ≤ ∑ i in range n, r ^ i : sum_le_sum (λ k _, one_le_pow_of_one_le' hr k) } end end geometric /-! ### Sequences with geometrically decaying distance in metric spaces In this paragraph, we discuss sequences in metric spaces or emetric spaces for which the distance between two consecutive terms decays geometrically. We show that such sequences are Cauchy sequences, and bound their distances to the limit. We also discuss series with geometrically decaying terms. -/ section edist_le_geometric variables [pseudo_emetric_space α] (r C : ℝ≥0∞) (hr : r < 1) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C * r^n) include hr hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, `C ≠ ∞`, `r < 1`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric : cauchy_seq f := begin refine cauchy_seq_of_edist_le_of_tsum_ne_top _ hu _, rw [ennreal.tsum_mul_left, ennreal.tsum_geometric], refine ennreal.mul_ne_top hC (ennreal.inv_ne_top.2 _), exact (tsub_pos_iff_lt.2 hr).ne' end omit hr hC /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ (C * r^n) / (1 - r) := begin convert edist_le_tsum_of_edist_le_of_tendsto _ hu ha _, simp only [pow_add, ennreal.tsum_mul_left, ennreal.tsum_geometric, div_eq_mul_inv, mul_assoc] end /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ C / (1 - r) := by simpa only [pow_zero, mul_one] using edist_le_of_edist_le_geometric_of_tendsto r C hu ha 0 end edist_le_geometric section edist_le_geometric_two variables [pseudo_emetric_space α] (C : ℝ≥0∞) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C / 2^n) {a : α} (ha : tendsto f at_top (𝓝 a)) include hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric_two : cauchy_seq f := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at hu, refine cauchy_seq_of_edist_le_geometric 2⁻¹ C _ hC hu, simp [ennreal.one_lt_two] end omit hC include ha /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f n` to the limit of `f` is bounded above by `2 * C * 2^-n`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto (n : ℕ) : edist (f n) a ≤ 2 * C / 2^n := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at *, rw [mul_assoc, mul_comm], convert edist_le_of_edist_le_geometric_of_tendsto 2⁻¹ C hu ha n, rw [ennreal.one_sub_inv_two, inv_inv] end /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f 0` to the limit of `f` is bounded above by `2 * C`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto₀: edist (f 0) a ≤ 2 * C := by simpa only [pow_zero, div_eq_mul_inv, inv_one, mul_one] using edist_le_of_edist_le_geometric_two_of_tendsto C hu ha 0 end edist_le_geometric_two section le_geometric variables [pseudo_metric_space α] {r C : ℝ} (hr : r < 1) {f : ℕ → α} (hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n) include hr hu lemma aux_has_sum_of_le_geometric : has_sum (λ n : ℕ, C * r^n) (C / (1 - r)) := begin rcases sign_cases_of_C_mul_pow_nonneg (λ n, dist_nonneg.trans (hu n)) with rfl | ⟨C₀, r₀⟩, { simp [has_sum_zero] }, { refine has_sum.mul_left C _, simpa using has_sum_geometric_of_lt_1 r₀ hr } end variables (r C) /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then `f` is a Cauchy sequence. Note that this lemma does not assume `0 ≤ C` or `0 ≤ r`. -/ lemma cauchy_seq_of_le_geometric : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C / (1 - r) := (aux_has_sum_of_le_geometric hr hu).tsum_eq ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ ha /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ (C * r^n) / (1 - r) := begin have := aux_has_sum_of_le_geometric hr hu, convert dist_le_tsum_of_dist_le_of_tendsto _ hu ⟨_, this⟩ ha n, simp only [pow_add, mul_left_comm C, mul_div_right_comm], rw [mul_comm], exact (this.mul_left _).tsum_eq.symm end omit hr hu variable (hu₂ : ∀ n, dist (f n) (f (n+1)) ≤ (C / 2) / 2^n) /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then `f` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_geometric_two : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu₂ $ ⟨_, has_sum_geometric_two' C⟩ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C`. -/ lemma dist_le_of_le_geometric_two_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C := (tsum_geometric_two' C) ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu₂ (summable_geometric_two' C) ha include hu₂ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f n` to the limit of `f` is bounded above by `C / 2^n`. -/ lemma dist_le_of_le_geometric_two_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ C / 2^n := begin convert dist_le_tsum_of_dist_le_of_tendsto _ hu₂ (summable_geometric_two' C) ha n, simp only [add_comm n, pow_add, ← div_div], symmetry, exact ((has_sum_geometric_two' C).div_const _).tsum_eq end end le_geometric /-! ### Summability tests based on comparison with geometric series -/ /-- A series whose terms are bounded by the terms of a converging geometric series converges. -/ lemma summable_one_div_pow_of_le {m : ℝ} {f : ℕ → ℕ} (hm : 1 < m) (fi : ∀ i, i ≤ f i) : summable (λ i, 1 / m ^ f i) := begin refine summable_of_nonneg_of_le (λ a, one_div_nonneg.mpr (pow_nonneg (zero_le_one.trans hm.le) _)) (λ a, _) (summable_geometric_of_lt_1 (one_div_nonneg.mpr (zero_le_one.trans hm.le)) ((one_div_lt (zero_lt_one.trans hm) zero_lt_one).mpr (one_div_one.le.trans_lt hm))), rw [div_pow, one_pow], refine (one_div_le_one_div _ _).mpr (pow_le_pow hm.le (fi a)); exact pow_pos (zero_lt_one.trans hm) _ end /-! ### Positive sequences with small sums on countable types -/ /-- For any positive `ε`, define on an encodable type a positive sequence with sum less than `ε` -/ def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, has_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : has_sum f ε := has_sum_geometric_two' _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos zero_lt_two _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, rcases hf.summable.comp_injective (@encodable.encode_injective ι _) with ⟨c, hg⟩, refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_rfl } end lemma set.countable.exists_pos_has_sum_le {ι : Type*} {s : set ι} (hs : s.countable) {ε : ℝ} (hε : 0 < ε) : ∃ ε' : ι → ℝ, (∀ i, 0 < ε' i) ∧ ∃ c, has_sum (λ i : s, ε' i) c ∧ c ≤ ε := begin haveI := hs.to_encodable, rcases pos_sum_of_encodable hε s with ⟨f, hf0, ⟨c, hfc, hcε⟩⟩, refine ⟨λ i, if h : i ∈ s then f ⟨i, h⟩ else 1, λ i, _, ⟨c, _, hcε⟩⟩, { split_ifs, exacts [hf0 _, zero_lt_one] }, { simpa only [subtype.coe_prop, dif_pos, subtype.coe_eta] } end lemma set.countable.exists_pos_forall_sum_le {ι : Type*} {s : set ι} (hs : s.countable) {ε : ℝ} (hε : 0 < ε) : ∃ ε' : ι → ℝ, (∀ i, 0 < ε' i) ∧ ∀ t : finset ι, ↑t ⊆ s → ∑ i in t, ε' i ≤ ε := begin rcases hs.exists_pos_has_sum_le hε with ⟨ε', hpos, c, hε'c, hcε⟩, refine ⟨ε', hpos, λ t ht, _⟩, rw [← sum_subtype_of_mem _ ht], refine (sum_le_has_sum _ _ hε'c).trans hcε, exact λ _ _, (hpos _).le end namespace nnreal theorem exists_pos_sum_of_countable {ε : ℝ≥0} (hε : ε ≠ 0) (ι) [countable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∃c, has_sum ε' c ∧ c < ε := begin casesI nonempty_encodable ι, obtain ⟨a, a0, aε⟩ := exists_between (pos_iff_ne_zero.2 hε), obtain ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι, exact ⟨λ i, ⟨ε' i, (hε' i).le⟩, λ i, nnreal.coe_lt_coe.1 $ hε' i, ⟨c, has_sum_le (λ i, (hε' i).le) has_sum_zero hc⟩, nnreal.has_sum_coe.1 hc, aε.trans_le' $ nnreal.coe_le_coe.1 hcε⟩, end end nnreal namespace ennreal theorem exists_pos_sum_of_countable {ε : ℝ≥0∞} (hε : ε ≠ 0) (ι) [countable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∑' i, (ε' i : ℝ≥0∞) < ε := begin rcases exists_between (pos_iff_ne_zero.2 hε) with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_countable (coe_pos.1 h0r).ne' ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end theorem exists_pos_sum_of_countable' {ε : ℝ≥0∞} (hε : ε ≠ 0) (ι) [countable ι] : ∃ ε' : ι → ℝ≥0∞, (∀ i, 0 < ε' i) ∧ (∑' i, ε' i) < ε := let ⟨δ, δpos, hδ⟩ := exists_pos_sum_of_countable hε ι in ⟨λ i, δ i, λ i, ennreal.coe_pos.2 (δpos i), hδ⟩ theorem exists_pos_tsum_mul_lt_of_countable {ε : ℝ≥0∞} (hε : ε ≠ 0) {ι} [countable ι] (w : ι → ℝ≥0∞) (hw : ∀ i, w i ≠ ∞) : ∃ δ : ι → ℝ≥0, (∀ i, 0 < δ i) ∧ ∑' i, (w i * δ i : ℝ≥0∞) < ε := begin lift w to ι → ℝ≥0 using hw, rcases exists_pos_sum_of_countable hε ι with ⟨δ', Hpos, Hsum⟩, have : ∀ i, 0 < max 1 (w i), from λ i, zero_lt_one.trans_le (le_max_left _ _), refine ⟨λ i, δ' i / max 1 (w i), λ i, div_pos (Hpos _) (this i), _⟩, refine lt_of_le_of_lt (ennreal.tsum_le_tsum $ λ i, _) Hsum, rw [coe_div (this i).ne'], refine mul_le_of_le_div' (mul_le_mul_left' (ennreal.inv_le_inv.2 _) _), exact coe_le_coe.2 (le_max_right _ _) end end ennreal /-! ### Factorial -/ lemma factorial_tendsto_at_top : tendsto nat.factorial at_top at_top := tendsto_at_top_at_top_of_monotone nat.monotone_factorial (λ n, ⟨n, n.self_le_factorial⟩) lemma tendsto_factorial_div_pow_self_at_top : tendsto (λ n, n! / n^n : ℕ → ℝ) at_top (𝓝 0) := tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_const_div_at_top_nhds_0_nat 1) (eventually_of_forall $ λ n, div_nonneg (by exact_mod_cast n.factorial_pos.le) (pow_nonneg (by exact_mod_cast n.zero_le) _)) begin refine (eventually_gt_at_top 0).mono (λ n hn, _), rcases nat.exists_eq_succ_of_ne_zero hn.ne.symm with ⟨k, rfl⟩, rw [← prod_range_add_one_eq_factorial, pow_eq_prod_const, div_eq_mul_inv, ← inv_eq_one_div, prod_nat_cast, nat.cast_succ, ← prod_inv_distrib, ← prod_mul_distrib, finset.prod_range_succ'], simp only [prod_range_succ', one_mul, nat.cast_add, zero_add, nat.cast_one], refine mul_le_of_le_one_left (inv_nonneg.mpr $ by exact_mod_cast hn.le) (prod_le_one _ _); intros x hx; rw finset.mem_range at hx, { refine mul_nonneg _ (inv_nonneg.mpr _); norm_cast; linarith }, { refine (div_le_one $ by exact_mod_cast hn).mpr _, norm_cast, linarith } end /-! ### Ceil and floor -/ section lemma tendsto_nat_floor_at_top {α : Type*} [linear_ordered_semiring α] [floor_semiring α] : tendsto (λ (x : α), ⌊x⌋₊) at_top at_top := nat.floor_mono.tendsto_at_top_at_top (λ x, ⟨max 0 (x + 1), by simp [nat.le_floor_iff]⟩) variables {R : Type*} [topological_space R] [linear_ordered_field R] [order_topology R] [floor_ring R] lemma tendsto_nat_floor_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌊a * x⌋₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a - x⁻¹) at_top (𝓝 (a - 0)) := tendsto_const_nhds.sub tendsto_inv_at_top_zero, rw sub_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' A tendsto_const_nhds, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp only [le_div_iff (zero_lt_one.trans_le hx), sub_mul, inv_mul_cancel (zero_lt_one.trans_le hx).ne'], have := nat.lt_floor_add_one (a * x), linarith }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw div_le_iff (zero_lt_one.trans_le hx), simp [nat.floor_le (mul_nonneg ha (zero_le_one.trans hx))] } end lemma tendsto_nat_floor_div_at_top : tendsto (λ x, (⌊x⌋₊ : R) / x) at_top (𝓝 1) := by simpa using tendsto_nat_floor_mul_div_at_top (zero_le_one' R) lemma tendsto_nat_ceil_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌈a * x⌉₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a + x⁻¹) at_top (𝓝 (a + 0)) := tendsto_const_nhds.add tendsto_inv_at_top_zero, rw add_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds A, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw le_div_iff (zero_lt_one.trans_le hx), exact nat.le_ceil _ }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp [div_le_iff (zero_lt_one.trans_le hx), inv_mul_cancel (zero_lt_one.trans_le hx).ne', (nat.ceil_lt_add_one ((mul_nonneg ha (zero_le_one.trans hx)))).le, add_mul] } end lemma tendsto_nat_ceil_div_at_top : tendsto (λ x, (⌈x⌉₊ : R) / x) at_top (𝓝 1) := by simpa using tendsto_nat_ceil_mul_div_at_top (zero_le_one' R) end
b0a5eaaaf28389a30c61f603150dceb1a8f47940
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/delabUnexpand.lean
eea0f8505f4bf1f378313dba19aa64ac2dbb2d9d
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
754
lean
structure Foo where x : Nat y : Nat macro a:term " ♬ " b:term : term => `(Foo.mk $a $b) @[appUnexpander Foo.mk] def unexpandFooFailure1 : Lean.PrettyPrinter.Unexpander | _ => throw () @[appUnexpander Foo.mk] def unexpandFoo : Lean.PrettyPrinter.Unexpander | `(Foo.mk $a $b) => `($a ♬ $b) | _ => throw () @[appUnexpander Foo.mk] def unexpandFooFailure2 : Lean.PrettyPrinter.Unexpander | _ => throw () #check 3 ♬ 4 def foo (k : Nat → Nat) (n : Nat) : Nat := k (n+1) @[appUnexpander foo] def unexpandFooApp : Lean.PrettyPrinter.Unexpander | `(foo $k $a) => `(My.foo $k $a) | _ => throw () #check foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ foo $ id
d5d41374370ab53f23578d3d3df6ab0a848c694c
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/uniform_space/cauchy.lean
7c0f4e83bc157891daa21c57caa191124b640ecc
[ "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
30,090
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.bases import topology.uniform_space.basic /-! # Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ universes u v open filter topological_space set classical uniform_space open_locale classical uniformity topological_space filter variables {α : Type u} {β : Type v} [uniform_space α] /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := ne_bot f ∧ f ×ᶠ f ≤ (𝓤 α) /-- A set `s` is called *complete*, if any Cauchy filter `f` such that `s ∈ f` has a limit in `s` (formally, it satisfies `f ≤ 𝓝 x` for some `x ∈ s`). -/ def is_complete (s : set α) := ∀f, cauchy f → f ≤ 𝓟 s → ∃x∈s, f ≤ 𝓝 x lemma filter.has_basis.cauchy_iff {ι} {p : ι → Prop} {s : ι → set (α × α)} (h : (𝓤 α).has_basis p s) {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ i, p i → ∃ t ∈ f, ∀ x y ∈ t, (x, y) ∈ s i)) := and_congr iff.rfl $ (f.basis_sets.prod_self.le_basis_iff h).trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id, ball_mem_comm] lemma cauchy_iff' {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, ∀ x y ∈ t, (x, y) ∈ s)) := (𝓤 α).basis_sets.cauchy_iff lemma cauchy_iff {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, t ×ˢ t ⊆ s)) := cauchy_iff'.trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id, ball_mem_comm] lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (ne_bot l ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α)) := by rw [cauchy, map_ne_bot_iff, prod_map_map_eq, tendsto] lemma cauchy_map_iff' {l : filter β} [hl : ne_bot l] {f : β → α} : cauchy (l.map f) ↔ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α) := cauchy_map_iff.trans $ and_iff_right hl lemma cauchy.mono {f g : filter α} [hg : ne_bot g] (h_c : cauchy f) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy.mono' {f g : filter α} (h_c : cauchy f) (hg : ne_bot g) (h_le : g ≤ f) : cauchy g := h_c.mono h_le lemma cauchy_nhds {a : α} : cauchy (𝓝 a) := ⟨nhds_ne_bot, nhds_prod_eq.symm.trans_le (nhds_le_uniformity a)⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_nhds.mono (pure_le_nhds a) lemma filter.tendsto.cauchy_map {l : filter β} [ne_bot l] {f : β → α} {a : α} (h : tendsto f l (𝓝 a)) : cauchy (map f l) := cauchy_nhds.mono h lemma cauchy.prod [uniform_space β] {f : filter α} {g : filter β} (hf : cauchy f) (hg : cauchy g) : cauchy (f ×ᶠ g) := begin refine ⟨hf.1.prod hg.1, _⟩, simp only [uniformity_prod, le_inf_iff, ← map_le_iff_le_comap, ← prod_map_map_eq], exact ⟨le_trans (prod_mono tendsto_fst tendsto_fst) hf.2, le_trans (prod_mono tendsto_snd tendsto_snd) hg.2⟩ end /-- The common part of the proofs of `le_nhds_of_cauchy_adhp` and `sequentially_complete.le_nhds_of_seq_tendsto_nhds`: if for any entourage `s` one can choose a set `t ∈ f` of diameter `s` such that it contains a point `y` with `(x, y) ∈ s`, then `f` converges to `x`. -/ lemma le_nhds_of_cauchy_adhp_aux {f : filter α} {x : α} (adhs : ∀ s ∈ 𝓤 α, ∃ t ∈ f, (t ×ˢ t ⊆ s) ∧ ∃ y, (x, y) ∈ s ∧ y ∈ t) : f ≤ 𝓝 x := begin -- Consider a neighborhood `s` of `x` assume s hs, -- Take an entourage twice smaller than `s` rcases comp_mem_uniformity_sets (mem_nhds_uniformity_iff_right.1 hs) with ⟨U, U_mem, hU⟩, -- Take a set `t ∈ f`, `t × t ⊆ U`, and a point `y ∈ t` such that `(x, y) ∈ U` rcases adhs U U_mem with ⟨t, t_mem, ht, y, hxy, hy⟩, apply mem_of_superset t_mem, -- Given a point `z ∈ t`, we have `(x, y) ∈ U` and `(y, z) ∈ t × t ⊆ U`, hence `z ∈ s` exact (λ z hz, hU (prod_mk_mem_comp_rel hxy (ht $ mk_mem_prod hy hz)) rfl) end /-- If `x` is an adherent (cluster) point for a Cauchy filter `f`, then it is a limit point for `f`. -/ lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : cluster_pt x f) : f ≤ 𝓝 x := le_nhds_of_cauchy_adhp_aux begin assume s hs, obtain ⟨t, t_mem, ht⟩ : ∃ t ∈ f, t ×ˢ t ⊆ s, from (cauchy_iff.1 hf).2 s hs, use [t, t_mem, ht], exact (forall_mem_nonempty_iff_ne_bot.2 adhs _ (inter_mem_inf (mem_nhds_left x hs) t_mem )) end lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ 𝓝 x ↔ cluster_pt x f := ⟨assume h, cluster_pt.of_le_nhds' h hf.1, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy.map [uniform_space β] {f : filter α} {m : α → β} (hf : cauchy f) (hm : uniform_continuous m) : cauchy (map m f) := ⟨hf.1.map _, calc map m f ×ᶠ map m f = map (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy.comap [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) [ne_bot (comap m f)] : cauchy (comap m f) := ⟨‹_›, calc comap m f ×ᶠ comap m f = comap (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ lemma cauchy.comap' [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hb : ne_bot (comap m f)) : cauchy (comap m f) := hf.comap hm /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq.tendsto_uniformity [semilattice_sup β] {u : β → α} (h : cauchy_seq u) : tendsto (prod.map u u) at_top (𝓤 α) := by simpa only [tendsto, prod_map_map_eq', prod_at_top_at_top_eq] using h.right lemma cauchy_seq.nonempty [semilattice_sup β] {u : β → α} (hu : cauchy_seq u) : nonempty β := @nonempty_of_ne_bot _ _ $ (map_ne_bot_iff _).1 hu.1 lemma cauchy_seq.mem_entourage {β : Type*} [semilattice_sup β] {u : β → α} (h : cauchy_seq u) {V : set (α × α)} (hV : V ∈ 𝓤 α) : ∃ k₀, ∀ i j, k₀ ≤ i → k₀ ≤ j → (u i, u j) ∈ V := begin haveI := h.nonempty, have := h.tendsto_uniformity, rw ← prod_at_top_at_top_eq at this, simpa [maps_to] using at_top_basis.prod_self.tendsto_left_iff.1 this V hV end lemma filter.tendsto.cauchy_seq [semilattice_sup β] [nonempty β] {f : β → α} {x} (hx : tendsto f at_top (𝓝 x)) : cauchy_seq f := hx.cauchy_map lemma cauchy_seq_const (x : α) : cauchy_seq (λ n : ℕ, x) := tendsto_const_nhds.cauchy_seq lemma cauchy_seq_iff_tendsto [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ tendsto (prod.map u u) at_top (𝓤 α) := cauchy_map_iff'.trans $ by simp only [prod_at_top_at_top_eq, prod.map_def] lemma cauchy_seq.comp_tendsto {γ} [semilattice_sup β] [semilattice_sup γ] [nonempty γ] {f : β → α} (hf : cauchy_seq f) {g : γ → β} (hg : tendsto g at_top at_top) : cauchy_seq (f ∘ g) := cauchy_seq_iff_tendsto.2 $ hf.tendsto_uniformity.comp (hg.prod_at_top hg) lemma cauchy_seq.subseq_subseq_mem {V : ℕ → set (α × α)} (hV : ∀ n, V n ∈ 𝓤 α) {u : ℕ → α} (hu : cauchy_seq u) {f g : ℕ → ℕ} (hf : tendsto f at_top at_top) (hg : tendsto g at_top at_top) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, ((u ∘ f ∘ φ) n, (u ∘ g ∘ φ) n) ∈ V n := begin rw cauchy_seq_iff_tendsto at hu, exact ((hu.comp $ hf.prod_at_top hg).comp tendsto_at_top_diagonal).subseq_mem hV, end lemma cauchy_seq_iff' {u : ℕ → α} : cauchy_seq u ↔ ∀ V ∈ 𝓤 α, ∀ᶠ k in at_top, k ∈ (prod.map u u) ⁻¹' V := by simpa only [cauchy_seq_iff_tendsto] lemma cauchy_seq_iff {u : ℕ → α} : cauchy_seq u ↔ ∀ V ∈ 𝓤 α, ∃ N, ∀ k ≥ N, ∀ l ≥ N, (u k, u l) ∈ V := by simp [cauchy_seq_iff', filter.eventually_at_top_prod_self', prod_map] lemma cauchy_seq.prod_map {γ δ} [uniform_space β] [semilattice_sup γ] [semilattice_sup δ] {u : γ → α} {v : δ → β} (hu : cauchy_seq u) (hv : cauchy_seq v) : cauchy_seq (prod.map u v) := by simpa only [cauchy_seq, prod_map_map_eq', prod_at_top_at_top_eq] using hu.prod hv lemma cauchy_seq.prod {γ} [uniform_space β] [semilattice_sup γ] {u : γ → α} {v : γ → β} (hu : cauchy_seq u) (hv : cauchy_seq v) : cauchy_seq (λ x, (u x, v x)) := begin haveI := hu.nonempty, exact (hu.prod hv).mono (tendsto.prod_mk le_rfl le_rfl) end lemma cauchy_seq.eventually_eventually [semilattice_sup β] {u : β → α} (hu : cauchy_seq u) {V : set (α × α)} (hV : V ∈ 𝓤 α) : ∀ᶠ k in at_top, ∀ᶠ l in at_top, (u k, u l) ∈ V := eventually_at_top_curry $ hu.tendsto_uniformity hV lemma uniform_continuous.comp_cauchy_seq {γ} [uniform_space β] [semilattice_sup γ] {f : α → β} (hf : uniform_continuous f) {u : γ → α} (hu : cauchy_seq u) : cauchy_seq (f ∘ u) := hu.map hf lemma cauchy_seq.subseq_mem {V : ℕ → set (α × α)} (hV : ∀ n, V n ∈ 𝓤 α) {u : ℕ → α} (hu : cauchy_seq u) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, (u $ φ (n + 1), u $ φ n) ∈ V n := begin have : ∀ n, ∃ N, ∀ k ≥ N, ∀ l ≥ k, (u l, u k) ∈ V n, { intro n, rw [cauchy_seq_iff] at hu, rcases hu _ (hV n) with ⟨N, H⟩, exact ⟨N, λ k hk l hl, H _ (le_trans hk hl) _ hk ⟩ }, obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ, hφ : ∀ n, ∀ l ≥ φ n, (u l, u $ φ n) ∈ V n⟩ := extraction_forall_of_eventually' this, exact ⟨φ, φ_extr, λ n, hφ _ _ (φ_extr $ lt_add_one n).le⟩, end lemma filter.tendsto.subseq_mem_entourage {V : ℕ → set (α × α)} (hV : ∀ n, V n ∈ 𝓤 α) {u : ℕ → α} {a : α} (hu : tendsto u at_top (𝓝 a)) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ (u (φ 0), a) ∈ V 0 ∧ ∀ n, (u $ φ (n + 1), u $ φ n) ∈ V (n + 1) := begin rcases mem_at_top_sets.1 (hu (ball_mem_nhds a (symm_le_uniformity $ hV 0))) with ⟨n, hn⟩, rcases (hu.comp (tendsto_add_at_top_nat n)).cauchy_seq.subseq_mem (λ n, hV (n + 1)) with ⟨φ, φ_mono, hφV⟩, exact ⟨λ k, φ k + n, φ_mono.add_const _, hn _ le_add_self, hφV⟩ end /-- If a Cauchy sequence has a convergent subsequence, then it converges. -/ lemma tendsto_nhds_of_cauchy_seq_of_subseq [semilattice_sup β] {u : β → α} (hu : cauchy_seq u) {ι : Type*} {f : ι → β} {p : filter ι} [ne_bot p] (hf : tendsto f p at_top) {a : α} (ha : tendsto (u ∘ f) p (𝓝 a)) : tendsto u at_top (𝓝 a) := le_nhds_of_cauchy_adhp hu (map_cluster_pt_of_comp hf ha) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma filter.has_basis.cauchy_seq_iff {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (h : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀m n≥N, (u m, u n) ∈ s i := begin rw [cauchy_seq_iff_tendsto, ← prod_at_top_at_top_eq], refine (at_top_basis.prod_self.tendsto_iff h).trans _, simp only [exists_prop, true_and, maps_to, preimage, subset_def, prod.forall, mem_prod_eq, mem_set_of_eq, mem_Ici, and_imp, prod.map, ge_iff_le, @forall_swap (_ ≤ _) β] end lemma filter.has_basis.cauchy_seq_iff' {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (H : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀n≥N, (u n, u N) ∈ s i := begin refine H.cauchy_seq_iff.trans ⟨λ h i hi, _, λ h i hi, _⟩, { exact (h i hi).imp (λ N hN n hn, hN n hn N le_rfl) }, { rcases comp_symm_of_uniformity (H.mem_of_mem hi) with ⟨t, ht, ht', hts⟩, rcases H.mem_iff.1 ht with ⟨j, hj, hjt⟩, refine (h j hj).imp (λ N hN m hm n hn, hts ⟨u N, hjt _, ht' $ hjt _⟩), { exact hN m hm }, { exact hN n hn } } end lemma cauchy_seq_of_controlled [semilattice_sup β] [nonempty β] (U : β → set (α × α)) (hU : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) {f : β → α} (hf : ∀ {N m n : β}, N ≤ m → N ≤ n → (f m, f n) ∈ U N) : cauchy_seq f := cauchy_seq_iff_tendsto.2 begin assume s hs, rw [mem_map, mem_at_top_sets], cases hU s hs with N hN, refine ⟨(N, N), λ mn hmn, _⟩, cases mn with m n, exact hN (hf hmn.1 hmn.2) end /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ 𝓝 x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, mem_univ x, hx⟩ end instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ hf.map uniform_continuous_fst in let ⟨x2, hx2⟩ := complete_space.complete $ hf.map uniform_continuous_snd in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma complete_space_iff_is_complete_univ : complete_space α ↔ is_complete (univ : set α) := ⟨@complete_univ α _, complete_space_of_is_complete_univ⟩ lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} [ne_bot l] : cauchy l ↔ (∃x, l ≤ 𝓝 x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_nhds.mono hx⟩ lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} [ne_bot l] : cauchy (l.map f) ↔ (∃x, tendsto f l (𝓝 x)) := cauchy_iff_exists_le_nhds /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (𝓝 x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (𝓝 v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_iff_exists_image.2 ⟨univ, univ_mem, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem cauchy.le_nhds_Lim [complete_space α] [nonempty α] {f : filter α} (hf : cauchy f) : f ≤ 𝓝 (Lim f) := le_nhds_Lim (complete_space.complete hf) theorem cauchy_seq.tendsto_lim [semilattice_sup β] [complete_space α] [nonempty α] {u : β → α} (h : cauchy_seq u) : tendsto u at_top (𝓝 $ lim at_top u) := h.le_nhds_Lim lemma is_closed.is_complete [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_cluster_pt.mp h x (cf.left.mono (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := k ∩ {y | ∃ x ∈ s, (x, y) ∈ r}, choose hk f hfs hfr using λ x : u, x.coe_prop, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 hfs }, { haveI : fintype u := (fk.inter_of_left _).fintype, exact finite_range f }, { intros x xs, obtain ⟨y, hy, xy⟩ : ∃ y ∈ k, (x, y) ∈ r, from mem_Union₂.1 (ks xs), rw [bUnion_range, mem_Union], set z : ↥u := ⟨y, hy, ⟨x, xs, xy⟩⟩, exact ⟨z, rd $ mem_comp_rel.2 ⟨y, xy, rs (hfr z)⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_of_forall_symm {s : set α} (h : ∀ V ∈ 𝓤 α, symmetric_rel V → ∃ t : set α, finite t ∧ s ⊆ ⋃ y ∈ t, ball y V) : totally_bounded s := begin intros V V_in, rcases h _ (symmetrize_mem_uniformity V_in) (symmetric_symmetrize_rel V) with ⟨t, tfin, h⟩, refine ⟨t, tfin, subset.trans h _⟩, mono, intros x x_in z z_in, exact z_in.right end lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ /-- The closure of a totally bounded set is totally bounded. -/ lemma totally_bounded.closure {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : is_closed.closure_eq $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : Union₂_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ /-- The image of a totally bounded set under a unifromly continuous map is totally bounded. -/ lemma totally_bounded.image [uniform_space β] {f : α → β} {s : set α} (hs : totally_bounded s) (hf : uniform_continuous f) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, hfc.image f, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp, exact hct x hx end⟩ lemma ultrafilter.cauchy_of_totally_bounded {s : set α} (f : ultrafilter α) (hs : totally_bounded s) (h : ↑f ≤ 𝓟 s) : cauchy (f : filter α) := ⟨f.ne_bot', assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f, from mem_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f, from (ultrafilter.finite_bUnion_mem_iff hi).1 this, let ⟨y, hy, hif⟩ := this in have {x | (x,y) ∈ t'} ×ˢ {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, mem_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, ne_bot f → f ≤ 𝓟 s → ∃c ≤ f, cauchy c) := begin split, { introsI H f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, (ultrafilter.of f).cauchy_of_totally_bounded H ((ultrafilter.of_le f).trans hfs)⟩ }, { intros H d hd, contrapose! H with hd_cover, set f := ⨅ t : finset α, 𝓟 (s \ ⋃ y ∈ t, {x | (x, y) ∈ d}), have : ne_bot f, { refine infi_ne_bot_of_directed' (directed_of_sup _) _, { intros t₁ t₂ h, exact principal_mono.2 (diff_subset_diff_right $ bUnion_subset_bUnion_left h) }, { intro t, simpa [nonempty_diff] using hd_cover t t.finite_to_set } }, have : f ≤ 𝓟 s, from infi_le_of_le ∅ (by simp), refine ⟨f, ‹_›, ‹_›, λ c hcf hc, _⟩, rcases mem_prod_same_iff.1 (hc.2 hd) with ⟨m, hm, hmd⟩, have : m ∩ s ∈ c, from inter_mem hm (le_principal_iff.mp (hcf.trans ‹_›)), rcases hc.1.nonempty_of_mem this with ⟨y, hym, hys⟩, set ys := ⋃ y' ∈ ({y} : finset α), {x | (x, y') ∈ d}, have : m ⊆ ys, by simpa [ys] using λ x hx, hmd (mk_mem_prod hx hym), have : c ≤ 𝓟 (s \ ys) := hcf.trans (infi_le_of_le {y} le_rfl), refine hc.1.ne (empty_mem_iff_bot.mp _), filter_upwards [le_principal_iff.1 this, hm], refine λ x hx hxm, hx.2 _, simpa [ys] using hmd (mk_mem_prod hxm hym) } end lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f : ultrafilter α, ↑f ≤ 𝓟 s → cauchy (f : filter α)) := begin refine ⟨λ hs f, f.cauchy_of_totally_bounded hs, λ H, totally_bounded_iff_filter.2 _⟩, introsI f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, H _ ((ultrafilter.of_le f).trans hfs)⟩ end lemma compact_iff_totally_bounded_complete {s : set α} : is_compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf, let ⟨x, xs, fx⟩ := is_compact_iff_ultrafilter_le_nhds.1 hs f hf in cauchy_nhds.mono fx), λ f fc fs, let ⟨a, as, fa⟩ := @hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, is_compact_iff_ultrafilter_le_nhds.2 (λf hf, hc _ (totally_bounded_iff_ultrafilter.1 ht f hf) hf)⟩ lemma is_compact.totally_bounded {s : set α} (h : is_compact s) : totally_bounded s := (compact_iff_totally_bounded_complete.1 h).1 lemma is_compact.is_complete {s : set α} (h : is_compact s) : is_complete s := (compact_iff_totally_bounded_complete.1 h).2 @[priority 100] -- see Note [lower instance priority] instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : is_compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, hc.is_complete⟩ /-! ### Sequentially complete space In this section we prove that a uniform space is complete provided that it is sequentially complete (i.e., any Cauchy sequence converges) and its uniformity filter admits a countable generating set. In particular, this applies to (e)metric spaces, see the files `topology/metric_space/emetric_space` and `topology/metric_space/basic`. More precisely, we assume that there is a sequence of entourages `U_n` such that any other entourage includes one of `U_n`. Then any Cauchy filter `f` generates a decreasing sequence of sets `s_n ∈ f` such that `s_n × s_n ⊆ U_n`. Choose a sequence `x_n∈s_n`. It is easy to show that this is a Cauchy sequence. If this sequence converges to some `a`, then `f ≤ 𝓝 a`. -/ namespace sequentially_complete variables {f : filter α} (hf : cauchy f) {U : ℕ → set (α × α)} (U_mem : ∀ n, U n ∈ 𝓤 α) (U_le : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) open set finset noncomputable theory /-- An auxiliary sequence of sets approximating a Cauchy filter. -/ def set_seq_aux (n : ℕ) : {s : set α // ∃ (_ : s ∈ f), s ×ˢ s ⊆ U n } := indefinite_description _ $ (cauchy_iff.1 hf).2 (U n) (U_mem n) /-- Given a Cauchy filter `f` and a sequence `U` of entourages, `set_seq` provides an antitone sequence of sets `s n ∈ f` such that `s n ×ˢ s n ⊆ U`. -/ def set_seq (n : ℕ) : set α := ⋂ m ∈ Iic n, (set_seq_aux hf U_mem m).val lemma set_seq_mem (n : ℕ) : set_seq hf U_mem n ∈ f := (bInter_mem (finite_le_nat n)).2 (λ m _, (set_seq_aux hf U_mem m).2.fst) lemma set_seq_mono ⦃m n : ℕ⦄ (h : m ≤ n) : set_seq hf U_mem n ⊆ set_seq hf U_mem m := bInter_subset_bInter_left (λ k hk, le_trans hk h) lemma set_seq_sub_aux (n : ℕ) : set_seq hf U_mem n ⊆ set_seq_aux hf U_mem n := bInter_subset_of_mem right_mem_Iic lemma set_seq_prod_subset {N m n} (hm : N ≤ m) (hn : N ≤ n) : set_seq hf U_mem m ×ˢ set_seq hf U_mem n ⊆ U N := begin assume p hp, refine (set_seq_aux hf U_mem N).2.snd ⟨_, _⟩; apply set_seq_sub_aux, exact set_seq_mono hf U_mem hm hp.1, exact set_seq_mono hf U_mem hn hp.2 end /-- A sequence of points such that `seq n ∈ set_seq n`. Here `set_seq` is an antitone sequence of sets `set_seq n ∈ f` with diameters controlled by a given sequence of entourages. -/ def seq (n : ℕ) : α := some $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_mem (n : ℕ) : seq hf U_mem n ∈ set_seq hf U_mem n := some_spec $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_pair_mem ⦃N m n : ℕ⦄ (hm : N ≤ m) (hn : N ≤ n) : (seq hf U_mem m, seq hf U_mem n) ∈ U N := set_seq_prod_subset hf U_mem hm hn ⟨seq_mem hf U_mem m, seq_mem hf U_mem n⟩ include U_le theorem seq_is_cauchy_seq : cauchy_seq $ seq hf U_mem := cauchy_seq_of_controlled U U_le $ seq_pair_mem hf U_mem /-- If the sequence `sequentially_complete.seq` converges to `a`, then `f ≤ 𝓝 a`. -/ theorem le_nhds_of_seq_tendsto_nhds ⦃a : α⦄ (ha : tendsto (seq hf U_mem) at_top (𝓝 a)) : f ≤ 𝓝 a := le_nhds_of_cauchy_adhp_aux begin assume s hs, rcases U_le s hs with ⟨m, hm⟩, rcases tendsto_at_top'.1 ha _ (mem_nhds_left a (U_mem m)) with ⟨n, hn⟩, refine ⟨set_seq hf U_mem (max m n), set_seq_mem hf U_mem _, _, seq hf U_mem (max m n), _, seq_mem hf U_mem _⟩, { have := le_max_left m n, exact set.subset.trans (set_seq_prod_subset hf U_mem this this) hm }, { exact hm (hn _ $ le_max_right m n) } end end sequentially_complete namespace uniform_space open sequentially_complete variables [is_countably_generated (𝓤 α)] /-- A uniform space is complete provided that (a) its uniformity filter has a countable basis; (b) any sequence satisfying a "controlled" version of the Cauchy condition converges. -/ theorem complete_of_convergent_controlled_sequences (U : ℕ → set (α × α)) (U_mem : ∀ n, U n ∈ 𝓤 α) (HU : ∀ u : ℕ → α, (∀ N m n, N ≤ m → N ≤ n → (u m, u n) ∈ U N) → ∃ a, tendsto u at_top (𝓝 a)) : complete_space α := begin obtain ⟨U', U'_mono, hU'⟩ := (𝓤 α).exists_antitone_seq, have Hmem : ∀ n, U n ∩ U' n ∈ 𝓤 α, from λ n, inter_mem (U_mem n) (hU'.2 ⟨n, subset.refl _⟩), refine ⟨λ f hf, (HU (seq hf Hmem) (λ N m n hm hn, _)).imp $ le_nhds_of_seq_tendsto_nhds _ _ (λ s hs, _)⟩, { rcases (hU'.1 hs) with ⟨N, hN⟩, exact ⟨N, subset.trans (inter_subset_right _ _) hN⟩ }, { exact inter_subset_left _ _ (seq_pair_mem hf Hmem hm hn) } end /-- A sequentially complete uniform space with a countable basis of the uniformity filter is complete. -/ theorem complete_of_cauchy_seq_tendsto (H' : ∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) : complete_space α := let ⟨U', U'_mono, hU'⟩ := (𝓤 α).exists_antitone_seq in complete_of_convergent_controlled_sequences U' (λ n, hU'.2 ⟨n, subset.refl _⟩) (λ u hu, H' u $ cauchy_seq_of_controlled U' (λ s hs, hU'.1 hs) hu) variable (α) @[priority 100] instance first_countable_topology : first_countable_topology α := ⟨λ a, by { rw nhds_eq_comap_uniformity, apply_instance }⟩ /-- A separable uniform space with countably generated uniformity filter is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational "radii" from a countable open symmetric antitone basis of `𝓤 α`. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable [separable_space α] : second_countable_topology α := begin rcases exists_countable_dense α with ⟨s, hsc, hsd⟩, obtain ⟨t : ℕ → set (α × α), hto : ∀ (i : ℕ), t i ∈ (𝓤 α).sets ∧ is_open (t i) ∧ symmetric_rel (t i), h_basis : (𝓤 α).has_antitone_basis t⟩ := (@uniformity_has_basis_open_symmetric α _).exists_antitone_subbasis, choose ht_mem hto hts using hto, refine ⟨⟨⋃ (x ∈ s), range (λ k, ball x (t k)), hsc.bUnion (λ x hx, countable_range _), _⟩⟩, refine (is_topological_basis_of_open_of_nhds _ _).eq_generate_from, { simp only [mem_Union₂, mem_range], rintros _ ⟨x, hxs, k, rfl⟩, exact is_open_ball x (hto k) }, { intros x V hxV hVo, simp only [mem_Union₂, mem_range, exists_prop], rcases uniform_space.mem_nhds_iff.1 (is_open.mem_nhds hVo hxV) with ⟨U, hU, hUV⟩, rcases comp_symm_of_uniformity hU with ⟨U', hU', hsymm, hUU'⟩, rcases h_basis.to_has_basis.mem_iff.1 hU' with ⟨k, -, hk⟩, rcases hsd.inter_open_nonempty (ball x $ t k) (is_open_ball x (hto k)) ⟨x, uniform_space.mem_ball_self _ (ht_mem k)⟩ with ⟨y, hxy, hys⟩, refine ⟨_, ⟨y, hys, k, rfl⟩, (hts k).subset hxy, λ z hz, _⟩, exact hUV (ball_subset_of_comp_subset (hk hxy) hUU' (hk hz)) } end end uniform_space
9db89052a1b0c508cbbaf95353ccfe0f0bd9aaa6
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/category/CommRing/adjunctions.lean
c8addf2e02596a06a02a5ded3818b77e08e63245
[]
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,402
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.category.CommRing.basic import Mathlib.data.mv_polynomial.default import Mathlib.PostPort universes u u_1 u_2 namespace Mathlib /-! Multivariable polynomials on a type is the left adjoint of the forgetful functor from commutative rings to types. -/ namespace CommRing /-- The free functor `Type u ⥤ CommRing` sending a type `X` to the multivariable (commutative) polynomials with variables `x : X`. -/ def free : Type u ⥤ CommRing := category_theory.functor.mk (fun (α : Type u) => of (mv_polynomial α ℤ)) fun (X Y : Type u) (f : X ⟶ Y) => ↑(mv_polynomial.rename f) @[simp] theorem free_obj_coe {α : Type u} : ↥(category_theory.functor.obj free α) = mv_polynomial α ℤ := rfl @[simp] theorem free_map_coe {α : Type u} {β : Type u} {f : α → β} : ⇑(category_theory.functor.map free f) = ⇑(mv_polynomial.rename f) := rfl /-- The free-forgetful adjunction for commutative rings. -/ def adj : free ⊣ category_theory.forget CommRing := category_theory.adjunction.mk_of_hom_equiv (category_theory.adjunction.core_hom_equiv.mk fun (X : Type (max u_1 u_2)) (R : CommRing) => mv_polynomial.hom_equiv)
37f388411f8e1f1c15e4832ca504c0e1c5c17279
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/bases_auto.lean
399d05c8345b3f6db76017c4b9e43d07f49e52e1
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,388
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 Bases of topologies. Countability axioms. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.continuous_on import Mathlib.PostPort universes u l u_1 u_2 namespace Mathlib namespace topological_space /- countability axioms 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. -/ /-- 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). -/ def is_topological_basis {α : Type u} [t : topological_space α] (s : set (set α)) := (∀ (t₁ : set α) (H : t₁ ∈ s) (t₂ : set α) (H : t₂ ∈ s) (x : α) (H : x ∈ t₁ ∩ t₂), ∃ (t₃ : set α), ∃ (H : t₃ ∈ s), x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧ ⋃₀s = set.univ ∧ t = generate_from s theorem is_topological_basis_of_subbasis {α : Type u} [t : topological_space α] {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((fun (f : set (set α)) => ⋂₀f) '' set_of fun (f : set (set α)) => set.finite f ∧ f ⊆ s ∧ set.nonempty (⋂₀f)) := sorry theorem is_topological_basis_of_open_of_nhds {α : Type u} [t : topological_space α] {s : set (set α)} (h_open : ∀ (u : set α), u ∈ s → is_open u) (h_nhds : ∀ (a : α) (u : set α), a ∈ u → is_open u → ∃ (v : set α), ∃ (H : v ∈ s), a ∈ v ∧ v ⊆ u) : is_topological_basis s := sorry theorem mem_nhds_of_is_topological_basis {α : Type u} [t : topological_space α] {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ nhds a ↔ ∃ (t : set α), ∃ (H : t ∈ b), a ∈ t ∧ t ⊆ s := sorry theorem is_topological_basis.nhds_has_basis {α : Type u} [t : topological_space α] {b : set (set α)} (hb : is_topological_basis b) {a : α} : filter.has_basis (nhds a) (fun (t : set α) => t ∈ b ∧ a ∈ t) fun (t : set α) => t := sorry theorem is_open_of_is_topological_basis {α : Type u} [t : topological_space α] {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : is_open s := sorry theorem mem_basis_subset_of_mem_open {α : Type u} [t : topological_space α] {b : set (set α)} (hb : is_topological_basis b) {a : α} {u : set α} (au : a ∈ u) (ou : is_open u) : ∃ (v : set α), ∃ (H : v ∈ b), a ∈ v ∧ v ⊆ u := iff.mp (mem_nhds_of_is_topological_basis hb) (mem_nhds_sets ou au) theorem sUnion_basis_of_is_open {α : Type u} [t : topological_space α] {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (S : set (set α)), ∃ (H : S ⊆ B), u = ⋃₀S := sorry theorem Union_basis_of_is_open {α : Type u} [t : topological_space α] {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (β : Type u), ∃ (f : β → set α), (u = set.Union fun (i : β) => f i) ∧ ∀ (i : β), f i ∈ B := sorry /-- 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 (α : Type u) [t : topological_space α] where exists_countable_dense : ∃ (s : set α), set.countable s ∧ dense s theorem exists_countable_dense (α : Type u) [t : topological_space α] [separable_space α] : ∃ (s : set α), set.countable s ∧ 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 `α`. -/ theorem exists_dense_seq (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : ∃ (u : ℕ → α), dense_range u := sorry /-- A sequence dense 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 (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : ℕ → α := classical.some (exists_dense_seq α) /-- The sequence `dense_seq α` has dense range. -/ @[simp] theorem dense_range_dense_seq (α : Type u) [t : topological_space α] [separable_space α] [Nonempty α] : dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α) end topological_space /-- 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 theorem dense_range.separable_space {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space.separable_space α] [topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) : topological_space.separable_space β := sorry namespace topological_space /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology (α : Type u) [t : topological_space α] where nhds_generated_countable : ∀ (a : α), filter.is_countably_generated (nhds a) namespace first_countable_topology theorem tendsto_subseq {α : Type u} [t : topological_space α] [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x filter.at_top u) : ∃ (ψ : ℕ → ℕ), strict_mono ψ ∧ filter.tendsto (u ∘ ψ) filter.at_top (nhds x) := filter.is_countably_generated.subseq_tendsto (nhds_generated_countable x) hx end first_countable_topology theorem is_countably_generated_nhds {α : Type u} [t : topological_space α] [first_countable_topology α] (x : α) : filter.is_countably_generated (nhds x) := first_countable_topology.nhds_generated_countable x theorem is_countably_generated_nhds_within {α : Type u} [t : topological_space α] [first_countable_topology α] (x : α) (s : set α) : filter.is_countably_generated (nhds_within x s) := filter.is_countably_generated.inf_principal (is_countably_generated_nhds x) s /-- A second-countable space is one with a countable basis. -/ class second_countable_topology (α : Type u) [t : topological_space α] where is_open_generated_countable : ∃ (b : set (set α)), set.countable b ∧ t = generate_from b protected instance second_countable_topology.to_first_countable_topology (α : Type u) [t : topological_space α] [second_countable_topology α] : first_countable_topology α := sorry theorem second_countable_topology_induced (α : Type u) (β : Type u_1) [t : topological_space β] [second_countable_topology β] (f : α → β) : second_countable_topology α := sorry protected instance subtype.second_countable_topology (α : Type u) [t : topological_space α] (s : set α) [second_countable_topology α] : second_countable_topology ↥s := second_countable_topology_induced (↥s) α coe theorem is_open_generated_countable_inter (α : Type u) [t : topological_space α] [second_countable_topology α] : ∃ (b : set (set α)), set.countable b ∧ ¬∅ ∈ b ∧ is_topological_basis b := sorry /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ protected instance prod.second_countable_topology (α : Type u) [t : topological_space α] {β : Type u_1} [topological_space β] [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := second_countable_topology.mk sorry protected instance second_countable_topology_fintype {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [t : (a : ι) → topological_space (π a)] [sc : ∀ (a : ι), second_countable_topology (π a)] : second_countable_topology ((a : ι) → π a) := (fun (this : ∀ (i : ι), ∃ (b : set (set (π i))), set.countable b ∧ ¬∅ ∈ b ∧ is_topological_basis b) => sorry) fun (a : ι) => is_open_generated_countable_inter (π a) protected instance second_countable_topology.to_separable_space (α : Type u) [t : topological_space α] [second_countable_topology α] : separable_space α := sorry theorem is_open_Union_countable {α : Type u} [t : topological_space α] [second_countable_topology α] {ι : Type u_1} (s : ι → set α) (H : ∀ (i : ι), is_open (s i)) : ∃ (T : set ι), set.countable T ∧ (set.Union fun (i : ι) => set.Union fun (H : i ∈ T) => s i) = set.Union fun (i : ι) => s i := sorry theorem is_open_sUnion_countable {α : Type u} [t : topological_space α] [second_countable_topology α] (S : set (set α)) (H : ∀ (s : set α), s ∈ S → is_open s) : ∃ (T : set (set α)), set.countable T ∧ T ⊆ S ∧ ⋃₀T = ⋃₀S := sorry /-- 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. -/ theorem countable_cover_nhds {α : Type u} [t : topological_space α] [second_countable_topology α] {f : α → set α} (hf : ∀ (x : α), f x ∈ nhds x) : ∃ (s : set α), set.countable s ∧ (set.Union fun (x : α) => set.Union fun (H : x ∈ s) => f x) = set.univ := sorry end Mathlib
ef423a4fe790a9a664cb9e701490af8559a28d3b
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/group_theory/order_of_element.lean
4a33f8f2019e3c14f8d5e84053915b2dc3915e3d
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
24,592
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.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, have hn : order_of a ∣ n, from dvd_of_mod_eq_zero $ by_contradiction (λ ha, by rw pow_eq_mod_order_of at h; exact (not_le_of_gt (nat.mod_lt n (order_of_pos a))) (order_of_le_of_pow_eq_one (nat.pos_of_ne_zero ha) h)), let ⟨m, hm⟩ := dvd_gcd hn order_of_dvd_card_univ in by rw [hm, pow_mul, pow_order_of_eq_one, one_pow], λ 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
ef2e7abbeeb31ac7b4daa6f46288a3f80fa3c751
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/data/pnat/basic.lean
c2149b34bbb31b99825abb3ec35fe817e6fa4de4
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,489
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Neil Strickland -/ import data.nat.prime /-- `ℕ+` is the type of positive natural numbers. It is defined as a subtype, and the VM representation of `ℕ+` is the same as `ℕ` because the proof is not stored. -/ def pnat := {n : ℕ // 0 < n} notation `ℕ+` := pnat instance coe_pnat_nat : has_coe ℕ+ ℕ := ⟨subtype.val⟩ instance : has_repr ℕ+ := ⟨λ n, repr n.1⟩ namespace nat /-- Convert a natural number to a positive natural number. The positivity assumption is inferred by `dec_trivial`. -/ def to_pnat (n : ℕ) (h : 0 < n . tactic.exact_dec_trivial) : ℕ+ := ⟨n, h⟩ /-- Write a successor as an element of `ℕ+`. -/ def succ_pnat (n : ℕ) : ℕ+ := ⟨succ n, succ_pos n⟩ @[simp] theorem succ_pnat_coe (n : ℕ) : (succ_pnat n : ℕ) = succ n := rfl theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m → n = m := λ h, by { let h' := congr_arg (coe : ℕ+ → ℕ) h, exact nat.succ_inj h' } /-- Convert a natural number to a pnat. `n+1` is mapped to itself, and `0` becomes `1`. -/ def to_pnat' (n : ℕ) : ℕ+ := succ_pnat (pred n) @[simp] theorem to_pnat'_coe : ∀ (n : ℕ), ((to_pnat' n) : ℕ) = ite (0 < n) n 1 | 0 := rfl | (m + 1) := by {rw [if_pos (succ_pos m)], refl} namespace primes instance coe_pnat : has_coe nat.primes ℕ+ := ⟨λ p, ⟨(p : ℕ), p.property.pos⟩⟩ theorem coe_pnat_nat (p : nat.primes) : ((p : ℕ+) : ℕ) = p := rfl theorem coe_pnat_inj (p q : nat.primes) : (p : ℕ+) = (q : ℕ+) → p = q := λ h, begin replace h : ((p : ℕ+) : ℕ) = ((q : ℕ+) : ℕ) := congr_arg subtype.val h, rw [coe_pnat_nat, coe_pnat_nat] at h, exact subtype.eq h, end end primes end nat namespace pnat open nat /-- We now define a long list of structures on ℕ+ induced by similar structures on ℕ. Most of these behave in a completely obvious way, but there are a few things to be said about subtraction, division and powers. -/ instance : decidable_eq ℕ+ := λ (a b : ℕ+), by apply_instance instance : decidable_linear_order ℕ+ := subtype.decidable_linear_order _ @[simp] lemma mk_le_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) ≤ ⟨k, hk⟩ ↔ n ≤ k := iff.rfl @[simp] lemma mk_lt_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) < ⟨k, hk⟩ ↔ n < k := iff.rfl @[simp, norm_cast] lemma coe_le_coe (n k : ℕ+) : (n:ℕ) ≤ k ↔ n ≤ k := iff.rfl @[simp, norm_cast] lemma coe_lt_coe (n k : ℕ+) : (n:ℕ) < k ↔ n < k := iff.rfl @[simp] theorem pos (n : ℕ+) : 0 < (n : ℕ) := n.2 theorem eq {m n : ℕ+} : (m : ℕ) = n → m = n := subtype.eq @[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : ℕ+) : ℕ) = n := rfl instance : add_comm_semigroup ℕ+ := { add := λ a b, ⟨(a + b : ℕ), add_pos a.pos b.pos⟩, add_comm := λ a b, subtype.eq (add_comm a b), add_assoc := λ a b c, subtype.eq (add_assoc a b c) } @[simp] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl instance coe_add_hom : is_add_hom (coe : ℕ+ → ℕ) := ⟨add_coe⟩ instance : add_left_cancel_semigroup ℕ+ := { add_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_left_inj (a : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } instance : add_right_cancel_semigroup ℕ+ := { add_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_right_inj (b : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } @[simp] theorem ne_zero (n : ℕ+) : (n : ℕ) ≠ 0 := ne_of_gt n.2 theorem to_pnat'_coe {n : ℕ} : 0 < n → (n.to_pnat' : ℕ) = n := succ_pred_eq_of_pos @[simp] theorem coe_to_pnat' (n : ℕ+) : (n : ℕ).to_pnat' = n := eq (to_pnat'_coe n.pos) instance : comm_monoid ℕ+ := { mul := λ m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩, mul_assoc := λ a b c, subtype.eq (mul_assoc _ _ _), one := succ_pnat 0, one_mul := λ a, subtype.eq (one_mul _), mul_one := λ a, subtype.eq (mul_one _), mul_comm := λ a b, subtype.eq (mul_comm _ _) } theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b := λ a b, nat.lt_add_one_iff theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b := λ a b, nat.add_one_le_iff @[simp] lemma one_le (n : ℕ+) : (1 : ℕ+) ≤ n := n.2 instance : order_bot ℕ+ := { bot := 1, bot_le := λ a, a.property, ..(by apply_instance : partial_order ℕ+) } @[simp] lemma bot_eq_zero : (⊥ : ℕ+) = 1 := rfl instance : inhabited ℕ+ := ⟨1⟩ -- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals. @[simp] lemma mk_one {h} : (⟨1, h⟩ : ℕ+) = (1 : ℕ+) := rfl @[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl @[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl -- Some lemmas that rewrite inequalities between explicit numerals in `pnat` -- into the corresponding inequalities in `nat`. -- TODO: perhaps this should not be attempted by `simp`, -- and instead we should expect `norm_num` to take care of these directly? -- TODO: these lemmas are perhaps incomplete: -- * 1 is not represented as a bit0 or bit1 -- * strict inequalities? @[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.refl _ @[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.refl _ @[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.refl _ @[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.refl _ @[simp] theorem one_coe : ((1 : ℕ+) : ℕ) = 1 := rfl @[simp] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl instance coe_mul_hom : is_monoid_hom (coe : ℕ+ → ℕ) := {map_one := one_coe, map_mul := mul_coe} @[simp] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl @[simp] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl @[simp] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n := by induction n with n ih; [refl, rw [nat.pow_succ, _root_.pow_succ, mul_coe, mul_comm, ih]] instance : left_cancel_semigroup ℕ+ := { mul_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_left_inj a.pos).mp h)}, .. (pnat.comm_monoid) } instance : right_cancel_semigroup ℕ+ := { mul_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_right_inj b.pos).mp h)}, .. (pnat.comm_monoid) } instance : distrib ℕ+ := { left_distrib := λ a b c, eq (mul_add a b c), right_distrib := λ a b c, eq (add_mul a b c), ..(pnat.add_comm_semigroup), ..(pnat.comm_monoid) } /-- Subtraction a - b is defined in the obvious way when a > b, and by a - b = 1 if a ≤ b. -/ instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩ theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 := begin change ((to_pnat' ((a : ℕ) - (b : ℕ)) : ℕ)) = ite ((a : ℕ) > (b : ℕ)) ((a : ℕ) - (b : ℕ)) 1, split_ifs with h, { exact to_pnat'_coe (nat.sub_pos_of_lt h) }, { rw [nat.sub_eq_zero_iff_le.mpr (le_of_not_gt h)], refl } end theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b := λ h, eq $ by { rw [add_coe, sub_coe, if_pos h], exact nat.add_sub_of_le (le_of_lt h) } /-- We define m % k and m / k in the same way as for nat except that when m = n * k we take m % k = k and m / k = n - 1. This ensures that m % k is always positive and m = (m % k) + k * (m / k) in all cases. Later we define a function div_exact which gives the usual m / k in the case where k divides m. -/ def mod_div_aux : ℕ+ → ℕ → ℕ → ℕ+ × ℕ | k 0 q := ⟨k, q.pred⟩ | k (r + 1) q := ⟨⟨r + 1, nat.succ_pos r⟩, q⟩ lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)), (((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q)) | k 0 0 h := (h ⟨rfl, rfl⟩).elim | k 0 (q + 1) h := by { change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1), rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]} | k (r + 1) q h := rfl def mod_div (m k : ℕ+) : ℕ+ × ℕ := mod_div_aux k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) def mod (m k : ℕ+) : ℕ+ := (mod_div m k).1 def div (m k : ℕ+) : ℕ := (mod_div m k).2 theorem mod_add_div (m k : ℕ+) : (m : ℕ) = (mod m k) + k * (div m k) := begin let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ), have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0), by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀, exact (m.ne_zero h₀.symm).elim }, have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this, exact (this.trans h₀).symm, end theorem mod_coe (m k : ℕ+) : ((mod m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) (k : ℕ) ((m : ℕ) % (k : ℕ)) := begin dsimp [mod, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem div_coe (m k : ℕ+) : ((div m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) ((m : ℕ) / (k : ℕ)).pred ((m : ℕ) / (k : ℕ)) := begin dsimp [div, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k := begin change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ), rw [mod_coe], split_ifs, { have hm : (m : ℕ) > 0 := m.pos, rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢, by_cases h' : ((m : ℕ) / (k : ℕ)) = 0, { rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim}, { let h' := nat.mul_le_mul_left (k : ℕ) (nat.succ_le_of_lt (nat.pos_of_ne_zero h')), rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } }, { exact ⟨nat.mod_le (m : ℕ) (k : ℕ), le_of_lt (nat.mod_lt (m : ℕ) k.pos)⟩ } end instance : has_dvd ℕ+ := ⟨λ k m, (k : ℕ) ∣ (m : ℕ)⟩ theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := by {refl} theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k := begin change (k : ℕ) ∣ (m : ℕ) ↔ mod m k = k, rw [nat.dvd_iff_mod_eq_zero], split, { intro h, apply eq, rw [mod_coe, if_pos h] }, { intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0, { exact h'}, { replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h, rw [mod_coe, if_neg h'] at h, exact (ne_of_lt (nat.mod_lt (m : ℕ) k.pos) h).elim } } end def div_exact {m k : ℕ+} (h : k ∣ m) : ℕ+ := ⟨(div m k).succ, nat.succ_pos _⟩ theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact h) = m := begin apply eq, rw [mul_coe], change (k : ℕ) * (div m k).succ = m, rw [mod_add_div m k, dvd_iff'.mp h, nat.mul_succ, add_comm], end theorem dvd_iff'' {k n : ℕ+} : k ∣ n ↔ ∃ m, k * m = n := ⟨λ h, ⟨div_exact h, mul_div_exact h⟩, λ ⟨m, h⟩, dvd.intro (m : ℕ) ((mul_coe k m).symm.trans (congr_arg subtype.val h))⟩ theorem dvd_intro {k n : ℕ+} (m : ℕ+) (h : k * m = n) : k ∣ n := dvd_iff''.mpr ⟨m, h⟩ theorem dvd_refl (m : ℕ+) : m ∣ m := dvd_intro 1 (mul_one m) theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n := λ hmn hnm, subtype.eq (nat.dvd_antisymm hmn hnm) theorem dvd_trans {k m n : ℕ+} : k ∣ m → m ∣ n → k ∣ n := @_root_.dvd_trans ℕ _ (k : ℕ) (m : ℕ) (n : ℕ) theorem one_dvd (n : ℕ+) : 1 ∣ n := dvd_intro n (one_mul n) theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 := ⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩ def gcd (n m : ℕ+) : ℕ+ := ⟨nat.gcd (n : ℕ) (m : ℕ), nat.gcd_pos_of_pos_left (m : ℕ) n.pos⟩ def lcm (n m : ℕ+) : ℕ+ := ⟨nat.lcm (n : ℕ) (m : ℕ), by { let h := mul_pos n.pos m.pos, rw [← gcd_mul_lcm (n : ℕ) (m : ℕ), mul_comm] at h, exact pos_of_dvd_of_pos (dvd.intro (nat.gcd (n : ℕ) (m : ℕ)) rfl) h }⟩ @[simp] theorem gcd_coe (n m : ℕ+) : ((gcd n m) : ℕ) = nat.gcd n m := rfl @[simp] theorem lcm_coe (n m : ℕ+) : ((lcm n m) : ℕ) = nat.lcm n m := rfl theorem gcd_dvd_left (n m : ℕ+) : (gcd n m) ∣ n := nat.gcd_dvd_left (n : ℕ) (m : ℕ) theorem gcd_dvd_right (n m : ℕ+) : (gcd n m) ∣ m := nat.gcd_dvd_right (n : ℕ) (m : ℕ) theorem dvd_gcd {m n k : ℕ+} (hm : k ∣ m) (hn : k ∣ n) : k ∣ gcd m n := @nat.dvd_gcd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem dvd_lcm_left (n m : ℕ+) : n ∣ lcm n m := nat.dvd_lcm_left (n : ℕ) (m : ℕ) theorem dvd_lcm_right (n m : ℕ+) : m ∣ lcm n m := nat.dvd_lcm_right (n : ℕ) (m : ℕ) theorem lcm_dvd {m n k : ℕ+} (hm : m ∣ k) (hn : n ∣ k) : lcm m n ∣ k := @nat.lcm_dvd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem gcd_mul_lcm (n m : ℕ+) : (gcd n m) * (lcm n m) = n * m := subtype.eq (nat.gcd_mul_lcm (n : ℕ) (m : ℕ)) def prime (p : ℕ+) : Prop := (p : ℕ).prime end pnat
ebeb879670ccf36ac00afd5cc4ce32f1658462b1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/limits/colimit_limit.lean
50e24cd03ff4f841e6cbf6f233b504346481e61d
[ "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
4,436
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.types import category_theory.functor.currying import category_theory.limits.functor_category /-! # The morphism comparing a colimit of limits with the corresponding limit of colimits. For `F : J × K ⥤ C` there is always a morphism $\colim_k \lim_j F(j,k) → \lim_j \colim_k F(j, k)$. While it is not usually an isomorphism, with additional hypotheses on `J` and `K` it may be, in which case we say that "colimits commute with limits". The prototypical example, proved in `category_theory.limits.filtered_colimit_commutes_finite_limit`, is that when `C = Type`, filtered colimits commute with finite limits. ## References * Borceux, Handbook of categorical algebra 1, Section 2.13 * [Stacks: Filtered colimits](https://stacks.math.columbia.edu/tag/002W) -/ universes v u open category_theory namespace category_theory.limits variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [category.{v} C] variables (F : J × K ⥤ C) open category_theory.prod lemma map_id_left_eq_curry_map {j : J} {k k' : K} {f : k ⟶ k'} : F.map ((𝟙 j, f) : (j, k) ⟶ (j, k')) = ((curry.obj F).obj j).map f := rfl lemma map_id_right_eq_curry_swap_map {j j' : J} {f : j ⟶ j'} {k : K} : F.map ((f, 𝟙 k) : (j, k) ⟶ (j', k)) = ((curry.obj (swap K J ⋙ F)).obj k).map f := rfl variables [has_limits_of_shape J C] variables [has_colimits_of_shape K C] /-- The universal morphism $\colim_k \lim_j F(j,k) → \lim_j \colim_k F(j, k)$. -/ noncomputable def colimit_limit_to_limit_colimit : colimit ((curry.obj (swap K J ⋙ F)) ⋙ lim) ⟶ limit ((curry.obj F) ⋙ colim) := limit.lift ((curry.obj F) ⋙ colim) { X := _, π := { app := λ j, colimit.desc ((curry.obj (swap K J ⋙ F)) ⋙ lim) { X := _, ι := { app := λ k, limit.π ((curry.obj (swap K J ⋙ F)).obj k) j ≫ colimit.ι ((curry.obj F).obj j) k, naturality' := begin dsimp, intros k k' f, simp only [functor.comp_map, curry_obj_map_app, limits.lim_map_π_assoc, swap_map, category.comp_id, map_id_left_eq_curry_map, colimit.w], end }, }, naturality' := begin dsimp, intros j j' f, ext k, simp only [limits.colimit.ι_map, curry_obj_map_app, limits.colimit.ι_desc_assoc, limits.colimit.ι_desc, category.id_comp, category.assoc, map_id_right_eq_curry_swap_map, limit.w_assoc], end } } /-- Since `colimit_limit_to_limit_colimit` is a morphism from a colimit to a limit, this lemma characterises it. -/ @[simp, reassoc] lemma ι_colimit_limit_to_limit_colimit_π (j) (k) : colimit.ι _ k ≫ colimit_limit_to_limit_colimit F ≫ limit.π _ j = limit.π ((curry.obj (swap K J ⋙ F)).obj k) j ≫ colimit.ι ((curry.obj F).obj j) k := by { dsimp [colimit_limit_to_limit_colimit], simp, } @[simp] lemma ι_colimit_limit_to_limit_colimit_π_apply (F : J × K ⥤ Type v) (j) (k) (f) : limit.π ((curry.obj F) ⋙ colim) j (colimit_limit_to_limit_colimit F (colimit.ι ((curry.obj (swap K J ⋙ F)) ⋙ lim) k f)) = colimit.ι ((curry.obj F).obj j) k (limit.π ((curry.obj (swap K J ⋙ F)).obj k) j f) := by { dsimp [colimit_limit_to_limit_colimit], simp, } /-- The map `colimit_limit_to_limit_colimit` realized as a map of cones. -/ @[simps] noncomputable def colimit_limit_to_limit_colimit_cone (G : J ⥤ K ⥤ C) [has_limit G] : colim.map_cone (limit.cone G) ⟶ limit.cone (G ⋙ colim) := { hom := colim.map (limit_iso_swap_comp_lim G).hom ≫ colimit_limit_to_limit_colimit (uncurry.obj G : _) ≫ lim.map (whisker_right (currying.unit_iso.app G).inv colim), w' := λ j, begin ext1 k, simp only [limit_obj_iso_limit_comp_evaluation_hom_π_assoc, iso.app_inv, ι_colimit_limit_to_limit_colimit_π_assoc, whisker_right_app, colimit.ι_map, functor.map_cone_π_app, category.id_comp, eq_to_hom_refl, eq_to_hom_app, colimit.ι_map_assoc, limit.cone_π, lim_map_π_assoc, lim_map_π, category.assoc, currying_unit_iso_inv_app_app_app, limit_iso_swap_comp_lim_hom_app, lim_map_eq_lim_map], dsimp, simp only [category.id_comp], erw limit_obj_iso_limit_comp_evaluation_hom_π_assoc, end } end category_theory.limits