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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1b55908a5f1e45bb08e95aa9ba03992b980e47ee | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/lua4.lean | bc8660b3c7c10c194d8317f43a52c641598e2c3b | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 294 | lean | import Int.
variable x : Int
(*
-- Add a variable to the environment using Lua
-- The type of the new variable is equal to the type
-- of x
local env = get_environment()
typeofx = env:type_check(Const("x"))
print("type of x is " .. tostring(typeofx))
env:add_var("y", typeofx)
*)
check x + y
|
7b607179d123535f615bdc40ab2a513dd7d9f87e | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebraic_geometry/sheafed_space.lean | 884b4b8dc3d2a1c0cfd183a379558a2fd1e57cbe | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 5,960 | 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 algebraic_geometry.presheafed_space.has_colimits
import topology.sheaves.functors
/-!
# Sheafed spaces
Introduces the category of topological spaces equipped with a sheaf (taking values in an
arbitrary target category `C`.)
We further describe how to apply functors and natural transformations to the values of the
presheaves.
-/
universes v u
open category_theory
open Top
open topological_space
open opposite
open category_theory.limits
open category_theory.category category_theory.functor
variables (C : Type u) [category.{v} C] [limits.has_products C]
local attribute [tidy] tactic.op_induction'
namespace algebraic_geometry
/-- A `SheafedSpace C` is a topological space equipped with a sheaf of `C`s. -/
structure SheafedSpace extends PresheafedSpace C :=
(is_sheaf : presheaf.is_sheaf)
variables {C}
namespace SheafedSpace
instance coe_carrier : has_coe (SheafedSpace C) Top :=
{ coe := λ X, X.carrier }
/-- Extract the `sheaf C (X : Top)` from a `SheafedSpace C`. -/
def sheaf (X : SheafedSpace C) : sheaf C (X : Top.{v}) := ⟨X.presheaf, X.is_sheaf⟩
@[simp] lemma as_coe (X : SheafedSpace C) : X.carrier = (X : Top.{v}) := rfl
@[simp] lemma mk_coe (carrier) (presheaf) (h) :
(({ carrier := carrier, presheaf := presheaf, is_sheaf := h } : SheafedSpace.{v} C) :
Top.{v}) = carrier :=
rfl
instance (X : SheafedSpace.{v} C) : topological_space X := X.carrier.str
/-- The trivial `punit` valued sheaf on any topological space. -/
def punit (X : Top) : SheafedSpace (discrete punit) :=
{ is_sheaf := presheaf.is_sheaf_punit _,
..@PresheafedSpace.const (discrete punit) _ X punit.star }
instance : inhabited (SheafedSpace (discrete _root_.punit)) := ⟨punit (Top.of pempty)⟩
instance : category (SheafedSpace C) :=
show category (induced_category (PresheafedSpace C) SheafedSpace.to_PresheafedSpace),
by apply_instance
/-- Forgetting the sheaf condition is a functor from `SheafedSpace C` to `PresheafedSpace C`. -/
@[derive [full, faithful]]
def forget_to_PresheafedSpace : (SheafedSpace C) ⥤ (PresheafedSpace C) :=
induced_functor _
instance is_PresheafedSpace_iso {X Y : SheafedSpace C} (f : X ⟶ Y) [is_iso f] :
@is_iso (PresheafedSpace C) _ _ _ f :=
SheafedSpace.forget_to_PresheafedSpace.map_is_iso f
variables {C}
section
local attribute [simp] id comp
@[simp] lemma id_base (X : SheafedSpace C) :
((𝟙 X) : X ⟶ X).base = (𝟙 (X : Top.{v})) := rfl
lemma id_c (X : SheafedSpace C) :
((𝟙 X) : X ⟶ X).c = eq_to_hom (presheaf.pushforward.id_eq X.presheaf).symm := rfl
@[simp] lemma id_c_app (X : SheafedSpace C) (U) :
((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { induction U using opposite.rec, cases U, refl }) :=
by { induction U using opposite.rec, cases U, simp only [id_c], dsimp, simp, }
@[simp] lemma comp_base {X Y Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).base = f.base ≫ g.base := rfl
@[simp] lemma comp_c_app {X Y Z : SheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) :
(α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.base)).obj (unop U)))
:= rfl
lemma comp_c_app' {X Y Z : SheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) :
(α ≫ β).c.app (op U) = (β.c).app (op U) ≫ (α.c).app (op ((opens.map (β.base)).obj U))
:= rfl
lemma congr_app {X Y : SheafedSpace C} {α β : X ⟶ Y} (h : α = β) (U) :
α.c.app U = β.c.app U ≫ X.presheaf.map (eq_to_hom (by subst h)) :=
PresheafedSpace.congr_app h U
variables (C)
/-- The forgetful functor from `SheafedSpace` to `Top`. -/
def forget : SheafedSpace C ⥤ Top :=
{ obj := λ X, (X : Top.{v}),
map := λ X Y f, f.base }
end
open Top.presheaf
/--
The restriction of a sheafed space along an open embedding into the space.
-/
def restrict {U : Top} (X : SheafedSpace C)
{f : U ⟶ (X : Top.{v})} (h : open_embedding f) : SheafedSpace C :=
{ is_sheaf := λ ι 𝒰, ⟨is_limit.of_iso_limit
((is_limit.postcompose_inv_equiv _ _).inv_fun (X.is_sheaf _).some)
(sheaf_condition_equalizer_products.fork.iso_of_open_embedding h 𝒰).symm⟩,
..X.to_PresheafedSpace.restrict h }
/--
The restriction of a sheafed space `X` to the top subspace is isomorphic to `X` itself.
-/
def restrict_top_iso (X : SheafedSpace C) :
X.restrict (opens.open_embedding ⊤) ≅ X :=
@preimage_iso _ _ _ _ forget_to_PresheafedSpace _ _
(X.restrict (opens.open_embedding ⊤)) _
X.to_PresheafedSpace.restrict_top_iso
/--
The global sections, notated Gamma.
-/
def Γ : (SheafedSpace C)ᵒᵖ ⥤ C :=
forget_to_PresheafedSpace.op ⋙ PresheafedSpace.Γ
lemma Γ_def : (Γ : _ ⥤ C) = forget_to_PresheafedSpace.op ⋙ PresheafedSpace.Γ := rfl
@[simp] lemma Γ_obj (X : (SheafedSpace C)ᵒᵖ) : Γ.obj X = (unop X).presheaf.obj (op ⊤) := rfl
lemma Γ_obj_op (X : SheafedSpace C) : Γ.obj (op X) = X.presheaf.obj (op ⊤) := rfl
@[simp] lemma Γ_map {X Y : (SheafedSpace C)ᵒᵖ} (f : X ⟶ Y) :
Γ.map f = f.unop.c.app (op ⊤) := rfl
lemma Γ_map_op {X Y : SheafedSpace C} (f : X ⟶ Y) :
Γ.map f.op = f.c.app (op ⊤) := rfl
noncomputable
instance [has_limits C] : creates_colimits (forget_to_PresheafedSpace : SheafedSpace C ⥤ _) :=
⟨λ J hJ, by exactI ⟨λ K, creates_colimit_of_fully_faithful_of_iso
⟨(PresheafedSpace.colimit_cocone (K ⋙ forget_to_PresheafedSpace)).X,
limit_is_sheaf _ (λ j, sheaf.pushforward_sheaf_of_sheaf _ (K.obj (unop j)).2)⟩
(colimit.iso_colimit_cocone ⟨_, PresheafedSpace.colimit_cocone_is_colimit _⟩).symm⟩⟩
instance [has_limits C] : has_colimits (SheafedSpace C) :=
has_colimits_of_has_colimits_creates_colimits forget_to_PresheafedSpace
noncomputable instance [has_limits C] : preserves_colimits (forget C) :=
limits.comp_preserves_colimits forget_to_PresheafedSpace (PresheafedSpace.forget C)
end SheafedSpace
end algebraic_geometry
|
e7dd57509485a562acd2e63791d2cb036e735513 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Group/Z_Module_equivalence.lean | d24307095d6e2f3e7a215661701b262f912a0dbf | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,750 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.category.Module.basic
/-!
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The forgetful functor from ℤ-modules to additive commutative groups is
an equivalence of categories.
TODO:
either use this equivalence to transport the monoidal structure from `Module ℤ` to `Ab`,
or, having constructed that monoidal structure directly, show this functor is monoidal.
-/
open category_theory
open category_theory.equivalence
universes u
namespace Module
/-- The forgetful functor from `ℤ` modules to `AddCommGroup` is full. -/
instance forget₂_AddCommGroup_full : full (forget₂ (Module ℤ) AddCommGroup.{u}) :=
{ preimage := λ A B f,
-- `add_monoid_hom.to_int_linear_map` doesn't work here because `A` and `B` are not definitionally
-- equal to the canonical `add_comm_group.int_module` module instances it expects.
{ to_fun := f,
map_add' := add_monoid_hom.map_add f,
map_smul' := λ n x, by rw [int_smul_eq_zsmul, int_smul_eq_zsmul, map_zsmul,
ring_hom.id_apply] } }
/-- The forgetful functor from `ℤ` modules to `AddCommGroup` is essentially surjective. -/
instance forget₂_AddCommGroup_ess_surj : ess_surj (forget₂ (Module ℤ) AddCommGroup.{u}) :=
{ mem_ess_image := λ A, ⟨Module.of ℤ A, ⟨{ hom := 𝟙 A, inv := 𝟙 A }⟩⟩}
noncomputable instance forget₂_AddCommGroup_is_equivalence :
is_equivalence (forget₂ (Module ℤ) AddCommGroup.{u}) :=
equivalence.of_fully_faithfully_ess_surj (forget₂ (Module ℤ) AddCommGroup)
end Module
|
a382ba25e4a3db9781ebb05d23b3c2ea160b7ec7 | 5a8eb1c11f93715e070b588e85f2961065c3714d | /books/theorem-proving-in-lean/ch02-03-2.lean | 4b766f8afc9ae48f03b0666b44bd151c5e11540c | [
"MIT"
] | permissive | luksamuk/study | 0e19bf99d33e0793127c3d3f8ad3936fbeb36505 | 6a9417e071a8624c4cd9db696c16a3abcc430219 | refs/heads/master | 1,677,960,533,266 | 1,676,234,529,000 | 1,676,234,529,000 | 151,009,060 | 4 | 1 | MIT | 1,676,234,531,000 | 1,538,343,224,000 | C++ | UTF-8 | Lean | false | false | 378 | lean | constants m n : ℕ
constant b : bool
#print "Reducing pairs"
#reduce (m, n).1
#reduce (m, n).2
#print "reducing boolean expressions"
#reduce tt && ff
#reduce ff && b
#reduce b && ff
#print "reducing arithmetic expressions"
#reduce n + 0
#reduce n + 2
#reduce 2 + 3
-- Evaluation example
#eval 12345 * 54321
-- #reduce is more trustworthy but less efficient
-- than #eval.
|
6715eef0e9a8d137a12f76cd7697e2174607bc8d | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/measure/stieltjes.lean | b9658b31a4774d1686b450f5dbfe43d485e0637c | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 16,615 | lean | /-
Copyright (c) 2021 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov, Sébastien Gouëzel
-/
import measure_theory.constructions.borel_space
/-!
# Stieltjes measures on the real line
Consider a function `f : ℝ → ℝ` which is monotone and right-continuous. Then one can define a
corrresponding measure, giving mass `f b - f a` to the interval `(a, b]`.
## Main definitions
* `stieltjes_function` is a structure containing a function from `ℝ → ℝ`, together with the
assertions that it is monotone and right-continuous. To `f : stieltjes_function`, one associates
a Borel measure `f.measure`.
* `f.left_lim x` is the limit of `f` to the left of `x`.
* `f.measure_Ioc` asserts that `f.measure (Ioc a b) = of_real (f b - f a)`
* `f.measure_Ioo` asserts that `f.measure (Ioo a b) = of_real (f.left_lim b - f a)`.
* `f.measure_Icc` and `f.measure_Ico` are analogous.
-/
noncomputable theory
open classical set filter
open ennreal (of_real)
open_locale big_operators ennreal nnreal topological_space
/-! ### Basic properties of Stieltjes functions -/
/-- Bundled monotone right-continuous real functions, used to construct Stieltjes measures. -/
structure stieltjes_function :=
(to_fun : ℝ → ℝ)
(mono' : monotone to_fun)
(right_continuous' : ∀ x, continuous_within_at to_fun (Ici x) x)
namespace stieltjes_function
instance : has_coe_to_fun stieltjes_function (λ _, ℝ → ℝ) := ⟨to_fun⟩
initialize_simps_projections stieltjes_function (to_fun → apply)
variable (f : stieltjes_function)
lemma mono : monotone f := f.mono'
lemma right_continuous (x : ℝ) : continuous_within_at f (Ici x) x := f.right_continuous' x
/-- The limit of a Stieltjes function to the left of `x` (it exists by monotonicity). The fact that
it is indeed a left limit is asserted in `tendsto_left_lim` -/
@[irreducible] def left_lim (x : ℝ) := Sup (f '' (Iio x))
lemma tendsto_left_lim (x : ℝ) : tendsto f (𝓝[<] x) (𝓝 (f.left_lim x)) :=
by { rw left_lim, exact f.mono.tendsto_nhds_within_Iio x }
lemma left_lim_le {x y : ℝ} (h : x ≤ y) : f.left_lim x ≤ f y :=
begin
apply le_of_tendsto (f.tendsto_left_lim x),
filter_upwards [self_mem_nhds_within] with _ hz using (f.mono (le_of_lt hz)).trans (f.mono h),
end
lemma le_left_lim {x y : ℝ} (h : x < y) : f x ≤ f.left_lim y :=
begin
apply ge_of_tendsto (f.tendsto_left_lim y),
apply mem_nhds_within_Iio_iff_exists_Ioo_subset.2 ⟨x, h, _⟩,
assume z hz,
exact f.mono hz.1.le,
end
lemma left_lim_le_left_lim {x y : ℝ} (h : x ≤ y) : f.left_lim x ≤ f.left_lim y :=
begin
rcases eq_or_lt_of_le h with rfl|hxy,
{ exact le_rfl },
{ exact (f.left_lim_le le_rfl).trans (f.le_left_lim hxy) }
end
/-- The identity of `ℝ` as a Stieltjes function, used to construct Lebesgue measure. -/
@[simps] protected def id : stieltjes_function :=
{ to_fun := id,
mono' := λ x y, id,
right_continuous' := λ x, continuous_within_at_id }
@[simp] lemma id_left_lim (x : ℝ) : stieltjes_function.id.left_lim x = x :=
tendsto_nhds_unique (stieltjes_function.id.tendsto_left_lim x) $
(continuous_at_id).tendsto.mono_left nhds_within_le_nhds
instance : inhabited stieltjes_function := ⟨stieltjes_function.id⟩
/-! ### The outer measure associated to a Stieltjes function -/
/-- Length of an interval. This is the largest monotone function which correctly measures all
intervals. -/
def length (s : set ℝ) : ℝ≥0∞ := ⨅a b (h : s ⊆ Ioc a b), of_real (f b - f a)
@[simp] lemma length_empty : f.length ∅ = 0 :=
nonpos_iff_eq_zero.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp
@[simp] lemma length_Ioc (a b : ℝ) :
f.length (Ioc a b) = of_real (f b - f a) :=
begin
refine le_antisymm (infi_le_of_le a $ infi₂_le b subset.rfl)
(le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, ennreal.coe_le_coe.2 _),
cases le_or_lt b a with ab ab,
{ rw real.to_nnreal_of_nonpos (sub_nonpos.2 (f.mono ab)), apply zero_le, },
cases (Ioc_subset_Ioc_iff ab).1 h with h₁ h₂,
exact real.to_nnreal_le_to_nnreal (sub_le_sub (f.mono h₁) (f.mono h₂))
end
lemma length_mono {s₁ s₂ : set ℝ} (h : s₁ ⊆ s₂) : f.length s₁ ≤ f.length s₂ :=
infi_mono $ λ a, binfi_mono $ λ b, h.trans
open measure_theory
/-- The Stieltjes outer measure associated to a Stieltjes function. -/
protected def outer : outer_measure ℝ :=
outer_measure.of_function f.length f.length_empty
lemma outer_le_length (s : set ℝ) : f.outer s ≤ f.length s :=
outer_measure.of_function_le _
/-- If a compact interval `[a, b]` is covered by a union of open interval `(c i, d i)`, then
`f b - f a ≤ ∑ f (d i) - f (c i)`. This is an auxiliary technical statement to prove the same
statement for half-open intervals, the point of the current statement being that one can use
compactness to reduce it to a finite sum, and argue by induction on the size of the covering set. -/
lemma length_subadditive_Icc_Ioo {a b : ℝ} {c d : ℕ → ℝ}
(ss : Icc a b ⊆ ⋃ i, Ioo (c i) (d i)) :
of_real (f b - f a) ≤ ∑' i, of_real (f (d i) - f (c i)) :=
begin
suffices : ∀ (s:finset ℕ) b
(cv : Icc a b ⊆ ⋃ i ∈ (↑s:set ℕ), Ioo (c i) (d i)),
(of_real (f b - f a) : ℝ≥0∞) ≤ ∑ i in s, of_real (f (d i) - f (c i)),
{ rcases is_compact_Icc.elim_finite_subcover_image (λ (i : ℕ) (_ : i ∈ univ),
@is_open_Ioo _ _ _ _ (c i) (d i)) (by simpa using ss) with ⟨s, su, hf, hs⟩,
have e : (⋃ i ∈ (↑hf.to_finset:set ℕ), Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)),
by simp only [ext_iff, exists_prop, finset.set_bUnion_coe, mem_Union, forall_const, iff_self,
finite.mem_to_finset],
rw ennreal.tsum_eq_supr_sum,
refine le_trans _ (le_supr _ hf.to_finset),
exact this hf.to_finset _ (by simpa only [e]) },
clear ss b,
refine λ s, finset.strong_induction_on s (λ s IH b cv, _),
cases le_total b a with ab ab,
{ rw ennreal.of_real_eq_zero.2 (sub_nonpos.2 (f.mono ab)), exact zero_le _, },
have := cv ⟨ab, le_rfl⟩, simp at this,
rcases this with ⟨i, is, cb, bd⟩,
rw [← finset.insert_erase is] at cv ⊢,
rw [finset.coe_insert, bUnion_insert] at cv,
rw [finset.sum_insert (finset.not_mem_erase _ _)],
refine le_trans _ (add_le_add_left (IH _ (finset.erase_ssubset is) (c i) _) _),
{ refine le_trans (ennreal.of_real_le_of_real _) ennreal.of_real_add_le,
rw sub_add_sub_cancel,
exact sub_le_sub_right (f.mono bd.le) _ },
{ rintro x ⟨h₁, h₂⟩,
refine (cv ⟨h₁, le_trans h₂ (le_of_lt cb)⟩).resolve_left
(mt and.left (not_lt_of_le h₂)) }
end
@[simp] lemma outer_Ioc (a b : ℝ) :
f.outer (Ioc a b) = of_real (f b - f a) :=
begin
/- It suffices to show that, if `(a, b]` is covered by sets `s i`, then `f b - f a` is bounded
by `∑ f.length (s i) + ε`. The difficulty is that `f.length` is expressed in terms of half-open
intervals, while we would like to have a compact interval covered by open intervals to use
compactness and finite sums, as provided by `length_subadditive_Icc_Ioo`. The trick is to use the
right-continuity of `f`. If `a'` is close enough to `a` on its right, then `[a', b]` is still
covered by the sets `s i` and moreover `f b - f a'` is very close to `f b - f a` (up to `ε/2`).
Also, by definition one can cover `s i` by a half-closed interval `(p i, q i]` with `f`-length
very close to that of `s i` (within a suitably small `ε' i`, say). If one moves `q i` very
slightly to the right, then the `f`-length will change very little by right continuity, and we
will get an open interval `(p i, q' i)` covering `s i` with `f (q' i) - f (p i)` within `ε' i`
of the `f`-length of `s i`. -/
refine le_antisymm (by { rw ← f.length_Ioc, apply outer_le_length })
(le_infi₂ $ λ s hs, ennreal.le_of_forall_pos_le_add $ λ ε εpos h, _),
let δ := ε / 2,
have δpos : 0 < (δ : ℝ≥0∞), by simpa using εpos.ne',
rcases ennreal.exists_pos_sum_of_encodable δpos.ne' ℕ with ⟨ε', ε'0, hε⟩,
obtain ⟨a', ha', aa'⟩ : ∃ a', f a' - f a < δ ∧ a < a',
{ have A : continuous_within_at (λ r, f r - f a) (Ioi a) a,
{ refine continuous_within_at.sub _ continuous_within_at_const,
exact (f.right_continuous a).mono Ioi_subset_Ici_self },
have B : f a - f a < δ, by rwa [sub_self, nnreal.coe_pos, ← ennreal.coe_pos],
exact (((tendsto_order.1 A).2 _ B).and self_mem_nhds_within).exists },
have : ∀ i, ∃ p:ℝ×ℝ, s i ⊆ Ioo p.1 p.2 ∧
(of_real (f p.2 - f p.1) : ℝ≥0∞) < f.length (s i) + ε' i,
{ intro i,
have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne
(ennreal.coe_ne_zero.2 (ε'0 i).ne')),
conv at this { to_lhs, rw length },
simp only [infi_lt_iff, exists_prop] at this,
rcases this with ⟨p, q', spq, hq'⟩,
have : continuous_within_at (λ r, of_real (f r - f p)) (Ioi q') q',
{ apply ennreal.continuous_of_real.continuous_at.comp_continuous_within_at,
refine continuous_within_at.sub _ continuous_within_at_const,
exact (f.right_continuous q').mono Ioi_subset_Ici_self },
rcases (((tendsto_order.1 this).2 _ hq').and self_mem_nhds_within).exists with ⟨q, hq, q'q⟩,
exact ⟨⟨p, q⟩, spq.trans (Ioc_subset_Ioo_right q'q), hq⟩ },
choose g hg using this,
have I_subset : Icc a' b ⊆ ⋃ i, Ioo (g i).1 (g i).2 := calc
Icc a' b ⊆ Ioc a b : λ x hx, ⟨aa'.trans_le hx.1, hx.2⟩
... ⊆ ⋃ i, s i : hs
... ⊆ ⋃ i, Ioo (g i).1 (g i).2 : Union_mono (λ i, (hg i).1),
calc of_real (f b - f a)
= of_real ((f b - f a') + (f a' - f a)) : by rw sub_add_sub_cancel
... ≤ of_real (f b - f a') + of_real (f a' - f a) : ennreal.of_real_add_le
... ≤ (∑' i, of_real (f (g i).2 - f (g i).1)) + of_real δ :
add_le_add (f.length_subadditive_Icc_Ioo I_subset) (ennreal.of_real_le_of_real ha'.le)
... ≤ (∑' i, (f.length (s i) + ε' i)) + δ :
add_le_add (ennreal.tsum_le_tsum (λ i, (hg i).2.le))
(by simp only [ennreal.of_real_coe_nnreal, le_rfl])
... = (∑' i, f.length (s i)) + (∑' i, ε' i) + δ : by rw [ennreal.tsum_add]
... ≤ (∑' i, f.length (s i)) + δ + δ : add_le_add (add_le_add le_rfl hε.le) le_rfl
... = ∑' (i : ℕ), f.length (s i) + ε : by simp [add_assoc, ennreal.add_halves]
end
lemma measurable_set_Ioi {c : ℝ} :
f.outer.caratheodory.measurable_set' (Ioi c) :=
begin
apply outer_measure.of_function_caratheodory (λ t, _),
refine le_infi (λ a, le_infi (λ b, le_infi (λ h, _))),
refine le_trans (add_le_add
(f.length_mono $ inter_subset_inter_left _ h)
(f.length_mono $ diff_subset_diff_left h)) _,
cases le_total a c with hac hac; cases le_total b c with hbc hbc,
{ simp only [Ioc_inter_Ioi, f.length_Ioc, hac, sup_eq_max, hbc, le_refl, Ioc_eq_empty,
max_eq_right, min_eq_left, Ioc_diff_Ioi, f.length_empty, zero_add, not_lt] },
{ simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right,
sup_eq_max, ←ennreal.of_real_add, f.mono hac, f.mono hbc, sub_nonneg, sub_add_sub_cancel,
le_refl, max_eq_right] },
{ simp only [hbc, le_refl, Ioc_eq_empty, Ioc_inter_Ioi, min_eq_left, Ioc_diff_Ioi,
f.length_empty, zero_add, or_true, le_sup_iff, f.length_Ioc, not_lt] },
{ simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right,
sup_eq_max, le_refl, Ioc_eq_empty, add_zero, max_eq_left, f.length_empty, not_lt] }
end
theorem outer_trim : f.outer.trim = f.outer :=
begin
refine le_antisymm (λ s, _) (outer_measure.le_trim _),
rw outer_measure.trim_eq_infi,
refine le_infi (λ t, le_infi $ λ ht,
ennreal.le_of_forall_pos_le_add $ λ ε ε0 h, _),
rcases ennreal.exists_pos_sum_of_encodable
(ennreal.coe_pos.2 ε0).ne' ℕ with ⟨ε', ε'0, hε⟩,
refine le_trans _ (add_le_add_left (le_of_lt hε) _),
rw ← ennreal.tsum_add,
choose g hg using show
∀ i, ∃ s, t i ⊆ s ∧ measurable_set s ∧
f.outer s ≤ f.length (t i) + of_real (ε' i),
{ intro i,
have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne
(ennreal.coe_pos.2 (ε'0 i)).ne'),
conv at this {to_lhs, rw length},
simp only [infi_lt_iff] at this,
rcases this with ⟨a, b, h₁, h₂⟩,
rw ← f.outer_Ioc at h₂,
exact ⟨_, h₁, measurable_set_Ioc, le_of_lt $ by simpa using h₂⟩ },
simp at hg,
apply infi_le_of_le (Union g) _,
apply infi_le_of_le (ht.trans $ Union_mono (λ i, (hg i).1)) _,
apply infi_le_of_le (measurable_set.Union (λ i, (hg i).2.1)) _,
exact le_trans (f.outer.Union _) (ennreal.tsum_le_tsum $ λ i, (hg i).2.2)
end
lemma borel_le_measurable : borel ℝ ≤ f.outer.caratheodory :=
begin
rw borel_eq_generate_from_Ioi,
refine measurable_space.generate_from_le _,
simp [f.measurable_set_Ioi] { contextual := tt }
end
/-! ### The measure associated to a Stieltjes function -/
/-- The measure associated to a Stieltjes function, giving mass `f b - f a` to the
interval `(a, b]`. -/
@[irreducible] protected def measure : measure ℝ :=
{ to_outer_measure := f.outer,
m_Union := λ s hs, f.outer.Union_eq_of_caratheodory $
λ i, f.borel_le_measurable _ (hs i),
trimmed := f.outer_trim }
@[simp] lemma measure_Ioc (a b : ℝ) : f.measure (Ioc a b) = of_real (f b - f a) :=
by { rw stieltjes_function.measure, exact f.outer_Ioc a b }
@[simp] lemma measure_singleton (a : ℝ) : f.measure {a} = of_real (f a - f.left_lim a) :=
begin
obtain ⟨u, u_mono, u_lt_a, u_lim⟩ : ∃ (u : ℕ → ℝ), strict_mono u ∧ (∀ (n : ℕ), u n < a)
∧ tendsto u at_top (𝓝 a) := exists_seq_strict_mono_tendsto a,
have A : {a} = ⋂ n, Ioc (u n) a,
{ refine subset.antisymm (λ x hx, by simp [mem_singleton_iff.1 hx, u_lt_a]) (λ x hx, _),
simp at hx,
have : a ≤ x := le_of_tendsto' u_lim (λ n, (hx n).1.le),
simp [le_antisymm this (hx 0).2] },
have L1 : tendsto (λ n, f.measure (Ioc (u n) a)) at_top (𝓝 (f.measure {a})),
{ rw A,
refine tendsto_measure_Inter (λ n, measurable_set_Ioc) (λ m n hmn, _) _,
{ exact Ioc_subset_Ioc (u_mono.monotone hmn) le_rfl },
{ exact ⟨0, by simpa only [measure_Ioc] using ennreal.of_real_ne_top⟩ } },
have L2 : tendsto (λ n, f.measure (Ioc (u n) a)) at_top (𝓝 (of_real (f a - f.left_lim a))),
{ simp only [measure_Ioc],
have : tendsto (λ n, f (u n)) at_top (𝓝 (f.left_lim a)),
{ apply (f.tendsto_left_lim a).comp,
exact tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ u_lim
(eventually_of_forall (λ n, u_lt_a n)) },
exact ennreal.continuous_of_real.continuous_at.tendsto.comp (tendsto_const_nhds.sub this) },
exact tendsto_nhds_unique L1 L2
end
@[simp] lemma measure_Icc (a b : ℝ) : f.measure (Icc a b) = of_real (f b - f.left_lim a) :=
begin
rcases le_or_lt a b with hab|hab,
{ have A : disjoint {a} (Ioc a b), by simp,
simp [← Icc_union_Ioc_eq_Icc le_rfl hab, -singleton_union, ← ennreal.of_real_add, f.left_lim_le,
measure_union A measurable_set_Ioc, f.mono hab] },
{ simp only [hab, measure_empty, Icc_eq_empty, not_le],
symmetry,
simp [ennreal.of_real_eq_zero, f.le_left_lim hab] }
end
@[simp] lemma measure_Ioo {a b : ℝ} : f.measure (Ioo a b) = of_real (f.left_lim b - f a) :=
begin
rcases le_or_lt b a with hab|hab,
{ simp only [hab, measure_empty, Ioo_eq_empty, not_lt],
symmetry,
simp [ennreal.of_real_eq_zero, f.left_lim_le hab] },
{ have A : disjoint (Ioo a b) {b}, by simp,
have D : f b - f a = (f b - f.left_lim b) + (f.left_lim b - f a), by abel,
have := f.measure_Ioc a b,
simp only [←Ioo_union_Icc_eq_Ioc hab le_rfl, measure_singleton,
measure_union A (measurable_set_singleton b), Icc_self] at this,
rw [D, ennreal.of_real_add, add_comm] at this,
{ simpa only [ennreal.add_right_inj ennreal.of_real_ne_top] },
{ simp only [f.left_lim_le, sub_nonneg] },
{ simp only [f.le_left_lim hab, sub_nonneg] } },
end
@[simp] lemma measure_Ico (a b : ℝ) : f.measure (Ico a b) = of_real (f.left_lim b - f.left_lim a) :=
begin
rcases le_or_lt b a with hab|hab,
{ simp only [hab, measure_empty, Ico_eq_empty, not_lt],
symmetry,
simp [ennreal.of_real_eq_zero, f.left_lim_le_left_lim hab] },
{ have A : disjoint {a} (Ioo a b) := by simp,
simp [← Icc_union_Ioo_eq_Ico le_rfl hab, -singleton_union, hab.ne, f.left_lim_le,
measure_union A measurable_set_Ioo, f.le_left_lim hab,
← ennreal.of_real_add] }
end
end stieltjes_function
|
1d5d560d18a86ea4ac17338b194a0bcbea2d4b36 | 856e2e1615a12f95b551ed48fa5b03b245abba44 | /src/algebraic_geometry/presheafed_space.lean | bfea68a5df708d8df8dd6d5f44b3c550f7aa03e3 | [
"Apache-2.0"
] | permissive | pimsp/mathlib | 8b77e1ccfab21703ba8fbe65988c7de7765aa0e5 | 913318ca9d6979686996e8d9b5ebf7e74aae1c63 | refs/heads/master | 1,669,812,465,182 | 1,597,133,610,000 | 1,597,133,610,000 | 281,890,685 | 1 | 0 | null | 1,595,491,577,000 | 1,595,491,576,000 | null | UTF-8 | Lean | false | false | 8,434 | 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.sheaves.presheaf
/-!
# Presheafed spaces
Introduces the category of topological spaces equipped with a presheaf (taking values in an
arbitrary target category `C`.)
We further describe how to apply functors and natural transformations to the values of the
presheaves.
-/
universes v u
open category_theory
open Top
open topological_space
open opposite
open category_theory.category category_theory.functor
variables (C : Type u) [𝒞 : category.{v} C]
include 𝒞
local attribute [tidy] tactic.op_induction'
namespace algebraic_geometry
/-- A `PresheafedSpace C` is a topological space equipped with a presheaf of `C`s. -/
structure PresheafedSpace :=
(to_Top : Top)
(𝒪 : to_Top.presheaf C)
variables {C}
namespace PresheafedSpace
instance coe_to_Top : has_coe (PresheafedSpace C) Top :=
{ coe := λ X, X.to_Top }
@[simp] lemma as_coe (X : PresheafedSpace C) : X.to_Top = (X : Top.{v}) := rfl
@[simp] lemma mk_coe (to_Top) (𝒪) : (({ to_Top := to_Top, 𝒪 := 𝒪 } :
PresheafedSpace.{v} C) : Top.{v}) = to_Top := rfl
instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str
/-- A morphism between presheafed spaces `X` and `Y` consists of a continuous map
`f` between the underlying topological spaces, and a (notice contravariant!) map
from the presheaf on `Y` to the pushforward of the presheaf on `X` via `f`. -/
structure hom (X Y : PresheafedSpace C) :=
(f : (X : Top.{v}) ⟶ (Y : Top.{v}))
(c : Y.𝒪 ⟶ f _* X.𝒪)
@[ext] lemma ext {X Y : PresheafedSpace C} (α β : hom X Y)
(w : α.f = β.f) (h : α.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.𝒪) = β.c) :
α = β :=
begin
cases α, cases β,
dsimp [presheaf.pushforward] at *,
tidy, -- TODO including `injections` would make tidy work earlier.
end
.
def id (X : PresheafedSpace C) : hom X X :=
{ f := 𝟙 (X : Top.{v}),
c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id (X.to_Top)).hom) _) }
def comp (X Y Z : PresheafedSpace C) (α : hom X Y) (β : hom Y Z) : hom X Z :=
{ f := α.f ≫ β.f,
c := β.c ≫ (whisker_left (opens.map β.f).op α.c) ≫ (Top.presheaf.pushforward.comp _ _ _).inv }
variables (C)
section
local attribute [simp] id comp presheaf.pushforward
/- The proofs below can be done by `tidy`, but it is too slow,
and we don't have a tactic caching mechanism. -/
/-- The category of PresheafedSpaces. Morphisms are pairs, a continuous map and a presheaf map
from the presheaf on the target to the pushforward of the presheaf on the source. -/
instance category_of_PresheafedSpaces : category (PresheafedSpace C) :=
{ hom := hom,
id := id,
comp := comp,
id_comp' := λ X Y f,
begin
ext1, swap,
{ dsimp, simp only [id_comp] },
{ ext U, op_induction, cases U,
dsimp,
simp only [comp_id, id_comp, map_id, presheaf.pushforward, presheaf.pushforward.comp_inv_app],
dsimp,
simp only [comp_id], },
end,
comp_id' := λ X Y f,
begin
ext1, swap,
{ dsimp, simp only [comp_id] },
{ ext U, op_induction, cases U,
dsimp,
simp only [comp_id, id_comp, map_id, presheaf.pushforward, presheaf.pushforward.comp_inv_app],
dsimp,
simp only [comp_id], }
end,
assoc' := λ W X Y Z f g h,
begin
ext1, swap,
refl,
{ ext U, op_induction, cases U,
dsimp,
simp only [assoc, map_id, comp_id, presheaf.pushforward, presheaf.pushforward.comp_inv_app],
dsimp,
simp only [comp_id, id_comp], }
end }
end
variables {C}
instance {X Y : PresheafedSpace C} : has_coe (X ⟶ Y) ((X : Top.{v}) ⟶ (Y : Top.{v})) :=
{ coe := λ α, α.f }
-- see Note [function coercion]
instance {X Y : PresheafedSpace C} : has_coe_to_fun (X ⟶ Y) :=
⟨λ _, (X : Top.{v}) → (Y : Top.{v}), λ h, h⟩
@[simp] lemma hom_mk_coe {X Y : PresheafedSpace C} (f) (c) :
(({ f := f, c := c } : X ⟶ Y) : (X : Top.{v}) ⟶ (Y : Top.{v})) = f := rfl
@[simp] lemma f_as_coe {X Y : PresheafedSpace C} (α : X ⟶ Y) :
α.f = (α : (X : Top.{v}) ⟶ (Y : Top.{v})) := rfl
@[simp] lemma id_coe (X : PresheafedSpace C) :
(((𝟙 X) : X ⟶ X) : (X : Top.{v}) ⟶ X) = 𝟙 (X : Top.{v}) := rfl
@[simp] lemma id_coe_fn (X : PresheafedSpace C) :
(((𝟙 X) : X ⟶ X) : (X : Top.{v}) → X) = 𝟙 (X : Top.{v}) := rfl
@[simp] lemma comp_coe {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) :
((α ≫ β : X ⟶ Z) : (X : Top.{v}) ⟶ Z) = (α : (X : Top.{v}) ⟶ Y) ≫ (β : Y ⟶ Z) := rfl
lemma id_c (X : PresheafedSpace C) :
((𝟙 X) : X ⟶ X).c =
(((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id (X.to_Top)).hom) _)) := rfl
-- Implementation note: this harmless looking lemma causes deterministic timeouts,
-- but happily we can survive without it.
-- lemma comp_c {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) :
-- (α ≫ β).c = (β.c ≫ (whisker_left (opens.map β.f).op α.c)) := rfl
@[simp] lemma id_c_app (X : PresheafedSpace C) (U) :
((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { op_induction U, cases U, refl }) :=
by { op_induction U, cases U, simp only [id_c], dsimp, simp, }
@[simp] lemma comp_c_app {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) :
(α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.f)).obj (unop U))) ≫
(Top.presheaf.pushforward.comp _ _ _).inv.app U := rfl
/-- The forgetful functor from `PresheafedSpace` to `Top`. -/
def forget : PresheafedSpace C ⥤ Top :=
{ obj := λ X, (X : Top.{v}),
map := λ X Y f, f }
end PresheafedSpace
end algebraic_geometry
open algebraic_geometry algebraic_geometry.PresheafedSpace
variables {C}
namespace category_theory
variables {D : Type u} [category.{v} D]
local attribute [simp] presheaf.pushforward
namespace functor
/-- We can apply a functor `F : C ⥤ D` to the values of the presheaf in any `PresheafedSpace C`,
giving a functor `PresheafedSpace C ⥤ PresheafedSpace D` -/
/- The proofs below can be done by `tidy`, but it is too slow,
and we don't have a tactic caching mechanism. -/
def map_presheaf (F : C ⥤ D) : PresheafedSpace C ⥤ PresheafedSpace D :=
{ obj := λ X, { to_Top := X.to_Top, 𝒪 := X.𝒪 ⋙ F },
map := λ X Y f, { f := f.f, c := whisker_right f.c F },
map_id' := λ X,
begin
ext1, swap,
{ refl },
{ ext,
dsimp,
simp only [presheaf.pushforward, eq_to_hom_map, map_id, comp_id, id_c_app],
refl }
end,
map_comp' := λ X Y Z f g,
begin
ext1, swap,
{ refl, },
{ ext, dsimp,
simp only [comp_id, assoc, map_comp, map_id, comp_c_app,
presheaf.pushforward, presheaf.pushforward.comp_inv_app],
dsimp,
simp only [comp_id, map_id] }
end }
@[simp] lemma map_presheaf_obj_X (F : C ⥤ D) (X : PresheafedSpace C) :
((F.map_presheaf.obj X) : Top.{v}) = (X : Top.{v}) := rfl
@[simp] lemma map_presheaf_obj_𝒪 (F : C ⥤ D) (X : PresheafedSpace C) :
(F.map_presheaf.obj X).𝒪 = X.𝒪 ⋙ F := rfl
@[simp] lemma map_presheaf_map_f (F : C ⥤ D) {X Y : PresheafedSpace C} (f : X ⟶ Y) :
((F.map_presheaf.map f) : (F.map_presheaf.obj X : Top.{v}) ⟶ (F.map_presheaf.obj Y : Top.{v})) =
(f : (X : Top.{v}) ⟶ (Y : Top.{v})) := rfl
@[simp] lemma map_presheaf_map_c (F : C ⥤ D) {X Y : PresheafedSpace C} (f : X ⟶ Y) :
(F.map_presheaf.map f).c = whisker_right f.c F := rfl
end functor
namespace nat_trans
/- The proofs below can be done by `tidy`, but it is too slow,
and we don't have a tactic caching mechanism. -/
def on_presheaf {F G : C ⥤ D} (α : F ⟶ G) : G.map_presheaf ⟶ F.map_presheaf :=
{ app := λ X,
{ f := 𝟙 _,
c := whisker_left X.𝒪 α ≫ ((functor.left_unitor _).inv) ≫
(whisker_right (nat_trans.op (opens.map_id X.to_Top).hom) _) },
naturality' := λ X Y f,
begin
ext1, swap,
{ refl },
{ ext U,
op_induction,
cases U,
dsimp,
simp only [comp_id, assoc, map_id, presheaf.pushforward, presheaf.pushforward.comp_inv_app],
dsimp,
simp only [comp_id, nat_trans.naturality], }
end }
-- TODO Assemble the last two constructions into a functor
-- `(C ⥤ D) ⥤ (PresheafedSpace C ⥤ PresheafedSpace D)`
end nat_trans
end category_theory
|
f5e7f158a7cbf1e5eceaf26a585827ea80f65ecf | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/ppNotationCode.lean | d15d20501d5279a6351e2dc6a9368ac37b9dca34 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 67 | lean | set_option trace.Elab.definition.body true
infix "+++" => Nat.add
|
a0fefb6792c8b98aa328acdb8b7ef9d44574d8c9 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/pfunctor/multivariate/W.lean | 1acbb174491bc2d8e0091c177aa1fde5f738d1e8 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 10,499 | lean | /-
Copyright (c) 2018 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad, Simon Hudon
-/
import data.pfunctor.multivariate.basic
/-!
# The W construction as a multivariate polynomial functor.
W types are well-founded tree-like structures. They are defined
as the least fixpoint of a polynomial functor.
## Main definitions
* `W_mk` - constructor
* `W_dest - destructor
* `W_rec` - recursor: basis for defining functions by structural recursion on `P.W α`
* `W_rec_eq` - defining equation for `W_rec`
* `W_ind` - induction principle for `P.W α`
## Implementation notes
Three views of M-types:
* `Wp`: polynomial functor
* `W`: data type inductively defined by a triple:
shape of the root, data in the root and children of the root
* `W`: least fixed point of a polynomial functor
Specifically, we define the polynomial functor `Wp` as:
* A := a tree-like structure without information in the nodes
* B := given the tree-like structure `t`, `B t` is a valid path
(specified inductively by `W_path`) from the root of `t` to any given node.
As a result `Wp.obj α` is made of a dataless tree and a function from
its valid paths to values of `α`
## Reference
* [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019]
-/
universes u v
namespace mvpfunctor
open typevec
open_locale mvfunctor
variables {n : ℕ} (P : mvpfunctor.{u} (n+1))
/-- A path from the root of a tree to one of its node -/
inductive W_path : P.last.W → fin2 n → Type u
| root (a : P.A) (f : P.last.B a → P.last.W) (i : fin2 n) (c : P.drop.B a i) :
W_path ⟨a, f⟩ i
| child (a : P.A) (f : P.last.B a → P.last.W) (i : fin2 n) (j : P.last.B a) (c : W_path (f j) i) :
W_path ⟨a, f⟩ i
instance W_path.inhabited (x : P.last.W) {i} [I : inhabited (P.drop.B x.head i)] :
inhabited (W_path P x i) :=
⟨ match x, I with
| ⟨a, f ⟩, I := W_path.root a f i (@default _ I)
end ⟩
/-- Specialized destructor on `W_path` -/
def W_path_cases_on {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) :
P.W_path ⟨a, f⟩ ⟹ α :=
begin
intros i x, cases x,
case W_path.root : _ _ i c { exact g' i c },
case W_path.child : _ _ i j c { exact g j i c}
end
/-- Specialized destructor on `W_path` -/
def W_path_dest_left {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(h : P.W_path ⟨a, f⟩ ⟹ α) :
P.drop.B a ⟹ α :=
λ i c, h i (W_path.root a f i c)
/-- Specialized destructor on `W_path` -/
def W_path_dest_right {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(h : P.W_path ⟨a, f⟩ ⟹ α) :
Π j : P.last.B a, P.W_path (f j) ⟹ α :=
λ j i c, h i (W_path.child a f i j c)
theorem W_path_dest_left_W_path_cases_on
{α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) :
P.W_path_dest_left (P.W_path_cases_on g' g) = g' := rfl
theorem W_path_dest_right_W_path_cases_on
{α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) :
P.W_path_dest_right (P.W_path_cases_on g' g) = g := rfl
theorem W_path_cases_on_eta {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W}
(h : P.W_path ⟨a, f⟩ ⟹ α) :
P.W_path_cases_on (P.W_path_dest_left h) (P.W_path_dest_right h) = h :=
by ext i x; cases x; reflexivity
theorem comp_W_path_cases_on {α β : typevec n} (h : α ⟹ β) {a : P.A} {f : P.last.B a → P.last.W}
(g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) :
h ⊚ P.W_path_cases_on g' g = P.W_path_cases_on (h ⊚ g') (λ i, h ⊚ g i) :=
by ext i x; cases x; reflexivity
/-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded
tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so
that `Wp.obj α` is made of a tree and a function from its valid paths to
the values it contains -/
def Wp : mvpfunctor n :=
{ A := P.last.W, B := P.W_path }
/-- W-type of `P` -/
@[nolint has_inhabited_instance]
def W (α : typevec n) : Type* := P.Wp.obj α
instance mvfunctor_W : mvfunctor P.W := by delta mvpfunctor.W; apply_instance
/-!
First, describe operations on `W` as a polynomial functor.
-/
/-- Constructor for `Wp` -/
def Wp_mk {α : typevec n} (a : P.A) (f : P.last.B a → P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ α) :
P.W α :=
⟨⟨a, f⟩, f'⟩
/-- Recursor for `Wp` -/
def Wp_rec {α : typevec n} {C : Type*}
(g : Π (a : P.A) (f : P.last.B a → P.last.W),
(P.W_path ⟨a, f⟩ ⟹ α) → (P.last.B a → C) → C) :
Π (x : P.last.W) (f' : P.W_path x ⟹ α), C
| ⟨a, f⟩ f' := g a f f' (λ i, Wp_rec (f i) (P.W_path_dest_right f' i))
theorem Wp_rec_eq {α : typevec n} {C : Type*}
(g : Π (a : P.A) (f : P.last.B a → P.last.W),
(P.W_path ⟨a, f⟩ ⟹ α) → (P.last.B a → C) → C)
(a : P.A) (f : P.last.B a → P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ α) :
P.Wp_rec g ⟨a, f⟩ f' = g a f f' (λ i, P.Wp_rec g (f i) (P.W_path_dest_right f' i)) :=
rfl
-- Note: we could replace Prop by Type* and obtain a dependent recursor
theorem Wp_ind {α : typevec n} {C : Π x : P.last.W, P.W_path x ⟹ α → Prop}
(ih : ∀ (a : P.A) (f : P.last.B a → P.last.W)
(f' : P.W_path ⟨a, f⟩ ⟹ α),
(∀ i : P.last.B a, C (f i) (P.W_path_dest_right f' i)) → C ⟨a, f⟩ f') :
Π (x : P.last.W) (f' : P.W_path x ⟹ α), C x f'
| ⟨a, f⟩ f' := ih a f f' (λ i, Wp_ind _ _)
/-!
Now think of W as defined inductively by the data ⟨a, f', f⟩ where
- `a : P.A` is the shape of the top node
- `f' : P.drop.B a ⟹ α` is the contents of the top node
- `f : P.last.B a → P.last.W` are the subtrees
-/
/-- Constructor for `W` -/
def W_mk {α : typevec n} (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) :
P.W α :=
let g : P.last.B a → P.last.W := λ i, (f i).fst,
g' : P.W_path ⟨a, g⟩ ⟹ α := P.W_path_cases_on f' (λ i, (f i).snd) in
⟨⟨a, g⟩, g'⟩
/-- Recursor for `W` -/
def W_rec {α : typevec n} {C : Type*}
(g : Π a : P.A, ((P.drop).B a ⟹ α) → ((P.last).B a → P.W α) → ((P.last).B a → C) → C) :
P.W α → C
| ⟨a, f'⟩ :=
let g' (a : P.A) (f : P.last.B a → P.last.W) (h : P.W_path ⟨a, f⟩ ⟹ α)
(h' : P.last.B a → C) : C :=
g a (P.W_path_dest_left h) (λ i, ⟨f i, P.W_path_dest_right h i⟩) h' in
P.Wp_rec g' a f'
/-- Defining equation for the recursor of `W` -/
theorem W_rec_eq {α : typevec n} {C : Type*}
(g : Π a : P.A, ((P.drop).B a ⟹ α) → ((P.last).B a → P.W α) → ((P.last).B a → C) → C)
(a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) :
P.W_rec g (P.W_mk a f' f) = g a f' f (λ i, P.W_rec g (f i)) :=
begin
rw [W_mk, W_rec], dsimp, rw [Wp_rec_eq],
dsimp only [W_path_dest_left_W_path_cases_on, W_path_dest_right_W_path_cases_on],
congr; ext1 i; cases (f i); refl
end
/-- Induction principle for `W` -/
theorem W_ind {α : typevec n} {C : P.W α → Prop}
(ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α),
(∀ i, C (f i)) → C (P.W_mk a f' f)) :
∀ x, C x :=
begin
intro x, cases x with a f,
apply @Wp_ind n P α (λ a f, C ⟨a, f⟩), dsimp,
intros a f f' ih',
dsimp [W_mk] at ih,
let ih'' := ih a (P.W_path_dest_left f') (λ i, ⟨f i, P.W_path_dest_right f' i⟩),
dsimp at ih'', rw W_path_cases_on_eta at ih'',
apply ih'',
apply ih'
end
theorem W_cases {α : typevec n} {C : P.W α → Prop}
(ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α), C (P.W_mk a f' f)) :
∀ x, C x :=
P.W_ind (λ a f' f ih', ih a f' f)
/-- W-types are functorial -/
def W_map {α β : typevec n} (g : α ⟹ β) : P.W α → P.W β :=
λ x, g <$$> x
theorem W_mk_eq {α : typevec n} (a : P.A) (f : P.last.B a → P.last.W)
(g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) :
P.W_mk a g' (λ i, ⟨f i, g i⟩) =
⟨⟨a, f⟩, P.W_path_cases_on g' g⟩ := rfl
theorem W_map_W_mk {α β : typevec n} (g : α ⟹ β)
(a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) :
g <$$> P.W_mk a f' f = P.W_mk a (g ⊚ f') (λ i, g <$$> f i) :=
begin
show _ = P.W_mk a (g ⊚ f') (mvfunctor.map g ∘ f),
have : mvfunctor.map g ∘ f = λ i, ⟨(f i).fst, g ⊚ ((f i).snd)⟩,
{ ext i : 1, dsimp [function.comp], cases (f i), refl },
rw this,
have : f = λ i, ⟨(f i).fst, (f i).snd⟩,
{ ext1, cases (f x), refl },
rw this, dsimp,
rw [W_mk_eq, W_mk_eq],
have h := mvpfunctor.map_eq P.Wp g,
rw [h, comp_W_path_cases_on]
end
-- TODO: this technical theorem is used in one place in constructing the initial algebra.
-- Can it be avoided?
/-- Constructor of a value of `P.obj (α ::: β)` from components.
Useful to avoid complicated type annotation -/
@[reducible] def obj_append1 {α : typevec n} {β : Type*}
(a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) :
P.obj (α ::: β) :=
⟨a, split_fun f' f⟩
theorem map_obj_append1 {α γ : typevec n} (g : α ⟹ γ)
(a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) :
append_fun g (P.W_map g) <$$> P.obj_append1 a f' f =
P.obj_append1 a (g ⊚ f') (λ x, P.W_map g (f x)) :=
by rw [obj_append1, obj_append1, map_eq, append_fun, ← split_fun_comp]; refl
/-!
Yet another view of the W type: as a fixed point for a multivariate polynomial functor.
These are needed to use the W-construction to construct a fixed point of a qpf, since
the qpf axioms are expressed in terms of `map` on `P`.
-/
/-- Constructor for the W-type of `P` -/
def W_mk' {α : typevec n} : P.obj (α ::: P.W α) → P.W α
| ⟨a, f⟩ := P.W_mk a (drop_fun f) (last_fun f)
/-- Destructor for the W-type of `P` -/
def W_dest' {α : typevec.{u} n} : P.W α → P.obj (α.append1 (P.W α)) :=
P.W_rec (λ a f' f _, ⟨a, split_fun f' f⟩)
theorem W_dest'_W_mk {α : typevec n}
(a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) :
P.W_dest' (P.W_mk a f' f) = ⟨a, split_fun f' f⟩ :=
by rw [W_dest', W_rec_eq]
theorem W_dest'_W_mk' {α : typevec n} (x : P.obj (α.append1 (P.W α))) :
P.W_dest' (P.W_mk' x) = x :=
by cases x with a f; rw [W_mk', W_dest'_W_mk, split_drop_fun_last_fun]
end mvpfunctor
|
783c6117b74ceab72b923169a71414b8519bc88b | c9b68131de1dfe4e7f0ea5749b11e67a774bc839 | /src/constraints.lean | 9e78fc611c820b84d23a377e5c3dddac4ba70bb8 | [] | no_license | congge666/formal-proofs | 2013f158f310abcfc07c156bb2a5113fb78f7831 | b5f6964d0220c8f89668357f2c08e44861128fe3 | refs/heads/master | 1,691,374,567,671 | 1,632,704,604,000 | 1,632,706,366,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,743 | lean | /-
The constraints specifying the trace of a Cairo execution.
-/
import algebra.field data.nat.basic data.fin tactic.norm_num
import cpu -- for names of flags
open_locale big_operators
structure input_data_aux (F : Type*) :=
(T : nat)
(pc_I : F)
(pc_F : F)
(ap_I : F)
(ap_F : F)
(mem_star : F → option F)
(rc_min : nat)
(rc_max : nat)
/- constraints -/
(h_rc_lt : rc_max < 2^16)
(h_rc_le : rc_min ≤ rc_max)
/- functions for accessing `mem_star` -/
/-- the domain of the partial memory specification -/
def mem_dom {F : Type*} (mem_star : F → option F) :=
{ x // option.is_some (mem_star x) }
/-- the value of the memory -/
def mem_val {F : Type*} {mem_star : F → option F} (a : mem_dom mem_star) : F :=
option.get a.property
instance {F : Type*} [fintype F] (mem_star : F → option F) : fintype (mem_dom mem_star) :=
by {rw mem_dom, apply_instance}
/- auxiliary functions for talking about flags extracted from f_tilde -/
/-- a sequence of trace cells for storing a bitvector of flags -/
def tilde_type (F : Type*) := fin 16 → F
namespace tilde_type
variables {F : Type*} [field F] (f_tilde : tilde_type F)
def to_f := λ i : fin 15, f_tilde i.cast_succ - 2 * f_tilde i.succ
def f_dst_reg := f_tilde.to_f DST_REG
def f_op0_reg := f_tilde.to_f OP0_REG
def f_op1_imm := f_tilde.to_f OP1_IMM
def f_op1_fp := f_tilde.to_f OP1_FP
def f_op1_ap := f_tilde.to_f OP1_AP
def f_res_add := f_tilde.to_f RES_ADD
def f_res_mul := f_tilde.to_f RES_MUL
def f_pc_jump_abs := f_tilde.to_f PC_JUMP_ABS
def f_pc_jump_rel := f_tilde.to_f PC_JUMP_REL
def f_pc_jnz := f_tilde.to_f PC_JNZ
def f_ap_add := f_tilde.to_f AP_ADD
def f_ap_add1 := f_tilde.to_f AP_ADD1
def f_opcode_call := f_tilde.to_f OPCODE_CALL
def f_opcode_ret := f_tilde.to_f OPCODE_RET
def f_opcode_assert_eq := f_tilde.to_f OPCODE_ASSERT_EQ
def instruction_size := f_tilde.f_op1_imm + 1
end tilde_type
/-
The constraints on how information is stored in memory.
For some reason, elaboration was too slow (and timed out) before I split this into smaller
structures.
-/
structure memory_embedding_constraints {F : Type*} [field F] [fintype F]
(T : nat)
(pc : fin T → F)
(inst : fin T → F)
(dst_addr : fin T → F)
(dst : fin T → F)
(op0_addr : fin T → F)
(op0 : fin T → F)
(op1_addr : fin T → F)
(op1 : fin T → F)
(mem_star : F → option F)
(n : nat)
(a : fin (n + 1) → F)
(v : fin (n + 1) → F) :=
/- embedding of data -/
(embed_inst : fin T → fin (n + 1))
(embed_dst : fin T → fin (n + 1))
(embed_op0 : fin T → fin (n + 1))
(embed_op1 : fin T → fin (n + 1))
(embed_mem : mem_dom mem_star → fin (n + 1))
(h_embed_pc : ∀ i, a (embed_inst i) = pc i)
(h_embed_inst : ∀ i, v (embed_inst i) = inst i)
(h_embed_dst_addr : ∀ i, a (embed_dst i) = dst_addr i)
(h_embed_dst : ∀ i, v (embed_dst i) = dst i)
(h_embed_op0_addr : ∀ i, a (embed_op0 i) = op0_addr i)
(h_embed_op0 : ∀ i, v (embed_op0 i) = op0 i)
(h_embed_op1_addr : ∀ i, a (embed_op1 i) = op1_addr i)
(h_embed_op1 : ∀ i, v (embed_op1 i) = op1 i)
(h_embed_dom : ∀ i : mem_dom mem_star, a (embed_mem i) = 0)
(h_embed_val : ∀ i : mem_dom mem_star, v (embed_mem i) = 0)
(h_embed_mem_inj : function.injective embed_mem)
(h_embed_mem_disj_inst : ∀ i j, embed_mem i ≠ embed_inst j)
(h_embed_mem_disj_dst : ∀ i j, embed_mem i ≠ embed_dst j)
(h_embed_mem_disj_op0 : ∀ i j, embed_mem i ≠ embed_op0 j)
(h_embed_mem_disj_op1 : ∀ i j, embed_mem i ≠ embed_op1 j)
structure memory_block_constraints {F : Type*} [field F] [fintype F]
(n : nat)
(a : fin (n + 1) → F)
(v : fin (n + 1) → F)
(mem_star : F → option F) :=
(a' : fin (n + 1) → F)
(v' : fin (n + 1) → F)
(p : fin (n + 1) → F)
(alpha : F)
(z : F)
(h_continuity : ∀ i : fin n, (a' i.succ - a' i.cast_succ) * (a' i.succ - a' i.cast_succ - 1) = 0)
(h_single_valued : ∀ i : fin n, (v' i.succ - v' i.cast_succ) * (a' i.succ - a' i.cast_succ - 1) = 0)
(h_initial : (z - (a' 0 + alpha * v' 0)) * p 0 = z - (a 0 + alpha * v 0))
(h_cumulative : ∀ i : fin n, (z - (a' i.succ + alpha * v' i.succ)) * p i.succ =
(z - (a i.succ + alpha * v i.succ)) * p i.cast_succ)
(h_final : p (fin.last n) * ∏ a : mem_dom mem_star, (z - (a.val + alpha * mem_val a)) =
z^(fintype.card (mem_dom mem_star)))
structure memory_constraints {F : Type*} [field F] [fintype F]
(T : nat)
(pc : fin T → F)
(inst : fin T → F)
(dst_addr : fin T → F)
(dst : fin T → F)
(op0_addr : fin T → F)
(op0 : fin T → F)
(op1_addr : fin T → F)
(op1 : fin T → F)
(mem_star : F → option F) :=
(n : nat)
(a : fin (n + 1) → F)
(v : fin (n + 1) → F)
(em : memory_embedding_constraints T pc inst dst_addr dst op0_addr op0 op1_addr op1 mem_star
n a v)
(mb : memory_block_constraints n a v mem_star)
(h_n_lt : n < ring_char F)
/-
Range check constraints.
-/
structure range_check_constraints {F : Type*} [field F]
(T : nat)
(off_op0_tilde : fin T → F)
(off_op1_tilde : fin T → F)
(off_dst_tilde : fin T → F)
(rc_min : nat)
(rc_max : nat) :=
(n : nat)
(a : fin (n + 1) → F)
(a' : fin (n + 1) → F)
(p : fin (n + 1) → F)
(z : F)
/- embedding of `op0`, `op1`, and `dst` data in `a` -/
(embed_off_op0 : fin T → fin (n + 1))
(embed_off_op1 : fin T → fin (n + 1))
(embed_off_dst : fin T → fin (n + 1))
(h_embed_op0 : ∀ i, a (embed_off_op0 i) = off_op0_tilde i)
(h_embed_op1 : ∀ i, a (embed_off_op1 i) = off_op1_tilde i)
(h_embed_dst : ∀ i, a (embed_off_dst i) = off_dst_tilde i)
/- constraints -/
(h_continuity : ∀ i : fin n, (a' i.succ - a' i.cast_succ) * (a' i.succ - a' i.cast_succ - 1) = 0)
(h_initial : (z - a' 0) * p 0 = z - a 0)
(h_cumulative : ∀ i : fin n, (z - a' i.succ) * p i.succ = (z - a i.succ) * p i.cast_succ)
(h_final : p (fin.last n) = 1)
(h_rc_min : a' 0 = rc_min)
(h_rc_max : a' (fin.last n) = rc_max)
(h_n_lt : n < ring_char F)
/-
Constraints for each instruction.
-/
structure instruction_constraints {F : Type*} [field F]
(inst : F)
(off_op0_tilde : F)
(off_op1_tilde : F)
(off_dst_tilde : F)
(f_tilde : tilde_type F) :=
(h_instruction : inst = off_dst_tilde + 2^16 * off_op0_tilde + 2^32 * off_op1_tilde +
2^48 * f_tilde 0)
(h_bit : ∀ i : fin 15, f_tilde.to_f i * (f_tilde.to_f i - 1) = 0)
(h_last_value : f_tilde ⟨15, by norm_num⟩ = 0)
/-
Constraints relating each state to the next one.
-/
structure step_constraints {F : Type*} [field F]
(off_op0_tilde : F)
(off_op1_tilde : F)
(off_dst_tilde : F)
(f_tilde : tilde_type F)
(fp : F)
(ap : F)
(pc : F)
(next_fp : F)
(next_ap : F)
(next_pc : F)
(dst_addr : F)
(op0_addr : F)
(op1_addr : F)
(dst : F)
(op0 : F)
(op1 : F) :=
(mul : F)
(res : F)
(t0 : F)
(t1 : F)
(h_dst_addr : dst_addr = f_tilde.f_dst_reg * fp + (1 - f_tilde.f_dst_reg) * ap +
(off_dst_tilde - 2^15))
(h_op0_addr : op0_addr = f_tilde.f_op0_reg * fp + (1 - f_tilde.f_op0_reg) * ap +
(off_op0_tilde - 2^15))
(h_op1_addr : op1_addr = f_tilde.f_op1_imm * pc + f_tilde.f_op1_ap * ap +
f_tilde.f_op1_fp * fp +
(1 - f_tilde.f_op1_imm - f_tilde.f_op1_ap - f_tilde.f_op1_fp) * op0 +
(off_op1_tilde - 2^15))
(h_mul : mul = op0 * op1)
(h_res : (1 - f_tilde.f_pc_jnz) * res =
f_tilde.f_res_add * (op0 + op1) + f_tilde.f_res_mul * mul +
(1 - f_tilde.f_res_add - f_tilde.f_res_mul - f_tilde.f_pc_jnz) * op1)
(h_t0_eq : t0 = f_tilde.f_pc_jnz * dst)
(h_t1_eq : t1 = t0 * res)
(h_next_pc_eq : (t1 - f_tilde.f_pc_jnz) * (next_pc - (pc + (f_tilde.f_op1_imm + 1))) = 0)
(h_next_pc_eq' : t0 * (next_pc - (pc + op1)) + (1 - f_tilde.f_pc_jnz) * next_pc -
((1 - f_tilde.f_pc_jump_abs - f_tilde.f_pc_jump_rel - f_tilde.f_pc_jnz) *
(pc + (f_tilde.f_op1_imm + 1)) +
f_tilde.f_pc_jump_abs * res + f_tilde.f_pc_jump_rel * (pc + res))
= 0)
(h_opcode_call : f_tilde.f_opcode_call * (dst - fp) = 0)
(h_opcode_call' : f_tilde.f_opcode_call * (op0 - (pc + (f_tilde.f_op1_imm + 1))) = 0)
(h_opcode_assert_eq : f_tilde.f_opcode_assert_eq * (dst - res) = 0)
(h_next_ap : next_ap = ap + f_tilde.f_ap_add * res + f_tilde.f_ap_add1 +
f_tilde.f_opcode_call * 2)
(h_next_fp : next_fp = f_tilde.f_opcode_ret * dst + f_tilde.f_opcode_call * (ap + 2) +
(1 - f_tilde.f_opcode_ret - f_tilde.f_opcode_call) * fp)
/-
All the trace data and constraints (except for the probabilistic assumptions, and assumption
`char F > 2^16`)
-/
structure constraints {F : Type*} [field F] [fintype F] (inp : input_data_aux F) :=
/- the execution trace -/
(fp : fin (inp.T + 1) → F)
(ap : fin (inp.T + 1) → F)
(pc : fin (inp.T + 1) → F)
/- the sequence of instructions -/
(inst : fin inp.T → F)
(off_op0_tilde : fin inp.T → F)
(off_op1_tilde : fin inp.T → F)
(off_dst_tilde : fin inp.T → F)
(f_tilde : fin inp.T → tilde_type F)
/- the memory accesses-/
(dst_addr : fin inp.T → F)
(dst : fin inp.T → F)
(op0_addr : fin inp.T → F)
(op0 : fin inp.T → F)
(op1_addr : fin inp.T → F)
(op1 : fin inp.T → F)
/- starting and ending constraints -/
(h_pc_I : pc 0 = inp.pc_I)
(h_ap_I : ap 0 = inp.ap_I)
(h_fp_I : fp 0 = inp.ap_I)
(h_pc_F : pc (fin.last inp.T) = inp.pc_F)
(h_ap_F : ap (fin.last inp.T) = inp.ap_F)
/- the main constraints -/
(mc : memory_constraints inp.T (λ i : fin inp.T, pc (i.cast_succ)) inst
dst_addr dst op0_addr op0 op1_addr op1 inp.mem_star)
(rc : range_check_constraints inp.T off_op0_tilde off_op1_tilde off_dst_tilde
inp.rc_min inp.rc_max)
(ic : ∀ i : fin inp.T, instruction_constraints
(inst i)
(off_op0_tilde i)
(off_op1_tilde i)
(off_dst_tilde i)
(f_tilde i))
(sc : ∀ i : fin inp.T, step_constraints
(off_op0_tilde i)
(off_op1_tilde i)
(off_dst_tilde i)
(f_tilde i)
(fp i.cast_succ)
(ap i.cast_succ)
(pc i.cast_succ)
(fp i.succ)
(ap i.succ)
(pc i.succ)
(dst_addr i)
(op0_addr i)
(op1_addr i)
(dst i)
(op0 i)
(op1 i))
|
c1596fc5007223e0c0f896200c12f598eec8965b | b3fced0f3ff82d577384fe81653e47df68bb2fa1 | /src/measure_theory/l1_space.lean | 87a267b293bfdf3fc9fb08d7b4df4bf89fdb6469 | [
"Apache-2.0"
] | permissive | ratmice/mathlib | 93b251ef5df08b6fd55074650ff47fdcc41a4c75 | 3a948a6a4cd5968d60e15ed914b1ad2f4423af8d | refs/heads/master | 1,599,240,104,318 | 1,572,981,183,000 | 1,572,981,183,000 | 219,830,178 | 0 | 0 | Apache-2.0 | 1,572,980,897,000 | 1,572,980,896,000 | null | UTF-8 | Lean | false | false | 6,535 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
Integrable functions and L¹ space
-/
import measure_theory.ae_eq_fun
noncomputable theory
open_locale classical
set_option class.instance_max_depth 100
namespace measure_theory
open set lattice filter topological_space ennreal emetric
universes u v
variables {α : Type u} {β : Type v} [measure_space α]
variables {γ : Type*} [normed_group γ]
infixr ` →ₘ `:25 := ae_eq_fun
def integrable (f : α → γ) : Prop := (∫⁻ a, nnnorm (f a)) < ⊤
@[simp] lemma lintegral_nnnorm_zero : (∫⁻ a : α, nnnorm (0 : γ)) = 0 := by simp
lemma integrable_zero : integrable (0 : α → γ) :=
by { have := coe_lt_top, simpa [integrable] }
lemma lintegral_nnnorm_add {f g : α → γ} (hf : measurable f) (hg : measurable g) :
(∫⁻ a, nnnorm (f a) + nnnorm (g a)) = (∫⁻ a, nnnorm (f a)) + ∫⁻ a, nnnorm (g a) :=
lintegral_add (measurable_coe_nnnorm hf) (measurable_coe_nnnorm hg)
lemma integrable_add {f g : α → γ} (hfm : measurable f) (hgm : measurable g) :
integrable f → integrable g → integrable (f + g) :=
assume hfi hgi,
calc
(∫⁻ (a : α), ↑(nnnorm ((f + g) a))) ≤ ∫⁻ (a : α), ↑(nnnorm (f a)) + ↑(nnnorm (g a)) :
lintegral_le_lintegral _ _
(assume a, by { simp only [coe_add.symm, coe_le_coe], exact nnnorm_triangle _ _ })
... = _ :
lintegral_nnnorm_add hfm hgm
... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩
-- We don't need `f` to be measurable here, but it's easier to have a uniform API
@[nolint]
lemma lintegral_nnnorm_neg {f : α → γ} (hf : measurable f) :
(∫⁻ (a : α), ↑(nnnorm ((-f) a))) = ∫⁻ (a : α), ↑(nnnorm ((f) a)) :=
lintegral_congr_ae $ by { filter_upwards [], simp }
lemma integrable_neg {f : α → γ} (hfm : measurable f) : integrable f → integrable (-f) :=
assume hfi, calc _ = _ : lintegral_nnnorm_neg hfm
... < ⊤ : hfi
section normed_space
variables {K : Type*} [normed_field K] [normed_space K γ]
lemma integrable_smul {c : K} {f : α → γ} (hfm : measurable f) : integrable f → integrable (c • f) :=
begin
simp only [integrable], assume hfi,
calc
(∫⁻ (a : α), nnnorm ((c • f) a)) = (∫⁻ (a : α), (nnnorm c) * nnnorm (f a)) : by
{ apply lintegral_congr_ae, filter_upwards [], assume a, simp [nnnorm_smul] }
... < ⊤ :
begin
rw lintegral_const_mul, apply mul_lt_top,
{ simp }, { exact hfi }, { exact measurable_coe_nnnorm hfm }
end
end
end normed_space
variables [second_countable_topology γ]
namespace ae_eq_fun
def integrable (f : α →ₘ γ) : Prop := f ∈ ball (0 : α →ₘ γ) ⊤
lemma integrable_mk (f : α → γ) (hf : measurable f) :
(integrable (mk f hf)) ↔ measure_theory.integrable f :=
by simp [integrable, zero_def, edist_mk_mk', measure_theory.integrable, nndist_eq_nnnorm]
local attribute [simp] integrable_mk
lemma integrable_zero : integrable (0 : α →ₘ γ) := mem_ball_self coe_lt_top
lemma integrable_add : ∀ {f g : α →ₘ γ}, integrable f → integrable g → integrable (f + g) :=
begin
rintros ⟨f, hf⟩ ⟨g, hg⟩,
have := measure_theory.integrable_add hf hg,
simpa [mem_ball, zero_def]
end
lemma integrable_neg : ∀ {f : α →ₘ γ}, integrable f → integrable (-f) :=
by { rintros ⟨f, hf⟩, have := measure_theory.integrable_neg hf, simpa }
instance : is_add_subgroup (ball (0 : α →ₘ γ) ⊤) :=
{ zero_mem := integrable_zero,
add_mem := λ _ _, integrable_add,
neg_mem := λ _, integrable_neg }
section normed_space
variables {K : Type*} [normed_field K] [second_countable_topology K] [normed_space K γ]
lemma integrable_smul : ∀ {c : K} {f : α →ₘ γ}, integrable f → integrable (c • f) :=
by { assume c, rintros ⟨f, hf⟩, have := integrable_smul hf, simpa }
end normed_space
end ae_eq_fun
section
variables (α γ)
def l1 : Type* := subtype (@ae_eq_fun.integrable α _ γ _ _)
local notation `L¹` := l1
infixr ` →₁ `:25 := l1
end
namespace l1
open ae_eq_fun
section normed_group
def mk (f : α → γ) : measurable f → integrable f → (α →₁ γ) :=
assume hfm hfi, ⟨mk f hfm, by { rw integrable_mk, assumption }⟩
protected lemma ext_iff {f g : α →₁ γ} : f = g ↔ f.1 = g.1 := ⟨congr rfl , subtype.eq⟩
instance : emetric_space (α →₁ γ) := subtype.emetric_space
instance : metric_space (α →₁ γ) := metric_space_emetric_ball 0 ⊤
lemma dist_def (f g : α →₁ γ) : dist f g = ennreal.to_real (edist f.1 g.1) := rfl
instance : add_comm_group (α →₁ γ) := subtype.add_comm_group
lemma zero_def : (0 : α →₁ γ) = ⟨(0 : α →ₘ γ), ae_eq_fun.integrable_zero⟩ := rfl
lemma add_def (f g : α →₁ γ) : f + g = ⟨f.1 + g.1, ae_eq_fun.integrable_add f.2 g.2⟩ := rfl
instance : has_norm (α →₁ γ) := ⟨λ f, dist f 0⟩
lemma norm_def (f : α →₁ γ) : (norm f) = ennreal.to_real (edist f.1 0) := rfl
instance : normed_group (α →₁ γ) := normed_group.of_add_dist (λ x, rfl) $ by
{ rintros ⟨f, _⟩ ⟨g, _⟩ ⟨h, _⟩, simp only [dist_def, add_def], rw [edist_eq_add_add] }
end normed_group
section normed_space
variables {K : Type*} [normed_field K] [second_countable_topology K] [normed_space K γ]
protected def smul : K → (α →₁ γ) → (α →₁ γ) := λ x f, ⟨x • f.1, ae_eq_fun.integrable_smul f.2⟩
instance : has_scalar K (α →₁ γ) := ⟨l1.smul⟩
lemma smul_def {x : K} {f : α →₁ γ} : x • f = ⟨x • f.1, ae_eq_fun.integrable_smul f.2⟩ := rfl
local attribute [simp] smul_def norm_def add_def zero_def dist_def ext_iff
instance : semimodule K (α →₁ γ) :=
{ one_smul := by { rintros ⟨f, hf⟩, simp [ae_eq_fun.semimodule.one_smul] },
mul_smul := by { rintros x y ⟨f, hf⟩, simp [ae_eq_fun.semimodule.mul_smul] },
smul_add := by { rintros x ⟨f, hf⟩ ⟨g, hg⟩, simp [smul_add] },
smul_zero := by { assume x, simp [smul_zero x] },
add_smul := by { rintros x y ⟨f, hf⟩, simp [add_smul x y f] },
zero_smul := by { rintro ⟨f, hf⟩, simp [zero_smul K f] } }
instance : vector_space K (α →₁ γ) := { .. l1.semimodule }
instance : normed_space K (α →₁ γ) :=
⟨ begin
rintros x ⟨f, hf⟩,
show ennreal.to_real (edist (x • f) 0) = ∥x∥ * ennreal.to_real (edist f 0),
rw [edist_smul, to_real_of_real_mul], exact norm_nonneg _
end ⟩
end normed_space
end l1
end measure_theory
|
b1beed47d2d25b5d639f93c9cbed2c7f122e2b62 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/topology/uniform_space/basic.lean | 609d2ef36735760d1ddf69cb2c776f11416bc79f | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 69,139 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import order.filter.lift
import topology.separation
/-!
# Uniform spaces
Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly
generalize to uniform spaces, e.g.
* uniform continuity (in this file)
* completeness (in `cauchy.lean`)
* extension of uniform continuous functions to complete spaces (in `uniform_embedding.lean`)
* totally bounded sets (in `cauchy.lean`)
* totally bounded complete sets are compact (in `cauchy.lean`)
A uniform structure on a type `X` is a filter `𝓤 X` on `X × X` satisfying some conditions
which makes it reasonable to say that `∀ᶠ (p : X × X) in 𝓤 X, ...` means
"for all p.1 and p.2 in X close enough, ...". Elements of this filter are called entourages
of `X`. The two main examples are:
* If `X` is a metric space, `V ∈ 𝓤 X ↔ ∃ ε > 0, { p | dist p.1 p.2 < ε } ⊆ V`
* If `G` is an additive topological group, `V ∈ 𝓤 G ↔ ∃ U ∈ 𝓝 (0 : G), {p | p.2 - p.1 ∈ U} ⊆ V`
Those examples are generalizations in two different directions of the elementary example where
`X = ℝ` and `V ∈ 𝓤 ℝ ↔ ∃ ε > 0, { p | |p.2 - p.1| < ε } ⊆ V` which features both the topological
group structure on `ℝ` and its metric space structure.
Each uniform structure on `X` induces a topology on `X` characterized by
> `nhds_eq_comap_uniformity : ∀ {x : X}, 𝓝 x = comap (prod.mk x) (𝓤 X)`
where `prod.mk x : X → X × X := (λ y, (x, y))` is the partial evaluation of the product
constructor.
The dictionary with metric spaces includes:
* an upper bound for `dist x y` translates into `(x, y) ∈ V` for some `V ∈ 𝓤 X`
* a ball `ball x r` roughly corresponds to `uniform_space.ball x V := {y | (x, y) ∈ V}`
for some `V ∈ 𝓤 X`, but the later is more general (it includes in
particular both open and closed balls for suitable `V`).
In particular we have:
`is_open_iff_ball_subset {s : set X} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 X, ball x V ⊆ s`
The triangle inequality is abstracted to a statement involving the composition of relations in `X`.
First note that the triangle inequality in a metric space is equivalent to
`∀ (x y z : X) (r r' : ℝ), dist x y ≤ r → dist y z ≤ r' → dist x z ≤ r + r'`.
Then, for any `V` and `W` with type `set (X × X)`, the composition `V ○ W : set (X × X)` is
defined as `{ p : X × X | ∃ z, (p.1, z) ∈ V ∧ (z, p.2) ∈ W }`.
In the metric space case, if `V = { p | dist p.1 p.2 ≤ r }` and `W = { p | dist p.1 p.2 ≤ r' }`
then the triangle inequality, as reformulated above, says `V ○ W` is contained in
`{p | dist p.1 p.2 ≤ r + r'}` which is the entourage associated to the radius `r + r'`.
In general we have `mem_ball_comp (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W)`.
Note that this discussion does not depend on any axiom imposed on the uniformity filter,
it is simply captured by the definition of composition.
The uniform space axioms ask the filter `𝓤 X` to satisfy the following:
* every `V ∈ 𝓤 X` contains the diagonal `id_rel = { p | p.1 = p.2 }`. This abstracts the fact
that `dist x x ≤ r` for every non-negative radius `r` in the metric space case and also that
`x - x` belongs to every neighborhood of zero in the topological group case.
* `V ∈ 𝓤 X → prod.swap '' V ∈ 𝓤 X`. This is tightly related the fact that `dist x y = dist y x`
in a metric space, and to continuity of negation in the topological group case.
* `∀ V ∈ 𝓤 X, ∃ W ∈ 𝓤 X, W ○ W ⊆ V`. In the metric space case, it corresponds
to cutting the radius of a ball in half and applying the triangle inequality.
In the topological group case, it comes from continuity of addition at `(0, 0)`.
These three axioms are stated more abstractly in the definition below, in terms of
operations on filters, without directly manipulating entourages.
## Main definitions
* `uniform_space X` is a uniform space structure on a type `X`
* `uniform_continuous f` is a predicate saying a function `f : α → β` between uniform spaces
is uniformly continuous : `∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r`
In this file we also define a complete lattice structure on the type `uniform_space X`
of uniform structures on `X`, as well as the pullback (`uniform_space.comap`) of uniform structures
coming from the pullback of filters.
Like distance functions, uniform structures cannot be pushed forward in general.
## Notations
Localized in `uniformity`, we have the notation `𝓤 X` for the uniformity on a uniform space `X`,
and `○` for composition of relations, seen as terms with type `set (X × X)`.
## Implementation notes
There is already a theory of relations in `data/rel.lean` where the main definition is
`def rel (α β : Type*) := α → β → Prop`.
The relations used in the current file involve only one type, but this is not the reason why
we don't reuse `data/rel.lean`. We use `set (α × α)`
instead of `rel α α` because we really need sets to use the filter library, and elements
of filters on `α × α` have type `set (α × α)`.
The structure `uniform_space X` bundles a uniform structure on `X`, a topology on `X` and
an assumption saying those are compatible. This may not seem mathematically reasonable at first,
but is in fact an instance of the forgetful inheritance pattern. See Note [forgetful inheritance]
below.
## References
The formalization uses the books:
* [N. Bourbaki, *General Topology*][bourbaki1966]
* [I. M. James, *Topologies and Uniformities*][james1999]
But it makes a more systematic use of the filter library.
-/
open set filter classical
open_locale classical topological_space filter
set_option eqn_compiler.zeta true
universes u
/-!
### Relations, seen as `set (α × α)`
-/
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*}
/-- The identity relation, or the graph of the identity function -/
def id_rel {α : Type*} := {p : α × α | p.1 = p.2}
@[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl
@[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s :=
by simp [subset_def]; exact forall_congr (λ a, by simp)
/-- The composition of relations -/
def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂}
localized "infix ` ○ `:55 := comp_rel" in uniformity
@[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)}
{x y : α} : (x, y) ∈ r₁ ○ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl
@[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α :=
set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm
theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)}
(hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ○ (g x)) :=
assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩
@[mono]
lemma comp_rel_mono {f g h k: set (α×α)} (h₁ : f ⊆ h) (h₂ : g ⊆ k) : f ○ g ⊆ h ○ k :=
λ ⟨x, y⟩ ⟨z, h, h'⟩, ⟨z, h₁ h, h₂ h'⟩
lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) :
(a, b) ∈ s ○ t :=
⟨c, h₁, h₂⟩
@[simp] lemma id_comp_rel {r : set (α×α)} : id_rel ○ r = r :=
set.ext $ assume ⟨a, b⟩, by simp
lemma comp_rel_assoc {r s t : set (α×α)} :
(r ○ s) ○ t = r ○ (s ○ t) :=
by ext p; cases p; simp only [mem_comp_rel]; tauto
lemma subset_comp_self {α : Type*} {s : set (α × α)} (h : id_rel ⊆ s) : s ⊆ s ○ s :=
λ ⟨x, y⟩ xy_in, ⟨x, h (by rw mem_id_rel), xy_in⟩
/-- The relation is invariant under swapping factors. -/
def symmetric_rel (V : set (α × α)) : Prop := prod.swap ⁻¹' V = V
/-- The maximal symmetric relation contained in a given relation. -/
def symmetrize_rel (V : set (α × α)) : set (α × α) := V ∩ prod.swap ⁻¹' V
lemma symmetric_symmetrize_rel (V : set (α × α)) : symmetric_rel (symmetrize_rel V) :=
by simp [symmetric_rel, symmetrize_rel, preimage_inter, inter_comm, ← preimage_comp]
lemma symmetrize_rel_subset_self (V : set (α × α)) : symmetrize_rel V ⊆ V :=
sep_subset _ _
@[mono]
lemma symmetrize_mono {V W: set (α × α)} (h : V ⊆ W) : symmetrize_rel V ⊆ symmetrize_rel W :=
inter_subset_inter h $ preimage_mono h
lemma symmetric_rel_inter {U V : set (α × α)} (hU : symmetric_rel U) (hV : symmetric_rel V) :
symmetric_rel (U ∩ V) :=
begin
unfold symmetric_rel at *,
rw [preimage_inter, hU, hV],
end
/-- This core description of a uniform space is outside of the type class hierarchy. It is useful
for constructions of uniform spaces, when the topology is derived from the uniform space. -/
structure uniform_space.core (α : Type u) :=
(uniformity : filter (α × α))
(refl : 𝓟 id_rel ≤ uniformity)
(symm : tendsto prod.swap uniformity uniformity)
(comp : uniformity.lift' (λs, s ○ s) ≤ uniformity)
/-- An alternative constructor for `uniform_space.core`. This version unfolds various
`filter`-related definitions. -/
def uniform_space.core.mk' {α : Type u} (U : filter (α × α))
(refl : ∀ (r ∈ U) x, (x, x) ∈ r)
(symm : ∀ r ∈ U, prod.swap ⁻¹' r ∈ U)
(comp : ∀ r ∈ U, ∃ t ∈ U, t ○ t ⊆ r) : uniform_space.core α :=
⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm,
begin
intros r ru,
rw [mem_lift'_sets],
exact comp _ ru,
apply monotone_comp_rel; exact monotone_id,
end⟩
/-- A uniform space generates a topological space -/
def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) :
topological_space α :=
{ is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity,
is_open_univ := by simp; intro; exact univ_mem_sets,
is_open_inter :=
assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt},
is_open_sUnion :=
assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ }
lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂
| ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*]
section prio
/-- Suppose that one can put two mathematical structures on a type, a rich one `R` and a poor one
`P`, and that one can deduce the poor structure from the rich structure through a map `F` (called a
forgetful functor) (think `R = metric_space` and `P = topological_space`). A possible
implementation would be to have a type class `rich` containing a field `R`, a type class `poor`
containing a field `P`, and an instance from `rich` to `poor`. However, this creates diamond
problems, and a better approach is to let `rich` extend `poor` and have a field saying that
`F R = P`.
To illustrate this, consider the pair `metric_space` / `topological_space`. Consider the topology
on a product of two metric spaces. With the first approach, it could be obtained by going first from
each metric space to its topology, and then taking the product topology. But it could also be
obtained by considering the product metric space (with its sup distance) and then the topology
coming from this distance. These would be the same topology, but not definitionally, which means
that from the point of view of Lean's kernel, there would be two different `topological_space`
instances on the product. This is not compatible with the way instances are designed and used:
there should be at most one instance of a kind on each type. This approach has created an instance
diamond that does not commute definitionally.
The second approach solves this issue. Now, a metric space contains both a distance, a topology, and
a proof that the topology coincides with the one coming from the distance. When one defines the
product of two metric spaces, one uses the sup distance and the product topology, and one has to
give the proof that the sup distance induces the product topology. Following both sides of the
instance diamond then gives rise (definitionally) to the product topology on the product space.
Another approach would be to have the rich type class take the poor type class as an instance
parameter. It would solve the diamond problem, but it would lead to a blow up of the number
of type classes one would need to declare to work with complicated classes, say a real inner
product space, and would create exponential complexity when working with products of
such complicated spaces, that are avoided by bundling things carefully as above.
Note that this description of this specific case of the product of metric spaces is oversimplified
compared to mathlib, as there is an intermediate typeclass between `metric_space` and
`topological_space` called `uniform_space`. The above scheme is used at both levels, embedding a
topology in the uniform space structure, and a uniform structure in the metric space structure.
Note also that, when `P` is a proposition, there is no such issue as any two proofs of `P` are
definitionally equivalent in Lean.
To avoid boilerplate, there are some designs that can automatically fill the poor fields when
creating a rich structure if one doesn't want to do something special about them. For instance,
in the definition of metric spaces, default tactics fill the uniform space fields if they are
not given explicitly. One can also have a helper function creating the rich structure from a
structure with less fields, where the helper function fills the remaining fields. See for instance
`uniform_space.of_core` or `real_inner_product.of_core`.
For more details on this question, called the forgetful inheritance pattern, see [Competing
inheritance paths in dependent type theory: a case study in functional
analysis](https://hal.inria.fr/hal-02463336).
-/
library_note "forgetful inheritance"
set_option default_priority 100 -- see Note [default priority]
/-- A uniform space is a generalization of the "uniform" topological aspects of a
metric space. It consists of a filter on `α × α` called the "uniformity", which
satisfies properties analogous to the reflexivity, symmetry, and triangle properties
of a metric.
A metric space has a natural uniformity, and a uniform space has a natural topology.
A topological group also has a natural uniformity, even when it is not metrizable. -/
class uniform_space (α : Type u) extends topological_space α, uniform_space.core α :=
(is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity))
end prio
/-- Alternative constructor for `uniform_space α` when a topology is already given. -/
@[pattern] def uniform_space.mk' {α} (t : topological_space α)
(c : uniform_space.core α)
(is_open_uniformity : ∀s:set α, t.is_open s ↔
(∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) :
uniform_space α := ⟨c, is_open_uniformity⟩
/-- Construct a `uniform_space` from a `uniform_space.core`. -/
def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α :=
{ to_core := u,
to_topological_space := u.to_topological_space,
is_open_uniformity := assume a, iff.rfl }
/-- Construct a `uniform_space` from a `u : uniform_space.core` and a `topological_space` structure
that is equal to `u.to_topological_space`. -/
def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α)
(h : t = u.to_topological_space) : uniform_space α :=
{ to_core := u,
to_topological_space := t,
is_open_uniformity := assume a, h.symm ▸ iff.rfl }
lemma uniform_space.to_core_to_topological_space (u : uniform_space α) :
u.to_core.to_topological_space = u.to_topological_space :=
topological_space_eq $ funext $ assume s,
by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity]
@[ext]
lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂
| (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h :=
have u₁ = u₂, from uniform_space.core_eq h,
have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this],
by simp [*]
lemma uniform_space.of_core_eq_to_core
(u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) :
uniform_space.of_core_eq u.to_core t h = u :=
uniform_space_eq rfl
section uniform_space
variables [uniform_space α]
/-- The uniformity is a filter on α × α (inferred from an ambient uniform space
structure on α). -/
def uniformity (α : Type u) [uniform_space α] : filter (α × α) :=
(@uniform_space.to_core α _).uniformity
localized "notation `𝓤` := uniformity" in uniformity
lemma is_open_uniformity {s : set α} :
is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) :=
uniform_space.is_open_uniformity s
lemma refl_le_uniformity : 𝓟 id_rel ≤ 𝓤 α :=
(@uniform_space.to_core α _).refl
lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) :
(x, x) ∈ s :=
refl_le_uniformity h rfl
lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) :=
(@uniform_space.to_core α _).symm
lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), s ○ s) ≤ 𝓤 α :=
(@uniform_space.to_core α _).comp
lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) :=
symm_le_uniformity
lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, t ○ t ⊆ s :=
have s ∈ (𝓤 α).lift' (λt:set (α×α), t ○ t),
from comp_le_uniformity hs,
(mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is transitive. -/
lemma filter.tendsto.uniformity_trans {l : filter β} {f₁ f₂ f₃ : β → α}
(h₁₂ : tendsto (λ x, (f₁ x, f₂ x)) l (𝓤 α)) (h₂₃ : tendsto (λ x, (f₂ x, f₃ x)) l (𝓤 α)) :
tendsto (λ x, (f₁ x, f₃ x)) l (𝓤 α) :=
begin
refine le_trans (le_lift' $ λ s hs, mem_map.2 _) comp_le_uniformity,
filter_upwards [h₁₂ hs, h₂₃ hs],
exact λ x hx₁₂ hx₂₃, ⟨_, hx₁₂, hx₂₃⟩
end
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is symmetric -/
lemma filter.tendsto.uniformity_symm {l : filter β} {f : β → α × α}
(h : tendsto f l (𝓤 α)) :
tendsto (λ x, ((f x).2, (f x).1)) l (𝓤 α) :=
tendsto_swap_uniformity.comp h
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is reflexive. -/
lemma tendsto_diag_uniformity (f : β → α) (l : filter β) :
tendsto (λ x, (f x, f x)) l (𝓤 α) :=
assume s hs, mem_map.2 $ univ_mem_sets' $ λ x, refl_mem_uniformity hs
lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) :=
tendsto_diag_uniformity (λ _, a) f
lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s :=
have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs,
⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩
lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ t ○ t ⊆ s :=
let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in
let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in
⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩
lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α :=
by rw [map_swap_eq_comap_swap];
from map_le_iff_le_comap.1 tendsto_swap_uniformity
lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α :=
le_antisymm uniformity_le_symm symm_le_uniformity
lemma symmetrize_mem_uniformity {V : set (α × α)} (h : V ∈ 𝓤 α) : symmetrize_rel V ∈ 𝓤 α :=
begin
apply (𝓤 α).inter_sets h,
rw [← image_swap_eq_preimage_swap, uniformity_eq_symm],
exact image_mem_map h,
end
theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g)
(h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f :=
calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g :
lift_mono uniformity_le_symm (le_refl _)
... ≤ _ :
by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h
lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) :
(𝓤 α).lift (λs, f (s ○ s)) ≤ (𝓤 α).lift f :=
calc (𝓤 α).lift (λs, f (s ○ s)) =
((𝓤 α).lift' (λs:set (α×α), s ○ s)).lift f :
begin
rw [lift_lift'_assoc],
exact monotone_comp_rel monotone_id monotone_id,
exact h
end
... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity (le_refl _)
lemma comp_le_uniformity3 :
(𝓤 α).lift' (λs:set (α×α), s ○ (s ○ s)) ≤ (𝓤 α) :=
calc (𝓤 α).lift' (λd, d ○ (d ○ d)) =
(𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ (t ○ t))) :
begin
rw [lift_lift'_same_eq_lift'],
exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id),
exact (assume x, monotone_comp_rel monotone_id monotone_const),
end
... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ t)) :
lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (𝓟 ∘ (○) s) $
monotone_principal.comp (monotone_comp_rel monotone_const monotone_id)
... = (𝓤 α).lift' (λs:set(α×α), s ○ s) :
lift_lift'_same_eq_lift'
(assume s, monotone_comp_rel monotone_const monotone_id)
(assume s, monotone_comp_rel monotone_id monotone_const)
... ≤ (𝓤 α) : comp_le_uniformity
lemma comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ⊆ s :=
begin
obtain ⟨w, w_in, w_sub⟩ : ∃ w ∈ 𝓤 α, w ○ w ⊆ s := comp_mem_uniformity_sets hs,
use [symmetrize_rel w, symmetrize_mem_uniformity w_in, symmetric_symmetrize_rel w],
have : symmetrize_rel w ⊆ w := symmetrize_rel_subset_self w,
calc symmetrize_rel w ○ symmetrize_rel w ⊆ w ○ w : by mono
... ⊆ s : w_sub,
end
lemma subset_comp_self_of_mem_uniformity {s : set (α × α)} (h : s ∈ 𝓤 α) : s ⊆ s ○ s :=
subset_comp_self (refl_le_uniformity h)
lemma comp_comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ○ t ⊆ s :=
begin
rcases comp_symm_mem_uniformity_sets hs with ⟨w, w_in, w_symm, w_sub⟩,
rcases comp_symm_mem_uniformity_sets w_in with ⟨t, t_in, t_symm, t_sub⟩,
use [t, t_in, t_symm],
have : t ⊆ t ○ t := subset_comp_self_of_mem_uniformity t_in,
calc
t ○ t ○ t ⊆ w ○ t : by mono
... ⊆ w ○ (t ○ t) : by mono
... ⊆ w ○ w : by mono
... ⊆ s : w_sub,
end
/-!
### Balls in uniform spaces
-/
/-- The ball around `(x : β)` with respect to `(V : set (β × β))`. Intended to be
used for `V ∈ 𝓤 β`, but this is not needed for the definition. Recovers the
notions of metric space ball when `V = {p | dist p.1 p.2 < r }`. -/
def uniform_space.ball (x : β) (V : set (β × β)) : set β := (prod.mk x) ⁻¹' V
open uniform_space (ball)
/-- The triangle inequality for `uniform_space.ball` -/
lemma mem_ball_comp {V W : set (β × β)} {x y z} (h : y ∈ ball x V) (h' : z ∈ ball y W) :
z ∈ ball x (V ○ W) :=
prod_mk_mem_comp_rel h h'
lemma ball_subset_of_comp_subset {V W : set (β × β)} {x y} (h : x ∈ ball y W) (h' : W ○ W ⊆ V) :
ball x W ⊆ ball y V :=
λ z z_in, h' (mem_ball_comp h z_in)
lemma ball_mono {V W : set (β × β)} (h : V ⊆ W) (x : β) : ball x V ⊆ ball x W :=
by tauto
lemma mem_ball_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x y} :
x ∈ ball y V ↔ y ∈ ball x V :=
show (x, y) ∈ prod.swap ⁻¹' V ↔ (x, y) ∈ V, by { unfold symmetric_rel at hV, rw hV }
lemma ball_eq_of_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x} :
ball x V = {y | (y, x) ∈ V} :=
by { ext y, rw mem_ball_symmetry hV, exact iff.rfl }
lemma mem_comp_of_mem_ball {V W : set (β × β)} {x y z : β} (hV : symmetric_rel V)
(hx : x ∈ ball z V) (hy : y ∈ ball z W) : (x, y) ∈ V ○ W :=
begin
rw mem_ball_symmetry hV at hx,
exact ⟨z, hx, hy⟩
end
lemma mem_comp_comp {V W M : set (β × β)} (hW' : symmetric_rel W) {p : β × β} :
p ∈ V ○ M ○ W ↔ ((ball p.1 V).prod (ball p.2 W) ∩ M).nonempty :=
begin
cases p with x y,
split,
{ rintros ⟨z, ⟨w, hpw, hwz⟩, hzy⟩,
exact ⟨(w, z), ⟨hpw, by rwa mem_ball_symmetry hW'⟩, hwz⟩, },
{ rintro ⟨⟨w, z⟩, ⟨w_in, z_in⟩, hwz⟩,
rwa mem_ball_symmetry hW' at z_in,
use [z, w] ; tauto },
end
/-!
### Neighborhoods in uniform spaces
-/
lemma mem_nhds_uniformity_iff_right {x : α} {s : set α} :
s ∈ 𝓝 x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α :=
⟨ begin
simp only [mem_nhds_sets_iff, is_open_uniformity, and_imp, exists_imp_distrib],
exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq
end,
assume hs,
mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α},
assume x' hx', refl_mem_uniformity hx' rfl,
is_open_uniformity.mpr $ assume x' hx',
let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in
by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'),
by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b),
have hp : (x', b) ∈ t, from hax' ▸ hp',
have (b, b') ∈ t, from hab ▸ hp'',
have (x', b') ∈ t ○ t, from ⟨b, hp, this⟩,
show b' ∈ s,
from tr this rfl,
hs⟩⟩
lemma mem_nhds_uniformity_iff_left {x : α} {s : set α} :
s ∈ 𝓝 x ↔ {p : α × α | p.2 = x → p.1 ∈ s} ∈ 𝓤 α :=
by { rw [uniformity_eq_symm, mem_nhds_uniformity_iff_right], refl }
lemma nhds_eq_comap_uniformity_aux {α : Type u} {x : α} {s : set α} {F : filter (α × α)} :
{p : α × α | p.fst = x → p.snd ∈ s} ∈ F ↔ s ∈ comap (prod.mk x) F :=
by rw mem_comap_sets ; from iff.intro
(assume hs, ⟨_, hs, assume x hx, hx rfl⟩)
(assume ⟨t, h, ht⟩, F.sets_of_superset h $
assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp])
lemma nhds_eq_comap_uniformity {x : α} : 𝓝 x = (𝓤 α).comap (prod.mk x) :=
by { ext s, rw [mem_nhds_uniformity_iff_right], exact nhds_eq_comap_uniformity_aux }
lemma is_open_iff_ball_subset {s : set α} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 α, ball x V ⊆ s :=
begin
simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity],
exact iff.rfl,
end
lemma nhds_basis_uniformity' {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} :
(𝓝 x).has_basis p (λ i, {y | (x, y) ∈ s i}) :=
by { rw [nhds_eq_comap_uniformity], exact h.comap (prod.mk x) }
lemma nhds_basis_uniformity {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} :
(𝓝 x).has_basis p (λ i, {y | (y, x) ∈ s i}) :=
begin
replace h := h.comap prod.swap,
rw [← map_swap_eq_comap_swap, ← uniformity_eq_symm] at h,
exact nhds_basis_uniformity' h
end
lemma uniform_space.mem_nhds_iff {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, ball x V ⊆ s :=
begin
rw [nhds_eq_comap_uniformity, mem_comap_sets],
exact iff.rfl,
end
lemma uniform_space.ball_mem_nhds (x : α) ⦃V : set (α × α)⦄ (V_in : V ∈ 𝓤 α) : ball x V ∈ 𝓝 x :=
begin
rw uniform_space.mem_nhds_iff,
exact ⟨V, V_in, subset.refl _⟩
end
lemma uniform_space.mem_nhds_iff_symm {x : α} {s : set α} :
s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, symmetric_rel V ∧ ball x V ⊆ s :=
begin
rw uniform_space.mem_nhds_iff,
split,
{ rintros ⟨V, V_in, V_sub⟩,
use [symmetrize_rel V, symmetrize_mem_uniformity V_in, symmetric_symmetrize_rel V],
exact subset.trans (ball_mono (symmetrize_rel_subset_self V) x) V_sub },
{ rintros ⟨V, V_in, V_symm, V_sub⟩,
exact ⟨V, V_in, V_sub⟩ }
end
lemma uniform_space.has_basis_nhds (x : α) :
has_basis (𝓝 x) (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) (λ s, ball x s) :=
⟨λ t, by simp [uniform_space.mem_nhds_iff_symm, and_assoc]⟩
open uniform_space
lemma uniform_space.has_basis_nhds_prod (x y : α) :
has_basis (𝓝 (x, y)) (λ s, s ∈ 𝓤 α ∧ symmetric_rel s) $ λ s, (ball x s).prod (ball y s) :=
begin
rw nhds_prod_eq,
apply (has_basis_nhds x).prod' (has_basis_nhds y),
rintro U V ⟨U_in, U_symm⟩ ⟨V_in, V_symm⟩,
exact ⟨U ∩ V, ⟨(𝓤 α).inter_sets U_in V_in, symmetric_rel_inter U_symm V_symm⟩,
ball_mono (inter_subset_left U V) x, ball_mono (inter_subset_right U V) y⟩,
end
lemma nhds_eq_uniformity {x : α} : 𝓝 x = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) :=
(nhds_basis_uniformity' (𝓤 α).basis_sets).eq_binfi
lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) :
{y : α | (x, y) ∈ s} ∈ 𝓝 x :=
(nhds_basis_uniformity' (𝓤 α).basis_sets).mem_of_mem h
lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) :
{x : α | (x, y) ∈ s} ∈ 𝓝 y :=
mem_nhds_left _ (symm_le_uniformity h)
lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (𝓝 a) (𝓤 α) :=
assume s, mem_nhds_right a
lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (𝓝 a) (𝓤 α) :=
assume s, mem_nhds_left a
lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) :
(𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) :=
eq.trans
begin
rw [nhds_eq_uniformity],
exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage )
end
(congr_arg _ $ funext $ assume s, filter.lift_principal hg)
lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) :
(𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) :=
calc (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg
... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm]
... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) :
map_lift_eq2 $ hg.comp monotone_preimage
... = _ : by simp [image_swap_eq_preimage_swap]
lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} :
filter.prod (𝓝 a) (𝓝 b) =
(𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α),
set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) :=
begin
rw [prod_def],
show (𝓝 a).lift (λs:set α, (𝓝 b).lift (λt:set α, 𝓟 (set.prod s t))) = _,
rw [lift_nhds_right],
apply congr_arg, funext s,
rw [lift_nhds_left],
refl,
exact monotone_principal.comp (monotone_prod monotone_const monotone_id),
exact (monotone_lift' monotone_const $ monotone_lam $
assume x, monotone_prod monotone_id monotone_const)
end
lemma nhds_eq_uniformity_prod {a b : α} :
𝓝 (a, b) =
(𝓤 α).lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) :=
begin
rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'],
{ intro s, exact monotone_prod monotone_const monotone_preimage },
{ intro t, exact monotone_prod monotone_preimage monotone_const }
end
lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) :
∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} :=
let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in
have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from
assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $
show cl_d ∈ 𝓝 (x, y),
begin
rw [nhds_eq_uniformity_prod, mem_lift'_sets],
exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩,
exact monotone_prod monotone_preimage monotone_preimage
end,
have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)),
∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h,
by simp [classical.skolem] at this; simp; assumption,
match this with
| ⟨t, ht⟩ :=
⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)),
is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left,
assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end,
Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩
end
/-- Entourages are neighborhoods of the diagonal. -/
lemma nhds_le_uniformity : (⨆ x : α, 𝓝 (x, x)) ≤ 𝓤 α :=
begin
apply supr_le _,
intros x V V_in,
rcases comp_symm_mem_uniformity_sets V_in with ⟨w, w_in, w_symm, w_sub⟩,
have : (ball x w).prod (ball x w) ∈ 𝓝 (x, x),
{ rw nhds_prod_eq,
exact prod_mem_prod (ball_mem_nhds x w_in) (ball_mem_nhds x w_in) },
apply mem_sets_of_superset this,
rintros ⟨u, v⟩ ⟨u_in, v_in⟩,
exact w_sub (mem_comp_of_mem_ball w_symm u_in v_in)
end
/-!
### Closure and interior in uniform spaces
-/
lemma closure_eq_uniformity (s : set $ α × α) :
closure s = ⋂ V ∈ {V | V ∈ 𝓤 α ∧ symmetric_rel V}, V ○ s ○ V :=
begin
ext ⟨x, y⟩,
simp_rw [mem_closure_iff_nhds_basis (uniform_space.has_basis_nhds_prod x y),
mem_Inter, mem_set_of_eq],
apply forall_congr,
intro V,
apply forall_congr,
rintros ⟨V_in, V_symm⟩,
simp_rw [mem_comp_comp V_symm, inter_comm, exists_prop],
exact iff.rfl,
end
lemma uniformity_has_basis_closed : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_closed V) id :=
begin
rw filter.has_basis_self,
intro t,
split,
{ intro h,
rcases comp_comp_symm_mem_uniformity_sets h with ⟨w, w_in, w_symm, r⟩,
refine ⟨closure w, _, is_closed_closure, _⟩,
apply mem_sets_of_superset w_in subset_closure,
refine subset.trans _ r,
rw closure_eq_uniformity,
apply Inter_subset_of_subset,
apply Inter_subset,
exact ⟨w_in, w_symm⟩ },
{ rintros ⟨r, r_in, r_closed, r_sub⟩,
exact mem_sets_of_superset r_in r_sub, }
end
/-- Closed entourages form a basis of the uniformity filter. -/
lemma uniformity_has_basis_closure : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α) closure :=
⟨begin
intro t,
rw uniformity_has_basis_closed.mem_iff,
split,
{ rintros ⟨r, ⟨r_in, r_closed⟩, r_sub⟩,
use [r, r_in],
convert r_sub,
rw r_closed.closure_eq,
refl },
{ rintros ⟨r, r_in, r_sub⟩,
exact ⟨closure r, ⟨mem_sets_of_superset r_in subset_closure, is_closed_closure⟩, r_sub⟩ }
end⟩
lemma closure_eq_inter_uniformity {t : set (α×α)} :
closure t = (⋂ d ∈ 𝓤 α, d ○ (t ○ d)) :=
set.ext $ assume ⟨a, b⟩,
calc (a, b) ∈ closure t ↔ (𝓝 (a, b) ⊓ 𝓟 t ≠ ⊥) : by simp [closure_eq_cluster_pts, cluster_pt]
... ↔ (((@prod.swap α α) <$> 𝓤 α).lift'
(λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod]
... ↔ ((map (@prod.swap α α) (𝓤 α)).lift'
(λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
by refl
... ↔ ((𝓤 α).lift'
(λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
begin
rw [map_lift'_eq2],
simp [image_swap_eq_preimage_swap, function.comp],
exact monotone_prod monotone_preimage monotone_preimage
end
... ↔ (∀s ∈ 𝓤 α, (set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t).nonempty) :
begin
rw [lift'_inf_principal_eq, lift'_ne_bot_iff],
exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const
end
... ↔ (∀ s ∈ 𝓤 α, (a, b) ∈ s ○ (t ○ s)) :
forall_congr $ assume s, forall_congr $ assume hs,
⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩,
assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩
... ↔ _ : by simp
lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure)
(calc (𝓤 α).lift' closure ≤ (𝓤 α).lift' (λd, d ○ (d ○ d)) :
lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs)
... ≤ (𝓤 α) : comp_le_uniformity3)
lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior :=
le_antisymm
(le_infi $ assume d, le_infi $ assume hd,
let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $
monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in
let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in
have s ⊆ interior d, from
calc s ⊆ t : hst
... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $
assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩,
have interior d ∈ 𝓤 α, by filter_upwards [hs] this,
by simp [this])
(assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset)
lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
interior s ∈ 𝓤 α :=
by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs
lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) :
∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s :=
have s ∈ (𝓤 α).lift' closure, by rwa [uniformity_eq_uniformity_closure] at h,
have ∃ t ∈ 𝓤 α, closure t ⊆ s,
by rwa [mem_lift'_sets] at this; apply closure_mono,
let ⟨t, ht, hst⟩ := this in
⟨closure t, (𝓤 α).sets_of_superset ht subset_closure, is_closed_closure, hst⟩
/-!
### Uniformity bases
-/
lemma filter.has_basis.mem_uniformity_iff {p : β → Prop} {s : β → set (α×α)}
(h : (𝓤 α).has_basis p s) {t : set (α × α)} :
t ∈ 𝓤 α ↔ ∃ i (hi : p i), ∀ a b, (a, b) ∈ s i → (a, b) ∈ t :=
h.mem_iff.trans $ by simp only [prod.forall, subset_def]
/-- Symmetric entourages form a basis of `𝓤 α` -/
lemma uniform_space.has_basis_symmetric :
(𝓤 α).has_basis (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) id :=
⟨λ t, ⟨λ t_in, ⟨symmetrize_rel t,
⟨⟨symmetrize_mem_uniformity t_in, symmetric_symmetrize_rel t⟩,
symmetrize_rel_subset_self _⟩⟩,
λ ⟨s, ⟨s_in, h⟩, hst⟩, mem_sets_of_superset s_in hst⟩⟩
lemma uniform_space.has_seq_basis (h : is_countably_generated $ 𝓤 α) :
∃ V : ℕ → set (α × α), has_antimono_basis (𝓤 α) (λ _, true) V ∧ ∀ n, symmetric_rel (V n) :=
begin
rcases h.has_antimono_basis with ⟨U, hbasis, hdec, monotrue⟩, clear monotrue,
simp only [forall_prop_of_true] at hdec,
use λ n, symmetrize_rel (U n),
refine ⟨⟨⟨_⟩, by intros ; mono, by tauto⟩, λ n, symmetric_symmetrize_rel _⟩,
intros t,
rw hbasis.mem_iff,
split,
{ rintro ⟨i, _, hi⟩,
exact ⟨i, trivial, subset.trans (inter_subset_left _ _) hi⟩ },
{ rintro ⟨i, _, hi⟩,
rcases hbasis.mem_iff.mp (symmetrize_mem_uniformity $ hbasis.mem_of_mem trivial)
with ⟨j, _, hj⟩,
use j,
tauto }
end
/-! ### Uniform continuity -/
/-- A function `f : α → β` is *uniformly continuous* if `(f x, f y)` tends to the diagonal
as `(x, y)` tends to the diagonal. In other words, if `x` is sufficiently close to `y`, then
`f x` is close to `f y` no matter where `x` and `y` are located in `α`. -/
def uniform_continuous [uniform_space β] (f : α → β) :=
tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β)
/-- A function `f : α → β` is *uniformly continuous* on `s : set α` if `(f x, f y)` tends to
the diagonal as `(x, y)` tends to the diagonal while remaining in `s.prod s`.
In other words, if `x` is sufficiently close to `y`, then `f x` is close to
`f y` no matter where `x` and `y` are located in `s`.-/
def uniform_continuous_on [uniform_space β] (f : α → β) (s : set α) : Prop :=
tendsto (λ x : α × α, (f x.1, f x.2)) (𝓤 α ⊓ principal (s.prod s)) (𝓤 β)
theorem uniform_continuous_def [uniform_space β] {f : α → β} :
uniform_continuous f ↔ ∀ r ∈ 𝓤 β, { x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α :=
iff.rfl
theorem uniform_continuous_iff_eventually [uniform_space β] {f : α → β} :
uniform_continuous f ↔ ∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r :=
iff.rfl
lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) :
uniform_continuous c :=
have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from
eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b,
le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity
lemma uniform_continuous_id : uniform_continuous (@id α) :=
by simp [uniform_continuous]; exact tendsto_id
lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) :=
uniform_continuous_of_const $ λ _ _, rfl
lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {g : β → γ} {f : α → β}
(hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) :=
hg.comp hf
lemma filter.has_basis.uniform_continuous_iff [uniform_space β] {p : γ → Prop} {s : γ → set (α×α)}
(ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)} (hb : (𝓤 β).has_basis q t)
{f : α → β} :
uniform_continuous f ↔ ∀ i (hi : q i), ∃ j (hj : p j), ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ t i :=
(ha.tendsto_iff hb).trans $ by simp only [prod.forall]
end uniform_space
open_locale uniformity
section constructions
instance : partial_order (uniform_space α) :=
{ le := λt s, t.uniformity ≤ s.uniformity,
le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂,
le_refl := assume t, le_refl _,
le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ }
instance : has_Inf (uniform_space α) :=
⟨assume s, uniform_space.of_core {
uniformity := (⨅u∈s, @uniformity α u),
refl := le_infi $ assume u, le_infi $ assume hu, u.refl,
symm := le_infi $ assume u, le_infi $ assume hu,
le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm,
comp := le_infi $ assume u, le_infi $ assume hu,
le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩
private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) :
Inf tt ≤ t :=
show (⨅u∈tt, @uniformity α u) ≤ t.uniformity,
from infi_le_of_le t $ infi_le _ h
private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') :
t ≤ Inf tt :=
show t.uniformity ≤ (⨅u∈tt, @uniformity α u),
from le_infi $ assume t', le_infi $ assume ht', h t' ht'
instance : has_top (uniform_space α) :=
⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩
instance : has_bot (uniform_space α) :=
⟨{ to_topological_space := ⊥,
uniformity := 𝓟 id_rel,
refl := le_refl _,
symm := by simp [tendsto]; apply subset.refl,
comp :=
begin
rw [lift'_principal], {simp},
exact monotone_comp_rel monotone_id monotone_id
end,
is_open_uniformity :=
assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩
instance : complete_lattice (uniform_space α) :=
{ sup := λa b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h),
le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h),
sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩,
inf := λ a b, Inf {a, b},
le_inf := λ a b c h₁ h₂, le_Inf (λ u h,
by { cases h, exact h.symm ▸ h₁, exact (mem_singleton_iff.1 h).symm ▸ h₂ }),
inf_le_left := λ a b, Inf_le (by simp),
inf_le_right := λ a b, Inf_le (by simp),
top := ⊤,
le_top := λ a, show a.uniformity ≤ ⊤, from le_top,
bot := ⊥,
bot_le := λ u, u.refl,
Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t},
le_Sup := λ s u h, le_Inf (λ u' h', h' u h),
Sup_le := λ s u h, Inf_le h,
Inf := Inf,
le_Inf := λ s a hs, le_Inf hs,
Inf_le := λ s a ha, Inf_le ha,
..uniform_space.partial_order }
lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} :
(infi u).uniformity = (⨅i, (u i).uniformity) :=
show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from
le_antisymm
(le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩)
(le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _)
lemma inf_uniformity {u v : uniform_space α} :
(u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity :=
have (u ⊓ v) = (⨅i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq],
calc (u ⊓ v).uniformity = ((⨅i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this]
... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq]
instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩
instance inhabited_uniform_space_core : inhabited (uniform_space.core α) :=
⟨@uniform_space.to_core _ (default _)⟩
/-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f`
is the inverse image in the filter sense of the induced function `α × α → β × β`. -/
def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α :=
{ uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)),
to_topological_space := u.to_topological_space.induced f,
refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl),
symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap,
comp := le_trans
begin
rw [comap_lift'_eq, comap_lift'_eq2],
exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩),
repeat { exact monotone_comp_rel monotone_id monotone_id }
end
(comap_mono u.comp),
is_open_uniformity := λ s, begin
change (@is_open α (u.to_topological_space.induced f) s ↔ _),
simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff_right, filter.comap, and_comm],
refine ball_congr (λ x hx, ⟨_, _⟩),
{ rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩,
rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) },
{ rintro ⟨t, ht, hts⟩,
exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl,
mem_nhds_uniformity_iff_right.1 $ mem_nhds_left _ ht⟩ }
end }
lemma uniformity_comap [uniform_space α] [uniform_space β] {f : α → β}
(h : ‹uniform_space α› = uniform_space.comap f ‹uniform_space β›) :
𝓤 α = comap (prod.map f f) (𝓤 β) :=
by { rw h, refl }
lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id :=
by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id]
lemma uniform_space.comap_comap_comp {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} :
uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) :=
by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap_comp
lemma uniform_continuous_iff {α β} [uα : uniform_space α] [uβ : uniform_space β] {f : α → β} :
uniform_continuous f ↔ uα ≤ uβ.comap f :=
filter.map_le_iff_le_comap
lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] :
@uniform_continuous α β (uniform_space.comap f u) u f :=
tendsto_comap
theorem to_topological_space_comap {f : α → β} {u : uniform_space β} :
@uniform_space.to_topological_space _ (uniform_space.comap f u) =
topological_space.induced f (@uniform_space.to_topological_space β u) := rfl
lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α]
(h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g :=
tendsto_comap_iff.2 h
lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) :
@uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ :=
le_of_nhds_le_nhds $ assume a,
by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _)
lemma uniform_continuous.continuous [uniform_space α] [uniform_space β] {f : α → β}
(hf : uniform_continuous f) : continuous f :=
continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf
lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl
lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ :=
top_unique $ assume s hs, s.eq_empty_or_nonempty.elim
(assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤)
(assume ⟨x, hx⟩,
have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl,
this.symm ▸ @is_open_univ _ ⊤)
lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} :
(infi u).to_topological_space = ⨅i, (u i).to_topological_space :=
classical.by_cases
(assume h : nonempty ι,
eq_of_nhds_eq_nhds $ assume a,
begin
rw [nhds_infi, nhds_eq_uniformity],
change (infi u).uniformity.lift' (preimage $ prod.mk a) = _,
begin
rw [infi_uniformity, lift'_infi],
exact (congr_arg _ $ funext $ assume i, (@nhds_eq_uniformity α (u i) a).symm),
exact h,
exact assume a b, rfl
end
end)
(assume : ¬ nonempty ι,
le_antisymm
(le_infi $ assume i, to_topological_space_mono $ infi_le _ _)
(have infi u = ⊤, from top_unique $ le_infi $ assume i, (this ⟨i⟩).elim,
have @uniform_space.to_topological_space _ (infi u) = ⊤,
from this.symm ▸ to_topological_space_top,
this.symm ▸ le_top))
lemma to_topological_space_Inf {s : set (uniform_space α)} :
(Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) :=
begin
rw [Inf_eq_infi, to_topological_space_infi],
apply congr rfl,
funext x,
exact to_topological_space_infi
end
lemma to_topological_space_inf {u v : uniform_space α} :
(u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space :=
by rw [to_topological_space_Inf, infi_pair]
instance : uniform_space empty := ⊥
instance : uniform_space unit := ⊥
instance : uniform_space bool := ⊥
instance : uniform_space ℕ := ⊥
instance : uniform_space ℤ := ⊥
instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) :=
uniform_space.comap subtype.val t
lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] :
𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) :=
rfl
lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] :
uniform_continuous (subtype.val : {a : α // p a} → α) :=
uniform_continuous_comap
lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β]
{f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) :
uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) :=
uniform_continuous_comap' hf
lemma uniform_continuous_on_iff_restrict [uniform_space α] [uniform_space β] (f : α → β) (s : set α) :
uniform_continuous_on f s ↔ uniform_continuous (s.restrict f) :=
begin
unfold uniform_continuous_on set.restrict uniform_continuous tendsto,
rw [show (λ x : s × s, (f x.1, f x.2)) = prod.map f f ∘ coe, by ext x; cases x; refl,
uniformity_comap rfl,
show prod.map subtype.val subtype.val = (coe : s × s → α × α), by ext x; cases x; refl],
conv in (map _ (comap _ _)) { rw ← filter.map_map },
rw subtype_coe_map_comap_prod, refl,
end
lemma tendsto_of_uniform_continuous_subtype
[uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α}
(hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ 𝓝 a) :
tendsto f (𝓝 a) (𝓝 (f a)) :=
by rw [(@map_nhds_subtype_coe_eq α _ s a (mem_of_nhds ha) ha).symm]; exact
tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _)
section prod
/- a similar product space is possible on the function space (uniformity of pointwise convergence),
but we want to have the uniformity of uniform convergence on function spaces -/
instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) :=
uniform_space.of_core_eq
(u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_core
prod.topological_space
(calc prod.topological_space = (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_topological_space :
by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl
... = _ : by rw [uniform_space.to_core_to_topological_space])
theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) =
(𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓
(𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) :=
inf_uniformity
lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] :
𝓤 (α×β) =
map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (filter.prod (𝓤 α) (𝓤 β)) :=
have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) =
comap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))),
from funext $ assume f, map_eq_comap_of_inverse
(funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl),
by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap_comp, comap_comap_comp]
lemma mem_map_sets_iff' {α : Type*} {β : Type*} {f : filter α} {m : α → β} {t : set β} :
t ∈ (map m f).sets ↔ (∃s∈f, m '' s ⊆ t) :=
mem_map_sets_iff
lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space α] {s:set (α×α)} {f : α → α → α}
(hf : uniform_continuous (λp:α×α, f p.1 p.2)) (hs : s ∈ 𝓤 α) :
∃u∈𝓤 α, ∀a b c, (a, b) ∈ u → (f a c, f b c) ∈ s :=
begin
rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf,
rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf,
rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht,
refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩,
exact hab,
exact refl_mem_uniformity hv,
refl
end
lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)}
(ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) :
{p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) :=
by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb)
lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] :
tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) :=
le_trans (map_mono (@inf_le_left (uniform_space (α×β)) _ _ _)) map_comap_le
lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] :
tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) :=
le_trans (map_mono (@inf_le_right (uniform_space (α×β)) _ _ _)) map_comap_le
lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) :=
tendsto_prod_uniformity_fst
lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) :=
tendsto_prod_uniformity_snd
variables [uniform_space α] [uniform_space β] [uniform_space γ]
lemma uniform_continuous.prod_mk
{f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) :
uniform_continuous (λa, (f₁ a, f₂ a)) :=
by rw [uniform_continuous, uniformity_prod]; exact
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) :
uniform_continuous (λ a, f (a,b)) :=
h.comp (uniform_continuous_id.prod_mk uniform_continuous_const)
lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) :
uniform_continuous (λ b, f (a,b)) :=
h.comp (uniform_continuous_const.prod_mk uniform_continuous_id)
lemma uniform_continuous.prod_map [uniform_space δ] {f : α → γ} {g : β → δ}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
uniform_continuous (prod.map f g) :=
(hf.comp uniform_continuous_fst).prod_mk (hg.comp uniform_continuous_snd)
lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] :
@uniform_space.to_topological_space (α × β) prod.uniform_space =
@prod.topological_space α β u.to_topological_space v.to_topological_space := rfl
end prod
section
open uniform_space function
variables {δ' : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ]
[uniform_space δ']
local notation f `∘₂` g := function.bicompr f g
/-- Uniform continuity for functions of two variables. -/
def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry f)
lemma uniform_continuous₂_def (f : α → β → γ) :
uniform_continuous₂ f ↔ uniform_continuous (uncurry f) := iff.rfl
lemma uniform_continuous₂.uniform_continuous {f : α → β → γ} (h : uniform_continuous₂ f) :
uniform_continuous (uncurry f) := h
lemma uniform_continuous₂_curry (f : α × β → γ) :
uniform_continuous₂ (function.curry f) ↔ uniform_continuous f :=
by rw [uniform_continuous₂, uncurry_curry]
lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ}
(hg : uniform_continuous g) (hf : uniform_continuous₂ f) :
uniform_continuous₂ (g ∘₂ f) :=
hg.comp hf
lemma uniform_continuous₂.bicompl {f : α → β → γ} {ga : δ → α} {gb : δ' → β}
(hf : uniform_continuous₂ f) (hga : uniform_continuous ga) (hgb : uniform_continuous gb) :
uniform_continuous₂ (bicompl f ga gb) :=
hf.uniform_continuous.comp (hga.prod_map hgb)
end
lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} :
@uniform_space.to_topological_space (subtype p) subtype.uniform_space =
@subtype.topological_space α p u.to_topological_space := rfl
section sum
variables [uniform_space α] [uniform_space β]
open sum
/-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained
by taking independently an entourage of the diagonal in the first part, and an entourage of
the diagonal in the second part. -/
def uniform_space.core.sum : uniform_space.core (α ⊕ β) :=
uniform_space.core.mk'
(map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β))
(λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂])
(λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩)
(λ r ⟨Hrα, Hrβ⟩, begin
rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩,
rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩,
refine ⟨_,
⟨mem_map_sets_iff.2 ⟨tα, htα, subset_union_left _ _⟩,
mem_map_sets_iff.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩,
rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩,
⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩,
{ have A : (a, c) ∈ tα ○ tα := ⟨b, hab, hbc⟩,
exact Htα A },
{ have A : (a, c) ∈ tβ ○ tβ := ⟨b, hab, hbc⟩,
exact Htβ A }
end)
/-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage
of the diagonal. -/
lemma union_mem_uniformity_sum
{a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) :
((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈
(@uniform_space.core.sum α β _ _).uniformity :=
⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩
/- To prove that the topology defined by the uniform structure on the disjoint union coincides with
the disjoint union topology, we need two lemmas saying that open sets can be characterized by
the uniform structure -/
lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) :
{ p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity :=
begin
cases x,
{ refine mem_sets_of_superset
(union_mem_uniformity_sum (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.1 xs)) univ_mem_sets)
(union_subset _ _);
rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩,
exact h rfl },
{ refine mem_sets_of_superset
(union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.2 xs)))
(union_subset _ _);
rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩,
exact h rfl },
end
lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)}
(hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity) :
is_open s :=
begin
split,
{ refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff_right.2 _),
rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩,
refine mem_sets_of_superset ht _,
rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl },
{ refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff_right.2 _),
rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩,
refine mem_sets_of_superset ht _,
rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }
end
/- We can now define the uniform structure on the disjoint union -/
instance sum.uniform_space : uniform_space (α ⊕ β) :=
{ to_core := uniform_space.core.sum,
is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ }
lemma sum.uniformity : 𝓤 (α ⊕ β) =
map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔
map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl
end sum
end constructions
-- For a version of the Lebesgue number lemma assuming only a sequentially compact space,
-- see topology/sequences.lean
lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α}
(hs : compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) :
∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i :=
begin
let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ m ○ n} ⊆ c i},
have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n),
{ refine λ n hn, is_open_uniformity.2 _,
rintro x ⟨i, m, hm, h⟩,
rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩,
apply (𝓤 α).sets_of_superset hm',
rintros ⟨x, y⟩ hp rfl,
refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩,
dsimp at hz ⊢, rw comp_rel_assoc,
exact ⟨y, hp, hz⟩ },
have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n,
{ intros x hx,
rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩,
rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩,
exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ },
rcases hs.elim_finite_subcover_image hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩,
refine ⟨_, Inter_mem_sets b_fin bu, λ x hx, _⟩,
rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩,
refine ⟨i, λ y hy, h _⟩,
exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy)
end
lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)}
(hs : compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) :
∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t :=
by rw sUnion_eq_Union at hc₂;
simpa using lebesgue_number_lemma hs (by simpa) hc₂
/-!
### Expressing continuity properties in uniform spaces
We reformulate the various continuity properties of functions taking values in a uniform space
in terms of the uniformity in the target. Since the same lemmas (essentially with the same names)
also exist for metric spaces and emetric spaces (reformulating things in terms of the distance or
the edistance in the target), we put them in a namespace `uniform` here.
In the metric and emetric space setting, there are also similar lemmas where one assumes that
both the source and the target are metric spaces, reformulating things in terms of the distance
on both sides. These lemmas are generally written without primes, and the versions where only
the target is a metric space is primed. We follow the same convention here, thus giving lemmas
with primes.
-/
namespace uniform
variables [uniform_space α]
theorem tendsto_nhds_right {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ tendsto (λ x, (a, u x)) f (𝓤 α) :=
⟨λ H, tendsto_left_nhds_uniformity.comp H,
λ H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_right.1 hs)⟩
theorem tendsto_nhds_left {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ tendsto (λ x, (u x, a)) f (𝓤 α) :=
⟨λ H, tendsto_right_nhds_uniformity.comp H,
λ H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_left.1 hs)⟩
theorem continuous_at_iff'_right [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔ tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) :=
by rw [continuous_at, tendsto_nhds_right]
theorem continuous_at_iff'_left [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔ tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) :=
by rw [continuous_at, tendsto_nhds_left]
theorem continuous_within_at_iff'_right [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔ tendsto (λ x, (f b, f x)) (nhds_within b s) (𝓤 α) :=
by rw [continuous_within_at, tendsto_nhds_right]
theorem continuous_within_at_iff'_left [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔ tendsto (λ x, (f x, f b)) (nhds_within b s) (𝓤 α) :=
by rw [continuous_within_at, tendsto_nhds_left]
theorem continuous_on_iff'_right [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f b, f x)) (nhds_within b s) (𝓤 α) :=
by simp [continuous_on, continuous_within_at_iff'_right]
theorem continuous_on_iff'_left [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f x, f b)) (nhds_within b s) (𝓤 α) :=
by simp [continuous_on, continuous_within_at_iff'_left]
theorem continuous_iff'_right [topological_space β] {f : β → α} :
continuous f ↔ ∀ b, tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_right
theorem continuous_iff'_left [topological_space β] {f : β → α} :
continuous f ↔ ∀ b, tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_left
end uniform
lemma filter.tendsto.congr_uniformity {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β}
(hf : tendsto f l (𝓝 b)) (hg : tendsto (λ x, (f x, g x)) l (𝓤 β)) :
tendsto g l (𝓝 b) :=
uniform.tendsto_nhds_right.2 $ (uniform.tendsto_nhds_right.1 hf).uniformity_trans hg
lemma uniform.tendsto_congr {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β}
(hfg : tendsto (λ x, (f x, g x)) l (𝓤 β)) :
tendsto f l (𝓝 b) ↔ tendsto g l (𝓝 b) :=
⟨λ h, h.congr_uniformity hfg, λ h, h.congr_uniformity hfg.uniformity_symm⟩
|
4886ccab14b072cb22e5c2f4d0a48ae28bd0d64e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/analysis/normed_space/lp_space.lean | 52dae330881881193bc486912d0aa5a7f2d78db3 | [
"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 | 42,927 | lean | /-
Copyright (c) 2021 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import analysis.mean_inequalities
import analysis.mean_inequalities_pow
import topology.algebra.order.liminf_limsup
/-!
# ℓp space
This file describes properties of elements `f` of a pi-type `Π i, E i` with finite "norm",
defined for `p:ℝ≥0∞` as the size of the support of `f` if `p=0`, `(∑' a, ‖f a‖^p) ^ (1/p)` for
`0 < p < ∞` and `⨆ a, ‖f a‖` for `p=∞`.
The Prop-valued `mem_ℓp f p` states that a function `f : Π i, E i` has finite norm according
to the above definition; that is, `f` has finite support if `p = 0`, `summable (λ a, ‖f a‖^p)` if
`0 < p < ∞`, and `bdd_above (norm '' (set.range f))` if `p = ∞`.
The space `lp E p` is the subtype of elements of `Π i : α, E i` which satisfy `mem_ℓp f p`. For
`1 ≤ p`, the "norm" is genuinely a norm and `lp` is a complete metric space.
## Main definitions
* `mem_ℓp f p` : property that the function `f` satisfies, as appropriate, `f` finitely supported
if `p = 0`, `summable (λ a, ‖f a‖^p)` if `0 < p < ∞`, and `bdd_above (norm '' (set.range f))` if
`p = ∞`.
* `lp E p` : elements of `Π i : α, E i` such that `mem_ℓp f p`. Defined as an `add_subgroup` of
a type synonym `pre_lp` for `Π i : α, E i`, and equipped with a `normed_add_comm_group` structure.
Under appropriate conditions, this is also equipped with the instances `lp.normed_space`,
`lp.complete_space`. For `p=∞`, there is also `lp.infty_normed_ring`,
`lp.infty_normed_algebra`, `lp.infty_star_ring` and `lp.infty_cstar_ring`.
## Main results
* `mem_ℓp.of_exponent_ge`: For `q ≤ p`, a function which is `mem_ℓp` for `q` is also `mem_ℓp` for
`p`
* `lp.mem_ℓp_of_tendsto`, `lp.norm_le_of_tendsto`: A pointwise limit of functions in `lp`, all with
`lp` norm `≤ C`, is itself in `lp` and has `lp` norm `≤ C`.
* `lp.tsum_mul_le_mul_norm`: basic form of Hölder's inequality
## Implementation
Since `lp` is defined as an `add_subgroup`, dot notation does not work. Use `lp.norm_neg f` to
say that `‖-f‖ = ‖f‖`, instead of the non-working `f.norm_neg`.
## TODO
* More versions of Hölder's inequality (for example: the case `p = 1`, `q = ∞`; a version for normed
rings which has `‖∑' i, f i * g i‖` rather than `∑' i, ‖f i‖ * g i‖` on the RHS; a version for
three exponents satisfying `1 / r = 1 / p + 1 / q`)
-/
noncomputable theory
open_locale nnreal ennreal big_operators
variables {α : Type*} {E : α → Type*} {p q : ℝ≥0∞} [Π i, normed_add_comm_group (E i)]
/-!
### `mem_ℓp` predicate
-/
/-- The property that `f : Π i : α, E i`
* is finitely supported, if `p = 0`, or
* admits an upper bound for `set.range (λ i, ‖f i‖)`, if `p = ∞`, or
* has the series `∑' i, ‖f i‖ ^ p` be summable, if `0 < p < ∞`. -/
def mem_ℓp (f : Π i, E i) (p : ℝ≥0∞) : Prop :=
if p = 0 then (set.finite {i | f i ≠ 0}) else
(if p = ∞ then bdd_above (set.range (λ i, ‖f i‖)) else summable (λ i, ‖f i‖ ^ p.to_real))
lemma mem_ℓp_zero_iff {f : Π i, E i} : mem_ℓp f 0 ↔ set.finite {i | f i ≠ 0} :=
by dsimp [mem_ℓp]; rw [if_pos rfl]
lemma mem_ℓp_zero {f : Π i, E i} (hf : set.finite {i | f i ≠ 0}) : mem_ℓp f 0 :=
mem_ℓp_zero_iff.2 hf
lemma mem_ℓp_infty_iff {f : Π i, E i} : mem_ℓp f ∞ ↔ bdd_above (set.range (λ i, ‖f i‖)) :=
by dsimp [mem_ℓp]; rw [if_neg ennreal.top_ne_zero, if_pos rfl]
lemma mem_ℓp_infty {f : Π i, E i} (hf : bdd_above (set.range (λ i, ‖f i‖))) : mem_ℓp f ∞ :=
mem_ℓp_infty_iff.2 hf
lemma mem_ℓp_gen_iff (hp : 0 < p.to_real) {f : Π i, E i} :
mem_ℓp f p ↔ summable (λ i, ‖f i‖ ^ p.to_real) :=
begin
rw ennreal.to_real_pos_iff at hp,
dsimp [mem_ℓp],
rw [if_neg hp.1.ne', if_neg hp.2.ne],
end
lemma mem_ℓp_gen {f : Π i, E i} (hf : summable (λ i, ‖f i‖ ^ p.to_real)) :
mem_ℓp f p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
have H : summable (λ i : α, (1:ℝ)) := by simpa using hf,
exact (finite_of_summable_const (by norm_num) H).subset (set.subset_univ _) },
{ apply mem_ℓp_infty,
have H : summable (λ i : α, (1:ℝ)) := by simpa using hf,
simpa using ((finite_of_summable_const (by norm_num) H).image (λ i, ‖f i‖)).bdd_above },
exact (mem_ℓp_gen_iff hp).2 hf
end
lemma mem_ℓp_gen' {C : ℝ} {f : Π i, E i} (hf : ∀ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real ≤ C) :
mem_ℓp f p :=
begin
apply mem_ℓp_gen,
use ⨆ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real,
apply has_sum_of_is_lub_of_nonneg,
{ intros b,
exact real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
apply is_lub_csupr,
use C,
rintros - ⟨s, rfl⟩,
exact hf s
end
lemma zero_mem_ℓp : mem_ℓp (0 : Π i, E i) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp },
{ apply mem_ℓp_infty,
simp only [norm_zero, pi.zero_apply],
exact bdd_above_singleton.mono set.range_const_subset, },
{ apply mem_ℓp_gen,
simp [real.zero_rpow hp.ne', summable_zero], }
end
lemma zero_mem_ℓp' : mem_ℓp (λ i : α, (0 : E i)) p := zero_mem_ℓp
namespace mem_ℓp
lemma finite_dsupport {f : Π i, E i} (hf : mem_ℓp f 0) : set.finite {i | f i ≠ 0} :=
mem_ℓp_zero_iff.1 hf
lemma bdd_above {f : Π i, E i} (hf : mem_ℓp f ∞) : bdd_above (set.range (λ i, ‖f i‖)) :=
mem_ℓp_infty_iff.1 hf
lemma summable (hp : 0 < p.to_real) {f : Π i, E i} (hf : mem_ℓp f p) :
summable (λ i, ‖f i‖ ^ p.to_real) :=
(mem_ℓp_gen_iff hp).1 hf
lemma neg {f : Π i, E i} (hf : mem_ℓp f p) : mem_ℓp (-f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp [hf.finite_dsupport] },
{ apply mem_ℓp_infty,
simpa using hf.bdd_above },
{ apply mem_ℓp_gen,
simpa using hf.summable hp },
end
@[simp] lemma neg_iff {f : Π i, E i} : mem_ℓp (-f) p ↔ mem_ℓp f p :=
⟨λ h, neg_neg f ▸ h.neg, mem_ℓp.neg⟩
lemma of_exponent_ge {p q : ℝ≥0∞} {f : Π i, E i}
(hfq : mem_ℓp f q) (hpq : q ≤ p) :
mem_ℓp f p :=
begin
rcases ennreal.trichotomy₂ hpq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, hp⟩ | ⟨rfl, rfl⟩ | ⟨hq, rfl⟩
| ⟨hq, hp, hpq'⟩,
{ exact hfq },
{ apply mem_ℓp_infty,
obtain ⟨C, hC⟩ := (hfq.finite_dsupport.image (λ i, ‖f i‖)).bdd_above,
use max 0 C,
rintros x ⟨i, rfl⟩,
by_cases hi : f i = 0,
{ simp [hi] },
{ exact (hC ⟨i, hi, rfl⟩).trans (le_max_right _ _) } },
{ apply mem_ℓp_gen,
have : ∀ i ∉ hfq.finite_dsupport.to_finset, ‖f i‖ ^ p.to_real = 0,
{ intros i hi,
have : f i = 0 := by simpa using hi,
simp [this, real.zero_rpow hp.ne'] },
exact summable_of_ne_finset_zero this },
{ exact hfq },
{ apply mem_ℓp_infty,
obtain ⟨A, hA⟩ := (hfq.summable hq).tendsto_cofinite_zero.bdd_above_range_of_cofinite,
use A ^ (q.to_real⁻¹),
rintros x ⟨i, rfl⟩,
have : 0 ≤ ‖f i‖ ^ q.to_real := real.rpow_nonneg_of_nonneg (norm_nonneg _) _,
simpa [← real.rpow_mul, mul_inv_cancel hq.ne'] using
real.rpow_le_rpow this (hA ⟨i, rfl⟩) (inv_nonneg.mpr hq.le) },
{ apply mem_ℓp_gen,
have hf' := hfq.summable hq,
refine summable_of_norm_bounded_eventually _ hf' (@set.finite.subset _ {i | 1 ≤ ‖f i‖} _ _ _),
{ have H : {x : α | 1 ≤ ‖f x‖ ^ q.to_real}.finite,
{ simpa using eventually_lt_of_tendsto_lt (by norm_num : (0:ℝ) < 1)
hf'.tendsto_cofinite_zero },
exact H.subset (λ i hi, real.one_le_rpow hi hq.le) },
{ show ∀ i, ¬ (|‖f i‖ ^ p.to_real| ≤ ‖f i‖ ^ q.to_real) → 1 ≤ ‖f i‖,
intros i hi,
have : 0 ≤ ‖f i‖ ^ p.to_real := real.rpow_nonneg_of_nonneg (norm_nonneg _) p.to_real,
simp only [abs_of_nonneg, this] at hi,
contrapose! hi,
exact real.rpow_le_rpow_of_exponent_ge' (norm_nonneg _) hi.le hq.le hpq' } }
end
lemma add {f g : Π i, E i} (hf : mem_ℓp f p) (hg : mem_ℓp g p) : mem_ℓp (f + g) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
refine (hf.finite_dsupport.union hg.finite_dsupport).subset (λ i, _),
simp only [pi.add_apply, ne.def, set.mem_union, set.mem_set_of_eq],
contrapose!,
rintros ⟨hf', hg'⟩,
simp [hf', hg'] },
{ apply mem_ℓp_infty,
obtain ⟨A, hA⟩ := hf.bdd_above,
obtain ⟨B, hB⟩ := hg.bdd_above,
refine ⟨A + B, _⟩,
rintros a ⟨i, rfl⟩,
exact le_trans (norm_add_le _ _) (add_le_add (hA ⟨i, rfl⟩) (hB ⟨i, rfl⟩)) },
apply mem_ℓp_gen,
let C : ℝ := if p.to_real < 1 then 1 else 2 ^ (p.to_real - 1),
refine summable_of_nonneg_of_le _ (λ i, _) (((hf.summable hp).add (hg.summable hp)).mul_left C),
{ exact λ b, real.rpow_nonneg_of_nonneg (norm_nonneg (f b + g b)) p.to_real },
{ refine (real.rpow_le_rpow (norm_nonneg _) (norm_add_le _ _) hp.le).trans _,
dsimp [C],
split_ifs with h h,
{ simpa using nnreal.coe_le_coe.2 (nnreal.rpow_add_le_add_rpow (‖f i‖₊) (‖g i‖₊) hp.le h.le) },
{ let F : fin 2 → ℝ≥0 := ![‖f i‖₊, ‖g i‖₊],
have : ∀ i, (0:ℝ) ≤ F i := λ i, (F i).coe_nonneg,
simp only [not_lt] at h,
simpa [F, fin.sum_univ_succ] using
real.rpow_sum_le_const_mul_sum_rpow_of_nonneg (finset.univ : finset (fin 2)) h
(λ i _, (F i).coe_nonneg) } }
end
lemma sub {f g : Π i, E i} (hf : mem_ℓp f p) (hg : mem_ℓp g p) : mem_ℓp (f - g) p :=
by { rw sub_eq_add_neg, exact hf.add hg.neg }
lemma finset_sum {ι} (s : finset ι) {f : ι → Π i, E i} (hf : ∀ i ∈ s, mem_ℓp (f i) p) :
mem_ℓp (λ a, ∑ i in s, f i a) p :=
begin
haveI : decidable_eq ι := classical.dec_eq _,
revert hf,
refine finset.induction_on s _ _,
{ simp only [zero_mem_ℓp', finset.sum_empty, implies_true_iff], },
{ intros i s his ih hf,
simp only [his, finset.sum_insert, not_false_iff],
exact (hf i (s.mem_insert_self i)).add (ih (λ j hj, hf j (finset.mem_insert_of_mem hj))), },
end
section normed_space
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
lemma const_smul {f : Π i, E i} (hf : mem_ℓp f p) (c : 𝕜) : mem_ℓp (c • f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
refine hf.finite_dsupport.subset (λ i, (_ : ¬c • f i = 0 → ¬f i = 0)),
exact not_imp_not.mpr (λ hf', hf'.symm ▸ (smul_zero c)) },
{ obtain ⟨A, hA⟩ := hf.bdd_above,
refine mem_ℓp_infty ⟨‖c‖ * A, _⟩,
rintros a ⟨i, rfl⟩,
simpa [norm_smul] using mul_le_mul_of_nonneg_left (hA ⟨i, rfl⟩) (norm_nonneg c) },
{ apply mem_ℓp_gen,
convert (hf.summable hp).mul_left (‖c‖ ^ p.to_real),
ext i,
simp [norm_smul, real.mul_rpow (norm_nonneg c) (norm_nonneg (f i))] },
end
lemma const_mul {f : α → 𝕜} (hf : mem_ℓp f p) (c : 𝕜) : mem_ℓp (λ x, c * f x) p :=
@mem_ℓp.const_smul α (λ i, 𝕜) _ _ 𝕜 _ _ _ hf c
end normed_space
end mem_ℓp
/-!
### lp space
The space of elements of `Π i, E i` satisfying the predicate `mem_ℓp`.
-/
/-- We define `pre_lp E` to be a type synonym for `Π i, E i` which, importantly, does not inherit
the `pi` topology on `Π i, E i` (otherwise this topology would descend to `lp E p` and conflict
with the normed group topology we will later equip it with.)
We choose to deal with this issue by making a type synonym for `Π i, E i` rather than for the `lp`
subgroup itself, because this allows all the spaces `lp E p` (for varying `p`) to be subgroups of
the same ambient group, which permits lemma statements like `lp.monotone` (below). -/
@[derive add_comm_group, nolint unused_arguments]
def pre_lp (E : α → Type*) [Π i, normed_add_comm_group (E i)] : Type* := Π i, E i
instance pre_lp.unique [is_empty α] : unique (pre_lp E) := pi.unique_of_is_empty E
/-- lp space -/
def lp (E : α → Type*) [Π i, normed_add_comm_group (E i)]
(p : ℝ≥0∞) : add_subgroup (pre_lp E) :=
{ carrier := {f | mem_ℓp f p},
zero_mem' := zero_mem_ℓp,
add_mem' := λ f g, mem_ℓp.add,
neg_mem' := λ f, mem_ℓp.neg }
namespace lp
instance : has_coe (lp E p) (Π i, E i) := coe_subtype
instance : has_coe_to_fun (lp E p) (λ _, Π i, E i) := ⟨λ f, ((f : Π i, E i) : Π i, E i)⟩
@[ext] lemma ext {f g : lp E p} (h : (f : Π i, E i) = g) : f = g :=
subtype.ext h
protected lemma ext_iff {f g : lp E p} : f = g ↔ (f : Π i, E i) = g :=
subtype.ext_iff
lemma eq_zero' [is_empty α] (f : lp E p) : f = 0 := subsingleton.elim f 0
protected lemma monotone {p q : ℝ≥0∞} (hpq : q ≤ p) : lp E q ≤ lp E p :=
λ f hf, mem_ℓp.of_exponent_ge hf hpq
protected lemma mem_ℓp (f : lp E p) : mem_ℓp f p := f.prop
variables (E p)
@[simp] lemma coe_fn_zero : ⇑(0 : lp E p) = 0 := rfl
variables {E p}
@[simp] lemma coe_fn_neg (f : lp E p) : ⇑(-f) = -f := rfl
@[simp] lemma coe_fn_add (f g : lp E p) : ⇑(f + g) = f + g := rfl
@[simp] lemma coe_fn_sum {ι : Type*} (f : ι → lp E p) (s : finset ι) :
⇑(∑ i in s, f i) = ∑ i in s, ⇑(f i) :=
begin
classical,
refine finset.induction _ _ s,
{ simp },
intros i s his,
simp [finset.sum_insert his],
end
@[simp] lemma coe_fn_sub (f g : lp E p) : ⇑(f - g) = f - g := rfl
instance : has_norm (lp E p) :=
{ norm := λ f, if hp : p = 0 then by subst hp; exact (lp.mem_ℓp f).finite_dsupport.to_finset.card
else (if p = ∞ then ⨆ i, ‖f i‖ else (∑' i, ‖f i‖ ^ p.to_real) ^ (1/p.to_real)) }
lemma norm_eq_card_dsupport (f : lp E 0) : ‖f‖ = (lp.mem_ℓp f).finite_dsupport.to_finset.card :=
dif_pos rfl
lemma norm_eq_csupr (f : lp E ∞) : ‖f‖ = ⨆ i, ‖f i‖ :=
begin
dsimp [norm],
rw [dif_neg ennreal.top_ne_zero, if_pos rfl]
end
lemma is_lub_norm [nonempty α] (f : lp E ∞) : is_lub (set.range (λ i, ‖f i‖)) ‖f‖ :=
begin
rw lp.norm_eq_csupr,
exact is_lub_csupr (lp.mem_ℓp f)
end
lemma norm_eq_tsum_rpow (hp : 0 < p.to_real) (f : lp E p) :
‖f‖ = (∑' i, ‖f i‖ ^ p.to_real) ^ (1/p.to_real) :=
begin
dsimp [norm],
rw ennreal.to_real_pos_iff at hp,
rw [dif_neg hp.1.ne', if_neg hp.2.ne],
end
lemma norm_rpow_eq_tsum (hp : 0 < p.to_real) (f : lp E p) :
‖f‖ ^ p.to_real = ∑' i, ‖f i‖ ^ p.to_real :=
begin
rw [norm_eq_tsum_rpow hp, ← real.rpow_mul],
{ field_simp [hp.ne'] },
apply tsum_nonneg,
intros i,
calc (0:ℝ) = 0 ^ p.to_real : by rw real.zero_rpow hp.ne'
... ≤ _ : real.rpow_le_rpow rfl.le (norm_nonneg (f i)) hp.le
end
lemma has_sum_norm (hp : 0 < p.to_real) (f : lp E p) :
has_sum (λ i, ‖f i‖ ^ p.to_real) (‖f‖ ^ p.to_real) :=
begin
rw norm_rpow_eq_tsum hp,
exact ((lp.mem_ℓp f).summable hp).has_sum
end
lemma norm_nonneg' (f : lp E p) : 0 ≤ ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport f] },
{ cases is_empty_or_nonempty α with _i _i; resetI,
{ rw lp.norm_eq_csupr,
simp [real.csupr_empty] },
inhabit α,
exact (norm_nonneg (f default)).trans ((lp.is_lub_norm f).1 ⟨default, rfl⟩) },
{ rw lp.norm_eq_tsum_rpow hp f,
refine real.rpow_nonneg_of_nonneg (tsum_nonneg _) _,
exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
end
@[simp] lemma norm_zero : ‖(0 : lp E p)‖ = 0 :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport] },
{ simp [lp.norm_eq_csupr] },
{ rw lp.norm_eq_tsum_rpow hp,
have hp' : 1 / p.to_real ≠ 0 := one_div_ne_zero hp.ne',
simpa [real.zero_rpow hp.ne'] using real.zero_rpow hp' }
end
lemma norm_eq_zero_iff {f : lp E p} : ‖f‖ = 0 ↔ f = 0 :=
begin
classical,
refine ⟨λ h, _, by { rintros rfl, exact norm_zero }⟩,
rcases p.trichotomy with rfl | rfl | hp,
{ ext i,
have : {i : α | ¬f i = 0} = ∅ := by simpa [lp.norm_eq_card_dsupport f] using h,
have : (¬ (f i = 0)) = false := congr_fun this i,
tauto },
{ cases is_empty_or_nonempty α with _i _i; resetI,
{ simp },
have H : is_lub (set.range (λ i, ‖f i‖)) 0,
{ simpa [h] using lp.is_lub_norm f },
ext i,
have : ‖f i‖ = 0 := le_antisymm (H.1 ⟨i, rfl⟩) (norm_nonneg _),
simpa using this },
{ have hf : has_sum (λ (i : α), ‖f i‖ ^ p.to_real) 0,
{ have := lp.has_sum_norm hp f,
rwa [h, real.zero_rpow hp.ne'] at this },
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real := λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _,
rw has_sum_zero_iff_of_nonneg this at hf,
ext i,
have : f i = 0 ∧ p.to_real ≠ 0,
{ simpa [real.rpow_eq_zero_iff_of_nonneg (norm_nonneg (f i))] using congr_fun hf i },
exact this.1 },
end
lemma eq_zero_iff_coe_fn_eq_zero {f : lp E p} : f = 0 ↔ ⇑f = 0 :=
by rw [lp.ext_iff, coe_fn_zero]
@[simp] lemma norm_neg ⦃f : lp E p⦄ : ‖-f‖ = ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ simp [lp.norm_eq_card_dsupport] },
{ cases is_empty_or_nonempty α; resetI,
{ simp [lp.eq_zero' f], },
apply (lp.is_lub_norm (-f)).unique,
simpa using lp.is_lub_norm f },
{ suffices : ‖-f‖ ^ p.to_real = ‖f‖ ^ p.to_real,
{ exact real.rpow_left_inj_on hp.ne' (norm_nonneg' _) (norm_nonneg' _) this },
apply (lp.has_sum_norm hp (-f)).unique,
simpa using lp.has_sum_norm hp f }
end
instance [hp : fact (1 ≤ p)] : normed_add_comm_group (lp E p) :=
add_group_norm.to_normed_add_comm_group
{ to_fun := norm,
map_zero' := norm_zero,
neg' := norm_neg,
add_le' := λ f g, begin
unfreezingI { rcases p.dichotomy with rfl | hp' },
{ casesI is_empty_or_nonempty α,
{ simp [lp.eq_zero' f] },
refine (lp.is_lub_norm (f + g)).2 _,
rintros x ⟨i, rfl⟩,
refine le_trans _ (add_mem_upper_bounds_add (lp.is_lub_norm f).1 (lp.is_lub_norm g).1
⟨_, _, ⟨i, rfl⟩, ⟨i, rfl⟩, rfl⟩),
exact norm_add_le (f i) (g i) },
{ have hp'' : 0 < p.to_real := zero_lt_one.trans_le hp',
have hf₁ : ∀ i, 0 ≤ ‖f i‖ := λ i, norm_nonneg _,
have hg₁ : ∀ i, 0 ≤ ‖g i‖ := λ i, norm_nonneg _,
have hf₂ := lp.has_sum_norm hp'' f,
have hg₂ := lp.has_sum_norm hp'' g,
-- apply Minkowski's inequality
obtain ⟨C, hC₁, hC₂, hCfg⟩ :=
real.Lp_add_le_has_sum_of_nonneg hp' hf₁ hg₁ (norm_nonneg' _) (norm_nonneg' _) hf₂ hg₂,
refine le_trans _ hC₂,
rw ← real.rpow_le_rpow_iff (norm_nonneg' (f + g)) hC₁ hp'',
refine has_sum_le _ (lp.has_sum_norm hp'' (f + g)) hCfg,
intros i,
exact real.rpow_le_rpow (norm_nonneg _) (norm_add_le _ _) hp''.le },
end,
eq_zero_of_map_eq_zero' := λ f, norm_eq_zero_iff.1 }
-- TODO: define an `ennreal` version of `is_conjugate_exponent`, and then express this inequality
-- in a better version which also covers the case `p = 1, q = ∞`.
/-- Hölder inequality -/
protected lemma tsum_mul_le_mul_norm {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
summable (λ i, ‖f i‖ * ‖g i‖) ∧ ∑' i, ‖f i‖ * ‖g i‖ ≤ ‖f‖ * ‖g‖ :=
begin
have hf₁ : ∀ i, 0 ≤ ‖f i‖ := λ i, norm_nonneg _,
have hg₁ : ∀ i, 0 ≤ ‖g i‖ := λ i, norm_nonneg _,
have hf₂ := lp.has_sum_norm hpq.pos f,
have hg₂ := lp.has_sum_norm hpq.symm.pos g,
obtain ⟨C, -, hC', hC⟩ :=
real.inner_le_Lp_mul_Lq_has_sum_of_nonneg hpq (norm_nonneg' _) (norm_nonneg' _) hf₁ hg₁ hf₂ hg₂,
rw ← hC.tsum_eq at hC',
exact ⟨hC.summable, hC'⟩
end
protected lemma summable_mul {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
summable (λ i, ‖f i‖ * ‖g i‖) :=
(lp.tsum_mul_le_mul_norm hpq f g).1
protected lemma tsum_mul_le_mul_norm' {p q : ℝ≥0∞}
(hpq : p.to_real.is_conjugate_exponent q.to_real) (f : lp E p) (g : lp E q) :
∑' i, ‖f i‖ * ‖g i‖ ≤ ‖f‖ * ‖g‖ :=
(lp.tsum_mul_le_mul_norm hpq f g).2
section compare_pointwise
lemma norm_apply_le_norm (hp : p ≠ 0) (f : lp E p) (i : α) : ‖f i‖ ≤ ‖f‖ :=
begin
rcases eq_or_ne p ∞ with rfl | hp',
{ haveI : nonempty α := ⟨i⟩,
exact (is_lub_norm f).1 ⟨i, rfl⟩ },
have hp'' : 0 < p.to_real := ennreal.to_real_pos hp hp',
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real,
{ exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
rw ← real.rpow_le_rpow_iff (norm_nonneg _) (norm_nonneg' _) hp'',
convert le_has_sum (has_sum_norm hp'' f) i (λ i hi, this i),
end
lemma sum_rpow_le_norm_rpow (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
∑ i in s, ‖f i‖ ^ p.to_real ≤ ‖f‖ ^ p.to_real :=
begin
rw lp.norm_rpow_eq_tsum hp f,
have : ∀ i, 0 ≤ ‖f i‖ ^ p.to_real,
{ exact λ i, real.rpow_nonneg_of_nonneg (norm_nonneg _) _ },
refine sum_le_tsum _ (λ i hi, this i) _,
exact (lp.mem_ℓp f).summable hp
end
lemma norm_le_of_forall_le' [nonempty α] {f : lp E ∞} (C : ℝ) (hCf : ∀ i, ‖f i‖ ≤ C) : ‖f‖ ≤ C :=
begin
refine (is_lub_norm f).2 _,
rintros - ⟨i, rfl⟩,
exact hCf i,
end
lemma norm_le_of_forall_le {f : lp E ∞} {C : ℝ} (hC : 0 ≤ C) (hCf : ∀ i, ‖f i‖ ≤ C) : ‖f‖ ≤ C :=
begin
casesI is_empty_or_nonempty α,
{ simpa [eq_zero' f] using hC, },
{ exact norm_le_of_forall_le' C hCf },
end
lemma norm_le_of_tsum_le (hp : 0 < p.to_real) {C : ℝ} (hC : 0 ≤ C) {f : lp E p}
(hf : ∑' i, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real) :
‖f‖ ≤ C :=
begin
rw [← real.rpow_le_rpow_iff (norm_nonneg' _) hC hp, norm_rpow_eq_tsum hp],
exact hf,
end
lemma norm_le_of_forall_sum_le (hp : 0 < p.to_real) {C : ℝ} (hC : 0 ≤ C) {f : lp E p}
(hf : ∀ s : finset α, ∑ i in s, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real) :
‖f‖ ≤ C :=
norm_le_of_tsum_le hp hC (tsum_le_of_sum_le ((lp.mem_ℓp f).summable hp) hf)
end compare_pointwise
section normed_space
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
instance : module 𝕜 (pre_lp E) := pi.module α E 𝕜
lemma mem_lp_const_smul (c : 𝕜) (f : lp E p) : c • (f : pre_lp E) ∈ lp E p :=
(lp.mem_ℓp f).const_smul c
variables (E p 𝕜)
/-- The `𝕜`-submodule of elements of `Π i : α, E i` whose `lp` norm is finite. This is `lp E p`,
with extra structure. -/
def _root_.lp_submodule : submodule 𝕜 (pre_lp E) :=
{ smul_mem' := λ c f hf, by simpa using mem_lp_const_smul c ⟨f, hf⟩,
.. lp E p }
variables {E p 𝕜}
lemma coe_lp_submodule : (lp_submodule E p 𝕜).to_add_subgroup = lp E p := rfl
instance : module 𝕜 (lp E p) :=
{ .. (lp_submodule E p 𝕜).module }
@[simp] lemma coe_fn_smul (c : 𝕜) (f : lp E p) : ⇑(c • f) = c • f := rfl
lemma norm_const_smul (hp : p ≠ 0) {c : 𝕜} (f : lp E p) : ‖c • f‖ = ‖c‖ * ‖f‖ :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ exact absurd rfl hp },
{ cases is_empty_or_nonempty α; resetI,
{ simp [lp.eq_zero' f], },
apply (lp.is_lub_norm (c • f)).unique,
convert (lp.is_lub_norm f).mul_left (norm_nonneg c),
ext a,
simp [coe_fn_smul, norm_smul] },
{ suffices : ‖c • f‖ ^ p.to_real = (‖c‖ * ‖f‖) ^ p.to_real,
{ refine real.rpow_left_inj_on hp.ne' _ _ this,
{ exact norm_nonneg' _ },
{ exact mul_nonneg (norm_nonneg _) (norm_nonneg' _) } },
apply (lp.has_sum_norm hp (c • f)).unique,
convert (lp.has_sum_norm hp f).mul_left (‖c‖ ^ p.to_real),
{ simp [coe_fn_smul, norm_smul, real.mul_rpow (norm_nonneg c) (norm_nonneg _)] },
have hf : 0 ≤ ‖f‖ := lp.norm_nonneg' f,
simp [coe_fn_smul, norm_smul, real.mul_rpow (norm_nonneg c) hf] }
end
instance [fact (1 ≤ p)] : normed_space 𝕜 (lp E p) :=
{ norm_smul_le := λ c f, begin
have hp : 0 < p := ennreal.zero_lt_one.trans_le (fact.out _),
simp [norm_const_smul hp.ne']
end }
variables {𝕜' : Type*} [normed_field 𝕜']
instance [Π i, normed_space 𝕜' (E i)] [has_smul 𝕜' 𝕜] [Π i, is_scalar_tower 𝕜' 𝕜 (E i)] :
is_scalar_tower 𝕜' 𝕜 (lp E p) :=
begin
refine ⟨λ r c f, _⟩,
ext1,
exact (lp.coe_fn_smul _ _).trans (smul_assoc _ _ _)
end
end normed_space
section normed_star_group
variables [Π i, star_add_monoid (E i)] [Π i, normed_star_group (E i)]
lemma _root_.mem_ℓp.star_mem {f : Π i, E i}
(hf : mem_ℓp f p) : mem_ℓp (star f) p :=
begin
rcases p.trichotomy with rfl | rfl | hp,
{ apply mem_ℓp_zero,
simp [hf.finite_dsupport] },
{ apply mem_ℓp_infty,
simpa using hf.bdd_above },
{ apply mem_ℓp_gen,
simpa using hf.summable hp },
end
@[simp] lemma _root_.mem_ℓp.star_iff {f : Π i, E i} : mem_ℓp (star f) p ↔ mem_ℓp f p :=
⟨λ h, star_star f ▸ mem_ℓp.star_mem h ,mem_ℓp.star_mem⟩
instance : has_star (lp E p) :=
{ star := λ f, ⟨(star f : Π i, E i), f.property.star_mem⟩}
@[simp] lemma coe_fn_star (f : lp E p) : ⇑(star f) = star f := rfl
@[simp] protected theorem star_apply (f : lp E p) (i : α) : star f i = star (f i) := rfl
instance : has_involutive_star (lp E p) := { star_involutive := λ x, by {ext, simp} }
instance : star_add_monoid (lp E p) := { star_add := λ f g, ext $ star_add _ _ }
instance [hp : fact (1 ≤ p)] : normed_star_group (lp E p) :=
{ norm_star := λ f,
begin
unfreezingI { rcases p.trichotomy with rfl | rfl | h },
{ exfalso,
have := ennreal.to_real_mono ennreal.zero_ne_top hp.elim,
norm_num at this,},
{ simp only [lp.norm_eq_csupr, lp.star_apply, norm_star] },
{ simp only [lp.norm_eq_tsum_rpow h, lp.star_apply, norm_star] }
end }
variables {𝕜 : Type*} [has_star 𝕜] [normed_field 𝕜]
variables [Π i, normed_space 𝕜 (E i)] [Π i, star_module 𝕜 (E i)]
instance : star_module 𝕜 (lp E p) := { star_smul := λ r f, ext $ star_smul _ _ }
end normed_star_group
section non_unital_normed_ring
variables {I : Type*} {B : I → Type*} [Π i, non_unital_normed_ring (B i)]
lemma _root_.mem_ℓp.infty_mul {f g : Π i, B i} (hf : mem_ℓp f ∞) (hg : mem_ℓp g ∞) :
mem_ℓp (f * g) ∞ :=
begin
rw mem_ℓp_infty_iff,
obtain ⟨⟨Cf, hCf⟩, ⟨Cg, hCg⟩⟩ := ⟨hf.bdd_above, hg.bdd_above⟩,
refine ⟨Cf * Cg, _⟩,
rintros _ ⟨i, rfl⟩,
calc ‖(f * g) i‖ ≤ ‖f i‖ * ‖g i‖ : norm_mul_le (f i) (g i)
... ≤ Cf * Cg : mul_le_mul (hCf ⟨i, rfl⟩) (hCg ⟨i, rfl⟩) (norm_nonneg _)
((norm_nonneg _).trans (hCf ⟨i, rfl⟩))
end
instance : has_mul (lp B ∞) :=
{ mul := λ f g, ⟨(f * g : Π i, B i) , f.property.infty_mul g.property⟩}
@[simp] lemma infty_coe_fn_mul (f g : lp B ∞) : ⇑(f * g) = f * g := rfl
instance : non_unital_ring (lp B ∞) :=
function.injective.non_unital_ring lp.has_coe_to_fun.coe (subtype.coe_injective)
(lp.coe_fn_zero B ∞) lp.coe_fn_add infty_coe_fn_mul lp.coe_fn_neg lp.coe_fn_sub
(λ _ _, rfl) (λ _ _,rfl)
instance : non_unital_normed_ring (lp B ∞) :=
{ norm_mul := λ f g, lp.norm_le_of_forall_le (mul_nonneg (norm_nonneg f) (norm_nonneg g))
(λ i, calc ‖(f * g) i‖ ≤ ‖f i‖ * ‖g i‖ : norm_mul_le _ _
... ≤ ‖f‖ * ‖g‖
: mul_le_mul (lp.norm_apply_le_norm ennreal.top_ne_zero f i)
(lp.norm_apply_le_norm ennreal.top_ne_zero g i) (norm_nonneg _) (norm_nonneg _)),
.. lp.normed_add_comm_group }
-- we also want a `non_unital_normed_comm_ring` instance, but this has to wait for #13719
instance infty_is_scalar_tower {𝕜} [normed_field 𝕜] [Π i, normed_space 𝕜 (B i)]
[Π i, is_scalar_tower 𝕜 (B i) (B i)] :
is_scalar_tower 𝕜 (lp B ∞) (lp B ∞) :=
⟨λ r f g, lp.ext $ smul_assoc r ⇑f ⇑g⟩
instance infty_smul_comm_class {𝕜} [normed_field 𝕜] [Π i, normed_space 𝕜 (B i)]
[Π i, smul_comm_class 𝕜 (B i) (B i)] :
smul_comm_class 𝕜 (lp B ∞) (lp B ∞) :=
⟨λ r f g, lp.ext $ smul_comm r ⇑f ⇑g⟩
section star_ring
variables [Π i, star_ring (B i)] [Π i, normed_star_group (B i)]
instance infty_star_ring : star_ring (lp B ∞) :=
{ star_mul := λ f g, ext $ star_mul (_ : Π i, B i) _,
.. (show star_add_monoid (lp B ∞),
by { letI : Π i, star_add_monoid (B i) := λ i, infer_instance, apply_instance }) }
instance infty_cstar_ring [∀ i, cstar_ring (B i)] : cstar_ring (lp B ∞) :=
{ norm_star_mul_self := λ f,
begin
apply le_antisymm,
{ rw ←sq,
refine lp.norm_le_of_forall_le (sq_nonneg ‖ f ‖) (λ i, _),
simp only [lp.star_apply, cstar_ring.norm_star_mul_self, ←sq, infty_coe_fn_mul, pi.mul_apply],
refine sq_le_sq' _ (lp.norm_apply_le_norm ennreal.top_ne_zero _ _),
linarith [norm_nonneg (f i), norm_nonneg f] },
{ rw [←sq, ←real.le_sqrt (norm_nonneg _) (norm_nonneg _)],
refine lp.norm_le_of_forall_le (‖star f * f‖.sqrt_nonneg) (λ i, _),
rw [real.le_sqrt (norm_nonneg _) (norm_nonneg _), sq, ←cstar_ring.norm_star_mul_self],
exact lp.norm_apply_le_norm ennreal.top_ne_zero (star f * f) i, }
end }
end star_ring
end non_unital_normed_ring
section normed_ring
variables {I : Type*} {B : I → Type*} [Π i, normed_ring (B i)]
instance _root_.pre_lp.ring : ring (pre_lp B) := pi.ring
variables [Π i, norm_one_class (B i)]
lemma _root_.one_mem_ℓp_infty : mem_ℓp (1 : Π i, B i) ∞ :=
⟨1, by { rintros i ⟨i, rfl⟩, exact norm_one.le,}⟩
variables (B)
/-- The `𝕜`-subring of elements of `Π i : α, B i` whose `lp` norm is finite. This is `lp E ∞`,
with extra structure. -/
def _root_.lp_infty_subring : subring (pre_lp B) :=
{ carrier := {f | mem_ℓp f ∞},
one_mem' := one_mem_ℓp_infty,
mul_mem' := λ f g hf hg, hf.infty_mul hg,
.. lp B ∞ }
variables {B}
instance infty_ring : ring (lp B ∞) := (lp_infty_subring B).to_ring
lemma _root_.mem_ℓp.infty_pow {f : Π i, B i} (hf : mem_ℓp f ∞) (n : ℕ) : mem_ℓp (f ^ n) ∞ :=
(lp_infty_subring B).pow_mem hf n
lemma _root_.nat_cast_mem_ℓp_infty (n : ℕ) : mem_ℓp (n : Π i, B i) ∞ :=
nat_cast_mem (lp_infty_subring B) n
lemma _root_.int_cast_mem_ℓp_infty (z : ℤ) : mem_ℓp (z : Π i, B i) ∞ :=
coe_int_mem (lp_infty_subring B) z
@[simp] lemma infty_coe_fn_one : ⇑(1 : lp B ∞) = 1 := rfl
@[simp] lemma infty_coe_fn_pow (f : lp B ∞) (n : ℕ) : ⇑(f ^ n) = f ^ n := rfl
@[simp] lemma infty_coe_fn_nat_cast (n : ℕ) : ⇑(n : lp B ∞) = n := rfl
@[simp] lemma infty_coe_fn_int_cast (z : ℤ) : ⇑(z : lp B ∞) = z := rfl
instance [nonempty I] : norm_one_class (lp B ∞) :=
{ norm_one := by simp_rw [lp.norm_eq_csupr, infty_coe_fn_one, pi.one_apply, norm_one, csupr_const]}
instance infty_normed_ring : normed_ring (lp B ∞) :=
{ .. lp.infty_ring, .. lp.non_unital_normed_ring }
end normed_ring
section normed_comm_ring
variables {I : Type*} {B : I → Type*} [Π i, normed_comm_ring (B i)] [∀ i, norm_one_class (B i)]
instance infty_comm_ring : comm_ring (lp B ∞) :=
{ mul_comm := λ f g, by { ext, simp only [lp.infty_coe_fn_mul, pi.mul_apply, mul_comm] },
.. lp.infty_ring }
instance infty_normed_comm_ring : normed_comm_ring (lp B ∞) :=
{ .. lp.infty_comm_ring, .. lp.infty_normed_ring }
end normed_comm_ring
section algebra
variables {I : Type*} {𝕜 : Type*} {B : I → Type*}
variables [normed_field 𝕜] [Π i, normed_ring (B i)] [Π i, normed_algebra 𝕜 (B i)]
/-- A variant of `pi.algebra` that lean can't find otherwise. -/
instance _root_.pi.algebra_of_normed_algebra : algebra 𝕜 (Π i, B i) :=
@pi.algebra I 𝕜 B _ _ $ λ i, normed_algebra.to_algebra
instance _root_.pre_lp.algebra : algebra 𝕜 (pre_lp B) := _root_.pi.algebra_of_normed_algebra
variables [∀ i, norm_one_class (B i)]
lemma _root_.algebra_map_mem_ℓp_infty (k : 𝕜) : mem_ℓp (algebra_map 𝕜 (Π i, B i) k) ∞ :=
begin
rw algebra.algebra_map_eq_smul_one,
exact (one_mem_ℓp_infty.const_smul k : mem_ℓp (k • 1 : Π i, B i) ∞)
end
variables (𝕜 B)
/-- The `𝕜`-subalgebra of elements of `Π i : α, B i` whose `lp` norm is finite. This is `lp E ∞`,
with extra structure. -/
def _root_.lp_infty_subalgebra : subalgebra 𝕜 (pre_lp B) :=
{ carrier := {f | mem_ℓp f ∞},
algebra_map_mem' := algebra_map_mem_ℓp_infty,
.. lp_infty_subring B }
variables {𝕜 B}
instance infty_normed_algebra : normed_algebra 𝕜 (lp B ∞) :=
{ ..(lp_infty_subalgebra 𝕜 B).algebra,
..(lp.normed_space : normed_space 𝕜 (lp B ∞)) }
end algebra
section single
variables {𝕜 : Type*} [normed_field 𝕜] [Π i, normed_space 𝕜 (E i)]
variables [decidable_eq α]
/-- The element of `lp E p` which is `a : E i` at the index `i`, and zero elsewhere. -/
protected def single (p) (i : α) (a : E i) : lp E p :=
⟨ λ j, if h : j = i then eq.rec a h.symm else 0,
begin
refine (mem_ℓp_zero _).of_exponent_ge (zero_le p),
refine (set.finite_singleton i).subset _,
intros j,
simp only [forall_exists_index, set.mem_singleton_iff, ne.def, dite_eq_right_iff,
set.mem_set_of_eq, not_forall],
rintros rfl,
simp,
end ⟩
protected lemma single_apply (p) (i : α) (a : E i) (j : α) :
lp.single p i a j = if h : j = i then eq.rec a h.symm else 0 :=
rfl
protected lemma single_apply_self (p) (i : α) (a : E i) :
lp.single p i a i = a :=
by rw [lp.single_apply, dif_pos rfl]
protected lemma single_apply_ne (p) (i : α) (a : E i) {j : α} (hij : j ≠ i) :
lp.single p i a j = 0 :=
by rw [lp.single_apply, dif_neg hij]
@[simp] protected lemma single_neg (p) (i : α) (a : E i) :
lp.single p i (- a) = - lp.single p i a :=
begin
ext j,
by_cases hi : j = i,
{ subst hi,
simp [lp.single_apply_self] },
{ simp [lp.single_apply_ne p i _ hi] }
end
@[simp] protected lemma single_smul (p) (i : α) (a : E i) (c : 𝕜) :
lp.single p i (c • a) = c • lp.single p i a :=
begin
ext j,
by_cases hi : j = i,
{ subst hi,
simp [lp.single_apply_self] },
{ simp [lp.single_apply_ne p i _ hi] }
end
protected lemma norm_sum_single (hp : 0 < p.to_real) (f : Π i, E i) (s : finset α) :
‖∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ∑ i in s, ‖f i‖ ^ p.to_real :=
begin
refine (has_sum_norm hp (∑ i in s, lp.single p i (f i))).unique _,
simp only [lp.single_apply, coe_fn_sum, finset.sum_apply, finset.sum_dite_eq],
have h : ∀ i ∉ s, ‖ite (i ∈ s) (f i) 0‖ ^ p.to_real = 0,
{ intros i hi,
simp [if_neg hi, real.zero_rpow hp.ne'], },
have h' : ∀ i ∈ s, ‖f i‖ ^ p.to_real = ‖ite (i ∈ s) (f i) 0‖ ^ p.to_real,
{ intros i hi,
rw if_pos hi },
simpa [finset.sum_congr rfl h'] using has_sum_sum_of_ne_finset_zero h,
end
protected lemma norm_single (hp : 0 < p.to_real) (f : Π i, E i) (i : α) :
‖lp.single p i (f i)‖ = ‖f i‖ :=
begin
refine real.rpow_left_inj_on hp.ne' (norm_nonneg' _) (norm_nonneg _) _,
simpa using lp.norm_sum_single hp f {i},
end
protected lemma norm_sub_norm_compl_sub_single (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
‖f‖ ^ p.to_real - ‖f - ∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ∑ i in s, ‖f i‖ ^ p.to_real :=
begin
refine ((has_sum_norm hp f).sub (has_sum_norm hp (f - ∑ i in s, lp.single p i (f i)))).unique _,
let F : α → ℝ := λ i, ‖f i‖ ^ p.to_real - ‖(f - ∑ i in s, lp.single p i (f i)) i‖ ^ p.to_real,
have hF : ∀ i ∉ s, F i = 0,
{ intros i hi,
suffices : ‖f i‖ ^ p.to_real - ‖f i - ite (i ∈ s) (f i) 0‖ ^ p.to_real = 0,
{ simpa only [F, coe_fn_sum, lp.single_apply, coe_fn_sub, pi.sub_apply, finset.sum_apply,
finset.sum_dite_eq] using this, },
simp only [if_neg hi, sub_zero, sub_self] },
have hF' : ∀ i ∈ s, F i = ‖f i‖ ^ p.to_real,
{ intros i hi,
simp only [F, coe_fn_sum, lp.single_apply, if_pos hi, sub_self, eq_self_iff_true,
coe_fn_sub, pi.sub_apply, finset.sum_apply, finset.sum_dite_eq, sub_eq_self],
simp [real.zero_rpow hp.ne'], },
have : has_sum F (∑ i in s, F i) := has_sum_sum_of_ne_finset_zero hF,
rwa [finset.sum_congr rfl hF'] at this,
end
protected lemma norm_compl_sum_single (hp : 0 < p.to_real) (f : lp E p) (s : finset α) :
‖f - ∑ i in s, lp.single p i (f i)‖ ^ p.to_real = ‖f‖ ^ p.to_real - ∑ i in s, ‖f i‖ ^ p.to_real :=
by linarith [lp.norm_sub_norm_compl_sub_single hp f s]
/-- The canonical finitely-supported approximations to an element `f` of `lp` converge to it, in the
`lp` topology. -/
protected lemma has_sum_single [fact (1 ≤ p)] (hp : p ≠ ⊤) (f : lp E p) :
has_sum (λ i : α, lp.single p i (f i : E i)) f :=
begin
have hp₀ : 0 < p := ennreal.zero_lt_one.trans_le (fact.out _),
have hp' : 0 < p.to_real := ennreal.to_real_pos hp₀.ne' hp,
have := lp.has_sum_norm hp' f,
rw [has_sum, metric.tendsto_nhds] at this ⊢,
intros ε hε,
refine (this _ (real.rpow_pos_of_pos hε p.to_real)).mono _,
intros s hs,
rw ← real.rpow_lt_rpow_iff dist_nonneg (le_of_lt hε) hp',
rw dist_comm at hs,
simp only [dist_eq_norm, real.norm_eq_abs] at hs ⊢,
have H : ‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real
= ‖f‖ ^ p.to_real - ∑ i in s, ‖f i‖ ^ p.to_real,
{ simpa only [coe_fn_neg, pi.neg_apply, lp.single_neg, finset.sum_neg_distrib,
neg_sub_neg, norm_neg, _root_.norm_neg] using lp.norm_compl_sum_single hp' (-f) s },
rw ← H at hs,
have : |‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real|
= ‖∑ i in s, lp.single p i (f i : E i) - f‖ ^ p.to_real,
{ simp only [real.abs_rpow_of_nonneg (norm_nonneg _), abs_norm_eq_norm] },
linarith
end
end single
section topology
open filter
open_locale topological_space uniformity
/-- The coercion from `lp E p` to `Π i, E i` is uniformly continuous. -/
lemma uniform_continuous_coe [_i : fact (1 ≤ p)] : uniform_continuous (coe : lp E p → Π i, E i) :=
begin
have hp : p ≠ 0 := (ennreal.zero_lt_one.trans_le _i.elim).ne',
rw uniform_continuous_pi,
intros i,
rw normed_add_comm_group.uniformity_basis_dist.uniform_continuous_iff
normed_add_comm_group.uniformity_basis_dist,
intros ε hε,
refine ⟨ε, hε, _⟩,
rintros f g (hfg : ‖f - g‖ < ε),
have : ‖f i - g i‖ ≤ ‖f - g‖ := norm_apply_le_norm hp (f - g) i,
exact this.trans_lt hfg,
end
variables {ι : Type*} {l : filter ι} [filter.ne_bot l]
lemma norm_apply_le_of_tendsto {C : ℝ} {F : ι → lp E ∞} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C)
{f : Π a, E a} (hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) (a : α) :
‖f a‖ ≤ C :=
begin
have : tendsto (λ k, ‖F k a‖) l (𝓝 ‖f a‖) :=
(tendsto.comp (continuous_apply a).continuous_at hf).norm,
refine le_of_tendsto this (hCF.mono _),
intros k hCFk,
exact (norm_apply_le_norm ennreal.top_ne_zero (F k) a).trans hCFk,
end
variables [_i : fact (1 ≤ p)]
include _i
lemma sum_rpow_le_of_tendsto (hp : p ≠ ∞) {C : ℝ} {F : ι → lp E p} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C)
{f : Π a, E a} (hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) (s : finset α) :
∑ (i : α) in s, ‖f i‖ ^ p.to_real ≤ C ^ p.to_real :=
begin
have hp' : p ≠ 0 := (ennreal.zero_lt_one.trans_le _i.elim).ne',
have hp'' : 0 < p.to_real := ennreal.to_real_pos hp' hp,
let G : (Π a, E a) → ℝ := λ f, ∑ a in s, ‖f a‖ ^ p.to_real,
have hG : continuous G,
{ refine continuous_finset_sum s _,
intros a ha,
have : continuous (λ f : Π a, E a, f a):= continuous_apply a,
exact this.norm.rpow_const (λ _, or.inr hp''.le) },
refine le_of_tendsto (hG.continuous_at.tendsto.comp hf) _,
refine hCF.mono _,
intros k hCFk,
refine (lp.sum_rpow_le_norm_rpow hp'' (F k) s).trans _,
exact real.rpow_le_rpow (norm_nonneg _) hCFk hp''.le,
end
/-- "Semicontinuity of the `lp` norm": If all sufficiently large elements of a sequence in `lp E p`
have `lp` norm `≤ C`, then the pointwise limit, if it exists, also has `lp` norm `≤ C`. -/
lemma norm_le_of_tendsto {C : ℝ} {F : ι → lp E p} (hCF : ∀ᶠ k in l, ‖F k‖ ≤ C) {f : lp E p}
(hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) :
‖f‖ ≤ C :=
begin
obtain ⟨i, hi⟩ := hCF.exists,
have hC : 0 ≤ C := (norm_nonneg _).trans hi,
unfreezingI { rcases eq_top_or_lt_top p with rfl | hp },
{ apply norm_le_of_forall_le hC,
exact norm_apply_le_of_tendsto hCF hf, },
{ have : 0 < p := ennreal.zero_lt_one.trans_le _i.elim,
have hp' : 0 < p.to_real := ennreal.to_real_pos this.ne' hp.ne,
apply norm_le_of_forall_sum_le hp' hC,
exact sum_rpow_le_of_tendsto hp.ne hCF hf, }
end
/-- If `f` is the pointwise limit of a bounded sequence in `lp E p`, then `f` is in `lp E p`. -/
lemma mem_ℓp_of_tendsto {F : ι → lp E p} (hF : metric.bounded (set.range F)) {f : Π a, E a}
(hf : tendsto (id (λ i, F i) : ι → Π a, E a) l (𝓝 f)) :
mem_ℓp f p :=
begin
obtain ⟨C, hC, hCF'⟩ := hF.exists_pos_norm_le,
have hCF : ∀ k, ‖F k‖ ≤ C := λ k, hCF' _ ⟨k, rfl⟩,
unfreezingI { rcases eq_top_or_lt_top p with rfl | hp },
{ apply mem_ℓp_infty,
use C,
rintros _ ⟨a, rfl⟩,
refine norm_apply_le_of_tendsto (eventually_of_forall hCF) hf a, },
{ apply mem_ℓp_gen',
exact sum_rpow_le_of_tendsto hp.ne (eventually_of_forall hCF) hf },
end
/-- If a sequence is Cauchy in the `lp E p` topology and pointwise convergent to a element `f` of
`lp E p`, then it converges to `f` in the `lp E p` topology. -/
lemma tendsto_lp_of_tendsto_pi {F : ℕ → lp E p} (hF : cauchy_seq F) {f : lp E p}
(hf : tendsto (id (λ i, F i) : ℕ → Π a, E a) at_top (𝓝 f)) :
tendsto F at_top (𝓝 f) :=
begin
rw metric.nhds_basis_closed_ball.tendsto_right_iff,
intros ε hε,
have hε' : {p : (lp E p) × (lp E p) | ‖p.1 - p.2‖ < ε} ∈ 𝓤 (lp E p),
{ exact normed_add_comm_group.uniformity_basis_dist.mem_of_mem hε },
refine (hF.eventually_eventually hε').mono _,
rintros n (hn : ∀ᶠ l in at_top, ‖(λ f, F n - f) (F l)‖ < ε),
refine norm_le_of_tendsto (hn.mono (λ k hk, hk.le)) _,
rw tendsto_pi_nhds,
intros a,
exact (hf.apply a).const_sub (F n a),
end
variables [Π a, complete_space (E a)]
instance : complete_space (lp E p) :=
metric.complete_of_cauchy_seq_tendsto
begin
intros F hF,
-- A Cauchy sequence in `lp E p` is pointwise convergent; let `f` be the pointwise limit.
obtain ⟨f, hf⟩ := cauchy_seq_tendsto_of_complete (uniform_continuous_coe.comp_cauchy_seq hF),
-- Since the Cauchy sequence is bounded, its pointwise limit `f` is in `lp E p`.
have hf' : mem_ℓp f p := mem_ℓp_of_tendsto hF.bounded_range hf,
-- And therefore `f` is its limit in the `lp E p` topology as well as pointwise.
exact ⟨⟨f, hf'⟩, tendsto_lp_of_tendsto_pi hF hf⟩
end
end topology
end lp
|
5a0bc9a6ae56556ebd2424852071e2fb8f66f8a9 | 130c49f47783503e462c16b2eff31933442be6ff | /src/Lean/Data/Lsp/Extra.lean | 43b1a3fa045935b4187a98657124e2aa0cb68904 | [
"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 | 4,186 | 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 Lean.Data.Json
import Lean.Data.JsonRpc
import Lean.Data.Lsp.Basic
/-! This file contains Lean-specific extensions to LSP. See the structures below for which
additional requests and notifications are supported. -/
namespace Lean.Lsp
open Json
/-- `textDocument/waitForDiagnostics` client->server request.
Yields a response when all the diagnostics for a version of the document greater or equal to the
specified one have been emitted. If the request specifies a version above the most recently
processed one, the server will delay the response until it does receive the specified version.
Exists for synchronization purposes, e.g. during testing or when external tools might want to use
our LSP server. -/
structure WaitForDiagnosticsParams where
uri : DocumentUri
version : Nat
deriving FromJson, ToJson
structure WaitForDiagnostics
instance : FromJson WaitForDiagnostics :=
⟨fun j => WaitForDiagnostics.mk⟩
instance : ToJson WaitForDiagnostics :=
⟨fun o => mkObj []⟩
structure LeanFileProgressProcessingInfo where
range : Range
deriving FromJson, ToJson
/-- `$/lean/fileProgress` client<-server notification.
Contains the ranges of the document that are currently being processed by the server. -/
structure LeanFileProgressParams where
textDocument : VersionedTextDocumentIdentifier
processing : Array LeanFileProgressProcessingInfo
deriving FromJson, ToJson
/-- `$/lean/plainGoal` client->server request.
Returns the goal(s) at the specified position, pretty-printed as a string. -/
structure PlainGoalParams extends TextDocumentPositionParams
deriving FromJson, ToJson
structure PlainGoal where
rendered : String
goals : Array String
deriving FromJson, ToJson
/-- `$/lean/plainTermGoal` client->server request.
Returns the expected type at the specified position, pretty-printed as a string. -/
structure PlainTermGoalParams extends TextDocumentPositionParams
deriving FromJson, ToJson
structure PlainTermGoal where
goal : String
range : Range
deriving FromJson, ToJson
/-- An object which RPC clients can refer to without marshalling. -/
structure RpcRef where
/- NOTE(WN): It is important for this to be a single-field structure
in order to deserialize as an `Object` on the JS side. -/
p : USize
deriving BEq, Hashable, FromJson, ToJson
instance : ToString RpcRef where
toString r := toString r.p
/-- `$/lean/rpc/connect` client->server notification.
A notification to connect to the RPC session at the given file's worker. Should be sent:
- exactly once whenever RPC is first needed (e.g. on client startup)
- if an `RpcNeedsReconnect` error is received in an RPC request -/
structure RpcConnectParams where
uri : DocumentUri
deriving FromJson, ToJson
/-- `$/lean/rpc/connected` client<-server notification.
Indicates that an RPC connection had been made. On receiving this, the client should discard any
references it may still be holding from previous RPC sessions. -/
structure RpcConnected where
uri : DocumentUri
sessionId : UInt64
deriving FromJson, ToJson
/-- `$/lean/rpc/call` client->server request.
A request to execute a procedure bound for RPC. If an incorrect session ID is present, the server
errors with `RpcNeedsReconnect`.
Extending TDPP is weird. But in Lean, symbols exist in the context of a position within a source
file. So we need this to refer to code in the environment at that position. -/
structure RpcCallParams extends TextDocumentPositionParams where
sessionId : UInt64
/-- Procedure to invoke. Must be fully qualified. -/
method : Name
params : Json
deriving FromJson, ToJson
/-- `$/lean/rpc/release` client->server notification.
A notification to release a remote reference. Should be sent by the client when it no longer needs
an `RpcRef` it has previously received from the server. Not doing so is safe but will leak memory. -/
structure RpcReleaseParams where
uri : DocumentUri
sessionId : UInt64
ref : RpcRef
deriving FromJson, ToJson
end Lean.Lsp
|
1f40ee62f680b8ac8826d0deed53925feb9f53e9 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/polynomial/hasse_deriv.lean | 007abde213bad89a0e6010b6132fac2cf9dc37f8 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 10,466 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.polynomial.big_operators
import data.nat.choose.cast
import data.nat.choose.vandermonde
import data.polynomial.degree.lemmas
import data.polynomial.derivative
/-!
# Hasse derivative of polynomials
The `k`th Hasse derivative of a polynomial `∑ a_i X^i` is `∑ (i.choose k) a_i X^(i-k)`.
It is a variant of the usual derivative, and satisfies `k! * (hasse_deriv k f) = derivative^[k] f`.
The main benefit is that is gives an atomic way of talking about expressions such as
`(derivative^[k] f).eval r / k!`, that occur in Taylor expansions, for example.
## Main declarations
In the following, we write `D k` for the `k`-th Hasse derivative `hasse_deriv k`.
* `polynomial.hasse_deriv`: the `k`-th Hasse derivative of a polynomial
* `polynomial.hasse_deriv_zero`: the `0`th Hasse derivative is the identity
* `polynomial.hasse_deriv_one`: the `1`st Hasse derivative is the usual derivative
* `polynomial.factorial_smul_hasse_deriv`: the identity `k! • (D k f) = derivative^[k] f`
* `polynomial.hasse_deriv_comp`: the identity `(D k).comp (D l) = (k+l).choose k • D (k+l)`
* `polynomial.hasse_deriv_mul`:
the "Leibniz rule" `D k (f * g) = ∑ ij in antidiagonal k, D ij.1 f * D ij.2 g`
For the identity principle, see `polynomial.eq_zero_of_hasse_deriv_eq_zero`
in `data/polynomial/taylor.lean`.
## Reference
https://math.fontein.de/2009/08/12/the-hasse-derivative/
-/
noncomputable theory
namespace polynomial
open_locale nat big_operators
open function nat (hiding nsmul_eq_mul)
variables {R : Type*} [semiring R] (k : ℕ) (f : polynomial R)
/-- The `k`th Hasse derivative of a polynomial `∑ a_i X^i` is `∑ (i.choose k) a_i X^(i-k)`.
It satisfies `k! * (hasse_deriv k f) = derivative^[k] f`. -/
def hasse_deriv (k : ℕ) : polynomial R →ₗ[R] polynomial R :=
lsum (λ i, (monomial (i-k)) ∘ₗ distrib_mul_action.to_linear_map R R (i.choose k))
lemma hasse_deriv_apply :
hasse_deriv k f = f.sum (λ i r, monomial (i - k) (↑(i.choose k) * r)) :=
by simpa only [← nsmul_eq_mul]
lemma hasse_deriv_coeff (n : ℕ) :
(hasse_deriv k f).coeff n = (n + k).choose k * f.coeff (n + k) :=
begin
rw [hasse_deriv_apply, coeff_sum, sum_def, finset.sum_eq_single (n + k), coeff_monomial],
{ simp only [if_true, add_tsub_cancel_right, eq_self_iff_true], },
{ intros i hi hink,
rw [coeff_monomial],
by_cases hik : i < k,
{ simp only [nat.choose_eq_zero_of_lt hik, if_t_t, nat.cast_zero, zero_mul], },
{ push_neg at hik, rw if_neg, contrapose! hink,
exact (tsub_eq_iff_eq_add_of_le hik).mp hink, } },
{ intro h, simp only [not_mem_support_iff.mp h, monomial_zero_right, mul_zero, coeff_zero] }
end
lemma hasse_deriv_zero' : hasse_deriv 0 f = f :=
by simp only [hasse_deriv_apply, tsub_zero, nat.choose_zero_right,
nat.cast_one, one_mul, sum_monomial_eq]
@[simp] lemma hasse_deriv_zero : @hasse_deriv R _ 0 = linear_map.id :=
linear_map.ext $ hasse_deriv_zero'
lemma hasse_deriv_eq_zero_of_lt_nat_degree (p : polynomial R) (n : ℕ)
(h : p.nat_degree < n) : hasse_deriv n p = 0 :=
begin
rw [hasse_deriv_apply, sum_def],
refine finset.sum_eq_zero (λ x hx, _),
simp [nat.choose_eq_zero_of_lt ((le_nat_degree_of_mem_supp _ hx).trans_lt h)]
end
lemma hasse_deriv_one' : hasse_deriv 1 f = derivative f :=
by simp only [hasse_deriv_apply, derivative_apply, monomial_eq_C_mul_X, nat.choose_one_right,
(nat.cast_commute _ _).eq]
@[simp] lemma hasse_deriv_one : @hasse_deriv R _ 1 = derivative :=
linear_map.ext $ hasse_deriv_one'
@[simp] lemma hasse_deriv_monomial (n : ℕ) (r : R) :
hasse_deriv k (monomial n r) = monomial (n - k) (↑(n.choose k) * r) :=
begin
ext i,
simp only [hasse_deriv_coeff, coeff_monomial],
by_cases hnik : n = i + k,
{ rw [if_pos hnik, if_pos, ← hnik], apply tsub_eq_of_eq_add_rev, rwa add_comm },
{ rw [if_neg hnik, mul_zero],
by_cases hkn : k ≤ n,
{ rw [← tsub_eq_iff_eq_add_of_le hkn] at hnik, rw [if_neg hnik] },
{ push_neg at hkn, rw [nat.choose_eq_zero_of_lt hkn, nat.cast_zero, zero_mul, if_t_t] } }
end
lemma hasse_deriv_C (r : R) (hk : 0 < k) : hasse_deriv k (C r) = 0 :=
by rw [← monomial_zero_left, hasse_deriv_monomial, nat.choose_eq_zero_of_lt hk,
nat.cast_zero, zero_mul, monomial_zero_right]
lemma hasse_deriv_apply_one (hk : 0 < k) : hasse_deriv k (1 : polynomial R) = 0 :=
by rw [← C_1, hasse_deriv_C k _ hk]
lemma hasse_deriv_X (hk : 1 < k) : hasse_deriv k (X : polynomial R) = 0 :=
by rw [← monomial_one_one_eq_X, hasse_deriv_monomial, nat.choose_eq_zero_of_lt hk,
nat.cast_zero, zero_mul, monomial_zero_right]
lemma factorial_smul_hasse_deriv :
⇑(k! • @hasse_deriv R _ k) = ((@derivative R _)^[k]) :=
begin
induction k with k ih,
{ rw [hasse_deriv_zero, factorial_zero, iterate_zero, one_smul, linear_map.id_coe], },
ext f n : 2,
rw [iterate_succ_apply', ← ih],
simp only [linear_map.smul_apply, coeff_smul, linear_map.map_smul_of_tower, coeff_derivative,
hasse_deriv_coeff, ← @choose_symm_add _ k],
simp only [nsmul_eq_mul, factorial_succ, mul_assoc, succ_eq_add_one, ← add_assoc,
add_right_comm n 1 k, ← cast_succ],
rw ← (cast_commute (n+1) (f.coeff (n + k + 1))).eq,
simp only [← mul_assoc], norm_cast, congr' 2,
apply @cast_injective ℚ,
have h1 : n + 1 ≤ n + k + 1 := succ_le_succ le_self_add,
have h2 : k + 1 ≤ n + k + 1 := succ_le_succ le_add_self,
have H : ∀ (n : ℕ), (n! : ℚ) ≠ 0, { exact_mod_cast factorial_ne_zero },
-- why can't `field_simp` help me here?
simp only [cast_mul, cast_choose ℚ, h1, h2, -one_div, -mul_eq_zero,
succ_sub_succ_eq_sub, add_tsub_cancel_right, add_tsub_cancel_left] with field_simps,
rw [eq_div_iff_mul_eq (mul_ne_zero (H _) (H _)), eq_comm, div_mul_eq_mul_div,
eq_div_iff_mul_eq (mul_ne_zero (H _) (H _))],
norm_cast,
simp only [factorial_succ, succ_eq_add_one], ring,
end
lemma hasse_deriv_comp (k l : ℕ) :
(@hasse_deriv R _ k).comp (hasse_deriv l) = (k+l).choose k • hasse_deriv (k+l) :=
begin
ext i : 2,
simp only [linear_map.smul_apply, comp_app, linear_map.coe_comp, smul_monomial,
hasse_deriv_apply, mul_one, monomial_eq_zero_iff, sum_monomial_index, mul_zero,
← tsub_add_eq_tsub_tsub, add_comm l k],
rw_mod_cast nsmul_eq_mul,
congr' 2,
by_cases hikl : i < k + l,
{ rw [choose_eq_zero_of_lt hikl, mul_zero],
by_cases hil : i < l,
{ rw [choose_eq_zero_of_lt hil, mul_zero] },
{ push_neg at hil, rw [← tsub_lt_iff_right hil] at hikl,
rw [choose_eq_zero_of_lt hikl , zero_mul], }, },
push_neg at hikl, apply @cast_injective ℚ,
have h1 : l ≤ i := nat.le_of_add_le_right hikl,
have h2 : k ≤ i - l := le_tsub_of_add_le_right hikl,
have h3 : k ≤ k + l := le_self_add,
have H : ∀ (n : ℕ), (n! : ℚ) ≠ 0, { exact_mod_cast factorial_ne_zero },
-- why can't `field_simp` help me here?
simp only [cast_mul, cast_choose ℚ, h1, h2, h3, hikl, -one_div, -mul_eq_zero,
succ_sub_succ_eq_sub, add_tsub_cancel_right, add_tsub_cancel_left] with field_simps,
rw [eq_div_iff_mul_eq, eq_comm, div_mul_eq_mul_div, eq_div_iff_mul_eq, ← tsub_add_eq_tsub_tsub,
add_comm l k],
{ ring, },
all_goals { apply_rules [mul_ne_zero, H] }
end
lemma nat_degree_hasse_deriv_le (p : polynomial R) (n : ℕ) :
nat_degree (hasse_deriv n p) ≤ nat_degree p - n :=
begin
classical,
rw [hasse_deriv_apply, sum_def],
refine (nat_degree_sum_le _ _).trans _,
simp_rw [function.comp, nat_degree_monomial],
rw [finset.fold_ite, finset.fold_const],
{ simp only [if_t_t, max_eq_right, zero_le', finset.fold_max_le, true_and, and_imp,
tsub_le_iff_right, mem_support_iff, ne.def, finset.mem_filter],
intros x hx hx',
have hxp : x ≤ p.nat_degree := le_nat_degree_of_ne_zero hx,
have hxn : n ≤ x,
{ contrapose! hx',
simp [nat.choose_eq_zero_of_lt hx'] },
rwa [tsub_add_cancel_of_le (hxn.trans hxp)] },
{ simp }
end
lemma nat_degree_hasse_deriv [no_zero_smul_divisors ℕ R] (p : polynomial R) (n : ℕ) :
nat_degree (hasse_deriv n p) = nat_degree p - n :=
begin
cases lt_or_le p.nat_degree n with hn hn,
{ simpa [hasse_deriv_eq_zero_of_lt_nat_degree, hn] using (tsub_eq_zero_of_le hn.le).symm },
{ refine map_nat_degree_eq_sub _ _,
{ exact λ h, hasse_deriv_eq_zero_of_lt_nat_degree _ _ },
{ classical,
simp only [ite_eq_right_iff, ne.def, nat_degree_monomial, hasse_deriv_monomial],
intros k c c0 hh,
-- this is where we use the `smul_eq_zero` from `no_zero_smul_divisors`
rw [←nsmul_eq_mul, smul_eq_zero, nat.choose_eq_zero_iff] at hh,
exact (tsub_eq_zero_of_le (or.resolve_right hh c0).le).symm } }
end
section
open add_monoid_hom finset.nat
lemma hasse_deriv_mul (f g : polynomial R) :
hasse_deriv k (f * g) = ∑ ij in antidiagonal k, hasse_deriv ij.1 f * hasse_deriv ij.2 g :=
begin
let D := λ k, (@hasse_deriv R _ k).to_add_monoid_hom,
let Φ := @add_monoid_hom.mul (polynomial R) _,
show (comp_hom (D k)).comp Φ f g =
∑ (ij : ℕ × ℕ) in antidiagonal k, ((comp_hom.comp ((comp_hom Φ) (D ij.1))).flip (D ij.2) f) g,
simp only [← finset_sum_apply],
congr' 2, clear f g,
ext m r n s : 4,
simp only [finset_sum_apply, coe_mul_left, coe_comp, flip_apply, comp_app,
hasse_deriv_monomial, linear_map.to_add_monoid_hom_coe, comp_hom_apply_apply, coe_mul,
monomial_mul_monomial],
have aux : ∀ (x : ℕ × ℕ), x ∈ antidiagonal k →
monomial (m - x.1 + (n - x.2)) (↑(m.choose x.1) * r * (↑(n.choose x.2) * s)) =
monomial (m + n - k) (↑(m.choose x.1) * ↑(n.choose x.2) * (r * s)),
{ intros x hx, rw [finset.nat.mem_antidiagonal] at hx, subst hx,
by_cases hm : m < x.1,
{ simp only [nat.choose_eq_zero_of_lt hm, nat.cast_zero, zero_mul, monomial_zero_right], },
by_cases hn : n < x.2,
{ simp only [nat.choose_eq_zero_of_lt hn, nat.cast_zero,
zero_mul, mul_zero, monomial_zero_right], },
push_neg at hm hn,
rw [tsub_add_eq_add_tsub hm, ← add_tsub_assoc_of_le hn, ← tsub_add_eq_tsub_tsub,
add_comm x.2 x.1, mul_assoc, ← mul_assoc r, ← (nat.cast_commute _ r).eq, mul_assoc,
mul_assoc], },
conv_rhs { apply_congr, skip, rw aux _ H, },
rw_mod_cast [← linear_map.map_sum, ← finset.sum_mul, ← nat.add_choose_eq],
end
end
end polynomial
|
74f48fde511e74094967e9c9e16cb0ce1825afb0 | d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6 | /Papyrus/Script/Do.lean | 99d25ef302912807a49b0734298e378a44534207 | [
"Apache-2.0"
] | permissive | xubaiw/lean4-papyrus | c3fbbf8ba162eb5f210155ae4e20feb2d32c8182 | 02e82973a5badda26fc0f9fd15b3d37e2eb309e0 | refs/heads/master | 1,691,425,756,824 | 1,632,122,825,000 | 1,632,123,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,716 | lean | import Lean.Parser
namespace Papyrus.Script
open Lean Parser
-- # Module Do
declare_syntax_cat modDoElem (behavior := symbol)
def modDoElemParser (rbp : Nat := 0) := categoryParser `modDoElem rbp
def modDoSeqItem := leading_parser ppLine >> modDoElemParser >> Parser.optional "; "
def modDoSeqIndent := leading_parser many1Indent modDoSeqItem
def modDoSeqBracketed := leading_parser "{" >> withoutPosition (many1 modDoSeqItem) >> ppLine >> "}"
def modDoSeq := modDoSeqIndent <|> modDoSeqBracketed
attribute [runParserAttributeHooks]
modDoElemParser modDoSeqItem modDoSeqItem modDoSeqBracketed modDoSeq
def expandModDoSeq : Syntax → MacroM (Array Syntax)
| `(modDoSeq| { $[$elems:modDoElem]* }) => elems
| `(modDoSeq| $[$elems:modDoElem $[;]?]*) => elems
| stx => Macro.throwErrorAt stx "ill-formed module do sequence"
-- # Basic Block Do
declare_syntax_cat bbDoElem (behavior := symbol)
def bbDoElemParser (rbp : Nat := 0) := categoryParser `bbDoElem rbp
def bbDoSeqItem := leading_parser ppLine >> bbDoElemParser >> Parser.optional "; "
def bbDoSeqIndent := leading_parser many1Indent bbDoSeqItem
def bbDoSeqBracketed := leading_parser "{" >> withoutPosition (many1 bbDoSeqItem) >> ppLine >> "}"
def bbDoSeq := bbDoSeqIndent <|> bbDoSeqBracketed
attribute [runParserAttributeHooks]
bbDoElemParser bbDoSeqItem bbDoSeqIndent bbDoSeqBracketed bbDoSeq
def expandBbDoSeq : Syntax → MacroM (Array Syntax)
| `(bbDoSeq| { $[$elems:bbDoElem]* }) => elems
| `(bbDoSeq| $[$elems:bbDoElem $[;]?]*) => elems
| stx => Macro.throwErrorAt stx "ill-formed basic block do sequence"
-- # Nesting Lean Do Elements
macro (priority := low) x:doElem : modDoElem => x
macro (priority := low) x:doElem : bbDoElem => x
|
892fae589474ede147c7af549f2cc7c7b7e5d057 | 17d3c61bf162bf88be633867ed4cb201378a8769 | /tests/lean/vm_let_expr.lean | 94001f0d869073c331f664358ad7ac047fe53bf2 | [
"Apache-2.0"
] | permissive | u20024804/lean | 11def01468fb4796fb0da76015855adceac7e311 | d315e424ff17faf6fe096a0a1407b70193009726 | refs/heads/master | 1,611,388,567,561 | 1,485,836,506,000 | 1,485,836,625,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 249 | lean | meta def mk_value (n : nat) : nat :=
trace "mk_value" (λ _, 2 * n)
meta def mk_fn (sz : nat) : nat → nat :=
let n := mk_value sz in
λ x, x + n
vm_eval let f := mk_fn 10 in f 1 + f 2 + f 3 + f 4
vm_eval ((let x := mk_value 10 in mk_fn x) 10)
|
43c8353fc23daf8ac9cb9c190eb34be784c3663e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/order/invertible.lean | d862f4ae21ef8da32fb4f22cabc9ea3dceb8e2c4 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 1,241 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import algebra.order.ring.defs
import algebra.invertible
/-!
# Lemmas about `inv_of` in ordered (semi)rings.
-/
variables {α : Type*} [linear_ordered_semiring α] {a : α}
@[simp] lemma inv_of_pos [invertible a] : 0 < ⅟a ↔ 0 < a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, pos_of_mul_pos_left this h.le, λ h, pos_of_mul_pos_right this h.le⟩
end
@[simp] lemma inv_of_nonpos [invertible a] : ⅟a ≤ 0 ↔ a ≤ 0 :=
by simp only [← not_lt, inv_of_pos]
@[simp] lemma inv_of_nonneg [invertible a] : 0 ≤ ⅟a ↔ 0 ≤ a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, (pos_of_mul_pos_left this h).le, λ h, (pos_of_mul_pos_right this h).le⟩
end
@[simp] lemma inv_of_lt_zero [invertible a] : ⅟a < 0 ↔ a < 0 :=
by simp only [← not_le, inv_of_nonneg]
@[simp] lemma inv_of_le_one [invertible a] (h : 1 ≤ a) : ⅟a ≤ 1 :=
by haveI := @linear_order.decidable_le α _; exact
mul_inv_of_self a ▸ le_mul_of_one_le_left (inv_of_nonneg.2 $ zero_le_one.trans h) h
|
39bc95093bcfd44878c607311ef49f9902040a3b | de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41 | /old/expressions/axis_orientation.lean | d02db9cca4555719e7b8a98e9cab58f2436cb886 | [] | no_license | kevinsullivan/lang | d3e526ba363dc1ddf5ff1c2f36607d7f891806a7 | e9d869bff94fb13ad9262222a6f3c4aafba82d5e | refs/heads/master | 1,687,840,064,795 | 1,628,047,969,000 | 1,628,047,969,000 | 282,210,749 | 0 | 1 | null | 1,608,153,830,000 | 1,595,592,637,000 | Lean | UTF-8 | Lean | false | false | 745 | lean | import ...phys.metrology.axis_orientation
namespace lang.axisOrientation
open orientation
structure var : Type :=
mk :: (num : ℕ)
structure orientationVar extends var
inductive orientationExpr
| lit (v : AxisOrientation 3)
| var (v : orientationVar)
abbreviation orientationEnv := orientationVar → AxisOrientation 3
abbreviation orientationEval := orientationExpr → orientationEnv → AxisOrientation 3
structure env : Type :=
(or : orientationEnv)
def orientationVarEq : orientationVar → orientationVar → bool
| v1 v2 := v1.num=v2.num
--(fr : frameEnv sp)
--(vec : vectorEnv sp)
--(pt : pointEnv sp)
noncomputable def init := λ v : orientationVar, NWU
noncomputable def initEnv : env := ⟨init⟩
end lang.axisOrientation |
302cc443fd6d72884ff5995feaeb1cd0a951bc37 | b29f946a2f0afd23ef86b9219116968babbb9f4f | /src/limits.lean | e981755ad7df7502afe6f34afc8579e2071b95ce | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M1P1-lean | 58be7394fded719d95e45e6b10e1ecf2ed3c7c4c | 3723468cc50f8bebd00a9811caf25224a578de17 | refs/heads/master | 1,587,063,867,779 | 1,572,727,164,000 | 1,572,727,164,000 | 165,845,802 | 14 | 4 | Apache-2.0 | 1,549,730,698,000 | 1,547,554,675,000 | Lean | UTF-8 | Lean | false | false | 14,713 | lean | /- limits.lean
Limits of sequences.
Part of the M1P1 Lean project.
This file contains the basic definition of the limit of a sequence, and
proves basic properties about it.
It is full of comments in an attempt to make it more comprehensible to
mathematicians with little Lean experience, although by far the best
way to understand what is going on is to open the file within Lean 3.4.2
so you can check out the goal at each line -- this really helps understanding.
-/
-- The import below gives us a working copy of the real numbers ℝ,
-- and functions such as abs : ℝ → ℝ
import data.real.basic
-- This import has addition of functions, which we need for sums of limits.
import algebra.pi_instances
-- This next import gives us several tactics of use to mathematicians:
-- (1) norm_num [to prove basic facts about reals like 2+2 = 4]
-- (2) ring [to prove basic algebra identities like (a+b)^2 = a^2+2ab+b^2]
-- (3) linarith [to prove basic inequalities like x > 0 -> x/2 > 0]
import tactic.linarith
--import topology.basic
-- Ken Lee wanted to import this
--import analysis.exponential
-- These lines switch Lean into "maths proof mode" -- don't worry about them.
-- Basically they tell Lean to use the axiom of choice and the
-- law of the excluded middle, two standard maths facts which we
-- assume all the time in maths, usually without comment.
-- This imports Patrick's extension obv_ineq of linarith which solves
-- "obvious inequalities", and maybe other things too.
import xenalib.M1P1
noncomputable theory
local attribute [instance, priority 0] classical.prop_decidable
-- Let's also put things into an M1P1 namespace so we can define
-- stuff which is already defined in mathlib without breaking anything.
namespace M1P1
-- the maths starts here.
-- We introduce the usual mathematical notation for absolute value
notation `|` x `|` := abs x
-- We model a sequence a₀, a₁, a₂,... of real numbers as a function
-- from ℕ := {0,1,2,...} to ℝ, sending n to aₙ . So in the below
-- definition of the limit of a sequence, a : ℕ → ℝ is the sequence.
-- We first formalise the definition of "aₙ → l as n → ∞"
definition is_limit (a : ℕ → ℝ) (l : ℝ) : Prop :=
∀ ε > 0, ∃ N, ∀ n ≥ N, | a n - l | < ε
-- A sequence converges if and only if it has a limit. The difference
-- with this definition is that we don't specify the limit, we just
-- claim that it exists.
definition has_limit (a : ℕ → ℝ) : Prop := ∃ l : ℝ, is_limit a l
lemma tendsto_const (a : ℝ) : is_limit (λ n, a) a :=
begin
intros ε εpos,
use 0,
intros n _,
simpa using εpos
end
local attribute [-simp] sub_eq_add_neg
local attribute [simp] sub_zero
-- We will need an easy reformulation of the limit definition
lemma tendsto_iff_sub_tendsto_zero {a : ℕ → ℝ} {l : ℝ} :
is_limit (λ n, a n - l) 0 ↔ is_limit a l :=
begin
split ;
{ intros h ε εpos,
rcases h ε εpos with ⟨N, H⟩,
use N,
intros n hn,
simpa using H n hn }
end
-- In the definition of a limit, the final ε can be replaced
-- by a constant multiple of ε. We could assume this constant is positive
-- but we don't want to deal with this when applying the lemma.
lemma tendsto_of_mul_eps (a : ℕ → ℝ) (l : ℝ) (A : ℝ)
(h : ∀ ε > 0, ∃ N, ∀ n ≥ N, | a n - l | < A*ε) : is_limit a l :=
begin
-- Let ε be any positive number
intros ε εpos,
-- A is either non positive or positive
cases le_or_gt A 0 with Anonpos Apos,
{ -- If A is non positive then our assumed bound quickly
-- gives a contradiction.
exfalso,
-- Indeed we can apply our assumption to ε = 1 to get N such that
-- ∀ (n : ℕ), n ≥ N → |a n - l| < A * 1
rcases h 1 (by linarith) with ⟨N, H⟩,
-- in particular this holds when n = N
specialize H N (by linarith),
-- but |a N - l| ≥ 0 so we get a contradiction
have : |a N - l| ≥ 0, from abs_nonneg _,
linarith },
{ -- Now assume A is positive. Our assumption h gives N such that
-- ∀ n ≥ N, |a n - l| < A * (ε / A)
rcases h (ε/A) (div_pos εpos Apos) with ⟨N, H⟩,
-- we can simplify that A * (ε / A) and we are done.
rw mul_div_cancel' _ (ne_of_gt Apos) at H,
tauto }
end
-- We now start on the proof of the theorem that if a sequence has
-- two limits, they are equal.
-- Next lemma could be either hidden of given a user-friendly proof
lemma zero_of_abs_lt_all (x : ℝ) (h : ∀ ε > 0, |x| < ε) : x = 0 :=
eq_zero_of_abs_eq_zero $ eq_of_le_of_forall_le_of_dense (abs_nonneg x) $ λ ε ε_pos, le_of_lt (h ε ε_pos)
-- We're ready to prove the theorem.
theorem limits_are_unique (a : ℕ → ℝ) (l m : ℝ) (hl : is_limit a l)
(hm : is_limit a m) : l = m :=
begin
-- Let prove |l - m| is smaller than any positive number, since that will easily imply l = m
suffices : ∀ ε : ℝ, ε > 0 → |l - m| < ε,
from eq_of_sub_eq_zero (zero_of_abs_lt_all _ this),
-- Let ε be any positive number, and let's prove |l - m| < ε
intros ε ε_pos,
-- Because aₙ → l, there exists Nₗ such that n ≥ Nₗ → |aₙ - l| < ε/2
cases hl (ε/2) (by obvious_ineq) with Nₗ Hₗ,
-- Because aₙ → m, there exists Nₘ such that n ≥ Nₘ → |aₙ - m| < ε/2
cases hm (ε/2) (by obvious_ineq) with Nₘ Hₘ,
-- The trick is to let N be the max of Nₗ and Nₘ
let N := max Nₗ Nₘ,
-- Now clearly N ≥ Nₗ...
have H₁ : Nₗ ≤ N := by obvious_ineq,
-- ... so |a_N - l| < ε/2
have H : | a N - l| < ε/2 := Hₗ N H₁,
-- similarly N ≥ Nₘ...
have H₂ : Nₘ ≤ N := by obvious_ineq,
-- ... so |a_N - m| < ε/2 too
have H' : | a N - m| < ε/2 := Hₘ N H₂,
-- We now combine
calc
|l - m| = |(l - a N) + (a N - m)| : by ring
... ≤ |l - a N| + |a N - m| : abs_add _ _
... = |a N - l | + |a N - m| : by rw abs_sub
... < ε/2 + ε/2 : by obvious_ineq
... = ε : by ring,
end
-- We now prove that if aₙ → l and bₙ → m then aₙ + bₙ → l + m.
theorem tendsto_add (a : ℕ → ℝ) (b : ℕ → ℝ) (l m : ℝ)
(h1 : is_limit a l) (h2 : is_limit b m) :
is_limit (a + b) (l + m) :=
begin
apply tendsto_of_mul_eps,
-- let epsilon be a positive real
intros ε Hε,
-- Choose large M₁ such that n ≥ M₁ implies |a n - l| < ε
cases (h1 ε Hε) with M₁ HM₁,
-- similarly choose M₂ for the b sequence.
cases (h2 ε Hε) with M₂ HM₂,
-- let N be the max of M1 and M2
let N := max M₁ M₂,
-- and let's use that
use N,
-- Of course N ≥ M₁ and N ≥ M₂
have H₁ : N ≥ M₁ := by obvious_ineq,
have H₂ : N ≥ M₂ := by obvious_ineq,
-- Now say n ≥ N.
intros n Hn,
-- Then obviously n ≥ M₁...
have Hn₁ : n ≥ M₁ := by linarith,
-- ...so |aₙ - l| < ε
have H3 : |a n - l| < ε := HM₁ n Hn₁,
-- and similarly n ≥ M₂, so |bₙ - l| < ε
have H4 : |b n - m| < ε := HM₂ n (by linarith),
-- And now we can estimate (|aₙ + bₙ - (l + m)| < 2ε)
-- First do some obvious algebra
calc |(a + b) n - (l + m)| = |a n + b n - (l + m)| : rfl
... = |(a n - l) + (b n - m)| : by congr' 1; ring
-- now use the triangle inequality
... ≤ |(a n - l)| + |(b n - m)| : abs_add _ _
-- and our assumptions
... < ε + ε : by linarith
-- and a bit more algebra
... = 2*ε : by ring
end
-- A sequence (aₙ) is *bounded* if there exists some real number B such that |aₙ| ≤ B for all n≥0.
definition has_bound (a : ℕ → ℝ) (B : ℝ) := ∀ n, |a n| ≤ B
definition is_bounded (a : ℕ → ℝ) := ∃ B, has_bound a B
-- We really have a problem with that |.| notation
-- The following lemma is obvious
lemma has_bound_const (m : ℝ): has_bound (λ n, m) $ |m| :=
assume n, by simp
-- A convergent sequence is bounded.
open finset
theorem bounded_of_convergent (a : ℕ → ℝ) (Ha : has_limit a) : is_bounded a :=
begin
-- let l be the limit of the sequence.
cases Ha with l Hl,
-- By definition, there exist some N such that n ≥ N → |aₙ - l| < 1
cases Hl 1 (zero_lt_one) with N HN,
-- Let X be {|a₀|, |a₁|, ... , |a_N|}...
let X := image (abs ∘ a) (range (N + 1)),
-- ...let's remark that |a₀| ∈ X so X ≠ ∅ while we're here...
have H2 : |a 0| ∈ X := mem_image_of_mem _ (mem_range.2 (nat.zero_lt_succ _)),
have H3 : X ≠ ∅ := ne_empty_of_mem H2,
-- ...and let B₀ be the max of X.
let B₀ := max' X H3,
-- If n ≤ N then |aₙ| ≤ B₀.
have HB₀ : ∀ n ≤ N, |a n| ≤ B₀ := λ n Hn, le_max' X H3 _
(mem_image_of_mem _ (mem_range.2 (nat.lt_succ_of_le Hn))),
-- So now let B = max {B₀, |l| + 1}
let B := max B₀ ( |l| + 1),
-- and we claim this bound works, i.e. |aₙ| ≤ B for all n ∈ ℕ.
use B,
-- Because if n ∈ ℕ,
intro n,
-- then either n ≤ N or n > N.
cases le_or_gt n N with Hle Hgt,
{ -- if n ≤ N, then |aₙ| ≤ B₀
have h : |a n| ≤ B₀ := HB₀ n Hle,
-- and B₀ ≤ B
have h2 : B₀ ≤ B := le_max_left _ _,
-- so we're done
linarith },
{ -- and if n > N, then |aₙ - l| < 1...
have h : |a n - l| < 1 := HN n (le_of_lt Hgt),
-- ...so |aₙ| < 1 + |l|...
have h2 : |a n| < |l| + 1,
-- todo (kmb) -- remove linarith bug workaround
revert h,unfold abs,unfold max,split_ifs;intros;linarith {restrict_type := ℝ},
-- ...which is ≤ B
have h3 : |l| + 1 ≤ B := le_max_right _ _,
-- ...so we're done in this case too
linarith
}
end
-- more convenient theorem: a sequence with a limit
-- is bounded in absolute value by a positive real.
theorem bounded_pos_of_convergent (a : ℕ → ℝ) (Ha : has_limit a) :
∃ B : ℝ, B > 0 ∧ has_bound a B :=
begin
-- We know the sequence is bounded. Say it's bounded by B₀.
cases bounded_of_convergent a Ha with B₀ HB₀,
-- let B = |B₀| + 1; this bound works.
let B := |B₀| + 1,
use B,
-- B is obviously positive
split,
{ -- (because 1 > 0...
apply lt_of_lt_of_le zero_lt_one,
show 1 ≤ |B₀| + 1,
apply le_add_of_nonneg_left',
-- ... and |B₀| ≥ 0)
exact abs_nonneg _
},
-- so it suffices to prove B is a bound.
-- If n is a natural
intro n,
-- then |aₙ| ≤ B₀
apply le_trans (HB₀ n),
-- so it suffices to show B₀ ≤ |B₀| + 1
show B₀ ≤ |B₀| + 1,
-- which is obvious.
apply le_trans (le_abs_self B₀),
simp [zero_le_one],
end
lemma tendsto_bounded_mul_zero {a : ℕ → ℝ} {b : ℕ → ℝ} {A : ℝ} (Apos : A > 0)
(hA : has_bound a A) (hB : is_limit b 0)
: is_limit (a*b) 0 :=
begin
-- Let's apply our variant of the definition of limits where the final
-- ε gets multiplied by some constant to be determined
apply tendsto_of_mul_eps,
-- Let ε be any positive number
intros ε εpos,
-- by assumption hB, we get some N such that
-- ∀ (n : ℕ), n ≥ N → |b n| < ε
cases hB ε εpos with N H,
simp at H,
-- Let's use that N
use N,
-- And compute for any n ≥ N
intros n nN,
calc
|(a * b) n - 0| = |a n * b n| : by simp
... = |a n| * |b n| : abs_mul _ _
... ≤ A*|b n| : mul_le_mul_of_nonneg_right (hA n) (abs_nonneg _)
... < A*ε : mul_lt_mul_of_pos_left (H n nN) Apos
end
-- The limit of the product is the product of the limits.
-- If aₙ → l and bₙ → m then aₙ * bₙ → l * m.
theorem tendsto_mul (a : ℕ → ℝ) (b : ℕ → ℝ) (l m : ℝ)
(h1 : is_limit a l) (h2 : is_limit b m) :
is_limit (a * b) (l * m) :=
begin
-- We apply the difference criterion so we need to prove a*b - l*m goes to zero
rw ←tendsto_iff_sub_tendsto_zero,
-- The key idea is to introduce (a_n - l) and (b_n - m) in this difference
have key : (λ n, (a*b) n - l*m) = (λ n, (a n)*(b n - m) + m*(a n - l)),
simp, ext, ring,
rw key,
-- By addition of limit, it then suffices to prove a_n * (b_n - m) and m*(a_n - l)
-- both go to zero
suffices : is_limit (λ n, a n * (b n - m)) 0 ∧ is_limit (λ n, m * (a n - l)) 0,
{ rw [show (0 : ℝ) = 0 + 0, by simp],
exact tendsto_add _ _ _ _ this.left this.right},
-- Let's tacke one after the other
split,
{ -- Since a is convergent, it's bounded by some positive A
rcases bounded_pos_of_convergent a ⟨l, h1⟩ with ⟨A, A_pos, hA⟩,
-- We can reformulate the b convergence assumption as b_n - m goes to zero.
have limb : is_limit (λ n, b n - m) 0,
from tendsto_iff_sub_tendsto_zero.2 h2,
-- So we can conclude using our lemma about product of a bounded sequence and a
-- sequence converging to zero
exact tendsto_bounded_mul_zero A_pos hA limb },
{ -- It remains to prove m * (a_n - l) goes to zero
-- If m = 0 this is obvious.
by_cases Hm : m = 0,
{ simp [Hm, tendsto_const] },
-- Otherwise we follow the same strategy as above.
{ -- We reformulate our convergence assumption on a as a_n - l goes to zero
have lima : is_limit (λ n, a n - l) 0,
from tendsto_iff_sub_tendsto_zero.2 h1,
-- and conclude using the same lemma
exact tendsto_bounded_mul_zero (abs_pos_iff.2 Hm) (has_bound_const m) lima } }
end
-- If aₙ → l and bₙ → m, and aₙ ≤ bₙ for all n, then l ≤ m
theorem tendsto_le_of_le (a : ℕ → ℝ) (b : ℕ → ℝ)
(l : ℝ) (m : ℝ) (hl : is_limit a l) (hm : is_limit b m)
(hle : ∀ n, a n ≤ b n) : l ≤ m :=
begin
-- Assume for a contradiction that m < l
apply le_of_not_lt,
intro hlt,
-- Let ε be (l - m) / 2...
let ε := (l - m) /2,
-- ...and note that it's positive
have Hε : ε > 0 := show (l - m) / 2 > 0 , by linarith,
-- Choose Na s.t. |aₙ - l| < ε for n ≥ Na
cases hl ε Hε with Na HNa,
have Hε : ε > 0 := show (l - m) / 2 > 0 , by linarith,
-- Choose Nb s.t. |bₙ - m| < ε for n ≥ Nb
cases hm ε Hε with Nb HNb,
-- let N be the max of Na and Nb...
let N := max Na Nb,
-- ...and note N ≥ Na and N ≥ Nb,
have HNa' : Na ≤ N := le_max_left _ _,
have HNb' : Nb ≤ N := le_max_right _ _,
-- ... so |a_N - l| < ε and |b_N - m| < ε
have Hl' : |a N - l| < ε := HNa N HNa',
have Hm' : |b N - m| < ε := HNb N HNb',
-- ... and also a_N ≤ b_N.
have HN : a N ≤ b N := hle N,
-- This is probably a contradiction.
have Hε : ε = (l - m) / 2 := rfl,
revert Hl' Hm',
unfold abs,unfold max,split_ifs;intros;linarith
end
end M1P1
|
7016b26fbf11ce891b74e9f8b20cc9be57c9ca07 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/ring_theory/algebraic.lean | fdb0c04e1ec27b55c75b17b203389447abe67484 | [
"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 | 17,637 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import linear_algebra.finite_dimensional
import ring_theory.integral_closure
import data.polynomial.integral_normalization
/-!
# Algebraic elements and algebraic extensions
An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial.
An R-algebra is algebraic over R if and only if all its elements are algebraic over R.
The main result in this file proves transitivity of algebraicity:
a tower of algebraic field extensions is algebraic.
-/
universes u v w
open_locale classical polynomial
open polynomial
section
variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A]
/-- An element of an R-algebra is algebraic over R if it is a root of a nonzero polynomial
with coefficients in R. -/
def is_algebraic (x : A) : Prop :=
∃ p : R[X], p ≠ 0 ∧ aeval x p = 0
/-- An element of an R-algebra is transcendental over R if it is not algebraic over R. -/
def transcendental (x : A) : Prop := ¬ is_algebraic R x
lemma is_transcendental_of_subsingleton [subsingleton R] (x : A) : transcendental R x :=
λ ⟨p, h, _⟩, h $ subsingleton.elim p 0
variables {R}
/-- A subalgebra is algebraic if all its elements are algebraic. -/
def subalgebra.is_algebraic (S : subalgebra R A) : Prop := ∀ x ∈ S, is_algebraic R x
variables (R A)
/-- An algebra is algebraic if all its elements are algebraic. -/
def algebra.is_algebraic : Prop := ∀ x : A, is_algebraic R x
variables {R A}
/-- A subalgebra is algebraic if and only if it is algebraic as an algebra. -/
lemma subalgebra.is_algebraic_iff (S : subalgebra R A) :
S.is_algebraic ↔ @algebra.is_algebraic R S _ _ S.algebra :=
begin
delta algebra.is_algebraic subalgebra.is_algebraic,
rw subtype.forall',
refine forall_congr (λ x, exists_congr (λ p, and_congr iff.rfl _)),
have h : function.injective S.val := subtype.val_injective,
conv_rhs { rw [← h.eq_iff, alg_hom.map_zero] },
rw [← aeval_alg_hom_apply, S.val_apply]
end
/-- An algebra is algebraic if and only if it is algebraic as a subalgebra. -/
lemma algebra.is_algebraic_iff : algebra.is_algebraic R A ↔ (⊤ : subalgebra R A).is_algebraic :=
begin
delta algebra.is_algebraic subalgebra.is_algebraic,
simp only [algebra.mem_top, forall_prop_of_true, iff_self],
end
lemma is_algebraic_iff_not_injective {x : A} : is_algebraic R x ↔
¬ function.injective (polynomial.aeval x : R[X] →ₐ[R] A) :=
by simp only [is_algebraic, injective_iff_map_eq_zero, not_forall, and.comm, exists_prop]
end
section zero_ne_one
variables (R : Type u) {S : Type*} {A : Type v} [comm_ring R]
variables [comm_ring S] [ring A] [algebra R A] [algebra R S] [algebra S A]
variables [is_scalar_tower R S A]
/-- An integral element of an algebra is algebraic.-/
lemma is_integral.is_algebraic [nontrivial R] {x : A} : is_integral R x → is_algebraic R x :=
λ ⟨p, hp, hpx⟩, ⟨p, hp.ne_zero, hpx⟩
variables {R}
lemma is_algebraic_zero [nontrivial R] : is_algebraic R (0 : A) :=
⟨_, X_ne_zero, aeval_X 0⟩
/-- An element of `R` is algebraic, when viewed as an element of the `R`-algebra `A`. -/
lemma is_algebraic_algebra_map [nontrivial R] (x : R) : is_algebraic R (algebra_map R A x) :=
⟨_, X_sub_C_ne_zero x, by rw [_root_.map_sub, aeval_X, aeval_C, sub_self]⟩
lemma is_algebraic_one [nontrivial R] : is_algebraic R (1 : A) :=
by { rw ←_root_.map_one _, exact is_algebraic_algebra_map 1 }
lemma is_algebraic_nat [nontrivial R] (n : ℕ) : is_algebraic R (n : A) :=
by { rw ←map_nat_cast _, exact is_algebraic_algebra_map n }
lemma is_algebraic_int [nontrivial R] (n : ℤ) : is_algebraic R (n : A) :=
by { rw ←_root_.map_int_cast (algebra_map R A), exact is_algebraic_algebra_map n }
lemma is_algebraic_rat (R : Type u) {A : Type v} [division_ring A] [field R] [algebra R A] (n : ℚ) :
is_algebraic R (n : A) :=
by { rw ←map_rat_cast (algebra_map R A), exact is_algebraic_algebra_map n }
lemma is_algebraic_of_mem_root_set {R : Type u} {A : Type v} [field R] [field A] [algebra R A]
{p : R[X]} {x : A} (hx : x ∈ p.root_set A) : is_algebraic R x :=
⟨p, ne_zero_of_mem_root_set hx, aeval_eq_zero_of_mem_root_set hx⟩
open is_scalar_tower
lemma is_algebraic_algebra_map_of_is_algebraic {a : S} :
is_algebraic R a → is_algebraic R (algebra_map S A a) :=
λ ⟨f, hf₁, hf₂⟩, ⟨f, hf₁, by rw [←algebra_map_aeval, hf₂, map_zero]⟩
/-- This is slightly more general than `is_algebraic_algebra_map_of_is_algebraic` in that it
allows noncommutative intermediate rings `A`. -/
lemma is_algebraic_alg_hom_of_is_algebraic {B} [ring B] [algebra R B]
(f : A →ₐ[R] B) {a : A} (h : is_algebraic R a) : is_algebraic R (f a) :=
let ⟨p, hp, ha⟩ := h in ⟨p, hp, by rw [aeval_alg_hom, f.comp_apply, ha, map_zero]⟩
/-- Transfer `algebra.is_algebraic` across an `alg_equiv`. -/
lemma _root_.alg_equiv.is_algebraic {B} [ring B] [algebra R B] (e : A ≃ₐ[R] B)
(h : algebra.is_algebraic R A) : algebra.is_algebraic R B :=
λ b, by convert ← is_algebraic_alg_hom_of_is_algebraic e.to_alg_hom (h _); apply e.apply_symm_apply
lemma _root_.alg_equiv.is_algebraic_iff {B} [ring B] [algebra R B] (e : A ≃ₐ[R] B) :
algebra.is_algebraic R A ↔ algebra.is_algebraic R B := ⟨e.is_algebraic, e.symm.is_algebraic⟩
lemma is_algebraic_algebra_map_iff {a : S} (h : function.injective (algebra_map S A)) :
is_algebraic R (algebra_map S A a) ↔ is_algebraic R a :=
⟨λ ⟨p, hp0, hp⟩, ⟨p, hp0, h (by rwa [map_zero, algebra_map_aeval])⟩,
is_algebraic_algebra_map_of_is_algebraic⟩
lemma is_algebraic_of_pow {r : A} {n : ℕ} (hn : 0 < n) (ht : is_algebraic R (r ^ n)) :
is_algebraic R r :=
begin
obtain ⟨p, p_nonzero, hp⟩ := ht,
refine ⟨polynomial.expand _ n p, _, _⟩,
{ rwa polynomial.expand_ne_zero hn },
{ rwa polynomial.expand_aeval n p r },
end
lemma transcendental.pow {r : A} (ht : transcendental R r) {n : ℕ} (hn : 0 < n) :
transcendental R (r ^ n) :=
λ ht', ht $ is_algebraic_of_pow hn ht'
end zero_ne_one
section field
variables {K : Type u} {A : Type v} [field K] [ring A] [algebra K A]
/-- An element of an algebra over a field is algebraic if and only if it is integral.-/
lemma is_algebraic_iff_is_integral {x : A} :
is_algebraic K x ↔ is_integral K x :=
begin
refine ⟨_, is_integral.is_algebraic K⟩,
rintro ⟨p, hp, hpx⟩,
refine ⟨_, monic_mul_leading_coeff_inv hp, _⟩,
rw [← aeval_def, alg_hom.map_mul, hpx, zero_mul],
end
protected lemma algebra.is_algebraic_iff_is_integral :
algebra.is_algebraic K A ↔ algebra.is_integral K A :=
⟨λ h x, is_algebraic_iff_is_integral.mp (h x),
λ h x, is_algebraic_iff_is_integral.mpr (h x)⟩
end field
namespace algebra
variables {K : Type*} {L : Type*} {R : Type*} {S : Type*} {A : Type*}
variables [field K] [field L] [comm_ring R] [comm_ring S] [comm_ring A]
variables [algebra K L] [algebra L A] [algebra K A] [is_scalar_tower K L A]
variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A]
/-- If L is an algebraic field extension of K and A is an algebraic algebra over L,
then A is algebraic over K. -/
lemma is_algebraic_trans (L_alg : is_algebraic K L) (A_alg : is_algebraic L A) :
is_algebraic K A :=
begin
simp only [is_algebraic, is_algebraic_iff_is_integral] at L_alg A_alg ⊢,
exact is_integral_trans L_alg A_alg,
end
variables (K L)
/-- If x is algebraic over R, then x is algebraic over S when S is an extension of R,
and the map from `R` to `S` is injective. -/
lemma _root_.is_algebraic_of_larger_base_of_injective (hinj : function.injective (algebra_map R S))
{x : A} (A_alg : _root_.is_algebraic R x) : _root_.is_algebraic S x :=
let ⟨p, hp₁, hp₂⟩ := A_alg in
⟨p.map (algebra_map _ _),
by rwa [ne.def, ← degree_eq_bot, degree_map_eq_of_injective hinj, degree_eq_bot],
by simpa⟩
/-- If A is an algebraic algebra over R, then A is algebraic over S when S is an extension of R,
and the map from `R` to `S` is injective. -/
lemma is_algebraic_of_larger_base_of_injective (hinj : function.injective (algebra_map R S))
(A_alg : is_algebraic R A) : is_algebraic S A :=
λ x, is_algebraic_of_larger_base_of_injective hinj (A_alg x)
/-- If x is a algebraic over K, then x is algebraic over L when L is an extension of K -/
lemma _root_.is_algebraic_of_larger_base {x : A} (A_alg : _root_.is_algebraic K x) :
_root_.is_algebraic L x :=
_root_.is_algebraic_of_larger_base_of_injective (algebra_map K L).injective A_alg
/-- If A is an algebraic algebra over K, then A is algebraic over L when L is an extension of K -/
lemma is_algebraic_of_larger_base (A_alg : is_algebraic K A) : is_algebraic L A :=
is_algebraic_of_larger_base_of_injective (algebra_map K L).injective A_alg
variables (K L)
/-- A field extension is integral if it is finite. -/
lemma is_integral_of_finite [finite_dimensional K L] : algebra.is_integral K L :=
λ x, is_integral_of_submodule_noetherian ⊤
(is_noetherian.iff_fg.2 infer_instance) x algebra.mem_top
/-- A field extension is algebraic if it is finite. -/
lemma is_algebraic_of_finite [finite : finite_dimensional K L] : is_algebraic K L :=
algebra.is_algebraic_iff_is_integral.mpr (is_integral_of_finite K L)
variables {K L}
theorem is_algebraic.alg_hom_bijective
(ha : algebra.is_algebraic K L) (f : L →ₐ[K] L) : function.bijective f :=
begin
refine ⟨f.to_ring_hom.injective, λ b, _⟩,
obtain ⟨p, hp, he⟩ := ha b,
let f' : p.root_set L → p.root_set L :=
set.maps_to.restrict f _ _ (root_set_maps_to (map_ne_zero hp) f),
have : function.surjective f' := finite.injective_iff_surjective.1
(λ _ _ h, subtype.eq $ f.to_ring_hom.injective $ subtype.ext_iff.1 h),
obtain ⟨a, ha⟩ := this ⟨b, (mem_root_set_iff hp b).2 he⟩,
exact ⟨a, subtype.ext_iff.1 ha⟩,
end
theorem _root_.alg_hom.bijective [finite_dimensional K L] (ϕ : L →ₐ[K] L) : function.bijective ϕ :=
(algebra.is_algebraic_of_finite K L).alg_hom_bijective ϕ
variables (K L)
/-- Bijection between algebra equivalences and algebra homomorphisms -/
@[simps] noncomputable
def is_algebraic.alg_equiv_equiv_alg_hom
(ha : algebra.is_algebraic K L) : (L ≃ₐ[K] L) ≃* (L →ₐ[K] L) :=
{ to_fun := λ ϕ, ϕ.to_alg_hom,
inv_fun := λ ϕ, alg_equiv.of_bijective ϕ (ha.alg_hom_bijective ϕ),
left_inv := λ _, by {ext, refl},
right_inv := λ _, by {ext, refl},
map_mul' := λ _ _, rfl }
/-- Bijection between algebra equivalences and algebra homomorphisms -/
@[reducible] noncomputable def _root_.alg_equiv_equiv_alg_hom [finite_dimensional K L] :
(L ≃ₐ[K] L) ≃* (L →ₐ[K] L) := (algebra.is_algebraic_of_finite K L).alg_equiv_equiv_alg_hom K L
end algebra
variables {R S : Type*} [comm_ring R] [is_domain R] [comm_ring S]
lemma exists_integral_multiple [algebra R S] {z : S} (hz : is_algebraic R z)
(inj : ∀ x, algebra_map R S x = 0 → x = 0) :
∃ (x : integral_closure R S) (y ≠ (0 : R)),
z * algebra_map R S y = x :=
begin
rcases hz with ⟨p, p_ne_zero, px⟩,
set a := p.leading_coeff with a_def,
have a_ne_zero : a ≠ 0 := mt polynomial.leading_coeff_eq_zero.mp p_ne_zero,
have y_integral : is_integral R (algebra_map R S a) := is_integral_algebra_map,
have x_integral : is_integral R (z * algebra_map R S a) :=
⟨p.integral_normalization,
monic_integral_normalization p_ne_zero,
integral_normalization_aeval_eq_zero px inj⟩,
exact ⟨⟨_, x_integral⟩, a, a_ne_zero, rfl⟩
end
/-- A fraction `(a : S) / (b : S)` can be reduced to `(c : S) / (d : R)`,
if `S` is the integral closure of `R` in an algebraic extension `L` of `R`. -/
lemma is_integral_closure.exists_smul_eq_mul {L : Type*} [field L]
[algebra R S] [algebra S L] [algebra R L] [is_scalar_tower R S L] [is_integral_closure S R L]
(h : algebra.is_algebraic R L) (inj : function.injective (algebra_map R L))
(a : S) {b : S} (hb : b ≠ 0) : ∃ (c : S) (d ≠ (0 : R)), d • a = b * c :=
begin
obtain ⟨c, d, d_ne, hx⟩ := exists_integral_multiple
(h (algebra_map _ L a / algebra_map _ L b))
((injective_iff_map_eq_zero _).mp inj),
refine ⟨is_integral_closure.mk' S (c : L) c.2, d, d_ne,
is_integral_closure.algebra_map_injective S R L _⟩,
simp only [algebra.smul_def, ring_hom.map_mul, is_integral_closure.algebra_map_mk', ← hx,
← is_scalar_tower.algebra_map_apply],
rw [← mul_assoc _ (_ / _), mul_div_cancel' (algebra_map S L a), mul_comm],
exact mt ((injective_iff_map_eq_zero _).mp (is_integral_closure.algebra_map_injective S R L) _) hb
end
section field
variables {K L : Type*} [field K] [field L] [algebra K L] (A : subalgebra K L)
lemma inv_eq_of_aeval_div_X_ne_zero {x : L} {p : K[X]}
(aeval_ne : aeval x (div_X p) ≠ 0) :
x⁻¹ = aeval x (div_X p) / (aeval x p - algebra_map _ _ (p.coeff 0)) :=
begin
rw [inv_eq_iff_inv_eq, inv_div, div_eq_iff, sub_eq_iff_eq_add, mul_comm],
conv_lhs { rw ← div_X_mul_X_add p },
rw [alg_hom.map_add, alg_hom.map_mul, aeval_X, aeval_C],
exact aeval_ne
end
lemma inv_eq_of_root_of_coeff_zero_ne_zero {x : L} {p : K[X]}
(aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) :
x⁻¹ = - (aeval x (div_X p) / algebra_map _ _ (p.coeff 0)) :=
begin
convert inv_eq_of_aeval_div_X_ne_zero (mt (λ h, (algebra_map K L).injective _) coeff_zero_ne),
{ rw [aeval_eq, zero_sub, div_neg] },
rw ring_hom.map_zero,
convert aeval_eq,
conv_rhs { rw ← div_X_mul_X_add p },
rw [alg_hom.map_add, alg_hom.map_mul, h, zero_mul, zero_add, aeval_C]
end
lemma subalgebra.inv_mem_of_root_of_coeff_zero_ne_zero {x : A} {p : K[X]}
(aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) : (x⁻¹ : L) ∈ A :=
begin
suffices : (x⁻¹ : L) = (-p.coeff 0)⁻¹ • aeval x (div_X p),
{ rw [this], exact A.smul_mem (aeval x _).2 _ },
have : aeval (x : L) p = 0, by rw [subalgebra.aeval_coe, aeval_eq, subalgebra.coe_zero],
rw [inv_eq_of_root_of_coeff_zero_ne_zero this coeff_zero_ne, div_eq_inv_mul,
algebra.smul_def, map_inv₀, map_neg, inv_neg, neg_mul, subalgebra.aeval_coe]
end
lemma subalgebra.inv_mem_of_algebraic {x : A} (hx : is_algebraic K (x : L)) : (x⁻¹ : L) ∈ A :=
begin
obtain ⟨p, ne_zero, aeval_eq⟩ := hx,
rw [subalgebra.aeval_coe, subalgebra.coe_eq_zero] at aeval_eq,
revert ne_zero aeval_eq,
refine p.rec_on_horner _ _ _,
{ intro h,
contradiction },
{ intros p a hp ha ih ne_zero aeval_eq,
refine A.inv_mem_of_root_of_coeff_zero_ne_zero aeval_eq _,
rwa [coeff_add, hp, zero_add, coeff_C, if_pos rfl] },
{ intros p hp ih ne_zero aeval_eq,
rw [alg_hom.map_mul, aeval_X, mul_eq_zero] at aeval_eq,
cases aeval_eq with aeval_eq x_eq,
{ exact ih hp aeval_eq },
{ rw [x_eq, subalgebra.coe_zero, inv_zero],
exact A.zero_mem } }
end
/-- In an algebraic extension L/K, an intermediate subalgebra is a field. -/
lemma subalgebra.is_field_of_algebraic (hKL : algebra.is_algebraic K L) : is_field A :=
{ mul_inv_cancel := λ a ha, ⟨
⟨a⁻¹, A.inv_mem_of_algebraic (hKL a)⟩,
subtype.ext (mul_inv_cancel (mt (subalgebra.coe_eq_zero _).mp ha))⟩,
.. show nontrivial A, by apply_instance,
.. subalgebra.to_comm_ring A }
end field
section pi
variables (R' : Type u) (S' : Type v) (T' : Type w)
/-- This is not an instance as it forms a diamond with `pi.has_smul`.
See the `instance_diamonds` test for details. -/
def polynomial.has_smul_pi [semiring R'] [has_smul R' S'] :
has_smul (R'[X]) (R' → S') :=
⟨λ p f x, eval x p • f x⟩
/-- This is not an instance as it forms a diamond with `pi.has_smul`.
See the `instance_diamonds` test for details. -/
noncomputable def polynomial.has_smul_pi' [comm_semiring R'] [semiring S'] [algebra R' S']
[has_smul S' T'] :
has_smul (R'[X]) (S' → T') :=
⟨λ p f x, aeval x p • f x⟩
variables {R} {S}
local attribute [instance] polynomial.has_smul_pi polynomial.has_smul_pi'
@[simp] lemma polynomial_smul_apply [semiring R'] [has_smul R' S']
(p : R'[X]) (f : R' → S') (x : R') :
(p • f) x = eval x p • f x := rfl
@[simp] lemma polynomial_smul_apply' [comm_semiring R'] [semiring S'] [algebra R' S']
[has_smul S' T'] (p : R'[X]) (f : S' → T') (x : S') :
(p • f) x = aeval x p • f x := rfl
variables [comm_semiring R'] [comm_semiring S'] [comm_semiring T'] [algebra R' S'] [algebra S' T']
/-- This is not an instance for the same reasons as `polynomial.has_smul_pi'`. -/
noncomputable def polynomial.algebra_pi :
algebra (R'[X]) (S' → T') :=
{ to_fun := λ p z, algebra_map S' T' (aeval z p),
map_one' := funext $ λ z, by simp only [polynomial.aeval_one, pi.one_apply, map_one],
map_mul' := λ f g, funext $ λ z, by simp only [pi.mul_apply, map_mul],
map_zero' := funext $ λ z, by simp only [polynomial.aeval_zero, pi.zero_apply, map_zero],
map_add' := λ f g, funext $ λ z, by simp only [polynomial.aeval_add, pi.add_apply, map_add],
commutes' := λ p f, funext $ λ z, mul_comm _ _,
smul_def' := λ p f, funext $ λ z, by
simp only [algebra.algebra_map_eq_smul_one, polynomial_smul_apply', one_mul,
pi.mul_apply, algebra.smul_mul_assoc],
..polynomial.has_smul_pi' R' S' T' }
local attribute [instance] polynomial.algebra_pi
@[simp] lemma polynomial.algebra_map_pi_eq_aeval :
(algebra_map (R'[X]) (S' → T') : R'[X] → (S' → T')) =
λ p z, algebra_map _ _ (aeval z p) := rfl
@[simp] lemma polynomial.algebra_map_pi_self_eq_eval :
(algebra_map (R'[X]) (R' → R') : R'[X] → (R' → R')) = λ p z, eval z p := rfl
end pi
|
4e94fe0331b10141ef699dcc4b950d81fcdddfb7 | 5749d8999a76f3a8fddceca1f6941981e33aaa96 | /src/order/filter/pointwise.lean | 855cd02af3943c4eb87a74c43e81b142b0ec82b1 | [
"Apache-2.0"
] | permissive | jdsalchow/mathlib | 13ab43ef0d0515a17e550b16d09bd14b76125276 | 497e692b946d93906900bb33a51fd243e7649406 | refs/heads/master | 1,585,819,143,348 | 1,580,072,892,000 | 1,580,072,892,000 | 154,287,128 | 0 | 0 | Apache-2.0 | 1,540,281,610,000 | 1,540,281,609,000 | null | UTF-8 | Lean | false | false | 7,670 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
The pointwise operations on filters have nice properties, such as
• map m (f₁ * f₂) = map m f₁ * map m f₂
• 𝓝 x * 𝓝 y = 𝓝 (x * y)
-/
import algebra.pointwise
import order.filter.basic
open classical set lattice
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open_locale classical
local attribute [instance] pointwise_one pointwise_mul pointwise_add
namespace filter
open set
@[to_additive]
def pointwise_one [has_one α] : has_one (filter α) := ⟨principal {1}⟩
local attribute [instance] pointwise_one
@[simp, to_additive]
lemma mem_pointwise_one [has_one α] (s : set α) :
s ∈ (1 : filter α) ↔ (1:α) ∈ s :=
calc
s ∈ (1:filter α) ↔ {(1:α)} ⊆ s : iff.rfl
... ↔ (1:α) ∈ s : by simp
@[to_additive]
def pointwise_mul [monoid α] : has_mul (filter α) := ⟨λf g,
{ sets := { s | ∃t₁∈f, ∃t₂∈g, t₁ * t₂ ⊆ s },
univ_sets :=
begin
have h₁ : (∃x, x ∈ f) := ⟨univ, univ_sets f⟩,
have h₂ : (∃x, x ∈ g) := ⟨univ, univ_sets g⟩,
simpa using and.intro h₁ h₂
end,
sets_of_superset := λx y hx hxy,
begin
rcases hx with ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
exact ⟨t₁, ht₁, t₂, ht₂, subset.trans t₁t₂ hxy⟩
end,
inter_sets := λx y,
begin
simp only [exists_prop, mem_set_of_eq, subset_inter_iff],
rintros ⟨s₁, hs₁, s₂, hs₂, s₁s₂⟩ ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
exact ⟨s₁ ∩ t₁, inter_sets f hs₁ ht₁, s₂ ∩ t₂, inter_sets g hs₂ ht₂,
subset.trans (pointwise_mul_subset_mul (inter_subset_left _ _) (inter_subset_left _ _)) s₁s₂,
subset.trans (pointwise_mul_subset_mul (inter_subset_right _ _) (inter_subset_right _ _)) t₁t₂⟩,
end }⟩
local attribute [instance] pointwise_mul pointwise_add
@[to_additive]
lemma mem_pointwise_mul [monoid α] {f g : filter α} {s : set α} :
s ∈ f * g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ * t₂ ⊆ s := iff.rfl
@[to_additive]
lemma mul_mem_pointwise_mul [monoid α] {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) :
s * t ∈ f * g := ⟨_, hs, _, ht, subset.refl _⟩
@[to_additive]
lemma pointwise_mul_le_mul [monoid α] {f₁ f₂ g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁ * g₁ ≤ f₂ * g₂ := assume _ ⟨s, hs, t, ht, hst⟩, ⟨s, hf hs, t, hg ht, hst⟩
@[to_additive]
lemma pointwise_mul_ne_bot [monoid α] {f g : filter α} : f ≠ ⊥ → g ≠ ⊥ → f * g ≠ ⊥ :=
begin
simp only [forall_sets_ne_empty_iff_ne_bot.symm],
rintros hf hg s ⟨a, ha, b, hb, ab⟩,
rcases ne_empty_iff_exists_mem.1 (pointwise_mul_ne_empty (hf a ha) (hg b hb)) with ⟨x, hx⟩,
exact ne_empty_iff_exists_mem.2 ⟨x, ab hx⟩
end
@[to_additive]
lemma pointwise_mul_assoc [monoid α] (f g h : filter α) : f * g * h = f * (g * h) :=
begin
ext s, split,
{ rintros ⟨a, ⟨a₁, ha₁, a₂, ha₂, a₁a₂⟩, b, hb, ab⟩,
refine ⟨a₁, ha₁, a₂ * b, mul_mem_pointwise_mul ha₂ hb, _⟩,
rw [← pointwise_mul_semigroup.mul_assoc],
exact calc
a₁ * a₂ * b ⊆ a * b : pointwise_mul_subset_mul a₁a₂ (subset.refl _)
... ⊆ s : ab },
{ rintros ⟨a, ha, b, ⟨b₁, hb₁, b₂, hb₂, b₁b₂⟩, ab⟩,
refine ⟨a * b₁, mul_mem_pointwise_mul ha hb₁, b₂, hb₂, _⟩,
rw [pointwise_mul_semigroup.mul_assoc],
exact calc
a * (b₁ * b₂) ⊆ a * b : pointwise_mul_subset_mul (subset.refl _) b₁b₂
... ⊆ s : ab }
end
local attribute [instance] pointwise_mul_monoid
@[to_additive]
lemma pointwise_one_mul [monoid α] (f : filter α) : 1 * f = f :=
begin
ext s, split,
{ rintros ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
refine mem_sets_of_superset (mem_sets_of_superset ht₂ _) t₁t₂,
assume x hx,
exact ⟨1, by rwa [← mem_pointwise_one], x, hx, (one_mul _).symm⟩ },
{ assume hs,
refine ⟨(1:set α), mem_principal_self _, s, hs, by simp only [one_mul]⟩ }
end
@[to_additive]
lemma pointwise_mul_one [monoid α] (f : filter α) : f * 1 = f :=
begin
ext s, split,
{ rintros ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
refine mem_sets_of_superset (mem_sets_of_superset ht₁ _) t₁t₂,
assume x hx,
exact ⟨x, hx, 1, by rwa [← mem_pointwise_one], (mul_one _).symm⟩ },
{ assume hs,
refine ⟨s, hs, (1:set α), mem_principal_self _, by simp only [mul_one]⟩ }
end
@[to_additive pointwise_add_add_monoid]
def pointwise_mul_monoid [monoid α] : monoid (filter α) :=
{ mul_assoc := pointwise_mul_assoc,
one_mul := pointwise_one_mul,
mul_one := pointwise_mul_one,
.. pointwise_mul,
.. pointwise_one }
local attribute [instance] filter.pointwise_mul_monoid filter.pointwise_add_add_monoid
section map
open is_mul_hom
variables [monoid α] [monoid β] {f : filter α} (m : α → β)
@[to_additive]
lemma map_pointwise_mul [is_mul_hom m] {f₁ f₂ : filter α} : map m (f₁ * f₂) = map m f₁ * map m f₂ :=
filter_eq $ set.ext $ assume s,
begin
simp only [mem_pointwise_mul], split,
{ rintro ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
have : m '' (t₁ * t₂) ⊆ s := subset.trans (image_subset m t₁t₂) (image_preimage_subset _ _),
refine ⟨m '' t₁, image_mem_map ht₁, m '' t₂, image_mem_map ht₂, _⟩,
rwa ← image_pointwise_mul m t₁ t₂ },
{ rintro ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩,
refine ⟨m ⁻¹' t₁, ht₁, m ⁻¹' t₂, ht₂, image_subset_iff.1 _⟩,
rw image_pointwise_mul m,
exact subset.trans
(pointwise_mul_subset_mul (image_preimage_subset _ _) (image_preimage_subset _ _)) t₁t₂ },
end
@[to_additive]
lemma map_pointwise_one [is_monoid_hom m] : map m (1:filter α) = 1 :=
le_antisymm
(le_principal_iff.2 $ mem_map_sets_iff.2 ⟨(1:set α), by simp,
by { assume x, simp [is_monoid_hom.map_one m], rintros rfl, refl }⟩)
(le_map $ assume s hs,
begin
simp only [mem_pointwise_one],
exact ⟨(1:α), (mem_pointwise_one s).1 hs, is_monoid_hom.map_one _⟩
end)
-- TODO: prove similar statements when `m` is group homomorphism etc.
lemma pointwise_mul_map_is_monoid_hom [is_monoid_hom m] : is_monoid_hom (map m) :=
{ map_one := map_pointwise_one m,
map_mul := λ _ _, map_pointwise_mul m }
lemma pointwise_add_map_is_add_monoid_hom {α : Type*} {β : Type*} [add_monoid α] [add_monoid β]
(m : α → β) [is_add_monoid_hom m] : is_add_monoid_hom (map m) :=
{ map_zero := map_pointwise_zero m,
map_add := λ _ _, map_pointwise_add m }
attribute [to_additive pointwise_add_map_is_add_monoid_hom] pointwise_mul_map_is_monoid_hom
-- The other direction does not hold in general.
@[to_additive]
lemma comap_mul_comap_le [is_mul_hom m] {f₁ f₂ : filter β} :
comap m f₁ * comap m f₂ ≤ comap m (f₁ * f₂) :=
begin
rintros s ⟨t, ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, mt⟩,
refine ⟨m ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, m ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, _⟩,
have := subset.trans (preimage_mono t₁t₂) mt,
exact subset.trans (preimage_pointwise_mul_preimage_subset m _ _) this
end
variables {m}
@[to_additive]
lemma tendsto.mul_mul [is_mul_hom m] {f₁ g₁ : filter α} {f₂ g₂ : filter β} :
tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ * g₁) (f₂ * g₂) :=
assume hf hg, by { rw [tendsto, map_pointwise_mul m], exact pointwise_mul_le_mul hf hg }
end map
end filter
|
af63276f3834cb97cef2adecfe64874877c7c38e | e953c38599905267210b87fb5d82dcc3e52a4214 | /hott/types/bool.hlean | 5b8f7d466d44bc3bbd7b602a3a776049bd9f774e | [
"Apache-2.0"
] | permissive | c-cube/lean | 563c1020bff98441c4f8ba60111fef6f6b46e31b | 0fb52a9a139f720be418dafac35104468e293b66 | refs/heads/master | 1,610,753,294,113 | 1,440,451,356,000 | 1,440,499,588,000 | 41,748,334 | 0 | 0 | null | 1,441,122,656,000 | 1,441,122,656,000 | null | UTF-8 | Lean | false | false | 1,291 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Theorems about the booleans
-/
open is_equiv eq equiv function is_trunc option unit
namespace bool
definition ff_ne_tt : ff = tt → empty
| [none]
definition is_equiv_bnot [instance] [priority 500] : is_equiv bnot :=
begin
fapply is_equiv.mk,
exact bnot,
all_goals (intro b;cases b), do 6 reflexivity
-- all_goals (focus (intro b;cases b;all_goals reflexivity)),
end
definition equiv_bnot : bool ≃ bool := equiv.mk bnot _
definition eq_bnot : bool = bool := ua equiv_bnot
definition eq_bnot_ne_idp : eq_bnot ≠ idp :=
assume H : eq_bnot = idp,
assert H2 : bnot = id, from !cast_ua_fn⁻¹ ⬝ ap cast H,
absurd (ap10 H2 tt) ff_ne_tt
definition not_is_hset_type : ¬is_hset Type₀ :=
assume H : is_hset Type₀,
absurd !is_hset.elim eq_bnot_ne_idp
definition bool_equiv_option_unit : bool ≃ option unit :=
begin
fapply equiv.MK,
{ intro b, cases b, exact none, exact some star},
{ intro u, cases u, exact ff, exact tt},
{ intro u, cases u with u, reflexivity, cases u, reflexivity},
{ intro b, cases b, reflexivity, reflexivity},
end
end bool
|
b656fbf18c06abef19ef5e866c7b89167a6c4714 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/361.lean | afe69af83ed6b555f9ecc2840871438a1921aeee | [
"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 | 160 | lean | theorem ex1 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := by
theorem ex2 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := by
cases h1
case inl =>
|
4e6bc8bbd83835fabca17a8071e4707c0ef72ed8 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/euclidean_domain/defs.lean | 18d9d394c843783e7440a14b085e6df8e893fa38 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,282 | lean | /-
Copyright (c) 2018 Louis Carlin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Louis Carlin, Mario Carneiro
-/
import logic.nontrivial
import algebra.divisibility.basic
import algebra.group.basic
import algebra.ring.defs
/-!
# Euclidean domains
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise,
a slightly more general version is provided which is sometimes called a transfinite Euclidean domain
and differs in the fact that the degree function need not take values in `ℕ` but can take values in
any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which
don't satisfy the classical notion were provided independently by Hiblot and Nagata.
## Main definitions
* `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances
of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`.
* `gcd`: defines the greatest common divisors of two elements of a Euclidean domain.
* `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that
`x * a + y * b = gcd a b`.
* `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as
`a * b / (gcd a b)`
## Main statements
See `algebra.euclidean_domain.basic` for most of the theorems about Euclidean domains,
including Bézout's lemma.
See `algebra.euclidean_domain.instances` for the fact that `ℤ` is a Euclidean domain,
as is any field.
## Notation
`≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial
ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than
the degree of `q`.
## Implementation details
Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well
founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to
setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute
value of `j`.
## References
* [Th. Motzkin, *The Euclidean algorithm*][MR32592]
* [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*]
[MR399081]
* [M. Nagata, *On Euclid algorithm*][MR541021]
## Tags
Euclidean domain, transfinite Euclidean domain, Bézout's lemma
-/
universe u
/-- A `euclidean_domain` is an non-trivial commutative ring with a division and a remainder,
satisfying `b * (a / b) + a % b = a`.
The definition of a euclidean domain usually includes a valuation function `R → ℕ`.
This definition is slightly generalised to include a well founded relation
`r` with the property that `r (a % b) b`, instead of a valuation. -/
@[protect_proj without mul_left_not_lt r_well_founded]
class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R :=
(quotient : R → R → R)
(quotient_zero : ∀ a, quotient a 0 = 0)
(remainder : R → R → R)
(quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a)
(r : R → R → Prop)
(r_well_founded : well_founded r)
(remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b)
(mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a)
namespace euclidean_domain
variable {R : Type u}
variables [euclidean_domain R]
local infix ` ≺ `:50 := euclidean_domain.r
@[priority 70] -- see Note [lower instance priority]
instance : has_div R := ⟨euclidean_domain.quotient⟩
@[priority 70] -- see Note [lower instance priority]
instance : has_mod R := ⟨euclidean_domain.remainder⟩
theorem div_add_mod (a b : R) : b * (a / b) + a % b = a :=
euclidean_domain.quotient_mul_add_remainder_eq _ _
lemma mod_add_div (a b : R) : a % b + b * (a / b) = a :=
(add_comm _ _).trans (div_add_mod _ _)
lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m :=
by { rw mul_comm, exact mod_add_div _ _ }
lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m :=
by { rw mul_comm, exact div_add_mod _ _ }
lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) :
a % b = a - b * (a / b) :=
calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm
... = a - b * (a / b) : by rw div_add_mod
theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b :=
euclidean_domain.remainder_lt
theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b :=
by { rw mul_comm, exact mul_left_not_lt b h }
@[simp] lemma mod_zero (a : R) : a % 0 = a :=
by simpa only [zero_mul, zero_add] using div_add_mod a 0
lemma lt_one (a : R) : a ≺ (1:R) → a = 0 :=
by { haveI := classical.dec, exact
not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) }
lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b
| _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha)
@[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 :=
euclidean_domain.quotient_zero a
section
open_locale classical
@[elab_as_eliminator]
theorem gcd.induction {P : R → R → Prop} : ∀ a b : R,
(∀ x, P 0 x) →
(∀ a b, a ≠ 0 → P (b % a) a → P a b) →
P a b
| a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else
have h:_ := mod_lt b a0,
H1 _ _ a0 (gcd.induction (b%a) a H0 H1)
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
end
section gcd
variable [decidable_eq R]
/-- `gcd a b` is a (non-unique) element such that `gcd a b ∣ a` `gcd a b ∣ b`, and for
any element `c` such that `c ∣ a` and `c ∣ b`, then `c ∣ gcd a b` -/
def gcd : R → R → R
| a := λ b, if a0 : a = 0 then b else
have h:_ := mod_lt b a0,
gcd (b%a) a
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
@[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a :=
by { rw gcd, exact if_pos rfl }
/--
An implementation of the extended GCD algorithm.
At each step we are computing a triple `(r, s, t)`, where `r` is the next value of the GCD
algorithm, to compute the greatest common divisor of the input (say `x` and `y`), and `s` and `t`
are the coefficients in front of `x` and `y` to obtain `r` (i.e. `r = s * x + t * y`).
The function `xgcd_aux` takes in two triples, and from these recursively computes the next triple:
```
xgcd_aux (r, s, t) (r', s', t') = xgcd_aux (r' % r, s' - (r' / r) * s, t' - (r' / r) * t) (r, s, t)
```
-/
def xgcd_aux : R → R → R → R → R → R → R × R × R
| r := λ s t r' s' t',
if hr : r = 0 then (r', s', t')
else
have r' % r ≺ r, from mod_lt _ hr,
let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
@[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') :=
by { unfold xgcd_aux, exact if_pos rfl }
theorem xgcd_aux_rec {r s t r' s' t' : R} (h : r ≠ 0) :
xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t :=
by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h}
/-- Use the extended GCD algorithm to generate the `a` and `b` values
satisfying `gcd x y = x * a + y * b`. -/
def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2
/-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_a (x y : R) : R := (xgcd x y).1
/-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_b (x y : R) : R := (xgcd x y).2
@[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 :=
by { unfold gcd_a, rw [xgcd, xgcd_zero_left] }
@[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 :=
by { unfold gcd_b, rw [xgcd, xgcd_zero_left] }
theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) :=
prod.mk.eta.symm
end gcd
section lcm
variables [decidable_eq R]
/-- `lcm a b` is a (non-unique) element such that `a ∣ lcm a b` `b ∣ lcm a b`, and for
any element `c` such that `a ∣ c` and `b ∣ c`, then `lcm a b ∣ c` -/
def lcm (x y : R) : R :=
x * y / gcd x y
end lcm
end euclidean_domain
|
c436a54a58e20029da498fae1b70cf0d34a9d376 | 022547453607c6244552158ff25ab3bf17361760 | /src/measure_theory/simple_func_dense.lean | 769642aae28b475e3f837a8770adc66aa46b5a12 | [
"Apache-2.0"
] | permissive | 1293045656/mathlib | 5f81741a7c1ff1873440ec680b3680bfb6b7b048 | 4709e61525a60189733e72a50e564c58d534bed8 | refs/heads/master | 1,687,010,200,553 | 1,626,245,646,000 | 1,626,245,646,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 37,088 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth
-/
import measure_theory.l1_space
/-!
# Density of simple functions
Show that each Borel measurable function can be approximated pointwise, and each `Lᵖ` Borel
measurable function can be approximated in `Lᵖ` norm, by a sequence of simple functions.
## Main definitions
* `measure_theory.simple_func.nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ ℕ`: the `simple_func` sending
each `x : α` to the point `e k` which is the nearest to `x` among `e 0`, ..., `e N`.
* `measure_theory.simple_func.approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α)
(h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) : β →ₛ α` : a simple function that takes values in `s`
and approximates `f`.
* `measure_theory.Lp.simple_func`, the type of `Lp` simple functions
* `coe_to_Lp`, the embedding of `Lp.simple_func E p μ` into `Lp E p μ`
## Main results
* `tendsto_approx_on` (pointwise convergence): If `f x ∈ s`, then the sequence of simple
approximations `measure_theory.simple_func.approx_on f hf s y₀ h₀ n`, evaluated at `x`,
tends to `f x` as `n` tends to `∞`.
* `tendsto_approx_on_univ_Lp` (Lᵖ convergence): If `E` is a `normed_group` and `f` is measurable
and `mem_ℒp` (for `p < ∞`), then the simple functions `simple_func.approx_on f hf s 0 h₀ n` may
be considered as elements of `Lp E p μ`, and they tend in Lᵖ to `f`.
* `Lp.simple_func.dense_embedding`: the embedding `coe_to_Lp` of the `Lp` simple functions into
`Lp` is dense.
* `mem_ℒp.induction`: to prove a predicate for all elements of `Lp`, it suffices to check that
it behaves correctly on simple functions in `Lp`.
* `integrable.induction`: to prove a predicate for all elements of `L1`, it suffices to check that
it behaves correctly on simple functions in `L1`.
## TODO
For `E` finite-dimensional, simple functions `α →ₛ E` are dense in L^∞ -- prove this.
## Notations
* `α →ₛ β` (local notation): the type of simple functions `α → β`.
* `α →₁ₛ[μ] E`: the type of `L1` simple functions `α → β`.
-/
open set function filter topological_space ennreal emetric finset
open_locale classical topological_space ennreal measure_theory big_operators
variables {α β ι E F 𝕜 : Type*}
noncomputable theory
namespace measure_theory
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
/-! ### Pointwise approximation by simple functions -/
section pointwise
variables [measurable_space α] [emetric_space α] [opens_measurable_space α]
/-- `nearest_pt_ind e N x` is the index `k` such that `e k` is the nearest point to `x` among the
points `e 0`, ..., `e N`. If more than one point are at the same distance from `x`, then
`nearest_pt_ind e N x` returns the least of their indexes. -/
noncomputable def nearest_pt_ind (e : ℕ → α) : ℕ → α →ₛ ℕ
| 0 := const α 0
| (N + 1) := piecewise (⋂ k ≤ N, {x | edist (e (N + 1)) x < edist (e k) x})
(measurable_set.Inter $ λ k, measurable_set.Inter_Prop $ λ hk,
measurable_set_lt measurable_edist_right measurable_edist_right)
(const α $ N + 1) (nearest_pt_ind N)
/-- `nearest_pt e N x` is the nearest point to `x` among the points `e 0`, ..., `e N`. If more than
one point are at the same distance from `x`, then `nearest_pt e N x` returns the point with the
least possible index. -/
noncomputable def nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ α :=
(nearest_pt_ind e N).map e
@[simp] lemma nearest_pt_ind_zero (e : ℕ → α) : nearest_pt_ind e 0 = const α 0 := rfl
@[simp] lemma nearest_pt_zero (e : ℕ → α) : nearest_pt e 0 = const α (e 0) := rfl
lemma nearest_pt_ind_succ (e : ℕ → α) (N : ℕ) (x : α) :
nearest_pt_ind e (N + 1) x =
if ∀ k ≤ N, edist (e (N + 1)) x < edist (e k) x
then N + 1 else nearest_pt_ind e N x :=
by { simp only [nearest_pt_ind, coe_piecewise, set.piecewise], congr, simp }
lemma nearest_pt_ind_le (e : ℕ → α) (N : ℕ) (x : α) : nearest_pt_ind e N x ≤ N :=
begin
induction N with N ihN, { simp },
simp only [nearest_pt_ind_succ],
split_ifs,
exacts [le_rfl, ihN.trans N.le_succ]
end
lemma edist_nearest_pt_le (e : ℕ → α) (x : α) {k N : ℕ} (hk : k ≤ N) :
edist (nearest_pt e N x) x ≤ edist (e k) x :=
begin
induction N with N ihN generalizing k,
{ simp [nonpos_iff_eq_zero.1 hk, le_refl] },
{ simp only [nearest_pt, nearest_pt_ind_succ, map_apply],
split_ifs,
{ rcases hk.eq_or_lt with rfl|hk,
exacts [le_rfl, (h k (nat.lt_succ_iff.1 hk)).le] },
{ push_neg at h,
rcases h with ⟨l, hlN, hxl⟩,
rcases hk.eq_or_lt with rfl|hk,
exacts [(ihN hlN).trans hxl, ihN (nat.lt_succ_iff.1 hk)] } }
end
lemma tendsto_nearest_pt {e : ℕ → α} {x : α} (hx : x ∈ closure (range e)) :
tendsto (λ N, nearest_pt e N x) at_top (𝓝 x) :=
begin
refine (at_top_basis.tendsto_iff nhds_basis_eball).2 (λ ε hε, _),
rcases emetric.mem_closure_iff.1 hx ε hε with ⟨_, ⟨N, rfl⟩, hN⟩,
rw [edist_comm] at hN,
exact ⟨N, trivial, λ n hn, (edist_nearest_pt_le e x hn).trans_lt hN⟩
end
variables [measurable_space β] {f : β → α}
/-- Approximate a measurable function by a sequence of simple functions `F n` such that
`F n x ∈ s`. -/
noncomputable def approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α) (h₀ : y₀ ∈ s)
[separable_space s] (n : ℕ) :
β →ₛ α :=
by haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩;
exact comp (nearest_pt (λ k, nat.cases_on k y₀ (coe ∘ dense_seq s) : ℕ → α) n) f hf
@[simp] lemma approx_on_zero {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) :
approx_on f hf s y₀ h₀ 0 x = y₀ :=
rfl
lemma approx_on_mem {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (n : ℕ) (x : β) :
approx_on f hf s y₀ h₀ n x ∈ s :=
begin
haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩,
suffices : ∀ n, (nat.cases_on n y₀ (coe ∘ dense_seq s) : α) ∈ s, { apply this },
rintro (_|n),
exacts [h₀, subtype.mem _]
end
@[simp] lemma approx_on_comp {γ : Type*} [measurable_space γ] {f : β → α} (hf : measurable f)
{g : γ → β} (hg : measurable g) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) :
approx_on (f ∘ g) (hf.comp hg) s y₀ h₀ n = (approx_on f hf s y₀ h₀ n).comp g hg :=
rfl
lemma tendsto_approx_on {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] {x : β} (hx : f x ∈ closure s) :
tendsto (λ n, approx_on f hf s y₀ h₀ n x) at_top (𝓝 $ f x) :=
begin
haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩,
rw [← @subtype.range_coe _ s, ← image_univ, ← (dense_range_dense_seq s).closure_eq] at hx,
simp only [approx_on, coe_comp],
refine tendsto_nearest_pt (closure_minimal _ is_closed_closure hx),
simp only [nat.range_cases_on, closure_union, range_comp coe],
exact subset.trans (image_closure_subset_closure_image continuous_subtype_coe)
(subset_union_right _ _)
end
lemma edist_approx_on_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) (n : ℕ) :
edist (approx_on f hf s y₀ h₀ n x) (f x) ≤ edist y₀ (f x) :=
begin
dsimp only [approx_on, coe_comp, (∘)],
exact edist_nearest_pt_le _ _ (zero_le _)
end
lemma edist_approx_on_y0_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) (n : ℕ) :
edist y₀ (approx_on f hf s y₀ h₀ n x) ≤ edist y₀ (f x) + edist y₀ (f x) :=
calc edist y₀ (approx_on f hf s y₀ h₀ n x) ≤
edist y₀ (f x) + edist (approx_on f hf s y₀ h₀ n x) (f x) : edist_triangle_right _ _ _
... ≤ edist y₀ (f x) + edist y₀ (f x) : add_le_add_left (edist_approx_on_le hf h₀ x n) _
end pointwise
/-! ### Lp approximation by simple functions -/
section Lp
variables [measurable_space β]
variables [measurable_space E] [normed_group E] {q : ℝ} {p : ℝ≥0∞}
lemma nnnorm_approx_on_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s y₀ h₀ n x - f x∥₊ ≤ ∥f x - y₀∥₊ :=
begin
have := edist_approx_on_le hf h₀ x n,
rw edist_comm y₀ at this,
simp only [edist_nndist, nndist_eq_nnnorm] at this,
exact_mod_cast this
end
lemma norm_approx_on_y₀_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s y₀ h₀ n x - y₀∥ ≤ ∥f x - y₀∥ + ∥f x - y₀∥ :=
begin
have := edist_approx_on_y0_le hf h₀ x n,
repeat { rw [edist_comm y₀, edist_eq_coe_nnnorm_sub] at this },
exact_mod_cast this,
end
lemma norm_approx_on_zero_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} (h₀ : (0 : E) ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s 0 h₀ n x∥ ≤ ∥f x∥ + ∥f x∥ :=
begin
have := edist_approx_on_y0_le hf h₀ x n,
simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this,
exact_mod_cast this,
end
lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E]
{f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s]
(hp_ne_top : p ≠ ∞) {μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s)
(hi : snorm (λ x, f x - y₀) p μ < ∞) :
tendsto (λ n, snorm (approx_on f hf s y₀ h₀ n - f) p μ) at_top (𝓝 0) :=
begin
by_cases hp_zero : p = 0,
{ simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds },
have hp : 0 < p.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr hp_zero, hp_ne_top⟩,
suffices : tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ^ p.to_real ∂μ) at_top (𝓝 0),
{ simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top],
convert continuous_rpow_const.continuous_at.tendsto.comp this;
simp [_root_.inv_pos.mpr hp] },
-- We simply check the conditions of the Dominated Convergence Theorem:
-- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable
have hF_meas : ∀ n, measurable (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real),
{ simpa only [← edist_eq_coe_nnnorm_sub] using
λ n, (approx_on f hf s y₀ h₀ n).measurable_bind (λ y x, (edist y (f x)) ^ p.to_real)
(λ y, (measurable_edist_right.comp hf).pow_const p.to_real) },
-- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly
-- bounded, at any given point, by `λ x, ∥f x - y₀∥ ^ p.to_real`
have h_bound : ∀ n, (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real)
≤ᵐ[μ] (λ x, ∥f x - y₀∥₊ ^ p.to_real),
{ exact λ n, eventually_of_forall
(λ x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf h₀ x n)) to_real_nonneg) },
-- (3) The bounding function `λ x, ∥f x - y₀∥ ^ p.to_real` has finite integral
have h_fin : ∫⁻ (a : β), ∥f a - y₀∥₊ ^ p.to_real ∂μ < ⊤,
{ exact lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi },
-- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise
-- to zero
have h_lim : ∀ᵐ (a : β) ∂μ,
tendsto (λ n, (∥approx_on f hf s y₀ h₀ n a - f a∥₊ : ℝ≥0∞) ^ p.to_real) at_top (𝓝 0),
{ filter_upwards [hμ],
intros a ha,
have : tendsto (λ n, (approx_on f hf s y₀ h₀ n) a - f a) at_top (𝓝 (f a - f a)),
{ exact (tendsto_approx_on hf h₀ ha).sub tendsto_const_nhds },
convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm),
simp [zero_rpow_of_pos hp] },
-- Then we apply the Dominated Convergence Theorem
simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim,
end
lemma mem_ℒp_approx_on [borel_space E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : mem_ℒp f p μ) {s : set E} {y₀ : E}
(h₀ : y₀ ∈ s) [separable_space s] (hi₀ : mem_ℒp (λ x, y₀) p μ) (n : ℕ) :
mem_ℒp (approx_on f fmeas s y₀ h₀ n) p μ :=
begin
refine ⟨(approx_on f fmeas s y₀ h₀ n).ae_measurable, _⟩,
suffices : snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ < ⊤,
{ have : mem_ℒp (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ :=
⟨(approx_on f fmeas s y₀ h₀ n - const β y₀).ae_measurable, this⟩,
convert snorm_add_lt_top this hi₀,
ext x,
simp },
-- We don't necessarily have `mem_ℒp (λ x, f x - y₀) p μ`, because the `ae_measurable` part
-- requires `ae_measurable.add`, which requires second-countability
have hf' : mem_ℒp (λ x, ∥f x - y₀∥) p μ,
{ have h_meas : measurable (λ x, ∥f x - y₀∥),
{ simp only [← dist_eq_norm],
exact (continuous_id.dist continuous_const).measurable.comp fmeas },
refine ⟨h_meas.ae_measurable, _⟩,
rw snorm_norm,
convert snorm_add_lt_top hf hi₀.neg,
ext x,
simp [sub_eq_add_neg] },
have : ∀ᵐ x ∂μ, ∥approx_on f fmeas s y₀ h₀ n x - y₀∥ ≤ ∥(∥f x - y₀∥ + ∥f x - y₀∥)∥,
{ refine eventually_of_forall _,
intros x,
convert norm_approx_on_y₀_le fmeas h₀ x n,
rw [real.norm_eq_abs, abs_of_nonneg],
exact add_nonneg (norm_nonneg _) (norm_nonneg _) },
calc snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ
≤ snorm (λ x, ∥f x - y₀∥ + ∥f x - y₀∥) p μ : snorm_mono_ae this
... < ⊤ : snorm_add_lt_top hf' hf',
end
lemma tendsto_approx_on_univ_Lp_snorm [opens_measurable_space E] [second_countable_topology E]
{f : β → E} (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f) (hf : snorm f p μ < ∞) :
tendsto (λ n, snorm (approx_on f fmeas univ 0 trivial n - f) p μ) at_top (𝓝 0) :=
tendsto_approx_on_Lp_snorm fmeas trivial hp_ne_top (by simp) (by simpa using hf)
lemma mem_ℒp_approx_on_univ [borel_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : mem_ℒp f p μ) (n : ℕ) :
mem_ℒp (approx_on f fmeas univ 0 trivial n) p μ :=
mem_ℒp_approx_on fmeas hf (mem_univ _) zero_mem_ℒp n
lemma tendsto_approx_on_univ_Lp [borel_space E] [second_countable_topology E]
{f : β → E} [hp : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f)
(hf : mem_ℒp f p μ) :
tendsto (λ n, (mem_ℒp_approx_on_univ fmeas hf n).to_Lp (approx_on f fmeas univ 0 trivial n))
at_top (𝓝 (hf.to_Lp f)) :=
by simp [Lp.tendsto_Lp_iff_tendsto_ℒp'', tendsto_approx_on_univ_Lp_snorm hp_ne_top fmeas hf.2]
end Lp
/-! ### L1 approximation by simple functions -/
section integrable
variables [measurable_space β]
variables [measurable_space E] [normed_group E]
lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E]
{f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s]
{μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : has_finite_integral (λ x, f x - y₀) μ) :
tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ∂μ) at_top (𝓝 0) :=
by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf h₀ one_ne_top hμ
(by simpa [snorm_one_eq_lintegral_nnnorm] using hi)
lemma integrable_approx_on [borel_space E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s)
[separable_space s] (hi₀ : integrable (λ x, y₀) μ) (n : ℕ) :
integrable (approx_on f fmeas s y₀ h₀ n) μ :=
begin
rw ← mem_ℒp_one_iff_integrable at hf hi₀ ⊢,
exact mem_ℒp_approx_on fmeas hf h₀ hi₀ n,
end
lemma tendsto_approx_on_univ_L1_nnnorm [opens_measurable_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ) :
tendsto (λ n, ∫⁻ x, ∥approx_on f fmeas univ 0 trivial n x - f x∥₊ ∂μ) at_top (𝓝 0) :=
tendsto_approx_on_L1_nnnorm fmeas trivial (by simp) (by simpa using hf.2)
lemma integrable_approx_on_univ [borel_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ) (n : ℕ) :
integrable (approx_on f fmeas univ 0 trivial n) μ :=
integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n
end integrable
section simple_func_properties
variables [measurable_space α]
variables [normed_group E] [measurable_space E] [normed_group F]
variables {μ : measure α} {p : ℝ≥0∞}
/-!
### Properties of simple functions in `Lp` spaces
A simple function `f : α →ₛ E` into a normed group `E` verifies, for a measure `μ`:
- `mem_ℒp f 0 μ` and `mem_ℒp f ∞ μ`, since `f` is a.e.-measurable and bounded,
- for `0 < p < ∞`, `mem_ℒp f p μ ↔ integrable f μ ↔ f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞`.
-/
lemma exists_forall_norm_le (f : α →ₛ F) : ∃ C, ∀ x, ∥f x∥ ≤ C :=
exists_forall_le (f.map (λ x, ∥x∥))
lemma mem_ℒp_zero (f : α →ₛ E) (μ : measure α) : mem_ℒp f 0 μ :=
mem_ℒp_zero_iff_ae_measurable.mpr f.ae_measurable
lemma mem_ℒp_top (f : α →ₛ E) (μ : measure α) : mem_ℒp f ∞ μ :=
let ⟨C, hfC⟩ := f.exists_forall_norm_le in
mem_ℒp_top_of_bound f.ae_measurable C $ eventually_of_forall hfC
protected lemma snorm'_eq {p : ℝ} (f : α →ₛ F) (μ : measure α) :
snorm' f p μ = (∑ y in f.range, (nnnorm y : ℝ≥0∞) ^ p * μ (f ⁻¹' {y})) ^ (1/p) :=
have h_map : (λ a, (nnnorm (f a) : ℝ≥0∞) ^ p) = f.map (λ a : F, (nnnorm a : ℝ≥0∞) ^ p), by simp,
by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral]
lemma measure_preimage_lt_top_of_mem_ℒp (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) (f : α →ₛ E)
(hf : mem_ℒp f p μ) (y : E) (hy_ne : y ≠ 0) :
μ (f ⁻¹' {y}) < ∞ :=
begin
have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos_iff.mpr ⟨hp_pos, hp_ne_top⟩,
have hf_snorm := mem_ℒp.snorm_lt_top hf,
rw [snorm_eq_snorm' hp_pos.ne.symm hp_ne_top, f.snorm'_eq,
← @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]),
@ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]),
ennreal.sum_lt_top_iff] at hf_snorm,
by_cases hyf : y ∈ f.range,
swap,
{ suffices h_empty : f ⁻¹' {y} = ∅,
by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, },
ext1 x,
rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false],
refine λ hxy, hyf _,
rw [mem_range, set.mem_range],
exact ⟨x, hxy⟩, },
specialize hf_snorm y hyf,
rw ennreal.mul_lt_top_iff at hf_snorm,
cases hf_snorm,
{ exact hf_snorm.2, },
cases hf_snorm,
{ refine absurd _ hy_ne,
simpa [hp_pos_real] using hf_snorm, },
{ simp [hf_snorm], },
end
lemma mem_ℒp_of_finite_measure_preimage (p : ℝ≥0∞) {f : α →ₛ E} (hf : ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞) :
mem_ℒp f p μ :=
begin
by_cases hp0 : p = 0,
{ rw [hp0, mem_ℒp_zero_iff_ae_measurable], exact f.ae_measurable, },
by_cases hp_top : p = ∞,
{ rw hp_top, exact mem_ℒp_top f μ, },
refine ⟨f.ae_measurable, _⟩,
rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq],
refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (λ y hy, _)).ne,
by_cases hy0 : y = 0,
{ simp [hy0, ennreal.to_real_pos_iff.mpr ⟨lt_of_le_of_ne (zero_le _) (ne.symm hp0), hp_top⟩], },
{ refine ennreal.mul_lt_top _ (hf y hy0),
exact ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top, },
end
lemma mem_ℒp_iff {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ :=
⟨λ h, measure_preimage_lt_top_of_mem_ℒp hp_pos hp_ne_top f h,
λ h, mem_ℒp_of_finite_measure_preimage p h⟩
lemma integrable_iff {f : α →ₛ E} : integrable f μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ :=
mem_ℒp_one_iff_integrable.symm.trans $ mem_ℒp_iff ennreal.zero_lt_one ennreal.coe_ne_top
lemma mem_ℒp_iff_integrable {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ integrable f μ :=
(mem_ℒp_iff hp_pos hp_ne_top).trans integrable_iff.symm
lemma mem_ℒp_iff_fin_meas_supp {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ f.fin_meas_supp μ :=
(mem_ℒp_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm
lemma integrable_iff_fin_meas_supp {f : α →ₛ E} : integrable f μ ↔ f.fin_meas_supp μ :=
integrable_iff.trans fin_meas_supp_iff.symm
lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ :=
integrable_iff_fin_meas_supp.2 h
lemma integrable_pair [measurable_space F] {f : α →ₛ E} {g : α →ₛ F} :
integrable f μ → integrable g μ → integrable (pair f g) μ :=
by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair
lemma mem_ℒp_of_finite_measure (f : α →ₛ E) (p : ℝ≥0∞) (μ : measure α) [finite_measure μ] :
mem_ℒp f p μ :=
let ⟨C, hfC⟩ := f.exists_forall_norm_le in
mem_ℒp.of_bound f.ae_measurable C $ eventually_of_forall hfC
lemma integrable_of_finite_measure [finite_measure μ] (f : α →ₛ E) : integrable f μ :=
mem_ℒp_one_iff_integrable.mp (f.mem_ℒp_of_finite_measure 1 μ)
lemma measure_preimage_lt_top_of_integrable (f : α →ₛ E) (hf : integrable f μ) {x : E}
(hx : x ≠ 0) :
μ (f ⁻¹' {x}) < ∞ :=
integrable_iff.mp hf x hx
end simple_func_properties
end simple_func
/-! Construction of the space of `Lp` simple functions, and its dense embedding into `Lp`. -/
namespace Lp
open ae_eq_fun
variables
[measurable_space α]
[normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E]
[normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F]
(p : ℝ≥0∞) (μ : measure α)
variables (E)
/-- `Lp.simple_func` is a subspace of Lp consisting of equivalence classes of an integrable simple
function. -/
def simple_func : add_subgroup (Lp E p μ) :=
{ carrier := {f : Lp E p μ | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f},
zero_mem' := ⟨0, rfl⟩,
add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t,
by simp only [←hs, ←ht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]⟩,
neg_mem' := λ f ⟨s, hs⟩, ⟨-s,
by simp only [←hs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]⟩ }
variables {E p μ}
namespace simple_func
section instances
/-! Simple functions in Lp space form a `normed_space`. -/
@[norm_cast] lemma coe_coe (f : Lp.simple_func E p μ) : ⇑(f : Lp E p μ) = f := rfl
protected lemma eq' {f g : Lp.simple_func E p μ} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g :=
subtype.eq ∘ subtype.eq
/-! Implementation note: If `Lp.simple_func E p μ` were defined as a `𝕜`-submodule of `Lp E p μ`,
then the next few lemmas, putting a normed `𝕜`-group structure on `Lp.simple_func E p μ`, would be
unnecessary. But instead, `Lp.simple_func E p μ` is defined as an `add_subgroup` of `Lp E p μ`,
which does not permit this (but has the advantage of working when `E` itself is a normed group,
i.e. has no scalar action). -/
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a `has_scalar`. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def has_scalar : has_scalar 𝕜 (Lp.simple_func E p μ) := ⟨λk f, ⟨k • f,
begin
rcases f with ⟨f, ⟨s, hs⟩⟩,
use k • s,
apply eq.trans (smul_mk k s s.ae_measurable).symm _,
rw hs,
refl,
end ⟩⟩
local attribute [instance] simple_func.has_scalar
@[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : Lp.simple_func E p μ) :
((c • f : Lp.simple_func E p μ) : Lp E p μ) = c • (f : Lp E p μ) := rfl
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a module. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def module : module 𝕜 (Lp.simple_func E p μ) :=
{ one_smul := λf, by { ext1, exact one_smul _ _ },
mul_smul := λx y f, by { ext1, exact mul_smul _ _ _ },
smul_add := λx f g, by { ext1, exact smul_add _ _ _ },
smul_zero := λx, by { ext1, exact smul_zero _ },
add_smul := λx y f, by { ext1, exact add_smul _ _ _ },
zero_smul := λf, by { ext1, exact zero_smul _ _ } }
local attribute [instance] simple_func.module
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a normed space. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def normed_space [fact (1 ≤ p)] : normed_space 𝕜 (Lp.simple_func E p μ) :=
⟨ λc f, by { rw [coe_norm_subgroup, coe_norm_subgroup, coe_smul, norm_smul] } ⟩
end instances
local attribute [instance] simple_func.module simple_func.normed_space
section to_Lp
/-- Construct the equivalence class `[f]` of a simple function `f` satisfying `mem_ℒp`. -/
@[reducible] def to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) : (Lp.simple_func E p μ) :=
⟨hf.to_Lp f, ⟨f, rfl⟩⟩
lemma to_Lp_eq_to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) :
(to_Lp f hf : Lp E p μ) = hf.to_Lp f := rfl
lemma to_Lp_eq_mk (f : α →ₛ E) (hf : mem_ℒp f p μ) :
(to_Lp f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl
lemma to_Lp_zero : to_Lp (0 : α →ₛ E) zero_mem_ℒp = (0 : Lp.simple_func E p μ) := rfl
lemma to_Lp_add (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) :
to_Lp (f + g) (hf.add hg) = to_Lp f hf + to_Lp g hg := rfl
lemma to_Lp_neg (f : α →ₛ E) (hf : mem_ℒp f p μ) :
to_Lp (-f) hf.neg = -to_Lp f hf := rfl
lemma to_Lp_sub (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) :
to_Lp (f - g) (hf.sub hg) = to_Lp f hf - to_Lp g hg :=
by { simp only [sub_eq_add_neg, ← to_Lp_neg, ← to_Lp_add], refl }
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
lemma to_Lp_smul (f : α →ₛ E) (hf : mem_ℒp f p μ) (c : 𝕜) :
to_Lp (c • f) (hf.const_smul c) = c • to_Lp f hf := rfl
lemma norm_to_Lp [fact (1 ≤ p)] (f : α →ₛ E) (hf : mem_ℒp f p μ) :
∥to_Lp f hf∥ = ennreal.to_real (snorm f p μ) :=
norm_to_Lp f hf
end to_Lp
section to_simple_func
/-- Find a representative of a `Lp.simple_func`. -/
def to_simple_func (f : Lp.simple_func E p μ) : α →ₛ E := classical.some f.2
/-- `(to_simple_func f)` is measurable. -/
@[measurability]
protected lemma measurable (f : Lp.simple_func E p μ) : measurable (to_simple_func f) :=
(to_simple_func f).measurable
@[measurability]
protected lemma ae_measurable (f : Lp.simple_func E p μ) : ae_measurable (to_simple_func f) μ :=
(simple_func.measurable f).ae_measurable
lemma to_simple_func_eq_to_fun (f : Lp.simple_func E p μ) : to_simple_func f =ᵐ[μ] f :=
show ⇑(to_simple_func f) =ᵐ[μ] ⇑(f : α →ₘ[μ] E), by
begin
convert (ae_eq_fun.coe_fn_mk (to_simple_func f) (simple_func.ae_measurable f)).symm using 2,
exact (classical.some_spec f.2).symm,
end
/-- `to_simple_func f` satisfies the predicate `mem_ℒp`. -/
protected lemma mem_ℒp (f : Lp.simple_func E p μ) : mem_ℒp (to_simple_func f) p μ :=
mem_ℒp.ae_eq (to_simple_func_eq_to_fun f).symm $ mem_Lp_iff_mem_ℒp.mp (f : Lp E p μ).2
lemma to_Lp_to_simple_func (f : Lp.simple_func E p μ) :
to_Lp (to_simple_func f) (simple_func.mem_ℒp f) = f :=
simple_func.eq' (classical.some_spec f.2)
lemma to_simple_func_to_Lp (f : α →ₛ E) (hfi : mem_ℒp f p μ) :
to_simple_func (to_Lp f hfi) =ᵐ[μ] f :=
by { rw ← mk_eq_mk, exact classical.some_spec (to_Lp f hfi).2 }
variables (E μ)
lemma zero_to_simple_func : to_simple_func (0 : Lp.simple_func E p μ) =ᵐ[μ] 0 :=
begin
filter_upwards [to_simple_func_eq_to_fun (0 : Lp.simple_func E p μ), Lp.coe_fn_zero E 1 μ],
assume a h₁ h₂,
rwa h₁,
end
variables {E μ}
lemma add_to_simple_func (f g : Lp.simple_func E p μ) :
to_simple_func (f + g) =ᵐ[μ] to_simple_func f + to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Lp E p μ) g],
assume a,
simp only [← coe_coe, add_subgroup.coe_add, pi.add_apply],
iterate 4 { assume h, rw h }
end
lemma neg_to_simple_func (f : Lp.simple_func E p μ) :
to_simple_func (-f) =ᵐ[μ] - to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f,
Lp.coe_fn_neg (f : Lp E p μ)],
assume a,
simp only [pi.neg_apply, add_subgroup.coe_neg, ← coe_coe],
repeat { assume h, rw h }
end
lemma sub_to_simple_func (f g : Lp.simple_func E p μ) :
to_simple_func (f - g) =ᵐ[μ] to_simple_func f - to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Lp E p μ) g],
assume a,
simp only [add_subgroup.coe_sub, pi.sub_apply, ← coe_coe],
repeat { assume h, rw h }
end
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
lemma smul_to_simple_func (k : 𝕜) (f : Lp.simple_func E p μ) :
to_simple_func (k • f) =ᵐ[μ] k • to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f,
Lp.coe_fn_smul k (f : Lp E p μ)],
assume a,
simp only [pi.smul_apply, coe_smul, ← coe_coe],
repeat { assume h, rw h }
end
lemma norm_to_simple_func [fact (1 ≤ p)] (f : Lp.simple_func E p μ) :
∥f∥ = ennreal.to_real (snorm (to_simple_func f) p μ) :=
by simpa [to_Lp_to_simple_func] using norm_to_Lp (to_simple_func f) (simple_func.mem_ℒp f)
end to_simple_func
section coe_to_Lp
variables [fact (1 ≤ p)]
protected lemma uniform_continuous :
uniform_continuous (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
uniform_continuous_comap
protected lemma uniform_embedding :
uniform_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
uniform_embedding_comap subtype.val_injective
protected lemma uniform_inducing : uniform_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
simple_func.uniform_embedding.to_uniform_inducing
protected lemma dense_embedding (hp_ne_top : p ≠ ∞) :
dense_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
begin
apply simple_func.uniform_embedding.dense_embedding,
assume f,
rw mem_closure_iff_seq_limit,
have hfi' : mem_ℒp f p μ := Lp.mem_ℒp f,
refine ⟨λ n, ↑(to_Lp (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n)
(simple_func.mem_ℒp_approx_on_univ (Lp.measurable f) hfi' n)), λ n, mem_range_self _, _⟩,
convert simple_func.tendsto_approx_on_univ_Lp hp_ne_top (Lp.measurable f) hfi',
rw to_Lp_coe_fn f (Lp.mem_ℒp f)
end
protected lemma dense_inducing (hp_ne_top : p ≠ ∞) :
dense_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
(simple_func.dense_embedding hp_ne_top).to_dense_inducing
protected lemma dense_range (hp_ne_top : p ≠ ∞) :
dense_range (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
(simple_func.dense_inducing hp_ne_top).dense
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
variables (α E 𝕜)
/-- The embedding of Lp simple functions into Lp functions, as a continuous linear map. -/
def coe_to_Lp : (Lp.simple_func E p μ) →L[𝕜] (Lp E p μ) :=
{ map_smul' := λk f, rfl,
cont := Lp.simple_func.uniform_continuous.continuous,
.. add_subgroup.subtype (Lp.simple_func E p μ) }
variables {α E 𝕜}
end coe_to_Lp
end simple_func
end Lp
variables [measurable_space α] [normed_group E] [measurable_space E] [borel_space E]
[second_countable_topology E] {f : α → E} {p : ℝ≥0∞} {μ : measure α}
/-- To prove something for an arbitrary `mem_ℒp` function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `Lᵖ` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma mem_ℒp.induction [_i : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) (P : (α → E) → Prop)
(h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c)))
(h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → mem_ℒp f p μ → mem_ℒp g p μ →
P f → P g → P (f + g))
(h_closed : is_closed {f : Lp E p μ | P f} )
(h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → mem_ℒp f p μ → P f → P g) :
∀ ⦃f : α → E⦄ (hf : mem_ℒp f p μ), P f :=
begin
have : ∀ (f : simple_func α E), mem_ℒp f p μ → P f,
{ refine simple_func.induction _ _,
{ intros c s hs h,
by_cases hc : c = 0,
{ subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, ext, simp [const] },
apply h_ind c hs,
have hp_pos : 0 < p := lt_of_lt_of_le ennreal.zero_lt_one _i.elim,
rw simple_func.mem_ℒp_iff hp_pos hp_ne_top at h,
convert h c hc,
ext x,
simp only [simple_func.coe_const, set.mem_preimage, mem_singleton_iff, simple_func.coe_zero,
indicator_apply_eq_self, piecewise_eq_indicator, simple_func.const_zero,
simple_func.coe_piecewise, const_apply, hc],
exact not_not.symm },
{ intros f g hfg hf hg int_fg,
rw [simple_func.coe_add, mem_ℒp_add_of_disjoint hfg f.measurable g.measurable] at int_fg,
refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } },
have : ∀ (f : Lp.simple_func E p μ), P f,
{ intro f,
exact h_ae (Lp.simple_func.to_simple_func_eq_to_fun f) (Lp.simple_func.mem_ℒp f)
(this (Lp.simple_func.to_simple_func f) (Lp.simple_func.mem_ℒp f)) },
have : ∀ (f : Lp E p μ), P f :=
λ f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed this,
exact λ f hf, h_ae hf.coe_fn_to_Lp (Lp.mem_ℒp _) (this (hf.to_Lp f)),
end
section integrable
local attribute [instance] fact_one_le_one_ennreal
notation α ` →₁ₛ[`:25 μ `] ` E := @measure_theory.Lp.simple_func α E _ _ _ _ _ 1 μ
lemma L1.simple_func.to_Lp_one_eq_to_L1 (f : α →ₛ E) (hf : integrable f μ) :
(Lp.simple_func.to_Lp f (mem_ℒp_one_iff_integrable.2 hf) : α →₁[μ] E) = hf.to_L1 f :=
rfl
protected lemma L1.simple_func.integrable (f : α →₁ₛ[μ] E) :
integrable (Lp.simple_func.to_simple_func f) μ :=
by { rw ← mem_ℒp_one_iff_integrable, exact (Lp.simple_func.mem_ℒp f) }
/-- To prove something for an arbitrary integrable function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `L¹` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma integrable.induction (P : (α → E) → Prop)
(h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c)))
(h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → integrable f μ → integrable g μ →
P f → P g → P (f + g))
(h_closed : is_closed {f : α →₁[μ] E | P f} )
(h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → integrable f μ → P f → P g) :
∀ ⦃f : α → E⦄ (hf : integrable f μ), P f :=
begin
simp only [← mem_ℒp_one_iff_integrable] at *,
exact mem_ℒp.induction one_ne_top P h_ind h_add h_closed h_ae
end
end integrable
end measure_theory
|
bc58b31e96647b29bc6c78554fc6a1afba14eaeb | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/Coe.lean | b06fed21854edb4e09d13ea7e94a4a21e7a66146 | [
"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 | 9,457 | 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.Meta.WHNF
import Lean.Meta.Transform
import Lean.Meta.SynthInstance
import Lean.Meta.AppBuilder
namespace Lean.Meta
builtin_initialize coeDeclAttr : TagAttribute ←
registerTagAttribute `coe_decl "auxiliary definition used to implement coercion (unfolded during elaboration)"
/--
Return true iff `declName` is one of the auxiliary definitions/projections
used to implement coercions.
-/
def isCoeDecl (env : Environment) (declName : Name) : Bool :=
coeDeclAttr.hasTag env declName
/-- Expand coercions occurring in `e` -/
partial def expandCoe (e : Expr) : MetaM Expr :=
withReducibleAndInstances do
transform e fun e => do
let f := e.getAppFn
if f.isConst then
let declName := f.constName!
if isCoeDecl (← getEnv) declName then
if let some e ← unfoldDefinition? e then
return .visit e.headBeta
return .continue
register_builtin_option autoLift : Bool := {
defValue := true
descr := "insert monadic lifts (i.e., `liftM` and coercions) when needed"
}
/-- Coerces `expr` to `expectedType` using `CoeT`. -/
def coerceSimple? (expr expectedType : Expr) : MetaM (LOption Expr) := do
let eType ← inferType expr
let u ← getLevel eType
let v ← getLevel expectedType
let coeTInstType := mkAppN (mkConst ``CoeT [u, v]) #[eType, expr, expectedType]
match ← trySynthInstance coeTInstType with
| .some inst =>
let result ← expandCoe (mkAppN (mkConst ``CoeT.coe [u, v]) #[eType, expr, expectedType, inst])
unless ← isDefEq (← inferType result) expectedType do
throwError "could not coerce{indentExpr expr}\nto{indentExpr expectedType}\ncoerced expression has wrong type:{indentExpr result}"
return .some result
| .undef => return .undef
| .none => return .none
/-- Coerces `expr` to a function type. -/
def coerceToFunction? (expr : Expr) : MetaM (Option Expr) := do
-- constructing expression manually because mkAppM wouldn't assign universe mvars
let α ← inferType expr
let u ← getLevel α
let v ← mkFreshLevelMVar
let γ ← mkFreshExprMVar (← mkArrow α (mkSort v))
let .some inst ← trySynthInstance (mkApp2 (.const ``CoeFun [u,v]) α γ) | return none
let expanded ← expandCoe (mkApp4 (.const ``CoeFun.coe [u,v]) α γ inst expr)
unless (← whnf (← inferType expanded)).isForall do
throwError "failed to coerce{indentExpr expr}\nto a function, after applying `CoeFun.coe`, result is still not a function{indentExpr expanded}\nthis is often due to incorrect `CoeFun` instances, the synthesized instance was{indentExpr inst}"
return expanded
/-- Coerces `expr` to a type. -/
def coerceToSort? (expr : Expr) : MetaM (Option Expr) := do
-- constructing expression manually because mkAppM wouldn't assign universe mvars
let α ← inferType expr
let u ← getLevel α
let v ← mkFreshLevelMVar
let β ← mkFreshExprMVar (mkSort v)
let .some inst ← trySynthInstance (mkApp2 (.const ``CoeSort [u,v]) α β) | return none
let expanded ← expandCoe (mkApp4 (.const ``CoeSort.coe [u,v]) α β inst expr)
unless (← whnf (← inferType expanded)).isSort do
throwError "failed to coerce{indentExpr expr}\nto a type, after applying `CoeSort.coe`, result is still not a type{indentExpr expanded}\nthis is often due to incorrect `CoeSort` instances, the synthesized instance was{indentExpr inst}"
return expanded
/-- Return `some (m, α)` if `type` can be reduced to an application of the form `m α` using `[reducible]` transparency. -/
def isTypeApp? (type : Expr) : MetaM (Option (Expr × Expr)) := do
let type ← withReducible <| whnf type
match type with
| .app m α => return some ((← instantiateMVars m), (← instantiateMVars α))
| _ => return none
/--
Return `true` if `type` is of the form `m α` where `m` is a `Monad`.
Note that we reduce `type` using transparency `[reducible]`.
-/
def isMonadApp (type : Expr) : MetaM Bool := do
let some (m, _) ← isTypeApp? type | return false
return (← isMonad? m).isSome
/--
Try coercions and monad lifts to make sure `e` has type `expectedType`.
If `expectedType` is of the form `n β`, we try monad lifts and other extensions.
Extensions for monads.
1. Try to unify `n` and `m`. If it succeeds, then we use
```
coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β
```
`n` must be a `Monad` to use this one.
2. If there is monad lift from `m` to `n` and we can unify `α` and `β`, we use
```
liftM : ∀ {m : Type u_1 → Type u_2} {n : Type u_1 → Type u_3} [self : MonadLiftT m n] {α : Type u_1}, m α → n α
```
Note that `n` may not be a `Monad` in this case. This happens quite a bit in code such as
```
def g (x : Nat) : IO Nat := do
IO.println x
pure x
def f {m} [MonadLiftT IO m] : m Nat :=
g 10
```
3. If there is a monad lift from `m` to `n` and a coercion from `α` to `β`, we use
```
liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β
```
Note that approach 3 does not subsume 1 because it is only applicable if there is a coercion from `α` to `β` for all values in `α`.
This is not the case for example for `pure $ x > 0` when the expected type is `IO Bool`. The given type is `IO Prop`, and
we only have a coercion from decidable propositions. Approach 1 works because it constructs the coercion `CoeT (m Prop) (pure $ x > 0) (m Bool)`
using the instance `pureCoeDepProp`.
Note that, approach 2 is more powerful than `tryCoe`.
Recall that type class resolution never assigns metavariables created by other modules.
Now, consider the following scenario
```lean
def g (x : Nat) : IO Nat := ...
deg h (x : Nat) : StateT Nat IO Nat := do
v ← g x;
IO.Println v;
...
```
Let's assume there is no other occurrence of `v` in `h`.
Thus, we have that the expected of `g x` is `StateT Nat IO ?α`,
and the given type is `IO Nat`. So, even if we add a coercion.
```
instance {α m n} [MonadLiftT m n] {α} : Coe (m α) (n α) := ...
```
It is not applicable because TC would have to assign `?α := Nat`.
On the other hand, TC can easily solve `[MonadLiftT IO (StateT Nat IO)]`
since this goal does not contain any metavariables. And then, we
convert `g x` into `liftM $ g x`.
-/
def coerceMonadLift? (e expectedType : Expr) : MetaM (Option Expr) := do
let expectedType ← instantiateMVars expectedType
let eType ← instantiateMVars (← inferType e)
let some (n, β) ← isTypeApp? expectedType | return none
let some (m, α) ← isTypeApp? eType | return none
if (← isDefEq m n) then
let some monadInst ← isMonad? n | return none
try expandCoe (← mkAppOptM ``Lean.Internal.coeM #[m, α, β, none, monadInst, e]) catch _ => return none
else if autoLift.get (← getOptions) then
try
-- Construct lift from `m` to `n`
-- Note: we cannot use mkAppM here because mkAppM does not assign universe metavariables,
-- but we need to make sure that the domains of `m` and `n` have the same level.
let .forallE _ (.sort um₁) (.sort um₂) _ ← whnf (← inferType m) | return none
let .forallE _ (.sort un₁) (.sort un₂) _ ← whnf (← inferType n) | return none
let u ← decLevel um₁
let .true ← isLevelDefEq u (← decLevel un₁) | return none
let v ← decLevel um₂
let w ← decLevel un₂
let monadLiftType := mkAppN (.const ``MonadLiftT [u, v, w]) #[m, n]
let .some monadLiftVal ← trySynthInstance monadLiftType | return none
let u_1 ← getDecLevel α
let u_2 ← getDecLevel eType
let u_3 ← getDecLevel expectedType
let eNew := mkAppN (Lean.mkConst ``liftM [u_1, u_2, u_3]) #[m, n, monadLiftVal, α, e]
let eNewType ← inferType eNew
if (← isDefEq expectedType eNewType) then
return some eNew -- approach 2 worked
else
let some monadInst ← isMonad? n | return none
let u ← getLevel α
let v ← getLevel β
let coeTInstType := Lean.mkForall `a BinderInfo.default α <| mkAppN (mkConst ``CoeT [u, v]) #[α, mkBVar 0, β]
let .some coeTInstVal ← trySynthInstance coeTInstType | return none
let eNew ← expandCoe (mkAppN (Lean.mkConst ``Lean.Internal.liftCoeM [u_1, u_2, u_3]) #[m, n, α, β, monadLiftVal, coeTInstVal, monadInst, e])
let eNewType ← inferType eNew
unless (← isDefEq expectedType eNewType) do return none
return some eNew -- approach 3 worked
catch _ =>
/- If `m` is not a monad, then we try to use `tryCoe?`. -/
return none
else
return none
/-- Coerces `expr` to the type `expectedType`.
Returns `.some coerced` on successful coercion,
`.none` if the expression cannot by coerced to that type,
or `.undef` if we need more metavariable assignments. -/
def coerce? (expr expectedType : Expr) : MetaM (LOption Expr) := do
if let some lifted ← coerceMonadLift? expr expectedType then
return .some lifted
if (← whnfR expectedType).isForall then
if let some fn ← coerceToFunction? expr then
if ← isDefEq (← inferType fn) expectedType then
return .some fn
coerceSimple? expr expectedType
end Lean.Meta
|
d951ef4972e9baa4da8c7b6134bf8c7a6caaea86 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/lean/PPRoundtrip.lean | 6762e60e2a15ca01b82aac144ef944f437d99e9f | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,516 | lean | import Lean
open Lean
open Lean.Elab
open Lean.Elab.Term
open Lean.Elab.Command
open Lean.Format
open Lean.Meta
def check (stx : TermElabM Syntax) (optionsPerPos : OptionsPerPos := {}) : TermElabM Unit := do
stx ← stx;
e ← elabTermAndSynthesize stx none <* throwErrorIfErrors;
stx' ← liftMetaM $ delab e optionsPerPos;
stx' ← liftCoreM $ PrettyPrinter.parenthesizeTerm stx';
f' ← liftCoreM $ PrettyPrinter.formatTerm stx';
IO.println $ toString f';
env ← getEnv;
match Parser.runParserCategory env `term (toString f') "<input>" with
| Except.error e => throwErrorAt stx e
| Except.ok stx'' => do
e' ← elabTermAndSynthesize stx'' none <* throwErrorIfErrors;
unlessM (Term.isDefEq e e') $
throwErrorAt stx (fmt "failed to round-trip" ++ line ++ fmt e ++ line ++ fmt e')
-- set_option trace.PrettyPrinter.parenthesize true
-- #eval check `(?m) -- fails round-trip
#eval check `(Sort)
#eval check `(Type)
#eval check `(Type 0)
#eval check `(Type 1)
-- can't add a new universe variable inside a term...
#eval check `(Type _)
#eval check `(Type (_ + 2))
#eval check `(Nat)
#eval check `(List Nat)
#eval check `(id Nat)
#eval check `(id (id (id Nat)))
section
set_option pp.explicit true
#eval check `(List Nat)
#eval check `(id Nat)
end
section
set_option pp.universes true
#eval check `(List Nat)
#eval check `(id Nat)
end
#eval check `(id (id Nat)) (Std.RBMap.empty.insert 4 $ KVMap.empty.insert `pp.explicit true)
-- specify the expected type of `a` in a way that is not erased by the delaborator
def typeAs.{u} (α : Type u) (a : α) := ()
#eval check `(fun (a : Nat) => a)
#eval check `(fun (a b : Nat) => a)
#eval check `(fun (a : Nat) (b : Bool) => a)
#eval check `(fun {a b : Nat} => a)
-- implicit lambdas work as long as the expected type is preserved
#eval check `(typeAs ({α : Type} → (a : α) → α) fun a => a)
section
set_option pp.explicit true
#eval check `(fun {α : Type} [HasToString α] (a : α) => toString a)
end
#eval check `((α : Type) → α)
#eval check `((α β : Type) → α) -- group
#eval check `((α β : Type) → Type) -- don't group
#eval check `((α : Type) → (a : α) → α)
#eval check `((α : Type) → (a : α) → a = a)
#eval check `({α : Type} → α)
#eval check `({α : Type} → [HasToString α] → α)
-- TODO: hide `ofNat`
#eval check `(0)
#eval check `(1)
#eval check `(42)
#eval check `("hi")
#eval check `((1,2).fst)
#eval check `(1 < 2 || true)
#eval check `(id (fun a => a) 0)
|
22f02e086b077aeac817ce81760878cb1bd26c24 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/intro_under.lean | d863da66afd8d08014ffb07fc1029ea5acd65585 | [
"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 | 87 | lean | example (p q r : Prop) : p → q → r → r :=
begin
intro _ _ Hr,
assumption
end
|
27032bf36fcff8a13ee023b1b6d54873db490cb6 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/analysis/calculus/lagrange_multipliers.lean | c800f44d37cb5dd3c05fb094cecd40749667c5e8 | [
"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 | 6,793 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.calculus.local_extr
import analysis.calculus.implicit
import linear_algebra.dual
/-!
# Lagrange multipliers
In this file we formalize the
[Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) method of solving
conditional extremum problems: if a function `φ` has a local extremum at `x₀` on the set
`f ⁻¹' {f x₀}`, `f x = (f₀ x, ..., fₙ₋₁ x)`, then the differentials of `fₖ` and `φ` are linearly
dependent. First we formulate a geometric version of this theorem which does not rely on the
target space being `ℝⁿ`, then restate it in terms of coordinates.
## TODO
Formalize Karush-Kuhn-Tucker theorem
## Tags
lagrange multiplier, local extremum
-/
open filter set
open_locale topological_space filter big_operators
variables {E F : Type*} [normed_group E] [normed_space ℝ E] [complete_space E]
[normed_group F] [normed_space ℝ F] [complete_space F]
{f : E → F} {φ : E → ℝ} {x₀ : E} {f' : E →L[ℝ] F} {φ' : E →L[ℝ] ℝ}
/-- Lagrange multipliers theorem: if `φ : E → ℝ` has a local extremum on the set `{x | f x = f x₀}`
at `x₀`, both `f : E → F` and `φ` are strictly differentiable at `x₀`, and the codomain of `f` is
a complete space, then the linear map `x ↦ (f' x, φ' x)` is not surjective. -/
lemma is_local_extr_on.range_ne_top_of_has_strict_fderiv_at
(hextr : is_local_extr_on φ {x | f x = f x₀} x₀) (hf' : has_strict_fderiv_at f f' x₀)
(hφ' : has_strict_fderiv_at φ φ' x₀) :
(f'.prod φ').range ≠ ⊤ :=
begin
intro htop,
set fφ := λ x, (f x, φ x),
have A : map φ (𝓝[f ⁻¹' {f x₀}] x₀) = 𝓝 (φ x₀),
{ change map (prod.snd ∘ fφ) (𝓝[fφ ⁻¹' {p | p.1 = f x₀}] x₀) = 𝓝 (φ x₀),
rw [← map_map, nhds_within, map_inf_principal_preimage,
(hf'.prod hφ').map_nhds_eq_of_surj htop],
exact map_snd_nhds_within _ },
exact hextr.not_nhds_le_map A.ge
end
/-- Lagrange multipliers theorem: if `φ : E → ℝ` has a local extremum on the set `{x | f x = f x₀}`
at `x₀`, both `f : E → F` and `φ` are strictly differentiable at `x₀`, and the codomain of `f` is
a complete space, then there exist `Λ : dual ℝ F` and `Λ₀ : ℝ` such that `(Λ, Λ₀) ≠ 0` and
`Λ (f' x) + Λ₀ • φ' x = 0` for all `x`. -/
lemma is_local_extr_on.exists_linear_map_of_has_strict_fderiv_at
(hextr : is_local_extr_on φ {x | f x = f x₀} x₀) (hf' : has_strict_fderiv_at f f' x₀)
(hφ' : has_strict_fderiv_at φ φ' x₀) :
∃ (Λ : module.dual ℝ F) (Λ₀ : ℝ), (Λ, Λ₀) ≠ 0 ∧ ∀ x, Λ (f' x) + Λ₀ • φ' x = 0 :=
begin
rcases submodule.exists_le_ker_of_lt_top _
(lt_top_iff_ne_top.2 $ hextr.range_ne_top_of_has_strict_fderiv_at hf' hφ') with ⟨Λ', h0, hΛ'⟩,
set e : ((F →ₗ[ℝ] ℝ) × ℝ) ≃ₗ[ℝ] (F × ℝ →ₗ[ℝ] ℝ) :=
((linear_equiv.refl ℝ (F →ₗ[ℝ] ℝ)).prod (linear_map.ring_lmap_equiv_self ℝ ℝ ℝ).symm).trans
(linear_map.coprod_equiv ℝ),
rcases e.surjective Λ' with ⟨⟨Λ, Λ₀⟩, rfl⟩,
refine ⟨Λ, Λ₀, e.map_ne_zero_iff.1 h0, λ x, _⟩,
convert linear_map.congr_fun (linear_map.range_le_ker_iff.1 hΛ') x using 1,
-- squeezed `simp [mul_comm]` to speed up elaboration
simp only [linear_map.coprod_equiv_apply, linear_equiv.refl_apply,
linear_map.ring_lmap_equiv_self_symm_apply, linear_map.comp_apply,
continuous_linear_map.coe_coe, continuous_linear_map.prod_apply,
linear_equiv.trans_apply, linear_equiv.prod_apply, linear_map.coprod_apply,
linear_map.smul_right_apply, linear_map.one_apply, smul_eq_mul, mul_comm]
end
/-- Lagrange multipliers theorem. Let `f : ι → E → ℝ` be a finite family of functions.
Suppose that `φ : E → ℝ` has a local extremum on the set `{x | ∀ i, f i x = f i x₀}` at `x₀`.
Suppose that all functions `f i` as well as `φ` are strictly differentiable at `x₀`.
Then the derivatives `f' i : E → L[ℝ] ℝ` and `φ' : E →L[ℝ] ℝ` are linearly dependent:
there exist `Λ : ι → ℝ` and `Λ₀ : ℝ`, `(Λ, Λ₀) ≠ 0`, such that `∑ i, Λ i • f' i + Λ₀ • φ' = 0`.
See also `is_local_extr_on.linear_dependent_of_has_strict_fderiv_at` for a version that
states `¬linear_independent ℝ _` instead of existence of `Λ` and `Λ₀`. -/
lemma is_local_extr_on.exists_multipliers_of_has_strict_fderiv_at {ι : Type*} [fintype ι]
{f : ι → E → ℝ} {f' : ι → E →L[ℝ] ℝ}
(hextr : is_local_extr_on φ {x | ∀ i, f i x = f i x₀} x₀)
(hf' : ∀ i, has_strict_fderiv_at (f i) (f' i) x₀)
(hφ' : has_strict_fderiv_at φ φ' x₀) :
∃ (Λ : ι → ℝ) (Λ₀ : ℝ), (Λ, Λ₀) ≠ 0 ∧ ∑ i, Λ i • f' i + Λ₀ • φ' = 0 :=
begin
letI := classical.dec_eq ι,
replace hextr : is_local_extr_on φ {x | (λ i, f i x) = (λ i, f i x₀)} x₀,
by simpa only [function.funext_iff] using hextr,
rcases hextr.exists_linear_map_of_has_strict_fderiv_at
(has_strict_fderiv_at_pi.2 (λ i, hf' i)) hφ'
with ⟨Λ, Λ₀, h0, hsum⟩,
rcases (linear_equiv.pi_ring ℝ ℝ ι ℝ).symm.surjective Λ with ⟨Λ, rfl⟩,
refine ⟨Λ, Λ₀, _, _⟩,
{ simpa only [ne.def, prod.ext_iff, linear_equiv.map_eq_zero_iff, prod.fst_zero] using h0 },
{ ext x, simpa [mul_comm] using hsum x }
end
/-- Lagrange multipliers theorem. Let `f : ι → E → ℝ` be a finite family of functions.
Suppose that `φ : E → ℝ` has a local extremum on the set `{x | ∀ i, f i x = f i x₀}` at `x₀`.
Suppose that all functions `f i` as well as `φ` are strictly differentiable at `x₀`.
Then the derivatives `f' i : E → L[ℝ] ℝ` and `φ' : E →L[ℝ] ℝ` are linearly dependent.
See also `is_local_extr_on.exists_multipliers_of_has_strict_fderiv_at` for a version that
that states existence of Lagrange multipliers `Λ` and `Λ₀` instead of using
`¬linear_independent ℝ _` -/
lemma is_local_extr_on.linear_dependent_of_has_strict_fderiv_at {ι : Type*} [fintype ι]
{f : ι → E → ℝ} {f' : ι → E →L[ℝ] ℝ}
(hextr : is_local_extr_on φ {x | ∀ i, f i x = f i x₀} x₀)
(hf' : ∀ i, has_strict_fderiv_at (f i) (f' i) x₀)
(hφ' : has_strict_fderiv_at φ φ' x₀) :
¬linear_independent ℝ (λ i, option.elim i φ' f' : option ι → E →L[ℝ] ℝ) :=
begin
rw [fintype.linear_independent_iff], push_neg,
rcases hextr.exists_multipliers_of_has_strict_fderiv_at hf' hφ' with ⟨Λ, Λ₀, hΛ, hΛf⟩,
refine ⟨λ i, option.elim i Λ₀ Λ, _, _⟩,
{ simpa [add_comm] using hΛf },
{ simpa [function.funext_iff, not_and_distrib, or_comm, option.exists] using hΛ }
end
|
51069a690aa21e4b386aae52d4613a5158241ba4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/group_theory/monoid_localization.lean | b797ad903101ecf70610b95fff17954b1974c393 | [
"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 | 67,356 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston
-/
import group_theory.congruence
import group_theory.submonoid.membership
import algebra.group.units
/-!
# Localizations of commutative monoids
Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so
we can generalize localizations to commutative monoids.
We characterize the localization of a commutative monoid `M` at a submonoid `S` up to
isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a
monoid homomorphism `f : M →* N` satisfying 3 properties:
1. For all `y ∈ S`, `f y` is a unit;
2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`;
3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`.
Given such a localization map `f : M →* N`, we can define the surjection
`localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and
`localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps
elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids
`P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism
`g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations,
`localization_map.map`, from `N` to `Q`.
We treat the special case of localizing away from an element in the sections `away_map` and `away`.
We also define the quotient of `M × S` by the unique congruence relation (equivalence relation
preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S`
satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s`
whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard
localization relation.
This defines the localization as a quotient type, `localization`, but the majority of
subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps
which satisfy the characteristic predicate.
## Implementation notes
In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one
structure with an isomorphic one; one way around this is to isolate a predicate characterizing
a structure up to isomorphism, and reason about things that satisfy the predicate.
The infimum form of the localization congruence relation is chosen as 'canonical' here, since it
shortens some proofs.
To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for
this structure.
To reason about the localization as a quotient type, use `mk_eq_monoid_of_mk'` and associated
lemmas. These show the quotient map `mk : M → S → localization S` equals the
surjection `localization_map.mk'` induced by the map
`monoid_of : localization_map S (localization S)` (where `of` establishes the
localization as a quotient type satisfies the characteristic predicate). The lemma
`mk_eq_monoid_of_mk'` hence gives you access to the results in the rest of the file, which are
about the `localization_map.mk'` induced by any localization map.
## Tags
localization, monoid localization, quotient monoid, congruence relation, characteristic predicate,
commutative monoid
-/
namespace add_submonoid
variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N]
/-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_nonempty_instance] structure localization_map
extends add_monoid_hom M N :=
(map_add_units' : ∀ y : S, is_add_unit (to_fun y))
(surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1)
(eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c)
/-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/
add_decl_doc localization_map.to_add_monoid_hom
end add_submonoid
section comm_monoid
variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N]
{P : Type*} [comm_monoid P]
namespace submonoid
/-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_nonempty_instance] structure localization_map
extends monoid_hom M N :=
(map_units' : ∀ y : S, is_unit (to_fun y))
(surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1)
(eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c)
attribute [to_additive add_submonoid.localization_map] submonoid.localization_map
attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom]
submonoid.localization_map.to_monoid_hom
/-- The monoid hom underlying a `localization_map`. -/
add_decl_doc localization_map.to_monoid_hom
end submonoid
namespace localization
run_cmd to_additive.map_namespace `localization `add_localization
/-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose
quotient is the localization of `M` at `S`, defined as the unique congruence relation on
`M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`,
`(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies
`(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/
@[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S`
an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique
congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where
for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies
`(x₁, y₁) ∼ (x₂, y₂)` by `s`."]
def r (S : submonoid M) : con (M × S) :=
Inf {c | ∀ y : S, c 1 (y, y)}
/-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a
submonoid of `M`, whose quotient is the localization of `M` at `S`. -/
@[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and
`S` a submonoid of `M`, whose quotient is the localization of `M` at `S`."]
def r' : con (M × S) :=
begin
refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c,
iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩,
.. },
{ rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use b.2 * t₁ * t₂,
simp only [submonoid.coe_mul],
calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl
... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl }
... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } },
{ rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use t₁ * t₂,
calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) :
by ac_refl
... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } }
end
/-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed
equivalently as an infimum (see `localization.r`) or explicitly
(see `localization.r'`). -/
@[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a
submonoid can be expressed equivalently as an infimum (see `add_localization.r`) or
explicitly (see `add_localization.r'`)."]
theorem r_eq_r' : r S = r' S :=
le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $
le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩,
begin
rw [← mul_one (p, q), ← mul_one y],
refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _,
convert b.symm (b.mul (b.refl y) (H (q * t))) using 1,
rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc],
refl
end
variables {S}
@[to_additive]
lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c :=
by rw r_eq_r' S; refl
end localization
/-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/
@[to_additive add_localization "The localization of an `add_comm_monoid` at one
of its submonoids (as a quotient type)."]
def localization := (localization.r S).quotient
namespace localization
@[to_additive] instance inhabited :
inhabited (localization S) :=
con.quotient.inhabited
/-- Multiplication in a localization is defined as `⟨a, b⟩ * ⟨c, d⟩ = ⟨a * c, b * d⟩`. -/
@[to_additive "Addition in an `add_localization` is defined as `⟨a, b⟩ + ⟨c, d⟩ = ⟨a + c, b + d⟩`.
Should not be confused with the ring localization counterpart `localization.add`, which maps
`⟨a, b⟩ + ⟨c, d⟩` to `⟨d * a + b * c, b * d⟩`.", irreducible]
protected def mul : localization S → localization S → localization S :=
(r S).comm_monoid.mul
@[to_additive] instance : has_mul (localization S) :=
⟨localization.mul S⟩
/-- The identity element of a localization is defined as `⟨1, 1⟩`. -/
@[to_additive "The identity element of an `add_localization` is defined as `⟨0, 0⟩`.
Should not be confused with the ring localization counterpart `localization.zero`,
which is defined as `⟨0, 1⟩`.", irreducible] protected def one : localization S :=
(r S).comm_monoid.one
@[to_additive] instance : has_one (localization S) :=
⟨localization.one S⟩
/-- Exponentiation in a localization is defined as `⟨a, b⟩ ^ n = ⟨a ^ n, b ^ n⟩`.
This is a separate `irreducible` def to ensure the elaborator doesn't waste its time
trying to unify some huge recursive definition with itself, but unfolded one step less.
-/
@[to_additive
"Multiplication with a natural in an `add_localization` is defined as `n • ⟨a, b⟩ = ⟨n • a, n • b⟩`.
This is a separate `irreducible` def to ensure the elaborator doesn't waste its time
trying to unify some huge recursive definition with itself, but unfolded one step less.",
irreducible]
protected def npow : ℕ → localization S → localization S :=
(r S).comm_monoid.npow
local attribute [semireducible] localization.mul localization.one localization.npow
@[to_additive] instance : comm_monoid (localization S) :=
{ mul := (*),
one := 1,
mul_assoc :=
show ∀ (x y z : localization S), x * y * z = x * (y * z), from (r S).comm_monoid.mul_assoc,
mul_comm := show ∀ (x y : localization S), x * y = y * x, from (r S).comm_monoid.mul_comm,
mul_one := show ∀ (x : localization S), x * 1 = x, from (r S).comm_monoid.mul_one,
one_mul := show ∀ (x : localization S), 1 * x = x, from (r S).comm_monoid.one_mul,
npow := localization.npow S,
npow_zero' := show ∀ (x : localization S), localization.npow S 0 x = 1, from pow_zero,
npow_succ' := show ∀ (n : ℕ) (x : localization S),
localization.npow S n.succ x = x * localization.npow S n x, from λ n x, pow_succ x n }
variables {S}
/-- Given a `comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence
class of `(x, y)` in the localization of `M` at `S`. -/
@[to_additive "Given an `add_comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to
the equivalence class of `(x, y)` in the localization of `M` at `S`."]
def mk (x : M) (y : S) : localization S := (r S).mk' (x, y)
@[to_additive] theorem mk_eq_mk_iff {a c : M} {b d : S} :
mk a b = mk c d ↔ r S ⟨a, b⟩ ⟨c, d⟩ :=
(r S).eq
universes u
/-- Dependent recursion principle for localizations: given elements `f a b : p (mk a b)`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions),
then `f` is defined on the whole `localization S`. -/
@[elab_as_eliminator, to_additive
"Dependent recursion principle for `add_localizations`: given elements `f a b : p (mk a b)`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions),
then `f` is defined on the whole `add_localization S`."]
def rec {p : localization S → Sort u}
(f : ∀ (a : M) (b : S), p (mk a b))
(H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)),
(eq.rec (f a b) (mk_eq_mk_iff.mpr h) : p (mk c d)) = f c d)
(x) : p x :=
quot.rec (λ y, eq.rec (f y.1 y.2) (prod.mk.eta : (y.1, y.2) = y))
(λ y z h, by { cases y, cases z, exact H h }) x
attribute [irreducible] localization
@[to_additive] lemma mk_mul (a c : M) (b d : S) : mk a b * mk c d = mk (a * c) (b * d) := rfl
@[to_additive] lemma mk_one : mk 1 (1 : S) = 1 := rfl
@[to_additive] lemma mk_pow (n : ℕ) (a : M) (b : S) : (mk a b) ^ n = mk (a ^ n) (b ^ n) := rfl
@[simp, to_additive] lemma rec_mk {p : localization S → Sort u}
(f : ∀ (a : M) (b : S), p (mk a b)) (H) (a : M) (b : S) :
(rec f H (mk a b) : p (mk a b)) = f a b :=
rfl
/-- Non-dependent recursion principle for localizations: given elements `f a b : p`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`,
then `f` is defined on the whole `localization S`. -/
@[elab_as_eliminator, to_additive
"Non-dependent recursion principle for `add_localizations`: given elements `f a b : p`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`,
then `f` is defined on the whole `localization S`."]
def lift_on {p : Sort u} (x : localization S) (f : M → S → p)
(H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)), f a b = f c d) : p :=
rec f (λ a c b d h, by rw [eq_rec_constant, H h]) x
@[to_additive] lemma lift_on_mk {p : Sort u}
(f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) :
lift_on (mk a b) f H = f a b :=
rfl
@[elab_as_eliminator, to_additive]
theorem ind {p : localization S → Prop}
(H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x :=
rec (λ a b, H (a, b)) (λ _ _ _ _ _, rfl) x
@[elab_as_eliminator, to_additive]
theorem induction_on {p : localization S → Prop} (x)
(H : ∀ (y : M × S), p (mk y.1 y.2)) : p x := ind H x
/-- Non-dependent recursion principle for localizations: given elements `f x y : p`
for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`,
then `f` is defined on the whole `localization S`. -/
@[elab_as_eliminator, to_additive
"Non-dependent recursion principle for localizations: given elements `f x y : p`
for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`,
then `f` is defined on the whole `localization S`."]
def lift_on₂ {p : Sort u} (x y : localization S) (f : M → S → M → S → p)
(H : ∀ {a a' b b' c c' d d'} (hx : r S (a, b) (a', b')) (hy : r S (c, d) (c', d')),
f a b c d = f a' b' c' d') :
p :=
lift_on x (λ a b, lift_on y (f a b) (λ c c' d d' hy, H ((r S).refl _) hy))
(λ a a' b b' hx, induction_on y (λ ⟨c, d⟩, H hx ((r S).refl _)))
@[to_additive] lemma lift_on₂_mk {p : Sort*}
(f : M → S → M → S → p) (H) (a c : M) (b d : S) :
lift_on₂ (mk a b) (mk c d) f H = f a b c d :=
rfl
@[elab_as_eliminator, to_additive]
theorem induction_on₂ {p : localization S → localization S → Prop} (x y)
(H : ∀ (x y : M × S), p (mk x.1 x.2) (mk y.1 y.2)) : p x y :=
induction_on x $ λ x, induction_on y $ H x
@[elab_as_eliminator, to_additive]
theorem induction_on₃
{p : localization S → localization S → localization S → Prop} (x y z)
(H : ∀ (x y z : M × S), p (mk x.1 x.2) (mk y.1 y.2) (mk z.1 z.2)) : p x y z :=
induction_on₂ x y $ λ x y, induction_on z $ H x y
@[to_additive] lemma one_rel (y : S) : r S 1 (y, y) := λ b hb, hb y
@[to_additive] theorem r_of_eq {x y : M × S} (h : y.1 * x.2 = x.1 * y.2) : r S x y :=
r_iff_exists.2 ⟨1, by rw h⟩
@[to_additive] lemma mk_self (a : S) : mk (a : M) a = 1 :=
by { symmetry, rw [← mk_one, mk_eq_mk_iff], exact one_rel a }
section scalar
variables {R R₁ R₂ : Type*}
/-- Scalar multiplication in a monoid localization is defined as `c • ⟨a, b⟩ = ⟨c • a, b⟩`. -/
@[irreducible] protected def smul [has_smul R M] [is_scalar_tower R M M]
(c : R) (z : localization S) : localization S :=
localization.lift_on z (λ a b, mk (c • a) b) $
λ a a' b b' h, mk_eq_mk_iff.2
begin
cases b with b hb,
cases b' with b' hb',
rw r_eq_r' at h ⊢,
cases h with t ht,
use t,
simp only [smul_mul_assoc, ht]
end
instance [has_smul R M] [is_scalar_tower R M M] :
has_smul R (localization S) :=
{ smul := localization.smul }
lemma smul_mk [has_smul R M] [is_scalar_tower R M M] (c : R) (a b) :
c • (mk a b : localization S) = mk (c • a) b :=
by { unfold has_smul.smul localization.smul, apply lift_on_mk }
instance [has_smul R₁ M] [has_smul R₂ M] [is_scalar_tower R₁ M M] [is_scalar_tower R₂ M M]
[smul_comm_class R₁ R₂ M] : smul_comm_class R₁ R₂ (localization S) :=
{ smul_comm := λ s t, localization.ind $ prod.rec $ by exact λ r x,
by simp only [smul_mk, smul_comm s t r] }
instance [has_smul R₁ M] [has_smul R₂ M] [is_scalar_tower R₁ M M] [is_scalar_tower R₂ M M]
[has_smul R₁ R₂] [is_scalar_tower R₁ R₂ M] : is_scalar_tower R₁ R₂ (localization S) :=
{ smul_assoc := λ s t, localization.ind $ prod.rec $ by exact λ r x,
by simp only [smul_mk, smul_assoc s t r] }
instance smul_comm_class_right {R : Type*} [has_smul R M] [is_scalar_tower R M M] :
smul_comm_class R (localization S) (localization S) :=
{ smul_comm := λ s, localization.ind $ prod.rec $ by exact λ r₁ x₁,
localization.ind $ prod.rec $ by exact λ r₂ x₂,
by simp only [smul_mk, smul_eq_mul, mk_mul, mul_comm r₁, smul_mul_assoc] }
instance is_scalar_tower_right {R : Type*} [has_smul R M] [is_scalar_tower R M M] :
is_scalar_tower R (localization S) (localization S) :=
{ smul_assoc := λ s, localization.ind $ prod.rec $ by exact λ r₁ x₁,
localization.ind $ prod.rec $ by exact λ r₂ x₂,
by simp only [smul_mk, smul_eq_mul, mk_mul, smul_mul_assoc] }
instance [has_smul R M] [has_smul Rᵐᵒᵖ M] [is_scalar_tower R M M] [is_scalar_tower Rᵐᵒᵖ M M]
[is_central_scalar R M] : is_central_scalar R (localization S) :=
{ op_smul_eq_smul := λ s, localization.ind $ prod.rec $ by exact λ r x,
by simp only [smul_mk, op_smul_eq_smul] }
instance [monoid R] [mul_action R M] [is_scalar_tower R M M] : mul_action R (localization S) :=
{ one_smul := localization.ind $ prod.rec $
by { intros, simp only [localization.smul_mk, one_smul] },
mul_smul := λ s₁ s₂, localization.ind $ prod.rec $
by { intros, simp only [localization.smul_mk, mul_smul] } }
instance [monoid R] [mul_distrib_mul_action R M] [is_scalar_tower R M M] :
mul_distrib_mul_action R (localization S) :=
{ smul_one := λ s, by simp only [←localization.mk_one, localization.smul_mk, smul_one],
smul_mul := λ s x y, localization.induction_on₂ x y $
prod.rec $ by exact λ r₁ x₁, prod.rec $ by exact λ r₂ x₂,
by simp only [localization.smul_mk, localization.mk_mul, smul_mul']}
end scalar
end localization
variables {S N}
namespace monoid_hom
/-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/
@[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic
predicate."]
def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y))
(H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) :
submonoid.localization_map S N :=
{ map_units' := H1,
surj' := H2,
eq_iff_exists' := H3,
.. f }
end monoid_hom
namespace submonoid
namespace localization_map
/-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/
@[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."]
abbreviation to_map (f : localization_map S N) := f.to_monoid_hom
@[ext, to_additive] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) :
f = g :=
by { rcases f with ⟨⟨⟩⟩, rcases g with ⟨⟨⟩⟩, simp only, exact funext h, }
@[to_additive] lemma ext_iff {f g : localization_map S N} :
f = g ↔ ∀ x, f.to_map x = g.to_map x :=
⟨λ h x, h ▸ rfl, ext⟩
@[to_additive] lemma to_map_injective :
function.injective (@localization_map.to_map _ _ S N _) :=
λ _ _ h, ext $ monoid_hom.ext_iff.1 h
@[to_additive] lemma map_units (f : localization_map S N) (y : S) :
is_unit (f.to_map y) := f.2 y
@[to_additive] lemma surj (f : localization_map S N) (z : N) :
∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.3 z
@[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} :
f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.4 x y
/-- Given a localization map `f : M →* N`, a section function sending `z : N` to some
`(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/
@[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N`
to some `(x, y) : M × S` such that `f x - f y = z`."]
noncomputable def sec (f : localization_map S N) (z : N) : M × S :=
classical.some $ f.surj z
@[to_additive] lemma sec_spec {f : localization_map S N} (z : N) :
z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 :=
classical.some_spec $ f.surj z
@[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) :
f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z :=
by rw [mul_comm, sec_spec]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ Nˣ`, for all
`w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."]
lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : w * ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ = z ↔ w = f y * z :=
by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _;
exact (is_unit.coe_lift_right (f.restrict S) h _).symm
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ Nˣ`, for all
`w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."]
lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : z = w * ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ ↔ z * f y = w :=
by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that
`f(S) ⊆ Nˣ`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have
`f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/
@[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have
`f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."]
lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} :
f x₁ * ↑(is_unit.lift_right (f.restrict S) h y₁)⁻¹ =
f x₂ * ↑(is_unit.lift_right (f.restrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) :=
by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂,
f.map_mul, f.map_mul]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ Nˣ`, for all
`y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."]
lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z}
(h : (is_unit.lift_right (f.restrict S) hf y)⁻¹ = (is_unit.lift_right (f.restrict S) hf z)⁻¹) :
f y = f z :=
by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h];
convert units.inv_mul _; exact (is_unit.coe_lift_right (f.restrict S) hf _).symm
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ Nˣ`, for all
`y ∈ S`, `(f y)⁻¹` is unique. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."]
lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S}
{z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.restrict S) h y)⁻¹ = z :=
by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H]
variables (f : localization_map S N)
@[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) :
f.to_map x = f.to_map y :=
begin
rw [f.to_map.map_mul, f.to_map.map_mul] at h,
cases f.map_units c with u hu,
rw ←hu at h,
exact (units.mul_right_inj u).1 h,
end
@[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) :
f.to_map x = f.to_map y :=
f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h]
/-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to
`f x * (f y)⁻¹`. -/
@[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S`
to `f x - f y`."]
noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N :=
f.to_map x * ↑(is_unit.lift_right (f.to_map.restrict S) f.map_units y)⁻¹
@[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) :
f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ :=
(mul_inv_left f.map_units _ _ _).2 $
show _ = _ * (_ * _ * (_ * _)), by
rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc,
mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units,
submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul];
ac_refl
@[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x :=
by rw [mk', monoid_hom.map_one]; exact mul_one _
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if
`x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/
@[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N`
we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."]
lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z :=
show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z :=
⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩
@[to_additive] lemma mk'_spec (x) (y : S) :
f.mk' x y * f.to_map y = f.to_map x :=
show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_spec' (x) (y : S) :
f.to_map y * f.mk' x y = f.to_map x :=
by rw [mul_comm, mk'_spec]
@[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} :
z = f.mk' x y ↔ z * f.to_map y = f.to_map x :=
⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩
@[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} :
f.mk' x y = z ↔ f.to_map x = z * f.to_map y :=
by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm]
@[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} :
f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) :=
⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc,
mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul],
λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc,
←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩
@[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c :=
f.mk'_eq_iff_eq.trans $ f.eq_iff_exists
@[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) :=
by rw [f.eq, localization.r_iff_exists]
@[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} :
f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y :=
f.eq_iff_exists.trans g.eq_iff_exists.symm
@[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂}
{y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ :=
f.eq'.trans g.eq'.symm
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`,
if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S`
such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M`
and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists
`c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."]
lemma exists_of_sec_mk' (x) (y : S) :
∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c :=
f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm
@[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) :
f.mk' a₁ a₂ = f.mk' b₁ b₂ :=
f.mk'_eq_iff_eq.2 $ H ▸ rfl
@[simp, to_additive] lemma mk'_self' (y : S) :
f.mk' (y : M) y = 1 :=
show _ * _ = _, by rw [mul_inv_left, mul_one]
@[simp, to_additive] lemma mk'_self (x) (H : x ∈ S) :
f.mk' x ⟨x, H⟩ = 1 :=
by convert mk'_self' _ _; refl
@[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y :=
by rw [←mk'_one, ←mk'_mul, one_mul]
@[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y :=
by rw [mul_comm, mul_mk'_eq_mk'_of_mul]
@[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) :
f.to_map x * f.mk' 1 y = f.mk' x y :=
by rw [mul_mk'_eq_mk'_of_mul, mul_one]
@[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) :
f.mk' (x * y) y = f.to_map x :=
by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self', mul_one]
@[to_additive] lemma mk'_mul_cancel_left (x) (y : S) :
f.mk' ((y : M) * x) y = f.to_map x :=
by rw [mul_comm, mk'_mul_cancel_right]
@[to_additive] lemma is_unit_comp (j : N →* P) (y : S) :
is_unit (j.comp f.to_map y) :=
⟨units.map j $ is_unit.lift_right (f.to_map.restrict S) f.map_units y,
show j _ = j _, from congr_arg j $
(is_unit.coe_lift_right (f.to_map.restrict S) f.map_units _)⟩
variables {g : M →* P}
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y`
for all `x y : M`."]
lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) :
g x = g y :=
begin
obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h,
rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.restrict S) hg c],
show _ * (g c * _) = _,
rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm],
end
/-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids
`S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies
`k (g x) = k (g y)`. -/
@[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for
submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y`
implies `k (g x) = k (g y)`."]
lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q]
(hg : ∀ y : S, g y ∈ T) (k : localization_map T Q)
{x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) :=
f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h
variables (hg : ∀ y : S, is_unit (g y))
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from
`N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that
`z = f x * (f y)⁻¹`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism
induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that
`z = f x - f y`."]
noncomputable def lift : N →* P :=
{ to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.restrict S) hg (f.sec z).2)⁻¹,
map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg
(by rw [←sec_spec, one_mul]),
map_mul' := λ x y,
begin
rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg,
mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg],
repeat { rw ←g.map_mul },
exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl)
end }
variables {S g}
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from
`N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism
induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."]
lemma lift_mk' (x y) :
f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.restrict S) hg y)⁻¹ :=
(mul_inv hg).2 $ f.eq_of_eq hg $ by
rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm]
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have
`f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all
`z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma lift_spec (z v) :
f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v :=
mul_inv_left hg _ _ v
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have
`f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all
`z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such
that `z + f y = f x`."]
lemma lift_spec_mul (z w v) :
f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v :=
begin
rw mul_comm,
show _ * (_ * _) = _ ↔ _,
rw [←mul_assoc, mul_inv_left hg, mul_comm],
end
@[to_additive] lemma lift_mk'_spec (x v) (y : S) :
f.lift hg (f.mk' x y) = v ↔ g x = g y * v :=
by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have
`f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we
have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."]
lemma lift_mul_right (z) :
f.lift hg z * g (f.sec z).2 = g (f.sec z).1 :=
show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one]
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have
`g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we
have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."]
lemma lift_mul_left (z) :
g (f.sec z).2 * f.lift hg z = g (f.sec z).1 :=
by rw [mul_comm, lift_mul_right]
@[simp, to_additive] lemma lift_eq (x : M) :
f.lift hg (f.to_map x) = g x :=
by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul])
@[to_additive] lemma lift_eq_iff {x y : M × S} :
f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) :=
by rw [lift_mk', lift_mk', mul_inv hg]
@[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g :=
by ext; exact f.lift_eq hg _
@[simp, to_additive] lemma lift_of_comp (j : N →* P) :
f.lift (f.is_unit_comp j) = j :=
begin
ext,
rw lift_spec,
show j _ = j _ * _,
erw [←j.map_mul, sec_spec'],
end
@[to_additive] lemma epic_of_localization_map {j k : N →* P}
(h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k :=
begin
rw [←f.lift_of_comp j, ←f.lift_of_comp k],
congr' 1 with x, exact h x,
end
@[to_additive] lemma lift_unique {j : N →* P}
(hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j :=
begin
ext,
rw [lift_spec, ←hj, ←hj, ←j.map_mul],
apply congr_arg,
rw ←sec_spec',
end
@[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x :=
monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x
/-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`,
the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P`
induced by `k`. -/
@[simp, to_additive "Given two localization maps `f : M →+ N, k : M →+ P` for a submonoid `S ⊆ M`,
the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P`
induced by `k`."]
lemma lift_left_inverse {k : localization_map S P} (z : N) :
k.lift f.map_units (f.lift k.map_units z) = z :=
begin
rw lift_spec,
cases f.surj z with x hx,
conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx},
rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul],
apply k.eq_of_eq f.map_units,
rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul],
repeat { rw ←k.to_map.map_mul },
apply f.eq_of_eq k.map_units,
repeat { rw f.to_map.map_mul },
rw [sec_spec', ←hx],
ac_refl,
end
@[to_additive] lemma lift_surjective_iff :
function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 :=
begin
split,
{ intros H v,
obtain ⟨z, hz⟩ := H v,
obtain ⟨x, hx⟩ := f.surj z,
use x,
rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)],
erw [is_unit.mul_lift_right_inv (g.restrict S) hg, mul_one] },
{ intros H v,
obtain ⟨x, hx⟩ := H v,
use f.mk' x.1 x.2,
rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] }
end
@[to_additive] lemma lift_injective_iff :
function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y :=
begin
split,
{ intros H x y,
split,
{ exact f.eq_of_eq hg },
{ intro h,
rw [←f.lift_eq hg, ←f.lift_eq hg] at h,
exact H h } },
{ intros H z w h,
obtain ⟨x, hx⟩ := f.surj z,
obtain ⟨y, hy⟩ := f.surj w,
rw [←f.mk'_sec z, ←f.mk'_sec w],
exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) }
end
variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q]
(k : localization_map T Q)
/-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have
`g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the
localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and
`T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such
that `z = f x * (f y)⁻¹`. -/
@[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids
`S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M`
at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps
for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are
such that `z = f x - f y`."]
noncomputable def map : N →* Q :=
@lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩
variables {k}
@[to_additive] lemma map_eq (x) :
f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x
@[simp, to_additive] lemma map_comp :
(f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩
@[to_additive] lemma map_mk' (x) (y : S) :
f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ :=
begin
rw [map, lift_mk', mul_inv_left],
{ show k.to_map (g x) = k.to_map (g y) * _,
rw mul_mk'_eq_mk'_of_mul,
exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm },
end
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
`u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all
`z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such
that `z + f y = f x`."]
lemma map_spec (z u) :
f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u :=
f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then
for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma map_mul_right (z) :
f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) :=
f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all
`z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma map_mul_left (z) :
k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) :=
by rw [mul_comm, f.map_mul_right]
@[simp, to_additive] lemma map_id (z : N) :
f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z :=
f.lift_id z
/-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition
of the induced maps equals the map of localizations induced by `l ∘ g`. -/
@[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations,
the composition of the induced maps equals the map of localizations induced by `l ∘ g`."]
lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R]
(j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) :
(k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j :=
begin
ext z,
show j.to_map _ * _ = j.to_map (l _) * _,
{ rw [mul_inv_left, ←mul_assoc, mul_inv_right],
show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _,
rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul],
exact k.comp_eq_of_eq hl j
(by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) },
end
/-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition
of the induced maps equals the map of localizations induced by `l ∘ g`. -/
@[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations,
the composition of the induced maps equals the map of localizations induced by `l ∘ g`."]
lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R]
(j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) :
k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x :=
by rw ←f.map_comp_map hy j hl; refl
section away_map
variables (x : M)
/-- Given `x : M`, the type of `comm_monoid` homomorphisms `f : M →* N` such that `N`
is isomorphic to the localization of `M` at the submonoid generated by `x`. -/
@[reducible, to_additive "Given `x : M`, the type of `add_comm_monoid` homomorphisms `f : M →+ N`
such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`."]
def away_map (N' : Type*) [comm_monoid N'] :=
localization_map (powers x) N'
variables (F : away_map x N)
/-- Given `x : M` and a localization map `F : M →* N` away from `x`, `inv_self` is `(F x)⁻¹`. -/
noncomputable def away_map.inv_self : N :=
F.mk' 1 ⟨x, mem_powers _⟩
/-- Given `x : M`, a localization map `F : M →* N` away from `x`, and a map of `comm_monoid`s
`g : M →* P` such that `g x` is invertible, the homomorphism induced from `N` to `P` sending
`z : N` to `g y * (g x)⁻ⁿ`, where `y : M, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/
noncomputable def away_map.lift (hg : is_unit (g x)) : N →* P :=
F.lift $ λ y, show is_unit (g y.1),
begin
obtain ⟨n, hn⟩ := y.2,
rw [←hn, g.map_pow],
exact is_unit.pow n hg,
end
@[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : M) :
F.lift x hg (F.to_map a) = g a := lift_eq _ _ _
@[simp] lemma away_map.lift_comp (hg : is_unit (g x)) :
(F.lift x hg).comp F.to_map = g := lift_comp _ _
/-- Given `x y : M` and localization maps `F : M →* N, G : M →* P` away from `x` and `x * y`
respectively, the homomorphism induced from `N` to `P`. -/
noncomputable def away_to_away_right (y : M) (G : away_map (x * y) P) : N →* P :=
F.lift x $ show is_unit (G.to_map x), from
is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, mem_powers _⟩) $
by rw [mul_mk'_eq_mk'_of_mul, mk'_self]
end away_map
end localization_map
end submonoid
namespace add_submonoid
namespace localization_map
section away_map
variables {A : Type*} [add_comm_monoid A] (x : A) {B : Type*}
[add_comm_monoid B] (F : away_map x B) {C : Type*} [add_comm_monoid C] {g : A →+ C}
/-- Given `x : A` and a localization map `F : A →+ B` away from `x`, `neg_self` is `- (F x)`. -/
noncomputable def away_map.neg_self : B :=
F.mk' 0 ⟨x, mem_multiples _⟩
/-- Given `x : A`, a localization map `F : A →+ B` away from `x`, and a map of `add_comm_monoid`s
`g : A →+ C` such that `g x` is invertible, the homomorphism induced from `B` to `C` sending
`z : B` to `g y - n • g x`, where `y : A, n : ℕ` are such that `z = F y - n • F x`. -/
noncomputable def away_map.lift (hg : is_add_unit (g x)) : B →+ C :=
F.lift $ λ y, show is_add_unit (g y.1),
begin
obtain ⟨n, hn⟩ := y.2,
rw ← hn,
dsimp,
rw [g.map_nsmul],
exact is_add_unit.map (nsmul_add_monoid_hom n : C →+ C) hg,
end
@[simp] lemma away_map.lift_eq (hg : is_add_unit (g x)) (a : A) :
F.lift x hg (F.to_map a) = g a := lift_eq _ _ _
@[simp] lemma away_map.lift_comp (hg : is_add_unit (g x)) :
(F.lift x hg).comp F.to_map = g := lift_comp _ _
/-- Given `x y : A` and localization maps `F : A →+ B, G : A →+ C` away from `x` and `x + y`
respectively, the homomorphism induced from `B` to `C`. -/
noncomputable def away_to_away_right (y : A) (G : away_map (x + y) C) : B →+ C :=
F.lift x $ show is_add_unit (G.to_map x), from
is_add_unit_of_add_eq_zero (G.to_map x) (G.mk' y ⟨x + y, mem_multiples _⟩) $
by rw [add_mk'_eq_mk'_of_add, mk'_self]
end away_map
end localization_map
end add_submonoid
namespace submonoid
namespace localization_map
variables (f : S.localization_map N) {g : M →* P} (hg : ∀ (y : S), is_unit (g y))
{T : submonoid P} {Q : Type*} [comm_monoid Q]
/-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an
isomorphism of `N` and `P`. -/
@[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`,
we get an isomorphism of `N` and `R`."]
noncomputable def mul_equiv_of_localizations
(k : localization_map S P) : N ≃* P :=
⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse,
k.lift_left_inverse, monoid_hom.map_mul _⟩
@[simp, to_additive] lemma mul_equiv_of_localizations_apply
{k : localization_map S P} {x} :
f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl
@[simp, to_additive] lemma mul_equiv_of_localizations_symm_apply
{k : localization_map S P} {x} :
(f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl
@[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations
{k : localization_map S P} :
(k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl
/-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism
of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/
@[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an
isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."]
def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P :=
(k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y)
(λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in
let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩)
(λ x y, k.apply_eq_iff_eq.trans f.eq_iff_exists)
@[simp, to_additive] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) :
(f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl
@[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} :
(f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl
@[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) :
k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x :=
k.symm_apply_apply (f.to_map x)
@[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) :
k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x :=
k.apply_symm_apply (f.to_map x)
@[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} :
(f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y :=
k.to_equiv.eq_symm_apply.symm
@[to_additive add_equiv_of_localizations_right_inv]
lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) :
f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k :=
to_map_injective $ f.lift_comp k.map_units
@[simp, to_additive add_equiv_of_localizations_right_inv_apply]
lemma mul_equiv_of_localizations_right_inv_apply
{k : localization_map S P} {x} :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x :=
ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x
@[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) :
f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k :=
mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom
@[simp, to_additive] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) :
f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x :=
by rw mul_equiv_of_localizations_left_inv
@[simp, to_additive] lemma of_mul_equiv_of_localizations_id :
f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f :=
by ext; refl
@[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} :
(f.of_mul_equiv_of_localizations (k.trans j)).to_map =
j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map :=
by ext; refl
/-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization
map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k`
is a localization map for `T`. -/
@[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is
a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that
`k(T) = S`, `f ∘ k` is a localization map for `T`."]
def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) :
localization_map T N :=
let H' : S.comap k.to_monoid_hom = T :=
H ▸ (set_like.coe_injective $ T.1.preimage_image_eq k.to_equiv.injective) in
(f.to_map.comp k.to_monoid_hom).to_localization_map
(λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩)
(λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in
let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩),
show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩)
(λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists;
exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in
⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc;
exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩,
by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩)
@[simp, to_additive] lemma of_mul_equiv_of_dom_apply
{k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl
@[to_additive] lemma of_mul_equiv_of_dom_eq
{k : P ≃* M} (H : T.map k.to_monoid_hom = S) :
(f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl
@[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M}
(H : T.map k.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x :=
congr_arg f.to_map $ k.apply_symm_apply x
@[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P}
(H : T.map k.symm.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x :=
congr_arg f.to_map $ k.symm_apply_apply x
/-- A special case of `f ∘ id = f`, `f` a localization map. -/
@[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."]
lemma of_mul_equiv_of_dom_id :
f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from
submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f :=
by ext; refl
/-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an
isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations
`N ≃* U`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively,
an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of
localizations `N ≃+ U`."]
noncomputable def mul_equiv_of_mul_equiv
(k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
N ≃* Q :=
f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
f.mul_equiv_of_mul_equiv k H x =
f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl
@[to_additive] lemma mul_equiv_of_mul_equiv_eq_map
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
(f.mul_equiv_of_mul_equiv k H).to_monoid_hom =
f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q}
{j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) :=
f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q}
{j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) :
f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ :=
f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _
@[simp, to_additive] lemma of_mul_equiv_of_mul_equiv_apply
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) :=
ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x
@[to_additive] lemma of_mul_equiv_of_mul_equiv
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map =
k.to_map.comp j.to_monoid_hom :=
monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H
end localization_map
end submonoid
namespace localization
variables (S)
/-- Natural hom sending `x : M`, `M` a `comm_monoid`, to the equivalence class of
`(x, 1)` in the localization of `M` at a submonoid. -/
@[to_additive "Natural homomorphism sending `x : M`, `M` an `add_comm_monoid`, to the equivalence
class of `(x, 0)` in the localization of `M` at a submonoid."]
def monoid_of : submonoid.localization_map S (localization S) :=
{ to_fun := λ x, mk x 1,
map_one' := mk_one,
map_mul' := λ x y, by rw [mk_mul, mul_one],
map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, by rw [mk_mul, mul_one, one_mul, mk_self]⟩,
surj' := λ z, induction_on z $ λ x, ⟨x,
by rw [mk_mul, mul_comm x.fst, ← mk_mul, mk_self, one_mul]⟩,
eq_iff_exists' := λ x y, mk_eq_mk_iff.trans $ r_iff_exists.trans $
show (∃ (c : S), x * 1 * c = y * 1 * c) ↔ _, by rw [mul_one, mul_one],
..(r S).mk'.comp $ monoid_hom.inl M S }
variables {S}
@[to_additive] lemma mk_one_eq_monoid_of_mk (x) : mk x 1 = (monoid_of S).to_map x := rfl
@[to_additive] lemma mk_eq_monoid_of_mk'_apply (x y) : mk x y = (monoid_of S).mk' x y :=
show _ = _ * _, from (submonoid.localization_map.mul_inv_right (monoid_of S).map_units _ _ _).2 $
begin
rw [←mk_one_eq_monoid_of_mk, ←mk_one_eq_monoid_of_mk,
show mk x y * mk y 1 = mk (x * y) (1 * y), by rw [mul_comm 1 y, mk_mul],
show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]],
exact mk_eq_mk_iff.2 (con.symm _ $ (localization.r S).mul
(con.refl _ (x, 1)) $ one_rel _),
end
@[simp, to_additive] lemma mk_eq_monoid_of_mk' : mk = (monoid_of S).mk' :=
funext $ λ _, funext $ λ _, mk_eq_monoid_of_mk'_apply _ _
universes u
@[simp, to_additive] lemma lift_on_mk' {p : Sort u}
(f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) :
lift_on ((monoid_of S).mk' a b) f H = f a b :=
by rw [← mk_eq_monoid_of_mk', lift_on_mk]
@[simp, to_additive] lemma lift_on₂_mk' {p : Sort*}
(f : M → S → M → S → p) (H) (a c : M) (b d : S) :
lift_on₂ ((monoid_of S).mk' a b) ((monoid_of S).mk' c d) f H = f a b c d :=
by rw [← mk_eq_monoid_of_mk', lift_on₂_mk]
variables (f : submonoid.localization_map S N)
/-- Given a localization map `f : M →* N` for a submonoid `S`, we get an isomorphism between
the localization of `M` at `S` as a quotient type and `N`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S`, we get an isomorphism
between the localization of `M` at `S` as a quotient type and `N`."]
noncomputable def mul_equiv_of_quotient (f : submonoid.localization_map S N) :
localization S ≃* N :=
(monoid_of S).mul_equiv_of_localizations f
variables {f}
@[simp, to_additive] lemma mul_equiv_of_quotient_apply (x) :
mul_equiv_of_quotient f x = (monoid_of S).lift f.map_units x := rfl
@[simp, to_additive] lemma mul_equiv_of_quotient_mk' (x y) :
mul_equiv_of_quotient f ((monoid_of S).mk' x y) = f.mk' x y :=
(monoid_of S).lift_mk' _ _ _
@[to_additive] lemma mul_equiv_of_quotient_mk (x y) :
mul_equiv_of_quotient f (mk x y) = f.mk' x y :=
by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_mk' _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_monoid_of (x) :
mul_equiv_of_quotient f ((monoid_of S).to_map x) = f.to_map x :=
(monoid_of S).lift_eq _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_symm_mk' (x y) :
(mul_equiv_of_quotient f).symm (f.mk' x y) = (monoid_of S).mk' x y :=
f.lift_mk' _ _ _
@[to_additive] lemma mul_equiv_of_quotient_symm_mk (x y) :
(mul_equiv_of_quotient f).symm (f.mk' x y) = mk x y :=
by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_symm_mk' _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_symm_monoid_of (x) :
(mul_equiv_of_quotient f).symm (f.to_map x) = (monoid_of S).to_map x :=
f.lift_eq _ _
section away
variables (x : M)
/-- Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient. -/
@[reducible, to_additive "Given `x : M`, the localization of `M` at the submonoid generated
by `x`, as a quotient."]
def away := localization (submonoid.powers x)
/-- Given `x : M`, `inv_self` is `x⁻¹` in the localization (as a quotient type) of `M` at the
submonoid generated by `x`. -/
@[to_additive "Given `x : M`, `neg_self` is `-x` in the localization (as a quotient type) of `M`
at the submonoid generated by `x`."]
def away.inv_self : away x :=
mk 1 ⟨x, submonoid.mem_powers _⟩
/-- Given `x : M`, the natural hom sending `y : M`, `M` a `comm_monoid`, to the equivalence class
of `(y, 1)` in the localization of `M` at the submonoid generated by `x`. -/
@[reducible, to_additive "Given `x : M`, the natural hom sending `y : M`, `M` an `add_comm_monoid`,
to the equivalence class of `(y, 0)` in the localization of `M` at the submonoid
generated by `x`."]
def away.monoid_of : submonoid.localization_map.away_map x (away x) :=
monoid_of (submonoid.powers x)
@[simp, to_additive] lemma away.mk_eq_monoid_of_mk' : mk = (away.monoid_of x).mk' :=
mk_eq_monoid_of_mk'
/-- Given `x : M` and a localization map `f : M →* N` away from `x`, we get an isomorphism between
the localization of `M` at the submonoid generated by `x` as a quotient type and `N`. -/
@[to_additive "Given `x : M` and a localization map `f : M →+ N` away from `x`, we get an
isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type
and `N`."]
noncomputable def away.mul_equiv_of_quotient (f : submonoid.localization_map.away_map x N) :
away x ≃* N :=
mul_equiv_of_quotient f
end away
end localization
end comm_monoid
section comm_monoid_with_zero
variables {M : Type*} [comm_monoid_with_zero M] (S : submonoid M)
(N : Type*) [comm_monoid_with_zero N]
{P : Type*} [comm_monoid_with_zero P]
namespace submonoid
/-- The type of homomorphisms between monoids with zero satisfying the characteristic predicate:
if `f : M →*₀ N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at
`S`. -/
@[nolint has_nonempty_instance] structure localization_with_zero_map
extends localization_map S N :=
(map_zero' : to_fun 0 = 0)
attribute [nolint doc_blame] localization_with_zero_map.to_localization_map
variables {S N}
/-- The monoid with zero hom underlying a `localization_map`. -/
def localization_with_zero_map.to_monoid_with_zero_hom (f : localization_with_zero_map S N) :
M →*₀ N :=
{ .. f }
end submonoid
namespace localization
local attribute [semireducible] localization
/-- The zero element in a localization is defined as `(0, 1)`.
Should not be confused with `add_localization.zero` which is `(0, 0)`. -/
@[irreducible] protected def zero : localization S :=
mk 0 1
instance : has_zero (localization S) :=⟨localization.zero S⟩
local attribute [semireducible] localization.zero localization.mul
instance : comm_monoid_with_zero (localization S) :=
by refine_struct
{ zero := 0, .. localization.comm_monoid S };
exact λ x, localization.induction_on x $ by
{ intros,
refine mk_eq_mk_iff.mpr (r_of_eq _),
simp only [zero_mul, mul_zero] }
attribute [irreducible] localization
variables {S}
lemma mk_zero (x : S) : mk 0 (x : S) = 0 :=
calc mk 0 x = mk 0 1 : mk_eq_mk_iff.mpr (r_of_eq (by simp))
... = 0 : rfl
lemma lift_on_zero {p : Type*} (f : ∀ (x : M) (y : S), p) (H) : lift_on 0 f H = f 0 1 :=
by rw [← mk_zero 1, lift_on_mk]
end localization
variables {S N}
namespace submonoid
@[simp] lemma localization_map.sec_zero_fst {f : localization_map S N} :
f.to_map (f.sec 0).fst = 0 :=
by rw [localization_map.sec_spec', mul_zero]
namespace localization_with_zero_map
/-- Given a localization map `f : M →*₀ N` for a submonoid `S ⊆ M` and a map of
`comm_monoid_with_zero`s `g : M →*₀ P` such that `g y` is invertible for all `y : S`, the
homomorphism induced from `N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S`
are such that `z = f x * (f y)⁻¹`. -/
noncomputable def lift (f : localization_with_zero_map S N)
(g : M →*₀ P) (hg : ∀ y : S, is_unit (g y)) : N →*₀ P :=
{ map_zero' :=
begin
rw [monoid_hom.to_fun_eq_coe, localization_map.lift_spec, mul_zero,
←map_zero g, ←g.to_monoid_hom_coe],
refine f.to_localization_map.eq_of_eq hg _,
rw localization_map.sec_zero_fst,
exact f.to_monoid_with_zero_hom.map_zero.symm
end
.. @localization_map.lift _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg }
end localization_with_zero_map
end submonoid
end comm_monoid_with_zero
|
f247d70d4666f05d1b98a6ece38770a5a2bb4fce | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /tests/lean/run/meta3.lean | dfa85c0872d29210622eb1dee07bf62b30cf99c5 | [
"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 | 1,857 | lean | import Lean.Meta
open Lean
open Lean.Meta
def dbgOpt : Options :=
let opt : Options := {};
let opt := opt.setBool `trace.Meta true;
-- let opt := opt.setBool `trace.Meta.check false;
opt
def print (msg : MessageData) : MetaM Unit :=
trace! `Meta.debug msg
def check (x : MetaM Bool) : MetaM Unit :=
unless (← x) do throwError "check failed"
def getAssignment (m : Expr) : MetaM Expr :=
do let v? ← getExprMVarAssignment? m.mvarId!;
(match v? with
| some v => pure v
| none => throwError "metavariable is not assigned")
unsafe def run (mods : List Name) (x : MetaM Unit) (opts : Options := dbgOpt) : IO Unit :=
withImportModules (mods.map $ fun m => {module := m}) {} 0 fun env => do
let x : MetaM Unit := do { x; printTraces };
discard $ x.toIO { options := opts } { env := env };
pure ()
def nat := mkConst `Nat
def succ := mkConst `Nat.succ
def add := mkAppN (mkConst `Add.add [levelZero]) #[nat, mkConst `Nat.add]
def tst1 : MetaM Unit :=
do let d : DiscrTree Nat := {};
let mvar ← mkFreshExprMVar nat;
let d ← d.insert (mkAppN add #[mvar, mkNatLit 10]) 1;
let d ← d.insert (mkAppN add #[mkNatLit 0, mkNatLit 10]) 2;
let d ← d.insert (mkAppN (mkConst `Nat.add) #[mkNatLit 0, mkNatLit 20]) 3;
let d ← d.insert (mkAppN add #[mvar, mkNatLit 20]) 4;
let d ← d.insert mvar 5;
print (format d);
let vs ← d.getMatch (mkAppN add #[mkNatLit 1, mkNatLit 10]);
print (format vs);
let t := mkAppN add #[mvar, mvar];
print t;
let vs ← d.getMatch t;
print (format vs);
let vs ← d.getUnify t;
print (format vs);
let vs ← d.getUnify mvar;
print (format vs);
let vs ← d.getUnify $ mkAppN add #[mkNatLit 0, mvar];
print (format vs);
let vs ← d.getUnify $ mkAppN add #[mvar, mkNatLit 20];
print (format vs);
pure ()
#eval run [`Init.Data.Nat] tst1
|
1dccf03435b52ae9302cea1735c5a1ae8f5f7a87 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/measure_theory/measure/haar.lean | c988a723a3a6c448b4fc93dff523e600d5caf77d | [
"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 | 38,771 | 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.content
import measure_theory.group.prod
import group_theory.divisible
import topology.algebra.group.compact
/-!
# Haar measure
In this file we prove the existence and uniqueness (up to scalar multiples) of Haar measure
for a locally compact Hausdorff topological group.
For the construction, we follow the write-up by Jonathan Gleason,
*Existence and Uniqueness of Haar Measure*.
This is essentially the same argument as in
https://en.wikipedia.org/wiki/Haar_measure#A_construction_using_compact_subsets.
We construct the Haar measure first on compact sets. For this we define `(K : U)` as the (smallest)
number of left-translates of `U` that are needed to cover `K` (`index` in the formalization).
Then we define a function `h` on compact sets as `lim_U (K : U) / (K₀ : U)`,
where `U` becomes a smaller and smaller open neighborhood of `1`, and `K₀` is a fixed compact set
with nonempty interior. This function is `chaar` in the formalization, and we define the limit
formally using Tychonoff's theorem.
This function `h` forms a content, which we can extend to an outer measure and then a measure
(`haar_measure`).
We normalize the Haar measure so that the measure of `K₀` is `1`.
We show that for second countable spaces any left invariant Borel measure is a scalar multiple of
the Haar measure.
Note that `μ` need not coincide with `h` on compact sets, according to
[halmos1950measure, ch. X, §53 p.233]. However, we know that `h(K)` lies between `μ(Kᵒ)` and `μ(K)`,
where `ᵒ` denotes the interior.
## Main Declarations
* `haar_measure`: the Haar measure on a locally compact Hausdorff group. This is a left invariant
regular measure. It takes as argument a compact set of the group (with non-empty interior),
and is normalized so that the measure of the given set is 1.
* `haar_measure_self`: the Haar measure is normalized.
* `is_left_invariant_haar_measure`: the Haar measure is left invariant.
* `regular_haar_measure`: the Haar measure is a regular measure.
* `is_haar_measure_haar_measure`: the Haar measure satisfies the `is_haar_measure` typeclass, i.e.,
it is invariant and gives finite mass to compact sets and positive mass to nonempty open sets.
* `haar` : some choice of a Haar measure, on a locally compact Hausdorff group, constructed as
`haar_measure K` where `K` is some arbitrary choice of a compact set with nonempty interior.
* `haar_measure_unique`: Every σ-finite left invariant measure on a locally compact Hausdorff group
is a scalar multiple of the Haar measure.
## References
* Paul Halmos (1950), Measure Theory, §53
* Jonathan Gleason, Existence and Uniqueness of Haar Measure
- Note: step 9, page 8 contains a mistake: the last defined `μ` does not extend the `μ` on compact
sets, see Halmos (1950) p. 233, bottom of the page. This makes some other steps (like step 11)
invalid.
* https://en.wikipedia.org/wiki/Haar_measure
-/
noncomputable theory
open set has_inv function topological_space measurable_space
open_locale nnreal classical ennreal pointwise topology
namespace measure_theory
namespace measure
section group
variables {G : Type*} [group G]
/-! We put the internal functions in the construction of the Haar measure in a namespace,
so that the chosen names don't clash with other declarations.
We first define a couple of the functions before proving the properties (that require that `G`
is a topological group). -/
namespace haar
/-- The index or Haar covering number or ratio of `K` w.r.t. `V`, denoted `(K : V)`:
it is the smallest number of (left) translates of `V` that is necessary to cover `K`.
It is defined to be 0 if no finite number of translates cover `K`. -/
@[to_additive add_index "additive version of `measure_theory.measure.haar.index`"]
def index (K V : set G) : ℕ :=
Inf $ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V }
@[to_additive add_index_empty]
lemma index_empty {V : set G} : index ∅ V = 0 :=
begin
simp only [index, nat.Inf_eq_zero], left, use ∅,
simp only [finset.card_empty, empty_subset, mem_set_of_eq, eq_self_iff_true, and_self],
end
variables [topological_space G]
/-- `prehaar K₀ U K` is a weighted version of the index, defined as `(K : U)/(K₀ : U)`.
In the applications `K₀` is compact with non-empty interior, `U` is open containing `1`,
and `K` is any compact set.
The argument `K` is a (bundled) compact set, so that we can consider `prehaar K₀ U` as an
element of `haar_product` (below). -/
@[to_additive "additive version of `measure_theory.measure.haar.prehaar`"]
def prehaar (K₀ U : set G) (K : compacts G) : ℝ := (index (K : set G) U : ℝ) / index K₀ U
@[to_additive]
lemma prehaar_empty (K₀ : positive_compacts G) {U : set G} : prehaar (K₀ : set G) U ⊥ = 0 :=
by rw [prehaar, compacts.coe_bot, index_empty, nat.cast_zero, zero_div]
@[to_additive]
lemma prehaar_nonneg (K₀ : positive_compacts G) {U : set G} (K : compacts G) :
0 ≤ prehaar (K₀ : set G) U K :=
by apply div_nonneg; norm_cast; apply zero_le
/-- `haar_product K₀` is the product of intervals `[0, (K : K₀)]`, for all compact sets `K`.
For all `U`, we can show that `prehaar K₀ U ∈ haar_product K₀`. -/
@[to_additive "additive version of `measure_theory.measure.haar.haar_product`"]
def haar_product (K₀ : set G) : set (compacts G → ℝ) :=
pi univ (λ K, Icc 0 $ index (K : set G) K₀)
@[simp, to_additive]
lemma mem_prehaar_empty {K₀ : set G} {f : compacts G → ℝ} :
f ∈ haar_product K₀ ↔ ∀ K : compacts G, f K ∈ Icc (0 : ℝ) (index (K : set G) K₀) :=
by simp only [haar_product, pi, forall_prop_of_true, mem_univ, mem_set_of_eq]
/-- The closure of the collection of elements of the form `prehaar K₀ U`,
for `U` open neighbourhoods of `1`, contained in `V`. The closure is taken in the space
`compacts G → ℝ`, with the topology of pointwise convergence.
We show that the intersection of all these sets is nonempty, and the Haar measure
on compact sets is defined to be an element in the closure of this intersection. -/
@[to_additive "additive version of `measure_theory.measure.haar.cl_prehaar`"]
def cl_prehaar (K₀ : set G) (V : open_nhds_of (1 : G)) : set (compacts G → ℝ) :=
closure $ prehaar K₀ '' { U : set G | U ⊆ V.1 ∧ is_open U ∧ (1 : G) ∈ U }
variables [topological_group G]
/-!
### Lemmas about `index`
-/
/-- If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined,
there is a finite set `t` satisfying the desired properties. -/
@[to_additive add_index_defined "If `K` is compact and `V` has nonempty interior, then the index
`(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties."]
lemma index_defined {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) :
∃ n : ℕ, n ∈ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V } :=
by { rcases compact_covered_by_mul_left_translates hK hV with ⟨t, ht⟩, exact ⟨t.card, t, ht, rfl⟩ }
@[to_additive add_index_elim]
lemma index_elim {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) :
∃ (t : finset G), K ⊆ (⋃ g ∈ t, (λ h, g * h) ⁻¹' V) ∧ finset.card t = index K V :=
by { have := nat.Inf_mem (index_defined hK hV), rwa [mem_image] at this }
@[to_additive le_add_index_mul]
lemma le_index_mul (K₀ : positive_compacts G) (K : compacts G) {V : set G}
(hV : (interior V).nonempty) :
index (K : set G) V ≤ index (K : set G) K₀ * index (K₀ : set G) V :=
begin
obtain ⟨s, h1s, h2s⟩ := index_elim K.is_compact K₀.interior_nonempty,
obtain ⟨t, h1t, h2t⟩ := index_elim K₀.is_compact hV,
rw [← h2s, ← h2t, mul_comm],
refine le_trans _ finset.card_mul_le,
apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], refine subset.trans h1s _,
apply Union₂_subset, intros g₁ hg₁, rw preimage_subset_iff, intros g₂ hg₂,
have := h1t hg₂,
rcases this with ⟨_, ⟨g₃, rfl⟩, A, ⟨hg₃, rfl⟩, h2V⟩, rw [mem_preimage, ← mul_assoc] at h2V,
exact mem_bUnion (finset.mul_mem_mul hg₃ hg₁) h2V
end
@[to_additive add_index_pos]
lemma index_pos (K : positive_compacts G) {V : set G} (hV : (interior V).nonempty) :
0 < index (K : set G) V :=
begin
unfold index, rw [nat.Inf_def, nat.find_pos, mem_image],
{ rintro ⟨t, h1t, h2t⟩, rw [finset.card_eq_zero] at h2t, subst h2t,
obtain ⟨g, hg⟩ := K.interior_nonempty,
show g ∈ (∅ : set G), convert h1t (interior_subset hg), symmetry, apply bUnion_empty },
{ exact index_defined K.is_compact hV }
end
@[to_additive add_index_mono]
lemma index_mono {K K' V : set G} (hK' : is_compact K') (h : K ⊆ K')
(hV : (interior V).nonempty) : index K V ≤ index K' V :=
begin
rcases index_elim hK' hV with ⟨s, h1s, h2s⟩,
apply nat.Inf_le, rw [mem_image], refine ⟨s, subset.trans h h1s, h2s⟩
end
@[to_additive add_index_union_le]
lemma index_union_le (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty) :
index (K₁.1 ∪ K₂.1) V ≤ index K₁.1 V + index K₂.1 V :=
begin
rcases index_elim K₁.2 hV with ⟨s, h1s, h2s⟩,
rcases index_elim K₂.2 hV with ⟨t, h1t, h2t⟩,
rw [← h2s, ← h2t],
refine le_trans _ (finset.card_union_le _ _),
apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq],
apply union_subset; refine subset.trans (by assumption) _; apply bUnion_subset_bUnion_left;
intros g hg; simp only [mem_def] at hg;
simp only [mem_def, multiset.mem_union, finset.union_val, hg, or_true, true_or]
end
@[to_additive add_index_union_eq]
lemma index_union_eq (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty)
(h : disjoint (K₁.1 * V⁻¹) (K₂.1 * V⁻¹)) :
index (K₁.1 ∪ K₂.1) V = index K₁.1 V + index K₂.1 V :=
begin
apply le_antisymm (index_union_le K₁ K₂ hV),
rcases index_elim (K₁.2.union K₂.2) hV with ⟨s, h1s, h2s⟩, rw [← h2s],
have : ∀ (K : set G) , K ⊆ (⋃ g ∈ s, (λ h, g * h) ⁻¹' V) →
index K V ≤ (s.filter (λ g, ((λ (h : G), g * h) ⁻¹' V ∩ K).nonempty)).card,
{ intros K hK, apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq],
intros g hg, rcases hK hg with ⟨_, ⟨g₀, rfl⟩, _, ⟨h1g₀, rfl⟩, h2g₀⟩,
simp only [mem_preimage] at h2g₀,
simp only [mem_Union], use g₀, split,
{ simp only [finset.mem_filter, h1g₀, true_and], use g,
simp only [hg, h2g₀, mem_inter_iff, mem_preimage, and_self] },
exact h2g₀ },
refine le_trans (add_le_add (this K₁.1 $ subset.trans (subset_union_left _ _) h1s)
(this K₂.1 $ subset.trans (subset_union_right _ _) h1s)) _,
rw [← finset.card_union_eq, finset.filter_union_right],
exact s.card_filter_le _,
apply finset.disjoint_filter.mpr,
rintro g₁ h1g₁ ⟨g₂, h1g₂, h2g₂⟩ ⟨g₃, h1g₃, h2g₃⟩,
simp only [mem_preimage] at h1g₃ h1g₂,
refine h.le_bot (_ : g₁⁻¹ ∈ _),
split; simp only [set.mem_inv, set.mem_mul, exists_exists_and_eq_and, exists_and_distrib_left],
{ refine ⟨_, h2g₂, (g₁ * g₂)⁻¹, _, _⟩, simp only [inv_inv, h1g₂],
simp only [mul_inv_rev, mul_inv_cancel_left] },
{ refine ⟨_, h2g₃, (g₁ * g₃)⁻¹, _, _⟩, simp only [inv_inv, h1g₃],
simp only [mul_inv_rev, mul_inv_cancel_left] }
end
@[to_additive add_left_add_index_le]
lemma mul_left_index_le {K : set G} (hK : is_compact K) {V : set G} (hV : (interior V).nonempty)
(g : G) : index ((λ h, g * h) '' K) V ≤ index K V :=
begin
rcases index_elim hK hV with ⟨s, h1s, h2s⟩, rw [← h2s],
apply nat.Inf_le, rw [mem_image],
refine ⟨s.map (equiv.mul_right g⁻¹).to_embedding, _, finset.card_map _⟩,
{ simp only [mem_set_of_eq], refine subset.trans (image_subset _ h1s) _,
rintro _ ⟨g₁, ⟨_, ⟨g₂, rfl⟩, ⟨_, ⟨hg₂, rfl⟩, hg₁⟩⟩, rfl⟩,
simp only [mem_preimage] at hg₁, simp only [exists_prop, mem_Union, finset.mem_map,
equiv.coe_mul_right, exists_exists_and_eq_and, mem_preimage, equiv.to_embedding_apply],
refine ⟨_, hg₂, _⟩, simp only [mul_assoc, hg₁, inv_mul_cancel_left] }
end
@[to_additive is_left_invariant_add_index]
lemma is_left_invariant_index {K : set G} (hK : is_compact K) (g : G) {V : set G}
(hV : (interior V).nonempty) : index ((λ h, g * h) '' K) V = index K V :=
begin
refine le_antisymm (mul_left_index_le hK hV g) _,
convert mul_left_index_le (hK.image $ continuous_mul_left g) hV g⁻¹,
rw [image_image], symmetry, convert image_id' _, ext h, apply inv_mul_cancel_left
end
/-!
### Lemmas about `prehaar`
-/
@[to_additive add_prehaar_le_add_index]
lemma prehaar_le_index (K₀ : positive_compacts G) {U : set G} (K : compacts G)
(hU : (interior U).nonempty) : prehaar (K₀ : set G) U K ≤ index (K : set G) K₀ :=
begin
unfold prehaar, rw [div_le_iff]; norm_cast,
{ apply le_index_mul K₀ K hU },
{ exact index_pos K₀ hU }
end
@[to_additive]
lemma prehaar_pos (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty)
{K : set G} (h1K : is_compact K) (h2K : (interior K).nonempty) :
0 < prehaar (K₀ : set G) U ⟨K, h1K⟩ :=
by { apply div_pos; norm_cast, apply index_pos ⟨⟨K, h1K⟩, h2K⟩ hU, exact index_pos K₀ hU }
@[to_additive]
lemma prehaar_mono {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty)
{K₁ K₂ : compacts G} (h : (K₁ : set G) ⊆ K₂.1) :
prehaar (K₀ : set G) U K₁ ≤ prehaar (K₀ : set G) U K₂ :=
begin
simp only [prehaar], rw [div_le_div_right], exact_mod_cast index_mono K₂.2 h hU,
exact_mod_cast index_pos K₀ hU
end
@[to_additive]
lemma prehaar_self {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) :
prehaar (K₀ : set G) U K₀.to_compacts = 1 :=
div_self $ ne_of_gt $ by exact_mod_cast index_pos K₀ hU
@[to_additive]
lemma prehaar_sup_le {K₀ : positive_compacts G} {U : set G} (K₁ K₂ : compacts G)
(hU : (interior U).nonempty) :
prehaar (K₀ : set G) U (K₁ ⊔ K₂) ≤ prehaar (K₀ : set G) U K₁ + prehaar (K₀ : set G) U K₂ :=
begin
simp only [prehaar], rw [div_add_div_same, div_le_div_right],
exact_mod_cast index_union_le K₁ K₂ hU, exact_mod_cast index_pos K₀ hU
end
@[to_additive]
lemma prehaar_sup_eq {K₀ : positive_compacts G} {U : set G} {K₁ K₂ : compacts G}
(hU : (interior U).nonempty) (h : disjoint (K₁.1 * U⁻¹) (K₂.1 * U⁻¹)) :
prehaar (K₀ : set G) U (K₁ ⊔ K₂) = prehaar (K₀ : set G) U K₁ + prehaar (K₀ : set G) U K₂ :=
by { simp only [prehaar], rw [div_add_div_same], congr', exact_mod_cast index_union_eq K₁ K₂ hU h }
@[to_additive]
lemma is_left_invariant_prehaar {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty)
(g : G) (K : compacts G) :
prehaar (K₀ : set G) U (K.map _ $ continuous_mul_left g) = prehaar (K₀ : set G) U K :=
by simp only [prehaar, compacts.coe_map, is_left_invariant_index K.is_compact _ hU]
/-!
### Lemmas about `haar_product`
-/
@[to_additive]
lemma prehaar_mem_haar_product (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty) :
prehaar (K₀ : set G) U ∈ haar_product (K₀ : set G) :=
by { rintro ⟨K, hK⟩ h2K, rw [mem_Icc], exact ⟨prehaar_nonneg K₀ _, prehaar_le_index K₀ _ hU⟩ }
@[to_additive]
lemma nonempty_Inter_cl_prehaar (K₀ : positive_compacts G) :
(haar_product (K₀ : set G) ∩ ⋂ (V : open_nhds_of (1 : G)), cl_prehaar K₀ V).nonempty :=
begin
have : is_compact (haar_product (K₀ : set G)),
{ apply is_compact_univ_pi, intro K, apply is_compact_Icc },
refine this.inter_Inter_nonempty (cl_prehaar K₀) (λ s, is_closed_closure) (λ t, _),
let V₀ := ⋂ (V ∈ t), (V : open_nhds_of 1).carrier,
have h1V₀ : is_open V₀,
{ apply is_open_bInter, apply finset.finite_to_set, rintro ⟨⟨V, hV₁⟩, hV₂⟩ h2V, exact hV₁ },
have h2V₀ : (1 : G) ∈ V₀, { simp only [mem_Inter], rintro ⟨⟨V, hV₁⟩, hV₂⟩ h2V, exact hV₂ },
refine ⟨prehaar K₀ V₀, _⟩,
split,
{ apply prehaar_mem_haar_product K₀, use 1, rwa h1V₀.interior_eq },
{ simp only [mem_Inter], rintro ⟨V, hV⟩ h2V, apply subset_closure,
apply mem_image_of_mem, rw [mem_set_of_eq],
exact ⟨subset.trans (Inter_subset _ ⟨V, hV⟩) (Inter_subset _ h2V), h1V₀, h2V₀⟩ },
end
/-!
### Lemmas about `chaar`
-/
/-- This is the "limit" of `prehaar K₀ U K` as `U` becomes a smaller and smaller open
neighborhood of `(1 : G)`. More precisely, it is defined to be an arbitrary element
in the intersection of all the sets `cl_prehaar K₀ V` in `haar_product K₀`.
This is roughly equal to the Haar measure on compact sets,
but it can differ slightly. We do know that
`haar_measure K₀ (interior K) ≤ chaar K₀ K ≤ haar_measure K₀ K`. -/
@[to_additive add_chaar "additive version of `measure_theory.measure.haar.chaar`"]
def chaar (K₀ : positive_compacts G) (K : compacts G) : ℝ :=
classical.some (nonempty_Inter_cl_prehaar K₀) K
@[to_additive add_chaar_mem_add_haar_product]
lemma chaar_mem_haar_product (K₀ : positive_compacts G) : chaar K₀ ∈ haar_product (K₀ : set G) :=
(classical.some_spec (nonempty_Inter_cl_prehaar K₀)).1
@[to_additive add_chaar_mem_cl_add_prehaar]
lemma chaar_mem_cl_prehaar (K₀ : positive_compacts G) (V : open_nhds_of (1 : G)) :
chaar K₀ ∈ cl_prehaar (K₀ : set G) V :=
by { have := (classical.some_spec (nonempty_Inter_cl_prehaar K₀)).2, rw [mem_Inter] at this,
exact this V }
@[to_additive add_chaar_nonneg]
lemma chaar_nonneg (K₀ : positive_compacts G) (K : compacts G) : 0 ≤ chaar K₀ K :=
by { have := chaar_mem_haar_product K₀ K (mem_univ _), rw mem_Icc at this, exact this.1 }
@[to_additive add_chaar_empty]
lemma chaar_empty (K₀ : positive_compacts G) : chaar K₀ ⊥ = 0 :=
begin
let eval : (compacts G → ℝ) → ℝ := λ f, f ⊥,
have : continuous eval := continuous_apply ⊥,
show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)},
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⊤),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_empty },
{ apply continuous_iff_is_closed.mp this, exact is_closed_singleton },
end
@[to_additive add_chaar_self]
lemma chaar_self (K₀ : positive_compacts G) : chaar K₀ K₀.to_compacts = 1 :=
begin
let eval : (compacts G → ℝ) → ℝ := λ f, f K₀.to_compacts,
have : continuous eval := continuous_apply _,
show chaar K₀ ∈ eval ⁻¹' {(1 : ℝ)},
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⊤),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_self,
rw h2U.interior_eq, exact ⟨1, h3U⟩ },
{ apply continuous_iff_is_closed.mp this, exact is_closed_singleton }
end
@[to_additive add_chaar_mono]
lemma chaar_mono {K₀ : positive_compacts G} {K₁ K₂ : compacts G} (h : (K₁ : set G) ⊆ K₂) :
chaar K₀ K₁ ≤ chaar K₀ K₂ :=
begin
let eval : (compacts G → ℝ) → ℝ := λ f, f K₂ - f K₁,
have : continuous eval := (continuous_apply K₂).sub (continuous_apply K₁),
rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)),
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⊤),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg],
apply prehaar_mono _ h, rw h2U.interior_eq, exact ⟨1, h3U⟩ },
{ apply continuous_iff_is_closed.mp this, exact is_closed_Ici },
end
@[to_additive add_chaar_sup_le]
lemma chaar_sup_le {K₀ : positive_compacts G} (K₁ K₂ : compacts G) :
chaar K₀ (K₁ ⊔ K₂) ≤ chaar K₀ K₁ + chaar K₀ K₂ :=
begin
let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂),
have : continuous eval :=
((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub
(continuous_apply (K₁ ⊔ K₂)),
rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)),
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⊤),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg],
apply prehaar_sup_le, rw h2U.interior_eq, exact ⟨1, h3U⟩ },
{ apply continuous_iff_is_closed.mp this, exact is_closed_Ici },
end
@[to_additive add_chaar_sup_eq]
lemma chaar_sup_eq [t2_space G] {K₀ : positive_compacts G} {K₁ K₂ : compacts G}
(h : disjoint K₁.1 K₂.1) : chaar K₀ (K₁ ⊔ K₂) = chaar K₀ K₁ + chaar K₀ K₂ :=
begin
rcases is_compact_is_compact_separated K₁.2 K₂.2 h with ⟨U₁, U₂, h1U₁, h1U₂, h2U₁, h2U₂, hU⟩,
rcases compact_open_separated_mul_right K₁.2 h1U₁ h2U₁ with ⟨L₁, h1L₁, h2L₁⟩,
rcases mem_nhds_iff.mp h1L₁ with ⟨V₁, h1V₁, h2V₁, h3V₁⟩,
replace h2L₁ := subset.trans (mul_subset_mul_left h1V₁) h2L₁,
rcases compact_open_separated_mul_right K₂.2 h1U₂ h2U₂ with ⟨L₂, h1L₂, h2L₂⟩,
rcases mem_nhds_iff.mp h1L₂ with ⟨V₂, h1V₂, h2V₂, h3V₂⟩,
replace h2L₂ := subset.trans (mul_subset_mul_left h1V₂) h2L₂,
let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂),
have : continuous eval :=
((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub
(continuous_apply (K₁ ⊔ K₂)),
rw [eq_comm, ← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)},
let V := V₁ ∩ V₂,
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀
⟨⟨V⁻¹, (h2V₁.inter h2V₂).preimage continuous_inv⟩,
by simp only [mem_inv, inv_one, h3V₁, h3V₂, V, mem_inter_iff, true_and]⟩),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩,
simp only [mem_preimage, eval, sub_eq_zero, mem_singleton_iff], rw [eq_comm],
apply prehaar_sup_eq,
{ rw h2U.interior_eq, exact ⟨1, h3U⟩ },
{ refine disjoint_of_subset _ _ hU,
{ refine subset.trans (mul_subset_mul subset.rfl _) h2L₁,
exact subset.trans (inv_subset.mpr h1U) (inter_subset_left _ _) },
{ refine subset.trans (mul_subset_mul subset.rfl _) h2L₂,
exact subset.trans (inv_subset.mpr h1U) (inter_subset_right _ _) }}},
{ apply continuous_iff_is_closed.mp this, exact is_closed_singleton }
end
@[to_additive is_left_invariant_add_chaar]
lemma is_left_invariant_chaar {K₀ : positive_compacts G} (g : G) (K : compacts G) :
chaar K₀ (K.map _ $ continuous_mul_left g) = chaar K₀ K :=
begin
let eval : (compacts G → ℝ) → ℝ := λ f, f (K.map _ $ continuous_mul_left g) - f K,
have : continuous eval := (continuous_apply (K.map _ _)).sub (continuous_apply K),
rw [← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)},
apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⊤),
unfold cl_prehaar, rw is_closed.closure_subset_iff,
{ rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩,
simp only [mem_singleton_iff, mem_preimage, eval, sub_eq_zero],
apply is_left_invariant_prehaar, rw h2U.interior_eq, exact ⟨1, h3U⟩ },
{ apply continuous_iff_is_closed.mp this, exact is_closed_singleton },
end
variable [t2_space G]
/-- The function `chaar` interpreted in `ℝ≥0`, as a content -/
@[to_additive "additive version of `measure_theory.measure.haar.haar_content`"]
def haar_content (K₀ : positive_compacts G) : content G :=
{ to_fun := λ K, ⟨chaar K₀ K, chaar_nonneg _ _⟩,
mono' := λ K₁ K₂ h, by simp only [←nnreal.coe_le_coe, subtype.coe_mk, chaar_mono, h],
sup_disjoint' := λ K₁ K₂ h, by { simp only [chaar_sup_eq h], refl },
sup_le' := λ K₁ K₂,
by simp only [←nnreal.coe_le_coe, nnreal.coe_add, subtype.coe_mk, chaar_sup_le] }
/-! We only prove the properties for `haar_content` that we use at least twice below. -/
@[to_additive]
lemma haar_content_apply (K₀ : positive_compacts G) (K : compacts G) :
haar_content K₀ K = show nnreal, from ⟨chaar K₀ K, chaar_nonneg _ _⟩ := rfl
/-- The variant of `chaar_self` for `haar_content` -/
@[to_additive "The variant of `add_chaar_self` for `add_haar_content`."]
lemma haar_content_self {K₀ : positive_compacts G} : haar_content K₀ K₀.to_compacts = 1 :=
by { simp_rw [← ennreal.coe_one, haar_content_apply, ennreal.coe_eq_coe, chaar_self], refl }
/-- The variant of `is_left_invariant_chaar` for `haar_content` -/
@[to_additive "The variant of `is_left_invariant_add_chaar` for `add_haar_content`"]
lemma is_left_invariant_haar_content {K₀ : positive_compacts G} (g : G) (K : compacts G) :
haar_content K₀ (K.map _ $ continuous_mul_left g) = haar_content K₀ K :=
by simpa only [ennreal.coe_eq_coe, ←nnreal.coe_eq, haar_content_apply]
using is_left_invariant_chaar g K
@[to_additive]
lemma haar_content_outer_measure_self_pos {K₀ : positive_compacts G} :
0 < (haar_content K₀).outer_measure K₀ :=
begin
refine zero_lt_one.trans_le _,
rw [content.outer_measure_eq_infi],
refine le_infi₂ (λ U hU, le_infi $ λ hK₀, le_trans _ $ le_supr₂ K₀.to_compacts hK₀),
exact haar_content_self.ge,
end
end haar
open haar
/-!
### The Haar measure
-/
variables [topological_space G] [t2_space G] [topological_group G] [measurable_space G]
[borel_space G]
/-- The Haar measure on the locally compact group `G`, scaled so that `haar_measure K₀ K₀ = 1`. -/
@[to_additive "The Haar measure on the locally compact additive group `G`,
scaled so that `add_haar_measure K₀ K₀ = 1`."]
def haar_measure (K₀ : positive_compacts G) : measure G :=
((haar_content K₀).outer_measure K₀)⁻¹ • (haar_content K₀).measure
@[to_additive]
lemma haar_measure_apply {K₀ : positive_compacts G} {s : set G} (hs : measurable_set s) :
haar_measure K₀ s = (haar_content K₀).outer_measure s / (haar_content K₀).outer_measure K₀ :=
begin
change (((haar_content K₀).outer_measure) K₀)⁻¹ * (haar_content K₀).measure s = _,
simp only [hs, div_eq_mul_inv, mul_comm, content.measure_apply],
end
@[to_additive]
instance is_mul_left_invariant_haar_measure (K₀ : positive_compacts G) :
is_mul_left_invariant (haar_measure K₀) :=
begin
rw [← forall_measure_preimage_mul_iff],
intros g A hA,
rw [haar_measure_apply hA, haar_measure_apply (measurable_const_mul g hA)],
congr' 1,
apply content.is_mul_left_invariant_outer_measure,
apply is_left_invariant_haar_content,
end
@[to_additive]
lemma haar_measure_self {K₀ : positive_compacts G} : haar_measure K₀ K₀ = 1 :=
begin
haveI : locally_compact_space G := K₀.locally_compact_space_of_group,
rw [haar_measure_apply K₀.is_compact.measurable_set, ennreal.div_self],
{ rw [← pos_iff_ne_zero], exact haar_content_outer_measure_self_pos },
{ exact (content.outer_measure_lt_top_of_is_compact _ K₀.is_compact).ne }
end
/-- The Haar measure is regular. -/
@[to_additive "The additive Haar measure is regular."]
instance regular_haar_measure {K₀ : positive_compacts G} :
(haar_measure K₀).regular :=
begin
haveI : locally_compact_space G := K₀.locally_compact_space_of_group,
apply regular.smul,
rw ennreal.inv_ne_top,
exact haar_content_outer_measure_self_pos.ne',
end
/-- The Haar measure is sigma-finite in a second countable group. -/
@[to_additive "The additive Haar measure is sigma-finite in a second countable group."]
instance sigma_finite_haar_measure [second_countable_topology G] {K₀ : positive_compacts G} :
sigma_finite (haar_measure K₀) :=
by { haveI : locally_compact_space G := K₀.locally_compact_space_of_group, apply_instance, }
/-- The Haar measure is a Haar measure, i.e., it is invariant and gives finite mass to compact
sets and positive mass to nonempty open sets. -/
@[to_additive "The additive Haar measure is an additive Haar measure, i.e., it is invariant and
gives finite mass to compact sets and positive mass to nonempty open sets."]
instance is_haar_measure_haar_measure (K₀ : positive_compacts G) :
is_haar_measure (haar_measure K₀) :=
begin
apply is_haar_measure_of_is_compact_nonempty_interior (haar_measure K₀) K₀ K₀.is_compact
K₀.interior_nonempty,
{ simp only [haar_measure_self], exact one_ne_zero },
{ simp only [haar_measure_self], exact ennreal.coe_ne_top },
end
/-- `haar` is some choice of a Haar measure, on a locally compact group. -/
@[reducible, to_additive "`add_haar` is some choice of a Haar measure, on a locally compact
additive group."]
def haar [locally_compact_space G] : measure G := haar_measure $ classical.arbitrary _
section second_countable
variables [second_countable_topology G]
/-- The Haar measure is unique up to scaling. More precisely: every σ-finite left invariant measure
is a scalar multiple of the Haar measure.
This is slightly weaker than assuming that `μ` is a Haar measure (in particular we don't require
`μ ≠ 0`). -/
@[to_additive "The additive Haar measure is unique up to scaling. More precisely: every σ-finite
left invariant measure is a scalar multiple of the additive Haar measure. This is slightly weaker
than assuming that `μ` is an additive Haar measure (in particular we don't require `μ ≠ 0`)."]
theorem haar_measure_unique (μ : measure G) [sigma_finite μ] [is_mul_left_invariant μ]
(K₀ : positive_compacts G) : μ = μ K₀ • haar_measure K₀ :=
(measure_eq_div_smul μ (haar_measure K₀) K₀.is_compact.measurable_set
(measure_pos_of_nonempty_interior _ K₀.interior_nonempty).ne'
K₀.is_compact.measure_lt_top.ne).trans (by rw [haar_measure_self, div_one])
example [locally_compact_space G] (μ : measure G) [is_haar_measure μ] (K₀ : positive_compacts G) :
μ = μ K₀.1 • haar_measure K₀ :=
haar_measure_unique μ K₀
/-- To show that an invariant σ-finite measure is regular it is sufficient to show that it is finite
on some compact set with non-empty interior. -/
@[to_additive "To show that an invariant σ-finite measure is regular it is sufficient to show that
it is finite on some compact set with non-empty interior."]
theorem regular_of_is_mul_left_invariant {μ : measure G} [sigma_finite μ] [is_mul_left_invariant μ]
{K : set G} (hK : is_compact K) (h2K : (interior K).nonempty) (hμK : μ K ≠ ∞) :
regular μ :=
by { rw [haar_measure_unique μ ⟨⟨K, hK⟩, h2K⟩], exact regular.smul hμK }
@[to_additive is_add_haar_measure_eq_smul_is_add_haar_measure]
theorem is_haar_measure_eq_smul_is_haar_measure
[locally_compact_space G] (μ ν : measure G) [is_haar_measure μ] [is_haar_measure ν] :
∃ (c : ℝ≥0∞), c ≠ 0 ∧ c ≠ ∞ ∧ μ = c • ν :=
begin
have K : positive_compacts G := classical.arbitrary _,
have νpos : 0 < ν K := measure_pos_of_nonempty_interior _ K.interior_nonempty,
have νne : ν K ≠ ∞ := K.is_compact.measure_lt_top.ne,
refine ⟨μ K / ν K, _, _, _⟩,
{ simp only [νne, (μ.measure_pos_of_nonempty_interior K.interior_nonempty).ne', ne.def,
ennreal.div_zero_iff, not_false_iff, or_self] },
{ simp only [div_eq_mul_inv, νpos.ne', (K.is_compact.measure_lt_top).ne, or_self,
ennreal.inv_eq_top, with_top.mul_eq_top_iff, ne.def, not_false_iff, and_false, false_and] },
{ calc
μ = μ K • haar_measure K : haar_measure_unique μ K
... = (μ K / ν K) • (ν K • haar_measure K) :
by rw [smul_smul, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel νpos.ne' νne, mul_one]
... = (μ K / ν K) • ν : by rw ← haar_measure_unique ν K }
end
@[priority 90, to_additive] -- see Note [lower instance priority]
instance regular_of_is_haar_measure
[locally_compact_space G] (μ : measure G) [is_haar_measure μ] :
regular μ :=
begin
have K : positive_compacts G := classical.arbitrary _,
obtain ⟨c, c0, ctop, hμ⟩ : ∃ (c : ℝ≥0∞), (c ≠ 0) ∧ (c ≠ ∞) ∧ (μ = c • haar_measure K) :=
is_haar_measure_eq_smul_is_haar_measure μ _,
rw hμ,
exact regular.smul ctop,
end
/-- **Steinhaus Theorem** In any locally compact group `G` with a haar measure `μ`, for any
measurable set `E` of positive measure, the set `E / E` is a neighbourhood of `1`. -/
@[to_additive "**Steinhaus Theorem** In any locally compact group `G` with a haar measure `μ`,
for any measurable set `E` of positive measure, the set `E - E` is a neighbourhood of `0`."]
theorem div_mem_nhds_one_of_haar_pos (μ : measure G) [is_haar_measure μ] [locally_compact_space G]
(E : set G) (hE : measurable_set E) (hEpos : 0 < μ E) :
E / E ∈ 𝓝 (1 : G) :=
begin
/- For any regular measure `μ` and set `E` of positive measure, we can find a compact set `K` of
positive measure inside `E`. Further, for any outer regular measure `μ` there exists an open
set `U` containing `K` with measure arbitrarily close to `K` (here `μ U < 2 * μ K` suffices).
Then, we can pick an open neighborhood of `1`, say `V` such that such that `V * K` is contained
in `U`. Now note that for any `v` in `V`, the sets `K` and `{v} * K` can not be disjoint
because they are both of measure `μ K` (since `μ` is left regular) and also contained in `U`,
yet we have that `μ U < 2 * μ K`. This show that `K / K` contains the neighborhood `V` of `1`,
and therefore that it is itself such a neighborhood. -/
obtain ⟨L, hL, hLE, hLpos, hLtop⟩ : ∃ (L : set G), measurable_set L ∧ L ⊆ E ∧ 0 < μ L ∧ μ L < ∞,
from exists_subset_measure_lt_top hE hEpos,
obtain ⟨K, hKL, hK, hKpos⟩ : ∃ (K : set G) (H : K ⊆ L), is_compact K ∧ 0 < μ K,
from measurable_set.exists_lt_is_compact_of_ne_top hL (ne_of_lt hLtop) hLpos,
have hKtop : μ K ≠ ∞,
{ apply ne_top_of_le_ne_top (ne_of_lt hLtop),
apply measure_mono hKL },
obtain ⟨U, hUK, hU, hμUK⟩ : ∃ (U : set G) (H : U ⊇ K), is_open U ∧ μ U < μ K + μ K,
from set.exists_is_open_lt_add K hKtop hKpos.ne',
obtain ⟨V, hV1, hVKU⟩ : ∃ (V ∈ 𝓝 (1 : G)), V * K ⊆ U,
from compact_open_separated_mul_left hK hU hUK,
have hv : ∀ (v : G), v ∈ V → ¬ disjoint ({v}* K) K,
{ intros v hv hKv,
have hKvsub : {v} * K ∪ K ⊆ U,
{ apply set.union_subset _ hUK,
apply subset_trans _ hVKU,
apply set.mul_subset_mul _ (set.subset.refl K),
simp only [set.singleton_subset_iff, hv] },
replace hKvsub := @measure_mono _ _ μ _ _ hKvsub,
have hcontr := lt_of_le_of_lt hKvsub hμUK,
rw measure_union hKv (is_compact.measurable_set hK) at hcontr,
have hKtranslate : μ ({v} * K) = μ K,
by simp only [singleton_mul, image_mul_left, measure_preimage_mul],
rw [hKtranslate, lt_self_iff_false] at hcontr,
assumption },
suffices : V ⊆ E / E, from filter.mem_of_superset hV1 this,
assume v hvV,
obtain ⟨x, hxK, hxvK⟩ : ∃ (x : G), x ∈ {v} * K ∧ x ∈ K, from set.not_disjoint_iff.1 (hv v hvV),
refine ⟨x, v⁻¹ * x, hLE (hKL hxvK), _, _⟩,
{ apply hKL.trans hLE,
simpa only [singleton_mul, image_mul_left, mem_preimage] using hxK },
{ simp only [div_eq_iff_eq_mul, ← mul_assoc, mul_right_inv, one_mul] },
end
end second_countable
end group
section comm_group
variables {G : Type*} [comm_group G] [topological_space G] [topological_group G] [t2_space G]
[measurable_space G] [borel_space G] [second_countable_topology G]
(μ : measure G) [is_haar_measure μ]
/-- Any Haar measure is invariant under inversion in an abelian group. -/
@[priority 100, to_additive
"Any additive Haar measure is invariant under negation in an abelian group."]
instance is_haar_measure.is_inv_invariant [locally_compact_space G] :
is_inv_invariant μ :=
begin
-- the image measure is a Haar measure. By uniqueness up to multiplication, it is of the form
-- `c μ`. Applying again inversion, one gets the measure `c^2 μ`. But since inversion is an
-- involution, this is also `μ`. Hence, `c^2 = 1`, which implies `c = 1`.
constructor,
haveI : is_haar_measure (measure.map has_inv.inv μ) :=
(mul_equiv.inv G).is_haar_measure_map μ continuous_inv continuous_inv,
obtain ⟨c, cpos, clt, hc⟩ : ∃ (c : ℝ≥0∞), (c ≠ 0) ∧ (c ≠ ∞) ∧ (measure.map has_inv.inv μ = c • μ)
:= is_haar_measure_eq_smul_is_haar_measure _ _,
have : map has_inv.inv (map has_inv.inv μ) = c^2 • μ,
by simp only [hc, smul_smul, pow_two, measure.map_smul],
have μeq : μ = c^2 • μ,
{ rw [map_map continuous_inv.measurable continuous_inv.measurable] at this,
{ simpa only [inv_involutive, involutive.comp_self, map_id] },
all_goals { apply_instance } },
have K : positive_compacts G := classical.arbitrary _,
have : c^2 * μ K = 1^2 * μ K,
by { conv_rhs { rw μeq },
simp, },
have : c^2 = 1^2 :=
(ennreal.mul_eq_mul_right (measure_pos_of_nonempty_interior _ K.interior_nonempty).ne'
K.is_compact.measure_lt_top.ne).1 this,
have : c = 1 := (ennreal.pow_strict_mono two_ne_zero).injective this,
rw [measure.inv, hc, this, one_smul]
end
@[to_additive]
lemma measure_preserving_zpow [compact_space G] [rootable_by G ℤ] {n : ℤ} (hn : n ≠ 0) :
measure_preserving (λ (g : G), g^n) μ μ :=
{ measurable := (continuous_zpow n).measurable,
map_eq :=
begin
let f := @zpow_group_hom G _ n,
have hf : continuous f := continuous_zpow n,
haveI : (μ.map f).is_haar_measure :=
is_haar_measure_map μ f hf (rootable_by.surjective_pow G ℤ hn) (by simp),
obtain ⟨C, -, -, hC⟩ := is_haar_measure_eq_smul_is_haar_measure (μ.map f) μ,
suffices : C = 1, { rwa [this, one_smul] at hC, },
have h_univ : (μ.map f) univ = μ univ,
{ rw [map_apply_of_ae_measurable hf.measurable.ae_measurable measurable_set.univ,
preimage_univ], },
have hμ₀ : μ univ ≠ 0 := is_open_pos_measure.open_pos univ is_open_univ univ_nonempty,
have hμ₁ : μ univ ≠ ∞ := compact_space.is_finite_measure.measure_univ_lt_top.ne,
rwa [hC, smul_apply, algebra.id.smul_eq_mul, mul_comm, ← ennreal.eq_div_iff hμ₀ hμ₁,
ennreal.div_self hμ₀ hμ₁] at h_univ,
end, }
@[to_additive]
lemma measure_preserving.zpow [compact_space G] [rootable_by G ℤ] {n : ℤ} (hn : n ≠ 0)
{X : Type*} [measurable_space X] {μ' : measure X} {f : X → G} (hf : measure_preserving f μ' μ) :
measure_preserving (λ x, (f x)^n) μ' μ :=
(measure_preserving_zpow μ hn).comp hf
end comm_group
end measure
end measure_theory
|
aaa0c3cdefd6707652771cc58806f837ccd9871b | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/finset/card.lean | 13778c0ec9a3cd25d1f2dac87e1e1a324ef74e1a | [
"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 | 24,064 | 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, Jeremy Avigad
-/
import data.finset.basic
import tactic.by_contra
/-!
# Cardinality of a finite set
This defines the cardinality of a `finset` and provides induction principles for finsets.
## Main declarations
* `finset.card`: `s.card : ℕ` returns the cardinality of `s : finset α`.
### Induction principles
* `finset.strong_induction`: Strong induction
* `finset.strong_induction_on`
* `finset.strong_downward_induction`
* `finset.strong_downward_induction_on`
* `finset.case_strong_induction_on`
## TODO
Should we add a noncomputable version?
-/
open function multiset nat
variables {α β : Type*}
namespace finset
variables {s t : finset α} {a b : α}
/-- `s.card` is the number of elements of `s`, aka its cardinality. -/
def card (s : finset α) : ℕ := s.1.card
lemma card_def (s : finset α) : s.card = s.1.card := rfl
@[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.card := rfl
@[simp] lemma card_empty : card (∅ : finset α) = 0 := rfl
lemma card_le_of_subset : s ⊆ t → s.card ≤ t.card := multiset.card_le_of_le ∘ val_le_iff.mpr
@[mono] lemma card_mono : monotone (@card α) := by apply card_le_of_subset
@[simp] lemma card_eq_zero : s.card = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero
lemma card_pos : 0 < s.card ↔ s.nonempty :=
pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm
alias finset.card_pos ↔ _ finset.nonempty.card_pos
lemma card_ne_zero_of_mem (h : a ∈ s) : s.card ≠ 0 := (not_congr card_eq_zero).2 $ ne_empty_of_mem h
@[simp] lemma card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _
lemma card_singleton_inter [decidable_eq α] : ({a} ∩ s).card ≤ 1 :=
begin
cases (finset.decidable_mem a s),
{ simp [finset.singleton_inter_of_not_mem h] },
{ simp [finset.singleton_inter_of_mem h] }
end
@[simp] lemma card_cons (h : a ∉ s) : (s.cons a h).card = s.card + 1 := card_cons _ _
section insert_erase
variables [decidable_eq α]
@[simp] lemma card_insert_of_not_mem (h : a ∉ s) : (insert a s).card = s.card + 1 :=
by rw [←cons_eq_insert _ _ h, card_cons]
lemma card_insert_of_mem (h : a ∈ s) : card (insert a s) = s.card := by rw insert_eq_of_mem h
lemma card_insert_le (a : α) (s : finset α) : card (insert a s) ≤ s.card + 1 :=
by by_cases a ∈ s; [{rw insert_eq_of_mem h, exact nat.le_succ _ }, rw card_insert_of_not_mem h]
/-- If `a ∈ s` is known, see also `finset.card_insert_of_mem` and `finset.card_insert_of_not_mem`.
-/
lemma card_insert_eq_ite : card (insert a s) = if a ∈ s then s.card else s.card + 1 :=
begin
by_cases h : a ∈ s,
{ rw [card_insert_of_mem h, if_pos h] },
{ rw [card_insert_of_not_mem h, if_neg h] }
end
@[simp] lemma card_doubleton (h : a ≠ b) : ({a, b} : finset α).card = 2 :=
by rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton]
@[simp] lemma card_erase_of_mem : a ∈ s → (s.erase a).card = s.card - 1 := card_erase_of_mem
@[simp] lemma card_erase_add_one : a ∈ s → (s.erase a).card + 1 = s.card := card_erase_add_one
lemma card_erase_lt_of_mem : a ∈ s → (s.erase a).card < s.card := card_erase_lt_of_mem
lemma card_erase_le : (s.erase a).card ≤ s.card := card_erase_le
lemma pred_card_le_card_erase : s.card - 1 ≤ (s.erase a).card :=
begin
by_cases h : a ∈ s,
{ exact (card_erase_of_mem h).ge },
{ rw erase_eq_of_not_mem h,
exact nat.sub_le _ _ }
end
/-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/
lemma card_erase_eq_ite : (s.erase a).card = if a ∈ s then s.card - 1 else s.card :=
card_erase_eq_ite
end insert_erase
@[simp] lemma card_range (n : ℕ) : (range n).card = n := card_range n
@[simp] lemma card_attach : s.attach.card = s.card := multiset.card_attach
end finset
section to_list_multiset
variables [decidable_eq α] (m : multiset α) (l : list α)
lemma multiset.card_to_finset : m.to_finset.card = m.dedup.card := rfl
lemma multiset.to_finset_card_le : m.to_finset.card ≤ m.card := card_le_of_le $ dedup_le _
lemma multiset.to_finset_card_of_nodup {m : multiset α} (h : m.nodup) : m.to_finset.card = m.card :=
congr_arg card $ multiset.dedup_eq_self.mpr h
lemma list.card_to_finset : l.to_finset.card = l.dedup.length := rfl
lemma list.to_finset_card_le : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧
lemma list.to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length :=
multiset.to_finset_card_of_nodup h
end to_list_multiset
namespace finset
variables {s t : finset α} {f : α → β} {n : ℕ}
@[simp] lemma length_to_list (s : finset α) : s.to_list.length = s.card :=
by { rw [to_list, ←multiset.coe_card, multiset.coe_to_list], refl }
lemma card_image_le [decidable_eq β] : (s.image f).card ≤ s.card :=
by simpa only [card_map] using (s.1.map f).to_finset_card_le
lemma card_image_of_inj_on [decidable_eq β] (H : set.inj_on f s) : (s.image f).card = s.card :=
by simp only [card, image_val_of_inj_on H, card_map]
lemma inj_on_of_card_image_eq [decidable_eq β] (H : (s.image f).card = s.card) : set.inj_on f s :=
begin
change (s.1.map f).dedup.card = s.1.card at H,
have : (s.1.map f).dedup = s.1.map f,
{ refine multiset.eq_of_le_of_card_le (multiset.dedup_le _) _,
rw H,
simp only [multiset.card_map] },
rw multiset.dedup_eq_self at this,
exact inj_on_of_nodup_map this,
end
lemma card_image_eq_iff_inj_on [decidable_eq β] : (s.image f).card = s.card ↔ set.inj_on f s :=
⟨inj_on_of_card_image_eq, card_image_of_inj_on⟩
lemma card_image_of_injective [decidable_eq β] (s : finset α) (H : injective f) :
(s.image f).card = s.card :=
card_image_of_inj_on $ λ x _ y _ h, H h
lemma fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) :
(s.filter (λ x, f x = y)).card ≠ 0 ↔ y ∈ s.image f :=
by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] }
@[simp] lemma card_map (f : α ↪ β) : (s.map f).card = s.card := multiset.card_map _ _
@[simp] lemma card_subtype (p : α → Prop) [decidable_pred p] (s : finset α) :
(s.subtype p).card = (s.filter p).card :=
by simp [finset.subtype]
lemma card_filter_le (s : finset α) (p : α → Prop) [decidable_pred p] :
(s.filter p).card ≤ s.card :=
card_le_of_subset $ filter_subset _ _
lemma eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : t.card ≤ s.card) : s = t :=
eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂
lemma map_eq_of_subset {f : α ↪ α} (hs : s.map f ⊆ s) : s.map f = s :=
eq_of_subset_of_card_le hs (card_map _).ge
lemma filter_card_eq {p : α → Prop} [decidable_pred p] (h : (s.filter p).card = s.card) (x : α)
(hx : x ∈ s) :
p x :=
begin
rw [←eq_of_subset_of_card_le (s.filter_subset p) h.ge, mem_filter] at hx,
exact hx.2,
end
lemma card_lt_card (h : s ⊂ t) : s.card < t.card := card_lt_of_lt $ val_lt_iff.2 h
lemma card_eq_of_bijective (f : ∀ i, i < n → α) (hf : ∀ a ∈ s, ∃ i, ∃ h : i < n, f i h = a)
(hf' : ∀ i (h : i < n), f i h ∈ s) (f_inj : ∀ i j (hi : i < n)
(hj : j < n), f i hi = f j hj → i = j) :
s.card = n :=
begin
classical,
have : ∀ (a : α), a ∈ s ↔ ∃ i (hi : i ∈ range n), f i (mem_range.1 hi) = a,
from λ a, ⟨λ ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩,
λ ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩,
have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)),
by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and],
calc s.card = card ((range n).attach.image $ λ i, f i.1 (mem_range.1 i.2)) :
by rw this
... = card ((range n).attach) :
card_image_of_injective _ $ λ ⟨i, hi⟩ ⟨j, hj⟩ eq,
subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
... = card (range n) : card_attach
... = n : card_range n
end
lemma card_congr {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t)
(h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) :
s.card = t.card :=
by classical;
calc s.card = s.attach.card : card_attach.symm
... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card
: eq.symm (card_image_of_injective _ $ λ a b h, subtype.eq $ h₂ _ _ _ _ h)
... = t.card : congr_arg card (finset.ext $ λ b,
⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _,
λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩)
lemma card_le_card_of_inj_on {t : finset β} (f : α → β) (hf : ∀ a ∈ s, f a ∈ t)
(f_inj : ∀ a₁ ∈ s, ∀ a₂ ∈ s, f a₁ = f a₂ → a₁ = a₂) :
s.card ≤ t.card :=
by classical;
calc s.card = (s.image f).card : (card_image_of_inj_on f_inj).symm
... ≤ t.card : card_le_of_subset $ image_subset_iff.2 hf
/-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole.
-/
lemma exists_ne_map_eq_of_card_lt_of_maps_to {t : finset β} (hc : t.card < s.card)
{f : α → β} (hf : ∀ a ∈ s, f a ∈ t) :
∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y :=
begin
classical,
by_contra' hz,
refine hc.not_le (card_le_card_of_inj_on f hf _),
intros x hx y hy, contrapose, exact hz x hx y hy,
end
lemma le_card_of_inj_on_range (f : ℕ → α) (hf : ∀ i < n, f i ∈ s)
(f_inj : ∀ (i < n) (j < n), f i = f j → i = j) :
n ≤ s.card :=
calc n = card (range n) : (card_range n).symm
... ≤ s.card : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simpa only [mem_range])
lemma surj_on_of_inj_on_of_card_le {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
(hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : t.card ≤ s.card) :
∀ b ∈ t, ∃ a ha, b = f a ha :=
begin
classical,
intros b hb,
have h : (s.attach.image $ λ (a : {a // a ∈ s}), f a a.prop).card = s.card,
{ exact @card_attach _ s ▸ card_image_of_injective _
(λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h) },
have h' : image (λ a : {a // a ∈ s}, f a a.prop) s.attach = t,
{ exact eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in
ha₂ ▸ hf _ _) (by simp [hst, h]) },
rw ←h' at hb,
obtain ⟨a, ha₁, ha₂⟩ := mem_image.1 hb,
exact ⟨a, a.2, ha₂.symm⟩,
end
lemma inj_on_of_surj_on_of_card_le {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
(hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : s.card ≤ t.card) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s)
(ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) :
a₁ = a₂ :=
by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact
let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in
let g : {x // x ∈ t} → {x // x ∈ s} :=
@surj_inv _ _ f'
(λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in
have hg : injective g, from injective_surj_inv _,
have hsg : surjective g, from λ x,
let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x)
(λ x _, show (g x) ∈ s.attach, from mem_attach _ _)
(λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in
⟨y, hy.snd.symm⟩,
have hif : injective f',
from (left_inverse_of_surjective_of_right_inverse hsg
(right_inverse_surj_inv _)).injective,
subtype.ext_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂))
@[simp] lemma card_disj_union (s t : finset α) (h) : (s.disj_union t h).card = s.card + t.card :=
multiset.card_add _ _
/-! ### Lattice structure -/
section lattice
variables [decidable_eq α]
lemma card_union_add_card_inter (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card :=
finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc
lemma card_union_le (s t : finset α) : (s ∪ t).card ≤ s.card + t.card :=
card_union_add_card_inter s t ▸ nat.le_add_right _ _
lemma card_union_eq (h : disjoint s t) : (s ∪ t).card = s.card + t.card :=
by rw [←disj_union_eq_union s t $ λ x, disjoint_left.mp h, card_disj_union _ _ _]
@[simp] lemma card_disjoint_union (h : disjoint s t) : card (s ∪ t) = s.card + t.card :=
card_union_eq h
lemma card_sdiff (h : s ⊆ t) : card (t \ s) = t.card - s.card :=
suffices card (t \ s) = card ((t \ s) ∪ s) - s.card, by rwa sdiff_union_of_subset h at this,
by rw [card_disjoint_union sdiff_disjoint, add_tsub_cancel_right]
lemma card_sdiff_add_card_eq_card {s t : finset α} (h : s ⊆ t) : card (t \ s) + card s = card t :=
((nat.sub_eq_iff_eq_add (card_le_of_subset h)).mp (card_sdiff h).symm).symm
lemma le_card_sdiff (s t : finset α) : t.card - s.card ≤ card (t \ s) :=
calc card t - card s
≤ card t - card (s ∩ t) : tsub_le_tsub_left (card_le_of_subset (inter_subset_left s t)) _
... = card (t \ (s ∩ t)) : (card_sdiff (inter_subset_right s t)).symm
... ≤ card (t \ s) : by rw sdiff_inter_self_right t s
lemma card_sdiff_add_card : (s \ t).card + t.card = (s ∪ t).card :=
by rw [←card_disjoint_union sdiff_disjoint, sdiff_union_self_eq_union]
end lattice
lemma filter_card_add_filter_neg_card_eq_card (p : α → Prop) [decidable_pred p] :
(s.filter p).card + (s.filter (not ∘ p)).card = s.card :=
by { classical, simp [←card_union_eq, filter_union_filter_neg_eq, disjoint_filter] }
/-- Given a set `A` and a set `B` inside it, we can shrink `A` to any appropriate size, and keep `B`
inside it. -/
lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) :
∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B :=
begin
classical,
rcases nat.le.dest h₁ with ⟨k, _⟩,
clear h₁,
induction k with k ih generalizing A,
{ exact ⟨A, h₂, subset.refl _, h.symm⟩ },
have : (A \ B).nonempty,
{ rw [←card_pos, card_sdiff h₂, ←h, nat.add_right_comm,
add_tsub_cancel_right, nat.add_succ],
apply nat.succ_pos },
rcases this with ⟨a, ha⟩,
have z : i + card B + k = card (erase A a),
{ rw [card_erase_of_mem (mem_sdiff.1 ha).1, ←h],
refl },
rcases ih _ z with ⟨B', hB', B'subA', cards⟩,
{ exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ },
{ rintro t th,
apply mem_erase_of_ne_of_mem _ (h₂ th),
rintro rfl,
exact not_mem_sdiff_of_mem_right th ha }
end
/-- We can shrink `A` to any smaller size. -/
lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) :
∃ (B : finset α), B ⊆ A ∧ card B = i :=
let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩
lemma exists_subset_or_subset_of_two_mul_lt_card [decidable_eq α] {X Y : finset α} {n : ℕ}
(hXY : 2 * n < (X ∪ Y).card) :
∃ C : finset α, n < C.card ∧ (C ⊆ X ∨ C ⊆ Y) :=
begin
have h₁ : (X ∩ (Y \ X)).card = 0 := finset.card_eq_zero.mpr (finset.inter_sdiff_self X Y),
have h₂ : (X ∪ Y).card = X.card + (Y \ X).card,
{ rw [←card_union_add_card_inter X (Y \ X), finset.union_sdiff_self_eq_union, h₁, add_zero] },
rw [h₂, two_mul] at hXY,
rcases lt_or_lt_of_add_lt_add hXY with h|h,
{ exact ⟨X, h, or.inl (finset.subset.refl X)⟩ },
{ exact ⟨Y \ X, h, or.inr (finset.sdiff_subset Y X)⟩ }
end
/-! ### Explicit description of a finset from its card -/
lemma card_eq_one : s.card = 1 ↔ ∃ a, s = {a} :=
by cases s; simp only [multiset.card_eq_one, finset.card, ←val_inj, singleton_val]
lemma exists_eq_insert_iff [decidable_eq α] {s t : finset α} :
(∃ a ∉ s, insert a s = t) ↔ s ⊆ t ∧ s.card + 1 = t.card :=
begin
split,
{ rintro ⟨a, ha, rfl⟩,
exact ⟨subset_insert _ _, (card_insert_of_not_mem ha).symm⟩ },
{ rintro ⟨hst, h⟩,
obtain ⟨a, ha⟩ : ∃ a, t \ s = {a},
{ exact card_eq_one.1 (by rw [card_sdiff hst, ←h, add_tsub_cancel_left]) },
refine ⟨a, λ hs, (_ : a ∉ {a}) $ mem_singleton_self _,
by rw [insert_eq, ←ha, sdiff_union_of_subset hst]⟩,
rw ←ha,
exact not_mem_sdiff_of_mem_right hs }
end
lemma card_le_one : s.card ≤ 1 ↔ ∀ (a ∈ s) (b ∈ s), a = b :=
begin
obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty,
{ simp },
refine (nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨_, _⟩),
{ rintro ⟨y, rfl⟩,
simp },
{ exact λ h, ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, λ y hy, h _ hy _ hx⟩⟩ }
end
lemma card_le_one_iff : s.card ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b := by { rw card_le_one, tauto }
lemma card_le_one_iff_subset_singleton [nonempty α] : s.card ≤ 1 ↔ ∃ (x : α), s ⊆ {x} :=
begin
refine ⟨λ H, _, _⟩,
{ obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty,
{ exact ⟨classical.arbitrary α, empty_subset _⟩ },
{ exact ⟨x, λ y hy, by rw [card_le_one.1 H y hy x hx, mem_singleton]⟩ } },
{ rintro ⟨x, hx⟩,
rw ←card_singleton x,
exact card_le_of_subset hx }
end
/-- A `finset` of a subsingleton type has cardinality at most one. -/
lemma card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 :=
finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _
lemma one_lt_card : 1 < s.card ↔ ∃ (a ∈ s) (b ∈ s), a ≠ b :=
by { rw ←not_iff_not, push_neg, exact card_le_one }
lemma one_lt_card_iff : 1 < s.card ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b :=
by { rw one_lt_card, simp only [exists_prop, exists_and_distrib_left] }
lemma two_lt_card_iff : 2 < s.card ↔ ∃ a b c, a ∈ s ∧ b ∈ s ∧ c ∈ s ∧ a ≠ b ∧ a ≠ c ∧ b ≠ c :=
begin
classical,
refine ⟨λ h, _, _⟩,
{ obtain ⟨c, hc⟩ := card_pos.mp (zero_lt_two.trans h),
have : 1 < (s.erase c).card := by rwa [←add_lt_add_iff_right 1, card_erase_add_one hc],
obtain ⟨a, b, ha, hb, hab⟩ := one_lt_card_iff.mp this,
exact ⟨a, b, c, mem_of_mem_erase ha, mem_of_mem_erase hb, hc,
hab, ne_of_mem_erase ha, ne_of_mem_erase hb⟩ },
{ rintros ⟨a, b, c, ha, hb, hc, hab, hac, hbc⟩,
rw [←card_erase_add_one hc, ←card_erase_add_one (mem_erase_of_ne_of_mem hbc hb),
←card_erase_add_one (mem_erase_of_ne_of_mem hab (mem_erase_of_ne_of_mem hac ha))],
apply nat.le_add_left },
end
lemma two_lt_card : 2 < s.card ↔ ∃ (a ∈ s) (b ∈ s) (c ∈ s), a ≠ b ∧ a ≠ c ∧ b ≠ c :=
by simp_rw [two_lt_card_iff, exists_prop, exists_and_distrib_left]
lemma exists_ne_of_one_lt_card (hs : 1 < s.card) (a : α) : ∃ b, b ∈ s ∧ b ≠ a :=
begin
obtain ⟨x, hx, y, hy, hxy⟩ := finset.one_lt_card.mp hs,
by_cases ha : y = a,
{ exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩ },
{ exact ⟨y, hy, ha⟩ }
end
lemma card_eq_succ [decidable_eq α] : s.card = n + 1 ↔ ∃ a t, a ∉ t ∧ insert a t = s ∧ t.card = n :=
⟨λ h,
let ⟨a, has⟩ := card_pos.mp (h.symm ▸ nat.zero_lt_succ _ : 0 < s.card) in
⟨a, s.erase a, s.not_mem_erase a, insert_erase has,
by simp only [h, card_erase_of_mem has, add_tsub_cancel_right]⟩,
λ ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat⟩
lemma card_eq_two [decidable_eq α] : s.card = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} :=
begin
split,
{ rw card_eq_succ,
simp_rw [card_eq_one],
rintro ⟨a, _, hab, rfl, b, rfl⟩,
exact ⟨a, b, not_mem_singleton.1 hab, rfl⟩ },
{ rintro ⟨x, y, h, rfl⟩,
exact card_doubleton h }
end
lemma card_eq_three [decidable_eq α] :
s.card = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} :=
begin
split,
{ rw card_eq_succ,
simp_rw [card_eq_two],
rintro ⟨a, _, abc, rfl, b, c, bc, rfl⟩,
rw [mem_insert, mem_singleton, not_or_distrib] at abc,
exact ⟨a, b, c, abc.1, abc.2, bc, rfl⟩ },
{ rintro ⟨x, y, z, xy, xz, yz, rfl⟩,
simp only [xy, xz, yz, mem_insert, card_insert_of_not_mem, not_false_iff, mem_singleton,
or_self, card_singleton] }
end
/-! ### Inductions -/
/-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to
define an object on `s`. Then one can inductively define an object on all finsets, starting from
the empty set and iterating. This can be used either to define data, or to prove properties. -/
def strong_induction {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
∀ (s : finset α), p s
| s := H s (λ t h, have t.card < s.card, from card_lt_card h, strong_induction t)
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]}
lemma strong_induction_eq {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) (s : finset α) :
strong_induction H s = H s (λ t h, strong_induction H t) :=
by rw strong_induction
/-- Analogue of `strong_induction` with order of arguments swapped. -/
@[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} (s : finset α) :
(∀ s, (∀ t ⊂ s, p t) → p s) → p s :=
λ H, strong_induction H s
lemma strong_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
s.strong_induction_on H = H s (λ t h, t.strong_induction_on H) :=
by { dunfold strong_induction_on, rw strong_induction }
@[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop}
(s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀ t ⊆ s, p t) → p (insert a s)) :
p s :=
finset.strong_induction_on s $ λ s,
finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $
λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
/-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than
`n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of
cardinality less than `n`, starting from finsets of card `n` and iterating. This
can be used either to define data, or to prove properties. -/
def strong_downward_induction {p : finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : finset α},
t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) :
∀ (s : finset α), s.card ≤ n → p s
| s := H s (λ t ht h, have n - t.card < n - s.card,
from (tsub_lt_tsub_iff_left_of_le ht).2 (finset.card_lt_card h),
strong_downward_induction t ht)
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : finset α), n - t.card)⟩]}
lemma strong_downward_induction_eq {p : finset α → Sort*}
(H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁)
(s : finset α) :
strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) :=
by rw strong_downward_induction
/-- Analogue of `strong_downward_induction` with order of arguments swapped. -/
@[elab_as_eliminator] def strong_downward_induction_on {p : finset α → Sort*} (s : finset α)
(H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) :
s.card ≤ n → p s :=
strong_downward_induction H s
lemma strong_downward_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ t₁,
(∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) :
s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) :=
by { dunfold strong_downward_induction_on, rw strong_downward_induction }
lemma lt_wf {α} : well_founded (@has_lt.lt (finset α) _) :=
have H : subrelation (@has_lt.lt (finset α) _)
(inv_image ( < ) card),
from λ x y hxy, card_lt_card hxy,
subrelation.wf H $ inv_image.wf _ $ nat.lt_wf
end finset
|
853f6f6773c168726b2ad21767a548cdd594d429 | c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447 | /examples/universes.lean | 60f74e0cfe1bead995cb07f370aadb53d8ef3243 | [
"Apache-2.0"
] | permissive | rwbarton/lean-category-theory-pr | 77207b6674eeec1e258ec85dea58f3bff8d27065 | 591847d70c6a11c4d5561cd0eaf69b1fe85a70ab | refs/heads/master | 1,584,595,111,303 | 1,528,029,041,000 | 1,528,029,041,000 | 135,919,126 | 0 | 0 | null | 1,528,041,805,000 | 1,528,041,805,000 | null | UTF-8 | Lean | false | false | 2,781 | lean | universes u₁ v₁ u₂ v₂
structure Category :=
(Obj : Type u₁)
(Hom : Obj → Obj → Type v₁)
(identity : Π X : Obj, Hom X X)
(compose : Π {X Y Z : Obj}, Hom X Y → Hom Y Z → Hom X Z)
(left_identity : ∀ {X Y : Obj} (f : Hom X Y), compose (identity X) f = f)
(right_identity : ∀ {X Y : Obj} (f : Hom X Y), compose f (identity Y) = f)
(associativity : ∀ {W X Y Z : Obj} (f : Hom W X) (g : Hom X Y) (h : Hom Y Z), compose (compose f g) h = compose f (compose g h))
structure Functor (C : Category.{u₁ v₁}) (D : Category.{u₂ v₂}) :=
(onObjects : C.Obj → D.Obj)
(onMorphisms : Π {X Y : C.Obj}, (C.Hom X Y) → (D.Hom (onObjects X) (onObjects Y)))
(identities : ∀ (X : C.Obj), onMorphisms (C.identity X) = D.identity (onObjects X))
(functoriality : ∀ {X Y Z : C.Obj} (f : C.Hom X Y) (g : C.Hom Y Z), onMorphisms (C.compose f g) = D.compose (onMorphisms f) (onMorphisms g))
structure NaturalTransformation {C : Category.{u₁ v₁}} {D : Category.{u₂ v₂}} (F G : Functor C D) :=
(components: Π X : C.Obj, D.Hom (F.onObjects X) (G.onObjects X))
(naturality: ∀ {X Y : C.Obj} (f : C.Hom X Y), D.compose (F.onMorphisms f) (components Y) = D.compose (components X) (G.onMorphisms f))
#print Category -- Type (max (u₁+1) (v₁+1))
#print Functor -- Type (max u₁ u₂ v₁ v₂)
#print NaturalTransformation -- Type (max u₁ v₂)
definition FunctorCategory (C : Category.{u₁ v₁}) (D : Category.{u₂ v₂}) : Category :=
by refine { Obj := Functor C D,
Hom := λ F G, NaturalTransformation F G, .. } ; sorry
set_option pp.universes true
#print FunctorCategory -- Category.{(max u₁ u₂ v₁ v₂) (max u₁ v₂)}
/-
Summarising, we have:
Category : Type (max (u₁+1) (v₁+1))
C : Category.{u₁ v₁}
D : Category.{u₂ v₂}
Functor C D : Type (max u₁ u₂ v₁ v₂)
NaturalTransformation : Type (max u₁ v₂)
FunctorCategory : Category.{(max u₁ u₂ v₁ v₂) (max u₁ v₂)}
If uᵢ=vᵢ+1, we then have:
Category : Type (v₁+2)
C : Category.{v₁+1 v₁}
D : Category.{v₂+1 v₂}
Functor C D : Type (max v₁+1 v₂+1)
NaturalTransformation : Type (max (v₁+1) v₂)
FunctorCategory C D : Category.{(max (v₁+1) (v₂+1)) (max (v₁+1) v₂)}
If moreover v₂=v₁, then
FunctorCategory C D : Category.{(v₁+1) (v₁+1)}
That is, the FunctorCategory is a whole level up (but small...)
If C is a small category, so u₁=v₁, and D has u₂=v₂+1, we then have:
Functor C D : Type (max v₁ v₂+1)
FunctorCategory C D : Category.{(mav v₁ (v₂+1)) (max v₁ v₂)}
If moreover v₂=v₁, then
FunctorCategory C D : Category.{(v₁+1) v₁}
That is, FunctorCategory is a large category at the same level as we started.
-/ |
4ec293b8c57bb1f55e33700fbf260a6f8bd68ddc | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /tactic/chain.lean | b9a3da2a742a7fdd48b049f5eb3a01ea8487d35b | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 5,231 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison, Mario Carneiro
import tactic
import data.option
open interactive
namespace tactic
/-
This file defines a `chain` tactic, which takes a list of tactics, and exhaustively tries to apply them
to the goals, until no tactic succeeds on any goal.
Along the way, it generates auxiliary declarations, in order to speed up elaboration time
of the resulting (sometimes long!) proofs.
This tactic is used by the `tidy` tactic.
-/
-- α is the return type of our tactics. When `chain` is called by `tidy`, this is string,
-- describing what that tactic did as an interactive tactic.
variable {α : Type}
/-
Because chain sometimes pauses work on the first goal and works on later goals, we need a method
for combining a list of results generated while working on a later goal into a single result.
This enables `tidy {trace_result := tt}` to output faithfully reproduces its operation, e.g.
````
intros,
simp,
apply lemma_1,
work_on_goal 2 {
dsimp,
simp
},
refl
````
-/
namespace interactive
open lean.parser
meta def work_on_goal : parse small_nat → itactic → tactic unit
| n t := do goals ← get_goals,
let earlier_goals := goals.take n,
let later_goals := goals.drop (n+1),
set_goals (goals.nth n).to_list,
t,
new_goals ← get_goals,
set_goals (earlier_goals ++ new_goals ++ later_goals)
end interactive
inductive tactic_script (α : Type) : Type
| base : α → tactic_script
| work (index : ℕ) (first : α) (later : list tactic_script) (closed : bool) : tactic_script
meta def tactic_script.to_string : tactic_script string → string
| (tactic_script.base a) := a
| (tactic_script.work n a l c) := "work_on_goal " ++ (to_string n) ++ " { " ++ (", ".intercalate (a :: l.map(λ m : tactic_script string, m.to_string))) ++ " }"
meta instance : has_to_string (tactic_script string) :=
{ to_string := λ s, s.to_string }
meta instance tactic_script_unit_has_to_string : has_to_string (tactic_script unit) :=
{ to_string := λ s, "[chain tactic]" }
meta def abstract_if_success {α} (tac : expr → tactic α) (g : expr) : tactic α :=
do
type ← infer_type g,
is_lemma ← is_prop type,
if is_lemma then -- there's no point making the abstraction, and indeed it's slower
tac g
else do
m ← mk_meta_var type,
a ← tac m,
do {
val ← instantiate_mvars m,
guard (val.list_meta_vars = []),
c ← new_aux_decl_name,
gs ← get_goals,
set_goals [g],
add_aux_decl c type val ff >>= unify g,
set_goals gs }
<|> unify m g,
return a
/--
`chain_many tac` recursively tries `tac` on all goals, working depth-first on generated subgoals,
until it no longer succeeds on any goal. `chain_many` automatically makes auxiliary definitions.
-/
meta mutual def chain_single, chain_many, chain_iter {α} (tac : tactic α)
with chain_single : expr → tactic (α × list (tactic_script α)) | g :=
do set_goals [g],
a ← tac,
l ← get_goals >>= chain_many,
return (a, l)
with chain_many : list expr → tactic (list (tactic_script α))
| [] := return []
| [g] := do {
(a, l) ← chain_single g,
return (tactic_script.base a :: l) } <|> return []
| gs := chain_iter gs []
with chain_iter : list expr → list expr → tactic (list (tactic_script α))
| [] _ := return []
| (g :: later_goals) stuck_goals := do {
(a, l) ← abstract_if_success chain_single g,
new_goals ← get_goals,
let w := tactic_script.work stuck_goals.length a l (new_goals = []),
let current_goals := stuck_goals.reverse ++ new_goals ++ later_goals,
set_goals current_goals, -- we keep the goals up to date, so they are correct at the end
l' ← chain_many current_goals,
return (w :: l') } <|> chain_iter later_goals (g :: stuck_goals)
meta def chain_core {α : Type} [has_to_string (tactic_script α)] (tactics : list (tactic α)) : tactic (list string) :=
do results ← (get_goals >>= chain_many (first tactics)),
when (results.empty) (fail "`chain` tactic made no progress"),
return (results.map (λ r : tactic_script α, to_string r))
variables [has_to_string (tactic_script α)] [has_to_format α]
declare_trace chain
meta def trace_output (t : tactic α) : tactic α :=
do tgt ← target,
r ← t,
name ← decl_name,
trace format!"`chain` successfully applied a tactic during elaboration of {name}:",
tgt ← pp tgt,
trace format!"previous target: {tgt}",
trace format!"tactic result: {r}",
tgt ← try_core target,
tgt ← match tgt with
| (some tgt) := pp tgt
| none := return "no goals"
end,
trace format!"new target: {tgt}",
pure r
private meta def chain_handle_trace (tactics : list (tactic α)) : tactic (list string) :=
if is_trace_enabled_for `chain then
chain_core (tactics.map trace_output)
else
chain_core tactics
meta def chain (tactics : list (tactic α)) : tactic (list string) :=
do sequence ← chain_handle_trace tactics,
when (sequence.empty) (fail "`chain` tactic made no progress"),
pure sequence
end tactic |
95b1e2d463437563f94d84ec790d4104762a7ae1 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/polynomial/derivative.lean | 68438eebd7f6dc8e5664289b7b357b75f9fd8c61 | [
"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,327 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import data.polynomial.eval
import algebra.iterate_hom
/-!
# The derivative map on polynomials
## Main definitions
* `polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map.
-/
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open finsupp finset
open_locale big_operators
namespace polynomial
universes u v w y z
variables {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {A : Type z} {a b : R} {n : ℕ}
section derivative
section semiring
variables [semiring R]
/-- `derivative p` is the formal derivative of the polynomial `p` -/
def derivative : polynomial R →ₗ[R] polynomial R :=
finsupp.total ℕ (polynomial R) R (λ n, C ↑n * X^(n - 1))
lemma derivative_apply (p : polynomial R) :
derivative p = p.sum (λn a, C (a * n) * X^(n - 1)) :=
begin
rw [derivative, total_apply],
apply congr rfl,
ext,
simp [mul_assoc, coeff_C_mul],
end
lemma coeff_derivative (p : polynomial R) (n : ℕ) :
coeff (derivative p) n = coeff p (n + 1) * (n + 1) :=
begin
rw [derivative_apply],
simp only [coeff_X_pow, coeff_sum, coeff_C_mul],
rw [sum_def, finset.sum_eq_single (n + 1)],
simp only [nat.add_succ_sub_one, add_zero, mul_one, if_true, eq_self_iff_true], norm_cast,
swap,
{ rw [if_pos (nat.add_sub_cancel _ _).symm, mul_one, nat.cast_add, nat.cast_one, mem_support_iff],
intro h, push_neg at h, simp [h], },
{ assume b, cases b,
{ intros, rw [nat.cast_zero, mul_zero, zero_mul], },
{ intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } }
end
@[simp]
lemma derivative_zero : derivative (0 : polynomial R) = 0 :=
derivative.map_zero
@[simp]
lemma iterate_derivative_zero {k : ℕ} : derivative^[k] (0 : polynomial R) = 0 :=
begin
induction k with k ih,
{ simp, },
{ simp [ih], },
end
@[simp]
lemma derivative_monomial (a : R) (n : ℕ) : derivative (monomial n a) = monomial (n - 1) (a * n) :=
(derivative_apply _).trans ((sum_single_index $ by simp).trans (C_mul_X_pow_eq_monomial _ _))
lemma derivative_C_mul_X_pow (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) :=
by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial]
@[simp] lemma derivative_X_pow (n : ℕ) :
derivative (X ^ n : polynomial R) = (n : polynomial R) * X ^ (n - 1) :=
by convert derivative_C_mul_X_pow (1 : R) n; simp
@[simp] lemma derivative_C {a : R} : derivative (C a) = 0 :=
by simp [derivative_apply]
@[simp] lemma derivative_X : derivative (X : polynomial R) = 1 :=
(derivative_monomial _ _).trans $ by simp
@[simp] lemma derivative_one : derivative (1 : polynomial R) = 0 :=
derivative_C
@[simp] lemma derivative_bit0 {a : polynomial R} : derivative (bit0 a) = bit0 (derivative a) :=
by simp [bit0]
@[simp] lemma derivative_bit1 {a : polynomial R} : derivative (bit1 a) = bit0 (derivative a) :=
by simp [bit1]
@[simp] lemma derivative_add {f g : polynomial R} :
derivative (f + g) = derivative f + derivative g :=
derivative.map_add f g
@[simp] lemma iterate_derivative_add {f g : polynomial R} {k : ℕ} :
derivative^[k] (f + g) = (derivative^[k] f) + (derivative^[k] g) :=
derivative.to_add_monoid_hom.iterate_map_add _ _ _
@[simp] lemma derivative_neg {R : Type*} [ring R] (f : polynomial R) :
derivative (-f) = - derivative f :=
linear_map.map_neg derivative f
@[simp] lemma iterate_derivative_neg {R : Type*} [ring R] {f : polynomial R} {k : ℕ} :
derivative^[k] (-f) = - (derivative^[k] f) :=
(@derivative R _).to_add_monoid_hom.iterate_map_neg _ _
@[simp] lemma derivative_sub {R : Type*} [ring R] {f g : polynomial R} :
derivative (f - g) = derivative f - derivative g :=
linear_map.map_sub derivative f g
@[simp] lemma iterate_derivative_sub {R : Type*} [ring R] {k : ℕ} {f g : polynomial R} :
derivative^[k] (f - g) = (derivative^[k] f) - (derivative^[k] g) :=
begin
induction k with k ih generalizing f g,
{ simp [nat.iterate], },
{ simp [nat.iterate, ih], }
end
@[simp] lemma derivative_sum {s : finset ι} {f : ι → polynomial R} :
derivative (∑ b in s, f b) = ∑ b in s, derivative (f b) :=
derivative.map_sum
@[simp] lemma derivative_smul (r : R) (p : polynomial R) : derivative (r • p) = r • derivative p :=
derivative.map_smul _ _
@[simp] lemma iterate_derivative_smul (r : R) (p : polynomial R) (k : ℕ) :
derivative^[k] (r • p) = r • (derivative^[k] p) :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih], },
end
/-- We can't use `derivative_mul` here because
we want to prove this statement also for noncommutative rings.-/
@[simp]
lemma derivative_C_mul (a : R) (p : polynomial R) : derivative (C a * p) = C a * derivative p :=
by convert derivative_smul a p; apply C_mul'
@[simp]
lemma iterate_derivative_C_mul (a : R) (p : polynomial R) (k : ℕ) :
derivative^[k] (C a * p) = C a * (derivative^[k] p) :=
by convert iterate_derivative_smul a p k; apply C_mul'
end semiring
section comm_semiring
variables [comm_semiring R]
lemma derivative_eval (p : polynomial R) (x : R) :
p.derivative.eval x = p.sum (λ n a, (a * n)*x^(n-1)) :=
by simp only [derivative_apply, eval_sum, eval_pow, eval_C, eval_X, eval_nat_cast, eval_mul]
@[simp] lemma derivative_mul {f g : polynomial R} :
derivative (f * g) = derivative f * g + f * derivative g :=
calc derivative (f * g) = f.sum (λn a, g.sum (λm b, C ((a * b) * (n + m : ℕ)) * X^((n + m) - 1))) :
begin
transitivity, exact derivative_sum,
transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum },
apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm,
transitivity,
{ apply congr_arg, exact single_eq_C_mul_X },
exact derivative_C_mul_X_pow _ _
end
... = f.sum (λn a, g.sum (λm b,
(C (a * n) * X^(n - 1)) * (C b * X^m) + (C a * X^n) * (C (b * m) * X^(m - 1)))) :
sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm,
by simp only [nat.cast_add, mul_add, add_mul, C_add, C_mul];
cases n; simp only [nat.succ_sub_succ, pow_zero];
cases m; simp only [nat.cast_zero, C_0, nat.succ_sub_succ, zero_mul, mul_zero,
nat.sub_zero, pow_zero, pow_add, one_mul, pow_succ, mul_comm, mul_left_comm]
... = derivative f * g + f * derivative g :
begin
conv { to_rhs, congr,
{ rw [← sum_C_mul_X_eq g] },
{ rw [← sum_C_mul_X_eq f] } },
simp only [finsupp.sum, sum_add_distrib, finset.mul_sum, finset.sum_mul, derivative_apply]
end
theorem derivative_pow_succ (p : polynomial R) (n : ℕ) :
(p ^ (n + 1)).derivative = (n + 1) * (p ^ n) * p.derivative :=
nat.rec_on n (by rw [pow_one, nat.cast_zero, zero_add, one_mul, pow_zero, one_mul]) $ λ n ih,
by rw [pow_succ', derivative_mul, ih, mul_right_comm, ← add_mul,
add_mul (n.succ : polynomial R), one_mul, pow_succ', mul_assoc, n.cast_succ]
theorem derivative_pow (p : polynomial R) (n : ℕ) :
(p ^ n).derivative = n * (p ^ (n - 1)) * p.derivative :=
nat.cases_on n (by rw [pow_zero, derivative_one, nat.cast_zero, zero_mul, zero_mul]) $ λ n,
by rw [p.derivative_pow_succ n, n.succ_sub_one, n.cast_succ]
lemma derivative_comp (p q : polynomial R) :
(p.comp q).derivative = q.derivative * p.derivative.comp q :=
begin
apply polynomial.induction_on' p,
{ intros p₁ p₂ h₁ h₂, simp [h₁, h₂, mul_add], },
{ intros n r,
simp only [derivative_pow, derivative_mul, monomial_comp, derivative_monomial, derivative_C,
zero_mul, C_eq_nat_cast, zero_add, ring_hom.map_mul],
-- is there a tactic for this? (a multiplicative `abel`):
rw [mul_comm (derivative q)],
simp only [mul_assoc], }
end
@[simp]
theorem derivative_map [comm_semiring S] (p : polynomial R) (f : R →+* S) :
(p.map f).derivative = p.derivative.map f :=
polynomial.induction_on p
(λ r, by rw [map_C, derivative_C, derivative_C, map_zero])
(λ p q ihp ihq, by rw [map_add, derivative_add, ihp, ihq, derivative_add, map_add])
(λ n r ih, by rw [map_mul, map_C, map_pow, map_X,
derivative_mul, derivative_pow_succ, derivative_C, zero_mul, zero_add, derivative_X, mul_one,
derivative_mul, derivative_pow_succ, derivative_C, zero_mul, zero_add, derivative_X, mul_one,
map_mul, map_C, map_mul, map_pow, map_add, map_nat_cast, map_one, map_X])
@[simp]
theorem iterate_derivative_map [comm_semiring S] (p : polynomial R) (f : R →+* S) (k : ℕ):
polynomial.derivative^[k] (p.map f) = (polynomial.derivative^[k] p).map f :=
begin
induction k with k ih generalizing p,
{ simp, },
{ simp [ih], },
end
/-- Chain rule for formal derivative of polynomials. -/
theorem derivative_eval₂_C (p q : polynomial R) :
(p.eval₂ C q).derivative = p.derivative.eval₂ C q * q.derivative :=
polynomial.induction_on p
(λ r, by rw [eval₂_C, derivative_C, eval₂_zero, zero_mul])
(λ p₁ p₂ ih₁ ih₂, by rw [eval₂_add, derivative_add, ih₁, ih₂, derivative_add, eval₂_add, add_mul])
(λ n r ih, by rw [pow_succ', ← mul_assoc, eval₂_mul, eval₂_X, derivative_mul, ih,
@derivative_mul _ _ _ X, derivative_X, mul_one, eval₂_add, @eval₂_mul _ _ _ _ X, eval₂_X,
add_mul, mul_right_comm])
theorem of_mem_support_derivative {p : polynomial R} {n : ℕ} (h : n ∈ p.derivative.support) :
n + 1 ∈ p.support :=
finsupp.mem_support_iff.2 $ λ (h1 : p.coeff (n+1) = 0), finsupp.mem_support_iff.1 h $
show p.derivative.coeff n = 0, by rw [coeff_derivative, h1, zero_mul]
theorem degree_derivative_lt {p : polynomial R} (hp : p ≠ 0) : p.derivative.degree < p.degree :=
(finset.sup_lt_iff $ bot_lt_iff_ne_bot.2 $ mt degree_eq_bot.1 hp).2 $ λ n hp, lt_of_lt_of_le
(with_bot.some_lt_some.2 n.lt_succ_self) $ finset.le_sup $ of_mem_support_derivative hp
theorem nat_degree_derivative_lt {p : polynomial R} (hp : p.derivative ≠ 0) :
p.derivative.nat_degree < p.nat_degree :=
have hp1 : p ≠ 0, from λ h, hp $ by rw [h, derivative_zero],
with_bot.some_lt_some.1 $
begin
rw [nat_degree, option.get_or_else_of_ne_none $ mt degree_eq_bot.1 hp, nat_degree,
option.get_or_else_of_ne_none $ mt degree_eq_bot.1 hp1],
exact degree_derivative_lt hp1
end
theorem degree_derivative_le {p : polynomial R} : p.derivative.degree ≤ p.degree :=
if H : p = 0 then le_of_eq $ by rw [H, derivative_zero] else le_of_lt $ degree_derivative_lt H
/-- The formal derivative of polynomials, as linear homomorphism. -/
def derivative_lhom (R : Type*) [comm_ring R] : polynomial R →ₗ[R] polynomial R :=
{ to_fun := derivative,
map_add' := λ p q, derivative_add,
map_smul' := λ r p, derivative_smul r p }
@[simp] lemma derivative_lhom_coe {R : Type*} [comm_ring R] :
(polynomial.derivative_lhom R : polynomial R → polynomial R) = polynomial.derivative :=
rfl
@[simp] lemma derivative_cast_nat {n : ℕ} : derivative (n : polynomial R) = 0 :=
begin
rw ← C.map_nat_cast n,
exact derivative_C,
end
@[simp] lemma iterate_derivative_cast_nat_mul {n k : ℕ} {f : polynomial R} :
derivative^[k] (n * f) = n * (derivative^[k] f) :=
begin
induction k with k ih generalizing f,
{ simp [nat.iterate], },
{ simp [nat.iterate, ih], }
end
end comm_semiring
section domain
variables [integral_domain R]
lemma mem_support_derivative [char_zero R] (p : polynomial R) (n : ℕ) :
n ∈ (derivative p).support ↔ n + 1 ∈ p.support :=
suffices (¬(coeff p (n + 1) = 0 ∨ ((n + 1:ℕ) : R) = 0)) ↔ coeff p (n + 1) ≠ 0,
by simpa only [mem_support_iff, coeff_derivative, ne.def, mul_eq_zero],
by { rw [nat.cast_eq_zero], simp only [nat.succ_ne_zero, or_false] }
@[simp] lemma degree_derivative_eq [char_zero R] (p : polynomial R) (hp : 0 < nat_degree p) :
degree (derivative p) = (nat_degree p - 1 : ℕ) :=
begin
have h0 : p ≠ 0,
{ contrapose! hp,
simp [hp] },
apply le_antisymm,
{ rw derivative_apply,
apply le_trans (degree_sum_le _ _) (sup_le (λ n hn, _)),
apply le_trans (degree_C_mul_X_pow_le _ _) (with_bot.coe_le_coe.2 (nat.sub_le_sub_right _ _)),
apply le_nat_degree_of_mem_supp _ hn },
{ refine le_sup _,
rw [mem_support_derivative, nat.sub_add_cancel, mem_support_iff],
{ show ¬ leading_coeff p = 0,
rw [leading_coeff_eq_zero],
assume h, rw [h, nat_degree_zero] at hp,
exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), },
exact hp }
end
theorem nat_degree_eq_zero_of_derivative_eq_zero
[char_zero R] {f : polynomial R} (h : f.derivative = 0) :
f.nat_degree = 0 :=
begin
by_cases hf : f = 0,
{ exact (congr_arg polynomial.nat_degree hf).trans rfl },
{ rw nat_degree_eq_zero_iff_degree_le_zero,
by_contra absurd,
have f_nat_degree_pos : 0 < f.nat_degree,
{ rwa [not_le, ←nat_degree_pos_iff_degree_pos] at absurd },
let m := f.nat_degree - 1,
have hm : m + 1 = f.nat_degree := nat.sub_add_cancel f_nat_degree_pos,
have h2 := coeff_derivative f m,
rw polynomial.ext_iff at h,
rw [h m, coeff_zero, zero_eq_mul] at h2,
cases h2,
{ rw [hm, ←leading_coeff, leading_coeff_eq_zero] at h2,
exact hf h2, },
{ norm_cast at h2 } }
end
end domain
end derivative
end polynomial
|
c8aea4c512efa007d3e9a3f2225007ea155bb6ab | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/category_theory/equivalence.lean | 9cbe8fa3df704e6ec4bf7d7b504b46c71fdb48d4 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 23,964 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn
-/
import category_theory.fully_faithful
import category_theory.full_subcategory
import category_theory.whiskering
import category_theory.essential_image
import tactic.slice
/-!
# Equivalence of categories
An equivalence of categories `C` and `D` is a pair of functors `F : C ⥤ D` and `G : D ⥤ C` such
that `η : 𝟭 C ≅ F ⋙ G` and `ε : G ⋙ F ≅ 𝟭 D`. In many situations, equivalences are a better
notion of "sameness" of categories than the stricter isomorphims of categories.
Recall that one way to express that two functors `F : C ⥤ D` and `G : D ⥤ C` are adjoint is using
two natural transformations `η : 𝟭 C ⟶ F ⋙ G` and `ε : G ⋙ F ⟶ 𝟭 D`, called the unit and the
counit, such that the compositions `F ⟶ FGF ⟶ F` and `G ⟶ GFG ⟶ G` are the identity. Unfortunately,
it is not the case that the natural isomorphisms `η` and `ε` in the definition of an equivalence
automatically give an adjunction. However, it is true that
* if one of the two compositions is the identity, then so is the other, and
* given an equivalence of categories, it is always possible to refine `η` in such a way that the
identities are satisfied.
For this reason, in mathlib we define an equivalence to be a "half-adjoint equivalence", which is
a tuple `(F, G, η, ε)` as in the first paragraph such that the composite `F ⟶ FGF ⟶ F` is the
identity. By the remark above, this already implies that the tuple is an "adjoint equivalence",
i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity.
We also define essentially surjective functors and show that a functor is an equivalence if and only
if it is full, faithful and essentially surjective.
## Main definitions
* `equivalence`: bundled (half-)adjoint equivalences of categories
* `is_equivalence`: type class on a functor `F` containing the data of the inverse `G` as well as
the natural isomorphisms `η` and `ε`.
* `ess_surj`: type class on a functor `F` containing the data of the preimages and the isomorphisms
`F.obj (preimage d) ≅ d`.
## Main results
* `equivalence.mk`: upgrade an equivalence to a (half-)adjoint equivalence
* `equivalence.of_fully_faithfully_ess_surj`: a fully faithful essentially surjective functor is an
equivalence.
## Notations
We write `C ≌ D` (`\backcong`, not to be confused with `≅`/`\cong`) for a bundled equivalence.
-/
namespace category_theory
open category_theory.functor nat_iso category
-- declare the `v`'s first; see `category_theory.category` for an explanation
universes v₁ v₂ v₃ u₁ u₂ u₃
/-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with
a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other
words the composite `F ⟶ FGF ⟶ F` is the identity.
In `unit_inverse_comp`, we show that this is actually an adjoint equivalence, i.e., that the
composite `G ⟶ GFG ⟶ G` is also the identity.
The triangle equation is written as a family of equalities between morphisms, it is more
complicated if we write it as an equality of natural transformations, because then we would have
to insert natural transformations like `F ⟶ F1`.
See https://stacks.math.columbia.edu/tag/001J
-/
structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] :=
mk' ::
(functor : C ⥤ D)
(inverse : D ⥤ C)
(unit_iso : 𝟭 C ≅ functor ⋙ inverse)
(counit_iso : inverse ⋙ functor ≅ 𝟭 D)
(functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫
counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously)
restate_axiom equivalence.functor_unit_iso_comp'
infixr ` ≌ `:10 := equivalence
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
namespace equivalence
/-- The unit of an equivalence of categories. -/
abbreviation unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom
/-- The counit of an equivalence of categories. -/
abbreviation counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom
/-- The inverse of the unit of an equivalence of categories. -/
abbreviation unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv
/-- The inverse of the counit of an equivalence of categories. -/
abbreviation counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv
/- While these abbreviations are convenient, they also cause some trouble,
preventing structure projections from unfolding. -/
@[simp] lemma equivalence_mk'_unit (functor inverse unit_iso counit_iso f) :
(⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit = unit_iso.hom := rfl
@[simp] lemma equivalence_mk'_counit (functor inverse unit_iso counit_iso f) :
(⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit = counit_iso.hom := rfl
@[simp] lemma equivalence_mk'_unit_inv (functor inverse unit_iso counit_iso f) :
(⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit_inv = unit_iso.inv := rfl
@[simp] lemma equivalence_mk'_counit_inv (functor inverse unit_iso counit_iso f) :
(⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit_inv = counit_iso.inv := rfl
@[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) :
e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) :=
e.functor_unit_iso_comp X
@[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) :
e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) :=
begin
erw [iso.inv_eq_inv
(e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)],
exact e.functor_unit_comp X
end
lemma counit_inv_app_functor (e : C ≌ D) (X : C) :
e.counit_inv.app (e.functor.obj X) = e.functor.map (e.unit.app X) :=
by { symmetry, erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl }
lemma counit_app_functor (e : C ≌ D) (X : C) :
e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) :=
by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl }
/-- The other triangle equality. The proof follows the following proof in Globular:
http://globular.science/1905.001 -/
@[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) :
e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) :=
begin
rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp,
←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)),
app_hom, app_inv],
slice_lhs 2 3 { erw [e.unit.naturality] },
slice_lhs 1 2 { erw [e.unit.naturality] },
slice_lhs 4 4
{ rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] },
slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality],
erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp],
slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] },
slice_lhs 3 4 { erw [e.unit_inv.naturality] },
slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] },
erw [id_comp],
slice_lhs 3 4 { erw [←e.unit_inv.naturality] },
slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality,
(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl
end
@[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) :
e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) :=
begin
erw [iso.inv_eq_inv
(e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)],
exact e.unit_inverse_comp Y
end
lemma unit_app_inverse (e : C ≌ D) (Y : D) :
e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) :=
by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl }
lemma unit_inv_app_inverse (e : C ≌ D) (Y : D) :
e.unit_inv.app (e.inverse.obj Y) = e.inverse.map (e.counit.app Y) :=
by { symmetry, erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl }
@[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) :
e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y :=
(nat_iso.naturality_2 (e.counit_iso) f).symm
@[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) :
e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y :=
(nat_iso.naturality_1 (e.unit_iso) f).symm
section
-- In this section we convert an arbitrary equivalence to a half-adjoint equivalence.
variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D)
/-- If `η : 𝟭 C ≅ F ⋙ G` is part of a (not necessarily half-adjoint) equivalence, we can upgrade it
to a refined natural isomorphism `adjointify_η η : 𝟭 C ≅ F ⋙ G` which exhibits the properties
required for a half-adjoint equivalence. See `equivalence.mk`. -/
def adjointify_η : 𝟭 C ≅ F ⋙ G :=
calc
𝟭 C ≅ F ⋙ G : η
... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm
... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G)
... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G)
... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm
... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G)
... ≅ F ⋙ G : left_unitor (F ⋙ G)
lemma adjointify_η_ε (X : C) :
F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) :=
begin
dsimp [adjointify_η], simp,
have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this,
rw [←assoc _ _ (F.map _)],
have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this,
have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this,
rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this]
end
end
/-- Every equivalence of categories consisting of functors `F` and `G` such that `F ⋙ G` and
`G ⋙ F` are naturally isomorphic to identity functors can be transformed into a half-adjoint
equivalence without changing `F` or `G`. -/
protected definition mk (F : C ⥤ D) (G : D ⥤ C)
(η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D :=
⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩
/-- Equivalence of categories is reflexive. -/
@[refl, simps] def refl : C ≌ C :=
⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩
instance : inhabited (C ≌ C) :=
⟨refl⟩
/-- Equivalence of categories is symmetric. -/
@[symm, simps] def symm (e : C ≌ D) : D ≌ C :=
⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩
variables {E : Type u₃} [category.{v₃} E]
/-- Equivalence of categories is transitive. -/
@[trans, simps] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E :=
{ functor := e.functor ⋙ f.functor,
inverse := f.inverse ⋙ e.inverse,
unit_iso :=
begin
refine iso.trans e.unit_iso _,
exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) ,
end,
counit_iso :=
begin
refine iso.trans _ f.counit_iso,
exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor)
end,
-- We wouldn't have needed to give this proof if we'd used `equivalence.mk`,
-- but we choose to avoid using that here, for the sake of good structure projection `simp`
-- lemmas.
functor_unit_iso_comp' := λ X,
begin
dsimp,
rw [← f.functor.map_comp_assoc, e.functor.map_comp, ←counit_inv_app_functor, fun_inv_map,
iso.inv_hom_id_app_assoc, assoc, iso.inv_hom_id_app, counit_app_functor,
← functor.map_comp],
erw [comp_id, iso.hom_inv_id_app, functor.map_id],
end }
/-- Composing a functor with both functors of an equivalence yields a naturally isomorphic
functor. -/
def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F :=
(functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor
@[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) :
(fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) :=
by { dsimp [fun_inv_id_assoc], tidy }
@[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) :
(fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) :=
by { dsimp [fun_inv_id_assoc], tidy }
/-- Composing a functor with both functors of an equivalence yields a naturally isomorphic
functor. -/
def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F :=
(functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor
@[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) :
(inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) :=
by { dsimp [inv_fun_id_assoc], tidy }
@[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) :
(inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) :=
by { dsimp [inv_fun_id_assoc], tidy }
/-- If `C` is equivalent to `D`, then `C ⥤ E` is equivalent to `D ⥤ E`. -/
@[simps functor inverse unit_iso counit_iso]
def congr_left (e : C ≌ D) : (C ⥤ E) ≌ (D ⥤ E) :=
equivalence.mk
((whiskering_left _ _ _).obj e.inverse)
((whiskering_left _ _ _).obj e.functor)
(nat_iso.of_components (λ F, (e.fun_inv_id_assoc F).symm) (by tidy))
(nat_iso.of_components (λ F, e.inv_fun_id_assoc F) (by tidy))
/-- If `C` is equivalent to `D`, then `E ⥤ C` is equivalent to `E ⥤ D`. -/
@[simps functor inverse unit_iso counit_iso]
def congr_right (e : C ≌ D) : (E ⥤ C) ≌ (E ⥤ D) :=
equivalence.mk
((whiskering_right _ _ _).obj e.functor)
((whiskering_right _ _ _).obj e.inverse)
(nat_iso.of_components
(λ F, F.right_unitor.symm ≪≫ iso_whisker_left F e.unit_iso ≪≫ functor.associator _ _ _)
(by tidy))
(nat_iso.of_components
(λ F, functor.associator _ _ _ ≪≫ iso_whisker_left F e.counit_iso ≪≫ F.right_unitor)
(by tidy))
section cancellation_lemmas
variables (e : C ≌ D)
/- We need special forms of `cancel_nat_iso_hom_right(_assoc)` and
`cancel_nat_iso_inv_right(_assoc)` for units and counits, because neither `simp` or `rw` will apply
those lemmas in this setting without providing `e.unit_iso` (or similar) as an explicit argument.
We also provide the lemmas for length four compositions, since they're occasionally useful.
(e.g. in proving that equivalences take monos to monos) -/
@[simp] lemma cancel_unit_right {X Y : C}
(f f' : X ⟶ Y) :
f ≫ e.unit.app Y = f' ≫ e.unit.app Y ↔ f = f' :=
by simp only [cancel_mono]
@[simp] lemma cancel_unit_inv_right {X Y : C}
(f f' : X ⟶ e.inverse.obj (e.functor.obj Y)) :
f ≫ e.unit_inv.app Y = f' ≫ e.unit_inv.app Y ↔ f = f' :=
by simp only [cancel_mono]
@[simp] lemma cancel_counit_right {X Y : D}
(f f' : X ⟶ e.functor.obj (e.inverse.obj Y)) :
f ≫ e.counit.app Y = f' ≫ e.counit.app Y ↔ f = f' :=
by simp only [cancel_mono]
@[simp] lemma cancel_counit_inv_right {X Y : D}
(f f' : X ⟶ Y) :
f ≫ e.counit_inv.app Y = f' ≫ e.counit_inv.app Y ↔ f = f' :=
by simp only [cancel_mono]
@[simp] lemma cancel_unit_right_assoc {W X X' Y : C}
(f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) :
f ≫ g ≫ e.unit.app Y = f' ≫ g' ≫ e.unit.app Y ↔ f ≫ g = f' ≫ g' :=
by simp only [←category.assoc, cancel_mono]
@[simp] lemma cancel_counit_inv_right_assoc {W X X' Y : D}
(f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) :
f ≫ g ≫ e.counit_inv.app Y = f' ≫ g' ≫ e.counit_inv.app Y ↔ f ≫ g = f' ≫ g' :=
by simp only [←category.assoc, cancel_mono]
@[simp] lemma cancel_unit_right_assoc' {W X X' Y Y' Z : C}
(f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) :
f ≫ g ≫ h ≫ e.unit.app Z = f' ≫ g' ≫ h' ≫ e.unit.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' :=
by simp only [←category.assoc, cancel_mono]
@[simp] lemma cancel_counit_inv_right_assoc' {W X X' Y Y' Z : D}
(f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) :
f ≫ g ≫ h ≫ e.counit_inv.app Z = f' ≫ g' ≫ h' ≫ e.counit_inv.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' :=
by simp only [←category.assoc, cancel_mono]
end cancellation_lemmas
section
-- There's of course a monoid structure on `C ≌ C`,
-- but let's not encourage using it.
-- The power structure is nevertheless useful.
/-- Natural number powers of an auto-equivalence. Use `(^)` instead. -/
def pow_nat (e : C ≌ C) : ℕ → (C ≌ C)
| 0 := equivalence.refl
| 1 := e
| (n+2) := e.trans (pow_nat (n+1))
/-- Powers of an auto-equivalence. Use `(^)` instead. -/
def pow (e : C ≌ C) : ℤ → (C ≌ C)
| (int.of_nat n) := e.pow_nat n
| (int.neg_succ_of_nat n) := e.symm.pow_nat (n+1)
instance : has_pow (C ≌ C) ℤ := ⟨pow⟩
@[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl
@[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl
@[simp] lemma pow_neg_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl
-- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`.
-- At this point, we haven't even defined the category of equivalences.
end
end equivalence
/-- A functor that is part of a (half) adjoint equivalence -/
class is_equivalence (F : C ⥤ D) :=
mk' ::
(inverse : D ⥤ C)
(unit_iso : 𝟭 C ≅ F ⋙ inverse)
(counit_iso : inverse ⋙ F ≅ 𝟭 D)
(functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫
counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously)
restate_axiom is_equivalence.functor_unit_iso_comp'
namespace is_equivalence
instance of_equivalence (F : C ≌ D) : is_equivalence F.functor :=
{ ..F }
instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse :=
is_equivalence.of_equivalence F.symm
open equivalence
/-- To see that a functor is an equivalence, it suffices to provide an inverse functor `G` such that
`F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors. -/
protected definition mk {F : C ⥤ D} (G : D ⥤ C)
(η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F :=
⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩
end is_equivalence
namespace functor
/-- Interpret a functor that is an equivalence as an equivalence. -/
def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D :=
⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso,
is_equivalence.functor_unit_iso_comp⟩
instance is_equivalence_refl : is_equivalence (𝟭 C) :=
is_equivalence.of_equivalence equivalence.refl
/-- The inverse functor of a functor that is an equivalence. -/
def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C :=
is_equivalence.inverse F
instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv :=
is_equivalence.of_equivalence F.as_equivalence.symm
@[simp] lemma as_equivalence_functor (F : C ⥤ D) [is_equivalence F] :
F.as_equivalence.functor = F := rfl
@[simp] lemma as_equivalence_inverse (F : C ⥤ D) [is_equivalence F] :
F.as_equivalence.inverse = inv F := rfl
@[simp] lemma inv_inv (F : C ⥤ D) [is_equivalence F] :
inv (inv F) = F := rfl
variables {E : Type u₃} [category.{v₃} E]
instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] :
is_equivalence (F ⋙ G) :=
is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G))
end functor
namespace equivalence
@[simp]
lemma functor_inv (E : C ≌ D) : E.functor.inv = E.inverse := rfl
@[simp]
lemma inverse_inv (E : C ≌ D) : E.inverse.inv = E.functor := rfl
@[simp]
lemma functor_as_equivalence (E : C ≌ D) : E.functor.as_equivalence = E :=
by { cases E, congr, }
@[simp]
lemma inverse_as_equivalence (E : C ≌ D) : E.inverse.as_equivalence = E.symm :=
by { cases E, congr, }
end equivalence
namespace is_equivalence
@[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) :
F.map (F.inv.map f) = F.as_equivalence.counit.app X ≫ f ≫ F.as_equivalence.counit_inv.app Y :=
begin
erw [nat_iso.naturality_2],
refl
end
@[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) :
F.inv.map (F.map f) = F.as_equivalence.unit_inv.app X ≫ f ≫ F.as_equivalence.unit.app Y :=
begin
erw [nat_iso.naturality_1],
refl
end
end is_equivalence
namespace equivalence
/--
An equivalence is essentially surjective.
See https://stacks.math.columbia.edu/tag/02C3.
-/
lemma ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F :=
⟨λ Y, ⟨F.inv.obj Y, ⟨F.as_equivalence.counit_iso.app Y⟩⟩⟩
/--
An equivalence is faithful.
See https://stacks.math.columbia.edu/tag/02C3.
-/
@[priority 100] -- see Note [lower instance priority]
instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F :=
{ map_injective' := λ X Y f g w,
begin
have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w,
simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p
end }.
/--
An equivalence is full.
See https://stacks.math.columbia.edu/tag/02C3.
-/
@[priority 100] -- see Note [lower instance priority]
instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F :=
{ preimage := λ X Y f, F.as_equivalence.unit.app X ≫ F.inv.map f ≫ F.as_equivalence.unit_inv.app Y,
witness' := λ X Y f, F.inv.map_injective $
by simpa only [is_equivalence.inv_fun_map, assoc, iso.inv_hom_id_app_assoc, iso.inv_hom_id_app]
using comp_id _ }
@[simps] private noncomputable def equivalence_inverse (F : C ⥤ D) [full F] [faithful F]
[ess_surj F] : D ⥤ C :=
{ obj := λ X, F.obj_preimage X,
map := λ X Y f, F.preimage ((F.obj_obj_preimage_iso X).hom ≫ f ≫ (F.obj_obj_preimage_iso Y).inv),
map_id' := λ X, begin apply F.map_injective, tidy end,
map_comp' := λ X Y Z f g, by apply F.map_injective; simp }
/--
A functor which is full, faithful, and essentially surjective is an equivalence.
See https://stacks.math.columbia.edu/tag/02C3.
-/
noncomputable def of_fully_faithfully_ess_surj
(F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F :=
is_equivalence.mk (equivalence_inverse F)
(nat_iso.of_components
(λ X, (preimage_iso $ F.obj_obj_preimage_iso $ F.obj X).symm)
(λ X Y f, by { apply F.map_injective, obviously }))
(nat_iso.of_components F.obj_obj_preimage_iso (by tidy))
@[simp] lemma functor_map_inj_iff (e : C ≌ D) {X Y : C} (f g : X ⟶ Y) :
e.functor.map f = e.functor.map g ↔ f = g :=
⟨λ h, e.functor.map_injective h, λ h, h ▸ rfl⟩
@[simp] lemma inverse_map_inj_iff (e : C ≌ D) {X Y : D} (f g : X ⟶ Y) :
e.inverse.map f = e.inverse.map g ↔ f = g :=
functor_map_inj_iff e.symm f g
instance ess_surj_induced_functor {C' : Type*} (e : C' ≃ D) : ess_surj (induced_functor e) :=
{ mem_ess_image := λ Y, ⟨e.symm Y, by simp⟩, }
noncomputable
instance induced_functor_of_equiv {C' : Type*} (e : C' ≃ D) : is_equivalence (induced_functor e) :=
equivalence.of_fully_faithfully_ess_surj _
noncomputable
instance fully_faithful_to_ess_image (F : C ⥤ D) [full F] [faithful F] :
is_equivalence F.to_ess_image :=
of_fully_faithfully_ess_surj F.to_ess_image
end equivalence
end category_theory
|
73af9c1cd77b7750b375869ac27b0e757840c87c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/linear_algebra/nonsingular_inverse_auto.lean | f6dc81d2b34403d51f8fe9c0fa2ea96989c652ff | [] | 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 | 13,954 | lean | /-
Copyright (c) 2019 Tim Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Tim Baanen.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.associated
import Mathlib.linear_algebra.determinant
import Mathlib.tactic.linarith.default
import Mathlib.tactic.ring_exp
import Mathlib.PostPort
universes u v u_1
namespace Mathlib
/-!
# Nonsingular inverses
In this file, we define an inverse for square matrices of invertible
determinant. For matrices that are not square or not of full rank, there is a
more general notion of pseudoinverses which we do not consider here.
The definition of inverse used in this file is the adjugate divided by the determinant.
The adjugate is calculated with Cramer's rule, which we introduce first.
The vectors returned by Cramer's rule are given by the linear map `cramer`,
which sends a matrix `A` and vector `b` to the vector consisting of the
determinant of replacing the `i`th column of `A` with `b` at index `i`
(written as `(A.update_column i b).det`).
Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`.
The entries of the adjugate are the determinants of each minor of `A`.
Instead of defining a minor to be `A` with row `i` and column `j` deleted, we
replace the `i`th row of `A` with the `j`th basis vector; this has the same
determinant as the minor but more importantly equals Cramer's rule applied
to `A` and the `j`th basis vector, simplifying the subsequent proofs.
We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing
the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will
result in a multiplicative inverse to `A`.
## References
* https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix
## Tags
matrix inverse, cramer, cramer's rule, adjugate
-/
namespace matrix
/-!
### `cramer` section
Introduce the linear map `cramer` with values defined by `cramer_map`.
After defining `cramer_map` and showing it is linear,
we will restrict our proofs to using `cramer`.
-/
/--
`cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`,
and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`.
If `A ⬝ x = b` has a unique solution in `x`, `cramer_map A` sends the vector `b` to `A.det • x`.
Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful.
-/
def cramer_map {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (b : n → α) (i : n) : α :=
det (update_column A i b)
theorem cramer_map_is_linear {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (i : n) : is_linear_map α fun (b : n → α) => cramer_map A b i :=
sorry
theorem cramer_is_linear {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : is_linear_map α (cramer_map A) :=
is_linear_map.mk
(fun (x y : n → α) =>
funext fun (i : n) => is_linear_map.map_add (cramer_map_is_linear A i) x y)
fun (c : α) (x : n → α) =>
funext fun (i : n) => is_linear_map.map_smul (cramer_map_is_linear A i) c x
/--
`cramer A b i` is the determinant of the matrix `A` with column `i` replaced with `b`,
and thus `cramer A b` is the vector output by Cramer's rule on `A` and `b`.
If `A ⬝ x = b` has a unique solution in `x`, `cramer A` sends the vector `b` to `A.det • x`.
Otherwise, the outcome of `cramer` is well-defined but not necessarily useful.
-/
def cramer {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α] (A : matrix n n α) :
linear_map α (n → α) (n → α) :=
is_linear_map.mk' (cramer_map A) sorry
theorem cramer_apply {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (b : n → α) (i : n) : coe_fn (cramer A) b i = det (update_column A i b) :=
rfl
theorem cramer_transpose_row_self {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (i : n) :
coe_fn (cramer (transpose A)) (A i) = fun (j : n) => ite (i = j) (det A) 0 :=
sorry
/-- Use linearity of `cramer` to take it out of a summation. -/
theorem sum_cramer {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) {β : Type u_1} (s : finset β) (f : β → n → α) :
(finset.sum s fun (x : β) => coe_fn (cramer A) (f x)) =
coe_fn (cramer A) (finset.sum s fun (x : β) => f x) :=
Eq.symm (linear_map.map_sum (cramer A))
/-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/
theorem sum_cramer_apply {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) {β : Type u_1} (s : finset β) (f : n → β → α) (i : n) :
(finset.sum s fun (x : β) => coe_fn (cramer A) (fun (j : n) => f j x) i) =
coe_fn (cramer A) (fun (j : n) => finset.sum s fun (x : β) => f j x) i :=
sorry
/-!
### `adjugate` section
Define the `adjugate` matrix and a few equations.
These will hold for any matrix over a commutative ring,
while the `inv` section is specifically for invertible matrices.
-/
/-- The adjugate matrix is the transpose of the cofactor matrix.
Typically, the cofactor matrix is defined by taking the determinant of minors,
i.e. the matrix with a row and column removed.
However, the proof of `mul_adjugate` becomes a lot easier if we define the
minor as replacing a column with a basis vector, since it allows us to use
facts about the `cramer` map.
-/
def adjugate {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : matrix n n α :=
fun (i : n) => coe_fn (cramer (transpose A)) fun (j : n) => ite (i = j) 1 0
theorem adjugate_def {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) :
adjugate A = fun (i : n) => coe_fn (cramer (transpose A)) fun (j : n) => ite (i = j) 1 0 :=
rfl
theorem adjugate_apply {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (i : n) (j : n) :
adjugate A i j = det (update_row A j fun (j : n) => ite (i = j) 1 0) :=
sorry
theorem adjugate_transpose {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : transpose (adjugate A) = adjugate (transpose A) :=
sorry
/-- Since the map `b ↦ cramer A b` is linear in `b`, it must be multiplication by some matrix. This
matrix is `A.adjugate`. -/
theorem cramer_eq_adjugate_mul_vec {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (b : n → α) : coe_fn (cramer A) b = mul_vec (adjugate A) b :=
sorry
theorem mul_adjugate_apply {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (i : n) (j : n) (k : n) :
A i k * adjugate A k j =
coe_fn (cramer (transpose A)) (fun (j : n) => ite (k = j) (A i k) 0) j :=
sorry
theorem mul_adjugate {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : matrix.mul A (adjugate A) = det A • 1 :=
sorry
theorem adjugate_mul {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : matrix.mul (adjugate A) A = det A • 1 :=
sorry
/-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`,
used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`.
The formula for the determinant of the adjugate of an `n` by `n` matrix `A`
is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases.
This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels
on the left of the equation `A.det * b = A.det ^ n`.
-/
theorem det_adjugate_of_cancel {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
{A : matrix n n α}
(h : ∀ (b : α), det A * b = det A ^ fintype.card n → b = det A ^ (fintype.card n - 1)) :
det (adjugate A) = det A ^ (fintype.card n - 1) :=
sorry
theorem adjugate_eq_one_of_card_eq_one {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 :=
sorry
@[simp] theorem adjugate_zero {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(h : 1 < fintype.card n) : adjugate 0 = 0 :=
sorry
theorem det_adjugate_eq_one {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
{A : matrix n n α} (h : det A = 1) : det (adjugate A) = 1 :=
sorry
/-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse.
The formula for the determinant of the adjugate of an `n` by `n` matrix `A`
is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases.
This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse.
-/
theorem det_adjugate_of_is_unit {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
{A : matrix n n α} (h : is_unit (det A)) : det (adjugate A) = det A ^ (fintype.card n - 1) :=
sorry
/-!
### `inv` section
Defines the matrix `nonsing_inv A` and proves it is the inverse matrix
of a square matrix `A` as long as `det A` has a multiplicative inverse.
-/
theorem is_unit_det_transpose {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : is_unit (det (transpose A)) :=
eq.mpr (id (Eq._oldrec (Eq.refl (is_unit (det (transpose A)))) (det_transpose A))) h
/-- The inverse of a square matrix, when it is invertible (and zero otherwise).-/
def nonsing_inv {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : matrix n n α :=
dite (is_unit (det A)) (fun (h : is_unit (det A)) => ↑(is_unit.unit h⁻¹) • adjugate A)
fun (h : ¬is_unit (det A)) => 0
protected instance has_inv {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α] :
has_inv (matrix n n α) :=
has_inv.mk nonsing_inv
theorem nonsing_inv_apply {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : A⁻¹ = ↑(is_unit.unit h⁻¹) • adjugate A :=
sorry
theorem transpose_nonsing_inv {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : transpose (A⁻¹) = (transpose A⁻¹) :=
sorry
/-- The `nonsing_inv` of `A` is a right inverse. -/
@[simp] theorem mul_nonsing_inv {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : matrix.mul A (A⁻¹) = 1 :=
sorry
/-- The `nonsing_inv` of `A` is a left inverse. -/
@[simp] theorem nonsing_inv_mul {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : matrix.mul (A⁻¹) A = 1 :=
sorry
@[simp] theorem nonsing_inv_det {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : det (A⁻¹) * det A = 1 :=
eq.mpr (id (Eq._oldrec (Eq.refl (det (A⁻¹) * det A = 1)) (Eq.symm (det_mul (A⁻¹) A))))
(eq.mpr (id (Eq._oldrec (Eq.refl (det (matrix.mul (A⁻¹) A) = 1)) (nonsing_inv_mul A h)))
(eq.mpr (id (Eq._oldrec (Eq.refl (det 1 = 1)) det_one)) (Eq.refl 1)))
theorem is_unit_nonsing_inv_det {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : is_unit (det (A⁻¹)) :=
is_unit_of_mul_eq_one (det (A⁻¹)) (det A) (nonsing_inv_det A h)
@[simp] theorem nonsing_inv_nonsing_inv {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (h : is_unit (det A)) : A⁻¹⁻¹ = A :=
sorry
/-- A matrix whose determinant is a unit is itself a unit. -/
def nonsing_inv_unit {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (h : is_unit (det A)) : units (matrix n n α) :=
units.mk A (A⁻¹) sorry sorry
theorem is_unit_iff_is_unit_det {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) : is_unit A ↔ is_unit (det A) :=
sorry
theorem is_unit_det_of_left_inverse {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (B : matrix n n α) (h : matrix.mul B A = 1) :
is_unit (det A) :=
sorry
theorem is_unit_det_of_right_inverse {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (B : matrix n n α) (h : matrix.mul A B = 1) :
is_unit (det A) :=
sorry
theorem nonsing_inv_left_right {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (B : matrix n n α) (h : matrix.mul A B = 1) : matrix.mul B A = 1 :=
sorry
theorem nonsing_inv_right_left {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (B : matrix n n α) (h : matrix.mul B A = 1) : matrix.mul A B = 1 :=
nonsing_inv_left_right B A h
/- One form of Cramer's rule. -/
@[simp] theorem det_smul_inv_mul_vec_eq_cramer {n : Type u} [DecidableEq n] [fintype n] {α : Type v}
[comm_ring α] (A : matrix n n α) (b : n → α) (h : is_unit (det A)) :
det A • mul_vec (A⁻¹) b = coe_fn (cramer A) b :=
sorry
/- A stronger form of Cramer's rule that allows us to solve some instances of `A ⬝ x = b` even if
the determinant is not a unit. A sufficient (but still not necessary) condition is that `A.det`
divides `b`. -/
@[simp] theorem mul_vec_cramer {n : Type u} [DecidableEq n] [fintype n] {α : Type v} [comm_ring α]
(A : matrix n n α) (b : n → α) : mul_vec A (coe_fn (cramer A) b) = det A • b :=
sorry
end Mathlib |
611471de334f04bc663f2723b6262cc585dcdfa8 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/quandle.lean | ab30decf3f9ae8ff99fa2b5789e791f21f05291f | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,828 | lean | /-
Copyright (c) 2020 Kyle Miller. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller
-/
import data.zmod.basic
import data.equiv.mul_add
import tactic.group
/-!
# Racks and Quandles
This file defines racks and quandles, algebraic structures for sets
that bijectively act on themselves with a self-distributivity
property. If `R` is a rack and `act : R → (R ≃ R)` is the self-action,
then the self-distributivity is, equivalently, that
```
act (act x y) = act x * act y * (act x)⁻¹
```
where multiplication is composition in `R ≃ R` as a group.
Quandles are racks such that `act x x = x` for all `x`.
One example of a quandle (not yet in mathlib) is the action of a Lie
algebra on itself, defined by `act x y = Ad (exp x) y`.
Quandles and racks were independently developed by multiple
mathematicians. David Joyce introduced quandles in his thesis
[Joyce1982] to define an algebraic invariant of knot and link
complements that is analogous to the fundamental group of the
exterior, and he showed that the quandle associated to an oriented
knot is invariant up to orientation-reversed mirror image. Racks were
used by Fenn and Rourke for framed codimension-2 knots and
links.[FennRourke1992]
The name "rack" came from wordplay by Conway and Wraith for the "wrack
and ruin" of forgetting everything but the conjugation operation for a
group.
## Main definitions
* `shelf` is a type with a self-distributive action
* `rack` is a shelf whose action for each element is invertible
* `quandle` is a rack whose action for an element fixes that element
* `quandle.conj` defines a quandle of a group acting on itself by conjugation.
* `shelf_hom` is homomorphisms of shelves, racks, and quandles.
* `rack.envel_group` gives the universal group the rack maps to as a conjugation quandle.
* `rack.opp` gives the rack with the action replaced by its inverse.
## Main statements
* `rack.envel_group` is left adjoint to `quandle.conj` (`to_envel_group.map`).
The universality statements are `to_envel_group.univ` and `to_envel_group.univ_uniq`.
## Notation
The following notation is localized in `quandles`:
* `x ◃ y` is `shelf.act x y`
* `x ◃⁻¹ y` is `rack.inv_act x y`
* `S →◃ S'` is `shelf_hom S S'`
Use `open_locale quandles` to use these.
## Todo
* If `g` is the Lie algebra of a Lie group `G`, then `(x ◃ y) = Ad (exp x) x` forms a quandle.
* If `X` is a symmetric space, then each point has a corresponding involution that acts on `X`,
forming a quandle.
* Alexander quandle with `a ◃ b = t * b + (1 - t) * b`, with `a` and `b` elements
of a module over `Z[t,t⁻¹]`.
* If `G` is a group, `H` a subgroup, and `z` in `H`, then there is a quandle `(G/H;z)` defined by
`yH ◃ xH = yzy⁻¹xH`. Every homogeneous quandle (i.e., a quandle `Q` whose automorphism group acts
transitively on `Q` as a set) is isomorphic to such a quandle.
There is a generalization to this arbitrary quandles in [Joyce's paper (Theorem 7.2)][Joyce1982].
## Tags
rack, quandle
-/
open opposite
universes u v
/--
A *shelf* is a structure with a self-distributive binary operation.
The binary operation is regarded as a left action of the type on itself.
-/
class shelf (α : Type u) :=
(act : α → α → α)
(self_distrib : ∀ {x y z : α}, act x (act y z) = act (act x y) (act x z))
/--
The type of homomorphisms between shelves.
This is also the notion of rack and quandle homomorphisms.
-/
@[ext]
structure shelf_hom (S₁ : Type*) (S₂ : Type*) [shelf S₁] [shelf S₂] :=
(to_fun : S₁ → S₂)
(map_act' : ∀ {x y : S₁}, to_fun (shelf.act x y) = shelf.act (to_fun x) (to_fun y))
/--
A *rack* is an automorphic set (a set with an action on itself by
bijections) that is self-distributive. It is a shelf such that each
element's action is invertible.
The notations `x ◃ y` and `x ◃⁻¹ y` denote the action and the
inverse action, respectively, and they are right associative.
-/
class rack (α : Type u) extends shelf α :=
(inv_act : α → α → α)
(left_inv : ∀ x, function.left_inverse (inv_act x) (act x))
(right_inv : ∀ x, function.right_inverse (inv_act x) (act x))
localized "infixr ` ◃ `:65 := shelf.act" in quandles
localized "infixr ` ◃⁻¹ `:65 := rack.inv_act" in quandles
localized "infixr ` →◃ `:25 := shelf_hom" in quandles
open_locale quandles
namespace rack
variables {R : Type*} [rack R]
lemma self_distrib {x y z : R} : x ◃ (y ◃ z) = (x ◃ y) ◃ (x ◃ z) :=
shelf.self_distrib
/--
A rack acts on itself by equivalences.
-/
def act (x : R) : R ≃ R :=
{ to_fun := shelf.act x,
inv_fun := inv_act x,
left_inv := left_inv x,
right_inv := right_inv x }
@[simp] lemma act_apply (x y : R) : act x y = x ◃ y := rfl
@[simp] lemma act_symm_apply (x y : R) : (act x).symm y = x ◃⁻¹ y := rfl
@[simp] lemma inv_act_apply (x y : R) : (act x)⁻¹ y = x ◃⁻¹ y := rfl
@[simp] lemma inv_act_act_eq (x y : R) : x ◃⁻¹ x ◃ y = y := left_inv x y
@[simp] lemma act_inv_act_eq (x y : R) : x ◃ x ◃⁻¹ y = y := right_inv x y
lemma left_cancel (x : R) {y y' : R} : x ◃ y = x ◃ y' ↔ y = y' :=
by { split, apply (act x).injective, rintro rfl, refl }
lemma left_cancel_inv (x : R) {y y' : R} : x ◃⁻¹ y = x ◃⁻¹ y' ↔ y = y' :=
by { split, apply (act x).symm.injective, rintro rfl, refl }
lemma self_distrib_inv {x y z : R} : x ◃⁻¹ y ◃⁻¹ z = (x ◃⁻¹ y) ◃⁻¹ (x ◃⁻¹ z) :=
begin
rw [←left_cancel (x ◃⁻¹ y), right_inv, ←left_cancel x, right_inv, self_distrib],
repeat {rw right_inv },
end
/--
The *adjoint action* of a rack on itself is `op'`, and the adjoint
action of `x ◃ y` is the conjugate of the action of `y` by the action
of `x`. It is another way to understand the self-distributivity axiom.
This is used in the natural rack homomorphism `to_conj` from `R` to
`conj (R ≃ R)` defined by `op'`.
-/
lemma ad_conj {R : Type*} [rack R] (x y : R) :
act (x ◃ y) = act x * act y * (act x)⁻¹ :=
begin
apply @mul_right_cancel _ _ _ (act x), ext z,
simp only [inv_mul_cancel_right],
apply self_distrib.symm,
end
/--
The opposite rack, swapping the roles of `◃` and `◃⁻¹`.
-/
instance opposite_rack : rack Rᵒᵖ :=
{ act := λ x y, op (inv_act (unop x) (unop y)),
self_distrib := λ (x y z : Rᵒᵖ), begin
induction x using opposite.rec, induction y using opposite.rec, induction z using opposite.rec,
simp only [unop_op, op_inj_iff],
exact self_distrib_inv,
end,
inv_act := λ x y, op (shelf.act (unop x) (unop y)),
left_inv := λ x y, begin
induction x using opposite.rec, induction y using opposite.rec, simp,
end,
right_inv := λ x y, begin
induction x using opposite.rec, induction y using opposite.rec, simp,
end }
@[simp] lemma op_act_op_eq {x y : R} : (op x) ◃ (op y) = op (x ◃⁻¹ y) := rfl
@[simp] lemma op_inv_act_op_eq {x y : R} : (op x) ◃⁻¹ (op y) = op (x ◃ y) := rfl
@[simp]
lemma self_act_act_eq {x y : R} : (x ◃ x) ◃ y = x ◃ y :=
by { rw [←right_inv x y, ←self_distrib] }
@[simp]
lemma self_inv_act_inv_act_eq {x y : R} : (x ◃⁻¹ x) ◃⁻¹ y = x ◃⁻¹ y :=
by { have h := @self_act_act_eq _ _ (op x) (op y), simpa using h }
@[simp]
lemma self_act_inv_act_eq {x y : R} : (x ◃ x) ◃⁻¹ y = x ◃⁻¹ y :=
by { rw ←left_cancel (x ◃ x), rw right_inv, rw self_act_act_eq, rw right_inv }
@[simp]
lemma self_inv_act_act_eq {x y : R} : (x ◃⁻¹ x) ◃ y = x ◃ y :=
by { have h := @self_act_inv_act_eq _ _ (op x) (op y), simpa using h }
lemma self_act_eq_iff_eq {x y : R} : x ◃ x = y ◃ y ↔ x = y :=
begin
split, swap, rintro rfl, refl,
intro h,
transitivity (x ◃ x) ◃⁻¹ (x ◃ x),
rw [←left_cancel (x ◃ x), right_inv, self_act_act_eq],
rw [h, ←left_cancel (y ◃ y), right_inv, self_act_act_eq],
end
lemma self_inv_act_eq_iff_eq {x y : R} : x ◃⁻¹ x = y ◃⁻¹ y ↔ x = y :=
by { have h := @self_act_eq_iff_eq _ _ (op x) (op y), simpa using h }
/--
The map `x ↦ x ◃ x` is a bijection. (This has applications for the
regular isotopy version of the Reidemeister I move for knot diagrams.)
-/
def self_apply_equiv (R : Type*) [rack R] : R ≃ R :=
{ to_fun := λ x, x ◃ x,
inv_fun := λ x, x ◃⁻¹ x,
left_inv := λ x, by simp,
right_inv := λ x, by simp }
/--
An involutory rack is one for which `rack.op R x` is an involution for every x.
-/
def is_involutory (R : Type*) [rack R] : Prop := ∀ x : R, function.involutive (shelf.act x)
lemma involutory_inv_act_eq_act {R : Type*} [rack R] (h : is_involutory R) (x y : R) :
x ◃⁻¹ y = x ◃ y :=
begin
rw [←left_cancel x, right_inv],
exact ((h x).left_inverse y).symm,
end
/--
An abelian rack is one for which the mediality axiom holds.
-/
def is_abelian (R : Type*) [rack R] : Prop :=
∀ (x y z w : R), (x ◃ y) ◃ (z ◃ w) = (x ◃ z) ◃ (y ◃ w)
/--
Associative racks are uninteresting.
-/
lemma assoc_iff_id {R : Type*} [rack R] {x y z : R} :
x ◃ y ◃ z = (x ◃ y) ◃ z ↔ x ◃ z = z :=
by { rw self_distrib, rw left_cancel }
end rack
namespace shelf_hom
variables {S₁ : Type*} {S₂ : Type*} {S₃ : Type*} [shelf S₁] [shelf S₂] [shelf S₃]
instance : has_coe_to_fun (S₁ →◃ S₂) (λ _, S₁ → S₂) := ⟨shelf_hom.to_fun⟩
@[simp] lemma to_fun_eq_coe (f : S₁ →◃ S₂) : f.to_fun = f := rfl
@[simp] lemma map_act (f : S₁ →◃ S₂) {x y : S₁} : f (x ◃ y) = f x ◃ f y := map_act' f
/-- The identity homomorphism -/
def id (S : Type*) [shelf S] : S →◃ S :=
{ to_fun := id,
map_act' := by simp }
instance inhabited (S : Type*) [shelf S] : inhabited (S →◃ S) :=
⟨id S⟩
/-- The composition of shelf homomorphisms -/
def comp (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) : S₁ →◃ S₃ :=
{ to_fun := g.to_fun ∘ f.to_fun,
map_act' := by simp }
@[simp]
lemma comp_apply (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) (x : S₁) :
(g.comp f) x = g (f x) := rfl
end shelf_hom
/--
A quandle is a rack such that each automorphism fixes its corresponding element.
-/
class quandle (α : Type*) extends rack α :=
(fix : ∀ {x : α}, act x x = x)
namespace quandle
open rack
variables {Q : Type*} [quandle Q]
attribute [simp] fix
@[simp]
lemma fix_inv {x : Q} : x ◃⁻¹ x = x :=
by { rw ←left_cancel x, simp }
instance opposite_quandle : quandle Qᵒᵖ :=
{ fix := λ x, by { induction x using opposite.rec, simp } }
/--
The conjugation quandle of a group. Each element of the group acts by
the corresponding inner automorphism.
-/
@[nolint has_inhabited_instance]
def conj (G : Type*) := G
instance conj.quandle (G : Type*) [group G] : quandle (conj G) :=
{ act := (λ x, @mul_aut.conj G _ x),
self_distrib := λ x y z, begin
dsimp only [mul_equiv.coe_to_equiv, mul_aut.conj_apply, conj],
group,
end,
inv_act := (λ x, (@mul_aut.conj G _ x).symm),
left_inv := λ x y, by { dsimp [act, conj], group },
right_inv := λ x y, by { dsimp [act, conj], group },
fix := λ x, by simp }
@[simp]
lemma conj_act_eq_conj {G : Type*} [group G] (x y : conj G) :
x ◃ y = ((x : G) * (y : G) * (x : G)⁻¹ : G) := rfl
lemma conj_swap {G : Type*} [group G] (x y : conj G) :
x ◃ y = y ↔ y ◃ x = x :=
begin
dsimp, split,
repeat { intro h, conv_rhs { rw eq_mul_inv_of_mul_eq (eq_mul_inv_of_mul_eq h) }, simp, },
end
/--
`conj` is functorial
-/
def conj.map {G : Type*} {H : Type*} [group G] [group H] (f : G →* H) : conj G →◃ conj H :=
{ to_fun := f,
map_act' := by simp }
instance {G : Type*} {H : Type*} [group G] [group H] : has_lift (G →* H) (conj G →◃ conj H) :=
{ lift := conj.map }
/--
The dihedral quandle. This is the conjugation quandle of the dihedral group restrict to flips.
Used for Fox n-colorings of knots.
-/
@[nolint has_inhabited_instance]
def dihedral (n : ℕ) := zmod n
/--
The operation for the dihedral quandle. It does not need to be an equivalence
because it is an involution (see `dihedral_act.inv`).
-/
def dihedral_act (n : ℕ) (a : zmod n) : zmod n → zmod n :=
λ b, 2 * a - b
lemma dihedral_act.inv (n : ℕ) (a : zmod n) : function.involutive (dihedral_act n a) :=
by { intro b, dsimp [dihedral_act], ring }
instance (n : ℕ) : quandle (dihedral n) :=
{ act := dihedral_act n,
self_distrib := λ x y z, begin
dsimp [function.involutive.to_equiv, dihedral_act], ring,
end,
inv_act := dihedral_act n,
left_inv := λ x, (dihedral_act.inv n x).left_inverse,
right_inv := λ x, (dihedral_act.inv n x).right_inverse,
fix := λ x, begin
dsimp [function.involutive.to_equiv, dihedral_act], ring,
end }
end quandle
namespace rack
/--
This is the natural rack homomorphism to the conjugation quandle of the group `R ≃ R`
that acts on the rack.
-/
def to_conj (R : Type*) [rack R] : R →◃ quandle.conj (R ≃ R) :=
{ to_fun := act,
map_act' := ad_conj }
section envel_group
/-!
### Universal enveloping group of a rack
The universal enveloping group `envel_group R` of a rack `R` is the
universal group such that every rack homomorphism `R →◃ conj G` is
induced by a unique group homomorphism `envel_group R →* G`.
For quandles, Joyce called this group `AdConj R`.
The `envel_group` functor is left adjoint to the `conj` forgetful
functor, and the way we construct the enveloping group is via a
technique that should work for left adjoints of forgetful functors in
general. It involves thinking a little about 2-categories, but the
payoff is that the map `envel_group R →* G` has a nice description.
Let's think of a group as being a one-object category. The first step
is to define `pre_envel_group`, which gives formal expressions for all
the 1-morphisms and includes the unit element, elements of `R`,
multiplication, and inverses. To introduce relations, the second step
is to define `pre_envel_group_rel'`, which gives formal expressions
for all 2-morphisms between the 1-morphisms. The 2-morphisms include
associativity, multiplication by the unit, multiplication by inverses,
compatibility with multiplication and inverses (`congr_mul` and
`congr_inv`), the axioms for an equivalence relation, and,
importantly, the relationship between conjugation and the rack action
(see `rack.ad_conj`).
None of this forms a 2-category yet, for example due to lack of
associativity of `trans`. The `pre_envel_group_rel` relation is a
`Prop`-valued version of `pre_envel_group_rel'`, and making it
`Prop`-valued essentially introduces enough 3-isomorphisms so that
every pair of compatible 2-morphisms is isomorphic. Now, while
composition in `pre_envel_group` does not strictly satisfy the category
axioms, `pre_envel_group` and `pre_envel_group_rel'` do form a weak
2-category.
Since we just want a 1-category, the last step is to quotient
`pre_envel_group` by `pre_envel_group_rel'`, and the result is the
group `envel_group`.
For a homomorphism `f : R →◃ conj G`, how does
`envel_group.map f : envel_group R →* G` work? Let's think of `G` as
being a 2-category with one object, a 1-morphism per element of `G`,
and a single 2-morphism called `eq.refl` for each 1-morphism. We
define the map using a "higher `quotient.lift`" -- not only do we
evaluate elements of `pre_envel_group` as expressions in `G` (this is
`to_envel_group.map_aux`), but we evaluate elements of
`pre_envel_group'` as expressions of 2-morphisms of `G` (this is
`to_envel_group.map_aux.well_def`). That is to say,
`to_envel_group.map_aux.well_def` recursively evaluates formal
expressions of 2-morphisms as equality proofs in `G`. Now that all
morphisms are accounted for, the map descends to a homomorphism
`envel_group R →* G`.
Note: `Type`-valued relations are not common. The fact it is
`Type`-valued is what makes `to_envel_group.map_aux.well_def` have
well-founded recursion.
-/
/--
Free generators of the enveloping group.
-/
inductive pre_envel_group (R : Type u) : Type u
| unit : pre_envel_group
| incl (x : R) : pre_envel_group
| mul (a b : pre_envel_group) : pre_envel_group
| inv (a : pre_envel_group) : pre_envel_group
instance pre_envel_group.inhabited (R : Type u) : inhabited (pre_envel_group R) :=
⟨pre_envel_group.unit⟩
open pre_envel_group
/--
Relations for the enveloping group. This is a type-valued relation because
`to_envel_group.map_aux.well_def` inducts on it to show `to_envel_group.map`
is well-defined. The relation `pre_envel_group_rel` is the `Prop`-valued version,
which is used to define `envel_group` itself.
-/
inductive pre_envel_group_rel' (R : Type u) [rack R] :
pre_envel_group R → pre_envel_group R → Type u
| refl {a : pre_envel_group R} : pre_envel_group_rel' a a
| symm {a b : pre_envel_group R} (hab : pre_envel_group_rel' a b) : pre_envel_group_rel' b a
| trans {a b c : pre_envel_group R}
(hab : pre_envel_group_rel' a b) (hbc : pre_envel_group_rel' b c) : pre_envel_group_rel' a c
| congr_mul {a b a' b' : pre_envel_group R}
(ha : pre_envel_group_rel' a a') (hb : pre_envel_group_rel' b b') :
pre_envel_group_rel' (mul a b) (mul a' b')
| congr_inv {a a' : pre_envel_group R} (ha : pre_envel_group_rel' a a') :
pre_envel_group_rel' (inv a) (inv a')
| assoc (a b c : pre_envel_group R) : pre_envel_group_rel' (mul (mul a b) c) (mul a (mul b c))
| one_mul (a : pre_envel_group R) : pre_envel_group_rel' (mul unit a) a
| mul_one (a : pre_envel_group R) : pre_envel_group_rel' (mul a unit) a
| mul_left_inv (a : pre_envel_group R) : pre_envel_group_rel' (mul (inv a) a) unit
| act_incl (x y : R) :
pre_envel_group_rel' (mul (mul (incl x) (incl y)) (inv (incl x))) (incl (x ◃ y))
instance pre_envel_group_rel'.inhabited (R : Type u) [rack R] :
inhabited (pre_envel_group_rel' R unit unit) :=
⟨pre_envel_group_rel'.refl⟩
/--
The `pre_envel_group_rel` relation as a `Prop`. Used as the relation for `pre_envel_group.setoid`.
-/
inductive pre_envel_group_rel (R : Type u) [rack R] : pre_envel_group R → pre_envel_group R → Prop
| rel {a b : pre_envel_group R} (r : pre_envel_group_rel' R a b) : pre_envel_group_rel a b
/--
A quick way to convert a `pre_envel_group_rel'` to a `pre_envel_group_rel`.
-/
lemma pre_envel_group_rel'.rel {R : Type u} [rack R] {a b : pre_envel_group R} :
pre_envel_group_rel' R a b → pre_envel_group_rel R a b :=
pre_envel_group_rel.rel
@[refl]
lemma pre_envel_group_rel.refl {R : Type u} [rack R] {a : pre_envel_group R} :
pre_envel_group_rel R a a :=
pre_envel_group_rel.rel pre_envel_group_rel'.refl
@[symm]
lemma pre_envel_group_rel.symm {R : Type u} [rack R] {a b : pre_envel_group R} :
pre_envel_group_rel R a b → pre_envel_group_rel R b a
| ⟨r⟩ := r.symm.rel
@[trans]
lemma pre_envel_group_rel.trans {R : Type u} [rack R] {a b c : pre_envel_group R} :
pre_envel_group_rel R a b → pre_envel_group_rel R b c → pre_envel_group_rel R a c
| ⟨rab⟩ ⟨rbc⟩ := (rab.trans rbc).rel
instance pre_envel_group.setoid (R : Type*) [rack R] : setoid (pre_envel_group R) :=
{ r := pre_envel_group_rel R,
iseqv := begin
split, apply pre_envel_group_rel.refl,
split, apply pre_envel_group_rel.symm,
apply pre_envel_group_rel.trans
end }
/--
The universal enveloping group for the rack R.
-/
def envel_group (R : Type*) [rack R] := quotient (pre_envel_group.setoid R)
-- Define the `group` instances in two steps so `inv` can be inferred correctly.
-- TODO: is there a non-invasive way of defining the instance directly?
instance (R : Type*) [rack R] : div_inv_monoid (envel_group R) :=
{ mul := λ a b, quotient.lift_on₂ a b
(λ a b, ⟦pre_envel_group.mul a b⟧)
(λ a b a' b' ⟨ha⟩ ⟨hb⟩,
quotient.sound (pre_envel_group_rel'.congr_mul ha hb).rel),
one := ⟦unit⟧,
inv := λ a, quotient.lift_on a
(λ a, ⟦pre_envel_group.inv a⟧)
(λ a a' ⟨ha⟩,
quotient.sound (pre_envel_group_rel'.congr_inv ha).rel),
mul_assoc := λ a b c,
quotient.induction_on₃ a b c (λ a b c, quotient.sound (pre_envel_group_rel'.assoc a b c).rel),
one_mul := λ a,
quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.one_mul a).rel),
mul_one := λ a,
quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.mul_one a).rel),}
instance (R : Type*) [rack R] : group (envel_group R) :=
{ mul_left_inv := λ a,
quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.mul_left_inv a).rel),
.. envel_group.div_inv_monoid _ }
instance envel_group.inhabited (R : Type*) [rack R] : inhabited (envel_group R) := ⟨1⟩
/--
The canonical homomorphism from a rack to its enveloping group.
Satisfies universal properties given by `to_envel_group.map` and `to_envel_group.univ`.
-/
def to_envel_group (R : Type*) [rack R] : R →◃ quandle.conj (envel_group R) :=
{ to_fun := λ x, ⟦incl x⟧,
map_act' := λ x y, quotient.sound (pre_envel_group_rel'.act_incl x y).symm.rel }
/--
The preliminary definition of the induced map from the enveloping group.
See `to_envel_group.map`.
-/
def to_envel_group.map_aux {R : Type*} [rack R] {G : Type*} [group G]
(f : R →◃ quandle.conj G) : pre_envel_group R → G
| unit := 1
| (incl x) := f x
| (mul a b) := to_envel_group.map_aux a * to_envel_group.map_aux b
| (inv a) := (to_envel_group.map_aux a)⁻¹
namespace to_envel_group.map_aux
open pre_envel_group_rel'
/--
Show that `to_envel_group.map_aux` sends equivalent expressions to equal terms.
-/
lemma well_def {R : Type*} [rack R] {G : Type*} [group G] (f : R →◃ quandle.conj G) :
Π {a b : pre_envel_group R}, pre_envel_group_rel' R a b →
to_envel_group.map_aux f a = to_envel_group.map_aux f b
| a b refl := rfl
| a b (symm h) := (well_def h).symm
| a b (trans hac hcb) := eq.trans (well_def hac) (well_def hcb)
| _ _ (congr_mul ha hb) := by { simp [to_envel_group.map_aux, well_def ha, well_def hb] }
| _ _ (congr_inv ha) := by { simp [to_envel_group.map_aux, well_def ha] }
| _ _ (assoc a b c) := by { apply mul_assoc }
| _ _ (one_mul a) := by { simp [to_envel_group.map_aux] }
| _ _ (mul_one a) := by { simp [to_envel_group.map_aux] }
| _ _ (mul_left_inv a) := by { simp [to_envel_group.map_aux] }
| _ _ (act_incl x y) := by { simp [to_envel_group.map_aux] }
end to_envel_group.map_aux
/--
Given a map from a rack to a group, lift it to being a map from the enveloping group.
More precisely, the `envel_group` functor is left adjoint to `quandle.conj`.
-/
def to_envel_group.map {R : Type*} [rack R] {G : Type*} [group G] :
(R →◃ quandle.conj G) ≃ (envel_group R →* G) :=
{ to_fun := λ f,
{ to_fun := λ x, quotient.lift_on x (to_envel_group.map_aux f)
(λ a b ⟨hab⟩, to_envel_group.map_aux.well_def f hab),
map_one' := begin
change quotient.lift_on ⟦unit⟧ (to_envel_group.map_aux f) _ = 1,
simp [to_envel_group.map_aux],
end,
map_mul' := λ x y, quotient.induction_on₂ x y (λ x y, begin
change quotient.lift_on ⟦mul x y⟧ (to_envel_group.map_aux f) _ = _,
simp [to_envel_group.map_aux],
end) },
inv_fun := λ F, (quandle.conj.map F).comp (to_envel_group R),
left_inv := λ f, by { ext, refl },
right_inv := λ F, monoid_hom.ext $ λ x, quotient.induction_on x $ λ x, begin
induction x,
{ exact F.map_one.symm, },
{ refl, },
{ have hm : ⟦x_a.mul x_b⟧ = @has_mul.mul (envel_group R) _ ⟦x_a⟧ ⟦x_b⟧ := rfl,
rw [hm, F.map_mul, monoid_hom.map_mul, ←x_ih_a, ←x_ih_b] },
{ have hm : ⟦x_a.inv⟧ = @has_inv.inv (envel_group R) _ ⟦x_a⟧ := rfl,
rw [hm, F.map_inv, monoid_hom.map_inv, x_ih], }
end, }
/--
Given a homomorphism from a rack to a group, it factors through the enveloping group.
-/
lemma to_envel_group.univ (R : Type*) [rack R] (G : Type*) [group G]
(f : R →◃ quandle.conj G) :
(quandle.conj.map (to_envel_group.map f)).comp (to_envel_group R) = f :=
to_envel_group.map.symm_apply_apply f
/--
The homomorphism `to_envel_group.map f` is the unique map that fits into the commutative
triangle in `to_envel_group.univ`.
-/
lemma to_envel_group.univ_uniq (R : Type*) [rack R] (G : Type*) [group G]
(f : R →◃ quandle.conj G)
(g : envel_group R →* G) (h : f = (quandle.conj.map g).comp (to_envel_group R)) :
g = to_envel_group.map f :=
h.symm ▸ (to_envel_group.map.apply_symm_apply g).symm
/--
The induced group homomorphism from the enveloping group into bijections of the rack,
using `rack.to_conj`. Satisfies the property `envel_action_prop`.
This gives the rack `R` the structure of an augmented rack over `envel_group R`.
-/
def envel_action {R : Type*} [rack R] : envel_group R →* (R ≃ R) :=
to_envel_group.map (to_conj R)
@[simp]
lemma envel_action_prop {R : Type*} [rack R] (x y : R) :
envel_action (to_envel_group R x) y = x ◃ y := rfl
end envel_group
end rack
|
8fe0de35e36caa44cc18ddda26a49b157f03cb51 | 90109f8736c31bdbe0cf5318c1c226250ff3bbff | /lean/cat.lean | 5a7d7796554cb7dbc92d319ef11e2cf447f6b0f2 | [] | no_license | akella/groupoid.space | c64db5a98a04cd73ae1259eeb6bc14dd1676e28c | 18ba185b80e2114c13faf983dbe0edb335a63587 | refs/heads/master | 1,584,768,713,904 | 1,529,586,461,000 | 1,529,586,461,000 | 138,165,171 | 1 | 0 | null | 1,529,582,805,000 | 1,529,582,804,000 | null | UTF-8 | Lean | false | false | 519 | lean |
import data.nat data.bool data.prod data.list data.sum data.stream
data.unit data.uprod data.tuple data.option
application persistence storage effects
open nat bool prod sum list classical option stream
application persistence storage effects
namespace cat
record category (P S: Type) := (spawn : P → S)
(run : S → P)
(action : P → S → S)
check action
end cat
|
fc5f1f0dcfc386a12f7ec7255c4ddbb1abae5420 | ec62863c729b7eedee77b86d974f2c529fa79d25 | /17/b.lean | 0c238ab1d27bf3df1d57e8797f7425b37eda5415 | [] | no_license | rwbarton/advent-of-lean-4 | 2ac9b17ba708f66051e3d8cd694b0249bc433b65 | 417c7e2718253ba7148c0279fcb251b6fc291477 | refs/heads/main | 1,675,917,092,057 | 1,609,864,581,000 | 1,609,864,581,000 | 317,700,289 | 24 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,332 | lean | instance : Monad List :=
{ pure := λ a => [a],
map := List.map,
bind := λ x f => (x.map f).join }
def fromCoord (w x y z : Int) : Nat :=
Int.toNat (5200 * w + 400 * x + 20 * y + z)
@[reducible] def Grid : Type := Array Bool
def Grid.get (g : Grid) (w x y z : Int) : Bool :=
if 0 ≤ w ∧ w < 13 ∧ 0 ≤ x ∧ x < 13 ∧ 0 ≤ y ∧ y < 20 ∧ 0 ≤ z ∧ z < 20
then g.get! (fromCoord w x y z) else false
def Grid.neighborCount (g : Grid) (w x y z : Int) : Nat :=
List.length $ do
let dw ← [(-1 : Int), 0, 1]
let dx ← [(-1 : Int), 0, 1]
let dy ← [(-1 : Int), 0, 1]
let dz ← [(-1 : Int), 0, 1]
if dw == 0 ∧ dx == 0 ∧ dy == 0 ∧ dz == 0 then []
if g.get (w+dw) (x+dx) (y+dy) (z+dz) then [()] else []
def Grid.evolve (g : Grid) : Grid :=
Array.mk $ do
let w ← List.range 13
let x ← List.range 13
let y ← List.range 20
let z ← List.range 20
let c := g.neighborCount w x y z
pure $ if g.get w x y z then c == 2 || c == 3 else c == 3
def main : IO Unit := do
let input ← IO.FS.lines "a.in"
let mut g : Grid := Array.mkArray (13*13*20*20) false
for i in [0:input.size] do
for j in [0:(input.get! i).length] do
g := g.set! (fromCoord 6 6 ((i:Int)+6) ((j:Int)+6)) ((input.get! i).get j == '#')
for _ in [0:6] do
g := g.evolve
IO.print s!"{(g.toList.filter id).length}\n"
|
24f0e81453dfc8f0589d9a7c4dcb650a216f0c14 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/category/Top/limits/pullbacks.lean | 8e0d847277388ef1babcfe6e699490c6e9e83268 | [
"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 | 15,848 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Scott Morrison, Mario Carneiro, Andrew Yang
-/
import topology.category.Top.limits.products
import category_theory.concrete_category.elementwise
/-!
# Pullbacks in the category of topological spaces.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
open topological_space
open category_theory
open category_theory.limits
universes u v w
noncomputable theory
namespace Top
variables {J : Type v} [small_category J]
section pullback
variables {X Y Z : Top.{u}}
/-- The first projection from the pullback. -/
abbreviation pullback_fst (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ X :=
⟨prod.fst ∘ subtype.val⟩
/-- The second projection from the pullback. -/
abbreviation pullback_snd (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ Y :=
⟨prod.snd ∘ subtype.val⟩
/-- The explicit pullback cone of `X, Y` given by `{ p : X × Y // f p.1 = g p.2 }`. -/
def pullback_cone (f : X ⟶ Z) (g : Y ⟶ Z) : pullback_cone f g :=
pullback_cone.mk (pullback_fst f g) (pullback_snd f g) (by { ext ⟨x, h⟩, simp [h] })
/-- The constructed cone is a limit. -/
def pullback_cone_is_limit (f : X ⟶ Z) (g : Y ⟶ Z) :
is_limit (pullback_cone f g) := pullback_cone.is_limit_aux' _
begin
intro s,
split, swap,
exact { to_fun := λ x, ⟨⟨s.fst x, s.snd x⟩,
by simpa using concrete_category.congr_hom s.condition x⟩ },
refine ⟨_,_,_⟩,
{ ext, delta pullback_cone, simp },
{ ext, delta pullback_cone, simp },
{ intros m h₁ h₂,
ext x,
{ simpa using concrete_category.congr_hom h₁ x },
{ simpa using concrete_category.congr_hom h₂ x } }
end
/-- The pullback of two maps can be identified as a subspace of `X × Y`. -/
def pullback_iso_prod_subtype (f : X ⟶ Z) (g : Y ⟶ Z) :
pullback f g ≅ Top.of { p : X × Y // f p.1 = g p.2 } :=
(limit.is_limit _).cone_point_unique_up_to_iso (pullback_cone_is_limit f g)
@[simp, reassoc] lemma pullback_iso_prod_subtype_inv_fst (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).inv ≫ pullback.fst = pullback_fst f g :=
by simpa [pullback_iso_prod_subtype]
@[simp] lemma pullback_iso_prod_subtype_inv_fst_apply (f : X ⟶ Z) (g : Y ⟶ Z)
(x : { p : X × Y // f p.1 = g p.2 }) :
(pullback.fst : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).fst :=
concrete_category.congr_hom (pullback_iso_prod_subtype_inv_fst f g) x
@[simp, reassoc] lemma pullback_iso_prod_subtype_inv_snd (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).inv ≫ pullback.snd = pullback_snd f g :=
by simpa [pullback_iso_prod_subtype]
@[simp] lemma pullback_iso_prod_subtype_inv_snd_apply (f : X ⟶ Z) (g : Y ⟶ Z)
(x : { p : X × Y // f p.1 = g p.2 }) :
(pullback.snd : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).snd :=
concrete_category.congr_hom (pullback_iso_prod_subtype_inv_snd f g) x
lemma pullback_iso_prod_subtype_hom_fst (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).hom ≫ pullback_fst f g = pullback.fst :=
by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_fst]
lemma pullback_iso_prod_subtype_hom_snd (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).hom ≫ pullback_snd f g = pullback.snd :=
by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_snd]
@[simp] lemma pullback_iso_prod_subtype_hom_apply {f : X ⟶ Z} {g : Y ⟶ Z}
(x : pullback f g) : (pullback_iso_prod_subtype f g).hom x =
⟨⟨(pullback.fst : pullback f g ⟶ _) x, (pullback.snd : pullback f g ⟶ _) x⟩,
by simpa using concrete_category.congr_hom pullback.condition x⟩ :=
begin
ext,
exacts [concrete_category.congr_hom (pullback_iso_prod_subtype_hom_fst f g) x,
concrete_category.congr_hom (pullback_iso_prod_subtype_hom_snd f g) x]
end
lemma pullback_topology {X Y Z : Top.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback f g).topological_space =
induced (pullback.fst : pullback f g ⟶ _) X.topological_space ⊓
induced (pullback.snd : pullback f g ⟶ _) Y.topological_space :=
begin
let homeo := homeo_of_iso (pullback_iso_prod_subtype f g),
refine homeo.inducing.induced.trans _,
change induced homeo (induced _ (_ ⊓ _)) = _,
simpa [induced_compose]
end
lemma range_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
set.range (prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) =
{ x | (limits.prod.fst ≫ f) x = (limits.prod.snd ≫ g) x } :=
begin
ext x,
split,
{ rintros ⟨y, rfl⟩,
simp only [←comp_apply, set.mem_set_of_eq],
congr' 1,
simp [pullback.condition] },
{ intro h,
use (pullback_iso_prod_subtype f g).inv ⟨⟨_, _⟩, h⟩,
apply concrete.limit_ext,
rintro ⟨⟨⟩⟩; simp, }
end
lemma inducing_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
inducing ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) :=
⟨by simp [prod_topology, pullback_topology, induced_compose, ←coe_comp]⟩
lemma embedding_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
embedding ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) :=
⟨inducing_pullback_to_prod f g, (Top.mono_iff_injective _).mp infer_instance⟩
/-- If the map `S ⟶ T` is mono, then there is a description of the image of `W ×ₛ X ⟶ Y ×ₜ Z`. -/
lemma range_pullback_map {W X Y Z S T : Top} (f₁ : W ⟶ S) (f₂ : X ⟶ S)
(g₁ : Y ⟶ T) (g₂ : Z ⟶ T) (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) [H₃ : mono i₃]
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
set.range (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) =
(pullback.fst : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₁) ∩
(pullback.snd : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₂) :=
begin
ext,
split,
{ rintro ⟨y, rfl⟩, simp, },
rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩⟩,
have : f₁ x₁ = f₂ x₂,
{ apply (Top.mono_iff_injective _).mp H₃,
simp only [←comp_apply, eq₁, eq₂],
simp only [comp_apply, hx₁, hx₂],
simp only [←comp_apply, pullback.condition] },
use (pullback_iso_prod_subtype f₁ f₂).inv ⟨⟨x₁, x₂⟩, this⟩,
apply concrete.limit_ext,
rintros (_|_|_),
{ simp only [Top.comp_app, limit.lift_π_apply, category.assoc, pullback_cone.mk_π_app_one,
hx₁, pullback_iso_prod_subtype_inv_fst_apply, subtype.coe_mk],
simp only [← comp_apply],
congr,
apply limit.w _ walking_cospan.hom.inl },
{ simp [hx₁] },
{ simp [hx₂] },
end
lemma pullback_fst_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) :
set.range (pullback.fst : pullback f g ⟶ _) = { x : X | ∃ y : Y, f x = g y} :=
begin
ext x,
split,
{ rintro ⟨y, rfl⟩,
use (pullback.snd : pullback f g ⟶ _) y,
exact concrete_category.congr_hom pullback.condition y },
{ rintro ⟨y, eq⟩,
use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩,
simp },
end
lemma pullback_snd_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) :
set.range (pullback.snd : pullback f g ⟶ _) = { y : Y | ∃ x : X, f x = g y} :=
begin
ext y,
split,
{ rintro ⟨x, rfl⟩,
use (pullback.fst : pullback f g ⟶ _) x,
exact concrete_category.congr_hom pullback.condition x },
{ rintro ⟨x, eq⟩,
use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩,
simp },
end
/--
If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are embeddings,
then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an embedding.
W ⟶ Y
↘ ↘
S ⟶ T
↗ ↗
X ⟶ Z
-/
lemma pullback_map_embedding_of_embeddings {W X Y Z S T : Top}
(f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z}
(H₁ : embedding i₁) (H₂ : embedding i₂) (i₃ : S ⟶ T)
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) :=
begin
refine embedding_of_embedding_compose (continuous_map.continuous_to_fun _)
(show continuous (prod.lift pullback.fst pullback.snd : pullback g₁ g₂ ⟶ Y ⨯ Z), from
continuous_map.continuous_to_fun _) _,
suffices : embedding
(prod.lift pullback.fst pullback.snd ≫ limits.prod.map i₁ i₂ : pullback f₁ f₂ ⟶ _),
{ simpa [←coe_comp] using this },
rw coe_comp,
refine embedding.comp (embedding_prod_map H₁ H₂)
(embedding_pullback_to_prod _ _)
end
/--
If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are open embeddings, and `S ⟶ T`
is mono, then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an open embedding.
W ⟶ Y
↘ ↘
S ⟶ T
↗ ↗
X ⟶ Z
-/
lemma pullback_map_open_embedding_of_open_embeddings {W X Y Z S T : Top}
(f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z}
(H₁ : open_embedding i₁) (H₂ : open_embedding i₂) (i₃ : S ⟶ T) [H₃ : mono i₃]
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
open_embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) :=
begin
split,
{ apply pullback_map_embedding_of_embeddings
f₁ f₂ g₁ g₂ H₁.to_embedding H₂.to_embedding i₃ eq₁ eq₂ },
{ rw range_pullback_map,
apply is_open.inter; apply continuous.is_open_preimage,
continuity,
exacts [H₁.open_range, H₂.open_range] }
end
lemma snd_embedding_of_left_embedding {X Y S : Top}
{f : X ⟶ S} (H : embedding f) (g : Y ⟶ S) :
embedding ⇑(pullback.snd : pullback f g ⟶ Y) :=
begin
convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).embedding.comp
(pullback_map_embedding_of_embeddings f g (𝟙 _) g H
(homeo_of_iso (iso.refl _)).embedding (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma fst_embedding_of_right_embedding {X Y S : Top}
(f : X ⟶ S) {g : Y ⟶ S} (H : embedding g) :
embedding ⇑(pullback.fst : pullback f g ⟶ X) :=
begin
convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).embedding.comp
(pullback_map_embedding_of_embeddings f g f (𝟙 _)
(homeo_of_iso (iso.refl _)).embedding H (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma embedding_of_pullback_embeddings {X Y S : Top}
{f : X ⟶ S} {g : Y ⟶ S} (H₁ : embedding f) (H₂ : embedding g) :
embedding (limit.π (cospan f g) walking_cospan.one) :=
begin
convert H₂.comp (snd_embedding_of_left_embedding H₁ g),
erw ←coe_comp,
congr,
exact (limit.w _ walking_cospan.hom.inr).symm
end
lemma snd_open_embedding_of_left_open_embedding {X Y S : Top}
{f : X ⟶ S} (H : open_embedding f) (g : Y ⟶ S) :
open_embedding ⇑(pullback.snd : pullback f g ⟶ Y) :=
begin
convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).open_embedding.comp
(pullback_map_open_embedding_of_open_embeddings f g (𝟙 _) g H
(homeo_of_iso (iso.refl _)).open_embedding (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma fst_open_embedding_of_right_open_embedding {X Y S : Top}
(f : X ⟶ S) {g : Y ⟶ S} (H : open_embedding g) :
open_embedding ⇑(pullback.fst : pullback f g ⟶ X) :=
begin
convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).open_embedding.comp
(pullback_map_open_embedding_of_open_embeddings f g f (𝟙 _)
(homeo_of_iso (iso.refl _)).open_embedding H (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
/-- If `X ⟶ S`, `Y ⟶ S` are open embeddings, then so is `X ×ₛ Y ⟶ S`. -/
lemma open_embedding_of_pullback_open_embeddings {X Y S : Top}
{f : X ⟶ S} {g : Y ⟶ S} (H₁ : open_embedding f) (H₂ : open_embedding g) :
open_embedding (limit.π (cospan f g) walking_cospan.one) :=
begin
convert H₂.comp (snd_open_embedding_of_left_open_embedding H₁ g),
erw ←coe_comp,
congr,
exact (limit.w _ walking_cospan.hom.inr).symm
end
lemma fst_iso_of_right_embedding_range_subset {X Y S : Top} (f : X ⟶ S) {g : Y ⟶ S}
(hg : embedding g) (H : set.range f ⊆ set.range g) : is_iso (pullback.fst : pullback f g ⟶ X) :=
begin
let : (pullback f g : Top) ≃ₜ X :=
(homeomorph.of_embedding _ (fst_embedding_of_right_embedding f hg)).trans
{ to_fun := coe,
inv_fun := (λ x, ⟨x,
by { rw pullback_fst_range, exact ⟨_, (H (set.mem_range_self x)).some_spec.symm⟩ }⟩),
left_inv := λ ⟨_,_⟩, rfl,
right_inv := λ x, rfl },
convert is_iso.of_iso (iso_of_homeo this),
ext,
refl
end
lemma snd_iso_of_left_embedding_range_subset {X Y S : Top} {f : X ⟶ S} (hf : embedding f)
(g : Y ⟶ S) (H : set.range g ⊆ set.range f) : is_iso (pullback.snd : pullback f g ⟶ Y) :=
begin
let : (pullback f g : Top) ≃ₜ Y :=
(homeomorph.of_embedding _ (snd_embedding_of_left_embedding hf g)).trans
{ to_fun := coe,
inv_fun := (λ x, ⟨x,
by { rw pullback_snd_range, exact ⟨_, (H (set.mem_range_self x)).some_spec⟩ }⟩),
left_inv := λ ⟨_,_⟩, rfl,
right_inv := λ x, rfl },
convert is_iso.of_iso (iso_of_homeo this),
ext,
refl
end
lemma pullback_snd_image_fst_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set X) :
(pullback.snd : pullback f g ⟶ _) '' ((pullback.fst : pullback f g ⟶ _) ⁻¹' U) =
g ⁻¹' (f '' U) :=
begin
ext x,
split,
{ rintros ⟨y, hy, rfl⟩,
exact ⟨(pullback.fst : pullback f g ⟶ _) y, hy,
concrete_category.congr_hom pullback.condition y⟩ },
{ rintros ⟨y, hy, eq⟩,
exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩, eq⟩, by simpa, by simp⟩ },
end
lemma pullback_fst_image_snd_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set Y) :
(pullback.fst : pullback f g ⟶ _) '' ((pullback.snd : pullback f g ⟶ _) ⁻¹' U) =
f ⁻¹' (g '' U) :=
begin
ext x,
split,
{ rintros ⟨y, hy, rfl⟩,
exact ⟨(pullback.snd : pullback f g ⟶ _) y, hy,
(concrete_category.congr_hom pullback.condition y).symm⟩ },
{ rintros ⟨y, hy, eq⟩,
exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩,eq.symm⟩, by simpa, by simp⟩ },
end
end pullback
lemma coinduced_of_is_colimit {F : J ⥤ Top.{max v u}} (c : cocone F) (hc : is_colimit c) :
c.X.topological_space = ⨆ j, (F.obj j).topological_space.coinduced (c.ι.app j) :=
begin
let homeo := homeo_of_iso (hc.cocone_point_unique_up_to_iso (colimit_cocone_is_colimit F)),
ext,
refine homeo.symm.is_open_preimage.symm.trans (iff.trans _ is_open_supr_iff.symm),
exact is_open_supr_iff
end
lemma colimit_topology (F : J ⥤ Top.{max v u}) :
(colimit F).topological_space = ⨆ j, (F.obj j).topological_space.coinduced (colimit.ι F j) :=
coinduced_of_is_colimit _ (colimit.is_colimit F)
lemma colimit_is_open_iff (F : J ⥤ Top.{max v u}) (U : set ((colimit F : _) : Type (max v u))) :
is_open U ↔ ∀ j, is_open (colimit.ι F j ⁻¹' U) :=
begin
conv_lhs { rw colimit_topology F },
exact is_open_supr_iff
end
lemma coequalizer_is_open_iff (F : walking_parallel_pair ⥤ Top.{u})
(U : set ((colimit F : _) : Type u)) :
is_open U ↔ is_open (colimit.ι F walking_parallel_pair.one ⁻¹' U) :=
begin
rw colimit_is_open_iff.{u},
split,
{ intro H, exact H _ },
{ intros H j,
cases j,
{ rw ←colimit.w F walking_parallel_pair_hom.left,
exact (F.map walking_parallel_pair_hom.left).continuous_to_fun.is_open_preimage _ H },
{ exact H } }
end
end Top
|
739ab39ecccc5ccf840360137396c115c17978af | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /archive/oxford_invariants/2021summer/week3_p1.lean | d9958cd21cac4289fcbc62cc6f6153c29d92c90b | [
"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 | 6,779 | lean | /-
Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Bhavik Mehta
-/
import algebra.big_operators.order
import algebra.big_operators.ring
import algebra.char_zero.lemmas
import data.rat.cast
/-!
# The Oxford Invariants Puzzle Challenges - Summer 2021, Week 3, Problem 1
## Original statement
Let `n ≥ 3`, `a₁, ..., aₙ` be strictly positive integers such that `aᵢ ∣ aᵢ₋₁ + aᵢ₊₁` for
`i = 2, ..., n - 1`. Show that $\sum_{i=1}^{n-1}\dfrac{a_0a_n}{a_ia_{i+1}} ∈ \mathbb N$.
## Comments
Mathlib is based on type theory, so saying that a rational is a natural doesn't make sense. Instead,
we ask that there exists `b : ℕ` whose cast to `α` is the sum we want.
In mathlib, `ℕ` starts at `0`. To make the indexing cleaner, we use `a₀, ..., aₙ₋₁` instead of
`a₁, ..., aₙ`. Similarly, it's nicer to not use substraction of naturals, so we replace
`aᵢ ∣ aᵢ₋₁ + aᵢ₊₁` by `aᵢ₊₁ ∣ aᵢ + aᵢ₊₂`.
We don't actually have to work in `ℚ` or `ℝ`. We can be even more general by stating the result for
any linearly ordered field.
Instead of having `n` naturals, we use a function `a : ℕ → ℕ`.
In the proof itself, we replace `n : ℕ, 1 ≤ n` by `n + 1`.
The statement is actually true for `n = 0, 1` (`n = 1, 2` before the reindexing) as the sum is
simply `0` and `1` respectively. So the version we prove is slightly more general.
Overall, the indexing is a bit of a mess to understand. But, trust Lean, it works.
## Formalised statement
Let `n : ℕ`, `a : ℕ → ℕ`, `∀ i ≤ n, 0 < a i`, `∀ i, i + 2 ≤ n → aᵢ₊₁ ∣ aᵢ + aᵢ₊₂` (read `→` as
"implies"). Then there exists `b : ℕ` such that `b` as an element of any linearly ordered field
equals $\sum_{i=0}^{n-1} (a_0 a_n) / (a_i a_{i+1})$.
## Proof outline
The case `n = 0` is trivial.
For `n + 1`, we prove the result by induction but by adding `aₙ₊₁ ∣ aₙ * b - a₀` to the induction
hypothesis, where `b` is the previous sum, $\sum_{i=0}^{n-1} (a_0 a_n) / (a_i a_{i+1})$, as a
natural.
* Base case:
* $\sum_{i=0}^0 (a_0 a_{0+1}) / (a_0 a_{0+1})$ is a natural:
$\sum_{i=0}^0 (a_0 a_{0+1}) / (a_0 a_{0+1}) = (a_0 a_1) / (a_0 a_1) = 1$.
* Divisibility condition:
`a₀ * 1 - a₀ = 0` is clearly divisible by `a₁`.
* Induction step:
* $\sum_{i=0}^n (a_0 a_{n+1}) / (a_i a_{i+1})$ is a natural:
$$\sum_{i=0}^{n+1} (a_0 a_{n+2}) / (a_i a_{i+1})
= \sum_{i=0}^n\ (a_0 a_{n+2}) / (a_i a_{i+1}) + (a_0 a_{n+2}) / (a_{n+1} a_{n+2})
= a_{n+2} / a_{n+1} × \sum_{i=0}^n (a_0 a_{n+1}) / (a_i a_{i+1}) + a_0 / a_{n+1}
= a_{n+2} / a_{n+1} × b + a_0 / a_{n+1}
= (a_n + a_{n+2}) / a_{n+1} × b - (a_n b - a_0)(a_{n+1})$$
which is a natural because `(aₙ + aₙ₊₂)/aₙ₊₁`, `b` and `(aₙ * b - a₀)/aₙ₊₁` are (plus an
annoying inequality, or the fact that the original sum is positive because its terms are).
* Divisibility condition:
`aₙ₊₁ * ((aₙ + aₙ₊₂)/aₙ₊₁ * b - (aₙ * b - a₀)/aₙ₊₁) - a₀ = aₙ₊₁aₙ₊₂b` is divisible by `aₙ₊₂`.
-/
open_locale big_operators
variables {α : Type*} [linear_ordered_field α]
theorem week3_p1 (n : ℕ) (a : ℕ → ℕ) (a_pos : ∀ i ≤ n, 0 < a i)
(ha : ∀ i, i + 2 ≤ n → a (i + 1) ∣ a i + a (i + 2)) :
∃ b : ℕ, (b : α) = ∑ i in finset.range n, (a 0 * a n)/(a i * a (i + 1)) :=
begin
-- Treat separately `n = 0` and `n ≥ 1`
cases n,
/- Case `n = 0`
The sum is trivially equal to `0` -/
{ exact ⟨0, by rw [nat.cast_zero, finset.sum_range_zero]⟩ }, -- `⟨Claim it, Prove it⟩`
/- Case `n ≥ 1`. We replace `n` by `n + 1` everywhere to make this inequality explicit
Set up the stronger induction hypothesis -/
rsuffices ⟨b, hb, -⟩ :
∃ b : ℕ, (b : α) = ∑ i in finset.range (n + 1), (a 0 * a (n + 1)) / (a i * a (i + 1))
∧ a (n + 1) ∣ a n * b - a 0,
{ exact ⟨b, hb⟩ },
simp_rw ←@nat.cast_pos α at a_pos,
/- Declare the induction
`ih` will be the induction hypothesis -/
induction n with n ih,
/- Base case
Claim that the sum equals `1`-/
{ refine ⟨1, _, _⟩,
-- Check that this indeed equals the sum
{ rw [nat.cast_one, finset.sum_range_one, div_self],
exact (mul_pos (a_pos 0 (nat.zero_le _)) (a_pos 1 (nat.zero_lt_succ _))).ne' },
-- Check the divisibility condition
{ rw [mul_one, tsub_self],
exact dvd_zero _ } },
/- Induction step
`b` is the value of the previous sum as a natural, `hb` is the proof that it is indeed the value,
and `han` is the divisibility condition -/
obtain ⟨b, hb, han⟩ := ih (λ i hi, ha i $ nat.le_succ_of_le hi)
(λ i hi, a_pos i $ nat.le_succ_of_le hi),
specialize ha n le_rfl,
have ha₀ : a 0 ≤ a n * b, -- Needing this is an artifact of `ℕ`-substraction.
{ rw [←@nat.cast_le α, nat.cast_mul, hb, ←div_le_iff' (a_pos _ $ n.le_succ.trans $ nat.le_succ _),
←mul_div_mul_right _ _ (a_pos _ $ nat.le_succ _).ne'],
suffices h : ∀ i, i ∈ finset.range (n + 1) → 0 ≤ (a 0 : α) * a (n + 1) / (a i * a (i + 1)),
{ exact finset.single_le_sum h (finset.self_mem_range_succ n) },
refine (λ i _, div_nonneg _ _); refine mul_nonneg _ _; exact nat.cast_nonneg _ },
-- Claim that the sum equals `(aₙ + aₙ₊₂)/aₙ₊₁ * b - (aₙ * b - a₀)/aₙ₊₁`
refine ⟨(a n + a (n + 2))/ a (n + 1) * b - (a n * b - a 0) / a (n + 1), _, _⟩,
-- Check that this indeed equals the sum
{ calc
(((a n + a (n + 2)) / a (n + 1) * b - (a n * b - a 0) / a (n + 1) : ℕ) : α)
= (a n + a (n + 2)) / a (n + 1) * b - (a n * b - a 0) / a (n + 1) : begin
norm_cast,
rw nat.cast_sub (nat.div_le_of_le_mul _),
rw [←mul_assoc, nat.mul_div_cancel' ha, add_mul],
exact tsub_le_self.trans (nat.le_add_right _ _),
end
... = a (n + 2) / a (n + 1) * b + (a 0 * a (n + 2)) / (a (n + 1) * a (n + 2))
: by rw [add_div, add_mul, sub_div, mul_div_right_comm, add_sub_sub_cancel,
mul_div_mul_right _ _ (a_pos _ le_rfl).ne']
... = ∑ (i : ℕ) in finset.range (n + 2), a 0 * a (n + 2) / (a i * a (i + 1))
: begin
rw [finset.sum_range_succ, hb, finset.mul_sum],
congr, ext i,
rw [←mul_div_assoc, ←mul_div_right_comm, mul_div_assoc, mul_div_cancel _
(a_pos _ $ nat.le_succ _).ne', mul_comm],
end },
-- Check the divisibility condition
{ rw [mul_tsub, ← mul_assoc, nat.mul_div_cancel' ha, add_mul,
nat.mul_div_cancel' han, add_tsub_tsub_cancel ha₀, add_tsub_cancel_right],
exact dvd_mul_right _ _ }
end
|
9618cda6809e039a96d51e766262f9f6a39b095b | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/theories/topology/order_topology.lean | fe7a19058e0476932c5c78fe9a114b321b64cd66 | [
"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 | 3,184 | lean | /-
Copyright (c) 2016 Jacob Gross. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jacob Gross
The order topology.
-/
import data.set theories.topology.basic algebra.interval
open algebra eq.ops set interval topology
namespace order_topology
variables {X : Type} [linear_strong_order_pair X]
definition linorder_generators : set (set X) := {y | ∃ a, y = '(a, ∞) } ∪ {y | ∃ a, y = '(-∞, a)}
definition linorder_topology [instance] : topology X :=
topology.generated_by linorder_generators
theorem Open_Ioi {a : X} : Open '(a, ∞) :=
(generators_mem_topology_generated_by linorder_generators) (!mem_unionl (exists.intro a rfl))
theorem Open_Iio {a : X} : Open '(-∞, a) :=
(generators_mem_topology_generated_by linorder_generators) (!mem_unionr (exists.intro a rfl))
theorem closed_Ici (a : X) : closed '[a,∞) :=
!compl_Ici⁻¹ ▸ Open_Iio
theorem closed_Iic (a : X) : closed '(-∞,a] :=
have '(a, ∞) = -'(-∞,a], from ext(take x, iff.intro
(assume H, not_le_of_gt H)
(assume H, lt_of_not_ge H)),
this ▸ Open_Ioi
theorem Open_Ioo (a b : X) : Open '(a, b) :=
Open_inter !Open_Ioi !Open_Iio
theorem closed_Icc (a b : X) : closed '[a, b] :=
closed_inter !closed_Ici !closed_Iic
section
open classical
theorem linorder_separation {x y : X} :
x < y → ∃ a b, (x < a ∧ b < y) ∧ '(-∞, a) ∩ '(b, ∞) = ∅ :=
suppose x < y,
if H1 : ∃ z, x < z ∧ z < y then
obtain z (Hz : x < z ∧ z < y), from H1,
have '(-∞, z) ∩ '(z, ∞) = ∅, from ext (take r, iff.intro
(assume H, absurd (!lt.trans (and.elim_left H) (and.elim_right H)) !lt.irrefl)
(assume H, !not.elim !not_mem_empty H)),
exists.intro z (exists.intro z (and.intro Hz this))
else
have '(-∞, y) ∩ '(x, ∞) = ∅, from ext(take r, iff.intro
(assume H, absurd (exists.intro r (iff.elim_left and.comm H)) H1)
(assume H, !not.elim !not_mem_empty H)),
exists.intro y (exists.intro x (and.intro (and.intro `x < y` `x < y`) this))
end
protected definition T2_space.of_linorder_topology [trans_instance] :
T2_space X :=
⦃ T2_space, linorder_topology,
T2 := abstract
take x y, assume H,
or.elim (lt_or_gt_of_ne H)
(assume H,
obtain a [b Hab], from linorder_separation H,
show _, from exists.intro '(-∞, a) (exists.intro '(b, ∞)
(and.intro Open_Iio (and.intro Open_Ioi (iff.elim_left and.assoc Hab)))))
(assume H,
obtain a [b Hab], from linorder_separation H,
have Hx : x ∈ '(b, ∞), from and.elim_right (and.elim_left Hab),
have Hy : y ∈ '(-∞, a), from and.elim_left (and.elim_left Hab),
have Hi : '(b, ∞) ∩ '(-∞, a) = ∅, from !inter_comm ▸ (and.elim_right Hab),
have (Open '(b,∞)) ∧ (Open '(-∞, a)) ∧ x ∈ '(b, ∞) ∧ y ∈ '(-∞, a) ∧
'(b, ∞) ∩ '(-∞, a) = ∅, from
and.intro Open_Ioi (and.intro Open_Iio (and.intro Hx (and.intro Hy Hi))),
show _, from exists.intro '(b,∞) (exists.intro '(-∞, a) this))
end ⦄
end order_topology
|
0a0067ac1f01a955db3669b3d0da4dc5b74d5ae9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/smt/default_auto.lean | 30e2aca6fe90d7776aa161d08a231e5d8b15e8dd | [] | 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 | 502 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.smt.congruence_closure
import Mathlib.Lean3Lib.init.cc_lemmas
import Mathlib.Lean3Lib.init.meta.smt.ematch
import Mathlib.Lean3Lib.init.meta.smt.smt_tactic
import Mathlib.Lean3Lib.init.meta.smt.interactive
import Mathlib.Lean3Lib.init.meta.smt.rsimp
namespace Mathlib
end Mathlib |
53dc1b607de3e3ca31f2b829e3349e5eaea970c9 | 9d2e3d5a2e2342a283affd97eead310c3b528a24 | /src/exercises_sources/thursday/morning/order.lean | fbf8f53047db2bc61ea485f0a6fd3236c6c0d336 | [] | permissive | Vtec234/lftcm2020 | ad2610ab614beefe44acc5622bb4a7fff9a5ea46 | bbbd4c8162f8c2ef602300ab8fdeca231886375d | refs/heads/master | 1,668,808,098,623 | 1,594,989,081,000 | 1,594,990,079,000 | 280,423,039 | 0 | 0 | MIT | 1,594,990,209,000 | 1,594,990,209,000 | null | UTF-8 | Lean | false | false | 19,124 | lean | /-
# Orders
Groups, rings, fields, modules etc are in the "algebra hierarchy".
Metric and topological spaces are in the "topology hierarchy".
The other important hierarchy is the "order hierarchy".
It starts with partially ordered sets, and then goes on to lattices.
Because I like algebra, let's demonstrate the order hierarchy by
making an algebraic type, namely the type of subgroups of a group G,
and then working up the order hierarchy with it. Subgroups of a group
are ordered by inclusion, and this is where we shall start. We will
then define infs and sups, and bot and top, and go on from there.
-/
import tactic
-- We will be using all of the theory of subsets of a type
-- without further comment (e.g. `inter_subset_left A B : A ∩ B ⊆ A`)
-- so let's open the `set` namespace.
open set
-- The type of subgroups of a group G is called `subgroup G` in Lean.
-- It already has a lattice structure in Lean.
-- So let's just redo the entire theory and call it `subgp G`.
/-- The type of subgroups of a group `G`. -/
structure subgp (G : Type) [group G] :=
-- A subgroup of G is a sub*set* of G, called `carrier`
(carrier : set G)
-- and then axioms saying it's closed under the group structure (i.e. *, 1, ⁻¹)
(mul_mem {a b : G} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier)
(one_mem : (1 : G) ∈ carrier)
(inv_mem {a : G} : a ∈ carrier → a⁻¹ ∈ carrier)
namespace subgp
/-
Note in particular that we have a function `subgp.carrier : subgp G → set G`,
sending a subgroup of `G` to the underlying subset (`set G` is the type
of subsets of G).
-/
-- Let G be a group, let H,J,K be subgroups of G, and let a,b,c be elements of G.
variables {G : Type} [group G] (H J K : subgp G) (a b c : G)
/- # Extensionality
One of the first things you should consider proving about a newly-defined
type is an extensionality lemma: a sensible criterion to check that
two terms are equal. When are two subgroups of `G` equal? A subgroup
is defined by four things: a subset, and three proofs. But two proofs
of a proposition `P` are equal by definition in Lean, so two subgroups
of `G` are equal iff their underlying subsets are equal, which is
true iff their underlying subsets have the same elements. Let's give
names to these basic results because they'll show up everywhere.
Let's start by showing that two subgroups are equal if their
underlying subsets are equal. This is precisely the statement that
`∀ H J : subgp G, H.carrier = J.carrier → H = J`, and a good name
for this would be `carrier_injective`. We adopt the Lean tradition
of putting as many things to the left of the `:` as we can; it
doesn't change the statement of the theorem.
-/
lemma carrier_injective (H J : subgp G) (h : H.carrier = J.carrier) : H = J :=
begin
-- take H and J apart
cases H, cases J,
-- and note that they are the same set, and then a bunch of proofs
-- which are equal by definition, so it's obvious
simp * at *,
end
-- Now let's prove that two subgroups are equal iff they have the same elements.
-- This is the most useful "extensionality lemma" so we tag it `@[ext]`.
@[ext] theorem ext {H J : subgp G} (h : ∀ (x : G), x ∈ H.carrier ↔ x ∈ J.carrier) :
H = J :=
begin
-- it suffices to prove the subsets are equal
apply carrier_injective,
-- Now let's use extensionality for subsets
ext x,
exact h x,
end
-- We also want the `iff` version of this.
theorem ext_iff {H J : subgp G} : H = J ↔ ∀ (x : G), x ∈ H.carrier ↔ x ∈ J.carrier :=
begin
split,
{ -- one way is just a rewrite!
intro h,
rw h,
simp,
},
{ -- the other way we just did
exact subgp.ext,
}
end
/-
## Partial orders
-/
-- These are familiar to most mathematicians. We will put a partial order
-- structure on `subgp G`. In other words, we will create a term of
-- type `partial_order (subgp G)`.
-- Let's define `H ≤ J` to mean `H.carrier ⊆ J.carrier`, using the `has_le` notation typeclass
instance : has_le (subgp G) := ⟨λ H J, H.carrier ⊆ J.carrier⟩
-- "tidy" is a one-size-fits-all tactic which solves certain kinds of "follow your nose" goals.
instance : partial_order (subgp G) :=
{ le := (≤),
le_refl := by tidy,
le_trans := by tidy,
le_antisymm := by tidy }
/- Here is a second proof. If X → Y is injective, and Y is partially ordered,
then X inherits a partial order. This construction (it's not a theorem, because
it involves data) is called `partial_order.lift`. Applying it to the injection
`subgp.carrier` and the fact that Lean already knows that `set G` is partially
ordered, turns into this second construction, (which I won't call an `instance`
because if I did then I would have committed the sin of making two terms of
type `partial_order (subgp G)`, and `partial_order (subgp G)` is a class so
should have at most one instance):
-/
-- partial_order.lift is the function which pulls a partial order back along an injection.
example : partial_order (subgp G) := partial_order.lift subgp.carrier carrier_injective
/- Note that we magically just inherited `<` notatation, because
`#check partial_order` tells you that `partial_order` extends `preorder`,
which extends `has_lt`, which is a notation typeclass. In other words,
`#check (H < J)` makes sense, and is a `Prop`. In fact `H < J` is
defined to mean `H ≤ J ∧ ¬ (J ≤ H)`.
# From partial orders to lattices.
Let's now prove that `subgp G` is a `semilattice_inf_top`. This is a class
which extends `partial_order` -- it is a partial order equipped with a top element,
and a function `inf : subgp G → subgp G → subgp G` (called "inf" or "meet"
or "greatest lower bound", satisfying some axioms. In our case, `top`
will be the subgroup `G` of `G` (or more precisely `univ`), and `inf` will
just be intersection. The work we need to do is to check that these are
subgroups, and to prove the axioms for a `semilattice_inf_top`, which
we'll come to later.
First let's define `top` -- the biggest subgroup. The underlying carrier
is `univ : set G`, i.e. the subset `G` of `G`. I'll leave it to you to prove
that the subgroup axioms hold!
The useful piece of interface for `univ` you'll need is `mem_univ g : g ∈ univ`.
-/
def top : subgp G :=
{ carrier := set.univ,
mul_mem := begin
intros,
apply mem_univ,
end,
one_mem := begin
apply mem_univ,
end,
inv_mem := begin
intros,
apply mem_univ,
end }
-- Add the `⊤` notation (typed with `\top`) for this subgroup:
instance : has_top (subgp G) := ⟨top⟩
-- Now `#check (⊤ : subgp G)` works
/-
We'll now prove the theorem that the intersection of
two subgroups is a subgroup. This is a *definition* in Lean,
indeed it is a construction which given two subgroups `H` and `K` of `G`
produces a third subgroup `H ⊓ K` (Lean's notation for `inf H K`).
The part of the interface for `∩` you'll need is that `a ∈ B ∩ C` is
definitionally equal to `a ∈ B ∧ a ∈ C`, so you can use `split`
if you have a goal `⊢ a ∈ B ∩ C`, and you can use `cases h` if you
have a hypothesis `h : a ∈ B ∩ C`. Don't forget `mul_mem H`, `one_mem H`
and `inv_mem H`, the axioms for `H` if `H : subgp G`.
-/
/-- "Theorem" : intersection of two subgps is a subgp -/
definition inf (H K : subgp G) : subgp G :=
{ carrier := H.carrier ∩ K.carrier,
mul_mem := begin
rintros a b ⟨haH, haK⟩ ⟨hbH, hbK⟩,
split,
{ apply H.mul_mem haH hbH },
{ apply K.mul_mem haK hbK },
end,
one_mem := begin
split,
{ apply one_mem },
{ apply one_mem },
end,
inv_mem := begin
rintros a ⟨haH, haK⟩,
exact ⟨H.inv_mem haH, K.inv_mem haK⟩,
end }
-- Add the `⊓` notation (type with `\inf`) for the intersection (inf) of two subgroups:
instance : has_inf (subgp G) := ⟨inf⟩
-- We now check the four axioms for a semilattice_inf_top.
-- They are called `le_top`, `inf_le_left`, `inf_le_right` and `le_inf`.
-- You might be able to guess the statementss of the axioms
-- from their names.
lemma le_top (H : subgp G) : H ≤ ⊤ :=
begin
intros x hx,
apply mem_univ,
end
lemma inf_le_left (H K : subgp G) : H ⊓ K ≤ H :=
begin
-- by definition this says `H.carrier ∩ K.carrier ⊆ H.carrier`
change H.carrier ∩ K.carrier ⊆ H.carrier,
-- now try `library_search`, to find that this is called `inter_subset_left
apply inter_subset_left,
end
lemma inf_le_right (H K : subgp G) : H ⊓ K ≤ K :=
inter_subset_right _ _
-- Can you use `library_search`, or other methods, to find the name of the
-- statement that if `A B C : set G` then `A ⊆ B → A ⊆ C → A ⊆ (B ∩ C)`?
lemma le_inf (H J K : subgp G) (h1 : H ≤ J) (h2 : H ≤ K) : H ≤ J ⊓ K :=
begin
exact subset_inter h1 h2,
end
-- Now we're ready to make the instance.
instance : semilattice_inf_top (subgp G) :=
{ top := top,
le_top := le_top,
inf := inf,
inf_le_left := inf_le_left,
inf_le_right := inf_le_right,
le_inf := le_inf,
.. subgp.partial_order } -- don't forget to inlude the partial order
/- The logic behind `semilattice_inf_top` is that it is the simplest class
which is closed under all finite "meet"s. The meet of 0 subgroups
is `top`, the meet of one subgroup is the subgroup, the meet of two
subgroups is their inf, and for three or more you proceed by induction.
We could now go on to make a `semilattice_sup_bot` structure, and then
a lattice structure. But let's jump straight to the strongest type
in the order hierarchy -- a `complete_lattice`. This has arbitrary `Inf` and `Sup`s.
So let's first note that we can do better than finite intersections -- we can take
arbitrary intersections! Let's now define the `Inf` of an arbitrary
set of subgroups of `G`.
The part of the interface for sets you'll need to know here is that if `S` is a
set of subsets of `G`, then `⋂₀ S` is notation for their intersection, and
to work with it you'll need to know
`set.mem_sInter : g ∈ ⋂₀ S ↔ ∀ (U : set G), U ∈ S → g ∈ U`.
-/
def Inf (S : set (subgp G)) : subgp G :=
{ carrier := Inf (subgp.carrier '' S),
mul_mem := begin
intros x y hx hy,
rw mem_sInter at hx hy ⊢,
rintro t ⟨H, hH, rfl⟩,
apply H.mul_mem,
apply hx, use H, tauto,
apply hy, use H, tauto,
end,
one_mem := begin
rw mem_sInter,
rintro t ⟨H, hH, rfl⟩,
apply subgp.one_mem,
end,
inv_mem := begin
intros x hx,
rw mem_sInter at hx ⊢,
rintro t ⟨H, Hh, rfl⟩,
apply H.inv_mem,
apply hx,
use [H, Hh],
end }
-- We now equip `subgp G` with an Inf. I think the notation is `⨅`, or `\Inf`,
-- but I find it hard to use, and `#print notation ⨅` returns garbage.
instance : has_Inf (subgp G) := ⟨Inf⟩
/- # Complete lattices
Let's jump straight from `semilattice_inf_bot` to `complete_lattice`.
A complete lattice has arbitrary Infs and arbitrary Sups, and satisfies
some other axioms which you can probably imagine. Our next goal
is to make `subgp G` into a complete lattice. We will do it in two ways.
The first way is to show that if our `Inf` satisfies
`(∀ (S : set (subgp G)), is_glb S (Inf S))` then we can build a complete
lattice from this, using `complete_lattice_of_Inf`.
-/
instance : complete_lattice (subgp G) := complete_lattice_of_Inf _ begin
-- ⊢ ∀ (s : set (subgp G)), is_glb s (has_Inf.Inf s)
-- See if you can figure out what this says, and how to prove it.
-- You might find the function `is_glb.of_image` useful.
intro S,
apply @is_glb.of_image _ _ _ _ subgp.carrier,
intros, refl,
apply is_glb_Inf,
end
/- Now let me show you another way to do this.
# Galois connections
A Galois conection is a pair of adjoint functors between two
partially ordered sets, considered as categories whose hom sets Hom(H,J) have
size 1 if H ≤ J and size 0 otherwise. In other words, a Galois
connection between two partial orders α and β is a pair of monotone functions
`l : α → β` and `u : β → α` such that `∀ (a : α) (b : β), l a ≤ b ↔ a ≤ u b`.
There is an example coming from Galois theory (between subfields and subgroups),
and an example coming from classical algebraic geometry (between affine
varieties and ideals); note that in both cases you have to use the opposite
partial order on one side to make everything covariant.
The examples we want to keep in mind here are:
1) α = subsets of G, β = subgroups of G, l = "subgroup generated by", u = `carrier`
2) X = topological space, α := set (set X), β := topologies on X,
l = topology generated by a collection of open sets, u = the open sets regarded as subsets.
As you can imagine, there are a bunch of abstract theorems with simple proofs
proved for Galois connections. You can see them by `#check galois_connection`,
jumping to the definition, and reading the next 150 lines of the mathlib file
after the definition. Examples of theorems you might recognise from contexts
where you have seen this before:
lemma le_u_l (a : α) : a ≤ u (l a) := ...
lemma l_u_le (b : β) : l (u b) ≤ b := ...
lemma u_l_u_eq_u : u ∘ l ∘ u = u := ...
lemma l_u_l_eq_l : l ∘ u ∘ l = l := ...
# Galois insertions
A particularly cool kind of Galois connection is a Galois insertion, which
is a Galois connection such that `l ∘ u = id`. This is true for both
the examples we're keeping in mind (the subgroup of `G` generated
by a subgroup is the same subgroup; the topology on `X` generated by a
topology is the same topology).
Our new goal: let's make subgroups of a group into a complete lattice,
using the fact that `carrier` is part of a Galois insertion.
-/
-- The adjoint functor to the `carrier` functor is the `span` functor
-- from subsets to subgps. Here we will CHEAT by using `Inf` to
-- define `span`. We could have built `span` directly with
-- an inductive definition.
def span (S : set G) : subgp G := Inf {H : subgp G | S ⊆ H.carrier}
-- Here are some theorems about it.
lemma monotone_carrier : monotone (subgp.carrier : subgp G → set G) :=
λ H J, id
lemma monotone_span : monotone (span : set G → subgp G) :=
λ S T h, Inf_le_Inf $ λ H hH x hx, hH $ h hx
lemma subset_span (S : set G) : S ≤ (span S).carrier :=
begin
rintro x hx _ ⟨H, hH, rfl⟩,
exact hH hx,
end
lemma span_subgp (H : subgp G) : span H.carrier = H :=
begin
ext,
split,
{ intro hx,
unfold span at hx,
replace hx := mem_sInter.1 hx,
apply hx,
use H,
simp },
{ intro h,
apply subset_span,
exact h},
end
-- We have proved all the things we need to show that `span` and `carrier`
-- form a Galois insertion, using `galois_insertion.monotone_intro`.
def gi_subgp : galois_insertion (span : set G → subgp G) (subgp.carrier : subgp G → set G) :=
galois_insertion.monotone_intro monotone_carrier monotone_span subset_span span_subgp
-- Note that `set G` is already a complete lattice:
example : complete_lattice (set G) := by apply_instance
-- and now `subgp G` can also be made into a complete lattice, by
-- a theorem about Galois insertions. Again, I don't use `instance`
-- because we already made the instance above.
example : complete_lattice (subgp G) := galois_insertion.lift_complete_lattice gi_subgp
end subgp
-- Because Alex defined the topology generated by a collection of subsets
-- yesterday, I'll show you how you can use Galois insertions to prove
-- that if `X : Type` then the type of topological space structures on `X`
-- is a complete lattice. We use the topology generated by a collection
-- of subsets, which is a functor adjoint to the forgetful functor.
-- We start by literally copying some stuff from Alex' talk.
open set
@[ext]
class topological_space (X : Type) :=
(is_open : set X → Prop) -- why set X → Prop not set (set X)? former plays
-- nicer with typeclasses later
(univ_mem : is_open univ)
(union : ∀ (B : set (set X)) (h : ∀ b ∈ B, is_open b), is_open (⋃₀ B))
(inter : ∀ (A B : set X) (hA : is_open A) (hB : is_open B), is_open (A ∩ B))
namespace topological_space
def forget {X : Type} : topological_space X → set (set X) := @is_open X
/-- The open sets of the least topology containing a collection of basic sets. -/
inductive generated_open (X : Type) (g : set (set X)) : set X → Prop
| basic : ∀ s ∈ g, generated_open s
| univ : generated_open univ
| inter : ∀s t, generated_open s → generated_open t → generated_open (s ∩ t)
| sUnion : ∀k, (∀ s ∈ k, generated_open s) → generated_open (⋃₀ k)
/-- The smallest topological space containing the collection `g` of basic sets -/
def generate_from {X : Type} (g : set (set X)) : topological_space X :=
{ is_open := generated_open X g,
univ_mem := sorry,
inter := sorry,
union := sorry }
-- Recall that `topological_space X` is the type of topological space structures
-- on `X`. Our Galois insertion will use the adjoint functors
-- `generate_from` and `is_open`.
-- We'd better start by giving the collection of topological space structures on X
-- a partial order:
instance (X : Type) : partial_order (topological_space X) :=
partial_order.lift (forget)
begin
-- need to show that a top space is determined by its open sets
intros τ₁ τ₂ h,
cases τ₁, cases τ₂,
simp [forget, *] at *,
end
-- Exercise (LONG): First, show that we have a Galois insertion.
lemma monotone_is_open {X : Type} :
monotone (forget : topological_space X → set (set X)) :=
begin
intros τ₁ τ₂,
intro h,
intros U hU,
exact h hU,
end
lemma monotone_span {X : Type} :
monotone (generate_from : set (set X) → topological_space X) :=
begin
intros C₁ C₂,
intro h,
intros U hU,
induction hU with V hV V W _ _ hV2 hW2 C _ hC,
{ apply generated_open.basic,
apply h, assumption },
{ apply generated_open.univ },
{ apply generated_open.inter, exact hV2, exact hW2},
{ apply generated_open.sUnion, exact hC },
end
lemma subset_forget {X : Type} (Us : set (set X)) :
Us ≤ forget (generate_from Us) :=
begin
intros U hU,
apply generated_open.basic,
assumption,
end
lemma generate_forget {X : Type} (τ : topological_space X) :
generate_from (forget τ) = τ :=
begin
ext U,
split,
{ intro hU,
induction hU with V hV V W _ _ hV2 hW2 C _ hC,
{ exact hV },
{ apply univ_mem },
{ apply inter _ _ hV2 hW2 },
{ apply union _ hC }},
{ -- easy way
intro hU,
apply generated_open.basic,
exact hU }
end
def gi_top (X : Type) :
galois_insertion (generate_from : set (set X) → topological_space X)
(forget : topological_space X → set (set X)) :=
galois_insertion.monotone_intro monotone_is_open monotone_span subset_forget generate_forget
/-
Then deduce that the type of topological space structures on X
is a complete lattice, i.e. that there is a good definition of
arbitrary Infs and Sups of topological space structures on a type, and
they satisfy all the correct properties of Infs and Sups. In
other words,
-/
example (X : Type) : complete_lattice (topological_space X) :=
galois_insertion.lift_complete_lattice (gi_top X)
end topological_space
|
20e17539037b88689222c48b3be5d6500e7e40ec | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Meta/Tactic/ElimInfo.lean | 85c9a10407d03588344ce9a8a1ded1226bb5790b | [
"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 | 6,611 | 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.Basic
import Lean.Meta.Check
import Lean.ScopedEnvExtension
namespace Lean.Meta
structure ElimAltInfo where
name : Name
numFields : Nat
deriving Repr, Inhabited
structure ElimInfo where
name : Name
motivePos : Nat
targetsPos : Array Nat := #[]
altsInfo : Array ElimAltInfo := #[]
deriving Repr, Inhabited
def getElimInfo (declName : Name) : MetaM ElimInfo := do
let declInfo ← getConstInfo declName
forallTelescopeReducing declInfo.type fun xs type => do
let motive := type.getAppFn
let targets := type.getAppArgs
unless motive.isFVar && targets.all (·.isFVar) && targets.size > 0 do
throwError "unexpected eliminator resulting type{indentExpr type}"
let motiveType ← inferType motive
forallTelescopeReducing motiveType fun motiveArgs motiveResultType => do
unless motiveArgs.size == targets.size do
throwError "unexpected number of arguments at motive type{indentExpr motiveType}"
unless motiveResultType.isSort do
throwError "motive result type must be a sort{indentExpr motiveType}"
let some motivePos ← pure (xs.indexOf? motive) |
throwError "unexpected eliminator type{indentExpr declInfo.type}"
let targetsPos ← targets.mapM fun target => do
match xs.indexOf? target with
| none => throwError "unexpected eliminator type{indentExpr declInfo.type}"
| some targetPos => pure targetPos.val
let mut altsInfo := #[]
for i in [:xs.size] do
let x := xs[i]!
if x != motive && !targets.contains x then
let xDecl ← x.fvarId!.getDecl
if xDecl.binderInfo.isExplicit then
let numFields ← forallTelescopeReducing xDecl.type fun args _ => pure args.size
altsInfo := altsInfo.push { name := xDecl.userName, numFields := numFields : ElimAltInfo }
pure { name := declName, motivePos := motivePos, targetsPos := targetsPos, altsInfo := altsInfo }
/--
Eliminators/recursors may have implicit targets. For builtin recursors, all indices are implicit targets.
Given an eliminator and the sequence of explicit targets, this methods returns a new sequence containing
implicit and explicit targets.
-/
partial def addImplicitTargets (elimInfo : ElimInfo) (targets : Array Expr) : MetaM (Array Expr) :=
withNewMCtxDepth do
let f ← mkConstWithFreshMVarLevels elimInfo.name
let targets ← collect (← inferType f) 0 0 #[]
let targets ← targets.mapM instantiateMVars
for target in targets do
if (← hasAssignableMVar target) then
throwError "failed to infer implicit target, it contains unresolved metavariables{indentExpr target}"
return targets
where
collect (type : Expr) (argIdx targetIdx : Nat) (targets' : Array Expr) : MetaM (Array Expr) := do
match (← whnfD type) with
| Expr.forallE _ d b bi =>
if elimInfo.targetsPos.contains argIdx then
if bi.isExplicit then
unless targetIdx < targets.size do
throwError "insufficient number of targets for '{elimInfo.name}'"
let target := targets[targetIdx]!
let targetType ← inferType target
unless (← isDefEq d targetType) do
throwError "target{indentExpr target}\n{← mkHasTypeButIsExpectedMsg targetType d}"
collect (b.instantiate1 target) (argIdx+1) (targetIdx+1) (targets'.push target)
else
let implicitTarget ← mkFreshExprMVar d
collect (b.instantiate1 implicitTarget) (argIdx+1) targetIdx (targets'.push implicitTarget)
else
collect (b.instantiate1 (← mkFreshExprMVar d)) (argIdx+1) targetIdx targets'
| _ =>
return targets'
structure CustomEliminator where
typeNames : Array Name
elimInfo : ElimInfo
deriving Inhabited, Repr
structure CustomEliminators where
map : SMap (Array Name) ElimInfo := {}
deriving Inhabited, Repr
def addCustomEliminatorEntry (es : CustomEliminators) (e : CustomEliminator) : CustomEliminators :=
match es with
| { map := map } => { map := map.insert e.typeNames e.elimInfo }
builtin_initialize customEliminatorExt : SimpleScopedEnvExtension CustomEliminator CustomEliminators ←
registerSimpleScopedEnvExtension {
name := `elimExt
initial := {}
addEntry := addCustomEliminatorEntry
finalizeImport := fun { map := map } => { map := map.switch }
}
def mkCustomEliminator (declName : Name) : MetaM CustomEliminator := do
let info ← getConstInfo declName
let elimInfo ← getElimInfo declName
forallTelescopeReducing info.type fun xs _ => do
let mut typeNames := #[]
for i in [:elimInfo.targetsPos.size] do
let targetPos := elimInfo.targetsPos[i]!
let x := xs[targetPos]!
/- Return true if there is another target that depends on `x`. -/
let isImplicitTarget : MetaM Bool := do
for j in [i+1:elimInfo.targetsPos.size] do
let y := xs[elimInfo.targetsPos[j]!]!
let yType ← inferType y
if (← dependsOn yType x.fvarId!) then
return true
return false
/- We should only use explicit targets when creating the key for the `CustomEliminators` map.
See test `tests/lean/run/eliminatorImplicitTargets.lean`. -/
unless (← isImplicitTarget) do
let xType ← inferType x
let .const typeName .. := xType.getAppFn | throwError "unexpected eliminator target type{indentExpr xType}"
typeNames := typeNames.push typeName
return { typeNames, elimInfo }
def addCustomEliminator (declName : Name) (attrKind : AttributeKind) : MetaM Unit := do
let e ← mkCustomEliminator declName
customEliminatorExt.add e attrKind
builtin_initialize
registerBuiltinAttribute {
name := `eliminator
descr := "custom eliminator for `cases` and `induction` tactics"
add := fun declName _ attrKind => do
discard <| addCustomEliminator declName attrKind |>.run {} {}
}
def getCustomEliminators : CoreM CustomEliminators := do
return customEliminatorExt.getState (← getEnv)
def getCustomEliminator? (targets : Array Expr) : MetaM (Option ElimInfo) := do
let mut key := #[]
for target in targets do
let targetType := (← instantiateMVars (← inferType target)).headBeta
let .const declName .. := targetType.getAppFn | return none
key := key.push declName
return customEliminatorExt.getState (← getEnv) |>.map.find? key
end Lean.Meta
|
ae37a731dfe39a615d5a31c2516c4714e51b2b00 | 8eeb99d0fdf8125f5d39a0ce8631653f588ee817 | /src/logic/basic.lean | 8765feae67266088a15a71b9cb1cdb1a2dc9fcde | [
"Apache-2.0"
] | permissive | jesse-michael-han/mathlib | a15c58378846011b003669354cbab7062b893cfe | fa6312e4dc971985e6b7708d99a5bc3062485c89 | refs/heads/master | 1,625,200,760,912 | 1,602,081,753,000 | 1,602,081,753,000 | 181,787,230 | 0 | 0 | null | 1,555,460,682,000 | 1,555,460,682,000 | null | UTF-8 | Lean | false | false | 49,001 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
import tactic.doc_commands
/-!
# Basic logic properties
This file is one of the earliest imports in mathlib.
## Implementation notes
Theorems that require decidability hypotheses are in the namespace "decidable".
Classical versions are in the namespace "classical".
In the presence of automation, this whole file may be unnecessary. On the other hand,
maybe it is useful for writing automation.
-/
local attribute [instance, priority 10] classical.prop_decidable
section miscellany
/- We add the `inline` attribute to optimize VM computation using these declarations. For example,
`if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/
attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable
decidable.true implies.decidable not.decidable ne.decidable
bool.decidable_eq decidable.to_bool
attribute [simp] cast_eq
variables {α : Type*} {β : Type*}
/-- An identity function with its main argument implicit. This will be printed as `hidden` even
if it is applied to a large term, so it can be used for elision,
as done in the `elide` and `unelide` tactics. -/
@[reducible] def hidden {α : Sort*} {a : α} := a
/-- Ex falso, the nondependent eliminator for the `empty` type. -/
def empty.elim {C : Sort*} : empty → C.
instance : subsingleton empty := ⟨λa, a.elim⟩
instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) :=
⟨by { intros a b, cases a, cases b, congr, }⟩
instance : decidable_eq empty := λa, a.elim
instance sort.inhabited : inhabited (Sort*) := ⟨punit⟩
instance sort.inhabited' : inhabited (default (Sort*)) := ⟨punit.star⟩
instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl (default _)⟩
instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr (default _)⟩
@[priority 10] instance decidable_eq_of_subsingleton
{α} [subsingleton α] : decidable_eq α
| a b := is_true (subsingleton.elim a b)
@[simp] lemma eq_iff_true_of_subsingleton [subsingleton α] (x y : α) :
x = y ↔ true :=
by cc
/-- Add an instance to "undo" coercion transitivity into a chain of coercions, because
most simp lemmas are stated with respect to simple coercions and will not match when
part of a chain. -/
@[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ]
(a : α) : (a : γ) = (a : β) := rfl
theorem coe_fn_coe_trans
{α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ]
(x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl
@[simp] theorem coe_fn_coe_base
{α β} [has_coe α β] [has_coe_to_fun β]
(x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl
theorem coe_sort_coe_trans
{α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ]
(x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl
/--
Many structures such as bundled morphisms coerce to functions so that you can
transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α`
then you can write `e a` and this is elaborated as `⇑e a`. This type of
coercion is implemented using the `has_coe_to_fun`type class. There is one
important consideration:
If a type coerces to another type which in turn coerces to a function,
then it **must** implement `has_coe_to_fun` directly:
```lean
structure sparkling_equiv (α β) extends α ≃ β
-- if we add a `has_coe` instance,
instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) :=
⟨sparkling_equiv.to_equiv⟩
-- then a `has_coe_to_fun` instance **must** be added as well:
instance {α β} : has_coe_to_fun (sparkling_equiv α β) :=
⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩
```
(Rationale: if we do not declare the direct coercion, then `⇑e a` is not in
simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This
often causes loops in the simplifier.)
-/
library_note "function coercion"
@[simp] theorem coe_sort_coe_base
{α β} [has_coe α β] [has_coe_to_sort β]
(x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl
/-- `pempty` is the universe-polymorphic analogue of `empty`. -/
@[derive decidable_eq]
inductive {u} pempty : Sort u
/-- Ex falso, the nondependent eliminator for the `pempty` type. -/
def pempty.elim {C : Sort*} : pempty → C.
instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩
@[simp] lemma not_nonempty_pempty : ¬ nonempty pempty :=
assume ⟨h⟩, h.elim
@[simp] theorem forall_pempty {P : pempty → Prop} : (∀ x : pempty, P x) ↔ true :=
⟨λ h, trivial, λ h x, by cases x⟩
@[simp] theorem exists_pempty {P : pempty → Prop} : (∃ x : pempty, P x) ↔ false :=
⟨λ h, by { cases h with w, cases w }, false.elim⟩
lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂
| a _ rfl := heq.rfl
lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b
| ⟨a⟩ ⟨b⟩ rfl := rfl
-- missing [symm] attribute for ne in core.
attribute [symm] ne.symm
lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩
@[simp] lemma eq_iff_eq_cancel_left {b c : α} :
(∀ {a}, a = b ↔ a = c) ↔ (b = c) :=
⟨λ h, by rw [← h], λ h a, by rw h⟩
@[simp] lemma eq_iff_eq_cancel_right {a b : α} :
(∀ {c}, a = c ↔ b = c) ↔ (a = b) :=
⟨λ h, by rw h, λ h a, by rw h⟩
/-- Wrapper for adding elementary propositions to the type class systems.
Warning: this can easily be abused. See the rest of this docstring for details.
Certain propositions should not be treated as a class globally,
but sometimes it is very convenient to be able to use the type class system
in specific circumstances.
For example, `zmod p` is a field if and only if `p` is a prime number.
In order to be able to find this field instance automatically by type class search,
we have to turn `p.prime` into an instance implicit assumption.
On the other hand, making `nat.prime` a class would require a major refactoring of the library,
and it is questionable whether making `nat.prime` a class is desirable at all.
The compromise is to add the assumption `[fact p.prime]` to `zmod.field`.
In particular, this class is not intended for turning the type class system
into an automated theorem prover for first order logic. -/
@[class]
def fact (p : Prop) := p
lemma fact.elim {p : Prop} (h : fact p) : p := h
end miscellany
/-!
### Declarations about propositional connectives
-/
theorem false_ne_true : false ≠ true
| h := h.symm ▸ trivial
section propositional
variables {a b c d : Prop}
/-! ### Declarations about `implies` -/
theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl
theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩
@[simp] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm
@[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id
theorem imp_intro {α β : Prop} (h : α) : β → α := λ _, h
theorem imp_false : (a → false) ↔ ¬ a := iff.rfl
theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) :=
⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩,
λ h ha, ⟨h.left ha, h.right ha⟩⟩
@[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) :=
iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb)
theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff_iff_implies_and_implies _ _
theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) :=
iff_def.trans and.comm
theorem imp_true_iff {α : Sort*} : (α → true) ↔ true :=
iff_true_intro $ λ_, trivial
@[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b :=
⟨λf, f ha, imp_intro⟩
/-! ### Declarations about `not` -/
/-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with
the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/
def not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1
@[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2
theorem not_not_of_not_imp : ¬(a → b) → ¬¬a :=
mt not.elim
theorem not_of_not_imp {a : Prop} : ¬(a → b) → ¬b :=
mt imp_intro
theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p
theorem em (p : Prop) : p ∨ ¬ p := classical.em _
theorem or_not {p : Prop} : p ∨ ¬ p := em _
theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction
-- alias by_contradiction ← by_contra
theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction
/--
In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely.
The `decidable` namespace contains versions of lemmas from the root namespace that explicitly
attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs.
You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if
`classical.choice` appears in the list.
-/
library_note "decidable namespace"
-- See Note [decidable namespace]
protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a :=
iff.intro decidable.by_contradiction not_not_intro
/-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`.
The left-to-right direction, double negation elimination (DNE),
is classically true but not constructively. -/
@[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not
theorem of_not_not : ¬¬a → a := by_contra
-- See Note [decidable namespace]
protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a :=
decidable.by_contradiction (not_not_of_not_imp h)
theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp
-- See Note [decidable namespace]
protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a :=
decidable.by_contradiction $ hb ∘ h
theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm
theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm
-- See Note [decidable namespace]
protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) :=
⟨not.decidable_imp_symm, not.decidable_imp_symm⟩
theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm
theorem imp.swap : (a → b → c) ↔ (b → a → c) :=
⟨function.swap, function.swap⟩
theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) :=
imp.swap
/-! ### Declarations about `and` -/
theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) :=
mt and.left
theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) :=
mt and.right
theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c :=
and.imp h id
theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b :=
and.imp id h
lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b :=
by simp [and.left_comm, and.comm]
lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a :=
by simp [and.left_comm, and.comm]
theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false :=
iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)
theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false :=
iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim
theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, h ha⟩)
theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b :=
iff.intro and.right (λ hb, ⟨h hb, hb⟩)
@[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) :=
⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩
@[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) :=
⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩
lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) :=
⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩
@[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b :=
⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩
@[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b :=
⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩
/-! ### Declarations about `or` -/
theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b]
theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d :=
or.imp h₂ h₃ h₁
theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c :=
or.imp_left h h₁
theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b :=
or.imp_right h h₁
theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d :=
or.elim h ha (assume h₂, or.elim h₂ hb hc)
theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) :=
⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩,
assume ⟨ha, hb⟩, or.rec ha hb⟩
-- See Note [decidable namespace]
protected theorem decidable.or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) :=
⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩
theorem or_iff_not_imp_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left
-- See Note [decidable namespace]
protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) :=
or.comm.trans decidable.or_iff_not_imp_left
theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right
-- See Note [decidable namespace]
protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) :=
⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩
theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not
/-! ### Declarations about distributivity -/
/-- `∧` distributes over `∨` (on the left). -/
theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) :=
⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha),
or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩
/-- `∧` distributes over `∨` (on the right). -/
theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) :=
(and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm)
/-- `∨` distributes over `∧` (on the left). -/
theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) :=
⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),
and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩
/-- `∨` distributes over `∧` (on the right). -/
theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) :=
(or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm)
@[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b :=
⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩
@[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b :=
⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩
/-! Declarations about `iff` -/
theorem iff_of_true (ha : a) (hb : b) : a ↔ b :=
⟨λ_, hb, λ _, ha⟩
theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b :=
⟨ha.elim, hb.elim⟩
theorem iff_true_left (ha : a) : (a ↔ b) ↔ b :=
⟨λ h, h.1 ha, iff_of_true ha⟩
theorem iff_true_right (ha : a) : (b ↔ a) ↔ b :=
iff.comm.trans (iff_true_left ha)
theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b :=
⟨λ h, mt h.2 ha, iff_of_false ha⟩
theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b :=
iff.comm.trans (iff_false_left ha)
-- See Note [decidable namespace]
protected theorem decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b :=
if ha : a then or.inr (h ha) else or.inl ha
theorem not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp
-- See Note [decidable namespace]
protected theorem decidable.imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) :=
⟨decidable.not_or_of_imp, or.neg_resolve_left⟩
theorem imp_iff_not_or : (a → b) ↔ (¬ a ∨ b) := decidable.imp_iff_not_or
-- See Note [decidable namespace]
protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by simp [decidable.imp_iff_not_or, or.comm, or.left_comm]
theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib
-- See Note [decidable namespace]
protected theorem decidable.imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)]
theorem imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib'
theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b)
| ⟨ha, hb⟩ h := hb $ h ha
-- See Note [decidable namespace]
protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b :=
⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩
theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp
-- for monotonicity
lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) :=
assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀
-- See Note [decidable namespace]
protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a :=
if ha : a then λ h, ha else λ h, h ha.elim
theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _
theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id
-- See Note [decidable namespace]
protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) :=
by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not
theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not
-- See Note [decidable namespace]
protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) :=
by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr decidable.not_imp_comm imp_not_comm
theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm
-- See Note [decidable namespace]
protected theorem decidable.not_iff [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) :=
by split; intro h; [split, skip]; intro h'; [by_contra, intro, skip];
try { refine h _; simp [*] }; rw [h', not_iff_self] at h; exact h
theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff
-- See Note [decidable namespace]
protected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) :=
by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm decidable.not_imp_comm
theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm
-- See Note [decidable namespace]
protected theorem decidable.iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) :=
by { split; intro h,
{ rw h; by_cases b; [left,right]; split; assumption },
{ cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }
theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) :=
decidable.iff_iff_and_or_not_and_not
lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] :
(a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) :=
begin
rw [iff_iff_implies_and_implies a b],
simp only [decidable.imp_iff_not_or, or.comm]
end
lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) :=
decidable.iff_iff_not_or_and_or_not
-- See Note [decidable namespace]
protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) :=
⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩
theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right
/-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent.
**Important**: this function should be used instead of `rw` on `decidable b`, because the
kernel will get stuck reducing the usage of `propext` otherwise,
and `dec_trivial` will not work. -/
@[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b :=
decidable_of_decidable_of_iff D h
/-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent.
This is the same as `decidable_of_iff` but the iff is flipped. -/
@[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a :=
decidable_of_decidable_of_iff D h.symm
/-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`.
(This is sometimes taken as an alternate definition of decidability.) -/
def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a
| tt h := is_true (h.1 rfl)
| ff h := is_false (mt h.2 bool.ff_ne_tt)
/-! ### De Morgan's laws -/
theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b)
| ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb)
-- See Note [decidable namespace]
protected theorem decidable.not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩
-- See Note [decidable namespace]
protected theorem decidable.not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩
/-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the
disjunction of the negations. -/
theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib
@[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp
theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a :=
not_and.trans imp_not_comm
/-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the
conjunction of the negations. -/
theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b :=
⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩,
λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩
-- See Note [decidable namespace]
protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) :=
by rw [← not_or_distrib, decidable.not_not]
theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not
-- See Note [decidable namespace]
protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) :=
by rw [← decidable.not_and_distrib, decidable.not_not]
theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not
end propositional
/-! ### Declarations about equality -/
section equality
variables {α : Sort*} {a b : α}
@[simp] theorem heq_iff_eq : a == b ↔ a = b :=
⟨eq_of_heq, heq_of_eq⟩
theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=
have p = q, from propext ⟨λ _, hq, λ _, hp⟩,
by subst q; refl
theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α}
(h : a ∈ s) : b ∉ s → a ≠ b :=
mt $ λ e, e ▸ h
theorem eq_equivalence : equivalence (@eq α) :=
⟨eq.refl, @eq.symm _, @eq.trans _⟩
/-- Transport through trivial families is the identity. -/
@[simp]
lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') :
(@eq.rec α a (λ a, β) y a' h) = y :=
by { cases h, refl, }
@[simp]
lemma eq_mp_rfl {α : Sort*} {a : α} : eq.mp (eq.refl α) a = a := rfl
@[simp]
lemma eq_mpr_rfl {α : Sort*} {a : α} : eq.mpr (eq.refl α) a = a := rfl
lemma heq_of_eq_mp :
∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a'
| α ._ a a' rfl h := eq.rec_on h (heq.refl _)
lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) :
@eq.rec α a C x b eq == y :=
by subst eq; exact h
@[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x :=
by subst h; refl
protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) :
(x₁ = x₂) ↔ (y₁ = y₂) :=
by { subst h₁, subst h₂ }
lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h]
lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h]
lemma congr_arg2 {α β γ : Type*} (f : α → β → γ) {x x' : α} {y y' : β}
(hx : x = x') (hy : y = y') : f x y = f x' y' :=
by { subst hx, subst hy }
end equality
/-! ### Declarations about quantifiers -/
section quantifiers
variables {α : Sort*} {β : Sort*} {p q : α → Prop} {b : Prop}
lemma forall_imp (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a :=
λ h' a, h a (h' a)
lemma Exists.imp (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists_imp_exists h p
lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a))
(hp : ∃ a, p a) : ∃ b, q b :=
exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩)
theorem forall_swap {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y :=
⟨function.swap, function.swap⟩
theorem exists_swap {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y :=
⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩
@[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b :=
⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩
/--
Extract an element from a existential statement, using `classical.some`.
-/
-- This enables projection notation.
@[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P
/--
Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`.
-/
lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P
--theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x :=
--forall_imp_of_exists_imp h
theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x :=
exists_imp_distrib.2 h
@[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x :=
exists_imp_distrib
theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x
| ⟨x, hn⟩ h := hn (h x)
-- See Note [decidable namespace]
protected theorem decidable.not_forall {p : α → Prop}
[decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x :=
⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩,
not_forall_of_exists_not⟩
@[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall
-- See Note [decidable namespace]
protected theorem decidable.not_forall_not [decidable (∃ x, p x)] :
(¬ ∀ x, ¬ p x) ↔ ∃ x, p x :=
(@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists
theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not
-- See Note [decidable namespace]
protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x :=
by simp [decidable.not_not]
@[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not
@[simp] theorem forall_true_iff : (α → true) ↔ true :=
iff_true_intro (λ _, trivial)
-- Unfortunately this causes simp to loop sometimes, so we
-- add the 2 and 3 cases as simp lemmas instead
theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true :=
iff_true_intro (λ _, of_iff_true (h _))
@[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true :=
forall_true_iff' $ λ _, forall_true_iff
@[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} :
(∀ a (b : β a), γ a b → true) ↔ true :=
forall_true_iff' $ λ _, forall_2_true_iff
@[simp] theorem forall_const (α : Sort*) [i : nonempty α] : (α → b) ↔ b :=
⟨i.elim, λ hb x, hb⟩
@[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b :=
⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩
theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) :=
⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩
theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) :=
⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩),
λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩
@[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} :
(∃x, q ∧ p x) ↔ q ∧ (∃x, p x) :=
⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩
@[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} :
(∃x, p x ∧ q) ↔ (∃x, p x) ∧ q :=
by simp [and_comm]
@[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' :=
⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩
@[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' :=
by simp [@eq_comm _ a']
-- this lemma is needed to simplify the output of `list.mem_cons_iff`
@[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a :=
by simp only [or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩
@[simp] theorem exists_eq' {a' : α} : ∃ a, a' = a := ⟨_, rfl⟩
@[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' :=
⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩
@[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' :=
(exists_congr $ by exact λ a, and.comm).trans exists_eq_left
@[simp] theorem exists_apply_eq_apply {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a = f a' :=
⟨a', rfl⟩
@[simp] theorem exists_apply_eq_apply' {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a' = f a :=
⟨a', rfl⟩
@[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} :
(∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) :=
⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩
@[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} :
(∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) :=
⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩
@[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} :
(∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) :=
⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩
@[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} :
(∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) :=
by { rw forall_swap, simp }
@[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} :
(∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) :=
by simp [@eq_comm _ _ (f _)]
@[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} :
(∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) :=
by { rw forall_swap, simp }
@[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' :=
by simp [@eq_comm _ a']
@[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' :=
by simp [@eq_comm _ a']
theorem exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b :=
⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩
theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x :=
h.imp_right $ λ h₂, h₂ x
-- See Note [decidable namespace]
protected theorem decidable.forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) :=
⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq,
forall_or_of_or_forall⟩
theorem forall_or_distrib_left {q : Prop} {p : α → Prop} :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left
-- See Note [decidable namespace]
protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] :
(∀x, p x ∨ q) ↔ (∀x, p x) ∨ q :=
by simp [or_comm, decidable.forall_or_distrib_left]
theorem forall_or_distrib_right {q : Prop} {p : α → Prop} :
(∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right
/-- A predicate holds everywhere on the image of a surjective functions iff
it holds everywhere. -/
theorem forall_iff_forall_surj
{α β : Type*} {f : α → β} (h : function.surjective f) {P : β → Prop} :
(∀ a, P (f a)) ↔ ∀ b, P b :=
⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, λ hb a, hb $ f a⟩
@[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩
@[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h
theorem Exists.fst {p : b → Prop} : Exists p → b
| ⟨h, _⟩ := h
theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst
| ⟨_, h⟩ := h
@[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h :=
@forall_const (q h) p ⟨h⟩
@[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h :=
@exists_const (q h) p ⟨h⟩
@[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true :=
iff_true_intro $ λ h, hn.elim h
@[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') :=
mt Exists.fst
lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x :=
exists.elim h (λ x hx, ⟨x, and.left hx⟩)
lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x)
{y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ :=
unique_of_exists_unique h py₁ py₂
@[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} :
(∃! x, p x) ↔ ∃ x, p x :=
⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩
lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)]
{q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h)
(h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b :=
begin
simp only [exists_unique_iff_exists] at h₂,
apply h₂.elim,
exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩)
end
lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)]
{q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp)
(H : ∀ y (hy : p y), q y hy → y = w) :
∃! x (hx : p x), q x hx :=
begin
simp only [exists_unique_iff_exists],
exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq)
end
lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop}
(h : ∃! x (hx : p x), q x hx) :
∃ x (hx : p x), q x hx :=
h.exists.imp (λ x hx, hx.exists)
lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)]
{q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx)
{y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁)
(hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ :=
begin
simp only [exists_unique_iff_exists] at h,
exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩
end
end quantifiers
/-! ### Classical lemmas -/
namespace classical
variables {α : Sort*} {p : α → Prop}
theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a :=
assume a, cases_on a h1 h2
/- use shortened names to avoid conflict when classical namespace is open. -/
noncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma]
by apply_instance
noncomputable lemma dec_pred (p : α → Prop) : decidable_pred p := -- see Note [classical lemma]
by apply_instance
noncomputable lemma dec_rel (p : α → α → Prop) : decidable_rel p := -- see Note [classical lemma]
by apply_instance
noncomputable lemma dec_eq (α : Sort*) : decidable_eq α := -- see Note [classical lemma]
by apply_instance
/--
We make decidability results that depends on `classical.choice` noncomputable lemmas.
* We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode
for them, and fail because it depends on `classical.choice`.
* We make them lemmas, and not definitions, because otherwise later definitions will raise
\"failed to generate bytecode\" errors when writing something like
`letI := classical.dec_eq _`.
Cf. <https://leanprover-community.github.io/archive/113488general/08268noncomputabletheorem.html>
-/
library_note "classical lemma"
/-- Construct a function from a default value `H0`, and a function to use if there exists a value
satisfying the predicate. -/
@[elab_as_eliminator]
noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C :=
if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0
lemma some_spec2 {α : Sort*} {p : α → Prop} {h : ∃a, p a}
(q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) :=
hpq _ $ some_spec _
/-- A version of classical.indefinite_description which is definitionally equal to a pair -/
noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} :=
⟨classical.some h, classical.some_spec h⟩
end classical
/-- This function has the same type as `exists.rec_on`, and can be used to case on an equality,
but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe
using the axiom of choice. -/
@[elab_as_eliminator]
noncomputable def {u} exists.classical_rec_on
{α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C :=
H (classical.some h) (classical.some_spec h)
/-! ### Declarations about bounded quantifiers -/
section bounded_quantifiers
variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop}
theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x :=
⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩
theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b
| ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂
theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h :=
⟨a, h₁, h₂⟩
theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) :
(∀ x h, P x h) ↔ (∀ x h, Q x h) :=
forall_congr $ λ x, forall_congr (H x)
theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) :
(∃ x h, P x h) ↔ (∃ x h, Q x h) :=
exists_congr $ λ x, exists_congr (H x)
theorem ball.imp_right (H : ∀ x h, (P x h → Q x h))
(h₁ : ∀ x h, P x h) (x h) : Q x h :=
H _ _ $ h₁ _ _
theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) :
(∃ x h, P x h) → ∃ x h, Q x h
| ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩
theorem ball.imp_left (H : ∀ x, p x → q x)
(h₁ : ∀ x, q x → r x) (x) (h : p x) : r x :=
h₁ _ $ H _ h
theorem bex.imp_left (H : ∀ x, p x → q x) :
(∃ x (_ : p x), r x) → ∃ x (_ : q x), r x
| ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩
theorem ball_of_forall (h : ∀ x, p x) (x) : p x :=
h x
theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x :=
h x $ H x
theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x
| ⟨x, hq⟩ := ⟨x, H x, hq⟩
theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x
| ⟨x, _, hq⟩ := ⟨x, hq⟩
@[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) :=
by simp
theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h :=
bex_imp_distrib
theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h
| ⟨x, h, hp⟩ al := hp $ al x h
-- See Note [decidable namespace]
protected theorem decidable.not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) :=
⟨not.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩,
not_ball_of_bex_not⟩
theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball
theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true :=
iff_true_intro (λ h hrx, trivial)
theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) :=
iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib
theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) :=
iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib
end bounded_quantifiers
namespace classical
local attribute [instance] prop_decidable
theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball
end classical
lemma ite_eq_iff {α} {p : Prop} [decidable p] {a b c : α} :
(if p then a else b) = c ↔ p ∧ a = c ∨ ¬p ∧ b = c :=
by by_cases p; simp *
/-! ### Declarations about `nonempty` -/
section nonempty
universe variables u v w
variables {α : Type u} {β : Type v} {γ : α → Type w}
attribute [simp] nonempty_of_inhabited
@[priority 20]
instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩
@[priority 20]
instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩
lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α :=
iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩)
@[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p :=
iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩)
lemma not_nonempty_iff_imp_false : ¬ nonempty α ↔ α → false :=
⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩
@[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) :=
iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩)
@[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} :
nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end)
@[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} :
nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end)
@[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} :
nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_empty : ¬ nonempty empty :=
assume ⟨h⟩, h.elim
@[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} :
(∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) :=
iff.intro (assume h a, h _) (assume h ⟨a⟩, h _)
@[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} :
(∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) :=
iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩)
lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} :
nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) :=
iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩)
/-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued)
`inhabited` instance. `classical.inhabited_of_nonempty` already exists, in
`core/init/classical.lean`, but the assumption is not a type class argument,
which makes it unsuitable for some applications. -/
noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α :=
⟨classical.choice h⟩
/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/
@[reducible] protected noncomputable def nonempty.some {α : Sort u} (h : nonempty α) : α :=
classical.choice h
/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/
@[reducible] protected noncomputable def classical.arbitrary (α : Sort u) [h : nonempty α] : α :=
classical.choice h
/-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty.
`nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/
lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β
| ⟨h⟩ := ⟨f h⟩
protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ
| ⟨x⟩ ⟨y⟩ := ⟨f x y⟩
protected lemma nonempty.congr {α : Sort u} {β : Sort v} (f : α → β) (g : β → α) :
nonempty α ↔ nonempty β :=
⟨nonempty.map f, nonempty.map g⟩
lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop}
(f : inhabited α → p) : p :=
h.elim $ f ∘ inhabited.mk
instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) :=
h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩
end nonempty
section ite
/-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/
lemma apply_dite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x : P → α) (y : ¬P → α) :
f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) :=
by { by_cases h : P; simp [h] }
/-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/
lemma apply_ite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x y : α) :
f (ite P x y) = ite P (f x) (f y) :=
apply_dite f P (λ _, x) (λ _, y)
/-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function
applied to each of the branches. -/
lemma apply_dite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a : P → α)
(b : ¬P → α) (c : P → β) (d : ¬P → β) :
f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) :=
by { by_cases h : P; simp [h] }
/-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function
applied to each of the branches. -/
lemma apply_ite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) :
f (ite P a b) (ite P c d) = ite P (f a c) (f b d) :=
apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d)
/-- A 'dite' producing a `Pi` type `Π a, β a`, applied to a value `x : α`
is a `dite` that applies either branch to `x`. -/
lemma dite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P]
(f : P → Π a, β a) (g : ¬ P → Π a, β a) (x : α) :
(dite P f g) x = dite P (λ h, f h x) (λ h, g h x) :=
by { by_cases h : P; simp [h] }
/-- A 'ite' producing a `Pi` type `Π a, β a`, applied to a value `x : α`
is a `ite` that applies either branch to `x` -/
lemma ite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P]
(f g : Π a, β a) (x : α) :
(ite P f g) x = ite P (f x) (g x) :=
dite_apply P (λ _, f) (λ _, g) x
/-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/
@[simp] lemma dite_not {α : Sort*} (P : Prop) [decidable P] (x : ¬ P → α) (y : ¬¬ P → α) :
dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x :=
by { by_cases h : P; simp [h] }
/-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/
@[simp] lemma ite_not {α : Sort*} (P : Prop) [decidable P] (x y : α) :
ite (¬ P) x y = ite P y x :=
dite_not P (λ _, x) (λ _, y)
end ite
|
3891a0625ade35c246a5185841775e25f409d139 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/group_theory/coset.lean | 01fc5e6c7e3e571f30cc487996121353efb90db4 | [
"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 | 27,302 | lean | /-
Copyright (c) 2018 Mitchell Rowett. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mitchell Rowett, Scott Morrison
-/
import algebra.quotient
import data.fintype.prod
import group_theory.group_action.basic
import group_theory.subgroup.mul_opposite
import tactic.group
/-!
# Cosets
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file develops the basic theory of left and right cosets.
## Main definitions
* `left_coset a s`: the left coset `a * s` for an element `a : α` and a subset `s ⊆ α`, for an
`add_group` this is `left_add_coset a s`.
* `right_coset s a`: the right coset `s * a` for an element `a : α` and a subset `s ⊆ α`, for an
`add_group` this is `right_add_coset s a`.
* `quotient_group.quotient s`: the quotient type representing the left cosets with respect to a
subgroup `s`, for an `add_group` this is `quotient_add_group.quotient s`.
* `quotient_group.mk`: the canonical map from `α` to `α/s` for a subgroup `s` of `α`, for an
`add_group` this is `quotient_add_group.mk`.
* `subgroup.left_coset_equiv_subgroup`: the natural bijection between a left coset and the subgroup,
for an `add_group` this is `add_subgroup.left_coset_equiv_add_subgroup`.
## Notation
* `a *l s`: for `left_coset a s`.
* `a +l s`: for `left_add_coset a s`.
* `s *r a`: for `right_coset s a`.
* `s +r a`: for `right_add_coset s a`.
* `G ⧸ H` is the quotient of the (additive) group `G` by the (additive) subgroup `H`
-/
open set function
variable {α : Type*}
/-- The left coset `a * s` for an element `a : α` and a subset `s : set α` -/
@[to_additive left_add_coset "The left coset `a+s` for an element `a : α`
and a subset `s : set α`"]
def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s
/-- The right coset `s * a` for an element `a : α` and a subset `s : set α` -/
@[to_additive right_add_coset "The right coset `s+a` for an element `a : α`
and a subset `s : set α`"]
def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s
localized "infix (name := left_coset) ` *l `:70 := left_coset" in coset
localized "infix (name := left_add_coset) ` +l `:70 := left_add_coset" in coset
localized "infix (name := right_coset) ` *r `:70 := right_coset" in coset
localized "infix (name := right_add_coset) ` +r `:70 := right_add_coset" in coset
section coset_mul
variable [has_mul α]
@[to_additive mem_left_add_coset]
lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s :=
mem_image_of_mem (λ b : α, a * b) hxS
@[to_additive mem_right_add_coset]
lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a :=
mem_image_of_mem (λ b : α, b * a) hxS
/-- Equality of two left cosets `a * s` and `b * s`. -/
@[to_additive left_add_coset_equivalence "Equality of two left cosets `a + s` and `b + s`."]
def left_coset_equivalence (s : set α) (a b : α) := a *l s = b *l s
@[to_additive left_add_coset_equivalence_rel]
lemma left_coset_equivalence_rel (s : set α) : equivalence (left_coset_equivalence s) :=
mk_equivalence (left_coset_equivalence s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans)
/-- Equality of two right cosets `s * a` and `s * b`. -/
@[to_additive right_add_coset_equivalence "Equality of two right cosets `s + a` and `s + b`."]
def right_coset_equivalence (s : set α) (a b : α) := s *r a = s *r b
@[to_additive right_add_coset_equivalence_rel]
lemma right_coset_equivalence_rel (s : set α) : equivalence (right_coset_equivalence s) :=
mk_equivalence (right_coset_equivalence s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans)
end coset_mul
section coset_semigroup
variable [semigroup α]
@[simp, to_additive left_add_coset_assoc] lemma left_coset_assoc (s : set α) (a b : α) :
a *l (b *l s) = (a * b) *l s :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
@[simp, to_additive right_add_coset_assoc] lemma right_coset_assoc (s : set α) (a b : α) :
s *r a *r b = s *r (a * b) :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
@[to_additive left_add_coset_right_add_coset]
lemma left_coset_right_coset (s : set α) (a b : α) : a *l s *r b = a *l (s *r b) :=
by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc]
end coset_semigroup
section coset_monoid
variables [monoid α] (s : set α)
@[simp, to_additive zero_left_add_coset] lemma one_left_coset : 1 *l s = s :=
set.ext $ by simp [left_coset]
@[simp, to_additive right_add_coset_zero] lemma right_coset_one : s *r 1 = s :=
set.ext $ by simp [right_coset]
end coset_monoid
section coset_submonoid
open submonoid
variables [monoid α] (s : submonoid α)
@[to_additive mem_own_left_add_coset]
lemma mem_own_left_coset (a : α) : a ∈ a *l s :=
suffices a * 1 ∈ a *l s, by simpa,
mem_left_coset a (one_mem s : 1 ∈ s)
@[to_additive mem_own_right_add_coset]
lemma mem_own_right_coset (a : α) : a ∈ (s : set α) *r a :=
suffices 1 * a ∈ (s : set α) *r a, by simpa,
mem_right_coset a (one_mem s : 1 ∈ s)
@[to_additive mem_left_add_coset_left_add_coset]
lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s :=
by rw [←set_like.mem_coe, ←ha]; exact mem_own_left_coset s a
@[to_additive mem_right_add_coset_right_add_coset]
lemma mem_right_coset_right_coset {a : α} (ha : (s : set α) *r a = s) : a ∈ s :=
by rw [←set_like.mem_coe, ←ha]; exact mem_own_right_coset s a
end coset_submonoid
section coset_group
variables [group α] {s : set α} {x : α}
@[to_additive mem_left_add_coset_iff]
lemma mem_left_coset_iff (a : α) : x ∈ a *l s ↔ a⁻¹ * x ∈ s :=
iff.intro
(assume ⟨b, hb, eq⟩, by simp [eq.symm, hb])
(assume h, ⟨a⁻¹ * x, h, by simp⟩)
@[to_additive mem_right_add_coset_iff]
lemma mem_right_coset_iff (a : α) : x ∈ s *r a ↔ x * a⁻¹ ∈ s :=
iff.intro
(assume ⟨b, hb, eq⟩, by simp [eq.symm, hb])
(assume h, ⟨x * a⁻¹, h, by simp⟩)
end coset_group
section coset_subgroup
open subgroup
variables [group α] (s : subgroup α)
@[to_additive left_add_coset_mem_left_add_coset]
lemma left_coset_mem_left_coset {a : α} (ha : a ∈ s) : a *l s = s :=
set.ext $ by simp [mem_left_coset_iff, mul_mem_cancel_left (s.inv_mem ha)]
@[to_additive right_add_coset_mem_right_add_coset]
lemma right_coset_mem_right_coset {a : α} (ha : a ∈ s) : (s : set α) *r a = s :=
set.ext $ assume b, by simp [mem_right_coset_iff, mul_mem_cancel_right (s.inv_mem ha)]
@[to_additive] lemma orbit_subgroup_eq_right_coset (a : α) : mul_action.orbit s a = s *r a :=
set.ext (λ b, ⟨λ ⟨c, d⟩, ⟨c, c.2, d⟩, λ ⟨c, d, e⟩, ⟨⟨c, d⟩, e⟩⟩)
@[to_additive] lemma orbit_subgroup_eq_self_of_mem {a : α} (ha : a ∈ s) :
mul_action.orbit s a = s :=
(orbit_subgroup_eq_right_coset s a).trans (right_coset_mem_right_coset s ha)
@[to_additive] lemma orbit_subgroup_one_eq_self : mul_action.orbit s (1 : α) = s :=
orbit_subgroup_eq_self_of_mem s s.one_mem
@[to_additive eq_add_cosets_of_normal]
theorem eq_cosets_of_normal (N : s.normal) (g : α) : g *l s = s *r g :=
set.ext $ assume a, by simp [mem_left_coset_iff, mem_right_coset_iff]; rw [N.mem_comm_iff]
@[to_additive normal_of_eq_add_cosets]
theorem normal_of_eq_cosets (h : ∀ g : α, g *l s = s *r g) : s.normal :=
⟨assume a ha g, show g * a * g⁻¹ ∈ (s : set α),
by rw [← mem_right_coset_iff, ← h]; exact mem_left_coset g ha⟩
@[to_additive normal_iff_eq_add_cosets]
theorem normal_iff_eq_cosets : s.normal ↔ ∀ g : α, g *l s = s *r g :=
⟨@eq_cosets_of_normal _ _ s, normal_of_eq_cosets s⟩
@[to_additive left_add_coset_eq_iff]
lemma left_coset_eq_iff {x y : α} : left_coset x s = left_coset y s ↔ x⁻¹ * y ∈ s :=
begin
rw set.ext_iff,
simp_rw [mem_left_coset_iff, set_like.mem_coe],
split,
{ intro h, apply (h y).mpr, rw mul_left_inv, exact s.one_mem },
{ intros h z, rw ←mul_inv_cancel_right x⁻¹ y, rw mul_assoc, exact s.mul_mem_cancel_left h },
end
@[to_additive right_add_coset_eq_iff]
lemma right_coset_eq_iff {x y : α} : right_coset ↑s x = right_coset s y ↔ y * x⁻¹ ∈ s :=
begin
rw set.ext_iff,
simp_rw [mem_right_coset_iff, set_like.mem_coe],
split,
{ intro h, apply (h y).mpr, rw mul_right_inv, exact s.one_mem },
{ intros h z, rw ←inv_mul_cancel_left y x⁻¹, rw ←mul_assoc, exact s.mul_mem_cancel_right h },
end
end coset_subgroup
run_cmd to_additive.map_namespace `quotient_group `quotient_add_group
namespace quotient_group
variables [group α] (s : subgroup α)
/-- The equivalence relation corresponding to the partition of a group by left cosets
of a subgroup.-/
@[to_additive "The equivalence relation corresponding to the partition of a group by left cosets
of a subgroup."]
def left_rel : setoid α := mul_action.orbit_rel s.opposite α
variables {s}
@[to_additive]
lemma left_rel_apply {x y : α} : @setoid.r _ (left_rel s) x y ↔ (x⁻¹ * y ∈ s) :=
calc (∃ a : s.opposite, y * mul_opposite.unop a = x)
↔ ∃ a : s, y * a = x : s.opposite_equiv.symm.exists_congr_left
... ↔ ∃ a : s, x⁻¹ * y = a⁻¹ : by simp only [inv_mul_eq_iff_eq_mul, eq_mul_inv_iff_mul_eq]
... ↔ x⁻¹ * y ∈ s : by simp [set_like.exists]
variables (s)
@[to_additive]
lemma left_rel_eq : @setoid.r _ (left_rel s) = λ x y, x⁻¹ * y ∈ s :=
funext₂ $ by { simp only [eq_iff_iff], apply left_rel_apply }
lemma left_rel_r_eq_left_coset_equivalence :
@setoid.r _ (quotient_group.left_rel s) = left_coset_equivalence s :=
by { ext, rw left_rel_eq, exact (left_coset_eq_iff s).symm }
@[to_additive]
instance left_rel_decidable [decidable_pred (∈ s)] :
decidable_rel (left_rel s).r :=
λ x y, by { rw left_rel_eq, exact ‹decidable_pred (∈ s)› _ }
/-- `α ⧸ s` is the quotient type representing the left cosets of `s`.
If `s` is a normal subgroup, `α ⧸ s` is a group -/
@[to_additive "`α ⧸ s` is the quotient type representing the left cosets of `s`. If `s` is a
normal subgroup, `α ⧸ s` is a group"]
instance : has_quotient α (subgroup α) := ⟨λ s, quotient (left_rel s)⟩
/-- The equivalence relation corresponding to the partition of a group by right cosets of a
subgroup. -/
@[to_additive "The equivalence relation corresponding to the partition of a group by right cosets of
a subgroup."]
def right_rel : setoid α := mul_action.orbit_rel s α
variables {s}
@[to_additive]
lemma right_rel_apply {x y : α} : @setoid.r _ (right_rel s) x y ↔ (y * x⁻¹ ∈ s) :=
calc (∃ a : s, (a:α) * y = x)
↔ ∃ a : s, y * x⁻¹ = a⁻¹ : by simp only [mul_inv_eq_iff_eq_mul, eq_inv_mul_iff_mul_eq]
... ↔ y * x⁻¹ ∈ s : by simp [set_like.exists]
variables (s)
@[to_additive]
lemma right_rel_eq : @setoid.r _ (right_rel s) = λ x y, y * x⁻¹ ∈ s :=
funext₂ $ by { simp only [eq_iff_iff], apply right_rel_apply }
lemma right_rel_r_eq_right_coset_equivalence :
@setoid.r _ (quotient_group.right_rel s) = right_coset_equivalence s :=
by { ext, rw right_rel_eq, exact (right_coset_eq_iff s).symm }
@[to_additive]
instance right_rel_decidable [decidable_pred (∈ s)] :
decidable_rel (right_rel s).r :=
λ x y, by { rw right_rel_eq, exact ‹decidable_pred (∈ s)› _ }
/-- Right cosets are in bijection with left cosets. -/
@[to_additive "Right cosets are in bijection with left cosets."]
def quotient_right_rel_equiv_quotient_left_rel : quotient (quotient_group.right_rel s) ≃ α ⧸ s :=
{ to_fun := quotient.map' (λ g, g⁻¹) (λ a b, by { rw [left_rel_apply, right_rel_apply],
exact λ h, (congr_arg (∈ s) (by group)).mp (s.inv_mem h) }),
inv_fun := quotient.map' (λ g, g⁻¹) (λ a b, by { rw [left_rel_apply, right_rel_apply],
exact λ h, (congr_arg (∈ s) (by group)).mp (s.inv_mem h) }),
left_inv := λ g, quotient.induction_on' g (λ g, quotient.sound' (by
{ simp only [inv_inv],
exact quotient.exact' rfl })),
right_inv := λ g, quotient.induction_on' g (λ g, quotient.sound' (by
{ simp only [inv_inv],
exact quotient.exact' rfl })) }
@[to_additive] instance fintype_quotient_right_rel [fintype (α ⧸ s)] :
fintype (quotient (quotient_group.right_rel s)) :=
fintype.of_equiv (α ⧸ s) (quotient_group.quotient_right_rel_equiv_quotient_left_rel s).symm
@[to_additive] lemma card_quotient_right_rel [fintype (α ⧸ s)] :
fintype.card (quotient (quotient_group.right_rel s)) = fintype.card (α ⧸ s) :=
fintype.of_equiv_card (quotient_group.quotient_right_rel_equiv_quotient_left_rel s).symm
end quotient_group
namespace quotient_group
variables [group α] {s : subgroup α}
@[to_additive]
instance fintype [fintype α] (s : subgroup α) [decidable_rel (left_rel s).r] :
fintype (α ⧸ s) :=
quotient.fintype (left_rel s)
/-- The canonical map from a group `α` to the quotient `α ⧸ s`. -/
@[to_additive "The canonical map from an `add_group` `α` to the quotient `α ⧸ s`."]
abbreviation mk (a : α) : α ⧸ s :=
quotient.mk' a
@[to_additive]
lemma mk_surjective : function.surjective $ @mk _ _ s := quotient.surjective_quotient_mk'
@[elab_as_eliminator, to_additive]
lemma induction_on {C : α ⧸ s → Prop} (x : α ⧸ s)
(H : ∀ z, C (quotient_group.mk z)) : C x :=
quotient.induction_on' x H
@[to_additive]
instance : has_coe_t α (α ⧸ s) := ⟨mk⟩ -- note [use has_coe_t]
@[elab_as_eliminator, to_additive]
lemma induction_on' {C : α ⧸ s → Prop} (x : α ⧸ s)
(H : ∀ z : α, C z) : C x :=
quotient.induction_on' x H
@[simp, to_additive]
lemma quotient_lift_on_coe {β} (f : α → β) (h) (x : α) :
quotient.lift_on' (x : α ⧸ s) f h = f x := rfl
@[to_additive] lemma forall_coe {C : α ⧸ s → Prop} : (∀ x : α ⧸ s, C x) ↔ ∀ x : α, C x :=
mk_surjective.forall
@[to_additive] lemma exists_coe {C : α ⧸ s → Prop} : (∃ x : α ⧸ s, C x) ↔ ∃ x : α, C x :=
mk_surjective.exists
@[to_additive]
instance (s : subgroup α) : inhabited (α ⧸ s) :=
⟨((1 : α) : α ⧸ s)⟩
@[to_additive quotient_add_group.eq]
protected lemma eq {a b : α} : (a : α ⧸ s) = b ↔ a⁻¹ * b ∈ s :=
calc _ ↔ @setoid.r _ (left_rel s) a b : quotient.eq'
... ↔ _ : by rw left_rel_apply
@[to_additive quotient_add_group.eq']
lemma eq' {a b : α} : (mk a : α ⧸ s) = mk b ↔ a⁻¹ * b ∈ s :=
quotient_group.eq
@[simp, to_additive quotient_add_group.out_eq']
lemma out_eq' (a : α ⧸ s) : mk a.out' = a :=
quotient.out_eq' a
variables (s)
/- It can be useful to write `obtain ⟨h, H⟩ := mk_out'_eq_mul ...`, and then `rw [H]` or
`simp_rw [H]` or `simp only [H]`. In order for `simp_rw` and `simp only` to work, this lemma is
stated in terms of an arbitrary `h : s`, rathern that the specific `h = g⁻¹ * (mk g).out'`. -/
@[to_additive quotient_add_group.mk_out'_eq_mul]
lemma mk_out'_eq_mul (g : α) : ∃ h : s, (mk g : α ⧸ s).out' = g * h :=
⟨⟨g⁻¹ * (mk g).out', eq'.mp (mk g).out_eq'.symm⟩, by rw [set_like.coe_mk, mul_inv_cancel_left]⟩
variables {s} {a b : α}
@[simp, to_additive quotient_add_group.mk_add_of_mem]
lemma mk_mul_of_mem (a : α) (hb : b ∈ s) : (mk (a * b) : α ⧸ s) = mk a :=
by rwa [eq', mul_inv_rev, inv_mul_cancel_right, s.inv_mem_iff]
@[to_additive]
lemma eq_class_eq_left_coset (s : subgroup α) (g : α) :
{x : α | (x : α ⧸ s) = g} = left_coset g s :=
set.ext $ λ z,
by rw [mem_left_coset_iff, set.mem_set_of_eq, eq_comm, quotient_group.eq, set_like.mem_coe]
@[to_additive]
lemma preimage_image_coe (N : subgroup α) (s : set α) :
coe ⁻¹' ((coe : α → α ⧸ N) '' s) = ⋃ x : N, (λ y : α, y * x) ⁻¹' s :=
begin
ext x,
simp only [quotient_group.eq, set_like.exists, exists_prop, set.mem_preimage, set.mem_Union,
set.mem_image, set_like.coe_mk, ← eq_inv_mul_iff_mul_eq],
exact ⟨λ ⟨y, hs, hN⟩, ⟨_, N.inv_mem hN, by simpa using hs⟩,
λ ⟨z, hz, hxz⟩, ⟨x*z, hxz, by simpa using hz⟩⟩,
end
end quotient_group
namespace subgroup
open quotient_group
variables [group α] {s : subgroup α}
/-- The natural bijection between a left coset `g * s` and `s`. -/
@[to_additive "The natural bijection between the cosets `g + s` and `s`."]
def left_coset_equiv_subgroup (g : α) : left_coset g s ≃ s :=
⟨λ x, ⟨g⁻¹ * x.1, (mem_left_coset_iff _).1 x.2⟩,
λ x, ⟨g * x.1, x.1, x.2, rfl⟩,
λ ⟨x, hx⟩, subtype.eq $ by simp,
λ ⟨g, hg⟩, subtype.eq $ by simp⟩
/-- The natural bijection between a right coset `s * g` and `s`. -/
@[to_additive "The natural bijection between the cosets `s + g` and `s`."]
def right_coset_equiv_subgroup (g : α) : right_coset ↑s g ≃ s :=
⟨λ x, ⟨x.1 * g⁻¹, (mem_right_coset_iff _).1 x.2⟩,
λ x, ⟨x.1 * g, x.1, x.2, rfl⟩,
λ ⟨x, hx⟩, subtype.eq $ by simp,
λ ⟨g, hg⟩, subtype.eq $ by simp⟩
/-- A (non-canonical) bijection between a group `α` and the product `(α/s) × s` -/
@[to_additive "A (non-canonical) bijection between an add_group `α` and the product `(α/s) × s`"]
noncomputable def group_equiv_quotient_times_subgroup :
α ≃ (α ⧸ s) × s :=
calc α ≃ Σ L : α ⧸ s, {x : α // (x : α ⧸ s) = L} :
(equiv.sigma_fiber_equiv quotient_group.mk).symm
... ≃ Σ L : α ⧸ s, left_coset (quotient.out' L) s :
equiv.sigma_congr_right (λ L,
begin
rw ← eq_class_eq_left_coset,
show _root_.subtype (λ x : α, quotient.mk' x = L) ≃
_root_.subtype (λ x : α, quotient.mk' x = quotient.mk' _),
simp [-quotient.eq'],
end)
... ≃ Σ L : α ⧸ s, s :
equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _)
... ≃ (α ⧸ s) × s :
equiv.sigma_equiv_prod _ _
variables {t : subgroup α}
/-- If two subgroups `M` and `N` of `G` are equal, their quotients are in bijection. -/
@[to_additive "If two subgroups `M` and `N` of `G` are equal, their quotients are in bijection."]
def quotient_equiv_of_eq (h : s = t) : α ⧸ s ≃ α ⧸ t :=
{ to_fun := quotient.map' id (λ a b h', h ▸ h'),
inv_fun := quotient.map' id (λ a b h', h.symm ▸ h'),
left_inv := λ q, induction_on' q (λ g, rfl),
right_inv := λ q, induction_on' q (λ g, rfl) }
lemma quotient_equiv_of_eq_mk (h : s = t) (a : α) :
quotient_equiv_of_eq h (quotient_group.mk a) = (quotient_group.mk a) :=
rfl
/-- If `H ≤ K`, then `G/H ≃ G/K × K/H` constructively, using the provided right inverse
of the quotient map `G → G/K`. The classical version is `quotient_equiv_prod_of_le`. -/
@[to_additive "If `H ≤ K`, then `G/H ≃ G/K × K/H` constructively, using the provided right inverse
of the quotient map `G → G/K`. The classical version is `quotient_equiv_prod_of_le`.", simps]
def quotient_equiv_prod_of_le' (h_le : s ≤ t)
(f : α ⧸ t → α) (hf : function.right_inverse f quotient_group.mk) :
α ⧸ s ≃ (α ⧸ t) × (t ⧸ s.subgroup_of t) :=
{ to_fun := λ a, ⟨a.map' id (λ b c h, left_rel_apply.mpr (h_le (left_rel_apply.mp h))),
a.map' (λ g : α, ⟨(f (quotient.mk' g))⁻¹ * g, left_rel_apply.mp (quotient.exact' (hf g))⟩)
(λ b c h, by
{ rw left_rel_apply,
change ((f b)⁻¹ * b)⁻¹ * ((f c)⁻¹ * c) ∈ s,
have key : f b = f c :=
congr_arg f (quotient.sound' (left_rel_apply.mpr (h_le (left_rel_apply.mp h)))),
rwa [key, mul_inv_rev, inv_inv, mul_assoc, mul_inv_cancel_left, ← left_rel_apply] })⟩,
inv_fun := λ a, a.2.map' (λ b, f a.1 * b) (λ b c h, by
{ rw left_rel_apply at ⊢ h,
change (f a.1 * b)⁻¹ * (f a.1 * c) ∈ s,
rwa [mul_inv_rev, mul_assoc, inv_mul_cancel_left] }),
left_inv := by
{ refine quotient.ind' (λ a, _),
simp_rw [quotient.map'_mk', id.def, set_like.coe_mk, mul_inv_cancel_left] },
right_inv := by
{ refine prod.rec _,
refine quotient.ind' (λ a, _),
refine quotient.ind' (λ b, _),
have key : quotient.mk' (f (quotient.mk' a) * b) = quotient.mk' a :=
(quotient_group.mk_mul_of_mem (f a) b.2).trans (hf a),
simp_rw [quotient.map'_mk', id.def, key, inv_mul_cancel_left, subtype.coe_eta] } }
/-- If `H ≤ K`, then `G/H ≃ G/K × K/H` nonconstructively.
The constructive version is `quotient_equiv_prod_of_le'`. -/
@[to_additive "If `H ≤ K`, then `G/H ≃ G/K × K/H` nonconstructively.
The constructive version is `quotient_equiv_prod_of_le'`.", simps]
noncomputable def quotient_equiv_prod_of_le (h_le : s ≤ t) :
α ⧸ s ≃ (α ⧸ t) × (t ⧸ s.subgroup_of t) :=
quotient_equiv_prod_of_le' h_le quotient.out' quotient.out_eq'
/-- If `s ≤ t`, then there is an embedding `s ⧸ H.subgroup_of s ↪ t ⧸ H.subgroup_of t`. -/
@[to_additive "If `s ≤ t`, then there is an embedding
`s ⧸ H.add_subgroup_of s ↪ t ⧸ H.add_subgroup_of t`."]
def quotient_subgroup_of_embedding_of_le (H : subgroup α) (h : s ≤ t) :
s ⧸ H.subgroup_of s ↪ t ⧸ H.subgroup_of t :=
{ to_fun := quotient.map' (inclusion h) (λ a b, by { simp_rw left_rel_eq, exact id }),
inj' := quotient.ind₂' $ by { intros a b h, simpa only [quotient.map'_mk', eq'] using h } }
@[simp, to_additive]
lemma quotient_subgroup_of_embedding_of_le_apply_mk (H : subgroup α) (h : s ≤ t) (g : s) :
quotient_subgroup_of_embedding_of_le H h (quotient_group.mk g) =
quotient_group.mk (inclusion h g) :=
rfl
/-- If `s ≤ t`, then there is a map `H ⧸ s.subgroup_of H → H ⧸ t.subgroup_of H`. -/
@[to_additive "If `s ≤ t`, then there is an map
`H ⧸ s.add_subgroup_of H → H ⧸ t.add_subgroup_of H`."]
def quotient_subgroup_of_map_of_le (H : subgroup α) (h : s ≤ t) :
H ⧸ s.subgroup_of H → H ⧸ t.subgroup_of H :=
quotient.map' id $ λ a b, by { simp_rw [left_rel_eq], apply h }
@[simp, to_additive]
lemma quotient_subgroup_of_map_of_le_apply_mk (H : subgroup α) (h : s ≤ t) (g : H) :
quotient_subgroup_of_map_of_le H h (quotient_group.mk g) = quotient_group.mk g :=
rfl
/-- If `s ≤ t`, then there is a map `α ⧸ s → α ⧸ t`. -/
@[to_additive "If `s ≤ t`, then there is an map `α ⧸ s → α ⧸ t`."]
def quotient_map_of_le (h : s ≤ t) : α ⧸ s → α ⧸ t :=
quotient.map' id $ λ a b, by { simp_rw [left_rel_eq], apply h }
@[simp, to_additive]
lemma quotient_map_of_le_apply_mk (h : s ≤ t) (g : α) :
quotient_map_of_le h (quotient_group.mk g) = quotient_group.mk g :=
rfl
/-- The natural embedding `H ⧸ (⨅ i, f i).subgroup_of H ↪ Π i, H ⧸ (f i).subgroup_of H`. -/
@[to_additive "The natural embedding
`H ⧸ (⨅ i, f i).add_subgroup_of H) ↪ Π i, H ⧸ (f i).add_subgroup_of H`.", simps]
def quotient_infi_subgroup_of_embedding {ι : Type*} (f : ι → subgroup α) (H : subgroup α) :
H ⧸ (⨅ i, f i).subgroup_of H ↪ Π i, H ⧸ (f i).subgroup_of H :=
{ to_fun := λ q i, quotient_subgroup_of_map_of_le H (infi_le f i) q,
inj' := quotient.ind₂' $ by simp_rw [funext_iff, quotient_subgroup_of_map_of_le_apply_mk,
eq', mem_subgroup_of, mem_infi, imp_self, forall_const] }
@[simp, to_additive] lemma quotient_infi_subgroup_of_embedding_apply_mk
{ι : Type*} (f : ι → subgroup α) (H : subgroup α) (g : H) (i : ι) :
quotient_infi_subgroup_of_embedding f H (quotient_group.mk g) i = quotient_group.mk g :=
rfl
/-- The natural embedding `α ⧸ (⨅ i, f i) ↪ Π i, α ⧸ f i`. -/
@[to_additive "The natural embedding `α ⧸ (⨅ i, f i) ↪ Π i, α ⧸ f i`.", simps]
def quotient_infi_embedding {ι : Type*} (f : ι → subgroup α) : α ⧸ (⨅ i, f i) ↪ Π i, α ⧸ f i :=
{ to_fun := λ q i, quotient_map_of_le (infi_le f i) q,
inj' := quotient.ind₂' $ by simp_rw [funext_iff, quotient_map_of_le_apply_mk,
eq', mem_infi, imp_self, forall_const] }
@[simp, to_additive] lemma quotient_infi_embedding_apply_mk
{ι : Type*} (f : ι → subgroup α) (g : α) (i : ι) :
quotient_infi_embedding f (quotient_group.mk g) i = quotient_group.mk g :=
rfl
@[to_additive] lemma card_eq_card_quotient_mul_card_subgroup
[fintype α] (s : subgroup α) [fintype s] [decidable_pred (λ a, a ∈ s)] :
fintype.card α = fintype.card (α ⧸ s) * fintype.card s :=
by rw ← fintype.card_prod;
exact fintype.card_congr (subgroup.group_equiv_quotient_times_subgroup)
/-- **Lagrange's Theorem**: The order of a subgroup divides the order of its ambient group. -/
@[to_additive "**Lagrange's Theorem**: The order of an additive subgroup divides the order of its
ambient group."]
lemma card_subgroup_dvd_card [fintype α] (s : subgroup α) [fintype s] :
fintype.card s ∣ fintype.card α :=
by classical; simp [card_eq_card_quotient_mul_card_subgroup s, @dvd_mul_left ℕ]
@[to_additive] lemma card_quotient_dvd_card [fintype α] (s : subgroup α) [decidable_pred (∈ s)] :
fintype.card (α ⧸ s) ∣ fintype.card α :=
by simp [card_eq_card_quotient_mul_card_subgroup s, @dvd_mul_right ℕ]
open fintype
variables {H : Type*} [group H]
@[to_additive] lemma card_dvd_of_injective [fintype α] [fintype H] (f : α →* H)
(hf : function.injective f) : card α ∣ card H :=
by classical;
calc card α = card (f.range : subgroup H) : card_congr (equiv.of_injective f hf)
...∣ card H : card_subgroup_dvd_card _
@[to_additive] lemma card_dvd_of_le {H K : subgroup α} [fintype H] [fintype K] (hHK : H ≤ K) :
card H ∣ card K :=
card_dvd_of_injective (inclusion hHK) (inclusion_injective hHK)
@[to_additive] lemma card_comap_dvd_of_injective (K : subgroup H) [fintype K]
(f : α →* H) [fintype (K.comap f)] (hf : function.injective f) :
fintype.card (K.comap f) ∣ fintype.card K :=
by haveI : fintype ((K.comap f).map f) :=
fintype.of_equiv _ (equiv_map_of_injective _ _ hf).to_equiv;
calc fintype.card (K.comap f) = fintype.card ((K.comap f).map f) :
fintype.card_congr (equiv_map_of_injective _ _ hf).to_equiv
... ∣ fintype.card K : card_dvd_of_le (map_comap_le _ _)
end subgroup
namespace quotient_group
variables [group α]
/-- If `s` is a subgroup of the group `α`, and `t` is a subset of `α ⧸ s`, then there is a
(typically non-canonical) bijection between the preimage of `t` in `α` and the product `s × t`. -/
@[to_additive "If `s` is a subgroup of the additive group `α`, and `t` is a subset of `α ⧸ s`, then
there is a (typically non-canonical) bijection between the preimage of `t` in `α` and the product
`s × t`."]
noncomputable def preimage_mk_equiv_subgroup_times_set (s : subgroup α) (t : set (α ⧸ s)) :
quotient_group.mk ⁻¹' t ≃ s × t :=
{ to_fun := λ a, ⟨⟨(quotient.out' (quotient_group.mk a))⁻¹ * a,
left_rel_apply.mp (@quotient.exact' _ (left_rel s) _ _ $ (quotient.out_eq' _))⟩,
⟨quotient_group.mk a, a.2⟩⟩,
inv_fun := λ a, ⟨quotient.out' a.2.1 * a.1.1, show quotient_group.mk _ ∈ t,
by { rw [mk_mul_of_mem _ a.1.2, out_eq'], exact a.2.2 }⟩,
left_inv := λ ⟨a, ha⟩, subtype.eq $ show _ * _ = a, by simp,
right_inv := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, by ext; simp [ha] }
end quotient_group
/--
We use the class `has_coe_t` instead of `has_coe` if the first argument is a variable,
or if the second argument is a variable not occurring in the first.
Using `has_coe` would cause looping of type-class inference. See
<https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/remove.20all.20instances.20with.20variable.20domain>
-/
library_note "use has_coe_t"
|
7cc27224acf9c67d07d5d359e5f33596628ad605 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Init/System/Uri.lean | a84943f6c109e01190a05ac8f3683c754a9f6cfb | [
"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 | 4,555 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Lovett
-/
prelude
import Init.Data.String.Extra
import Init.System.FilePath
namespace System
namespace Uri
namespace UriEscape
/- https://www.ietf.org/rfc/rfc3986.txt -/
@[inline] def zero : UInt8 := '0'.toNat.toUInt8
@[inline] def nine : UInt8 := '9'.toNat.toUInt8
@[inline] def lettera : UInt8 := 'a'.toNat.toUInt8
@[inline] def letterf : UInt8 := 'f'.toNat.toUInt8
@[inline] def letterA : UInt8 := 'A'.toNat.toUInt8
@[inline] def letterF : UInt8 := 'F'.toNat.toUInt8
/-- Decode %HH escapings in the given string. Note that sometimes a consecutive
sequence of multiple escapings can represet a utf-8 encoded sequence for
a single unicode code point and these will also be decoded correctly. -/
def decodeUri (uri : String) : String := Id.run do
let mut decoded : ByteArray := ByteArray.empty
let rawBytes := uri.toUTF8
let len := rawBytes.size
let mut i := 0
let percent := '%'.toNat.toUInt8
while i < len do
let c := rawBytes[i]!
(decoded, i) := if c == percent && i + 1 < len then
let h1 := rawBytes[i + 1]!
if let some hd1 := hexDigitToUInt8? h1 then
if i + 2 < len then
let h2 := rawBytes[i + 2]!
if let some hd2 := hexDigitToUInt8? h2 then
-- decode the hex digits into a byte.
(decoded.push (hd1 * 16 + hd2), i + 3)
else
-- not a valid second hex digit so keep the original bytes
(((decoded.push c).push h1).push h2, i + 3)
else
-- hit end of string, there is no h2.
((decoded.push c).push h1, i + 2)
else
-- not a valid hex digit so keep the original bytes
((decoded.push c).push h1, i + 2)
else
(decoded.push c, i + 1)
return String.fromUTF8Unchecked decoded
where hexDigitToUInt8? (c : UInt8) : Option UInt8 :=
if zero ≤ c ∧ c ≤ nine then some (c - zero)
else if lettera ≤ c ∧ c ≤ letterf then some (c - lettera + 10)
else if letterA ≤ c ∧ c ≤ letterF then some (c - letterA + 10)
else none
def rfc3986ReservedChars : List Char := [ ';', ':', '?', '#', '[', ']', '@', '&', '=', '+', '$', ',', '!', '\'', '(', ')', '*', '%', ' ' ]
def uriEscapeAsciiChar (c : Char) : String :=
if rfc3986ReservedChars.contains c || c < ' ' then
"%" ++ uInt8ToHex c.toNat.toUInt8
else if (Char.toNat c) < 127 then
c.toString
else
c.toString.toUTF8.foldl (fun s b => s ++ "%" ++ (uInt8ToHex b)) ""
where
uInt8ToHex (c : UInt8) : String :=
let d2 := c / 16;
let d1 := c % 16;
(hexDigitRepr d2.toNat ++ hexDigitRepr d1.toNat).toUpper
end UriEscape
/-- Replaces special characters in the given Uri with %HH Uri escapings. -/
def escapeUri (uri: String) : String :=
uri.foldl (fun s c => s ++ UriEscape.uriEscapeAsciiChar c) ""
/-- Replaces all %HH Uri escapings in the given string with their
corresponding unicode code points. Note that sometimes a consecutive
sequence of multiple escapings can represet a utf-8 encoded sequence for
a single unicode code point and these will also be decoded correctly. -/
def unescapeUri (s: String) : String :=
UriEscape.decodeUri s
/-- Convert the given FilePath to a "file:///encodedpath" Uri
where the encoded path may contain %xx escaping when needed. -/
def pathToUri (fname : System.FilePath) : String := Id.run do
let mut uri := fname.normalize.toString
if System.Platform.isWindows then
uri := uri.map (fun c => if c == '\\' then '/' else c)
uri := uri.foldl (fun s c => s ++ UriEscape.uriEscapeAsciiChar c) ""
let result := if uri.startsWith "/" then "file://" ++ uri else "file:///" ++ uri
result
/-- Convert the given uri to a FilePath stripping the 'file://' prefix,
ignoring the optional host name and unescaping any %HH escaped chars.
It is also careful to create correct paths on Windows that start with a drive letter. -/
def fileUriToPath? (uri : String) : Option System.FilePath := Id.run do
if !uri.startsWith "file://" then
none
else
let mut p := (unescapeUri uri).drop "file://".length
p := p.dropWhile (λ c => c != '/') -- drop the hostname.
-- on windows the path "/c:/temp" needs to become "c:/temp"
-- but only when it is a valid drive letter.
if System.Platform.isWindows &&
p.length > 3 &&
"/" == (p.take 1) &&
((p.drop 1).take 1).all Char.isAlpha &&
":" == ((p.drop 2).take 1) then
p := p.drop 1
some p
end Uri
end System
|
2e2b7d20683e18f0243d67b6dc1a16ebded6e5d0 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/issue332.lean | bcf5114affe5f18b60e1176ac4b7ac6b3760e410 | [
"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 | 157 | lean | import tools.tactic logic.eq
variable {a : Type}
definition foo {A : Type} : A → A :=
begin
intro a, exact a
end
check @foo
example : foo 10 = 10 :=
rfl
|
b0bd3baa5446b3b04cae5a5f1d531ffdd989092e | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/analysis/analytic/radius_liminf.lean | 63ed1376f14362aa7c90a7874607fe1674fed1d1 | [
"Apache-2.0"
] | permissive | hamdysalah1/mathlib | b915f86b2503feeae268de369f1b16932321f097 | 95454452f6b3569bf967d35aab8d852b1ddf8017 | refs/heads/master | 1,677,154,116,545 | 1,611,797,994,000 | 1,611,797,994,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,479 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import analysis.analytic.basic
import analysis.special_functions.pow
/-!
# Representation of `formal_multilinear_series.radius` as a `liminf`
In this file we prove that the radius of convergence of a `formal_multilinear_series` is equal to
$\liminf_{n\to\infty} \frac{1}{\sqrt[n]{∥p n∥}}$. This lemma can't go to `basic.lean` because this
would create a circular dependency once we redefine `exp` using `formal_multilinear_series`.
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
open_locale topological_space classical big_operators nnreal
open filter asymptotics
namespace formal_multilinear_series
variables (p : formal_multilinear_series 𝕜 E F)
/-- The radius of a formal multilinear series is equal to
$\liminf_{n\to\infty} \frac{1}{\sqrt[n]{∥p n∥}}$. The actual statement uses `ℝ≥0` and some
coercions. -/
lemma radius_eq_liminf : p.radius = liminf at_top (λ n, 1/((nnnorm (p n)) ^ (1 / (n : ℝ)) : ℝ≥0)) :=
begin
have : ∀ (r : ℝ≥0) {n : ℕ}, 0 < n →
((r : ennreal) ≤ 1 / ↑(nnnorm (p n) ^ (1 / (n : ℝ))) ↔ nnnorm (p n) * r ^ n ≤ 1),
{ intros r n hn,
have : 0 < (n : ℝ) := nat.cast_pos.2 hn,
conv_lhs {rw [one_div, ennreal.le_inv_iff_mul_le, ← ennreal.coe_mul,
ennreal.coe_le_one_iff, one_div, ← nnreal.rpow_one r, ← mul_inv_cancel this.ne',
nnreal.rpow_mul, ← nnreal.mul_rpow, ← nnreal.one_rpow (n⁻¹),
nnreal.rpow_le_rpow_iff (inv_pos.2 this), mul_comm, nnreal.rpow_nat_cast] } },
apply le_antisymm; refine ennreal.le_of_forall_nnreal_lt (λ r hr, _),
{ rcases ((tfae_exists_lt_is_o_pow (λ n, ∥p n∥ * r ^ n) 1).out 1 7).1 (p.is_o_of_lt_radius hr)
with ⟨a, ha, H⟩,
refine le_Liminf_of_le (by apply_auto_param) (eventually_map.2 $ _),
refine H.mp ((eventually_gt_at_top 0).mono $ λ n hn₀ hn, (this _ hn₀).2
(nnreal.coe_le_coe.1 _)),
push_cast,
exact (le_abs_self _).trans (hn.trans (pow_le_one _ ha.1.le ha.2.le)) },
{ refine p.le_radius_of_is_O (is_O.of_bound 1 _),
refine (eventually_lt_of_lt_liminf hr).mp ((eventually_gt_at_top 0).mono (λ n hn₀ hn, _)),
simpa using nnreal.coe_le_coe.2 ((this _ hn₀).1 hn.le) }
end
end formal_multilinear_series
|
a93adcc5e9e9ad835149bd2d93c07263c381323a | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/linear_algebra/linear_pmap.lean | f36af6edf057ef966f7143799f28619fc471a8ec | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 27,103 | lean | /-
Copyright (c) 2020 Yury Kudryashov All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Moritz Doll
-/
import linear_algebra.basic
import linear_algebra.prod
/-!
# Partially defined linear maps
A `linear_pmap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`.
We define a `semilattice_inf` with `order_bot` instance on this this, and define three operations:
* `mk_span_singleton` defines a partial linear map defined on the span of a singleton.
* `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their
domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that
extends both `f` and `g`.
* `Sup` takes a `directed_on (≤)` set of partial linear maps, and returns the unique
partial linear map on the `Sup` of their domains that extends all these maps.
Moreover, we define
* `linear_pmap.graph` is the graph of the partial linear map viewed as a submodule of `E × F`.
Partially defined maps are currently used in `mathlib` to prove Hahn-Banach theorem
and its variations. Namely, `linear_pmap.Sup` implies that every chain of `linear_pmap`s
is bounded above.
They are also the basis for the theory of unbounded operators.
-/
open set
universes u v w
/-- A `linear_pmap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. -/
structure linear_pmap (R : Type u) [ring R] (E : Type v) [add_comm_group E] [module R E]
(F : Type w) [add_comm_group F] [module R F] :=
(domain : submodule R E)
(to_fun : domain →ₗ[R] F)
notation E ` →ₗ.[`:25 R:25 `] `:0 F:0 := linear_pmap R E F
variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E]
{F : Type*} [add_comm_group F] [module R F]
{G : Type*} [add_comm_group G] [module R G]
namespace linear_pmap
open submodule
instance : has_coe_to_fun (E →ₗ.[R] F) (λ f : E →ₗ.[R] F, f.domain → F) :=
⟨λ f, f.to_fun⟩
@[simp] lemma to_fun_eq_coe (f : E →ₗ.[R] F) (x : f.domain) :
f.to_fun x = f x := rfl
@[ext] lemma ext {f g : E →ₗ.[R] F} (h : f.domain = g.domain)
(h' : ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y) : f = g :=
begin
rcases f with ⟨f_dom, f⟩,
rcases g with ⟨g_dom, g⟩,
obtain rfl : f_dom = g_dom := h,
obtain rfl : f = g := linear_map.ext (λ x, h' rfl),
refl,
end
@[simp] lemma map_zero (f : E →ₗ.[R] F) : f 0 = 0 := f.to_fun.map_zero
lemma ext_iff {f g : E →ₗ.[R] F} :
f = g ↔
∃ (domain_eq : f.domain = g.domain),
∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y :=
⟨λ EQ, EQ ▸ ⟨rfl, λ x y h, by { congr, exact_mod_cast h }⟩, λ ⟨deq, feq⟩, ext deq feq⟩
lemma map_add (f : E →ₗ.[R] F) (x y : f.domain) : f (x + y) = f x + f y :=
f.to_fun.map_add x y
lemma map_neg (f : E →ₗ.[R] F) (x : f.domain) : f (-x) = -f x :=
f.to_fun.map_neg x
lemma map_sub (f : E →ₗ.[R] F) (x y : f.domain) : f (x - y) = f x - f y :=
f.to_fun.map_sub x y
lemma map_smul (f : E →ₗ.[R] F) (c : R) (x : f.domain) : f (c • x) = c • f x :=
f.to_fun.map_smul c x
@[simp] lemma mk_apply (p : submodule R E) (f : p →ₗ[R] F) (x : p) :
mk p f x = f x := rfl
/-- The unique `linear_pmap` on `R ∙ x` that sends `x` to `y`. This version works for modules
over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/
noncomputable def mk_span_singleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) :
E →ₗ.[R] F :=
{ domain := R ∙ x,
to_fun :=
have H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y,
{ intros c₁ c₂ h,
rw [← sub_eq_zero, ← sub_smul] at h ⊢,
exact H _ h },
{ to_fun := λ z, (classical.some (mem_span_singleton.1 z.prop) • y),
map_add' := λ y z, begin
rw [← add_smul],
apply H,
simp only [add_smul, sub_smul, classical.some_spec (mem_span_singleton.1 _)],
apply coe_add
end,
map_smul' := λ c z, begin
rw [smul_smul],
apply H,
simp only [mul_smul, classical.some_spec (mem_span_singleton.1 _)],
apply coe_smul
end } }
@[simp] lemma domain_mk_span_singleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) :
(mk_span_singleton' x y H).domain = R ∙ x := rfl
@[simp] lemma mk_span_singleton'_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0)
(c : R) (h) :
mk_span_singleton' x y H ⟨c • x, h⟩ = c • y :=
begin
dsimp [mk_span_singleton'],
rw [← sub_eq_zero, ← sub_smul],
apply H,
simp only [sub_smul, one_smul, sub_eq_zero],
apply classical.some_spec (mem_span_singleton.1 h),
end
@[simp] lemma mk_span_singleton'_apply_self (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0)
(h) :
mk_span_singleton' x y H ⟨x, h⟩ = y :=
by convert mk_span_singleton'_apply x y H 1 _; rwa one_smul
/-- The unique `linear_pmap` on `span R {x}` that sends a non-zero vector `x` to `y`.
This version works for modules over division rings. -/
@[reducible] noncomputable def mk_span_singleton {K E F : Type*} [division_ring K]
[add_comm_group E] [module K E] [add_comm_group F] [module K F] (x : E) (y : F) (hx : x ≠ 0) :
E →ₗ.[K] F :=
mk_span_singleton' x y $ λ c hc, (smul_eq_zero.1 hc).elim
(λ hc, by rw [hc, zero_smul]) (λ hx', absurd hx' hx)
lemma mk_span_singleton_apply (K : Type*) {E F : Type*} [division_ring K]
[add_comm_group E] [module K E] [add_comm_group F] [module K F] {x : E} (hx : x ≠ 0) (y : F) :
mk_span_singleton x y hx
⟨x, (submodule.mem_span_singleton_self x : x ∈ submodule.span K {x})⟩ = y :=
linear_pmap.mk_span_singleton'_apply_self _ _ _ _
/-- Projection to the first coordinate as a `linear_pmap` -/
protected def fst (p : submodule R E) (p' : submodule R F) : (E × F) →ₗ.[R] E :=
{ domain := p.prod p',
to_fun := (linear_map.fst R E F).comp (p.prod p').subtype }
@[simp] lemma fst_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') :
linear_pmap.fst p p' x = (x : E × F).1 := rfl
/-- Projection to the second coordinate as a `linear_pmap` -/
protected def snd (p : submodule R E) (p' : submodule R F) : (E × F) →ₗ.[R] F :=
{ domain := p.prod p',
to_fun := (linear_map.snd R E F).comp (p.prod p').subtype }
@[simp] lemma snd_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') :
linear_pmap.snd p p' x = (x : E × F).2 := rfl
instance : has_neg (E →ₗ.[R] F) :=
⟨λ f, ⟨f.domain, -f.to_fun⟩⟩
@[simp] lemma neg_apply (f : E →ₗ.[R] F) (x) : (-f) x = -(f x) := rfl
instance : has_le (E →ₗ.[R] F) :=
⟨λ f g, f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y⟩
lemma eq_of_le_of_domain_eq {f g : E →ₗ.[R] F} (hle : f ≤ g) (heq : f.domain = g.domain) :
f = g :=
ext heq hle.2
/-- Given two partial linear maps `f`, `g`, the set of points `x` such that
both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/
def eq_locus (f g : E →ₗ.[R] F) : submodule R E :=
{ carrier := {x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩},
zero_mem' := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩,
add_mem' := λ x y ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩, ⟨add_mem hfx hfy, add_mem hgx hgy,
by erw [f.map_add ⟨x, hfx⟩ ⟨y, hfy⟩, g.map_add ⟨x, hgx⟩ ⟨y, hgy⟩, hx, hy]⟩,
smul_mem' := λ c x ⟨hfx, hgx, hx⟩, ⟨smul_mem _ c hfx, smul_mem _ c hgx,
by erw [f.map_smul c ⟨x, hfx⟩, g.map_smul c ⟨x, hgx⟩, hx]⟩ }
instance : has_inf (E →ₗ.[R] F) :=
⟨λ f g, ⟨f.eq_locus g, f.to_fun.comp $ of_le $ λ x hx, hx.fst⟩⟩
instance : has_bot (E →ₗ.[R] F) := ⟨⟨⊥, 0⟩⟩
instance : inhabited (E →ₗ.[R] F) := ⟨⊥⟩
instance : semilattice_inf (E →ₗ.[R] F) :=
{ le := (≤),
le_refl := λ f, ⟨le_refl f.domain, λ x y h, subtype.eq h ▸ rfl⟩,
le_trans := λ f g h ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩,
⟨le_trans fg_le gh_le, λ x z hxz,
have hxy : (x:E) = of_le fg_le x, from rfl,
(fg_eq hxy).trans (gh_eq $ hxy.symm.trans hxz)⟩,
le_antisymm := λ f g fg gf, eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1),
inf := (⊓),
le_inf := λ f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩,
⟨λ x hx, ⟨fg_le hx, fh_le hx,
by refine (fg_eq _).symm.trans (fh_eq _); [exact ⟨x, hx⟩, refl, refl]⟩,
λ x ⟨y, yg, hy⟩ h, by { apply fg_eq, exact h }⟩,
inf_le_left := λ f g, ⟨λ x hx, hx.fst,
λ x y h, congr_arg f $ subtype.eq $ by exact h⟩,
inf_le_right := λ f g, ⟨λ x hx, hx.snd.fst,
λ ⟨x, xf, xg, hx⟩ y h, hx.trans $ congr_arg g $ subtype.eq $ by exact h⟩ }
instance : order_bot (E →ₗ.[R] F) :=
{ bot := ⊥,
bot_le := λ f, ⟨bot_le, λ x y h,
have hx : x = 0, from subtype.eq ((mem_bot R).1 x.2),
have hy : y = 0, from subtype.eq (h.symm.trans (congr_arg _ hx)),
by rw [hx, hy, map_zero, map_zero]⟩ }
lemma le_of_eq_locus_ge {f g : E →ₗ.[R] F} (H : f.domain ≤ f.eq_locus g) :
f ≤ g :=
suffices f ≤ f ⊓ g, from le_trans this inf_le_right,
⟨H, λ x y hxy, ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩
lemma domain_mono : strict_mono (@domain R _ E _ _ F _ _) :=
λ f g hlt, lt_of_le_of_ne hlt.1.1 $ λ heq, ne_of_lt hlt $
eq_of_le_of_domain_eq (le_of_lt hlt) heq
private lemma sup_aux (f g : E →ₗ.[R] F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F,
∀ (x : f.domain) (y : g.domain) (z),
(x:E) + y = ↑z → fg z = f x + g y :=
begin
choose x hx y hy hxy using λ z : f.domain ⊔ g.domain, mem_sup.1 z.prop,
set fg := λ z, f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩,
have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : f.domain ⊔ g.domain) (H : (x':E) + y' = z'),
fg z' = f x' + g y',
{ intros x' y' z' H,
dsimp [fg],
rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub],
apply h,
simp only [← eq_sub_iff_add_eq] at hxy,
simp only [add_subgroup_class.coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self,
zero_sub, ← H],
apply neg_add_eq_sub },
refine ⟨{ to_fun := fg, .. }, fg_eq⟩,
{ rintros ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩,
rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add],
apply fg_eq,
simp only [coe_add, coe_mk, ← add_assoc],
rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] },
{ intros c z,
rw [smul_add, ← map_smul, ← map_smul],
apply fg_eq,
simp only [coe_smul, coe_mk, ← smul_add, hxy, ring_hom.id_apply] },
end
/-- Given two partial linear maps that agree on the intersection of their domains,
`f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees
with `f` and `g`. -/
protected noncomputable def sup (f g : E →ₗ.[R] F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
E →ₗ.[R] F :=
⟨_, classical.some (sup_aux f g h)⟩
@[simp] lemma domain_sup (f g : E →ₗ.[R] F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
(f.sup g h).domain = f.domain ⊔ g.domain :=
rfl
lemma sup_apply {f g : E →ₗ.[R] F}
(H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y)
(x y z) (hz : (↑x:E) + ↑y = ↑z) :
f.sup g H z = f x + g y :=
classical.some_spec (sup_aux f g H) x y z hz
protected lemma left_le_sup (f g : E →ₗ.[R] F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
f ≤ f.sup g h :=
begin
refine ⟨le_sup_left, λ z₁ z₂ hz, _⟩,
rw [← add_zero (f _), ← g.map_zero],
refine (sup_apply h _ _ _ _).symm,
simpa
end
protected lemma right_le_sup (f g : E →ₗ.[R] F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
g ≤ f.sup g h :=
begin
refine ⟨le_sup_right, λ z₁ z₂ hz, _⟩,
rw [← zero_add (g _), ← f.map_zero],
refine (sup_apply h _ _ _ _).symm,
simpa
end
protected lemma sup_le {f g h : E →ₗ.[R] F}
(H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y)
(fh : f ≤ h) (gh : g ≤ h) :
f.sup g H ≤ h :=
have Hf : f ≤ (f.sup g H) ⊓ h, from le_inf (f.left_le_sup g H) fh,
have Hg : g ≤ (f.sup g H) ⊓ h, from le_inf (f.right_le_sup g H) gh,
le_of_eq_locus_ge $ sup_le Hf.1 Hg.1
/-- Hypothesis for `linear_pmap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/
lemma sup_h_of_disjoint (f g : E →ₗ.[R] F) (h : disjoint f.domain g.domain)
(x : f.domain) (y : g.domain) (hxy : (x:E) = y) :
f x = g y :=
begin
rw [disjoint_def] at h,
have hy : y = 0, from subtype.eq (h y (hxy ▸ x.2) y.2),
have hx : x = 0, from subtype.eq (hxy.trans $ congr_arg _ hy),
simp [*]
end
section smul
variables {M N : Type*} [monoid M] [distrib_mul_action M F] [smul_comm_class R M F]
variables [monoid N] [distrib_mul_action N F] [smul_comm_class R N F]
instance : has_smul M (E →ₗ.[R] F) :=
⟨λ a f,
{ domain := f.domain,
to_fun := a • f.to_fun }⟩
lemma smul_apply (a : M) (f : E →ₗ.[R] F) (x : ((a • f).domain)) :
(a • f) x = a • f x := rfl
@[simp] lemma coe_smul (a : M) (f : E →ₗ.[R] F) : ⇑(a • f) = a • f := rfl
instance [smul_comm_class M N F] : smul_comm_class M N (E →ₗ.[R] F) :=
⟨λ a b f, ext rfl $ λ x y hxy, by simp_rw [smul_apply, subtype.eq hxy, smul_comm]⟩
instance [has_smul M N] [is_scalar_tower M N F] : is_scalar_tower M N (E →ₗ.[R] F) :=
⟨λ a b f, ext rfl $ λ x y hxy, by simp_rw [smul_apply, subtype.eq hxy, smul_assoc]⟩
end smul
section
variables {K : Type*} [division_ring K] [module K E] [module K F]
/-- Extend a `linear_pmap` to `f.domain ⊔ K ∙ x`. -/
noncomputable def sup_span_singleton (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) :
E →ₗ.[K] F :=
f.sup (mk_span_singleton x y (λ h₀, hx $ h₀.symm ▸ f.domain.zero_mem)) $
sup_h_of_disjoint _ _ $ by simpa [disjoint_span_singleton]
@[simp] lemma domain_sup_span_singleton (f : E →ₗ.[K] F) (x : E) (y : F)
(hx : x ∉ f.domain) :
(f.sup_span_singleton x y hx).domain = f.domain ⊔ K ∙ x := rfl
@[simp] lemma sup_span_singleton_apply_mk (f : E →ₗ.[K] F) (x : E) (y : F)
(hx : x ∉ f.domain) (x' : E) (hx' : x' ∈ f.domain) (c : K) :
f.sup_span_singleton x y hx ⟨x' + c • x,
mem_sup.2 ⟨x', hx', _, mem_span_singleton.2 ⟨c, rfl⟩, rfl⟩⟩ = f ⟨x', hx'⟩ + c • y :=
begin
erw [sup_apply _ ⟨x', hx'⟩ ⟨c • x, _⟩, mk_span_singleton'_apply],
refl,
exact mem_span_singleton.2 ⟨c, rfl⟩
end
end
private lemma Sup_aux (c : set (E →ₗ.[R] F)) (hc : directed_on (≤) c) :
∃ f : ↥(Sup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : E →ₗ.[R] F) ∈ upper_bounds c :=
begin
cases c.eq_empty_or_nonempty with ceq cne, { subst c, simp },
have hdir : directed_on (≤) (domain '' c),
from directed_on_image.2 (hc.mono domain_mono.monotone),
have P : Π x : Sup (domain '' c), {p : c // (x : E) ∈ p.val.domain },
{ rintros x,
apply classical.indefinite_description,
have := (mem_Sup_of_directed (cne.image _) hdir).1 x.2,
rwa [bex_image_iff, set_coe.exists'] at this },
set f : Sup (domain '' c) → F := λ x, (P x).val.val ⟨x, (P x).property⟩,
have f_eq : ∀ (p : c) (x : Sup (domain '' c)) (y : p.1.1) (hxy : (x : E) = y), f x = p.1 y,
{ intros p x y hxy,
rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, hqc, hxq, hpq⟩,
refine (hxq.2 _).trans (hpq.2 _).symm,
exacts [of_le hpq.1 y, hxy, rfl] },
refine ⟨{ to_fun := f, .. }, _⟩,
{ intros x y,
rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩,
set x' := of_le hpx.1 ⟨x, (P x).2⟩,
set y' := of_le hpy.1 ⟨y, (P y).2⟩,
rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl,
map_add] },
{ intros c x,
simp [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] },
{ intros p hpc,
refine ⟨le_Sup $ mem_image_of_mem domain hpc, λ x y hxy, eq.symm _⟩,
exact f_eq ⟨p, hpc⟩ _ _ hxy.symm }
end
/-- Glue a collection of partially defined linear maps to a linear map defined on `Sup`
of these submodules. -/
protected noncomputable def Sup (c : set (E →ₗ.[R] F)) (hc : directed_on (≤) c) :
E →ₗ.[R] F :=
⟨_, classical.some $ Sup_aux c hc⟩
protected lemma le_Sup {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c)
{f : E →ₗ.[R] F} (hf : f ∈ c) : f ≤ linear_pmap.Sup c hc :=
classical.some_spec (Sup_aux c hc) hf
protected lemma Sup_le {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c)
{g : E →ₗ.[R] F} (hg : ∀ f ∈ c, f ≤ g) : linear_pmap.Sup c hc ≤ g :=
le_of_eq_locus_ge $ Sup_le $ λ _ ⟨f, hf, eq⟩, eq ▸
have f ≤ (linear_pmap.Sup c hc) ⊓ g, from le_inf (linear_pmap.le_Sup _ hf) (hg f hf),
this.1
protected lemma Sup_apply {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c)
{l : E →ₗ.[R] F} (hl : l ∈ c) (x : l.domain) :
(linear_pmap.Sup c hc) ⟨x, (linear_pmap.le_Sup hc hl).1 x.2⟩ = l x :=
begin
symmetry,
apply (classical.some_spec (Sup_aux c hc) hl).2,
refl,
end
end linear_pmap
namespace linear_map
/-- Restrict a linear map to a submodule, reinterpreting the result as a `linear_pmap`. -/
def to_pmap (f : E →ₗ[R] F) (p : submodule R E) : E →ₗ.[R] F :=
⟨p, f.comp p.subtype⟩
@[simp] lemma to_pmap_apply (f : E →ₗ[R] F) (p : submodule R E) (x : p) :
f.to_pmap p x = f x := rfl
/-- Compose a linear map with a `linear_pmap` -/
def comp_pmap (g : F →ₗ[R] G) (f : E →ₗ.[R] F) : E →ₗ.[R] G :=
{ domain := f.domain,
to_fun := g.comp f.to_fun }
@[simp] lemma comp_pmap_apply (g : F →ₗ[R] G) (f : E →ₗ.[R] F) (x) :
g.comp_pmap f x = g (f x) := rfl
end linear_map
namespace linear_pmap
/-- Restrict codomain of a `linear_pmap` -/
def cod_restrict (f : E →ₗ.[R] F) (p : submodule R F) (H : ∀ x, f x ∈ p) : E →ₗ.[R] p :=
{ domain := f.domain,
to_fun := f.to_fun.cod_restrict p H }
/-- Compose two `linear_pmap`s -/
def comp (g : F →ₗ.[R] G) (f : E →ₗ.[R] F)
(H : ∀ x : f.domain, f x ∈ g.domain) : E →ₗ.[R] G :=
g.to_fun.comp_pmap $ f.cod_restrict _ H
/-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`,
and sending `p` to `f p.1 + g p.2`. -/
def coprod (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) : (E × F) →ₗ.[R] G :=
{ domain := f.domain.prod g.domain,
to_fun := (f.comp (linear_pmap.fst f.domain g.domain) (λ x, x.2.1)).to_fun +
(g.comp (linear_pmap.snd f.domain g.domain) (λ x, x.2.2)).to_fun }
@[simp] lemma coprod_apply (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) (x) :
f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ :=
rfl
/-! ### Graph -/
section graph
/-- The graph of a `linear_pmap` viewed as a submodule on `E × F`. -/
def graph (f : E →ₗ.[R] F) : submodule R (E × F) :=
f.to_fun.graph.map (f.domain.subtype.prod_map linear_map.id)
lemma mem_graph_iff' (f : E →ₗ.[R] F) {x : E × F} :
x ∈ f.graph ↔ ∃ y : f.domain, (↑y, f y) = x :=
by simp [graph]
@[simp] lemma mem_graph_iff (f : E →ₗ.[R] F) {x : E × F} :
x ∈ f.graph ↔ ∃ y : f.domain, (↑y : E) = x.1 ∧ f y = x.2 :=
by { cases x, simp_rw [mem_graph_iff', prod.mk.inj_iff] }
/-- The tuple `(x, f x)` is contained in the graph of `f`. -/
lemma mem_graph (f : E →ₗ.[R] F) (x : domain f) : ((x : E), f x) ∈ f.graph :=
by simp
variables {M : Type*} [monoid M] [distrib_mul_action M F] [smul_comm_class R M F] (y : M)
/-- The graph of `z • f` as a pushforward. -/
lemma smul_graph (f : E →ₗ.[R] F) (z : M) :
(z • f).graph = f.graph.map (linear_map.id.prod_map (z • linear_map.id)) :=
begin
ext x, cases x,
split; intros h,
{ rw mem_graph_iff at h,
rcases h with ⟨y, hy, h⟩,
rw linear_pmap.smul_apply at h,
rw submodule.mem_map,
simp only [mem_graph_iff, linear_map.prod_map_apply, linear_map.id_coe, id.def,
linear_map.smul_apply, prod.mk.inj_iff, prod.exists, exists_exists_and_eq_and],
use [x_fst, y],
simp [hy, h] },
rw submodule.mem_map at h,
rcases h with ⟨x', hx', h⟩,
cases x',
simp only [linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.smul_apply,
prod.mk.inj_iff] at h,
rw mem_graph_iff at hx' ⊢,
rcases hx' with ⟨y, hy, hx'⟩,
use y,
rw [←h.1, ←h.2],
simp[hy, hx'],
end
/-- The graph of `-f` as a pushforward. -/
lemma neg_graph (f : E →ₗ.[R] F) :
(-f).graph = f.graph.map (linear_map.id.prod_map (-linear_map.id)) :=
begin
ext, cases x,
split; intros h,
{ rw mem_graph_iff at h,
rcases h with ⟨y, hy, h⟩,
rw linear_pmap.neg_apply at h,
rw submodule.mem_map,
simp only [mem_graph_iff, linear_map.prod_map_apply, linear_map.id_coe, id.def,
linear_map.neg_apply, prod.mk.inj_iff, prod.exists, exists_exists_and_eq_and],
use [x_fst, y],
simp [hy, h] },
rw submodule.mem_map at h,
rcases h with ⟨x', hx', h⟩,
cases x',
simp only [linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.neg_apply,
prod.mk.inj_iff] at h,
rw mem_graph_iff at hx' ⊢,
rcases hx' with ⟨y, hy, hx'⟩,
use y,
rw [←h.1, ←h.2],
simp [hy, hx'],
end
lemma mem_graph_snd_inj (f : E →ₗ.[R] F) {x y : E} {x' y' : F} (hx : (x,x') ∈ f.graph)
(hy : (y,y') ∈ f.graph) (hxy : x = y) : x' = y' :=
begin
rw [mem_graph_iff] at hx hy,
rcases hx with ⟨x'', hx1, hx2⟩,
rcases hy with ⟨y'', hy1, hy2⟩,
simp only at hx1 hx2 hy1 hy2,
rw [←hx1, ←hy1, set_like.coe_eq_coe] at hxy,
rw [←hx2, ←hy2, hxy],
end
lemma mem_graph_snd_inj' (f : E →ₗ.[R] F) {x y : E × F} (hx : x ∈ f.graph) (hy : y ∈ f.graph)
(hxy : x.1 = y.1) : x.2 = y.2 :=
by { cases x, cases y, exact f.mem_graph_snd_inj hx hy hxy }
/-- The property that `f 0 = 0` in terms of the graph. -/
lemma graph_fst_eq_zero_snd (f : E →ₗ.[R] F) {x : E} {x' : F} (h : (x,x') ∈ f.graph)
(hx : x = 0) : x' = 0 :=
f.mem_graph_snd_inj h f.graph.zero_mem hx
lemma mem_domain_iff {f : E →ₗ.[R] F} {x : E} : x ∈ f.domain ↔ ∃ y : F, (x,y) ∈ f.graph :=
begin
split; intro h,
{ use f ⟨x, h⟩,
exact f.mem_graph ⟨x, h⟩ },
cases h with y h,
rw mem_graph_iff at h,
cases h with x' h,
simp only at h,
rw ←h.1,
simp,
end
lemma image_iff {f : E →ₗ.[R] F} {x : E} {y : F} (hx : x ∈ f.domain) :
y = f ⟨x, hx⟩ ↔ (x, y) ∈ f.graph :=
begin
rw mem_graph_iff,
split; intro h,
{ use ⟨x, hx⟩,
simp [h] },
rcases h with ⟨⟨x', hx'⟩, ⟨h1, h2⟩⟩,
simp only [submodule.coe_mk] at h1 h2,
simp only [←h2, h1],
end
lemma mem_range_iff {f : E →ₗ.[R] F} {y : F} : y ∈ set.range f ↔ ∃ x : E, (x,y) ∈ f.graph :=
begin
split; intro h,
{ rw set.mem_range at h,
rcases h with ⟨⟨x, hx⟩, h⟩,
use x,
rw ←h,
exact f.mem_graph ⟨x, hx⟩ },
cases h with x h,
rw mem_graph_iff at h,
cases h with x h,
rw set.mem_range,
use x,
simp only at h,
rw h.2,
end
lemma mem_domain_iff_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) {x : E} :
x ∈ f.domain ↔ x ∈ g.domain :=
by simp_rw [mem_domain_iff, h]
lemma le_of_le_graph {f g : E →ₗ.[R] F} (h : f.graph ≤ g.graph) : f ≤ g :=
begin
split,
{ intros x hx,
rw mem_domain_iff at hx ⊢,
cases hx with y hx,
use y,
exact h hx },
rintros ⟨x, hx⟩ ⟨y, hy⟩ hxy,
rw image_iff,
refine h _,
simp only [submodule.coe_mk] at hxy,
rw hxy at hx,
rw ←image_iff hx,
simp [hxy],
end
lemma eq_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) : f = g :=
by {ext, exact mem_domain_iff_of_eq_graph h, exact (le_of_le_graph h.le).2 }
end graph
end linear_pmap
namespace submodule
section submodule_to_linear_pmap
lemma exists_unique_from_graph {g : submodule R (E × F)}
(hg : ∀ {x : E × F} (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E}
(ha : a ∈ g.map (linear_map.fst R E F)) :
∃! (b : F), (a,b) ∈ g :=
begin
refine exists_unique_of_exists_of_unique _ _,
{ convert ha, simp },
intros y₁ y₂ hy₁ hy₂,
have hy : ((0 : E), y₁ - y₂) ∈ g :=
begin
convert g.sub_mem hy₁ hy₂,
exact (sub_self _).symm,
end,
exact sub_eq_zero.mp (hg hy (by simp)),
end
/-- Auxiliary definition to unfold the existential quantifier. -/
noncomputable
def val_from_graph {g : submodule R (E × F)}
(hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E}
(ha : a ∈ g.map (linear_map.fst R E F)) : F :=
(exists_of_exists_unique (exists_unique_from_graph hg ha)).some
lemma val_from_graph_mem {g : submodule R (E × F)}
(hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E}
(ha : a ∈ g.map (linear_map.fst R E F)) : (a, val_from_graph hg ha) ∈ g :=
(exists_of_exists_unique (exists_unique_from_graph hg ha)).some_spec
/-- Define a `linear_pmap` from its graph. -/
noncomputable
def to_linear_pmap (g : submodule R (E × F))
(hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) : E →ₗ.[R] F :=
{ domain := g.map (linear_map.fst R E F),
to_fun :=
{ to_fun := λ x, val_from_graph hg x.2,
map_add' := λ v w, begin
have hadd := (g.map (linear_map.fst R E F)).add_mem v.2 w.2,
have hvw := val_from_graph_mem hg hadd,
have hvw' := g.add_mem (val_from_graph_mem hg v.2) (val_from_graph_mem hg w.2),
rw [prod.mk_add_mk] at hvw',
exact (exists_unique_from_graph hg hadd).unique hvw hvw',
end,
map_smul' := λ a v, begin
have hsmul := (g.map (linear_map.fst R E F)).smul_mem a v.2,
have hav := val_from_graph_mem hg hsmul,
have hav' := g.smul_mem a (val_from_graph_mem hg v.2),
rw [prod.smul_mk] at hav',
exact (exists_unique_from_graph hg hsmul).unique hav hav',
end } }
lemma mem_graph_to_linear_pmap (g : submodule R (E × F))
(hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0)
(x : g.map (linear_map.fst R E F)) : (x.val, g.to_linear_pmap hg x) ∈ g :=
val_from_graph_mem hg x.2
@[simp] lemma to_linear_pmap_graph_eq (g : submodule R (E × F))
(hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) :
(g.to_linear_pmap hg).graph = g :=
begin
ext,
split; intro hx,
{ rw [linear_pmap.mem_graph_iff] at hx,
rcases hx with ⟨y,hx1,hx2⟩,
convert g.mem_graph_to_linear_pmap hg y,
rw [subtype.val_eq_coe],
exact prod.ext hx1.symm hx2.symm },
rw linear_pmap.mem_graph_iff,
cases x,
have hx_fst : x_fst ∈ g.map (linear_map.fst R E F) :=
begin
simp only [mem_map, linear_map.fst_apply, prod.exists, exists_and_distrib_right,
exists_eq_right],
exact ⟨x_snd, hx⟩,
end,
refine ⟨⟨x_fst, hx_fst⟩, subtype.coe_mk x_fst hx_fst, _⟩,
exact (exists_unique_from_graph hg hx_fst).unique (val_from_graph_mem hg hx_fst) hx,
end
end submodule_to_linear_pmap
end submodule
|
0dbc26f794382b2c1b9adf33a6b8c5be0abe620f | 2d041ea7f2e9b29093ffd7c99b11decfaa8b20ca | /ch9.lean | d66720f3d92afd53b38164040d5097075d4ad302 | [] | no_license | 0xpr/lean_tutorial | 1a66577602baa9a52c2b01130b9d70089653ea37 | 56ef609d8df9e392916012db5354bf182cbbb8d8 | refs/heads/master | 1,606,955,421,810 | 1,499,014,388,000 | 1,499,014,388,000 | 96,036,899 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,946 | lean | import data.nat
open nat decidable algebra
definition bex (n : nat) (P : nat → Prop) : Prop :=
∃ x : nat, x < n ∧ P x
definition not_bex_zero (P : nat → Prop) : ¬ bex 0 P :=
(assume H: bex 0 P,
exists.elim H
(take x,
(assume H' : x < 0 ∧ P x,
have H_le_0 : x < 0, from and.elim_left H',
or.elim (eq_zero_or_pos x)
(λ H : x = 0, (ne_of_lt H_le_0) H)
(λ H : x > 0, (lt.asymm H_le_0) H))))
variables {n : nat} {P : nat → Prop}
definition bex_succ (H : bex n P) : bex (succ n) P :=
exists.elim H
(take x,
(assume H : x < n ∧ P x,
have H' : x < n, from and.elim_left H,
have Hsucc : x < succ n, from lt.trans H' (nat.lt_succ_self n),
exists.intro x (and.intro Hsucc (and.elim_right H))))
definition bex_succ_of_pred (H : P n) : bex (succ n) P :=
exists.intro n (and.intro (nat.lt_succ_self n) H)
definition not_bex_succ (H_1 : ¬ bex n P) (H_2 : ¬ P n) : ¬ bex (succ n) P :=
(assume H : bex (succ n) P,
exists.elim H
(take x,
(assume H' : x < succ n ∧ P x,
have H_le : x < succ n, from and.elim_left H',
have H_px : P x, from and.elim_right H',
or.elim (lt_or_eq_of_le (le_of_lt_succ H_le))
(λ H : x < n, H_1 (exists.intro x (and.intro H H_px)))
(λ H_eq : x = n, H_2 (eq.rec (and.elim_right H') H_eq)))))
definition dec_bex [instance] (H : decidable_pred P) : Π (n : nat), decidable (bex n P)
| dec_bex 0 := inr (not_bex_zero P)
| dec_bex (a + 1) :=
match dec_bex a with
| inl iH := inl (bex_succ iH)
| inr niH := if h : P a then inl (bex_succ_of_pred h) else inr (not_bex_succ niH h)
end
|
9e25583cbb5185da14393c11ea353addc16ec332 | d72901cc240bd78b8b0384565e4f4dee8abd3a86 | /src/topology/metric_space/isometry.lean | 3ca928c8ea023ed67bf47ee9fe6fc7ef0ee38dcb | [
"Apache-2.0"
] | permissive | leon-volq/mathlib | 513b24765349bb5187df9d898b92beadf96124d9 | 0cc93a137e9b2e243f8ae1f808fa7225ce0fe143 | refs/heads/master | 1,676,294,376,990 | 1,610,838,688,000 | 1,610,838,688,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,565 | lean | /-
Copyright (c) 2018 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Isometries of emetric and metric spaces
Authors: Sébastien Gouëzel
-/
import topology.bounded_continuous_function
import topology.compacts
/-!
# Isometries
We define isometries, i.e., maps between emetric spaces that preserve
the edistance (on metric spaces, these are exactly the maps that preserve distances),
and prove their basic properties. We also introduce isometric bijections.
-/
noncomputable theory
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open function set
/-- An isometry (also known as isometric embedding) is a map preserving the edistance
between emetric spaces, or equivalently the distance between metric space. -/
def isometry [emetric_space α] [emetric_space β] (f : α → β) : Prop :=
∀x1 x2 : α, edist (f x1) (f x2) = edist x1 x2
/-- On metric spaces, a map is an isometry if and only if it preserves distances. -/
lemma isometry_emetric_iff_metric [metric_space α] [metric_space β] {f : α → β} :
isometry f ↔ (∀x y, dist (f x) (f y) = dist x y) :=
⟨assume H x y, by simp [dist_edist, H x y],
assume H x y, by simp [edist_dist, H x y]⟩
/-- An isometry preserves edistances. -/
theorem isometry.edist_eq [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f)
(x y : α) :
edist (f x) (f y) = edist x y :=
hf x y
/-- An isometry preserves distances. -/
theorem isometry.dist_eq [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (x y : α) :
dist (f x) (f y) = dist x y :=
by rw [dist_edist, dist_edist, hf]
section emetric_isometry
variables [emetric_space α] [emetric_space β] [emetric_space γ]
variables {f : α → β} {x y z : α} {s : set α}
lemma isometry.lipschitz (h : isometry f) : lipschitz_with 1 f :=
lipschitz_with.of_edist_le $ λ x y, le_of_eq (h x y)
lemma isometry.antilipschitz (h : isometry f) : antilipschitz_with 1 f :=
λ x y, by simp only [h x y, ennreal.coe_one, one_mul, le_refl]
/-- An isometry is injective -/
lemma isometry.injective (h : isometry f) : injective f := h.antilipschitz.injective
/-- Any map on a subsingleton is an isometry -/
theorem isometry_subsingleton [subsingleton α] : isometry f :=
λx y, by rw subsingleton.elim x y; simp
/-- The identity is an isometry -/
lemma isometry_id : isometry (id : α → α) :=
λx y, rfl
/-- The composition of isometries is an isometry -/
theorem isometry.comp {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) :
isometry (g ∘ f) :=
assume x y, calc
edist ((g ∘ f) x) ((g ∘ f) y) = edist (f x) (f y) : hg _ _
... = edist x y : hf _ _
/-- An isometry is an embedding -/
theorem isometry.uniform_embedding (hf : isometry f) : uniform_embedding f :=
hf.antilipschitz.uniform_embedding hf.lipschitz.uniform_continuous
/-- An isometry is continuous. -/
lemma isometry.continuous (hf : isometry f) : continuous f :=
hf.lipschitz.continuous
/-- The right inverse of an isometry is an isometry. -/
lemma isometry.right_inv {f : α → β} {g : β → α} (h : isometry f) (hg : right_inverse g f) :
isometry g :=
λ x y, by rw [← h, hg _, hg _]
/-- Isometries preserve the diameter in emetric spaces. -/
lemma isometry.ediam_image (hf : isometry f) (s : set α) :
emetric.diam (f '' s) = emetric.diam s :=
eq_of_forall_ge_iff $ λ d,
by simp only [emetric.diam_le_iff_forall_edist_le, ball_image_iff, hf.edist_eq]
lemma isometry.ediam_range (hf : isometry f) :
emetric.diam (range f) = emetric.diam (univ : set α) :=
by { rw ← image_univ, exact hf.ediam_image univ }
/-- The injection from a subtype is an isometry -/
lemma isometry_subtype_coe {s : set α} : isometry (coe : s → α) :=
λx y, rfl
end emetric_isometry --section
/-- An isometry preserves the diameter in metric spaces. -/
lemma isometry.diam_image [metric_space α] [metric_space β]
{f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s :=
by rw [metric.diam, metric.diam, hf.ediam_image]
lemma isometry.diam_range [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) :
metric.diam (range f) = metric.diam (univ : set α) :=
by { rw ← image_univ, exact hf.diam_image univ }
/-- `α` and `β` are isometric if there is an isometric bijection between them. -/
@[nolint has_inhabited_instance] -- such a bijection need not exist
structure isometric (α : Type*) (β : Type*) [emetric_space α] [emetric_space β]
extends α ≃ β :=
(isometry_to_fun : isometry to_fun)
infix ` ≃ᵢ `:25 := isometric
namespace isometric
variables [emetric_space α] [emetric_space β] [emetric_space γ]
instance : has_coe_to_fun (α ≃ᵢ β) := ⟨λ_, α → β, λe, e.to_equiv⟩
lemma coe_eq_to_equiv (h : α ≃ᵢ β) (a : α) : h a = h.to_equiv a := rfl
@[simp] lemma coe_to_equiv (h : α ≃ᵢ β) : ⇑h.to_equiv = h := rfl
protected lemma isometry (h : α ≃ᵢ β) : isometry h := h.isometry_to_fun
protected lemma edist_eq (h : α ≃ᵢ β) (x y : α) : edist (h x) (h y) = edist x y :=
h.isometry.edist_eq x y
protected lemma dist_eq {α β : Type*} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (x y : α) :
dist (h x) (h y) = dist x y :=
h.isometry.dist_eq x y
protected lemma continuous (h : α ≃ᵢ β) : continuous h := h.isometry.continuous
lemma to_equiv_inj : ∀ ⦃h₁ h₂ : α ≃ᵢ β⦄, (h₁.to_equiv = h₂.to_equiv) → h₁ = h₂
| ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ H := by { dsimp at H, subst e₁ }
@[ext] lemma ext ⦃h₁ h₂ : α ≃ᵢ β⦄ (H : ∀ x, h₁ x = h₂ x) : h₁ = h₂ :=
to_equiv_inj $ equiv.ext H
/-- Alternative constructor for isometric bijections,
taking as input an isometry, and a right inverse. -/
def mk' (f : α → β) (g : β → α) (hfg : ∀ x, f (g x) = x) (hf : isometry f) : α ≃ᵢ β :=
{ to_fun := f,
inv_fun := g,
left_inv := λ x, hf.injective $ hfg _,
right_inv := hfg,
isometry_to_fun := hf }
/-- The identity isometry of a space. -/
protected def refl (α : Type*) [emetric_space α] : α ≃ᵢ α :=
{ isometry_to_fun := isometry_id, .. equiv.refl α }
/-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/
protected def trans (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ :=
{ isometry_to_fun := h₂.isometry_to_fun.comp h₁.isometry_to_fun,
.. equiv.trans h₁.to_equiv h₂.to_equiv }
@[simp] lemma trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : α) : h₁.trans h₂ x = h₂ (h₁ x) := rfl
/-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/
protected def symm (h : α ≃ᵢ β) : β ≃ᵢ α :=
{ isometry_to_fun := h.isometry.right_inv h.right_inv,
to_equiv := h.to_equiv.symm }
@[simp] lemma symm_symm (h : α ≃ᵢ β) : h.symm.symm = h := to_equiv_inj h.to_equiv.symm_symm
@[simp] lemma apply_symm_apply (h : α ≃ᵢ β) (y : β) : h (h.symm y) = y :=
h.to_equiv.apply_symm_apply y
@[simp] lemma symm_apply_apply (h : α ≃ᵢ β) (x : α) : h.symm (h x) = x :=
h.to_equiv.symm_apply_apply x
lemma symm_apply_eq (h : α ≃ᵢ β) {x : α} {y : β} :
h.symm y = x ↔ y = h x :=
h.to_equiv.symm_apply_eq
lemma eq_symm_apply (h : α ≃ᵢ β) {x : α} {y : β} :
x = h.symm y ↔ h x = y :=
h.to_equiv.eq_symm_apply
lemma symm_comp_self (h : α ≃ᵢ β) : ⇑h.symm ∘ ⇑h = id :=
funext $ assume a, h.to_equiv.left_inv a
lemma self_comp_symm (h : α ≃ᵢ β) : ⇑h ∘ ⇑h.symm = id :=
funext $ assume a, h.to_equiv.right_inv a
lemma range_coe (h : α ≃ᵢ β) : range h = univ :=
eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩
lemma image_symm (h : α ≃ᵢ β) : image h.symm = preimage h :=
image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv
lemma preimage_symm (h : α ≃ᵢ β) : preimage h.symm = image h :=
(image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm
@[simp] lemma symm_trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : γ) :
(h₁.trans h₂).symm x = h₁.symm (h₂.symm x) := rfl
/-- The (bundled) homeomorphism associated to an isometric isomorphism. -/
protected def to_homeomorph (h : α ≃ᵢ β) : α ≃ₜ β :=
{ continuous_to_fun := h.continuous,
continuous_inv_fun := h.symm.continuous,
.. h }
@[simp] lemma coe_to_homeomorph (h : α ≃ᵢ β) : ⇑(h.to_homeomorph) = h := rfl
@[simp] lemma coe_to_homeomorph_symm (h : α ≃ᵢ β) : ⇑(h.to_homeomorph.symm) = h.symm := rfl
@[simp] lemma to_homeomorph_to_equiv (h : α ≃ᵢ β) :
h.to_homeomorph.to_equiv = h.to_equiv :=
rfl
/-- The group of isometries. -/
instance : group (α ≃ᵢ α) :=
{ one := isometric.refl _,
mul := λ e₁ e₂, e₁.trans e₂,
inv := isometric.symm,
mul_assoc := λ e₁ e₂ e₃, rfl,
one_mul := λ e, ext $ λ _, rfl,
mul_one := λ e, ext $ λ _, rfl,
mul_left_inv := λ e, ext e.apply_symm_apply }
@[simp] lemma coe_one : ⇑(1 : α ≃ᵢ α) = id := rfl
@[simp] lemma coe_mul (e₁ e₂ : α ≃ᵢ α) : ⇑(e₁ * e₂) = e₂ ∘ e₁ := rfl
lemma mul_apply (e₁ e₂ : α ≃ᵢ α) (x : α) : (e₁ * e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma inv_apply_self (e : α ≃ᵢ α) (x: α) : e⁻¹ (e x) = x := e.symm_apply_apply x
@[simp] lemma apply_inv_self (e : α ≃ᵢ α) (x: α) : e (e⁻¹ x) = x := e.apply_symm_apply x
section normed_group
variables {G : Type*} [normed_group G]
/-- Addition `y ↦ y + x` as an `isometry`. -/
protected def add_right (x : G) : G ≃ᵢ G :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _,
.. equiv.add_right x }
@[simp] lemma add_right_to_equiv (x : G) :
(isometric.add_right x).to_equiv = equiv.add_right x := rfl
@[simp] lemma coe_add_right (x : G) : (isometric.add_right x : G → G) = λ y, y + x := rfl
lemma add_right_apply (x y : G) : (isometric.add_right x : G → G) y = y + x := rfl
@[simp] lemma add_right_symm (x : G) :
(isometric.add_right x).symm = isometric.add_right (-x) :=
ext $ λ y, rfl
/-- Addition `y ↦ x + y` as an `isometry`. -/
protected def add_left (x : G) : G ≃ᵢ G :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _,
to_equiv := equiv.add_left x }
@[simp] lemma add_left_to_equiv (x : G) :
(isometric.add_left x).to_equiv = equiv.add_left x := rfl
@[simp] lemma coe_add_left (x : G) : ⇑(isometric.add_left x) = (+) x := rfl
@[simp] lemma add_left_symm (x : G) :
(isometric.add_left x).symm = isometric.add_left (-x) :=
ext $ λ y, rfl
variable (G)
/-- Negation `x ↦ -x` as an `isometry`. -/
protected def neg : G ≃ᵢ G :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _,
to_equiv := equiv.neg G }
variable {G}
@[simp] lemma neg_symm : (isometric.neg G).symm = isometric.neg G := rfl
@[simp] lemma neg_to_equiv : (isometric.neg G).to_equiv = equiv.neg G := rfl
@[simp] lemma coe_neg : ⇑(isometric.neg G) = has_neg.neg := rfl
end normed_group
end isometric
/-- An isometry induces an isometric isomorphism between the source space and the
range of the isometry. -/
def isometry.isometric_on_range [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) :
α ≃ᵢ range f :=
{ isometry_to_fun := λx y, by simpa [subtype.edist_eq] using h x y,
.. equiv.set.range f h.injective }
@[simp] lemma isometry.isometric_on_range_apply [emetric_space α] [emetric_space β]
{f : α → β} (h : isometry f) (x : α) : h.isometric_on_range x = ⟨f x, mem_range_self _⟩ :=
rfl
/-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/
lemma algebra_map_isometry (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜']
[normed_algebra 𝕜 𝕜'] : isometry (algebra_map 𝕜 𝕜') :=
begin
refine isometry_emetric_iff_metric.2 (λx y, _),
rw [dist_eq_norm, dist_eq_norm, ← ring_hom.map_sub, norm_algebra_map_eq],
end
/-- The space of bounded sequences, with its sup norm -/
@[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ
open bounded_continuous_function metric topological_space
namespace Kuratowski_embedding
/-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/
variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α)
/-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in
a fixed countable set, if this set is dense. This map is given in the next definition,
without density assumptions. -/
def embedding_of_subset : ℓ_infty_ℝ :=
of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0))
(λ_, abs_dist_sub_le _ _ _)
lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl
/-- The embedding map is always a semi-contraction. -/
lemma embedding_of_subset_dist_le (a b : α) :
dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b :=
begin
refine (dist_le dist_nonneg).2 (λn, _),
simp only [embedding_of_subset_coe, real.dist_eq],
convert abs_dist_sub_le a b (x n) using 2,
ring
end
/-- When the reference set is dense, the embedding map is an isometry on its image. -/
lemma embedding_of_subset_isometry (H : dense_range x) : isometry (embedding_of_subset x) :=
begin
refine isometry_emetric_iff_metric.2 (λa b, _),
refine (embedding_of_subset_dist_le x a b).antisymm (le_of_forall_pos_le_add (λe epos, _)),
/- First step: find n with dist a (x n) < e -/
rcases metric.mem_closure_range_iff.1 (H a) (e/2) (half_pos epos) with ⟨n, hn⟩,
/- Second step: use the norm control at index n to conclude -/
have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n :=
by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] },
have := calc
dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _
... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring }
... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) :
by apply_rules [add_le_add_left, le_abs_self]
... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) :
begin rw C, apply_rules [add_le_add, mul_le_mul_of_nonneg_left, hn.le, le_refl], norm_num end
... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) :
by simp [← real.dist_eq, dist_coe_le_dist]
... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring,
simpa [dist_comm] using this
end
/-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/
theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] :
∃(f : α → ℓ_infty_ℝ), isometry f :=
begin
cases (univ : set α).eq_empty_or_nonempty with h h,
{ use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) },
{ /- We construct a map x : ℕ → α with dense image -/
rcases h with ⟨basepoint⟩,
haveI : inhabited α := ⟨basepoint⟩,
have : ∃s:set α, countable s ∧ dense s := exists_countable_dense α,
rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩,
rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩,
/- Use embedding_of_subset to construct the desired isometry -/
exact ⟨embedding_of_subset x, embedding_of_subset_isometry x (S_dense.mono x_range)⟩ }
end
end Kuratowski_embedding
open topological_space Kuratowski_embedding
/-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/
def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ :=
classical.some (Kuratowski_embedding.exists_isometric_embedding α)
/-- The Kuratowski embedding is an isometry -/
protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] :
isometry (Kuratowski_embedding α) :=
classical.some_spec (exists_isometric_embedding α)
/-- Version of the Kuratowski embedding for nonempty compacts -/
def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α]
[nonempty α] :
nonempty_compacts ℓ_infty_ℝ :=
⟨range (Kuratowski_embedding α), range_nonempty _,
compact_range (Kuratowski_embedding.isometry α).continuous⟩
|
e174b318d79a804b22eb518ec6736d0f03158024 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/set/intervals/ord_connected.lean | b0f3f1bec8817739526eba4cc646e48cda66e47e | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 9,461 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import data.set.intervals.unordered_interval
import data.set.lattice
import order.antichain
/-!
# Order-connected sets
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the
interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with
the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α` is a
`linear_ordered_field`, then this condition is also equivalent to `convex α s`.
In this file we prove that intersection of a family of `ord_connected` sets is `ord_connected` and
that all standard intervals are `ord_connected`.
-/
open_locale interval
open order_dual (to_dual of_dual)
namespace set
section preorder
variables {α β : Type*} [preorder α] [preorder β] {s t : set α}
/--
We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the
interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with
the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α` is a
`linear_ordered_field`, then this condition is also equivalent to `convex α s`.
-/
class ord_connected (s : set α) : Prop :=
(out' ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s) : Icc x y ⊆ s)
lemma ord_connected.out (h : ord_connected s) :
∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), Icc x y ⊆ s := h.1
lemma ord_connected_def : ord_connected s ↔ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), Icc x y ⊆ s :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
/-- It suffices to prove `[x, y] ⊆ s` for `x y ∈ s`, `x ≤ y`. -/
lemma ord_connected_iff : ord_connected s ↔ ∀ (x ∈ s) (y ∈ s), x ≤ y → Icc x y ⊆ s :=
ord_connected_def.trans
⟨λ hs x hx y hy hxy, hs hx hy, λ H x hx y hy z hz, H x hx y hy (le_trans hz.1 hz.2) hz⟩
lemma ord_connected_of_Ioo {α : Type*} [partial_order α] {s : set α}
(hs : ∀ (x ∈ s) (y ∈ s), x < y → Ioo x y ⊆ s) :
ord_connected s :=
begin
rw ord_connected_iff,
intros x hx y hy hxy,
rcases eq_or_lt_of_le hxy with rfl|hxy', { simpa },
rw [←Ioc_insert_left hxy, ←Ioo_insert_right hxy'],
exact insert_subset.2 ⟨hx, insert_subset.2 ⟨hy, hs x hx y hy hxy'⟩⟩,
end
lemma ord_connected.preimage_mono {f : β → α} (hs : ord_connected s) (hf : monotone f) :
ord_connected (f ⁻¹' s) :=
⟨λ x hx y hy z hz, hs.out hx hy ⟨hf hz.1, hf hz.2⟩⟩
lemma ord_connected.preimage_anti {f : β → α} (hs : ord_connected s) (hf : antitone f) :
ord_connected (f ⁻¹' s) :=
⟨λ x hx y hy z hz, hs.out hy hx ⟨hf hz.2, hf hz.1⟩⟩
protected lemma Icc_subset (s : set α) [hs : ord_connected s] {x y} (hx : x ∈ s) (hy : y ∈ s) :
Icc x y ⊆ s := hs.out hx hy
lemma ord_connected.inter {s t : set α} (hs : ord_connected s) (ht : ord_connected t) :
ord_connected (s ∩ t) :=
⟨λ x hx y hy, subset_inter (hs.out hx.1 hy.1) (ht.out hx.2 hy.2)⟩
instance ord_connected.inter' {s t : set α} [ord_connected s] [ord_connected t] :
ord_connected (s ∩ t) :=
ord_connected.inter ‹_› ‹_›
lemma ord_connected.dual {s : set α} (hs : ord_connected s) :
ord_connected (order_dual.of_dual ⁻¹' s) :=
⟨λ x hx y hy z hz, hs.out hy hx ⟨hz.2, hz.1⟩⟩
lemma ord_connected_dual {s : set α} : ord_connected (order_dual.of_dual ⁻¹' s) ↔ ord_connected s :=
⟨λ h, by simpa only [ord_connected_def] using h.dual, λ h, h.dual⟩
lemma ord_connected_sInter {S : set (set α)} (hS : ∀ s ∈ S, ord_connected s) :
ord_connected (⋂₀ S) :=
⟨λ x hx y hy, subset_sInter $ λ s hs, (hS s hs).out (hx s hs) (hy s hs)⟩
lemma ord_connected_Inter {ι : Sort*} {s : ι → set α} (hs : ∀ i, ord_connected (s i)) :
ord_connected (⋂ i, s i) :=
ord_connected_sInter $ forall_range_iff.2 hs
instance ord_connected_Inter' {ι : Sort*} {s : ι → set α} [∀ i, ord_connected (s i)] :
ord_connected (⋂ i, s i) :=
ord_connected_Inter ‹_›
lemma ord_connected_bInter {ι : Sort*} {p : ι → Prop} {s : Π (i : ι) (hi : p i), set α}
(hs : ∀ i hi, ord_connected (s i hi)) :
ord_connected (⋂ i hi, s i hi) :=
ord_connected_Inter $ λ i, ord_connected_Inter $ hs i
lemma ord_connected_pi {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s : set ι}
{t : Π i, set (α i)} (h : ∀ i ∈ s, ord_connected (t i)) : ord_connected (s.pi t) :=
⟨λ x hx y hy z hz i hi, (h i hi).out (hx i hi) (hy i hi) ⟨hz.1 i, hz.2 i⟩⟩
instance ord_connected_pi' {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s : set ι}
{t : Π i, set (α i)} [h : ∀ i, ord_connected (t i)] : ord_connected (s.pi t) :=
ord_connected_pi $ λ i hi, h i
@[instance] lemma ord_connected_Ici {a : α} : ord_connected (Ici a) :=
⟨λ x hx y hy z hz, le_trans hx hz.1⟩
@[instance] lemma ord_connected_Iic {a : α} : ord_connected (Iic a) :=
⟨λ x hx y hy z hz, le_trans hz.2 hy⟩
@[instance] lemma ord_connected_Ioi {a : α} : ord_connected (Ioi a) :=
⟨λ x hx y hy z hz, lt_of_lt_of_le hx hz.1⟩
@[instance] lemma ord_connected_Iio {a : α} : ord_connected (Iio a) :=
⟨λ x hx y hy z hz, lt_of_le_of_lt hz.2 hy⟩
@[instance] lemma ord_connected_Icc {a b : α} : ord_connected (Icc a b) :=
ord_connected_Ici.inter ord_connected_Iic
@[instance] lemma ord_connected_Ico {a b : α} : ord_connected (Ico a b) :=
ord_connected_Ici.inter ord_connected_Iio
@[instance] lemma ord_connected_Ioc {a b : α} : ord_connected (Ioc a b) :=
ord_connected_Ioi.inter ord_connected_Iic
@[instance] lemma ord_connected_Ioo {a b : α} : ord_connected (Ioo a b) :=
ord_connected_Ioi.inter ord_connected_Iio
@[instance] lemma ord_connected_singleton {α : Type*} [partial_order α] {a : α} :
ord_connected ({a} : set α) :=
by { rw ← Icc_self, exact ord_connected_Icc }
@[instance] lemma ord_connected_empty : ord_connected (∅ : set α) := ⟨λ x, false.elim⟩
@[instance] lemma ord_connected_univ : ord_connected (univ : set α) := ⟨λ _ _ _ _, subset_univ _⟩
/-- In a dense order `α`, the subtype from an `ord_connected` set is also densely ordered. -/
instance [densely_ordered α] {s : set α} [hs : ord_connected s] :
densely_ordered s :=
⟨λ a b (h : (a : α) < b), let ⟨x, H⟩ := exists_between h in
⟨⟨x, (hs.out a.2 b.2) (Ioo_subset_Icc_self H)⟩, H⟩ ⟩
@[instance] lemma ord_connected_preimage {F : Type*} [order_hom_class F α β] (f : F) {s : set β}
[hs : ord_connected s] : ord_connected (f ⁻¹' s) :=
⟨λ x hx y hy z hz, hs.out hx hy ⟨order_hom_class.mono _ hz.1, order_hom_class.mono _ hz.2⟩⟩
@[instance] lemma ord_connected_image {E : Type*} [order_iso_class E α β] (e : E) {s : set α}
[hs : ord_connected s] : ord_connected (e '' s) :=
by { erw [(e : α ≃o β).image_eq_preimage], apply ord_connected_preimage }
@[instance] lemma ord_connected_range {E : Type*} [order_iso_class E α β] (e : E) :
ord_connected (range e) :=
by simp_rw [← image_univ, ord_connected_image e]
@[simp] lemma dual_ord_connected_iff {s : set α} :
ord_connected (of_dual ⁻¹' s) ↔ ord_connected s :=
begin
simp_rw [ord_connected_def, to_dual.surjective.forall, dual_Icc, subtype.forall'],
exact forall_swap
end
@[instance] lemma dual_ord_connected {s : set α} [ord_connected s] :
ord_connected (of_dual ⁻¹' s) :=
dual_ord_connected_iff.2 ‹_›
end preorder
section partial_order
variables {α : Type*} [partial_order α] {s : set α}
protected lemma _root_.is_antichain.ord_connected (hs : is_antichain (≤) s) : s.ord_connected :=
⟨λ x hx y hy z hz, by { obtain rfl := hs.eq hx hy (hz.1.trans hz.2),
rw [Icc_self, mem_singleton_iff] at hz, rwa hz }⟩
end partial_order
section linear_order
variables {α : Type*} [linear_order α] {s : set α} {x : α}
@[instance] lemma ord_connected_uIcc {a b : α} : ord_connected [a, b] := ord_connected_Icc
@[instance] lemma ord_connected_uIoc {a b : α} : ord_connected (Ι a b) := ord_connected_Ioc
lemma ord_connected.uIcc_subset (hs : ord_connected s) ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s) :
[x, y] ⊆ s :=
hs.out (min_rec' (∈ s) hx hy) (max_rec' (∈ s) hx hy)
lemma ord_connected.uIoc_subset (hs : ord_connected s) ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s) :
Ι x y ⊆ s :=
Ioc_subset_Icc_self.trans $ hs.uIcc_subset hx hy
lemma ord_connected_iff_uIcc_subset :
ord_connected s ↔ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), [x, y] ⊆ s :=
⟨λ h, h.uIcc_subset, λ H, ⟨λ x hx y hy, Icc_subset_uIcc.trans $ H hx hy⟩⟩
lemma ord_connected_of_uIcc_subset_left (h : ∀ y ∈ s, [x, y] ⊆ s) :
ord_connected s :=
ord_connected_iff_uIcc_subset.2 $ λ y hy z hz,
calc [y, z] ⊆ [y, x] ∪ [x, z] : uIcc_subset_uIcc_union_uIcc
... = [x, y] ∪ [x, z] : by rw [uIcc_comm]
... ⊆ s : union_subset (h y hy) (h z hz)
lemma ord_connected_iff_uIcc_subset_left (hx : x ∈ s) :
ord_connected s ↔ ∀ ⦃y⦄, y ∈ s → [x, y] ⊆ s :=
⟨λ hs, hs.uIcc_subset hx, ord_connected_of_uIcc_subset_left⟩
lemma ord_connected_iff_uIcc_subset_right (hx : x ∈ s) :
ord_connected s ↔ ∀ ⦃y⦄, y ∈ s → [y, x] ⊆ s :=
by simp_rw [ord_connected_iff_uIcc_subset_left hx, uIcc_comm]
end linear_order
end set
|
28b9dc385e5de91526a7c2f580a7e63b1e41885f | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/category_theory/comma.lean | 558acb0d3009fe0353649a9c24e9be7fa5e91e54 | [
"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 | 6,883 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johan Commelin, Bhavik Mehta
-/
import category_theory.natural_isomorphism
/-!
# Comma categories
A comma category is a construction in category theory, which builds a category out of two functors
with a common codomain. Specifically, for functors `L : A ⥤ T` and `R : B ⥤ T`, an object in
`comma L R` is a morphism `hom : L.obj left ⟶ R.obj right` for some objects `left : A` and
`right : B`, and a morphism in `comma L R` between `hom : L.obj left ⟶ R.obj right` and
`hom' : L.obj left' ⟶ R.obj right'` is a commutative square
L.obj left ⟶ L.obj left'
| |
hom | | hom'
↓ ↓
R.obj right ⟶ R.obj right',
where the top and bottom morphism come from morphisms `left ⟶ left'` and `right ⟶ right'`,
respectively.
## Main definitions
* `comma L R`: the comma category of the functors `L` and `R`.
* `over X`: the over category of the object `X` (developed in `over.lean`).
* `under X`: the under category of the object `X` (also developed in `over.lean`).
* `arrow T`: the arrow category of the category `T` (developed in `arrow.lean`).
## References
* https://ncatlab.org/nlab/show/comma+category
## Tags
comma, slice, coslice, over, under, arrow
-/
namespace category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {A : Type u₁} [category.{v₁} A]
variables {B : Type u₂} [category.{v₂} B]
variables {T : Type u₃} [category.{v₃} T]
/-- The objects of the comma category are triples of an object `left : A`, an object
`right : B` and a morphism `hom : L.obj left ⟶ R.obj right`. -/
structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) :=
(left : A . obviously)
(right : B . obviously)
(hom : L.obj left ⟶ R.obj right)
-- Satisfying the inhabited linter
instance comma.inhabited [inhabited T] : inhabited (comma (𝟭 T) (𝟭 T)) :=
{ default :=
{ left := default T,
right := default T,
hom := 𝟙 (default T) } }
variables {L : A ⥤ T} {R : B ⥤ T}
/-- A morphism between two objects in the comma category is a commutative square connecting the
morphisms coming from the two objects using morphisms in the image of the functors `L` and `R`.
-/
@[ext] structure comma_morphism (X Y : comma L R) :=
(left : X.left ⟶ Y.left . obviously)
(right : X.right ⟶ Y.right . obviously)
(w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously)
-- Satisfying the inhabited linter
instance comma_morphism.inhabited [inhabited (comma L R)] :
inhabited (comma_morphism (default (comma L R)) (default (comma L R))) :=
{ default :=
{ left := 𝟙 _,
right := 𝟙 _ } }
restate_axiom comma_morphism.w'
attribute [simp, reassoc] comma_morphism.w
instance comma_category : category (comma L R) :=
{ hom := comma_morphism,
id := λ X,
{ left := 𝟙 X.left,
right := 𝟙 X.right },
comp := λ X Y Z f g,
{ left := f.left ≫ g.left,
right := f.right ≫ g.right } }
namespace comma
section
variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z}
@[simp] lemma id_left : ((𝟙 X) : comma_morphism X X).left = 𝟙 X.left := rfl
@[simp] lemma id_right : ((𝟙 X) : comma_morphism X X).right = 𝟙 X.right := rfl
@[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl
end
variables (L) (R)
/-- The functor sending an object `X` in the comma category to `X.left`. -/
@[simps]
def fst : comma L R ⥤ A :=
{ obj := λ X, X.left,
map := λ _ _ f, f.left }
/-- The functor sending an object `X` in the comma category to `X.right`. -/
@[simps]
def snd : comma L R ⥤ B :=
{ obj := λ X, X.right,
map := λ _ _ f, f.right }
/-- We can interpret the commutative square constituting a morphism in the comma category as a
natural transformation between the functors `fst ⋙ L` and `snd ⋙ R` from the comma category
to `T`, where the components are given by the morphism that constitutes an object of the comma
category. -/
@[simps]
def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R :=
{ app := λ X, X.hom }
section
variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T}
/-- A natural transformation `L₁ ⟶ L₂` induces a functor `comma L₂ R ⥤ comma L₁ R`. -/
@[simps]
def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := l.app X.left ≫ X.hom },
map := λ X Y f,
{ left := f.left,
right := f.right } }
/-- The functor `comma L R ⥤ comma L R` induced by the identity natural transformation on `L` is
naturally isomorphic to the identity functor. -/
@[simps]
def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- The functor `comma L₁ R ⥤ comma L₃ R` induced by the composition of two natural transformations
`l : L₁ ⟶ L₂` and `l' : L₂ ⟶ L₃` is naturally isomorphic to the composition of the two functors
induced by these natural transformations. -/
@[simps]
def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) :
(map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- A natural transformation `R₁ ⟶ R₂` induces a functor `comma L R₁ ⥤ comma L R₂`. -/
@[simps]
def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := X.hom ≫ r.app X.right },
map := λ X Y f,
{ left := f.left,
right := f.right } }
/-- The functor `comma L R ⥤ comma L R` induced by the identity natural transformation on `R` is
naturally isomorphic to the identity functor. -/
@[simps]
def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- The functor `comma L R₁ ⥤ comma L R₃` induced by the composition of the natural transformations
`r : R₁ ⟶ R₂` and `r' : R₂ ⟶ R₃` is naturally isomorphic to the composition of the functors
induced by these natural transformations. -/
@[simps]
def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) :
(map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
end
end comma
end category_theory
|
5390861f32874696d2f2dfa628c4347ac1003952 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Lean/MetavarContext.lean | f69a69c62a504e22bf6a3781a1325865e8386295 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 53,740 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.MonadCache
import Lean.LocalContext
namespace Lean
/-
The metavariable context stores metavariable declarations and their
assignments. It is used in the elaborator, tactic framework, unifier
(aka `isDefEq`), and type class resolution (TC). First, we list all
the requirements imposed by these modules.
- We may invoke TC while executing `isDefEq`. We need this feature to
be able to solve unification problems such as:
```
f ?a (ringAdd ?s) ?x ?y =?= f Int intAdd n m
```
where `(?a : Type) (?s : Ring ?a) (?x ?y : ?a)`
During `isDefEq` (i.e., unification), it will need to solve the constrain
```
ringAdd ?s =?= intAdd
```
We say `ringAdd ?s` is stuck because it cannot be reduced until we
synthesize the term `?s : Ring ?a` using TC. This can be done since we
have assigned `?a := Int` when solving `?a =?= Int`.
- TC uses `isDefEq`, and `isDefEq` may create TC problems as shown
above. Thus, we may have nested TC problems.
- `isDefEq` extends the local context when going inside binders. Thus,
the local context for nested TC may be an extension of the local
context for outer TC.
- TC should not assign metavariables created by the elaborator, simp,
tactic framework, and outer TC problems. Reason: TC commits to the
first solution it finds. Consider the TC problem `Coe Nat ?x`,
where `?x` is a metavariable created by the caller. There are many
solutions to this problem (e.g., `?x := Int`, `?x := Real`, ...),
and it doesn’t make sense to commit to the first one since TC does
not know the constraints the caller may impose on `?x` after the
TC problem is solved.
Remark: we claim it is not feasible to make the whole system backtrackable,
and allow the caller to backtrack back to TC and ask it for another solution
if the first one found did not work. We claim it would be too inefficient.
- TC metavariables should not leak outside of TC. Reason: we want to
get rid of them after we synthesize the instance.
- `simp` invokes `isDefEq` for matching the left-hand-side of
equations to terms in our goal. Thus, it may invoke TC indirectly.
- In Lean3, we didn’t have to create a fresh pattern for trying to
match the left-hand-side of equations when executing `simp`. We had a
mechanism called "tmp" metavariables. It avoided this overhead, but it
created many problems since `simp` may indirectly call TC which may
recursively call TC. Moreover, we may want to allow TC to invoke
tactics in the future. Thus, when `simp` invokes `isDefEq`, it may indirectly invoke
a tactic and `simp` itself. The Lean3 approach assumed that
metavariables were short-lived, this is not true in Lean4, and to some
extent was also not true in Lean3 since `simp`, in principle, could
trigger an arbitrary number of nested TC problems.
- Here are some possible call stack traces we could have in Lean3 (and Lean4).
```
Elaborator (-> TC -> isDefEq)+
Elaborator -> isDefEq (-> TC -> isDefEq)*
Elaborator -> simp -> isDefEq (-> TC -> isDefEq)*
```
In Lean4, TC may also invoke tactics in the future.
- In Lean3 and Lean4, TC metavariables are not really short-lived. We
solve an arbitrary number of unification problems, and we may have
nested TC invocations.
- TC metavariables do not share the same local context even in the
same invocation. In the C++ and Lean implementations we use a trick to
ensure they do:
https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L3583-L3594
- Metavariables may be natural, synthetic or syntheticOpaque.
a) Natural metavariables may be assigned by unification (i.e., `isDefEq`).
b) Synthetic metavariables may still be assigned by unification,
but whenever possible `isDefEq` will avoid the assignment. For example,
if we have the unification constaint `?m =?= ?n`, where `?m` is synthetic,
but `?n` is not, `isDefEq` solves it by using the assignment `?n := ?m`.
We use synthetic metavariables for type class resolution.
Any module that creates synthetic metavariables, must also check
whether they have been assigned by `isDefEq`, and then still synthesize
them, and check whether the sythesized result is compatible with the one
assigned by `isDefEq`.
c) SyntheticOpaque metavariables are never assigned by `isDefEq`.
That is, the constraint `?n =?= Nat.succ Nat.zero` always fail
if `?n` is a syntheticOpaque metavariable. This kind of metavariable
is created by tactics such as `intro`. Reason: in the tactic framework,
subgoals as represented as metavariables, and a subgoal `?n` is considered
as solved whenever the metavariable is assigned.
This distinction was not precise in Lean3 and produced
counterintuitive behavior. For example, the following hack was added
in Lean3 to work around one of these issues:
https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L2751
- When creating lambda/forall expressions, we need to convert/abstract
free variables and convert them to bound variables. Now, suppose we a
trying to create a lambda/forall expression by abstracting free
variable `xs` and a term `t[?m]` which contains a metavariable `?m`,
and the local context of `?m` contains `xs`. The term
```
fun xs => t[?m]
```
will be ill-formed if we later assign a term `s` to `?m`, and
`s` contains free variables in `xs`. We address this issue by changing
the free variable abstraction procedure. We consider two cases: `?m`
is natural, `?m` is synthetic. Assume the type of `?m` is
`A[xs]`. Then, in both cases we create an auxiliary metavariable `?n` with
type `forall xs => A[xs]`, and local context := local context of `?m` - `xs`.
In both cases, we produce the term `fun xs => t[?n xs]`
1- If `?m` is natural or synthetic, then we assign `?m := ?n xs`, and we produce
the term `fun xs => t[?n xs]`
2- If `?m` is syntheticOpaque, then we mark `?n` as a syntheticOpaque variable.
However, `?n` is managed by the metavariable context itself.
We say we have a "delayed assignment" `?n xs := ?m`.
That is, after a term `s` is assigned to `?m`, and `s`
does not contain metavariables, we replace any occurrence
`?n ts` with `s[xs := ts]`.
Gruesome details:
- When we create the type `forall xs => A` for `?n`, we may
encounter the same issue if `A` contains metavariables. So, the
process above is recursive. We claim it terminates because we keep
creating new metavariables with smaller local contexts.
- Suppose, we have `t[?m]` and we want to create a let-expression by
abstracting a let-decl free variable `x`, and the local context of
`?m` contatins `x`. Similarly to the previous case
```
let x : T := v; t[?m]
```
will be ill-formed if we later assign a term `s` to `?m`, and
`s` contains free variable `x`. Again, assume the type of `?m` is `A[x]`.
1- If `?m` is natural or synthetic, then we create `?n : (let x : T := v; A[x])` with
and local context := local context of `?m` - `x`, we assign `?m := ?n`,
and produce the term `let x : T := v; t[?n]`. That is, we are just making
sure `?n` must never be assigned to a term containing `x`.
2- If `?m` is syntheticOpaque, we create a fresh syntheticOpaque `?n`
with type `?n : T -> (let x : T := v; A[x])` and local context := local context of `?m` - `x`,
create the delayed assignment `?n #[x] := ?m`, and produce the term `let x : T := v; t[?n x]`.
Now suppose we assign `s` to `?m`. We do not assign the term `fun (x : T) => s` to `?n`, since
`fun (x : T) => s` may not even be type correct. Instead, we just replace applications `?n r`
with `s[x/r]`. The term `r` may not necessarily be a bound variable. For example, a tactic
may have reduced `let x : T := v; t[?n x]` into `t[?n v]`.
We are essentially using the pair "delayed assignment + application" to implement a delayed
substitution.
- We use TC for implementing coercions. Both Joe Hendrix and Reid Barton
reported a nasty limitation. In Lean3, TC will not be used if there are
metavariables in the TC problem. For example, the elaborator will not try
to synthesize `Coe Nat ?x`. This is good, but this constraint is too
strict for problems such as `Coe (Vector Bool ?n) (BV ?n)`. The coercion
exists independently of `?n`. Thus, during TC, we want `isDefEq` to throw
an exception instead of return `false` whenever it tries to assign
a metavariable owned by its caller. The idea is to sign to the caller that
it cannot solve the TC problem at this point, and more information is needed.
That is, the caller must make progress an assign its metavariables before
trying to invoke TC again.
In Lean4, we are using a simpler design for the `MetavarContext`.
- No distinction betwen temporary and regular metavariables.
- Metavariables have a `depth` Nat field.
- MetavarContext also has a `depth` field.
- We bump the `MetavarContext` depth when we create a nested problem.
Example: Elaborator (depth = 0) -> Simplifier matcher (depth = 1) -> TC (level = 2) -> TC (level = 3) -> ...
- When `MetavarContext` is at depth N, `isDefEq` does not assign variables from `depth < N`.
- Metavariables from depth N+1 must be fully assigned before we return to level N.
- New design even allows us to invoke tactics from TC.
* Main concern
We don't have tmp metavariables anymore in Lean4. Thus, before trying to match
the left-hand-side of an equation in `simp`. We first must bump the level of the `MetavarContext`,
create fresh metavariables, then create a new pattern by replacing the free variable on the left-hand-side with
these metavariables. We are hoping to minimize this overhead by
- Using better indexing data structures in `simp`. They should reduce the number of time `simp` must invoke `isDefEq`.
- Implementing `isDefEqApprox` which ignores metavariables and returns only `false` or `undef`.
It is a quick filter that allows us to fail quickly and avoid the creation of new fresh metavariables,
and a new pattern.
- Adding built-in support for arithmetic, Logical connectives, etc. Thus, we avoid a bunch of lemmas in the simp set.
- Adding support for AC-rewriting. In Lean3, users use AC lemmas as
rewriting rules for "sorting" terms. This is inefficient, requires
a quadratic number of rewrite steps, and does not preserve the
structure of the goal.
The temporary metavariables were also used in the "app builder" module used in Lean3. The app builder uses
`isDefEq`. So, it could, in principle, invoke an arbitrary number of nested TC problems. However, in Lean3,
all app builder uses are controlled. That is, it is mainly used to synthesize implicit arguments using
very simple unification and/or non-nested TC. So, if the "app builder" becomes a bottleneck without tmp metavars,
we may solve the issue by implementing `isDefEqCheap` that never invokes TC and uses tmp metavars.
-/
structure LocalInstance where
className : Name
fvar : Expr
deriving Inhabited
abbrev LocalInstances := Array LocalInstance
instance : BEq LocalInstance where
beq i₁ i₂ := i₁.fvar == i₂.fvar
/-- Remove local instance with the given `fvarId`. Do nothing if `localInsts` does not contain any free variable with id `fvarId`. -/
def LocalInstances.erase (localInsts : LocalInstances) (fvarId : FVarId) : LocalInstances :=
match localInsts.findIdx? (fun inst => inst.fvar.fvarId! == fvarId) with
| some idx => localInsts.eraseIdx idx
| _ => localInsts
inductive MetavarKind where
| natural
| synthetic
| syntheticOpaque
deriving Inhabited
def MetavarKind.isSyntheticOpaque : MetavarKind → Bool
| MetavarKind.syntheticOpaque => true
| _ => false
def MetavarKind.isNatural : MetavarKind → Bool
| MetavarKind.natural => true
| _ => false
structure MetavarDecl where
userName : Name := Name.anonymous
lctx : LocalContext
type : Expr
depth : Nat
localInstances : LocalInstances
kind : MetavarKind
numScopeArgs : Nat := 0 -- See comment at `CheckAssignment` `Meta/ExprDefEq.lean`
deriving Inhabited
@[export lean_mk_metavar_decl]
def mkMetavarDeclEx (userName : Name) (lctx : LocalContext) (type : Expr) (depth : Nat) (localInstances : LocalInstances) (kind : MetavarKind) : MetavarDecl :=
{ userName := userName, lctx := lctx, type := type, depth := depth, localInstances := localInstances, kind := kind }
/--
A delayed assignment for a metavariable `?m`. It represents an assignment of the form
`?m := (fun fvars => val)`. The local context `lctx` provides the declarations for `fvars`.
Note that `fvars` may not be defined in the local context for `?m`.
- TODO: after we delete the old frontend, we can remove the field `lctx`.
This field is only used in old C++ implementation. -/
structure DelayedMetavarAssignment where
lctx : LocalContext
fvars : Array Expr
val : Expr
open Std (HashMap PersistentHashMap)
structure MetavarContext where
depth : Nat := 0
lDepth : PersistentHashMap MVarId Nat := {}
decls : PersistentHashMap MVarId MetavarDecl := {}
lAssignment : PersistentHashMap MVarId Level := {}
eAssignment : PersistentHashMap MVarId Expr := {}
dAssignment : PersistentHashMap MVarId DelayedMetavarAssignment := {}
class MonadMCtx (m : Type → Type) where
getMCtx : m MetavarContext
modifyMCtx : (MetavarContext → MetavarContext) → m Unit
export MonadMCtx (getMCtx modifyMCtx)
instance (m n) [MonadLift m n] [MonadMCtx m] : MonadMCtx n where
getMCtx := liftM (getMCtx : m _)
modifyMCtx := fun f => liftM (modifyMCtx f : m _)
namespace MetavarContext
instance : Inhabited MetavarContext := ⟨{}⟩
@[export lean_mk_metavar_ctx]
def mkMetavarContext : Unit → MetavarContext := fun _ => {}
/- Low level API for adding/declaring metavariable declarations.
It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`.
It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/
def addExprMVarDecl (mctx : MetavarContext)
(mvarId : MVarId)
(userName : Name)
(lctx : LocalContext)
(localInstances : LocalInstances)
(type : Expr)
(kind : MetavarKind := MetavarKind.natural)
(numScopeArgs : Nat := 0) : MetavarContext :=
{ mctx with
decls := mctx.decls.insert mvarId {
userName := userName,
lctx := lctx,
localInstances := localInstances,
type := type,
depth := mctx.depth,
kind := kind,
numScopeArgs := numScopeArgs } }
def addExprMVarDeclExp (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) (lctx : LocalContext) (localInstances : LocalInstances)
(type : Expr) (kind : MetavarKind) : MetavarContext :=
addExprMVarDecl mctx mvarId userName lctx localInstances type kind
/- Low level API for adding/declaring universe level metavariable declarations.
It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`.
It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/
def addLevelMVarDecl (mctx : MetavarContext) (mvarId : MVarId) : MetavarContext :=
{ mctx with lDepth := mctx.lDepth.insert mvarId mctx.depth }
def findDecl? (mctx : MetavarContext) (mvarId : MVarId) : Option MetavarDecl :=
mctx.decls.find? mvarId
def getDecl (mctx : MetavarContext) (mvarId : MVarId) : MetavarDecl :=
match mctx.decls.find? mvarId with
| some decl => decl
| none => panic! "unknown metavariable"
def findUserName? (mctx : MetavarContext) (userName : Name) : Option MVarId :=
let search : Except MVarId Unit := mctx.decls.forM fun mvarId decl =>
if decl.userName == userName then throw mvarId else pure ()
match search with
| Except.ok _ => none
| Except.error mvarId => some mvarId
def setMVarKind (mctx : MetavarContext) (mvarId : MVarId) (kind : MetavarKind) : MetavarContext :=
let decl := mctx.getDecl mvarId
{ mctx with decls := mctx.decls.insert mvarId { decl with kind := kind } }
def setMVarUserName (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) : MetavarContext :=
let decl := mctx.getDecl mvarId
{ mctx with decls := mctx.decls.insert mvarId { decl with userName := userName } }
/- Update the type of the given metavariable. This function assumes the new type is
definitionally equal to the current one -/
def setMVarType (mctx : MetavarContext) (mvarId : MVarId) (type : Expr) : MetavarContext :=
let decl := mctx.getDecl mvarId
{ mctx with decls := mctx.decls.insert mvarId { decl with type := type } }
def findLevelDepth? (mctx : MetavarContext) (mvarId : MVarId) : Option Nat :=
mctx.lDepth.find? mvarId
def getLevelDepth (mctx : MetavarContext) (mvarId : MVarId) : Nat :=
match mctx.findLevelDepth? mvarId with
| some d => d
| none => panic! "unknown metavariable"
def isAnonymousMVar (mctx : MetavarContext) (mvarId : MVarId) : Bool :=
match mctx.findDecl? mvarId with
| none => false
| some mvarDecl => mvarDecl.userName.isAnonymous
def renameMVar (mctx : MetavarContext) (mvarId : MVarId) (newUserName : Name) : MetavarContext :=
match mctx.findDecl? mvarId with
| none => panic! "unknown metavariable"
| some mvarDecl => { mctx with decls := mctx.decls.insert mvarId { mvarDecl with userName := newUserName } }
def assignLevel (m : MetavarContext) (mvarId : MVarId) (val : Level) : MetavarContext :=
{ m with lAssignment := m.lAssignment.insert mvarId val }
def assignExpr (m : MetavarContext) (mvarId : MVarId) (val : Expr) : MetavarContext :=
{ m with eAssignment := m.eAssignment.insert mvarId val }
def assignDelayed (m : MetavarContext) (mvarId : MVarId) (lctx : LocalContext) (fvars : Array Expr) (val : Expr) : MetavarContext :=
{ m with dAssignment := m.dAssignment.insert mvarId { lctx := lctx, fvars := fvars, val := val } }
def getLevelAssignment? (m : MetavarContext) (mvarId : MVarId) : Option Level :=
m.lAssignment.find? mvarId
def getExprAssignment? (m : MetavarContext) (mvarId : MVarId) : Option Expr :=
m.eAssignment.find? mvarId
def getDelayedAssignment? (m : MetavarContext) (mvarId : MVarId) : Option DelayedMetavarAssignment :=
m.dAssignment.find? mvarId
def isLevelAssigned (m : MetavarContext) (mvarId : MVarId) : Bool :=
m.lAssignment.contains mvarId
def isExprAssigned (m : MetavarContext) (mvarId : MVarId) : Bool :=
m.eAssignment.contains mvarId
def isDelayedAssigned (m : MetavarContext) (mvarId : MVarId) : Bool :=
m.dAssignment.contains mvarId
def eraseDelayed (m : MetavarContext) (mvarId : MVarId) : MetavarContext :=
{ m with dAssignment := m.dAssignment.erase mvarId }
/- Given a sequence of delayed assignments
```
mvarId₁ := mvarId₂ ...;
...
mvarIdₙ := mvarId_root ... -- where `mvarId_root` is not delayed assigned
```
in `mctx`, `getDelayedRoot mctx mvarId₁` return `mvarId_root`.
If `mvarId₁` is not delayed assigned then return `mvarId₁` -/
partial def getDelayedRoot (m : MetavarContext) : MVarId → MVarId
| mvarId => match getDelayedAssignment? m mvarId with
| some d => match d.val.getAppFn with
| Expr.mvar mvarId _ => getDelayedRoot m mvarId
| _ => mvarId
| none => mvarId
def isLevelAssignable (mctx : MetavarContext) (mvarId : MVarId) : Bool :=
match mctx.lDepth.find? mvarId with
| some d => d == mctx.depth
| _ => panic! "unknown universe metavariable"
def isExprAssignable (mctx : MetavarContext) (mvarId : MVarId) : Bool :=
let decl := mctx.getDecl mvarId
decl.depth == mctx.depth
def incDepth (mctx : MetavarContext) : MetavarContext :=
{ mctx with depth := mctx.depth + 1 }
/-- Return true iff the given level contains an assigned metavariable. -/
def hasAssignedLevelMVar (mctx : MetavarContext) : Level → Bool
| Level.succ lvl _ => lvl.hasMVar && hasAssignedLevelMVar mctx lvl
| Level.max lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignedLevelMVar mctx lvl₁) || (lvl₂.hasMVar && hasAssignedLevelMVar mctx lvl₂)
| Level.imax lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignedLevelMVar mctx lvl₁) || (lvl₂.hasMVar && hasAssignedLevelMVar mctx lvl₂)
| Level.mvar mvarId _ => mctx.isLevelAssigned mvarId
| Level.zero _ => false
| Level.param _ _ => false
/-- Return `true` iff expression contains assigned (level/expr) metavariables or delayed assigned mvars -/
def hasAssignedMVar (mctx : MetavarContext) : Expr → Bool
| Expr.const _ lvls _ => lvls.any (hasAssignedLevelMVar mctx)
| Expr.sort lvl _ => hasAssignedLevelMVar mctx lvl
| Expr.app f a _ => (f.hasMVar && hasAssignedMVar mctx f) || (a.hasMVar && hasAssignedMVar mctx a)
| Expr.letE _ t v b _ => (t.hasMVar && hasAssignedMVar mctx t) || (v.hasMVar && hasAssignedMVar mctx v) || (b.hasMVar && hasAssignedMVar mctx b)
| Expr.forallE _ d b _ => (d.hasMVar && hasAssignedMVar mctx d) || (b.hasMVar && hasAssignedMVar mctx b)
| Expr.lam _ d b _ => (d.hasMVar && hasAssignedMVar mctx d) || (b.hasMVar && hasAssignedMVar mctx b)
| Expr.fvar _ _ => false
| Expr.bvar _ _ => false
| Expr.lit _ _ => false
| Expr.mdata _ e _ => e.hasMVar && hasAssignedMVar mctx e
| Expr.proj _ _ e _ => e.hasMVar && hasAssignedMVar mctx e
| Expr.mvar mvarId _ => mctx.isExprAssigned mvarId || mctx.isDelayedAssigned mvarId
/-- Return true iff the given level contains a metavariable that can be assigned. -/
def hasAssignableLevelMVar (mctx : MetavarContext) : Level → Bool
| Level.succ lvl _ => lvl.hasMVar && hasAssignableLevelMVar mctx lvl
| Level.max lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignableLevelMVar mctx lvl₁) || (lvl₂.hasMVar && hasAssignableLevelMVar mctx lvl₂)
| Level.imax lvl₁ lvl₂ _ => (lvl₁.hasMVar && hasAssignableLevelMVar mctx lvl₁) || (lvl₂.hasMVar && hasAssignableLevelMVar mctx lvl₂)
| Level.mvar mvarId _ => mctx.isLevelAssignable mvarId
| Level.zero _ => false
| Level.param _ _ => false
/-- Return `true` iff expression contains a metavariable that can be assigned. -/
def hasAssignableMVar (mctx : MetavarContext) : Expr → Bool
| Expr.const _ lvls _ => lvls.any (hasAssignableLevelMVar mctx)
| Expr.sort lvl _ => hasAssignableLevelMVar mctx lvl
| Expr.app f a _ => (f.hasMVar && hasAssignableMVar mctx f) || (a.hasMVar && hasAssignableMVar mctx a)
| Expr.letE _ t v b _ => (t.hasMVar && hasAssignableMVar mctx t) || (v.hasMVar && hasAssignableMVar mctx v) || (b.hasMVar && hasAssignableMVar mctx b)
| Expr.forallE _ d b _ => (d.hasMVar && hasAssignableMVar mctx d) || (b.hasMVar && hasAssignableMVar mctx b)
| Expr.lam _ d b _ => (d.hasMVar && hasAssignableMVar mctx d) || (b.hasMVar && hasAssignableMVar mctx b)
| Expr.fvar _ _ => false
| Expr.bvar _ _ => false
| Expr.lit _ _ => false
| Expr.mdata _ e _ => e.hasMVar && hasAssignableMVar mctx e
| Expr.proj _ _ e _ => e.hasMVar && hasAssignableMVar mctx e
| Expr.mvar mvarId _ => mctx.isExprAssignable mvarId
/-
Notes on artificial eta-expanded terms due to metavariables.
We try avoid synthetic terms such as `((fun x y => t) a b)` in the output produced by the elaborator.
This kind of term may be generated when instantiating metavariable assignments.
This module tries to avoid their generation because they often introduce unnecessary dependencies and
may affect automation.
When elaborating terms, we use metavariables to represent "holes". Each hole has a context which includes
all free variables that may be used to "fill" the hole. Suppose, we create a metavariable (hole) `?m : Nat` in a context
containing `(x : Nat) (y : Nat) (b : Bool)`, then we can assign terms such as `x + y` to `?m` since `x` and `y`
are in the context used to create `?m`. Now, suppose we have the term `?m + 1` and we want to create the lambda expression
`fun x => ?m + 1`. This term is not correct since we may assign to `?m` a term containing `x`.
We address this issue by create a synthetic metavariable `?n : Nat → Nat` and adding the delayed assignment
`?n #[x] := ?m`, and the term `fun x => ?n x + 1`. When we later assign a term `t[x]` to `?m`, `fun x => t[x]` is assigned to
`?n`, and if we substitute it at `fun x => ?n x + 1`, we produce `fun x => ((fun x => t[x]) x) + 1`.
To avoid this term eta-expanded term, we apply beta-reduction when instantiating metavariable assignments in this module.
This operation is performed at `instantiateExprMVars`, `elimMVarDeps`, and `levelMVarToParam`.
-/
partial def instantiateLevelMVars [Monad m] [MonadMCtx m] : Level → m Level
| lvl@(Level.succ lvl₁ _) => return Level.updateSucc! lvl (← instantiateLevelMVars lvl₁)
| lvl@(Level.max lvl₁ lvl₂ _) => return Level.updateMax! lvl (← instantiateLevelMVars lvl₁) (← instantiateLevelMVars lvl₂)
| lvl@(Level.imax lvl₁ lvl₂ _) => return Level.updateIMax! lvl (← instantiateLevelMVars lvl₁) (← instantiateLevelMVars lvl₂)
| lvl@(Level.mvar mvarId _) => do
match getLevelAssignment? (← getMCtx) mvarId with
| some newLvl =>
if !newLvl.hasMVar then pure newLvl
else do
let newLvl' ← instantiateLevelMVars newLvl
modifyMCtx fun mctx => mctx.assignLevel mvarId newLvl'
pure newLvl'
| none => pure lvl
| lvl => pure lvl
/-- instantiateExprMVars main function -/
partial def instantiateExprMVars [Monad m] [MonadMCtx m] [STWorld ω m] [MonadLiftT (ST ω) m] (e : Expr) : MonadCacheT ExprStructEq Expr m Expr :=
if !e.hasMVar then
pure e
else checkCache { val := e : ExprStructEq } fun _ => do match e with
| Expr.proj _ _ s _ => return e.updateProj! (← instantiateExprMVars s)
| Expr.forallE _ d b _ => return e.updateForallE! (← instantiateExprMVars d) (← instantiateExprMVars b)
| Expr.lam _ d b _ => return e.updateLambdaE! (← instantiateExprMVars d) (← instantiateExprMVars b)
| Expr.letE _ t v b _ => return e.updateLet! (← instantiateExprMVars t) (← instantiateExprMVars v) (← instantiateExprMVars b)
| Expr.const _ lvls _ => return e.updateConst! (← lvls.mapM instantiateLevelMVars)
| Expr.sort lvl _ => return e.updateSort! (← instantiateLevelMVars lvl)
| Expr.mdata _ b _ => return e.updateMData! (← instantiateExprMVars b)
| Expr.app .. => e.withApp fun f args => do
let instArgs (f : Expr) : MonadCacheT ExprStructEq Expr m Expr := do
let args ← args.mapM instantiateExprMVars
pure (mkAppN f args)
let instApp : MonadCacheT ExprStructEq Expr m Expr := do
let wasMVar := f.isMVar
let f ← instantiateExprMVars f
if wasMVar && f.isLambda then
/- Some of the arguments in args are irrelevant after we beta reduce. -/
instantiateExprMVars (f.betaRev args.reverse)
else
instArgs f
match f with
| Expr.mvar mvarId _ =>
let mctx ← getMCtx
match mctx.getDelayedAssignment? mvarId with
| none => instApp
| some { fvars := fvars, val := val, .. } =>
/-
Apply "delayed substitution" (i.e., delayed assignment + application).
That is, `f` is some metavariable `?m`, that is delayed assigned to `val`.
If after instantiating `val`, we obtain `newVal`, and `newVal` does not contain
metavariables, we replace the free variables `fvars` in `newVal` with the first
`fvars.size` elements of `args`. -/
if fvars.size > args.size then
/- We don't have sufficient arguments for instantiating the free variables `fvars`.
This can only happy if a tactic or elaboration function is not implemented correctly.
We decided to not use `panic!` here and report it as an error in the frontend
when we are checking for unassigned metavariables in an elaborated term. -/
instArgs f
else
let newVal ← instantiateExprMVars val
if newVal.hasExprMVar then
instArgs f
else do
let args ← args.mapM instantiateExprMVars
/-
Example: suppose we have
`?m t1 t2 t3`
That is, `f := ?m` and `args := #[t1, t2, t3]`
Morever, `?m` is delayed assigned
`?m #[x, y] := f x y`
where, `fvars := #[x, y]` and `newVal := f x y`.
After abstracting `newVal`, we have `f (Expr.bvar 0) (Expr.bvar 1)`.
After `instantiaterRevRange 0 2 args`, we have `f t1 t2`.
After `mkAppRange 2 3`, we have `f t1 t2 t3` -/
let newVal := newVal.abstract fvars
let result := newVal.instantiateRevRange 0 fvars.size args
let result := mkAppRange result fvars.size args.size args
pure result
| _ => instApp
| e@(Expr.mvar mvarId _) => checkCache { val := e : ExprStructEq } fun _ => do
let mctx ← getMCtx
match mctx.getExprAssignment? mvarId with
| some newE => do
let newE' ← instantiateExprMVars newE
modifyMCtx fun mctx => mctx.assignExpr mvarId newE'
pure newE'
| none => pure e
| e => pure e
instance : MonadMCtx (StateRefT MetavarContext (ST ω)) where
getMCtx := get
modifyMCtx := modify
def instantiateMVars (mctx : MetavarContext) (e : Expr) : Expr × MetavarContext :=
if !e.hasMVar then
(e, mctx)
else
let instantiate {ω} (e : Expr) : (MonadCacheT ExprStructEq Expr $ StateRefT MetavarContext $ ST ω) Expr :=
instantiateExprMVars e
runST fun _ => instantiate e |>.run |>.run mctx
def instantiateLCtxMVars (mctx : MetavarContext) (lctx : LocalContext) : LocalContext × MetavarContext :=
lctx.foldl (init := ({}, mctx)) fun (lctx, mctx) ldecl =>
match ldecl with
| LocalDecl.cdecl _ fvarId userName type bi =>
let (type, mctx) := mctx.instantiateMVars type
(lctx.mkLocalDecl fvarId userName type bi, mctx)
| LocalDecl.ldecl _ fvarId userName type value nonDep =>
let (type, mctx) := mctx.instantiateMVars type
let (value, mctx) := mctx.instantiateMVars value
(lctx.mkLetDecl fvarId userName type value nonDep, mctx)
def instantiateMVarDeclMVars (mctx : MetavarContext) (mvarId : MVarId) : MetavarContext :=
let mvarDecl := mctx.getDecl mvarId
let (lctx, mctx) := mctx.instantiateLCtxMVars mvarDecl.lctx
let (type, mctx) := mctx.instantiateMVars mvarDecl.type
{ mctx with decls := mctx.decls.insert mvarId { mvarDecl with lctx := lctx, type := type } }
namespace DependsOn
private abbrev M := StateM ExprSet
private def shouldVisit (e : Expr) : M Bool := do
if !e.hasMVar && !e.hasFVar then
return false
else if (← get).contains e then
return false
else
modify fun s => s.insert e
return true
@[specialize] private partial def dep (mctx : MetavarContext) (p : FVarId → Bool) (e : Expr) : M Bool :=
let rec
visit (e : Expr) : M Bool := do
if !(← shouldVisit e) then
pure false
else
visitMain e,
visitMain : Expr → M Bool
| Expr.proj _ _ s _ => visit s
| Expr.forallE _ d b _ => visit d <||> visit b
| Expr.lam _ d b _ => visit d <||> visit b
| Expr.letE _ t v b _ => visit t <||> visit v <||> visit b
| Expr.mdata _ b _ => visit b
| Expr.app f a _ => visit a <||> if f.isApp then visitMain f else visit f
| Expr.mvar mvarId _ =>
match mctx.getExprAssignment? mvarId with
| some a => visit a
| none =>
let lctx := (mctx.getDecl mvarId).lctx
return lctx.any fun decl => p decl.fvarId
| Expr.fvar fvarId _ => return p fvarId
| e => pure false
visit e
@[inline] partial def main (mctx : MetavarContext) (p : FVarId → Bool) (e : Expr) : M Bool :=
if !e.hasFVar && !e.hasMVar then pure false else dep mctx p e
end DependsOn
/--
Return `true` iff `e` depends on a free variable `x` s.t. `p x` is `true`.
For each metavariable `?m` occurring in `x`
1- If `?m := t`, then we visit `t` looking for `x`
2- If `?m` is unassigned, then we consider the worst case and check whether `x` is in the local context of `?m`.
This case is a "may dependency". That is, we may assign a term `t` to `?m` s.t. `t` contains `x`. -/
@[inline] def findExprDependsOn (mctx : MetavarContext) (e : Expr) (p : FVarId → Bool) : Bool :=
DependsOn.main mctx p e |>.run' {}
/--
Similar to `findExprDependsOn`, but checks the expressions in the given local declaration
depends on a free variable `x` s.t. `p x` is `true`. -/
@[inline] def findLocalDeclDependsOn (mctx : MetavarContext) (localDecl : LocalDecl) (p : FVarId → Bool) : Bool :=
match localDecl with
| LocalDecl.cdecl (type := t) .. => findExprDependsOn mctx t p
| LocalDecl.ldecl (type := t) (value := v) .. => (DependsOn.main mctx p t <||> DependsOn.main mctx p v).run' {}
def exprDependsOn (mctx : MetavarContext) (e : Expr) (fvarId : FVarId) : Bool :=
findExprDependsOn mctx e fun fvarId' => fvarId == fvarId'
def localDeclDependsOn (mctx : MetavarContext) (localDecl : LocalDecl) (fvarId : FVarId) : Bool :=
findLocalDeclDependsOn mctx localDecl fun fvarId' => fvarId == fvarId'
namespace MkBinding
inductive Exception where
| revertFailure (mctx : MetavarContext) (lctx : LocalContext) (toRevert : Array Expr) (decl : LocalDecl)
instance : ToString Exception where
toString
| Exception.revertFailure _ lctx toRevert decl =>
"failed to revert "
++ toString (toRevert.map (fun x => "'" ++ toString (lctx.getFVar! x).userName ++ "'"))
++ ", '" ++ toString decl.userName ++ "' depends on them, and it is an auxiliary declaration created by the elaborator"
++ " (possible solution: use tactic 'clear' to remove '" ++ toString decl.userName ++ "' from local context)"
/--
`MkBinding` and `elimMVarDepsAux` are mutually recursive, but `cache` is only used at `elimMVarDepsAux`.
We use a single state object for convenience.
We have a `NameGenerator` because we need to generate fresh auxiliary metavariables. -/
structure State where
mctx : MetavarContext
ngen : NameGenerator
cache : HashMap ExprStructEq Expr := {}
abbrev MCore := EStateM Exception State
abbrev M := ReaderT Bool (EStateM Exception State)
def preserveOrder : M Bool := read
instance : MonadHashMapCacheAdapter ExprStructEq Expr M where
getCache := do let s ← get; pure s.cache
modifyCache := fun f => modify fun s => { s with cache := f s.cache }
/-- Return the local declaration of the free variable `x` in `xs` with the smallest index -/
private def getLocalDeclWithSmallestIdx (lctx : LocalContext) (xs : Array Expr) : LocalDecl := do
let mut d : LocalDecl := lctx.getFVar! xs[0]
for i in [1:xs.size] do
let curr := lctx.getFVar! xs[i]
if curr.index < d.index then
d := curr
return d
/--
Given `toRevert` an array of free variables s.t. `lctx` contains their declarations,
return a new array of free variables that contains `toRevert` and all free variables
in `lctx` that may depend on `toRevert`.
Remark: the result is sorted by `LocalDecl` indices.
Remark: We used to throw an `Exception.revertFailure` exception when an auxiliary declaration
had to be reversed. Recall that auxiliary declarations are created when compiling (mutually)
recursive definitions. The `revertFailure` due to auxiliary declaration dependency was originally
introduced in Lean3 to address issue https://github.com/leanprover/lean/issues/1258.
In Lean4, this solution is not satisfactory because all definitions/theorems are potentially
recursive. So, even an simple (incomplete) definition such as
```
variables {α : Type} in
def f (a : α) : List α :=
_
```
would trigger the `Exception.revertFailure` exception. In the definition above,
the elaborator creates the auxiliary definition `f : {α : Type} → List α`.
The `_` is elaborated as a new fresh variable `?m` that contains `α : Type`, `a : α`, and `f : α → List α` in its context,
When we try to create the lambda `fun {α : Type} (a : α) => ?m`, we first need to create
an auxiliary `?n` which do not contain `α` and `a` in its context. That is,
we create the metavariable `?n : {α : Type} → (a : α) → (f : α → List α) → List α`,
add the delayed assignment `?n #[α, a, f] := ?m α a f`, and create the lambda
`fun {α : Type} (a : α) => ?n α a f`.
See `elimMVarDeps` for more information.
If we kept using the Lean3 approach, we would get the `Exception.revertFailure` exception because we are
reverting the auxiliary definition `f`.
Note that https://github.com/leanprover/lean/issues/1258 is not an issue in Lean4 because
we have changed how we compile recursive definitions.
-/
def collectDeps (mctx : MetavarContext) (lctx : LocalContext) (toRevert : Array Expr) (preserveOrder : Bool) : Except Exception (Array Expr) := do
if toRevert.size == 0 then
pure toRevert
else
if preserveOrder then
-- Make sure none of `toRevert` is an AuxDecl
-- Make sure toRevert[j] does not depend on toRevert[i] for j > i
toRevert.size.forM fun i => do
let fvar := toRevert[i]
let decl := lctx.getFVar! fvar
i.forM fun j => do
let prevFVar := toRevert[j]
let prevDecl := lctx.getFVar! prevFVar
if localDeclDependsOn mctx prevDecl fvar.fvarId! then
throw (Exception.revertFailure mctx lctx toRevert prevDecl)
let newToRevert := if preserveOrder then toRevert else Array.mkEmpty toRevert.size
let firstDeclToVisit := getLocalDeclWithSmallestIdx lctx toRevert
let initSize := newToRevert.size
lctx.foldlM (init := newToRevert) (start := firstDeclToVisit.index) fun (newToRevert : Array Expr) decl =>
if initSize.any fun i => decl.fvarId == (newToRevert.get! i).fvarId! then pure newToRevert
else if toRevert.any fun x => decl.fvarId == x.fvarId! then
pure (newToRevert.push decl.toExpr)
else if findLocalDeclDependsOn mctx decl (fun fvarId => newToRevert.any fun x => x.fvarId! == fvarId) then
pure (newToRevert.push decl.toExpr)
else
pure newToRevert
/-- Create a new `LocalContext` by removing the free variables in `toRevert` from `lctx`.
We use this function when we create auxiliary metavariables at `elimMVarDepsAux`. -/
def reduceLocalContext (lctx : LocalContext) (toRevert : Array Expr) : LocalContext :=
toRevert.foldr (init := lctx) fun x lctx =>
lctx.erase x.fvarId!
@[inline] private def getMCtx : M MetavarContext :=
return (← get).mctx
/-- Return free variables in `xs` that are in the local context `lctx` -/
private def getInScope (lctx : LocalContext) (xs : Array Expr) : Array Expr :=
xs.foldl (init := #[]) fun scope x =>
if lctx.contains x.fvarId! then
scope.push x
else
scope
/-- Execute `x` with an empty cache, and then restore the original cache. -/
@[inline] private def withFreshCache (x : M α) : M α := do
let cache ← modifyGet fun s => (s.cache, { s with cache := {} })
let a ← x
modify fun s => { s with cache := cache }
pure a
/--
Create an application `mvar ys` where `ys` are the free variables.
See "Gruesome details" in the beginning of the file for understanding
how let-decl free variables are handled. -/
private def mkMVarApp (lctx : LocalContext) (mvar : Expr) (xs : Array Expr) (kind : MetavarKind) : Expr :=
xs.foldl (init := mvar) fun e x =>
match kind with
| MetavarKind.syntheticOpaque => mkApp e x
| _ => if (lctx.getFVar! x).isLet then e else mkApp e x
/-- Return true iff some `e` in `es` depends on `fvarId` -/
private def anyDependsOn (mctx : MetavarContext) (es : Array Expr) (fvarId : FVarId) : Bool :=
es.any fun e => exprDependsOn mctx e fvarId
mutual
private partial def visit (xs : Array Expr) (e : Expr) : M Expr :=
if !e.hasMVar then pure e else checkCache { val := e : ExprStructEq } fun _ => elim xs e
private partial def elim (xs : Array Expr) (e : Expr) : M Expr :=
match e with
| Expr.proj _ _ s _ => return e.updateProj! (← visit xs s)
| Expr.forallE _ d b _ => return e.updateForallE! (← visit xs d) (← visit xs b)
| Expr.lam _ d b _ => return e.updateLambdaE! (← visit xs d) (← visit xs b)
| Expr.letE _ t v b _ => return e.updateLet! (← visit xs t) (← visit xs v) (← visit xs b)
| Expr.mdata _ b _ => return e.updateMData! (← visit xs b)
| Expr.app _ _ _ => e.withApp fun f args => elimApp xs f args
| Expr.mvar mvarId _ => elimApp xs e #[]
| e => return e
private partial def mkAuxMVarType (lctx : LocalContext) (xs : Array Expr) (kind : MetavarKind) (e : Expr) : M Expr := do
let e ← abstractRangeAux xs xs.size e
xs.size.foldRevM (init := e) fun i e =>
let x := xs[i]
match lctx.getFVar! x with
| LocalDecl.cdecl _ _ n type bi => do
let type := type.headBeta
let type ← abstractRangeAux xs i type
pure <| Lean.mkForall n bi type e
| LocalDecl.ldecl _ _ n type value nonDep => do
let type := type.headBeta
let type ← abstractRangeAux xs i type
let value ← abstractRangeAux xs i value
let e := mkLet n type value e nonDep
match kind with
| MetavarKind.syntheticOpaque =>
-- See "Gruesome details" section in the beginning of the file
let e := e.liftLooseBVars 0 1
pure <| mkForall n BinderInfo.default type e
| _ => pure e
where
abstractRangeAux (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do
let e ← elim xs e
pure (e.abstractRange i xs)
private partial def elimMVar (xs : Array Expr) (mvarId : MVarId) (args : Array Expr) : M (Expr × Array Expr) := do
let mctx ← getMCtx
let mvarDecl := mctx.getDecl mvarId
let mvarLCtx := mvarDecl.lctx
let toRevert := getInScope mvarLCtx xs
if toRevert.size == 0 then
let args ← args.mapM (visit xs)
return (mkAppN (mkMVar mvarId) args, #[])
else
let newMVarKind := if !mctx.isExprAssignable mvarId then MetavarKind.syntheticOpaque else mvarDecl.kind
/- If `mvarId` is the lhs of a delayed assignment `?m #[x_1, ... x_n] := val`,
then `nestedFVars` is `#[x_1, ..., x_n]`.
In this case, we produce a new `syntheticOpaque` metavariable `?n` and a delayed assignment
```
?n #[y_1, ..., y_m, x_1, ... x_n] := ?m x_1 ... x_n
```
where `#[y_1, ..., y_m]` is `toRevert` after `collectDeps`.
Remark: `newMVarKind != MetavarKind.syntheticOpaque ==> nestedFVars == #[]`
-/
let rec cont (nestedFVars : Array Expr) (nestedLCtx : LocalContext) : M (Expr × Array Expr) := do
let args ← args.mapM (visit xs)
let preserve ← preserveOrder
match collectDeps mctx mvarLCtx toRevert preserve with
| Except.error ex => throw ex
| Except.ok toRevert =>
let newMVarLCtx := reduceLocalContext mvarLCtx toRevert
let newLocalInsts := mvarDecl.localInstances.filter fun inst => toRevert.all fun x => inst.fvar != x
-- Remark: we must reset the before processing `mkAuxMVarType` because `toRevert` may not be equal to `xs`
let newMVarType ← withFreshCache do mkAuxMVarType mvarLCtx toRevert newMVarKind mvarDecl.type
let newMVarId := (← get).ngen.curr
let newMVar := mkMVar newMVarId
let result := mkMVarApp mvarLCtx newMVar toRevert newMVarKind
let numScopeArgs := mvarDecl.numScopeArgs + result.getAppNumArgs
modify fun s => { s with
mctx := s.mctx.addExprMVarDecl newMVarId Name.anonymous newMVarLCtx newLocalInsts newMVarType newMVarKind numScopeArgs,
ngen := s.ngen.next
}
match newMVarKind with
| MetavarKind.syntheticOpaque =>
modify fun s => { s with mctx := assignDelayed s.mctx newMVarId nestedLCtx (toRevert ++ nestedFVars) (mkAppN (mkMVar mvarId) nestedFVars) }
| _ =>
modify fun s => { s with mctx := assignExpr s.mctx mvarId result }
return (mkAppN result args, toRevert)
if !mvarDecl.kind.isSyntheticOpaque then
cont #[] mvarLCtx
else match mctx.getDelayedAssignment? mvarId with
| none => cont #[] mvarLCtx
| some { fvars := fvars, lctx := nestedLCtx, .. } => cont fvars nestedLCtx -- Remark: nestedLCtx is bigger than mvarLCtx
private partial def elimApp (xs : Array Expr) (f : Expr) (args : Array Expr) : M Expr := do
match f with
| Expr.mvar mvarId _ =>
match (← getMCtx).getExprAssignment? mvarId with
| some newF =>
if newF.isLambda then
let args ← args.mapM (visit xs)
elim xs <| newF.betaRev args.reverse
else
elimApp xs newF args
| none => return (← elimMVar xs mvarId args).1
| _ =>
return mkAppN (← visit xs f) (← args.mapM (visit xs))
end
partial def elimMVarDeps (xs : Array Expr) (e : Expr) : M Expr :=
if !e.hasMVar then
pure e
else
withFreshCache do
elim xs e
partial def revert (xs : Array Expr) (mvarId : MVarId) : M (Expr × Array Expr) :=
withFreshCache do
elimMVar xs mvarId #[]
/--
Similar to `Expr.abstractRange`, but handles metavariables correctly.
It uses `elimMVarDeps` to ensure `e` and the type of the free variables `xs` do not
contain a metavariable `?m` s.t. local context of `?m` contains a free variable in `xs`.
`elimMVarDeps` is defined later in this file. -/
@[inline] private def abstractRange (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do
let e ← elimMVarDeps xs e
pure (e.abstractRange i xs)
/--
Similar to `LocalContext.mkBinding`, but handles metavariables correctly.
If `usedOnly == false` then `forall` and `lambda` expressions are created only for used variables.
If `usedLetOnly == false` then `let` expressions are created only for used (let-) variables. -/
@[specialize] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (e : Expr) (usedOnly : Bool) (usedLetOnly : Bool) : M (Expr × Nat) := do
let e ← abstractRange xs xs.size e
xs.size.foldRevM
(fun i (p : Expr × Nat) => do
let (e, num) := p;
let x := xs[i]
match lctx.getFVar! x with
| LocalDecl.cdecl _ _ n type bi =>
if !usedOnly || e.hasLooseBVar 0 then
let type := type.headBeta;
let type ← abstractRange xs i type
if isLambda then
pure (Lean.mkLambda n bi type e, num + 1)
else
pure (Lean.mkForall n bi type e, num + 1)
else
pure (e.lowerLooseBVars 1 1, num)
| LocalDecl.ldecl _ _ n type value nonDep =>
if !usedLetOnly || e.hasLooseBVar 0 then
let type ← abstractRange xs i type
let value ← abstractRange xs i value
pure (mkLet n type value e nonDep, num + 1)
else
pure (e.lowerLooseBVars 1 1, num))
(e, 0)
end MkBinding
abbrev MkBindingM := ReaderT LocalContext MkBinding.MCore
def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool) : MkBindingM Expr := fun _ =>
MkBinding.elimMVarDeps xs e preserveOrder
def revert (xs : Array Expr) (mvarId : MVarId) (preserveOrder : Bool) : MkBindingM (Expr × Array Expr) := fun _ =>
MkBinding.revert xs mvarId preserveOrder
def mkBinding (isLambda : Bool) (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MkBindingM (Expr × Nat) := fun lctx =>
MkBinding.mkBinding isLambda lctx xs e usedOnly usedLetOnly false
@[inline] def mkLambda (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MkBindingM Expr := do
let (e, _) ← mkBinding (isLambda := true) xs e usedOnly usedLetOnly
pure e
@[inline] def mkForall (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MkBindingM Expr := do
let (e, _) ← mkBinding (isLambda := false) xs e usedOnly usedLetOnly
pure e
/--
`isWellFormed mctx lctx e` return true if
- All locals in `e` are declared in `lctx`
- All metavariables `?m` in `e` have a local context which is a subprefix of `lctx` or are assigned, and the assignment is well-formed. -/
partial def isWellFormed (mctx : MetavarContext) (lctx : LocalContext) : Expr → Bool
| Expr.mdata _ e _ => isWellFormed mctx lctx e
| Expr.proj _ _ e _ => isWellFormed mctx lctx e
| e@(Expr.app f a _) => (!e.hasExprMVar && !e.hasFVar) || (isWellFormed mctx lctx f && isWellFormed mctx lctx a)
| e@(Expr.lam _ d b _) => (!e.hasExprMVar && !e.hasFVar) || (isWellFormed mctx lctx d && isWellFormed mctx lctx b)
| e@(Expr.forallE _ d b _) => (!e.hasExprMVar && !e.hasFVar) || (isWellFormed mctx lctx d && isWellFormed mctx lctx b)
| e@(Expr.letE _ t v b _) => (!e.hasExprMVar && !e.hasFVar) || (isWellFormed mctx lctx t && isWellFormed mctx lctx v && isWellFormed mctx lctx b)
| Expr.const .. => true
| Expr.bvar .. => true
| Expr.sort .. => true
| Expr.lit .. => true
| Expr.mvar mvarId _ =>
let mvarDecl := mctx.getDecl mvarId;
if mvarDecl.lctx.isSubPrefixOf lctx then true
else match mctx.getExprAssignment? mvarId with
| none => false
| some v => isWellFormed mctx lctx v
| Expr.fvar fvarId _ => lctx.contains fvarId
namespace LevelMVarToParam
structure Context where
paramNamePrefix : Name
alreadyUsedPred : Name → Bool
structure State where
mctx : MetavarContext
paramNames : Array Name := #[]
nextParamIdx : Nat
cache : HashMap ExprStructEq Expr := {}
abbrev M := ReaderT Context $ StateM State
instance : MonadCache ExprStructEq Expr M where
findCached? e := return (← get).cache.find? e
cache e v := modify fun s => { s with cache := s.cache.insert e v }
partial def mkParamName : M Name := do
let ctx ← read
let s ← get
let newParamName := ctx.paramNamePrefix.appendIndexAfter s.nextParamIdx
if ctx.alreadyUsedPred newParamName then
modify fun s => { s with nextParamIdx := s.nextParamIdx + 1 }
mkParamName
else do
modify fun s => { s with nextParamIdx := s.nextParamIdx + 1, paramNames := s.paramNames.push newParamName }
pure newParamName
partial def visitLevel (u : Level) : M Level := do
match u with
| Level.succ v _ => return u.updateSucc! (← visitLevel v)
| Level.max v₁ v₂ _ => return u.updateMax! (← visitLevel v₁) (← visitLevel v₂)
| Level.imax v₁ v₂ _ => return u.updateIMax! (← visitLevel v₁) (← visitLevel v₂)
| Level.zero _ => pure u
| Level.param .. => pure u
| Level.mvar mvarId _ =>
let s ← get
match s.mctx.getLevelAssignment? mvarId with
| some v => visitLevel v
| none =>
let p ← mkParamName
let p := mkLevelParam p
modify fun s => { s with mctx := s.mctx.assignLevel mvarId p }
pure p
partial def main (e : Expr) : M Expr :=
if !e.hasMVar then
return e
else
checkCache { val := e : ExprStructEq } fun _ => do
match e with
| Expr.proj _ _ s _ => return e.updateProj! (← main s)
| Expr.forallE _ d b _ => return e.updateForallE! (← main d) (← main b)
| Expr.lam _ d b _ => return e.updateLambdaE! (← main d) (← main b)
| Expr.letE _ t v b _ => return e.updateLet! (← main t) (← main v) (← main b)
| Expr.app .. => e.withApp fun f args => visitApp f args
| Expr.mdata _ b _ => return e.updateMData! (← main b)
| Expr.const _ us _ => return e.updateConst! (← us.mapM visitLevel)
| Expr.sort u _ => return e.updateSort! (← visitLevel u)
| Expr.mvar .. => visitApp e #[]
| e => return e
where
visitApp (f : Expr) (args : Array Expr) : M Expr := do
match f with
| Expr.mvar mvarId .. =>
match (← get).mctx.getExprAssignment? mvarId with
| some v => return (← visitApp v args).headBeta
| none => return mkAppN f (← args.mapM main)
| _ => return mkAppN (← main f) (← args.mapM main)
end LevelMVarToParam
structure UnivMVarParamResult where
mctx : MetavarContext
newParamNames : Array Name
nextParamIdx : Nat
expr : Expr
def levelMVarToParam (mctx : MetavarContext) (alreadyUsedPred : Name → Bool) (e : Expr) (paramNamePrefix : Name := `u) (nextParamIdx : Nat := 1)
: UnivMVarParamResult :=
let (e, s) := LevelMVarToParam.main e { paramNamePrefix := paramNamePrefix, alreadyUsedPred := alreadyUsedPred } { mctx := mctx, nextParamIdx := nextParamIdx }
{ mctx := s.mctx,
newParamNames := s.paramNames,
nextParamIdx := s.nextParamIdx,
expr := e }
def getExprAssignmentDomain (mctx : MetavarContext) : Array MVarId :=
mctx.eAssignment.foldl (init := #[]) fun a mvarId _ => Array.push a mvarId
end MetavarContext
end Lean
|
e887a2238a8126fd795e6cbb496184f50dbe436a | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/number_theory/primes_congruent_one.lean | 9a50b9dab32e067ead7a1ca6e633e61e22bf3e01 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,677 | lean | /-
Copyright (c) 2020 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca
-/
import ring_theory.polynomial.cyclotomic
import topology.algebra.polynomial
import field_theory.finite.basic
/-!
# Primes congruent to one
We prove that, for any positive `k : ℕ`, there are infinitely many primes `p` such that
`p ≡ 1 [MOD k]`.
-/
namespace nat
open polynomial nat filter
/-- For any positive `k : ℕ` there are infinitely many primes `p` such that `p ≡ 1 [MOD k]`. -/
lemma exists_prime_ge_modeq_one (k n : ℕ) (hpos : 0 < k) :
∃ (p : ℕ), nat.prime p ∧ n ≤ p ∧ p ≡ 1 [MOD k] :=
begin
have hli : tendsto (abs ∘ (λ (a : ℕ), abs(a : ℚ))) at_top at_top,
{ simp only [(∘), abs_cast],
exact nat.strict_mono_cast.monotone.tendsto_at_top_at_top exists_nat_ge },
have hcff : int.cast_ring_hom ℚ (cyclotomic k ℤ).leading_coeff ≠ 0,
{ simp only [cyclotomic.monic, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def,
not_false_iff, one_ne_zero] },
obtain ⟨a, ha⟩ := tendsto_at_top_at_top.1 (tendsto_abv_eval₂_at_top (int.cast_ring_hom ℚ) abs
(cyclotomic k ℤ) (degree_cyclotomic_pos k ℤ hpos) hcff hli) 2,
let b := a * (k * n.factorial),
have hgt : 1 < (eval ↑(a * (k * n.factorial)) (cyclotomic k ℤ)).nat_abs,
{ suffices hgtabs : 1 < abs (eval ↑b (cyclotomic k ℤ)),
{ rw [int.abs_eq_nat_abs] at hgtabs,
exact_mod_cast hgtabs },
suffices hgtrat : 1 < abs (eval ↑b (cyclotomic k ℚ)),
{ rw [← map_cyclotomic_int k ℚ, ← int.cast_coe_nat, ← int.coe_cast_ring_hom, eval_map,
eval₂_hom, int.coe_cast_ring_hom] at hgtrat,
assumption_mod_cast },
suffices hleab : a ≤ b,
{ replace ha := lt_of_lt_of_le one_lt_two (ha b hleab),
rwa [← eval_map, map_cyclotomic_int k ℚ, abs_cast] at ha },
exact le_mul_of_pos_right (mul_pos hpos (factorial_pos n)) },
let p := min_fac (eval ↑b (cyclotomic k ℤ)).nat_abs,
haveI hprime : fact p.prime := ⟨min_fac_prime (ne_of_lt hgt).symm⟩,
have hroot : is_root (cyclotomic k (zmod p)) (cast_ring_hom (zmod p) b),
{ rw [is_root.def, ← map_cyclotomic_int k (zmod p), eval_map, coe_cast_ring_hom,
← int.cast_coe_nat, ← int.coe_cast_ring_hom, eval₂_hom, int.coe_cast_ring_hom,
zmod.int_coe_zmod_eq_zero_iff_dvd _ _],
apply int.dvd_nat_abs.1,
exact_mod_cast min_fac_dvd (eval ↑b (cyclotomic k ℤ)).nat_abs },
refine ⟨p, hprime.1, _, _⟩,
{ by_contra habs,
exact (prime.dvd_iff_not_coprime hprime.1).1
(dvd_factorial (min_fac_pos _) (le_of_not_ge habs))
(coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_left_right },
{ have hdiv := order_of_dvd_of_pow_eq_one (zmod.units_pow_card_sub_one_eq_one p
(zmod.unit_of_coprime b (coprime_of_root_cyclotomic hpos hroot))),
have : ¬p ∣ k := hprime.1.coprime_iff_not_dvd.1
(coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_right_right,
rw [order_of_root_cyclotomic_eq hpos this hroot] at hdiv,
exact ((modeq_iff_dvd' hprime.1.pos).2 hdiv).symm }
end
lemma frequently_at_top_modeq_one (k : ℕ) (hpos : 0 < k) :
∃ᶠ p in at_top, nat.prime p ∧ p ≡ 1 [MOD k] :=
begin
refine frequently_at_top.2 (λ n, _),
obtain ⟨p, hp⟩ := exists_prime_ge_modeq_one k n hpos,
exact ⟨p, ⟨hp.2.1, hp.1, hp.2.2⟩⟩
end
lemma infinite_set_of_prime_modeq_one (k : ℕ) (hpos : 0 < k) :
set.infinite {p : ℕ | nat.prime p ∧ p ≡ 1 [MOD k]} :=
frequently_at_top_iff_infinite.1 (frequently_at_top_modeq_one k hpos)
end nat
|
b4e36febdf2c546ceec22f1140120fe760632402 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /hott/init/bool.hlean | 51893d3e4574b5761320db7ba1e1ab2af84c2910 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 551 | 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
-/
prelude
import init.datatypes init.reserved_notation
namespace bool
definition cond {A : Type} (b : bool) (t e : A) :=
rec_on b e t
definition bor (a b : bool) :=
rec_on a (rec_on b ff tt) tt
notation a || b := bor a b
definition band (a b : bool) :=
rec_on a ff (rec_on b ff tt)
notation a && b := band a b
definition bnot (a : bool) :=
rec_on a tt ff
end bool
|
70e79d593e72527b890c65008c66f554fdd0749a | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/group_theory/specific_groups/cyclic.lean | f6508ca1c93dd1303d2368c7d4b54fef5da0cd90 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 19,206 | 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 data.nat.totient
import group_theory.order_of_element
import tactic.group
/-!
# Cyclic groups
A group `G` is called cyclic if there exists an element `g : G` such that every element of `G` is of
the form `g ^ n` for some `n : ℕ`. This file only deals with the predicate on a group to be cyclic.
For the concrete cyclic group of order `n`, see `data.zmod.basic`.
## Main definitions
* `is_cyclic` is a predicate on a group stating that the group is cyclic.
## Main statements
* `is_cyclic_of_prime_card` proves that a finite group of prime order is cyclic.
* `is_simple_group_of_prime_card`, `is_simple_group.is_cyclic`,
and `is_simple_group.prime_card` classify finite simple abelian groups.
## Implementation details
This file is currently only available for multiplicatively written groups.
## Tags
cyclic group
## TODO
* Add the attribute `@[to_additive]` to the declarations about `is_cyclic`, so that they work for
additive groups.
-/
universe u
variables {α : Type u} {a : α}
section cyclic
open_locale big_operators
local attribute [instance] set_fintype
open subgroup
/-- A group is called *cyclic* if it is generated by a single element. -/
class is_cyclic (α : Type u) [group α] : Prop :=
(exists_generator [] : ∃ g : α, ∀ x, x ∈ zpowers g)
@[priority 100]
instance is_cyclic_of_subsingleton [group α] [subsingleton α] : is_cyclic α :=
⟨⟨1, λ x, by { rw subsingleton.elim x 1, exact mem_zpowers 1 }⟩⟩
/-- 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 ▸ zpow_mul_comm _ _ _,
..hg }
variables [group α]
lemma monoid_hom.map_cyclic {G : Type*} [group G] [h : is_cyclic G] (σ : G →* G) :
∃ m : ℤ, ∀ g : G, σ g = g ^ m :=
begin
obtain ⟨h, hG⟩ := is_cyclic.exists_generator G,
obtain ⟨m, hm⟩ := hG (σ h),
use m,
intro g,
obtain ⟨n, rfl⟩ := hG g,
rw [monoid_hom.map_zpow, ←hm, ←zpow_mul, ←zpow_mul'],
end
lemma is_cyclic_of_order_of_eq_card [fintype α] (x : α)
(hx : order_of x = fintype.card α) : is_cyclic α :=
begin
classical,
use x,
simp_rw ← set_like.mem_coe,
rw ← set.eq_univ_iff_forall,
apply set.eq_of_subset_of_card_le (set.subset_univ _),
rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_zpowers],
end
/-- A finite group of prime order is cyclic. -/
lemma is_cyclic_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime]
(h : fintype.card α = p) : is_cyclic α :=
⟨begin
obtain ⟨g, hg⟩ : ∃ g : α, g ≠ 1,
from fintype.exists_ne_of_one_lt_card (by { rw h, exact hp.1.one_lt }) 1,
classical, -- for fintype (subgroup.zpowers g)
have : fintype.card (subgroup.zpowers g) ∣ p,
{ rw ←h,
apply card_subgroup_dvd_card },
rw nat.dvd_prime hp.1 at this,
cases this,
{ rw fintype.card_eq_one_iff at this,
cases this with t ht,
suffices : g = 1,
{ contradiction },
have hgt := ht ⟨g, by { change g ∈ subgroup.zpowers g, exact subgroup.mem_zpowers g }⟩,
rw [←ht 1] at hgt,
change (⟨_, _⟩ : subgroup.zpowers g) = ⟨_, _⟩ at hgt,
simpa using hgt },
{ use g,
intro x,
rw [←h] at this,
rw subgroup.eq_top_of_card_eq _ this,
exact subgroup.mem_top _ }
end⟩
lemma order_of_eq_card_of_forall_mem_zpowers [fintype α]
{g : α} (hx : ∀ x, x ∈ zpowers g) : order_of g = fintype.card α :=
by { classical, rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_zpowers],
simp [hx], apply fintype.card_of_finset', simp, intro x, exact hx x}
instance bot.is_cyclic {α : Type u} [group α] : is_cyclic (⊥ : subgroup α) :=
⟨⟨1, λ x, ⟨0, subtype.eq $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩
instance subgroup.is_cyclic {α : Type u} [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, zpow_zero]),
match k, hk with
| (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← zpow_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)) ∈ zpowers (g ^ nat.find hex),
from ⟨k / nat.find hex, by rw [← zpow_coe_nat, zpow_mul]⟩,
have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H,
by { rw zpow_mul, apply H.zpow_mem, exact_mod_cast (nat.find_spec hex).2 },
have hk₃ : g ^ (k % nat.find hex) ∈ H,
from (subgroup.mul_mem_cancel_right H hk₂).1 $
by rw [← zpow_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 [← zpow_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 [zpow_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
section classical
open_locale classical
lemma is_cyclic.card_pow_eq_one_le [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
≤ ((zpowers (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_zpowers.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 [zpow_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_zpowers 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.elim' 1 },
begin
rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_zpowers,
order_of_pow g, order_of_eq_card_of_forall_mem_zpowers 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
end classical
lemma is_cyclic.exists_monoid_generator [fintype α]
[is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ submonoid.powers x :=
by { simp_rw [mem_powers_iff_mem_zpowers], exact is_cyclic.exists_generator α }
section
variables [decidable_eq α] [fintype α]
lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ zpowers a) :
finset.image (λ i, a ^ i) (range (order_of a)) = univ :=
begin
simp_rw [←set_like.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 ∈ zpowers a) :
finset.image (λ i, a ^ i) (range (fintype.card α)) = univ :=
by rw [← order_of_eq_card_of_forall_mem_zpowers ha, is_cyclic.image_range_order_of ha]
end
section totient
variables [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 a))
(calc order_of a = @fintype.card (zpowers a) (id _) : order_eq_card_zpowers
... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α)
(fintype.of_finset _ (λ _, iff.rfl)) :
@fintype.card_le_of_injective (zpowers 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, ← zpow_coe_nat, ← zpow_mul, mul_comm, zpow_mul, zpow_coe_nat,
pow_order_of_eq_one, one_zpow]⟩⟩) (λ _ _ 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₁ (hm.trans hd) (finset.card_pos.2
⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _,
by { rw [order_of_pow a, 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 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, 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_rfl];
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 [is_cyclic α] [fintype α]
{d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d :=
begin
classical,
apply card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd
end
/-- A finite group of prime order is simple. -/
lemma is_simple_group_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime]
(h : fintype.card α = p) : is_simple_group α :=
⟨begin
have h' := nat.prime.one_lt (fact.out p.prime),
rw ← h at h',
haveI := fintype.one_lt_card_iff_nontrivial.1 h',
apply exists_pair_ne α,
end, λ H Hn, begin
classical,
have hcard := card_subgroup_dvd_card H,
rw [h, dvd_prime (fact.out p.prime)] at hcard,
refine hcard.imp (λ h1, _) (λ hp, _),
{ haveI := fintype.card_le_one_iff_subsingleton.1 (le_of_eq h1),
apply eq_bot_of_subsingleton },
{ exact eq_top_of_card_eq _ (hp.trans h.symm) }
end⟩
end cyclic
section quotient_center
open subgroup
variables {G : Type*} {H : Type*} [group G] [group H]
/-- A group is commutative if the quotient by the center is cyclic.
Also see `comm_group_of_cycle_center_quotient` for the `comm_group` instance -/
lemma commutative_of_cyclic_center_quotient [is_cyclic H] (f : G →* H)
(hf : f.ker ≤ center G) (a b : G) : a * b = b * a :=
let ⟨⟨x, y, (hxy : f y = x)⟩, (hx : ∀ a : f.range, a ∈ zpowers _)⟩ :=
is_cyclic.exists_generator f.range in
let ⟨m, hm⟩ := hx ⟨f a, a, rfl⟩ in
let ⟨n, hn⟩ := hx ⟨f b, b, rfl⟩ in
have hm : x ^ m = f a, by simpa [subtype.ext_iff] using hm,
have hn : x ^ n = f b, by simpa [subtype.ext_iff] using hn,
have ha : y ^ (-m) * a ∈ center G,
from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hm, inv_mul_self]),
have hb : y ^ (-n) * b ∈ center G,
from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hn, inv_mul_self]),
calc a * b = y ^ m * ((y ^ (-m) * a) * y ^ n) * (y ^ (-n) * b) : by simp [mul_assoc]
... = y ^ m * (y ^ n * (y ^ (-m) * a)) * (y ^ (-n) * b) : by rw [mem_center_iff.1 ha]
... = y ^ m * y ^ n * y ^ (-m) * (a * (y ^ (-n) * b)) : by simp [mul_assoc]
... = y ^ m * y ^ n * y ^ (-m) * ((y ^ (-n) * b) * a) : by rw [mem_center_iff.1 hb]
... = b * a : by group
/-- A group is commutative if the quotient by the center is cyclic. -/
def comm_group_of_cycle_center_quotient [is_cyclic H] (f : G →* H)
(hf : f.ker ≤ center G) : comm_group G :=
{ mul_comm := commutative_of_cyclic_center_quotient f hf,
..show group G, by apply_instance }
end quotient_center
namespace is_simple_group
section comm_group
variables [comm_group α] [is_simple_group α]
@[priority 100]
instance : is_cyclic α :=
begin
cases subsingleton_or_nontrivial α with hi hi; haveI := hi,
{ apply is_cyclic_of_subsingleton },
{ obtain ⟨g, hg⟩ := exists_ne (1 : α),
refine ⟨⟨g, λ x, _⟩⟩,
cases is_simple_lattice.eq_bot_or_eq_top (subgroup.zpowers g) with hb ht,
{ exfalso,
apply hg,
rw [← subgroup.mem_bot, ← hb],
apply subgroup.mem_zpowers },
{ rw ht,
apply subgroup.mem_top } }
end
theorem prime_card [fintype α] : (fintype.card α).prime :=
begin
have h0 : 0 < fintype.card α := fintype.card_pos_iff.2 (by apply_instance),
obtain ⟨g, hg⟩ := is_cyclic.exists_generator α,
refine ⟨fintype.one_lt_card_iff_nontrivial.2 infer_instance, λ n hn, _⟩,
refine (is_simple_lattice.eq_bot_or_eq_top (subgroup.zpowers (g ^ n))).symm.imp _ _,
{ intro h,
have hgo := order_of_pow g,
rw [order_of_eq_card_of_forall_mem_zpowers hg, nat.gcd_eq_right_iff_dvd.1 hn,
order_of_eq_card_of_forall_mem_zpowers, eq_comm,
nat.div_eq_iff_eq_mul_left (nat.pos_of_dvd_of_pos hn h0) hn] at hgo,
{ exact (mul_left_cancel₀ (ne_of_gt h0) ((mul_one (fintype.card α)).trans hgo)).symm },
{ intro x,
rw h,
exact subgroup.mem_top _ } },
{ intro h,
apply le_antisymm (nat.le_of_dvd h0 hn),
rw ← order_of_eq_card_of_forall_mem_zpowers hg,
apply order_of_le_of_pow_eq_one (nat.pos_of_dvd_of_pos hn h0),
rw [← subgroup.mem_bot, ← h],
exact subgroup.mem_zpowers _ }
end
end comm_group
end is_simple_group
theorem comm_group.is_simple_iff_is_cyclic_and_prime_card [fintype α] [comm_group α] :
is_simple_group α ↔ is_cyclic α ∧ (fintype.card α).prime :=
begin
split,
{ introI h,
exact ⟨is_simple_group.is_cyclic, is_simple_group.prime_card⟩ },
{ rintro ⟨hc, hp⟩,
haveI : fact (fintype.card α).prime := ⟨hp⟩,
exact is_simple_group_of_prime_card rfl }
end
|
10fbc35468295bccd7071cad1700b91deeb0e188 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/char_p/pi.lean | dd4a28105a20f7a5d2104acbd08aaf10d4c64500 | [] | 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 | 723 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.char_p.basic
import Mathlib.algebra.ring.pi
import Mathlib.PostPort
universes u v
namespace Mathlib
/-!
# Characteristic of semirings of functions
-/
namespace char_p
protected instance pi (ι : Type u) [hi : Nonempty ι] (R : Type v) [semiring R] (p : ℕ) [char_p R p] : char_p (ι → R) p :=
mk fun (x : ℕ) => sorry
-- diamonds
protected instance pi' (ι : Type u) [hi : Nonempty ι] (R : Type v) [comm_ring R] (p : ℕ) [char_p R p] : char_p (ι → R) p :=
char_p.pi ι R p
|
0511b109cab998e69f274fecff8e944387bfd269 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/subring.lean | 1932fe8a33568607e1e59c6c7742417e9523f145 | [] | 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 | 32,220 | lean | /-
Copyright (c) 2020 Ashvni Narayanan. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors : Ashvni Narayanan
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.deprecated.subring
import Mathlib.group_theory.subgroup
import Mathlib.ring_theory.subsemiring
import Mathlib.PostPort
universes u l u_1 u_2 v w
namespace Mathlib
/-!
# Subrings
Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type
whose terms correspond to subrings of `R`. This is the preferred way to talk
about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`)
are not in this file, and they will ultimately be deprecated.
We prove that subrings are a complete lattice, and that you can `map` (pushforward) and
`comap` (pull back) them along ring homomorphisms.
We define the `closure` construction from `set R` to `subring R`, sending a subset of `R`
to the subring it generates, and prove that it is a Galois insertion.
## Main definitions
Notation used here:
`(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)`
`(A : subring R) (B : subring S) (s : set R)`
* `subring R` : the type of subrings of a ring `R`.
* `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings.
* `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set.
* `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M`
form a `galois_insertion`.
* `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f`
* `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`.
* `prod A B : subring (R × S)` : the product of subrings
* `f.range : subring B` : the range of the ring homomorphism `f`.
* `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`,
the subring of `R` where `f x = g x`
## Implementation notes
A subring is implemented as a subsemiring which is also an additive subgroup.
The initial PR was as a submonoid which is also an additive subgroup.
Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although
`∈` is defined as membership of a subring's underlying set.
## Tags
subring, subrings
-/
/-- `subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a
multiplicative submonoid and an additive subgroup. Note in particular that it shares the
same 0 and 1 as R. -/
structure subring (R : Type u) [ring R]
extends subsemiring R, add_subgroup R
where
/-- Reinterpret a `subring` as a `subsemiring`. -/
/-- Reinterpret a `subring` as an `add_subgroup`. -/
namespace subring
/-- The underlying submonoid of a subring. -/
def to_submonoid {R : Type u} [ring R] (s : subring R) : submonoid R :=
submonoid.mk (carrier s) sorry sorry
protected instance set.has_coe {R : Type u} [ring R] : has_coe (subring R) (set R) :=
has_coe.mk carrier
protected instance has_mem {R : Type u} [ring R] : has_mem R (subring R) :=
has_mem.mk fun (m : R) (S : subring R) => m ∈ ↑S
protected instance has_coe_to_sort {R : Type u} [ring R] : has_coe_to_sort (subring R) :=
has_coe_to_sort.mk (Type u) fun (S : subring R) => Subtype fun (x : R) => x ∈ S
/-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive
subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/
protected def mk' {R : Type u} [ring R] (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R :=
mk s sorry sorry sorry sorry sorry
@[simp] theorem coe_mk' {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : ↑(subring.mk' s sm sa hm ha) = s :=
rfl
@[simp] theorem mem_mk' {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s :=
iff.rfl
@[simp] theorem mk'_to_submonoid {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : to_submonoid (subring.mk' s sm sa hm ha) = sm :=
submonoid.ext' (Eq.symm hm)
@[simp] theorem mk'_to_add_subgroup {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : to_add_subgroup (subring.mk' s sm sa hm ha) = sa :=
add_subgroup.ext' (Eq.symm ha)
end subring
/-- Construct a `subring` from a set satisfying `is_subring`. -/
def set.to_subring {R : Type u} [ring R] (S : set R) [is_subring S] : subring R :=
subring.mk S sorry sorry sorry sorry sorry
protected theorem subring.exists {R : Type u} [ring R] {s : subring R} {p : ↥s → Prop} : (∃ (x : ↥s), p x) ↔ ∃ (x : R), ∃ (H : x ∈ s), p { val := x, property := H } :=
set_coe.exists
protected theorem subring.forall {R : Type u} [ring R] {s : subring R} {p : ↥s → Prop} : (∀ (x : ↥s), p x) ↔ ∀ (x : R) (H : x ∈ s), p { val := x, property := H } :=
set_coe.forall
/-- A `subsemiring` containing -1 is a `subring`. -/
def subsemiring.to_subring {R : Type u} [ring R] (s : subsemiring R) (hneg : -1 ∈ s) : subring R :=
subring.mk (submonoid.carrier (subsemiring.to_submonoid s)) sorry sorry sorry sorry sorry
namespace subring
/-- Two subrings are equal if the underlying subsets are equal. -/
theorem ext' {R : Type u} [ring R] {s : subring R} {t : subring R} (h : ↑s = ↑t) : s = t := sorry
/-- Two subrings are equal if and only if the underlying subsets are equal. -/
protected theorem ext'_iff {R : Type u} [ring R] {s : subring R} {t : subring R} : s = t ↔ ↑s = ↑t :=
{ mp := fun (h : s = t) => h ▸ rfl, mpr := fun (h : ↑s = ↑t) => ext' h }
/-- Two subrings are equal if they have the same elements. -/
theorem ext {R : Type u} [ring R] {S : subring R} {T : subring R} (h : ∀ (x : R), x ∈ S ↔ x ∈ T) : S = T :=
ext' (set.ext h)
/-- A subring contains the ring's 1. -/
theorem one_mem {R : Type u} [ring R] (s : subring R) : 1 ∈ s :=
one_mem' s
/-- A subring contains the ring's 0. -/
theorem zero_mem {R : Type u} [ring R] (s : subring R) : 0 ∈ s :=
zero_mem' s
/-- A subring is closed under multiplication. -/
theorem mul_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} : x ∈ s → y ∈ s → x * y ∈ s :=
mul_mem' s
/-- A subring is closed under addition. -/
theorem add_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} : x ∈ s → y ∈ s → x + y ∈ s :=
add_mem' s
/-- A subring is closed under negation. -/
theorem neg_mem {R : Type u} [ring R] (s : subring R) {x : R} : x ∈ s → -x ∈ s :=
neg_mem' s
/-- A subring is closed under subtraction -/
theorem sub_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s :=
eq.mpr (id (Eq._oldrec (Eq.refl (x - y ∈ s)) (sub_eq_add_neg x y))) (add_mem s hx (neg_mem s hy))
/-- Product of a list of elements in a subring is in the subring. -/
theorem list_prod_mem {R : Type u} [ring R] (s : subring R) {l : List R} : (∀ (x : R), x ∈ l → x ∈ s) → list.prod l ∈ s :=
submonoid.list_prod_mem (to_submonoid s)
/-- Sum of a list of elements in a subring is in the subring. -/
theorem list_sum_mem {R : Type u} [ring R] (s : subring R) {l : List R} : (∀ (x : R), x ∈ l → x ∈ s) → list.sum l ∈ s :=
add_subgroup.list_sum_mem (to_add_subgroup s)
/-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/
theorem multiset_prod_mem {R : Type u_1} [comm_ring R] (s : subring R) (m : multiset R) : (∀ (a : R), a ∈ m → a ∈ s) → multiset.prod m ∈ s :=
submonoid.multiset_prod_mem (to_submonoid s) m
/-- Sum of a multiset of elements in an `subring` of a `ring` is
in the `subring`. -/
theorem multiset_sum_mem {R : Type u_1} [ring R] (s : subring R) (m : multiset R) : (∀ (a : R), a ∈ m → a ∈ s) → multiset.sum m ∈ s :=
add_subgroup.multiset_sum_mem (to_add_subgroup s) m
/-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the
subring. -/
theorem prod_mem {R : Type u_1} [comm_ring R] (s : subring R) {ι : Type u_2} {t : finset ι} {f : ι → R} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.prod t fun (i : ι) => f i) ∈ s :=
submonoid.prod_mem (to_submonoid s) h
/-- Sum of elements in a `subring` of a `ring` indexed by a `finset`
is in the `subring`. -/
theorem sum_mem {R : Type u_1} [ring R] (s : subring R) {ι : Type u_2} {t : finset ι} {f : ι → R} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.sum t fun (i : ι) => f i) ∈ s :=
add_subgroup.sum_mem (to_add_subgroup s) h
theorem pow_mem {R : Type u} [ring R] (s : subring R) {x : R} (hx : x ∈ s) (n : ℕ) : x ^ n ∈ s :=
submonoid.pow_mem (to_submonoid s) hx n
theorem gsmul_mem {R : Type u} [ring R] (s : subring R) {x : R} (hx : x ∈ s) (n : ℤ) : n •ℤ x ∈ s :=
add_subgroup.gsmul_mem (to_add_subgroup s) hx n
theorem coe_int_mem {R : Type u} [ring R] (s : subring R) (n : ℤ) : ↑n ∈ s := sorry
/-- A subring of a ring inherits a ring structure -/
protected instance to_ring {R : Type u} [ring R] (s : subring R) : ring ↥s :=
ring.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry
monoid.mul sorry monoid.one sorry sorry sorry sorry
@[simp] theorem coe_add {R : Type u} [ring R] (s : subring R) (x : ↥s) (y : ↥s) : ↑(x + y) = ↑x + ↑y :=
rfl
@[simp] theorem coe_neg {R : Type u} [ring R] (s : subring R) (x : ↥s) : ↑(-x) = -↑x :=
rfl
@[simp] theorem coe_mul {R : Type u} [ring R] (s : subring R) (x : ↥s) (y : ↥s) : ↑(x * y) = ↑x * ↑y :=
rfl
@[simp] theorem coe_zero {R : Type u} [ring R] (s : subring R) : ↑0 = 0 :=
rfl
@[simp] theorem coe_one {R : Type u} [ring R] (s : subring R) : ↑1 = 1 :=
rfl
@[simp] theorem coe_eq_zero_iff {R : Type u} [ring R] (s : subring R) {x : ↥s} : ↑x = 0 ↔ x = 0 :=
{ mp := fun (h : ↑x = 0) => subtype.ext (trans h (Eq.symm (coe_zero s))),
mpr := fun (h : x = 0) => Eq.symm h ▸ coe_zero s }
/-- A subring of a `comm_ring` is a `comm_ring`. -/
protected instance to_comm_ring {R : Type u_1} [comm_ring R] (s : subring R) : comm_ring ↥s :=
comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry
sorry sorry sorry
/-- The natural ring hom from a subring of ring `R` to `R`. -/
def subtype {R : Type u} [ring R] (s : subring R) : ↥s →+* R :=
ring_hom.mk coe sorry sorry sorry sorry
@[simp] theorem coe_subtype {R : Type u} [ring R] (s : subring R) : ⇑(subtype s) = coe :=
rfl
/-! # Partial order -/
protected instance partial_order {R : Type u} [ring R] : partial_order (subring R) :=
partial_order.mk (fun (s t : subring R) => ∀ {x : R}, x ∈ s → x ∈ t) partial_order.lt sorry sorry sorry
theorem le_def {R : Type u} [ring R] {s : subring R} {t : subring R} : s ≤ t ↔ ∀ {x : R}, x ∈ s → x ∈ t :=
iff.rfl
@[simp] theorem coe_subset_coe {R : Type u} [ring R] {s : subring R} {t : subring R} : ↑s ⊆ ↑t ↔ s ≤ t :=
iff.rfl
@[simp] theorem coe_ssubset_coe {R : Type u} [ring R] {s : subring R} {t : subring R} : ↑s ⊂ ↑t ↔ s < t :=
iff.rfl
@[simp] theorem mem_coe {R : Type u} [ring R] {S : subring R} {m : R} : m ∈ ↑S ↔ m ∈ S :=
iff.rfl
@[simp] theorem coe_coe {R : Type u} [ring R] (s : subring R) : ↥↑s = ↥s :=
rfl
@[simp] theorem mem_to_submonoid {R : Type u} [ring R] {s : subring R} {x : R} : x ∈ to_submonoid s ↔ x ∈ s :=
iff.rfl
@[simp] theorem coe_to_submonoid {R : Type u} [ring R] (s : subring R) : ↑(to_submonoid s) = ↑s :=
rfl
@[simp] theorem mem_to_add_subgroup {R : Type u} [ring R] {s : subring R} {x : R} : x ∈ to_add_subgroup s ↔ x ∈ s :=
iff.rfl
@[simp] theorem coe_to_add_subgroup {R : Type u} [ring R] (s : subring R) : ↑(to_add_subgroup s) = ↑s :=
rfl
/-! # top -/
/-- The subring `R` of the ring `R`. -/
protected instance has_top {R : Type u} [ring R] : has_top (subring R) :=
has_top.mk (mk (submonoid.carrier ⊤) sorry sorry sorry sorry sorry)
@[simp] theorem mem_top {R : Type u} [ring R] (x : R) : x ∈ ⊤ :=
set.mem_univ x
@[simp] theorem coe_top {R : Type u} [ring R] : ↑⊤ = set.univ :=
rfl
/-! # comap -/
/-- The preimage of a subring along a ring homomorphism is a subring. -/
def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R :=
mk (⇑f ⁻¹' carrier s) sorry sorry sorry sorry sorry
@[simp] theorem coe_comap {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) (f : R →+* S) : ↑(comap f s) = ⇑f ⁻¹' ↑s :=
rfl
@[simp] theorem mem_comap {R : Type u} {S : Type v} [ring R] [ring S] {s : subring S} {f : R →+* S} {x : R} : x ∈ comap f s ↔ coe_fn f x ∈ s :=
iff.rfl
theorem comap_comap {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (s : subring T) (g : S →+* T) (f : R →+* S) : comap f (comap g s) = comap (ring_hom.comp g f) s :=
rfl
/-! # map -/
/-- The image of a subring along a ring homomorphism is a subring. -/
def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S :=
mk (⇑f '' carrier s) sorry sorry sorry sorry sorry
@[simp] theorem coe_map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : ↑(map f s) = ⇑f '' ↑s :=
rfl
@[simp] theorem mem_map {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {s : subring R} {y : S} : y ∈ map f s ↔ ∃ (x : R), ∃ (H : x ∈ s), coe_fn f x = y :=
set.mem_image_iff_bex
theorem map_map {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (s : subring R) (g : S →+* T) (f : R →+* S) : map g (map f s) = map (ring_hom.comp g f) s :=
ext' (set.image_image (fun (a : S) => coe_fn g a) (fun (a : R) => coe_fn f a) (carrier s))
theorem map_le_iff_le_comap {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {s : subring R} {t : subring S} : map f s ≤ t ↔ s ≤ comap f t :=
set.image_subset_iff
theorem gc_map_comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : galois_connection (map f) (comap f) :=
fun (S_1 : subring R) (T : subring S) => map_le_iff_le_comap
end subring
namespace ring_hom
/-! # range -/
/-- The range of a ring homomorphism, as a subring of the target. -/
def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S :=
subring.map f ⊤
@[simp] theorem coe_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : ↑(range f) = set.range ⇑f :=
set.image_univ
@[simp] theorem mem_range {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {y : S} : y ∈ range f ↔ ∃ (x : R), coe_fn f x = y := sorry
theorem mem_range_self {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (x : R) : coe_fn f x ∈ range f :=
iff.mpr mem_range (Exists.intro x rfl)
theorem map_range {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (g : S →+* T) (f : R →+* S) : subring.map g (range f) = range (comp g f) :=
subring.map_map ⊤ g f
-- TODO -- rename to `cod_restrict` when is_ring_hom is deprecated
/-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/
def cod_restrict' {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) (h : ∀ (x : R), coe_fn f x ∈ s) : R →+* ↥s :=
mk (fun (x : R) => { val := coe_fn f x, property := h x }) sorry sorry sorry sorry
theorem surjective_onto_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : function.surjective ⇑(cod_restrict' f (range f) (mem_range_self f)) := sorry
end ring_hom
namespace subring
/-- A subring of a commutative ring is a commutative ring. -/
def subset_comm_ring {cR : Type u} [comm_ring cR] (S : subring cR) : comm_ring ↥S :=
comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry
sorry sorry sorry
/-- A subring of a non-trivial ring is non-trivial. -/
protected instance nontrivial {D : Type u_1} [ring D] [nontrivial D] (S : subring D) : nontrivial ↥S :=
subsemiring.nontrivial (to_subsemiring S)
/-- A subring of a ring with no zero divisors has no zero divisors. -/
protected instance no_zero_divisors {D : Type u_1} [ring D] [no_zero_divisors D] (S : subring D) : no_zero_divisors ↥S :=
subsemiring.no_zero_divisors (to_subsemiring S)
/-- A subring of an integral domain is an integral domain. -/
protected instance subring.domain {D : Type u_1} [integral_domain D] (S : subring D) : integral_domain ↥S :=
integral_domain.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul
sorry comm_ring.one sorry sorry sorry sorry sorry sorry sorry
/-! # bot -/
protected instance has_bot {R : Type u} [ring R] : has_bot (subring R) :=
has_bot.mk (ring_hom.range (int.cast_ring_hom R))
protected instance inhabited {R : Type u} [ring R] : Inhabited (subring R) :=
{ default := ⊥ }
theorem coe_bot {R : Type u} [ring R] : ↑⊥ = set.range coe :=
ring_hom.coe_range (int.cast_ring_hom R)
theorem mem_bot {R : Type u} [ring R] {x : R} : x ∈ ⊥ ↔ ∃ (n : ℤ), ↑n = x :=
ring_hom.mem_range
/-! # inf -/
/-- The inf of two subrings is their intersection. -/
protected instance has_inf {R : Type u} [ring R] : has_inf (subring R) :=
has_inf.mk fun (s t : subring R) => mk (↑s ∩ ↑t) sorry sorry sorry sorry sorry
@[simp] theorem coe_inf {R : Type u} [ring R] (p : subring R) (p' : subring R) : ↑(p ⊓ p') = ↑p ∩ ↑p' :=
rfl
@[simp] theorem mem_inf {R : Type u} [ring R] {p : subring R} {p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' :=
iff.rfl
protected instance has_Inf {R : Type u} [ring R] : has_Inf (subring R) :=
has_Inf.mk
fun (s : set (subring R)) =>
subring.mk' (set.Inter fun (t : subring R) => set.Inter fun (H : t ∈ s) => ↑t)
(infi fun (t : subring R) => infi fun (H : t ∈ s) => to_submonoid t)
(infi fun (t : subring R) => infi fun (H : t ∈ s) => to_add_subgroup t) sorry sorry
@[simp] theorem coe_Inf {R : Type u} [ring R] (S : set (subring R)) : ↑(Inf S) = set.Inter fun (s : subring R) => set.Inter fun (H : s ∈ S) => ↑s :=
rfl
theorem mem_Inf {R : Type u} [ring R] {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ (p : subring R), p ∈ S → x ∈ p :=
set.mem_bInter_iff
@[simp] theorem Inf_to_submonoid {R : Type u} [ring R] (s : set (subring R)) : to_submonoid (Inf s) = infi fun (t : subring R) => infi fun (H : t ∈ s) => to_submonoid t :=
mk'_to_submonoid (has_Inf._proof_1 s) (has_Inf._proof_2 s)
@[simp] theorem Inf_to_add_subgroup {R : Type u} [ring R] (s : set (subring R)) : to_add_subgroup (Inf s) = infi fun (t : subring R) => infi fun (H : t ∈ s) => to_add_subgroup t :=
mk'_to_add_subgroup (has_Inf._proof_1 s) (has_Inf._proof_2 s)
/-- Subrings of a ring form a complete lattice. -/
protected instance complete_lattice {R : Type u} [ring R] : complete_lattice (subring R) :=
complete_lattice.mk complete_lattice.sup complete_lattice.le complete_lattice.lt sorry sorry sorry sorry sorry sorry
has_inf.inf sorry sorry sorry ⊤ sorry ⊥ sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry
/-! # subring closure of a subset -/
/-- The `subring` generated by a set. -/
def closure {R : Type u} [ring R] (s : set R) : subring R :=
Inf (set_of fun (S : subring R) => s ⊆ ↑S)
theorem mem_closure {R : Type u} [ring R] {x : R} {s : set R} : x ∈ closure s ↔ ∀ (S : subring R), s ⊆ ↑S → x ∈ S :=
mem_Inf
/-- The subring generated by a set includes the set. -/
@[simp] theorem subset_closure {R : Type u} [ring R] {s : set R} : s ⊆ ↑(closure s) :=
fun (x : R) (hx : x ∈ s) => iff.mpr mem_closure fun (S : subring R) (hS : s ⊆ ↑S) => hS hx
/-- A subring `t` includes `closure s` if and only if it includes `s`. -/
@[simp] theorem closure_le {R : Type u} [ring R] {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ ↑t :=
{ mp := set.subset.trans subset_closure, mpr := fun (h : s ⊆ ↑t) => Inf_le h }
/-- Subring closure of a set is monotone in its argument: if `s ⊆ t`,
then `closure s ≤ closure t`. -/
theorem closure_mono {R : Type u} [ring R] {s : set R} {t : set R} (h : s ⊆ t) : closure s ≤ closure t :=
iff.mpr closure_le (set.subset.trans h subset_closure)
theorem closure_eq_of_le {R : Type u} [ring R] {s : set R} {t : subring R} (h₁ : s ⊆ ↑t) (h₂ : t ≤ closure s) : closure s = t :=
le_antisymm (iff.mpr closure_le h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements
of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements
of the closure of `s`. -/
theorem closure_induction {R : Type u} [ring R] {s : set R} {p : R → Prop} {x : R} (h : x ∈ closure s) (Hs : ∀ (x : R), x ∈ s → p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ (x y : R), p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ (x y : R), p x → p y → p (x * y)) : p x :=
iff.mpr closure_le Hs x h
theorem mem_closure_iff {R : Type u} [ring R] {s : set R} {x : R} : x ∈ closure s ↔ x ∈ add_subgroup.closure ↑(submonoid.closure s) := sorry
theorem exists_list_of_mem_closure {R : Type u} [ring R] {s : set R} {x : R} (h : x ∈ closure s) : ∃ (L : List (List R)), (∀ (t : List R), t ∈ L → ∀ (y : R), y ∈ t → y ∈ s ∨ y = -1) ∧ list.sum (list.map list.prod L) = x := sorry
/-- `closure` forms a Galois insertion with the coercion to set. -/
protected def gi (R : Type u) [ring R] : galois_insertion closure coe :=
galois_insertion.mk (fun (s : set R) (_x : ↑(closure s) ≤ s) => closure s) sorry sorry sorry
/-- Closure of a subring `S` equals `S`. -/
theorem closure_eq {R : Type u} [ring R] (s : subring R) : closure ↑s = s :=
galois_insertion.l_u_eq (subring.gi R) s
@[simp] theorem closure_empty {R : Type u} [ring R] : closure ∅ = ⊥ :=
galois_connection.l_bot (galois_insertion.gc (subring.gi R))
@[simp] theorem closure_univ {R : Type u} [ring R] : closure set.univ = ⊤ :=
coe_top ▸ closure_eq ⊤
theorem closure_union {R : Type u} [ring R] (s : set R) (t : set R) : closure (s ∪ t) = closure s ⊔ closure t :=
galois_connection.l_sup (galois_insertion.gc (subring.gi R))
theorem closure_Union {R : Type u} [ring R] {ι : Sort u_1} (s : ι → set R) : closure (set.Union fun (i : ι) => s i) = supr fun (i : ι) => closure (s i) :=
galois_connection.l_supr (galois_insertion.gc (subring.gi R))
theorem closure_sUnion {R : Type u} [ring R] (s : set (set R)) : closure (⋃₀s) = supr fun (t : set R) => supr fun (H : t ∈ s) => closure t :=
galois_connection.l_Sup (galois_insertion.gc (subring.gi R))
theorem map_sup {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring R) (f : R →+* S) : map f (s ⊔ t) = map f s ⊔ map f t :=
galois_connection.l_sup (gc_map_comap f)
theorem map_supr {R : Type u} {S : Type v} [ring R] [ring S] {ι : Sort u_1} (f : R →+* S) (s : ι → subring R) : map f (supr s) = supr fun (i : ι) => map f (s i) :=
galois_connection.l_supr (gc_map_comap f)
theorem comap_inf {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) (t : subring S) (f : R →+* S) : comap f (s ⊓ t) = comap f s ⊓ comap f t :=
galois_connection.u_inf (gc_map_comap f)
theorem comap_infi {R : Type u} {S : Type v} [ring R] [ring S] {ι : Sort u_1} (f : R →+* S) (s : ι → subring S) : comap f (infi s) = infi fun (i : ι) => comap f (s i) :=
galois_connection.u_infi (gc_map_comap f)
@[simp] theorem map_bot {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : map f ⊥ = ⊥ :=
galois_connection.l_bot (gc_map_comap f)
@[simp] theorem comap_top {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : comap f ⊤ = ⊤ :=
galois_connection.u_top (gc_map_comap f)
/-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s × t`
as a subring of `R × S`. -/
def prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : subring (R × S) :=
mk (set.prod ↑s ↑t) sorry sorry sorry sorry sorry
theorem coe_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : ↑(prod s t) = set.prod ↑s ↑t :=
rfl
theorem mem_prod {R : Type u} {S : Type v} [ring R] [ring S] {s : subring R} {t : subring S} {p : R × S} : p ∈ prod s t ↔ prod.fst p ∈ s ∧ prod.snd p ∈ t :=
iff.rfl
theorem prod_mono {R : Type u} {S : Type v} [ring R] [ring S] {s₁ : subring R} {s₂ : subring R} (hs : s₁ ≤ s₂) {t₁ : subring S} {t₂ : subring S} (ht : t₁ ≤ t₂) : prod s₁ t₁ ≤ prod s₂ t₂ :=
set.prod_mono hs ht
theorem prod_mono_right {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) : monotone fun (t : subring S) => prod s t :=
prod_mono (le_refl s)
theorem prod_mono_left {R : Type u} {S : Type v} [ring R] [ring S] (t : subring S) : monotone fun (s : subring R) => prod s t :=
fun (s₁ s₂ : subring R) (hs : s₁ ≤ s₂) => prod_mono hs (le_refl t)
theorem prod_top {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) : prod s ⊤ = comap (ring_hom.fst R S) s := sorry
theorem top_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) : prod ⊤ s = comap (ring_hom.snd R S) s := sorry
@[simp] theorem top_prod_top {R : Type u} {S : Type v} [ring R] [ring S] : prod ⊤ ⊤ = ⊤ :=
Eq.trans (top_prod ⊤) (comap_top (ring_hom.snd R S))
/-- Product of subrings is isomorphic to their product as rings. -/
def prod_equiv {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : ↥(prod s t) ≃+* ↥s × ↥t :=
ring_equiv.mk (equiv.to_fun (equiv.set.prod ↑s ↑t)) (equiv.inv_fun (equiv.set.prod ↑s ↑t)) sorry sorry sorry sorry
/-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings.
Note that this fails without the directedness assumption (the union of two subrings is
typically not a subring) -/
theorem mem_supr_of_directed {R : Type u} [ring R] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subring R} (hS : directed LessEq S) {x : R} : (x ∈ supr fun (i : ι) => S i) ↔ ∃ (i : ι), x ∈ S i := sorry
theorem coe_supr_of_directed {R : Type u} [ring R] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subring R} (hS : directed LessEq S) : ↑(supr fun (i : ι) => S i) = set.Union fun (i : ι) => ↑(S i) := sorry
theorem mem_Sup_of_directed_on {R : Type u} [ring R] {S : set (subring R)} (Sne : set.nonempty S) (hS : directed_on LessEq S) {x : R} : x ∈ Sup S ↔ ∃ (s : subring R), ∃ (H : s ∈ S), x ∈ s := sorry
theorem coe_Sup_of_directed_on {R : Type u} [ring R] {S : set (subring R)} (Sne : set.nonempty S) (hS : directed_on LessEq S) : ↑(Sup S) = set.Union fun (s : subring R) => set.Union fun (H : s ∈ S) => ↑s := sorry
end subring
namespace ring_hom
/-- Restriction of a ring homomorphism to a subring of the domain. -/
def restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : ↥s →+* S :=
comp f (subring.subtype s)
@[simp] theorem restrict_apply {R : Type u} {S : Type v} [ring R] [ring S] {s : subring R} (f : R →+* S) (x : ↥s) : coe_fn (restrict f s) x = coe_fn f ↑x :=
rfl
/-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. -/
def range_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : R →+* ↥(range f) :=
cod_restrict' f (range f) sorry
@[simp] theorem coe_range_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (x : R) : ↑(coe_fn (range_restrict f) x) = coe_fn f x :=
rfl
theorem range_top_iff_surjective {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} : range f = ⊤ ↔ function.surjective ⇑f := sorry
/-- The range of a surjective ring homomorphism is the whole of the codomain. -/
theorem range_top_of_surjective {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (hf : function.surjective ⇑f) : range f = ⊤ :=
iff.mpr range_top_iff_surjective hf
/-- The subring of elements `x : R` such that `f x = g x`, i.e.,
the equalizer of f and g as a subring of R -/
def eq_locus {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (g : R →+* S) : subring R :=
subring.mk (set_of fun (x : R) => coe_fn f x = coe_fn g x) sorry sorry sorry sorry sorry
/-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/
theorem eq_on_set_closure {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {g : R →+* S} {s : set R} (h : set.eq_on (⇑f) (⇑g) s) : set.eq_on ⇑f ⇑g ↑(subring.closure s) :=
(fun (this : subring.closure s ≤ eq_locus f g) => this) (iff.mpr subring.closure_le h)
theorem eq_of_eq_on_set_top {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {g : R →+* S} (h : set.eq_on ⇑f ⇑g ↑⊤) : f = g :=
ext fun (x : R) => h trivial
theorem eq_of_eq_on_set_dense {R : Type u} {S : Type v} [ring R] [ring S] {s : set R} (hs : subring.closure s = ⊤) {f : R →+* S} {g : R →+* S} (h : set.eq_on (⇑f) (⇑g) s) : f = g :=
eq_of_eq_on_set_top (hs ▸ eq_on_set_closure h)
theorem closure_preimage_le {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) : subring.closure (⇑f ⁻¹' s) ≤ subring.comap f (subring.closure s) :=
iff.mpr subring.closure_le
fun (x : R) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr subring.mem_coe (iff.mpr subring.mem_comap (subring.subset_closure hx))
/-- The image under a ring homomorphism of the subring generated by a set equals
the subring generated by the image of the set. -/
theorem map_closure {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set R) : subring.map f (subring.closure s) = subring.closure (⇑f '' s) := sorry
end ring_hom
namespace subring
/-- The ring homomorphism associated to an inclusion of subrings. -/
def inclusion {R : Type u} [ring R] {S : subring R} {T : subring R} (h : S ≤ T) : ↥S →* ↥T :=
↑(ring_hom.cod_restrict' (subtype S) T sorry)
@[simp] theorem range_subtype {R : Type u} [ring R] (s : subring R) : ring_hom.range (subtype s) = s :=
ext' (Eq.trans (ring_hom.coe_srange (subtype s)) subtype.range_coe)
@[simp] theorem range_fst {R : Type u} {S : Type v} [ring R] [ring S] : ring_hom.srange (ring_hom.fst R S) = ⊤ :=
ring_hom.srange_top_of_surjective (ring_hom.fst R S) prod.fst_surjective
@[simp] theorem range_snd {R : Type u} {S : Type v} [ring R] [ring S] : ring_hom.srange (ring_hom.snd R S) = ⊤ :=
ring_hom.srange_top_of_surjective (ring_hom.snd R S) prod.snd_surjective
@[simp] theorem prod_bot_sup_bot_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : prod s ⊥ ⊔ prod ⊥ t = prod s t := sorry
end subring
namespace ring_equiv
/-- Makes the identity isomorphism from a proof two subrings of a multiplicative
monoid are equal. -/
def subring_congr {R : Type u} [ring R] {s : subring R} {t : subring R} (h : s = t) : ↥s ≃+* ↥t :=
mk (equiv.to_fun (equiv.set_congr sorry)) (equiv.inv_fun (equiv.set_congr sorry)) sorry sorry sorry sorry
end ring_equiv
namespace subring
protected theorem in_closure.rec_on {R : Type u} [ring R] {s : set R} {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ (z : R), z ∈ s → ∀ (n : R), C n → C (z * n)) (ha : ∀ {x y : R}, C x → C y → C (x + y)) : C x := sorry
theorem closure_preimage_le {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) : closure (⇑f ⁻¹' s) ≤ comap f (closure s) :=
iff.mpr closure_le fun (x : R) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr mem_coe (iff.mpr mem_comap (subset_closure hx))
end subring
theorem add_subgroup.int_mul_mem {R : Type u} [ring R] {G : add_subgroup R} (k : ℤ) {g : R} (h : g ∈ G) : ↑k * g ∈ G := sorry
|
c884619d3fc072b30ba3725070897267668b7943 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /tests/lean/run/alias3.lean | b5e7f914b5527b799e3407b6a5a2a3659cf87b66 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 380 | lean | import logic
namespace N1
context
context
parameter A : Type
definition foo (a : A) : Prop := true
check foo
end
check foo
end
check foo
end N1
check N1.foo
namespace N2
context
parameter A : Type
inductive list : Type :=
| nil {} : list
| cons : A → list → list
check list
end
check list
end N2
check N2.list
|
4734e25a85002e0e42e49eeb8cbebe0197131e83 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/multiplicity_auto.lean | b6f215e769b34fa5a2af26710a67fc87ae2794a2 | [] | 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,267 | 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, Chris Hughes
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.associated
import Mathlib.algebra.big_operators.basic
import Mathlib.data.nat.enat
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-- `multiplicity a b` returns the largest natural number `n` such that
`a ^ n ∣ b`, as an `enat` or natural with infinity. If `∀ n, a ^ n ∣ b`,
then it returns `⊤`-/
def multiplicity {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] (a : α) (b : α) : enat :=
enat.find fun (n : ℕ) => ¬a ^ (n + 1) ∣ b
namespace multiplicity
/-- `multiplicity.finite a b` indicates that the multiplicity of `a` in `b` is finite. -/
def finite {α : Type u_1} [comm_monoid α] (a : α) (b : α) := ∃ (n : ℕ), ¬a ^ (n + 1) ∣ b
theorem finite_iff_dom {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α} :
finite a b ↔ roption.dom (multiplicity a b) :=
iff.rfl
theorem finite_def {α : Type u_1} [comm_monoid α] {a : α} {b : α} :
finite a b ↔ ∃ (n : ℕ), ¬a ^ (n + 1) ∣ b :=
iff.rfl
theorem int.coe_nat_multiplicity (a : ℕ) (b : ℕ) : multiplicity ↑a ↑b = multiplicity a b := sorry
theorem not_finite_iff_forall {α : Type u_1} [comm_monoid α] {a : α} {b : α} :
¬finite a b ↔ ∀ (n : ℕ), a ^ n ∣ b :=
sorry
theorem not_unit_of_finite {α : Type u_1} [comm_monoid α] {a : α} {b : α} (h : finite a b) :
¬is_unit a :=
sorry
theorem finite_of_finite_mul_left {α : Type u_1} [comm_monoid α] {a : α} {b : α} {c : α} :
finite a (b * c) → finite a c :=
sorry
theorem finite_of_finite_mul_right {α : Type u_1} [comm_monoid α] {a : α} {b : α} {c : α} :
finite a (b * c) → finite a b :=
eq.mpr (id (Eq._oldrec (Eq.refl (finite a (b * c) → finite a b)) (mul_comm b c)))
finite_of_finite_mul_left
theorem pow_dvd_of_le_multiplicity {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} {k : ℕ} : ↑k ≤ multiplicity a b → a ^ k ∣ b :=
sorry
theorem pow_multiplicity_dvd {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} (h : finite a b) : a ^ roption.get (multiplicity a b) h ∣ b :=
pow_dvd_of_le_multiplicity
(eq.mpr
(id
(Eq._oldrec (Eq.refl (↑(roption.get (multiplicity a b) h) ≤ multiplicity a b))
(enat.coe_get h)))
(le_refl (multiplicity a b)))
theorem is_greatest {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α}
{m : ℕ} (hm : multiplicity a b < ↑m) : ¬a ^ m ∣ b :=
sorry
theorem is_greatest' {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α}
{m : ℕ} (h : finite a b) (hm : roption.get (multiplicity a b) h < m) : ¬a ^ m ∣ b :=
is_greatest
(eq.mp (Eq._oldrec (Eq.refl (↑(roption.get (multiplicity a b) h) < ↑m)) (enat.coe_get h))
(eq.mp
(Eq._oldrec (Eq.refl (roption.get (multiplicity a b) h < m))
(Eq.symm (propext enat.coe_lt_coe)))
hm))
theorem unique {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α} {k : ℕ}
(hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) : ↑k = multiplicity a b :=
sorry
theorem unique' {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α} {k : ℕ}
(hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) :
k = roption.get (multiplicity a b) (Exists.intro k hsucc) :=
sorry
theorem le_multiplicity_of_pow_dvd {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} {k : ℕ} (hk : a ^ k ∣ b) : ↑k ≤ multiplicity a b :=
le_of_not_gt fun (hk' : ↑k > multiplicity a b) => is_greatest hk' hk
theorem pow_dvd_iff_le_multiplicity {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd]
{a : α} {b : α} {k : ℕ} : a ^ k ∣ b ↔ ↑k ≤ multiplicity a b :=
{ mp := le_multiplicity_of_pow_dvd, mpr := pow_dvd_of_le_multiplicity }
theorem multiplicity_lt_iff_neg_dvd {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd]
{a : α} {b : α} {k : ℕ} : multiplicity a b < ↑k ↔ ¬a ^ k ∣ b :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (multiplicity a b < ↑k ↔ ¬a ^ k ∣ b))
(propext pow_dvd_iff_le_multiplicity)))
(eq.mpr
(id (Eq._oldrec (Eq.refl (multiplicity a b < ↑k ↔ ¬↑k ≤ multiplicity a b)) (propext not_le)))
(iff.refl (multiplicity a b < ↑k)))
theorem eq_some_iff {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α}
{n : ℕ} : multiplicity a b = ↑n ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b :=
sorry
theorem eq_top_iff {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α} {b : α} :
multiplicity a b = ⊤ ↔ ∀ (n : ℕ), a ^ n ∣ b :=
sorry
theorem one_right {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
(ha : ¬is_unit a) : multiplicity a 1 = 0 :=
sorry
@[simp] theorem get_one_right {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
(ha : finite a 1) : roption.get (multiplicity a 1) ha = 0 :=
sorry
@[simp] theorem multiplicity_unit {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
(b : α) (ha : is_unit a) : multiplicity a b = ⊤ :=
iff.mpr eq_top_iff fun (_x : ℕ) => iff.mp is_unit_iff_forall_dvd (is_unit.pow _x ha) b
@[simp] theorem one_left {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] (b : α) :
multiplicity 1 b = ⊤ :=
sorry
theorem multiplicity_eq_zero_of_not_dvd {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd]
{a : α} {b : α} (ha : ¬a ∣ b) : multiplicity a b = 0 :=
sorry
theorem eq_top_iff_not_finite {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} : multiplicity a b = ⊤ ↔ ¬finite a b :=
roption.eq_none_iff'
theorem multiplicity_le_multiplicity_iff {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd]
{a : α} {b : α} {c : α} {d : α} :
multiplicity a b ≤ multiplicity c d ↔ ∀ (n : ℕ), a ^ n ∣ b → c ^ n ∣ d :=
sorry
theorem multiplicity_le_multiplicity_of_dvd {α : Type u_1} [comm_monoid α]
[DecidableRel has_dvd.dvd] {a : α} {b : α} {c : α} (hdvd : a ∣ b) :
multiplicity b c ≤ multiplicity a c :=
iff.mpr multiplicity_le_multiplicity_iff
fun (n : ℕ) (h : b ^ n ∣ c) => dvd_trans (pow_dvd_pow_of_dvd hdvd n) h
theorem dvd_of_multiplicity_pos {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} (h : 0 < multiplicity a b) : a ∣ b :=
eq.mpr (id (Eq._oldrec (Eq.refl (a ∣ b)) (Eq.symm (pow_one a))))
(pow_dvd_of_le_multiplicity (iff.mp enat.pos_iff_one_le h))
theorem dvd_iff_multiplicity_pos {α : Type u_1} [comm_monoid α] [DecidableRel has_dvd.dvd] {a : α}
{b : α} : 0 < multiplicity a b ↔ a ∣ b :=
sorry
theorem finite_nat_iff {a : ℕ} {b : ℕ} : finite a b ↔ a ≠ 1 ∧ 0 < b := sorry
theorem ne_zero_of_finite {α : Type u_1} [comm_monoid_with_zero α] {a : α} {b : α}
(h : finite a b) : b ≠ 0 :=
sorry
@[simp] protected theorem zero {α : Type u_1} [comm_monoid_with_zero α] [DecidableRel has_dvd.dvd]
(a : α) : multiplicity a 0 = ⊤ :=
sorry
@[simp] theorem multiplicity_zero_eq_zero_of_ne_zero {α : Type u_1} [comm_monoid_with_zero α]
[DecidableRel has_dvd.dvd] (a : α) (ha : a ≠ 0) : multiplicity 0 a = 0 :=
multiplicity_eq_zero_of_not_dvd
(eq.mpr (id (Eq._oldrec (Eq.refl (¬0 ∣ a)) (propext zero_dvd_iff))) ha)
theorem min_le_multiplicity_add {α : Type u_1} [comm_semiring α] [DecidableRel has_dvd.dvd] {p : α}
{a : α} {b : α} : min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) :=
sorry
@[simp] protected theorem neg {α : Type u_1} [comm_ring α] [DecidableRel has_dvd.dvd] (a : α)
(b : α) : multiplicity a (-b) = multiplicity a b :=
sorry
theorem multiplicity_add_of_gt {α : Type u_1} [comm_ring α] [DecidableRel has_dvd.dvd] {p : α}
{a : α} {b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a + b) = multiplicity p b :=
sorry
theorem multiplicity_sub_of_gt {α : Type u_1} [comm_ring α] [DecidableRel has_dvd.dvd] {p : α}
{a : α} {b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a - b) = multiplicity p b :=
sorry
theorem multiplicity_add_eq_min {α : Type u_1} [comm_ring α] [DecidableRel has_dvd.dvd] {p : α}
{a : α} {b : α} (h : multiplicity p a ≠ multiplicity p b) :
multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) :=
sorry
theorem finite_mul_aux {α : Type u_1} [comm_cancel_monoid_with_zero α] {p : α} (hp : prime p)
{n : ℕ} {m : ℕ} {a : α} {b : α} :
¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b :=
sorry
theorem finite_mul {α : Type u_1} [comm_cancel_monoid_with_zero α] {p : α} {a : α} {b : α}
(hp : prime p) : finite p a → finite p b → finite p (a * b) :=
sorry
theorem finite_mul_iff {α : Type u_1} [comm_cancel_monoid_with_zero α] {p : α} {a : α} {b : α}
(hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b :=
{ mp :=
fun (h : finite p (a * b)) =>
{ left := finite_of_finite_mul_right h, right := finite_of_finite_mul_left h },
mpr := fun (h : finite p a ∧ finite p b) => finite_mul hp (and.left h) (and.right h) }
theorem finite_pow {α : Type u_1} [comm_cancel_monoid_with_zero α] {p : α} {a : α} (hp : prime p)
{k : ℕ} (ha : finite p a) : finite p (a ^ k) :=
sorry
@[simp] theorem multiplicity_self {α : Type u_1} [comm_cancel_monoid_with_zero α]
[DecidableRel has_dvd.dvd] {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) : multiplicity a a = 1 :=
sorry
@[simp] theorem get_multiplicity_self {α : Type u_1} [comm_cancel_monoid_with_zero α]
[DecidableRel has_dvd.dvd] {a : α} (ha : finite a a) : roption.get (multiplicity a a) ha = 1 :=
sorry
protected theorem mul' {α : Type u_1} [comm_cancel_monoid_with_zero α] [DecidableRel has_dvd.dvd]
{p : α} {a : α} {b : α} (hp : prime p) (h : roption.dom (multiplicity p (a * b))) :
roption.get (multiplicity p (a * b)) h =
roption.get (multiplicity p a) (and.left (iff.mp (finite_mul_iff hp) h)) +
roption.get (multiplicity p b) (and.right (iff.mp (finite_mul_iff hp) h)) :=
sorry
protected theorem mul {α : Type u_1} [comm_cancel_monoid_with_zero α] [DecidableRel has_dvd.dvd]
{p : α} {a : α} {b : α} (hp : prime p) :
multiplicity p (a * b) = multiplicity p a + multiplicity p b :=
sorry
theorem finset.prod {α : Type u_1} [comm_cancel_monoid_with_zero α] [DecidableRel has_dvd.dvd]
{β : Type u_2} {p : α} (hp : prime p) (s : finset β) (f : β → α) :
multiplicity p (finset.prod s fun (x : β) => f x) =
finset.sum s fun (x : β) => multiplicity p (f x) :=
sorry
protected theorem pow' {α : Type u_1} [comm_cancel_monoid_with_zero α] [DecidableRel has_dvd.dvd]
{p : α} {a : α} (hp : prime p) (ha : finite p a) {k : ℕ} :
roption.get (multiplicity p (a ^ k)) (finite_pow hp ha) =
k * roption.get (multiplicity p a) ha :=
sorry
theorem pow {α : Type u_1} [comm_cancel_monoid_with_zero α] [DecidableRel has_dvd.dvd] {p : α}
{a : α} (hp : prime p) {k : ℕ} : multiplicity p (a ^ k) = k •ℕ multiplicity p a :=
sorry
theorem multiplicity_pow_self {α : Type u_1} [comm_cancel_monoid_with_zero α]
[DecidableRel has_dvd.dvd] {p : α} (h0 : p ≠ 0) (hu : ¬is_unit p) (n : ℕ) :
multiplicity p (p ^ n) = ↑n :=
sorry
theorem multiplicity_pow_self_of_prime {α : Type u_1} [comm_cancel_monoid_with_zero α]
[DecidableRel has_dvd.dvd] {p : α} (hp : prime p) (n : ℕ) : multiplicity p (p ^ n) = ↑n :=
multiplicity_pow_self (prime.ne_zero hp) (prime.not_unit hp) n
end multiplicity
theorem multiplicity_eq_zero_of_coprime {p : ℕ} {a : ℕ} {b : ℕ} (hp : p ≠ 1)
(hle : multiplicity p a ≤ multiplicity p b) (hab : nat.coprime a b) : multiplicity p a = 0 :=
sorry
end Mathlib |
90ee211498bd0d8394a15a1b33f02458cca335e9 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/order/bounds.lean | d9972992fe65d054cac8316500e5282c25d45ef8 | [
"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 | 45,240 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov
-/
import data.set.intervals.basic
/-!
# Upper / lower bounds
In this file we define:
* `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set;
* `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper
(resp., lower) bounds of `s` is nonempty;
* `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`;
for a partial order, it is unique if exists;
* `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound)
of `s`; for a partial order, it is unique if exists.
We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide
formulas for `∅`, `univ`, and intervals.
-/
open set order_dual (to_dual of_dual)
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section
variables [preorder α] [preorder β] {s t : set α} {a b : α}
/-!
### Definitions
-/
/-- The set of upper bounds of a set. -/
def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x }
/-- The set of lower bounds of a set. -/
def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a }
/-- A set is bounded above if there exists an upper bound. -/
def bdd_above (s : set α) := (upper_bounds s).nonempty
/-- A set is bounded below if there exists a lower bound. -/
def bdd_below (s : set α) := (lower_bounds s).nonempty
/-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/
def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s
/-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/
def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s
/-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/
def is_lub (s : set α) : α → Prop := is_least (upper_bounds s)
/-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/
def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s)
lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl
lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl
lemma bdd_above_def : bdd_above s ↔ ∃ x, ∀ y ∈ s, y ≤ x := iff.rfl
lemma bdd_below_def : bdd_below s ↔ ∃ x, ∀ y ∈ s, x ≤ y := iff.rfl
lemma bot_mem_lower_bounds [order_bot α] (s : set α) : ⊥ ∈ lower_bounds s := λ _ _, bot_le
lemma top_mem_upper_bounds [order_top α] (s : set α) : ⊤ ∈ upper_bounds s := λ _ _, le_top
@[simp] lemma is_least_bot_iff [order_bot α] : is_least s ⊥ ↔ ⊥ ∈ s :=
and_iff_left $ bot_mem_lower_bounds _
@[simp] lemma is_greatest_top_iff [order_top α] : is_greatest s ⊤ ↔ ⊤ ∈ s :=
and_iff_left $ top_mem_upper_bounds _
/-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x`
is not greater than or equal to `y`. This version only assumes `preorder` structure and uses
`¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/
lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) :=
by simp [bdd_above, upper_bounds, set.nonempty]
/-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x`
is not less than or equal to `y`. This version only assumes `preorder` structure and uses
`¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/
lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬ x ≤ y := @not_bdd_above_iff' αᵒᵈ _ _
/-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater
than `x`. A version for preorders is called `not_bdd_above_iff'`. -/
lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} :
¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y :=
by simp only [not_bdd_above_iff', not_le]
/-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less
than `x`. A version for preorders is called `not_bdd_below_iff'`. -/
lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} :
¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x :=
@not_bdd_above_iff αᵒᵈ _ _
lemma bdd_above.dual (h : bdd_above s) : bdd_below (of_dual ⁻¹' s) := h
lemma bdd_below.dual (h : bdd_below s) : bdd_above (of_dual ⁻¹' s) := h
lemma is_least.dual (h : is_least s a) : is_greatest (of_dual ⁻¹' s) (to_dual a) := h
lemma is_greatest.dual (h : is_greatest s a) : is_least (of_dual ⁻¹' s) (to_dual a) := h
lemma is_lub.dual (h : is_lub s a) : is_glb (of_dual ⁻¹' s) (to_dual a) := h
lemma is_glb.dual (h : is_glb s a) : is_lub (of_dual ⁻¹' s) (to_dual a) := h
/-- If `a` is the least element of a set `s`, then subtype `s` is an order with bottom element. -/
@[reducible] def is_least.order_bot (h : is_least s a) : order_bot s :=
{ bot := ⟨a, h.1⟩,
bot_le := subtype.forall.2 h.2 }
/-- If `a` is the greatest element of a set `s`, then subtype `s` is an order with top element. -/
@[reducible] def is_greatest.order_top (h : is_greatest s a) : order_top s :=
{ top := ⟨a, h.1⟩,
le_top := subtype.forall.2 h.2 }
/-!
### Monotonicity
-/
lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
upper_bounds t ⊆ upper_bounds s :=
λ b hb x h, hb $ hst h
lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
lower_bounds t ⊆ lower_bounds s :=
λ b hb x h, hb $ hst h
lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s :=
λ ha x h, le_trans (ha h) hab
lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s :=
λ hb x h, le_trans hab (hb h)
lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
a ∈ upper_bounds t → b ∈ upper_bounds s :=
λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha
lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
b ∈ lower_bounds t → a ∈ lower_bounds s :=
λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb
/-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/
lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s :=
nonempty.mono $ upper_bounds_mono_set h
/-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/
lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s :=
nonempty.mono $ lower_bounds_mono_set h
/-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a :=
⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩
/-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a :=
hs.dual.of_subset_of_superset hp hst htp
lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a :=
hb.2 (hst ha.1)
lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b :=
hb.2 (hst ha.1)
lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b :=
hb.mono ha $ upper_bounds_mono_set hst
lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a :=
hb.mono ha $ lower_bounds_mono_set hst
lemma subset_lower_bounds_upper_bounds (s : set α) : s ⊆ lower_bounds (upper_bounds s) :=
λ x hx y hy, hy hx
lemma subset_upper_bounds_lower_bounds (s : set α) : s ⊆ upper_bounds (lower_bounds s) :=
λ x hx y hy, hy hx
lemma set.nonempty.bdd_above_lower_bounds (hs : s.nonempty) : bdd_above (lower_bounds s) :=
hs.mono (subset_upper_bounds_lower_bounds s)
lemma set.nonempty.bdd_below_upper_bounds (hs : s.nonempty) : bdd_below (upper_bounds s) :=
hs.mono (subset_lower_bounds_upper_bounds s)
/-!
### Conversions
-/
lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a :=
set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩
lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a := h.dual.upper_bounds_eq
lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a :=
h.is_glb.lower_bounds_eq
lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a :=
h.is_lub.upper_bounds_eq
lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s :=
by { rw h.upper_bounds_eq, refl }
lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s :=
by { rw h.lower_bounds_eq, refl }
lemma is_lub_iff_le_iff : is_lub s a ↔ ∀ b, a ≤ b ↔ b ∈ upper_bounds s :=
⟨λ h b, is_lub_le_iff h, λ H, ⟨(H _).1 le_rfl, λ b hb, (H b).2 hb⟩⟩
lemma is_glb_iff_le_iff : is_glb s a ↔ ∀ b, b ≤ a ↔ b ∈ lower_bounds s :=
@is_lub_iff_le_iff αᵒᵈ _ _ _
/-- If `s` has a least upper bound, then it is bounded above. -/
lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩
/-- If `s` has a greatest lower bound, then it is bounded below. -/
lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩
/-- If `s` has a greatest element, then it is bounded above. -/
lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩
/-- If `s` has a least element, then it is bounded below. -/
lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩
lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩
lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩
/-!
### Union and intersection
-/
@[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t :=
subset.antisymm
(λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩)
(λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht))
@[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t :=
@upper_bounds_union αᵒᵈ _ s t
lemma union_upper_bounds_subset_upper_bounds_inter :
upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) :=
union_subset
(upper_bounds_mono_set $ inter_subset_left _ _)
(upper_bounds_mono_set $ inter_subset_right _ _)
lemma union_lower_bounds_subset_lower_bounds_inter :
lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) :=
@union_upper_bounds_subset_upper_bounds_inter αᵒᵈ _ s t
lemma is_least_union_iff {a : α} {s t : set α} :
is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) :=
by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc]
lemma is_greatest_union_iff :
is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨
a ∈ upper_bounds s ∧ is_greatest t a) :=
@is_least_union_iff αᵒᵈ _ a s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/
lemma bdd_above.union [semilattice_sup γ] {s t : set γ} :
bdd_above s → bdd_above t → bdd_above (s ∪ t) :=
begin
rintros ⟨bs, hs⟩ ⟨bt, ht⟩,
use bs ⊔ bt,
rw upper_bounds_union,
exact ⟨upper_bounds_mono_mem le_sup_left hs,
upper_bounds_mono_mem le_sup_right ht⟩
end
/-- The union of two sets is bounded above if and only if each of the sets is. -/
lemma bdd_above_union [semilattice_sup γ] {s t : set γ} :
bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t :=
⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩,
λ h, h.1.union h.2⟩
lemma bdd_below.union [semilattice_inf γ] {s t : set γ} :
bdd_below s → bdd_below t → bdd_below (s ∪ t) :=
@bdd_above.union γᵒᵈ _ s t
/--The union of two sets is bounded above if and only if each of the sets is.-/
lemma bdd_below_union [semilattice_inf γ] {s t : set γ} :
bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t :=
@bdd_above_union γᵒᵈ _ s t
/-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`,
then `a ⊔ b` is the least upper bound of `s ∪ t`. -/
lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ}
(hs : is_lub s a) (ht : is_lub t b) :
is_lub (s ∪ t) (a ⊔ b) :=
⟨λ c h, h.cases_on (λ h, le_sup_of_le_left $ hs.left h) (λ h, le_sup_of_le_right $ ht.left h),
assume c hc, sup_le
(hs.right $ assume d hd, hc $ or.inl hd) (ht.right $ assume d hd, hc $ or.inr hd)⟩
/-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`,
then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/
lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ}
(hs : is_glb s a₁) (ht : is_glb t a₂) :
is_glb (s ∪ t) (a₁ ⊓ a₂) :=
hs.dual.union ht
/-- If `a` is the least element of `s` and `b` is the least element of `t`,
then `min a b` is the least element of `s ∪ t`. -/
lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ}
(ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_glb.union hb.is_glb).1⟩
/-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`,
then `max a b` is the greatest element of `s ∪ t`. -/
lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ}
(ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_lub.union hb.is_lub).1⟩
lemma is_lub.inter_Ici_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_lub s a)
(hb : b ∈ s) : is_lub (s ∩ Ici b) a :=
⟨λ x hx, ha.1 hx.1, λ c hc, have hbc : b ≤ c, from hc ⟨hb, le_rfl⟩,
ha.2 $ λ x hx, (le_total x b).elim (λ hxb, hxb.trans hbc) $ λ hbx, hc ⟨hx, hbx⟩⟩
lemma is_glb.inter_Iic_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_glb s a)
(hb : b ∈ s) : is_glb (s ∩ Iic b) a :=
ha.dual.inter_Ici_of_mem hb
lemma bdd_above_iff_exists_ge [semilattice_sup γ] {s : set γ} (x₀ : γ) :
bdd_above s ↔ ∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x :=
by { rw [bdd_above_def, exists_ge_and_iff_exists], exact monotone.ball (λ x hx, monotone_le) }
lemma bdd_below_iff_exists_le [semilattice_inf γ] {s : set γ} (x₀ : γ) :
bdd_below s ↔ ∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y :=
bdd_above_iff_exists_ge (to_dual x₀)
lemma bdd_above.exists_ge [semilattice_sup γ] {s : set γ} (hs : bdd_above s) (x₀ : γ) :
∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x :=
(bdd_above_iff_exists_ge x₀).mp hs
lemma bdd_below.exists_le [semilattice_inf γ] {s : set γ} (hs : bdd_below s) (x₀ : γ) :
∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y :=
(bdd_below_iff_exists_le x₀).mp hs
/-!
### Specific sets
#### Unbounded intervals
-/
lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩
lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩
lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub
lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb
lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq
lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq
lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above
lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below
lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩
lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩
lemma lub_Iio_le (a : α) (hb : is_lub (set.Iio a) b) : b ≤ a :=
(is_lub_le_iff hb).mpr $ λ k hk, le_of_lt hk
lemma le_glb_Ioi (a : α) (hb : is_glb (set.Ioi a) b) : a ≤ b := @lub_Iio_le αᵒᵈ _ _ a hb
lemma lub_Iio_eq_self_or_Iio_eq_Iic [partial_order γ] {j : γ} (i : γ) (hj : is_lub (set.Iio i) j) :
j = i ∨ set.Iio i = set.Iic j :=
begin
cases eq_or_lt_of_le (lub_Iio_le i hj) with hj_eq_i hj_lt_i,
{ exact or.inl hj_eq_i, },
{ right,
exact set.ext (λ k, ⟨λ hk_lt, hj.1 hk_lt, λ hk_le_j, lt_of_le_of_lt hk_le_j hj_lt_i⟩), },
end
lemma glb_Ioi_eq_self_or_Ioi_eq_Ici [partial_order γ] {j : γ} (i : γ) (hj : is_glb (set.Ioi i) j) :
j = i ∨ set.Ioi i = set.Ici j :=
@lub_Iio_eq_self_or_Iio_eq_Iic γᵒᵈ _ j i hj
section
variables [linear_order γ]
lemma exists_lub_Iio (i : γ) : ∃ j, is_lub (set.Iio i) j :=
begin
by_cases h_exists_lt : ∃ j, j ∈ upper_bounds (set.Iio i) ∧ j < i,
{ obtain ⟨j, hj_ub, hj_lt_i⟩ := h_exists_lt,
exact ⟨j, hj_ub, λ k hk_ub, hk_ub hj_lt_i⟩, },
{ refine ⟨i, λ j hj, le_of_lt hj, _⟩,
rw mem_lower_bounds,
by_contra,
refine h_exists_lt _,
push_neg at h,
exact h, },
end
lemma exists_glb_Ioi (i : γ) : ∃ j, is_glb (set.Ioi i) j := @exists_lub_Iio γᵒᵈ _ i
variables [densely_ordered γ]
lemma is_lub_Iio {a : γ} : is_lub (Iio a) a :=
⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩
lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio γᵒᵈ _ _ a
lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq
lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq
end
/-!
#### Singleton
-/
lemma is_greatest_singleton : is_greatest {a} a :=
⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩
lemma is_least_singleton : is_least {a} a := @is_greatest_singleton αᵒᵈ _ a
lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub
lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb
lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above
lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below
@[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq
@[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq
/-!
#### Bounded intervals
-/
lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩
lemma bdd_below_Icc : bdd_below (Icc a b) := ⟨a, λ _, and.left⟩
lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self
lemma bdd_below_Ico : bdd_below (Ico a b) := bdd_below_Icc.mono Ico_subset_Icc_self
lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self
lemma bdd_below_Ioc : bdd_below (Ioc a b) := bdd_below_Icc.mono Ioc_subset_Icc_self
lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self
lemma bdd_below_Ioo : bdd_below (Ioo a b) := bdd_below_Icc.mono Ioo_subset_Icc_self
lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b :=
⟨right_mem_Icc.2 h, λ x, and.right⟩
lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub
lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b :=
(is_lub_Icc h).upper_bounds_eq
lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a :=
⟨left_mem_Icc.2 h, λ x, and.left⟩
lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb
lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a :=
(is_glb_Icc h).lower_bounds_eq
lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b :=
⟨right_mem_Ioc.2 h, λ x, and.right⟩
lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b :=
(is_greatest_Ioc h).is_lub
lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b :=
(is_lub_Ioc h).upper_bounds_eq
lemma is_least_Ico (h : a < b) : is_least (Ico a b) a :=
⟨left_mem_Ico.2 h, λ x, and.left⟩
lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a :=
(is_least_Ico h).is_glb
lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a :=
(is_glb_Ico h).lower_bounds_eq
section
variables [semilattice_sup γ] [densely_ordered γ]
lemma is_glb_Ioo {a b : γ} (h : a < b) :
is_glb (Ioo a b) a :=
⟨λ x hx, hx.1.le, λ x hx,
begin
cases eq_or_lt_of_le (le_sup_right : a ≤ x ⊔ a) with h₁ h₂,
{ exact h₁.symm ▸ le_sup_left },
obtain ⟨y, lty, ylt⟩ := exists_between h₂,
apply (not_lt_of_le (sup_le (hx ⟨lty, ylt.trans_le (sup_le _ h.le)⟩) lty.le) ylt).elim,
obtain ⟨u, au, ub⟩ := exists_between h,
apply (hx ⟨au, ub⟩).trans ub.le,
end⟩
lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a :=
(is_glb_Ioo hab).lower_bounds_eq
lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a :=
(is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc hab.le) Ioo_subset_Ioc_self Ioc_subset_Icc_self
lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a :=
(is_glb_Ioc hab).lower_bounds_eq
end
section
variables [semilattice_inf γ] [densely_ordered γ]
lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b :=
by simpa only [dual_Ioo] using is_glb_Ioo hab.dual
lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b :=
(is_lub_Ioo hab).upper_bounds_eq
lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b :=
by simpa only [dual_Ioc] using is_glb_Ioc hab.dual
lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b :=
(is_lub_Ico hab).upper_bounds_eq
end
lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl
lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl
lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b :=
by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici,
bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right]
/-!
#### Univ
-/
lemma is_greatest_univ [preorder γ] [order_top γ] : is_greatest (univ : set γ) ⊤ :=
⟨mem_univ _, λ x hx, le_top⟩
@[simp] lemma order_top.upper_bounds_univ [partial_order γ] [order_top γ] :
upper_bounds (univ : set γ) = {⊤} :=
by rw [is_greatest_univ.upper_bounds_eq, Ici_top]
lemma is_lub_univ [preorder γ] [order_top γ] : is_lub (univ : set γ) ⊤ :=
is_greatest_univ.is_lub
@[simp] lemma order_bot.lower_bounds_univ [partial_order γ] [order_bot γ] :
lower_bounds (univ : set γ) = {⊥} :=
@order_top.upper_bounds_univ γᵒᵈ _ _
lemma is_least_univ [preorder γ] [order_bot γ] : is_least (univ : set γ) ⊥ :=
@is_greatest_univ γᵒᵈ _ _
lemma is_glb_univ [preorder γ] [order_bot γ] : is_glb (univ : set γ) ⊥ :=
is_least_univ.is_glb
@[simp] lemma no_max_order.upper_bounds_univ [no_max_order α] : upper_bounds (univ : set α) = ∅ :=
eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := exists_gt b in
not_le_of_lt hx (hb trivial)
@[simp] lemma no_min_order.lower_bounds_univ [no_min_order α] : lower_bounds (univ : set α) = ∅ :=
@no_max_order.upper_bounds_univ αᵒᵈ _ _
@[simp] lemma not_bdd_above_univ [no_max_order α] : ¬bdd_above (univ : set α) :=
by simp [bdd_above]
@[simp] lemma not_bdd_below_univ [no_min_order α] : ¬bdd_below (univ : set α) :=
@not_bdd_above_univ αᵒᵈ _ _
/-!
#### Empty set
-/
@[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ :=
by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff]
@[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ := @upper_bounds_empty αᵒᵈ _
@[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) :=
by simp only [bdd_above, upper_bounds_empty, univ_nonempty]
@[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) :=
by simp only [bdd_below, lower_bounds_empty, univ_nonempty]
lemma is_glb_empty [preorder γ] [order_top γ] : is_glb ∅ (⊤:γ) :=
by simp only [is_glb, lower_bounds_empty, is_greatest_univ]
lemma is_lub_empty [preorder γ] [order_bot γ] : is_lub ∅ (⊥:γ) := @is_glb_empty γᵒᵈ _ _
lemma is_lub.nonempty [no_min_order α] (hs : is_lub s a) : s.nonempty :=
let ⟨a', ha'⟩ := exists_lt a in
ne_empty_iff_nonempty.1 $ assume h,
have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty],
not_le_of_lt ha' this
lemma is_glb.nonempty [no_max_order α] (hs : is_glb s a) : s.nonempty := hs.dual.nonempty
lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty :=
nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst
lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty :=
@nonempty_of_not_bdd_above αᵒᵈ _ _ _ h
/-!
#### insert
-/
/-- Adding a point to a set preserves its boundedness above. -/
@[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} :
bdd_above (insert a s) ↔ bdd_above s :=
by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and]
lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) :
bdd_above (insert a s) :=
(bdd_above_insert a).2 hs
/--Adding a point to a set preserves its boundedness below.-/
@[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} :
bdd_below (insert a s) ↔ bdd_below s :=
by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and]
lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) :
bdd_below (insert a s) :=
(bdd_below_insert a).2 hs
lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) :
is_lub (insert a s) (a ⊔ b) :=
by { rw insert_eq, exact is_lub_singleton.union hs }
lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) :
is_glb (insert a s) (a ⊓ b) :=
by { rw insert_eq, exact is_glb_singleton.union hs }
lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) :
is_greatest (insert a s) (max a b) :=
by { rw insert_eq, exact is_greatest_singleton.union hs }
lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) :
is_least (insert a s) (min a b) :=
by { rw insert_eq, exact is_least_singleton.union hs }
@[simp] lemma upper_bounds_insert (a : α) (s : set α) :
upper_bounds (insert a s) = Ici a ∩ upper_bounds s :=
by rw [insert_eq, upper_bounds_union, upper_bounds_singleton]
@[simp] lemma lower_bounds_insert (a : α) (s : set α) :
lower_bounds (insert a s) = Iic a ∩ lower_bounds s :=
by rw [insert_eq, lower_bounds_union, lower_bounds_singleton]
/-- When there is a global maximum, every set is bounded above. -/
@[simp] protected lemma order_top.bdd_above [preorder γ] [order_top γ] (s : set γ) : bdd_above s :=
⟨⊤, assume a ha, order_top.le_top a⟩
/-- When there is a global minimum, every set is bounded below. -/
@[simp] protected lemma order_bot.bdd_below [preorder γ] [order_bot γ] (s : set γ) : bdd_below s :=
⟨⊥, assume a ha, order_bot.bot_le a⟩
/-!
#### Pair
-/
lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) :=
is_lub_singleton.insert _
lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) :=
is_glb_singleton.insert _
lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) :=
is_least_singleton.insert _
lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) :=
is_greatest_singleton.insert _
/-!
#### Lower/upper bounds
-/
@[simp] lemma is_lub_lower_bounds : is_lub (lower_bounds s) a ↔ is_glb s a :=
⟨λ H, ⟨λ x hx, H.2 $ subset_upper_bounds_lower_bounds s hx, H.1⟩, is_greatest.is_lub⟩
@[simp] lemma is_glb_upper_bounds : is_glb (upper_bounds s) a ↔ is_lub s a :=
@is_lub_lower_bounds αᵒᵈ _ _ _
end
/-!
### (In)equalities with the least upper bound and the greatest lower bound
-/
section preorder
variables [preorder α] {s : set α} {a b : α}
lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) :
s.nonempty → a ≤ b
| ⟨c, hc⟩ := le_trans (ha hc) (hb hc)
lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b :=
lower_bounds_le_upper_bounds ha.1 hb.1 hs
lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b :=
⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩
lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c := is_lub_lt_iff ha.dual
lemma le_of_is_lub_le_is_glb {x y} (ha : is_glb s a) (hb : is_lub s b) (hab : b ≤ a)
(hx : x ∈ s) (hy : y ∈ s) : x ≤ y :=
calc x ≤ b : hb.1 hx
... ≤ a : hab
... ≤ y : ha.1 hy
end preorder
section partial_order
variables [partial_order α] {s : set α} {a b : α}
lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b :=
le_antisymm (Ha.right Hb.left) (Hb.right Ha.left)
lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b :=
le_antisymm (Hb.right Ha.left) (Ha.right Hb.left)
lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b :=
Ha.unique Hb
lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b :=
Ha.unique Hb
lemma set.subsingleton_of_is_lub_le_is_glb (Ha : is_glb s a) (Hb : is_lub s b) (hab : b ≤ a) :
s.subsingleton :=
λ x hx y hy, le_antisymm (le_of_is_lub_le_is_glb Ha Hb hab hx hy)
(le_of_is_lub_le_is_glb Ha Hb hab hy hx)
lemma is_glb_lt_is_lub_of_ne (Ha : is_glb s a) (Hb : is_lub s b)
{x y} (Hx : x ∈ s) (Hy : y ∈ s) (Hxy : x ≠ y) :
a < b :=
lt_iff_le_not_le.2
⟨lower_bounds_le_upper_bounds Ha.1 Hb.1 ⟨x, Hx⟩,
λ hab, Hxy $ set.subsingleton_of_is_lub_le_is_glb Ha Hb hab Hx Hy⟩
end partial_order
section linear_order
variables [linear_order α] {s : set α} {a b : α}
lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c :=
by simp only [← not_le, is_lub_le_iff h, mem_upper_bounds, not_forall]
lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b := lt_is_lub_iff h.dual
lemma is_lub.exists_between (h : is_lub s a) (hb : b < a) :
∃ c ∈ s, b < c ∧ c ≤ a :=
let ⟨c, hcs, hbc⟩ := (lt_is_lub_iff h).1 hb in ⟨c, hcs, hbc, h.1 hcs⟩
lemma is_lub.exists_between' (h : is_lub s a) (h' : a ∉ s) (hb : b < a) :
∃ c ∈ s, b < c ∧ c < a :=
let ⟨c, hcs, hbc, hca⟩ := h.exists_between hb
in ⟨c, hcs, hbc, hca.lt_of_ne $ λ hac, h' $ hac ▸ hcs⟩
lemma is_glb.exists_between (h : is_glb s a) (hb : a < b) :
∃ c ∈ s, a ≤ c ∧ c < b :=
let ⟨c, hcs, hbc⟩ := (is_glb_lt_iff h).1 hb in ⟨c, hcs, h.1 hcs, hbc⟩
lemma is_glb.exists_between' (h : is_glb s a) (h' : a ∉ s) (hb : a < b) :
∃ c ∈ s, a < c ∧ c < b :=
let ⟨c, hcs, hac, hcb⟩ := h.exists_between hb
in ⟨c, hcs, hac.lt_of_ne $ λ hac, h' $ hac.symm ▸ hcs, hcb⟩
end linear_order
/-!
### Least upper bound and the greatest lower bound in linear ordered additive commutative groups
-/
section linear_ordered_add_comm_group
variables [linear_ordered_add_comm_group α] {s : set α} {a ε : α}
lemma is_glb.exists_between_self_add (h : is_glb s a) (hε : 0 < ε) :
∃ b ∈ s, a ≤ b ∧ b < a + ε :=
h.exists_between $ lt_add_of_pos_right _ hε
lemma is_glb.exists_between_self_add' (h : is_glb s a) (h₂ : a ∉ s) (hε : 0 < ε) :
∃ b ∈ s, a < b ∧ b < a + ε :=
h.exists_between' h₂ $ lt_add_of_pos_right _ hε
lemma is_lub.exists_between_sub_self (h : is_lub s a) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b ≤ a :=
h.exists_between $ sub_lt_self _ hε
lemma is_lub.exists_between_sub_self' (h : is_lub s a) (h₂ : a ∉ s) (hε : 0 < ε) :
∃ b ∈ s, a - ε < b ∧ b < a :=
h.exists_between' h₂ $ sub_lt_self _ hε
end linear_ordered_add_comm_group
/-!
### Images of upper/lower bounds under monotone functions
-/
namespace monotone_on
variables [preorder α] [preorder β] {f : α → β} {s t : set α}
(Hf : monotone_on f t) {a : α} (Hst : s ⊆ t)
include Hf Hst
lemma mem_upper_bounds_image (Has : a ∈ upper_bounds s) (Hat : a ∈ t) :
f a ∈ upper_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Hst H) Hat (Has H))
lemma mem_lower_bounds_image (Has : a ∈ lower_bounds s) (Hat : a ∈ t) :
f a ∈ lower_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf Hat (Hst H) (Has H))
lemma image_upper_bounds_subset_upper_bounds_image :
f '' (upper_bounds s ∩ t) ⊆ upper_bounds (f '' s) :=
by { rintro _ ⟨a, ha, rfl⟩, exact Hf.mem_upper_bounds_image Hst ha.1 ha.2 }
lemma image_lower_bounds_subset_lower_bounds_image :
f '' (lower_bounds s ∩ t) ⊆ lower_bounds (f '' s) :=
Hf.dual.image_upper_bounds_subset_upper_bounds_image Hst
/-- The image under a monotone function on a set `t` of a subset which has an upper bound in `t`
is bounded above. -/
lemma map_bdd_above : (upper_bounds s ∩ t).nonempty → bdd_above (f '' s) :=
λ ⟨C, hs, ht⟩, ⟨f C, Hf.mem_upper_bounds_image Hst hs ht⟩
/-- The image under a monotone function on a set `t` of a subset which has a lower bound in `t`
is bounded below. -/
lemma map_bdd_below : (lower_bounds s ∩ t).nonempty → bdd_below (f '' s) :=
λ ⟨C, hs, ht⟩, ⟨f C, Hf.mem_lower_bounds_image Hst hs ht⟩
/-- A monotone map sends a least element of a set to a least element of its image. -/
lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Hst Ha.2 (Hst Ha.1)⟩
/-- A monotone map sends a greatest element of a set to a greatest element of its image. -/
lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Hst Ha.2 (Hst Ha.1)⟩
lemma is_lub_image_le (Ha : is_lub s a) (Hat : a ∈ t) {b : β} (Hb : is_lub (f '' s) b) :
b ≤ f a :=
Hb.2 (Hf.mem_upper_bounds_image Hst Ha.1 Hat)
lemma le_is_glb_image (Ha : is_glb s a) (Hat : a ∈ t) {b : β} (Hb : is_glb (f '' s) b) :
f a ≤ b :=
Hb.2 (Hf.mem_lower_bounds_image Hst Ha.1 Hat)
end monotone_on
namespace antitone_on
variables [preorder α] [preorder β] {f : α → β} {s t : set α}
(Hf : antitone_on f t) {a : α} (Hst : s ⊆ t)
include Hf Hst
lemma mem_upper_bounds_image (Has : a ∈ lower_bounds s) (Hat : a ∈ t) :
f a ∈ upper_bounds (f '' s) :=
Hf.dual_right.mem_lower_bounds_image Hst Has Hat
lemma mem_lower_bounds_image (Has : a ∈ upper_bounds s) (Hat : a ∈ t) :
f a ∈ lower_bounds (f '' s) :=
Hf.dual_right.mem_upper_bounds_image Hst Has Hat
lemma image_lower_bounds_subset_upper_bounds_image :
f '' (lower_bounds s ∩ t) ⊆ upper_bounds (f '' s) :=
Hf.dual_right.image_lower_bounds_subset_lower_bounds_image Hst
lemma image_upper_bounds_subset_lower_bounds_image :
f '' (upper_bounds s ∩ t) ⊆ lower_bounds (f '' s) :=
Hf.dual_right.image_upper_bounds_subset_upper_bounds_image Hst
/-- The image under an antitone function of a set which is bounded above is bounded below. -/
lemma map_bdd_above : (upper_bounds s ∩ t).nonempty → bdd_below (f '' s) :=
Hf.dual_right.map_bdd_above Hst
/-- The image under an antitone function of a set which is bounded below is bounded above. -/
lemma map_bdd_below : (lower_bounds s ∩ t).nonempty → bdd_above (f '' s) :=
Hf.dual_right.map_bdd_below Hst
/-- An antitone map sends a greatest element of a set to a least element of its image. -/
lemma map_is_greatest (Ha : is_greatest s a) : is_least (f '' s) (f a) :=
Hf.dual_right.map_is_greatest Hst Ha
/-- An antitone map sends a least element of a set to a greatest element of its image. -/
lemma map_is_least (Ha : is_least s a) : is_greatest (f '' s) (f a) :=
Hf.dual_right.map_is_least Hst Ha
lemma is_lub_image_le (Ha : is_glb s a) (Hat : a ∈ t) {b : β} (Hb : is_lub (f '' s) b) : b ≤ f a :=
Hf.dual_left.is_lub_image_le Hst Ha Hat Hb
lemma le_is_glb_image (Ha : is_lub s a) (Hat : a ∈ t) {b : β} (Hb : is_glb (f '' s) b) : f a ≤ b :=
Hf.dual_left.le_is_glb_image Hst Ha Hat Hb
end antitone_on
namespace monotone
variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α}
lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) :
f a ∈ upper_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) :
f a ∈ lower_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
lemma image_upper_bounds_subset_upper_bounds_image (hf : monotone f) :
f '' upper_bounds s ⊆ upper_bounds (f '' s) :=
begin
rintro _ ⟨a, ha, rfl⟩,
exact hf.mem_upper_bounds_image ha,
end
lemma image_lower_bounds_subset_lower_bounds_image (hf : monotone f) :
f '' lower_bounds s ⊆ lower_bounds (f '' s) :=
hf.dual.image_upper_bounds_subset_upper_bounds_image
/-- The image under a monotone function of a set which is bounded above is bounded above. -/
lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩
/-- The image under a monotone function of a set which is bounded below is bounded below. -/
lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩
/-- A monotone map sends a least element of a set to a least element of its image. -/
lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩
/-- A monotone map sends a greatest element of a set to a greatest element of its image. -/
lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩
lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) :
b ≤ f a :=
Hb.2 (Hf.mem_upper_bounds_image Ha.1)
lemma le_is_glb_image (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) :
f a ≤ b :=
Hb.2 (Hf.mem_lower_bounds_image Ha.1)
end monotone
namespace antitone
variables [preorder α] [preorder β] {f : α → β} (hf : antitone f) {a : α} {s : set α}
lemma mem_upper_bounds_image (ha : a ∈ lower_bounds s) :
f a ∈ upper_bounds (f '' s) :=
hf.dual_right.mem_lower_bounds_image ha
lemma mem_lower_bounds_image (ha : a ∈ upper_bounds s) :
f a ∈ lower_bounds (f '' s) :=
hf.dual_right.mem_upper_bounds_image ha
lemma image_lower_bounds_subset_upper_bounds_image (hf : antitone f) :
f '' lower_bounds s ⊆ upper_bounds (f '' s) :=
hf.dual_right.image_lower_bounds_subset_lower_bounds_image
lemma image_upper_bounds_subset_lower_bounds_image (hf : antitone f) :
f '' upper_bounds s ⊆ lower_bounds (f '' s) :=
hf.dual_right.image_upper_bounds_subset_upper_bounds_image
/-- The image under an antitone function of a set which is bounded above is bounded below. -/
lemma map_bdd_above (hf : antitone f) : bdd_above s → bdd_below (f '' s) :=
hf.dual_right.map_bdd_above
/-- The image under an antitone function of a set which is bounded below is bounded above. -/
lemma map_bdd_below (hf : antitone f) : bdd_below s → bdd_above (f '' s) :=
hf.dual_right.map_bdd_below
/-- An antitone map sends a greatest element of a set to a least element of its image. -/
lemma map_is_greatest (ha : is_greatest s a) : is_least (f '' s) (f a) :=
hf.dual_right.map_is_greatest ha
/-- An antitone map sends a least element of a set to a greatest element of its image. -/
lemma map_is_least (ha : is_least s a) : is_greatest (f '' s) (f a) :=
hf.dual_right.map_is_least ha
lemma is_lub_image_le (ha : is_glb s a) {b : β} (hb : is_lub (f '' s) b) : b ≤ f a :=
hf.dual_left.is_lub_image_le ha hb
lemma le_is_glb_image (ha : is_lub s a) {b : β} (hb : is_glb (f '' s) b) : f a ≤ b :=
hf.dual_left.le_is_glb_image ha hb
end antitone
lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y)
{s : set α} {x : α} (hx : is_glb (f '' s) (f x)) :
is_glb s x :=
⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy,
λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩
lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y)
{s : set α} {x : α} (hx : is_lub (f '' s) (f x)) :
is_lub s x :=
@is_glb.of_image αᵒᵈ βᵒᵈ _ _ f (λ x y, hf) _ _ hx
lemma is_lub_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} :
is_lub s f ↔ ∀ a, is_lub (function.eval a '' s) (f a) :=
begin
classical,
refine ⟨λ H a, ⟨(function.monotone_eval a).mem_upper_bounds_image H.1, λ b hb, _⟩, λ H, ⟨_, _⟩⟩,
{ suffices : function.update f a b ∈ upper_bounds s,
from function.update_same a b f ▸ H.2 this a,
refine λ g hg, le_update_iff.2 ⟨hb $ mem_image_of_mem _ hg, λ i hi, H.1 hg i⟩ },
{ exact λ g hg a, (H a).1 (mem_image_of_mem _ hg) },
{ exact λ g hg a, (H a).2 ((function.monotone_eval a).mem_upper_bounds_image hg) }
end
lemma is_glb_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} :
is_glb s f ↔ ∀ a, is_glb (function.eval a '' s) (f a) :=
@is_lub_pi α (λ a, (π a)ᵒᵈ) _ s f
lemma is_lub_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) :
is_lub s p ↔ is_lub (prod.fst '' s) p.1 ∧ is_lub (prod.snd '' s) p.2 :=
begin
refine ⟨λ H, ⟨⟨monotone_fst.mem_upper_bounds_image H.1, λ a ha, _⟩,
⟨monotone_snd.mem_upper_bounds_image H.1, λ a ha, _⟩⟩, λ H, ⟨_, _⟩⟩,
{ suffices : (a, p.2) ∈ upper_bounds s, from (H.2 this).1,
exact λ q hq, ⟨ha $ mem_image_of_mem _ hq, (H.1 hq).2⟩ },
{ suffices : (p.1, a) ∈ upper_bounds s, from (H.2 this).2,
exact λ q hq, ⟨(H.1 hq).1, ha $ mem_image_of_mem _ hq⟩ },
{ exact λ q hq, ⟨H.1.1 $ mem_image_of_mem _ hq, H.2.1 $ mem_image_of_mem _ hq⟩ },
{ exact λ q hq, ⟨H.1.2 $ monotone_fst.mem_upper_bounds_image hq,
H.2.2 $ monotone_snd.mem_upper_bounds_image hq⟩ }
end
lemma is_glb_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) :
is_glb s p ↔ is_glb (prod.fst '' s) p.1 ∧ is_glb (prod.snd '' s) p.2 :=
@is_lub_prod αᵒᵈ βᵒᵈ _ _ _ _
namespace order_iso
variables [preorder α] [preorder β] (f : α ≃o β)
lemma upper_bounds_image {s : set α} :
upper_bounds (f '' s) = f '' upper_bounds s :=
subset.antisymm
(λ x hx, ⟨f.symm x, λ y hy, f.le_symm_apply.2 (hx $ mem_image_of_mem _ hy), f.apply_symm_apply x⟩)
f.monotone.image_upper_bounds_subset_upper_bounds_image
lemma lower_bounds_image {s : set α} : lower_bounds (f '' s) = f '' lower_bounds s :=
@upper_bounds_image αᵒᵈ βᵒᵈ _ _ f.dual _
@[simp] lemma is_lub_image {s : set α} {x : β} :
is_lub (f '' s) x ↔ is_lub s (f.symm x) :=
⟨λ h, is_lub.of_image (λ _ _, f.le_iff_le) ((f.apply_symm_apply x).symm ▸ h),
λ h, is_lub.of_image (λ _ _, f.symm.le_iff_le) $ (f.symm_image_image s).symm ▸ h⟩
lemma is_lub_image' {s : set α} {x : α} :
is_lub (f '' s) (f x) ↔ is_lub s x :=
by rw [is_lub_image, f.symm_apply_apply]
@[simp] lemma is_glb_image {s : set α} {x : β} :
is_glb (f '' s) x ↔ is_glb s (f.symm x) :=
f.dual.is_lub_image
lemma is_glb_image' {s : set α} {x : α} :
is_glb (f '' s) (f x) ↔ is_glb s x :=
f.dual.is_lub_image'
@[simp] lemma is_lub_preimage {s : set β} {x : α} :
is_lub (f ⁻¹' s) x ↔ is_lub s (f x) :=
by rw [← f.symm_symm, ← image_eq_preimage, is_lub_image]
lemma is_lub_preimage' {s : set β} {x : β} :
is_lub (f ⁻¹' s) (f.symm x) ↔ is_lub s x :=
by rw [is_lub_preimage, f.apply_symm_apply]
@[simp] lemma is_glb_preimage {s : set β} {x : α} :
is_glb (f ⁻¹' s) x ↔ is_glb s (f x) :=
f.dual.is_lub_preimage
lemma is_glb_preimage' {s : set β} {x : β} :
is_glb (f ⁻¹' s) (f.symm x) ↔ is_glb s x :=
f.dual.is_lub_preimage'
end order_iso
|
1c0e1d55001026ec8ad9d28f7cdff693e9c7d259 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebra/category/Group/limits.lean | 0bca8734f6059fab37f164b6dfb61069e6488569 | [
"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 | 9,751 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.category.Mon.limits
import algebra.category.Group.preadditive
import category_theory.over
import category_theory.limits.concrete_category
import category_theory.limits.shapes.concrete_category
import group_theory.subgroup.basic
/-!
# The category of (commutative) (additive) groups has all limits
Further, these limits are preserved by the forgetful functor --- that is,
the underlying types are just the limits in the category of types.
-/
open category_theory
open category_theory.limits
universe u
noncomputable theory
variables {J : Type u} [small_category J]
namespace Group
@[to_additive]
instance group_obj (F : J ⥤ Group) (j) :
group ((F ⋙ forget Group).obj j) :=
by { change group (F.obj j), apply_instance }
/--
The flat sections of a functor into `Group` form a subgroup of all sections.
-/
@[to_additive
"The flat sections of a functor into `AddGroup` form an additive subgroup of all sections."]
def sections_subgroup (F : J ⥤ Group) :
subgroup (Π j, F.obj j) :=
{ carrier := (F ⋙ forget Group).sections,
inv_mem' := λ a ah j j' f,
begin
simp only [forget_map_eq_coe, functor.comp_map, pi.inv_apply, monoid_hom.map_inv, inv_inj],
dsimp [functor.sections] at ah,
rw ah f,
end,
..(Mon.sections_submonoid (F ⋙ forget₂ Group Mon)) }
@[to_additive]
instance limit_group (F : J ⥤ Group) :
group (types.limit_cone (F ⋙ forget Group.{u})).X :=
begin
change group (sections_subgroup F),
apply_instance,
end
/--
We show that the forgetful functor `Group ⥤ Mon` creates limits.
All we need to do is notice that the limit point has a `group` instance available,
and then reuse the existing limit.
-/
@[to_additive]
instance (F : J ⥤ Group) : creates_limit F (forget₂ Group Mon.{u}) :=
creates_limit_of_reflects_iso (λ c' t,
{ lifted_cone :=
{ X := Group.of (types.limit_cone (F ⋙ forget Group)).X,
π :=
{ app := Mon.limit_π_monoid_hom (F ⋙ forget₂ Group Mon.{u}),
naturality' := (Mon.has_limits.limit_cone (F ⋙ forget₂ _ _)).π.naturality, } },
valid_lift := by apply is_limit.unique_up_to_iso (Mon.has_limits.limit_cone_is_limit _) t,
makes_limit := is_limit.of_faithful (forget₂ Group Mon.{u}) (Mon.has_limits.limit_cone_is_limit _)
(λ s, _) (λ s, rfl) })
/--
A choice of limit cone for a functor into `Group`.
(Generally, you'll just want to use `limit F`.)
-/
@[to_additive "A choice of limit cone for a functor into `Group`.
(Generally, you'll just want to use `limit F`.)"]
def limit_cone (F : J ⥤ Group) : cone F :=
lift_limit (limit.is_limit (F ⋙ (forget₂ Group Mon.{u})))
/--
The chosen cone is a limit cone.
(Generally, you'll just want to use `limit.cone F`.)
-/
@[to_additive "The chosen cone is a limit cone.
(Generally, you'll just want to use `limit.cone F`.)"]
def limit_cone_is_limit (F : J ⥤ Group) : is_limit (limit_cone F) :=
lifted_limit_is_limit _
/-- The category of groups has all limits. -/
@[to_additive]
instance has_limits : has_limits Group :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit_of_created F (forget₂ Group Mon) } } -- TODO use the above instead?
/--
The forgetful functor from groups to monoids preserves all limits.
(That is, the underlying monoid could have been computed instead as limits in the category
of monoids.)
-/
@[to_additive AddGroup.forget₂_AddMon_preserves_limits]
instance forget₂_Mon_preserves_limits : preserves_limits (forget₂ Group Mon) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F, by apply_instance } }
/--
The forgetful functor from groups to types preserves all limits. (That is, the underlying
types could have been computed instead as limits in the category of types.)
-/
@[to_additive]
instance forget_preserves_limits : preserves_limits (forget Group) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, limits.comp_preserves_limit (forget₂ Group Mon) (forget Mon) } }
end Group
namespace CommGroup
@[to_additive]
instance comm_group_obj (F : J ⥤ CommGroup) (j) :
comm_group ((F ⋙ forget CommGroup).obj j) :=
by { change comm_group (F.obj j), apply_instance }
@[to_additive]
instance limit_comm_group (F : J ⥤ CommGroup) :
comm_group (types.limit_cone (F ⋙ forget CommGroup.{u})).X :=
@subgroup.to_comm_group (Π j, F.obj j) _
(Group.sections_subgroup (F ⋙ forget₂ CommGroup Group.{u}))
/--
We show that the forgetful functor `CommGroup ⥤ Group` creates limits.
All we need to do is notice that the limit point has a `comm_group` instance available,
and then reuse the existing limit.
-/
@[to_additive]
instance (F : J ⥤ CommGroup) : creates_limit F (forget₂ CommGroup Group.{u}) :=
creates_limit_of_reflects_iso (λ c' t,
{ lifted_cone :=
{ X := CommGroup.of (types.limit_cone (F ⋙ forget CommGroup)).X,
π :=
{ app := Mon.limit_π_monoid_hom (F ⋙ forget₂ CommGroup Group.{u} ⋙ forget₂ Group Mon),
naturality' := (Mon.has_limits.limit_cone _).π.naturality, } },
valid_lift := by apply is_limit.unique_up_to_iso (Group.limit_cone_is_limit _) t,
makes_limit := is_limit.of_faithful (forget₂ _ Group.{u} ⋙ forget₂ _ Mon.{u})
(by apply Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) })
/--
A choice of limit cone for a functor into `CommGroup`.
(Generally, you'll just want to use `limit F`.)
-/
@[to_additive "A choice of limit cone for a functor into `CommGroup`.
(Generally, you'll just want to use `limit F`.)"]
def limit_cone (F : J ⥤ CommGroup) : cone F :=
lift_limit (limit.is_limit (F ⋙ (forget₂ CommGroup Group.{u})))
/--
The chosen cone is a limit cone.
(Generally, you'll just want to use `limit.cone F`.)
-/
@[to_additive "The chosen cone is a limit cone.
(Generally, you'll just wantto use `limit.cone F`.)"]
def limit_cone_is_limit (F : J ⥤ CommGroup) : is_limit (limit_cone F) :=
lifted_limit_is_limit _
/-- The category of commutative groups has all limits. -/
@[to_additive]
instance has_limits : has_limits CommGroup :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit_of_created F (forget₂ CommGroup Group) } }
/--
The forgetful functor from commutative groups to groups preserves all limits.
(That is, the underlying group could have been computed instead as limits in the category
of groups.)
-/
@[to_additive AddCommGroup.forget₂_AddGroup_preserves_limits]
instance forget₂_Group_preserves_limits : preserves_limits (forget₂ CommGroup Group) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F, by apply_instance } }
/--
An auxiliary declaration to speed up typechecking.
-/
@[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits_aux
"An auxiliary declaration to speed up typechecking."]
def forget₂_CommMon_preserves_limits_aux (F : J ⥤ CommGroup) :
is_limit ((forget₂ CommGroup CommMon).map_cone (limit_cone F)) :=
CommMon.limit_cone_is_limit (F ⋙ forget₂ CommGroup CommMon)
/--
The forgetful functor from commutative groups to commutative monoids preserves all limits.
(That is, the underlying commutative monoids could have been computed instead as limits
in the category of commutative monoids.)
-/
@[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits]
instance forget₂_CommMon_preserves_limits : preserves_limits (forget₂ CommGroup CommMon) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (forget₂_CommMon_preserves_limits_aux F) } }
/--
The forgetful functor from commutative groups to types preserves all limits. (That is, the
underlying types could have been computed instead as limits in the category of types.)
-/
@[to_additive AddCommGroup.forget_preserves_limits]
instance forget_preserves_limits : preserves_limits (forget CommGroup) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommGroup Group) (forget Group) } }
end CommGroup
namespace AddCommGroup
/--
The categorical kernel of a morphism in `AddCommGroup`
agrees with the usual group-theoretical kernel.
-/
def kernel_iso_ker {G H : AddCommGroup} (f : G ⟶ H) :
kernel f ≅ AddCommGroup.of f.ker :=
{ hom :=
{ to_fun := λ g, ⟨kernel.ι f g,
begin
-- TODO where is this `has_coe_t_aux.coe` coming from? can we prevent it appearing?
change (kernel.ι f) g ∈ f.ker,
simp [add_monoid_hom.mem_ker],
end⟩,
map_zero' := by { ext, simp, },
map_add' := λ g g', by { ext, simp, }, },
inv := kernel.lift f (add_subgroup.subtype f.ker) (by tidy),
hom_inv_id' := by { apply equalizer.hom_ext _, ext, simp, },
inv_hom_id' :=
begin
apply AddCommGroup.ext,
simp only [add_monoid_hom.coe_mk, coe_id, coe_comp],
rintro ⟨x, mem⟩,
simp,
end, }.
@[simp]
lemma kernel_iso_ker_hom_comp_subtype {G H : AddCommGroup} (f : G ⟶ H) :
(kernel_iso_ker f).hom ≫ add_subgroup.subtype f.ker = kernel.ι f :=
by ext; refl
@[simp]
lemma kernel_iso_ker_inv_comp_ι {G H : AddCommGroup} (f : G ⟶ H) :
(kernel_iso_ker f).inv ≫ kernel.ι f = add_subgroup.subtype f.ker :=
begin
ext,
simp [kernel_iso_ker],
end
/--
The categorical kernel inclusion for `f : G ⟶ H`, as an object over `G`,
agrees with the `subtype` map.
-/
@[simps]
def kernel_iso_ker_over {G H : AddCommGroup.{u}} (f : G ⟶ H) :
over.mk (kernel.ι f) ≅ @over.mk _ _ G (AddCommGroup.of f.ker) (add_subgroup.subtype f.ker) :=
over.iso_mk (kernel_iso_ker f) (by simp)
end AddCommGroup
|
f92f5c06764e768afb0b6ee53ef64279572e251d | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/tactic/lift.lean | f9f2ce52faa4c257d8ab5b9e1ca9b6a97b1f7dd5 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 7,904 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import tactic.rcases
/-!
# lift tactic
This file defines the lift tactic, allowing the user to lift elements from one type to another
under a specified condition.
## Tags
lift, tactic
-/
universe variables u v w
/-- A class specifying that you can lift elements from `α` to `β` assuming `cond` is true.
Used by the tactic `lift`. -/
class can_lift (α : Type u) (β : Type v) : Type (max u v) :=
(coe : β → α)
(cond : α → Prop)
(prf : ∀(x : α), cond x → ∃(y : β), coe y = x)
open tactic
@[user_attribute]
meta def can_lift_attr : user_attribute (list name) :=
{ name := "_can_lift",
descr := "internal attribute used by the lift tactic",
cache_cfg := { mk_cache := λ _,
do { ls ← attribute.get_instances `instance,
ls.mfilter $ λ l,
do { (_,t) ← mk_const l >>= infer_type >>= mk_local_pis,
return $ t.is_app_of `can_lift } },
dependencies := [`instance] } }
instance : can_lift ℤ ℕ :=
⟨coe, λ n, 0 ≤ n, λ n hn, ⟨n.nat_abs, int.nat_abs_of_nonneg hn⟩⟩
/-- Enable automatic handling of pi types in `can_lift`. -/
instance pi.can_lift (ι : Type u) (α : Π i : ι, Type v) (β : Π i : ι, Type w)
[Π i : ι, can_lift (α i) (β i)] :
can_lift (Π i : ι, α i) (Π i : ι, β i) :=
{ coe := λ f i, can_lift.coe (α i) (f i),
cond := λ f, ∀ i, can_lift.cond (β i) (f i),
prf := λ f hf, ⟨λ i, classical.some (can_lift.prf (f i) (hf i)), funext $ λ i,
classical.some_spec (can_lift.prf (f i) (hf i))⟩ }
namespace tactic
/- Construct the proof of `cond x` in the lift tactic.
`e` is the expression being lifted and `h` is the specified proof of `can_lift.cond e`.
`old_tp` and `new_tp` are the arguments to `can_lift` and `inst` is the `can_lift`-instance.
`s` and `to_unfold` contain the information of the simp set used to simplify.
If the proof was specified, we check whether it has the correct type.
If it doesn't have the correct type, we display an error message
(but first call dsimp on the expression in the message).
If the proof was not specified, we create assert it as a local constant.
(The name of this local constant doesn't matter, since `lift` will remove it from the context) -/
meta def get_lift_prf (h : option pexpr) (old_tp new_tp inst e : expr)
(s : simp_lemmas) (to_unfold : list name) : tactic expr :=
if h_some : h.is_some then
(do prf ← i_to_expr (option.get h_some), prf_ty ← infer_type prf,
expected_prf_ty ← mk_app `can_lift.cond [old_tp, new_tp, inst, e],
unify prf_ty expected_prf_ty <|>
(do expected_prf_ty2 ← s.dsimplify to_unfold expected_prf_ty,
pformat!"lift tactic failed. The type of\n {prf}\nis\n {prf_ty}\nbut it is expected to be\n {expected_prf_ty2}" >>= fail),
return prf)
else (do prf_nm ← get_unused_name,
prf ← mk_app `can_lift.cond [old_tp, new_tp, inst, e] >>= assert prf_nm,
dsimp_target s to_unfold {}, swap, return prf)
/-- Lift the expression `p` to the type `t`, with proof obligation given by `h`.
The list `n` is used for the two newly generated names, and to specify whether `h` should
remain in the local context. See the doc string of `tactic.interactive.lift` for more information.
-/
meta def lift (p : pexpr) (t : pexpr) (h : option pexpr) (n : list name) : tactic unit :=
do
propositional_goal <|>
fail "lift tactic failed. Tactic is only applicable when the target is a proposition.",
e ← i_to_expr p,
old_tp ← infer_type e,
new_tp ← i_to_expr t,
inst_type ← mk_app ``can_lift [old_tp, new_tp],
inst ← mk_instance inst_type <|>
pformat!"Failed to find a lift from {old_tp} to {new_tp}. Provide an instance of\n {inst_type}"
>>= fail,
/- make the simp set to get rid of `can_lift` projections -/
can_lift_instances ← can_lift_attr.get_cache >>= λ l, l.mmap resolve_name,
(s, to_unfold) ← mk_simp_set tt [] $ can_lift_instances.map simp_arg_type.expr,
prf_cond ← get_lift_prf h old_tp new_tp inst e s to_unfold,
let prf_nm := if prf_cond.is_local_constant then some prf_cond.local_pp_name else none,
/- We use mk_mapp to apply `can_lift.prf` to all but one argument, and then just use expr.app
for the last argument. For some reason we get an error when applying mk_mapp it to all
arguments. -/
prf_ex0 ← mk_mapp `can_lift.prf [old_tp, new_tp, inst, e],
let prf_ex := prf_ex0 prf_cond,
/- Find the name of the new variable -/
new_nm ← if n ≠ [] then return n.head
else if e.is_local_constant then return e.local_pp_name
else get_unused_name,
/- Find the name of the proof of the equation -/
eq_nm ← if hn : 1 < n.length then return (n.nth_le 1 hn)
else if e.is_local_constant then return `rfl
else get_unused_name `h,
/- We add the proof of the existential statement to the context and then apply
`dsimp` to it, unfolding all `can_lift` instances. -/
temp_nm ← get_unused_name,
temp_e ← note temp_nm none prf_ex,
dsimp_hyp temp_e s to_unfold {},
/- We case on the existential. We use `rcases` because `eq_nm` could be `rfl`. -/
rcases none (pexpr.of_expr temp_e) [[rcases_patt.one new_nm, rcases_patt.one eq_nm]],
/- If the lifted variable is not a local constant, try to rewrite it away using the new equality-/
when (¬ e.is_local_constant) (get_local eq_nm >>=
λ e, interactive.rw ⟨[⟨⟨0, 0⟩, tt, (pexpr.of_expr e)⟩], none⟩ interactive.loc.wildcard),
/- If the proof `prf_cond` is a local constant, remove it from the context,
unless `n` specifies to keep it. -/
if h_prf_nm : prf_nm.is_some ∧ n.nth 2 ≠ prf_nm then
get_local (option.get h_prf_nm.1) >>= clear else skip
open lean.parser interactive interactive.types
local postfix `?`:9001 := optional
meta def using_texpr := (tk "using" *> texpr)?
reserve notation `to`
meta def to_texpr := (tk "to" *> texpr)
namespace interactive
/-- Lift an expression to another type.
* Usage: `'lift' expr 'to' expr ('using' expr)? ('with' id (id id?)?)?`.
* If `n : ℤ` and `hn : n ≥ 0` then the tactic `lift n to ℕ using hn` creates a new
constant of type ℕ, also named `n` and replaces all occurrences of the old variable `(n : ℤ)`
with `↑n` (where `n` in the new variable). It will remove `n` and `hn` from the context.
* The argument `using hn` is optional, the tactic `lift n to ℕ` does the same, but also creates a
new subgoal that `n ≥ 0` (where `n` is the old variable).
* You can also use `lift n to ℕ using e` where `e` is any expression of type `n ≥ 0`.
* Use `lift n to ℕ with k` to specify the name of the new variable.
* Use `lift n to ℕ with k hk` to also specify the name of the equality `↑k = n`. In this case, `n`
will remain in the context. You can use `rfl` for the name of `hk` to substitute it away.
* You can also use `lift e to ℕ with k hk` where `e` is any expression of type `ℤ`.
In this case, the `hk` will always stay in the context, but it will be used to rewrite `e` in
all hypotheses and the target.
* The tactic `lift n to ℕ using h` will remove `h` from the context. If you want to keep it,
specify it again as the third argument to `with`, like this: `lift n to ℕ using h with n rfl h`.
* More generally, this can lift an expression from `α` to `β` assuming that there is an instance
of `can_lift α β`. In this case the proof obligation is specified by `can_lift.cond`.
* Given an instance `can_lift β γ`, it can also lift `α → β` to `α → γ`, and similarly for
general pi types, see `pi.can_lift`.
-/
meta def lift (p : parse texpr) (t : parse to_texpr) (h : parse using_texpr)
(n : parse with_ident_list) : tactic unit :=
tactic.lift p t h n
end interactive
end tactic
|
58b113442cceeada956ca5cf9ff49c0f4c526ab5 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/Match/CaseValues.lean | 2d0d6ecb02458e5d3b45dd35b2305460742ec32c | [
"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,770 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Subst
import Lean.Meta.Tactic.Clear
import Lean.Meta.Match.Value
namespace Lean.Meta
structure CaseValueSubgoal where
mvarId : MVarId
newH : FVarId
subst : FVarSubst := {}
deriving Inhabited
/--
Split goal `... |- C x` into two subgoals
`..., (h : x = value) |- C value`
`..., (h : x != value) |- C x`
where `fvarId` is `x`s id.
The type of `x` must have decidable equality.
Remark: `subst` field of the second subgoal is equal to the input `subst`. -/
private def caseValueAux (mvarId : MVarId) (fvarId : FVarId) (value : Expr) (hName : Name := `h) (subst : FVarSubst := {})
: MetaM (CaseValueSubgoal × CaseValueSubgoal) :=
mvarId.withContext do
let tag ← mvarId.getTag
mvarId.checkNotAssigned `caseValue
let target ← mvarId.getType
let xEqValue ← mkEq (mkFVar fvarId) (foldPatValue value)
let xNeqValue := mkApp (mkConst `Not) xEqValue
let thenTarget := Lean.mkForall hName BinderInfo.default xEqValue target
let elseTarget := Lean.mkForall hName BinderInfo.default xNeqValue target
let thenMVar ← mkFreshExprSyntheticOpaqueMVar thenTarget tag
let elseMVar ← mkFreshExprSyntheticOpaqueMVar elseTarget tag
let val ← mkAppOptM `dite #[none, xEqValue, none, thenMVar, elseMVar]
mvarId.assign val
let (elseH, elseMVarId) ← elseMVar.mvarId!.intro1P
let elseSubgoal := { mvarId := elseMVarId, newH := elseH, subst := subst : CaseValueSubgoal }
let (thenH, thenMVarId) ← thenMVar.mvarId!.intro1P
let symm := false
let clearH := false
let (thenSubst, thenMVarId) ← substCore thenMVarId thenH symm subst clearH
thenMVarId.withContext do
trace[Meta] "subst domain: {thenSubst.domain.map (·.name)}"
let thenH := (thenSubst.get thenH).fvarId!
trace[Meta] "searching for decl"
let _ ← thenH.getDecl
trace[Meta] "found decl"
let thenSubgoal := { mvarId := thenMVarId, newH := (thenSubst.get thenH).fvarId!, subst := thenSubst : CaseValueSubgoal }
pure (thenSubgoal, elseSubgoal)
def caseValue (mvarId : MVarId) (fvarId : FVarId) (value : Expr) : MetaM (CaseValueSubgoal × CaseValueSubgoal) := do
let s ← caseValueAux mvarId fvarId value
appendTagSuffix s.1.mvarId `thenBranch
appendTagSuffix s.2.mvarId `elseBranch
pure s
structure CaseValuesSubgoal where
mvarId : MVarId
newHs : Array FVarId := #[]
subst : FVarSubst := {}
deriving Inhabited
/--
Split goal `... |- C x` into values.size + 1 subgoals
1) `..., (h_1 : x = value[0]) |- C value[0]`
...
n) `..., (h_n : x = value[n - 1]) |- C value[n - 1]`
n+1) `..., (h_1 : x != value[0]) ... (h_n : x != value[n-1]) |- C x`
where `n = values.size`
where `fvarId` is `x`s id.
The type of `x` must have decidable equality.
Remark: the last subgoal is for the "else" catchall case, and its `subst` is `{}`.
Remark: the fiels `newHs` has size 1 forall but the last subgoal.
If `substNewEqs = true`, then the new `h_i` equality hypotheses are substituted in the first `n` cases.
-/
def caseValues (mvarId : MVarId) (fvarId : FVarId) (values : Array Expr) (hNamePrefix := `h) (substNewEqs := false) : MetaM (Array CaseValuesSubgoal) :=
let rec loop : Nat → MVarId → List Expr → Array FVarId → Array CaseValuesSubgoal → MetaM (Array CaseValuesSubgoal)
| _, mvarId, [], _, _ => throwTacticEx `caseValues mvarId "list of values must not be empty"
| i, mvarId, v::vs, hs, subgoals => do
let (thenSubgoal, elseSubgoal) ← caseValueAux mvarId fvarId v (hNamePrefix.appendIndexAfter i) {}
appendTagSuffix thenSubgoal.mvarId ((`case).appendIndexAfter i)
let thenMVarId ← hs.foldlM
(fun thenMVarId h => match thenSubgoal.subst.get h with
| Expr.fvar fvarId => thenMVarId.tryClear fvarId
| _ => pure thenMVarId)
thenSubgoal.mvarId
let subgoals ← if substNewEqs then
let (subst, mvarId) ← substCore thenMVarId thenSubgoal.newH false thenSubgoal.subst true
pure <| subgoals.push { mvarId := mvarId, newHs := #[], subst := subst }
else
pure <| subgoals.push { mvarId := thenMVarId, newHs := #[thenSubgoal.newH], subst := thenSubgoal.subst }
match vs with
| [] => do
appendTagSuffix elseSubgoal.mvarId ((`case).appendIndexAfter (i+1))
pure $ subgoals.push { mvarId := elseSubgoal.mvarId, newHs := hs.push elseSubgoal.newH, subst := {} }
| vs => loop (i+1) elseSubgoal.mvarId vs (hs.push elseSubgoal.newH) subgoals
loop 1 mvarId values.toList #[] #[]
end Lean.Meta
|
83f58017b0e292cfc00991cd8b68fb9287ce9abb | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/simp3.lean | 7b6c6551935800d013f75e8946e8aaa249df62d7 | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,412 | lean | definition double x := x + x
(*
function show(x) print(x) end
local t1 = parse_lean("0 ≠ 1")
show(simplify(t1))
local t2 = parse_lean("3 ≥ 2")
show(simplify(t2))
local t3 = parse_lean("double (double 2) + 1")
show(simplify(t3))
local t4 = parse_lean("0 = 1")
show(simplify(t4))
*)
(*
local opt = options({"simplifier", "unfold"}, true, {"simplifier", "eval"}, false)
local t1 = parse_lean("double (double 2) + 1 ≥ 3")
show(simplify(t1, 'default', opt))
*)
set_opaque Nat::ge false
rewrite_set basic
add_rewrite Nat::add_assoc Nat::distributer Nat::distributel : basic
(*
local opt = options({"simplifier", "unfold"}, true, {"simplifier", "eval"}, false)
local t1 = parse_lean("2 * double (double 2) + 1 ≥ 3")
show(simplify(t1, "basic", opt))
*)
variables a b c d : Nat
add_rewrite if_true if_false if_a_a Nat::add_zeror Nat::add_zerol eq_id : basic
(*
local t1 = parse_lean("(a + b) * (c + d)")
local r, pr = simplify(t1, "basic")
print(r)
print(pr)
*)
theorem congr2_congr1 {A B C : TypeU} {f g : A → B} (h : B → C) (Hfg : f = g) (a : A) :
congr2 h (congr1 Hfg a) = congr2 (λ x, h (x a)) Hfg
:= proof_irrel (congr2 h (congr1 Hfg a)) (congr2 (λ x, h (x a)) Hfg)
theorem congr2_congr2 {A B C : TypeU} {a b : A} (f : A → B) (h : B → C) (Hab : a = b) :
congr2 h (congr2 f Hab) = congr2 (λ x, h (f x)) Hab
:= proof_irrel (congr2 h (congr2 f Hab)) (congr2 (λ x, h (f x)) Hab)
theorem congr1_congr2 {A B C : TypeU} {a b : A} (f : A → B → C) (Hab : a = b) (c : B):
congr1 (congr2 f Hab) c = congr2 (λ x, f x c) Hab
:= proof_irrel (congr1 (congr2 f Hab) c) (congr2 (λ x, f x c) Hab)
rewrite_set proofsimp
add_rewrite congr2_congr1 congr2_congr2 congr1_congr2 : proofsimp
(*
local t2 = parse_lean("(if a > 0 then b else b + 0) + 10 = (if a > 0 then b else b) + 10")
local r, pr = simplify(t2, "basic")
print(r)
print(pr)
show(simplify(pr, 'proofsimp'))
*)
(*
local t1 = parse_lean("(a + b) * (a + b)")
local t2 = simplify(t1, "basic")
print(t2)
*)
-- add_rewrite imp_truer imp_truel imp_falsel imp_falser not_true not_false
-- print rewrite_set
(*
local t1 = parse_lean("true → false")
print(simplify(t1, "basic"))
*)
(*
local t1 = parse_lean("true → true")
print(simplify(t1, "basic"))
*)
(*
local t1 = parse_lean("false → false")
print(simplify(t1, "basic"))
*)
(*
local t1 = parse_lean("true ↔ false")
print(simplify(t1, "basic"))
*) |
75919f293e9af77e47efb2d2d32bdddad5a104f6 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/discrete_valuation_ring.lean | 90f2457dfa66ff2b5cbf5ff2df53341e5ee581b9 | [
"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 | 17,080 | lean | /-
Copyright (c) 2020 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard
-/
import ring_theory.principal_ideal_domain
import order.conditionally_complete_lattice
import ring_theory.ideal.local_ring
import ring_theory.multiplicity
import ring_theory.valuation.basic
import linear_algebra.adic_completion
/-!
# Discrete valuation rings
This file defines discrete valuation rings (DVRs) and develops a basic interface
for them.
## Important definitions
There are various definitions of a DVR in the literature; we define a DVR to be a local PID
which is not a field (the first definition in Wikipedia) and prove that this is equivalent
to being a PID with a unique non-zero prime ideal (the definition in Serre's
book "Local Fields").
Let R be an integral domain, assumed to be a principal ideal ring and a local ring.
* `discrete_valuation_ring R` : a predicate expressing that R is a DVR
### Definitions
* `add_val R : add_valuation R enat` : the additive valuation on a DVR.
## Implementation notes
It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible.
We do not hence define `uniformizer` at all, because we can use `irreducible` instead.
## Tags
discrete valuation ring
-/
open_locale classical
universe u
open ideal local_ring
/-- An integral domain is a *discrete valuation ring* (DVR) if it's a local PID which
is not a field. -/
class discrete_valuation_ring (R : Type u) [comm_ring R] [is_domain R]
extends is_principal_ideal_ring R, local_ring R : Prop :=
(not_a_field' : maximal_ideal R ≠ ⊥)
namespace discrete_valuation_ring
variables (R : Type u) [comm_ring R] [is_domain R] [discrete_valuation_ring R]
lemma not_a_field : maximal_ideal R ≠ ⊥ := not_a_field'
variable {R}
open principal_ideal_ring
/-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the
maximal ideal of R -/
theorem irreducible_iff_uniformizer (ϖ : R) :
irreducible ϖ ↔ maximal_ideal R = ideal.span {ϖ} :=
⟨λ hϖ, (eq_maximal_ideal (is_maximal_of_irreducible hϖ)).symm,
begin
intro h,
have h2 : ¬(is_unit ϖ) := show ϖ ∈ maximal_ideal R,
from h.symm ▸ submodule.mem_span_singleton_self ϖ,
refine ⟨h2, _⟩,
intros a b hab,
by_contra' h,
obtain ⟨ha : a ∈ maximal_ideal R, hb : b ∈ maximal_ideal R⟩ := h,
rw [h, mem_span_singleton'] at ha hb,
rcases ha with ⟨a, rfl⟩,
rcases hb with ⟨b, rfl⟩,
rw (show a * ϖ * (b * ϖ) = ϖ * (ϖ * (a * b)), by ring) at hab,
have h3 := eq_zero_of_mul_eq_self_right _ hab.symm,
{ apply not_a_field R,
simp [h, h3] },
{ exact λ hh, h2 (is_unit_of_dvd_one ϖ ⟨_, hh.symm⟩) }
end⟩
lemma _root_.irreducible.maximal_ideal_eq {ϖ : R} (h : irreducible ϖ) :
maximal_ideal R = ideal.span {ϖ} :=
(irreducible_iff_uniformizer _).mp h
variable (R)
/-- Uniformisers exist in a DVR -/
theorem exists_irreducible : ∃ ϖ : R, irreducible ϖ :=
by {simp_rw [irreducible_iff_uniformizer],
exact (is_principal_ideal_ring.principal $ maximal_ideal R).principal}
/-- Uniformisers exist in a DVR -/
theorem exists_prime : ∃ ϖ : R, prime ϖ :=
(exists_irreducible R).imp (λ _, principal_ideal_ring.irreducible_iff_prime.1)
/-- an integral domain is a DVR iff it's a PID with a unique non-zero prime ideal -/
theorem iff_pid_with_one_nonzero_prime (R : Type u) [comm_ring R] [is_domain R] :
discrete_valuation_ring R ↔ is_principal_ideal_ring R ∧ ∃! P : ideal R, P ≠ ⊥ ∧ is_prime P :=
begin
split,
{ intro RDVR,
rcases id RDVR with ⟨RPID, Rlocal, Rnotafield⟩,
split, assumption,
resetI,
use local_ring.maximal_ideal R,
split, split,
{ assumption },
{ apply_instance } ,
{ rintro Q ⟨hQ1, hQ2⟩,
obtain ⟨q, rfl⟩ := (is_principal_ideal_ring.principal Q).1,
have hq : q ≠ 0,
{ rintro rfl,
apply hQ1,
simp },
erw span_singleton_prime hq at hQ2,
replace hQ2 := hQ2.irreducible,
rw irreducible_iff_uniformizer at hQ2,
exact hQ2.symm } },
{ rintro ⟨RPID, Punique⟩,
haveI : local_ring R := local_ring.of_unique_nonzero_prime Punique,
refine {not_a_field' := _},
rcases Punique with ⟨P, ⟨hP1, hP2⟩, hP3⟩,
have hPM : P ≤ maximal_ideal R := le_maximal_ideal (hP2.1),
intro h, rw [h, le_bot_iff] at hPM, exact hP1 hPM }
end
lemma associated_of_irreducible {a b : R} (ha : irreducible a) (hb : irreducible b) :
associated a b :=
begin
rw irreducible_iff_uniformizer at ha hb,
rw [←span_singleton_eq_span_singleton, ←ha, hb],
end
end discrete_valuation_ring
namespace discrete_valuation_ring
variable (R : Type*)
/-- Alternative characterisation of discrete valuation rings. -/
def has_unit_mul_pow_irreducible_factorization [comm_ring R] : Prop :=
∃ p : R, irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ (n : ℕ), associated (p ^ n) x
namespace has_unit_mul_pow_irreducible_factorization
variables {R} [comm_ring R] (hR : has_unit_mul_pow_irreducible_factorization R)
include hR
lemma unique_irreducible ⦃p q : R⦄ (hp : irreducible p) (hq : irreducible q) :
associated p q :=
begin
rcases hR with ⟨ϖ, hϖ, hR⟩,
suffices : ∀ {p : R} (hp : irreducible p), associated p ϖ,
{ apply associated.trans (this hp) (this hq).symm, },
clear hp hq p q,
intros p hp,
obtain ⟨n, hn⟩ := hR hp.ne_zero,
have : irreducible (ϖ ^ n) := hn.symm.irreducible hp,
rcases lt_trichotomy n 1 with (H|rfl|H),
{ obtain rfl : n = 0, { clear hn this, revert H n, exact dec_trivial },
simpa only [not_irreducible_one, pow_zero] using this, },
{ simpa only [pow_one] using hn.symm, },
{ obtain ⟨n, rfl⟩ : ∃ k, n = 1 + k + 1 := nat.exists_eq_add_of_lt H,
rw pow_succ at this,
rcases this.is_unit_or_is_unit rfl with H0|H0,
{ exact (hϖ.not_unit H0).elim, },
{ rw [add_comm, pow_succ] at H0,
exact (hϖ.not_unit (is_unit_of_mul_is_unit_left H0)).elim } }
end
variables [is_domain R]
/-- An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p` is a unique factorization domain.
See `discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization`. -/
theorem to_unique_factorization_monoid : unique_factorization_monoid R :=
let p := classical.some hR in
let spec := classical.some_spec hR in
unique_factorization_monoid.of_exists_prime_factors $ λ x hx,
begin
use multiset.repeat p (classical.some (spec.2 hx)),
split,
{ intros q hq,
have hpq := multiset.eq_of_mem_repeat hq,
rw hpq,
refine ⟨spec.1.ne_zero, spec.1.not_unit, _⟩,
intros a b h,
by_cases ha : a = 0,
{ rw ha, simp only [true_or, dvd_zero], },
obtain ⟨m, u, rfl⟩ := spec.2 ha,
rw [mul_assoc, mul_left_comm, is_unit.dvd_mul_left _ _ _ (units.is_unit _)] at h,
rw is_unit.dvd_mul_right (units.is_unit _),
by_cases hm : m = 0,
{ simp only [hm, one_mul, pow_zero] at h ⊢, right, exact h },
left,
obtain ⟨m, rfl⟩ := nat.exists_eq_succ_of_ne_zero hm,
rw pow_succ,
apply dvd_mul_of_dvd_left dvd_rfl _ },
{ rw [multiset.prod_repeat], exact (classical.some_spec (spec.2 hx)), }
end
omit hR
lemma of_ufd_of_unique_irreducible [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
has_unit_mul_pow_irreducible_factorization R :=
begin
obtain ⟨p, hp⟩ := h₁,
refine ⟨p, hp, _⟩,
intros x hx,
cases wf_dvd_monoid.exists_factors x hx with fx hfx,
refine ⟨fx.card, _⟩,
have H := hfx.2,
rw ← associates.mk_eq_mk_iff_associated at H ⊢,
rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat],
congr' 1,
symmetry,
rw multiset.eq_repeat,
simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true,
multiset.mem_map, exists_imp_distrib],
rintros _ q hq rfl,
rw associates.mk_eq_mk_iff_associated,
apply h₂ (hfx.1 _ hq) hp,
end
end has_unit_mul_pow_irreducible_factorization
lemma aux_pid_of_ufd_of_unique_irreducible
(R : Type u) [comm_ring R] [is_domain R] [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
is_principal_ideal_ring R :=
begin
constructor,
intro I,
by_cases I0 : I = ⊥, { rw I0, use 0, simp only [set.singleton_zero, submodule.span_zero], },
obtain ⟨x, hxI, hx0⟩ : ∃ x ∈ I, x ≠ (0:R) := I.ne_bot_iff.mp I0,
obtain ⟨p, hp, H⟩ :=
has_unit_mul_pow_irreducible_factorization.of_ufd_of_unique_irreducible h₁ h₂,
have ex : ∃ n : ℕ, p ^ n ∈ I,
{ obtain ⟨n, u, rfl⟩ := H hx0,
refine ⟨n, _⟩,
simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hxI, },
constructor,
use p ^ (nat.find ex),
show I = ideal.span _,
apply le_antisymm,
{ intros r hr,
by_cases hr0 : r = 0,
{ simp only [hr0, submodule.zero_mem], },
obtain ⟨n, u, rfl⟩ := H hr0,
simp only [mem_span_singleton, units.is_unit, is_unit.dvd_mul_right],
apply pow_dvd_pow,
apply nat.find_min',
simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hr, },
{ erw submodule.span_singleton_le_iff_mem,
exact nat.find_spec ex, },
end
/--
A unique factorization domain with at least one irreducible element
in which all irreducible elements are associated
is a discrete valuation ring.
-/
lemma of_ufd_of_unique_irreducible
{R : Type u} [comm_ring R] [is_domain R] [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
discrete_valuation_ring R :=
begin
rw iff_pid_with_one_nonzero_prime,
haveI PID : is_principal_ideal_ring R := aux_pid_of_ufd_of_unique_irreducible R h₁ h₂,
obtain ⟨p, hp⟩ := h₁,
refine ⟨PID, ⟨ideal.span {p}, ⟨_, _⟩, _⟩⟩,
{ rw submodule.ne_bot_iff,
refine ⟨p, ideal.mem_span_singleton.mpr (dvd_refl p), hp.ne_zero⟩, },
{ rwa [ideal.span_singleton_prime hp.ne_zero,
← unique_factorization_monoid.irreducible_iff_prime], },
{ intro I,
rw ← submodule.is_principal.span_singleton_generator I,
rintro ⟨I0, hI⟩,
apply span_singleton_eq_span_singleton.mpr,
apply h₂ _ hp,
erw [ne.def, span_singleton_eq_bot] at I0,
rwa [unique_factorization_monoid.irreducible_iff_prime, ← ideal.span_singleton_prime I0],
apply_instance, },
end
/--
An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p`
is a discrete valuation ring.
-/
lemma of_has_unit_mul_pow_irreducible_factorization {R : Type u} [comm_ring R] [is_domain R]
(hR : has_unit_mul_pow_irreducible_factorization R) :
discrete_valuation_ring R :=
begin
letI : unique_factorization_monoid R := hR.to_unique_factorization_monoid,
apply of_ufd_of_unique_irreducible _ hR.unique_irreducible,
unfreezingI { obtain ⟨p, hp, H⟩ := hR, exact ⟨p, hp⟩, },
end
section
variables [comm_ring R] [is_domain R] [discrete_valuation_ring R]
variable {R}
lemma associated_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) :
∃ (n : ℕ), associated x (ϖ ^ n) :=
begin
have : wf_dvd_monoid R := is_noetherian_ring.wf_dvd_monoid,
cases wf_dvd_monoid.exists_factors x hx with fx hfx,
unfreezingI { use fx.card },
have H := hfx.2,
rw ← associates.mk_eq_mk_iff_associated at H ⊢,
rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat],
congr' 1,
rw multiset.eq_repeat,
simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true,
multiset.mem_map, exists_imp_distrib],
rintros _ _ _ rfl,
rw associates.mk_eq_mk_iff_associated,
refine associated_of_irreducible _ _ hirr,
apply hfx.1,
assumption
end
lemma eq_unit_mul_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) :
∃ (n : ℕ) (u : Rˣ), x = u * ϖ ^ n :=
begin
obtain ⟨n, hn⟩ := associated_pow_irreducible hx hirr,
obtain ⟨u, rfl⟩ := hn.symm,
use [n, u],
apply mul_comm,
end
open submodule.is_principal
lemma ideal_eq_span_pow_irreducible {s : ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : irreducible ϖ) :
∃ n : ℕ, s = ideal.span {ϖ ^ n} :=
begin
have gen_ne_zero : generator s ≠ 0,
{ rw [ne.def, ← eq_bot_iff_generator_eq_zero], assumption },
rcases associated_pow_irreducible gen_ne_zero hirr with ⟨n, u, hnu⟩,
use n,
have : span _ = _ := span_singleton_generator s,
rw [← this, ← hnu, span_singleton_eq_span_singleton],
use u
end
lemma unit_mul_pow_congr_pow {p q : R} (hp : irreducible p) (hq : irreducible q)
(u v : Rˣ) (m n : ℕ) (h : ↑u * p ^ m = v * q ^ n) :
m = n :=
begin
have key : associated (multiset.repeat p m).prod (multiset.repeat q n).prod,
{ rw [multiset.prod_repeat, multiset.prod_repeat, associated],
refine ⟨u * v⁻¹, _⟩,
simp only [units.coe_mul],
rw [mul_left_comm, ← mul_assoc, h, mul_right_comm, units.mul_inv, one_mul], },
have := multiset.card_eq_card_of_rel (unique_factorization_monoid.factors_unique _ _ key),
{ simpa only [multiset.card_repeat] },
all_goals
{ intros x hx,
unfreezingI { obtain rfl := multiset.eq_of_mem_repeat hx, assumption } },
end
lemma unit_mul_pow_congr_unit {ϖ : R} (hirr : irreducible ϖ) (u v : Rˣ) (m n : ℕ)
(h : ↑u * ϖ ^ m = v * ϖ ^ n) :
u = v :=
begin
obtain rfl : m = n := unit_mul_pow_congr_pow hirr hirr u v m n h,
rw ← sub_eq_zero at h,
rw [← sub_mul, mul_eq_zero] at h,
cases h,
{ rw sub_eq_zero at h, exact_mod_cast h },
{ apply (hirr.ne_zero (pow_eq_zero h)).elim, }
end
/-!
## The additive valuation on a DVR
-/
open multiplicity
/-- The `enat`-valued additive valuation on a DVR -/
noncomputable def add_val
(R : Type u) [comm_ring R] [is_domain R] [discrete_valuation_ring R] :
add_valuation R enat :=
add_valuation (classical.some_spec (exists_prime R))
lemma add_val_def (r : R) (u : Rˣ) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) (hr : r = u * ϖ ^ n) :
add_val R r = n :=
by rw [add_val, add_valuation_apply, hr,
eq_of_associated_left (associated_of_irreducible R hϖ
(classical.some_spec (exists_prime R)).irreducible),
eq_of_associated_right (associated.symm ⟨u, mul_comm _ _⟩),
multiplicity_pow_self_of_prime (principal_ideal_ring.irreducible_iff_prime.1 hϖ)]
lemma add_val_def' (u : Rˣ) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) :
add_val R ((u : R) * ϖ ^ n) = n :=
add_val_def _ u hϖ n rfl
@[simp] lemma add_val_zero : add_val R 0 = ⊤ :=
(add_val R).map_zero
@[simp] lemma add_val_one : add_val R 1 = 0 :=
(add_val R).map_one
@[simp] lemma add_val_uniformizer {ϖ : R} (hϖ : irreducible ϖ) : add_val R ϖ = 1 :=
by simpa only [one_mul, eq_self_iff_true, units.coe_one, pow_one, forall_true_left, nat.cast_one]
using add_val_def ϖ 1 hϖ 1
@[simp] lemma add_val_mul {a b : R} :
add_val R (a * b) = add_val R a + add_val R b :=
(add_val R).map_mul _ _
lemma add_val_pow (a : R) (n : ℕ) : add_val R (a ^ n) = n • add_val R a :=
(add_val R).map_pow _ _
lemma _root_.irreducible.add_val_pow {ϖ : R} (h : irreducible ϖ) (n : ℕ) :
add_val R (ϖ ^ n) = n :=
by rw [add_val_pow, add_val_uniformizer h, nsmul_one]
lemma add_val_eq_top_iff {a : R} : add_val R a = ⊤ ↔ a = 0 :=
begin
have hi := (classical.some_spec (exists_prime R)).irreducible,
split,
{ contrapose,
intro h,
obtain ⟨n, ha⟩ := associated_pow_irreducible h hi,
obtain ⟨u, rfl⟩ := ha.symm,
rw [mul_comm, add_val_def' u hi n],
exact enat.coe_ne_top _ },
{ rintro rfl,
exact add_val_zero }
end
lemma add_val_le_iff_dvd {a b : R} : add_val R a ≤ add_val R b ↔ a ∣ b :=
begin
have hp := classical.some_spec (exists_prime R),
split; intro h,
{ by_cases ha0 : a = 0,
{ rw [ha0, add_val_zero, top_le_iff, add_val_eq_top_iff] at h,
rw h,
apply dvd_zero },
obtain ⟨n, ha⟩ := associated_pow_irreducible ha0 hp.irreducible,
rw [add_val, add_valuation_apply, add_valuation_apply,
multiplicity_le_multiplicity_iff] at h,
exact ha.dvd.trans (h n ha.symm.dvd), },
{ rw [add_val, add_valuation_apply, add_valuation_apply],
exact multiplicity_le_multiplicity_of_dvd_right h }
end
lemma add_val_add {a b : R} :
min (add_val R a) (add_val R b) ≤ add_val R (a + b) :=
(add_val R).map_add _ _
end
instance (R : Type*) [comm_ring R] [is_domain R] [discrete_valuation_ring R] :
is_Hausdorff (maximal_ideal R) R :=
{ haus' := λ x hx,
begin
obtain ⟨ϖ, hϖ⟩ := exists_irreducible R,
simp only [← ideal.one_eq_top, smul_eq_mul, mul_one, smodeq.zero,
hϖ.maximal_ideal_eq, ideal.span_singleton_pow, ideal.mem_span_singleton,
← add_val_le_iff_dvd, hϖ.add_val_pow] at hx,
rwa [← add_val_eq_top_iff, enat.eq_top_iff_forall_le],
end }
end discrete_valuation_ring
|
6119853d45a8978ffb3764d65da1faa09e974766 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/meta/comp_value_tactics.lean | 34259f143237187c789db695dfc41303d073680d | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 2,072 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.tactic init.data.option.instances
meta constant mk_nat_val_ne_proof : expr → expr → option expr
meta constant mk_nat_val_lt_proof : expr → expr → option expr
meta constant mk_nat_val_le_proof : expr → expr → option expr
meta constant mk_fin_val_ne_proof : expr → expr → option expr
meta constant mk_char_val_ne_proof : expr → expr → option expr
meta constant mk_string_val_ne_proof : expr → expr → option expr
meta constant mk_int_val_ne_proof : expr → expr → option expr
namespace tactic
open expr
meta def comp_val : tactic unit :=
do t ← target,
guard (is_app t),
type ← infer_type t.app_arg,
(do is_def_eq type (const `nat []),
(do (a, b) ← is_ne t,
pr ← mk_nat_val_ne_proof a b,
exact pr)
<|>
(do (a, b) ← is_lt t,
pr ← mk_nat_val_lt_proof a b,
exact pr)
<|>
(do (a, b) ← is_gt t,
pr ← mk_nat_val_lt_proof b a,
exact pr)
<|>
(do (a, b) ← is_le t,
pr ← mk_nat_val_le_proof a b,
exact pr)
<|>
(do (a, b) ← is_ge t,
pr ← mk_nat_val_le_proof b a,
exact pr))
<|>
(do is_def_eq type (const `char []),
(a, b) ← is_ne t,
pr ← mk_char_val_ne_proof a b,
exact pr)
<|>
(do is_def_eq type (const `string []),
(a, b) ← is_ne t,
pr ← mk_string_val_ne_proof a b,
exact pr)
<|>
(do is_def_eq type (const `int []),
(a, b) ← is_ne t,
pr ← mk_int_val_ne_proof a b,
exact pr)
<|>
(do type ← whnf type,
guard (is_napp_of type `fin 1),
(a, b) ← is_ne t,
pr ← mk_fin_val_ne_proof a b,
exact pr)
<|>
(do (a, b) ← is_eq t,
unify a b, to_expr ``(eq.refl %%a) >>= exact)
end tactic
|
cc499d7f1d91e05051bdab9529cb7a8adeb95cca | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/data/nat/modeq.lean | 0c15e1fdb5de36642ab6931e74caadfc47cecd2e | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,193 | 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.int.gcd
import tactic.abel
import data.list.rotate
/-!
# Congruences modulo a natural number
This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers,
and proves basic properties about it such as the Chinese Remainder Theorem
`modeq_and_modeq_iff_modeq_mul`.
## Notations
`a ≡ b [MOD n]` is notation for `modeq n a b`, which is defined to mean `a % n = b % n`.
## Tags
modeq, congruence, mod, MOD, modulo
-/
namespace nat
/-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means
that `a - b` is a multiple of `n`. -/
@[derive decidable]
def modeq (n a b : ℕ) := a % n = b % n
notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b
namespace modeq
variables {n m a b c d : ℕ}
@[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _
@[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm
@[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans
protected theorem comm : a ≡ b [MOD n] ↔ b ≡ a [MOD n] := ⟨nat.modeq.symm, nat.modeq.symm⟩
theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a :=
by rw [modeq, zero_mod, dvd_iff_mod_eq_zero]
theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a :=
by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod,
int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero]
theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2
theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1
/-- A variant of `modeq_iff_dvd` with `nat` divisibility -/
theorem modeq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a :=
by rw [modeq_iff_dvd, ←int.coe_nat_dvd, int.coe_nat_sub h]
theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _
theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] :=
modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h)
theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] :=
by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h]
theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] :=
modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h
theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] :=
by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h
theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] :=
by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h
theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] :=
(modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁)
theorem modeq_pow (m : ℕ) (h : a ≡ b [MOD n]) : a ^ m ≡ b ^ m [MOD n] :=
begin
induction m with d hd, {refl},
rw [pow_succ, pow_succ],
exact modeq_mul h hd,
end
theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] :=
modeq_of_dvd begin
convert dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂) using 1,
simp [sub_eq_add_neg, add_left_comm, add_comm],
end
theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] :=
begin
simp only [modeq_iff_dvd] at *,
convert _root_.dvd_sub h₂ h₁ using 1,
simp [sub_eq_add_neg],
abel
end
theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] :=
by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂
theorem modeq_of_modeq_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] :=
by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left (n : ℤ) (m : ℤ)) h
theorem modeq_of_modeq_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] :=
mul_comm m n ▸ modeq_of_modeq_mul_left _
theorem modeq_one : a ≡ b [MOD 1] := modeq_of_dvd $ one_dvd _
local attribute [semireducible] int.nonneg
/-- The natural number less than `lcm n m` congruent to `a` mod `n` and `b` mod `m` -/
def chinese_remainder' (h : a ≡ b [MOD gcd n m]) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} :=
if hn : n = 0 then ⟨a, begin rw [hn, gcd_zero_left] at h, split, refl, exact h end⟩ else
if hm : m = 0 then ⟨b, begin rw [hm, gcd_zero_right] at h, split, exact h.symm, refl end⟩ else
⟨let (c, d) := xgcd n m in int.to_nat (((n * c * b + m * d * a) / gcd n m) % lcm n m), begin
rw xgcd_val,
dsimp [chinese_remainder'._match_1],
rw [modeq_iff_dvd, modeq_iff_dvd,
int.to_nat_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero.2 (lcm_ne_zero hn hm)))],
have hnonzero : (gcd n m : ℤ) ≠ 0 := begin
norm_cast,
rw [nat.gcd_eq_zero_iff, not_and],
exact λ _, hm,
end,
have hcoedvd : ∀ t, (gcd n m : ℤ) ∣ t * (b - a) := λ t, dvd_mul_of_dvd_right h.dvd_of_modeq _,
have := gcd_eq_gcd_ab n m,
split; rw [int.mod_def, ← sub_add]; refine dvd_add _ (dvd_mul_of_dvd_left _ _); try {norm_cast},
{ rw ← sub_eq_iff_eq_add' at this,
rw [← this, sub_mul, ← add_sub_assoc, add_comm, add_sub_assoc, ← mul_sub,
int.add_div_of_dvd_left, int.mul_div_cancel_left _ hnonzero,
int.mul_div_assoc _ h.dvd_of_modeq, ← sub_sub, sub_self, zero_sub, dvd_neg, mul_assoc],
exact dvd_mul_right _ _,
norm_cast, exact dvd_mul_right _ _, },
{ exact dvd_lcm_left n m, },
{ rw ← sub_eq_iff_eq_add at this,
rw [← this, sub_mul, sub_add, ← mul_sub, int.sub_div_of_dvd, int.mul_div_cancel_left _ hnonzero,
int.mul_div_assoc _ h.dvd_of_modeq, ← sub_add, sub_self, zero_add, mul_assoc],
exact dvd_mul_right _ _,
exact hcoedvd _ },
{ exact dvd_lcm_right n m, },
end⟩
/-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/
def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} :=
chinese_remainder' (by convert modeq_one)
lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℕ} (hmn : coprime m n) :
a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ (a ≡ b [MOD m * n]) :=
⟨λ h, begin
rw [nat.modeq.modeq_iff_dvd, nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs,
int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd] at h,
rw [nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd],
exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2
end,
λ h, ⟨nat.modeq.modeq_of_modeq_mul_right _ h, nat.modeq.modeq_of_modeq_mul_left _ h⟩⟩
lemma coprime_of_mul_modeq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : coprime a n :=
nat.coprime_of_dvd' (λ k kp ⟨ka, hka⟩ ⟨kb, hkb⟩, int.coe_nat_dvd.1 begin
rw [hka, hkb, modeq_iff_dvd] at h,
cases h with z hz,
rw [sub_eq_iff_eq_add] at hz,
rw [hz, int.coe_nat_mul, mul_assoc, mul_assoc, int.coe_nat_mul, ← mul_add],
exact dvd_mul_right _ _,
end)
end modeq
@[simp] lemma mod_mul_right_mod (a b c : ℕ) : a % (b * c) % b = a % b :=
modeq.modeq_of_modeq_mul_right _ (modeq.mod_modeq _ _)
@[simp] lemma mod_mul_left_mod (a b c : ℕ) : a % (b * c) % c = a % c :=
modeq.modeq_of_modeq_mul_left _ (modeq.mod_modeq _ _)
lemma div_mod_eq_mod_mul_div (a b c : ℕ) : a / b % c = a % (b * c) / b :=
if hb0 : b = 0 then by simp [hb0]
else by rw [← @add_right_cancel_iff _ _ (c * (a / b / c)), mod_add_div, nat.div_div_eq_div_mul,
← nat.mul_right_inj (nat.pos_of_ne_zero hb0),← @add_left_cancel_iff _ _ (a % b), mod_add_div,
mul_add, ← @add_left_cancel_iff _ _ (a % (b * c) % b), add_left_comm,
← add_assoc (a % (b * c) % b), mod_add_div, ← mul_assoc, mod_add_div, mod_mul_right_mod]
lemma add_mod_add_ite (a b c : ℕ) :
(a + b) % c + (if c ≤ a % c + b % c then c else 0) = a % c + b % c :=
have (a + b) % c = (a % c + b % c) % c,
from nat.modeq.modeq_add (nat.modeq.mod_modeq _ _).symm (nat.modeq.mod_modeq _ _).symm,
if hc0 : c = 0 then by simp [hc0]
else
begin
rw this,
split_ifs,
{ have h2 : (a % c + b % c) / c < 2,
from nat.div_lt_of_lt_mul (by rw mul_two;
exact add_lt_add (nat.mod_lt _ (nat.pos_of_ne_zero hc0))
(nat.mod_lt _ (nat.pos_of_ne_zero hc0))),
have h0 : 0 < (a % c + b % c) / c, from nat.div_pos h (nat.pos_of_ne_zero hc0),
rw [← @add_right_cancel_iff _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc,
mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] },
{ rw [nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] }
end
lemma add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) :
(a + b) % c = a % c + b % c :=
by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero]
lemma add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) :
(a + b) % c + c = a % c + b % c :=
by rw [← add_mod_add_ite, if_pos hc]
lemma add_div {a b c : ℕ} (hc0 : 0 < c) : (a + b) / c = a / c + b / c +
if c ≤ a % c + b % c then 1 else 0 :=
begin
rw [← nat.mul_right_inj hc0, ← @add_left_cancel_iff _ _ ((a + b) % c + a % c + b % c)],
suffices : (a + b) % c + c * ((a + b) / c) + a % c + b % c =
a % c + c * (a / c) + (b % c + c * (b / c)) + c * (if c ≤ a % c + b % c then 1 else 0) +
(a + b) % c,
{ simpa only [mul_add, add_comm, add_left_comm, add_assoc] },
rw [mod_add_div, mod_add_div, mod_add_div, mul_ite, add_assoc, add_assoc],
conv_lhs { rw ← add_mod_add_ite },
simp, ac_refl
end
lemma add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) :
(a + b) / c = a / c + b / c :=
if hc0 : c = 0 then by simp [hc0]
else by rw [add_div (nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero]
protected lemma add_div_of_dvd_right {a b c : ℕ} (hca : c ∣ a) :
(a + b) / c = a / c + b / c :=
if h : c = 0 then by simp [h] else add_div_eq_of_add_mod_lt begin
rw [nat.mod_eq_zero_of_dvd hca, zero_add],
exact nat.mod_lt _ (pos_iff_ne_zero.mpr h),
end
protected lemma add_div_of_dvd_left {a b c : ℕ} (hca : c ∣ b) :
(a + b) / c = a / c + b / c :=
by rwa [add_comm, nat.add_div_of_dvd_right, add_comm]
lemma add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) :
(a + b) / c = a / c + b / c + 1 :=
by rw [add_div hc0, if_pos hc]
lemma add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c :=
if hc0 : c = 0 then by simp [hc0]
else by rw [nat.add_div (nat.pos_of_ne_zero hc0)]; exact le_add_right _ _
lemma le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬ c ∣ a) :
c ≤ a % c + b % c :=
by_contradiction $ λ hc,
have (a + b) % c = a % c + b % c, from add_mod_of_add_mod_lt (lt_of_not_ge hc),
by simp [dvd_iff_mod_eq_zero, *] at *
lemma odd_mul_odd {n m : ℕ} : n % 2 = 1 → m % 2 = 1 → (n * m) % 2 = 1 :=
by simpa [nat.modeq] using @nat.modeq.modeq_mul 2 n 1 m 1
lemma odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) :
(m * n) / 2 = m * (n / 2) + m / 2 :=
have hm0 : 0 < m := nat.pos_of_ne_zero (λ h, by simp * at *),
have hn0 : 0 < n := nat.pos_of_ne_zero (λ h, by simp * at *),
(nat.mul_right_inj (show 0 < 2, from dec_trivial)).1 $
by rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1,
two_mul_odd_div_two (nat.odd_mul_odd hm1 hn1), nat.mul_sub_left_distrib, mul_one,
← nat.add_sub_assoc hm0, nat.sub_add_cancel (le_mul_of_one_le_right (nat.zero_le _) hn0)]
lemma odd_of_mod_four_eq_one {n : ℕ} : n % 4 = 1 → n % 2 = 1 :=
by simpa [modeq, show 2 * 2 = 4, by norm_num] using @modeq.modeq_of_modeq_mul_left 2 n 1 2
lemma odd_of_mod_four_eq_three {n : ℕ} : n % 4 = 3 → n % 2 = 1 :=
by simpa [modeq, show 2 * 2 = 4, by norm_num, show 3 % 4 = 3, by norm_num]
using @modeq.modeq_of_modeq_mul_left 2 n 3 2
end nat
namespace list
variable {α : Type*}
lemma nth_rotate : ∀ {l : list α} {n m : ℕ} (hml : m < l.length),
(l.rotate n).nth m = l.nth ((m + n) % l.length)
| [] n m hml := (nat.not_lt_zero _ hml).elim
| l 0 m hml := by simp [nat.mod_eq_of_lt hml]
| (a::l) (n+1) m hml :=
have h₃ : m < list.length (l ++ [a]), by simpa using hml,
(lt_or_eq_of_le (nat.le_of_lt_succ $ nat.mod_lt (m + n)
(lt_of_le_of_lt (nat.zero_le _) hml))).elim
(λ hml',
have h₁ : (m + (n + 1)) % ((a :: l : list α).length) =
(m + n) % ((a :: l : list α).length) + 1,
from calc (m + (n + 1)) % (l.length + 1) =
((m + n) % (l.length + 1) + 1) % (l.length + 1) :
add_assoc m n 1 ▸ nat.modeq.modeq_add (nat.mod_mod _ _).symm rfl
... = (m + n) % (l.length + 1) + 1 : nat.mod_eq_of_lt (nat.succ_lt_succ hml'),
have h₂ : (m + n) % (l ++ [a]).length < l.length, by simpa [nat.add_one] using hml',
by rw [list.rotate_cons_succ, nth_rotate h₃, list.nth_append h₂, h₁, list.nth]; simp)
(λ hml',
have h₁ : (m + (n + 1)) % (l.length + 1) = 0,
from calc (m + (n + 1)) % (l.length + 1) = (l.length + 1) % (l.length + 1) :
add_assoc m n 1 ▸ nat.modeq.modeq_add
(hml'.trans (nat.mod_eq_of_lt (nat.lt_succ_self _)).symm) rfl
... = 0 : by simp,
by rw [list.length, list.rotate_cons_succ, nth_rotate h₃, list.length_append,
list.length_cons, list.length, zero_add, hml', h₁, list.nth_concat_length]; refl)
lemma rotate_eq_self_iff_eq_repeat [hα : nonempty α] : ∀ {l : list α},
(∀ n, l.rotate n = l) ↔ ∃ a, l = list.repeat a l.length
| [] := ⟨λ h, nonempty.elim hα (λ a, ⟨a, by simp⟩), by simp⟩
| (a::l) :=
⟨λ h, ⟨a, list.ext_le (by simp) $ λ n hn h₁,
begin
rw [← option.some_inj, ← list.nth_le_nth],
conv {to_lhs, rw ← h ((list.length (a :: l)) - n)},
rw [nth_rotate hn, nat.add_sub_cancel' (le_of_lt hn),
nat.mod_self, nth_le_repeat], refl
end⟩,
λ ⟨a, ha⟩ n, ha.symm ▸ list.ext_le (by simp)
(λ m hm h,
have hm' : (m + n) % (list.repeat a (list.length (a :: l))).length < list.length (a :: l),
by rw list.length_repeat; exact nat.mod_lt _ (nat.succ_pos _),
by rw [nth_le_repeat, ← option.some_inj, ← list.nth_le_nth, nth_rotate h, list.nth_le_nth,
nth_le_repeat]; simp * at *)⟩
end list
|
5154dbfadc82b4761db441729685f924cbf845c8 | 4727251e0cd73359b15b664c3170e5d754078599 | /test/equiv.lean | a9b11aa92041b4306a6ac58ec92f04a94613ccba | [
"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 | 400 | lean | import data.set.finite
import data.finset.basic
def s : finset (fin 3) := {0, 1}
example : finset (fin 3) := s.map (equiv.add_left (1 : fin 3)).to_embedding
example : finset (fin 3) := s.map (equiv.add_left (1 : fin 3))
def f : equiv (fin 3) (fin 3) :=
{ to_fun := λ x, x + 1,
inv_fun := λ x, x - 1,
left_inv := dec_trivial,
right_inv := dec_trivial }
example : finset (fin 3) := s.map f
|
a763c901b71c4214f1e9ba8c51b59ed974f1fb1a | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/enat/basic.lean | 105dac551f4f097797cefa769695ec233823b137 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,754 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import data.nat.succ_pred
import algebra.char_zero.lemmas
import algebra.order.sub.with_top
import algebra.order.ring.with_top
/-!
# Definition and basic properties of extended natural numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define `enat` (notation: `ℕ∞`) to be `with_top ℕ` and prove some basic lemmas
about this type.
-/
/-- Extended natural numbers `ℕ∞ = with_top ℕ`. -/
@[derive [has_zero, add_comm_monoid_with_one, canonically_ordered_comm_semiring, nontrivial,
linear_order, order_bot, order_top, has_bot, has_top, canonically_linear_ordered_add_monoid,
has_sub, has_ordered_sub, linear_ordered_add_comm_monoid_with_top, succ_order, well_founded_lt,
has_well_founded, char_zero, has_coe_t ℕ]]
def enat : Type := with_top ℕ
notation `ℕ∞` := enat
namespace enat
instance : inhabited ℕ∞ := ⟨0⟩
instance : is_well_order ℕ∞ (<) := { }
variables {m n : ℕ∞}
-- eligible for `dsimp`
@[simp, nolint simp_nf, norm_cast] lemma coe_zero : ((0 : ℕ) : ℕ∞) = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : ℕ) : ℕ∞) = 1 := rfl
@[simp, norm_cast] lemma coe_add (m n : ℕ) : ↑(m + n) = (m + n : ℕ∞) := rfl
@[simp, norm_cast] lemma coe_sub (m n : ℕ) : ↑(m - n) = (m - n : ℕ∞) := rfl
@[simp, norm_cast] lemma coe_mul (m n : ℕ) : ↑(m * n) = (m * n : ℕ∞) := with_top.coe_mul
instance can_lift : can_lift ℕ∞ ℕ coe (λ n, n ≠ ⊤) := with_top.can_lift
/-- Conversion of `ℕ∞` to `ℕ` sending `∞` to `0`. -/
def to_nat : monoid_with_zero_hom ℕ∞ ℕ :=
{ to_fun := with_top.untop' 0,
map_one' := rfl,
map_zero' := rfl,
map_mul' := with_top.untop'_zero_mul }
@[simp] lemma to_nat_coe (n : ℕ) : to_nat n = n := rfl
@[simp] lemma to_nat_top : to_nat ⊤ = 0 := rfl
@[simp] lemma coe_to_nat_eq_self : ↑n.to_nat = n ↔ n ≠ ⊤ :=
with_top.rec_top_coe (by simp) (by simp) n
alias coe_to_nat_eq_self ↔ _ coe_to_nat
lemma coe_to_nat_le_self (n : ℕ∞) : ↑(to_nat n) ≤ n := with_top.rec_top_coe le_top (λ k, le_rfl) n
lemma to_nat_add {m n : ℕ∞} (hm : m ≠ ⊤) (hn : n ≠ ⊤) : to_nat (m + n) = to_nat m + to_nat n :=
by { lift m to ℕ using hm, lift n to ℕ using hn, refl }
lemma to_nat_sub {n : ℕ∞} (hn : n ≠ ⊤) (m : ℕ∞) : to_nat (m - n) = to_nat m - to_nat n :=
begin
lift n to ℕ using hn,
induction m using with_top.rec_top_coe,
{ rw [with_top.top_sub_coe, to_nat_top, zero_tsub] },
{ rw [← coe_sub, to_nat_coe, to_nat_coe, to_nat_coe] }
end
lemma to_nat_eq_iff {m : ℕ∞} {n : ℕ} (hn : n ≠ 0) : m.to_nat = n ↔ m = n :=
by induction m using with_top.rec_top_coe; simp [hn.symm]
@[simp] lemma succ_def (m : ℕ∞) : order.succ m = m + 1 := by cases m; refl
lemma add_one_le_of_lt (h : m < n) : m + 1 ≤ n :=
m.succ_def ▸ order.succ_le_of_lt h
lemma add_one_le_iff (hm : m ≠ ⊤) : m + 1 ≤ n ↔ m < n :=
m.succ_def ▸ (order.succ_le_iff_of_not_is_max $ by rwa [is_max_iff_eq_top])
lemma one_le_iff_pos : 1 ≤ n ↔ 0 < n := add_one_le_iff with_top.zero_ne_top
lemma one_le_iff_ne_zero : 1 ≤ n ↔ n ≠ 0 := one_le_iff_pos.trans pos_iff_ne_zero
lemma le_of_lt_add_one (h : m < n + 1) : m ≤ n := order.le_of_lt_succ $ n.succ_def.symm ▸ h
@[elab_as_eliminator]
lemma nat_induction {P : ℕ∞ → Prop} (a : ℕ∞) (h0 : P 0) (hsuc : ∀ n : ℕ, P n → P n.succ)
(htop : (∀ n : ℕ, P n) → P ⊤) : P a :=
begin
have A : ∀ n : ℕ, P n := λ n, nat.rec_on n h0 hsuc,
cases a,
exacts [htop A, A a]
end
end enat
|
9d55e76891206c0e5f416b79f489a800a8be89eb | 7da5ceac20aaab989eeb795a4be9639982e7b35a | /src/tactic/omitted.lean | 3dd6f1daa56330c2be2a6cfcfc34bcc692799aab | [
"MIT"
] | permissive | formalabstracts/formalabstracts | 46c2f1b3a172e62ca6ffeb46fbbdf1705718af49 | b0173da1af45421239d44492eeecd54bf65ee0f6 | refs/heads/master | 1,606,896,370,374 | 1,572,988,776,000 | 1,572,988,776,000 | 96,763,004 | 165 | 28 | null | 1,555,709,319,000 | 1,499,680,948,000 | Lean | UTF-8 | Lean | false | false | 6,750 | lean | /-
Copyright (c) 2019 Jesse Han. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jesse Han
Some goodies for working with `omitted`
- `depends_omitted e` checks if `omitted` occurs in `e`. it will unfold one layer of
proof terms (e.g. a propositional field of a structure being checked) and check if
`omitted` occurs in those proof terms
- `omitted` tries to close a propositional goal with `exact omitted`
- `omit_proofs` tries to close any visible goals with the `omitted` tactic
- `tidy_omitted` runs `tidy` and lets it use `omitted` (and still produces proof traces).
`tidy_omitted` will always attempt to split existential statements and sigma-types in the goal, and will fill in as little data as possible (it is not allowed to use assumption,
solve_by_elim, simp, or dsimp)
if it has used `omitted`, `tidy` will emit a trace urging the user to replace
the call to `tidy_omitted` with the proof trace generated by
`tidy_omitted {trace_result := tt}
-/
import tactic.tidy ..basic tactic.explode
section omitted_tactics
open tactic
/-- Check if the goal is a proposition; if so, prove it using omitted.
When called with "tidy using omitted", tidy will run as usual and fulfill all
proof obligations using omitted, leaving it to the user to specify the data. -/
meta def tactic.interactive.omitted : tactic unit :=
propositional_goal >> `[exact omitted] <|> tactic.fail "Goal is not a proposition and cannot be omitted"
meta def tactic.interactive.omit_proofs : tactic unit := `[all_goals {try {omitted}}]
meta def tactic.verbose_omitted : tactic string :=
tactic.interactive.omitted >> tactic.trace "`tidy` used `omitted`, please replace this call to `tidy_omitted` with the output of {trace_result := tt}"
>> return "omitted"
open tactic.tidy
meta def omitted_default_tactics : list (tactic string) :=
[ reflexivity >> pure "refl",
`[exact dec_trivial] >> pure "exact dec_trivial",
-- propositional_goal >> assumption >> pure "assumption",
ext1_wrapper,
intros1 >>= λ ns, pure ("intros " ++ (" ".intercalate (ns.map (λ e, e.to_string)))),
auto_cases,
`[apply_auto_param] >> pure "apply_auto_param",
-- `[dsimp at *] >> pure "dsimp at *",
-- `[simp at *] >> pure "simp at *",
fsplit >> pure "fsplit",
injections_and_clear >> pure "injections_and_clear",
-- propositional_goal >> (`[solve_by_elim]) >> pure "solve_by_elim",2
`[unfold_aux] >> pure "unfold_aux",--
-- tidy.run_tactics
tactic.verbose_omitted ]
meta structure omitted_cfg :=
(trace_result : bool := ff)
(trace_result_prefix : string := "/- `tidy` says -/ ")
(tactics : list (tactic string) := omitted_default_tactics)
meta def cfg_of_omitted_cfg : omitted_cfg → cfg :=
λ X, { trace_result := X.trace_result,
trace_result_prefix := X.trace_result_prefix,
tactics := X.tactics }
/- Calls tidy, but with `omitted` thrown into the tactic list.
tidy {trace_result := tt}` produces a proof trace as usual.-/
meta def tactic.interactive.tidy_omitted (cfg : omitted_cfg := {}): tactic unit :=
tidy (cfg_of_omitted_cfg cfg)
end omitted_tactics
section depends_omitted_cmd
open tactic expr
meta def extract_proof_names_aux : ∀(e : expr) (l : list name), tactic (list name)
| (const a b) l := do b <- infer_type (const a b) >>= is_prop,
if b then return (a::l) else return l
| e l := return l
meta def extract_proof_names (e : expr) : tactic $ list name :=
e.mfold [] (λ e n l₁, extract_proof_names_aux e l₁)
meta def depends_omitted_aux (n : name) : tactic expr :=
do const n _ ← resolve_name n | fail "cannot resolve name",
d ← get_decl n,
e ← match d with
| (declaration.defn _ _ _ e _ _) := return e
| (declaration.thm _ _ _ e) := return e.get
| _ := fail "not a definition"
end, return e
meta def depends_omitted_aux' (n : name) : tactic expr :=
do const n _ ← resolve_name n | fail "cannot resolve name",
d ← get_decl n,
e ← (match d with
| (declaration.defn _ _ _ e _ _) := return e
| (declaration.thm _ _ _ e) := return e.get
| _ := fail "not a definition"
end) <|> return expr.inhabited.default, return e
meta def depends_omitted (n : name) : tactic unit :=
do e <- depends_omitted_aux n,
o <- to_expr ``(omitted),
ls <- (extract_proof_names e) >>= λ l, l.mmap depends_omitted_aux',
b_l <- ls.mfoldr (λ e b, (kdepends_on e o) >>= return ∘ (bor b)) ff,
b <- kdepends_on e o,
if (bor b b_l) then trace (n ++ " directly depends on `omitted`") else
trace (n ++ " does not directly depend on `omitted`")
open interactive lean lean.parser interaction_monad.result
@[user_command] meta def depends_omitted_cmd (_ : parse $ tk "#depends_omitted") : lean.parser unit :=
do n ← ident,
depends_omitted n
end depends_omitted_cmd
/- Tests -/
-- section test0
-- lemma test : 1 + 1 = 2 := by omitted
-- noncomputable def test' : ℕ := classical.choice $ by omitted
-- structure test_structure :=
-- (x : ℕ)
-- noncomputable def test'' : test_structure :=
-- ⟨test'⟩
-- #depends_omitted test
-- --test. directly depends on `omitted`
-- #depends_omitted test'
-- --test'. directly depends on `omitted`
-- #depends_omitted test''
-- --test''. does not directly depend on `omitted`
-- end test0
-- section test1
-- variable {α : Type*}
-- variable (P : α → Prop)
-- variable (a : α)
-- open vector
-- example : vector α 1 ≃ α :=
-- begin
-- split, omit_proofs,
-- from λ x, ⟨[x], dec_trivial⟩,
-- from λ x, x.head
-- end
-- /- In this example, (a : α) is in context, but `tidy_omitted` refuses to use it -/
-- include a
-- example : Σ' a : α, P a := -- by {tidy_omitted, exact a}
-- by {/- `tidy` says -/ fsplit, work_on_goal 1 { omitted }, exact a}
-- end test1
-- section test2
-- private def is_even (n : ℕ) := ∃ k, 2 * k = n
-- private lemma test : ∃ m : ℕ, is_even m :=
-- begin
-- tidy_omitted, exact 2
-- end
-- private lemma test'' : ∃ m, is_even m :=
-- by {use 2, use 1, refl}
-- -- #print test''
-- -- #print test
-- /-
-- 92:1: theorem test : ∃ (m : ℕ), is_even m :=
-- id (Exists.intro (2 * 2) (Exists.intro 2 (eq.refl (2 * 2))))
-- -/
-- private lemma test' : ∃ m, is_even m := by omitted
-- -- #print test'
-- /-
-- 100:1: theorem hewwo' : ∃ (m : ℕ), is_even m :=
-- omitted
-- -/
-- end test2
|
ad14e1b5ab599a27f7d3fb8b8ee9cf89bb6cb214 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/order/bounds.lean | 88edbd74d23e898b0f2744ca39e7b8ea859e8aad | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 24,574 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov
-/
import algebra.order_functions data.set.intervals.basic
/-!
# Upper / lower bounds
In this file we define:
* `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set;
* `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper
(resp., lower) bounds of `s` is nonempty;
* `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`;
for a partial order, it is unique if exists;
* `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound)
of `s`; for a partial order, it is unique if exists.
We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide
formulas for `∅`, `univ`, and intervals.
-/
open set
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section
variables [preorder α] [preorder β] {s t : set α} {a b : α}
/-!
### Definitions
-/
/-- The set of upper bounds of a set. -/
def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x }
/-- The set of lower bounds of a set. -/
def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a }
/-- A set is bounded above if there exists an upper bound. -/
def bdd_above (s : set α) := (upper_bounds s).nonempty
/-- A set is bounded below if there exists a lower bound. -/
def bdd_below (s : set α) := (lower_bounds s).nonempty
/-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/
def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s
/-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/
def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s
/-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/
def is_lub (s : set α) : α → Prop := is_least (upper_bounds s)
/-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/
def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s)
/-!
### Monotonicity
-/
lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
upper_bounds t ⊆ upper_bounds s :=
λ b hb x h, hb $ hst h
lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
lower_bounds t ⊆ lower_bounds s :=
λ b hb x h, hb $ hst h
lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s :=
λ ha x h, le_trans (ha h) hab
lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s :=
λ hb x h, le_trans hab (hb h)
lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
a ∈ upper_bounds t → b ∈ upper_bounds s :=
λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha
lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
b ∈ lower_bounds t → a ∈ lower_bounds s :=
λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb
/-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/
lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s :=
nonempty.mono $ upper_bounds_mono_set h
/-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/
lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s :=
nonempty.mono $ lower_bounds_mono_set h
/-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a :=
⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩
/-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a :=
@is_lub.of_subset_of_superset (order_dual α) _ a s t p hs hp hst htp
/-!
### Conversions
-/
lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a :=
set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩
lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a :=
@is_lub.upper_bounds_eq (order_dual α) _ _ _ h
lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a :=
h.is_glb.lower_bounds_eq
lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a :=
h.is_lub.upper_bounds_eq
/-- If `s` has a least upper bound, then it is bounded above. -/
lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩
/-- If `s` has a greatest lower bound, then it is bounded below. -/
lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩
/-- If `s` has a greatest element, then it is bounded above. -/
lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩
/-- If `s` has a least element, then it is bounded below. -/
lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩
lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩
lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩
/-!
### Union and intersection
-/
@[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t :=
subset.antisymm
(λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩)
(λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht))
@[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t :=
@upper_bounds_union (order_dual α) _ s t
lemma union_upper_bounds_subset_upper_bounds_inter :
upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) :=
union_subset
(upper_bounds_mono_set $ inter_subset_left _ _)
(upper_bounds_mono_set $ inter_subset_right _ _)
lemma union_lower_bounds_subset_lower_bounds_inter :
lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) :=
@union_upper_bounds_subset_upper_bounds_inter (order_dual α) _ s t
lemma is_least_union_iff {a : α} {s t : set α} :
is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) :=
by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc]
lemma is_greatest_union_iff :
is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨
a ∈ upper_bounds s ∧ is_greatest t a) :=
@is_least_union_iff (order_dual α) _ a s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/
lemma bdd_above.union [semilattice_sup γ] {s t : set γ} :
bdd_above s → bdd_above t → bdd_above (s ∪ t) :=
begin
rintros ⟨bs, hs⟩ ⟨bt, ht⟩,
use bs ⊔ bt,
rw upper_bounds_union,
exact ⟨upper_bounds_mono_mem le_sup_left hs,
upper_bounds_mono_mem le_sup_right ht⟩
end
/-- The union of two sets is bounded above if and only if each of the sets is. -/
lemma bdd_above_union [semilattice_sup γ] {s t : set γ} :
bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t :=
⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩,
λ h, h.1.union h.2⟩
lemma bdd_below.union [semilattice_inf γ] {s t : set γ} :
bdd_below s → bdd_below t → bdd_below (s ∪ t) :=
@bdd_above.union (order_dual γ) _ s t
/--The union of two sets is bounded above if and only if each of the sets is.-/
lemma bdd_below_union [semilattice_inf γ] {s t : set γ} :
bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t :=
@bdd_above_union (order_dual γ) _ s t
/-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`,
then `a ⊔ b` is the least upper bound of `s ∪ t`. -/
lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ}
(hs : is_lub s a) (ht : is_lub t b) :
is_lub (s ∪ t) (a ⊔ b) :=
⟨assume c h, h.cases_on (λ h, le_sup_left_of_le $ hs.left h) (λ h, le_sup_right_of_le $ ht.left h),
assume c hc, sup_le
(hs.right $ assume d hd, hc $ or.inl hd) (ht.right $ assume d hd, hc $ or.inr hd)⟩
/-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`,
then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/
lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ}
(hs : is_glb s a₁) (ht : is_glb t a₂) :
is_glb (s ∪ t) (a₁ ⊓ a₂) :=
@is_lub.union (order_dual γ) _ _ _ _ _ hs ht
/-- If `a` is the least element of `s` and `b` is the least element of `t`,
then `min a b` is the least element of `s ∪ t`. -/
lemma is_least.union [decidable_linear_order γ] {a b : γ} {s t : set γ}
(ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_glb.union hb.is_glb).1⟩
/-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`,
then `max a b` is the greatest element of `s ∪ t`. -/
lemma is_greatest.union [decidable_linear_order γ] {a b : γ} {s t : set γ}
(ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_lub.union hb.is_lub).1⟩
/-!
### Specific sets
#### Unbounded intervals
-/
lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩
lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩
lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub
lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb
lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq
lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq
lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above
lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below
lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩
lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩
section
variables [linear_order γ] [densely_ordered γ]
lemma is_lub_Iio {a : γ} : is_lub (Iio a) a :=
⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩
lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio (order_dual γ) _ _ a
lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq
lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq
end
/-!
#### Singleton
-/
lemma is_greatest_singleton : is_greatest {a} a :=
⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩
lemma is_least_singleton : is_least {a} a :=
@is_greatest_singleton (order_dual α) _ a
lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub
lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb
lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above
lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below
lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq
lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq
/-!
#### Bounded intervals
-/
lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩
lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self
lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self
lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self
lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b :=
⟨right_mem_Icc.2 h, λ x, and.right⟩
lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub
lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b :=
(is_lub_Icc h).upper_bounds_eq
lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a :=
⟨left_mem_Icc.2 h, λ x, and.left⟩
lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb
lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a :=
(is_glb_Icc h).lower_bounds_eq
lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b :=
⟨right_mem_Ioc.2 h, λ x, and.right⟩
lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b :=
(is_greatest_Ioc h).is_lub
lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b :=
(is_lub_Ioc h).upper_bounds_eq
lemma is_least_Ico (h : a < b) : is_least (Ico a b) a :=
⟨left_mem_Ico.2 h, λ x, and.left⟩
lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a :=
(is_least_Ico h).is_glb
lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a :=
(is_glb_Ico h).lower_bounds_eq
section
variables [linear_order γ] [densely_ordered γ]
lemma is_glb_Ioo {a b : γ} (hab : a < b) : is_glb (Ioo a b) a :=
begin
refine ⟨λx hx, le_of_lt hx.1, λy hy, le_of_not_lt $ λ h, _⟩,
letI := classical.DLO γ,
have : a < min b y, by { rw lt_min_iff, exact ⟨hab, h⟩ },
rcases dense this with ⟨z, az, zy⟩,
rw lt_min_iff at zy,
exact lt_irrefl _ (lt_of_le_of_lt (hy ⟨az, zy.1⟩) zy.2)
end
lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a :=
(is_glb_Ioo hab).lower_bounds_eq
lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a :=
(is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc $ le_of_lt hab)
Ioo_subset_Ioc_self Ioc_subset_Icc_self
lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a :=
(is_glb_Ioc hab).lower_bounds_eq
lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b :=
by simpa only [dual_Ioo] using @is_glb_Ioo (order_dual γ) _ _ b a hab
lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b :=
(is_lub_Ioo hab).upper_bounds_eq
lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b :=
by simpa only [dual_Ioc] using @is_glb_Ioc (order_dual γ) _ _ b a hab
lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b :=
(is_lub_Ico hab).upper_bounds_eq
end
lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl
lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl
lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b :=
by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici,
bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right]
/-!
### Univ
-/
lemma order_top.upper_bounds_univ [order_top γ] : upper_bounds (univ : set γ) = {⊤} :=
set.ext $ λ b, iff.trans ⟨λ hb, top_unique $ hb trivial, λ hb x hx, hb.symm ▸ le_top⟩
mem_singleton_iff.symm
lemma is_greatest_univ [order_top γ] : is_greatest (univ : set γ) ⊤ :=
by simp only [is_greatest, order_top.upper_bounds_univ, mem_univ, mem_singleton, true_and]
lemma is_lub_univ [order_top γ] : is_lub (univ : set γ) ⊤ :=
is_greatest_univ.is_lub
lemma order_bot.lower_bounds_univ [order_bot γ] : lower_bounds (univ : set γ) = {⊥} :=
@order_top.upper_bounds_univ (order_dual γ) _
lemma is_least_univ [order_bot γ] : is_least (univ : set γ) ⊥ :=
@is_greatest_univ (order_dual γ) _
lemma is_glb_univ [order_bot γ] : is_glb (univ : set γ) ⊥ :=
is_least_univ.is_glb
lemma no_top_order.upper_bounds_univ [no_top_order α] : upper_bounds (univ : set α) = ∅ :=
eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := no_top b in
not_le_of_lt hx (hb trivial)
lemma no_bot_order.lower_bounds_univ [no_bot_order α] : lower_bounds (univ : set α) = ∅ :=
@no_top_order.upper_bounds_univ (order_dual α) _ _
/-!
### Empty set
-/
@[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ :=
by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff]
@[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ :=
by simp only [lower_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff]
@[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) :=
by simp only [bdd_above, upper_bounds_empty, univ_nonempty]
@[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) :=
by simp only [bdd_below, lower_bounds_empty, univ_nonempty]
lemma is_glb_empty [order_top γ] : is_glb ∅ (⊤:γ) :=
by simp only [is_glb, lower_bounds_empty, is_greatest_univ]
lemma is_lub_empty [order_bot γ] : is_lub ∅ (⊥:γ) :=
@is_glb_empty (order_dual γ) _
lemma is_lub.nonempty [no_bot_order α] (hs : is_lub s a) : s.nonempty :=
let ⟨a', ha'⟩ := no_bot a in
ne_empty_iff_nonempty.1 $ assume h,
have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty],
not_le_of_lt ha' this
lemma is_glb.nonempty [no_top_order α] (hs : is_glb s a) : s.nonempty :=
@is_lub.nonempty (order_dual α) _ _ _ _ hs
/-!
### insert
-/
/-- Adding a point to a set preserves its boundedness above. -/
@[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} :
bdd_above (insert a s) ↔ bdd_above s :=
by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and]
lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) :
bdd_above (insert a s) :=
(bdd_above_insert a).2 hs
/--Adding a point to a set preserves its boundedness below.-/
@[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} :
bdd_below (insert a s) ↔ bdd_below s :=
by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and]
lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) :
bdd_below (insert a s) :=
(bdd_below_insert a).2 hs
lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) :
is_lub (insert a s) (a ⊔ b) :=
by { rw insert_eq, exact is_lub_singleton.union hs }
lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) :
is_glb (insert a s) (a ⊓ b) :=
by { rw insert_eq, exact is_glb_singleton.union hs }
lemma is_greatest.insert [decidable_linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) :
is_greatest (insert a s) (max a b) :=
by { rw insert_eq, exact is_greatest_singleton.union hs }
lemma is_least.insert [decidable_linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) :
is_least (insert a s) (min a b) :=
by { rw insert_eq, exact is_least_singleton.union hs }
lemma upper_bounds_insert (a : α) (s : set α) :
upper_bounds (insert a s) = Ici a ∩ upper_bounds s :=
by rw [insert_eq, upper_bounds_union, upper_bounds_singleton]
lemma lower_bounds_insert (a : α) (s : set α) :
lower_bounds (insert a s) = Iic a ∩ lower_bounds s :=
by rw [insert_eq, lower_bounds_union, lower_bounds_singleton]
/-- When there is a global maximum, every set is bounded above. -/
@[simp] protected lemma order_top.bdd_above [order_top γ] (s : set γ) : bdd_above s :=
⟨⊤, assume a ha, order_top.le_top a⟩
/-- When there is a global minimum, every set is bounded below. -/
@[simp] protected lemma order_bot.bdd_below [order_bot γ] (s : set γ) : bdd_below s :=
⟨⊥, assume a ha, order_bot.bot_le a⟩
/-!
### Pair
-/
lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) :=
by { rw sup_comm, exact is_lub_singleton.insert _}
lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) :=
by { rw inf_comm, exact is_glb_singleton.insert _ }
lemma is_least_pair [decidable_linear_order γ] {a b : γ} : is_least {a, b} (min a b) :=
by { rw min_comm, exact is_least_singleton.insert _ }
lemma is_greatest_pair [decidable_linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) :=
by { rw max_comm, exact is_greatest_singleton.insert _ }
end
/-!
### (In)equalities with the least upper bound and the greatest lower bound
-/
section preorder
variables [preorder α] {s : set α} {a b : α}
lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) :
s.nonempty → a ≤ b
| ⟨c, hc⟩ := le_trans (ha hc) (hb hc)
lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b :=
lower_bounds_le_upper_bounds ha.1 hb.1 hs
lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b :=
⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩
lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c :=
@is_lub_lt_iff (order_dual α) _ s _ _ ha
end preorder
section partial_order
variables [partial_order α] {s : set α} {a b : α}
lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b :=
le_antisymm (Ha.right Hb.left) (Hb.right Ha.left)
lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b :=
le_antisymm (Hb.right Ha.left) (Ha.right Hb.left)
lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b :=
Ha.unique Hb
lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b :=
Ha.unique Hb
lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s :=
by { rw h.upper_bounds_eq, refl }
lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s :=
by { rw h.lower_bounds_eq, refl }
end partial_order
section linear_order
variables [linear_order α] {s : set α} {a b : α}
lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c :=
by haveI := classical.dec;
simpa [upper_bounds, not_ball] using
not_congr (@is_lub_le_iff _ _ _ _ b h)
lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b :=
@lt_is_lub_iff (order_dual α) _ _ _ _ h
end linear_order
/-!
### Images of upper/lower bounds under monotone functions
-/
namespace monotone
variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α}
lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) :
f a ∈ upper_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) :
f a ∈ lower_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
/-- The image under a monotone function of a set which is bounded above is bounded above. -/
lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩
/-- The image under a monotone function of a set which is bounded below is bounded below. -/
lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩
/-- A monotone map sends a least element of a set to a least element of its image. -/
lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩
/-- A monotone map sends a greatest element of a set to a greatest element of its image. -/
lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩
lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) :
b ≤ f a :=
Hb.2 (Hf.mem_upper_bounds_image Ha.1)
lemma le_is_glb_image_le (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) :
f a ≤ b :=
Hb.2 (Hf.mem_lower_bounds_image Ha.1)
end monotone
|
83019fb82e8102c3042d9900a6df68352441fad7 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/limits/shapes/zero_morphisms.lean | c99d2118ad41f52338b1afe22e82b590ef8343e6 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 19,975 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import data.pi.algebra
import category_theory.limits.shapes.products
import category_theory.limits.shapes.images
import category_theory.isomorphism_classes
import category_theory.limits.shapes.zero_objects
/-!
# Zero morphisms and zero objects
A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space,
and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra
structure, not merely a property.)
A category "has a zero object" if it has an object which is both initial and terminal. Having a
zero object provides zero morphisms, as the unique morphisms factoring through the zero object.
## References
* https://en.wikipedia.org/wiki/Zero_morphism
* [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2]
-/
noncomputable theory
universes v u
universes v' u'
open category_theory
open category_theory.category
open_locale classical
namespace category_theory.limits
variables (C : Type u) [category.{v} C]
variables (D : Type u') [category.{v'} D]
/-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space,
and compositions of zero morphisms with anything give the zero morphism. -/
class has_zero_morphisms :=
[has_zero : Π X Y : C, has_zero (X ⟶ Y)]
(comp_zero' : ∀ {X Y : C} (f : X ⟶ Y) (Z : C), f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) . obviously)
(zero_comp' : ∀ (X : C) {Y Z : C} (f : Y ⟶ Z), (0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) . obviously)
attribute [instance] has_zero_morphisms.has_zero
restate_axiom has_zero_morphisms.comp_zero'
restate_axiom has_zero_morphisms.zero_comp'
variables {C}
@[simp] lemma comp_zero [has_zero_morphisms C] {X Y : C} {f : X ⟶ Y} {Z : C} :
f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) := has_zero_morphisms.comp_zero f Z
@[simp] lemma zero_comp [has_zero_morphisms C] {X : C} {Y Z : C} {f : Y ⟶ Z} :
(0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) := has_zero_morphisms.zero_comp X f
instance has_zero_morphisms_pempty : has_zero_morphisms (discrete pempty) :=
{ has_zero := by tidy }
instance has_zero_morphisms_punit : has_zero_morphisms (discrete punit) :=
{ has_zero := by tidy }
namespace has_zero_morphisms
variables {C}
/-- This lemma will be immediately superseded by `ext`, below. -/
private lemma ext_aux (I J : has_zero_morphisms C)
(w : ∀ X Y : C, (@has_zero_morphisms.has_zero _ _ I X Y).zero =
(@has_zero_morphisms.has_zero _ _ J X Y).zero) : I = J :=
begin
casesI I, casesI J,
congr,
{ ext X Y,
exact w X Y },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, }
end
/--
If you're tempted to use this lemma "in the wild", you should probably
carefully consider whether you've made a mistake in allowing two
instances of `has_zero_morphisms` to exist at all.
See, particularly, the note on `zero_morphisms_of_zero_object` below.
-/
lemma ext (I J : has_zero_morphisms C) : I = J :=
begin
apply ext_aux,
intros X Y,
rw ←@has_zero_morphisms.comp_zero _ _ I X X (@has_zero_morphisms.has_zero _ _ J X X).zero,
rw @has_zero_morphisms.zero_comp _ _ J,
end
instance : subsingleton (has_zero_morphisms C) :=
⟨ext⟩
end has_zero_morphisms
open opposite has_zero_morphisms
instance has_zero_morphisms_opposite [has_zero_morphisms C] :
has_zero_morphisms Cᵒᵖ :=
{ has_zero := λ X Y, ⟨(0 : unop Y ⟶ unop X).op⟩,
comp_zero' := λ X Y f Z, congr_arg quiver.hom.op (has_zero_morphisms.zero_comp (unop Z) f.unop),
zero_comp' := λ X Y Z f, congr_arg quiver.hom.op (has_zero_morphisms.comp_zero f.unop (unop X)), }
section
variables {C} [has_zero_morphisms C]
lemma zero_of_comp_mono {X Y Z : C} {f : X ⟶ Y} (g : Y ⟶ Z) [mono g] (h : f ≫ g = 0) : f = 0 :=
by { rw [←zero_comp, cancel_mono] at h, exact h }
lemma zero_of_epi_comp {X Y Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 :=
by { rw [←comp_zero, cancel_epi] at h, exact h }
lemma eq_zero_of_image_eq_zero {X Y : C} {f : X ⟶ Y} [has_image f] (w : image.ι f = 0) : f = 0 :=
by rw [←image.fac f, w, has_zero_morphisms.comp_zero]
lemma nonzero_image_of_nonzero {X Y : C} {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : image.ι f ≠ 0 :=
λ h, w (eq_zero_of_image_eq_zero h)
end
section
variables [has_zero_morphisms D]
instance : has_zero_morphisms (C ⥤ D) :=
{ has_zero := λ F G, ⟨{ app := λ X, 0, }⟩ }
@[simp] lemma zero_app (F G : C ⥤ D) (j : C) : (0 : F ⟶ G).app j = 0 := rfl
end
namespace is_zero
variables [has_zero_morphisms C]
lemma eq_zero_of_src {X Y : C} (o : is_zero X) (f : X ⟶ Y) : f = 0 :=
o.eq_of_src _ _
lemma eq_zero_of_tgt {X Y : C} (o : is_zero Y) (f : X ⟶ Y) : f = 0 :=
o.eq_of_tgt _ _
lemma iff_id_eq_zero (X : C) : is_zero X ↔ (𝟙 X = 0) :=
⟨λ h, h.eq_of_src _ _,
λ h, ⟨
λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←id_comp f, ←id_comp default, h, zero_comp, zero_comp], }⟩⟩,
λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←comp_id f, ←comp_id default, h, comp_zero, comp_zero], }⟩⟩⟩⟩
lemma of_mono_zero (X Y : C) [mono (0 : X ⟶ Y)] : is_zero X :=
(iff_id_eq_zero X).mpr ((cancel_mono (0 : X ⟶ Y)).1 (by simp))
lemma of_epi_zero (X Y : C) [epi (0 : X ⟶ Y)] : is_zero Y :=
(iff_id_eq_zero Y).mpr ((cancel_epi (0 : X ⟶ Y)).1 (by simp))
lemma of_mono_eq_zero {X Y : C} (f : X ⟶ Y) [mono f] (h : f = 0) : is_zero X :=
by { unfreezingI { subst h, }, apply of_mono_zero X Y, }
lemma of_epi_eq_zero {X Y : C} (f : X ⟶ Y) [epi f] (h : f = 0) : is_zero Y :=
by { unfreezingI { subst h, }, apply of_epi_zero X Y, }
lemma iff_is_split_mono_eq_zero {X Y : C} (f : X ⟶ Y) [is_split_mono f] : is_zero X ↔ f = 0 :=
begin
rw iff_id_eq_zero,
split,
{ intro h, rw [←category.id_comp f, h, zero_comp], },
{ intro h, rw [←is_split_mono.id f], simp [h], },
end
lemma iff_is_split_epi_eq_zero {X Y : C} (f : X ⟶ Y) [is_split_epi f] : is_zero Y ↔ f = 0 :=
begin
rw iff_id_eq_zero,
split,
{ intro h, rw [←category.comp_id f, h, comp_zero], },
{ intro h, rw [←is_split_epi.id f], simp [h], },
end
lemma of_mono {X Y : C} (f : X ⟶ Y) [mono f] (i : is_zero Y) : is_zero X :=
begin
unfreezingI { have hf := i.eq_zero_of_tgt f, subst hf, },
exact is_zero.of_mono_zero X Y,
end
lemma of_epi {X Y : C} (f : X ⟶ Y) [epi f] (i : is_zero X) : is_zero Y :=
begin
unfreezingI { have hf := i.eq_zero_of_src f, subst hf, },
exact is_zero.of_epi_zero X Y,
end
end is_zero
/-- A category with a zero object has zero morphisms.
It is rarely a good idea to use this. Many categories that have a zero object have zero
morphisms for some other reason, for example from additivity. Library code that uses
`zero_morphisms_of_zero_object` will then be incompatible with these categories because
the `has_zero_morphisms` instances will not be definitionally equal. For this reason library
code should generally ask for an instance of `has_zero_morphisms` separately, even if it already
asks for an instance of `has_zero_objects`. -/
def is_zero.has_zero_morphisms {O : C} (hO : is_zero O) : has_zero_morphisms C :=
{ has_zero := λ X Y,
{ zero := hO.from X ≫ hO.to Y },
zero_comp' := λ X Y Z f, by { rw category.assoc, congr, apply hO.eq_of_src, },
comp_zero' := λ X Y Z f, by { rw ←category.assoc, congr, apply hO.eq_of_tgt, }}
namespace has_zero_object
variables [has_zero_object C]
open_locale zero_object
/-- A category with a zero object has zero morphisms.
It is rarely a good idea to use this. Many categories that have a zero object have zero
morphisms for some other reason, for example from additivity. Library code that uses
`zero_morphisms_of_zero_object` will then be incompatible with these categories because
the `has_zero_morphisms` instances will not be definitionally equal. For this reason library
code should generally ask for an instance of `has_zero_morphisms` separately, even if it already
asks for an instance of `has_zero_objects`. -/
def zero_morphisms_of_zero_object : has_zero_morphisms C :=
{ has_zero := λ X Y,
{ zero := (default : X ⟶ 0) ≫ default },
zero_comp' := λ X Y Z f, by { dunfold has_zero.zero, rw category.assoc, congr, },
comp_zero' := λ X Y Z f, by { dunfold has_zero.zero, rw ←category.assoc, congr, }}
section has_zero_morphisms
variables [has_zero_morphisms C]
@[simp] lemma zero_iso_is_initial_hom {X : C} (t : is_initial X) :
(zero_iso_is_initial t).hom = 0 :=
by ext
@[simp] lemma zero_iso_is_initial_inv {X : C} (t : is_initial X) :
(zero_iso_is_initial t).inv = 0 :=
by ext
@[simp] lemma zero_iso_is_terminal_hom {X : C} (t : is_terminal X) :
(zero_iso_is_terminal t).hom = 0 :=
by ext
@[simp] lemma zero_iso_is_terminal_inv {X : C} (t : is_terminal X) :
(zero_iso_is_terminal t).inv = 0 :=
by ext
@[simp] lemma zero_iso_initial_hom [has_initial C] : zero_iso_initial.hom = (0 : 0 ⟶ ⊥_ C) :=
by ext
@[simp] lemma zero_iso_initial_inv [has_initial C] : zero_iso_initial.inv = (0 : ⊥_ C ⟶ 0) :=
by ext
@[simp] lemma zero_iso_terminal_hom [has_terminal C] : zero_iso_terminal.hom = (0 : 0 ⟶ ⊤_ C) :=
by ext
@[simp] lemma zero_iso_terminal_inv [has_terminal C] : zero_iso_terminal.inv = (0 : ⊤_ C ⟶ 0) :=
by ext
end has_zero_morphisms
open_locale zero_object
instance {B : Type*} [category B] : has_zero_object (B ⥤ C) :=
(((category_theory.functor.const B).obj (0 : C)).is_zero $ λ X, is_zero_zero _).has_zero_object
end has_zero_object
open_locale zero_object
variables {D}
@[simp] lemma is_zero.map [has_zero_object D] [has_zero_morphisms D] {F : C ⥤ D} (hF : is_zero F)
{X Y : C} (f : X ⟶ Y) : F.map f = 0 :=
(hF.obj _).eq_of_src _ _
@[simp] lemma _root_.category_theory.functor.zero_obj [has_zero_object D]
(X : C) : is_zero ((0 : C ⥤ D).obj X) :=
(is_zero_zero _).obj _
@[simp] lemma _root_.category_theory.zero_map [has_zero_object D] [has_zero_morphisms D]
{X Y : C} (f : X ⟶ Y) : (0 : C ⥤ D).map f = 0 :=
(is_zero_zero _).map _
section
variables [has_zero_object C] [has_zero_morphisms C]
open_locale zero_object
@[simp]
lemma id_zero : 𝟙 (0 : C) = (0 : 0 ⟶ 0) :=
by ext
/-- An arrow ending in the zero object is zero -/
-- This can't be a `simp` lemma because the left hand side would be a metavariable.
lemma zero_of_to_zero {X : C} (f : X ⟶ 0) : f = 0 :=
by ext
lemma zero_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : f = 0 :=
begin
have h : f = f ≫ i.hom ≫ 𝟙 0 ≫ i.inv := by simp only [iso.hom_inv_id, id_comp, comp_id],
simpa using h,
end
/-- An arrow starting at the zero object is zero -/
lemma zero_of_from_zero {X : C} (f : 0 ⟶ X) : f = 0 :=
by ext
lemma zero_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : f = 0 :=
begin
have h : f = i.hom ≫ 𝟙 0 ≫ i.inv ≫ f := by simp only [iso.hom_inv_id_assoc, id_comp, comp_id],
simpa using h,
end
lemma zero_of_source_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic X 0) : f = 0 :=
zero_of_source_iso_zero f (nonempty.some i)
lemma zero_of_target_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic Y 0) : f = 0 :=
zero_of_target_iso_zero f (nonempty.some i)
lemma mono_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : mono f :=
⟨λ Z g h w, by rw [zero_of_target_iso_zero g i, zero_of_target_iso_zero h i]⟩
lemma epi_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : epi f :=
⟨λ Z g h w, by rw [zero_of_source_iso_zero g i, zero_of_source_iso_zero h i]⟩
/--
An object `X` has `𝟙 X = 0` if and only if it is isomorphic to the zero object.
Because `X ≅ 0` contains data (even if a subsingleton), we express this `↔` as an `≃`.
-/
def id_zero_equiv_iso_zero (X : C) : (𝟙 X = 0) ≃ (X ≅ 0) :=
{ to_fun := λ h, { hom := 0, inv := 0, },
inv_fun := λ i, zero_of_target_iso_zero (𝟙 X) i,
left_inv := by tidy,
right_inv := by tidy, }
@[simp]
lemma id_zero_equiv_iso_zero_apply_hom (X : C) (h : 𝟙 X = 0) :
((id_zero_equiv_iso_zero X) h).hom = 0 := rfl
@[simp]
lemma id_zero_equiv_iso_zero_apply_inv (X : C) (h : 𝟙 X = 0) :
((id_zero_equiv_iso_zero X) h).inv = 0 := rfl
/-- If `0 : X ⟶ Y` is an monomorphism, then `X ≅ 0`. -/
@[simps]
def iso_zero_of_mono_zero {X Y : C} (h : mono (0 : X ⟶ Y)) : X ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' := (cancel_mono (0 : X ⟶ Y)).mp (by simp) }
/-- If `0 : X ⟶ Y` is an epimorphism, then `Y ≅ 0`. -/
@[simps]
def iso_zero_of_epi_zero {X Y : C} (h : epi (0 : X ⟶ Y)) : Y ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' := (cancel_epi (0 : X ⟶ Y)).mp (by simp) }
/-- If a monomorphism out of `X` is zero, then `X ≅ 0`. -/
def iso_zero_of_mono_eq_zero {X Y : C} {f : X ⟶ Y} [mono f] (h : f = 0) : X ≅ 0 :=
by { unfreezingI { subst h, }, apply iso_zero_of_mono_zero ‹_›, }
/-- If an epimorphism in to `Y` is zero, then `Y ≅ 0`. -/
def iso_zero_of_epi_eq_zero {X Y : C} {f : X ⟶ Y} [epi f] (h : f = 0) : Y ≅ 0 :=
by { unfreezingI { subst h, }, apply iso_zero_of_epi_zero ‹_›, }
/-- If an object `X` is isomorphic to 0, there's no need to use choice to construct
an explicit isomorphism: the zero morphism suffices. -/
def iso_of_is_isomorphic_zero {X : C} (P : is_isomorphic X 0) : X ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' :=
begin
casesI P,
rw ←P.hom_inv_id,
rw ←category.id_comp P.inv,
simp,
end,
inv_hom_id' := by simp, }
end
section is_iso
variables [has_zero_morphisms C]
/--
A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if
the identities on both `X` and `Y` are zero.
-/
@[simps]
def is_iso_zero_equiv (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (𝟙 X = 0 ∧ 𝟙 Y = 0) :=
{ to_fun := by { introsI i, rw ←is_iso.hom_inv_id (0 : X ⟶ Y),
rw ←is_iso.inv_hom_id (0 : X ⟶ Y), simp },
inv_fun := λ h, ⟨⟨(0 : Y ⟶ X), by tidy⟩⟩,
left_inv := by tidy,
right_inv := by tidy, }
/--
A zero morphism `0 : X ⟶ X` is an isomorphism if and only if
the identity on `X` is zero.
-/
def is_iso_zero_self_equiv (X : C) : is_iso (0 : X ⟶ X) ≃ (𝟙 X = 0) :=
by simpa using is_iso_zero_equiv X X
variables [has_zero_object C]
open_locale zero_object
/--
A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if
`X` and `Y` are isomorphic to the zero object.
-/
def is_iso_zero_equiv_iso_zero (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (X ≅ 0) × (Y ≅ 0) :=
begin
-- This is lame, because `prod` can't cope with `Prop`, so we can't use `equiv.prod_congr`.
refine (is_iso_zero_equiv X Y).trans _,
symmetry,
fsplit,
{ rintros ⟨eX, eY⟩, fsplit,
exact (id_zero_equiv_iso_zero X).symm eX,
exact (id_zero_equiv_iso_zero Y).symm eY, },
{ rintros ⟨hX, hY⟩, fsplit,
exact (id_zero_equiv_iso_zero X) hX,
exact (id_zero_equiv_iso_zero Y) hY, },
{ tidy, },
{ tidy, },
end
lemma is_iso_of_source_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) (j : Y ≅ 0) : is_iso f :=
begin
rw zero_of_source_iso_zero f i,
exact (is_iso_zero_equiv_iso_zero _ _).inv_fun ⟨i, j⟩,
end
/--
A zero morphism `0 : X ⟶ X` is an isomorphism if and only if
`X` is isomorphic to the zero object.
-/
def is_iso_zero_self_equiv_iso_zero (X : C) : is_iso (0 : X ⟶ X) ≃ (X ≅ 0) :=
(is_iso_zero_equiv_iso_zero X X).trans subsingleton_prod_self_equiv
end is_iso
/-- If there are zero morphisms, any initial object is a zero object. -/
lemma has_zero_object_of_has_initial_object
[has_zero_morphisms C] [has_initial C] : has_zero_object C :=
begin
refine ⟨⟨⊥_ C, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩⟩,
calc
f = f ≫ 𝟙 _ : (category.comp_id _).symm
... = f ≫ 0 : by congr
... = 0 : has_zero_morphisms.comp_zero _ _
end
/-- If there are zero morphisms, any terminal object is a zero object. -/
lemma has_zero_object_of_has_terminal_object
[has_zero_morphisms C] [has_terminal C] : has_zero_object C :=
begin
refine ⟨⟨⊤_ C, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩⟩⟩,
calc
f = 𝟙 _ ≫ f : (category.id_comp _).symm
... = 0 ≫ f : by congr
... = 0 : zero_comp
end
section image
variable [has_zero_morphisms C]
lemma image_ι_comp_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f]
[epi (factor_thru_image f)] (h : f ≫ g = 0) : image.ι f ≫ g = 0 :=
zero_of_epi_comp (factor_thru_image f) $ by simp [h]
lemma comp_factor_thru_image_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image g]
(h : f ≫ g = 0) : f ≫ factor_thru_image g = 0 :=
zero_of_comp_mono (image.ι g) $ by simp [h]
variables [has_zero_object C]
open_locale zero_object
/--
The zero morphism has a `mono_factorisation` through the zero object.
-/
@[simps]
def mono_factorisation_zero (X Y : C) : mono_factorisation (0 : X ⟶ Y) :=
{ I := 0, m := 0, e := 0, }
/--
The factorisation through the zero object is an image factorisation.
-/
def image_factorisation_zero (X Y : C) : image_factorisation (0 : X ⟶ Y) :=
{ F := mono_factorisation_zero X Y,
is_image := { lift := λ F', 0 } }
instance has_image_zero {X Y : C} : has_image (0 : X ⟶ Y) :=
has_image.mk $ image_factorisation_zero _ _
/-- The image of a zero morphism is the zero object. -/
def image_zero {X Y : C} : image (0 : X ⟶ Y) ≅ 0 :=
is_image.iso_ext (image.is_image (0 : X ⟶ Y)) (image_factorisation_zero X Y).is_image
/-- The image of a morphism which is equal to zero is the zero object. -/
def image_zero' {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image f ≅ 0 :=
image.eq_to_iso h ≪≫ image_zero
@[simp]
lemma image.ι_zero {X Y : C} [has_image (0 : X ⟶ Y)] : image.ι (0 : X ⟶ Y) = 0 :=
begin
rw ←image.lift_fac (mono_factorisation_zero X Y),
simp,
end
/--
If we know `f = 0`,
it requires a little work to conclude `image.ι f = 0`,
because `f = g` only implies `image f ≅ image g`.
-/
@[simp]
lemma image.ι_zero' [has_equalizers C] {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] :
image.ι f = 0 :=
by { rw image.eq_fac h, simp }
end image
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_sigma_ι {β : Type u'} [has_zero_morphisms C] (f : β → C)
[has_colimit (discrete.functor f)] (b : β) : is_split_mono (sigma.ι f b) := is_split_mono.mk'
{ retraction := sigma.desc $ pi.single b (𝟙 _) }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_pi_π {β : Type u'} [has_zero_morphisms C] (f : β → C)
[has_limit (discrete.functor f)] (b : β) : is_split_epi (pi.π f b) := is_split_epi.mk'
{ section_ := pi.lift $ pi.single b (𝟙 _) }
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_coprod_inl [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] :
is_split_mono (coprod.inl : X ⟶ X ⨿ Y) := is_split_mono.mk'
{ retraction := coprod.desc (𝟙 X) 0, }
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_coprod_inr [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] :
is_split_mono (coprod.inr : Y ⟶ X ⨿ Y) := is_split_mono.mk'
{ retraction := coprod.desc 0 (𝟙 Y), }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_prod_fst [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] :
is_split_epi (prod.fst : X ⨯ Y ⟶ X) := is_split_epi.mk'
{ section_ := prod.lift (𝟙 X) 0, }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_prod_snd [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] :
is_split_epi (prod.snd : X ⨯ Y ⟶ Y) := is_split_epi.mk'
{ section_ := prod.lift 0 (𝟙 Y), }
end category_theory.limits
|
1eb1affe430ae7bff5d7d0070434e0016c94b905 | 7cdf3413c097e5d36492d12cdd07030eb991d394 | /src/game/world5/level6.lean | c76f3dbcca5b31426f66b9dff0633256f7d29cbf | [] | no_license | alreadydone/natural_number_game | 3135b9385a9f43e74cfbf79513fc37e69b99e0b3 | 1a39e693df4f4e871eb449890d3c7715a25c2ec9 | refs/heads/master | 1,599,387,390,105 | 1,573,200,587,000 | 1,573,200,691,000 | 220,397,084 | 0 | 0 | null | 1,573,192,734,000 | 1,573,192,733,000 | null | UTF-8 | Lean | false | false | 1,560 | lean | /-
# Function world.
## Level 6 : `(P → (Q → R)) → ((P → Q) → (P → R))`.
You can solve this level completely just using `intro`, `apply` and `exact`,
but if you want to argue forwards instead of backwards then don't forget
that you can do things like
`have j : Q → R := f p,`
if `f : P → (Q → R)` and `p : P`. Remember the trick with the colon in `have`:
we could just write `have j := f p,` but this way we can be sure that `j` is
what we actually expect it to be.
I recommend that you start with `intro f` rather than `intro p`
because even though the goal starts `P → ...`, the brackets mean that
the goal is not a function from `P` to anything, it's a function from
`P → (Q → R)` to something. In fact I'd recommend that you started
with `intros f h p`, which introduces three variables at once.
You then find that your your goal is `⊢ R`. If you try `have j : Q → R := f p`
now then you can `apply j`. Alternatively you can `apply (f p)` directly.
What happens if you just try `apply f`? Can you figure out what just happened? This is a little
`apply` easter egg. Why is it mathematically valid?
-/
/- Lemma : no-side-bar
Whatever the sets $P$ and $Q$ and $R$ are, we can always
make an element of $\operatorname{Hom}(\operatorname{Hom}(P,\operatorname{Hom}(Q,R)),
\operatorname{Hom}(\operatorname{Hom}(P,Q),\operatorname{Hom}(P,R)))$.
-/
example (P Q R : Type) : (P → (Q → R)) → ((P → Q) → (P → R)) :=
begin
intro f,
intro h,
intro p,
have j : Q → R := f p,
apply j,
apply h,
exact p,
end
|
22295b49125afce8dd27c3825c1b28ae2db5f975 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/986.lean | 1448c57b97990c490750baaff631d92e27560209 | [
"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 | 46 | lean | attribute [simp] Array.insertionSort.swapLoop
|
d9f585eef23c8121d2f11c2c338016e7cfa6c958 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/tactic/slice.lean | 901ac12dd6cfbcf456a0ac023628543ca69e59dc | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,495 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.category.basic
open category_theory
-- TODO someone might like to generalise this tactic to work with other associative structures.
namespace tactic
meta def repeat_with_results {α : Type} (t : tactic α) : tactic (list α) :=
(do r ← t,
s ← repeat_with_results,
return (r :: s)) <|> return []
meta def repeat_count {α : Type} (t : tactic α) : tactic ℕ :=
do r ← repeat_with_results t,
return r.length
end tactic
namespace conv
open tactic
meta def repeat_with_results {α : Type} (t : tactic α) : tactic (list α) :=
(do r ← t,
s ← repeat_with_results,
return (r :: s)) <|> return []
meta def repeat_count {α : Type} (t : tactic α) : tactic ℕ :=
do r ← repeat_with_results t,
return r.length
meta def slice (a b : ℕ) : conv unit :=
do repeat $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=ff},
iterate_range (a-1) (a-1) (do conv.congr, conv.skip),
k ← repeat_count $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=tt},
iterate_range (k+1+a-b) (k+1+a-b) conv.congr,
repeat $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=ff},
rotate 1,
iterate_exactly' (k+1+a-b) conv.skip
meta def slice_lhs (a b : ℕ) (t : conv unit) : tactic unit :=
do conv.interactive.to_lhs,
slice a b,
t
meta def slice_rhs (a b : ℕ) (t : conv unit) : tactic unit :=
do conv.interactive.to_rhs,
slice a b,
t
namespace interactive
/--
`slice` is a conv tactic; if the current focus is a composition of several morphisms,
`slice a b` reassociates as needed, and zooms in on the `a`-th through `b`-th morphisms.
Thus if the current focus is `(a ≫ b) ≫ ((c ≫ d) ≫ e)`, then `slice 2 3` zooms to `b ≫ c`.
-/
meta def slice := conv.slice
end interactive
end conv
namespace tactic
open conv
private meta def conv_target' (c : conv unit) : tactic unit :=
do t ← target,
(new_t, pr) ← c.convert t,
replace_target new_t pr,
try tactic.triv, try (tactic.reflexivity reducible)
namespace interactive
setup_tactic_parser
/--
`slice_lhs a b { tac }` zooms to the left hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
meta def slice_lhs (a b : parse small_nat) (t : conv.interactive.itactic) : tactic unit :=
do conv_target' (conv.interactive.to_lhs >> slice a b >> t)
/--
`slice_rhs a b { tac }` zooms to the right hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
meta def slice_rhs (a b : parse small_nat) (t : conv.interactive.itactic) : tactic unit :=
do conv_target' (conv.interactive.to_rhs >> slice a b >> t)
end interactive
end tactic
/--
`slice_lhs a b { tac }` zooms to the left hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
`slice_rhs a b { tac }` zooms to the right hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
add_tactic_doc
{ name := "slice",
category := doc_category.tactic,
decl_names := [`tactic.interactive.slice_lhs, `tactic.interactive.slice_rhs],
tags := ["category theory"] }
|
0e6fcaec90886abbac686620a08dc472eb905d72 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/clifford_algebra/even.lean | e977571460879a33570a4a97087c06e615d7a1e0 | [
"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 | 10,211 | lean | /-
Copyright (c) 2022 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import linear_algebra.clifford_algebra.fold
import linear_algebra.clifford_algebra.grading
/-!
# The universal property of the even subalgebra
## Main definitions
* `clifford_algebra.even Q`: The even subalgebra of `clifford_algebra Q`.
* `clifford_algebra.even_hom`: The type of bilinear maps that satisfy the universal property of the
even subalgebra
* `clifford_algebra.even.lift`: The universal property of the even subalgebra, which states
that every bilinear map `f` with `f v v = Q v` and `f u v * f v w = Q v • f u w` is in unique
correspondence with an algebra morphism from `clifford_algebra.even Q`.
## Implementation notes
The approach here is outlined in "Computing with the universal properties of the Clifford algebra
and the even subalgebra" (to appear).
The broad summary is that we have two tricks available to us for implementing complex recursors on
top of `clifford_algebra.lift`: the first is to use morphisms as the output type, such as
`A = module.End R N` which is how we obtained `clifford_algebra.foldr`; and the second is to use
`N = (N', S)` where `N'` is the value we wish to compute, and `S` is some auxiliary state passed
between one recursor invocation and the next.
For the universal property of the even subalgebra, we apply a variant of the first trick again by
choosing `S` to itself be a submodule of morphisms.
-/
namespace clifford_algebra
variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M]
variables {Q : quadratic_form R M}
-- put this after `Q` since we want to talk about morphisms from `clifford_algebra Q` to `A` and
-- that order is more natural
variables {A B : Type*} [ring A] [ring B] [algebra R A] [algebra R B]
open_locale direct_sum
variables (Q)
/-- The even submodule `clifford_algebra.even_odd Q 0` is also a subalgebra. -/
def even : subalgebra R (clifford_algebra Q) :=
(even_odd Q 0).to_subalgebra
set_like.graded_monoid.one_mem
(λ x y hx hy, add_zero (0 : zmod 2) ▸ set_like.graded_monoid.mul_mem hx hy)
@[simp] lemma even_to_submodule : (even Q).to_submodule = even_odd Q 0 :=
rfl
variables (A)
/-- The type of bilinear maps which are accepted by `clifford_algebra.even.lift`. -/
@[ext]
structure even_hom : Type (max u_2 u_3) :=
(bilin : M →ₗ[R] M →ₗ[R] A)
(contract (m : M) : bilin m m = algebra_map R A (Q m))
(contract_mid (m₁ m₂ m₃ : M) : bilin m₁ m₂ * bilin m₂ m₃ = Q m₂ • bilin m₁ m₃)
variables {A Q}
/-- Compose an `even_hom` with an `alg_hom` on the output. -/
@[simps]
def even_hom.compr₂ (g : even_hom Q A) (f : A →ₐ[R] B) : even_hom Q B :=
{ bilin := g.bilin.compr₂ f.to_linear_map,
contract := λ m, (f.congr_arg $ g.contract _).trans $ f.commutes _,
contract_mid := λ m₁ m₂ m₃, (f.map_mul _ _).symm.trans $
(f.congr_arg $ g.contract_mid _ _ _).trans $ f.map_smul _ _ }
variables (Q)
/-- The embedding of pairs of vectors into the even subalgebra, as a bilinear map. -/
@[simps bilin_apply_apply_coe]
def even.ι : even_hom Q (even Q) :=
{ bilin := linear_map.mk₂ R (λ m₁ m₂, ⟨ι Q m₁ * ι Q m₂, ι_mul_ι_mem_even_odd_zero _ _ _⟩)
(λ _ _ _, by { simp only [linear_map.map_add, add_mul], refl })
(λ _ _ _, by { simp only [linear_map.map_smul, smul_mul_assoc], refl })
(λ _ _ _, by { simp only [linear_map.map_add, mul_add], refl })
(λ _ _ _, by { simp only [linear_map.map_smul, mul_smul_comm], refl }),
contract := λ m, subtype.ext $ ι_sq_scalar Q m,
contract_mid := λ m₁ m₂ m₃, subtype.ext $
calc ι Q m₁ * ι Q m₂ * (ι Q m₂ * ι Q m₃)
= ι Q m₁ * ((ι Q m₂ * ι Q m₂) * ι Q m₃) : by simp only [mul_assoc]
... = Q m₂ • (ι Q m₁ * ι Q m₃) : by rw [algebra.smul_def, ι_sq_scalar, algebra.left_comm] }
instance : inhabited (even_hom Q (even Q)) := ⟨even.ι Q⟩
variables (f : even_hom Q A)
/-- Two algebra morphisms from the even subalgebra are equal if they agree on pairs of generators.
See note [partially-applied ext lemmas]. -/
@[ext]
lemma even.alg_hom_ext ⦃f g : even Q →ₐ[R] A⦄
(h : (even.ι Q).compr₂ f = (even.ι Q).compr₂ g) :
f = g :=
begin
rw even_hom.ext_iff at h,
ext ⟨x, hx⟩,
refine even_induction _ _ _ _ _ hx,
{ intro r,
exact (f.commutes r).trans (g.commutes r).symm },
{ intros x y hx hy ihx ihy,
have := congr_arg2 (+) ihx ihy,
exact (f.map_add _ _).trans (this.trans $ (g.map_add _ _).symm) },
{ intros m₁ m₂ x hx ih,
have := congr_arg2 (*) (linear_map.congr_fun (linear_map.congr_fun h m₁) m₂) ih,
exact (f.map_mul _ _).trans (this.trans $ (g.map_mul _ _).symm) },
end
variables {Q}
namespace even.lift
/-- An auxiliary submodule used to store the half-applied values of `f`.
This is the span of elements `f'` such that `∃ x m₂, ∀ m₁, f' m₁ = f m₁ m₂ * x`. -/
private def S : submodule R (M →ₗ[R] A) :=
submodule.span R
{f' | ∃ x m₂, f' = linear_map.lcomp R _ (f.bilin.flip m₂) (linear_map.mul_right R x)}
/-- An auxiliary bilinear map that is later passed into `clifford_algebra.fold`. Our desired result
is stored in the `A` part of the accumulator, while auxiliary recursion state is stored in the `S f`
part. -/
private def f_fold : M →ₗ[R] (A × S f) →ₗ[R] (A × S f) :=
linear_map.mk₂ R (λ m acc,
/- We could write this `snd` term in a point-free style as follows, but it wouldn't help as we
don't have any prod or subtype combinators to deal with n-linear maps of this degree.
```lean
(linear_map.lcomp R _ (algebra.lmul R A).to_linear_map.flip).comp $
(linear_map.llcomp R M A A).flip.comp f.flip : M →ₗ[R] A →ₗ[R] M →ₗ[R] A)
```
-/
(acc.2 m, ⟨(linear_map.mul_right R acc.1).comp (f.bilin.flip m),
submodule.subset_span $ ⟨_, _, rfl⟩⟩))
(λ m₁ m₂ a, prod.ext
(linear_map.map_add _ m₁ m₂)
(subtype.ext $ linear_map.ext $ λ m₃,
show f.bilin m₃ (m₁ + m₂) * a.1 = f.bilin m₃ m₁ * a.1 + f.bilin m₃ m₂ * a.1,
by rw [map_add, add_mul]))
(λ c m a, prod.ext
(linear_map.map_smul _ c m)
(subtype.ext $ linear_map.ext $ λ m₃,
show f.bilin m₃ (c • m) * a.1 = c • (f.bilin m₃ m * a.1),
by rw [linear_map.map_smul, smul_mul_assoc]))
(λ m a₁ a₂, prod.ext rfl (subtype.ext $ linear_map.ext $ λ m₃, mul_add _ _ _))
(λ c m a, prod.ext rfl (subtype.ext $ linear_map.ext $ λ m₃, mul_smul_comm _ _ _))
@[simp] private lemma fst_f_fold_f_fold (m₁ m₂ : M) (x : A × S f) :
(f_fold f m₁ (f_fold f m₂ x)).fst = f.bilin m₁ m₂ * x.fst := rfl
@[simp] private lemma snd_f_fold_f_fold (m₁ m₂ m₃ : M) (x : A × S f) :
((f_fold f m₁ (f_fold f m₂ x)).snd : M →ₗ[R] A) m₃ = f.bilin m₃ m₁ * (x.snd : M →ₗ[R] A) m₂ := rfl
private lemma f_fold_f_fold (m : M) (x : A × S f) :
f_fold f m (f_fold f m x) = Q m • x :=
begin
obtain ⟨a, ⟨g, hg⟩⟩ := x,
ext : 2,
{ change f.bilin m m * a = Q m • a,
rw [algebra.smul_def, f.contract] },
{ ext m₁,
change f.bilin _ _ * g m = Q m • g m₁,
apply submodule.span_induction' _ _ _ _ hg,
{ rintros _ ⟨b, m₃, rfl⟩,
change f.bilin _ _ * (f.bilin _ _ * b) = Q m • (f.bilin _ _ * b),
rw [←smul_mul_assoc, ←mul_assoc, f.contract_mid] },
{ change f.bilin m₁ m * 0 = Q m • 0,
rw [mul_zero, smul_zero] },
{ rintros x hx y hy ihx ihy,
rw [linear_map.add_apply, linear_map.add_apply, mul_add, smul_add, ihx, ihy] },
{ rintros x hx c ihx,
rw [linear_map.smul_apply, linear_map.smul_apply, mul_smul_comm, ihx, smul_comm] } },
end
/-- The final auxiliary construction for `clifford_algebra.even.lift`. This map is the forwards
direction of that equivalence, but not in the fully-bundled form. -/
@[simps apply {attrs := []}] def aux (f : even_hom Q A) : clifford_algebra.even Q →ₗ[R] A :=
begin
refine _ ∘ₗ (even Q).val.to_linear_map,
exact linear_map.fst _ _ _ ∘ₗ foldr Q (f_fold f) (f_fold_f_fold f) (1, 0),
end
@[simp] lemma aux_one : aux f 1 = 1 :=
(congr_arg prod.fst (foldr_one _ _ _ _))
@[simp] lemma aux_ι (m₁ m₂ : M) : aux f ((even.ι Q).bilin m₁ m₂) = f.bilin m₁ m₂ :=
(congr_arg prod.fst (foldr_mul _ _ _ _ _ _)).trans begin
rw [foldr_ι, foldr_ι],
exact mul_one _,
end
@[simp] lemma aux_algebra_map (r) (hr) :
aux f ⟨algebra_map R _ r, hr⟩ = algebra_map R _ r :=
(congr_arg prod.fst (foldr_algebra_map _ _ _ _ _)).trans (algebra.algebra_map_eq_smul_one r).symm
@[simp] lemma aux_mul (x y : even Q) :
aux f (x * y) = aux f x * aux f y :=
begin
cases x,
cases y,
refine (congr_arg prod.fst (foldr_mul _ _ _ _ _ _)).trans _,
dsimp only,
refine even_induction Q _ _ _ _ x_property,
{ intros r,
rw [foldr_algebra_map, aux_algebra_map],
exact (algebra.smul_def r _), },
{ intros x y hx hy ihx ihy,
rw [linear_map.map_add, prod.fst_add, ihx, ihy, ←add_mul, ←linear_map.map_add],
refl, },
{ rintros m₁ m₂ x (hx : x ∈ even Q) ih,
rw [aux_apply, foldr_mul, foldr_mul, foldr_ι, foldr_ι, fst_f_fold_f_fold, ih,
←mul_assoc, subtype.coe_mk, foldr_mul, foldr_mul, foldr_ι, foldr_ι, fst_f_fold_f_fold],
refl }
end
end even.lift
open even.lift
variables (Q) {A}
/-- Every algebra morphism from the even subalgebra is in one-to-one correspondence with a
bilinear map that sends duplicate arguments to the quadratic form, and contracts across
multiplication. -/
@[simps symm_apply_bilin]
def even.lift : even_hom Q A ≃ (clifford_algebra.even Q →ₐ[R] A) :=
{ to_fun := λ f, alg_hom.of_linear_map (aux f) (aux_one f) (aux_mul f),
inv_fun := λ F, (even.ι Q).compr₂ F,
left_inv := λ f, even_hom.ext _ _ $ linear_map.ext₂ $ even.lift.aux_ι f,
right_inv := λ F, even.alg_hom_ext Q $ even_hom.ext _ _ $ linear_map.ext₂ $ even.lift.aux_ι _ }
@[simp] lemma even.lift_ι (f : even_hom Q A) (m₁ m₂ : M) :
even.lift Q f ((even.ι Q).bilin m₁ m₂) = f.bilin m₁ m₂ :=
even.lift.aux_ι _ _ _
end clifford_algebra
|
fcb06c4d642daf4ac4181b2b66c16bc7c787d273 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/analysis/normed_space/hahn_banach.lean | 44302fc42f8a8c61abc12931d403aa8bfac016e2 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 7,251 | lean | /-
Copyright (c) 2020 Yury Kudryashov All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Heather Macbeth
-/
import analysis.normed_space.operator_norm
import analysis.normed_space.extend
import analysis.convex.cone
import data.complex.is_R_or_C
/-!
# Hahn-Banach theorem
In this file we prove a version of Hahn-Banach theorem for continuous linear
functions on normed spaces over `ℝ` and `ℂ`.
In order to state and prove its corollaries uniformly, we prove the statements for a field `𝕜`
satisfying `is_R_or_C 𝕜`.
In this setting, `exists_dual_vector` states that, for any nonzero `x`, there exists a continuous
linear form `g` of norm `1` with `g x = ∥x∥` (where the norm has to be interpreted as an element
of `𝕜`).
-/
universes u v
/--
The norm of `x` as an element of `𝕜` (a normed algebra over `ℝ`). This is needed in particular to
state equalities of the form `g x = norm' 𝕜 x` when `g` is a linear function.
For the concrete cases of `ℝ` and `ℂ`, this is just `∥x∥` and `↑∥x∥`, respectively.
-/
noncomputable def norm' (𝕜 : Type*) [nondiscrete_normed_field 𝕜] [semi_normed_algebra ℝ 𝕜]
{E : Type*} [semi_normed_group E] (x : E) : 𝕜 :=
algebra_map ℝ 𝕜 ∥x∥
lemma norm'_def (𝕜 : Type*) [nondiscrete_normed_field 𝕜] [semi_normed_algebra ℝ 𝕜]
{E : Type*} [semi_normed_group E] (x : E) :
norm' 𝕜 x = (algebra_map ℝ 𝕜 ∥x∥) := rfl
lemma norm_norm'
(𝕜 : Type*) [nondiscrete_normed_field 𝕜] [semi_normed_algebra ℝ 𝕜]
(A : Type*) [semi_normed_group A]
(x : A) : ∥norm' 𝕜 x∥ = ∥x∥ :=
by rw [norm'_def, norm_algebra_map_eq, norm_norm]
@[simp] lemma norm'_eq_zero_iff
(𝕜 : Type*) [nondiscrete_normed_field 𝕜] [semi_normed_algebra ℝ 𝕜]
(A : Type*) [normed_group A] (x : A) :
norm' 𝕜 x = 0 ↔ x = 0 :=
by simp [norm', ← norm_eq_zero, norm_algebra_map_eq]
namespace real
variables {E : Type*} [semi_normed_group E] [semi_normed_space ℝ E]
/-- Hahn-Banach theorem for continuous linear functions over `ℝ`. -/
theorem exists_extension_norm_eq (p : subspace ℝ E) (f : p →L[ℝ] ℝ) :
∃ g : E →L[ℝ] ℝ, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ :=
begin
rcases exists_extension_of_le_sublinear ⟨p, f⟩ (λ x, ∥f∥ * ∥x∥)
(λ c hc x, by simp only [norm_smul c x, real.norm_eq_abs, abs_of_pos hc, mul_left_comm])
(λ x y, _) (λ x, le_trans (le_abs_self _) (f.le_op_norm _))
with ⟨g, g_eq, g_le⟩,
set g' := g.mk_continuous (∥f∥)
(λ x, abs_le.2 ⟨neg_le.1 $ g.map_neg x ▸ norm_neg x ▸ g_le (-x), g_le x⟩),
{ refine ⟨g', g_eq, _⟩,
{ apply le_antisymm (g.mk_continuous_norm_le (norm_nonneg f) _),
refine f.op_norm_le_bound (norm_nonneg _) (λ x, _),
dsimp at g_eq,
rw ← g_eq,
apply g'.le_op_norm } },
{ simp only [← mul_add],
exact mul_le_mul_of_nonneg_left (norm_add_le x y) (norm_nonneg f) }
end
end real
section is_R_or_C
open is_R_or_C
variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [semi_normed_group F] [semi_normed_space 𝕜 F]
/-- Hahn-Banach theorem for continuous linear functions over `𝕜` satisyfing `is_R_or_C 𝕜`. -/
theorem exists_extension_norm_eq (p : subspace 𝕜 F) (f : p →L[𝕜] 𝕜) :
∃ g : F →L[𝕜] 𝕜, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ :=
begin
letI : module ℝ F := restrict_scalars.module ℝ 𝕜 F,
letI : is_scalar_tower ℝ 𝕜 F := restrict_scalars.is_scalar_tower _ _ _,
letI : semi_normed_space ℝ F := semi_normed_space.restrict_scalars _ 𝕜 _,
-- Let `fr: p →L[ℝ] ℝ` be the real part of `f`.
let fr := re_clm.comp (f.restrict_scalars ℝ),
have fr_apply : ∀ x, fr x = re (f x), by { assume x, refl },
-- Use the real version to get a norm-preserving extension of `fr`, which
-- we'll call `g : F →L[ℝ] ℝ`.
rcases real.exists_extension_norm_eq (p.restrict_scalars ℝ) fr with ⟨g, ⟨hextends, hnormeq⟩⟩,
-- Now `g` can be extended to the `F →L[𝕜] 𝕜` we need.
refine ⟨g.extend_to_𝕜, _⟩,
-- It is an extension of `f`.
have h : ∀ x : p, g.extend_to_𝕜 x = f x,
{ assume x,
rw [continuous_linear_map.extend_to_𝕜_apply, ←submodule.coe_smul, hextends, hextends],
have : (fr x : 𝕜) - I * ↑(fr (I • x)) = (re (f x) : 𝕜) - (I : 𝕜) * (re (f ((I : 𝕜) • x))),
by refl,
rw this,
apply ext,
{ simp only [add_zero, algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add,
zero_sub, I_im', zero_mul, of_real_re, eq_self_iff_true, sub_zero, mul_neg_eq_neg_mul_symm,
of_real_neg, mul_re, mul_zero, sub_neg_eq_add, continuous_linear_map.map_smul] },
{ simp only [algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add, zero_sub, I_im',
zero_mul, of_real_re, mul_neg_eq_neg_mul_symm, mul_im, zero_add, of_real_neg, mul_re,
sub_neg_eq_add, continuous_linear_map.map_smul] } },
-- And we derive the equality of the norms by bounding on both sides.
refine ⟨h, le_antisymm _ _⟩,
{ calc ∥g.extend_to_𝕜∥
≤ ∥g∥ : g.extend_to_𝕜.op_norm_le_bound g.op_norm_nonneg (norm_bound _)
... = ∥fr∥ : hnormeq
... ≤ ∥re_clm∥ * ∥f∥ : continuous_linear_map.op_norm_comp_le _ _
... = ∥f∥ : by rw [re_clm_norm, one_mul] },
{ exact f.op_norm_le_bound g.extend_to_𝕜.op_norm_nonneg (λ x, h x ▸ g.extend_to_𝕜.le_op_norm x) }
end
end is_R_or_C
section dual_vector
variables (𝕜 : Type v) [is_R_or_C 𝕜]
variables {E : Type u} [normed_group E] [normed_space 𝕜 E]
open continuous_linear_equiv submodule
open_locale classical
lemma coord_norm' (x : E) (h : x ≠ 0) : ∥norm' 𝕜 x • coord 𝕜 x h∥ = 1 :=
by rw [norm_smul, norm_norm', coord_norm, mul_inv_cancel (mt norm_eq_zero.mp h)]
/-- Corollary of Hahn-Banach. Given a nonzero element `x` of a normed space, there exists an
element of the dual space, of norm `1`, whose value on `x` is `∥x∥`. -/
theorem exists_dual_vector (x : E) (h : x ≠ 0) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = norm' 𝕜 x :=
begin
let p : submodule 𝕜 E := 𝕜 ∙ x,
let f := norm' 𝕜 x • coord 𝕜 x h,
obtain ⟨g, hg⟩ := exists_extension_norm_eq p f,
refine ⟨g, _, _⟩,
{ rw [hg.2, coord_norm'] },
{ calc g x = g (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw coe_mk
... = (norm' 𝕜 x • coord 𝕜 x h) (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw ← hg.1
... = norm' 𝕜 x : by simp }
end
/-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, and choosing
the dual element arbitrarily when `x = 0`. -/
theorem exists_dual_vector' [nontrivial E] (x : E) :
∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = norm' 𝕜 x :=
begin
by_cases hx : x = 0,
{ obtain ⟨y, hy⟩ := exists_ne (0 : E),
obtain ⟨g, hg⟩ : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g y = norm' 𝕜 y := exists_dual_vector 𝕜 y hy,
refine ⟨g, hg.left, _⟩,
rw [norm'_def, hx, norm_zero, ring_hom.map_zero, continuous_linear_map.map_zero] },
{ exact exists_dual_vector 𝕜 x hx }
end
end dual_vector
|
0ec60a95b3cd4895ec4a29e586d002475775f1bd | 892c0ca8b8ddfe17a2c64fb7b721f9e2d9287539 | /soundness.lean | 8d67dee1124238349da8fb0c59935a823f2b7122 | [] | no_license | ssomayyajula/stlc | f7ef96de541f9519b00912337332366317282dce | cf92bf387b4418f9a6261c7ea4876db4e4280dd2 | refs/heads/master | 1,630,729,373,254 | 1,516,045,347,000 | 1,516,045,347,000 | 115,835,353 | 5 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,682 | lean | import .typing .semantics
lemma progress {e : term} {τ : type} : has_type e τ → is_value e ∨ ∃ e', e ⇝ e' :=
begin
unfold has_type,
generalize g : @emptyf var type = Γ,
intro h,
induction h,
-- Impossible, variables cannot be typed in ∅
case has_type_under.var _ _ _ h
{ rw ←g at h, contradiction },
-- Unit and abstractions are trivially values
case has_type_under.unit
{ apply or.inl, apply is_value.unit },
case has_type_under.abs
{ apply or.inl, apply is_value.abs },
-- Applications always evaluate
case has_type_under.app _ e₁ e₂ _ _ te₁ _ ih₁ ih₂
{ apply or.inr,
-- Apply the inductive hypothesis on e₁
cases (ih₁ g).symm with e₁_steps e₁_iv,
-- If the LHS steps, apply CONTEXT
cases e₁_steps with e₁',
existsi term.app e₁' e₂,
let E := E.app_left E.hole e₂,
rw [show term.app e₁ e₂ = E e₁, from rfl,
show term.app e₁' e₂ = E e₁', from rfl],
apply step.context, assumption,
-- Apply the inductive hypothesis on e₂
cases (ih₂ g).symm with e₂_steps,
-- If the RHS steps, apply CONTEXT
cases e₂_steps with e₂',
existsi term.app e₁ e₂',
let E := E.app_right ⟨e₁, e₁_iv⟩ E.hole,
rw [show term.app e₁ e₂ = E e₂, from rfl,
show term.app e₁ e₂' = E e₂', from rfl],
apply step.context, assumption,
-- If both are values, then it β-reduces
cases e₁_iv with x _ e,
cases subst e e₂ x with e',
existsi e',
apply step.beta, repeat { assumption },
-- Discard superfluous goals
cases te₁ }
end
lemma has_type_halts_is_value {e : term} {τ : type} : has_type e τ → halts e → is_value e :=
or.resolve_right ∘ progress
lemma ctx_invar {e : term} {τ : type} {Γ : ctx} :
has_type e τ → ∀ Γ, has_type_under Γ e τ := sorry
-- See technical note in Programming Languages Foundations
-- for why we induct on e as opposed to x:τ' |- e:τ
lemma subst_lemma {x : var} {e e' es : term} {τ τ' : type} {Γ : ctx} :
has_type_under (extend Γ x τ') e τ → has_type e' τ' → is_subst es e e' x → has_type_under Γ es τ :=
begin
intros te te' is,
induction e generalizing Γ τ es,
case term.unit
{ cases te, cases is,
apply has_type_under.unit },
case term.var
{ cases te,
case has_type_under.var h
{ cases is,
case is_subst.same_var
{ rw extend_same at h,
injection h with h,
rw ←h, apply ctx_invar, assumption, assumption },
case is_subst.diff_var neq
{ rw extend_diff Γ τ' neq at h,
apply has_type_under.var, assumption } } },
case term.abs y _ ih
{ cases te, cases is,
apply has_type_under.abs,
apply ih,
rw extend_comm _ _ _ a_2,
repeat { assumption } },
case term.app
{ cases te, cases is,
let := ih_1 a_2 a_4,
let := ih_2 a_3 a_5,
apply has_type_under.app,
repeat {assumption} }
end
lemma uniqueness {e : term} {τ τ' : type} :
has_type e τ → has_type e τ' → τ = τ' := sorry
lemma E_lemma {E : E} {e e' : term} {τ τ' : type} :
has_type (E e) τ → has_type e τ' → has_type e' τ' → has_type (E e') τ :=
begin
intros tEe te te',
induction E generalizing τ,
rw uniqueness tEe te,
assumption,
cases tEe,
let := ih_1 a_2,
apply has_type_under.app,
repeat {assumption},
cases a,
cases tEe,
let := ih_1 a_2,
apply has_type_under.app,
repeat {assumption},
end
lemma typing_hole {E : E} {e : term} {τ : type} :
has_type (E e) τ → ∃ τ', has_type e τ' :=
begin
intro t,
induction E generalizing τ,
case E.hole
{ existsi τ, assumption },
case E.app_left _ _ ih
{ cases t,
case has_type_under.app _ h
{ exact ih h } },
case E.app_right v _ ih
{ cases v, cases t,
case has_type_under.app _ _ h
{ exact ih h } }
end
lemma preservation {e e' : term} {τ : type} :
has_type e τ → (e ⇝ e') → has_type e' τ :=
begin
intros t s,
induction s generalizing τ,
case step.beta
{ cases t,
case has_type_under.app _ t'
{ cases t',
apply subst_lemma,
repeat { assumption } } },
case step.context _ _ _ _ ih
{ cases typing_hole t with _ t',
let := ih t',
apply E_lemma,
repeat { assumption } },
end
theorem soundness {e e' : term} {τ : type} :
has_type e τ → (e ⇝* e') → halts e' → is_value e' ∧ has_type e' τ :=
begin
intros t s h,
induction s,
case rtc.refl
{ apply and.intro,
apply has_type_halts_is_value,
repeat { assumption } },
case rtc.trans _ _ _ s _ ih
{ exact ih (preservation t s) h }
end
|
6687e9d265d5d0e81424758d6e80fe188876e6b3 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /tests/lean/1207.lean | ec1305b4586f1b954349c513587038b20f556578 | [
"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 | 468 | lean | example : true :=
begin
note H : true := (by trivial),
exact H
end
example : true :=
begin
note H : true := (by tactic.triv),
exact H
end
meta example (h : tactic unit) : true :=
begin
h, -- ERROR h should not be visible here
trivial
end
example : false :=
begin
note H : true := (by foo), -- ERROR
exact sorry
end
constant P : Prop
example (p : P) : true :=
begin
note H : P := by do { p ← tactic.get_local `p, tactic.exact p },
trivial
end
|
a44894b690775bd8c6949cb424959af35240b842 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebraic_topology/dold_kan/projections.lean | 4b9ea21fdc872936468f49a27d749cff3f87ffbe | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,152 | lean | /-
Copyright (c) 2022 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou
-/
import algebraic_topology.dold_kan.faces
import category_theory.idempotents.basic
/-!
# Construction of projections for the Dold-Kan correspondence
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
TODO (@joelriou) continue adding the various files referenced below
In this file, we construct endomorphisms `P q : K[X] ⟶ K[X]` for all
`q : ℕ`. We study how they behave with respect to face maps with the lemmas
`higher_faces_vanish.of_P`, `higher_faces_vanish.comp_P_eq_self` and
`comp_P_eq_self_iff`.
Then, we show that they are projections (see `P_f_idem`
and `P_idem`). They are natural transformations (see `nat_trans_P`
and `P_f_naturality`) and are compatible with the application
of additive functors (see `map_P`).
By passing to the limit, these endomorphisms `P q` shall be used in `p_infty.lean`
in order to define `P_infty : K[X] ⟶ K[X]`, see `equivalence.lean` for the general
strategy of proof of the Dold-Kan equivalence.
(See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.)
-/
open category_theory category_theory.category category_theory.limits
category_theory.preadditive category_theory.simplicial_object opposite
category_theory.idempotents
open_locale simplicial dold_kan
noncomputable theory
namespace algebraic_topology
namespace dold_kan
variables {C : Type*} [category C] [preadditive C] {X : simplicial_object C}
/-- This is the inductive definition of the projections `P q : K[X] ⟶ K[X]`,
with `P 0 := 𝟙 _` and `P (q+1) := P q ≫ (𝟙 _ + Hσ q)`. -/
noncomputable def P : ℕ → (K[X] ⟶ K[X])
| 0 := 𝟙 _
| (q+1) := P q ≫ (𝟙 _ + Hσ q)
/-- All the `P q` coincide with `𝟙 _` in degree 0. -/
@[simp]
lemma P_f_0_eq (q : ℕ) : ((P q).f 0 : X _[0] ⟶ X _[0]) = 𝟙 _ :=
begin
induction q with q hq,
{ refl, },
{ unfold P,
simp only [homological_complex.add_f_apply, homological_complex.comp_f,
homological_complex.id_f, id_comp, hq, Hσ_eq_zero, add_zero], },
end
/-- `Q q` is the complement projection associated to `P q` -/
def Q (q : ℕ) : K[X] ⟶ K[X] := 𝟙 _ - P q
lemma P_add_Q (q : ℕ) : P q + Q q = 𝟙 K[X] := by { rw Q, abel, }
lemma P_add_Q_f (q n : ℕ) : (P q).f n + (Q q).f n = 𝟙 (X _[n]) :=
homological_complex.congr_hom (P_add_Q q) n
@[simp]
lemma Q_eq_zero : (Q 0 : K[X] ⟶ _) = 0 := sub_self _
lemma Q_eq (q : ℕ) : (Q (q+1) : K[X] ⟶ _) = Q q - P q ≫ Hσ q :=
by { unfold Q P, simp only [comp_add, comp_id], abel, }
/-- All the `Q q` coincide with `0` in degree 0. -/
@[simp]
lemma Q_f_0_eq (q : ℕ) : ((Q q).f 0 : X _[0] ⟶ X _[0]) = 0 :=
by simp only [homological_complex.sub_f_apply, homological_complex.id_f, Q, P_f_0_eq, sub_self]
namespace higher_faces_vanish
/-- This lemma expresses the vanishing of
`(P q).f (n+1) ≫ X.δ k : X _[n+1] ⟶ X _[n]` when `k≠0` and `k≥n-q+2` -/
lemma of_P : Π (q n : ℕ), higher_faces_vanish q (((P q).f (n+1) : X _[n+1] ⟶ X _[n+1]))
| 0 := λ n j hj₁, by { exfalso, have hj₂ := fin.is_lt j, linarith, }
| (q+1) := λ n, by { unfold P, exact (of_P q n).induction, }
@[reassoc]
lemma comp_P_eq_self {Y : C} {n q : ℕ} {φ : Y ⟶ X _[n+1]}
(v : higher_faces_vanish q φ) : φ ≫ (P q).f (n+1) = φ :=
begin
induction q with q hq,
{ unfold P,
apply comp_id, },
{ unfold P,
simp only [comp_add, homological_complex.comp_f, homological_complex.add_f_apply,
comp_id, ← assoc, hq v.of_succ, add_right_eq_self],
by_cases hqn : n<q,
{ exact v.of_succ.comp_Hσ_eq_zero hqn, },
{ cases nat.le.dest (not_lt.mp hqn) with a ha,
have hnaq : n=a+q := by linarith,
simp only [v.of_succ.comp_Hσ_eq hnaq, neg_eq_zero, ← assoc],
have eq := v ⟨a, by linarith⟩
(by simp only [hnaq, fin.coe_mk, nat.succ_eq_add_one, add_assoc]),
simp only [fin.succ_mk] at eq,
simp only [eq, zero_comp], }, },
end
end higher_faces_vanish
lemma comp_P_eq_self_iff {Y : C} {n q : ℕ} {φ : Y ⟶ X _[n+1]} :
φ ≫ (P q).f (n+1) = φ ↔ higher_faces_vanish q φ :=
begin
split,
{ intro hφ,
rw ← hφ,
apply higher_faces_vanish.of_comp,
apply higher_faces_vanish.of_P, },
{ exact higher_faces_vanish.comp_P_eq_self, },
end
@[simp, reassoc]
lemma P_f_idem (q n : ℕ) :
((P q).f n : X _[n] ⟶ _) ≫ ((P q).f n) = (P q).f n :=
begin
cases n,
{ rw [P_f_0_eq q, comp_id], },
{ exact (higher_faces_vanish.of_P q n).comp_P_eq_self, }
end
@[simp, reassoc]
lemma Q_f_idem (q n : ℕ) :
((Q q).f n : X _[n] ⟶ _) ≫ ((Q q).f n) = (Q q).f n :=
idem_of_id_sub_idem _ (P_f_idem q n)
@[simp, reassoc]
lemma P_idem (q : ℕ) : (P q : K[X] ⟶ K[X]) ≫ P q = P q :=
by { ext n, exact P_f_idem q n, }
@[simp, reassoc]
lemma Q_idem (q : ℕ) : (Q q : K[X] ⟶ K[X]) ≫ Q q = Q q :=
by { ext n, exact Q_f_idem q n, }
/-- For each `q`, `P q` is a natural transformation. -/
@[simps]
def nat_trans_P (q : ℕ) :
alternating_face_map_complex C ⟶ alternating_face_map_complex C :=
{ app := λ X, P q,
naturality' := λ X Y f, begin
induction q with q hq,
{ unfold P,
dsimp only [alternating_face_map_complex],
rw [id_comp, comp_id], },
{ unfold P,
simp only [add_comp, comp_add, assoc, comp_id, hq],
congr' 1,
rw [← assoc, hq, assoc],
congr' 1,
exact (nat_trans_Hσ q).naturality' f, }
end }
@[simp, reassoc]
lemma P_f_naturality (q n : ℕ) {X Y : simplicial_object C} (f : X ⟶ Y) :
f.app (op [n]) ≫ (P q).f n = (P q).f n ≫ f.app (op [n]) :=
homological_complex.congr_hom ((nat_trans_P q).naturality f) n
@[simp, reassoc]
lemma Q_f_naturality (q n : ℕ) {X Y : simplicial_object C} (f : X ⟶ Y) :
f.app (op [n]) ≫ (Q q).f n = (Q q).f n ≫ f.app (op [n]) :=
begin
simp only [Q, homological_complex.sub_f_apply, homological_complex.id_f,
comp_sub, P_f_naturality, sub_comp, sub_left_inj],
dsimp,
simp only [comp_id, id_comp],
end
/-- For each `q`, `Q q` is a natural transformation. -/
@[simps]
def nat_trans_Q (q : ℕ) :
alternating_face_map_complex C ⟶ alternating_face_map_complex C :=
{ app := λ X, Q q, }
lemma map_P {D : Type*} [category D] [preadditive D]
(G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n : ℕ) :
G.map ((P q : K[X] ⟶ _).f n) = (P q : K[((whiskering C D).obj G).obj X] ⟶ _).f n :=
begin
induction q with q hq,
{ unfold P,
apply G.map_id, },
{ unfold P,
simp only [comp_add, homological_complex.comp_f, homological_complex.add_f_apply,
comp_id, functor.map_add, functor.map_comp, hq, map_Hσ], }
end
lemma map_Q {D : Type*} [category D] [preadditive D]
(G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n : ℕ) :
G.map ((Q q : K[X] ⟶ _).f n) = (Q q : K[((whiskering C D).obj G).obj X] ⟶ _).f n :=
begin
rw [← add_right_inj (G.map ((P q : K[X] ⟶ _).f n)), ← G.map_add, map_P G X q n,
P_add_Q_f, P_add_Q_f],
apply G.map_id,
end
end dold_kan
end algebraic_topology
|
c44da40db1eeba95d66233902c3088a32a11104f | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/limits/shapes/wide_pullbacks.lean | b2183b92546df28480d7f35e0725902afb9da36c | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 15,559 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.has_limits
import category_theory.thin
/-!
# Wide pullbacks
We define the category `wide_pullback_shape`, (resp. `wide_pushout_shape`) which is the category
obtained from a discrete category of type `J` by adjoining a terminal (resp. initial) element.
Limits of this shape are wide pullbacks (pushouts).
The convenience method `wide_cospan` (`wide_span`) constructs a functor from this category, hitting
the given morphisms.
We use `wide_pullback_shape` to define ordinary pullbacks (pushouts) by using `J := walking_pair`,
which allows easy proofs of some related lemmas.
Furthermore, wide pullbacks are used to show the existence of limits in the slice category.
Namely, if `C` has wide pullbacks then `C/B` has limits for any object `B` in `C`.
Typeclasses `has_wide_pullbacks` and `has_finite_wide_pullbacks` assert the existence of wide
pullbacks and finite wide pullbacks.
-/
universes v u
open category_theory category_theory.limits opposite
namespace category_theory.limits
variable (J : Type v)
/-- A wide pullback shape for any type `J` can be written simply as `option J`. -/
@[derive inhabited]
def wide_pullback_shape := option J
/-- A wide pushout shape for any type `J` can be written simply as `option J`. -/
@[derive inhabited]
def wide_pushout_shape := option J
namespace wide_pullback_shape
variable {J}
/-- The type of arrows for the shape indexing a wide pullback. -/
@[derive decidable_eq]
inductive hom : wide_pullback_shape J → wide_pullback_shape J → Type v
| id : Π X, hom X X
| term : Π (j : J), hom (some j) none
attribute [nolint unused_arguments] hom.decidable_eq
instance struct : category_struct (wide_pullback_shape J) :=
{ hom := hom,
id := λ j, hom.id j,
comp := λ j₁ j₂ j₃ f g,
begin
cases f,
exact g,
cases g,
apply hom.term _
end }
instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pullback_shape J)⟩
local attribute [tidy] tactic.case_bash
instance subsingleton_hom (j j' : wide_pullback_shape J) : subsingleton (j ⟶ j') :=
⟨by tidy⟩
instance category : small_category (wide_pullback_shape J) := thin_category
@[simp] lemma hom_id (X : wide_pullback_shape J) : hom.id X = 𝟙 X := rfl
variables {C : Type u} [category.{v} C]
/--
Construct a functor out of the wide pullback shape given a J-indexed collection of arrows to a
fixed object.
-/
@[simps]
def wide_cospan (B : C) (objs : J → C) (arrows : Π (j : J), objs j ⟶ B) :
wide_pullback_shape J ⥤ C :=
{ obj := λ j, option.cases_on j B objs,
map := λ X Y f,
begin
cases f with _ j,
{ apply (𝟙 _) },
{ exact arrows j }
end,
map_comp' := λ _ _ _ f g,
begin
cases f,
{ simpa },
cases g,
simp
end }
/-- Every diagram is naturally isomorphic (actually, equal) to a `wide_cospan` -/
def diagram_iso_wide_cospan (F : wide_pullback_shape J ⥤ C) :
F ≅ wide_cospan (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.term j)) :=
nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy
/-- Construct a cone over a wide cospan. -/
@[simps]
def mk_cone {F : wide_pullback_shape J ⥤ C} {X : C}
(f : X ⟶ F.obj none) (π : Π j, X ⟶ F.obj (some j))
(w : ∀ j, π j ≫ F.map (hom.term j) = f) : cone F :=
{ X := X,
π :=
{ app := λ j, match j with
| none := f
| (some j) := π j
end,
naturality' := λ j j' f, by { cases j; cases j'; cases f; unfold_aux; dsimp; simp [w], }, } }
end wide_pullback_shape
namespace wide_pushout_shape
variable {J}
/-- The type of arrows for the shape indexing a wide psuhout. -/
@[derive decidable_eq]
inductive hom : wide_pushout_shape J → wide_pushout_shape J → Type v
| id : Π X, hom X X
| init : Π (j : J), hom none (some j)
attribute [nolint unused_arguments] hom.decidable_eq
instance struct : category_struct (wide_pushout_shape J) :=
{ hom := hom,
id := λ j, hom.id j,
comp := λ j₁ j₂ j₃ f g,
begin
cases f,
exact g,
cases g,
apply hom.init _
end }
instance hom.inhabited : inhabited (hom none none) := ⟨hom.id (none : wide_pushout_shape J)⟩
local attribute [tidy] tactic.case_bash
instance subsingleton_hom (j j' : wide_pushout_shape J) : subsingleton (j ⟶ j') :=
⟨by tidy⟩
instance category : small_category (wide_pushout_shape J) := thin_category
@[simp] lemma hom_id (X : wide_pushout_shape J) : hom.id X = 𝟙 X := rfl
variables {C : Type u} [category.{v} C]
/--
Construct a functor out of the wide pushout shape given a J-indexed collection of arrows from a
fixed object.
-/
@[simps]
def wide_span (B : C) (objs : J → C) (arrows : Π (j : J), B ⟶ objs j) : wide_pushout_shape J ⥤ C :=
{ obj := λ j, option.cases_on j B objs,
map := λ X Y f,
begin
cases f with _ j,
{ apply (𝟙 _) },
{ exact arrows j }
end,
map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_) (_|_); simpa <|> simp } }
/-- Every diagram is naturally isomorphic (actually, equal) to a `wide_span` -/
def diagram_iso_wide_span (F : wide_pushout_shape J ⥤ C) :
F ≅ wide_span (F.obj none) (λ j, F.obj (some j)) (λ j, F.map (hom.init j)) :=
nat_iso.of_components (λ j, eq_to_iso $ by tidy) $ by tidy
/-- Construct a cocone over a wide span. -/
@[simps]
def mk_cocone {F : wide_pushout_shape J ⥤ C} {X : C}
(f : F.obj none ⟶ X) (ι : Π j, F.obj (some j) ⟶ X)
(w : ∀ j, F.map (hom.init j) ≫ ι j = f) : cocone F :=
{ X := X,
ι :=
{ app := λ j, match j with
| none := f
| (some j) := ι j
end,
naturality' := λ j j' f, by { cases j; cases j'; cases f; unfold_aux; dsimp; simp [w], }, } }
end wide_pushout_shape
variables (C : Type u) [category.{v} C]
/-- `has_wide_pullbacks` represents a choice of wide pullback for every collection of morphisms -/
abbreviation has_wide_pullbacks : Prop :=
Π (J : Type v), has_limits_of_shape (wide_pullback_shape J) C
/-- `has_wide_pushouts` represents a choice of wide pushout for every collection of morphisms -/
abbreviation has_wide_pushouts : Prop :=
Π (J : Type v), has_colimits_of_shape (wide_pushout_shape J) C
variables {C J}
/-- `has_wide_pullback B objs arrows` means that `wide_cospan B objs arrows` has a limit. -/
abbreviation has_wide_pullback (B : C) (objs : J → C)
(arrows : Π (j : J), objs j ⟶ B) : Prop :=
has_limit (wide_pullback_shape.wide_cospan B objs arrows)
/-- `has_wide_pushout B objs arrows` means that `wide_span B objs arrows` has a colimit. -/
abbreviation has_wide_pushout (B : C) (objs : J → C)
(arrows : Π (j : J), B ⟶ objs j) : Prop :=
has_colimit (wide_pushout_shape.wide_span B objs arrows)
/-- A choice of wide pullback. -/
noncomputable
abbreviation wide_pullback (B : C) (objs : J → C) (arrows : Π (j : J), objs j ⟶ B)
[has_wide_pullback B objs arrows] : C :=
limit (wide_pullback_shape.wide_cospan B objs arrows)
/-- A choice of wide pushout. -/
noncomputable
abbreviation wide_pushout (B : C) (objs : J → C) (arrows : Π (j : J), B ⟶ objs j)
[has_wide_pushout B objs arrows] : C :=
colimit (wide_pushout_shape.wide_span B objs arrows)
variable (C)
namespace wide_pullback
variables {C} {B : C} {objs : J → C} (arrows : Π (j : J), objs j ⟶ B)
variables [has_wide_pullback B objs arrows]
/-- The `j`-th projection from the pullback. -/
noncomputable
abbreviation π (j : J) : wide_pullback _ _ arrows ⟶ objs j :=
limit.π (wide_pullback_shape.wide_cospan _ _ _) (option.some j)
/-- The unique map to the base from the pullback. -/
noncomputable
abbreviation base : wide_pullback _ _ arrows ⟶ B :=
limit.π (wide_pullback_shape.wide_cospan _ _ _) option.none
@[simp, reassoc]
lemma π_arrow (j : J) : π arrows j ≫ arrows _ = base arrows :=
by apply limit.w (wide_pullback_shape.wide_cospan _ _ _) (wide_pullback_shape.hom.term j)
variables {arrows}
/-- Lift a collection of morphisms to a morphism to the pullback. -/
noncomputable
abbreviation lift {X : C} (f : X ⟶ B) (fs : Π (j : J), X ⟶ objs j)
(w : ∀ j, fs j ≫ arrows j = f) : X ⟶ wide_pullback _ _ arrows :=
limit.lift (wide_pullback_shape.wide_cospan _ _ _)
(wide_pullback_shape.mk_cone f fs $ by exact w)
variables (arrows)
variables {X : C} (f : X ⟶ B) (fs : Π (j : J), X ⟶ objs j)
(w : ∀ j, fs j ≫ arrows j = f)
@[simp, reassoc]
lemma lift_π (j : J) : lift f fs w ≫ π arrows j = fs _ :=
by { simp, refl }
@[simp, reassoc]
lemma lift_base : lift f fs w ≫ base arrows = f :=
by { simp, refl }
lemma eq_lift_of_comp_eq (g : X ⟶ wide_pullback _ _ arrows) :
(∀ j : J, g ≫ π arrows j = fs j) → g ≫ base arrows = f → g = lift f fs w :=
begin
intros h1 h2,
apply (limit.is_limit (wide_pullback_shape.wide_cospan B objs arrows)).uniq
(wide_pullback_shape.mk_cone f fs $ by exact w),
rintro (_|_),
{ apply h2 },
{ apply h1 }
end
lemma hom_eq_lift (g : X ⟶ wide_pullback _ _ arrows) :
g = lift (g ≫ base arrows) (λ j, g ≫ π arrows j) (by tidy) :=
begin
apply eq_lift_of_comp_eq,
tidy,
end
@[ext]
lemma hom_ext (g1 g2 : X ⟶ wide_pullback _ _ arrows) :
(∀ j : J, g1 ≫ π arrows j = g2 ≫ π arrows j) →
g1 ≫ base arrows = g2 ≫ base arrows → g1 = g2 :=
begin
intros h1 h2,
apply limit.hom_ext,
rintros (_|_),
{ apply h2 },
{ apply h1 },
end
end wide_pullback
namespace wide_pushout
variables {C} {B : C} {objs : J → C} (arrows : Π (j : J), B ⟶ objs j)
variables [has_wide_pushout B objs arrows]
/-- The `j`-th inclusion to the pushout. -/
noncomputable
abbreviation ι (j : J) : objs j ⟶ wide_pushout _ _ arrows :=
colimit.ι (wide_pushout_shape.wide_span _ _ _) (option.some j)
/-- The unique map from the head to the pushout. -/
noncomputable
abbreviation head : B ⟶ wide_pushout B objs arrows :=
colimit.ι (wide_pushout_shape.wide_span _ _ _) option.none
@[simp, reassoc]
lemma arrow_ι (j : J) : arrows j ≫ ι arrows j = head arrows :=
by apply colimit.w (wide_pushout_shape.wide_span _ _ _) (wide_pushout_shape.hom.init j)
variables {arrows}
/-- Descend a collection of morphisms to a morphism from the pushout. -/
noncomputable
abbreviation desc {X : C} (f : B ⟶ X) (fs : Π (j : J), objs j ⟶ X)
(w : ∀ j, arrows j ≫ fs j = f) : wide_pushout _ _ arrows ⟶ X :=
colimit.desc (wide_pushout_shape.wide_span B objs arrows)
(wide_pushout_shape.mk_cocone f fs $ by exact w)
variables (arrows)
variables {X : C} (f : B ⟶ X) (fs : Π (j : J), objs j ⟶ X)
(w : ∀ j, arrows j ≫ fs j = f)
@[simp, reassoc]
lemma ι_desc (j : J) : ι arrows j ≫ desc f fs w = fs _ :=
by { simp, refl }
@[simp, reassoc]
lemma head_desc : head arrows ≫ desc f fs w = f :=
by { simp, refl }
lemma eq_desc_of_comp_eq (g : wide_pushout _ _ arrows ⟶ X) :
(∀ j : J, ι arrows j ≫ g = fs j) → head arrows ≫ g = f → g = desc f fs w :=
begin
intros h1 h2,
apply (colimit.is_colimit (wide_pushout_shape.wide_span B objs arrows)).uniq
(wide_pushout_shape.mk_cocone f fs $ by exact w),
rintro (_|_),
{ apply h2 },
{ apply h1 }
end
lemma hom_eq_desc (g : wide_pushout _ _ arrows ⟶ X) :
g = desc (head arrows ≫ g) (λ j, ι arrows j ≫ g) (λ j, by { rw ← category.assoc, simp }) :=
begin
apply eq_desc_of_comp_eq,
tidy,
end
@[ext]
lemma hom_ext (g1 g2 : wide_pushout _ _ arrows ⟶ X) :
(∀ j : J, ι arrows j ≫ g1 = ι arrows j ≫ g2) →
head arrows ≫ g1 = head arrows ≫ g2 → g1 = g2 :=
begin
intros h1 h2,
apply colimit.hom_ext,
rintros (_|_),
{ apply h2 },
{ apply h1 },
end
end wide_pushout
variable (J)
/-- The action on morphisms of the obvious functor
`wide_pullback_shape_op : wide_pullback_shape J ⥤ (wide_pushout_shape J)ᵒᵖ`-/
def wide_pullback_shape_op_map : Π (X Y : wide_pullback_shape J),
(X ⟶ Y) → ((op X : (wide_pushout_shape J)ᵒᵖ) ⟶ (op Y : (wide_pushout_shape J)ᵒᵖ))
| _ _ (wide_pullback_shape.hom.id X) := quiver.hom.op (wide_pushout_shape.hom.id _)
| _ _ (wide_pullback_shape.hom.term j) := quiver.hom.op (wide_pushout_shape.hom.init _)
/-- The obvious functor `wide_pullback_shape J ⥤ (wide_pushout_shape J)ᵒᵖ` -/
@[simps]
def wide_pullback_shape_op : wide_pullback_shape J ⥤ (wide_pushout_shape J)ᵒᵖ :=
{ obj := λ X, op X,
map := wide_pullback_shape_op_map J, }
/-- The action on morphisms of the obvious functor
`wide_pushout_shape_op : `wide_pushout_shape J ⥤ (wide_pullback_shape J)ᵒᵖ` -/
def wide_pushout_shape_op_map : Π (X Y : wide_pushout_shape J),
(X ⟶ Y) → ((op X : (wide_pullback_shape J)ᵒᵖ) ⟶ (op Y : (wide_pullback_shape J)ᵒᵖ))
| _ _ (wide_pushout_shape.hom.id X) := quiver.hom.op (wide_pullback_shape.hom.id _)
| _ _ (wide_pushout_shape.hom.init j) := quiver.hom.op (wide_pullback_shape.hom.term _)
/-- The obvious functor `wide_pushout_shape J ⥤ (wide_pullback_shape J)ᵒᵖ` -/
@[simps]
def wide_pushout_shape_op : wide_pushout_shape J ⥤ (wide_pullback_shape J)ᵒᵖ :=
{ obj := λ X, op X,
map := wide_pushout_shape_op_map J, }
/-- The obvious functor `(wide_pullback_shape J)ᵒᵖ ⥤ wide_pushout_shape J`-/
@[simps]
def wide_pullback_shape_unop : (wide_pullback_shape J)ᵒᵖ ⥤ wide_pushout_shape J :=
(wide_pullback_shape_op J).left_op
/-- The obvious functor `(wide_pushout_shape J)ᵒᵖ ⥤ wide_pullback_shape J` -/
@[simps]
def wide_pushout_shape_unop : (wide_pushout_shape J)ᵒᵖ ⥤ wide_pullback_shape J :=
(wide_pushout_shape_op J).left_op
/-- The inverse of the unit isomorphism of the equivalence
`wide_pushout_shape_op_equiv : (wide_pushout_shape J)ᵒᵖ ≌ wide_pullback_shape J` -/
def wide_pushout_shape_op_unop : wide_pushout_shape_unop J ⋙ wide_pullback_shape_op J ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso.refl _) (λ X Y f, dec_trivial)
/-- The counit isomorphism of the equivalence
`wide_pullback_shape_op_equiv : (wide_pullback_shape J)ᵒᵖ ≌ wide_pushout_shape J` -/
def wide_pushout_shape_unop_op : wide_pushout_shape_op J ⋙ wide_pullback_shape_unop J ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso.refl _) (λ X Y f, dec_trivial)
/-- The inverse of the unit isomorphism of the equivalence
`wide_pullback_shape_op_equiv : (wide_pullback_shape J)ᵒᵖ ≌ wide_pushout_shape J` -/
def wide_pullback_shape_op_unop : wide_pullback_shape_unop J ⋙ wide_pushout_shape_op J ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso.refl _) (λ X Y f, dec_trivial)
/-- The counit isomorphism of the equivalence
`wide_pushout_shape_op_equiv : (wide_pushout_shape J)ᵒᵖ ≌ wide_pullback_shape J` -/
def wide_pullback_shape_unop_op : wide_pullback_shape_op J ⋙ wide_pushout_shape_unop J ≅ 𝟭 _ :=
nat_iso.of_components (λ X, iso.refl _) (λ X Y f, dec_trivial)
/-- The duality equivalence `(wide_pushout_shape J)ᵒᵖ ≌ wide_pullback_shape J` -/
@[simps]
def wide_pushout_shape_op_equiv : (wide_pushout_shape J)ᵒᵖ ≌ wide_pullback_shape J :=
{ functor := wide_pushout_shape_unop J,
inverse := wide_pullback_shape_op J,
unit_iso := (wide_pushout_shape_op_unop J).symm,
counit_iso := wide_pullback_shape_unop_op J, }
/-- The duality equivalence `(wide_pullback_shape J)ᵒᵖ ≌ wide_pushout_shape J` -/
@[simps]
def wide_pullback_shape_op_equiv : (wide_pullback_shape J)ᵒᵖ ≌ wide_pushout_shape J :=
{ functor := wide_pullback_shape_unop J,
inverse := wide_pushout_shape_op J,
unit_iso := (wide_pullback_shape_op_unop J).symm,
counit_iso := wide_pushout_shape_unop_op J, }
end category_theory.limits
|
a7f11a053b13785b4311b59e9f770a44a39e2166 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/interactive/1403.lean | cf8c0a4aafd59ee3ab0f553a7c59088c48168d22 | [
"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 | 67 | lean | example : True := by
rewrite []
--^ textDocument/hover
trivial
|
01d97776d194d120f7193b6801c3e969310a7520 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/tactic/interactive.lean | d7e18631484c23500267419937e6d39b6dfddf10 | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 27,667 | lean |
/-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Simon Hudon, Sebastien Gouezel, Scott Morrison
-/
import tactic.core data.list.defs data.string.defs
open lean
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
namespace interactive
open interactive interactive.types expr
/-- `try_for n { tac }` executes `tac` for `n` ticks, otherwise uses `sorry` to close the goal.
Never fails. Useful for debugging. -/
meta def try_for (max : parse parser.pexpr) (tac : itactic) : tactic unit :=
do max ← i_to_expr_strict max >>= tactic.eval_expr nat,
λ s, match _root_.try_for max (tac s) with
| some r := r
| none := (tactic.trace "try_for timeout, using sorry" >> admit) s
end
/-- Multiple subst. `substs x y z` is the same as `subst x, subst y, subst z`. -/
meta def substs (l : parse ident*) : tactic unit :=
l.mmap' (λ h, get_local h >>= tactic.subst) >> try (tactic.reflexivity reducible)
/-- Unfold coercion-related definitions -/
meta def unfold_coes (loc : parse location) : tactic unit :=
unfold [
``coe, ``coe_t, ``has_coe_t.coe, ``coe_b,``has_coe.coe,
``lift, ``has_lift.lift, ``lift_t, ``has_lift_t.lift,
``coe_fn, ``has_coe_to_fun.coe, ``coe_sort, ``has_coe_to_sort.coe] loc
/-- Unfold auxiliary definitions associated with the current declaration. -/
meta def unfold_aux : tactic unit :=
do tgt ← target,
name ← decl_name,
let to_unfold := (tgt.list_names_with_prefix name),
guard (¬ to_unfold.empty),
-- should we be using simp_lemmas.mk_default?
simp_lemmas.mk.dsimplify to_unfold.to_list tgt >>= tactic.change
/-- For debugging only. This tactic checks the current state for any
missing dropped goals and restores them. Useful when there are no
goals to solve but "result contains meta-variables". -/
meta def recover : tactic unit :=
metavariables >>= tactic.set_goals
/-- Like `try { tac }`, but in the case of failure it continues
from the failure state instead of reverting to the original state. -/
meta def continue (tac : itactic) : tactic unit :=
λ s, result.cases_on (tac s)
(λ a, result.success ())
(λ e ref, result.success ())
/-- Move goal `n` to the front. -/
meta def swap (n := 2) : tactic unit :=
do gs ← get_goals,
match gs.nth (n-1) with
| (some g) := set_goals (g :: gs.remove_nth (n-1))
| _ := skip
end
/-- `rotate n` cyclically shifts the goals `n` times.
`rotate` defaults to `rotate 1`. -/
meta def rotate (n := 1) : tactic unit := tactic.rotate n
/-- Clear all hypotheses starting with `_`, like `_match` and `_let_match`. -/
meta def clear_ : tactic unit := tactic.repeat $ do
l ← local_context,
l.reverse.mfirst $ λ h, do
name.mk_string s p ← return $ local_pp_name h,
guard (s.front = '_'),
cl ← infer_type h >>= is_class, guard (¬ cl),
tactic.clear h
meta def apply_iff_congr_core : tactic unit :=
applyc ``iff_of_eq
meta def congr_core' : tactic unit :=
do tgt ← target,
apply_eq_congr_core tgt
<|> apply_heq_congr_core
<|> apply_iff_congr_core
<|> fail "congr tactic failed"
/--
Same as the `congr` tactic, but takes an optional argument which gives
the depth of recursive applications. This is useful when `congr`
is too aggressive in breaking down the goal. For example, given
`⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y`
and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. -/
meta def congr' : parse (with_desc "n" small_nat)? → tactic unit
| (some 0) := failed
| o := focus1 (assumption <|> (congr_core' >>
all_goals (reflexivity <|> `[apply proof_irrel_heq] <|>
`[apply proof_irrel] <|> try (congr' (nat.pred <$> o)))))
/--
Acts like `have`, but removes a hypothesis with the same name as
this one. For example if the state is `h : p ⊢ goal` and `f : p → q`,
then after `replace h := f h` the goal will be `h : q ⊢ goal`,
where `have h := f h` would result in the state `h : p, h : q ⊢ goal`.
This can be used to simulate the `specialize` and `apply at` tactics
of Coq. -/
meta def replace (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit :=
do let h := h.get_or_else `this,
old ← try_core (get_local h),
«have» h q₁ q₂,
match old, q₂ with
| none, _ := skip
| some o, some _ := tactic.clear o
| some o, none := swap >> tactic.clear o >> swap
end
/-- Make every propositions in the context decidable -/
meta def classical := tactic.classical
private meta def generalize_arg_p_aux : pexpr → parser (pexpr × name)
| (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x)
| _ := fail "parse error"
private meta def generalize_arg_p : parser (pexpr × name) :=
with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux
lemma {u} generalize_a_aux {α : Sort u}
(h : ∀ x : Sort u, (α → x) → x) : α := h α id
/--
Like `generalize` but also considers assumptions
specified by the user. The user can also specify to
omit the goal.
-/
meta def generalize_hyp (h : parse ident?) (_ : parse $ tk ":")
(p : parse generalize_arg_p)
(l : parse location) :
tactic unit :=
do h' ← get_unused_name `h,
x' ← get_unused_name `x,
g ← if ¬ l.include_goal then
do refine ``(generalize_a_aux _),
some <$> (prod.mk <$> tactic.intro x' <*> tactic.intro h')
else pure none,
n ← l.get_locals >>= tactic.revert_lst,
generalize h () p,
intron n,
match g with
| some (x',h') :=
do tactic.apply h',
tactic.clear h',
tactic.clear x'
| none := return ()
end
/--
Similar to `refine` but generates equality proof obligations
for every discrepancy between the goal and the type of the rule.
`convert e using n` (with `n : ℕ`) bounds the depth of the search
for discrepancies, analogous to `congr' n`.
-/
meta def convert (sym : parse (with_desc "←" (tk "<-")?)) (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit :=
do v ← mk_mvar,
if sym.is_some
then refine ``(eq.mp %%v %%r)
else refine ``(eq.mpr %%v %%r),
gs ← get_goals,
set_goals [v],
try (congr' n),
gs' ← get_goals,
set_goals $ gs' ++ gs
meta def compact_decl_aux : list name → binder_info → expr → list expr → tactic (list (list name × binder_info × expr))
| ns bi t [] := pure [(ns.reverse, bi, t)]
| ns bi t (v'@(local_const n pp bi' t') :: xs) :=
do t' ← infer_type v',
if bi = bi' ∧ t = t'
then compact_decl_aux (pp :: ns) bi t xs
else do vs ← compact_decl_aux [pp] bi' t' xs,
pure $ (ns.reverse, bi, t) :: vs
| ns bi t (_ :: xs) := compact_decl_aux ns bi t xs
meta def compact_decl : list expr → tactic (list (list name × binder_info × expr))
| [] := pure []
| (v@(local_const n pp bi t) :: xs) :=
do t ← infer_type v,
compact_decl_aux [pp] bi t xs
| (_ :: xs) := compact_decl xs
meta def clean_ids : list name :=
[``id, ``id_rhs, ``id_delta, ``hidden]
/--
Remove identity functions from a term. These are normally
automatically generated with terms like `show t, from p` or
`(p : t)` which translate to some variant on `@id t p` in
order to retain the type. -/
meta def clean (q : parse texpr) : tactic unit :=
do tgt : expr ← target,
e ← i_to_expr_strict ``(%%q : %%tgt),
tactic.exact $ e.replace (λ e n,
match e with
| (app (app (const n _) _) e') :=
if n ∈ clean_ids then some e' else none
| (app (lam _ _ _ (var 0)) e') := some e'
| _ := none
end)
meta def source_fields (missing : list name) (e : pexpr) : tactic (list (name × pexpr)) :=
do e ← to_expr e,
t ← infer_type e,
let struct_n : name := t.get_app_fn.const_name,
fields ← expanded_field_list struct_n,
let exp_fields := fields.filter (λ x, x.2 ∈ missing),
exp_fields.mmap $ λ ⟨p,n⟩,
(prod.mk n ∘ to_pexpr) <$> mk_mapp (n.update_prefix p) [none,some e]
meta def collect_struct' : pexpr → state_t (list $ expr×structure_instance_info) tactic pexpr | e :=
do some str ← pure (e.get_structure_instance_info)
| e.traverse collect_struct',
v ← monad_lift mk_mvar,
modify (list.cons (v,str)),
pure $ to_pexpr v
meta def collect_struct (e : pexpr) : tactic $ pexpr × list (expr×structure_instance_info) :=
prod.map id list.reverse <$> (collect_struct' e).run []
meta def refine_one (str : structure_instance_info) :
tactic $ list (expr×structure_instance_info) :=
do tgt ← target,
let struct_n : name := tgt.get_app_fn.const_name,
exp_fields ← expanded_field_list struct_n,
let missing_f := exp_fields.filter (λ f, (f.2 : name) ∉ str.field_names),
(src_field_names,src_field_vals) ← (@list.unzip name _ ∘ list.join) <$> str.sources.mmap (source_fields $ missing_f.map prod.snd),
let provided := exp_fields.filter (λ f, (f.2 : name) ∈ str.field_names),
let missing_f' := missing_f.filter (λ x, x.2 ∉ src_field_names),
vs ← mk_mvar_list missing_f'.length,
(field_values,new_goals) ← list.unzip <$> (str.field_values.mmap collect_struct : tactic _),
e' ← to_expr $ pexpr.mk_structure_instance
{ struct := some struct_n
, field_names := str.field_names ++ missing_f'.map prod.snd ++ src_field_names
, field_values := field_values ++ vs.map to_pexpr ++ src_field_vals },
tactic.exact e',
gs ← with_enable_tags (
mzip_with (λ (n : name × name) v, do
set_goals [v],
try (interactive.unfold (provided.map $ λ ⟨s,f⟩, f.update_prefix s) (loc.ns [none])),
apply_auto_param
<|> apply_opt_param
<|> (set_main_tag [`_field,n.2,n.1]),
get_goals)
missing_f' vs),
set_goals gs.join,
return new_goals.join
meta def refine_recursively : expr × structure_instance_info → tactic (list expr) | (e,str) :=
do set_goals [e],
rs ← refine_one str,
gs ← get_goals,
gs' ← rs.mmap refine_recursively,
return $ gs'.join ++ gs
/--
`refine_struct { .. }` acts like `refine` but works only with structure instance
literals. It creates a goal for each missing field and tags it with the name of the
field so that `have_field` can be used to generically refer to the field currently
being refined.
As an example, we can use `refine_struct` to automate the construction semigroup
instances:
```
refine_struct ( { .. } : semigroup α ),
-- case semigroup, mul
-- α : Type u,
-- ⊢ α → α → α
-- case semigroup, mul_assoc
-- α : Type u,
-- ⊢ ∀ (a b c : α), a * b * c = a * (b * c)
```
-/
meta def refine_struct : parse texpr → tactic unit | e :=
do (x,xs) ← collect_struct e,
refine x,
gs ← get_goals,
xs' ← xs.mmap refine_recursively,
set_goals (xs'.join ++ gs)
/--
`guard_hyp h := t` fails if the hypothesis `h` does not have type `t`.
We use this tactic for writing tests.
Fixes `guard_hyp` by instantiating meta variables
-/
meta def guard_hyp' (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit :=
do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_eq h p
/--
`guard_expr_strict t := e` fails if the expr `t` is not equal to `e`. By contrast
to `guard_expr`, this tests strict (syntactic) equality.
We use this tactic for writing tests.
-/
meta def guard_expr_strict (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit :=
do e ← to_expr p, guard (t = e)
/--
`guard_target_strict t` fails if the target of the main goal is not syntactically `t`.
We use this tactic for writing tests.
-/
meta def guard_target_strict (p : parse texpr) : tactic unit :=
do t ← target, guard_expr_strict t p
/--
`guard_hyp_strict h := t` fails if the hypothesis `h` does not have type syntactically equal
to `t`.
We use this tactic for writing tests.
-/
meta def guard_hyp_strict (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit :=
do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_strict h p
meta def guard_hyp_nums (n : ℕ) : tactic unit :=
do k ← local_context,
guard (n = k.length) <|> fail format!"{k.length} hypotheses found"
meta def guard_tags (tags : parse ident*) : tactic unit :=
do (t : list name) ← get_main_tag,
guard (t = tags)
/-- `success_if_fail_with_msg { tac } msg` succeeds if the interactive tactic `tac` fails with
error message `msg` (for test writing purposes). -/
meta def success_if_fail_with_msg (tac : tactic.interactive.itactic) :=
tactic.success_if_fail_with_msg tac
meta def get_current_field : tactic name :=
do [_,field,str] ← get_main_tag,
expr.const_name <$> resolve_name (field.update_prefix str)
meta def field (n : parse ident) (tac : itactic) : tactic unit :=
do gs ← get_goals,
ts ← gs.mmap get_tag,
([g],gs') ← pure $ (list.zip gs ts).partition (λ x, x.snd.nth 1 = some n),
set_goals [g.1],
tac, done,
set_goals $ gs'.map prod.fst
/--
`have_field`, used after `refine_struct _` poses `field` as a local constant
with the type of the field of the current goal:
```
refine_struct ({ .. } : semigroup α),
{ have_field, ... },
{ have_field, ... },
```
behaves like
```
refine_struct ({ .. } : semigroup α),
{ have field := @semigroup.mul, ... },
{ have field := @semigroup.mul_assoc, ... },
```
-/
meta def have_field : tactic unit :=
propagate_tags $
get_current_field
>>= mk_const
>>= note `field none
>> return ()
/-- `apply_field` functions as `have_field, apply field, clear field` -/
meta def apply_field : tactic unit :=
propagate_tags $
get_current_field >>= applyc
/--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the
first goal and the resulting subgoals, iteratively, at most `n` times.
`n` is 50 by default. `hs` can contain user attributes: in this case all theorems with this
attribute are added to the list of rules.
example, with or without user attribute:
```
@[user_attribute]
meta def mono_rules : user_attribute :=
{ name := `mono_rules,
descr := "lemmas usable to prove monotonicity" }
attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right
lemma my_test {a b c d e : real} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) :
a + c * e + a + c + 0 ≤ b + d * e + b + d + e :=
by apply_rules mono_rules
-- any of the following lines would also work:
-- add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3
-- by apply_rules [add_le_add, mul_le_mul_of_nonneg_right]
-- by apply_rules [mono_rules]
```
-/
meta def apply_rules (hs : parse pexpr_list_or_texpr) (n : nat := 50) : tactic unit :=
tactic.apply_rules hs n
meta def return_cast (f : option expr) (t : option (expr × expr))
(es : list (expr × expr × expr))
(e x x' eq_h : expr) :
tactic (option (expr × expr) × list (expr × expr × expr)) :=
(do guard (¬ e.has_var),
unify x x',
u ← mk_meta_univ,
f ← f <|> mk_mapp ``_root_.id [(expr.sort u : expr)],
t' ← infer_type e,
some (f',t) ← pure t | return (some (f,t'), (e,x',eq_h) :: es),
infer_type e >>= is_def_eq t,
unify f f',
return (some (f,t), (e,x',eq_h) :: es)) <|>
return (t, es)
meta def list_cast_of_aux (x : expr) (t : option (expr × expr))
(es : list (expr × expr × expr)) :
expr → tactic (option (expr × expr) × list (expr × expr × expr))
| e@`(cast %%eq_h %%x') := return_cast none t es e x x' eq_h
| e@`(eq.mp %%eq_h %%x') := return_cast none t es e x x' eq_h
| e@`(eq.mpr %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast none t es e x x'
| e@`(@eq.subst %%α %%p %%a %%b %%eq_h %%x') := return_cast p t es e x x' eq_h
| e@`(@eq.substr %%α %%p %%a %%b %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast p t es e x x'
| e@`(@eq.rec %%α %%a %%f %%x' _ %%eq_h) := return_cast f t es e x x' eq_h
| e@`(@eq.rec_on %%α %%a %%f %%b %%eq_h %%x') := return_cast f t es e x x' eq_h
| e := return (t,es)
meta def list_cast_of (x tgt : expr) : tactic (list (expr × expr × expr)) :=
(list.reverse ∘ prod.snd) <$> tgt.mfold (none, []) (λ e i es, list_cast_of_aux x es.1 es.2 e)
private meta def h_generalize_arg_p_aux : pexpr → parser (pexpr × name)
| (app (app (macro _ [const `heq _ ]) h) (local_const x _ _ _)) := pure (h, x)
| _ := fail "parse error"
private meta def h_generalize_arg_p : parser (pexpr × name) :=
with_desc "expr == id" $ parser.pexpr 0 >>= h_generalize_arg_p_aux
/--
`h_generalize Hx : e == x` matches on `cast _ e` in the goal and replaces it with
`x`. It also adds `Hx : e == x` as an assumption. If `cast _ e` appears multiple
times (not necessarily with the same proof), they are all replaced by `x`. `cast`
`eq.mp`, `eq.mpr`, `eq.subst`, `eq.substr`, `eq.rec` and `eq.rec_on` are all treated
as casts.
`h_generalize Hx : e == x with h` adds hypothesis `α = β` with `e : α, x : β`.
`h_generalize Hx : e == x with _` chooses automatically chooses the name of
assumption `α = β`.
`h_generalize! Hx : e == x` reverts `Hx`.
when `Hx` is omitted, assumption `Hx : e == x` is not added.
-/
meta def h_generalize (rev : parse (tk "!")?)
(h : parse ident_?)
(_ : parse (tk ":"))
(arg : parse h_generalize_arg_p)
(eqs_h : parse ( (tk "with" >> pure <$> ident_) <|> pure [])) :
tactic unit :=
do let (e,n) := arg,
let h' := if h = `_ then none else h,
h' ← (h' : tactic name) <|> get_unused_name ("h" ++ n.to_string : string),
e ← to_expr e,
tgt ← target,
((e,x,eq_h)::es) ← list_cast_of e tgt | fail "no cast found",
interactive.generalize h' () (to_pexpr e, n),
asm ← get_local h',
v ← get_local n,
hs ← es.mmap (λ ⟨e,_⟩, mk_app `eq [e,v]),
(eqs_h.zip [e]).mmap' (λ ⟨h,e⟩, do
h ← if h ≠ `_ then pure h else get_unused_name `h,
() <$ note h none eq_h ),
hs.mmap' (λ h,
do h' ← assert `h h,
tactic.exact asm,
try (rewrite_target h'),
tactic.clear h' ),
when h.is_some (do
(to_expr ``(heq_of_eq_rec_left %%eq_h %%asm)
<|> to_expr ``(heq_of_eq_mp %%eq_h %%asm))
>>= note h' none >> pure ()),
tactic.clear asm,
when rev.is_some (interactive.revert [n])
/-- `choose a b h using hyp` takes an hypothesis `hyp` of the form
`∀ (x : X) (y : Y), ∃ (a : A) (b : B), P x y a b` for some `P : X → Y → A → B → Prop` and outputs
into context a function `a : X → Y → A`, `b : X → Y → B` and a proposition `h` stating
`∀ (x : X) (y : Y), P x y (a x y) (b x y)`. It presumably also works with dependent versions.
Example:
```lean
example (h : ∀n m : ℕ, ∃i j, m = n + i ∨ m + j = n) : true :=
begin
choose i j h using h,
guard_hyp i := ℕ → ℕ → ℕ,
guard_hyp j := ℕ → ℕ → ℕ,
guard_hyp h := ∀ (n m : ℕ), m = n + i n m ∨ m + j n m = n,
trivial
end
```
-/
meta def choose (first : parse ident) (names : parse ident*) (tgt : parse (tk "using" *> texpr)?) :
tactic unit := do
tgt ← match tgt with
| none := get_local `this
| some e := tactic.i_to_expr_strict e
end,
tactic.choose tgt (first :: names),
try (interactive.simp none tt [simp_arg_type.expr ``(exists_prop)] [] (loc.ns $ some <$> names)),
try (tactic.clear tgt)
meta def guard_expr_eq' (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit :=
do e ← to_expr p, is_def_eq t e
/--
`guard_target t` fails if the target of the main goal is not `t`.
We use this tactic for writing tests.
-/
meta def guard_target' (p : parse texpr) : tactic unit :=
do t ← target, guard_expr_eq' t p
/--
a weaker version of `trivial` that tries to solve the goal by reflexivity or by reducing it to true,
unfolding only `reducible` constants. -/
meta def triv : tactic unit :=
tactic.triv' <|> tactic.reflexivity reducible <|> tactic.contradiction <|> fail "triv tactic failed"
/--
Similar to `existsi`. `use x` will instantiate the first term of an `∃` or `Σ` goal with `x`.
Unlike `existsi`, `x` is elaborated with respect to the expected type.
`use` will alternatively take a list of terms `[x0, ..., xn]`.
`use` will work with constructors of arbitrary inductive types.
Examples:
example (α : Type) : ∃ S : set α, S = S :=
by use ∅
example : ∃ x : ℤ, x = x :=
by use 42
example : ∃ a b c : ℤ, a + b + c = 6 :=
by use [1, 2, 3]
example : ∃ p : ℤ × ℤ, p.1 = 1 :=
by use ⟨1, 42⟩
example : Σ x y : ℤ, (ℤ × ℤ) × ℤ :=
by use [1, 2, 3, 4, 5]
inductive foo
| mk : ℕ → bool × ℕ → ℕ → foo
example : foo :=
by use [100, tt, 4, 3]
-/
meta def use (l : parse pexpr_list_or_texpr) : tactic unit :=
tactic.use l >> try triv
/--
`clear_aux_decl` clears every `aux_decl` in the local context for the current goal.
This includes the induction hypothesis when using the equation compiler and
`_let_match` and `_fun_match`.
It is useful when using a tactic such as `finish`, `simp *` or `subst` that may use these
auxiliary declarations, and produce an error saying the recursion is not well founded.
-/
meta def clear_aux_decl : tactic unit := tactic.clear_aux_decl
meta def loc.get_local_pp_names : loc → tactic (list name)
| loc.wildcard := list.map expr.local_pp_name <$> local_context
| (loc.ns l) := return l.reduce_option
meta def loc.get_local_uniq_names (l : loc) : tactic (list name) :=
list.map expr.local_uniq_name <$> l.get_locals
/--
The logic of `change x with y at l` fails when there are dependencies.
`change'` mimics the behavior of `change`, except in the case of `change x with y at l`.
In this case, it will correctly replace occurences of `x` with `y` at all possible hypotheses in `l`.
As long as `x` and `y` are defeq, it should never fail.
-/
meta def change' (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit
| none (loc.ns [none]) := do e ← i_to_expr q, change_core e none
| none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh)
| none _ := fail "change-at does not support multiple locations"
| (some w) l :=
do l' ← loc.get_local_pp_names l,
l'.mmap' (λ e, try (change_with_at q w e)),
when l.include_goal $ change q w (loc.ns [none])
meta def convert_to_core (r : pexpr) : tactic unit :=
do tgt ← target,
h ← to_expr ``(_ : %%tgt = %%r),
rewrite_target h,
swap
/--
`convert_to g using n` attempts to change the current goal to `g`,
using `congr' n` to resolve discrepancies.
`convert_to g` defaults to using `congr' 1`.
-/
meta def convert_to (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit :=
match n with
| none := convert_to_core r >> `[congr' 1]
| (some 0) := convert_to_core r
| (some o) := convert_to_core r >> congr' o
end
/-- `ac_change g using n` is `convert_to g using n; try {ac_refl}` -/
meta def ac_change (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit :=
convert_to r n; try ac_refl
private meta def opt_dir_with : parser (option (bool × name)) :=
(do tk "with",
arrow ← (tk "<-")?,
h ← ident,
return (arrow.is_some, h)) <|> return none
/--
`set a := t with h` is a variant of `let a := t`.
It adds the hypothesis `h : a = t` to the local context and replaces `t` with `a` everywhere it can.
`set a := t with ←h` will add `h : t = a` instead.
`set! a := t with h` does not do any replacing.
-/
meta def set (h_simp : parse (tk "!")?) (a : parse ident) (tp : parse ((tk ":") >> texpr)?) (_ : parse (tk ":=")) (pv : parse texpr)
(rev_name : parse opt_dir_with) :=
do let vt := match tp with | some t := t | none := pexpr.mk_placeholder end,
let pv := ``(%%pv : %%vt),
v ← to_expr pv,
tp ← infer_type v,
definev a tp v,
when h_simp.is_none $ change' pv (some (expr.const a [])) loc.wildcard,
match rev_name with
| some (flip, id) :=
do nv ← get_local a,
pf ← to_expr (cond flip ``(%%pv = %%nv) ``(%%nv = %%pv)) >>= assert id,
reflexivity
| none := skip
end
/--
`clear_except h₀ h₁` deletes all the assumptions it can except for `h₀` and `h₁`.
-/
meta def clear_except (xs : parse ident *) : tactic unit :=
do let ns := name_set.of_list xs,
local_context >>= mmap' (λ h : expr,
when (¬ ns.contains h.local_pp_name) $
try $ tactic.clear h) ∘ list.reverse
meta def format_names (ns : list name) : format :=
format.join $ list.intersperse " " (ns.map to_fmt)
private meta def format_binders : list name × binder_info × expr → tactic format
| (ns, binder_info.default, t) := pformat!"({format_names ns} : {t})"
| (ns, binder_info.implicit, t) := pformat!"{{{format_names ns} : {t}}"
| (ns, binder_info.strict_implicit, t) := pformat!"⦃{format_names ns} : {t}⦄"
| ([n], binder_info.inst_implicit, t) :=
if "_".is_prefix_of n.to_string
then pformat!"[{t}]"
else pformat!"[{format_names [n]} : {t}]"
| (ns, binder_info.inst_implicit, t) := pformat!"[{format_names ns} : {t}]"
| (ns, binder_info.aux_decl, t) := pformat!"({format_names ns} : {t})"
meta def mk_paragraph_aux (right_margin : ℕ) : format → format → ℕ → list format → format
| par ln len [] := par ++ format.line ++ ln
| par ln len (x :: xs) :=
let len' := x.to_string.length in
if len + len' ≤ right_margin then
mk_paragraph_aux par (ln ++ x ++ " ") (len + len' + 1) xs
else
mk_paragraph_aux (par ++ format.line ++ ln) (" " ++ x ++ " ") len' xs
/-- `mk_paragraph right_margin ls` packs `ls` into a paragraph where the lines have
length at most `right_margin` -/
meta def mk_paragraph (right_margin : ℕ) : list format → format :=
mk_paragraph_aux right_margin "" "" 0
/--
Format the current goal as a stand-alone example. Useful for testing tactic.
* `extract_goal`: formats the statement as an `example` declaration
* `extract_goal my_decl`: formats the statement as a `lemma` or `def` declaration
called `my_decl`
* `extract_goal with i j k:` only use local constants `i`, `j`, `k` in the declaration
Examples:
```lean
example (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k :=
begin
extract_goal,
-- prints:
-- example {i j k : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k :=
-- begin
-- end
extract_goal my_lemma
-- lemma my_lemma {i j k : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k :=
-- begin
-- end
end
example {i j k x y z w p q r m n : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k) (h₁ : k ≤ p) (h₁ : p ≤ q) : i ≤ k :=
begin
extract_goal my_lemma,
-- prints:
-- lemma my_lemma {i j k x y z w p q r m n : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k)
-- (h₁ : k ≤ p) (h₁ : p ≤ q) : i ≤ k :=
-- begin
-- end
extract_goal my_lemma with i j k
-- prints:
-- lemma my_lemma {i j k : ℕ} : i ≤ k :=
-- begin
-- end
end
```
-/
meta def extract_goal (n : parse ident?) (vs : parse with_ident_list) : tactic unit :=
do (cxt,_) ← solve_aux `(true) $
when (¬ vs.empty) (clear_except vs) >>
local_context,
tgt ← target,
is_prop ← is_prop tgt,
let title := match n, is_prop with
| none, _ := to_fmt "example"
| (some n), tt := format!"lemma {n}"
| (some n), ff := format!"def {n}"
end,
cxt ← compact_decl cxt,
cxt' ← cxt.init.mmap format_binders,
cxt'' ← cxt.last'.traverse $ λ x, pformat!"{format_binders x} :",
stmt ← pformat!"{tgt} :=",
let fmt := mk_paragraph 80 $ title :: cxt' ++ [cxt''.get_or_else ":", stmt],
trace fmt,
trace!"begin\n \nend"
end interactive
end tactic
|
cff80bf2035ea3b8d532880d42db05af042437b3 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/set/intervals/disjoint.lean | a121d359b7f935406d0fc7a6b6a222a25fe807d3 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 4,090 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Yury Kudryashov
-/
import data.set.lattice
/-!
# Extra lemmas about intervals
This file contains lemmas about intervals that cannot be included into `data.set.intervals.basic`
because this would create an `import` cycle. Namely, lemmas in this file can use definitions
from `data.set.lattice`, including `disjoint`.
-/
universes u v w
variables {ι : Sort u} {α : Type v} {β : Type w}
open set order_dual (to_dual)
namespace set
section preorder
variables [preorder α] {a b c : α}
@[simp] lemma Iic_disjoint_Ioi (h : a ≤ b) : disjoint (Iic a) (Ioi b) :=
λ x ⟨ha, hb⟩, not_le_of_lt (h.trans_lt hb) ha
@[simp] lemma Iic_disjoint_Ioc (h : a ≤ b) : disjoint (Iic a) (Ioc b c) :=
(Iic_disjoint_Ioi h).mono le_rfl (λ _, and.left)
@[simp] lemma Ioc_disjoint_Ioc_same {a b c : α} : disjoint (Ioc a b) (Ioc b c) :=
(Iic_disjoint_Ioc (le_refl b)).mono (λ _, and.right) le_rfl
@[simp] lemma Ico_disjoint_Ico_same {a b c : α} : disjoint (Ico a b) (Ico b c) :=
λ x hx, not_le_of_lt hx.1.2 hx.2.1
@[simp] lemma Ici_disjoint_Iic : disjoint (Ici a) (Iic b) ↔ ¬(a ≤ b) :=
by rw [set.disjoint_iff_inter_eq_empty, Ici_inter_Iic, Icc_eq_empty_iff]
@[simp] lemma Iic_disjoint_Ici : disjoint (Iic a) (Ici b) ↔ ¬(b ≤ a) :=
disjoint.comm.trans Ici_disjoint_Iic
end preorder
section linear_order
variables [linear_order α] {a₁ a₂ b₁ b₂ : α}
@[simp] lemma Ico_disjoint_Ico : disjoint (Ico a₁ a₂) (Ico b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ :=
by simp_rw [set.disjoint_iff_inter_eq_empty, Ico_inter_Ico, Ico_eq_empty_iff,
inf_eq_min, sup_eq_max, not_lt]
@[simp] lemma Ioc_disjoint_Ioc : disjoint (Ioc a₁ a₂) (Ioc b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ :=
have h : _ ↔ min (to_dual a₁) (to_dual b₁) ≤ max (to_dual a₂) (to_dual b₂) := Ico_disjoint_Ico,
by simpa only [dual_Ico] using h
/-- If two half-open intervals are disjoint and the endpoint of one lies in the other,
then it must be equal to the endpoint of the other. -/
lemma eq_of_Ico_disjoint {x₁ x₂ y₁ y₂ : α}
(h : disjoint (Ico x₁ x₂) (Ico y₁ y₂)) (hx : x₁ < x₂) (h2 : x₂ ∈ Ico y₁ y₂) :
y₁ = x₂ :=
begin
rw [Ico_disjoint_Ico, min_eq_left (le_of_lt h2.2), le_max_iff] at h,
apply le_antisymm h2.1,
exact h.elim (λ h, absurd hx (not_lt_of_le h)) id
end
@[simp] lemma Union_Ico_eq_Iio_self_iff {f : ι → α} {a : α} :
(⋃ i, Ico (f i) a) = Iio a ↔ ∀ x < a, ∃ i, f i ≤ x :=
by simp [← Ici_inter_Iio, ← Union_inter, subset_def]
@[simp] lemma Union_Ioc_eq_Ioi_self_iff {f : ι → α} {a : α} :
(⋃ i, Ioc a (f i)) = Ioi a ↔ ∀ x, a < x → ∃ i, x ≤ f i :=
by simp [← Ioi_inter_Iic, ← inter_Union, subset_def]
@[simp] lemma bUnion_Ico_eq_Iio_self_iff {p : ι → Prop} {f : Π i, p i → α} {a : α} :
(⋃ i (hi : p i), Ico (f i hi) a) = Iio a ↔ ∀ x < a, ∃ i hi, f i hi ≤ x :=
by simp [← Ici_inter_Iio, ← Union_inter, subset_def]
@[simp] lemma bUnion_Ioc_eq_Ioi_self_iff {p : ι → Prop} {f : Π i, p i → α} {a : α} :
(⋃ i (hi : p i), Ioc a (f i hi)) = Ioi a ↔ ∀ x, a < x → ∃ i hi, x ≤ f i hi :=
by simp [← Ioi_inter_Iic, ← inter_Union, subset_def]
end linear_order
end set
section Union_Ixx
variables [linear_order α] {s : set α} {a : α} {f : ι → α}
lemma is_glb.bUnion_Ioi_eq (h : is_glb s a) : (⋃ x ∈ s, Ioi x) = Ioi a :=
begin
refine (Union₂_subset $ λ x hx, _).antisymm (λ x hx, _),
{ exact Ioi_subset_Ioi (h.1 hx) },
{ rcases h.exists_between hx with ⟨y, hys, hay, hyx⟩,
exact mem_bUnion hys hyx }
end
lemma is_glb.Union_Ioi_eq (h : is_glb (range f) a) :
(⋃ x, Ioi (f x)) = Ioi a :=
bUnion_range.symm.trans h.bUnion_Ioi_eq
lemma is_lub.bUnion_Iio_eq (h : is_lub s a) :
(⋃ x ∈ s, Iio x) = Iio a :=
h.dual.bUnion_Ioi_eq
lemma is_lub.Union_Iio_eq (h : is_lub (range f) a) :
(⋃ x, Iio (f x)) = Iio a :=
h.dual.Union_Ioi_eq
end Union_Ixx
|
5db274d41bb7fc038fd011725dbacc161b6f26b0 | 0845ae2ca02071debcfd4ac24be871236c01784f | /library/init/lean/compiler/ir/freevars.lean | 803327e6659a23341950e49577f214eac4378e8a | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 9,535 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.lean.compiler.ir.basic
namespace Lean
namespace IR
namespace MaxIndex
/- Compute the maximum index `M` used in a declaration.
We `M` to initialize the fresh index generator used to create fresh
variable and join point names.
Recall that we variable and join points share the same namespace in
our implementation.
-/
abbrev Collector := Index → Index
@[inline] private def skip : Collector := id
@[inline] private def collect (x : Index) : Collector := fun y => if x > y then x else y
@[inline] private def collectVar (x : VarId) : Collector := collect x.idx
@[inline] private def collectJP (j : JoinPointId) : Collector := collect j.idx
@[inline] private def seq (k₁ k₂ : Collector) : Collector := k₂ ∘ k₁
instance : HasAndthen Collector := ⟨seq⟩
private def collectArg : Arg → Collector
| (Arg.var x) := collectVar x
| irrelevant := skip
@[specialize] private def collectArray {α : Type} (as : Array α) (f : α → Collector) : Collector :=
fun m => as.foldl (fun m a => f a m) m
private def collectArgs (as : Array Arg) : Collector := collectArray as collectArg
private def collectParam (p : Param) : Collector := collectVar p.x
private def collectParams (ps : Array Param) : Collector := collectArray ps collectParam
private def collectExpr : Expr → Collector
| (Expr.ctor _ ys) := collectArgs ys
| (Expr.reset _ x) := collectVar x
| (Expr.reuse x _ _ ys) := collectVar x >> collectArgs ys
| (Expr.proj _ x) := collectVar x
| (Expr.uproj _ x) := collectVar x
| (Expr.sproj _ _ x) := collectVar x
| (Expr.fap _ ys) := collectArgs ys
| (Expr.pap _ ys) := collectArgs ys
| (Expr.ap x ys) := collectVar x >> collectArgs ys
| (Expr.box _ x) := collectVar x
| (Expr.unbox x) := collectVar x
| (Expr.lit v) := skip
| (Expr.isShared x) := collectVar x
| (Expr.isTaggedPtr x) := collectVar x
private def collectAlts (f : FnBody → Collector) (alts : Array Alt) : Collector :=
collectArray alts $ fun alt => f alt.body
partial def collectFnBody : FnBody → Collector
| (FnBody.vdecl x _ v b) := collectExpr v >> collectFnBody b
| (FnBody.jdecl j ys v b) := collectFnBody v >> collectParams ys >> collectFnBody b
| (FnBody.set x _ y b) := collectVar x >> collectArg y >> collectFnBody b
| (FnBody.uset x _ y b) := collectVar x >> collectVar y >> collectFnBody b
| (FnBody.sset x _ _ y _ b) := collectVar x >> collectVar y >> collectFnBody b
| (FnBody.setTag x _ b) := collectVar x >> collectFnBody b
| (FnBody.inc x _ _ b) := collectVar x >> collectFnBody b
| (FnBody.dec x _ _ b) := collectVar x >> collectFnBody b
| (FnBody.del x b) := collectVar x >> collectFnBody b
| (FnBody.mdata _ b) := collectFnBody b
| (FnBody.case _ x alts) := collectVar x >> collectAlts collectFnBody alts
| (FnBody.jmp j ys) := collectJP j >> collectArgs ys
| (FnBody.ret x) := collectArg x
| FnBody.unreachable := skip
partial def collectDecl : Decl → Collector
| (Decl.fdecl _ xs _ b) := collectParams xs >> collectFnBody b
| (Decl.extern _ xs _ _) := collectParams xs
end MaxIndex
def FnBody.maxIndex (b : FnBody) : Index :=
MaxIndex.collectFnBody b 0
def Decl.maxIndex (d : Decl) : Index :=
MaxIndex.collectDecl d 0
namespace FreeIndices
/- We say a variable (join point) index (aka name) is free in a function body
if there isn't a `FnBody.vdecl` (`Fnbody.jdecl`) binding it. -/
abbrev Collector := IndexSet → IndexSet → IndexSet
@[inline] private def skip : Collector :=
fun bv fv => fv
@[inline] private def collectIndex (x : Index) : Collector :=
fun bv fv => if bv.contains x then fv else fv.insert x
@[inline] private def collectVar (x : VarId) : Collector :=
collectIndex x.idx
@[inline] private def collectJP (x : JoinPointId) : Collector :=
collectIndex x.idx
@[inline] private def withIndex (x : Index) : Collector → Collector :=
fun k bv fv => k (bv.insert x) fv
@[inline] private def withVar (x : VarId) : Collector → Collector :=
withIndex x.idx
@[inline] private def withJP (x : JoinPointId) : Collector → Collector :=
withIndex x.idx
def insertParams (s : IndexSet) (ys : Array Param) : IndexSet :=
ys.foldl (fun s p => s.insert p.x.idx) s
@[inline] private def withParams (ys : Array Param) : Collector → Collector :=
fun k bv fv => k (insertParams bv ys) fv
@[inline] private def seq : Collector → Collector → Collector :=
fun k₁ k₂ bv fv => k₂ bv (k₁ bv fv)
instance : HasAndthen Collector := ⟨seq⟩
private def collectArg : Arg → Collector
| (Arg.var x) := collectVar x
| irrelevant := skip
@[specialize] private def collectArray {α : Type} (as : Array α) (f : α → Collector) : Collector :=
fun bv fv => as.foldl (fun fv a => f a bv fv) fv
private def collectArgs (as : Array Arg) : Collector :=
collectArray as collectArg
private def collectExpr : Expr → Collector
| (Expr.ctor _ ys) := collectArgs ys
| (Expr.reset _ x) := collectVar x
| (Expr.reuse x _ _ ys) := collectVar x >> collectArgs ys
| (Expr.proj _ x) := collectVar x
| (Expr.uproj _ x) := collectVar x
| (Expr.sproj _ _ x) := collectVar x
| (Expr.fap _ ys) := collectArgs ys
| (Expr.pap _ ys) := collectArgs ys
| (Expr.ap x ys) := collectVar x >> collectArgs ys
| (Expr.box _ x) := collectVar x
| (Expr.unbox x) := collectVar x
| (Expr.lit v) := skip
| (Expr.isShared x) := collectVar x
| (Expr.isTaggedPtr x) := collectVar x
private def collectAlts (f : FnBody → Collector) (alts : Array Alt) : Collector :=
collectArray alts $ fun alt => f alt.body
partial def collectFnBody : FnBody → Collector
| (FnBody.vdecl x _ v b) := collectExpr v >> withVar x (collectFnBody b)
| (FnBody.jdecl j ys v b) := withParams ys (collectFnBody v) >> withJP j (collectFnBody b)
| (FnBody.set x _ y b) := collectVar x >> collectArg y >> collectFnBody b
| (FnBody.uset x _ y b) := collectVar x >> collectVar y >> collectFnBody b
| (FnBody.sset x _ _ y _ b) := collectVar x >> collectVar y >> collectFnBody b
| (FnBody.setTag x _ b) := collectVar x >> collectFnBody b
| (FnBody.inc x _ _ b) := collectVar x >> collectFnBody b
| (FnBody.dec x _ _ b) := collectVar x >> collectFnBody b
| (FnBody.del x b) := collectVar x >> collectFnBody b
| (FnBody.mdata _ b) := collectFnBody b
| (FnBody.case _ x alts) := collectVar x >> collectAlts collectFnBody alts
| (FnBody.jmp j ys) := collectJP j >> collectArgs ys
| (FnBody.ret x) := collectArg x
| FnBody.unreachable := skip
end FreeIndices
def FnBody.collectFreeIndices (b : FnBody) (vs : IndexSet) : IndexSet :=
FreeIndices.collectFnBody b {} vs
def FnBody.freeIndices (b : FnBody) : IndexSet :=
b.collectFreeIndices {}
namespace HasIndex
/- In principle, we can check whether a function body `b` contains an index `i` using
`b.freeIndices.contains i`, but it is more efficient to avoid the construction
of the set of freeIndices and just search whether `i` occurs in `b` or not.
-/
def visitVar (w : Index) (x : VarId) : Bool := w == x.idx
def visitJP (w : Index) (x : JoinPointId) : Bool := w == x.idx
def visitArg (w : Index) : Arg → Bool
| (Arg.var x) := visitVar w x
| _ := false
def visitArgs (w : Index) (xs : Array Arg) : Bool :=
xs.any (visitArg w)
def visitParams (w : Index) (ps : Array Param) : Bool :=
ps.any (fun p => w == p.x.idx)
def visitExpr (w : Index) : Expr → Bool
| (Expr.ctor _ ys) := visitArgs w ys
| (Expr.reset _ x) := visitVar w x
| (Expr.reuse x _ _ ys) := visitVar w x || visitArgs w ys
| (Expr.proj _ x) := visitVar w x
| (Expr.uproj _ x) := visitVar w x
| (Expr.sproj _ _ x) := visitVar w x
| (Expr.fap _ ys) := visitArgs w ys
| (Expr.pap _ ys) := visitArgs w ys
| (Expr.ap x ys) := visitVar w x || visitArgs w ys
| (Expr.box _ x) := visitVar w x
| (Expr.unbox x) := visitVar w x
| (Expr.lit v) := false
| (Expr.isShared x) := visitVar w x
| (Expr.isTaggedPtr x) := visitVar w x
partial def visitFnBody (w : Index) : FnBody → Bool
| (FnBody.vdecl x _ v b) := visitExpr w v || visitFnBody b
| (FnBody.jdecl j ys v b) := visitFnBody v || visitFnBody b
| (FnBody.set x _ y b) := visitVar w x || visitArg w y || visitFnBody b
| (FnBody.uset x _ y b) := visitVar w x || visitVar w y || visitFnBody b
| (FnBody.sset x _ _ y _ b) := visitVar w x || visitVar w y || visitFnBody b
| (FnBody.setTag x _ b) := visitVar w x || visitFnBody b
| (FnBody.inc x _ _ b) := visitVar w x || visitFnBody b
| (FnBody.dec x _ _ b) := visitVar w x || visitFnBody b
| (FnBody.del x b) := visitVar w x || visitFnBody b
| (FnBody.mdata _ b) := visitFnBody b
| (FnBody.jmp j ys) := visitJP w j || visitArgs w ys
| (FnBody.ret x) := visitArg w x
| (FnBody.case _ x alts) := visitVar w x || alts.any (fun alt => visitFnBody alt.body)
| (FnBody.unreachable) := false
end HasIndex
def Arg.hasFreeVar (arg : Arg) (x : VarId) : Bool := HasIndex.visitArg x.idx arg
def Expr.hasFreeVar (e : Expr) (x : VarId) : Bool := HasIndex.visitExpr x.idx e
def FnBody.hasFreeVar (b : FnBody) (x : VarId) : Bool := HasIndex.visitFnBody x.idx b
end IR
end Lean
|
2d62e636514a9fc06e4800ca34d113069b026ff1 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/order/category/BoolAlg.lean | 7301e685995a9f54ba19e47cc063d5b71aa4b0ec | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 2,565 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import order.category.BoundedDistribLattice
/-!
# The category of boolean algebras
This defines `BoolAlg`, the category of boolean algebras.
-/
open order_dual opposite set
universes u
open category_theory
/-- The category of boolean algebras. -/
def BoolAlg := bundled boolean_algebra
namespace BoolAlg
instance : has_coe_to_sort BoolAlg Type* := bundled.has_coe_to_sort
instance (X : BoolAlg) : boolean_algebra X := X.str
/-- Construct a bundled `BoolAlg` from a `boolean_algebra`. -/
def of (α : Type*) [boolean_algebra α] : BoolAlg := bundled.of α
@[simp] lemma coe_of (α : Type*) [boolean_algebra α] : ↥(of α) = α := rfl
instance : inhabited BoolAlg := ⟨of punit⟩
/-- Turn a `BoolAlg` into a `BoundedDistribLattice` by forgetting its complement operation. -/
def to_BoundedDistribLattice (X : BoolAlg) : BoundedDistribLattice := BoundedDistribLattice.of X
@[simp] lemma coe_to_BoundedDistribLattice (X : BoolAlg) : ↥X.to_BoundedDistribLattice = ↥X := rfl
instance : large_category.{u} BoolAlg := induced_category.category to_BoundedDistribLattice
instance : concrete_category BoolAlg := induced_category.concrete_category to_BoundedDistribLattice
instance has_forget_to_BoundedDistribLattice : has_forget₂ BoolAlg BoundedDistribLattice :=
induced_category.has_forget₂ to_BoundedDistribLattice
/-- Constructs an equivalence between boolean algebras from an order isomorphism
between them. -/
@[simps] def iso.mk {α β : BoolAlg.{u}} (e : α ≃o β) : α ≅ β :=
{ hom := (e : bounded_lattice_hom α β),
inv := (e.symm : bounded_lattice_hom β α),
hom_inv_id' := by { ext, exact e.symm_apply_apply _ },
inv_hom_id' := by { ext, exact e.apply_symm_apply _ } }
/-- `order_dual` as a functor. -/
@[simps] def dual : BoolAlg ⥤ BoolAlg :=
{ obj := λ X, of Xᵒᵈ, map := λ X Y, bounded_lattice_hom.dual }
/-- The equivalence between `BoolAlg` and itself induced by `order_dual` both ways. -/
@[simps functor inverse] def dual_equiv : BoolAlg ≌ BoolAlg :=
equivalence.mk dual dual
(nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl)
(nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl)
end BoolAlg
lemma BoolAlg_dual_comp_forget_to_BoundedDistribLattice :
BoolAlg.dual ⋙ forget₂ BoolAlg BoundedDistribLattice =
forget₂ BoolAlg BoundedDistribLattice ⋙ BoundedDistribLattice.dual := rfl
|
59595f4197bd91b9aa260ac655b70b7f93d65125 | 137c667471a40116a7afd7261f030b30180468c2 | /src/data/sym2.lean | c9542a385046f8efefa4b2437588f4fd944fc446 | [
"Apache-2.0"
] | permissive | bragadeesh153/mathlib | 46bf814cfb1eecb34b5d1549b9117dc60f657792 | b577bb2cd1f96eb47031878256856020b76f73cd | refs/heads/master | 1,687,435,188,334 | 1,626,384,207,000 | 1,626,384,207,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,158 | lean | /-
Copyright (c) 2020 Kyle Miller All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller
-/
import tactic.linarith
import data.sym
/-!
# The symmetric square
This file defines the symmetric square, which is `α × α` modulo
swapping. This is also known as the type of unordered pairs.
More generally, the symmetric square is the second symmetric power
(see `data.sym`). The equivalence is `sym2.equiv_sym`.
From the point of view that an unordered pair is equivalent to a
multiset of cardinality two (see `sym2.equiv_multiset`), there is a
`has_mem` instance `sym2.mem`, which is a `Prop`-valued membership
test. Given `h : a ∈ z` for `z : sym2 α`, then `h.other` is the other
element of the pair, defined using `classical.choice`. If `α` has
decidable equality, then `h.other'` computably gives the other element.
Recall that an undirected graph (allowing self loops, but no multiple
edges) is equivalent to a symmetric relation on the vertex type `α`.
Given a symmetric relation on `α`, the corresponding edge set is
constructed by `sym2.from_rel`.
## Notation
The symmetric square has a setoid instance, so `⟦(a, b)⟧` denotes a
term of the symmetric square.
## Tags
symmetric square, unordered pairs, symmetric powers
-/
open function
open sym
universe u
variables {α : Type u}
namespace sym2
/--
This is the relation capturing the notion of pairs equivalent up to permutations.
-/
inductive rel (α : Type u) : (α × α) → (α × α) → Prop
| refl (x y : α) : rel (x, y) (x, y)
| swap (x y : α) : rel (x, y) (y, x)
attribute [refl] rel.refl
@[symm] lemma rel.symm {x y : α × α} : rel α x y → rel α y x :=
by rintro ⟨_, _⟩; constructor
@[trans] lemma rel.trans {x y z : α × α} : rel α x y → rel α y z → rel α x z :=
by { intros a b, cases_matching* rel _ _ _; apply rel.refl <|> apply rel.swap }
lemma rel.is_equivalence : equivalence (rel α) := by tidy; apply rel.trans; assumption
instance rel.setoid (α : Type u) : setoid (α × α) := ⟨rel α, rel.is_equivalence⟩
end sym2
/--
`sym2 α` is the symmetric square of `α`, which, in other words, is the
type of unordered pairs.
It is equivalent in a natural way to multisets of cardinality 2 (see
`sym2.equiv_multiset`).
-/
@[reducible]
def sym2 (α : Type u) := quotient (sym2.rel.setoid α)
namespace sym2
lemma eq_swap {a b : α} : ⟦(a, b)⟧ = ⟦(b, a)⟧ :=
by { rw quotient.eq, apply rel.swap }
lemma congr_right {a b c : α} : ⟦(a, b)⟧ = ⟦(a, c)⟧ ↔ b = c :=
by { split; intro h, { rw quotient.eq at h, cases h; refl }, rw h }
lemma congr_left {a b c : α} : ⟦(b, a)⟧ = ⟦(c, a)⟧ ↔ b = c :=
by { split; intro h, { rw quotient.eq at h, cases h; refl }, rw h }
lemma eq_iff {x y z w : α} :
⟦(x, y)⟧ = ⟦(z, w)⟧ ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) :=
begin
split; intro h,
{ rw quotient.eq at h, cases h; tidy },
{ cases h; rw [h.1, h.2], rw eq_swap }
end
/--
The functor `sym2` is functorial, and this function constructs the induced maps.
-/
def map {α β : Type*} (f : α → β) : sym2 α → sym2 β :=
quotient.map (prod.map f f)
(by { rintros _ _ h, cases h, { refl }, apply rel.swap })
@[simp]
lemma map_id : sym2.map (@id α) = id := by tidy
lemma map_comp {α β γ : Type*} {g : β → γ} {f : α → β} :
sym2.map (g ∘ f) = sym2.map g ∘ sym2.map f := by tidy
lemma map_map {α β γ : Type*} {g : β → γ} {f : α → β} (x : sym2 α) :
map g (map f x) = map (g ∘ f) x := by tidy
@[simp]
lemma map_pair_eq {α β : Type*} (f : α → β) (x y : α) : map f ⟦(x, y)⟧ = ⟦(f x, f y)⟧ := rfl
lemma map.injective {α β : Type*} {f : α → β} (hinj : injective f) : injective (map f) :=
begin
intros z z',
refine quotient.ind₂ (λ z z', _) z z',
cases z with x y,
cases z' with x' y',
repeat { rw [map_pair_eq, eq_iff] },
rintro (h|h); simp [hinj h.1, hinj h.2],
end
section membership
/-! ### Declarations about membership -/
/--
This is a predicate that determines whether a given term is a member of a term of the
symmetric square. From this point of view, the symmetric square is the subtype of
cardinality-two multisets on `α`.
-/
def mem (x : α) (z : sym2 α) : Prop :=
∃ (y : α), z = ⟦(x, y)⟧
instance : has_mem α (sym2 α) := ⟨mem⟩
lemma mk_has_mem (x y : α) : x ∈ ⟦(x, y)⟧ := ⟨y, rfl⟩
lemma mk_has_mem_right (x y : α) : y ∈ ⟦(x, y)⟧ := by { rw eq_swap, apply mk_has_mem }
/--
Given an element of the unordered pair, give the other element using `classical.some`.
See also `mem.other'` for the computable version.
-/
noncomputable def mem.other {a : α} {z : sym2 α} (h : a ∈ z) : α :=
classical.some h
@[simp]
lemma mem_other_spec {a : α} {z : sym2 α} (h : a ∈ z) : ⟦(a, h.other)⟧ = z :=
by erw ← classical.some_spec h
@[simp] lemma mem_iff {a b c : α} : a ∈ ⟦(b, c)⟧ ↔ a = b ∨ a = c :=
{ mp := by { rintro ⟨_, h⟩, rw eq_iff at h, tidy },
mpr := by { rintro ⟨_⟩; subst a, { apply mk_has_mem }, apply mk_has_mem_right } }
lemma mem_other_mem {a : α} {z : sym2 α} (h : a ∈ z) :
h.other ∈ z :=
by { convert mk_has_mem_right a h.other, rw mem_other_spec h }
lemma elems_iff_eq {x y : α} {z : sym2 α} (hne : x ≠ y) :
x ∈ z ∧ y ∈ z ↔ z = ⟦(x, y)⟧ :=
begin
split,
{ refine quotient.rec_on_subsingleton z _,
rintros ⟨z₁, z₂⟩ ⟨hx, hy⟩,
rw eq_iff,
cases mem_iff.mp hx with hx hx; cases mem_iff.mp hy with hy hy; cc },
{ rintro rfl, simp },
end
@[ext]
lemma sym2_ext (z z' : sym2 α) (h : ∀ x, x ∈ z ↔ x ∈ z') : z = z' :=
begin
refine quotient.rec_on_subsingleton z (λ w, _) h,
refine quotient.rec_on_subsingleton z' (λ w', _),
intro h,
cases w with x y, cases w' with x' y',
simp only [mem_iff] at h,
apply eq_iff.mpr,
have hx := h x, have hy := h y, have hx' := h x', have hy' := h y',
simp only [true_iff, true_or, eq_self_iff_true, iff_true, or_true] at hx hy hx' hy',
cases hx; subst x; cases hy; subst y; cases hx'; try { subst x' }; cases hy'; try { subst y' };
cc,
end
instance mem.decidable [decidable_eq α] (x : α) (z : sym2 α) : decidable (x ∈ z) :=
quotient.rec_on_subsingleton z (λ ⟨y₁, y₂⟩, decidable_of_iff' _ mem_iff)
end membership
/--
A type `α` is naturally included in the diagonal of `α × α`, and this function gives the image
of this diagonal in `sym2 α`.
-/
def diag (x : α) : sym2 α := ⟦(x, x)⟧
/--
A predicate for testing whether an element of `sym2 α` is on the diagonal.
-/
def is_diag (z : sym2 α) : Prop := z ∈ set.range (@diag α)
@[simp]
lemma diag_is_diag (a : α) : is_diag (diag a) :=
by use a
@[simp]
lemma is_diag_iff_proj_eq (z : α × α) : is_diag ⟦z⟧ ↔ z.1 = z.2 :=
begin
cases z with a, split,
{ rintro ⟨_, h⟩, dsimp only, erw eq_iff at h, rcases h; cc },
{ rintro ⟨⟩, use a, refl },
end
instance is_diag.decidable_pred (α : Type u) [decidable_eq α] : decidable_pred (@is_diag α) :=
by { refine λ z, quotient.rec_on_subsingleton z (λ a, _), erw is_diag_iff_proj_eq, apply_instance }
lemma mem_other_ne {a : α} {z : sym2 α} (hd : ¬is_diag z) (h : a ∈ z) : h.other ≠ a :=
begin
intro hn, apply hd,
have h' := sym2.mem_other_spec h,
rw hn at h',
rw ←h',
simp,
end
section relations
/-! ### Declarations about symmetric relations -/
variables {r : α → α → Prop}
/--
Symmetric relations define a set on `sym2 α` by taking all those pairs
of elements that are related.
-/
def from_rel (sym : symmetric r) : set (sym2 α) :=
quotient.lift (uncurry r) (by { rintros _ _ ⟨_, _⟩, tidy })
@[simp]
lemma from_rel_proj_prop {sym : symmetric r} {z : α × α} :
⟦z⟧ ∈ from_rel sym ↔ r z.1 z.2 := iff.rfl
@[simp]
lemma from_rel_prop {sym : symmetric r} {a b : α} :
⟦(a, b)⟧ ∈ from_rel sym ↔ r a b := by simp only [from_rel_proj_prop]
lemma from_rel_irreflexive {sym : symmetric r} :
irreflexive r ↔ ∀ {z}, z ∈ from_rel sym → ¬is_diag z :=
{ mp := by { intros h z hr hd, induction z,
erw is_diag_iff_proj_eq at hd, erw from_rel_proj_prop at hr, tidy },
mpr := by { intros h x hr, rw ← @from_rel_prop _ _ sym at hr, exact h hr ⟨x, rfl⟩ }}
lemma mem_from_rel_irrefl_other_ne {sym : symmetric r} (irrefl : irreflexive r)
{a : α} {z : sym2 α} (hz : z ∈ from_rel sym) (h : a ∈ z) : h.other ≠ a :=
mem_other_ne (from_rel_irreflexive.mp irrefl hz) h
instance from_rel.decidable_pred (sym : symmetric r) [h : decidable_rel r] :
decidable_pred (∈ sym2.from_rel sym) :=
λ z, quotient.rec_on_subsingleton z (λ x, h _ _)
end relations
section sym_equiv
/-! ### Equivalence to the second symmetric power -/
local attribute [instance] vector.perm.is_setoid
private def from_vector {α : Type*} : vector α 2 → α × α
| ⟨[a, b], h⟩ := (a, b)
private lemma perm_card_two_iff {α : Type*} {a₁ b₁ a₂ b₂ : α} :
[a₁, b₁].perm [a₂, b₂] ↔ (a₁ = a₂ ∧ b₁ = b₂) ∨ (a₁ = b₂ ∧ b₁ = a₂) :=
{ mp := by { simp [← multiset.coe_eq_coe, ← multiset.cons_coe, multiset.cons_eq_cons]; tidy },
mpr := by { intro h, cases h; rw [h.1, h.2], apply list.perm.swap', refl } }
/--
The symmetric square is equivalent to length-2 vectors up to permutations.
-/
def sym2_equiv_sym' {α : Type*} : equiv (sym2 α) (sym' α 2) :=
{ to_fun := quotient.map
(λ (x : α × α), ⟨[x.1, x.2], rfl⟩)
(by { rintros _ _ ⟨_⟩, { refl }, apply list.perm.swap', refl }),
inv_fun := quotient.map from_vector (begin
rintros ⟨x, hx⟩ ⟨y, hy⟩ h,
cases x with _ x, { simpa using hx, },
cases x with _ x, { simpa using hx, },
cases x with _ x, swap, { exfalso, simp at hx, linarith [hx] },
cases y with _ y, { simpa using hy, },
cases y with _ y, { simpa using hy, },
cases y with _ y, swap, { exfalso, simp at hy, linarith [hy] },
rcases perm_card_two_iff.mp h with ⟨rfl,rfl⟩|⟨rfl,rfl⟩, { refl },
apply sym2.rel.swap,
end),
left_inv := by tidy,
right_inv := λ x, begin
refine quotient.rec_on_subsingleton x (λ x, _),
{ cases x with x hx,
cases x with _ x, { simpa using hx, },
cases x with _ x, { simpa using hx, },
cases x with _ x, swap, { exfalso, simp at hx, linarith [hx] },
refl },
end }
/--
The symmetric square is equivalent to the second symmetric power.
-/
def equiv_sym (α : Type*) : sym2 α ≃ sym α 2 :=
equiv.trans sym2_equiv_sym' sym_equiv_sym'.symm
/--
The symmetric square is equivalent to multisets of cardinality
two. (This is currently a synonym for `equiv_sym`, but it's provided
in case the definition for `sym` changes.)
-/
def equiv_multiset (α : Type*) : sym2 α ≃ {s : multiset α // s.card = 2} :=
equiv_sym α
end sym_equiv
section decidable
/--
An algorithm for computing `sym2.rel`.
-/
def rel_bool [decidable_eq α] (x y : α × α) : bool :=
if x.1 = y.1 then x.2 = y.2 else
if x.1 = y.2 then x.2 = y.1 else ff
lemma rel_bool_spec [decidable_eq α] (x y : α × α) :
↥(rel_bool x y) ↔ rel α x y :=
begin
cases x with x₁ x₂, cases y with y₁ y₂,
dsimp [rel_bool], split_ifs;
simp only [false_iff, bool.coe_sort_ff, bool.of_to_bool_iff],
rotate 2, { contrapose! h, cases h; cc },
all_goals { subst x₁, split; intro h1,
{ subst h1; apply sym2.rel.swap },
{ cases h1; cc } }
end
/--
Given `[decidable_eq α]` and `[fintype α]`, the following instance gives `fintype (sym2 α)`.
-/
instance (α : Type*) [decidable_eq α] : decidable_rel (sym2.rel α) :=
λ x y, decidable_of_bool (rel_bool x y) (rel_bool_spec x y)
/--
A function that gives the other element of a pair given one of the elements. Used in `mem.other'`.
-/
private def pair_other [decidable_eq α] (a : α) (z : α × α) : α := if a = z.1 then z.2 else z.1
/--
Get the other element of the unordered pair using the decidable equality.
This is the computable version of `mem.other`.
-/
def mem.other' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : α :=
quot.rec (λ x h', pair_other a x) (begin
clear h z,
intros x y h,
ext hy,
convert_to pair_other a x = _,
{ have h' : ∀ {c e h}, @eq.rec _ ⟦x⟧ (λ s, a ∈ s → α)
(λ _, pair_other a x) c e h = pair_other a x,
{ intros _ e _, subst e },
apply h', },
have h' := (rel_bool_spec x y).mpr h,
cases x with x₁ x₂, cases y with y₁ y₂,
cases mem_iff.mp hy with hy'; subst a; dsimp [rel_bool] at h';
split_ifs at h'; try { rw bool.of_to_bool_iff at h', subst x₁, subst x₂ }; dsimp [pair_other],
simp only [ne.symm h_1, if_true, eq_self_iff_true, if_false],
exfalso, exact bool.not_ff h',
simp only [h_1, if_true, eq_self_iff_true, if_false],
exfalso, exact bool.not_ff h',
end) z h
@[simp]
lemma mem_other_spec' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) :
⟦(a, h.other')⟧ = z :=
begin
induction z, cases z with x y,
have h' := mem_iff.mp h,
dsimp [mem.other', quot.rec, pair_other],
cases h'; subst a,
{ simp only [if_true, eq_self_iff_true], refl, },
{ split_ifs, subst h_1, refl, rw eq_swap, refl, },
refl,
end
@[simp]
lemma other_eq_other' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : h.other = h.other' :=
by rw [←congr_right, mem_other_spec' h, mem_other_spec]
lemma mem_other_mem' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) :
h.other' ∈ z :=
by { rw ←other_eq_other', exact mem_other_mem h }
lemma other_invol' [decidable_eq α] {a : α} {z : sym2 α} (ha : a ∈ z) (hb : ha.other' ∈ z):
hb.other' = a :=
begin
induction z, cases z with x y,
dsimp [mem.other', quot.rec, pair_other] at hb,
split_ifs at hb; dsimp [mem.other', quot.rec, pair_other],
simp only [h, if_true, eq_self_iff_true],
split_ifs, assumption, refl,
simp only [h, if_false, if_true, eq_self_iff_true],
cases mem_iff.mp ha; cc,
refl,
end
lemma other_invol {a : α} {z : sym2 α} (ha : a ∈ z) (hb : ha.other ∈ z):
hb.other = a :=
begin
classical,
rw other_eq_other' at hb ⊢,
convert other_invol' ha hb,
rw other_eq_other',
end
end decidable
end sym2
|
4b78ba6039956fb6131a05e0ae585d0c357ca9e7 | 4727251e0cd73359b15b664c3170e5d754078599 | /archive/100-theorems-list/82_cubing_a_cube.lean | 1f94ec93cee2c0132b19b799ad404762bcf9c779 | [
"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 | 23,626 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import data.fin.tuple
import data.real.basic
import data.set.intervals
import data.set.pairwise
import set_theory.cardinal.basic
/-!
Proof that a cube (in dimension n ≥ 3) cannot be cubed:
There does not exist a partition of a cube into finitely many smaller cubes (at least two)
of different sizes.
We follow the proof described here:
http://www.alaricstephen.com/main-featured/2017/9/28/cubing-a-cube-proof
-/
open real set function fin
open_locale cardinal
noncomputable theory
variable {n : ℕ}
/-- Given three intervals `I, J, K` such that `J ⊂ I`,
neither endpoint of `J` coincides with an endpoint of `I`, `¬ (K ⊆ J)` and
`K` does not lie completely to the left nor completely to the right of `J`.
Then `I ∩ K \ J` is nonempty. -/
lemma Ico_lemma {α} [linear_order α] {x₁ x₂ y₁ y₂ z₁ z₂ w : α}
(h₁ : x₁ < y₁) (hy : y₁ < y₂) (h₂ : y₂ < x₂)
(hz₁ : z₁ ≤ y₂) (hz₂ : y₁ ≤ z₂) (hw : w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂) :
∃w, w ∈ Ico x₁ x₂ ∧ w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂ :=
begin
simp only [not_and, not_lt, mem_Ico] at hw,
refine ⟨max x₁ (min w y₂), _, _, _⟩,
{ simp [le_refl, lt_trans h₁ (lt_trans hy h₂), h₂] },
{ simp [hw, lt_irrefl, not_le_of_lt h₁] {contextual := tt} },
{ simp [hw.2.1, hw.2.2, hz₁, lt_of_lt_of_le h₁ hz₂] at ⊢ }
end
/-- A (hyper)-cube (in standard orientation) is a vector `b` consisting of the bottom-left point
of the cube, a width `w` and a proof that `w > 0`. We use functions from `fin n` to denote vectors.
-/
structure cube (n : ℕ) : Type :=
(b : fin n → ℝ) -- bottom-left coordinate
(w : ℝ) -- width
(hw : 0 < w)
namespace cube
lemma hw' (c : cube n) : 0 ≤ c.w := le_of_lt c.hw
/-- The j-th side of a cube is the half-open interval `[b j, b j + w)` -/
def side (c : cube n) (j : fin n) : set ℝ :=
Ico (c.b j) (c.b j + c.w)
@[simp] lemma b_mem_side (c : cube n) (j : fin n) : c.b j ∈ c.side j :=
by simp [side, cube.hw, le_refl]
def to_set (c : cube n) : set (fin n → ℝ) :=
{ x | ∀j, x j ∈ side c j }
def to_set_subset {c c' : cube n} : c.to_set ⊆ c'.to_set ↔ ∀j, c.side j ⊆ c'.side j :=
begin
split, intros h j x hx,
let f : fin n → ℝ := λ j', if j' = j then x else c.b j',
have : f ∈ c.to_set,
{ intro j', by_cases hj' : j' = j; simp [f, hj', if_pos, if_neg, hx] },
convert h this j, { simp [f, if_pos] },
intros h f hf j, exact h j (hf j)
end
def to_set_disjoint {c c' : cube n} : disjoint c.to_set c'.to_set ↔
∃j, disjoint (c.side j) (c'.side j) :=
begin
split, intros h, classical, by_contra h',
simp only [not_disjoint_iff, classical.skolem, not_exists] at h',
cases h' with f hf,
apply not_disjoint_iff.mpr ⟨f, _, _⟩ h; intro j, exact (hf j).1, exact (hf j).2,
rintro ⟨j, hj⟩, rw [set.disjoint_iff], rintros f ⟨h1f, h2f⟩,
apply not_disjoint_iff.mpr ⟨f j, h1f j, h2f j⟩ hj
end
lemma b_mem_to_set (c : cube n) : c.b ∈ c.to_set :=
by simp [to_set]
protected def tail (c : cube (n+1)) : cube n :=
⟨tail c.b, c.w, c.hw⟩
lemma side_tail (c : cube (n+1)) (j : fin n) : c.tail.side j = c.side j.succ := rfl
def bottom (c : cube (n+1)) : set (fin (n+1) → ℝ) :=
{ x | x 0 = c.b 0 ∧ tail x ∈ c.tail.to_set }
lemma b_mem_bottom (c : cube (n+1)) : c.b ∈ c.bottom :=
by simp [bottom, to_set, side, cube.hw, le_refl, cube.tail]
def xm (c : cube (n+1)) : ℝ :=
c.b 0 + c.w
lemma b_lt_xm (c : cube (n+1)) : c.b 0 < c.xm := by simp [xm, hw]
lemma b_ne_xm (c : cube (n+1)) : c.b 0 ≠ c.xm := ne_of_lt c.b_lt_xm
def shift_up (c : cube (n+1)) : cube (n+1) :=
⟨cons c.xm $ tail c.b, c.w, c.hw⟩
@[simp] lemma tail_shift_up (c : cube (n+1)) : c.shift_up.tail = c.tail :=
by simp [shift_up, cube.tail]
@[simp] lemma head_shift_up (c : cube (n+1)) : c.shift_up.b 0 = c.xm := rfl
def unit_cube : cube n :=
⟨λ _, 0, 1, by norm_num⟩
@[simp] lemma side_unit_cube {j : fin n} : unit_cube.side j = Ico 0 1 :=
by norm_num [unit_cube, side]
end cube
open cube
variables {ι : Type} [fintype ι] {cs : ι → cube (n+1)} {i i' : ι}
/-- A finite family of (at least 2) cubes partitioning the unit cube with different sizes -/
def correct (cs : ι → cube n) : Prop :=
pairwise (disjoint on (cube.to_set ∘ cs)) ∧
(⋃(i : ι), (cs i).to_set) = unit_cube.to_set ∧
injective (cube.w ∘ cs) ∧
2 ≤ #ι ∧
3 ≤ n
variable (h : correct cs)
include h
lemma to_set_subset_unit_cube {i} : (cs i).to_set ⊆ unit_cube.to_set :=
by { rw [←h.2.1], exact subset_Union _ i }
lemma side_subset {i j} : (cs i).side j ⊆ Ico 0 1 :=
by { have := to_set_subset_unit_cube h, rw [to_set_subset] at this,
convert this j, norm_num [unit_cube] }
lemma zero_le_of_mem_side {i j x} (hx : x ∈ (cs i).side j) : 0 ≤ x :=
(side_subset h hx).1
lemma zero_le_of_mem {i p} (hp : p ∈ (cs i).to_set) (j) : 0 ≤ p j :=
zero_le_of_mem_side h (hp j)
lemma zero_le_b {i j} : 0 ≤ (cs i).b j :=
zero_le_of_mem h (cs i).b_mem_to_set j
lemma b_add_w_le_one {j} : (cs i).b j + (cs i).w ≤ 1 :=
by { have := side_subset h, rw [side, Ico_subset_Ico_iff] at this, convert this.2, simp [hw] }
/-- The width of any cube in the partition cannot be 1. -/
lemma w_ne_one (i : ι) : (cs i).w ≠ 1 :=
begin
intro hi,
have := h.2.2.2.1, rw [cardinal.two_le_iff' i] at this, cases this with i' hi',
let p := (cs i').b,
have hp : p ∈ (cs i').to_set := (cs i').b_mem_to_set,
have h2p : p ∈ (cs i).to_set,
{ intro j, split,
transitivity (0 : ℝ),
{ rw [←add_le_add_iff_right (1 : ℝ)], convert b_add_w_le_one h, rw hi, rw zero_add },
apply zero_le_b h, apply lt_of_lt_of_le (side_subset h $ (cs i').b_mem_side j).2,
simp [hi, zero_le_b h] },
apply not_disjoint_iff.mpr ⟨p, hp, h2p⟩,
apply h.1, exact hi'.symm
end
/-- The top of a cube (which is the bottom of the cube shifted up by its width) must be covered by
bottoms of (other) cubes in the family. -/
lemma shift_up_bottom_subset_bottoms (hc : (cs i).xm ≠ 1) :
(cs i).shift_up.bottom ⊆ ⋃(i : ι), (cs i).bottom :=
begin
intros p hp, cases hp with hp0 hps, rw [tail_shift_up] at hps,
have : p ∈ (unit_cube : cube (n+1)).to_set,
{ simp only [to_set, forall_fin_succ, hp0, side_unit_cube, mem_set_of_eq, mem_Ico,
head_shift_up], refine ⟨⟨_, _⟩, _⟩,
{ rw [←zero_add (0 : ℝ)], apply add_le_add, apply zero_le_b h, apply (cs i).hw' },
{ exact lt_of_le_of_ne (b_add_w_le_one h) hc },
intro j, exact side_subset h (hps j) },
rw [←h.2.1] at this, rcases this with ⟨_, ⟨i', rfl⟩, hi'⟩,
rw [mem_Union], use i', refine ⟨_, λ j, hi' j.succ⟩,
have : i ≠ i', { rintro rfl, apply not_le_of_lt (hi' 0).2, rw [hp0], refl },
have := h.1 i i' this, rw [on_fun, to_set_disjoint, exists_fin_succ] at this,
rcases this with h0|⟨j, hj⟩,
rw [hp0], symmetry, apply eq_of_Ico_disjoint h0 (by simp [hw]) _,
convert hi' 0, rw [hp0], refl,
exfalso, apply not_disjoint_iff.mpr ⟨tail p j, hps j, hi' j.succ⟩ hj
end
omit h
/-- A valley is a square on which cubes in the family of cubes are placed, so that the cubes
completely cover the valley and none of those cubes is partially outside the square.
We also require that no cube on it has the same size as the valley (so that there are at least
two cubes on the valley).
This is the main concept in the formalization.
We prove that the smallest cube on a valley has another valley on the top of it, which
gives an infinite sequence of cubes in the partition, which contradicts the finiteness.
A valley is characterized by a cube `c` (which is not a cube in the family cs) by considering
the bottom face of `c`. -/
def valley (cs : ι → cube (n+1)) (c : cube (n+1)) : Prop :=
c.bottom ⊆ (⋃(i : ι), (cs i).bottom) ∧
(∀i, (cs i).b 0 = c.b 0 → (∃x, x ∈ (cs i).tail.to_set ∩ c.tail.to_set) →
(cs i).tail.to_set ⊆ c.tail.to_set) ∧
∀(i : ι), (cs i).b 0 = c.b 0 → (cs i).w ≠ c.w
variables {c : cube (n+1)} (v : valley cs c)
/-- The bottom of the unit cube is a valley -/
lemma valley_unit_cube (h : correct cs) : valley cs unit_cube :=
begin
refine ⟨_, _, _⟩,
{ intro v,
simp only [bottom, and_imp, mem_Union, mem_set_of_eq],
intros h0 hv,
have : v ∈ (unit_cube : cube (n+1)).to_set,
{ dsimp only [to_set, unit_cube, mem_set_of_eq],
rw [forall_fin_succ, h0], split, norm_num [side, unit_cube], exact hv },
rw [←h.2.1] at this, rcases this with ⟨_, ⟨i, rfl⟩, hi⟩,
use i,
split, { apply le_antisymm, rw h0, exact zero_le_b h, exact (hi 0).1 },
intro j, exact hi _ },
{ intros i hi h', rw to_set_subset, intro j, convert side_subset h using 1, simp [side_tail] },
{ intros i hi, exact w_ne_one h i }
end
/-- the cubes which lie in the valley `c` -/
def bcubes (cs : ι → cube (n+1)) (c : cube (n+1)) : set ι :=
{ i : ι | (cs i).b 0 = c.b 0 ∧ (cs i).tail.to_set ⊆ c.tail.to_set }
/-- A cube which lies on the boundary of a valley in dimension `j` -/
def on_boundary (hi : i ∈ bcubes cs c) (j : fin n) : Prop :=
c.b j.succ = (cs i).b j.succ ∨ (cs i).b j.succ + (cs i).w = c.b j.succ + c.w
lemma tail_sub (hi : i ∈ bcubes cs c) : ∀j, (cs i).tail.side j ⊆ c.tail.side j :=
by { rw [←to_set_subset], exact hi.2 }
lemma bottom_mem_side (hi : i ∈ bcubes cs c) : c.b 0 ∈ (cs i).side 0 :=
by { convert b_mem_side (cs i) _ using 1, rw hi.1 }
lemma b_le_b (hi : i ∈ bcubes cs c) (j : fin n) : c.b j.succ ≤ (cs i).b j.succ :=
(tail_sub hi j $ b_mem_side _ _).1
lemma t_le_t (hi : i ∈ bcubes cs c) (j : fin n) :
(cs i).b j.succ + (cs i).w ≤ c.b j.succ + c.w :=
begin
have h' := tail_sub hi j, dsimp only [side] at h', rw [Ico_subset_Ico_iff] at h',
exact h'.2, simp [hw]
end
include h v
/-- Every cube in the valley must be smaller than it -/
lemma w_lt_w (hi : i ∈ bcubes cs c) : (cs i).w < c.w :=
begin
apply lt_of_le_of_ne _ (v.2.2 i hi.1),
have j : fin n := ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩,
rw [←add_le_add_iff_left ((cs i).b j.succ)],
apply le_trans (t_le_t hi j), rw [add_le_add_iff_right], apply b_le_b hi,
end
open cardinal
/-- There are at least two cubes in a valley -/
lemma two_le_mk_bcubes : 2 ≤ #(bcubes cs c) :=
begin
rw [two_le_iff],
rcases v.1 c.b_mem_bottom with ⟨_, ⟨i, rfl⟩, hi⟩,
have h2i : i ∈ bcubes cs c :=
⟨hi.1.symm, v.2.1 i hi.1.symm ⟨tail c.b, hi.2, λ j, c.b_mem_side j.succ⟩⟩,
let j : fin (n+1) := ⟨2, h.2.2.2.2⟩,
have hj : 0 ≠ j := by { simp only [fin.ext_iff, ne.def], contradiction },
let p : fin (n+1) → ℝ := λ j', if j' = j then c.b j + (cs i).w else c.b j',
have hp : p ∈ c.bottom,
{ split, { simp only [bottom, p, if_neg hj] },
intro j', simp only [tail, side_tail],
by_cases hj' : j'.succ = j,
{ simp [p, -add_comm, if_pos, side, hj', hw', w_lt_w h v h2i] },
{ simp [p, -add_comm, if_neg hj'] }},
rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩,
have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩,
refine ⟨⟨i, h2i⟩, ⟨i', h2i'⟩, _⟩,
intro hii', cases congr_arg subtype.val hii',
apply not_le_of_lt (hi'.2 ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩).2,
simp only [-add_comm, tail, cube.tail, p],
rw [if_pos, add_le_add_iff_right],
{ exact (hi.2 _).1 },
refl
end
/-- There is a cube in the valley -/
lemma nonempty_bcubes : (bcubes cs c).nonempty :=
begin
rw [←set.ne_empty_iff_nonempty], intro h', have := two_le_mk_bcubes h v, rw h' at this,
apply not_lt_of_le this, rw mk_emptyc, norm_cast, norm_num
end
/-- There is a smallest cube in the valley -/
lemma exists_mi : ∃(i : ι), i ∈ bcubes cs c ∧ ∀(i' ∈ bcubes cs c),
(cs i).w ≤ (cs i').w :=
by simpa
using (bcubes cs c).exists_min_image (λ i, (cs i).w) (finite.of_fintype _) (nonempty_bcubes h v)
/-- We let `mi` be the (index for the) smallest cube in the valley `c` -/
def mi : ι := classical.some $ exists_mi h v
variables {h v}
lemma mi_mem_bcubes : mi h v ∈ bcubes cs c :=
(classical.some_spec $ exists_mi h v).1
lemma mi_minimal (hi : i ∈ bcubes cs c) : (cs $ mi h v).w ≤ (cs i).w :=
(classical.some_spec $ exists_mi h v).2 i hi
lemma mi_strict_minimal (hii' : mi h v ≠ i) (hi : i ∈ bcubes cs c) :
(cs $ mi h v).w < (cs i).w :=
by { apply lt_of_le_of_ne (mi_minimal hi), apply h.2.2.1.ne, apply hii' }
/-- The top of `mi` cannot be 1, since there is a larger cube in the valley -/
lemma mi_xm_ne_one : (cs $ mi h v).xm ≠ 1 :=
begin
apply ne_of_lt, rcases (two_le_iff' _).mp (two_le_mk_bcubes h v) with ⟨⟨i, hi⟩, h2i⟩,
swap, exact ⟨mi h v, mi_mem_bcubes⟩,
apply lt_of_lt_of_le _ (b_add_w_le_one h), exact i, exact 0,
rw [xm, mi_mem_bcubes.1, hi.1, _root_.add_lt_add_iff_left],
apply mi_strict_minimal _ hi, intro h', apply h2i, rw subtype.ext_iff_val, exact h'
end
/-- If `mi` lies on the boundary of the valley in dimension j, then this lemma expresses that all
other cubes on the same boundary extend further from the boundary.
More precisely, there is a j-th coordinate `x : ℝ` in the valley, but not in `mi`,
such that every cube that shares a (particular) j-th coordinate with `mi` also contains j-th
coordinate `x` -/
lemma smallest_on_boundary {j} (bi : on_boundary (mi_mem_bcubes : mi h v ∈ _) j) :
∃(x : ℝ), x ∈ c.side j.succ \ (cs $ mi h v).side j.succ ∧
∀{{i'}} (hi' : i' ∈ bcubes cs c), i' ≠ mi h v →
(cs $ mi h v).b j.succ ∈ (cs i').side j.succ → x ∈ (cs i').side j.succ :=
begin
let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes,
cases bi,
{ refine ⟨(cs i).b j.succ + (cs i).w, ⟨_, _⟩, _⟩,
{ simp [side, bi, hw', w_lt_w h v hi] },
{ intro h', simpa [i, lt_irrefl] using h'.2 },
intros i' hi' i'_i h2i', split,
apply le_trans h2i'.1, { simp [hw'] },
apply lt_of_lt_of_le (add_lt_add_left (mi_strict_minimal i'_i.symm hi') _),
simp [bi.symm, b_le_b hi'] },
let s := bcubes cs c \ { i },
have hs : s.nonempty,
{ rcases (two_le_iff' (⟨i, hi⟩ : bcubes cs c)).mp (two_le_mk_bcubes h v) with ⟨⟨i', hi'⟩, h2i'⟩,
refine ⟨i', hi', _⟩, simp only [mem_singleton_iff], intro h, apply h2i', simp [h] },
rcases set.exists_min_image s (w ∘ cs) (finite.of_fintype _) hs with ⟨i', ⟨hi', h2i'⟩, h3i'⟩,
rw [mem_singleton_iff] at h2i',
let x := c.b j.succ + c.w - (cs i').w,
have hx : x < (cs i).b j.succ,
{ dsimp only [x], rw [←bi, add_sub_assoc, add_lt_iff_neg_left, sub_lt_zero],
apply mi_strict_minimal (ne.symm h2i') hi' },
refine ⟨x, ⟨_, _⟩, _⟩,
{ simp only [side, x, -add_comm, -add_assoc, neg_lt_zero, hw, add_lt_iff_neg_left, and_true,
mem_Ico, sub_eq_add_neg],
rw [add_assoc, le_add_iff_nonneg_right, ←sub_eq_add_neg, sub_nonneg],
apply le_of_lt (w_lt_w h v hi') },
{ simp only [side, not_and_distrib, not_lt, add_comm, not_le, mem_Ico], left, exact hx },
intros i'' hi'' h2i'' h3i'', split, swap, apply lt_trans hx h3i''.2,
simp only [x], rw [le_sub_iff_add_le],
refine le_trans _ (t_le_t hi'' j), rw [add_le_add_iff_left], apply h3i' i'' ⟨hi'', _⟩,
simp [mem_singleton, h2i'']
end
variables (h v)
/-- `mi` cannot lie on the boundary of the valley. Otherwise, the cube adjacent to it in the `j`-th
direction will intersect one of the neighbouring cubes on the same boundary as `mi`. -/
lemma mi_not_on_boundary (j : fin n) : ¬on_boundary (mi_mem_bcubes : mi h v ∈ _) j :=
begin
let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes,
rcases (two_le_iff' j).mp _ with ⟨j', hj'⟩, swap,
{ rw [mk_fin, ←nat.cast_two, nat_cast_le], apply nat.le_of_succ_le_succ h.2.2.2.2 },
intro hj,
rcases smallest_on_boundary hj with ⟨x, ⟨hx, h2x⟩, h3x⟩,
let p : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j then x else (cs i).b j₂.succ),
have hp : p ∈ c.bottom,
{ suffices : ∀ (j' : fin n), ite (j' = j) x ((cs i).b j'.succ) ∈ c.side j'.succ,
{ simpa [bottom, p, to_set, tail, side_tail] },
intro j₂,
by_cases hj₂ : j₂ = j, { simp [hj₂, hx] },
simp only [hj₂, if_false], apply tail_sub hi, apply b_mem_side },
rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩,
have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩,
have i_i' : i ≠ i', { rintro rfl, simpa [p, side_tail, i, h2x] using hi'.2 j },
have : nonempty ↥((cs i').tail.side j' \ (cs i).tail.side j'),
{ apply nonempty_Ico_sdiff, apply mi_strict_minimal i_i' h2i', apply hw },
rcases this with ⟨⟨x', hx'⟩⟩,
let p' : fin (n+1) → ℝ :=
cons (c.b 0) (λ j₂, if j₂ = j' then x' else (cs i).b j₂.succ),
have hp' : p' ∈ c.bottom,
{ suffices : ∀ (j : fin n), ite (j = j') x' ((cs i).b j.succ) ∈ c.side j.succ,
{ simpa [bottom, p', to_set, tail, side_tail] },
intro j₂,
by_cases hj₂ : j₂ = j', simp [hj₂], apply tail_sub h2i', apply hx'.1,
simp only [if_congr, if_false, hj₂], apply tail_sub hi, apply b_mem_side },
rcases v.1 hp' with ⟨_, ⟨i'', rfl⟩, hi''⟩,
have h2i'' : i'' ∈ bcubes cs c := ⟨hi''.1.symm, v.2.1 i'' hi''.1.symm ⟨tail p', hi''.2, hp'.2⟩⟩,
have i'_i'' : i' ≠ i'',
{ rintro ⟨⟩,
have : (cs i).b ∈ (cs i').to_set,
{ simp only [to_set, forall_fin_succ, hi.1, bottom_mem_side h2i', true_and, mem_set_of_eq],
intro j₂, by_cases hj₂ : j₂ = j,
{ simpa [side_tail, p', hj', hj₂] using hi''.2 j },
{ simpa [hj₂] using hi'.2 j₂ } },
apply not_disjoint_iff.mpr ⟨(cs i).b, (cs i).b_mem_to_set, this⟩ (h.1 i i' i_i') },
have i_i'' : i ≠ i'', { intro h, induction h, simpa [hx'.2] using hi''.2 j' },
apply not.elim _ (h.1 i' i'' i'_i''),
simp only [on_fun, to_set_disjoint, not_disjoint_iff, forall_fin_succ, not_exists, comp_app],
refine ⟨⟨c.b 0, bottom_mem_side h2i', bottom_mem_side h2i''⟩, _⟩,
intro j₂,
by_cases hj₂ : j₂ = j,
{ cases hj₂, refine ⟨x, _, _⟩,
{ convert hi'.2 j, simp [p] },
apply h3x h2i'' i_i''.symm, convert hi''.2 j, simp [p', hj'] },
by_cases h2j₂ : j₂ = j',
{ cases h2j₂, refine ⟨x', hx'.1, _⟩, convert hi''.2 j', simp },
refine ⟨(cs i).b j₂.succ, _, _⟩,
{ convert hi'.2 j₂, simp [hj₂] },
{ convert hi''.2 j₂, simp [h2j₂] }
end
variables {h v}
/-- The same result that `mi` cannot lie on the boundary of the valley written as inequalities. -/
lemma mi_not_on_boundary' (j : fin n) : c.tail.b j < (cs (mi h v)).tail.b j ∧
(cs (mi h v)).tail.b j + (cs (mi h v)).w < c.tail.b j + c.w :=
begin
have := mi_not_on_boundary h v j,
simp only [on_boundary, not_or_distrib] at this, cases this with h1 h2,
split,
apply lt_of_le_of_ne (b_le_b mi_mem_bcubes _) h1,
apply lt_of_le_of_ne _ h2,
apply ((Ico_subset_Ico_iff _).mp (tail_sub mi_mem_bcubes j)).2,
simp [hw]
end
/-- The top of `mi` gives rise to a new valley, since the neighbouring cubes extend further upward
than `mi`. -/
def valley_mi : valley cs ((cs (mi h v)).shift_up) :=
begin
let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes,
refine ⟨_, _, _⟩,
{ intro p, apply shift_up_bottom_subset_bottoms h mi_xm_ne_one },
{ rintros i' hi' ⟨p2, hp2, h2p2⟩, simp only [head_shift_up] at hi', classical, by_contra h2i',
rw [tail_shift_up] at h2p2, simp only [not_subset, tail_shift_up] at h2i',
rcases h2i' with ⟨p1, hp1, h2p1⟩,
have : ∃p3, p3 ∈ (cs i').tail.to_set ∧ p3 ∉ (cs i).tail.to_set ∧ p3 ∈ c.tail.to_set,
{ simp only [to_set, not_forall, mem_set_of_eq] at h2p1, cases h2p1 with j hj,
rcases Ico_lemma (mi_not_on_boundary' j).1 (by simp [hw]) (mi_not_on_boundary' j).2
(le_trans (hp2 j).1 $ le_of_lt (h2p2 j).2)
(le_trans (h2p2 j).1 $ le_of_lt (hp2 j).2) ⟨hj, hp1 j⟩ with ⟨w, hw, h2w, h3w⟩,
refine ⟨λ j', if j' = j then w else p2 j', _, _, _⟩,
{ intro j', by_cases h : j' = j,
{ simp only [if_pos h], convert h3w },
{ simp only [if_neg h], exact hp2 j' } },
{ simp only [to_set, not_forall, mem_set_of_eq], use j, rw [if_pos rfl], convert h2w },
{ intro j', by_cases h : j' = j,
{ simp only [if_pos h, side_tail], convert hw },
{ simp only [if_neg h], apply hi.2, apply h2p2 } } },
rcases this with ⟨p3, h1p3, h2p3, h3p3⟩,
let p := @cons n (λ_, ℝ) (c.b 0) p3,
have hp : p ∈ c.bottom, { refine ⟨rfl, _⟩, rwa [tail_cons] },
rcases v.1 hp with ⟨_, ⟨i'', rfl⟩, hi''⟩,
have h2i'' : i'' ∈ bcubes cs c,
{ use hi''.1.symm, apply v.2.1 i'' hi''.1.symm,
use tail p, split, exact hi''.2, rw [tail_cons], exact h3p3 },
have h3i'' : (cs i).w < (cs i'').w,
{ apply mi_strict_minimal _ h2i'', rintro rfl, apply h2p3, convert hi''.2, rw [tail_cons] },
let p' := @cons n (λ_, ℝ) (cs i).xm p3,
have hp' : p' ∈ (cs i').to_set,
{ simpa [to_set, forall_fin_succ, p', hi'.symm] using h1p3 },
have h2p' : p' ∈ (cs i'').to_set,
{ simp only [to_set, forall_fin_succ, p', cons_succ, cons_zero, mem_set_of_eq],
refine ⟨_, by simpa [to_set, p] using hi''.2⟩,
have : (cs i).b 0 = (cs i'').b 0, { rw [hi.1, h2i''.1] },
simp [side, hw', xm, this, h3i''] },
apply not_disjoint_iff.mpr ⟨p', hp', h2p'⟩,
apply h.1, rintro rfl, apply (cs i).b_ne_xm, rw [←hi', ←hi''.1, hi.1], refl },
{ intros i' hi' h2i',
dsimp only [shift_up] at h2i',
replace h2i' := h.2.2.1 h2i'.symm,
induction h2i',
exact b_ne_xm (cs i) hi' }
end
variables (h)
omit v
/-- We get a sequence of cubes whose size is decreasing -/
noncomputable def sequence_of_cubes : ℕ → { i : ι // valley cs ((cs i).shift_up) }
| 0 := let v := valley_unit_cube h in ⟨mi h v, valley_mi⟩
| (k+1) := let v := (sequence_of_cubes k).2 in ⟨mi h v, valley_mi⟩
def decreasing_sequence (k : ℕ) : ℝ := (cs (sequence_of_cubes h k).1).w
lemma strict_anti_sequence_of_cubes : strict_anti $ decreasing_sequence h :=
strict_anti_nat_of_succ_lt $ λ k,
begin
let v := (sequence_of_cubes h k).2, dsimp only [decreasing_sequence, sequence_of_cubes],
apply w_lt_w h v (mi_mem_bcubes : mi h v ∈ _),
end
omit h
/-- The infinite sequence of cubes contradicts the finiteness of the family. -/
theorem not_correct : ¬correct cs :=
begin
intro h, apply (lt_omega_of_fintype ι).not_le,
rw [omega, lift_id], fapply mk_le_of_injective, exact λ n, (sequence_of_cubes h n).1,
intros n m hnm, apply (strict_anti_sequence_of_cubes h).injective,
dsimp only [decreasing_sequence], rw hnm
end
/-- **Dissection of Cubes**: A cube cannot be cubed. -/
theorem cannot_cube_a_cube :
∀{n : ℕ}, n ≥ 3 → -- In ℝ^n for n ≥ 3
∀{ι : Type} [fintype ι] {cs : ι → cube n}, -- given a finite collection of (hyper)cubes
2 ≤ #ι → -- containing at least two elements
pairwise (disjoint on (cube.to_set ∘ cs)) → -- which is pairwise disjoint
(⋃(i : ι), (cs i).to_set) = unit_cube.to_set → -- whose union is the unit cube
injective (cube.w ∘ cs) → -- such that the widths of all cubes are different
false := -- then we can derive a contradiction
begin
intros n hn ι hι cs h1 h2 h3 h4, resetI,
rcases n, cases hn,
exact not_correct ⟨h2, h3, h4, h1, hn⟩
end
|
03120e7ec83dad41ee267f24e2e4cfeb66d9c1b7 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/matchRw.lean | 6672cd121915402881965da432859f8af8ad7083 | [
"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 | 313 | lean | def f (x y : Nat) :=
match x with
| 0 => y + 1
| n+1 => y + 1
private theorem matchEq (x y : Nat) :
(match x with
| 0 => y + 1
| n+1 => y + 1) = y + 1 := by
cases x <;> simp
theorem fex1 : f x y = y + 1 := by
simp [f, matchEq]
theorem fex2 : f x y = y + 1 := by
simp [f]
rw [matchEq]
|
528fa78e62df963cdd91cbea8b7e277f7f0c2803 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/analysis/special_functions/arsinh.lean | 49d41ae88a67b007587ab5ce81f8995c0f03fbd7 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 8,753 | lean | /-
Copyright (c) 2020 James Arthur. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: James Arthur, Chris Hughes, Shing Tak Lam
-/
import analysis.special_functions.trigonometric.deriv
import analysis.special_functions.log.basic
/-!
# Inverse of the sinh function
In this file we prove that sinh is bijective and hence has an
inverse, arsinh.
## Main definitions
- `real.arsinh`: The inverse function of `real.sinh`.
- `real.sinh_equiv`, `real.sinh_order_iso`, `real.sinh_homeomorph`: `real.sinh` as an `equiv`,
`order_iso`, and `homeomorph`, respectively.
## Main Results
- `real.sinh_surjective`, `real.sinh_bijective`: `real.sinh` is surjective and bijective;
- `real.arsinh_injective`, `real.arsinh_surjective`, `real.arsinh_bijective`: `real.arsinh` is
injective, surjective, and bijective;
- `real.continuous_arsinh`, `real.differentiable_arsinh`, `real.cont_diff_arsinh`: `real.arsinh` is
continuous, differentiable, and continuously differentiable; we also provide dot notation
convenience lemmas like `filter.tendsto.arsinh` and `cont_diff_at.arsinh`.
## Tags
arsinh, arcsinh, argsinh, asinh, sinh injective, sinh bijective, sinh surjective
-/
noncomputable theory
open function filter set
open_locale topological_space
namespace real
variables {x y : ℝ}
/-- `arsinh` is defined using a logarithm, `arsinh x = log (x + sqrt(1 + x^2))`. -/
@[pp_nodot] def arsinh (x : ℝ) := log (x + sqrt (1 + x^2))
lemma exp_arsinh (x : ℝ) : exp (arsinh x) = x + sqrt (1 + x^2) :=
begin
apply exp_log,
rw [← neg_lt_iff_pos_add'],
calc -x ≤ sqrt (x ^ 2) : le_sqrt_of_sq_le (neg_pow_bit0 _ _).le
... < sqrt (1 + x ^ 2) : sqrt_lt_sqrt (sq_nonneg _) (lt_one_add _)
end
@[simp] lemma arsinh_zero : arsinh 0 = 0 := by simp [arsinh]
@[simp] lemma arsinh_neg (x : ℝ) : arsinh (-x) = -arsinh x :=
begin
rw [← exp_eq_exp, exp_arsinh, exp_neg, exp_arsinh],
apply eq_inv_of_mul_eq_one_left,
rw [neg_sq, neg_add_eq_sub, add_comm x, mul_comm, ← sq_sub_sq, sq_sqrt, add_sub_cancel],
exact add_nonneg zero_le_one (sq_nonneg _)
end
/-- `arsinh` is the right inverse of `sinh`. -/
@[simp] lemma sinh_arsinh (x : ℝ) : sinh (arsinh x) = x :=
by { rw [sinh_eq, ← arsinh_neg, exp_arsinh, exp_arsinh, neg_sq], field_simp }
@[simp] lemma cosh_arsinh (x : ℝ) : cosh (arsinh x) = sqrt (1 + x^2) :=
by rw [← sqrt_sq (cosh_pos _).le, cosh_sq', sinh_arsinh]
/-- `sinh` is surjective, `∀ b, ∃ a, sinh a = b`. In this case, we use `a = arsinh b`. -/
lemma sinh_surjective : surjective sinh := left_inverse.surjective sinh_arsinh
/-- `sinh` is bijective, both injective and surjective. -/
lemma sinh_bijective : bijective sinh := ⟨sinh_injective, sinh_surjective⟩
/-- `arsinh` is the left inverse of `sinh`. -/
@[simp] lemma arsinh_sinh (x : ℝ) : arsinh (sinh x) = x :=
right_inverse_of_injective_of_left_inverse sinh_injective sinh_arsinh x
/-- `real.sinh` as an `equiv`. -/
@[simps] def sinh_equiv : ℝ ≃ ℝ :=
{ to_fun := sinh,
inv_fun := arsinh,
left_inv := arsinh_sinh,
right_inv := sinh_arsinh }
/-- `real.sinh` as an `order_iso`. -/
@[simps { fully_applied := ff }] def sinh_order_iso : ℝ ≃o ℝ :=
{ to_equiv := sinh_equiv,
map_rel_iff' := @sinh_le_sinh }
/-- `real.sinh` as a `homeomorph`. -/
@[simps { fully_applied := ff }] def sinh_homeomorph : ℝ ≃ₜ ℝ := sinh_order_iso.to_homeomorph
lemma arsinh_bijective : bijective arsinh := sinh_equiv.symm.bijective
lemma arsinh_injective : injective arsinh := sinh_equiv.symm.injective
lemma arsinh_surjective : surjective arsinh := sinh_equiv.symm.surjective
lemma arsinh_strict_mono : strict_mono arsinh := sinh_order_iso.symm.strict_mono
@[simp] lemma arsinh_inj : arsinh x = arsinh y ↔ x = y := arsinh_injective.eq_iff
@[simp] lemma arsinh_le_arsinh : arsinh x ≤ arsinh y ↔ x ≤ y := sinh_order_iso.symm.le_iff_le
@[simp] lemma arsinh_lt_arsinh : arsinh x < arsinh y ↔ x < y := sinh_order_iso.symm.lt_iff_lt
@[simp] lemma arsinh_eq_zero_iff : arsinh x = 0 ↔ x = 0 :=
arsinh_injective.eq_iff' arsinh_zero
@[simp] lemma arsinh_nonneg_iff : 0 ≤ arsinh x ↔ 0 ≤ x :=
by rw [← sinh_le_sinh, sinh_zero, sinh_arsinh]
@[simp] lemma arsinh_nonpos_iff : arsinh x ≤ 0 ↔ x ≤ 0 :=
by rw [← sinh_le_sinh, sinh_zero, sinh_arsinh]
@[simp] lemma arsinh_pos_iff : 0 < arsinh x ↔ 0 < x :=
lt_iff_lt_of_le_iff_le arsinh_nonpos_iff
@[simp] lemma arsinh_neg_iff : arsinh x < 0 ↔ x < 0 :=
lt_iff_lt_of_le_iff_le arsinh_nonneg_iff
lemma has_strict_deriv_at_arsinh (x : ℝ) : has_strict_deriv_at arsinh (sqrt (1 + x ^ 2))⁻¹ x :=
begin
convert sinh_homeomorph.to_local_homeomorph.has_strict_deriv_at_symm (mem_univ x)
(cosh_pos _).ne' (has_strict_deriv_at_sinh _),
exact (cosh_arsinh _).symm
end
lemma has_deriv_at_arsinh (x : ℝ) : has_deriv_at arsinh (sqrt (1 + x ^ 2))⁻¹ x :=
(has_strict_deriv_at_arsinh x).has_deriv_at
lemma differentiable_arsinh : differentiable ℝ arsinh :=
λ x, (has_deriv_at_arsinh x).differentiable_at
lemma cont_diff_arsinh {n : with_top ℕ} : cont_diff ℝ n arsinh :=
sinh_homeomorph.cont_diff_symm_deriv (λ x, (cosh_pos x).ne') has_deriv_at_sinh cont_diff_sinh
@[continuity] lemma continuous_arsinh : continuous arsinh := sinh_homeomorph.symm.continuous
end real
open real
lemma filter.tendsto.arsinh {α : Type*} {l : filter α} {f : α → ℝ} {a : ℝ}
(h : tendsto f l (𝓝 a)) : tendsto (λ x, arsinh (f x)) l (𝓝 (arsinh a)) :=
(continuous_arsinh.tendsto _).comp h
section continuous
variables {X : Type*} [topological_space X] {f : X → ℝ} {s : set X} {a : X}
lemma continuous_at.arsinh (h : continuous_at f a) : continuous_at (λ x, arsinh (f x)) a := h.arsinh
lemma continuous_within_at.arsinh (h : continuous_within_at f s a) :
continuous_within_at (λ x, arsinh (f x)) s a :=
h.arsinh
lemma continuous_on.arsinh (h : continuous_on f s) : continuous_on (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
lemma continuous.arsinh (h : continuous f) : continuous (λ x, arsinh (f x)) :=
continuous_arsinh.comp h
end continuous
section fderiv
variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {s : set E} {a : E}
{f' : E →L[ℝ] ℝ} {n : with_top ℕ}
lemma has_strict_fderiv_at.arsinh (hf : has_strict_fderiv_at f f' a) :
has_strict_fderiv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_strict_deriv_at_arsinh _).comp_has_strict_fderiv_at a hf
lemma has_fderiv_at.arsinh (hf : has_fderiv_at f f' a) :
has_fderiv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_deriv_at_arsinh _).comp_has_fderiv_at a hf
lemma has_fderiv_within_at.arsinh (hf : has_fderiv_within_at f f' s a) :
has_fderiv_within_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') s a :=
(has_deriv_at_arsinh _).comp_has_fderiv_within_at a hf
lemma differentiable_at.arsinh (h : differentiable_at ℝ f a) :
differentiable_at ℝ (λ x, arsinh (f x)) a :=
(differentiable_arsinh _).comp a h
lemma differentiable_within_at.arsinh (h : differentiable_within_at ℝ f s a) :
differentiable_within_at ℝ (λ x, arsinh (f x)) s a :=
(differentiable_arsinh _).comp_differentiable_within_at a h
lemma differentiable_on.arsinh (h : differentiable_on ℝ f s) :
differentiable_on ℝ (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
lemma differentiable.arsinh (h : differentiable ℝ f) :
differentiable ℝ (λ x, arsinh (f x)) :=
differentiable_arsinh.comp h
lemma cont_diff_at.arsinh (h : cont_diff_at ℝ n f a) :
cont_diff_at ℝ n (λ x, arsinh (f x)) a :=
cont_diff_arsinh.cont_diff_at.comp a h
lemma cont_diff_within_at.arsinh (h : cont_diff_within_at ℝ n f s a) :
cont_diff_within_at ℝ n (λ x, arsinh (f x)) s a :=
cont_diff_arsinh.cont_diff_at.comp_cont_diff_within_at a h
lemma cont_diff.arsinh (h : cont_diff ℝ n f) : cont_diff ℝ n (λ x, arsinh (f x)) :=
cont_diff_arsinh.comp h
lemma cont_diff_on.arsinh (h : cont_diff_on ℝ n f s) : cont_diff_on ℝ n (λ x, arsinh (f x)) s :=
λ x hx, (h x hx).arsinh
end fderiv
section deriv
variables {f : ℝ → ℝ} {s : set ℝ} {a f' : ℝ}
lemma has_strict_deriv_at.arsinh (hf : has_strict_deriv_at f f' a) :
has_strict_deriv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_strict_deriv_at_arsinh _).comp a hf
lemma has_deriv_at.arsinh (hf : has_deriv_at f f' a) :
has_deriv_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') a :=
(has_deriv_at_arsinh _).comp a hf
lemma has_deriv_within_at.arsinh (hf : has_deriv_within_at f f' s a) :
has_deriv_within_at (λ x, arsinh (f x)) ((sqrt (1 + (f a) ^ 2))⁻¹ • f') s a :=
(has_deriv_at_arsinh _).comp_has_deriv_within_at a hf
end deriv
|
a0b2bfacbdccd9e0efa97847cb28ce24652b30cd | 205f0fc16279a69ea36e9fd158e3a97b06834ce2 | /src/00_Introduction/03_truth_is_proof.lean | 04dc6f9bc286cf3175661f7be482f56c1a71080a | [] | no_license | kevinsullivan/cs-dm-lean | b21d3ca1a9b2a0751ba13fcb4e7b258010a5d124 | a06a94e98be77170ca1df486c8189338b16cf6c6 | refs/heads/master | 1,585,948,743,595 | 1,544,339,346,000 | 1,544,339,346,000 | 155,570,767 | 1 | 3 | null | 1,541,540,372,000 | 1,540,995,993,000 | Lean | UTF-8 | Lean | false | false | 3,127 | lean | /-
Now here we're going to pull a little trick.
It's based on the idea that a "proof", whatever
that is (!), serves as "evidence" that justifies
a truth judgment about a proposition. So we are
now technically distinguishing between a proof
of a proposition and the truth judgment that it
justifies. Nevertheless, we will assume that a
truth judgment for some proposition, P, is in
fact justified if and only if we have a proof
of P.
To denote the fact that we have a proof, p,
of some proposition, P, we will write p : P.
Now, the key idea is that having a proof is
the evidence we need to judge that P is true.
Moreover, if we've judged P to be true, then
we must have a proof, p, of P.
In other words, P is true if and only if we
have evidence (a proof), p, for P. We can
write this statement in the language of logic
like this: "p : P → P : true". We'd read
this as saying "if p is a proof of P then P
is true."
The evidence, i.e., proof, p, for P, justifies
the judgement, P : true. Moreover, the only
way to justify such a judgement is with such
a piece of evidence, i.e., with a proof. In
the logic of Lean, proof is truth and truth
requires a proof.
-/
/-
Given that having a proof of P means that one
can conclude the truth of P, we can write an
equivalent inference rule like this (compare
with the previous):
p : P, q : Q
------------ (and.intro)
r : P ∧ Q
This then says, "If you give me a proof, p, of
some proposition, P, and if you also given me a
proof, q, of some proposition Q, then I promise
to give you back a proof, r, of the proposition,
P ∧ Q."
As a shorthand, mathematicians and logicians
would usually elide the ": true" and the "p :"
bits, so you'd usually see this rule written
informally like this:
P, Q
----- and.intro
P ∧ Q
However, if we want to be really precise, we'd
write the rule like this:
{ P Q : Prop } (p : P) (q : Q)
------------------------------ (and.intro)
pf : P ∧ Q
-/
variable P : Prop
variable Q : Prop
variable pfP: P
variable pfQ: Q
def pf : P ∧ Q := and.intro pfP pfQ
/-
This is the final form, as we'll see in more
detail in material to come. It says that if
P and Q are propositions, and if p is a proof
of P and q is a proof of Q, then applying the
and.intro rule to p and q returns a proof of
P ∧ Q. We thus now have a rule to establish
the truth of a proposition of P ∧ Q by using
this inference rule to construct a proof of
the proposition P ∧ Q from given proofs of
its constituent propositions.
As we'll see in a bit, the expression,
(and.intro p q), which you can read as simply
applying the function, and.intro. to the proof
"values", p and q, returns (reduces to) a value
that is a proof of P ∧ Q. That's where we are
headed.
-/
/-
EXERCISE: What is returned when the and-introduction
inference rule, viewed as a program, is applied to two
proofs: one of the proposition, 0 = 0, and one of the
proposition, "Hello" = "Hello"?
EXERCISE: Why could this rule never be applied (in
any reasonable logic) to produce a proof (thus a
truth judgement) for the proposition, 0 = 0 ∧ 0 = 1?
-/ |
5c1926aab51e714d79f90731899cdef9e5aed610 | 4376c25f060c13471bb89cdb12aeac1d53e53876 | /src/espaces-metriques/metric.lean | 5ccdedadf5ca14302c0a7e82193d1a7eef20678c | [
"MIT"
] | permissive | RaitoBezarius/projet-maths-lean | 8fa7df563d64c256561ab71893c523fc1424b85c | 42356e980e021a20c3468f5ca1639fec01bb934f | refs/heads/master | 1,613,002,128,339 | 1,589,289,282,000 | 1,589,289,282,000 | 244,431,534 | 0 | 1 | MIT | 1,584,312,574,000 | 1,583,169,883,000 | TeX | UTF-8 | Lean | false | false | 5,926 | lean | import tactic
import data.real.basic
import .custom.defs
import .custom.sequences
import .custom.cauchy
noncomputable theory
open set
open_locale classical
section suites
open espace_metrique
variables {X: Type} [espace_metrique X]
variables {Y: Type} [espace_metrique Y]
variables {Z: Type} [espace_metrique Z]
/-- on définit le *type* des suites de Cauchy --/
def cauchy_seqs (X: Type*) [espace_metrique X] := { f : ℕ → X // cauchy f }
/-- Si le temps le permet, et seulement dans ce cas,
-- montrer que le pre-ecart induit une structure
d'espace pre-metrique sur l'ensemble des suites de cauchy pour
pouvoir éventuellement construire la completion de l'espace X
comme le quotient de l'ensemble des suites de cauchy par la
relation d'équivalence appropriée --/
-- Retourne la limite de d(x_n, y_n) (bien définie par complétude de R), requiert l'axiome du choix.
-- Définit la distance entre 2 suites de Cauchy par lim d(x_n, y_n).
def cauchy.dist (T: Type*) [espace_metrique T] (x y: cauchy_seqs T): ℝ
:= cauchy.limit (pre_ecart x.val y.val) (pre_ecart_cauchy x.val y.val x.property y.property)
def cauchy.d_pos (T: Type*) [espace_metrique T] (x y: cauchy_seqs T): cauchy.dist T x y ≥ 0 := begin
rw cauchy.dist,
apply cauchy.limit_ge_of_seq_ge,
intro n,
rw pre_ecart,
simp,
exact espace_metrique.d_pos _ _,
end
def cauchy.pre_ecart_self_eq_zero_seq (T: Type*) [espace_metrique T] (x: cauchy_seqs T):
pre_ecart x.val x.val = ((λ n, 0): ℕ → ℝ) := begin
rw pre_ecart,
ext,
apply espace_metrique.presep,
refl,
end
instance pre_ecart.premetrique (X: Type*) [espace_metrique X]: espace_pre_metrique (cauchy_seqs X) :=
{ d := cauchy.dist X,
d_pos := cauchy.d_pos X,
presep := begin
intros x y h,
rw cauchy.dist,
rw h,
conv {
congr,
congr,
rw cauchy.pre_ecart_self_eq_zero_seq X y,
skip,
skip,
},
apply cauchy.constant_limit 0,
end,
sym := begin
intros x y,
rw cauchy.dist,
rw cauchy.dist,
conv {
congr,
congr,
rw cauchy.pre_ecart_sym,
skip,
skip,
},
end,
triangle := begin
intros x y z,
rw cauchy.dist,
rw cauchy.dist,
rw cauchy.dist,
apply cauchy.limit_le_of_add_seq_le,
exact pre_ecart.triangle _ _ _,
end
}
def cauchy.cong (T: Type*) [espace_metrique T] (x y: cauchy_seqs T): Prop := cauchy.dist T x y = 0
lemma cauchy.cong_refl (T: Type*) [espace_metrique T]: reflexive (cauchy.cong T) := begin
intro x,
rw cauchy.cong,
exact espace_pre_metrique.presep x x (by refl),
end
lemma cauchy.cong_symm (T: Type*) [espace_metrique T]: symmetric (cauchy.cong T) := begin
intros x y H,
rw cauchy.cong,
rw cauchy.cong at H,
rw ← H,
symmetry,
exact espace_pre_metrique.sym x y,
end
lemma cauchy.cong_trans (T: Type*) [espace_metrique T]: transitive (cauchy.cong T) := begin
intros x y z Hxy Hyz,
rw cauchy.cong,
rw cauchy.cong at Hxy,
rw cauchy.cong at Hyz,
apply le_antisymm,
calc
cauchy.dist T x z ≤ cauchy.dist T x y + cauchy.dist T y z : espace_pre_metrique.triangle x y z
... ≤ 0 + 0 : by rw [Hxy, Hyz]
... ≤ 0 : by simp,
exact espace_pre_metrique.d_pos x z,
end
theorem cauchy.cong_equiv (T: Type*) [espace_metrique T]: equivalence (cauchy.cong T) :=
⟨cauchy.cong_refl T, cauchy.cong_symm T, cauchy.cong_trans T⟩
instance pre_ecart.setoid (T: Type*) [espace_metrique T] : setoid (cauchy_seqs T) :=
{
r := cauchy.cong T,
iseqv := cauchy.cong_equiv T
}
local attribute [instance] pre_ecart.premetrique
def completion.dist_soundness (T: Type*) [espace_metrique T]:
∀ x₁ x₂: cauchy_seqs T, ∀ y₁ y₂: cauchy_seqs T, (x₁ ≈ y₁) → (x₂ ≈ y₂) → (espace_pre_metrique.d x₁ x₂ = espace_pre_metrique.d y₁ y₂) := begin
intros x y z t Hxz Hyt,
change (espace_pre_metrique.d x z = 0) at Hxz,
change (espace_pre_metrique.d y t = 0) at Hyt,
apply le_antisymm,
calc
espace_pre_metrique.d x y ≤ espace_pre_metrique.d x z + espace_pre_metrique.d z y : espace_pre_metrique.triangle x z y
... ≤ espace_pre_metrique.d x z + (espace_pre_metrique.d z t + espace_pre_metrique.d t y) : add_le_add_left (espace_pre_metrique.triangle z t y) _
... = espace_pre_metrique.d z t + espace_pre_metrique.d y t : by rw [Hxz, zero_add, espace_pre_metrique.sym t y]
... = espace_pre_metrique.d z t : by rw Hyt; simp,
calc
espace_pre_metrique.d z t ≤ espace_pre_metrique.d z x + espace_pre_metrique.d x t : espace_pre_metrique.triangle z x t
... ≤ espace_pre_metrique.d z x + (espace_pre_metrique.d x y + espace_pre_metrique.d y t) : add_le_add_left (espace_pre_metrique.triangle x y t) _
... = espace_pre_metrique.d x z + espace_pre_metrique.d x y : by rw [Hyt, add_zero, espace_pre_metrique.sym z x]
... = espace_pre_metrique.d x y : by rw Hxz; simp,
end
end suites
-- Le complété !
section completion
def completion (T: Type*) [espace_metrique T]: Type* := quotient (pre_ecart.setoid T)
local attribute [instance] pre_ecart.setoid
def completion.dist (T: Type*) [espace_metrique T] (x y: completion T): ℝ :=
quotient.lift₂ (cauchy.dist T) (completion.dist_soundness T) x y
instance completion.metric_space (T: Type*) [espace_metrique T]: espace_metrique (completion T) :=
{
d := completion.dist T,
d_pos := λ x y,
quotient.induction_on₂ x y $ λ X Y, show cauchy.dist T X Y ≥ 0, from cauchy.d_pos T X Y,
presep := λ x y, quotient.induction_on₂ x y $ λ X Y H, show cauchy.dist T X Y = 0, from quotient.eq.1 H,
sep := λ x y, quotient.induction_on₂ x y $ λ X Y H, show ⟦ X ⟧ = ⟦ Y ⟧, from quotient.sound H,
sym := λ x y, quotient.induction_on₂ x y $ λ X Y, show cauchy.dist T X Y = cauchy.dist T Y X,
from espace_pre_metrique.sym X Y,
triangle := λ x y z, quotient.induction_on₃ x y z $ λ X Y Z, show cauchy.dist T X Z ≤ cauchy.dist T X Y + cauchy.dist T Y Z,
from espace_pre_metrique.triangle X Y Z
}
end completion |
d8a12ab59e4085fa3e9be2ed1427453a0b9707f0 | bdb33f8b7ea65f7705fc342a178508e2722eb851 | /set_theory/ordinal_notation.lean | d55c879738a2a2bea0d58388ffc4f10112de4ea5 | [
"Apache-2.0"
] | permissive | rwbarton/mathlib | 939ae09bf8d6eb1331fc2f7e067d39567e10e33d | c13c5ea701bb1eec057e0a242d9f480a079105e9 | refs/heads/master | 1,584,015,335,862 | 1,524,142,167,000 | 1,524,142,167,000 | 130,614,171 | 0 | 0 | Apache-2.0 | 1,548,902,667,000 | 1,524,437,371,000 | Lean | UTF-8 | Lean | false | false | 35,741 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Ordinal notations (constructive ordinal arithmetic for ordinals < ε₀).
-/
import set_theory.ordinal data.pnat
open ordinal
local notation `ω` := omega.{0}
/-- Recursive definition of an ordinal notation. `zero` denotes the
ordinal 0, and `oadd e n a` is intended to refer to `ω^e * n + a`.
For this to be valid Cantor normal form, we must have the exponents
decrease to the right, but we can't state this condition until we've
defined `repr`, so it is a separate definition `NF`. -/
@[derive decidable_eq]
inductive onote : Type
| zero : onote
| oadd : onote → ℕ+ → onote → onote
namespace onote
/-- Notation for 0 -/
instance : has_zero onote := ⟨zero⟩
@[simp] theorem zero_def : zero = 0 := rfl
/-- Notation for 1 -/
instance : has_one onote := ⟨oadd 0 1 0⟩
/-- Notation for ω -/
def omega : onote := oadd 1 1 0
/-- The ordinal denoted by a notation -/
@[simp] noncomputable def repr : onote → ordinal.{0}
| 0 := 0
| (oadd e n a) := ω ^ repr e * n + repr a
def to_string_aux1 (e : onote) (n : ℕ) (s : string) : string :=
if e = 0 then _root_.to_string n else
(if e = 1 then "ω" else "ω^(" ++ s ++ ")") ++
if n = 1 then "" else "*" ++ _root_.to_string n
/-- Print an ordinal notation -/
def to_string : onote → string
| zero := "0"
| (oadd e n 0) := to_string_aux1 e n (to_string e)
| (oadd e n a) := to_string_aux1 e n (to_string e) ++ " + " ++ to_string a
/-- Print an ordinal notation -/
def repr' : onote → string
| zero := "0"
| (oadd e n a) := "(oadd " ++ repr' e ++ " " ++ _root_.to_string (n:ℕ) ++ " " ++ repr' a ++ ")"
instance : has_to_string onote := ⟨to_string⟩
instance : has_repr onote := ⟨repr'⟩
instance : preorder onote :=
{ le := λ x y, repr x ≤ repr y,
lt := λ x y, repr x < repr y,
le_refl := λ a, @le_refl ordinal _ _,
le_trans := λ a b c, @le_trans ordinal _ _ _ _,
lt_iff_le_not_le := λ a b, @lt_iff_le_not_le ordinal _ _ _ }
theorem lt_def {x y : onote} : x < y ↔ repr x < repr y := iff.rfl
theorem le_def {x y : onote} : x ≤ y ↔ repr x ≤ repr y := iff.rfl
/-- Convert a `nat` into an ordinal -/
@[simp] def of_nat : ℕ → onote
| 0 := 0
| (nat.succ n) := oadd 0 n.succ_pnat 0
@[simp] theorem of_nat_one : of_nat 1 = 1 := rfl
@[simp] theorem repr_of_nat (n : ℕ) : repr (of_nat n) = n :=
by cases n; simp
@[simp] theorem repr_one : repr 1 = 1 :=
by simpa using repr_of_nat 1
theorem omega_le_oadd (e n a) : ω ^ repr e ≤ repr (oadd e n a) :=
begin
unfold repr,
refine le_trans _ (le_add_right _ _),
simpa using (mul_le_mul_iff_left $ power_pos (repr e) omega_pos).2 (nat_cast_le.2 n.2)
end
theorem oadd_pos (e n a) : 0 < oadd e n a :=
@lt_of_lt_of_le _ _ _ _ _ (power_pos _ omega_pos)
(omega_le_oadd _ _ _)
/-- Compare ordinal notations -/
def cmp : onote → onote → ordering
| 0 0 := ordering.eq
| _ 0 := ordering.gt
| 0 _ := ordering.lt
| o₁@(oadd e₁ n₁ a₁) o₂@(oadd e₂ n₂ a₂) :=
(cmp e₁ e₂).or_else $ (_root_.cmp (n₁:ℕ) n₂).or_else (cmp a₁ a₂)
theorem eq_of_cmp_eq : ∀ {o₁ o₂}, cmp o₁ o₂ = ordering.eq → o₁ = o₂
| 0 0 h := rfl
| (oadd e n a) 0 h := by injection h
| 0 (oadd e n a) h := by injection h
| o₁@(oadd e₁ n₁ a₁) o₂@(oadd e₂ n₂ a₂) h := begin
revert h, simp [cmp],
cases h₁ : cmp e₁ e₂; intro h; try {cases h},
have := eq_of_cmp_eq h₁, subst e₂,
revert h, cases h₂ : _root_.cmp (n₁:ℕ) n₂; intro h; try {cases h},
have := eq_of_cmp_eq h, subst a₂,
rw [_root_.cmp, cmp_using_eq_eq] at h₂,
have := subtype.eq (eq_of_incomp h₂), subst n₂, simp
end
theorem zero_lt_one : (0 : onote) < 1 :=
by rw [lt_def, repr, repr_one]; exact zero_lt_one
/-- `NF_below o b` says that `o` is a normal form ordinal notation
satisfying `repr o < ω ^ b`. -/
inductive NF_below : onote → ordinal.{0} → Prop
| zero {b} : NF_below 0 b
| oadd' {e n a eb b} : NF_below e eb →
NF_below a (repr e) → repr e < b → NF_below (oadd e n a) b
/-- A normal form ordinal notation has the form
ω ^ a₁ * n₁ + ω ^ a₂ * n₂ + ... ω ^ aₖ * nₖ
where `a₁ > a₂ > ... > aₖ` and all the `aᵢ` are
also in normal form.
We will essentially only be interested in normal form
ordinal notations, but to avoid complicating the algorithms
we define everything over general ordinal notations and
only prove correctness with normal form as an invariant. -/
@[class] def NF (o : onote) := Exists (NF_below o)
instance NF.zero : NF 0 := ⟨0, NF_below.zero⟩
theorem NF_below.oadd {e n a b} : NF e →
NF_below a (repr e) → repr e < b → NF_below (oadd e n a) b
| ⟨eb, h⟩ := NF_below.oadd' h
theorem NF_below.fst {e n a b} (h : NF_below (oadd e n a) b) : NF e :=
by cases h with _ _ _ _ eb _ h₁ h₂ h₃; exact ⟨_, h₁⟩
theorem NF.fst {e n a} : NF (oadd e n a) → NF e
| ⟨b, h⟩ := h.fst
theorem NF_below.snd {e n a b} (h : NF_below (oadd e n a) b) : NF_below a (repr e) :=
by cases h with _ _ _ _ eb _ h₁ h₂ h₃; exact h₂
theorem NF.snd' {e n a} : NF (oadd e n a) → NF_below a (repr e)
| ⟨b, h⟩ := h.snd
theorem NF.snd {e n a} (h : NF (oadd e n a)) : NF a :=
⟨_, h.snd'⟩
theorem NF.oadd {e a} (h₁ : NF e) (n)
(h₂ : NF_below a (repr e)) : NF (oadd e n a) :=
⟨_, NF_below.oadd h₁ h₂ (ordinal.lt_succ_self _)⟩
instance NF.oadd_zero (e n) [h : NF e] : NF (oadd e n 0) :=
h.oadd _ NF_below.zero
theorem NF_below.lt {e n a b} (h : NF_below (oadd e n a) b) : repr e < b :=
by cases h with _ _ _ _ eb _ h₁ h₂ h₃; exact h₃
theorem NF_below_zero : ∀ {o}, NF_below o 0 ↔ o = 0
| 0 := ⟨λ _, rfl, λ _, NF_below.zero⟩
| (oadd e n a) := ⟨λ h, (not_le_of_lt h.lt).elim (zero_le _),
λ e, e.symm ▸ NF_below.zero⟩
theorem NF.zero_of_zero {e n a} (h : NF (oadd e n a)) (e0 : e = 0) : a = 0 :=
by simpa [e0, NF_below_zero] using h.snd'
theorem NF_below.repr_lt {o b} (h : NF_below o b) : repr o < ω ^ b :=
begin
induction h with _ e n a eb b h₁ h₂ h₃ _ IH,
{ exact power_pos _ omega_pos },
{ rw repr,
refine lt_of_lt_of_le ((ordinal.add_lt_add_iff_left _).2 IH) _,
rw ← mul_succ,
refine le_trans (mul_le_mul_left _ $ ordinal.succ_le.2 $ nat_lt_omega _) _,
rw ← power_succ,
exact power_le_power_right omega_pos (ordinal.succ_le.2 h₃) }
end
theorem NF_below.mono {o b₁ b₂} (bb : b₁ ≤ b₂) (h : NF_below o b₁) : NF_below o b₂ :=
begin
induction h with _ e n a eb b h₁ h₂ h₃ _ IH; constructor,
exacts [h₁, h₂, lt_of_lt_of_le h₃ bb]
end
theorem NF.below_of_lt {e n a b} (H : repr e < b) : NF (oadd e n a) → NF_below (oadd e n a) b
| ⟨b', h⟩ := by cases h with _ _ _ _ eb _ h₁ h₂ h₃;
exact NF_below.oadd' h₁ h₂ H
theorem NF.below_of_lt' : ∀ {o b}, repr o < ω ^ b → NF o → NF_below o b
| 0 b H _ := NF_below.zero
| (oadd e n a) b H h := h.below_of_lt $ (power_lt_power_iff_right one_lt_omega).1 $
(lt_of_le_of_lt (omega_le_oadd _ _ _) H)
theorem NF_below_of_nat : ∀ n, NF_below (of_nat n) 1
| 0 := NF_below.zero
| (nat.succ n) := NF_below.oadd NF.zero NF_below.zero ordinal.zero_lt_one
instance NF_of_nat (n) : NF (of_nat n) := ⟨_, NF_below_of_nat n⟩
instance NF_one : NF 1 := by rw ← of_nat_one; apply_instance
theorem oadd_lt_oadd_1 {e₁ n₁ o₁ e₂ n₂ o₂}
(h₁ : NF (oadd e₁ n₁ o₁)) (h₂ : NF (oadd e₂ n₂ o₂))
(h : e₁ < e₂) : oadd e₁ n₁ o₁ < oadd e₂ n₂ o₂ :=
@lt_of_lt_of_le _ _ _ _ _ ((h₁.below_of_lt h).repr_lt) (omega_le_oadd _ _ _)
theorem oadd_lt_oadd_2 {e n₁ o₁ n₂ o₂}
(h₁ : NF (oadd e n₁ o₁)) (h₂ : NF (oadd e n₂ o₂))
(h : (n₁:ℕ) < n₂) : oadd e n₁ o₁ < oadd e n₂ o₂ :=
begin
simp [lt_def],
refine lt_of_lt_of_le ((ordinal.add_lt_add_iff_left _).2 h₁.snd'.repr_lt)
(le_trans _ (le_add_right _ _)),
rwa [← mul_succ, mul_le_mul_iff_left (power_pos _ omega_pos),
ordinal.succ_le, nat_cast_lt]
end
theorem oadd_lt_oadd_3 {e n a₁ a₂}
(h₁ : NF (oadd e n a₁)) (h₂ : NF (oadd e n a₂))
(h : a₁ < a₂) : oadd e n a₁ < oadd e n a₂ :=
begin
rw lt_def, unfold repr,
exact (ordinal.add_lt_add_iff_left _).2 h
end
theorem cmp_compares : ∀ (a b : onote) [NF a] [NF b], (cmp a b).compares a b
| 0 0 h₁ h₂ := rfl
| (oadd e n a) 0 h₁ h₂ := oadd_pos _ _ _
| 0 (oadd e n a) h₁ h₂ := oadd_pos _ _ _
| o₁@(oadd e₁ n₁ a₁) o₂@(oadd e₂ n₂ a₂) h₁ h₂ := begin
rw cmp,
have IHe := @cmp_compares _ _ h₁.fst h₂.fst,
cases cmp e₁ e₂,
case ordering.lt { exact oadd_lt_oadd_1 h₁ h₂ IHe },
case ordering.gt { exact oadd_lt_oadd_1 h₂ h₁ IHe },
change e₁ = e₂ at IHe, subst IHe,
unfold _root_.cmp, cases nh : cmp_using (<) (n₁:ℕ) n₂,
case ordering.lt {
rw cmp_using_eq_lt at nh, exact oadd_lt_oadd_2 h₁ h₂ nh },
case ordering.gt {
rw cmp_using_eq_gt at nh, exact oadd_lt_oadd_2 h₂ h₁ nh },
rw cmp_using_eq_eq at nh,
have := subtype.eq (eq_of_incomp nh), subst n₂,
have IHa := @cmp_compares _ _ h₁.snd h₂.snd,
cases cmp a₁ a₂,
case ordering.lt { exact oadd_lt_oadd_3 h₁ h₂ IHa },
case ordering.gt { exact oadd_lt_oadd_3 h₂ h₁ IHa },
change a₁ = a₂ at IHa, subst IHa, exact rfl
end
theorem repr_inj {a b} [NF a] [NF b] : repr a = repr b ↔ a = b :=
⟨match cmp a b, cmp_compares a b with
| ordering.lt, (h : repr a < repr b), e := (ne_of_lt h e).elim
| ordering.gt, (h : repr a > repr b), e := (ne_of_gt h e).elim
| ordering.eq, h, e := h
end, congr_arg _⟩
theorem NF.of_dvd_omega_power {b e n a} (h : NF (oadd e n a)) (d : ω ^ b ∣ repr (oadd e n a)) :
b ≤ repr e ∧ ω ^ b ∣ repr a :=
begin
have := mt repr_inj.1 (λ h, by injection h : oadd e n a ≠ 0),
have L := le_of_not_lt (λ l, not_le_of_lt (h.below_of_lt l).repr_lt (le_of_dvd this d)),
simp at d,
exact ⟨L, (dvd_add_iff $ dvd_mul_of_dvd _ $ power_dvd_power _ L).1 d⟩
end
theorem NF.of_dvd_omega {e n a} (h : NF (oadd e n a)) :
ω ∣ repr (oadd e n a) → repr e ≠ 0 ∧ ω ∣ repr a :=
by rw [← power_one ω, ← one_le_iff_ne_zero]; exact h.of_dvd_omega_power
/-- `top_below b o` asserts that the largest exponent in `o`, if
it exists, is less than `b`. This is an auxiliary definition
for decidability of `NF`. -/
def top_below (b) : onote → Prop
| 0 := true
| (oadd e n a) := cmp e b = ordering.lt
instance decidable_top_below : decidable_rel top_below :=
by intros b o; cases o; delta top_below; apply_instance
theorem NF_below_iff_top_below {b} [NF b] : ∀ {o},
NF_below o (repr b) ↔ NF o ∧ top_below b o
| 0 := ⟨λ h, ⟨⟨_, h⟩, trivial⟩, λ _, NF_below.zero⟩
| (oadd e n a) :=
⟨λ h, ⟨⟨_, h⟩, (@cmp_compares _ b h.fst _).eq_lt.2 h.lt⟩,
λ ⟨h₁, h₂⟩, h₁.below_of_lt $ (@cmp_compares _ b h₁.fst _).eq_lt.1 h₂⟩
instance decidable_NF : decidable_pred NF
| 0 := is_true NF.zero
| (oadd e n a) := begin
have := decidable_NF e,
have := decidable_NF a, resetI,
apply decidable_of_iff (NF e ∧ NF a ∧ top_below e a),
abstract {
rw ← and_congr_right (λ h, @NF_below_iff_top_below _ h _),
exact ⟨λ ⟨h₁, h₂⟩, NF.oadd h₁ n h₂, λ h, ⟨h.fst, h.snd'⟩⟩ },
end
/-- Addition of ordinal notations (correct only for normal input) -/
def add : onote → onote → onote
| 0 o := o
| (oadd e n a) o := match add a o with
| 0 := oadd e n 0
| o'@(oadd e' n' a') := match cmp e e' with
| ordering.lt := o'
| ordering.eq := oadd e (n + n') a'
| ordering.gt := oadd e n o'
end
end
instance : has_add onote := ⟨add⟩
@[simp] theorem zero_add (o : onote) : 0 + o = o := rfl
theorem oadd_add (e n a o) : oadd e n a + o = add._match_1 e n (a + o) := rfl
/-- Subtraction of ordinal notations (correct only for normal input) -/
def sub : onote → onote → onote
| 0 o := 0
| o 0 := o
| o₁@(oadd e₁ n₁ a₁) (oadd e₂ n₂ a₂) := match cmp e₁ e₂ with
| ordering.lt := 0
| ordering.gt := o₁
| ordering.eq := match (n₁:ℕ) - n₂ with
| 0 := if n₁ = n₂ then sub a₁ a₂ else 0
| (nat.succ k) := oadd e₁ k.succ_pnat a₁
end
end
instance : has_sub onote := ⟨sub⟩
theorem add_NF_below {b} : ∀ {o₁ o₂}, NF_below o₁ b → NF_below o₂ b → NF_below (o₁ + o₂) b
| 0 o h₁ h₂ := h₂
| (oadd e n a) o h₁ h₂ := begin
have h' := add_NF_below (h₁.snd.mono $ le_of_lt h₁.lt) h₂,
simp [oadd_add], cases a + o with e' n' a',
{ exact NF_below.oadd h₁.fst NF_below.zero h₁.lt },
simp [add], have := @cmp_compares _ _ h₁.fst h'.fst,
cases cmp e e'; simp [add],
{ exact h' },
{ simp at this, subst e',
exact NF_below.oadd h'.fst h'.snd h'.lt },
{ exact NF_below.oadd h₁.fst (NF.below_of_lt this ⟨_, h'⟩) h₁.lt }
end
instance add_NF (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ + o₂)
| ⟨b₁, h₁⟩ ⟨b₂, h₂⟩ := (b₁.le_total b₂).elim
(λ h, ⟨b₂, add_NF_below (h₁.mono h) h₂⟩)
(λ h, ⟨b₁, add_NF_below h₁ (h₂.mono h)⟩)
@[simp] theorem repr_add : ∀ o₁ o₂ [NF o₁] [NF o₂], repr (o₁ + o₂) = repr o₁ + repr o₂
| 0 o h₁ h₂ := by simp
| (oadd e n a) o h₁ h₂ := begin
haveI := h₁.snd, have h' := repr_add a o,
conv at h' in (_+o) {simp [(+)]},
have nf := onote.add_NF a o,
conv at nf in (_+o) {simp [(+)]},
conv in (_+o) {simp [(+), add]},
cases add a o with e' n' a'; simp [add, h'.symm],
have := h₁.fst, have := nf.fst, have ee := cmp_compares e e',
cases cmp e e'; simp [add],
{ rw [← add_assoc, @add_absorp _ (repr e') (ω ^ repr e' * (n':ℕ))],
{ have := (h₁.below_of_lt ee).repr_lt, unfold repr at this,
exact lt_of_le_of_lt (le_add_right _ _) this },
{ simpa using (mul_le_mul_iff_left $
power_pos (repr e') omega_pos).2 (nat_cast_le.2 n'.pos) } },
{ change e = e' at ee, subst e',
rw [← add_assoc, ← ordinal.mul_add, ← nat.cast_add] }
end
theorem sub_NF_below : ∀ {o₁ o₂ b}, NF_below o₁ b → NF o₂ → NF_below (o₁ - o₂) b
| 0 o b h₁ h₂ := by cases o; exact NF_below.zero
| (oadd e n a) 0 b h₁ h₂ := h₁
| (oadd e₁ n₁ a₁) (oadd e₂ n₂ a₂) b h₁ h₂ := begin
have h' := sub_NF_below h₁.snd h₂.snd,
simp [has_sub.sub, sub] at h' ⊢,
have := @cmp_compares _ _ h₁.fst h₂.fst,
cases cmp e₁ e₂; simp [sub],
{ apply NF_below.zero },
{ simp at this, subst e₂,
cases mn : (n₁:ℕ) - n₂; simp [sub],
{ by_cases en : n₁ = n₂; simp [en],
{ exact h'.mono (le_of_lt h₁.lt) },
{ exact NF_below.zero } },
{ exact NF_below.oadd h₁.fst h₁.snd h₁.lt } },
{ exact h₁ }
end
instance sub_NF (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ - o₂)
| ⟨b₁, h₁⟩ h₂ := ⟨b₁, sub_NF_below h₁ h₂⟩
@[simp] theorem repr_sub : ∀ o₁ o₂ [NF o₁] [NF o₂], repr (o₁ - o₂) = repr o₁ - repr o₂
| 0 o h₁ h₂ := by cases o; exact (ordinal.zero_sub _).symm
| (oadd e n a) 0 h₁ h₂ := (ordinal.sub_zero _).symm
| (oadd e₁ n₁ a₁) (oadd e₂ n₂ a₂) h₁ h₂ := begin
haveI := h₁.snd, haveI := h₂.snd, have h' := repr_sub a₁ a₂,
conv at h' in (a₁-a₂) {simp [has_sub.sub]},
have nf := onote.sub_NF a₁ a₂,
conv at nf in (a₁-a₂) {simp [has_sub.sub]},
conv in (_-oadd _ _ _) {simp [has_sub.sub, sub]},
have ee := @cmp_compares _ _ h₁.fst h₂.fst,
cases cmp e₁ e₂,
{ rw [sub_eq_zero_iff_le.2], {refl},
exact le_of_lt (oadd_lt_oadd_1 h₁ h₂ ee) },
{ change e₁ = e₂ at ee, subst e₂, unfold sub._match_1,
cases mn : (n₁:ℕ) - n₂; dsimp only [sub._match_2],
{ by_cases en : n₁ = n₂,
{ simp [en], rwa [add_sub_add_cancel] },
{ simp [en, -repr],
exact (sub_eq_zero_iff_le.2 $ le_of_lt $ oadd_lt_oadd_2 h₁ h₂ $
lt_of_le_of_ne (nat.sub_eq_zero_iff_le.1 mn) (mt pnat.eq en)).symm } },
{ simp [nat.succ_pnat, -nat.cast_succ],
rw [(nat.sub_eq_iff_eq_add $ le_of_lt $ nat.lt_of_sub_eq_succ mn).1 mn,
add_comm, nat.cast_add, ordinal.mul_add, add_assoc, add_sub_add_cancel],
refine (ordinal.sub_eq_of_add_eq $ add_absorp h₂.snd'.repr_lt $
le_trans _ (le_add_right _ _)).symm,
simpa using mul_le_mul_left _ (nat_cast_le.2 $ nat.succ_pos _) } },
{ exact (ordinal.sub_eq_of_add_eq $ add_absorp (h₂.below_of_lt ee).repr_lt $
omega_le_oadd _ _ _).symm }
end
/-- Multiplication of ordinal notations (correct only for normal input) -/
def mul : onote → onote → onote
| 0 _ := 0
| _ 0 := 0
| o₁@(oadd e₁ n₁ a₁) (oadd e₂ n₂ a₂) :=
if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else
oadd (e₁ + e₂) n₂ (mul o₁ a₂)
instance : has_mul onote := ⟨mul⟩
@[simp] theorem zero_mul (o : onote) : 0 * o = 0 := by cases o; refl
@[simp] theorem mul_zero (o : onote) : o * 0 = 0 := by cases o; refl
theorem oadd_mul (e₁ n₁ a₁ e₂ n₂ a₂) : oadd e₁ n₁ a₁ * oadd e₂ n₂ a₂ =
if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else
oadd (e₁ + e₂) n₂ (oadd e₁ n₁ a₁ * a₂) := rfl
theorem oadd_mul_NF_below {e₁ n₁ a₁ b₁} (h₁ : NF_below (oadd e₁ n₁ a₁) b₁) :
∀ {o₂ b₂}, NF_below o₂ b₂ → NF_below (oadd e₁ n₁ a₁ * o₂) (repr e₁ + b₂)
| 0 b₂ h₂ := NF_below.zero
| (oadd e₂ n₂ a₂) b₂ h₂ := begin
have IH := oadd_mul_NF_below h₂.snd,
by_cases e0 : e₂ = 0; simp [e0, oadd_mul],
{ apply NF_below.oadd h₁.fst h₁.snd,
simpa using (add_lt_add_iff_left (repr e₁)).2
(lt_of_le_of_lt (ordinal.zero_le _) h₂.lt) },
{ haveI := h₁.fst, haveI := h₂.fst,
apply NF_below.oadd, apply_instance,
{ rwa repr_add },
{ rw [repr_add, ordinal.add_lt_add_iff_left], exact h₂.lt } }
end
instance mul_NF : ∀ o₁ o₂ [NF o₁] [NF o₂], NF (o₁ * o₂)
| 0 o h₁ h₂ := by cases o; exact NF.zero
| (oadd e n a) o ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ :=
⟨_, oadd_mul_NF_below hb₁ hb₂⟩
@[simp] theorem repr_mul : ∀ o₁ o₂ [NF o₁] [NF o₂], repr (o₁ * o₂) = repr o₁ * repr o₂
| 0 o h₁ h₂ := by cases o; exact (ordinal.zero_mul _).symm
| (oadd e₁ n₁ a₁) 0 h₁ h₂ := (ordinal.mul_zero _).symm
| (oadd e₁ n₁ a₁) (oadd e₂ n₂ a₂) h₁ h₂ := begin
have IH : repr (mul _ _) = _ := @repr_mul _ _ h₁ h₂.snd,
conv {to_lhs, simp [(*)]},
have ao : repr a₁ + ω ^ repr e₁ * (n₁:ℕ) = ω ^ repr e₁ * (n₁:ℕ),
{ apply add_absorp h₁.snd'.repr_lt,
simpa using (mul_le_mul_iff_left $ power_pos _ omega_pos).2
(nat_cast_le.2 n₁.2) },
by_cases e0 : e₂ = 0; simp [e0, mul],
{ cases nat.exists_eq_succ_of_ne_zero n₂.ne_zero with x xe,
simp [h₂.zero_of_zero e0, xe, -nat.cast_succ],
rw [← nat_cast_succ x, add_mul_succ _ ao, mul_assoc] },
{ haveI := h₁.fst, haveI := h₂.fst,
simp [IH, repr_add, power_add, ordinal.mul_add],
rw ← mul_assoc, congr_n 2,
have := mt repr_inj.1 e0,
rw [add_mul_limit ao (power_is_limit_left omega_is_limit this),
mul_assoc, mul_omega_dvd (nat_cast_pos.2 n₁.pos) (nat_lt_omega _)],
simpa using power_dvd_power ω (one_le_iff_ne_zero.2 this) },
end
/-- Calculate division and remainder of `o` mod ω.
`split' o = (a, n)` means `o = ω * a + n`. -/
def split' : onote → onote × ℕ
| 0 := (0, 0)
| (oadd e n a) := if e = 0 then (0, n) else
let (a', m) := split' a in (oadd (e - 1) n a', m)
/-- Calculate division and remainder of `o` mod ω.
`split o = (a, n)` means `o = a + n`, where `ω ∣ a`. -/
def split : onote → onote × ℕ
| 0 := (0, 0)
| (oadd e n a) := if e = 0 then (0, n) else
let (a', m) := split a in (oadd e n a', m)
/-- `scale x o` is the ordinal notation for `ω ^ x * o`. -/
def scale (x : onote) : onote → onote
| 0 := 0
| (oadd e n a) := oadd (x + e) n (scale a)
/-- `mul_nat o n` is the ordinal notation for `o * n`. -/
def mul_nat : onote → ℕ → onote
| 0 m := 0
| _ 0 := 0
| (oadd e n a) (m+1) := oadd e (n * m.succ_pnat) a
def power_aux (e a0 a : onote) : ℕ → ℕ → onote
| _ 0 := 0
| 0 (m+1) := oadd e m.succ_pnat 0
| (k+1) m := scale (e + mul_nat a0 k) a + power_aux k m
/-- `power o₁ o₂` calculates the ordinal notation for
the ordinal exponential `o₁ ^ o₂`. -/
def power (o₁ o₂ : onote) : onote :=
match split o₁ with
| (0, 0) := if o₂ = 0 then 1 else 0
| (0, 1) := 1
| (0, m+1) := let (b', k) := split' o₂ in
oadd b' (@has_pow.pow ℕ+ _ _ m.succ_pnat k) 0
| (a@(oadd a0 _ _), m) := match split o₂ with
| (b, 0) := oadd (a0 * b) 1 0
| (b, k+1) := let eb := a0*b in
scale (eb + mul_nat a0 k) a + power_aux eb a0 (mul_nat a m) k m
end
end
instance : has_pow onote onote := ⟨power⟩
theorem power_def (o₁ o₂ : onote) : o₁ ^ o₂ = power._match_1 o₂ (split o₁) := rfl
theorem split_eq_scale_split' : ∀ {o o' m} [NF o], split' o = (o', m) → split o = (scale 1 o', m)
| 0 o' m h p := by injection p; substs o' m; refl
| (oadd e n a) o' m h p := begin
by_cases e0 : e = 0; simp [e0, split, split'] at p ⊢,
{ rcases p with ⟨rfl, rfl⟩, exact ⟨rfl, rfl⟩ },
{ revert p, cases h' : split' a with a' m',
haveI := h.fst, haveI := h.snd,
simp [split_eq_scale_split' h', split, split'],
have : 1 + (e - 1) = e,
{ refine repr_inj.1 _, simp,
have := mt repr_inj.1 e0,
exact add_sub_cancel_of_le (one_le_iff_ne_zero.2 this) },
intros, substs o' m, simp [scale, this] }
end
theorem NF_repr_split' : ∀ {o o' m} [NF o], split' o = (o', m) → NF o' ∧ repr o = ω * repr o' + m
| 0 o' m h p := by injection p; substs o' m; simp [NF.zero]
| (oadd e n a) o' m h p := begin
by_cases e0 : e = 0; simp [e0, split, split'] at p ⊢,
{ rcases p with ⟨rfl, rfl⟩,
simp [h.zero_of_zero e0, NF.zero] },
{ revert p, cases h' : split' a with a' m',
haveI := h.fst, haveI := h.snd,
cases NF_repr_split' h' with IH₁ IH₂,
simp [IH₂, split'],
intros, substs o' m,
have : ω ^ repr e = ω ^ (1 : ordinal.{0}) * ω ^ (repr e - 1),
{ have := mt repr_inj.1 e0,
rw [← power_add, add_sub_cancel_of_le (one_le_iff_ne_zero.2 this)] },
refine ⟨NF.oadd (by apply_instance) _ _, _⟩,
{ simp at this ⊢,
refine IH₁.below_of_lt' ((mul_lt_mul_iff_left omega_pos).1 $
lt_of_le_of_lt (le_add_right _ m') _),
rw [← this, ← IH₂], exact h.snd'.repr_lt },
{ rw this, simp [ordinal.mul_add, mul_assoc] } }
end
theorem scale_eq_mul (x) [NF x] : ∀ o [NF o], scale x o = oadd x 1 0 * o
| 0 h := rfl
| (oadd e n a) h := begin
simp [(*)], simp [mul, scale],
haveI := h.snd,
by_cases e0 : e = 0,
{ rw scale_eq_mul, simp [e0, h.zero_of_zero, show x + 0 = x, from repr_inj.1 (by simp)] },
{ simp [e0, scale_eq_mul, (*)] }
end
instance NF_scale (x) [NF x] (o) [NF o] : NF (scale x o) :=
by rw scale_eq_mul; apply_instance
@[simp] theorem repr_scale (x) [NF x] (o) [NF o] : repr (scale x o) = ω ^ repr x * repr o :=
by simp [scale_eq_mul]
theorem NF_repr_split {o o' m} [NF o] (h : split o = (o', m)) : NF o' ∧ repr o = repr o' + m :=
begin
cases e : split' o with a n,
cases NF_repr_split' e with s₁ s₂, resetI,
rw split_eq_scale_split' e at h,
injection h, substs o' n,
simp [repr_scale, s₂.symm],
apply_instance
end
theorem split_dvd {o o' m} [NF o] (h : split o = (o', m)) : ω ∣ repr o' :=
begin
cases e : split' o with a n,
rw split_eq_scale_split' e at h,
injection h, subst o',
cases NF_repr_split' e, resetI, simp [dvd_mul]
end
theorem split_add_lt {o e n a m} [NF o] (h : split o = (oadd e n a, m)) : repr a + m < ω ^ repr e :=
begin
cases NF_repr_split h with h₁ h₂,
cases h₁.of_dvd_omega (split_dvd h) with e0 d,
have := h₁.fst, have := h₁.snd,
refine add_lt_omega_power h₁.snd'.repr_lt (lt_of_lt_of_le (nat_lt_omega _) _),
simpa using power_le_power_right omega_pos (one_le_iff_ne_zero.2 e0),
end
@[simp] theorem mul_nat_eq_mul (n o) : mul_nat o n = o * of_nat n :=
by cases o; cases n; refl
instance NF_mul_nat (o) [NF o] (n) : NF (mul_nat o n) :=
by simp; apply_instance
instance NF_power_aux (e a0 a) [NF e] [NF a0] [NF a] : ∀ k m, NF (power_aux e a0 a k m)
| k 0 := by cases k; exact NF.zero
| 0 (m+1) := NF.oadd_zero _ _
| (k+1) (m+1) := by haveI := NF_power_aux k;
simp [power_aux, nat.succ_ne_zero]; apply_instance
instance NF_power (o₁ o₂) [NF o₁] [NF o₂] : NF (o₁ ^ o₂) :=
begin
cases e₁ : split o₁ with a m,
have na := (NF_repr_split e₁).1,
cases e₂ : split' o₂ with b' k,
haveI := (NF_repr_split' e₂).1,
cases a with a0 n a',
{ cases m with m,
{ by_cases o₂ = 0; simp [pow, power, e₁, h]; apply_instance },
{ by_cases m = 0; simp [pow, power, e₁, e₂, h]; apply_instance } },
{ simp [pow, power, e₁, e₂, split_eq_scale_split' e₂],
have := na.fst,
cases k with k; simp [succ_eq_add_one, power]; apply_instance }
end
theorem scale_power_aux (e a0 a : onote) [NF e] [NF a0] [NF a] :
∀ k m, repr (power_aux e a0 a k m) = ω ^ repr e * repr (power_aux 0 a0 a k m)
| 0 m := by cases m; simp [power_aux]
| (k+1) m := by by_cases m = 0; simp [h, power_aux,
ordinal.mul_add, power_add, mul_assoc, scale_power_aux]
theorem repr_power_aux₁ {e a} [Ne : NF e] [Na : NF a] {a' : ordinal}
(e0 : repr e ≠ 0) (h : a' < ω ^ repr e) (aa : repr a = a') (n : ℕ+) :
(ω ^ repr e * (n:ℕ) + a') ^ ω = (ω ^ repr e) ^ ω :=
begin
subst aa,
have No := Ne.oadd n (Na.below_of_lt' h),
have := omega_le_oadd e n a, unfold repr at this,
refine le_antisymm _ (power_le_power_left _ this),
apply (power_le_of_limit
(ne_of_gt $ lt_of_lt_of_le (power_pos _ omega_pos) this) omega_is_limit).2,
intros b l,
have := (No.below_of_lt (lt_succ_self _)).repr_lt, unfold repr at this,
apply le_trans (power_le_power_left b $ le_of_lt this),
rw [← power_mul, ← power_mul],
apply power_le_power_right omega_pos,
cases le_or_lt ω (repr e) with h h,
{ apply le_trans (mul_le_mul_left _ $ le_of_lt $ lt_succ_self _),
rw [succ, add_mul_succ _ (one_add_of_omega_le h), ← succ,
succ_le, mul_lt_mul_iff_left (pos_iff_ne_zero.2 e0)],
exact omega_is_limit.2 _ l },
{ refine le_trans (le_of_lt $ mul_lt_omega (omega_is_limit.2 _ h) l) _,
simpa using mul_le_mul_right ω (one_le_iff_ne_zero.2 e0) }
end
section
local infixr ^ := @pow ordinal.{0} ordinal ordinal.has_pow
theorem repr_power_aux₂ {a0 a'} [N0 : NF a0] [Na' : NF a'] (m : ℕ)
(d : ω ∣ repr a')
(e0 : repr a0 ≠ 0) (h : repr a' + m < ω ^ repr a0) (n : ℕ+) (k : ℕ) :
let R := repr (power_aux 0 a0 (oadd a0 n a' * of_nat m) k m) in
(k ≠ 0 → R < (ω ^ repr a0) ^ succ k) ∧
(ω ^ repr a0) ^ k * (ω ^ repr a0 * (n:ℕ) + repr a') + R =
(ω ^ repr a0 * (n:ℕ) + repr a' + m) ^ succ k :=
begin
intro,
haveI No : NF (oadd a0 n a') :=
N0.oadd n (Na'.below_of_lt' $ lt_of_le_of_lt (le_add_right _ _) h),
induction k with k IH, {cases m; simp [power_aux, R]},
rename R R', let R := repr (power_aux 0 a0 (oadd a0 n a' * of_nat m) k m),
let ω0 := ω ^ repr a0, let α' := ω0 * n + repr a',
change (k ≠ 0 → R < ω0 ^ succ k) ∧ ω0 ^ k * α' + R = (α' + m) ^ succ k at IH,
have RR : R' = ω0 ^ k * (α' * m) + R,
{ by_cases m = 0; simp [h, R', power_aux, R, power_mul],
{ cases k; simp [power_aux] }, { refl } },
have α0 : 0 < α', {simpa [α', lt_def, repr] using oadd_pos a0 n a'},
have ω00 : 0 < ω0 ^ k := power_pos _ (power_pos _ omega_pos),
have Rl : R < ω ^ (repr a0 * succ ↑k),
{ by_cases k0 : k = 0,
{ simp [k0],
refine lt_of_lt_of_le _ (power_le_power_right omega_pos (one_le_iff_ne_zero.2 e0)),
cases m with m; simp [k0, R, power_aux, omega_pos],
rw [← nat.cast_succ], apply nat_lt_omega },
{ rw power_mul, exact IH.1 k0 } },
refine ⟨λ_, _, _⟩,
{ rw [RR, ← power_mul _ _ (succ k.succ)],
have e0 := pos_iff_ne_zero.2 e0,
have rr0 := lt_of_lt_of_le e0 (le_add_left _ _),
apply add_lt_omega_power,
{ simp [power_mul, ω0, power_add],
rw [mul_lt_mul_iff_left ω00, ← ordinal.power_add],
have := (No.below_of_lt _).repr_lt, unfold repr at this,
refine mul_lt_omega_power rr0 this (nat_lt_omega _),
simpa using (add_lt_add_iff_left (repr a0)).2 e0 },
{ refine lt_of_lt_of_le Rl (power_le_power_right omega_pos $
mul_le_mul_left _ $ succ_le_succ.2 $ nat_cast_le.2 $ le_of_lt k.lt_succ_self) } },
refine calc
ω0 ^ k.succ * α' + R'
= ω0 ^ succ k * α' + (ω0 ^ k * α' * m + R) : by rw [nat_cast_succ, RR, ← mul_assoc]
... = (ω0 ^ k * α' + R) * α' + (ω0 ^ k * α' + R) * m : _
... = (α' + m) ^ succ k.succ : by rw [← ordinal.mul_add, ← nat_cast_succ, power_succ, IH.2],
congr_n 1,
{ have αd : ω ∣ α' := dvd_add (dvd_mul_of_dvd _
(by simpa using power_dvd_power ω (one_le_iff_ne_zero.2 e0))) d,
rw [ordinal.mul_add (ω0 ^ k), add_assoc, ← mul_assoc, ← power_succ,
add_mul_limit _ (is_limit_iff_omega_dvd.2 ⟨ne_of_gt α0, αd⟩), mul_assoc,
@mul_omega_dvd n (nat_cast_pos.2 n.pos) (nat_lt_omega _) _ αd],
apply @add_absorp _ (repr a0 * succ k),
{ refine add_lt_omega_power _ Rl,
rw [power_mul, power_succ, mul_lt_mul_iff_left ω00],
exact No.snd'.repr_lt },
{ have := mul_le_mul_left (ω0 ^ succ k) (one_le_iff_pos.2 $ nat_cast_pos.2 n.pos),
rw power_mul, simpa [-power_succ] } },
{ cases m,
{ have : R = 0, {cases k; simp [R, power_aux]}, simp [this] },
{ rw [← nat_cast_succ, add_mul_succ],
apply add_absorp Rl,
rw [power_mul, power_succ],
apply ordinal.mul_le_mul_left,
simpa [α', repr] using omega_le_oadd a0 n a' } }
end
end
theorem repr_power (o₁ o₂) [NF o₁] [NF o₂] : repr (o₁ ^ o₂) = repr o₁ ^ repr o₂ :=
begin
cases e₁ : split o₁ with a m,
cases NF_repr_split e₁ with N₁ r₁,
cases a with a0 n a',
{ cases m with m,
{ by_cases o₂ = 0; simp [power_def, power, e₁, h, r₁],
have := mt repr_inj.1 h, rw zero_power this },
{ cases e₂ : split' o₂ with b' k,
cases NF_repr_split' e₂ with _ r₂,
by_cases m = 0; simp [power_def, power, e₁, h, r₁, e₂, r₂, -nat.cast_succ],
rw [power_add, power_mul, power_omega _ (nat_lt_omega _)],
simpa using nat_cast_lt.2 (nat.succ_lt_succ $ nat.pos_iff_ne_zero.2 h) } },
{ haveI := N₁.fst, haveI := N₁.snd,
cases N₁.of_dvd_omega (split_dvd e₁) with a00 ad,
have al := split_add_lt e₁,
have aa : repr (a' + of_nat m) = repr a' + m, {simp},
cases e₂ : split' o₂ with b' k,
cases NF_repr_split' e₂ with _ r₂,
simp [power_def, power, e₁, r₁, split_eq_scale_split' e₂],
cases k with k,
{ simp [power, r₂, power_mul, repr_power_aux₁ a00 al aa] },
{ simp [succ_eq_add_one, power, r₂, power_add, power_mul, mul_assoc],
rw [repr_power_aux₁ a00 al aa, scale_power_aux], simp [power_mul],
rw [← ordinal.mul_add, ← add_assoc (ω ^ repr a0 * (n:ℕ))], congr_n 1,
rw [← power_succ],
exact (repr_power_aux₂ _ ad a00 al _ _).2 } }
end
end onote
/-- The type of normal ordinal notations. (It would have been
nicer to define this right in the inductive type, but `NF o`
requires `repr` which requires `onote`, so all these things
would have to be defined at once, which messes up the VM
representation.) -/
def nonote := {o : onote // o.NF}
instance : decidable_eq nonote := by unfold nonote; apply_instance
namespace nonote
open onote
instance NF (o : nonote) : NF o.1 := o.2
/-- Construct a `nonote` from an ordinal notation
(and infer normality) -/
def mk (o : onote) [h : NF o] : nonote := ⟨o, h⟩
/-- The ordinal represented by an ordinal notation.
(This function is noncomputable because ordinal
arithmetic is noncomputable. In computational applications
`nonote` can be used exclusively without reference
to `ordinal`, but this function allows for correctness
results to be stated.) -/
noncomputable def repr (o : nonote) : ordinal := o.1.repr
instance : has_to_string nonote := ⟨λ x, x.1.to_string⟩
instance : has_repr nonote := ⟨λ x, x.1.repr'⟩
instance : preorder nonote :=
{ le := λ x y, repr x ≤ repr y,
lt := λ x y, repr x < repr y,
le_refl := λ a, @le_refl ordinal _ _,
le_trans := λ a b c, @le_trans ordinal _ _ _ _,
lt_iff_le_not_le := λ a b, @lt_iff_le_not_le ordinal _ _ _ }
instance : has_zero nonote := ⟨⟨0, NF.zero⟩⟩
/-- Convert a natural number to an ordinal notation -/
def of_nat (n : ℕ) : nonote := ⟨of_nat n, _, NF_below_of_nat _⟩
/-- Compare ordinal notations -/
def cmp (a b : nonote) : ordering :=
cmp a.1 b.1
theorem cmp_compares : ∀ a b : nonote, (cmp a b).compares a b
| ⟨a, ha⟩ ⟨b, hb⟩ := begin
resetI,
dsimp [cmp], have := onote.cmp_compares a b,
cases onote.cmp a b; try {exact this},
exact subtype.mk_eq_mk.2 this
end
instance : linear_order nonote :=
{ le_antisymm := λ a b, match cmp a b, cmp_compares a b with
| ordering.lt, h, h₁, h₂ := (not_lt_of_le h₂).elim h
| ordering.eq, h, h₁, h₂ := h
| ordering.gt, h, h₁, h₂ := (not_lt_of_le h₁).elim h
end,
le_total := λ a b, match cmp a b, cmp_compares a b with
| ordering.lt, h := or.inl (le_of_lt h)
| ordering.eq, h := or.inl (le_of_eq h)
| ordering.gt, h := or.inr (le_of_lt h)
end,
..nonote.preorder }
instance decidable_lt : @decidable_rel nonote (<)
| a b := decidable_of_iff _ (cmp_compares a b).eq_lt
instance : decidable_linear_order nonote :=
{ decidable_le := λ a b, decidable_of_iff _ not_lt,
decidable_lt := nonote.decidable_lt,
..nonote.linear_order }
/-- Asserts that `repr a < ω ^ repr b`. Used in `nonote.rec_on` -/
def below (a b : nonote) : Prop := NF_below a.1 (repr b)
/-- The `oadd` pseudo-constructor for `nonote` -/
def oadd (e : nonote) (n : ℕ+) (a : nonote) (h : below a e) : nonote := ⟨_, NF.oadd e.2 n h⟩
/-- This is a recursor-like theorem for `nonote` suggesting an
inductive definition, which can't actually be defined this
way due to conflicting dependencies. -/
@[elab_as_eliminator] def rec_on {C : nonote → Sort*} (o : nonote)
(H0 : C 0)
(H1 : ∀ e n a h, C e → C a → C (oadd e n a h)) : C o :=
begin
cases o with o h, induction o with e n a IHe IHa,
{ exact H0 },
{ exact H1 ⟨e, h.fst⟩ n ⟨a, h.snd⟩ h.snd' (IHe _) (IHa _) }
end
/-- Addition of ordinal notations -/
instance : has_add nonote := ⟨λ x y, mk (x.1 + y.1)⟩
theorem repr_add (a b) : repr (a + b) = repr a + repr b :=
onote.repr_add a.1 b.1
/-- Subtraction of ordinal notations -/
instance : has_sub nonote := ⟨λ x y, mk (x.1 - y.1)⟩
theorem repr_sub (a b) : repr (a - b) = repr a - repr b :=
onote.repr_sub a.1 b.1
/-- Multiplication of ordinal notations -/
instance : has_mul nonote := ⟨λ x y, mk (x.1 * y.1)⟩
theorem repr_mul (a b) : repr (a * b) = repr a * repr b :=
onote.repr_mul a.1 b.1
/-- Exponentiation of ordinal notations -/
def power (x y : nonote) := mk (x.1.power y.1)
theorem repr_power (a b) : repr (power a b) = (repr a).power (repr b) :=
onote.repr_power a.1 b.1
end nonote
|
ebe66e08e39e636835e060af333d5c3cf09c419a | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/show_term.lean | 7a175f76b71986c0ba4fbee8883e2d4b0a98d6a9 | [] | 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,116 | 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.tactic.core
import Mathlib.PostPort
namespace Mathlib
namespace tactic.interactive
/--
`show_term { tac }` runs the tactic `tac`,
and then prints the term that was constructed.
This is useful for
* constructing term mode proofs from tactic mode proofs, and
* understanding what tactics are doing, and how metavariables are handled.
As an example, in
```
example {P Q R : Prop} (h₁ : Q → P) (h₂ : R) (h₃ : R → Q) : P ∧ R :=
by show_term { tauto }
```
the term mode proof `⟨h₁ (h₃ h₂), eq.mpr rfl h₂⟩` produced by `tauto` will be printed.
As another example, if the goal is `ℕ × ℕ`, `show_term { split, exact 0 }` will
print `refine (0, _)`, and afterwards there will be one remaining goal (of type `ℕ`).
This indicates that `split, exact 0` partially filled in the original metavariable,
but created a new metavariable for the resulting sub-goal.
-/
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.