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
548568973bca92d082da8b6339bb3a5edd7f4bd1
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/bounds.lean
ea3cb54de8655e1d8eb62f35ba70e7b14f0b51d8
[ "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
30,673
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import data.set.intervals.basic import algebra.ordered_group /-! # Upper / lower bounds In this file we define: * `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set; * `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper (resp., lower) bounds of `s` is nonempty; * `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`; for a partial order, it is unique if exists; * `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound) of `s`; for a partial order, it is unique if exists. We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ open set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section variables [preorder α] [preorder β] {s t : set α} {a b : α} /-! ### Definitions -/ /-- The set of upper bounds of a set. -/ def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x } /-- The set of lower bounds of a set. -/ def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a } /-- A set is bounded above if there exists an upper bound. -/ def bdd_above (s : set α) := (upper_bounds s).nonempty /-- A set is bounded below if there exists a lower bound. -/ def bdd_below (s : set α) := (lower_bounds s).nonempty /-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/ def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s /-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/ def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s /-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/ def is_lub (s : set α) : α → Prop := is_least (upper_bounds s) /-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/ def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s) lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/ lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) := by simp [bdd_above, upper_bounds, set.nonempty] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/ lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬(x ≤ y) := @not_bdd_above_iff' (order_dual α) _ _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bdd_above_iff'`. -/ lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y := by simp only [not_bdd_above_iff', not_le] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bdd_below_iff'`. -/ lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x := @not_bdd_above_iff (order_dual α) _ _ /-! ### Monotonicity -/ lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : upper_bounds t ⊆ upper_bounds s := λ b hb x h, hb $ hst h lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : lower_bounds t ⊆ lower_bounds s := λ b hb x h, hb $ hst h lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s := λ ha x h, le_trans (ha h) hab lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s := λ hb x h, le_trans hab (hb h) lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds t → b ∈ upper_bounds s := λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds t → a ∈ lower_bounds s := λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s := nonempty.mono $ upper_bounds_mono_set h /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s := nonempty.mono $ lower_bounds_mono_set h /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a := ⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩ /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a := @is_lub.of_subset_of_superset (order_dual α) _ a s t p hs hp hst htp lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a := hb.2 (hst ha.1) lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b := hb.2 (hst ha.1) lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b := hb.mono ha $ upper_bounds_mono_set hst lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a := hb.mono ha $ lower_bounds_mono_set hst /-! ### Conversions -/ lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a := set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩ lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a := @is_lub.upper_bounds_eq (order_dual α) _ _ _ h lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a := h.is_glb.lower_bounds_eq lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a := h.is_lub.upper_bounds_eq lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s := by { rw h.upper_bounds_eq, refl } lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s := by { rw h.lower_bounds_eq, refl } /-- If `s` has a least upper bound, then it is bounded above. -/ lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩ /-- If `s` has a greatest lower bound, then it is bounded below. -/ lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩ /-- If `s` has a greatest element, then it is bounded above. -/ lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩ /-- If `s` has a least element, then it is bounded below. -/ lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩ lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩ lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩ /-! ### Union and intersection -/ @[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t := subset.antisymm (λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩) (λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht)) @[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t := @upper_bounds_union (order_dual α) _ s t lemma union_upper_bounds_subset_upper_bounds_inter : upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) := union_subset (upper_bounds_mono_set $ inter_subset_left _ _) (upper_bounds_mono_set $ inter_subset_right _ _) lemma union_lower_bounds_subset_lower_bounds_inter : lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) := @union_upper_bounds_subset_upper_bounds_inter (order_dual α) _ s t lemma is_least_union_iff {a : α} {s t : set α} : is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) := by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc] lemma is_greatest_union_iff : is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨ a ∈ upper_bounds s ∧ is_greatest t a) := @is_least_union_iff (order_dual α) _ a s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/ lemma bdd_above.union [semilattice_sup γ] {s t : set γ} : bdd_above s → bdd_above t → bdd_above (s ∪ t) := begin rintros ⟨bs, hs⟩ ⟨bt, ht⟩, use bs ⊔ bt, rw upper_bounds_union, exact ⟨upper_bounds_mono_mem le_sup_left hs, upper_bounds_mono_mem le_sup_right ht⟩ end /-- The union of two sets is bounded above if and only if each of the sets is. -/ lemma bdd_above_union [semilattice_sup γ] {s t : set γ} : bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t := ⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩, λ h, h.1.union h.2⟩ lemma bdd_below.union [semilattice_inf γ] {s t : set γ} : bdd_below s → bdd_below t → bdd_below (s ∪ t) := @bdd_above.union (order_dual γ) _ s t /--The union of two sets is bounded above if and only if each of the sets is.-/ lemma bdd_below_union [semilattice_inf γ] {s t : set γ} : bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t := @bdd_above_union (order_dual γ) _ s t /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ} (hs : is_lub s a) (ht : is_lub t b) : is_lub (s ∪ t) (a ⊔ b) := ⟨λ 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₂) := @is_lub.union (order_dual γ) _ _ _ _ _ hs ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_glb.union hb.is_glb).1⟩ /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_lub.union hb.is_lub).1⟩ 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 := @is_lub.inter_Ici_of_mem (order_dual γ) _ _ _ _ ha hb /-! ### Specific sets #### Unbounded intervals -/ lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩ lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩ lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩ lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩ section variables [linear_order γ] [densely_ordered γ] lemma is_lub_Iio {a : γ} : is_lub (Iio a) a := ⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩ lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio (order_dual γ) _ _ a lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq end /-! #### Singleton -/ lemma is_greatest_singleton : is_greatest {a} a := ⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩ lemma is_least_singleton : is_least {a} a := @is_greatest_singleton (order_dual α) _ a lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below @[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq @[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq /-! #### Bounded intervals -/ lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩ lemma bdd_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 (order_dual γ) _ _ b a hab lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b := (is_lub_Ioo hab).upper_bounds_eq lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b := by simpa only [dual_Ioc] using @is_glb_Ioc (order_dual γ) _ _ b a hab lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b := (is_lub_Ico hab).upper_bounds_eq end lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b := by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici, bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right] /-! ### Univ -/ lemma order_top.upper_bounds_univ [order_top γ] : upper_bounds (univ : set γ) = {⊤} := set.ext $ λ b, iff.trans ⟨λ hb, top_unique $ hb trivial, λ hb x hx, hb.symm ▸ le_top⟩ mem_singleton_iff.symm lemma is_greatest_univ [order_top γ] : is_greatest (univ : set γ) ⊤ := by simp only [is_greatest, order_top.upper_bounds_univ, mem_univ, mem_singleton, true_and] lemma is_lub_univ [order_top γ] : is_lub (univ : set γ) ⊤ := is_greatest_univ.is_lub lemma order_bot.lower_bounds_univ [order_bot γ] : lower_bounds (univ : set γ) = {⊥} := @order_top.upper_bounds_univ (order_dual γ) _ lemma is_least_univ [order_bot γ] : is_least (univ : set γ) ⊥ := @is_greatest_univ (order_dual γ) _ lemma is_glb_univ [order_bot γ] : is_glb (univ : set γ) ⊥ := is_least_univ.is_glb lemma no_top_order.upper_bounds_univ [no_top_order α] : upper_bounds (univ : set α) = ∅ := eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := no_top b in not_le_of_lt hx (hb trivial) lemma no_bot_order.lower_bounds_univ [no_bot_order α] : lower_bounds (univ : set α) = ∅ := @no_top_order.upper_bounds_univ (order_dual α) _ _ /-! ### Empty set -/ @[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ := by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff] @[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ := @upper_bounds_empty (order_dual α) _ @[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) := by simp only [bdd_above, upper_bounds_empty, univ_nonempty] @[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) := by simp only [bdd_below, lower_bounds_empty, univ_nonempty] lemma is_glb_empty [order_top γ] : is_glb ∅ (⊤:γ) := by simp only [is_glb, lower_bounds_empty, is_greatest_univ] lemma is_lub_empty [order_bot γ] : is_lub ∅ (⊥:γ) := @is_glb_empty (order_dual γ) _ lemma is_lub.nonempty [no_bot_order α] (hs : is_lub s a) : s.nonempty := let ⟨a', ha'⟩ := no_bot a in ne_empty_iff_nonempty.1 $ assume h, have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty], not_le_of_lt ha' this lemma is_glb.nonempty [no_top_order α] (hs : is_glb s a) : s.nonempty := @is_lub.nonempty (order_dual α) _ _ _ _ hs lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty := nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty := @nonempty_of_not_bdd_above (order_dual α) _ _ _ h /-! ### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} : bdd_above (insert a s) ↔ bdd_above s := by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and] lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) : bdd_above (insert a s) := (bdd_above_insert a).2 hs /--Adding a point to a set preserves its boundedness below.-/ @[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} : bdd_below (insert a s) ↔ bdd_below s := by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and] lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) : bdd_below (insert a s) := (bdd_below_insert a).2 hs lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) : is_lub (insert a s) (a ⊔ b) := by { rw insert_eq, exact is_lub_singleton.union hs } lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) : is_glb (insert a s) (a ⊓ b) := by { rw insert_eq, exact is_glb_singleton.union hs } lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) : is_greatest (insert a s) (max a b) := by { rw insert_eq, exact is_greatest_singleton.union hs } lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) : is_least (insert a s) (min a b) := by { rw insert_eq, exact is_least_singleton.union hs } @[simp] lemma upper_bounds_insert (a : α) (s : set α) : upper_bounds (insert a s) = Ici a ∩ upper_bounds s := by rw [insert_eq, upper_bounds_union, upper_bounds_singleton] @[simp] lemma lower_bounds_insert (a : α) (s : set α) : lower_bounds (insert a s) = Iic a ∩ lower_bounds s := by rw [insert_eq, lower_bounds_union, lower_bounds_singleton] /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected lemma order_top.bdd_above [order_top γ] (s : set γ) : bdd_above s := ⟨⊤, assume a ha, order_top.le_top a⟩ /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected lemma order_bot.bdd_below [order_bot γ] (s : set γ) : bdd_below s := ⟨⊥, assume a ha, order_bot.bot_le a⟩ /-! ### Pair -/ lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) := is_lub_singleton.insert _ lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) := is_glb_singleton.insert _ lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) := is_least_singleton.insert _ lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) := is_greatest_singleton.insert _ end /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ section preorder variables [preorder α] {s : set α} {a b : α} lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : s.nonempty → a ≤ b | ⟨c, hc⟩ := le_trans (ha hc) (hb hc) lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b := lower_bounds_le_upper_bounds ha.1 hb.1 hs lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b := ⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩ lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c := @is_lub_lt_iff (order_dual α) _ s _ _ ha 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 (order_dual α) _ _ _ _ h 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 variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) : f a ∈ upper_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) : f a ∈ lower_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) /-- The image under a monotone function of a set which is bounded above is bounded above. -/ lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩ /-- The image under a monotone function of a set which is bounded below is bounded below. -/ lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩ /-- A monotone map sends a least element of a set to a least element of its image. -/ lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩ /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩ lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) : b ≤ f a := Hb.2 (Hf.mem_upper_bounds_image Ha.1) lemma le_is_glb_image (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) : f a ≤ b := Hb.2 (Hf.mem_lower_bounds_image Ha.1) end monotone lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_glb (f '' s) (f x)) : is_glb s x := ⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy, λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩ lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_lub (f '' s) (f x)) : is_lub s x := @is_glb.of_image (order_dual α) (order_dual β) _ _ f (λ x y, hf) _ _ hx
053c97bbab8645081a7061c41826a0252224abfa
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monoidal/transport_auto.lean
7832daffd6ccaf02fc06e89ba0402d6eca17ba29
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,684
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.category_theory.monoidal.natural_transformation import Mathlib.PostPort universes u₁ u₂ v₁ v₂ namespace Mathlib /-! # Transport a monoidal structure along an equivalence. When `C` and `D` are equivalent as categories, we can transport a monoidal structure on `C` along the equivalence, obtaining a monoidal structure on `D`. We don't yet prove anything about this transported structure! The next step would be to show that the original functor can be upgraded to a monoidal functor with respect to this new structure. -/ namespace category_theory.monoidal /-- Transport a monoidal structure along an equivalence of (plain) categories. -/ @[simp] theorem transport_tensor_unit {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : 𝟙_ = functor.obj (equivalence.functor e) 𝟙_ := Eq.refl 𝟙_ /-- A type synonym for `D`, which will carry the transported monoidal structure. -/ def transported {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) := D protected instance transported.category_theory.monoidal_category {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : monoidal_category (transported e) := transport e protected instance transported.inhabited {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : Inhabited (transported e) := { default := 𝟙_ } /-- We can upgrade `e.functor` to a lax monoidal functor from `C` to `D` with the transported structure. -/ @[simp] theorem lax_to_transported_to_functor_map {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) {X : C} {Y : C} : ∀ (ᾰ : X ⟶ Y), functor.map (lax_monoidal_functor.to_functor (lax_to_transported e)) ᾰ = functor.map (equivalence.functor e) ᾰ := fun (ᾰ : X ⟶ Y) => Eq.refl (functor.map (lax_monoidal_functor.to_functor (lax_to_transported e)) ᾰ) /-- We can upgrade `e.functor` to a monoidal functor from `C` to `D` with the transported structure. -/ @[simp] theorem to_transported_ε_is_iso {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : monoidal_functor.ε_is_iso (to_transported e) = id (is_iso.id (functor.obj (equivalence.functor e) 𝟙_)) := Eq.refl (monoidal_functor.ε_is_iso (to_transported e)) /-- We can upgrade `e.inverse` to a lax monoidal functor from `D` with the transported structure to `C`. -/ def lax_from_transported {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : lax_monoidal_functor (transported e) C := lax_monoidal_functor.mk (functor.mk (functor.obj (equivalence.inverse e)) (functor.map (equivalence.inverse e))) (nat_trans.app (equivalence.unit e) 𝟙_) fun (X Y : transported e) => nat_trans.app (equivalence.unit e) (functor.obj (equivalence.inverse e) X ⊗ functor.obj (equivalence.inverse e) Y) /-- We can upgrade `e.inverse` to a monoidal functor from `D` with the transported structure to `C`. -/ def from_transported {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : monoidal_functor (transported e) C := monoidal_functor.mk (lax_monoidal_functor.mk (lax_monoidal_functor.to_functor (lax_from_transported e)) (lax_monoidal_functor.ε (lax_from_transported e)) (lax_monoidal_functor.μ (lax_from_transported e))) /-- The unit isomorphism upgrades to a monoidal isomorphism. -/ def transported_monoidal_unit_iso {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) : lax_monoidal_functor.id C ≅ lax_to_transported e ⊗⋙ lax_from_transported e := monoidal_nat_iso.of_components (fun (X : C) => iso.app (equivalence.unit_iso e) X) sorry sorry sorry /-- The counit isomorphism upgrades to a monoidal isomorphism. -/ @[simp] theorem transported_monoidal_counit_iso_hom_to_nat_trans_app {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] (e : C ≌ D) (X : transported e) : nat_trans.app (monoidal_nat_trans.to_nat_trans (iso.hom (transported_monoidal_counit_iso e))) X = nat_trans.app (iso.hom (equivalence.counit_iso e)) X := Eq.refl (nat_trans.app (iso.hom (equivalence.counit_iso e)) X) end Mathlib
ddd442c0156d99761986d95034a6e24612cfc7c2
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/logic.lean
5d8719ad21851f9433d008729b5d3f96946eff8d
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
10,242
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Leonardo de Moura, Jeremy Avigad definition Prop [inline] := Type.{0} inductive false : Prop theorem false_elim (c : Prop) (H : false) : c := false_rec c H inductive true : Prop := | trivial : true abbreviation not (a : Prop) := a → false prefix `¬`:40 := not notation `assume` binders `,` r:(scoped f, f) := r notation `take` binders `,` r:(scoped f, f) := r theorem not_intro {a : Prop} (H : a → false) : ¬a := H theorem not_elim {a : Prop} (H1 : ¬a) (H2 : a) : false := H1 H2 theorem absurd {a : Prop} (H1 : a) (H2 : ¬a) : false := H2 H1 theorem not_not_intro {a : Prop} (Ha : a) : ¬¬a := assume Hna : ¬a, absurd Ha Hna theorem mt {a b : Prop} (H1 : a → b) (H2 : ¬b) : ¬a := assume Ha : a, absurd (H1 Ha) H2 theorem contrapos {a b : Prop} (H : a → b) : ¬b → ¬a := assume Hnb : ¬b, mt H Hnb theorem absurd_elim {a : Prop} (b : Prop) (H1 : a) (H2 : ¬a) : b := false_elim b (absurd H1 H2) theorem absurd_not_true (H : ¬true) : false := absurd trivial H theorem not_false_trivial : ¬false := assume H : false, H theorem not_implies_left {a b : Prop} (H : ¬(a → b)) : ¬¬a := assume Hna : ¬a, absurd (assume Ha : a, absurd_elim b Ha Hna) H theorem not_implies_right {a b : Prop} (H : ¬(a → b)) : ¬b := assume Hb : b, absurd (assume Ha : a, Hb) H inductive and (a b : Prop) : Prop := | and_intro : a → b → and a b infixr `/\`:35 := and infixr `∧`:35 := and theorem and_elim {a b c : Prop} (H1 : a → b → c) (H2 : a ∧ b) : c := and_rec H1 H2 theorem and_elim_left {a b : Prop} (H : a ∧ b) : a := and_rec (λa b, a) H theorem and_elim_right {a b : Prop} (H : a ∧ b) : b := and_rec (λa b, b) H theorem and_swap {a b : Prop} (H : a ∧ b) : b ∧ a := and_intro (and_elim_right H) (and_elim_left H) theorem and_not_left {a : Prop} (b : Prop) (Hna : ¬a) : ¬(a ∧ b) := assume H : a ∧ b, absurd (and_elim_left H) Hna theorem and_not_right (a : Prop) {b : Prop} (Hnb : ¬b) : ¬(a ∧ b) := assume H : a ∧ b, absurd (and_elim_right H) Hnb inductive or (a b : Prop) : Prop := | or_intro_left : a → or a b | or_intro_right : b → or a b infixr `\/`:30 := or infixr `∨`:30 := or theorem or_inl {a b : Prop} (Ha : a) : a ∨ b := or_intro_left b Ha theorem or_inr {a b : Prop} (Hb : b) : a ∨ b := or_intro_right a Hb theorem or_elim {a b c : Prop} (H1 : a ∨ b) (H2 : a → c) (H3 : b → c) : c := or_rec H2 H3 H1 theorem resolve_right {a b : Prop} (H1 : a ∨ b) (H2 : ¬a) : b := or_elim H1 (assume Ha, absurd_elim b Ha H2) (assume Hb, Hb) theorem resolve_left {a b : Prop} (H1 : a ∨ b) (H2 : ¬b) : a := or_elim H1 (assume Ha, Ha) (assume Hb, absurd_elim a Hb H2) theorem or_swap {a b : Prop} (H : a ∨ b) : b ∨ a := or_elim H (assume Ha, or_inr Ha) (assume Hb, or_inl Hb) theorem or_not_intro {a b : Prop} (Hna : ¬a) (Hnb : ¬b) : ¬(a ∨ b) := assume H : a ∨ b, or_elim H (assume Ha, absurd_elim _ Ha Hna) (assume Hb, absurd_elim _ Hb Hnb) theorem or_imp_or {a b c d : Prop} (H1 : a ∨ b) (H2 : a → c) (H3 : b → d) : c ∨ d := or_elim H1 (assume Ha : a, or_inl (H2 Ha)) (assume Hb : b, or_inr (H3 Hb)) theorem imp_or_left {a b c : Prop} (H1 : a ∨ c) (H : a → b) : b ∨ c := or_elim H1 (assume H2 : a, or_inl (H H2)) (assume H2 : c, or_inr H2) theorem imp_or_right {a b c : Prop} (H1 : c ∨ a) (H : a → b) : c ∨ b := or_elim H1 (assume H2 : c, or_inl H2) (assume H2 : a, or_inr (H H2)) inductive eq {A : Type} (a : A) : A → Prop := | refl : eq a a infix `=`:50 := eq theorem subst {A : Type} {a b : A} {P : A → Prop} (H1 : a = b) (H2 : P a) : P b := eq_rec H2 H1 theorem trans {A : Type} {a b c : A} (H1 : a = b) (H2 : b = c) : a = c := subst H2 H1 calc_subst subst calc_refl refl calc_trans trans theorem true_ne_false : ¬true = false := assume H : true = false, subst H trivial theorem symm {A : Type} {a b : A} (H : a = b) : b = a := subst H (refl a) namespace eq_proofs postfix `⁻¹`:100 := symm infixr `⬝`:75 := trans infixr `▸`:75 := subst end using eq_proofs theorem congr1 {A : Type} {B : A → Type} {f g : Π x, B x} (H : f = g) (a : A) : f a = g a := H ▸ (refl (f a)) theorem congr2 {A : Type} {B : Type} {a b : A} (f : A → B) (H : a = b) : f a = f b := H ▸ (refl (f a)) theorem congr {A : Type} {B : Type} {f g : A → B} {a b : A} (H1 : f = g) (H2 : a = b) : f a = g b := H1 ▸ H2 ▸ (refl (f a)) theorem equal_f {A : Type} {B : A → Type} {f g : Π x, B x} (H : f = g) : ∀x, f x = g x := take x, congr1 H x theorem not_congr {a b : Prop} (H : a = b) : (¬a) = (¬b) := congr2 not H theorem eqmp {a b : Prop} (H1 : a = b) (H2 : a) : b := H1 ▸ H2 infixl `<|`:100 := eqmp infixl `◂`:100 := eqmp theorem eqmpr {a b : Prop} (H1 : a = b) (H2 : b) : a := H1⁻¹ ◂ H2 theorem eqt_elim {a : Prop} (H : a = true) : a := H⁻¹ ◂ trivial theorem eqf_elim {a : Prop} (H : a = false) : ¬a := assume Ha : a, H ◂ Ha theorem imp_trans {a b c : Prop} (H1 : a → b) (H2 : b → c) : a → c := assume Ha, H2 (H1 Ha) theorem imp_eq_trans {a b c : Prop} (H1 : a → b) (H2 : b = c) : a → c := assume Ha, H2 ◂ (H1 Ha) theorem eq_imp_trans {a b c : Prop} (H1 : a = b) (H2 : b → c) : a → c := assume Ha, H2 (H1 ◂ Ha) definition ne [inline] {A : Type} (a b : A) := ¬(a = b) infix `≠`:50 := ne theorem ne_intro {A : Type} {a b : A} (H : a = b → false) : a ≠ b := H theorem ne_elim {A : Type} {a b : A} (H1 : a ≠ b) (H2 : a = b) : false := H1 H2 theorem a_neq_a_elim {A : Type} {a : A} (H : a ≠ a) : false := H (refl a) theorem ne_irrefl {A : Type} {a : A} (H : a ≠ a) : false := H (refl a) theorem ne_symm {A : Type} {a b : A} (H : a ≠ b) : b ≠ a := assume H1 : b = a, H (H1⁻¹) theorem eq_ne_trans {A : Type} {a b c : A} (H1 : a = b) (H2 : b ≠ c) : a ≠ c := H1⁻¹ ▸ H2 theorem ne_eq_trans {A : Type} {a b c : A} (H1 : a ≠ b) (H2 : b = c) : a ≠ c := H2 ▸ H1 calc_trans eq_ne_trans calc_trans ne_eq_trans definition iff (a b : Prop) := (a → b) ∧ (b → a) infix `<->`:25 := iff infix `↔`:25 := iff theorem iff_intro {a b : Prop} (H1 : a → b) (H2 : b → a) : a ↔ b := and_intro H1 H2 theorem iff_elim {a b c : Prop} (H1 : (a → b) → (b → a) → c) (H2 : a ↔ b) : c := and_rec H1 H2 theorem iff_elim_left {a b : Prop} (H : a ↔ b) : a → b := iff_elim (assume H1 H2, H1) H theorem iff_elim_right {a b : Prop} (H : a ↔ b) : b → a := iff_elim (assume H1 H2, H2) H theorem iff_mp_left {a b : Prop} (H1 : a ↔ b) (H2 : a) : b := (iff_elim_left H1) H2 theorem iff_mp_right {a b : Prop} (H1 : a ↔ b) (H2 : b) : a := (iff_elim_right H1) H2 theorem iff_flip_sign {a b : Prop} (H1 : a ↔ b) : ¬a ↔ ¬b := iff_intro (assume Hna, mt (iff_elim_right H1) Hna) (assume Hnb, mt (iff_elim_left H1) Hnb) theorem iff_refl (a : Prop) : a ↔ a := iff_intro (assume H, H) (assume H, H) theorem iff_trans {a b c : Prop} (H1 : a ↔ b) (H2 : b ↔ c) : a ↔ c := iff_intro (assume Ha, iff_mp_left H2 (iff_mp_left H1 Ha)) (assume Hc, iff_mp_right H1 (iff_mp_right H2 Hc)) theorem iff_symm {a b : Prop} (H : a ↔ b) : b ↔ a := iff_intro (assume Hb, iff_mp_right H Hb) (assume Ha, iff_mp_left H Ha) calc_trans iff_trans theorem eq_to_iff {a b : Prop} (H : a = b) : a ↔ b := iff_intro (λ Ha, H ▸ Ha) (λ Hb, H⁻¹ ▸ Hb) theorem and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := iff_intro (λH, and_swap H) (λH, and_swap H) theorem and_assoc (a b c : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := iff_intro (assume H, and_intro (and_elim_left (and_elim_left H)) (and_intro (and_elim_right (and_elim_left H)) (and_elim_right H))) (assume H, and_intro (and_intro (and_elim_left H) (and_elim_left (and_elim_right H))) (and_elim_right (and_elim_right H))) theorem or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := iff_intro (λH, or_swap H) (λH, or_swap H) theorem or_assoc (a b c : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := iff_intro (assume H, or_elim H (assume H1, or_elim H1 (assume Ha, or_inl Ha) (assume Hb, or_inr (or_inl Hb))) (assume Hc, or_inr (or_inr Hc))) (assume H, or_elim H (assume Ha, (or_inl (or_inl Ha))) (assume H1, or_elim H1 (assume Hb, or_inl (or_inr Hb)) (assume Hc, or_inr Hc))) inductive Exists {A : Type} (P : A → Prop) : Prop := | exists_intro : ∀ (a : A), P a → Exists P notation `exists` binders `,` r:(scoped P, Exists P) := r notation `∃` binders `,` r:(scoped P, Exists P) := r theorem exists_elim {A : Type} {p : A → Prop} {B : Prop} (H1 : ∃x, p x) (H2 : ∀ (a : A) (H : p a), B) : B := Exists_rec H2 H1 theorem exists_not_forall {A : Type} {p : A → Prop} (H : ∃x, p x) : ¬∀x, ¬p x := assume H1 : ∀x, ¬p x, obtain (w : A) (Hw : p w), from H, absurd Hw (H1 w) theorem forall_not_exists {A : Type} {p : A → Prop} (H2 : ∀x, p x) : ¬∃x, ¬p x := assume H1 : ∃x, ¬p x, obtain (w : A) (Hw : ¬p w), from H1, absurd (H2 w) Hw definition exists_unique {A : Type} (p : A → Prop) := ∃x, p x ∧ ∀y, y ≠ x → ¬p y notation `∃!` binders `,` r:(scoped P, exists_unique P) := r theorem exists_unique_intro {A : Type} {p : A → Prop} (w : A) (H1 : p w) (H2 : ∀y, y ≠ w → ¬p y) : ∃!x, p x := exists_intro w (and_intro H1 H2) theorem exists_unique_elim {A : Type} {p : A → Prop} {b : Prop} (H2 : ∃!x, p x) (H1 : ∀x, p x → (∀y, y ≠ x → ¬p y) → b) : b := obtain w Hw, from H2, H1 w (and_elim_left Hw) (and_elim_right Hw) inductive inhabited (A : Type) : Prop := | inhabited_intro : A → inhabited A theorem inhabited_elim {A : Type} {B : Prop} (H1 : inhabited A) (H2 : A → B) : B := inhabited_rec H2 H1 theorem inhabited_Prop [instance] : inhabited Prop := inhabited_intro true theorem inhabited_fun [instance] (A : Type) {B : Type} (H : inhabited B) : inhabited (A → B) := inhabited_elim H (take b, inhabited_intro (λa, b)) theorem inhabited_exists {A : Type} {p : A → Prop} (H : ∃x, p x) : inhabited A := obtain w Hw, from H, inhabited_intro w
b8df80a39f03f527ae544c9808c89a7f8f29fd02
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/data/nat/parity.lean
5ad13ab7158bee9c4936c44020172a9279e92f60
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,081
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 Parity. -/ import data.nat.power logic.identities namespace nat open decidable open algebra definition even (n : nat) := n % 2 = 0 definition decidable_even [instance] : ∀ n, decidable (even n) := take n, !nat.has_decidable_eq definition odd (n : nat) := ¬even n definition decidable_odd [instance] : ∀ n, decidable (odd n) := take n, decidable_not lemma even_of_dvd {n} : 2 ∣ n → even n := mod_eq_zero_of_dvd lemma dvd_of_even {n} : even n → 2 ∣ n := dvd_of_mod_eq_zero lemma not_odd_zero : ¬ odd 0 := dec_trivial lemma even_zero : even 0 := dec_trivial lemma odd_one : odd 1 := dec_trivial lemma not_even_one : ¬ even 1 := dec_trivial lemma odd_eq_not_even (n : nat) : odd n = ¬ even n := rfl lemma odd_iff_not_even (n : nat) : odd n ↔ ¬ even n := !iff.refl lemma odd_of_not_even {n} : ¬ even n → odd n := suppose ¬ even n, iff.mpr !odd_iff_not_even this lemma even_of_not_odd {n} : ¬ odd n → even n := suppose ¬ odd n, not_not_elim (iff.mp (not_iff_not_of_iff !odd_iff_not_even) this) lemma not_odd_of_even {n} : even n → ¬ odd n := suppose even n, iff.mpr (not_iff_not_of_iff !odd_iff_not_even) (not_not_intro this) lemma not_even_of_odd {n} : odd n → ¬ even n := suppose odd n, iff.mp !odd_iff_not_even this lemma odd_succ_of_even {n} : even n → odd (succ n) := suppose even n, have n ≡ 0 [mod 2], from this, have n+1 ≡ 0+1 [mod 2], from add_mod_eq_add_mod_right 1 this, have h : n+1 ≡ 1 [mod 2], from this, by_contradiction (suppose ¬ odd (succ n), have n+1 ≡ 0 [mod 2], from even_of_not_odd this, have 1 ≡ 0 [mod 2], from eq.trans (eq.symm h) this, assert 1 = 0, from this, by contradiction) lemma eq_1_of_ne_0_lt_2 : ∀ {n : nat}, n ≠ 0 → n < 2 → n = 1 | 0 h₁ h₂ := absurd rfl h₁ | 1 h₁ h₂ := rfl | (n+2) h₁ h₂ := absurd (lt_of_succ_lt_succ (lt_of_succ_lt_succ h₂)) !not_lt_zero lemma mod_eq_of_odd {n} : odd n → n % 2 = 1 := suppose odd n, have ¬ n % 2 = 0, from this, have n % 2 < 2, from mod_lt n dec_trivial, eq_1_of_ne_0_lt_2 `¬ n % 2 = 0` `n % 2 < 2` lemma odd_of_mod_eq {n} : n % 2 = 1 → odd n := suppose n % 2 = 1, by_contradiction (suppose ¬ odd n, assert n % 2 = 0, from even_of_not_odd this, by rewrite this at *; contradiction) lemma even_succ_of_odd {n} : odd n → even (succ n) := suppose odd n, assert n % 2 = 1 % 2, from mod_eq_of_odd this, assert (n+1) % 2 = 2 % 2, from add_mod_eq_add_mod_right 1 this, by rewrite mod_self at this; exact this lemma odd_succ_succ_of_odd {n} : odd n → odd (succ (succ n)) := suppose odd n, odd_succ_of_even (even_succ_of_odd this) lemma even_succ_succ_of_even {n} : even n → even (succ (succ n)) := suppose even n, even_succ_of_odd (odd_succ_of_even this) lemma even_of_odd_succ {n} : odd (succ n) → even n := suppose odd (succ n), by_contradiction (suppose ¬ even n, have odd n, from odd_of_not_even this, have even (succ n), from even_succ_of_odd this, absurd this (not_even_of_odd `odd (succ n)`)) lemma odd_of_even_succ {n} : even (succ n) → odd n := suppose even (succ n), by_contradiction (suppose ¬ odd n, have even n, from even_of_not_odd this, have odd (succ n), from odd_succ_of_even this, absurd `even (succ n)` (not_even_of_odd this)) lemma even_of_even_succ_succ {n} : even (succ (succ n)) → even n := suppose even (n+2), even_of_odd_succ (odd_of_even_succ this) lemma odd_of_odd_succ_succ {n} : odd (succ (succ n)) → odd n := suppose odd (n+2), odd_of_even_succ (even_of_odd_succ this) lemma dvd_of_odd {n} : odd n → 2 ∣ n+1 := suppose odd n, dvd_of_even (even_succ_of_odd this) lemma odd_of_dvd {n} : 2 ∣ n+1 → odd n := suppose 2 ∣ n+1, odd_of_even_succ (even_of_dvd this) lemma even_two_mul : ∀ n, even (2 * n) := take n, even_of_dvd (dvd_mul_right 2 n) lemma odd_two_mul_plus_one : ∀ n, odd (2 * n + 1) := take n, odd_succ_of_even (even_two_mul n) lemma not_even_two_mul_plus_one : ∀ n, ¬ even (2 * n + 1) := take n, not_even_of_odd (odd_two_mul_plus_one n) lemma not_odd_two_mul : ∀ n, ¬ odd (2 * n) := take n, not_odd_of_even (even_two_mul n) lemma even_pred_of_odd : ∀ {n}, odd n → even (pred n) | 0 h := absurd h not_odd_zero | (n+1) h := even_of_odd_succ h lemma even_or_odd : ∀ n, even n ∨ odd n := λ n, by_cases (λ h : even n, or.inl h) (λ h : ¬ even n, or.inr (odd_of_not_even h)) lemma exists_of_even {n} : even n → ∃ k, n = 2*k := λ h, exists_eq_mul_right_of_dvd (dvd_of_even h) lemma exists_of_odd : ∀ {n}, odd n → ∃ k, n = 2*k + 1 | 0 h := absurd h not_odd_zero | (n+1) h := obtain k (hk : n = 2*k), from exists_of_even (even_of_odd_succ h), exists.intro k (by subst n) lemma even_of_exists {n} : (∃ k, n = 2 * k) → even n := suppose ∃ k, n = 2 * k, obtain k (hk : n = 2 * k), from this, have 2 ∣ n, by subst n; apply dvd_mul_right, even_of_dvd this lemma odd_of_exists {n} : (∃ k, n = 2 * k + 1) → odd n := assume h, by_contradiction (λ hn, have even n, from even_of_not_odd hn, have ∃ k, n = 2 * k, from exists_of_even this, obtain k₁ (hk₁ : n = 2 * k₁ + 1), from h, obtain k₂ (hk₂ : n = 2 * k₂), from this, assert (2 * k₁ + 1) % 2 = (2 * k₂) % 2, by rewrite [-hk₁, -hk₂], begin rewrite [mul_mod_right at this, add.comm at this, add_mul_mod_self_left at this], contradiction end) lemma even_add_of_even_of_even {n m} : even n → even m → even (n+m) := suppose even n, suppose even m, obtain k₁ (hk₁ : n = 2 * k₁), from exists_of_even `even n`, obtain k₂ (hk₂ : m = 2 * k₂), from exists_of_even `even m`, even_of_exists (exists.intro (k₁+k₂) (by rewrite [hk₁, hk₂, left_distrib])) lemma even_add_of_odd_of_odd {n m} : odd n → odd m → even (n+m) := suppose odd n, suppose odd m, assert even (succ n + succ m), from even_add_of_even_of_even (even_succ_of_odd `odd n`) (even_succ_of_odd `odd m`), have even(succ (succ (n + m))), by rewrite [add_succ at this, succ_add at this]; exact this, even_of_even_succ_succ this lemma odd_add_of_even_of_odd {n m} : even n → odd m → odd (n+m) := suppose even n, suppose odd m, assert even (n + succ m), from even_add_of_even_of_even `even n` (even_succ_of_odd `odd m`), odd_of_even_succ this lemma odd_add_of_odd_of_even {n m} : odd n → even m → odd (n+m) := suppose odd n, suppose even m, assert odd (m+n), from odd_add_of_even_of_odd `even m` `odd n`, by rewrite add.comm at this; exact this lemma even_mul_of_even_left {n} (m) : even n → even (n*m) := suppose even n, obtain k (hk : n = 2*k), from exists_of_even this, even_of_exists (exists.intro (k*m) (by rewrite [hk, mul.assoc])) lemma even_mul_of_even_right {n} (m) : even n → even (m*n) := suppose even n, assert even (n*m), from even_mul_of_even_left _ this, by rewrite mul.comm at this; exact this lemma odd_mul_of_odd_of_odd {n m} : odd n → odd m → odd (n*m) := suppose odd n, suppose odd m, assert even (n * succ m), from even_mul_of_even_right _ (even_succ_of_odd `odd m`), assert even (n * m + n), by rewrite mul_succ at this; exact this, by_contradiction (suppose ¬ odd (n*m), assert even (n*m), from even_of_not_odd this, absurd `even (n * m + n)` (not_even_of_odd (odd_add_of_even_of_odd this `odd n`))) lemma even_of_even_mul_self {n} : even (n * n) → even n := suppose even (n * n), by_contradiction (suppose odd n, have odd (n * n), from odd_mul_of_odd_of_odd this this, show false, from this `even (n * n)`) lemma odd_of_odd_mul_self {n} : odd (n * n) → odd n := suppose odd (n * n), suppose even n, have even (n * n), from !even_mul_of_even_left this, show false, from `odd (n * n)` this lemma odd_pow {n m} (h : odd n) : odd (n^m) := nat.induction_on m (show odd (n^0), from dec_trivial) (take m, suppose odd (n^m), show odd (n^(m+1)), from odd_mul_of_odd_of_odd h this) lemma even_pow {n m} (mpos : m > 0) (h : even n) : even (n^m) := have h₁ : ∀ m, even (n^succ m), from take m, nat.induction_on m (show even (n^1), by rewrite pow_one; apply h) (take m, suppose even (n^succ m), show even (n^(succ (succ m))), from !even_mul_of_even_left h), obtain m' (h₂ : m = succ m'), from exists_eq_succ_of_pos mpos, show even (n^m), by rewrite h₂; apply h₁ lemma odd_of_odd_pow {n m} (mpos : m > 0) (h : odd (n^m)) : odd n := suppose even n, have even (n^m), from even_pow mpos this, show false, from `odd (n^m)` this lemma even_of_even_pow {n m} (h : even (n^m)) : even n := by_contradiction (suppose odd n, have odd (n^m), from odd_pow this, show false, from this `even (n^m)`) lemma eq_of_div2_of_even {n m : nat} : n / 2 = m / 2 → (even n ↔ even m) → n = m := assume h₁ h₂, or.elim (em (even n)) (suppose even n, or.elim (em (even m)) (suppose even m, obtain w₁ (hw₁ : n = 2*w₁), from exists_of_even `even n`, obtain w₂ (hw₂ : m = 2*w₂), from exists_of_even `even m`, begin substvars, rewrite [mul.comm 2 w₁ at h₁, mul.comm 2 w₂ at h₁, *nat.mul_div_cancel _ (dec_trivial : 2 > 0) at h₁, h₁] end) (suppose odd m, absurd `odd m` (not_odd_of_even (iff.mp h₂ `even n`)))) (suppose odd n, or.elim (em (even m)) (suppose even m, absurd `odd n` (not_odd_of_even (iff.mpr h₂ `even m`))) (suppose odd m, assert d : 1 / 2 = (0:nat), from dec_trivial, obtain w₁ (hw₁ : n = 2*w₁ + 1), from exists_of_odd `odd n`, obtain w₂ (hw₂ : m = 2*w₂ + 1), from exists_of_odd `odd m`, begin substvars, rewrite [add.comm at h₁, add_mul_div_self_left _ _ (dec_trivial : 2 > 0) at h₁, d at h₁, zero_add at h₁], rewrite [add.comm at h₁, add_mul_div_self_left _ _ (dec_trivial : 2 > 0) at h₁, d at h₁, zero_add at h₁], rewrite h₁ end)) end nat
47588153f4e5b046fd305785e09ba7d9266c5b58
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/field_theory/finiteness.lean
e07eeebdd51fbea9da08304b924fa02193d1e717
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,172
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.finiteness import linear_algebra.dimension /-! # A module over a division ring is noetherian if and only if it is finite. -/ universes u v open_locale classical open cardinal submodule module function namespace is_noetherian variables {K : Type u} {V : Type v} [division_ring K] [add_comm_group V] [module K V] -- PROJECT: Show all division rings are noetherian. -- This is currently annoying because we only have ideal of commutative rings. variables [is_noetherian_ring K] /-- A module over a division ring is noetherian if and only if its dimension (as a cardinal) is strictly less than the first infinite cardinal `omega`. -/ lemma iff_dim_lt_omega : is_noetherian K V ↔ module.rank K V < omega.{v} := begin let b := basis.of_vector_space K V, rw [← b.mk_eq_dim'', lt_omega_iff_finite], split, { introI, exact finite_of_linear_independent (basis.of_vector_space_index.linear_independent K V) }, { assume hbfinite, refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _ _ _ _ _ (linear_equiv.of_top _ rfl) (id _), refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩, rw [set.finite.coe_to_finset, ← b.span_eq, basis.coe_of_vector_space, subtype.range_coe] } end variables (K V) /-- The dimension of a noetherian module over a division ring, as a cardinal, is strictly less than the first infinite cardinal `omega`. -/ lemma dim_lt_omega : ∀ [is_noetherian K V], module.rank K V < omega.{v} := is_noetherian.iff_dim_lt_omega.1 variables {K V} /-- In a noetherian module over a division ring, all bases are indexed by a finite type. -/ noncomputable def fintype_basis_index {ι : Type*} [is_noetherian K V] (b : basis ι K V) : fintype ι := b.fintype_index_of_dim_lt_omega (dim_lt_omega K V) /-- In a noetherian module over a division ring, `basis.of_vector_space` is indexed by a finite type. -/ noncomputable instance [is_noetherian K V] : fintype (basis.of_vector_space_index K V) := fintype_basis_index (basis.of_vector_space K V) /-- In a noetherian module over a division ring, if a basis is indexed by a set, that set is finite. -/ lemma finite_basis_index {ι : Type*} {s : set ι} [is_noetherian K V] (b : basis s K V) : s.finite := b.finite_index_of_dim_lt_omega (dim_lt_omega K V) variables (K V) /-- In a noetherian module over a division ring, there exists a finite basis. This is the indexing `finset`. -/ noncomputable def finset_basis_index [is_noetherian K V] : finset V := (finite_basis_index (basis.of_vector_space K V)).to_finset @[simp] lemma coe_finset_basis_index [is_noetherian K V] : (↑(finset_basis_index K V) : set V) = basis.of_vector_space_index K V := set.finite.coe_to_finset _ @[simp] lemma coe_sort_finset_basis_index [is_noetherian K V] : ((finset_basis_index K V) : Type*) = basis.of_vector_space_index K V := set.finite.coe_sort_to_finset _ /-- In a noetherian module over a division ring, there exists a finite basis. This is indexed by the `finset` `finite_dimensional.finset_basis_index`. This is in contrast to the result `finite_basis_index (basis.of_vector_space K V)`, which provides a set and a `set.finite`. -/ noncomputable def finset_basis [is_noetherian K V] : basis (finset_basis_index K V) K V := (basis.of_vector_space K V).reindex (by simp) @[simp] lemma range_finset_basis [is_noetherian K V] : set.range (finset_basis K V) = basis.of_vector_space_index K V := by rw [finset_basis, basis.range_reindex, basis.range_of_vector_space] variables {K V} /-- A module over a division ring is noetherian if and only if it is finitely generated. -/ lemma iff_fg : is_noetherian K V ↔ module.finite K V := begin split, { introI h, exact ⟨⟨finset_basis_index K V, by { convert (finset_basis K V).span_eq, simp }⟩⟩ }, { rintros ⟨s, hs⟩, rw [is_noetherian.iff_dim_lt_omega, ← dim_top, ← hs], exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) } end end is_noetherian
7246aca1c09d9d73bfc6f54d013ae719170c0267
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/factorization.lean
46228f6d72a36eb8fe10c36291a44957cbf35f5e
[ "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
11,533
lean
/- Copyright (c) 2021 Stuart Presnell. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stuart Presnell -/ import data.nat.prime import data.nat.mul_ind /-! # Prime factorizations `n.factorization` is the finitely supported function `ℕ →₀ ℕ` mapping each prime factor of `n` to its multiplicity in `n`. For example, since 2000 = 2^4 * 5^3, * `factorization 2000 2` is 4 * `factorization 2000 5` is 3 * `factorization 2000 k` is 0 for all other `k : ℕ`. ## TODO * As discussed in this Zulip thread: https://leanprover.zulipchat.com/#narrow/stream/217875/topic/Multiplicity.20in.20the.20naturals We have lots of disparate ways of talking about the multiplicity of a prime in a natural number, including `factors.count`, `padic_val_nat`, `multiplicity`, and the material in `data/pnat/factors`. Move some of this material to this file, prove results about the relationships between these definitions, and (where appropriate) choose a uniform canonical way of expressing these ideas. * Moreover, the results here should be generalised to an arbitrary unique factorization monoid with a normalization function, and then deduplicated. The basics of this have been started in `ring_theory/unique_factorization_domain`. -/ open nat finset list finsupp open_locale big_operators namespace nat /-- `n.factorization` is the finitely supported function `ℕ →₀ ℕ` mapping each prime factor of `n` to its multiplicity in `n`. -/ noncomputable def factorization (n : ℕ) : ℕ →₀ ℕ := (n.factors : multiset ℕ).to_finsupp @[simp] lemma factorization_prod_pow_eq_self {n : ℕ} (hn : n ≠ 0) : n.factorization.prod pow = n := begin simp only [←prod_to_multiset, factorization, multiset.coe_prod, multiset.to_finsupp_to_multiset], exact prod_factors hn.bot_lt, end lemma factorization_eq_count {n p : ℕ} : n.factorization p = n.factors.count p := by simp [factorization] -- TODO: As part of the unification mentioned in the TODO above, -- consider making this a [simp] lemma from `n.factors.count` to `n.factorization` /-- Every nonzero natural number has a unique prime factorization -/ lemma factorization_inj : set.inj_on factorization { x : ℕ | x ≠ 0 } := λ a ha b hb h, eq_of_count_factors_eq (zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb) (λ p, by simp [←factorization_eq_count, h]) @[simp] lemma factorization_zero : factorization 0 = 0 := by simp [factorization] @[simp] lemma factorization_one : factorization 1 = 0 := by simp [factorization] /-- The support of `n.factorization` is exactly `n.factors.to_finset` -/ @[simp] lemma support_factorization {n : ℕ} : n.factorization.support = n.factors.to_finset := by simpa [factorization, multiset.to_finsupp_support] lemma factor_iff_mem_factorization {n p : ℕ} : p ∈ n.factorization.support ↔ p ∈ n.factors := by simp only [support_factorization, list.mem_to_finset] lemma prime_of_mem_factorization {n p : ℕ} : p ∈ n.factorization.support → p.prime := (@prime_of_mem_factors n p) ∘ (@factor_iff_mem_factorization n p).mp lemma pos_of_mem_factorization {n p : ℕ} : p ∈ n.factorization.support → 0 < p := (@prime.pos p) ∘ (@prime_of_mem_factorization n p) lemma factorization_eq_zero_of_non_prime (n p : ℕ) (hp : ¬p.prime) : n.factorization p = 0 := not_mem_support_iff.1 (mt prime_of_mem_factorization hp) /-- The only numbers with empty prime factorization are `0` and `1` -/ lemma factorization_eq_zero_iff (n : ℕ) : n.factorization = 0 ↔ n = 0 ∨ n = 1 := by simp [factorization, add_equiv.map_eq_zero_iff, multiset.coe_eq_zero] /-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/ @[simp] lemma factorization_mul {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) : (a * b).factorization = a.factorization + b.factorization := by { ext p, simp only [add_apply, factorization_eq_count, count_factors_mul_of_pos (zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb)] } /-- For any `p`, the power of `p` in `n^k` is `k` times the power in `n` -/ lemma factorization_pow {n k : ℕ} : factorization (n^k) = k • n.factorization := by { ext p, simp [factorization_eq_count, factors_count_pow] } /-- The only prime factor of prime `p` is `p` itself, with multiplicity `1` -/ @[simp] lemma prime.factorization {p : ℕ} (hp : prime p) : p.factorization = single p 1 := begin ext q, rw [factorization_eq_count, factors_prime hp, single_apply, count_singleton', if_congr eq_comm]; refl, end /-- For prime `p` the only prime factor of `p^k` is `p` with multiplicity `k` -/ @[simp] lemma prime.factorization_pow {p k : ℕ} (hp : prime p) : factorization (p^k) = single p k := by simp [factorization_pow, hp.factorization] /-- For any `p : ℕ` and any function `g : α → ℕ` that's non-zero on `S : finset α`, the power of `p` in `S.prod g` equals the sum over `x ∈ S` of the powers of `p` in `g x`. Generalises `factorization_mul`, which is the special case where `S.card = 2` and `g = id`. -/ lemma factorization_prod {α : Type*} {S : finset α} {g : α → ℕ} (hS : ∀ x ∈ S, g x ≠ 0) : (S.prod g).factorization = S.sum (λ x, (g x).factorization) := begin classical, ext p, apply finset.induction_on' S, { simp }, { intros x T hxS hTS hxT IH, have hT : T.prod g ≠ 0 := prod_ne_zero_iff.mpr (λ x hx, hS x (hTS hx)), simp [prod_insert hxT, sum_insert hxT, ←IH, factorization_mul (hS x hxS) hT] } end /-! ### Factorizations of pairs of coprime numbers -/ /-- The prime factorizations of coprime `a` and `b` are disjoint -/ lemma factorization_disjoint_of_coprime {a b : ℕ} (hab : coprime a b) : disjoint a.factorization.support b.factorization.support := by simpa only [support_factorization] using disjoint_to_finset_iff_disjoint.mpr (coprime_factors_disjoint hab) /-- For coprime `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/ lemma factorization_mul_of_coprime {a b : ℕ} (hab : coprime a b) : (a * b).factorization = a.factorization + b.factorization := begin ext q, simp only [finsupp.coe_add, add_apply, factorization_eq_count, count_factors_mul_of_coprime hab], end /-- For coprime `a` and `b` the prime factorization `a * b` is the union of those of `a` and `b` -/ lemma factorization_mul_support_of_coprime {a b : ℕ} (hab : coprime a b) : (a * b).factorization.support = a.factorization.support ∪ b.factorization.support := begin rw factorization_mul_of_coprime hab, exact support_add_eq (factorization_disjoint_of_coprime hab), end lemma factorization_mul_support {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) : (a * b).factorization.support = a.factorization.support ∪ b.factorization.support := begin ext q, simp only [finset.mem_union, factor_iff_mem_factorization], rw mem_factors_mul ha hb, end /-- For any multiplicative function `f` with `f 1 = 1` and any `n > 0`, we can evaluate `f n` by evaluating `f` at `p ^ k` over the factorization of `n` -/ lemma multiplicative_factorization {β : Type*} [comm_monoid β] (f : ℕ → β) (h_mult : ∀ x y : ℕ, coprime x y → f (x * y) = f x * f y) (hf : f 1 = 1) : ∀ {n : ℕ}, n ≠ 0 → f n = n.factorization.prod (λ p k, f (p ^ k)) := begin apply' nat.rec_on_pos_prime_coprime, { intros p k hp hk hpk, simp [prime.factorization_pow hp, finsupp.prod_single_index _, hf] }, { simp }, { rintros -, rw [factorization_one, hf], simp }, { intros a b _ _ hab ha hb hab_pos, rw [h_mult a b hab, ha (left_ne_zero_of_mul hab_pos), hb (right_ne_zero_of_mul hab_pos), factorization_mul_of_coprime hab, ←prod_add_index_of_disjoint], convert (factorization_disjoint_of_coprime hab) }, end /-- For any multiplicative function `f` with `f 1 = 1` and `f 0 = 1`, we can evaluate `f n` by evaluating `f` at `p ^ k` over the factorization of `n` -/ lemma multiplicative_factorization' {β : Type*} [comm_monoid β] (f : ℕ → β) (h_mult : ∀ x y : ℕ, coprime x y → f (x * y) = f x * f y) (hf0 : f 0 = 1) (hf1 : f 1 = 1) : ∀ {n : ℕ}, f n = n.factorization.prod (λ p k, f (p ^ k)) := begin apply' nat.rec_on_pos_prime_coprime, { intros p k hp hk, simp only [hp.factorization_pow], rw prod_single_index _, simp [hf1] }, { simp [hf0] }, { rw [factorization_one, hf1], simp }, { intros a b _ _ hab ha hb, rw [h_mult a b hab, ha, hb, factorization_mul_of_coprime hab, ←prod_add_index_of_disjoint], convert (factorization_disjoint_of_coprime hab) }, end /-! ### Factorization and divisibility -/ lemma factorization_le_iff_dvd {d n : ℕ} (hd : d ≠ 0) (hn : n ≠ 0) : d.factorization ≤ n.factorization ↔ d ∣ n := begin split, { intro hdn, set K := n.factorization - d.factorization with hK, use K.prod pow, rw [←factorization_prod_pow_eq_self hn, ←factorization_prod_pow_eq_self hd, ←finsupp.prod_add_index pow_zero pow_add, hK, add_tsub_cancel_of_le hdn] }, { rintro ⟨c, rfl⟩, rw factorization_mul hd (right_ne_zero_of_mul hn), simp }, end lemma prime_pow_dvd_iff_le_factorization (p k n : ℕ) (pp : prime p) (hn : n ≠ 0) : p ^ k ∣ n ↔ k ≤ n.factorization p := by rw [←factorization_le_iff_dvd (pow_pos pp.pos k).ne' hn, pp.factorization_pow, single_le_iff] lemma exists_factorization_lt_of_lt {a b : ℕ} (ha : a ≠ 0) (hab : a < b) : ∃ p : ℕ, a.factorization p < b.factorization p := begin have hb : b ≠ 0 := (ha.bot_lt.trans hab).ne', contrapose! hab, rw [←finsupp.le_def, factorization_le_iff_dvd hb ha] at hab, exact le_of_dvd ha.bot_lt hab, end @[simp] lemma div_factorization_eq_tsub_of_dvd {d n : ℕ} (hn : n ≠ 0) (h : d ∣ n) : (n / d).factorization = n.factorization - d.factorization := begin have hd : d ≠ 0 := ne_zero_of_dvd_ne_zero hn h, cases dvd_iff_exists_eq_mul_left.mp h with c hc, have hc_pos : c ≠ 0, { subst hc, exact left_ne_zero_of_mul hn }, rw [hc, nat.mul_div_cancel c hd.bot_lt, factorization_mul hc_pos hd, add_tsub_cancel_right], end lemma dvd_iff_div_factorization_eq_tsub (d n : ℕ) (hd : d ≠ 0) (hdn : d ≤ n) : d ∣ n ↔ (n / d).factorization = n.factorization - d.factorization := begin have hn : n ≠ 0 := (lt_of_lt_of_le hd.bot_lt hdn).ne.symm, refine ⟨div_factorization_eq_tsub_of_dvd hn, _⟩, { rcases eq_or_lt_of_le hdn with rfl | hd_lt_n, { simp }, have h1 : n / d ≠ 0 := λ H, nat.lt_asymm hd_lt_n ((nat.div_eq_zero_iff hd.bot_lt).mp H), intros h, rw dvd_iff_le_div_mul n d, by_contra h2, cases (exists_factorization_lt_of_lt (mul_ne_zero h1 hd) (not_le.mp h2)) with p hp, rwa [factorization_mul h1 hd, add_apply, ←lt_tsub_iff_right, h, tsub_apply, lt_self_iff_false] at hp }, end lemma pow_factorization_dvd (p d : ℕ) : p ^ d.factorization p ∣ d := begin rcases eq_or_ne d 0 with rfl | hd, { simp }, by_cases pp : prime p, { rw prime_pow_dvd_iff_le_factorization p _ d pp hd }, { rw factorization_eq_zero_of_non_prime d p pp, simp }, end lemma dvd_iff_prime_pow_dvd_dvd {n d : ℕ} (hd : d ≠ 0) (hn : n ≠ 0) : d ∣ n ↔ ∀ p k : ℕ, prime p → p^k ∣ d → p^k ∣ n := begin split, { exact λ h p k pp hpkd, dvd_trans hpkd h }, { intros h, rw [←factorization_le_iff_dvd hd hn, finsupp.le_def], intros p, by_cases pp : prime p, swap, { rw factorization_eq_zero_of_non_prime d p pp, exact zero_le' }, rw ←prime_pow_dvd_iff_le_factorization p _ n pp hn, exact h p _ pp (pow_factorization_dvd p _) }, end end nat
9a33897034981cd4f5bd9f21b5ea39f180de9ef3
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/rquote.lean
813d20fcdc85c6a1d59b3d0d4aa7db9890f1db70
[ "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
383
lean
-- constant nat.gcd : nat → nat → nat namespace foo constant f : nat → nat constant g : nat → nat end foo namespace boo constant f : nat → nat end boo open foo boo #check ``f #check ``g open nat #check ``has_add.add #check ``gcd #check `f #check `foo.f namespace bla section parameter A : Type definition ID : A → A := λ x, x #check ``ID end end bla
bc9bee58ee3987afc11bb28e0f24f083cfeb5fa1
94e33a31faa76775069b071adea97e86e218a8ee
/src/model_theory/encoding.lean
41b3d2bf7835fc37366d1ecea09fb79525ebafaf
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
14,211
lean
/- Copyright (c) 2022 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import computability.encoding import model_theory.syntax import set_theory.cardinal.ordinal /-! # Encodings and Cardinality of First-Order Syntax ## Main Definitions * `first_order.language.term.encoding` encodes terms as lists. * `first_order.language.bounded_formula.encoding` encodes bounded formulas as lists. ## Main Results * `first_order.language.term.card_le` shows that the number of terms in `L.term α` is at most `max ℵ₀ # (α ⊕ Σ i, L.functions i)`. * `first_order.language.bounded_formula.card_le` shows that the number of bounded formulas in `Σ n, L.bounded_formula α n` is at most `max ℵ₀ (cardinal.lift.{max u v} (#α) + cardinal.lift.{u'} L.card)`. ## TODO * `primcodable` instances for terms and formulas, based on the `encoding`s * Computability facts about term and formula operations, to set up a computability approach to incompleteness -/ universes u v w u' v' namespace first_order namespace language variables {L : language.{u v}} variables {M : Type w} {N P : Type*} [L.Structure M] [L.Structure N] [L.Structure P] variables {α : Type u'} {β : Type v'} open_locale first_order cardinal open computability list Structure cardinal fin namespace term /-- Encodes a term as a list of variables and function symbols. -/ def list_encode : L.term α → list (α ⊕ Σ i, L.functions i) | (var i) := [sum.inl i] | (func f ts) := ((sum.inr (⟨_, f⟩ : Σ i, L.functions i)) :: ((list.fin_range _).bind (λ i, (ts i).list_encode))) /-- Decodes a list of variables and function symbols as a list of terms. -/ def list_decode : list (α ⊕ Σ i, L.functions i) → list (option (L.term α)) | [] := [] | ((sum.inl a) :: l) := some (var a) :: list_decode l | ((sum.inr ⟨n, f⟩) :: l) := if h : ∀ (i : fin n), ((list_decode l).nth i).join.is_some then func f (λ i, option.get (h i)) :: ((list_decode l).drop n) else [none] theorem list_decode_encode_list (l : list (L.term α)) : list_decode (l.bind list_encode) = l.map option.some := begin suffices h : ∀ (t : L.term α) (l : list (α ⊕ Σ i, L.functions i)), list_decode (t.list_encode ++ l) = some t :: list_decode l, { induction l with t l lih, { refl }, { rw [cons_bind, h t (l.bind list_encode), lih, list.map] } }, { intro t, induction t with a n f ts ih; intro l, { rw [list_encode, singleton_append, list_decode] }, { rw [list_encode, cons_append, list_decode], have h : list_decode ((fin_range n).bind (λ (i : fin n), (ts i).list_encode) ++ l) = (fin_range n).map (option.some ∘ ts) ++ list_decode l, { induction (fin_range n) with i l' l'ih, { refl }, { rw [cons_bind, append_assoc, ih, map_cons, l'ih, cons_append] } }, have h' : ∀ i, (list_decode ((fin_range n).bind (λ (i : fin n), (ts i).list_encode) ++ l)).nth ↑i = some (some (ts i)), { intro i, rw [h, nth_append, nth_map], { simp only [option.map_eq_some', function.comp_app, nth_eq_some], refine ⟨i, ⟨lt_of_lt_of_le i.2 (ge_of_eq (length_fin_range _)), _⟩, rfl⟩, rw [nth_le_fin_range, fin.eta] }, { refine lt_of_lt_of_le i.2 _, simp } }, refine (dif_pos (λ i, option.is_some_iff_exists.2 ⟨ts i, _⟩)).trans _, { rw [option.join_eq_some, h'] }, refine congr (congr rfl (congr rfl (congr rfl (funext (λ i, option.get_of_mem _ _))))) _, { simp [h'] }, { rw [h, drop_left'], rw [length_map, length_fin_range] } } } end /-- An encoding of terms as lists. -/ @[simps] protected def encoding : encoding (L.term α) := { Γ := α ⊕ Σ i, L.functions i, encode := list_encode, decode := λ l, (list_decode l).head'.join, decode_encode := λ t, begin have h := list_decode_encode_list [t], rw [bind_singleton] at h, simp only [h, option.join, head', list.map, option.some_bind, id.def], end } lemma list_encode_injective : function.injective (list_encode : L.term α → list (α ⊕ Σ i, L.functions i)) := term.encoding.encode_injective theorem card_le : # (L.term α) ≤ max ℵ₀ (# (α ⊕ Σ i, L.functions i)) := lift_le.1 (trans term.encoding.card_le_card_list (lift_le.2 (mk_list_le_max _))) theorem card_sigma : # (Σ n, (L.term (α ⊕ fin n))) = max ℵ₀ (# (α ⊕ Σ i, L.functions i)) := begin refine le_antisymm _ _, { rw mk_sigma, refine (sum_le_supr_lift _).trans _, rw [mk_nat, lift_aleph_0, mul_eq_max_of_aleph_0_le_left le_rfl, max_le_iff, csupr_le_iff' (bdd_above_range _)], { refine ⟨le_max_left _ _, λ i, card_le.trans _⟩, rw max_le_iff, refine ⟨le_max_left _ _, _⟩, rw [← add_eq_max le_rfl, mk_sum, mk_sum, mk_sum, add_comm (cardinal.lift (#α)), lift_add, add_assoc, lift_lift, lift_lift], refine add_le_add_right _ _, rw [lift_le_aleph_0, ← encodable_iff], exact ⟨infer_instance⟩ }, { rw [← one_le_iff_ne_zero], refine trans _ (le_csupr (bdd_above_range _) 1), rw [one_le_iff_ne_zero, mk_ne_zero_iff], exact ⟨var (sum.inr 0)⟩ } }, { rw [max_le_iff, ← infinite_iff], refine ⟨infinite.of_injective (λ i, ⟨i + 1, var (sum.inr i)⟩) (λ i j ij, _), _⟩, { cases ij, refl }, { rw [cardinal.le_def], refine ⟨⟨sum.elim (λ i, ⟨0, var (sum.inl i)⟩) (λ F, ⟨1, func F.2 (λ _, var (sum.inr 0))⟩), _⟩⟩, { rintros (a | a) (b | b) h, { simp only [sum.elim_inl, eq_self_iff_true, heq_iff_eq, true_and] at h, rw h }, { simp only [sum.elim_inl, sum.elim_inr, nat.zero_ne_one, false_and] at h, exact h.elim }, { simp only [sum.elim_inr, sum.elim_inl, nat.one_ne_zero, false_and] at h, exact h.elim }, { simp only [sum.elim_inr, eq_self_iff_true, heq_iff_eq, true_and] at h, rw sigma.ext_iff.2 ⟨h.1, h.2.1⟩, } } } } end instance [encodable α] [encodable ((Σ i, L.functions i))] : encodable (L.term α) := encodable.of_left_injection list_encode (λ l, (list_decode l).head'.join) (λ t, begin rw [← bind_singleton list_encode, list_decode_encode_list], simp only [option.join, head', list.map, option.some_bind, id.def], end) lemma card_le_aleph_0 [h1 : nonempty (encodable α)] [h2 : L.countable_functions] : # (L.term α) ≤ ℵ₀ := begin refine (card_le.trans _), rw [max_le_iff], simp only [le_refl, mk_sum, add_le_aleph_0, lift_le_aleph_0, true_and], exact ⟨encodable_iff.1 h1, L.card_functions_le_aleph_0⟩, end instance small [small.{u} α] : small.{u} (L.term α) := small_of_injective list_encode_injective end term namespace bounded_formula /-- Encodes a bounded formula as a list of symbols. -/ def list_encode : ∀ {n : ℕ}, L.bounded_formula α n → list ((Σ k, L.term (α ⊕ fin k)) ⊕ (Σ n, L.relations n) ⊕ ℕ) | n falsum := [sum.inr (sum.inr (n + 2))] | n (equal t₁ t₂) := [sum.inl ⟨_, t₁⟩, sum.inl ⟨_, t₂⟩] | n (rel R ts) := [sum.inr (sum.inl ⟨_, R⟩), sum.inr (sum.inr n)] ++ ((list.fin_range _).map (λ i, sum.inl ⟨n, (ts i)⟩)) | n (imp φ₁ φ₂) := (sum.inr (sum.inr 0)) :: φ₁.list_encode ++ φ₂.list_encode | n (all φ) := (sum.inr (sum.inr 1)) :: φ.list_encode /-- Applies the `forall` quantifier to an element of `(Σ n, L.bounded_formula α n)`, or returns `default` if not possible. -/ def sigma_all : (Σ n, L.bounded_formula α n) → Σ n, L.bounded_formula α n | ⟨(n + 1), φ⟩ := ⟨n, φ.all⟩ | _ := default /-- Applies `imp` to two elements of `(Σ n, L.bounded_formula α n)`, or returns `default` if not possible. -/ def sigma_imp : (Σ n, L.bounded_formula α n) → (Σ n, L.bounded_formula α n) → (Σ n, L.bounded_formula α n) | ⟨m, φ⟩ ⟨n, ψ⟩ := if h : m = n then ⟨m, φ.imp (eq.mp (by rw h) ψ)⟩ else default /-- Decodes a list of symbols as a list of formulas. -/ @[simp] def list_decode : Π (l : list ((Σ k, L.term (α ⊕ fin k)) ⊕ (Σ n, L.relations n) ⊕ ℕ)), (Σ n, L.bounded_formula α n) × { l' : list ((Σ k, L.term (α ⊕ fin k)) ⊕ (Σ n, L.relations n) ⊕ ℕ) // l'.sizeof ≤ max 1 l.sizeof } | ((sum.inr (sum.inr (n + 2))) :: l) := ⟨⟨n, falsum⟩, l, le_max_of_le_right le_add_self⟩ | ((sum.inl ⟨n₁, t₁⟩) :: sum.inl ⟨n₂, t₂⟩ :: l) := ⟨if h : n₁ = n₂ then ⟨n₁, equal t₁ (eq.mp (by rw h) t₂)⟩ else default, l, begin simp only [list.sizeof, ← add_assoc], exact le_max_of_le_right le_add_self, end⟩ | (sum.inr (sum.inl ⟨n, R⟩) :: (sum.inr (sum.inr k)) :: l) := ⟨ if h : ∀ (i : fin n), ((l.map sum.get_left).nth i).join.is_some then if h' : ∀ i, (option.get (h i)).1 = k then ⟨k, bounded_formula.rel R (λ i, eq.mp (by rw h' i) (option.get (h i)).2)⟩ else default else default, l.drop n, le_max_of_le_right (le_add_left (le_add_left (list.drop_sizeof_le _ _)))⟩ | ((sum.inr (sum.inr 0)) :: l) := have (↑((list_decode l).2) : list ((Σ k, L.term (α ⊕ fin k)) ⊕ (Σ n, L.relations n) ⊕ ℕ)).sizeof < 1 + (1 + 1) + l.sizeof, from begin refine lt_of_le_of_lt (list_decode l).2.2 (max_lt _ (nat.lt_add_of_pos_left dec_trivial)), rw [add_assoc, add_comm, nat.lt_succ_iff, add_assoc], exact le_self_add, end, ⟨sigma_imp (list_decode l).1 (list_decode (list_decode l).2).1, (list_decode (list_decode l).2).2, le_max_of_le_right (trans (list_decode _).2.2 (max_le (le_add_right le_self_add) (trans (list_decode _).2.2 (max_le (le_add_right le_self_add) le_add_self))))⟩ | ((sum.inr (sum.inr 1)) :: l) := ⟨sigma_all (list_decode l).1, (list_decode l).2, (list_decode l).2.2.trans (max_le_max le_rfl le_add_self)⟩ | _ := ⟨default, [], le_max_left _ _⟩ @[simp] theorem list_decode_encode_list (l : list (Σ n, L.bounded_formula α n)) : (list_decode (l.bind (λ φ, φ.2.list_encode))).1 = l.head := begin suffices h : ∀ (φ : (Σ n, L.bounded_formula α n)) l, (list_decode (list_encode φ.2 ++ l)).1 = φ ∧ (list_decode (list_encode φ.2 ++ l)).2.1 = l, { induction l with φ l lih, { rw [list.nil_bind], simp [list_decode], }, { rw [cons_bind, (h φ _).1, head_cons] } }, { rintro ⟨n, φ⟩, induction φ with _ _ _ _ _ _ _ ts _ _ _ ih1 ih2 _ _ ih; intro l, { rw [list_encode, singleton_append, list_decode], simp only [eq_self_iff_true, heq_iff_eq, and_self], }, { rw [list_encode, cons_append, cons_append, list_decode, dif_pos], { simp only [eq_mp_eq_cast, cast_eq, eq_self_iff_true, heq_iff_eq, and_self, nil_append], }, { simp only [eq_self_iff_true, heq_iff_eq, and_self], } }, { rw [list_encode, cons_append, cons_append, singleton_append, cons_append, list_decode], { have h : ∀ (i : fin φ_l), ((list.map sum.get_left (list.map (λ (i : fin φ_l), sum.inl (⟨(⟨φ_n, rel φ_R ts⟩ : Σ n, L.bounded_formula α n).fst, ts i⟩ : Σ n, L.term (α ⊕ fin n))) (fin_range φ_l) ++ l)).nth ↑i).join = some ⟨_, ts i⟩, { intro i, simp only [option.join, map_append, map_map, option.bind_eq_some, id.def, exists_eq_right, nth_eq_some, length_append, length_map, length_fin_range], refine ⟨lt_of_lt_of_le i.2 le_self_add, _⟩, rw [nth_le_append, nth_le_map], { simp only [sum.get_left, nth_le_fin_range, fin.eta, function.comp_app, eq_self_iff_true, heq_iff_eq, and_self] }, { exact lt_of_lt_of_le i.is_lt (ge_of_eq (length_fin_range _)) }, { rw [length_map, length_fin_range], exact i.2 } }, rw dif_pos, swap, { exact λ i, option.is_some_iff_exists.2 ⟨⟨_, ts i⟩, h i⟩ }, rw dif_pos, swap, { intro i, obtain ⟨h1, h2⟩ := option.eq_some_iff_get_eq.1 (h i), rw h2 }, simp only [eq_self_iff_true, heq_iff_eq, true_and], refine ⟨funext (λ i, _), _⟩, { obtain ⟨h1, h2⟩ := option.eq_some_iff_get_eq.1 (h i), rw [eq_mp_eq_cast, cast_eq_iff_heq], exact (sigma.ext_iff.1 ((sigma.eta (option.get h1)).trans h2)).2 }, rw [list.drop_append_eq_append_drop, length_map, length_fin_range, nat.sub_self, drop, drop_eq_nil_of_le, nil_append], rw [length_map, length_fin_range], }, }, { rw [list_encode, append_assoc, cons_append, list_decode], simp only [subtype.val_eq_coe] at *, rw [(ih1 _).1, (ih1 _).2, (ih2 _).1, (ih2 _).2, sigma_imp, dif_pos rfl], exact ⟨rfl, rfl⟩, }, { rw [list_encode, cons_append, list_decode], simp only, simp only [subtype.val_eq_coe] at *, rw [(ih _).1, (ih _).2, sigma_all], exact ⟨rfl, rfl⟩ } } end /-- An encoding of bounded formulas as lists. -/ @[simps] protected def encoding : encoding (Σ n, L.bounded_formula α n) := { Γ := (Σ k, L.term (α ⊕ fin k)) ⊕ (Σ n, L.relations n) ⊕ ℕ, encode := λ φ, φ.2.list_encode, decode := λ l, (list_decode l).1, decode_encode := λ φ, begin have h := list_decode_encode_list [φ], rw [bind_singleton] at h, rw h, refl, end } lemma list_encode_sigma_injective : function.injective (λ (φ : Σ n, L.bounded_formula α n), φ.2.list_encode) := bounded_formula.encoding.encode_injective theorem card_le : # (Σ n, L.bounded_formula α n) ≤ max ℵ₀ (cardinal.lift.{max u v} (#α) + cardinal.lift.{u'} L.card) := begin refine lift_le.1 ((bounded_formula.encoding.card_le_card_list).trans _), rw [encoding_Γ, mk_list_eq_max_mk_aleph_0, lift_max, lift_aleph_0, lift_max, lift_aleph_0, max_le_iff], refine ⟨_, le_max_left _ _⟩, rw [mk_sum, term.card_sigma, mk_sum, ← add_eq_max le_rfl, mk_sum, mk_nat], simp only [lift_add, lift_lift, lift_aleph_0], rw [← add_assoc, add_comm, ← add_assoc, ← add_assoc, aleph_0_add_aleph_0, add_assoc, add_eq_max le_rfl, add_assoc, card, symbols, mk_sum, lift_add, lift_lift, lift_lift], end end bounded_formula end language end first_order
55400f3f4761e9b9baea27c820446e464a71f681
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/old/expressions/measurementSystem.lean
75d4cb4417e47c7251d19cf2cd05e6831762a649
[]
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
716
lean
import ...phys.metrology.measurement namespace lang.measurementSystem open measurementSystem structure var : Type := mk :: (num : ℕ) structure measureVar extends var inductive measureExpr | lit (v : MeasurementSystem) | var (v : measureVar) abbreviation measureEnv := measureVar → MeasurementSystem abbreviation measureEval := measureExpr → measureEnv → MeasurementSystem structure env : Type := (ms : measureEnv) def measureVarEq : measureVar → measureVar → bool | v1 v2 := v1.num=v2.num --(fr : frameEnv sp) --(vec : vectorEnv sp) --(pt : pointEnv sp) noncomputable def init := λ v : measureVar, si_measurement_system noncomputable def initEnv : env := ⟨init⟩ end lang.measurementSystem
1b7cad255a4a7a12e94822ef5c372e8c20006c7e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1771.lean
3feb7c98c24b3826867e892a3e5eb49ea5b7b1fa
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
535
lean
inductive is_some {A : Type} (mx : option A) : Prop | mk : ∀ x : A, mx = some x → is_some lemma foo {A : Type} (x : A) (mx : option A) (H : mx = some x) : is_some mx := begin existsi x, assumption end set_option pp.all true set_option pp.beta false set_option pp.instantiate_mvars false -- same lemma as above lemma foo' {A : Type} (x : A) (mx : option A) (H : mx = some x) : is_some mx := begin apply (if true then _ else _), -- in this case, we branch first { existsi x, assumption }, { existsi x, assumption } end
e1c787b06d27c26fba60ea5fe6fab6f3304d201d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/preadditive/yoneda/projective.lean
0cc596a1ce183770609efbc3671634c8c50c2a04
[ "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,898
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import category_theory.preadditive.yoneda.basic import category_theory.preadditive.projective import algebra.category.Group.epi_mono import algebra.category.Module.epi_mono /-! > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. An object is projective iff the preadditive coyoneda functor on it preserves epimorphisms. -/ universes v u open opposite namespace category_theory variables {C : Type u} [category.{v} C] section preadditive variables [preadditive C] namespace projective lemma projective_iff_preserves_epimorphisms_preadditive_coyoneda_obj (P : C) : projective P ↔ (preadditive_coyoneda.obj (op P)).preserves_epimorphisms := begin rw projective_iff_preserves_epimorphisms_coyoneda_obj, refine ⟨λ (h : (preadditive_coyoneda.obj (op P) ⋙ (forget _)).preserves_epimorphisms), _, _⟩, { exactI functor.preserves_epimorphisms_of_preserves_of_reflects (preadditive_coyoneda.obj (op P)) (forget _) }, { introI, exact (infer_instance : (preadditive_coyoneda.obj (op P) ⋙ forget _).preserves_epimorphisms) } end lemma projective_iff_preserves_epimorphisms_preadditive_coyoneda_obj' (P : C) : projective P ↔ (preadditive_coyoneda_obj (op P)).preserves_epimorphisms := begin rw projective_iff_preserves_epimorphisms_coyoneda_obj, refine ⟨λ (h : (preadditive_coyoneda_obj (op P) ⋙ (forget _)).preserves_epimorphisms), _, _⟩, { exactI functor.preserves_epimorphisms_of_preserves_of_reflects (preadditive_coyoneda_obj (op P)) (forget _) }, { introI, exact (infer_instance : (preadditive_coyoneda_obj (op P) ⋙ forget _).preserves_epimorphisms) } end end projective end preadditive end category_theory
c4e980ad6628f950a32476b79ec1f7b428b2c82b
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/prod/decl.lean
74732b8f2938317bb2385e65fa91df285d4239ce
[ "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
504
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura, Jeremy Avigad import data.unit.decl logic.eq structure prod (A B : Type) := mk :: (pr1 : A) (pr2 : B) definition pair := @prod.mk namespace prod notation A × B := prod A B notation `pr₁` := pr1 notation `pr₂` := pr2 -- notation for n-ary tuples notation `(` h `,` t:(foldl `,` (e r, prod.mk r e) h) `)` := t end prod
23e5c8578fe663182e165ecbc498ea0a512737a6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/adjoin_root.lean
efb922d50237808584a4f8cbd9ec402da09bf2d7
[ "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
32,413
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes -/ import algebra.algebra.basic import data.polynomial.field_division import field_theory.minpoly.basic import ring_theory.adjoin.basic import ring_theory.finite_presentation import ring_theory.finite_type import ring_theory.power_basis import ring_theory.principal_ideal_domain import ring_theory.quotient_noetherian /-! # Adjoining roots of polynomials > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is irreducible, the field structure on `adjoin_root f` is constructed. We suggest stating results on `is_adjoin_root` instead of `adjoin_root` to achieve higher generality, since `is_adjoin_root` works for all different constructions of `R[α]` including `adjoin_root f = R[X]/(f)` itself. ## Main definitions and results The main definitions are in the `adjoin_root` namespace. * `mk f : R[X] →+* adjoin_root f`, the natural ring homomorphism. * `of f : R →+* adjoin_root f`, the natural ring homomorphism. * `root f : adjoin_root f`, the image of X in R[X]/(f). * `lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S`, the ring homomorphism from R[X]/(f) to S extending `i : R →+* S` and sending `X` to `x`. * `lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S`, the algebra homomorphism from R[X]/(f) to S extending `algebra_map R S` and sending `X` to `x` * `equiv : (adjoin_root f →ₐ[F] E) ≃ {x // x ∈ (f.map (algebra_map F E)).roots}` a bijection between algebra homomorphisms from `adjoin_root` and roots of `f` in `S` -/ noncomputable theory open_locale classical open_locale big_operators polynomial universes u v w variables {R : Type u} {S : Type v} {K : Type w} open polynomial ideal /-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring as the quotient of `R[X]` by the principal ideal generated by `f`. -/ def adjoin_root [comm_ring R] (f : R[X]) : Type u := polynomial R ⧸ (span {f} : ideal R[X]) namespace adjoin_root section comm_ring variables [comm_ring R] (f : R[X]) instance : comm_ring (adjoin_root f) := ideal.quotient.comm_ring _ instance : inhabited (adjoin_root f) := ⟨0⟩ instance : decidable_eq (adjoin_root f) := classical.dec_eq _ protected lemma nontrivial [is_domain R] (h : degree f ≠ 0) : nontrivial (adjoin_root f) := ideal.quotient.nontrivial begin simp_rw [ne.def, span_singleton_eq_top, polynomial.is_unit_iff, not_exists, not_and], rintro x hx rfl, exact h (degree_C hx.ne_zero), end /-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/ def mk : R[X] →+* adjoin_root f := ideal.quotient.mk _ @[elab_as_eliminator] theorem induction_on {C : adjoin_root f → Prop} (x : adjoin_root f) (ih : ∀ p : R[X], C (mk f p)) : C x := quotient.induction_on' x ih /-- Embedding of the original ring `R` into `adjoin_root f`. -/ def of : R →+* adjoin_root f := (mk f).comp C instance [distrib_smul S R] [is_scalar_tower S R R] : has_smul S (adjoin_root f) := submodule.quotient.has_smul' _ instance [distrib_smul S R] [is_scalar_tower S R R] : distrib_smul S (adjoin_root f) := submodule.quotient.distrib_smul' _ @[simp] lemma smul_mk [distrib_smul S R] [is_scalar_tower S R R] (a : S) (x : R[X]) : a • mk f x = mk f (a • x) := rfl lemma smul_of [distrib_smul S R] [is_scalar_tower S R R] (a : S) (x : R) : a • of f x = of f (a • x) := by rw [of, ring_hom.comp_apply, ring_hom.comp_apply, smul_mk, smul_C] instance (R₁ R₂ : Type*) [has_smul R₁ R₂] [distrib_smul R₁ R] [distrib_smul R₂ R] [is_scalar_tower R₁ R R] [is_scalar_tower R₂ R R] [is_scalar_tower R₁ R₂ R] (f : R[X]) : is_scalar_tower R₁ R₂ (adjoin_root f) := submodule.quotient.is_scalar_tower _ _ instance (R₁ R₂ : Type*) [distrib_smul R₁ R] [distrib_smul R₂ R] [is_scalar_tower R₁ R R] [is_scalar_tower R₂ R R] [smul_comm_class R₁ R₂ R] (f : R[X]) : smul_comm_class R₁ R₂ (adjoin_root f) := submodule.quotient.smul_comm_class _ _ instance is_scalar_tower_right [distrib_smul S R] [is_scalar_tower S R R] : is_scalar_tower S (adjoin_root f) (adjoin_root f) := ideal.quotient.is_scalar_tower_right instance [monoid S] [distrib_mul_action S R] [is_scalar_tower S R R] (f : R[X]) : distrib_mul_action S (adjoin_root f) := submodule.quotient.distrib_mul_action' _ instance [comm_semiring S] [algebra S R] : algebra S (adjoin_root f) := ideal.quotient.algebra S @[simp] lemma algebra_map_eq : algebra_map R (adjoin_root f) = of f := rfl variables (S) lemma algebra_map_eq' [comm_semiring S] [algebra S R] : algebra_map S (adjoin_root f) = (of f).comp (algebra_map S R) := rfl variables {S} lemma finite_type : algebra.finite_type R (adjoin_root f) := (algebra.finite_type.polynomial R).of_surjective _ (ideal.quotient.mkₐ_surjective R _) lemma finite_presentation : algebra.finite_presentation R (adjoin_root f) := (algebra.finite_presentation.polynomial R).quotient (submodule.fg_span_singleton f) /-- The adjoined root. -/ def root : adjoin_root f := mk f X variables {f} instance has_coe_t : has_coe_t R (adjoin_root f) := ⟨of f⟩ /-- Two `R`-`alg_hom` from `adjoin_root f` to the same `R`-algebra are the same iff they agree on `root f`. -/ @[ext] lemma alg_hom_ext [semiring S] [algebra R S] {g₁ g₂ : adjoin_root f →ₐ[R] S} (h : g₁ (root f) = g₂ (root f)) : g₁ = g₂ := ideal.quotient.alg_hom_ext R $ polynomial.alg_hom_ext h @[simp] lemma mk_eq_mk {g h : R[X]} : mk f g = mk f h ↔ f ∣ g - h := ideal.quotient.eq.trans ideal.mem_span_singleton @[simp] lemma mk_eq_zero {g : R[X]} : mk f g = 0 ↔ f ∣ g := mk_eq_mk.trans $ by rw sub_zero @[simp] lemma mk_self : mk f f = 0 := quotient.sound' $ quotient_add_group.left_rel_apply.mpr (mem_span_singleton.2 $ by simp) @[simp] lemma mk_C (x : R) : mk f (C x) = x := rfl @[simp] lemma mk_X : mk f X = root f := rfl lemma mk_ne_zero_of_degree_lt (hf : monic f) {g : R[X]} (h0 : g ≠ 0) (hd : degree g < degree f) : mk f g ≠ 0 := mk_eq_zero.not.2 $ hf.not_dvd_of_degree_lt h0 hd lemma mk_ne_zero_of_nat_degree_lt (hf : monic f) {g : R[X]} (h0 : g ≠ 0) (hd : nat_degree g < nat_degree f) : mk f g ≠ 0 := mk_eq_zero.not.2 $ hf.not_dvd_of_nat_degree_lt h0 hd @[simp] lemma aeval_eq (p : R[X]) : aeval (root f) p = mk f p := polynomial.induction_on p (λ x, by { rw aeval_C, refl }) (λ p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq]) (λ n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X, ring_hom.map_mul, mk_C, ring_hom.map_pow, mk_X], refl }) theorem adjoin_root_eq_top : algebra.adjoin R ({root f} : set (adjoin_root f)) = ⊤ := algebra.eq_top_iff.2 $ λ x, induction_on f x $ λ p, (algebra.adjoin_singleton_eq_range_aeval R (root f)).symm ▸ ⟨p, aeval_eq p⟩ @[simp] lemma eval₂_root (f : R[X]) : f.eval₂ (of f) (root f) = 0 := by rw [← algebra_map_eq, ← aeval_def, aeval_eq, mk_self] lemma is_root_root (f : R[X]) : is_root (f.map (of f)) (root f) := by rw [is_root, eval_map, eval₂_root] lemma is_algebraic_root (hf : f ≠ 0) : is_algebraic R (root f) := ⟨f, hf, eval₂_root f⟩ lemma of.injective_of_degree_ne_zero [is_domain R] (hf : f.degree ≠ 0) : function.injective (adjoin_root.of f) := begin rw injective_iff_map_eq_zero, intros p hp, rw [adjoin_root.of, ring_hom.comp_apply, adjoin_root.mk_eq_zero] at hp, by_cases h : f = 0, { exact C_eq_zero.mp (eq_zero_of_zero_dvd (by rwa h at hp)) }, { contrapose! hf with h_contra, rw ← degree_C h_contra, apply le_antisymm (degree_le_of_dvd hp (by rwa [ne.def, C_eq_zero])) _, rwa [degree_C h_contra, zero_le_degree_iff] }, end variables [comm_ring S] /-- Lift a ring homomorphism `i : R →+* S` to `adjoin_root f →+* S`. -/ def lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S := begin apply ideal.quotient.lift _ (eval₂_ring_hom i x), intros g H, rcases mem_span_singleton.1 H with ⟨y, hy⟩, rw [hy, ring_hom.map_mul, coe_eval₂_ring_hom, h, zero_mul] end variables {i : R →+* S} {a : S} (h : f.eval₂ i a = 0) @[simp] lemma lift_mk (g : R[X]) : lift i a h (mk f g) = g.eval₂ i a := ideal.quotient.lift_mk _ _ _ @[simp] lemma lift_root : lift i a h (root f) = a := by rw [root, lift_mk, eval₂_X] @[simp] lemma lift_of {x : R} : lift i a h x = i x := by rw [← mk_C x, lift_mk, eval₂_C] @[simp] lemma lift_comp_of : (lift i a h).comp (of f) = i := ring_hom.ext $ λ _, @lift_of _ _ _ _ _ _ _ h _ variables (f) [algebra R S] /-- Produce an algebra homomorphism `adjoin_root f →ₐ[R] S` sending `root f` to a root of `f` in `S`. -/ def lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S := { commutes' := λ r, show lift _ _ hfx r = _, from lift_of hfx, .. lift (algebra_map R S) x hfx } @[simp] lemma coe_lift_hom (x : S) (hfx : aeval x f = 0) : (lift_hom f x hfx : adjoin_root f →+* S) = lift (algebra_map R S) x hfx := rfl @[simp] lemma aeval_alg_hom_eq_zero (ϕ : adjoin_root f →ₐ[R] S) : aeval (ϕ (root f)) f = 0 := begin have h : ϕ.to_ring_hom.comp (of f) = algebra_map R S := ring_hom.ext_iff.mpr (ϕ.commutes), rw [aeval_def, ←h, ←ring_hom.map_zero ϕ.to_ring_hom, ←eval₂_root f, hom_eval₂], refl, end @[simp] lemma lift_hom_eq_alg_hom (f : R[X]) (ϕ : adjoin_root f →ₐ[R] S) : lift_hom f (ϕ (root f)) (aeval_alg_hom_eq_zero f ϕ) = ϕ := begin suffices : ϕ.equalizer (lift_hom f (ϕ (root f)) (aeval_alg_hom_eq_zero f ϕ)) = ⊤, { exact (alg_hom.ext (λ x, (set_like.ext_iff.mp (this) x).mpr algebra.mem_top)).symm }, rw [eq_top_iff, ←adjoin_root_eq_top, algebra.adjoin_le_iff, set.singleton_subset_iff], exact (@lift_root _ _ _ _ _ _ _ (aeval_alg_hom_eq_zero f ϕ)).symm, end variables (hfx : aeval a f = 0) @[simp] lemma lift_hom_mk {g : R[X]} : lift_hom f a hfx (mk f g) = aeval a g := lift_mk hfx g @[simp] lemma lift_hom_root : lift_hom f a hfx (root f) = a := lift_root hfx @[simp] lemma lift_hom_of {x : R} : lift_hom f a hfx (of f x) = algebra_map _ _ x := lift_of hfx section adjoin_inv @[simp] lemma root_is_inv (r : R) : of _ r * root (C r * X - 1) = 1 := by convert sub_eq_zero.1 ((eval₂_sub _).symm.trans $ eval₂_root $ C r * X - 1); simp only [eval₂_mul, eval₂_C, eval₂_X, eval₂_one] lemma alg_hom_subsingleton {S : Type*} [comm_ring S] [algebra R S] {r : R} : subsingleton (adjoin_root (C r * X - 1) →ₐ[R] S) := ⟨λ f g, alg_hom_ext (@inv_unique _ _ (algebra_map R S r) _ _ (by rw [← f.commutes, ← f.map_mul, algebra_map_eq, root_is_inv, map_one]) (by rw [← g.commutes, ← g.map_mul, algebra_map_eq, root_is_inv, map_one]))⟩ end adjoin_inv section prime variable {f} theorem is_domain_of_prime (hf : prime f) : is_domain (adjoin_root f) := (ideal.quotient.is_domain_iff_prime (span {f} : ideal R[X])).mpr $ (ideal.span_singleton_prime hf.ne_zero).mpr hf theorem no_zero_smul_divisors_of_prime_of_degree_ne_zero [is_domain R] (hf : prime f) (hf' : f.degree ≠ 0) : no_zero_smul_divisors R (adjoin_root f) := begin haveI := is_domain_of_prime hf, exact no_zero_smul_divisors.iff_algebra_map_injective.mpr (of.injective_of_degree_ne_zero hf') end end prime end comm_ring section irreducible variables [field K] {f : K[X]} instance span_maximal_of_irreducible [fact (irreducible f)] : (span {f}).is_maximal := principal_ideal_ring.is_maximal_of_irreducible $ fact.out _ noncomputable instance field [fact (irreducible f)] : field (adjoin_root f) := { rat_cast := λ a, of f (a : K), rat_cast_mk := λ a b h1 h2, begin letI : group_with_zero (adjoin_root f) := ideal.quotient.group_with_zero _, rw [rat.cast_mk', _root_.map_mul, _root_.map_int_cast, map_inv₀, map_nat_cast], end, qsmul := (•), qsmul_eq_mul' := λ a x, adjoin_root.induction_on _ x (λ p, by { rw [smul_mk, of, ring_hom.comp_apply, ← (mk f).map_mul, polynomial.rat_smul_eq_C_mul] }), ..adjoin_root.comm_ring f, ..ideal.quotient.group_with_zero (span {f} : ideal K[X]) } lemma coe_injective (h : degree f ≠ 0) : function.injective (coe : K → adjoin_root f) := have _ := adjoin_root.nontrivial f h, by exactI (of f).injective lemma coe_injective' [fact (irreducible f)] : function.injective (coe : K → adjoin_root f) := (of f).injective variable (f) lemma mul_div_root_cancel [fact (irreducible f)] : ((X - C (root f)) * (f.map (of f) / (X - C (root f)))) = f.map (of f) := mul_div_eq_iff_is_root.2 $ is_root_root _ end irreducible section is_noetherian_ring instance [comm_ring R] [is_noetherian_ring R] {f : R[X]} : is_noetherian_ring (adjoin_root f) := ideal.quotient.is_noetherian_ring _ end is_noetherian_ring section power_basis variables [comm_ring R] {g : R[X]} lemma is_integral_root' (hg : g.monic) : is_integral R (root g) := ⟨g, hg, eval₂_root g⟩ /-- `adjoin_root.mod_by_monic_hom` sends the equivalence class of `f` mod `g` to `f %ₘ g`. This is a well-defined right inverse to `adjoin_root.mk`, see `adjoin_root.mk_left_inverse`. -/ def mod_by_monic_hom (hg : g.monic) : adjoin_root g →ₗ[R] R[X] := (submodule.liftq _ (polynomial.mod_by_monic_hom g) (λ f (hf : f ∈ (ideal.span {g}).restrict_scalars R), (mem_ker_mod_by_monic hg).mpr (ideal.mem_span_singleton.mp hf))).comp $ (submodule.quotient.restrict_scalars_equiv R (ideal.span {g} : ideal R[X])) .symm.to_linear_map @[simp] lemma mod_by_monic_hom_mk (hg : g.monic) (f : R[X]) : mod_by_monic_hom hg (mk g f) = f %ₘ g := rfl lemma mk_left_inverse (hg : g.monic) : function.left_inverse (mk g) (mod_by_monic_hom hg) := λ f, induction_on g f $ λ f, begin rw [mod_by_monic_hom_mk hg, mk_eq_mk, mod_by_monic_eq_sub_mul_div _ hg, sub_sub_cancel_left, dvd_neg], apply dvd_mul_right end lemma mk_surjective (hg : g.monic) : function.surjective (mk g) := (mk_left_inverse hg).surjective /-- The elements `1, root g, ..., root g ^ (d - 1)` form a basis for `adjoin_root g`, where `g` is a monic polynomial of degree `d`. -/ def power_basis_aux' (hg : g.monic) : basis (fin g.nat_degree) R (adjoin_root g) := basis.of_equiv_fun { to_fun := λ f i, (mod_by_monic_hom hg f).coeff i, inv_fun := λ c, mk g $ ∑ (i : fin g.nat_degree), monomial i (c i), map_add' := λ f₁ f₂, funext $ λ i, by simp only [(mod_by_monic_hom hg).map_add, coeff_add, pi.add_apply], map_smul' := λ f₁ f₂, funext $ λ i, by simp only [(mod_by_monic_hom hg).map_smul, coeff_smul, pi.smul_apply, ring_hom.id_apply], left_inv := λ f, induction_on g f (λ f, eq.symm $ mk_eq_mk.mpr $ by { simp only [mod_by_monic_hom_mk, sum_mod_by_monic_coeff hg degree_le_nat_degree], rw [mod_by_monic_eq_sub_mul_div _ hg, sub_sub_cancel], exact dvd_mul_right _ _ }), right_inv := λ x, funext $ λ i, begin nontriviality R, simp only [mod_by_monic_hom_mk], rw [(mod_by_monic_eq_self_iff hg).mpr, finset_sum_coeff], { simp_rw [coeff_monomial, fin.coe_eq_coe, finset.sum_ite_eq', if_pos (finset.mem_univ _)] }, { simp_rw ← C_mul_X_pow_eq_monomial, exact (degree_eq_nat_degree $ hg.ne_zero).symm ▸ degree_sum_fin_lt _ }, end} /-- This lemma could be autogenerated by `@[simps]` but unfortunately that would require unfolding that causes a timeout. -/ @[simp] lemma power_basis_aux'_repr_symm_apply (hg : g.monic) (c : fin g.nat_degree →₀ R) : (power_basis_aux' hg).repr.symm c = mk g (∑ (i : fin _), monomial i (c i)) := rfl /-- This lemma could be autogenerated by `@[simps]` but unfortunately that would require unfolding that causes a timeout. -/ @[simp] theorem power_basis_aux'_repr_apply_to_fun (hg : g.monic) (f : adjoin_root g) (i : fin g.nat_degree) : (power_basis_aux' hg).repr f i = (mod_by_monic_hom hg f).coeff ↑i := rfl /-- The power basis `1, root g, ..., root g ^ (d - 1)` for `adjoin_root g`, where `g` is a monic polynomial of degree `d`. -/ @[simps] def power_basis' (hg : g.monic) : power_basis R (adjoin_root g) := { gen := root g, dim := g.nat_degree, basis := power_basis_aux' hg, basis_eq_pow := λ i, begin simp only [power_basis_aux', basis.coe_of_equiv_fun, linear_equiv.coe_symm_mk], rw finset.sum_eq_single i, { rw [function.update_same, monomial_one_right_eq_X_pow, (mk g).map_pow, mk_X] }, { intros j _ hj, rw ← monomial_zero_right _, convert congr_arg _ (function.update_noteq hj _ _) }, -- Fix `decidable_eq` mismatch { intros, have := finset.mem_univ i, contradiction }, end} variables [field K] {f : K[X]} lemma is_integral_root (hf : f ≠ 0) : is_integral K (root f) := is_algebraic_iff_is_integral.mp (is_algebraic_root hf) lemma minpoly_root (hf : f ≠ 0) : minpoly K (root f) = f * C (f.leading_coeff⁻¹) := begin have f'_monic : monic _ := monic_mul_leading_coeff_inv hf, refine (minpoly.unique K _ f'_monic _ _).symm, { rw [alg_hom.map_mul, aeval_eq, mk_self, zero_mul] }, intros q q_monic q_aeval, have commutes : (lift (algebra_map K (adjoin_root f)) (root f) q_aeval).comp (mk q) = mk f, { ext, { simp only [ring_hom.comp_apply, mk_C, lift_of], refl }, { simp only [ring_hom.comp_apply, mk_X, lift_root] } }, rw [degree_eq_nat_degree f'_monic.ne_zero, degree_eq_nat_degree q_monic.ne_zero, with_bot.coe_le_coe, nat_degree_mul hf, nat_degree_C, add_zero], apply nat_degree_le_of_dvd, { have : mk f q = 0, by rw [←commutes, ring_hom.comp_apply, mk_self, ring_hom.map_zero], rwa [←ideal.mem_span_singleton, ←ideal.quotient.eq_zero_iff_mem] }, { exact q_monic.ne_zero }, { rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] }, end /-- The elements `1, root f, ..., root f ^ (d - 1)` form a basis for `adjoin_root f`, where `f` is an irreducible polynomial over a field of degree `d`. -/ def power_basis_aux (hf : f ≠ 0) : basis (fin f.nat_degree) K (adjoin_root f) := begin set f' := f * C (f.leading_coeff⁻¹) with f'_def, have deg_f' : f'.nat_degree = f.nat_degree, { rw [nat_degree_mul hf, nat_degree_C, add_zero], { rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] } }, have minpoly_eq : minpoly K (root f) = f' := minpoly_root hf, apply @basis.mk _ _ _ (λ (i : fin f.nat_degree), (root f ^ i.val)), { rw [← deg_f', ← minpoly_eq], exact linear_independent_pow (root f) }, { rintros y -, rw [← deg_f', ← minpoly_eq], apply (is_integral_root hf).mem_span_pow, obtain ⟨g⟩ := y, use g, rw aeval_eq, refl } end /-- The power basis `1, root f, ..., root f ^ (d - 1)` for `adjoin_root f`, where `f` is an irreducible polynomial over a field of degree `d`. -/ @[simps] def power_basis (hf : f ≠ 0) : power_basis K (adjoin_root f) := { gen := root f, dim := f.nat_degree, basis := power_basis_aux hf, basis_eq_pow := basis.mk_apply _ _ } lemma minpoly_power_basis_gen (hf : f ≠ 0) : minpoly K (power_basis hf).gen = f * C (f.leading_coeff⁻¹) := by rw [power_basis_gen, minpoly_root hf] lemma minpoly_power_basis_gen_of_monic (hf : f.monic) (hf' : f ≠ 0 := hf.ne_zero) : minpoly K (power_basis hf').gen = f := by rw [minpoly_power_basis_gen hf', hf.leading_coeff, inv_one, C.map_one, mul_one] end power_basis section equiv section minpoly variables [comm_ring R] [comm_ring S] [algebra R S] (x : S) (R) open algebra polynomial /-- The surjective algebra morphism `R[X]/(minpoly R x) → R[x]`. If `R` is a GCD domain and `x` is integral, this is an isomorphism, see `adjoin_root.minpoly.equiv_adjoin`. -/ @[simps] def minpoly.to_adjoin : adjoin_root (minpoly R x) →ₐ[R] adjoin R ({x} : set S) := lift_hom _ ⟨x, self_mem_adjoin_singleton R x⟩ (by simp [← subalgebra.coe_eq_zero, aeval_subalgebra_coe]) variables {R x} lemma minpoly.to_adjoin_apply' (a : adjoin_root (minpoly R x)) : minpoly.to_adjoin R x a = lift_hom (minpoly R x) (⟨x, self_mem_adjoin_singleton R x⟩ : adjoin R ({x} : set S)) (by simp [← subalgebra.coe_eq_zero, aeval_subalgebra_coe]) a := rfl lemma minpoly.to_adjoin.apply_X : minpoly.to_adjoin R x (mk (minpoly R x) X) = ⟨x, self_mem_adjoin_singleton R x⟩ := by simp variables (R x) lemma minpoly.to_adjoin.surjective : function.surjective (minpoly.to_adjoin R x) := begin rw [← range_top_iff_surjective, _root_.eq_top_iff, ← adjoin_adjoin_coe_preimage], refine adjoin_le _, simp only [alg_hom.coe_range, set.mem_range], rintro ⟨y₁, y₂⟩ h, refine ⟨mk (minpoly R x) X, by simpa using h.symm⟩ end end minpoly section equiv' variables [comm_ring R] [comm_ring S] [algebra R S] variables (g : R[X]) (pb : _root_.power_basis R S) /-- If `S` is an extension of `R` with power basis `pb` and `g` is a monic polynomial over `R` such that `pb.gen` has a minimal polynomial `g`, then `S` is isomorphic to `adjoin_root g`. Compare `power_basis.equiv_of_root`, which would require `h₂ : aeval pb.gen (minpoly R (root g)) = 0`; that minimal polynomial is not guaranteed to be identical to `g`. -/ @[simps {fully_applied := ff}] def equiv' (h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) : adjoin_root g ≃ₐ[R] S := { to_fun := adjoin_root.lift_hom g pb.gen h₂, inv_fun := pb.lift (root g) h₁, left_inv := λ x, induction_on g x $ λ f, by rw [lift_hom_mk, pb.lift_aeval, aeval_eq], right_inv := λ x, begin nontriviality S, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, rw [pb.lift_aeval, aeval_eq, lift_hom_mk] end, .. adjoin_root.lift_hom g pb.gen h₂ } @[simp] lemma equiv'_to_alg_hom (h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) : (equiv' g pb h₁ h₂).to_alg_hom = adjoin_root.lift_hom g pb.gen h₂ := rfl @[simp] lemma equiv'_symm_to_alg_hom (h₁ : aeval (root g) (minpoly R pb.gen) = 0) (h₂ : aeval pb.gen g = 0) : (equiv' g pb h₁ h₂).symm.to_alg_hom = pb.lift (root g) h₁ := rfl end equiv' section field variables (K) (L F : Type*) [field F] [field K] [field L] [algebra F K] [algebra F L] variables (pb : _root_.power_basis F K) /-- If `L` is a field extension of `F` and `f` is a polynomial over `F` then the set of maps from `F[x]/(f)` into `L` is in bijection with the set of roots of `f` in `L`. -/ def equiv (f : F[X]) (hf : f ≠ 0) : (adjoin_root f →ₐ[F] L) ≃ {x // x ∈ (f.map (algebra_map F L)).roots} := (power_basis hf).lift_equiv'.trans ((equiv.refl _).subtype_equiv (λ x, begin rw [power_basis_gen, minpoly_root hf, polynomial.map_mul, roots_mul, polynomial.map_C, roots_C, add_zero, equiv.refl_apply], rw ← polynomial.map_mul, exact map_monic_ne_zero (monic_mul_leading_coeff_inv hf) end)) end field end equiv section open ideal double_quot polynomial variables [comm_ring R] (I : ideal R) (f : R[X]) /-- The natural isomorphism `R[α]/(I[α]) ≅ R[α]/((I[x] ⊔ (f)) / (f))` for `α` a root of `f : R[X]` and `I : ideal R`. See `adjoin_root.quot_map_of_equiv` for the isomorphism with `(R/I)[X] / (f mod I)`. -/ def quot_map_of_equiv_quot_map_C_map_span_mk : adjoin_root f ⧸ I.map (of f) ≃+* adjoin_root f ⧸ (I.map (C : R →+* R[X])).map (span {f})^.quotient.mk := ideal.quot_equiv_of_eq (by rw [of, adjoin_root.mk, ideal.map_map]) @[simp] lemma quot_map_of_equiv_quot_map_C_map_span_mk_mk (x : adjoin_root f) : quot_map_of_equiv_quot_map_C_map_span_mk I f (ideal.quotient.mk (I.map (of f)) x) = ideal.quotient.mk _ x := rfl --this lemma should have the simp tag but this causes a lint issue lemma quot_map_of_equiv_quot_map_C_map_span_mk_symm_mk (x : adjoin_root f) : (quot_map_of_equiv_quot_map_C_map_span_mk I f).symm (ideal.quotient.mk ((I.map (C : R →+* R[X])).map (span {f})^.quotient.mk) x) = ideal.quotient.mk (I.map (of f)) x := by rw [quot_map_of_equiv_quot_map_C_map_span_mk, ideal.quot_equiv_of_eq_symm, quot_equiv_of_eq_mk] /-- The natural isomorphism `R[α]/((I[x] ⊔ (f)) / (f)) ≅ (R[x]/I[x])/((f) ⊔ I[x] / I[x])` for `α` a root of `f : R[X]` and `I : ideal R`-/ def quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk : (adjoin_root f) ⧸ (I.map (C : R →+* R[X])).map (span ({f} : set R[X]))^.quotient.mk ≃+* (R[X] ⧸ I.map (C : R →+* R[X])) ⧸ (span ({f} : set R[X])).map (I.map (C : R →+* R[X]))^.quotient.mk := quot_quot_equiv_comm (ideal.span ({f} : set R[X])) (I.map (C : R →+* R[X])) @[simp] lemma quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_mk (p : R[X]) : quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f (ideal.quotient.mk _ (mk f p)) = quot_quot_mk (I.map C) (span {f}) p := rfl @[simp] lemma quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_symm_quot_quot_mk (p : R[X]) : (quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f).symm (quot_quot_mk (I.map C) (span {f}) p) = (ideal.quotient.mk _ (mk f p)) := rfl /-- The natural isomorphism `(R/I)[x]/(f mod I) ≅ (R[x]/I*R[x])/(f mod I[x])` where `f : R[X]` and `I : ideal R`-/ def polynomial.quot_quot_equiv_comm : (R ⧸ I)[X] ⧸ span ({f.map (I^.quotient.mk)} : set (polynomial (R ⧸ I))) ≃+* (R[X] ⧸ map C I) ⧸ span ({(ideal.quotient.mk (I.map C)) f} : set (R[X] ⧸ map C I)) := quotient_equiv (span ({f.map (I^.quotient.mk)} : set (polynomial (R ⧸ I)))) (span {ideal.quotient.mk (I.map polynomial.C) f}) (polynomial_quotient_equiv_quotient_polynomial I) (by rw [map_span, set.image_singleton, ring_equiv.coe_to_ring_hom, polynomial_quotient_equiv_quotient_polynomial_map_mk I f]) @[simp] lemma polynomial.quot_quot_equiv_comm_mk (p : R[X]) : (polynomial.quot_quot_equiv_comm I f) (ideal.quotient.mk _ (p.map I^.quotient.mk)) = (ideal.quotient.mk _ (ideal.quotient.mk _ p)) := by simp only [polynomial.quot_quot_equiv_comm, quotient_equiv_mk, polynomial_quotient_equiv_quotient_polynomial_map_mk] @[simp] lemma polynomial.quot_quot_equiv_comm_symm_mk_mk (p : R[X]) : (polynomial.quot_quot_equiv_comm I f).symm (ideal.quotient.mk _ (ideal.quotient.mk _ p)) = (ideal.quotient.mk _ (p.map I^.quotient.mk)) := by simp only [polynomial.quot_quot_equiv_comm, quotient_equiv_symm_mk, polynomial_quotient_equiv_quotient_polynomial_symm_mk] /-- The natural isomorphism `R[α]/I[α] ≅ (R/I)[X]/(f mod I)` for `α` a root of `f : R[X]` and `I : ideal R`.-/ def quot_adjoin_root_equiv_quot_polynomial_quot : (adjoin_root f) ⧸ (I.map (of f)) ≃+* (R ⧸ I)[X] ⧸ (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X])) := (quot_map_of_equiv_quot_map_C_map_span_mk I f).trans ((quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk I f).trans ((ideal.quot_equiv_of_eq (show (span ({f} : set R[X])).map (I.map (C : R →+* R[X]))^.quotient.mk = span ({(ideal.quotient.mk (I.map polynomial.C)) f} : set (R[X] ⧸ map C I)), from by rw [map_span, set.image_singleton])).trans (polynomial.quot_quot_equiv_comm I f).symm)) @[simp] lemma quot_adjoin_root_equiv_quot_polynomial_quot_mk_of (p : R[X]) : quot_adjoin_root_equiv_quot_polynomial_quot I f (ideal.quotient.mk (I.map (of f)) (mk f p)) = ideal.quotient.mk (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X])) (p.map I^.quotient.mk) := by rw [quot_adjoin_root_equiv_quot_polynomial_quot, ring_equiv.trans_apply, ring_equiv.trans_apply, ring_equiv.trans_apply, quot_map_of_equiv_quot_map_C_map_span_mk_mk, quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_mk, quot_quot_mk, ring_hom.comp_apply, quot_equiv_of_eq_mk, polynomial.quot_quot_equiv_comm_symm_mk_mk] @[simp] lemma quot_adjoin_root_equiv_quot_polynomial_quot_symm_mk_mk (p : R[X]) : (quot_adjoin_root_equiv_quot_polynomial_quot I f).symm (ideal.quotient.mk (span ({f.map (I^.quotient.mk)} : set (R ⧸ I)[X])) (p.map I^.quotient.mk)) = (ideal.quotient.mk (I.map (of f)) (mk f p)) := by rw [quot_adjoin_root_equiv_quot_polynomial_quot, ring_equiv.symm_trans_apply, ring_equiv.symm_trans_apply, ring_equiv.symm_trans_apply, ring_equiv.symm_symm, polynomial.quot_quot_equiv_comm_mk, ideal.quot_equiv_of_eq_symm, ideal.quot_equiv_of_eq_mk, ← ring_hom.comp_apply, ← double_quot.quot_quot_mk, quot_map_C_map_span_mk_equiv_quot_map_C_quot_map_span_mk_symm_quot_quot_mk, quot_map_of_equiv_quot_map_C_map_span_mk_symm_mk] /-- Promote `adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot` to an alg_equiv. -/ @[simps apply symm_apply] noncomputable def quot_equiv_quot_map (f : R[X]) (I : ideal R) : ((adjoin_root f) ⧸ (ideal.map (of f) I)) ≃ₐ[R] ((R ⧸ I) [X]) ⧸ (ideal.span ({polynomial.map I^.quotient.mk f} : set ((R ⧸ I) [X]))) := alg_equiv.of_ring_equiv (show ∀ x, (quot_adjoin_root_equiv_quot_polynomial_quot I f) (algebra_map R _ x) = algebra_map R _ x, from λ x, begin have : algebra_map R ((adjoin_root f) ⧸ (ideal.map (of f) I)) x = ideal.quotient.mk (ideal.map (adjoin_root.of f) I) ((mk f) (C x)) := rfl, simpa only [this, quot_adjoin_root_equiv_quot_polynomial_quot_mk_of, map_C] end) @[simp] lemma quot_equiv_quot_map_apply_mk (f g : R[X]) (I : ideal R) : adjoin_root.quot_equiv_quot_map f I (ideal.quotient.mk _ (adjoin_root.mk f g)) = ideal.quotient.mk _ (g.map I^.quotient.mk) := by rw [adjoin_root.quot_equiv_quot_map_apply, adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot_mk_of] @[simp] lemma quot_equiv_quot_map_symm_apply_mk (f g : R[X]) (I : ideal R) : (adjoin_root.quot_equiv_quot_map f I).symm (ideal.quotient.mk _ (map (ideal.quotient.mk I) g)) = ideal.quotient.mk _ (adjoin_root.mk f g) := by rw [adjoin_root.quot_equiv_quot_map_symm_apply, adjoin_root.quot_adjoin_root_equiv_quot_polynomial_quot_symm_mk_mk] end end adjoin_root namespace power_basis open adjoin_root alg_equiv variables [comm_ring R] [comm_ring S] [algebra R S] /-- Let `α` have minimal polynomial `f` over `R` and `I` be an ideal of `R`, then `R[α] / (I) = (R[x] / (f)) / pS = (R/p)[x] / (f mod p)`. -/ @[simps apply symm_apply] noncomputable def quotient_equiv_quotient_minpoly_map (pb : power_basis R S) (I : ideal R) : (S ⧸ I.map (algebra_map R S)) ≃ₐ[R] (polynomial (R ⧸ I)) ⧸ (ideal.span ({(minpoly R pb.gen).map I^.quotient.mk} : set (polynomial (R ⧸ I)))) := (of_ring_equiv (show ∀ x, (ideal.quotient_equiv _ (ideal.map (adjoin_root.of (minpoly R pb.gen)) I) (adjoin_root.equiv' (minpoly R pb.gen) pb (by rw [adjoin_root.aeval_eq, adjoin_root.mk_self]) (minpoly.aeval _ _)).symm.to_ring_equiv (by rw [ideal.map_map, alg_equiv.to_ring_equiv_eq_coe, ← alg_equiv.coe_ring_hom_commutes, ← adjoin_root.algebra_map_eq, alg_hom.comp_algebra_map])) (algebra_map R (S ⧸ I.map (algebra_map R S)) x) = algebra_map R _ x, from (λ x, by rw [← ideal.quotient.mk_algebra_map, ideal.quotient_equiv_apply, ring_hom.to_fun_eq_coe, ideal.quotient_map_mk, alg_equiv.to_ring_equiv_eq_coe, ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv, alg_equiv.commutes, quotient.mk_algebra_map]))).trans (adjoin_root.quot_equiv_quot_map _ _) @[simp] lemma quotient_equiv_quotient_minpoly_map_apply_mk (pb : power_basis R S) (I : ideal R) (g : R[X]) : pb.quotient_equiv_quotient_minpoly_map I (ideal.quotient.mk _ (aeval pb.gen g)) = ideal.quotient.mk _ (g.map I^.quotient.mk) := by rw [power_basis.quotient_equiv_quotient_minpoly_map, alg_equiv.trans_apply, alg_equiv.of_ring_equiv_apply, quotient_equiv_mk, alg_equiv.coe_ring_equiv', adjoin_root.equiv'_symm_apply, power_basis.lift_aeval, adjoin_root.aeval_eq, adjoin_root.quot_equiv_quot_map_apply_mk] @[simp] lemma quotient_equiv_quotient_minpoly_map_symm_apply_mk (pb : power_basis R S) (I : ideal R) (g : R[X]) : (pb.quotient_equiv_quotient_minpoly_map I).symm (ideal.quotient.mk _ (g.map I^.quotient.mk)) = (ideal.quotient.mk _ (aeval pb.gen g)) := begin simp only [quotient_equiv_quotient_minpoly_map, to_ring_equiv_eq_coe, symm_trans_apply, quot_equiv_quot_map_symm_apply_mk, of_ring_equiv_symm_apply, quotient_equiv_symm_mk, to_ring_equiv_symm, ring_equiv.symm_symm, adjoin_root.equiv'_apply, coe_ring_equiv, lift_hom_mk, symm_to_ring_equiv], end end power_basis
d6ab84e61a4ad33ebbcf1e8b3afe74b32806e0fb
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/alexandroff.lean
1bc1cc34fe59aeca4f9137db81121915e6abd0eb
[ "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
16,549
lean
/- Copyright (c) 2021 Yourong Zang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yourong Zang, Yury Kudryashov -/ import topology.separation import topology.opens /-! # The Alexandroff Compactification We construct the Alexandroff compactification (the one-point compactification) of an arbitrary topological space `X` and prove some properties inherited from `X`. ## Main definitions * `alexandroff`: the Alexandroff compactification, we use coercion for the canonical embedding `X → alexandroff X`; when `X` is already compact, the compactification adds an isolated point to the space. * `alexandroff.infty`: the extra point ## Main results * The topological structure of `alexandroff X` * The connectedness of `alexandroff X` for a noncompact, preconnected `X` * `alexandroff X` is `T₀` for a T₀ space `X` * `alexandroff X` is `T₁` for a T₁ space `X` * `alexandroff X` is normal if `X` is a locally compact Hausdorff space ## Tags one-point compactification, compactness -/ open set filter open_locale classical topological_space filter /-! ### Definition and basic properties In this section we define `alexandroff X` to be the disjoint union of `X` and `∞`, implemented as `option X`. Then we restate some lemmas about `option X` for `alexandroff X`. -/ /-- The Alexandroff extension of an arbitrary topological space `X` -/ def alexandroff (X : Type*) := option X namespace alexandroff variables {X : Type*} /-- The point at infinity -/ def infty : alexandroff X := none localized "notation `∞` := alexandroff.infty" in alexandroff instance : has_coe_t X (alexandroff X) := ⟨option.some⟩ instance : inhabited (alexandroff X) := ⟨∞⟩ lemma coe_injective : function.injective (coe : X → alexandroff X) := option.some_injective X @[norm_cast] lemma coe_eq_coe {x y : X} : (x : alexandroff X) = y ↔ x = y := coe_injective.eq_iff @[simp] lemma coe_ne_infty (x : X) : (x : alexandroff X) ≠ ∞ . @[simp] lemma infty_ne_coe (x : X) : ∞ ≠ (x : alexandroff X) . /-- Recursor for `alexandroff` using the preferred forms `∞` and `↑x`. -/ @[elab_as_eliminator] protected def rec (C : alexandroff X → Sort*) (h₁ : C ∞) (h₂ : Π x : X, C x) : Π (z : alexandroff X), C z := option.rec h₁ h₂ lemma is_compl_range_coe_infty : is_compl (range (coe : X → alexandroff X)) {∞} := is_compl_range_some_none X @[simp] lemma range_coe_union_infty : (range (coe : X → alexandroff X) ∪ {∞}) = univ := range_some_union_none X @[simp] lemma range_coe_inter_infty : (range (coe : X → alexandroff X) ∩ {∞}) = ∅ := range_some_inter_none X @[simp] lemma compl_range_coe : (range (coe : X → alexandroff X))ᶜ = {∞} := compl_range_some X lemma compl_infty : ({∞}ᶜ : set (alexandroff X)) = range (coe : X → alexandroff X) := (@is_compl_range_coe_infty X).symm.compl_eq lemma compl_image_coe (s : set X) : (coe '' s : set (alexandroff X))ᶜ = coe '' sᶜ ∪ {∞} := by rw [coe_injective.compl_image_eq, compl_range_coe] lemma ne_infty_iff_exists {x : alexandroff X} : x ≠ ∞ ↔ ∃ (y : X), (y : alexandroff X) = x := by induction x using alexandroff.rec; simp instance : can_lift (alexandroff X) X := { coe := coe, cond := λ x, x ≠ ∞, prf := λ x, ne_infty_iff_exists.1 } lemma not_mem_range_coe_iff {x : alexandroff X} : x ∉ range (coe : X → alexandroff X) ↔ x = ∞ := by rw [← mem_compl_iff, compl_range_coe, mem_singleton_iff] lemma infty_not_mem_range_coe : ∞ ∉ range (coe : X → alexandroff X) := not_mem_range_coe_iff.2 rfl lemma infty_not_mem_image_coe {s : set X} : ∞ ∉ (coe : X → alexandroff X) '' s := not_mem_subset (image_subset_range _ _) infty_not_mem_range_coe @[simp] lemma coe_preimage_infty : (coe : X → alexandroff X) ⁻¹' {∞} = ∅ := by { ext, simp } /-! ### Topological space structure on `alexandroff X` We define a topological space structure on `alexandroff X` so that `s` is open if and only if * `coe ⁻¹' s` is open in `X`; * if `∞ ∈ s`, then `(coe ⁻¹' s)ᶜ` is compact. Then we reformulate this definition in a few different ways, and prove that `coe : X → alexandroff X` is an open embedding. If `X` is not a compact space, then we also prove that `coe` has dense range, so it is a dense embedding. -/ variables [topological_space X] instance : topological_space (alexandroff X) := { is_open := λ s, (∞ ∈ s → is_compact ((coe : X → alexandroff X) ⁻¹' s)ᶜ) ∧ is_open ((coe : X → alexandroff X) ⁻¹' s), is_open_univ := by simp, is_open_inter := λ s t, begin rintros ⟨hms, hs⟩ ⟨hmt, ht⟩, refine ⟨_, hs.inter ht⟩, rintros ⟨hms', hmt'⟩, simpa [compl_inter] using (hms hms').union (hmt hmt') end, is_open_sUnion := λ S ho, begin suffices : is_open (coe ⁻¹' ⋃₀ S : set X), { refine ⟨_, this⟩, rintro ⟨s, hsS : s ∈ S, hs : ∞ ∈ s⟩, refine compact_of_is_closed_subset ((ho s hsS).1 hs) this.is_closed_compl _, exact compl_subset_compl.mpr (preimage_mono $ subset_sUnion_of_mem hsS) }, rw [preimage_sUnion], exact is_open_bUnion (λ s hs, (ho s hs).2) end } variables {s : set (alexandroff X)} {t : set X} lemma is_open_def : is_open s ↔ (∞ ∈ s → is_compact (coe ⁻¹' s : set X)ᶜ) ∧ is_open (coe ⁻¹' s : set X) := iff.rfl lemma is_open_iff_of_mem' (h : ∞ ∈ s) : is_open s ↔ is_compact (coe ⁻¹' s : set X)ᶜ ∧ is_open (coe ⁻¹' s : set X) := by simp [is_open_def, h] lemma is_open_iff_of_mem (h : ∞ ∈ s) : is_open s ↔ is_closed (coe ⁻¹' s : set X)ᶜ ∧ is_compact (coe ⁻¹' s : set X)ᶜ := by simp only [is_open_iff_of_mem' h, is_closed_compl_iff, and.comm] lemma is_open_iff_of_not_mem (h : ∞ ∉ s) : is_open s ↔ is_open (coe ⁻¹' s : set X) := by simp [is_open_def, h] lemma is_closed_iff_of_mem (h : ∞ ∈ s) : is_closed s ↔ is_closed (coe ⁻¹' s : set X) := have ∞ ∉ sᶜ, from λ H, H h, by rw [← is_open_compl_iff, is_open_iff_of_not_mem this, ← is_open_compl_iff, preimage_compl] lemma is_closed_iff_of_not_mem (h : ∞ ∉ s) : is_closed s ↔ is_closed (coe ⁻¹' s : set X) ∧ is_compact (coe ⁻¹' s : set X) := by rw [← is_open_compl_iff, is_open_iff_of_mem (mem_compl h), ← preimage_compl, compl_compl] @[simp] lemma is_open_image_coe {s : set X} : is_open (coe '' s : set (alexandroff X)) ↔ is_open s := by rw [is_open_iff_of_not_mem infty_not_mem_image_coe, preimage_image_eq _ coe_injective] lemma is_open_compl_image_coe {s : set X} : is_open (coe '' s : set (alexandroff X))ᶜ ↔ is_closed s ∧ is_compact s := begin rw [is_open_iff_of_mem, ← preimage_compl, compl_compl, preimage_image_eq _ coe_injective], exact infty_not_mem_image_coe end @[simp] lemma is_closed_image_coe {s : set X} : is_closed (coe '' s : set (alexandroff X)) ↔ is_closed s ∧ is_compact s := by rw [← is_open_compl_iff, is_open_compl_image_coe] /-- An open set in `alexandroff X` constructed from a closed compact set in `X` -/ def opens_of_compl (s : set X) (h₁ : is_closed s) (h₂ : is_compact s) : topological_space.opens (alexandroff X) := ⟨(coe '' s)ᶜ, is_open_compl_image_coe.2 ⟨h₁, h₂⟩⟩ lemma infty_mem_opens_of_compl {s : set X} (h₁ : is_closed s) (h₂ : is_compact s) : ∞ ∈ opens_of_compl s h₁ h₂ := mem_compl infty_not_mem_image_coe @[continuity] lemma continuous_coe : continuous (coe : X → alexandroff X) := continuous_def.mpr (λ s hs, hs.right) lemma is_open_map_coe : is_open_map (coe : X → alexandroff X) := λ s, is_open_image_coe.2 lemma open_embedding_coe : open_embedding (coe : X → alexandroff X) := open_embedding_of_continuous_injective_open continuous_coe coe_injective is_open_map_coe lemma is_open_range_coe : is_open (range (coe : X → alexandroff X)) := open_embedding_coe.open_range lemma is_closed_infty : is_closed ({∞} : set (alexandroff X)) := by { rw [← compl_range_coe, is_closed_compl_iff], exact is_open_range_coe } lemma nhds_coe_eq (x : X) : 𝓝 ↑x = map (coe : X → alexandroff X) (𝓝 x) := (open_embedding_coe.map_nhds_eq x).symm lemma nhds_within_coe_image (s : set X) (x : X) : 𝓝[coe '' s] (x : alexandroff X) = map coe (𝓝[s] x) := (open_embedding_coe.to_embedding.map_nhds_within_eq _ _).symm lemma nhds_within_coe (s : set (alexandroff X)) (x : X) : 𝓝[s] ↑x = map coe (𝓝[coe ⁻¹' s] x) := (open_embedding_coe.map_nhds_within_preimage_eq _ _).symm lemma comap_coe_nhds (x : X) : comap (coe : X → alexandroff X) (𝓝 x) = 𝓝 x := (open_embedding_coe.to_inducing.nhds_eq_comap x).symm /-- If `x` is not an isolated point of `X`, then `x : alexandroff X` is not an isolated point of `alexandroff X`. -/ instance nhds_within_compl_coe_ne_bot (x : X) [h : ne_bot (𝓝[{x}ᶜ] x)] : ne_bot (𝓝[{x}ᶜ] (x : alexandroff X)) := by simpa [nhds_within_coe, preimage, coe_eq_coe] using h.map coe lemma nhds_within_compl_infty_eq : 𝓝[{∞}ᶜ] (∞ : alexandroff X) = map coe (coclosed_compact X) := begin refine (nhds_within_basis_open ∞ _).ext (has_basis_coclosed_compact.map _) _ _, { rintro s ⟨hs, hso⟩, refine ⟨_, (is_open_iff_of_mem hs).mp hso, _⟩, simp }, { rintro s ⟨h₁, h₂⟩, refine ⟨_, ⟨mem_compl infty_not_mem_image_coe, is_open_compl_image_coe.2 ⟨h₁, h₂⟩⟩, _⟩, simp [compl_image_coe, ← diff_eq, subset_preimage_image] } end /-- If `X` is a non-compact space, then `∞` is not an isolated point of `alexandroff X`. -/ instance nhds_within_compl_infty_ne_bot [noncompact_space X] : ne_bot (𝓝[{∞}ᶜ] (∞ : alexandroff X)) := by { rw nhds_within_compl_infty_eq, apply_instance } @[priority 900] instance nhds_within_compl_ne_bot [∀ x : X, ne_bot (𝓝[{x}ᶜ] x)] [noncompact_space X] (x : alexandroff X) : ne_bot (𝓝[{x}ᶜ] x) := alexandroff.rec _ alexandroff.nhds_within_compl_infty_ne_bot (λ y, alexandroff.nhds_within_compl_coe_ne_bot y) x lemma nhds_infty_eq : 𝓝 (∞ : alexandroff X) = map coe (coclosed_compact X) ⊔ pure ∞ := by rw [← nhds_within_compl_infty_eq, nhds_within_compl_singleton_sup_pure] lemma has_basis_nhds_infty : (𝓝 (∞ : alexandroff X)).has_basis (λ s : set X, is_closed s ∧ is_compact s) (λ s, coe '' sᶜ ∪ {∞}) := begin rw nhds_infty_eq, exact (has_basis_coclosed_compact.map _).sup_pure _ end @[simp] lemma comap_coe_nhds_infty : comap (coe : X → alexandroff X) (𝓝 ∞) = coclosed_compact X := by simp [nhds_infty_eq, comap_sup, comap_map coe_injective] lemma le_nhds_infty {f : filter (alexandroff X)} : f ≤ 𝓝 ∞ ↔ ∀ s : set X, is_closed s → is_compact s → coe '' sᶜ ∪ {∞} ∈ f := by simp only [has_basis_nhds_infty.ge_iff, and_imp] lemma ultrafilter_le_nhds_infty {f : ultrafilter (alexandroff X)} : (f : filter (alexandroff X)) ≤ 𝓝 ∞ ↔ ∀ s : set X, is_closed s → is_compact s → coe '' s ∉ f := by simp only [le_nhds_infty, ← compl_image_coe, ultrafilter.mem_coe, ultrafilter.compl_mem_iff_not_mem] lemma tendsto_nhds_infty' {α : Type*} {f : alexandroff X → α} {l : filter α} : tendsto f (𝓝 ∞) l ↔ tendsto f (pure ∞) l ∧ tendsto (f ∘ coe) (coclosed_compact X) l := by simp [nhds_infty_eq, and_comm] lemma tendsto_nhds_infty {α : Type*} {f : alexandroff X → α} {l : filter α} : tendsto f (𝓝 ∞) l ↔ ∀ s ∈ l, f ∞ ∈ s ∧ ∃ t : set X, is_closed t ∧ is_compact t ∧ maps_to (f ∘ coe) tᶜ s := tendsto_nhds_infty'.trans $ by simp only [tendsto_pure_left, has_basis_coclosed_compact.tendsto_left_iff, forall_and_distrib, and_assoc, exists_prop] lemma continuous_at_infty' {Y : Type*} [topological_space Y] {f : alexandroff X → Y} : continuous_at f ∞ ↔ tendsto (f ∘ coe) (coclosed_compact X) (𝓝 (f ∞)) := tendsto_nhds_infty'.trans $ and_iff_right (tendsto_pure_nhds _ _) lemma continuous_at_infty {Y : Type*} [topological_space Y] {f : alexandroff X → Y} : continuous_at f ∞ ↔ ∀ s ∈ 𝓝 (f ∞), ∃ t : set X, is_closed t ∧ is_compact t ∧ maps_to (f ∘ coe) tᶜ s := continuous_at_infty'.trans $ by simp only [has_basis_coclosed_compact.tendsto_left_iff, exists_prop, and_assoc] lemma continuous_at_coe {Y : Type*} [topological_space Y] {f : alexandroff X → Y} {x : X} : continuous_at f x ↔ continuous_at (f ∘ coe) x := by rw [continuous_at, nhds_coe_eq, tendsto_map'_iff, continuous_at] /-- If `X` is not a compact space, then the natural embedding `X → alexandroff X` has dense range. -/ lemma dense_range_coe [noncompact_space X] : dense_range (coe : X → alexandroff X) := begin rw [dense_range, ← compl_infty], exact dense_compl_singleton _ end lemma dense_embedding_coe [noncompact_space X] : dense_embedding (coe : X → alexandroff X) := { dense := dense_range_coe, .. open_embedding_coe } /-! ### Compactness and separation properties In this section we prove that `alexandroff X` is a compact space; it is a T₀ (resp., T₁) space if the original space satisfies the same separation axiom. If the original space is a locally compact Hausdorff space, then `alexandroff X` is a normal (hence, regular and Hausdorff) space. Finally, if the original space `X` is *not* compact and is a preconnected space, then `alexandroff X` is a connected space. -/ /-- For any topological space `X`, its one point compactification is a compact space. -/ instance : compact_space (alexandroff X) := { compact_univ := begin refine is_compact_iff_ultrafilter_le_nhds.2 (λ f hf, _), clear hf, by_cases hf : (f : filter (alexandroff X)) ≤ 𝓝 ∞, { exact ⟨∞, mem_univ _, hf⟩ }, { simp only [ultrafilter_le_nhds_infty, not_forall, not_not] at hf, rcases hf with ⟨s, h₁, h₂, hsf⟩, have hf : range (coe : X → alexandroff X) ∈ f, from mem_of_superset hsf (image_subset_range _ _), have hsf' : s ∈ f.comap coe_injective hf, from (f.mem_comap _ _).2 hsf, rcases h₂.ultrafilter_le_nhds _ (le_principal_iff.2 hsf') with ⟨a, has, hle⟩, rw [ultrafilter.coe_comap, ← comap_coe_nhds, comap_le_comap_iff hf] at hle, exact ⟨a, mem_univ _, hle⟩ } end } /-- The one point compactification of a `t0_space` space is a `t0_space`. -/ instance [t0_space X] : t0_space (alexandroff X) := begin refine ⟨λ x y hxy, _⟩, induction x using alexandroff.rec; induction y using alexandroff.rec, { exact (hxy rfl).elim }, { use {∞}ᶜ, simp [is_closed_infty] }, { use {∞}ᶜ, simp [is_closed_infty] }, { rcases t0_space.t0 x y (mt coe_eq_coe.mpr hxy) with ⟨U, hUo, hU⟩, refine ⟨coe '' U, is_open_image_coe.2 hUo, _⟩, simpa [coe_eq_coe] } end /-- The one point compactification of a `t1_space` space is a `t1_space`. -/ instance [t1_space X] : t1_space (alexandroff X) := { t1 := λ z, begin induction z using alexandroff.rec, { exact is_closed_infty }, { simp only [← image_singleton, is_closed_image_coe], exact ⟨is_closed_singleton, is_compact_singleton⟩ } end } /-- The one point compactification of a locally compact Hausdorff space is a normal (hence, Hausdorff and regular) topological space. -/ instance [locally_compact_space X] [t2_space X] : normal_space (alexandroff X) := begin have key : ∀ z : X, ∃ u v : set (alexandroff X), is_open u ∧ is_open v ∧ ↑z ∈ u ∧ ∞ ∈ v ∧ u ∩ v = ∅, { intro z, rcases exists_open_with_compact_closure z with ⟨u, hu, huy', Hu⟩, refine ⟨coe '' u, (coe '' closure u)ᶜ, is_open_image_coe.2 hu, is_open_compl_image_coe.2 ⟨is_closed_closure, Hu⟩, mem_image_of_mem _ huy', mem_compl infty_not_mem_image_coe, _⟩, rw [← subset_compl_iff_disjoint, compl_compl], exact image_subset _ subset_closure }, refine @normal_of_compact_t2 _ _ _ ⟨λ x y hxy, _⟩, induction x using alexandroff.rec; induction y using alexandroff.rec, { exact (hxy rfl).elim }, { rcases key y with ⟨u, v, hu, hv, hxu, hyv, huv⟩, exact ⟨v, u, hv, hu, hyv, hxu, (inter_comm u v) ▸ huv⟩ }, { exact key x }, { exact separated_by_open_embedding open_embedding_coe (mt coe_eq_coe.mpr hxy) } end /-- If `X` is not a compact space, then `alexandroff X` is a connected space. -/ instance [preconnected_space X] [noncompact_space X] : connected_space (alexandroff X) := { to_preconnected_space := dense_embedding_coe.to_dense_inducing.preconnected_space, to_nonempty := infer_instance } end alexandroff
43e7de649cef123e6d20ec54711648da67c280bc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/subsemiring/pointwise.lean
602d99c4ccd9bcf36499d83ff9e0af09c8732962
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,907
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.group_ring_action.basic import ring_theory.subsemiring.basic import group_theory.submonoid.pointwise import data.set.pointwise.basic /-! # Pointwise instances on `subsemiring`s > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides the action `subsemiring.pointwise_mul_action` which matches the action of `mul_action_set`. This actions is available in the `pointwise` locale. ## Implementation notes This file is almost identical to `group_theory/submonoid/pointwise.lean`. Where possible, try to keep them in sync. -/ open set variables {M R : Type*} namespace subsemiring section monoid variables [monoid M] [semiring R] [mul_semiring_action M R] /-- The action on a subsemiring corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. -/ protected def pointwise_mul_action : mul_action M (subsemiring R) := { smul := λ a S, S.map (mul_semiring_action.to_ring_hom _ _ a), one_smul := λ S, (congr_arg (λ f, S.map f) (ring_hom.ext $ by exact one_smul M)).trans S.map_id, mul_smul := λ a₁ a₂ S, (congr_arg (λ f, S.map f) (ring_hom.ext $ by exact mul_smul _ _)).trans (S.map_map _ _).symm } localized "attribute [instance] subsemiring.pointwise_mul_action" in pointwise open_locale pointwise lemma pointwise_smul_def {a : M} (S : subsemiring R) : a • S = S.map (mul_semiring_action.to_ring_hom _ _ a) := rfl @[simp] lemma coe_pointwise_smul (m : M) (S : subsemiring R) : ↑(m • S) = m • (S : set R) := rfl @[simp] lemma pointwise_smul_to_add_submonoid (m : M) (S : subsemiring R) : (m • S).to_add_submonoid = m • S.to_add_submonoid := rfl lemma smul_mem_pointwise_smul (m : M) (r : R) (S : subsemiring R) : r ∈ S → m • r ∈ m • S := (set.smul_mem_smul_set : _ → _ ∈ m • (S : set R)) lemma mem_smul_pointwise_iff_exists (m : M) (r : R) (S : subsemiring R) : r ∈ m • S ↔ ∃ (s : R), s ∈ S ∧ m • s = r := (set.mem_smul_set : r ∈ m • (S : set R) ↔ _) @[simp] lemma smul_bot (a : M) : a • (⊥ : subsemiring R) = ⊥ := map_bot _ lemma smul_sup (a : M) (S T : subsemiring R) : a • (S ⊔ T) = a • S ⊔ a • T := map_sup _ _ _ lemma smul_closure (a : M) (s : set R) : a • closure s = closure (a • s) := ring_hom.map_sclosure _ _ instance pointwise_central_scalar [mul_semiring_action Mᵐᵒᵖ R] [is_central_scalar M R] : is_central_scalar M (subsemiring R) := ⟨λ a S, congr_arg (λ f, S.map f) $ ring_hom.ext $ by exact op_smul_eq_smul _⟩ end monoid section group variables [group M] [semiring R] [mul_semiring_action M R] open_locale pointwise @[simp] lemma smul_mem_pointwise_smul_iff {a : M} {S : subsemiring R} {x : R} : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff lemma mem_pointwise_smul_iff_inv_smul_mem {a : M} {S : subsemiring R} {x : R} : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem lemma mem_inv_pointwise_smul_iff {a : M} {S : subsemiring R} {x : R} : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff @[simp] lemma pointwise_smul_le_pointwise_smul_iff {a : M} {S T : subsemiring R} : a • S ≤ a • T ↔ S ≤ T := set_smul_subset_set_smul_iff lemma pointwise_smul_subset_iff {a : M} {S T : subsemiring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := set_smul_subset_iff lemma subset_pointwise_smul_iff {a : M} {S T : subsemiring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_set_smul_iff /-! TODO: add `equiv_smul` like we have for subgroup. -/ end group section group_with_zero variables [group_with_zero M] [semiring R] [mul_semiring_action M R] open_locale pointwise @[simp] lemma smul_mem_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : subsemiring R) (x : R) : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff₀ ha (S : set R) x lemma mem_pointwise_smul_iff_inv_smul_mem₀ {a : M} (ha : a ≠ 0) (S : subsemiring R) (x : R) : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem₀ ha (S : set R) x lemma mem_inv_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : subsemiring R) (x : R) : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff₀ ha (S : set R) x @[simp] lemma pointwise_smul_le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : subsemiring R} : a • S ≤ a • T ↔ S ≤ T := set_smul_subset_set_smul_iff₀ ha lemma pointwise_smul_le_iff₀ {a : M} (ha : a ≠ 0) {S T : subsemiring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := set_smul_subset_iff₀ ha lemma le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : subsemiring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_set_smul_iff₀ ha end group_with_zero end subsemiring
ee90c6b0241d168263230fe1da63953e591ed9c4
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/ring_theory/adjoin/basic.lean
ad5ea5b4b4a910c8e88b44288fba583fcbb0e348
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,203
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.polynomial.basic import algebra.algebra.subalgebra /-! # Adjoining elements to form subalgebras This file develops the basic theory of subalgebras of an R-algebra generated by a set of elements. A basic interface for `adjoin` is set up, and various results about finitely-generated subalgebras and submodules are proved. ## Definitions * `fg (S : subalgebra R A)` : A predicate saying that the subalgebra is finitely-generated as an A-algebra ## Tags adjoin, algebra, finitely-generated algebra -/ universes u v w open submodule namespace algebra variables {R : Type u} {A : Type v} {B : Type w} section semiring variables [comm_semiring R] [semiring A] [semiring B] variables [algebra R A] [algebra R B] {s t : set A} open subsemiring theorem subset_adjoin : s ⊆ adjoin R s := algebra.gc.le_u_l s theorem adjoin_le {S : subalgebra R A} (H : s ⊆ S) : adjoin R s ≤ S := algebra.gc.l_le H theorem adjoin_le_iff {S : subalgebra R A} : adjoin R s ≤ S ↔ s ⊆ S:= algebra.gc _ _ theorem adjoin_mono (H : s ⊆ t) : adjoin R s ≤ adjoin R t := algebra.gc.monotone_l H variables (R A) @[simp] theorem adjoin_empty : adjoin R (∅ : set A) = ⊥ := show adjoin R ⊥ = ⊥, by { apply galois_connection.l_bot, exact algebra.gc } variables (R) {A} (s) theorem adjoin_eq_span : (adjoin R s).to_submodule = span R (monoid.closure s) := begin apply le_antisymm, { intros r hr, rcases mem_closure_iff_exists_list.1 hr with ⟨L, HL, rfl⟩, clear hr, induction L with hd tl ih, { exact zero_mem _ }, rw list.forall_mem_cons at HL, rw [list.map_cons, list.sum_cons], refine submodule.add_mem _ _ (ih HL.2), replace HL := HL.1, clear ih tl, suffices : ∃ z r (hr : r ∈ monoid.closure s), has_scalar.smul.{u v} z r = list.prod hd, { rcases this with ⟨z, r, hr, hzr⟩, rw ← hzr, exact smul_mem _ _ (subset_span hr) }, induction hd with hd tl ih, { exact ⟨1, 1, is_submonoid.one_mem, one_smul _ _⟩ }, rw list.forall_mem_cons at HL, rcases (ih HL.2) with ⟨z, r, hr, hzr⟩, rw [list.prod_cons, ← hzr], rcases HL.1 with ⟨hd, rfl⟩ | hs, { refine ⟨hd * z, r, hr, _⟩, rw [smul_def, smul_def, (algebra_map _ _).map_mul, _root_.mul_assoc] }, { exact ⟨z, hd * r, is_submonoid.mul_mem (monoid.subset_closure hs) hr, (mul_smul_comm _ _ _).symm⟩ } }, exact span_le.2 (show monoid.closure s ⊆ adjoin R s, from monoid.closure_subset subset_adjoin) end lemma adjoin_image (f : A →ₐ[R] B) (s : set A) : adjoin R (f '' s) = (adjoin R s).map f := le_antisymm (adjoin_le $ set.image_subset _ subset_adjoin) $ subalgebra.map_le.2 $ adjoin_le $ set.image_subset_iff.1 subset_adjoin @[simp] lemma adjoin_insert_adjoin (x : A) : adjoin R (insert x ↑(adjoin R s)) = adjoin R (insert x s) := le_antisymm (adjoin_le (set.insert_subset.mpr ⟨subset_adjoin (set.mem_insert _ _), adjoin_mono (set.subset_insert _ _)⟩)) (algebra.adjoin_mono (set.insert_subset_insert algebra.subset_adjoin)) end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] variables [algebra R A] {s t : set A} open subsemiring variables (R s t) theorem adjoin_union : adjoin R (s ∪ t) = (adjoin R s).under (adjoin (adjoin R s) t) := le_antisymm (closure_mono $ set.union_subset (set.range_subset_iff.2 $ λ r, or.inl ⟨algebra_map R (adjoin R s) r, rfl⟩) (set.union_subset_union_left _ $ λ x hxs, ⟨⟨_, subset_adjoin hxs⟩, rfl⟩)) (closure_le.2 $ set.union_subset (set.range_subset_iff.2 $ λ x, adjoin_mono (set.subset_union_left _ _) x.2) (set.subset.trans (set.subset_union_right _ _) subset_adjoin)) theorem adjoin_eq_range : adjoin R s = (mv_polynomial.aeval (coe : s → A)).range := le_antisymm (adjoin_le $ λ x hx, ⟨mv_polynomial.X ⟨x, hx⟩, set.mem_univ _, mv_polynomial.eval₂_X _ _ _⟩) (λ x ⟨p, _, (hp : mv_polynomial.aeval coe p = x)⟩, hp ▸ mv_polynomial.induction_on p (λ r, by { rw [mv_polynomial.aeval_def, mv_polynomial.eval₂_C], exact (adjoin R s).algebra_map_mem r }) (λ p q hp hq, by rw alg_hom.map_add; exact subalgebra.add_mem _ hp hq) (λ p ⟨n, hn⟩ hp, by rw [alg_hom.map_mul, mv_polynomial.aeval_def _ (mv_polynomial.X _), mv_polynomial.eval₂_X]; exact subalgebra.mul_mem _ hp (subset_adjoin hn))) theorem adjoin_singleton_eq_range (x : A) : adjoin R {x} = (polynomial.aeval x).range := le_antisymm (adjoin_le $ set.singleton_subset_iff.2 ⟨polynomial.X, set.mem_univ _, polynomial.eval₂_X _ _⟩) (λ y ⟨p, _, (hp : polynomial.aeval x p = y)⟩, hp ▸ polynomial.induction_on p (λ r, by { rw [polynomial.aeval_def, polynomial.eval₂_C], exact (adjoin R _).algebra_map_mem r }) (λ p q hp hq, by rw alg_hom.map_add; exact subalgebra.add_mem _ hp hq) (λ n r ih, by { rw [pow_succ', ← mul_assoc, alg_hom.map_mul, polynomial.aeval_def _ polynomial.X, polynomial.eval₂_X], exact subalgebra.mul_mem _ ih (subset_adjoin rfl) })) lemma adjoin_singleton_one : adjoin R ({1} : set A) = ⊥ := eq_bot_iff.2 $ adjoin_le $ set.singleton_subset_iff.2 $ set_like.mem_coe.2 $ one_mem _ theorem adjoin_union_coe_submodule : (adjoin R (s ∪ t)).to_submodule = (adjoin R s).to_submodule * (adjoin R t).to_submodule := begin rw [adjoin_eq_span, adjoin_eq_span, adjoin_eq_span, span_mul_span], congr' 1 with z, simp [monoid.mem_closure_union_iff, set.mem_mul], end end comm_semiring section ring variables [comm_ring R] [ring A] variables [algebra R A] {s t : set A} variables {R s t} open ring theorem adjoin_int (s : set R) : adjoin ℤ s = subalgebra_of_is_subring (closure s) := le_antisymm (adjoin_le subset_closure) (closure_subset subset_adjoin : closure s ≤ adjoin ℤ s) theorem mem_adjoin_iff {s : set A} {x : A} : x ∈ adjoin R s ↔ x ∈ closure (set.range (algebra_map R A) ∪ s) := ⟨λ hx, subsemiring.closure_induction hx subset_closure is_add_submonoid.zero_mem is_submonoid.one_mem (λ _ _, is_add_submonoid.add_mem) (λ _ _, is_submonoid.mul_mem), suffices closure (set.range ⇑(algebra_map R A) ∪ s) ⊆ adjoin R s, from @this x, closure_subset subsemiring.subset_closure⟩ theorem adjoin_eq_ring_closure (s : set A) : (adjoin R s : set A) = closure (set.range (algebra_map R A) ∪ s) := set.ext $ λ x, mem_adjoin_iff end ring section comm_ring variables [comm_ring R] [comm_ring A] variables [algebra R A] {s t : set A} variables {R s t} open ring theorem fg_trans (h1 : (adjoin R s).to_submodule.fg) (h2 : (adjoin (adjoin R s) t).to_submodule.fg) : (adjoin R (s ∪ t)).to_submodule.fg := begin rcases fg_def.1 h1 with ⟨p, hp, hp'⟩, rcases fg_def.1 h2 with ⟨q, hq, hq'⟩, refine fg_def.2 ⟨p * q, hp.mul hq, le_antisymm _ _⟩, { rw [span_le], rintros _ ⟨x, y, hx, hy, rfl⟩, change x * y ∈ _, refine is_submonoid.mul_mem _ _, { have : x ∈ (adjoin R s).to_submodule, { rw ← hp', exact subset_span hx }, exact adjoin_mono (set.subset_union_left _ _) this }, have : y ∈ (adjoin (adjoin R s) t).to_submodule, { rw ← hq', exact subset_span hy }, change y ∈ adjoin R (s ∪ t), rwa adjoin_union }, { intros r hr, change r ∈ adjoin R (s ∪ t) at hr, rw adjoin_union at hr, change r ∈ (adjoin (adjoin R s) t).to_submodule at hr, haveI := classical.dec_eq A, haveI := classical.dec_eq R, rw [← hq', ← set.image_id q, finsupp.mem_span_iff_total (adjoin R s)] at hr, rcases hr with ⟨l, hlq, rfl⟩, have := @finsupp.total_apply A A (adjoin R s), rw [this, finsupp.sum], refine sum_mem _ _, intros z hz, change (l z).1 * _ ∈ _, have : (l z).1 ∈ (adjoin R s).to_submodule := (l z).2, rw [← hp', ← set.image_id p, finsupp.mem_span_iff_total R] at this, rcases this with ⟨l2, hlp, hl⟩, have := @finsupp.total_apply A A R, rw this at hl, rw [←hl, finsupp.sum_mul], refine sum_mem _ _, intros t ht, change _ * _ ∈ _, rw smul_mul_assoc, refine smul_mem _ _ _, exact subset_span ⟨t, z, hlp ht, hlq hz, rfl⟩ } end end comm_ring end algebra namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- A subalgebra `S` is finitely generated if there exists `t : finset A` such that `algebra.adjoin R t = S`. -/ def fg (S : subalgebra R A) : Prop := ∃ t : finset A, algebra.adjoin R ↑t = S lemma fg_adjoin_finset (s : finset A) : (algebra.adjoin R (↑s : set A)).fg := ⟨s, rfl⟩ theorem fg_def {S : subalgebra R A} : S.fg ↔ ∃ t : set A, set.finite t ∧ algebra.adjoin R t = S := ⟨λ ⟨t, ht⟩, ⟨↑t, set.finite_mem_finset t, ht⟩, λ ⟨t, ht1, ht2⟩, ⟨ht1.to_finset, by rwa set.finite.coe_to_finset⟩⟩ theorem fg_bot : (⊥ : subalgebra R A).fg := ⟨∅, algebra.adjoin_empty R A⟩ theorem fg_of_fg_to_submodule {S : subalgebra R A} : S.to_submodule.fg → S.fg := λ ⟨t, ht⟩, ⟨t, le_antisymm (algebra.adjoin_le (λ x hx, show x ∈ S.to_submodule, from ht ▸ subset_span hx)) $ show S.to_submodule ≤ (algebra.adjoin R ↑t).to_submodule, from (λ x hx, span_le.mpr (λ x hx, algebra.subset_adjoin hx) (show x ∈ span R ↑t, by { rw ht, exact hx }))⟩ theorem fg_of_noetherian [is_noetherian R A] (S : subalgebra R A) : S.fg := fg_of_fg_to_submodule (is_noetherian.noetherian S.to_submodule) lemma fg_of_submodule_fg (h : (⊤ : submodule R A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := h in ⟨s, to_submodule_injective $ by { rw [algebra.coe_top, eq_top_iff, ← hs, span_le], exact algebra.subset_adjoin }⟩ section open_locale classical lemma fg_map (S : subalgebra R A) (f : A →ₐ[R] B) (hs : S.fg) : (S.map f).fg := let ⟨s, hs⟩ := hs in ⟨s.image f, by rw [finset.coe_image, algebra.adjoin_image, hs]⟩ end lemma fg_of_fg_map (S : subalgebra R A) (f : A →ₐ[R] B) (hf : function.injective f) (hs : (S.map f).fg) : S.fg := let ⟨s, hs⟩ := hs in ⟨s.preimage f $ λ _ _ _ _ h, hf h, map_injective f hf $ by { rw [← algebra.adjoin_image, finset.coe_preimage, set.image_preimage_eq_of_subset, hs], rw [← alg_hom.coe_range, ← algebra.adjoin_le_iff, hs, ← algebra.map_top], exact map_mono le_top }⟩ lemma fg_top (S : subalgebra R A) : (⊤ : subalgebra R S).fg ↔ S.fg := ⟨λ h, by { rw [← S.range_val, ← algebra.map_top], exact fg_map _ _ h }, λ h, fg_of_fg_map _ S.val subtype.val_injective $ by { rw [algebra.map_top, range_val], exact h }⟩ lemma induction_on_adjoin [is_noetherian R A] (P : subalgebra R A → Prop) (base : P ⊥) (ih : ∀ (S : subalgebra R A) (x : A), P S → P (algebra.adjoin R (insert x S))) (S : subalgebra R A) : P S := begin classical, obtain ⟨t, rfl⟩ := S.fg_of_noetherian, refine finset.induction_on t _ _, { simpa using base }, intros x t hxt h, convert ih _ x h using 1, rw [finset.coe_insert, algebra.adjoin_insert_adjoin] end end subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] /-- The image of a Noetherian R-algebra under an R-algebra map is a Noetherian ring. -/ instance alg_hom.is_noetherian_ring_range (f : A →ₐ[R] B) [is_noetherian_ring A] : is_noetherian_ring f.range := is_noetherian_ring_range f.to_ring_hom theorem is_noetherian_ring_of_fg {S : subalgebra R A} (HS : S.fg) [is_noetherian_ring R] : is_noetherian_ring S := let ⟨t, ht⟩ := HS in ht ▸ (algebra.adjoin_eq_range R (↑t : set A)).symm ▸ by haveI : is_noetherian_ring (mv_polynomial (↑t : set A) R) := mv_polynomial.is_noetherian_ring; convert alg_hom.is_noetherian_ring_range _; apply_instance theorem is_noetherian_ring_closure (s : set R) (hs : s.finite) : @@is_noetherian_ring (ring.closure s) subset.ring := show is_noetherian_ring (subalgebra_of_is_subring (ring.closure s)), from algebra.adjoin_int s ▸ is_noetherian_ring_of_fg (subalgebra.fg_def.2 ⟨s, hs, rfl⟩)
734a14ce0d90433dbcff187b4245c94af3399c00
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_ematch7.lean
30f01c377166e323b906b6889d64e831d0ab6f1a
[ "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
289
lean
import algebra.ring data.int open algebra variables {A : Type} [s : ring A] (a b : A) include s set_option blast.strategy "ematch" attribute zero_mul [forward] example : a = 0 → a * b = 0 := by blast open int definition ex1 (a b : int) : a = 0 → a * b = 0 := by blast print ex1
8c64986a00563acea560092bed4414f3f1b6fbc0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Parser/StrInterpolation.lean
ad0571e6d988bb3d442680c108054cfbbaca268a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
2,145
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.Parser.Basic namespace Lean.Parser def isQuotableCharForStrInterpolant (c : Char) : Bool := c == '{' || isQuotableCharDefault c partial def interpolatedStrFn (p : ParserFn) : ParserFn := fun c s => let input := c.input let stackSize := s.stackSize let rec parse (startPos : String.Pos) (c : ParserContext) (s : ParserState) : ParserState := let i := s.pos if input.atEnd i then let s := s.pushSyntax Syntax.missing let s := s.mkNode interpolatedStrKind stackSize s.setError "unterminated string literal" else let curr := input.get i let s := s.setPos (input.next i) if curr == '\"' then let s := mkNodeToken interpolatedStrLitKind startPos c s s.mkNode interpolatedStrKind stackSize else if curr == '\\' then andthenFn (quotedCharCoreFn isQuotableCharForStrInterpolant) (parse startPos) c s else if curr == '{' then let s := mkNodeToken interpolatedStrLitKind startPos c s let s := p c s if s.hasError then s else let i := s.pos let curr := input.get i if curr == '}' then let s := s.setPos (input.next i) parse i c s else let s := s.pushSyntax Syntax.missing let s := s.mkNode interpolatedStrKind stackSize s.setError "'}'" else parse startPos c s let startPos := s.pos if input.atEnd startPos then s.mkEOIError else let curr := input.get s.pos; if curr != '\"' then s.mkError "interpolated string" else let s := s.next input startPos parse startPos c s @[inline] def interpolatedStrNoAntiquot (p : Parser) : Parser := { fn := interpolatedStrFn (withoutPosition p).fn, info := mkAtomicInfo "interpolatedStr" } def interpolatedStr (p : Parser) : Parser := withAntiquot (mkAntiquot "interpolatedStr" interpolatedStrKind) $ interpolatedStrNoAntiquot p end Lean.Parser
7da8376cfa6bb0913f39855ce4b973cb5d652865
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/replace.lean
ffa1bfd902ed8e923716fe069e40d32ae21966ea
[ "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
601
lean
import Lean open Lean partial def mkBig : Nat → Expr | 0 => mkConst `a | (n+1) => mkApp2 (mkConst `f []) (mkBig n) (mkBig n) def replaceTest (e : Expr) : Expr := e.replace $ fun e => match e with | Expr.const c _ => if c == `f then mkConst `g else none | _ => none #eval replaceTest $ mkBig 4 #eval (replaceTest $ mkBig 128).getAppFn def findTest (e : Expr) : Option Expr := e.find? $ fun e => match e with | Expr.const c _ => c == `g | _ => false #eval findTest $ mkBig 4 #eval findTest $ replaceTest $ mkBig 4 #eval findTest $ mkBig 128 #eval findTest $ (replaceTest $ mkBig 128)
50ffff542e9a0b2f48004158951003b77288ddf6
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/linear_algebra/matrix/basis.lean
6098379436261af11ce9a557df51cc70c0fc15a8
[ "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
5,277
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.to_lin /-! # Bases and matrices This file defines the map `basis.to_matrix` that sends a family of vectors to the matrix of their coordinates with respect to some basis. ## Main definitions * `basis.to_matrix e v` is the matrix whose `i, j`th entry is `e.repr (v j) i` * `basis.to_matrix_equiv` is `basis.to_matrix` bundled as a linear equiv ## Main results * `linear_map.to_matrix_id_eq_basis_to_matrix`: `linear_map.to_matrix b c id` is equal to `basis.to_matrix b c` * `basis.to_matrix_mul_to_matrix`: multiplying `basis.to_matrix` with another `basis.to_matrix` gives a `basis.to_matrix` ## Tags matrix, basis -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix section basis_to_matrix variables {ι ι' κ κ' : Type*} [fintype ι] [fintype ι'] [fintype κ] [fintype κ'] variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open function matrix /-- From a basis `e : ι → M` and a family of vectors `v : ι' → M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def basis.to_matrix (e : basis ι R M) (v : ι' → M) : matrix ι ι' R := λ i j, e.repr (v j) i variables (e : basis ι R M) (v : ι' → M) (i : ι) (j : ι') namespace basis lemma to_matrix_apply : e.to_matrix v i j = e.repr (v j) i := rfl lemma to_matrix_transpose_apply : (e.to_matrix v)ᵀ j = e.repr (v j) := funext $ (λ _, rfl) lemma to_matrix_eq_to_matrix_constr [decidable_eq ι] (v : ι → M) : e.to_matrix v = linear_map.to_matrix e e (e.constr ℕ v) := by { ext, rw [basis.to_matrix_apply, linear_map.to_matrix_apply, basis.constr_basis] } @[simp] lemma to_matrix_self [decidable_eq ι] : e.to_matrix e = 1 := begin rw basis.to_matrix, ext i j, simp [basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end lemma to_matrix_update [decidable_eq ι'] (x : M) : e.to_matrix (function.update v j x) = matrix.update_column (e.to_matrix v) j (e.repr x) := begin ext i' k, rw [basis.to_matrix, matrix.update_column_apply, e.to_matrix_apply], split_ifs, { rw [h, update_same j x v] }, { rw update_noteq h }, end @[simp] lemma sum_to_matrix_smul_self : ∑ (i : ι), e.to_matrix v i j • e i = v j := by simp_rw [e.to_matrix_apply, e.sum_repr] @[simp] lemma to_lin_to_matrix [decidable_eq ι'] (v : basis ι' R M) : matrix.to_lin v e (e.to_matrix v) = id := v.ext (λ i, by rw [to_lin_self, id_apply, e.sum_to_matrix_smul_self]) /-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def to_matrix_equiv (e : basis ι R M) : (ι → M) ≃ₗ[R] matrix ι ι R := { to_fun := e.to_matrix, map_add' := λ v w, begin ext i j, change _ = _ + _, rw [e.to_matrix_apply, pi.add_apply, linear_equiv.map_add], refl end, map_smul' := begin intros c v, ext i j, rw [e.to_matrix_apply, pi.smul_apply, linear_equiv.map_smul], refl end, inv_fun := λ m j, ∑ i, (m i j) • e i, left_inv := begin intro v, ext j, exact e.sum_to_matrix_smul_self v j end, right_inv := begin intros m, ext k l, simp only [e.to_matrix_apply, ← e.equiv_fun_apply, ← e.equiv_fun_symm_apply, linear_equiv.apply_symm_apply], end } end basis section mul_linear_map_to_matrix variables {N : Type*} [add_comm_group N] [module R N] variables (b : basis ι R M) (b' : basis ι' R M) (c : basis κ R N) (c' : basis κ' R N) variables (f : M →ₗ[R] N) open linear_map @[simp] lemma basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ι'] : c.to_matrix c' ⬝ linear_map.to_matrix b' c' f = linear_map.to_matrix b' c f := (matrix.to_lin b' c).injective (by haveI := classical.dec_eq κ'; rw [to_lin_to_matrix, to_lin_mul b' c' c, to_lin_to_matrix, c.to_lin_to_matrix, id_comp]) @[simp] lemma linear_map_to_matrix_mul_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] : linear_map.to_matrix b' c' f ⬝ b'.to_matrix b = linear_map.to_matrix b c' f := (matrix.to_lin b c').injective (by rw [to_lin_to_matrix, to_lin_mul b b' c', to_lin_to_matrix, b'.to_lin_to_matrix, comp_id]) /-- A generalization of `linear_map.to_matrix_id`. -/ @[simp] lemma linear_map.to_matrix_id_eq_basis_to_matrix [decidable_eq ι] : linear_map.to_matrix b b' id = b'.to_matrix b := by { haveI := classical.dec_eq ι', rw [← basis_to_matrix_mul_linear_map_to_matrix b b', to_matrix_id, matrix.mul_one] } /-- A generalization of `basis.to_matrix_self`, in the opposite direction. -/ @[simp] lemma basis.to_matrix_mul_to_matrix {ι'' : Type*} [fintype ι''] (b'' : ι'' → M) : b.to_matrix b' ⬝ b'.to_matrix b'' = b.to_matrix b'' := begin haveI := classical.dec_eq ι, haveI := classical.dec_eq ι', haveI := classical.dec_eq ι'', ext i j, simp only [matrix.mul_apply, basis.to_matrix_apply, basis.sum_repr_mul_repr], end end mul_linear_map_to_matrix end basis_to_matrix
176492b7e6974cf6c9e58743cdc9a969f400878d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/category/LinearOrder.lean
599b11cd17f4ab5901e9fcedd774f1df6f47196d
[ "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
2,095
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.category.PartialOrder /-! # Category of linear orders This defines `LinearOrder`, the category of linear orders with monotone maps. -/ open category_theory universe u /-- The category of linear orders. -/ def LinearOrder := bundled linear_order namespace LinearOrder instance : bundled_hom.parent_projection @linear_order.to_partial_order := ⟨⟩ attribute [derive [large_category, concrete_category]] LinearOrder instance : has_coe_to_sort LinearOrder Type* := bundled.has_coe_to_sort /-- Construct a bundled `LinearOrder` from the underlying type and typeclass. -/ def of (α : Type*) [linear_order α] : LinearOrder := bundled.of α instance : inhabited LinearOrder := ⟨of punit⟩ instance (α : LinearOrder) : linear_order α := α.str instance has_forget_to_PartialOrder : has_forget₂ LinearOrder PartialOrder := bundled_hom.forget₂ _ _ /-- Constructs an equivalence between linear orders from an order isomorphism between them. -/ @[simps] def iso.mk {α β : LinearOrder.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply x }, inv_hom_id' := by { ext, exact e.apply_symm_apply x } } /-- `order_dual` as a functor. -/ @[simps] def to_dual : LinearOrder ⥤ LinearOrder := { obj := λ X, of (order_dual X), map := λ X Y, order_hom.dual } /-- The equivalence between `PartialOrder` and itself induced by `order_dual` both ways. -/ @[simps functor inverse] def dual_equiv : LinearOrder ≌ LinearOrder := equivalence.mk to_dual to_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 LinearOrder lemma LinearOrder_dual_equiv_comp_forget_to_PartialOrder : LinearOrder.dual_equiv.functor ⋙ forget₂ LinearOrder PartialOrder = forget₂ LinearOrder PartialOrder ⋙ PartialOrder.dual_equiv.functor := rfl
1ac6df29b5c31490fbd6ea658adb5be7dd835817
ac89c256db07448984849346288e0eeffe8b20d0
/src/Lean/Compiler/InitAttr.lean
68c18671526a7a6f815aedb665991fbdaaadb646
[ "Apache-2.0" ]
permissive
chepinzhang/lean4
002cc667f35417a418f0ebc9cb4a44559bb0ccac
24fe2875c68549b5481f07c57eab4ad4a0ae5305
refs/heads/master
1,688,942,838,326
1,628,801,942,000
1,628,801,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,227
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.Environment import Lean.Attributes namespace Lean private def getIOTypeArg : Expr → Option Expr | Expr.app (Expr.const `IO _ _) arg _ => some arg | _ => none private def isUnitType : Expr → Bool | Expr.const `Unit _ _ => true | _ => false private def isIOUnit (type : Expr) : Bool := match getIOTypeArg type with | some type => isUnitType type | _ => false /-- Run the initializer for `decl` and store its value for global access. Should only be used while importing. -/ @[extern "lean_run_init"] unsafe constant runInit (env : @& Environment) (opts : @& Options) (decl initDecl : @& Name) : IO Unit unsafe def registerInitAttrUnsafe (attrName : Name) (runAfterImport : Bool) : IO (ParametricAttribute Name) := registerParametricAttribute { name := attrName, descr := "initialization procedure for global references", getParam := fun declName stx => do let decl ← getConstInfo declName match (← Attribute.Builtin.getIdent? stx) with | some initFnName => let initFnName ← resolveGlobalConstNoOverload initFnName let initDecl ← getConstInfo initFnName match getIOTypeArg initDecl.type with | none => throwError "initialization function '{initFnName}' must have type of the form `IO <type>`" | some initTypeArg => if decl.type == initTypeArg then pure initFnName else throwError "initialization function '{initFnName}' type mismatch" | none => if isIOUnit decl.type then pure Name.anonymous else throwError "initialization function must have type `IO Unit`" afterImport := fun entries => do let ctx ← read if runAfterImport then for modEntries in entries do for (decl, initDecl) in modEntries do if initDecl.isAnonymous then let initFn ← IO.ofExcept <| ctx.env.evalConst (IO Unit) ctx.opts decl initFn else runInit ctx.env ctx.opts decl initDecl } @[implementedBy registerInitAttrUnsafe] constant registerInitAttr (attrName : Name) (runAfterImport : Bool) : IO (ParametricAttribute Name) builtin_initialize regularInitAttr : ParametricAttribute Name ← registerInitAttr `init true builtin_initialize builtinInitAttr : ParametricAttribute Name ← registerInitAttr `builtinInit false def getInitFnNameForCore? (env : Environment) (attr : ParametricAttribute Name) (fn : Name) : Option Name := match attr.getParam env fn with | some Name.anonymous => none | some n => some n | _ => none @[export lean_get_builtin_init_fn_name_for] def getBuiltinInitFnNameFor? (env : Environment) (fn : Name) : Option Name := getInitFnNameForCore? env builtinInitAttr fn @[export lean_get_regular_init_fn_name_for] def getRegularInitFnNameFor? (env : Environment) (fn : Name) : Option Name := getInitFnNameForCore? env regularInitAttr fn @[export lean_get_init_fn_name_for] def getInitFnNameFor? (env : Environment) (fn : Name) : Option Name := getBuiltinInitFnNameFor? env fn <|> getRegularInitFnNameFor? env fn def isIOUnitInitFnCore (env : Environment) (attr : ParametricAttribute Name) (fn : Name) : Bool := match attr.getParam env fn with | some Name.anonymous => true | _ => false @[export lean_is_io_unit_regular_init_fn] def isIOUnitRegularInitFn (env : Environment) (fn : Name) : Bool := isIOUnitInitFnCore env regularInitAttr fn @[export lean_is_io_unit_builtin_init_fn] def isIOUnitBuiltinInitFn (env : Environment) (fn : Name) : Bool := isIOUnitInitFnCore env builtinInitAttr fn def isIOUnitInitFn (env : Environment) (fn : Name) : Bool := isIOUnitBuiltinInitFn env fn || isIOUnitRegularInitFn env fn def hasInitAttr (env : Environment) (fn : Name) : Bool := (getInitFnNameFor? env fn).isSome def setBuiltinInitAttr (env : Environment) (declName : Name) (initFnName : Name := Name.anonymous) : Except String Environment := builtinInitAttr.setParam env declName initFnName end Lean
76844340ba3b7635768616d8941d0533f0acdfe7
96338d06deb5f54f351493a71d6ecf6c546089a2
/priv/Lean/Setoid.lean
3dba845c80905e6a547c17852a673f5abdc01b79
[]
no_license
silky/exe
5f9e4eea772d74852a1a2fac57d8d20588282d2b
e81690d6e16f2a83c105cce446011af6ae905b81
refs/heads/master
1,609,385,766,412
1,472,164,223,000
1,472,164,223,000
66,610,224
1
0
null
1,472,178,919,000
1,472,178,919,000
null
UTF-8
Lean
false
false
7,329
lean
/- Setoid.lean -/ set_option pp.universes true set_option pp.metavar_args false /- naming conventions: - names with suffix Type are types, with suffix Prop are properties, with suffix Set are setoids, with suffix Cat are categories. - additional definitions for `MyType` are in `My` namespace -/ namespace EXE -- equivalence definition EquType.{uEl} : Type.{uEl} → Type.{max uEl 1} := λ El, ∀ (l r : El), Prop -- axioms of equivalence definition Equ.ReflProp {El : Type} (Equ : EquType El) : Prop := ∀{e0 : El}, Equ e0 e0 definition Equ.TransProp {El : Type} (Equ : EquType El) : Prop := ∀{e1 e2 e3 : El}, Equ e1 e2 → Equ e2 e3 → Equ e1 e3 definition Equ.SymProp {El : Type} (Equ : EquType El) : Prop := ∀{e1 e2 : El}, Equ e1 e2 → Equ e2 e1 /- - Definition of the type of categories (CatType) and the category of setoids (SetoidCat) -/ -- the type of setoids, objects of the category `Setoid` record SetoidType.{uEl} : Type.{uEl + 1} := (El : Type.{uEl}) (Equ : EquType El) (Refl : Equ.ReflProp Equ) (Trans : Equ.TransProp Equ) (Sym : Equ.SymProp Equ) print SetoidType abbreviation Setoid.MkOb := SetoidType.mk -- carrier of setoid attribute SetoidType.El [coercion] notation ` [` S `] ` := SetoidType.El S -- elements of `S` notation a ` ≡` S `≡ ` b :10 := SetoidType.Equ S a b -- `a=b` in `S` notation ` ⊜ ` := SetoidType.Refl _ notation ab ` ⊡` S `⊡ ` bc :100 := SetoidType.Trans S ab bc definition Equ.Closure {El : Type} : EquType El → EquType El := λ (rel : EquType El), λ (e1 e2 : El), ∀ (equ : EquType El), ∀ (refl : Equ.ReflProp equ)(trans : Equ.TransProp equ)(sym : Equ.SymProp equ), ∀ (impl : ∀ (ee1 ee2 : El), rel ee1 ee2 → equ ee1 ee2), equ e1 e2 definition Setoid.Closure {El : Type} : EquType El → SetoidType := λ (rel : EquType El), Setoid.MkOb El (Equ.Closure rel) ( λ (e0 : El), λ (equ : EquType El), λ (refl : Equ.ReflProp equ)(trans : Equ.TransProp equ)(sym : Equ.SymProp equ), λ (impl : ∀ (ee1 ee2 : El), rel ee1 ee2 → equ ee1 ee2), @refl e0) ( λ (e1 e2 e3 : El), λ (eq12 : Equ.Closure rel e1 e2), λ (eq23 : Equ.Closure rel e2 e3), λ (equ : EquType El), λ (refl : Equ.ReflProp equ)(trans : Equ.TransProp equ)(sym : Equ.SymProp equ), λ (impl : ∀ (ee1 ee2 : El), rel ee1 ee2 → equ ee1 ee2), @trans e1 e2 e3 (eq12 equ @refl @trans @sym impl) (eq23 equ @refl @trans @sym impl)) ( λ (e1 e2 : El), λ (eq12 : Equ.Closure rel e1 e2), λ (equ : EquType El), λ (refl : Equ.ReflProp equ)(trans : Equ.TransProp equ)(sym : Equ.SymProp equ), λ (impl : ∀ (ee1 ee2 : El), rel ee1 ee2 → equ ee1 ee2), @sym e1 e2 (eq12 equ @refl @trans @sym impl)) -- util definition Equ.Trans3Prop {El : Type} (Equ : EquType El) : Prop := ∀{e1 e2 e3 e4 : El}, Equ e1 e2 → Equ e2 e3 → Equ e3 e4 → Equ e1 e4 definition SetoidType.Trans3 (S : SetoidType) : Equ.Trans3Prop (SetoidType.Equ S) := λ e1 e2 e3 e4, λ eq12 eq23 eq34, eq12 ⊡S⊡ eq23 ⊡S⊡ eq34 -- morphisms in the category `Setoid` namespace Setoid record HomType (A : SetoidType) (B : SetoidType) : Type := (onEl : Π(a : A), B) (onEqu : ∀{a1 a2 : A}, (a1 ≡_≡ a2) → (onEl a1 ≡_≡ onEl a2)) print HomType abbreviation MkHom {A B : SetoidType} := @Setoid.HomType.mk A B end Setoid -- action on carrier attribute Setoid.HomType.onEl [coercion] infixl ` $ `:100 := Setoid.HomType.onEl attribute Setoid.HomType.onEqu [coercion] infixl ` $/ `:100 := Setoid.HomType.onEqu -- hom-sets in `Setoid` category definition Setoid.HomSet (A B : SetoidType) : SetoidType := Setoid.MkOb /- El-/ (Setoid.HomType A B) /- Equ-/ ( λ(f g : Setoid.HomType A B), ∀(a : A), (f a) ≡_≡ (g a)) /- Refl-/ ( λ f, λ a, ⊜) /- Trans -/ ( λ f g h, λ fg gh, λ a, (fg a) ⊡_⊡ (gh a)) /- Sym -/ ( λ f g, λ fg, λ a, SetoidType.Sym B (fg a)) definition app_set_hom_equ {A B : SetoidType} {f g : Setoid.HomType A B} (eq : ∀(a : A), (f a) ≡_≡ (g a)) (a : A) : (f a) ≡_≡ (g a) := eq a infixl ` /$ `:100 := app_set_hom_equ -- the dedicated arrow for morphisms of setoids infixr ` ⥤ `:10 := Setoid.HomSet namespace Setoid abbreviation MkHom2 (A B C : SetoidType) (onElEl : ∀(a : A), ∀(b : B), [C]) (onElEqu : ∀(a : A), ∀{b1 b2 : B}, ∀(e : b1 ≡_≡ b2), (onElEl a b1 ≡_≡ onElEl a b2)) (onEquEl : ∀{a1 a2 : A}, ∀(e : a1 ≡_≡ a2), ∀(b : B), (onElEl a1 b ≡_≡ onElEl a2 b)) : A ⥤ B ⥤ C := @MkHom A (B ⥤ C) ( λ (a : A), @MkHom B C (@onElEl a) (@onElEqu a)) @onEquEl definition Mul.onElEl {A B C : SetoidType} (f : B ⥤ C) (g : A ⥤ B) : A ⥤ C := MkHom ( λ (a : A), f $ (g $ a)) ( λ (a1 a2 : A), λ(a12 : a1 ≡_≡ a2), f $/ (g $/ a12) ) definition HomEquProp {A B : SetoidType} (f g : A ⥤ B) : Prop := f ≡(A ⥤ B)≡ g definition SubSingletonProp (S : SetoidType) : Prop := ∀(a b : S), a ≡_≡ b record SingletonType (S : SetoidType) : Type := (center : S) (connect : ∀(s : S), center ≡S≡ s) lemma SingletonIsSub {S : SetoidType} (ok : SingletonType S) : SubSingletonProp S := let okk := SingletonType.connect ok in λ a b, (SetoidType.Sym _ (okk a)) ⊡_⊡ ((okk b)) abbreviation MkSingleton {S : SetoidType} := @SingletonType.mk S definition FromType (T : Type) : SetoidType := Setoid.MkOb T ( λ x y, true) ( λ x, true.intro) ( λ x y z, λ xy yz, true.intro) ( λ x y, λ xy, true.intro) definition FromType.Singleton (T : Type) : Setoid.SubSingletonProp (FromType T) := λ x y, true.intro definition FromMap {T1 T2 : Type} (f : T1 → T2) : (FromType T1) ⥤ (FromType T2) := Setoid.MkHom f (λ x y, λ xy, true.intro) definition Const (X Y : SetoidType) (y : Y) : X ⥤ Y := Setoid.MkHom ( λ x, y ) ( λ x1 x2, λ e12, ⊜ ) end Setoid infixl `∙`: 100 := Setoid.Mul.onElEl infix `⥰`: 10 := Setoid.HomEquProp definition Prop.id {P : Prop} : (P → P) := λp, p definition Prop.mul {P Q R : Prop} (pq : P → Q) (qr : Q → R) : (P → R) := λp, qr (pq p) definition PropSet : SetoidType := Setoid.MkOb /- El -/ (Prop) /- Equ -/ ( λ(P Q : Prop), and (P → Q) (Q → P)) /- Refl -/ ( λ P, and.intro Prop.id Prop.id) /- Trans -/ ( λ P Q R, λ pq qr, and.intro (Prop.mul (and.left pq) (and.left qr)) (Prop.mul (and.right qr) (and.right pq))) /- Sym -/ ( λ P Q, λ pq, and.intro (and.right pq) (and.left pq)) -- TODO: Hom (pro)functor; -- TODO: Sigma: (B→Type) → (E→B), UnSigma: (E→B) → (B→Type) -- TODO: initial as limit, comma categories, cats of algebras -- TODO: adjunctions: by isomorphism of profunctors -- TODO: TT-like recursor, induction -- TODO: Id(Refl) coincide with SetoidType.Equ end EXE
9b72d39e04cd9403cc3a2b5d9554e624bfc9e420
618003631150032a5676f229d13a079ac875ff77
/src/algebra/opposites.lean
d06b45d4f8657aa220a78c8d08b9d7e2e166ada9
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,736
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Opposites. -/ import data.opposite import algebra.field namespace opposite universes u variables (α : Type u) instance [has_add α] : has_add (opposite α) := { add := λ x y, op (unop x + unop y) } instance [add_semigroup α] : add_semigroup (opposite α) := { add_assoc := λ x y z, unop_inj $ add_assoc (unop x) (unop y) (unop z), .. opposite.has_add α } instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup (opposite α) := { add_left_cancel := λ x y z H, unop_inj $ add_left_cancel $ op_inj H, .. opposite.add_semigroup α } instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup (opposite α) := { add_right_cancel := λ x y z H, unop_inj $ add_right_cancel $ op_inj H, .. opposite.add_semigroup α } instance [add_comm_semigroup α] : add_comm_semigroup (opposite α) := { add_comm := λ x y, unop_inj $ add_comm (unop x) (unop y), .. opposite.add_semigroup α } instance [has_zero α] : has_zero (opposite α) := { zero := op 0 } instance [add_monoid α] : add_monoid (opposite α) := { zero_add := λ x, unop_inj $ zero_add $ unop x, add_zero := λ x, unop_inj $ add_zero $ unop x, .. opposite.add_semigroup α, .. opposite.has_zero α } instance [add_comm_monoid α] : add_comm_monoid (opposite α) := { .. opposite.add_monoid α, .. opposite.add_comm_semigroup α } instance [has_neg α] : has_neg (opposite α) := { neg := λ x, op $ -(unop x) } instance [add_group α] : add_group (opposite α) := { add_left_neg := λ x, unop_inj $ add_left_neg $ unop x, .. opposite.add_monoid α, .. opposite.has_neg α } instance [add_comm_group α] : add_comm_group (opposite α) := { .. opposite.add_group α, .. opposite.add_comm_monoid α } instance [has_mul α] : has_mul (opposite α) := { mul := λ x y, op (unop y * unop x) } instance [semigroup α] : semigroup (opposite α) := { mul_assoc := λ x y z, unop_inj $ eq.symm $ mul_assoc (unop z) (unop y) (unop x), .. opposite.has_mul α } instance [right_cancel_semigroup α] : left_cancel_semigroup (opposite α) := { mul_left_cancel := λ x y z H, unop_inj $ mul_right_cancel $ op_inj H, .. opposite.semigroup α } instance [left_cancel_semigroup α] : right_cancel_semigroup (opposite α) := { mul_right_cancel := λ x y z H, unop_inj $ mul_left_cancel $ op_inj H, .. opposite.semigroup α } instance [comm_semigroup α] : comm_semigroup (opposite α) := { mul_comm := λ x y, unop_inj $ mul_comm (unop y) (unop x), .. opposite.semigroup α } instance [has_one α] : has_one (opposite α) := { one := op 1 } instance [monoid α] : monoid (opposite α) := { one_mul := λ x, unop_inj $ mul_one $ unop x, mul_one := λ x, unop_inj $ one_mul $ unop x, .. opposite.semigroup α, .. opposite.has_one α } instance [comm_monoid α] : comm_monoid (opposite α) := { .. opposite.monoid α, .. opposite.comm_semigroup α } instance [has_inv α] : has_inv (opposite α) := { inv := λ x, op $ (unop x)⁻¹ } instance [group α] : group (opposite α) := { mul_left_inv := λ x, unop_inj $ mul_inv_self $ unop x, .. opposite.monoid α, .. opposite.has_inv α } instance [comm_group α] : comm_group (opposite α) := { .. opposite.group α, .. opposite.comm_monoid α } instance [distrib α] : distrib (opposite α) := { left_distrib := λ x y z, unop_inj $ add_mul (unop y) (unop z) (unop x), right_distrib := λ x y z, unop_inj $ mul_add (unop z) (unop x) (unop y), .. opposite.has_add α, .. opposite.has_mul α } instance [semiring α] : semiring (opposite α) := { zero_mul := λ x, unop_inj $ mul_zero $ unop x, mul_zero := λ x, unop_inj $ zero_mul $ unop x, .. opposite.add_comm_monoid α, .. opposite.monoid α, .. opposite.distrib α } instance [ring α] : ring (opposite α) := { .. opposite.add_comm_group α, .. opposite.monoid α, .. opposite.semiring α } instance [comm_ring α] : comm_ring (opposite α) := { .. opposite.ring α, .. opposite.comm_semigroup α } instance [has_zero α] [has_one α] [nonzero α] : nonzero (opposite α) := { zero_ne_one := λ h : op (0 : α) = op 1, zero_ne_one (op_inj h) } instance [integral_domain α] : integral_domain (opposite α) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y (H : op (_ * _) = op (0:α)), or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ op_inj H) (λ hy, or.inr $ unop_inj $ hy) (λ hx, or.inl $ unop_inj $ hx), .. opposite.comm_ring α, .. opposite.nonzero α } instance [field α] : field (opposite α) := { mul_inv_cancel := λ x hx, unop_inj $ inv_mul_cancel $ λ hx', hx $ unop_inj hx', inv_zero := unop_inj inv_zero, .. opposite.comm_ring α, .. opposite.nonzero α, .. opposite.has_inv α } @[simp] lemma op_zero [has_zero α] : op (0 : α) = 0 := rfl @[simp] lemma unop_zero [has_zero α] : unop (0 : αᵒᵖ) = 0 := rfl @[simp] lemma op_one [has_one α] : op (1 : α) = 1 := rfl @[simp] lemma unop_one [has_one α] : unop (1 : αᵒᵖ) = 1 := rfl variable {α} @[simp] lemma op_add [has_add α] (x y : α) : op (x + y) = op x + op y := rfl @[simp] lemma unop_add [has_add α] (x y : αᵒᵖ) : unop (x + y) = unop x + unop y := rfl @[simp] lemma op_neg [has_neg α] (x : α) : op (-x) = -op x := rfl @[simp] lemma unop_neg [has_neg α] (x : αᵒᵖ) : unop (-x) = -unop x := rfl @[simp] lemma op_mul [has_mul α] (x y : α) : op (x * y) = op y * op x := rfl @[simp] lemma unop_mul [has_mul α] (x y : αᵒᵖ) : unop (x * y) = unop y * unop x := rfl @[simp] lemma op_inv [has_inv α] (x : α) : op (x⁻¹) = (op x)⁻¹ := rfl @[simp] lemma unop_inv [has_inv α] (x : αᵒᵖ) : unop (x⁻¹) = (unop x)⁻¹ := rfl end opposite
0a8ac773065ce669d70a9cf52fa0b5588980e2c8
ed27983dd289b3bcad416f0b1927105d6ef19db8
/src/assignments/assignment_4/assignment_4.lean
1d9d6eb837a7290350f7b1171cccabbb996e1ef3
[]
no_license
liuxin-James/complogic-s21
0d55b76dbe25024473d31d98b5b83655c365f811
13e03e0114626643b44015c654151fb651603486
refs/heads/master
1,681,109,264,463
1,618,848,261,000
1,618,848,261,000
337,599,491
0
0
null
1,613,141,619,000
1,612,925,555,000
null
UTF-8
Lean
false
false
8,850
lean
/- 1. Write a polymorphic function, someSatisfies, that takes a a predicate function, p, of type α → bool, and list of values of type α (a type parameter), and and that returns true (tt) if and only if there is some value, v, in the list, for which (p v) is true (tt). Your implementation must use list_map to convert the given list of α values to a list of bool values, which it must then pass to a helper function, the job of which is to return true (tt) if and only there is some tt value in the list. -/ universe u def isEqN: ℕ → (ℕ → bool) := λ n, λ m, n=m def list_map {α : Type u} : (α → bool) → list α → list bool | f list.nil := list.nil | f (h::t) := (f h)::(list_map f t) def anytt: list bool → bool | list.nil := ff | (h::t) := bor h (anytt t) def someSatisfies {α : Type u} (p: α → bool) (l : list α): bool := anytt (list_map p l) #eval someSatisfies (isEqN 4) [1,2,3,4] #eval someSatisfies (isEqN 4) [1,2,3] /- 2. Write a polymorphic function, allSatisfy, that takes a predicate function, p, of type α → bool, and list of values of type α (a type parameter), and and that returns true (tt) if and only if for every value, v, in the list, (p v) is true (tt). Your implementation must use list_map to convert the given list of α values to a list of bool values, which it must then pass to a helper function, the job of which is to return true (tt) if and only every value in the list is tt. -/ def alltt: list bool → bool | list.nil := tt | (h::t) := band h (alltt t) def allSatisfy {α : Type u} (p: α → bool) (l : list α): bool := alltt (list_map p l) #eval allSatisfy (isEqN 4) [1,2,3,4] #eval allSatisfy (isEqN 4) [4,4,4,4] /- 3. Write a function called simple_fold_list. It has a type parameter, α, and takes (1) a binary function, f, of type α → α → α, (2) a single value, i, of type α, as a list, l, of type list α The purpose of simple_fold_list is to "reduce" a list to a single value by (1) returning i for the empty list, otherwise (2) returning the result of applying f to the head of the list and the reduction of the rest of the list. Here are two examples: simple_fold_list nat.add 0 [1,2,3,4,5] = 15 simple_fold_list nat.mul 1 [1,2,3,4,5] = 120 -/ def simple_fold_list { α : Type u} : ( α → α → α ) → α → list α → α | f id list.nil := id | f id (h::t) := f h (simple_fold_list f id t) #eval simple_fold_list nat.add 0 [1,2,3,4,5] #eval simple_fold_list nat.mul 1 [1,2,3,4,5] /- 4. Write an application of simple_fold_list to reduce a list of strings to a single string in which all the individual lists are appended to each other. You can use ++ (or string.append) to append strings without writing your own function to do so. For example, reduce ["Hello", " ", "Lean!"] to "Hello, Lean!" -/ #eval simple_fold_list (++) "" ["Hello", " ", "Lean!"] /- 5. Re-implement here your helper functions from questions 1 and 2 using simple_fold_list. -/ def anytt' (l : list bool): bool := simple_fold_list (bor) ff l #eval anytt' [tt, tt, ff] #eval anytt' [ff, ff, ff] def alltt' (l : list bool): bool := simple_fold_list (band) tt l #eval alltt' [tt, tt, tt] #eval alltt' [ff, ff, ff] /- 6. This question asks you to understand how to write inductive families in a slightly different way than we've seen. Here's the definition of an inductive family, ev, indexed by natural numbers. Read the definition. We explain it further below. -/ inductive ev : ℕ → Type | ev_base : ev 0 | ev_ind {n : nat} (evn : ev n) : ev (n + 2) open ev #check ev_base #check ev_ind ev_base #check ev_ind (ev_ind ev_base) /- The first line of this definition explains that ev is a family of types indexed by values of type nat. In other words, for every nat, n, there is a corresponding type, ev n. In particular, there are types, ev 0, ev 1, ev 2, ev 3, etc. The next two lines, the constructors, explain how values of all of these types can be constructed. The first constructor, ev_base, is declared to be a term (value) of type ev 0. You can think of this term as being our version of "evidence" that "zero is even". The second constructor takes two arguments. The first implcit argument is a natural number, n. The second, evn, must be a term of type (ev n), *for that particular n* (dependent typing here). Finally, focus on this, a term (ev_ind n evn) is of type (ev (n+2)). You can think of such a term as being our form of evidence that n+2 is even. -/ /- 6A. Complete the following definitions by filling in the placeholders with terms of the indicated dependent types. Use the available constructors to create these values. (Remember that the first argument to ev_ind is impliict, and note that it can be inferred from the second argument.) -/ def ev0 : ev 0 := ev_base def ev2 : ev 2 := ev_ind ev_base def ev4 : ev 4 := ev_ind (ev_ind ev_base) def ev6 : ev 6 := ev_ind (ev_ind (ev_ind ev_base)) def ev8 : ev 8 := ev_ind (ev_ind (ev_ind (ev_ind ev_base))) def ev10 : ev 10 := ev_ind (ev_ind (ev_ind (ev_ind (ev_ind ev_base)))) #check ev0 #check ev10 /- 6B. You should have been able to give values for each of the preceding six types ev 0, ..., ev 10. What single word can you use to indate that each of these types has at least one value? -/ /- 6C. Try to give values for each of the types in the following definitions. Explain as clearly and in as few words as you can why you won't be able to do this, and state the word that best describes each of these types, in relation to the fact that these types have no values. -/ def ev1 : ev 1 := ev_ind (ev _) def ev3 : ev 3 := ev_ind ev1 def ev5 : ev 5 := ev_ind ev3 --Answer: since the ev function only works for even numbers, if n equals to odd numbers, the ev function cannot use any constructor to return the values of these types. /- 6D. Define an inductive family, odd n, indexed by natural numbers, such that the type for every odd number has at least one value, and the types for even numbers have no values. Then show that you can complete the preceding three definitions if you replace ev by odd. -/ inductive odd : ℕ → Type | odd_base : odd 1 | odd_ind {n : nat} (oddn : odd n) : odd (n + 2) open odd <<<<<<< HEAD def odd1 : odd 1 := odd_base def odd3 : odd 3 := odd_ind odd_base def odd5 : odd 5 := odd_ind (odd_ind odd_base) ======= inductive empty' : Type >>>>>>> upstream/master /- 7. As you know, the type, empty, is uninhabited. That is, it has no values. So what does it tell us if we can define a function of a type that "returns a value of type empty?" Show that there is a function, let's call it foo, of type ev 1 → empty. Then show there's a function, let's call it bar, of type ev 3 → empty. Finally, show that there's a function, baz, of type ev 5 → empty. (NB: To show that there is a function of a given type, you must write some (any) function of that type. What it actually does doesn't matter.) Once you've done the preceding exercises, write a short answer (in English) to the question at the beginning of this problem. -/ def foo : ev 1 → empty := <<<<<<< HEAD fun (e : ev 1), match e with end #check foo def bar : ev 3 → empty := fun (e : ev 3), match e with end #check bar def baz : ev 5 → empty := fun (e : ev 5), match e with end #check baz --what does it tell us if we can define a function of a type that "returns a value of type empty? --Answer: It means that this type has no values. ======= λ (e : ev 1), _ -- match ... with ... end -- for every possible form of e, return a value of type empty >>>>>>> upstream/master /- 8. Define evdp to be a sigma (dependent pair) type, a value of which has a natural number, n, as its first component, and a value of type, ev n, as its second. Then define evp0, evp2, and evp4 to be values of this type, whose first elements are, respectively, 0, 2, and 4. -/ <<<<<<< HEAD def evdp := Σ (n : nat), ev n def evp0 : evdp := ⟨ 0, ev0 ⟩ def evp2 : evdp := ⟨ 2, ev2 ⟩ def evp4 : evdp := ⟨ 4, ev4 ⟩ #reduce evp2 #check evp0 #check evp2 #check evp4 ======= -- hint #check Σ (n : nat), ev n >>>>>>> upstream/master -- Your answers here /- 9. Write a function, mkEvp, that takes <<<<<<< HEAD a argument, n, of type nat, implicitly, and an argument, nEv of type, ev n, and that ======= a argument, mn, of type nat, iplicitly, and an argument, nEv ot type, ev n, and that >>>>>>> upstream/master returns a value of type evdp (from the last problem). Then briefly answer the question, in what sense does mkEvp have a dependent function type? -/ def ntoev : Π (n : nat), ev n | 0 := ev_base | (n' + 2) := ev_ind (ntoev n') def mkEvp {n : ℕ} (nEv : ev n): evdp := ⟨n, ntoev n⟩ <<<<<<< HEAD #reduce mkEvp ev2 #check mkEvp ev2 -- Your answers here ======= -- Your answers here >>>>>>> upstream/master
46d045f66d61d1078e232a7b2b0003c8f9cd7410
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/algebra/category/Group.lean
99715e0aa9b014c64671029ca1fcb3825f09f63b
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
2,166
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.punit_instances import algebra.category.Mon.basic /-! # Category instances for group, add_group, comm_group, and add_comm_group. We introduce the bundled categories: * `Group` * `AddGroup` * `CommGroup` * `AddCommGroup` along with the relevant forgetful functors between them, and to the bundled monoid categories. -/ universes u v open category_theory /-- The category of groups and group morphisms. -/ @[reducible, to_additive AddGroup] def Group : Type (u+1) := bundled group namespace Group @[to_additive add_group] instance (G : Group) : group G := G.str /-- Construct a bundled Group from the underlying type and typeclass. -/ @[to_additive] def of (X : Type u) [group X] : Group := bundled.of X @[to_additive] instance bundled_hom : bundled_hom _ := Mon.bundled_hom.full_subcategory @group.to_monoid @[to_additive] instance : has_one Group := ⟨Group.of punit⟩ @[to_additive has_forget_to_AddMon] instance has_forget_to_Mon : has_forget₂ Group.{u} Mon.{u} := Mon.bundled_hom.full_subcategory_has_forget₂ _ end Group /-- The category of commutative groups and group morphisms. -/ @[reducible, to_additive AddCommGroup] def CommGroup : Type (u+1) := bundled comm_group namespace CommGroup @[to_additive add_comm_group] instance (G : CommGroup) : comm_group G := G.str /-- Construct a bundled CommGroup from the underlying type and typeclass. -/ @[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G @[to_additive] instance : bundled_hom _ := Group.bundled_hom.full_subcategory @comm_group.to_group @[to_additive has_forget_to_AddGroup] instance has_forget_to_Group : has_forget₂ CommGroup.{u} Group.{u} := Group.bundled_hom.full_subcategory_has_forget₂ _ @[to_additive has_forget_to_AddCommMon] instance has_forget_to_CommMon : has_forget₂ CommGroup.{u} CommMon.{u} := CommMon.bundled_hom.full_subcategory_has_forget₂ comm_group.to_comm_monoid @[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩ end CommGroup
4c341744ec51fc842a7e53d207de4fc1bd62fb6b
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/interactive/t4.lean
7f73e61272895927c1894e5c74a47171fb15e1ae
[ "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
37
lean
theorem (a : Bool) : a. print "done".
21640272ddcad18c2ba25fbc3081bd77f56ca80e
618003631150032a5676f229d13a079ac875ff77
/src/data/list/range.lean
b41cfc89d27f2d2f397cce2ccbcd5b73f0622a6f
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
8,381
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Scott Morrison -/ import data.list.chain import data.list.nodup import data.list.of_fn open nat namespace list /- iota and range(') -/ universe u variables {α : Type u} @[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n | s 0 := rfl | s (n+1) := congr_arg succ (length_range' _ _) @[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n | s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1 | s (succ n) := have m = s → m < s + n + 1, from λ e, e ▸ lt_succ_of_le (le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@le_iff_eq_or_lt _ _ s m).symm, (mem_cons_iff _ _ _).trans $ by simp only [mem_range', or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; refl theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n | s 0 := rfl | s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n) theorem map_sub_range' (a) : ∀ (s n : ℕ) (h : a ≤ s), map (λ x, x - a) (range' s n) = range' (s - a) n | s 0 _ := rfl | s (n+1) h := begin convert congr_arg (cons (s-a)) (map_sub_range' (s+1) n (nat.le_succ_of_le h)), rw nat.succ_sub h, refl, end theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n) | s 0 := chain.nil | s (n+1) := (chain_succ_range' (s+1) n).cons rfl theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) := (chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _) theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n) | s 0 := pairwise.nil | s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) @[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m) | s 0 n := rfl | s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m), by rw [add_right_comm, range'_append] theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := ⟨λ h, by simpa only [length_range'] using length_le_of_sublist h, λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, (range'_sublist_right.2 h).subset⟩ theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m) | s 0 (n+1) _ := rfl | s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $ by rw add_right_comm; refl theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] := by rw add_comm n 1; exact (range'_append s n 1).symm theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s) | 0 n := rfl | (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1]; exact range_core_range' s (n+1) theorem range_eq_range' (n : ℕ) : range n = range' 0 n := (range_core_range' n 0).trans $ by rw zero_add theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) := by rw [range_eq_range', range_eq_range', range', add_comm, ← map_add_range']; congr; exact funext one_add theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) := by rw [range_eq_range', map_add_range']; refl @[simp] theorem length_range (n : ℕ) : length (range n) = n := by simp only [range_eq_range', length_range'] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp only [range_eq_range', pairwise_lt_range'] theorem nodup_range (n : ℕ) : nodup (range n) := by simp only [range_eq_range', nodup_range'] theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n := by simp only [range_eq_range', range'_sublist_right] theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := by simp only [range_eq_range', range'_subset_right] @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add] @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := mt mem_range.1 $ lt_irrefl _ @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := by simp only [succ_pos', lt_add_iff_pos_right, mem_range] theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m := by simp only [range_eq_range', nth_range' _ h, zero_add] theorem range_concat (n : ℕ) : range (succ n) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n) | 0 := rfl | (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n, reverse_append, add_comm]; refl @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := by simp only [iota_eq_reverse_range', length_reverse, length_range'] theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) := by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range'] theorem nodup_iota (n : ℕ) : nodup (iota n) := by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range'] theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff] theorem reverse_range' : ∀ s n : ℕ, reverse (range' s n) = map (λ i, s + n - 1 - i) (range n) | s 0 := rfl | s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map]; simpa only [show s + (n + 1) - 1 = s + n, from rfl, (∘), λ a i, show a - 1 - i = a - succ i, from pred_sub _ _, reverse_singleton, map_cons, nat.sub_zero, cons_append, nil_append, eq_self_iff_true, true_and, map_map] using reverse_range' s n /-- All elements of `fin n`, from `0` to `n-1`. -/ def fin_range (n : ℕ) : list (fin n) := (range n).pmap fin.mk (λ _, list.mem_range.1) @[simp] lemma mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n := mem_pmap.2 ⟨a.1, mem_range.2 a.2, fin.eta _ _⟩ lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup := nodup_pmap (λ _ _ _ _, fin.veq_of_eq) (nodup_range _) @[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n := by rw [fin_range, length_pmap, length_range] @[to_additive] theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = ((range n).map f).prod * f n := by rw [range_concat, map_append, map_singleton, prod_append, prod_cons, prod_nil, mul_one] /-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the last.-/ @[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum rather than the last."] theorem prod_range_succ' {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = f 0 * ((range n).map (λ i, f (succ i))).prod := nat.rec_on n (show 1 * f 0 = f 0 * 1, by rw [one_mul, mul_one]) (λ _ hd, by rw [list.prod_range_succ, hd, mul_assoc, ←list.prod_range_succ]) @[simp] theorem enum_from_map_fst : ∀ n (l : list α), map prod.fst (enum_from n l) = range' n l.length | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _) @[simp] theorem enum_map_fst (l : list α) : map prod.fst (enum l) = range l.length := by simp only [enum, enum_from_map_fst, range_eq_range'] @[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) : nth_le (range n) i H = i := option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)] theorem of_fn_eq_pmap {α n} {f : fin n → α} : of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) := by rw [pmap_eq_map_attach]; from ext_le (by simp) (λ i hi1 hi2, by simp at hi1; simp [nth_le_of_fn f ⟨i, hi1⟩]) theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := by rw of_fn_eq_pmap; from nodup_pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n) end list
3d3652dabcf2643dad7a3ae8890e7ede0f0c3f1b
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/list/nat_antidiagonal.lean
de578eb557c0d7b3e544be1be1800d03893a8eea
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
3,214
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 data.list.nodup import data.list.range /-! # Antidiagonals in ℕ × ℕ as lists This file defines the antidiagonals of ℕ × ℕ as lists: the `n`-th antidiagonal is the list of pairs `(i, j)` such that `i + j = n`. This is useful for polynomial multiplication and more generally for sums going from `0` to `n`. ## Notes Files `data.multiset.nat_antidiagonal` and `data.finset.nat_antidiagonal` successively turn the `list` definition we have here into `multiset` and `finset`. -/ open list function nat namespace list namespace nat /-- The antidiagonal of a natural number `n` is the list of pairs `(i, j)` such that `i + j = n`. -/ def antidiagonal (n : ℕ) : list (ℕ × ℕ) := (range (n+1)).map (λ i, (i, n - i)) /-- A pair (i, j) is contained in the antidiagonal of `n` if and only if `i + j = n`. -/ @[simp] lemma mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} : x ∈ antidiagonal n ↔ x.1 + x.2 = n := begin rw [antidiagonal, mem_map], split, { rintros ⟨i, hi, rfl⟩, rw [mem_range, lt_succ_iff] at hi, exact add_tsub_cancel_of_le hi }, { rintro rfl, refine ⟨x.fst, _, _⟩, { rw [mem_range, add_assoc, lt_add_iff_pos_right], exact zero_lt_succ _ }, { exact prod.ext rfl (add_tsub_cancel_left _ _) } } end /-- The length of the antidiagonal of `n` is `n + 1`. -/ @[simp] lemma length_antidiagonal (n : ℕ) : (antidiagonal n).length = n+1 := by rw [antidiagonal, length_map, length_range] /-- The antidiagonal of `0` is the list `[(0, 0)]` -/ @[simp] lemma antidiagonal_zero : antidiagonal 0 = [(0, 0)] := rfl /-- The antidiagonal of `n` does not contain duplicate entries. -/ lemma nodup_antidiagonal (n : ℕ) : nodup (antidiagonal n) := (nodup_range _).map (@left_inverse.injective ℕ (ℕ × ℕ) prod.fst (λ i, (i, n-i)) $ λ i, rfl) @[simp] lemma antidiagonal_succ {n : ℕ} : antidiagonal (n + 1) = (0, n + 1) :: ((antidiagonal n).map (prod.map nat.succ id)) := begin simp only [antidiagonal, range_succ_eq_map, map_cons, true_and, nat.add_succ_sub_one, add_zero, id.def, eq_self_iff_true, tsub_zero, map_map, prod.map_mk], apply congr (congr rfl _) rfl, ext; simp, end lemma antidiagonal_succ' {n : ℕ} : antidiagonal (n + 1) = ((antidiagonal n).map (prod.map id nat.succ)) ++ [(n + 1, 0)] := begin simp only [antidiagonal, range_succ, add_tsub_cancel_left, map_append, append_assoc, tsub_self, singleton_append, map_map, map], congr' 1, apply map_congr, simp [le_of_lt, nat.succ_eq_add_one, nat.sub_add_comm] { contextual := tt }, end lemma antidiagonal_succ_succ' {n : ℕ} : antidiagonal (n + 2) = (0, n + 2) :: ((antidiagonal n).map (prod.map nat.succ nat.succ)) ++ [(n + 2, 0)] := by { rw antidiagonal_succ', simpa } lemma map_swap_antidiagonal {n : ℕ} : (antidiagonal n).map prod.swap = (antidiagonal n).reverse := begin rw [antidiagonal, map_map, prod.swap, ← list.map_reverse, range_eq_range', reverse_range', ← range_eq_range', map_map], apply map_congr, simp [nat.sub_sub_self, lt_succ_iff] { contextual := tt }, end end nat end list
0d9f24bcd8d5e10689d8f1426a084359df1b29f6
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Parser/Command.lean
d59e675476566582d862d858fbe6599b7b3b8df0
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,202
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, Sebastian Ullrich -/ import Lean.Parser.Term import Lean.Parser.Do namespace Lean namespace Parser /-- Syntax quotation for terms and (lists of) commands. We prefer terms, so ambiguous quotations like `($x $y) will be parsed as an application, not two commands. Use `($x:command $y:command) instead. Multiple command will be put in a `null node, but a single command will not (so that you can directly match against a quotation in a command kind's elaborator). -/ -- TODO: use two separate quotation parsers with parser priorities instead @[builtinTermParser] def Term.quot := parser! "`(" >> toggleInsideQuot (termParser <|> many1Unbox commandParser) >> ")" namespace Command def namedPrio := parser! (atomic ("(" >> nonReservedSymbol "priority") >> " := " >> priorityParser >> ")") def optNamedPrio := optional namedPrio def «private» := parser! "private " def «protected» := parser! "protected " def visibility := «private» <|> «protected» def «noncomputable» := parser! "noncomputable " def «unsafe» := parser! "unsafe " def «partial» := parser! "partial " def declModifiers (inline : Bool) := parser! optional docComment >> optional (Term.«attributes» >> if inline then skip else ppDedent ppLine) >> optional visibility >> optional «noncomputable» >> optional «unsafe» >> optional «partial» def declId := parser! ident >> optional (".{" >> sepBy1 ident ", " >> "}") def declSig := parser! many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.typeSpec def optDeclSig := parser! many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.optType def declValSimple := parser! " :=\n" >> termParser >> optional Term.whereDecls def declValEqns := parser! Term.matchAltsWhereDecls def declVal := declValSimple <|> declValEqns <|> Term.whereDecls def «abbrev» := parser! "abbrev " >> declId >> optDeclSig >> declVal def «def» := parser! "def " >> declId >> optDeclSig >> declVal def «theorem» := parser! "theorem " >> declId >> declSig >> declVal def «constant» := parser! "constant " >> declId >> declSig >> optional declValSimple def «instance» := parser! Term.attrKind >> "instance " >> optNamedPrio >> optional declId >> declSig >> declVal def «axiom» := parser! "axiom " >> declId >> declSig def «example» := parser! "example " >> declSig >> declVal def inferMod := parser! atomic (symbol "{" >> "}") def ctor := parser! "\n| " >> declModifiers true >> ident >> optional inferMod >> optDeclSig def optDeriving := parser! optional (atomic ("deriving " >> notSymbol "instance") >> sepBy1 ident ", ") def «inductive» := parser! "inductive " >> declId >> optDeclSig >> optional (symbol ":=" <|> "where") >> many ctor >> optDeriving def classInductive := parser! atomic (group (symbol "class " >> "inductive ")) >> declId >> optDeclSig >> optional (symbol ":=" <|> "where") >> many ctor >> optDeriving def structExplicitBinder := parser! atomic (declModifiers true >> "(") >> many1 ident >> optional inferMod >> optDeclSig >> optional Term.binderDefault >> ")" def structImplicitBinder := parser! atomic (declModifiers true >> "{") >> many1 ident >> optional inferMod >> declSig >> "}" def structInstBinder := parser! atomic (declModifiers true >> "[") >> many1 ident >> optional inferMod >> declSig >> "]" def structSimpleBinder := parser! atomic (declModifiers true >> ident) >> optional inferMod >> optDeclSig >> optional Term.binderDefault def structFields := parser! manyIndent (ppLine >> checkColGe >>(structExplicitBinder <|> structImplicitBinder <|> structInstBinder <|> structSimpleBinder)) def structCtor := parser! atomic (declModifiers true >> ident >> optional inferMod >> " :: ") def structureTk := parser! "structure " def classTk := parser! "class " def «extends» := parser! " extends " >> sepBy1 termParser ", " def «structure» := parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> optional ((symbol " := " <|> " where ") >> optional structCtor >> structFields) >> optDeriving @[builtinCommandParser] def declaration := parser! declModifiers false >> («abbrev» <|> «def» <|> «theorem» <|> «constant» <|> «instance» <|> «axiom» <|> «example» <|> «inductive» <|> classInductive <|> «structure») @[builtinCommandParser] def «deriving» := parser! "deriving " >> "instance " >> sepBy1 ident ", " >> " for " >> sepBy1 ident ", " @[builtinCommandParser] def «section» := parser! "section " >> optional ident @[builtinCommandParser] def «namespace» := parser! "namespace " >> ident @[builtinCommandParser] def «end» := parser! "end " >> optional ident @[builtinCommandParser] def «variable» := parser! "variable" >> Term.bracketedBinder @[builtinCommandParser] def «variables» := parser! "variables" >> many1 Term.bracketedBinder @[builtinCommandParser] def «universe» := parser! "universe " >> ident @[builtinCommandParser] def «universes» := parser! "universes " >> many1 ident @[builtinCommandParser] def check := parser! "#check " >> termParser @[builtinCommandParser] def check_failure := parser! "#check_failure " >> termParser -- Like `#check`, but succeeds only if term does not type check @[builtinCommandParser] def reduce := parser! "#reduce " >> termParser @[builtinCommandParser] def eval := parser! "#eval " >> termParser @[builtinCommandParser] def synth := parser! "#synth " >> termParser @[builtinCommandParser] def exit := parser! "#exit" @[builtinCommandParser] def print := parser! "#print " >> (ident <|> strLit) @[builtinCommandParser] def printAxioms := parser! "#print " >> nonReservedSymbol "axioms " >> ident @[builtinCommandParser] def «resolve_name» := parser! "#resolve_name " >> ident @[builtinCommandParser] def «init_quot» := parser! "init_quot" @[builtinCommandParser] def «set_option» := parser! "set_option " >> ident >> ppSpace >> (nonReservedSymbol "true" <|> nonReservedSymbol "false" <|> strLit <|> numLit) @[builtinCommandParser] def «attribute» := parser! "attribute " >> "[" >> sepBy1 Term.attrInstance ", " >> "] " >> many1 ident @[builtinCommandParser] def «export» := parser! "export " >> ident >> "(" >> many1 ident >> ")" def openHiding := parser! atomic (ident >> "hiding") >> many1 ident def openRenamingItem := parser! ident >> unicodeSymbol "→" "->" >> ident def openRenaming := parser! atomic (ident >> "renaming") >> sepBy1 openRenamingItem ", " def openOnly := parser! atomic (ident >> "(") >> many1 ident >> ")" def openSimple := parser! many1 ident @[builtinCommandParser] def «open» := parser! "open " >> (openHiding <|> openRenaming <|> openOnly <|> openSimple) @[builtinCommandParser] def «mutual» := parser! "mutual " >> many1 (ppLine >> notSymbol "end" >> commandParser) >> ppDedent (ppLine >> "end") @[builtinCommandParser] def «initialize» := parser! "initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «builtin_initialize» := parser! "builtin_initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «in» := tparser! " in " >> commandParser @[runBuiltinParserAttributeHooks] abbrev declModifiersF := declModifiers false @[runBuiltinParserAttributeHooks] abbrev declModifiersT := declModifiers true builtin_initialize registerParserAlias! "declModifiers" declModifiersF registerParserAlias! "nestedDeclModifiers" declModifiersT registerParserAlias! "declId" declId registerParserAlias! "declSig" declSig registerParserAlias! "optDeclSig" optDeclSig end Command end Parser end Lean
99afe0709e2dfd0037eaef7c9ee48db5957a4b31
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/topology/algebra/floor_ring.lean
02f1195f01b301423498b5eaefa02247427cf872
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,813
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import algebra.floor import topology.algebra.ordered.basic /-! # Topological facts about `int.floor`, `int.ceil` and `int.fract` This file proves statements about limits and continuity of functions involving `floor`, `ceil` and `fract`. ## Main declarations * `tendsto_floor_at_top`, `tendsto_floor_at_bot`, `tendsto_ceil_at_top`, `tendsto_ceil_at_bot`: `int.floor` and `int.ceil` tend to +-∞ in +-∞. * `continuous_on_floor`: `int.floor` is continuous on `Ico n (n + 1)`, because constant. * `continuous_on_ceil`: `int.ceil` is continuous on `Ioc n (n + 1)`, because constant. * `continuous_on_fract`: `int.fract` is continuous on `Ico n (n + 1)`. * `continuous_on.comp_fract`: Precomposing a continuous function satisfying `f 0 = f 1` with `int.fract` yields another continuous function. -/ open filter function int set open_locale topological_space variables {α : Type*} [linear_ordered_ring α] [floor_ring α] lemma tendsto_floor_at_top : tendsto (floor : α → ℤ) at_top at_top := floor_mono.tendsto_at_top_at_top $ λ b, ⟨(b + 1 : ℤ), by { rw floor_coe, exact (lt_add_one _).le }⟩ lemma tendsto_floor_at_bot : tendsto (floor : α → ℤ) at_bot at_bot := floor_mono.tendsto_at_bot_at_bot $ λ b, ⟨b, (floor_coe _).le⟩ lemma tendsto_ceil_at_top : tendsto (ceil : α → ℤ) at_top at_top := ceil_mono.tendsto_at_top_at_top $ λ b, ⟨b, (ceil_coe _).ge⟩ lemma tendsto_ceil_at_bot : tendsto (ceil : α → ℤ) at_bot at_bot := ceil_mono.tendsto_at_bot_at_bot $ λ b, ⟨(b - 1 : ℤ), by { rw ceil_coe, exact (sub_one_lt _).le }⟩ variables [topological_space α] lemma continuous_on_floor (n : ℤ) : continuous_on (λ x, floor x : α → α) (Ico n (n+1) : set α) := (continuous_on_congr $ floor_eq_on_Ico' n).mpr continuous_on_const lemma continuous_on_ceil (n : ℤ) : continuous_on (λ x, ceil x : α → α) (Ioc (n-1) n : set α) := (continuous_on_congr $ ceil_eq_on_Ioc' n).mpr continuous_on_const lemma tendsto_floor_right' [order_closed_topology α] (n : ℤ) : tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝 n) := begin rw ← nhds_within_Ico_eq_nhds_within_Ici (lt_add_one (n : α)), simpa only [floor_coe] using (continuous_on_floor n _ (left_mem_Ico.mpr $ lt_add_one (_ : α))).tendsto end lemma tendsto_ceil_left' [order_closed_topology α] (n : ℤ) : tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝 n) := begin rw ← nhds_within_Ioc_eq_nhds_within_Iic (sub_one_lt (n : α)), simpa only [ceil_coe] using (continuous_on_ceil _ _ (right_mem_Ioc.mpr $ sub_one_lt (_ : α))).tendsto end lemma tendsto_floor_right [order_closed_topology α] (n : ℤ) : tendsto (λ x, floor x : α → α) (𝓝[Ici n] n) (𝓝[Ici n] n) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_floor_right' _) begin refine (eventually_nhds_with_of_forall $ λ x (hx : (n : α) ≤ x), _), change _ ≤ _, norm_cast, convert ← floor_mono hx, rw floor_eq_iff, exact ⟨le_refl _, lt_add_one _⟩ end lemma tendsto_ceil_left [order_closed_topology α] (n : ℤ) : tendsto (λ x, ceil x : α → α) (𝓝[Iic n] n) (𝓝[Iic n] n) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_ceil_left' _) begin refine (eventually_nhds_with_of_forall $ λ x (hx : x ≤ (n : α)), _), change _ ≤ _, norm_cast, convert ← ceil_mono hx, rw ceil_eq_iff, exact ⟨sub_one_lt _, le_refl _⟩ end lemma tendsto_floor_left [order_closed_topology α] (n : ℤ) : tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝[Iic (n-1)] (n-1)) := begin rw ← nhds_within_Ico_eq_nhds_within_Iio (sub_one_lt (n : α)), convert (tendsto_nhds_within_congr $ (λ x hx, (floor_eq_on_Ico' (n-1) x hx).symm)) (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds (eventually_of_forall (λ _, mem_Iic.mpr $ le_refl _))); norm_cast <|> apply_instance, ring end lemma tendsto_ceil_right [order_closed_topology α] (n : ℤ) : tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝[Ici (n+1)] (n+1)) := begin rw ← nhds_within_Ioc_eq_nhds_within_Ioi (lt_add_one (n : α)), convert (tendsto_nhds_within_congr $ (λ x hx, (ceil_eq_on_Ioc' (n+1) x hx).symm)) (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds (eventually_of_forall (λ _, mem_Ici.mpr $ le_refl _))); norm_cast <|> apply_instance, ring end lemma tendsto_floor_left' [order_closed_topology α] (n : ℤ) : tendsto (λ x, floor x : α → α) (𝓝[Iio n] n) (𝓝 (n-1)) := begin rw ← nhds_within_univ, exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_floor_left n), end lemma tendsto_ceil_right' [order_closed_topology α] (n : ℤ) : tendsto (λ x, ceil x : α → α) (𝓝[Ioi n] n) (𝓝 (n+1)) := begin rw ← nhds_within_univ, exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_ceil_right n), end lemma continuous_on_fract [topological_add_group α] (n : ℤ) : continuous_on (fract : α → α) (Ico n (n+1) : set α) := continuous_on_id.sub (continuous_on_floor n) lemma tendsto_fract_left' [order_closed_topology α] [topological_add_group α] (n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝 1) := begin convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_left' n); [{norm_cast, ring}, apply_instance, apply_instance] end lemma tendsto_fract_left [order_closed_topology α] [topological_add_group α] (n : ℤ) : tendsto (fract : α → α) (𝓝[Iio n] n) (𝓝[Iio 1] 1) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_fract_left' _) (eventually_of_forall fract_lt_one) lemma tendsto_fract_right' [order_closed_topology α] [topological_add_group α] (n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝 0) := begin convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_right' n); [exact (sub_self _).symm, apply_instance, apply_instance] end lemma tendsto_fract_right [order_closed_topology α] [topological_add_group α] (n : ℤ) : tendsto (fract : α → α) (𝓝[Ici n] n) (𝓝[Ici 0] 0) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_fract_right' _) (eventually_of_forall fract_nonneg) local notation `I` := (Icc 0 1 : set α) lemma continuous_on.comp_fract' {β γ : Type*} [order_topology α] [topological_add_group α] [topological_space β] [topological_space γ] {f : β → α → γ} (h : continuous_on (uncurry f) $ (univ : set β).prod I) (hf : ∀ s, f s 0 = f s 1) : continuous (λ st : β × α, f st.1 $ fract st.2) := begin change continuous ((uncurry f) ∘ (prod.map id (fract))), rw continuous_iff_continuous_at, rintro ⟨s, t⟩, by_cases ht : t = floor t, { rw ht, rw ← continuous_within_at_univ, have : (univ : set (β × α)) ⊆ (set.prod univ (Iio $ floor t)) ∪ (set.prod univ (Ici $ floor t)), { rintros p -, rw ← prod_union, exact ⟨true.intro, lt_or_le _ _⟩ }, refine continuous_within_at.mono _ this, refine continuous_within_at.union _ _, { simp only [continuous_within_at, fract_coe, nhds_within_prod_eq, nhds_within_univ, id.def, comp_app, prod.map_mk], have : (uncurry f) (s, 0) = (uncurry f) (s, (1 : α)), by simp [uncurry, hf], rw this, refine (h _ ⟨true.intro, by exact_mod_cast right_mem_Icc.mpr zero_le_one⟩).tendsto.comp _, rw [nhds_within_prod_eq, nhds_within_univ], rw nhds_within_Icc_eq_nhds_within_Iic (@zero_lt_one α _ _), exact tendsto_id.prod_map (tendsto_nhds_within_mono_right Iio_subset_Iic_self $ tendsto_fract_left _) }, { simp only [continuous_within_at, fract_coe, nhds_within_prod_eq, nhds_within_univ, id.def, comp_app, prod.map_mk], refine (h _ ⟨true.intro, by exact_mod_cast left_mem_Icc.mpr zero_le_one⟩).tendsto.comp _, rw [nhds_within_prod_eq, nhds_within_univ, nhds_within_Icc_eq_nhds_within_Ici (@zero_lt_one α _ _)], exact tendsto_id.prod_map (tendsto_fract_right _) } }, { have : t ∈ Ioo (floor t : α) ((floor t : α) + 1), from ⟨lt_of_le_of_ne (floor_le t) (ne.symm ht), lt_floor_add_one _⟩, apply (h ((prod.map _ fract) _) ⟨trivial, ⟨fract_nonneg _, (fract_lt_one _).le⟩⟩).tendsto.comp, simp only [nhds_prod_eq, nhds_within_prod_eq, nhds_within_univ, id.def, prod.map_mk], exact continuous_at_id.tendsto.prod_map (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (((continuous_on_fract _ _ (Ioo_subset_Ico_self this)).mono Ioo_subset_Ico_self).continuous_at (Ioo_mem_nhds this.1 this.2)) (eventually_of_forall (λ x, ⟨fract_nonneg _, (fract_lt_one _).le⟩)) ) } end lemma continuous_on.comp_fract {β : Type*} [order_topology α] [topological_add_group α] [topological_space β] {f : α → β} (h : continuous_on f I) (hf : f 0 = f 1) : continuous (f ∘ fract) := begin let f' : unit → α → β := λ x y, f y, have : continuous_on (uncurry f') ((univ : set unit).prod I), { rintros ⟨s, t⟩ ⟨-, ht : t ∈ I⟩, simp only [continuous_within_at, uncurry, nhds_within_prod_eq, nhds_within_univ, f'], rw tendsto_prod_iff, intros W hW, specialize h t ht hW, rw mem_map_iff_exists_image at h, rcases h with ⟨V, hV, hVW⟩, rw image_subset_iff at hVW, use [univ, univ_mem, V, hV], intros x y hx hy, exact hVW hy }, have key : continuous (λ s, ⟨unit.star, s⟩ : α → unit × α) := by continuity, exact (this.comp_fract' (λ s, hf)).comp key end
c6415c485cc56f987d63496ab33a0eafeb3f19b3
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/witt_vector/frobenius.lean
e7841e77eca4b5631adb1f5662d2f62a951c187c
[ "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
13,421
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.multiplicity import ring_theory.witt_vector.basic import ring_theory.witt_vector.is_poly /-! ## The Frobenius operator If `R` has characteristic `p`, then there is a ring endomorphism `frobenius R p` that raises `r : R` to the power `p`. By applying `witt_vector.map` to `frobenius R p`, we obtain a ring endomorphism `𝕎 R →+* 𝕎 R`. It turns out that this endomorphism can be described by polynomials over `ℤ` that do not depend on `R` or the fact that it has characteristic `p`. In this way, we obtain a Frobenius endomorphism `witt_vector.frobenius_fun : 𝕎 R → 𝕎 R` for every commutative ring `R`. Unfortunately, the aforementioned polynomials can not be obtained using the machinery of `witt_structure_int` that was developed in `structure_polynomial.lean`. We therefore have to define the polynomials by hand, and check that they have the required property. In case `R` has characteristic `p`, we show in `frobenius_fun_eq_map_frobenius` that `witt_vector.frobenius_fun` is equal to `witt_vector.map (frobenius R p)`. ### Main definitions and results * `frobenius_poly`: the polynomials that describe the coefficients of `frobenius_fun`; * `frobenius_fun`: the Frobenius endomorphism on Witt vectors; * `frobenius_fun_is_poly`: the tautological assertion that Frobenius is a polynomial function; * `frobenius_fun_eq_map_frobenius`: the fact that in characteristic `p`, Frobenius is equal to `witt_vector.map (frobenius R p)`. TODO: Show that `witt_vector.frobenius_fun` is a ring homomorphism, and bundle it into `witt_vector.frobenius`. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ namespace witt_vector variables {p : ℕ} {R S : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] local notation `𝕎` := witt_vector p -- type as `\bbW` noncomputable theory open mv_polynomial finset open_locale big_operators variables (p) include hp /-- The rational polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. These polynomials actually have integral coefficients, see `frobenius_poly` and `map_frobenius_poly`. -/ def frobenius_poly_rat (n : ℕ) : mv_polynomial ℕ ℚ := bind₁ (witt_polynomial p ℚ ∘ λ n, n + 1) (X_in_terms_of_W p ℚ n) lemma bind₁_frobenius_poly_rat_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly_rat p) (witt_polynomial p ℚ n) = (witt_polynomial p ℚ (n+1)) := begin delta frobenius_poly_rat, rw [← bind₁_bind₁, bind₁_X_in_terms_of_W_witt_polynomial, bind₁_X_right], end /-- An auxiliary definition, to avoid an excessive amount of finiteness proofs for `multiplicity p n`. -/ private def pnat_multiplicity (n : ℕ+) : ℕ := (multiplicity p n).get $ multiplicity.finite_nat_iff.mpr $ ⟨ne_of_gt hp.1.one_lt, n.2⟩ local notation `v` := pnat_multiplicity /-- An auxiliary polynomial over the integers, that satisfies `(frobenius_poly_aux p n - X n ^ p) / p = frobenius_poly p n`. This makes it easy to show that `frobenius_poly p n` is congruent to `X n ^ p` modulo `p`. -/ noncomputable def frobenius_poly_aux : ℕ → mv_polynomial ℕ ℤ | n := X (n + 1) - ∑ i : fin n, have _ := i.is_lt, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) lemma frobenius_poly_aux_eq (n : ℕ) : frobenius_poly_aux p n = X (n + 1) - ∑ i in range n, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux p i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) := by { rw [frobenius_poly_aux, ← fin.sum_univ_eq_sum_range] } /-- The polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. -/ def frobenius_poly (n : ℕ) : mv_polynomial ℕ ℤ := X n ^ p + C ↑p * (frobenius_poly_aux p n) /- Our next goal is to prove ``` lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n ``` This lemma has a rather long proof, but it mostly boils down to applying induction, and then using the following two key facts at the right point. -/ /-- A key divisibility fact for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₁ (n j : ℕ) (hj : j < p ^ (n)) : p ^ (n - v p ⟨j + 1, j.succ_pos⟩) ∣ (p ^ n).choose (j + 1) := begin apply multiplicity.pow_dvd_of_le_multiplicity, have aux : (multiplicity p ((p ^ n).choose (j + 1))).dom, { rw [← multiplicity.finite_iff_dom, multiplicity.finite_nat_iff], exact ⟨hp.1.ne_one, nat.choose_pos hj⟩, }, rw [← enat.coe_get aux, enat.coe_le_coe, nat.sub_le_left_iff_le_add, ← enat.coe_le_coe, enat.coe_add, pnat_multiplicity, enat.coe_get, enat.coe_get, add_comm], exact (hp.1.multiplicity_choose_prime_pow hj j.succ_pos).ge, end /-- A key numerical identity needed for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₂ {n i j : ℕ} (hi : i < n) (hj : j < p ^ (n - i)) : j - (v p ⟨j + 1, j.succ_pos⟩) + n = i + j + (n - i - v p ⟨j + 1, j.succ_pos⟩) := begin generalize h : (v p ⟨j + 1, j.succ_pos⟩) = m, suffices : m ≤ n - i ∧ m ≤ j, { rw [←nat.sub_add_comm this.2, add_comm i j, nat.add_sub_assoc (this.1.trans (nat.sub_le n i)), add_assoc, nat.sub.right_comm, add_comm i, nat.sub_add_cancel (nat.le_sub_right_of_add_le ((nat.le_sub_left_iff_add_le hi.le).mp this.1))] }, split, { rw [← h, ← enat.coe_le_coe, pnat_multiplicity, enat.coe_get, ← hp.1.multiplicity_choose_prime_pow hj j.succ_pos], apply le_add_left, refl }, { obtain ⟨c, hc⟩ : p ^ m ∣ j + 1, { rw [← h], exact multiplicity.pow_multiplicity_dvd _, }, obtain ⟨c, rfl⟩ : ∃ k : ℕ, c = k + 1, { apply nat.exists_eq_succ_of_ne_zero, rintro rfl, simpa only using hc }, rw [mul_add, mul_one] at hc, apply nat.le_of_lt_succ, calc m < p ^ m : nat.lt_pow_self hp.1.one_lt m ... ≤ j + 1 : by { rw ← nat.sub_eq_of_eq_add hc, apply nat.sub_le } } end lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_pow, map_C, map_X, ring_hom.eq_int_cast, int.cast_coe_nat, frobenius_poly_rat], apply nat.strong_induction_on n, clear n, intros n IH, rw [X_in_terms_of_W_eq], simp only [alg_hom.map_sum, alg_hom.map_sub, alg_hom.map_mul, alg_hom.map_pow, bind₁_C_right], have h1 : (↑p ^ n) * (⅟ (↑p : ℚ) ^ n) = 1 := by rw [←mul_pow, mul_inv_of_self, one_pow], rw [bind₁_X_right, function.comp_app, witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ, sum_range_succ, nat.sub_self, nat.add_sub_cancel_left, pow_zero, pow_one, pow_one, sub_mul, add_mul, add_mul, mul_right_comm, mul_right_comm (C (↑p ^ (n + 1))), ←C_mul, ←C_mul, pow_succ, mul_assoc ↑p (↑p ^ n), h1, mul_one, C_1, one_mul, add_comm _ (X n ^ p), add_assoc, ←add_sub, add_right_inj, frobenius_poly_aux_eq, ring_hom.map_sub, map_X, mul_sub, sub_eq_add_neg, add_comm _ (C ↑p * X (n + 1)), ←add_sub, add_right_inj, neg_eq_iff_neg_eq, neg_sub], simp only [ring_hom.map_sum, mul_sum, sum_mul, ←sum_sub_distrib], apply sum_congr rfl, intros i hi, rw mem_range at hi, rw [← IH i hi], clear IH, rw [add_comm (X i ^ p), add_pow, sum_range_succ', pow_zero, nat.sub_zero, nat.choose_zero_right, one_mul, nat.cast_one, mul_one, mul_add, add_mul, nat.succ_sub (le_of_lt hi), nat.succ_eq_add_one (n - i), pow_succ, pow_mul, add_sub_cancel, mul_sum, sum_mul], apply sum_congr rfl, intros j hj, rw mem_range at hj, rw [ring_hom.map_mul, ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, map_C, map_X, mul_pow], rw [mul_comm (C ↑p ^ i), mul_comm _ ((X i ^ p) ^ _), mul_comm (C ↑p ^ (j + 1)), mul_comm (C ↑p)], simp only [mul_assoc], apply congr_arg, apply congr_arg, rw [←C_eq_coe_nat], simp only [←ring_hom.map_pow, ←C_mul], rw C_inj, simp only [inv_of_eq_inv, ring_hom.eq_int_cast, inv_pow', int.cast_coe_nat, nat.cast_mul], rw [rat.coe_nat_div _ _ (map_frobenius_poly.key₁ p (n - i) j hj)], simp only [nat.cast_pow, pow_add, pow_one], suffices : ((p ^ (n - i)).choose (j + 1) * p ^ (j - v p ⟨j + 1, j.succ_pos⟩) * p * p ^ n : ℚ) = p ^ j * p * ((p ^ (n - i)).choose (j + 1) * p ^ i) * p ^ (n - i - v p ⟨j + 1, j.succ_pos⟩), { have aux : ∀ k : ℕ, (p ^ k : ℚ) ≠ 0, { intro, apply pow_ne_zero, exact_mod_cast hp.1.ne_zero }, simpa [aux, -one_div] with field_simps using this.symm }, rw [mul_comm _ (p : ℚ), mul_assoc, mul_assoc, ← pow_add, map_frobenius_poly.key₂ p hi hj], ring_exp end lemma frobenius_poly_zmod (n : ℕ) : mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n) = X n ^ p := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_pow, ring_hom.map_mul, map_X, map_C], simp only [int.cast_coe_nat, add_zero, ring_hom.eq_int_cast, zmod.nat_cast_self, zero_mul, C_0], end @[simp] lemma bind₁_frobenius_poly_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly p) (witt_polynomial p ℤ n) = (witt_polynomial p ℤ (n+1)) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_bind₁, map_frobenius_poly, bind₁_frobenius_poly_rat_witt_polynomial, map_witt_polynomial], end variables {p} /-- `frobenius_fun` is the function underlying the ring endomorphism `frobenius : 𝕎 R →+* frobenius 𝕎 R`. -/ def frobenius_fun (x : 𝕎 R) : 𝕎 R := mk p $ λ n, mv_polynomial.aeval x.coeff (frobenius_poly p n) lemma coeff_frobenius_fun (x : 𝕎 R) (n : ℕ) : coeff (frobenius_fun x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := by rw [frobenius_fun, coeff_mk] variables (p) /-- `frobenius_fun` is tautologically a polynomial function. See also `frobenius_is_poly`. -/ @[is_poly] lemma frobenius_fun_is_poly : is_poly p (λ R _Rcr, @frobenius_fun p R _ _Rcr) := ⟨⟨frobenius_poly p, by { introsI, funext n, apply coeff_frobenius_fun }⟩⟩ variable {p} @[ghost_simps] lemma ghost_component_frobenius_fun (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius_fun x) = ghost_component (n + 1) x := by simp only [ghost_component_apply, frobenius_fun, coeff_mk, ← bind₁_frobenius_poly_witt_polynomial, aeval_bind₁] /-- If `R` has characteristic `p`, then there is a ring endomorphism that raises `r : R` to the power `p`. By applying `witt_vector.map` to this endomorphism, we obtain a ring endomorphism `frobenius R p : 𝕎 R →+* 𝕎 R`. The underlying function of this morphism is `witt_vector.frobenius_fun`. -/ def frobenius : 𝕎 R →+* 𝕎 R := { to_fun := frobenius_fun, map_zero' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.zero_is_poly)) ((witt_vector.zero_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_one' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.one_is_poly)) ((witt_vector.one_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_add' := by ghost_calc _ _; ghost_simp, map_mul' := by ghost_calc _ _; ghost_simp } lemma coeff_frobenius (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := coeff_frobenius_fun _ _ @[ghost_simps] lemma ghost_component_frobenius (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius x) = ghost_component (n + 1) x := ghost_component_frobenius_fun _ _ variables (p) /-- `frobenius` is tautologically a polynomial function. -/ @[is_poly] lemma frobenius_is_poly : is_poly p (λ R _Rcr, @frobenius p R _ _Rcr) := frobenius_fun_is_poly _ section char_p variables [char_p R p] @[simp] lemma coeff_frobenius_char_p (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = (x.coeff n) ^ p := begin rw [coeff_frobenius], -- outline of the calculation, proofs follow below calc aeval (λ k, x.coeff k) (frobenius_poly p n) = aeval (λ k, x.coeff k) (mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n)) : _ ... = aeval (λ k, x.coeff k) (X n ^ p : mv_polynomial ℕ (zmod p)) : _ ... = (x.coeff n) ^ p : _, { conv_rhs { rw [aeval_eq_eval₂_hom, eval₂_hom_map_hom] }, apply eval₂_hom_congr (ring_hom.ext_int _ _) rfl rfl }, { rw frobenius_poly_zmod }, { rw [alg_hom.map_pow, aeval_X] } end lemma frobenius_eq_map_frobenius : @frobenius p R _ _ = map (_root_.frobenius R p) := begin ext x n, simp only [coeff_frobenius_char_p, map_coeff, frobenius_def], end @[simp] lemma frobenius_zmodp (x : 𝕎 (zmod p)) : (frobenius x) = x := by simp only [ext_iff, coeff_frobenius_char_p, zmod.pow_card, eq_self_iff_true, forall_const] end char_p end witt_vector
7ef1974e2065fb520c64cf4ce6a4ba98fbfc352c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/direct_sum/tensor_product_auto.lean
7e20ba7651bd2d71ba8fd068a43e2cfee563c04e
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,322
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.linear_algebra.tensor_product import Mathlib.linear_algebra.direct_sum_module import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u_5 namespace Mathlib namespace tensor_product /-- The linear equivalence `(⨁ i₁, M₁ i₁) ⊗ (⨁ i₂, M₂ i₂) ≃ (⨁ i₁, ⨁ i₂, M₁ i₁ ⊗ M₂ i₂)`, i.e. "tensor product distributes over direct sum". -/ def direct_sum (R : Type u_1) [comm_ring R] (ι₁ : Type u_2) (ι₂ : Type u_3) [DecidableEq ι₁] [DecidableEq ι₂] (M₁ : ι₁ → Type u_4) (M₂ : ι₂ → Type u_5) [(i₁ : ι₁) → add_comm_group (M₁ i₁)] [(i₂ : ι₂) → add_comm_group (M₂ i₂)] [(i₁ : ι₁) → module R (M₁ i₁)] [(i₂ : ι₂) → module R (M₂ i₂)] : linear_equiv R (tensor_product R (direct_sum ι₁ fun (i₁ : ι₁) => M₁ i₁) (direct_sum ι₂ fun (i₂ : ι₂) => M₂ i₂)) (direct_sum (ι₁ × ι₂) fun (i : ι₁ × ι₂) => tensor_product R (M₁ (prod.fst i)) (M₂ (prod.snd i))) := linear_equiv.of_linear (lift (direct_sum.to_module R ι₁ (linear_map R (direct_sum ι₂ fun (i₂ : ι₂) => M₂ i₂) (direct_sum (ι₁ × ι₂) fun (i : ι₁ × ι₂) => tensor_product R (M₁ (prod.fst i)) (M₂ (prod.snd i)))) fun (i₁ : ι₁) => linear_map.flip (direct_sum.to_module R ι₂ (linear_map R (M₁ i₁) (direct_sum (ι₁ × ι₂) fun (i : ι₁ × ι₂) => tensor_product R (M₁ (prod.fst i)) (M₂ (prod.snd i)))) fun (i₂ : ι₂) => linear_map.flip (curry (direct_sum.lof R (ι₁ × ι₂) (fun (i : ι₁ × ι₂) => tensor_product R (M₁ (prod.fst i)) (M₂ (prod.snd i))) (i₁, i₂)))))) (direct_sum.to_module R (ι₁ × ι₂) (tensor_product R (direct_sum ι₁ fun (i₁ : ι₁) => M₁ i₁) (direct_sum ι₂ fun (i₂ : ι₂) => M₂ i₂)) fun (i : ι₁ × ι₂) => map (direct_sum.lof R ι₁ M₁ (prod.fst i)) (direct_sum.lof R ι₂ M₂ (prod.snd i))) sorry sorry @[simp] theorem direct_sum_lof_tmul_lof (R : Type u_1) [comm_ring R] (ι₁ : Type u_2) (ι₂ : Type u_3) [DecidableEq ι₁] [DecidableEq ι₂] (M₁ : ι₁ → Type u_4) (M₂ : ι₂ → Type u_5) [(i₁ : ι₁) → add_comm_group (M₁ i₁)] [(i₂ : ι₂) → add_comm_group (M₂ i₂)] [(i₁ : ι₁) → module R (M₁ i₁)] [(i₂ : ι₂) → module R (M₂ i₂)] (i₁ : ι₁) (m₁ : M₁ i₁) (i₂ : ι₂) (m₂ : M₂ i₂) : coe_fn (direct_sum R ι₁ ι₂ M₁ M₂) (tmul R (coe_fn (direct_sum.lof R ι₁ M₁ i₁) m₁) (coe_fn (direct_sum.lof R ι₂ M₂ i₂) m₂)) = coe_fn (direct_sum.lof R (ι₁ × ι₂) (fun (i : ι₁ × ι₂) => tensor_product R (M₁ (prod.fst i)) (M₂ (prod.snd i))) (i₁, i₂)) (tmul R m₁ m₂) := sorry end Mathlib
806353201137b2c10096273a81f408c9b6ccb38a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/monoidal/functor_category.lean
937c29de0c9b96e1ace9e17c38ed2487cf34c0e8
[ "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
5,704
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.braided import category_theory.functor.category import category_theory.functor.const /-! # Monoidal structure on `C ⥤ D` when `D` is monoidal. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. When `C` is any category, and `D` is a monoidal category, there is a natural "pointwise" monoidal structure on `C ⥤ D`. The initial intended application is tensor product of presheaves. -/ universes v₁ v₂ u₁ u₂ open category_theory open category_theory.monoidal_category namespace category_theory.monoidal variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] namespace functor_category variables (F G F' G' : C ⥤ D) /-- (An auxiliary definition for `functor_category_monoidal`.) Tensor product of functors `C ⥤ D`, when `D` is monoidal. -/ @[simps] def tensor_obj : C ⥤ D := { obj := λ X, F.obj X ⊗ G.obj X, map := λ X Y f, F.map f ⊗ G.map f, map_id' := λ X, by rw [F.map_id, G.map_id, tensor_id], map_comp' := λ X Y Z f g, by rw [F.map_comp, G.map_comp, tensor_comp], } variables {F G F' G'} variables (α : F ⟶ G) (β : F' ⟶ G') /-- (An auxiliary definition for `functor_category_monoidal`.) Tensor product of natural transformations into `D`, when `D` is monoidal. -/ @[simps] def tensor_hom : tensor_obj F F' ⟶ tensor_obj G G' := { app := λ X, α.app X ⊗ β.app X, naturality' := λ X Y f, by { dsimp, rw [←tensor_comp, α.naturality, β.naturality, tensor_comp], } } end functor_category open category_theory.monoidal.functor_category /-- When `C` is any category, and `D` is a monoidal category, the functor category `C ⥤ D` has a natural pointwise monoidal structure, where `(F ⊗ G).obj X = F.obj X ⊗ G.obj X`. -/ instance functor_category_monoidal : monoidal_category (C ⥤ D) := { tensor_obj := λ F G, tensor_obj F G, tensor_hom := λ F G F' G' α β, tensor_hom α β, tensor_id' := λ F G, by { ext, dsimp, rw [tensor_id], }, tensor_comp' := λ F G H F' G' H' α β γ δ, by { ext, dsimp, rw [tensor_comp], }, tensor_unit := (category_theory.functor.const C).obj (𝟙_ D), left_unitor := λ F, nat_iso.of_components (λ X, λ_ (F.obj X)) (λ X Y f, by { dsimp, rw left_unitor_naturality, }), right_unitor := λ F, nat_iso.of_components (λ X, ρ_ (F.obj X)) (λ X Y f, by { dsimp, rw right_unitor_naturality, }), associator := λ F G H, nat_iso.of_components (λ X, α_ (F.obj X) (G.obj X) (H.obj X)) (λ X Y f, by { dsimp, rw associator_naturality, }), left_unitor_naturality' := λ F G α, by { ext X, dsimp, rw left_unitor_naturality, }, right_unitor_naturality' := λ F G α, by { ext X, dsimp, rw right_unitor_naturality, }, associator_naturality' := λ F G H F' G' H' α β γ, by { ext X, dsimp, rw associator_naturality, }, triangle' := λ F G, begin ext X, dsimp, rw triangle, end, pentagon' := λ F G H K, begin ext X, dsimp, rw pentagon, end, } @[simp] lemma tensor_unit_obj {X} : (𝟙_ (C ⥤ D)).obj X = 𝟙_ D := rfl @[simp] lemma tensor_unit_map {X Y} {f : X ⟶ Y} : (𝟙_ (C ⥤ D)).map f = 𝟙 (𝟙_ D) := rfl @[simp] lemma tensor_obj_obj {F G : C ⥤ D} {X} : (F ⊗ G).obj X = F.obj X ⊗ G.obj X := rfl @[simp] lemma tensor_obj_map {F G : C ⥤ D} {X Y} {f : X ⟶ Y} : (F ⊗ G).map f = F.map f ⊗ G.map f := rfl @[simp] lemma tensor_hom_app {F G F' G' : C ⥤ D} {α : F ⟶ G} {β : F' ⟶ G'} {X} : (α ⊗ β).app X = α.app X ⊗ β.app X := rfl @[simp] lemma left_unitor_hom_app {F : C ⥤ D} {X} : ((λ_ F).hom : (𝟙_ _) ⊗ F ⟶ F).app X = (λ_ (F.obj X)).hom := rfl @[simp] lemma left_unitor_inv_app {F : C ⥤ D} {X} : ((λ_ F).inv : F ⟶ (𝟙_ _) ⊗ F).app X = (λ_ (F.obj X)).inv := rfl @[simp] lemma right_unitor_hom_app {F : C ⥤ D} {X} : ((ρ_ F).hom : F ⊗ (𝟙_ _) ⟶ F).app X = (ρ_ (F.obj X)).hom := rfl @[simp] lemma right_unitor_inv_app {F : C ⥤ D} {X} : ((ρ_ F).inv : F ⟶ F ⊗ (𝟙_ _)).app X = (ρ_ (F.obj X)).inv := rfl @[simp] lemma associator_hom_app {F G H : C ⥤ D} {X} : ((α_ F G H).hom : (F ⊗ G) ⊗ H ⟶ F ⊗ (G ⊗ H)).app X = (α_ (F.obj X) (G.obj X) (H.obj X)).hom := rfl @[simp] lemma associator_inv_app {F G H : C ⥤ D} {X} : ((α_ F G H).inv : F ⊗ (G ⊗ H) ⟶ (F ⊗ G) ⊗ H).app X = (α_ (F.obj X) (G.obj X) (H.obj X)).inv := rfl section braided_category open category_theory.braided_category variables [braided_category.{v₂} D] /-- When `C` is any category, and `D` is a braided monoidal category, the natural pointwise monoidal structure on the functor category `C ⥤ D` is also braided. -/ instance functor_category_braided : braided_category (C ⥤ D) := { braiding := λ F G, nat_iso.of_components (λ X, β_ _ _) (by tidy), hexagon_forward' := λ F G H, by { ext X, apply hexagon_forward, }, hexagon_reverse' := λ F G H, by { ext X, apply hexagon_reverse, }, } example : braided_category (C ⥤ D) := category_theory.monoidal.functor_category_braided end braided_category section symmetric_category open category_theory.symmetric_category variables [symmetric_category.{v₂} D] /-- When `C` is any category, and `D` is a symmetric monoidal category, the natural pointwise monoidal structure on the functor category `C ⥤ D` is also symmetric. -/ instance functor_category_symmetric : symmetric_category (C ⥤ D) := { symmetry' := λ F G, by { ext X, apply symmetry, },} end symmetric_category end category_theory.monoidal
c6b3252a1cb4f06a3f1dfb15af12eef785ed043d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/finish.lean
1bd3fb4e7a8cb9ae15069a0fe2b297f8d61d1b4d
[]
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
8,123
lean
/- Copyright (c) 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Jesse Michael Han -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.hint import Mathlib.PostPort universes l u namespace Mathlib /-! # The `finish` family of tactics These tactics do straightforward things: they call the simplifier, split conjunctive assumptions, eliminate existential quantifiers on the left, and look for contradictions. They rely on ematching and congruence closure to try to finish off a goal at the end. The procedures *do* split on disjunctions and recreate the smt state for each terminal call, so they are only meant to be used on small, straightforward problems. ## Main definitions We provide the following tactics: * `finish` -- solves the goal or fails * `clarify` -- makes as much progress as possible while not leaving more than one goal * `safe` -- splits freely, finishes off whatever subgoals it can, and leaves the rest All accept an optional list of simplifier rules, typically definitions that should be expanded. (The equations and identities should not refer to the local context.) -/ namespace tactic namespace interactive end interactive end tactic namespace auto /-! ### Utilities -/ -- stolen from interactive.lean /-- Configuration information for the auto tactics. * `(use_simp := tt)`: call the simplifier * `(max_ematch_rounds := 20)`: for the "done" tactic -/ structure auto_config where use_simp : Bool max_ematch_rounds : ℕ /-! ### Preprocess goal. We want to move everything to the left of the sequent arrow. For intuitionistic logic, we replace the goal `p` with `∀ f, (p → f) → f` and introduce. -/ theorem by_contradiction_trick (p : Prop) (h : ∀ (f : Prop), (p → f) → f) : p := h p id /-! ### Normalize hypotheses Bring conjunctions to the outside (for splitting), bring universal quantifiers to the outside (for ematching). The classical normalizer eliminates `a → b` in favor of `¬ a ∨ b`. For efficiency, we push negations inwards from the top down. (For example, consider simplifying `¬ ¬ (p ∨ q)`.) -/ theorem not_not_eq (p : Prop) : (¬¬p) = p := propext not_not theorem not_and_eq (p : Prop) (q : Prop) : (¬(p ∧ q)) = (¬p ∨ ¬q) := propext not_and_distrib theorem not_or_eq (p : Prop) (q : Prop) : (¬(p ∨ q)) = (¬p ∧ ¬q) := propext not_or_distrib theorem not_forall_eq {α : Type u} (s : α → Prop) : (¬∀ (x : α), s x) = ∃ (x : α), ¬s x := propext not_forall theorem not_exists_eq {α : Type u} (s : α → Prop) : (¬∃ (x : α), s x) = ∀ (x : α), ¬s x := propext not_exists theorem not_implies_eq (p : Prop) (q : Prop) : (¬(p → q)) = (p ∧ ¬q) := propext not_imp theorem classical.implies_iff_not_or (p : Prop) (q : Prop) : p → q ↔ ¬p ∨ q := imp_iff_not_or def common_normalize_lemma_names : List name := sorry def classical_normalize_lemma_names : List name := sorry /-- optionally returns an equivalent expression and proof of equivalence -/ /-- given an expr `e`, returns a new expression and a proof of equality -/ /-! ### Eliminate existential quantifiers -/ /-- eliminate an existential quantifier if there is one -/ /-- eliminate all existential quantifiers, fails if there aren't any -/ /-! ### Substitute if there is a hypothesis `x = t` or `t = x` -/ /-- carries out a subst if there is one, fails otherwise -/ /-! ### Split all conjunctions -/ /-- Assumes `pr` is a proof of `t`. Adds the consequences of `t` to the context and returns `tt` if anything nontrivial has been added. -/ /-- return `tt` if any progress is made -/ /-- return `tt` if any progress is made -/ /-- fail if no progress is made -/ /-! ### Eagerly apply all the preprocessing rules -/ /-- Eagerly apply all the preprocessing rules -/ /-! ### Terminal tactic -/ /-- The terminal tactic, used to try to finish off goals: - Call the contradiction tactic. - Open an SMT state, and use ematching and congruence closure, with all the universal statements in the context. TODO(Jeremy): allow users to specify attribute for ematching lemmas? -/ /-- `done` first attempts to close the goal using `contradiction`. If this fails, it creates an SMT state and will repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `ps`) and congruence closure. -/ /-! ### Tactics that perform case splits -/ inductive case_option where | force : case_option | at_most_one : case_option | accept : case_option -- three possible outcomes: -- finds something to case, the continuations succeed ==> returns tt -- finds something to case, the continutations fail ==> fails -- doesn't find anything to case ==> returns ff /-! ### The main tactics -/ /-- `safe_core s ps cfg opt` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `s`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `ps`) and congruence closure. `safe_core` is complete for propositional logic. Depending on the form of `opt` it will: - (if `opt` is `case_option.force`) fail if it does not close the goal, - (if `opt` is `case_option.at_most_one`) fail if it produces more than one goal, and - (if `opt` is `case_option.accept`) ignore the number of goals it produces. -/ /-- `clarify` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.at_most_one`. -/ /-- `safe` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.accept`. -/ /-- `finish` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.force`. -/ end auto /-! ### interactive versions -/ namespace tactic namespace interactive /-- `clarify [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `clarify` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `clarify` will fail if it produces more than one goal. -/ /-- `safe [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `safe` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `safe` ignores the number of goals it produces, and should never fail. -/ /-- `finish [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `finish` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `finish` will fail if it does not close the goal. -/ /-- These tactics do straightforward things: they call the simplifier, split conjunctive assumptions,
f4da4ba8a5505ce466c87a86fbf55af95aaa64d5
82e44445c70db0f03e30d7be725775f122d72f3e
/src/set_theory/cardinal_ordinal.lean
c5973f3a067add73ccdfca8dd7bef601a78d5619
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
36,168
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, Floris van Doorn -/ import set_theory.ordinal_arithmetic import tactic.linarith /-! # Cardinals and ordinals Relationships between cardinals and ordinals, properties of cardinals that are proved using ordinals. ## Main definitions * The function `cardinal.aleph'` gives the cardinals listed by their ordinal index, and is the inverse of `cardinal.aleph_idx`. `aleph' n = n`, `aleph' ω = cardinal.omega = ℵ₀`, `aleph' (ω + 1) = ℵ₁`, etc. It is an order isomorphism between ordinals and cardinals. * The function `cardinal.aleph` gives the infinite cardinals listed by their ordinal index. `aleph 0 = cardinal.omega = ℵ₀`, `aleph 1 = ℵ₁` is the first uncountable cardinal, and so on. ## Main Statements * `cardinal.mul_eq_max` and `cardinal.add_eq_max` state that the product (resp. sum) of two infinite cardinals is just their maximum. Several variations around this fact are also given. * `cardinal.mk_list_eq_mk` : when `α` is infinite, `α` and `list α` have the same cardinality. * simp lemmas for inequalities between `bit0 a` and `bit1 b` are registered, making `simp` able to prove inequalities about numeral cardinals. ## Tags cardinal arithmetic (for infinite cardinals) -/ noncomputable theory open function cardinal set equiv open_locale classical cardinal universes u v w namespace cardinal section using_ordinals open ordinal theorem ord_is_limit {c} (co : omega ≤ c) : (ord c).is_limit := begin refine ⟨λ h, omega_ne_zero _, λ a, lt_imp_lt_of_le_imp_le _⟩, { rw [← ordinal.le_zero, ord_le] at h, simpa only [card_zero, nonpos_iff_eq_zero] using le_trans co h }, { intro h, rw [ord_le] at h ⊢, rwa [← @add_one_of_omega_le (card a), ← card_succ], rw [← ord_le, ← le_succ_of_is_limit, ord_le], { exact le_trans co h }, { rw ord_omega, exact omega_is_limit } } end /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this definition, we register additionally that this function is an initial segment, i.e., it is order preserving and its range is an initial segment of the ordinals. For the basic function version, see `aleph_idx`. For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx.initial_seg : @initial_seg cardinal ordinal (<) (<) := @rel_embedding.collapse cardinal ordinal (<) (<) _ cardinal.ord.order_embedding.lt_embedding /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx : cardinal → ordinal := aleph_idx.initial_seg @[simp] theorem aleph_idx.initial_seg_coe : (aleph_idx.initial_seg : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem aleph_idx_lt {a b} : aleph_idx a < aleph_idx b ↔ a < b := aleph_idx.initial_seg.to_rel_embedding.map_rel_iff @[simp] theorem aleph_idx_le {a b} : aleph_idx a ≤ aleph_idx b ↔ a ≤ b := by rw [← not_lt, ← not_lt, aleph_idx_lt] theorem aleph_idx.init {a b} : b < aleph_idx a → ∃ c, aleph_idx c = b := aleph_idx.initial_seg.init _ _ /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this version, we register additionally that this function is an order isomorphism between cardinals and ordinals. For the basic function version, see `aleph_idx`. -/ def aleph_idx.rel_iso : @rel_iso cardinal.{u} ordinal.{u} (<) (<) := @rel_iso.of_surjective cardinal.{u} ordinal.{u} (<) (<) aleph_idx.initial_seg.{u} $ (initial_seg.eq_or_principal aleph_idx.initial_seg.{u}).resolve_right $ λ ⟨o, e⟩, begin have : ∀ c, aleph_idx c < o := λ c, (e _).2 ⟨_, rfl⟩, refine ordinal.induction_on o _ this, introsI α r _ h, let s := sup.{u u} (λ a:α, inv_fun aleph_idx (ordinal.typein r a)), apply not_le_of_gt (lt_succ_self s), have I : injective aleph_idx := aleph_idx.initial_seg.to_embedding.injective, simpa only [typein_enum, left_inverse_inv_fun I (succ s)] using le_sup.{u u} (λ a, inv_fun aleph_idx (ordinal.typein r a)) (ordinal.enum r _ (h (succ s))), end @[simp] theorem aleph_idx.rel_iso_coe : (aleph_idx.rel_iso : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem type_cardinal : @ordinal.type cardinal (<) _ = ordinal.univ.{u (u+1)} := by rw ordinal.univ_id; exact quotient.sound ⟨aleph_idx.rel_iso⟩ @[simp] theorem mk_cardinal : mk cardinal = univ.{u (u+1)} := by simpa only [card_type, card_univ] using congr_arg card type_cardinal /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = ℵ₁`, etc. In this version, we register additionally that this function is an order isomorphism between ordinals and cardinals. For the basic function version, see `aleph'`. -/ def aleph'.rel_iso := cardinal.aleph_idx.rel_iso.symm /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = ℵ₁`, etc. -/ def aleph' : ordinal → cardinal := aleph'.rel_iso @[simp] theorem aleph'.rel_iso_coe : (aleph'.rel_iso : ordinal → cardinal) = aleph' := rfl @[simp] theorem aleph'_lt {o₁ o₂ : ordinal.{u}} : aleph' o₁ < aleph' o₂ ↔ o₁ < o₂ := aleph'.rel_iso.map_rel_iff @[simp] theorem aleph'_le {o₁ o₂ : ordinal.{u}} : aleph' o₁ ≤ aleph' o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph'_lt @[simp] theorem aleph'_aleph_idx (c : cardinal.{u}) : aleph' c.aleph_idx = c := cardinal.aleph_idx.rel_iso.to_equiv.symm_apply_apply c @[simp] theorem aleph_idx_aleph' (o : ordinal.{u}) : (aleph' o).aleph_idx = o := cardinal.aleph_idx.rel_iso.to_equiv.apply_symm_apply o @[simp] theorem aleph'_zero : aleph' 0 = 0 := by rw [← nonpos_iff_eq_zero, ← aleph'_aleph_idx 0, aleph'_le]; apply ordinal.zero_le @[simp] theorem aleph'_succ {o : ordinal.{u}} : aleph' o.succ = (aleph' o).succ := le_antisymm (cardinal.aleph_idx_le.1 $ by rw [aleph_idx_aleph', ordinal.succ_le, ← aleph'_lt, aleph'_aleph_idx]; apply cardinal.lt_succ_self) (cardinal.succ_le.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _) @[simp] theorem aleph'_nat : ∀ n : ℕ, aleph' n = n | 0 := aleph'_zero | (n+1) := show aleph' (ordinal.succ n) = n.succ, by rw [aleph'_succ, aleph'_nat, nat_succ] theorem aleph'_le_of_limit {o : ordinal.{u}} (l : o.is_limit) {c} : aleph' o ≤ c ↔ ∀ o' < o, aleph' o' ≤ c := ⟨λ h o' h', le_trans (aleph'_le.2 $ le_of_lt h') h, λ h, begin rw [← aleph'_aleph_idx c, aleph'_le, ordinal.limit_le l], intros x h', rw [← aleph'_le, aleph'_aleph_idx], exact h _ h' end⟩ @[simp] theorem aleph'_omega : aleph' ordinal.omega = omega := eq_of_forall_ge_iff $ λ c, begin simp only [aleph'_le_of_limit omega_is_limit, ordinal.lt_omega, exists_imp_distrib, omega_le], exact forall_swap.trans (forall_congr $ λ n, by simp only [forall_eq, aleph'_nat]), end /-- `aleph'` and `aleph_idx` form an equivalence between `ordinal` and `cardinal` -/ @[simp] def aleph'_equiv : ordinal ≃ cardinal := ⟨aleph', aleph_idx, aleph_idx_aleph', aleph'_aleph_idx⟩ /-- The `aleph` function gives the infinite cardinals listed by their ordinal index. `aleph 0 = ω`, `aleph 1 = succ ω` is the first uncountable cardinal, and so on. -/ def aleph (o : ordinal) : cardinal := aleph' (ordinal.omega + o) @[simp] theorem aleph_lt {o₁ o₂ : ordinal.{u}} : aleph o₁ < aleph o₂ ↔ o₁ < o₂ := aleph'_lt.trans (ordinal.add_lt_add_iff_left _) @[simp] theorem aleph_le {o₁ o₂ : ordinal.{u}} : aleph o₁ ≤ aleph o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph_lt @[simp] theorem aleph_succ {o : ordinal.{u}} : aleph o.succ = (aleph o).succ := by rw [aleph, ordinal.add_succ, aleph'_succ]; refl @[simp] theorem aleph_zero : aleph 0 = omega := by simp only [aleph, add_zero, aleph'_omega] theorem omega_le_aleph' {o : ordinal} : omega ≤ aleph' o ↔ ordinal.omega ≤ o := by rw [← aleph'_omega, aleph'_le] theorem omega_le_aleph (o : ordinal) : omega ≤ aleph o := by rw [aleph, omega_le_aleph']; apply ordinal.le_add_right theorem ord_aleph_is_limit (o : ordinal) : is_limit (aleph o).ord := ord_is_limit $ omega_le_aleph _ theorem exists_aleph {c : cardinal} : omega ≤ c ↔ ∃ o, c = aleph o := ⟨λ h, ⟨aleph_idx c - ordinal.omega, by rw [aleph, ordinal.add_sub_cancel_of_le, aleph'_aleph_idx]; rwa [← omega_le_aleph', aleph'_aleph_idx]⟩, λ ⟨o, e⟩, e.symm ▸ omega_le_aleph _⟩ theorem aleph'_is_normal : is_normal (ord ∘ aleph') := ⟨λ o, ord_lt_ord.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _, λ o l a, by simp only [ord_le, aleph'_le_of_limit l]⟩ theorem aleph_is_normal : is_normal (ord ∘ aleph) := aleph'_is_normal.trans $ add_is_normal ordinal.omega lemma countable_iff_lt_aleph_one {α : Type*} (s : set α) : countable s ↔ #s < aleph 1 := begin have : aleph 1 = (aleph 0).succ, by simp only [← aleph_succ, ordinal.succ_zero], rw [countable_iff, ← aleph_zero, this, lt_succ], end /-! ### Properties of `mul` -/ /-- If `α` is an infinite type, then `α × α` and `α` have the same cardinality. -/ theorem mul_eq_self {c : cardinal} (h : omega ≤ c) : c * c = c := begin refine le_antisymm _ (by simpa only [mul_one] using mul_le_mul_left' (one_lt_omega.le.trans h) c), -- the only nontrivial part is `c * c ≤ c`. We prove it inductively. refine acc.rec_on (cardinal.wf.apply c) (λ c _, quotient.induction_on c $ λ α IH ol, _) h, -- consider the minimal well-order `r` on `α` (a type with cardinality `c`). rcases ord_eq α with ⟨r, wo, e⟩, resetI, letI := linear_order_of_STO' r, haveI : is_well_order α (<) := wo, -- Define an order `s` on `α × α` by writing `(a, b) < (c, d)` if `max a b < max c d`, or -- the max are equal and `a < c`, or the max are equal and `a = c` and `b < d`. let g : α × α → α := λ p, max p.1 p.2, let f : α × α ↪ ordinal × (α × α) := ⟨λ p:α×α, (typein (<) (g p), p), λ p q, congr_arg prod.snd⟩, let s := f ⁻¹'o (prod.lex (<) (prod.lex (<) (<))), -- this is a well order on `α × α`. haveI : is_well_order _ s := (rel_embedding.preimage _ _).is_well_order, /- it suffices to show that this well order is smaller than `r` if it were larger, then `r` would be a strict prefix of `s`. It would be contained in `β × β` for some `β` of cardinality `< c`. By the inductive assumption, this set has the same cardinality as `β` (or it is finite if `β` is finite), so it is `< c`, which is a contradiction. -/ suffices : type s ≤ type r, {exact card_le_card this}, refine le_of_forall_lt (λ o h, _), rcases typein_surj s h with ⟨p, rfl⟩, rw [← e, lt_ord], refine lt_of_le_of_lt (_ : _ ≤ card (typein (<) (g p)).succ * card (typein (<) (g p)).succ) _, { have : {q|s q p} ⊆ (insert (g p) {x | x < (g p)}).prod (insert (g p) {x | x < (g p)}), { intros q h, simp only [s, embedding.coe_fn_mk, order.preimage, typein_lt_typein, prod.lex_def, typein_inj] at h, exact max_le_iff.1 (le_iff_lt_or_eq.2 $ h.imp_right and.left) }, suffices H : (insert (g p) {x | r x (g p)} : set α) ≃ ({x | r x (g p)} ⊕ punit), { exact ⟨(set.embedding_of_subset _ _ this).trans ((equiv.set.prod _ _).trans (H.prod_congr H)).to_embedding⟩ }, refine (equiv.set.insert _).trans ((equiv.refl _).sum_congr punit_equiv_punit), apply @irrefl _ r }, cases lt_or_ge (card (typein (<) (g p)).succ) omega with qo qo, { exact lt_of_lt_of_le (mul_lt_omega qo qo) ol }, { suffices, {exact lt_of_le_of_lt (IH _ this qo) this}, rw ← lt_ord, apply (ord_is_limit ol).2, rw [mk_def, e], apply typein_lt_type } end end using_ordinals /-- If `α` and `β` are infinite types, then the cardinality of `α × β` is the maximum of the cardinalities of `α` and `β`. -/ theorem mul_eq_max {a b : cardinal} (ha : omega ≤ a) (hb : omega ≤ b) : a * b = max a b := le_antisymm (mul_eq_self (le_trans ha (le_max_left a b)) ▸ mul_le_mul' (le_max_left _ _) (le_max_right _ _)) $ max_le (by simpa only [mul_one] using mul_le_mul_left' (one_lt_omega.le.trans hb) a) (by simpa only [one_mul] using mul_le_mul_right' (one_lt_omega.le.trans ha) b) theorem mul_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a * b < c := lt_of_le_of_lt (mul_le_mul' (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (mul_lt_omega h h) hc) (λ h, by rw mul_eq_self h; exact max_lt h1 h2) lemma mul_le_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) : a * b ≤ max a b := begin convert mul_le_mul' (le_max_left a b) (le_max_right a b), rw [mul_eq_self], refine le_trans h (le_max_left a b) end lemma mul_eq_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) (h' : b ≠ 0) : a * b = max a b := begin apply le_antisymm, apply mul_le_max_of_omega_le_left h, cases le_or_gt omega b with hb hb, rw [mul_eq_max h hb], have : b ≤ a, exact le_trans (le_of_lt hb) h, rw [max_eq_left this], convert mul_le_mul_left' (one_le_iff_ne_zero.mpr h') _, rw [mul_one], end lemma mul_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) (hb' : b ≠ 0) : a * b = a := by { rw [mul_eq_max_of_omega_le_left ha hb', max_eq_left hb] } lemma mul_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) (ha' : a ≠ 0) : a * b = b := by { rw [mul_comm, mul_eq_left hb ha ha'] } lemma le_mul_left {a b : cardinal} (h : b ≠ 0) : a ≤ b * a := by { convert mul_le_mul_right' (one_le_iff_ne_zero.mpr h) _, rw [one_mul] } lemma le_mul_right {a b : cardinal} (h : b ≠ 0) : a ≤ a * b := by { rw [mul_comm], exact le_mul_left h } lemma mul_eq_left_iff {a b : cardinal} : a * b = a ↔ ((max omega b ≤ a ∧ b ≠ 0) ∨ b = 1 ∨ a = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { have : a ≠ 0, { rintro rfl, exact not_lt_of_le ha omega_pos }, left, use ha, { rw [← not_lt], intro hb, apply ne_of_gt _ h, refine lt_of_lt_of_le hb (le_mul_left this) }, { rintro rfl, apply this, rw [_root_.mul_zero] at h, subst h }}, right, by_cases h2a : a = 0, { right, exact h2a }, have hb : b ≠ 0, { rintro rfl, apply h2a, rw [mul_zero] at h, subst h }, left, rw [← h, mul_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with rfl|rfl|⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, contradiction, contradiction, rw [← ne] at h2a, rw [← one_le_iff_ne_zero] at h2a hb, norm_cast at h2a hb h ⊢, apply le_antisymm _ hb, rw [← not_lt], intro h2b, apply ne_of_gt _ h, conv_lhs { rw [← mul_one n] }, rwa [mul_lt_mul_left], apply nat.lt_of_succ_le h2a }, { rintro (⟨⟨ha, hab⟩, hb⟩|rfl|rfl), { rw [mul_eq_max_of_omega_le_left ha hb, max_eq_left hab] }, all_goals {simp}} end /-! ### Properties of `add` -/ /-- If `α` is an infinite type, then `α ⊕ α` and `α` have the same cardinality. -/ theorem add_eq_self {c : cardinal} (h : omega ≤ c) : c + c = c := le_antisymm (by simpa only [nat.cast_bit0, nat.cast_one, mul_eq_self h, two_mul] using mul_le_mul_right' ((nat_lt_omega 2).le.trans h) c) (self_le_add_left c c) /-- If `α` is an infinite type, then the cardinality of `α ⊕ β` is the maximum of the cardinalities of `α` and `β`. -/ theorem add_eq_max {a b : cardinal} (ha : omega ≤ a) : a + b = max a b := le_antisymm (add_eq_self (le_trans ha (le_max_left a b)) ▸ add_le_add (le_max_left _ _) (le_max_right _ _)) $ max_le (self_le_add_right _ _) (self_le_add_left _ _) theorem add_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a + b < c := lt_of_le_of_lt (add_le_add (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (add_lt_omega h h) hc) (λ h, by rw add_eq_self h; exact max_lt h1 h2) lemma eq_of_add_eq_of_omega_le {a b c : cardinal} (h : a + b = c) (ha : a < c) (hc : omega ≤ c) : b = c := begin apply le_antisymm, { rw [← h], apply self_le_add_left }, rw[← not_lt], intro hb, have : a + b < c := add_lt_of_lt hc ha hb, simpa [h, lt_irrefl] using this end lemma add_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) : a + b = a := by { rw [add_eq_max ha, max_eq_left hb] } lemma add_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) : a + b = b := by { rw [add_comm, add_eq_left hb ha] } lemma add_eq_left_iff {a b : cardinal} : a + b = a ↔ (max omega b ≤ a ∨ b = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { left, use ha, rw [← not_lt], intro hb, apply ne_of_gt _ h, exact lt_of_lt_of_le hb (self_le_add_left b a) }, right, rw [← h, add_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, norm_cast at h ⊢, rw [← add_right_inj, h, add_zero] }, { rintro (⟨h1, h2⟩|h3), rw [add_eq_max h1, max_eq_left h2], rw [h3, add_zero] } end lemma add_eq_right_iff {a b : cardinal} : a + b = b ↔ (max omega a ≤ b ∨ a = 0) := by { rw [add_comm, add_eq_left_iff] } lemma add_one_eq {a : cardinal} (ha : omega ≤ a) : a + 1 = a := have 1 ≤ a, from le_trans (le_of_lt one_lt_omega) ha, add_eq_left ha this protected lemma eq_of_add_eq_add_left {a b c : cardinal} (h : a + b = a + c) (ha : a < omega) : b = c := begin cases le_or_lt omega b with hb hb, { have : a < b := lt_of_lt_of_le ha hb, rw [add_eq_right hb (le_of_lt this), eq_comm] at h, rw [eq_of_add_eq_of_omega_le h this hb] }, { have hc : c < omega, { rw [← not_le], intro hc, apply lt_irrefl omega, apply lt_of_le_of_lt (le_trans hc (self_le_add_left _ a)), rw [← h], apply add_lt_omega ha hb }, rw [lt_omega] at *, rcases ha with ⟨n, rfl⟩, rcases hb with ⟨m, rfl⟩, rcases hc with ⟨k, rfl⟩, norm_cast at h ⊢, apply add_left_cancel h } end protected lemma eq_of_add_eq_add_right {a b c : cardinal} (h : a + b = c + b) (hb : b < omega) : a = c := by { rw [add_comm a b, add_comm c b] at h, exact cardinal.eq_of_add_eq_add_left h hb } /-! ### Properties about power -/ theorem pow_le {κ μ : cardinal.{u}} (H1 : omega ≤ κ) (H2 : μ < omega) : κ ^ μ ≤ κ := let ⟨n, H3⟩ := lt_omega.1 H2 in H3.symm ▸ (quotient.induction_on κ (λ α H1, nat.rec_on n (le_of_lt $ lt_of_lt_of_le (by rw [nat.cast_zero, power_zero]; from one_lt_omega) H1) (λ n ih, trans_rel_left _ (by { rw [nat.cast_succ, power_add, power_one]; exact mul_le_mul_right' ih _ }) (mul_eq_self H1))) H1) lemma power_self_eq {c : cardinal} (h : omega ≤ c) : c ^ c = 2 ^ c := begin apply le_antisymm, { apply le_trans (power_le_power_right $ le_of_lt $ cantor c), rw [← power_mul, mul_eq_self h] }, { convert power_le_power_right (le_trans (le_of_lt $ nat_lt_omega 2) h), apply nat.cast_two.symm } end lemma power_nat_le {c : cardinal.{u}} {n : ℕ} (h : omega ≤ c) : c ^ (n : cardinal.{u}) ≤ c := pow_le h (nat_lt_omega n) lemma powerlt_omega {c : cardinal} (h : omega ≤ c) : c ^< omega = c := begin apply le_antisymm, { rw [powerlt_le], intro c', rw [lt_omega], rintro ⟨n, rfl⟩, apply power_nat_le h }, convert le_powerlt one_lt_omega, rw [power_one] end lemma powerlt_omega_le (c : cardinal) : c ^< omega ≤ max c omega := begin cases le_or_gt omega c, { rw [powerlt_omega h], apply le_max_left }, rw [powerlt_le], intros c' hc', refine le_trans (le_of_lt $ power_lt_omega h hc') (le_max_right _ _) end /-! ### Computing cardinality of various types -/ theorem mk_list_eq_mk {α : Type u} (H1 : omega ≤ mk α) : mk (list α) = mk α := eq.symm $ le_antisymm ⟨⟨λ x, [x], λ x y H, (list.cons.inj H).1⟩⟩ $ calc mk (list α) = sum (λ n : ℕ, mk α ^ (n : cardinal.{u})) : mk_list_eq_sum_pow α ... ≤ sum (λ n : ℕ, mk α) : sum_le_sum _ _ $ λ n, pow_le H1 $ nat_lt_omega n ... = sum (λ n : ulift.{u} ℕ, mk α) : quotient.sound ⟨@sigma_congr_left _ _ (λ _, quotient.out (mk α)) equiv.ulift.symm⟩ ... = omega * mk α : sum_const _ _ ... = max (omega) (mk α) : mul_eq_max (le_refl _) H1 ... = mk α : max_eq_right H1 theorem mk_finset_eq_mk {α : Type u} (h : omega ≤ mk α) : mk (finset α) = mk α := eq.symm $ le_antisymm (mk_le_of_injective (λ x y, finset.singleton_inj.1)) $ calc mk (finset α) ≤ mk (list α) : mk_le_of_surjective list.to_finset_surjective ... = mk α : mk_list_eq_mk h lemma mk_bounded_set_le_of_omega_le (α : Type u) (c : cardinal) (hα : omega ≤ mk α) : mk {t : set α // mk t ≤ c} ≤ mk α ^ c := begin refine le_trans _ (by rw [←add_one_eq hα]), refine quotient.induction_on c _, clear c, intro β, fapply mk_le_of_surjective, { intro f, use sum.inl ⁻¹' range f, refine le_trans (mk_preimage_of_injective _ _ (λ x y, sum.inl.inj)) _, apply mk_range_le }, rintro ⟨s, ⟨g⟩⟩, use λ y, if h : ∃(x : s), g x = y then sum.inl (classical.some h).val else sum.inr ⟨⟩, apply subtype.eq, ext, split, { rintro ⟨y, h⟩, dsimp only at h, by_cases h' : ∃ (z : s), g z = y, { rw [dif_pos h'] at h, cases sum.inl.inj h, exact (classical.some h').2 }, { rw [dif_neg h'] at h, cases h }}, { intro h, have : ∃(z : s), g z = g ⟨x, h⟩, exact ⟨⟨x, h⟩, rfl⟩, use g ⟨x, h⟩, dsimp only, rw [dif_pos this], congr', suffices : classical.some this = ⟨x, h⟩, exact congr_arg subtype.val this, apply g.2, exact classical.some_spec this } end lemma mk_bounded_set_le (α : Type u) (c : cardinal) : mk {t : set α // mk t ≤ c} ≤ max (mk α) omega ^ c := begin transitivity mk {t : set (ulift.{u} nat ⊕ α) // mk t ≤ c}, { refine ⟨embedding.subtype_map _ _⟩, apply embedding.image, use sum.inr, apply sum.inr.inj, intros s hs, exact le_trans mk_image_le hs }, refine le_trans (mk_bounded_set_le_of_omega_le (ulift.{u} nat ⊕ α) c (self_le_add_right omega (mk α))) _, rw [max_comm, ←add_eq_max]; refl end lemma mk_bounded_subset_le {α : Type u} (s : set α) (c : cardinal.{u}) : mk {t : set α // t ⊆ s ∧ mk t ≤ c} ≤ max (mk s) omega ^ c := begin refine le_trans _ (mk_bounded_set_le s c), refine ⟨embedding.cod_restrict _ _ _⟩, use λ t, coe ⁻¹' t.1, { rintros ⟨t, ht1, ht2⟩ ⟨t', h1t', h2t'⟩ h, apply subtype.eq, dsimp only at h ⊢, refine (preimage_eq_preimage' _ _).1 h; rw [subtype.range_coe]; assumption }, rintro ⟨t, h1t, h2t⟩, exact le_trans (mk_preimage_of_injective _ _ subtype.val_injective) h2t end /-! ### Properties of `compl` -/ lemma mk_compl_of_omega_le {α : Type*} (s : set α) (h : omega ≤ #α) (h2 : #s < #α) : #(sᶜ : set α) = #α := by { refine eq_of_add_eq_of_omega_le _ h2 h, exact mk_sum_compl s } lemma mk_compl_finset_of_omega_le {α : Type*} (s : finset α) (h : omega ≤ #α) : #((↑s)ᶜ : set α) = #α := by { apply mk_compl_of_omega_le _ h, exact lt_of_lt_of_le (finset_card_lt_omega s) h } lemma mk_compl_eq_mk_compl_infinite {α : Type*} {s t : set α} (h : omega ≤ #α) (hs : #s < #α) (ht : #t < #α) : #(sᶜ : set α) = #(tᶜ : set α) := by { rw [mk_compl_of_omega_le s h hs, mk_compl_of_omega_le t h ht] } lemma mk_compl_eq_mk_compl_finite_lift {α : Type u} {β : Type v} {s : set α} {t : set β} (hα : #α < omega) (h1 : lift.{u (max v w)} (#α) = lift.{v (max u w)} (#β)) (h2 : lift.{u (max v w)} (#s) = lift.{v (max u w)} (#t)) : lift.{u (max v w)} (#(sᶜ : set α)) = lift.{v (max u w)} (#(tᶜ : set β)) := begin have hα' := hα, have h1' := h1, rw [← mk_sum_compl s, ← mk_sum_compl t] at h1, rw [← mk_sum_compl s, add_lt_omega_iff] at hα, lift #s to ℕ using hα.1 with n hn, lift #(sᶜ : set α) to ℕ using hα.2 with m hm, have : #(tᶜ : set β) < omega, { refine lt_of_le_of_lt (mk_subtype_le _) _, rw [← lift_lt, lift_omega, ← h1', ← lift_omega.{u (max v w)}, lift_lt], exact hα' }, lift #(tᶜ : set β) to ℕ using this with k hk, simp [nat_eq_lift_eq_iff] at h2, rw [nat_eq_lift_eq_iff.{v (max u w)}] at h2, simp [h2.symm] at h1 ⊢, norm_cast at h1, simp at h1, exact h1 end lemma mk_compl_eq_mk_compl_finite {α β : Type u} {s : set α} {t : set β} (hα : #α < omega) (h1 : #α = #β) (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set β) := by { rw [← lift_inj], apply mk_compl_eq_mk_compl_finite_lift hα; rw [lift_inj]; assumption } lemma mk_compl_eq_mk_compl_finite_same {α : Type*} {s t : set α} (hα : #α < omega) (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set α) := mk_compl_eq_mk_compl_finite hα rfl h /-! ### Extending an injection to an equiv -/ theorem extend_function {α β : Type*} {s : set α} (f : s ↪ β) (h : nonempty ((sᶜ : set α) ≃ ((range f)ᶜ : set β))) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin intros, have := h, cases this with g, let h : α ≃ β := (set.sum_compl (s : set α)).symm.trans ((sum_congr (equiv.of_injective f f.2) g).trans (set.sum_compl (range f))), refine ⟨h, _⟩, rintro ⟨x, hx⟩, simp [set.sum_compl_symm_apply_of_mem, hx] end theorem extend_function_finite {α β : Type*} {s : set α} (f : s ↪ β) (hs : #α < omega) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, rw [←lift_mk_eq, mk_compl_eq_mk_compl_finite_lift hs h], rw [mk_range_eq_lift], exact f.2 end theorem extend_function_of_lt {α β : Type*} {s : set α} (f : s ↪ β) (hs : #s < #α) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin cases (le_or_lt omega (#α)) with hα hα, { apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, cases cardinal.eq.mp (mk_compl_of_omega_le s hα hs) with g2, cases cardinal.eq.mp (mk_compl_of_omega_le (range f) _ _) with g3, { constructor, exact g2.trans (g.trans g3.symm) }, { rw [← lift_le, ← h], refine le_trans _ (lift_le.mpr hα), simp }, rwa [← lift_lt, ← h, mk_range_eq_lift, lift_lt], exact f.2 }, { exact extend_function_finite f hα h } end section bit /-! This section proves inequalities for `bit0` and `bit1`, enabling `simp` to solve inequalities for numeral cardinals. The complexity of the resulting algorithm is not good, as in some cases `simp` reduces an inequality to a disjunction of two situations, depending on whether a cardinal is finite or infinite. Since the evaluation of the branches is not lazy, this is bad. It is good enough for practical situations, though. For specific numbers, these inequalities could also be deduced from the corresponding inequalities of natural numbers using `norm_cast`: ``` example : (37 : cardinal) < 42 := by { norm_cast, norm_num } ``` -/ @[simp] lemma bit0_ne_zero (a : cardinal) : ¬bit0 a = 0 ↔ ¬a = 0 := by simp [bit0] @[simp] lemma bit1_ne_zero (a : cardinal) : ¬bit1 a = 0 := by simp [bit1] @[simp] lemma zero_lt_bit0 (a : cardinal) : 0 < bit0 a ↔ 0 < a := by { rw ←not_iff_not, simp [bit0], } @[simp] lemma zero_lt_bit1 (a : cardinal) : 0 < bit1 a := lt_of_lt_of_le zero_lt_one (self_le_add_left _ _) @[simp] lemma one_le_bit0 (a : cardinal) : 1 ≤ bit0 a ↔ 0 < a := ⟨λ h, (zero_lt_bit0 a).mp (lt_of_lt_of_le zero_lt_one h), λ h, le_trans (one_le_iff_pos.mpr h) (self_le_add_left a a)⟩ @[simp] lemma one_le_bit1 (a : cardinal) : 1 ≤ bit1 a := self_le_add_left _ _ theorem bit0_eq_self {c : cardinal} (h : omega ≤ c) : bit0 c = c := add_eq_self h @[simp] theorem bit0_lt_omega {c : cardinal} : bit0 c < omega ↔ c < omega := by simp [bit0, add_lt_omega_iff] @[simp] theorem omega_le_bit0 {c : cardinal} : omega ≤ bit0 c ↔ omega ≤ c := by { rw ← not_iff_not, simp } @[simp] theorem bit1_eq_self_iff {c : cardinal} : bit1 c = c ↔ omega ≤ c := begin by_cases h : omega ≤ c, { simp only [bit1, bit0_eq_self h, h, eq_self_iff_true, add_one_of_omega_le] }, { simp only [h, iff_false], apply ne_of_gt, rcases lt_omega.1 (not_le.1 h) with ⟨n, rfl⟩, norm_cast, dsimp [bit1, bit0], linarith } end @[simp] theorem bit1_lt_omega {c : cardinal} : bit1 c < omega ↔ c < omega := by simp [bit1, bit0, add_lt_omega_iff, one_lt_omega] @[simp] theorem omega_le_bit1 {c : cardinal} : omega ≤ bit1 c ↔ omega ≤ c := by { rw ← not_iff_not, simp } @[simp] lemma bit0_le_bit0 {a b : cardinal} : bit0 a ≤ bit0 b ↔ a ≤ b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a ≤ b := le_of_lt (lt_of_lt_of_le ha hb), have I2 : bit0 a ≤ b := le_trans (le_of_lt (bit0_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit0_le_bit1 {a b : cardinal} : bit0 a ≤ bit1 b ↔ a ≤ b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit1_eq_self_iff.2 hb], }, { rw bit0_eq_self ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a ≤ b := le_of_lt (lt_of_lt_of_le ha hb), have I2 : bit0 a ≤ b := le_trans (le_of_lt (bit0_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_le_bit1 {a b : cardinal} : bit1 a ≤ bit1 b ↔ a ≤ b := begin split, { assume h, apply bit0_le_bit1.1 (le_trans (self_le_add_right (bit0 a) 1) h) }, { assume h, calc a + a + 1 ≤ a + b + 1 : add_le_add_right (add_le_add_left h a) 1 ... ≤ b + b + 1 : add_le_add_right (add_le_add_right h b) 1 } end @[simp] lemma bit1_le_bit0 {a b : cardinal} : bit1 a ≤ bit0 b ↔ (a < b ∨ (a ≤ b ∧ omega ≤ a)) := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { simp only [bit1_eq_self_iff.mpr ha, bit0_eq_self hb, ha, and_true], refine ⟨λ h, or.inr h, λ h, _⟩, cases h, { exact le_of_lt h }, { exact h } }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2, le_of_not_ge I1] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := lt_of_lt_of_le ha hb, have I2 : bit1 a ≤ b := le_trans (le_of_lt (bit1_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp [not_le.mpr ha], } end @[simp] lemma bit0_lt_bit0 {a b : cardinal} : bit0 a < bit0 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_trans ha (le_of_lt h) }, have I2 : ¬ (a < bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := lt_of_lt_of_le ha hb, have I2 : bit0 a < b := lt_of_lt_of_le (bit0_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_lt_bit0 {a b : cardinal} : bit1 a < bit0 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit1_eq_self_iff.2 ha, bit0_eq_self hb], }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit1 a < b := lt_of_lt_of_le (bit1_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_lt_bit1 {a b : cardinal} : bit1 a < bit1 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit1_eq_self_iff.2 ha, bit1_eq_self_iff.2 hb], }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit1 a < b := lt_of_lt_of_le (bit1_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit0_lt_bit1 {a b : cardinal} : bit0 a < bit1 b ↔ (a < b ∨ (a ≤ b ∧ a < omega)) := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { simp [bit0_eq_self ha, bit1_eq_self_iff.2 hb, not_lt.mpr ha] }, { rw bit0_eq_self ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2, not_lt.mpr ha] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit0 a < b := lt_of_lt_of_le (bit0_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp only [ha, and_true, nat.bit0_lt_bit1_iff], refine ⟨λ h, or.inr h, λ h, _⟩, cases h, { exact le_of_lt h }, { exact h } } end lemma one_lt_two : (1 : cardinal) < 2 := -- This strategy works generally to prove inequalities between numerals in `cardinality`. by { norm_cast, norm_num } @[simp] lemma one_lt_bit0 {a : cardinal} : 1 < bit0 a ↔ 0 < a := by simp [← bit1_zero] @[simp] lemma one_lt_bit1 (a : cardinal) : 1 < bit1 a ↔ 0 < a := by simp [← bit1_zero] @[simp] lemma one_le_one : (1 : cardinal) ≤ 1 := le_refl _ end bit end cardinal
68c38483812a498e71d8027d815ed3908136e732
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/smt/default.lean
d0e7e0373f51e6434e3ffe941f58cf5c50cb6b5f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
340
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.smt.congruence_closure init.cc_lemmas import init.meta.smt.ematch import init.meta.smt.smt_tactic init.meta.smt.interactive import init.meta.smt.rsimp
210004195272947b0bc2b8c0f9d659b68487987c
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/monad/coequalizer.lean
cf6e1c7a2176728f431aa0a3732d448dfb5bc280
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
3,775
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.shapes.reflexive import category_theory.limits.shapes.split_coequalizer import category_theory.monad.algebra /-! # Special coequalizers associated to a monad Associated to a monad `T : C ⥤ C` we have important coequalizer constructions: Any algebra is a coequalizer (in the category of algebras) of free algebras. Furthermore, this coequalizer is reflexive. In `C`, this cofork diagram is a split coequalizer (in particular, it is still a coequalizer). This split coequalizer is known as the Beck coequalizer (as it features heavily in Beck's monadicity theorem). -/ universes v₁ u₁ namespace category_theory namespace monad open limits variables {C : Type u₁} variables [category.{v₁} C] variables {T : monad C} (X : algebra T) /-! Show that any algebra is a coequalizer of free algebras. -/ /-- The top map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.top_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A := (monad.free T).map X.a /-- The bottom map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.bottom_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A := { f := T.μ.app X.A, h' := T.assoc X.A } /-- The cofork map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.π : (monad.free T).obj X.A ⟶ X := { f := X.a, h' := X.assoc.symm } lemma free_coequalizer.condition : free_coequalizer.top_map X ≫ free_coequalizer.π X = free_coequalizer.bottom_map X ≫ free_coequalizer.π X := algebra.hom.ext _ _ X.assoc.symm instance : is_reflexive_pair (free_coequalizer.top_map X) (free_coequalizer.bottom_map X) := begin apply is_reflexive_pair.mk' _ _ _, apply (free T).map (T.η.app X.A), { ext, dsimp, rw [← functor.map_comp, X.unit, functor.map_id] }, { ext, apply monad.right_unit } end /-- Construct the Beck cofork in the category of algebras. This cofork is reflexive as well as a coequalizer. -/ @[simps] def beck_algebra_cofork : cofork (free_coequalizer.top_map X) (free_coequalizer.bottom_map X) := cofork.of_π _ (free_coequalizer.condition X) /-- The cofork constructed is a colimit. This shows that any algebra is a (reflexive) coequalizer of free algebras. -/ def beck_algebra_coequalizer : is_colimit (beck_algebra_cofork X) := cofork.is_colimit.mk' _ $ λ s, begin have h₁ : (T : C ⥤ C).map X.a ≫ s.π.f = T.μ.app X.A ≫ s.π.f := congr_arg monad.algebra.hom.f s.condition, have h₂ : (T : C ⥤ C).map s.π.f ≫ s.X.a = T.μ.app X.A ≫ s.π.f := s.π.h, refine ⟨⟨T.η.app _ ≫ s.π.f, _⟩, _, _⟩, { dsimp, rw [functor.map_comp, category.assoc, h₂, monad.right_unit_assoc, (show X.a ≫ _ ≫ _ = _, from T.η.naturality_assoc _ _), h₁, monad.left_unit_assoc] }, { ext, simpa [← T.η.naturality_assoc, T.left_unit_assoc] using T.η.app ((T : C ⥤ C).obj X.A) ≫= h₁ }, { intros m hm, ext, dsimp only, rw ← hm, apply (X.unit_assoc _).symm } end /-- The Beck cofork is a split coequalizer. -/ def beck_split_coequalizer : is_split_coequalizer (T.map X.a) (T.μ.app _) X.a := ⟨T.η.app _, T.η.app _, X.assoc.symm, X.unit, T.left_unit _, (T.η.naturality _).symm⟩ /-- This is the Beck cofork. It is a split coequalizer, in particular a coequalizer. -/ @[simps] def beck_cofork : cofork (T.map X.a) (T.μ.app _) := (beck_split_coequalizer X).as_cofork /-- The Beck cofork is a coequalizer. -/ def beck_coequalizer : is_colimit (beck_cofork X) := (beck_split_coequalizer X).is_coequalizer end monad end category_theory
31ab930385177dbc7b722c47258f2f71c99b0069
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/hott/bool.lean
61d2c680d2c9ed85d65804f7482aea6c592d0313
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
220
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura inductive bool : Type := | true : bool | false : bool
345a3e88e7eddf80b5db1ed10f3292a79bc3d0a8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/set_theory/ordinal/arithmetic.lean
810d62bb70b879f38efccd67af1e7e6b2e87a14b
[ "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
98,556
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn, Violeta Hernández Palacios -/ import set_theory.ordinal.basic import tactic.by_contra /-! # Ordinal arithmetic Ordinals have an addition (corresponding to disjoint union) that turns them into an additive monoid, and a multiplication (corresponding to the lexicographic order on the product) that turns them into a monoid. One can also define correspondingly a subtraction, a division, a successor function, a power function and a logarithm function. We also define limit ordinals and prove the basic induction principle on ordinals separating successor ordinals and limit ordinals, in `limit_rec_on`. ## Main definitions and results * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. * `o₁ - o₂` is the unique ordinal `o` such that `o₂ + o = o₁`, when `o₂ ≤ o₁`. * `o₁ * o₂` is the lexicographic order on `o₂ × o₁`. * `o₁ / o₂` is the ordinal `o` such that `o₁ = o₂ * o + o'` with `o' < o₂`. We also define the divisibility predicate, and a modulo operation. * `order.succ o = o + 1` is the successor of `o`. * `pred o` if the predecessor of `o`. If `o` is not a successor, we set `pred o = o`. We also define the power function and the logarithm function on ordinals, and discuss the properties of casts of natural numbers of and of `ω` with respect to these operations. Some properties of the operations are also used to discuss general tools on ordinals: * `is_limit o`: an ordinal is a limit ordinal if it is neither `0` nor a successor. * `limit_rec_on` is the main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. * `is_normal`: a function `f : ordinal → ordinal` satisfies `is_normal` if it is strictly increasing and order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. * `enum_ord`: enumerates an unbounded set of ordinals by the ordinals themselves. * `sup`, `lsub`: the supremum / least strict upper bound of an indexed family of ordinals in `Type u`, as an ordinal in `Type u`. * `bsup`, `blsub`: the supremum / least strict upper bound of a set of ordinals indexed by ordinals less than a given ordinal `o`. Various other basic arithmetic results are given in `principal.lean` instead. -/ noncomputable theory open function cardinal set equiv order open_locale classical cardinal ordinal universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} namespace ordinal /-! ### Further properties of addition on ordinals -/ @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans (rel_iso.sum_lex_congr (rel_iso.preimage equiv.ulift _) (rel_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := by { rw [←add_one_eq_succ, lift_add, lift_one], refl } instance add_contravariant_class_le : contravariant_class ordinal.{u} ordinal.{u} (+) (≤) := ⟨λ a b c, induction_on a $ λ α r hr, induction_on b $ λ β₁ s₁ hs₁, induction_on c $ λ β₂ s₂ hs₂ ⟨f⟩, ⟨have fl : ∀ a, f (sum.inl a) = sum.inl a := λ a, by simpa only [initial_seg.trans_apply, initial_seg.le_add_apply] using @initial_seg.eq _ _ _ _ (@sum.lex.is_well_order _ _ _ _ hr hs₂) ((initial_seg.le_add r s₁).trans f) (initial_seg.le_add r s₂) a, have ∀ b, {b' // f (sum.inr b) = sum.inr b'}, begin intro b, cases e : f (sum.inr b), { rw ← fl at e, have := f.inj' e, contradiction }, { exact ⟨_, rfl⟩ } end, let g (b) := (this b).1 in have fr : ∀ b, f (sum.inr b) = sum.inr (g b), from λ b, (this b).2, ⟨⟨⟨g, λ x y h, by injection f.inj' (by rw [fr, fr, h] : f (sum.inr x) = f (sum.inr y))⟩, λ a b, by simpa only [sum.lex_inr_inr, fr, rel_embedding.coe_fn_to_embedding, initial_seg.coe_fn_to_rel_embedding, embedding.coe_fn_mk] using @rel_embedding.map_rel_iff _ _ _ _ f.to_rel_embedding (sum.inr a) (sum.inr b)⟩, λ a b H, begin rcases f.init' (by rw fr; exact sum.lex_inr_inr.2 H) with ⟨a'|a', h⟩, { rw fl at h, cases h }, { rw fr at h, exact ⟨a', sum.inr.inj h⟩ } end⟩⟩⟩ theorem add_left_cancel (a) {b c : ordinal} : a + b = a + c ↔ b = c := by simp only [le_antisymm_iff, add_le_add_iff_left] private theorem add_lt_add_iff_left' (a) {b c : ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] instance add_covariant_class_lt : covariant_class ordinal.{u} ordinal.{u} (+) (<) := ⟨λ a b c, (add_lt_add_iff_left' a).2⟩ instance add_contravariant_class_lt : contravariant_class ordinal.{u} ordinal.{u} (+) (<) := ⟨λ a b c, (add_lt_add_iff_left' a).1⟩ instance add_swap_contravariant_class_lt : contravariant_class ordinal.{u} ordinal.{u} (swap (+)) (<) := ⟨λ a b c, lt_imp_lt_of_le_imp_le (λ h, add_le_add_right h _)⟩ theorem add_le_add_iff_right {a b : ordinal} : ∀ n : ℕ, a + n ≤ b + n ↔ a ≤ b | 0 := by simp | (n+1) := by rw [nat_cast_succ, add_succ, add_succ, succ_le_succ_iff, add_le_add_iff_right] theorem add_right_cancel {a b : ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] theorem add_eq_zero_iff {a b : ordinal} : a + b = 0 ↔ (a = 0 ∧ b = 0) := induction_on a $ λ α r _, induction_on b $ λ β s _, begin simp_rw [←type_sum_lex, type_eq_zero_iff_is_empty], exact is_empty_sum end theorem left_eq_zero_of_add_eq_zero {a b : ordinal} (h : a + b = 0) : a = 0 := (add_eq_zero_iff.1 h).1 theorem right_eq_zero_of_add_eq_zero {a b : ordinal} (h : a + b = 0) : b = 0 := (add_eq_zero_iff.1 h).2 /-! ### The predecessor of an ordinal -/ /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : ordinal) : ordinal := if h : ∃ a, o = succ a then classical.some h else o @[simp] theorem pred_succ (o) : pred (succ o) = o := by have h : ∃ a, succ o = succ a := ⟨_, rfl⟩; simpa only [pred, dif_pos h] using (succ_injective $ classical.some_spec h).symm theorem pred_le_self (o) : pred o ≤ o := if h : ∃ a, o = succ a then let ⟨a, e⟩ := h in by rw [e, pred_succ]; exact le_succ a else by rw [pred, dif_neg h] theorem pred_eq_iff_not_succ {o} : pred o = o ↔ ¬ ∃ a, o = succ a := ⟨λ e ⟨a, e'⟩, by rw [e', pred_succ] at e; exact (lt_succ a).ne e, λ h, dif_neg h⟩ theorem pred_eq_iff_not_succ' {o} : pred o = o ↔ ∀ a, o ≠ succ a := by simpa using pred_eq_iff_not_succ theorem pred_lt_iff_is_succ {o} : pred o < o ↔ ∃ a, o = succ a := iff.trans (by simp only [le_antisymm_iff, pred_le_self, true_and, not_le]) (iff_not_comm.1 pred_eq_iff_not_succ).symm @[simp] theorem pred_zero : pred 0 = 0 := pred_eq_iff_not_succ'.2 $ λ a, (succ_ne_zero a).symm theorem succ_pred_iff_is_succ {o} : succ (pred o) = o ↔ ∃ a, o = succ a := ⟨λ e, ⟨_, e.symm⟩, λ ⟨a, e⟩, by simp only [e, pred_succ]⟩ theorem succ_lt_of_not_succ {o b : ordinal} (h : ¬ ∃ a, o = succ a) : succ b < o ↔ b < o := ⟨(lt_succ b).trans, λ l, lt_of_le_of_ne (succ_le_of_lt l) (λ e, h ⟨_, e.symm⟩)⟩ theorem lt_pred {a b} : a < pred b ↔ succ a < b := if h : ∃ a, b = succ a then let ⟨c, e⟩ := h in by rw [e, pred_succ, succ_lt_succ_iff] else by simp only [pred, dif_neg h, succ_lt_of_not_succ h] theorem pred_le {a b} : pred a ≤ b ↔ a ≤ succ b := le_iff_le_iff_lt_iff_lt.2 lt_pred @[simp] theorem lift_is_succ {o} : (∃ a, lift o = succ a) ↔ (∃ a, o = succ a) := ⟨λ ⟨a, h⟩, let ⟨b, e⟩ := lift_down $ show a ≤ lift o, from le_of_lt $ h.symm ▸ lt_succ a in ⟨b, lift_inj.1 $ by rw [h, ← e, lift_succ]⟩, λ ⟨a, h⟩, ⟨lift a, by simp only [h, lift_succ]⟩⟩ @[simp] theorem lift_pred (o) : lift (pred o) = pred (lift o) := if h : ∃ a, o = succ a then by cases h with a e; simp only [e, pred_succ, lift_succ] else by rw [pred_eq_iff_not_succ.2 h, pred_eq_iff_not_succ.2 (mt lift_is_succ.1 h)] /-! ### Limit ordinals -/ /-- A limit ordinal is an ordinal which is not zero and not a successor. -/ def is_limit (o : ordinal) : Prop := o ≠ 0 ∧ ∀ a < o, succ a < o theorem is_limit.succ_lt {o a : ordinal} (h : is_limit o) : a < o → succ a < o := h.2 a theorem not_zero_is_limit : ¬ is_limit 0 | ⟨h, _⟩ := h rfl theorem not_succ_is_limit (o) : ¬ is_limit (succ o) | ⟨_, h⟩ := lt_irrefl _ (h _ (lt_succ o)) theorem not_succ_of_is_limit {o} (h : is_limit o) : ¬ ∃ a, o = succ a | ⟨a, e⟩ := not_succ_is_limit a (e ▸ h) theorem succ_lt_of_is_limit {o a : ordinal} (h : is_limit o) : succ a < o ↔ a < o := ⟨(lt_succ a).trans, h.2 _⟩ theorem le_succ_of_is_limit {o} (h : is_limit o) {a} : o ≤ succ a ↔ o ≤ a := le_iff_le_iff_lt_iff_lt.2 $ succ_lt_of_is_limit h theorem limit_le {o} (h : is_limit o) {a} : o ≤ a ↔ ∀ x < o, x ≤ a := ⟨λ h x l, l.le.trans h, λ H, (le_succ_of_is_limit h).1 $ le_of_not_lt $ λ hn, not_lt_of_le (H _ hn) (lt_succ a)⟩ theorem lt_limit {o} (h : is_limit o) {a} : a < o ↔ ∃ x < o, a < x := by simpa only [not_ball, not_le] using not_congr (@limit_le _ h a) @[simp] theorem lift_is_limit (o) : is_limit (lift o) ↔ is_limit o := and_congr (not_congr $ by simpa only [lift_zero] using @lift_inj o 0) ⟨λ H a h, lift_lt.1 $ by simpa only [lift_succ] using H _ (lift_lt.2 h), λ H a h, begin obtain ⟨a', rfl⟩ := lift_down h.le, rw [←lift_succ, lift_lt], exact H a' (lift_lt.1 h) end⟩ theorem is_limit.pos {o : ordinal} (h : is_limit o) : 0 < o := lt_of_le_of_ne (ordinal.zero_le _) h.1.symm theorem is_limit.one_lt {o : ordinal} (h : is_limit o) : 1 < o := by simpa only [succ_zero] using h.2 _ h.pos theorem is_limit.nat_lt {o : ordinal} (h : is_limit o) : ∀ n : ℕ, (n : ordinal) < o | 0 := h.pos | (n+1) := h.2 _ (is_limit.nat_lt n) theorem zero_or_succ_or_limit (o : ordinal) : o = 0 ∨ (∃ a, o = succ a) ∨ is_limit o := if o0 : o = 0 then or.inl o0 else if h : ∃ a, o = succ a then or.inr (or.inl h) else or.inr $ or.inr ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ /-- Main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. -/ @[elab_as_eliminator] def limit_rec_on {C : ordinal → Sort*} (o : ordinal) (H₁ : C 0) (H₂ : ∀ o, C o → C (succ o)) (H₃ : ∀ o, is_limit o → (∀ o' < o, C o') → C o) : C o := lt_wf.fix (λ o IH, if o0 : o = 0 then by rw o0; exact H₁ else if h : ∃ a, o = succ a then by rw ← succ_pred_iff_is_succ.2 h; exact H₂ _ (IH _ $ pred_lt_iff_is_succ.2 h) else H₃ _ ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ IH) o @[simp] theorem limit_rec_on_zero {C} (H₁ H₂ H₃) : @limit_rec_on C 0 H₁ H₂ H₃ = H₁ := by rw [limit_rec_on, lt_wf.fix_eq, dif_pos rfl]; refl @[simp] theorem limit_rec_on_succ {C} (o H₁ H₂ H₃) : @limit_rec_on C (succ o) H₁ H₂ H₃ = H₂ o (@limit_rec_on C o H₁ H₂ H₃) := begin have h : ∃ a, succ o = succ a := ⟨_, rfl⟩, rw [limit_rec_on, lt_wf.fix_eq, dif_neg (succ_ne_zero o), dif_pos h], generalize : limit_rec_on._proof_2 (succ o) h = h₂, generalize : limit_rec_on._proof_3 (succ o) h = h₃, revert h₂ h₃, generalize e : pred (succ o) = o', intros, rw pred_succ at e, subst o', refl end @[simp] theorem limit_rec_on_limit {C} (o H₁ H₂ H₃ h) : @limit_rec_on C o H₁ H₂ H₃ = H₃ o h (λ x h, @limit_rec_on C x H₁ H₂ H₃) := by rw [limit_rec_on, lt_wf.fix_eq, dif_neg h.1, dif_neg (not_succ_of_is_limit h)]; refl instance order_top_out_succ (o : ordinal) : order_top (succ o).out.α := ⟨_, le_enum_succ⟩ theorem enum_succ_eq_top {o : ordinal} : enum (<) o (by { rw type_lt, exact lt_succ o }) = (⊤ : (succ o).out.α) := rfl lemma has_succ_of_type_succ_lt {α} {r : α → α → Prop} [wo : is_well_order α r] (h : ∀ a < type r, succ a < type r) (x : α) : ∃ y, r x y := begin use enum r (succ (typein r x)) (h _ (typein_lt_type r x)), convert (enum_lt_enum (typein_lt_type r x) _).mpr (lt_succ _), rw [enum_typein] end theorem out_no_max_of_succ_lt {o : ordinal} (ho : ∀ a < o, succ a < o) : no_max_order o.out.α := ⟨has_succ_of_type_succ_lt (by rwa type_lt)⟩ lemma bounded_singleton {r : α → α → Prop} [is_well_order α r] (hr : (type r).is_limit) (x) : bounded r {x} := begin refine ⟨enum r (succ (typein r x)) (hr.2 _ (typein_lt_type r x)), _⟩, intros b hb, rw mem_singleton_iff.1 hb, nth_rewrite 0 ←enum_typein r x, rw @enum_lt_enum _ r, apply lt_succ end lemma type_subrel_lt (o : ordinal.{u}) : type (subrel (<) {o' : ordinal | o' < o}) = ordinal.lift.{u+1} o := begin refine quotient.induction_on o _, rintro ⟨α, r, wo⟩, resetI, apply quotient.sound, constructor, symmetry, refine (rel_iso.preimage equiv.ulift r).trans (enum_iso r).symm end lemma mk_initial_seg (o : ordinal.{u}) : #{o' : ordinal | o' < o} = cardinal.lift.{u+1} o.card := by rw [lift_card, ←type_subrel_lt, card_type] /-! ### Normal ordinal functions -/ /-- A normal ordinal function is a strictly increasing function which is order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. -/ def is_normal (f : ordinal → ordinal) : Prop := (∀ o, f o < f (succ o)) ∧ ∀ o, is_limit o → ∀ a, f o ≤ a ↔ ∀ b < o, f b ≤ a theorem is_normal.limit_le {f} (H : is_normal f) : ∀ {o}, is_limit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := H.2 theorem is_normal.limit_lt {f} (H : is_normal f) {o} (h : is_limit o) {a} : a < f o ↔ ∃ b < o, a < f b := not_iff_not.1 $ by simpa only [exists_prop, not_exists, not_and, not_lt] using H.2 _ h a theorem is_normal.strict_mono {f} (H : is_normal f) : strict_mono f := λ a b, limit_rec_on b (not.elim (not_lt_of_le $ ordinal.zero_le _)) (λ b IH h, (lt_or_eq_of_le (le_of_lt_succ h)).elim (λ h, (IH h).trans (H.1 _)) (λ e, e ▸ H.1 _)) (λ b l IH h, lt_of_lt_of_le (H.1 a) ((H.2 _ l _).1 le_rfl _ (l.2 _ h))) theorem is_normal.monotone {f} (H : is_normal f) : monotone f := H.strict_mono.monotone theorem is_normal_iff_strict_mono_limit (f : ordinal → ordinal) : is_normal f ↔ (strict_mono f ∧ ∀ o, is_limit o → ∀ a, (∀ b < o, f b ≤ a) → f o ≤ a) := ⟨λ hf, ⟨hf.strict_mono, λ a ha c, (hf.2 a ha c).2⟩, λ ⟨hs, hl⟩, ⟨λ a, hs (lt_succ a), λ a ha c, ⟨λ hac b hba, ((hs hba).trans_le hac).le, hl a ha c⟩⟩⟩ theorem is_normal.lt_iff {f} (H : is_normal f) {a b} : f a < f b ↔ a < b := strict_mono.lt_iff_lt $ H.strict_mono theorem is_normal.le_iff {f} (H : is_normal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem is_normal.inj {f} (H : is_normal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem is_normal.self_le {f} (H : is_normal f) (a) : a ≤ f a := lt_wf.self_le_of_strict_mono H.strict_mono a theorem is_normal.le_set {f o} (H : is_normal f) (p : set ordinal) (p0 : p.nonempty) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f a ≤ o := ⟨λ h a pa, (H.le_iff.2 ((H₂ _).1 le_rfl _ pa)).trans h, λ h, begin revert H₂, refine limit_rec_on b (λ H₂, _) (λ S _ H₂, _) (λ S L _ H₂, (H.2 _ L _).2 (λ a h', _)), { cases p0 with x px, have := ordinal.le_zero.1 ((H₂ _).1 (ordinal.zero_le _) _ px), rw this at px, exact h _ px }, { rcases not_ball.1 (mt (H₂ S).2 $ (lt_succ S).not_le) with ⟨a, h₁, h₂⟩, exact (H.le_iff.2 $ succ_le_of_lt $ not_le.1 h₂).trans (h _ h₁) }, { rcases not_ball.1 (mt (H₂ a).2 h'.not_le) with ⟨b, h₁, h₂⟩, exact (H.le_iff.2 $ (not_le.1 h₂).le).trans (h _ h₁) } end⟩ theorem is_normal.le_set' {f o} (H : is_normal f) (p : set α) (p0 : p.nonempty) (g : α → ordinal) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, g a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f (g a) ≤ o := by simpa [H₂] using H.le_set (g '' p) (p0.image g) b theorem is_normal.refl : is_normal id := ⟨lt_succ, λ o l a, limit_le l⟩ theorem is_normal.trans {f g} (H₁ : is_normal f) (H₂ : is_normal g) : is_normal (f ∘ g) := ⟨λ x, H₁.lt_iff.2 (H₂.1 _), λ o l a, H₁.le_set' (< o) ⟨_, l.pos⟩ g _ (λ c, H₂.2 _ l _)⟩ theorem is_normal.is_limit {f} (H : is_normal f) {o} (l : is_limit o) : is_limit (f o) := ⟨ne_of_gt $ (ordinal.zero_le _).trans_lt $ H.lt_iff.2 l.pos, λ a h, let ⟨b, h₁, h₂⟩ := (H.limit_lt l).1 h in (succ_le_of_lt h₂).trans_lt (H.lt_iff.2 h₁)⟩ theorem is_normal.le_iff_eq {f} (H : is_normal f) {a} : f a ≤ a ↔ f a = a := (H.self_le a).le_iff_eq theorem add_le_of_limit {a b c : ordinal} (h : is_limit b) : a + b ≤ c ↔ ∀ b' < b, a + b' ≤ c := ⟨λ h b' l, (add_le_add_left l.le _).trans h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _ h H l, begin resetI, suffices : ∀ x : β, sum.lex r s (sum.inr x) (enum _ _ l), { cases enum _ _ l with x x, { cases this (enum s 0 h.pos) }, { exact irrefl _ (this _) } }, intros x, rw [←typein_lt_typein (sum.lex r s), typein_enum], have := H _ (h.2 _ (typein_lt_type s x)), rw [add_succ, succ_le_iff] at this, refine (rel_embedding.of_monotone (λ a, _) (λ a b, _)).ordinal_type_le.trans_lt this, { rcases a with ⟨a | b, h⟩, { exact sum.inl a }, { exact sum.inr ⟨b, by cases h; assumption⟩ } }, { rcases a with ⟨a | a, h₁⟩; rcases b with ⟨b | b, h₂⟩; cases h₁; cases h₂; rintro ⟨⟩; constructor; assumption } end) h H⟩ theorem add_is_normal (a : ordinal) : is_normal ((+) a) := ⟨λ b, (add_lt_add_iff_left a).2 (lt_succ b), λ b l c, add_le_of_limit l⟩ theorem add_is_limit (a) {b} : is_limit b → is_limit (a + b) := (add_is_normal a).is_limit alias add_is_limit ← is_limit.add /-! ### Subtraction on ordinals-/ /-- The set in the definition of subtraction is nonempty. -/ theorem sub_nonempty {a b : ordinal} : {o | a ≤ b + o}.nonempty := ⟨a, le_add_left _ _⟩ /-- `a - b` is the unique ordinal satisfying `b + (a - b) = a` when `b ≤ a`. -/ instance : has_sub ordinal := ⟨λ a b, Inf {o | a ≤ b + o}⟩ theorem le_add_sub (a b : ordinal) : a ≤ b + (a - b) := Inf_mem sub_nonempty theorem sub_le {a b c : ordinal} : a - b ≤ c ↔ a ≤ b + c := ⟨λ h, (le_add_sub a b).trans (add_le_add_left h _), λ h, cInf_le' h⟩ theorem lt_sub {a b c : ordinal} : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le sub_le theorem add_sub_cancel (a b : ordinal) : a + b - a = b := le_antisymm (sub_le.2 $ le_rfl) ((add_le_add_iff_left a).1 $ le_add_sub _ _) theorem sub_eq_of_add_eq {a b c : ordinal} (h : a + b = c) : c - a = b := h ▸ add_sub_cancel _ _ theorem sub_le_self (a b : ordinal) : a - b ≤ a := sub_le.2 $ le_add_left _ _ protected theorem add_sub_cancel_of_le {a b : ordinal} (h : b ≤ a) : b + (a - b) = a := (le_add_sub a b).antisymm' begin rcases zero_or_succ_or_limit (a-b) with e|⟨c,e⟩|l, { simp only [e, add_zero, h] }, { rw [e, add_succ, succ_le_iff, ← lt_sub, e], exact lt_succ c }, { exact (add_le_of_limit l).2 (λ c l, (lt_sub.1 l).le) } end theorem le_sub_of_le {a b c : ordinal} (h : b ≤ a) : c ≤ a - b ↔ b + c ≤ a := by rw [←add_le_add_iff_left b, ordinal.add_sub_cancel_of_le h] theorem sub_lt_of_le {a b c : ordinal} (h : b ≤ a) : a - b < c ↔ a < b + c := lt_iff_lt_of_le_iff_le (le_sub_of_le h) instance : has_exists_add_of_le ordinal := ⟨λ a b h, ⟨_, (ordinal.add_sub_cancel_of_le h).symm⟩⟩ @[simp] theorem sub_zero (a : ordinal) : a - 0 = a := by simpa only [zero_add] using add_sub_cancel 0 a @[simp] theorem zero_sub (a : ordinal) : 0 - a = 0 := by rw ← ordinal.le_zero; apply sub_le_self @[simp] theorem sub_self (a : ordinal) : a - a = 0 := by simpa only [add_zero] using add_sub_cancel a 0 protected theorem sub_eq_zero_iff_le {a b : ordinal} : a - b = 0 ↔ a ≤ b := ⟨λ h, by simpa only [h, add_zero] using le_add_sub a b, λ h, by rwa [← ordinal.le_zero, sub_le, add_zero]⟩ theorem sub_sub (a b c : ordinal) : a - b - c = a - (b + c) := eq_of_forall_ge_iff $ λ d, by rw [sub_le, sub_le, sub_le, add_assoc] theorem add_sub_add_cancel (a b c : ordinal) : a + b - (a + c) = b - c := by rw [← sub_sub, add_sub_cancel] theorem sub_is_limit {a b} (l : is_limit a) (h : b < a) : is_limit (a - b) := ⟨ne_of_gt $ lt_sub.2 $ by rwa add_zero, λ c h, by rw [lt_sub, add_succ]; exact l.2 _ (lt_sub.1 h)⟩ @[simp] theorem one_add_omega : 1 + ω = ω := begin refine le_antisymm _ (le_add_left _ _), rw [omega, ← lift_one.{0}, ← lift_add, lift_le, ← type_unit, ← type_sum_lex], refine ⟨rel_embedding.collapse (rel_embedding.of_monotone _ _)⟩, { apply sum.rec, exact λ _, 0, exact nat.succ }, { intros a b, cases a; cases b; intro H; cases H with _ _ H _ _ H; [cases H, exact nat.succ_pos _, exact nat.succ_lt_succ H] } end @[simp, priority 990] theorem one_add_of_omega_le {o} (h : ω ≤ o) : 1 + o = o := by rw [← ordinal.add_sub_cancel_of_le h, ← add_assoc, one_add_omega] /-! ### Multiplication of ordinals-/ /-- The multiplication of ordinals `o₁` and `o₂` is the (well founded) lexicographic order on `o₂ × o₁`. -/ instance : monoid ordinal.{u} := { mul := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, ⟦⟨β × α, prod.lex s r, by exactI prod.lex.is_well_order⟩⟧ : Well_order → Well_order → ordinal) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨rel_iso.prod_lex_congr g f⟩, one := 1, mul_assoc := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, eq.symm $ quotient.sound ⟨⟨prod_assoc _ _ _, λ a b, begin rcases a with ⟨⟨a₁, a₂⟩, a₃⟩, rcases b with ⟨⟨b₁, b₂⟩, b₃⟩, simp [prod.lex_def, and_or_distrib_left, or_assoc, and_assoc] end⟩⟩, mul_one := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨punit_prod _, λ a b, by rcases a with ⟨⟨⟨⟩⟩, a⟩; rcases b with ⟨⟨⟨⟩⟩, b⟩; simp only [prod.lex_def, empty_relation, false_or]; simp only [eq_self_iff_true, true_and]; refl⟩⟩, one_mul := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨prod_punit _, λ a b, by rcases a with ⟨a, ⟨⟨⟩⟩⟩; rcases b with ⟨b, ⟨⟨⟩⟩⟩; simp only [prod.lex_def, empty_relation, and_false, or_false]; refl⟩⟩ } @[simp] theorem type_prod_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type (prod.lex s r) = type r * type s := rfl private theorem mul_eq_zero' {a b : ordinal} : a * b = 0 ↔ a = 0 ∨ b = 0 := induction_on a $ λ α _ _, induction_on b $ λ β _ _, begin simp_rw [←type_prod_lex, type_eq_zero_iff_is_empty], rw or_comm, exact is_empty_prod end instance : monoid_with_zero ordinal := { zero := 0, mul_zero := λ a, mul_eq_zero'.2 $ or.inr rfl, zero_mul := λ a, mul_eq_zero'.2 $ or.inl rfl, ..ordinal.monoid } instance : no_zero_divisors ordinal := ⟨λ a b, mul_eq_zero'.1⟩ @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans (rel_iso.prod_lex_congr (rel_iso.preimage equiv.ulift _) (rel_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem card_mul (a b) : card (a * b) = card a * card b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, mul_comm (mk β) (mk α) instance : left_distrib_class ordinal.{u} := ⟨λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quotient.sound ⟨⟨sum_prod_distrib _ _ _, begin rintro ⟨a₁|a₁, a₂⟩ ⟨b₁|b₁, b₂⟩; simp only [prod.lex_def, sum.lex_inl_inl, sum.lex.sep, sum.lex_inr_inl, sum.lex_inr_inr, sum_prod_distrib_apply_left, sum_prod_distrib_apply_right]; simp only [sum.inl.inj_iff, true_or, false_and, false_or] end⟩⟩⟩ theorem mul_succ (a b : ordinal) : a * succ b = a * b + a := mul_add_one a b instance mul_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (*) (≤) := ⟨λ c a b, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine (rel_embedding.of_monotone (λ a : α × γ, (f a.1, a.2)) (λ a b h, _)).ordinal_type_le, clear_, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ (f.to_rel_embedding.map_rel_iff.2 h') }, { exact prod.lex.right _ h' } end⟩ instance mul_swap_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (swap (*)) (≤) := ⟨λ c a b, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine (rel_embedding.of_monotone (λ a : γ × α, (a.1, f a.2)) (λ a b h, _)).ordinal_type_le, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ h' }, { exact prod.lex.right _ (f.to_rel_embedding.map_rel_iff.2 h') } end⟩ theorem le_mul_left (a : ordinal) {b : ordinal} (hb : 0 < b) : a ≤ a * b := by { convert mul_le_mul_left' (one_le_iff_pos.2 hb) a, rw mul_one a } theorem le_mul_right (a : ordinal) {b : ordinal} (hb : 0 < b) : a ≤ b * a := by { convert mul_le_mul_right' (one_le_iff_pos.2 hb) a, rw one_mul a } private lemma mul_le_of_limit_aux {α β r s} [is_well_order α r] [is_well_order β s] {c} (h : is_limit (type s)) (H : ∀ b' < type s, type r * b' ≤ c) (l : c < type r * type s) : false := begin suffices : ∀ a b, prod.lex s r (b, a) (enum _ _ l), { cases enum _ _ l with b a, exact irrefl _ (this _ _) }, intros a b, rw [←typein_lt_typein (prod.lex s r), typein_enum], have := H _ (h.2 _ (typein_lt_type s b)), rw mul_succ at this, have := ((add_lt_add_iff_left _).2 (typein_lt_type _ a)).trans_le this, refine (rel_embedding.of_monotone (λ a, _) (λ a b, _)).ordinal_type_le.trans_lt this, { rcases a with ⟨⟨b', a'⟩, h⟩, by_cases e : b = b', { refine sum.inr ⟨a', _⟩, subst e, cases h with _ _ _ _ h _ _ _ h, { exact (irrefl _ h).elim }, { exact h } }, { refine sum.inl (⟨b', _⟩, a'), cases h with _ _ _ _ h _ _ _ h, { exact h }, { exact (e rfl).elim } } }, { rcases a with ⟨⟨b₁, a₁⟩, h₁⟩, rcases b with ⟨⟨b₂, a₂⟩, h₂⟩, intro h, by_cases e₁ : b = b₁; by_cases e₂ : b = b₂, { substs b₁ b₂, simpa only [subrel_val, prod.lex_def, @irrefl _ s _ b, true_and, false_or, eq_self_iff_true, dif_pos, sum.lex_inr_inr] using h }, { subst b₁, simp only [subrel_val, prod.lex_def, e₂, prod.lex_def, dif_pos, subrel_val, eq_self_iff_true, or_false, dif_neg, not_false_iff, sum.lex_inr_inl, false_and] at h ⊢, cases h₂; [exact asymm h h₂_h, exact e₂ rfl] }, { simp [e₂, dif_neg e₁, show b₂ ≠ b₁, by cc] }, { simpa only [dif_neg e₁, dif_neg e₂, prod.lex_def, subrel_val, subtype.mk_eq_mk, sum.lex_inl_inl] using h } } end theorem mul_le_of_limit {a b c : ordinal} (h : is_limit b) : a * b ≤ c ↔ ∀ b' < b, a * b' ≤ c := ⟨λ h b' l, (mul_le_mul_left' l.le _).trans h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _, by exactI mul_le_of_limit_aux) h H⟩ theorem mul_is_normal {a : ordinal} (h : 0 < a) : is_normal ((*) a) := ⟨λ b, by rw mul_succ; simpa only [add_zero] using (add_lt_add_iff_left (a*b)).2 h, λ b l c, mul_le_of_limit l⟩ theorem lt_mul_of_limit {a b c : ordinal} (h : is_limit c) : a < b * c ↔ ∃ c' < c, a < b * c' := by simpa only [not_ball, not_le] using not_congr (@mul_le_of_limit b c a h) theorem mul_lt_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b < a * c ↔ b < c := (mul_is_normal a0).lt_iff theorem mul_le_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := (mul_is_normal a0).le_iff theorem mul_lt_mul_of_pos_left {a b c : ordinal} (h : a < b) (c0 : 0 < c) : c * a < c * b := (mul_lt_mul_iff_left c0).2 h theorem mul_pos {a b : ordinal} (h₁ : 0 < a) (h₂ : 0 < b) : 0 < a * b := by simpa only [mul_zero] using mul_lt_mul_of_pos_left h₂ h₁ theorem mul_ne_zero {a b : ordinal} : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := by simpa only [ordinal.pos_iff_ne_zero] using mul_pos theorem le_of_mul_le_mul_left {a b c : ordinal} (h : c * a ≤ c * b) (h0 : 0 < c) : a ≤ b := le_imp_le_of_lt_imp_lt (λ h', mul_lt_mul_of_pos_left h' h0) h theorem mul_right_inj {a b c : ordinal} (a0 : 0 < a) : a * b = a * c ↔ b = c := (mul_is_normal a0).inj theorem mul_is_limit {a b : ordinal} (a0 : 0 < a) : is_limit b → is_limit (a * b) := (mul_is_normal a0).is_limit theorem mul_is_limit_left {a b : ordinal} (l : is_limit a) (b0 : 0 < b) : is_limit (a * b) := begin rcases zero_or_succ_or_limit b with rfl|⟨b,rfl⟩|lb, { exact b0.false.elim }, { rw mul_succ, exact add_is_limit _ l }, { exact mul_is_limit l.pos lb } end theorem smul_eq_mul : ∀ (n : ℕ) (a : ordinal), n • a = a * n | 0 a := by rw [zero_smul, nat.cast_zero, mul_zero] | (n + 1) a := by rw [succ_nsmul', nat.cast_add, mul_add, nat.cast_one, mul_one, smul_eq_mul] /-! ### Division on ordinals -/ /-- The set in the definition of division is nonempty. -/ theorem div_nonempty {a b : ordinal} (h : b ≠ 0) : {o | a < b * succ o}.nonempty := ⟨a, succ_le_iff.1 $ by simpa only [succ_zero, one_mul] using mul_le_mul_right' (succ_le_of_lt (ordinal.pos_iff_ne_zero.2 h)) (succ a)⟩ /-- `a / b` is the unique ordinal `o` satisfying `a = b * o + o'` with `o' < b`. -/ instance : has_div ordinal := ⟨λ a b, if h : b = 0 then 0 else Inf {o | a < b * succ o}⟩ @[simp] theorem div_zero (a : ordinal) : a / 0 = 0 := dif_pos rfl lemma div_def (a) {b : ordinal} (h : b ≠ 0) : a / b = Inf {o | a < b * succ o} := dif_neg h theorem lt_mul_succ_div (a) {b : ordinal} (h : b ≠ 0) : a < b * succ (a / b) := by rw div_def a h; exact Inf_mem (div_nonempty h) theorem lt_mul_div_add (a) {b : ordinal} (h : b ≠ 0) : a < b * (a / b) + b := by simpa only [mul_succ] using lt_mul_succ_div a h theorem div_le {a b c : ordinal} (b0 : b ≠ 0) : a / b ≤ c ↔ a < b * succ c := ⟨λ h, (lt_mul_succ_div a b0).trans_le (mul_le_mul_left' (succ_le_succ_iff.2 h) _), λ h, by rw div_def a b0; exact cInf_le' h⟩ theorem lt_div {a b c : ordinal} (h : c ≠ 0) : a < b / c ↔ c * succ a ≤ b := by rw [← not_le, div_le h, not_lt] theorem div_pos {b c : ordinal} (h : c ≠ 0) : 0 < b / c ↔ c ≤ b := by simp [lt_div h] theorem le_div {a b c : ordinal} (c0 : c ≠ 0) : a ≤ b / c ↔ c * a ≤ b := begin apply limit_rec_on a, { simp only [mul_zero, ordinal.zero_le] }, { intros, rw [succ_le_iff, lt_div c0] }, { simp only [mul_le_of_limit, limit_le, iff_self, forall_true_iff] {contextual := tt} } end theorem div_lt {a b c : ordinal} (b0 : b ≠ 0) : a / b < c ↔ a < b * c := lt_iff_lt_of_le_iff_le $ le_div b0 theorem div_le_of_le_mul {a b c : ordinal} (h : a ≤ b * c) : a / b ≤ c := if b0 : b = 0 then by simp only [b0, div_zero, ordinal.zero_le] else (div_le b0).2 $ h.trans_lt $ mul_lt_mul_of_pos_left (lt_succ c) (ordinal.pos_iff_ne_zero.2 b0) theorem mul_lt_of_lt_div {a b c : ordinal} : a < b / c → c * a < b := lt_imp_lt_of_le_imp_le div_le_of_le_mul @[simp] theorem zero_div (a : ordinal) : 0 / a = 0 := ordinal.le_zero.1 $ div_le_of_le_mul $ ordinal.zero_le _ theorem mul_div_le (a b : ordinal) : b * (a / b) ≤ a := if b0 : b = 0 then by simp only [b0, zero_mul, ordinal.zero_le] else (le_div b0).1 le_rfl theorem mul_add_div (a) {b : ordinal} (b0 : b ≠ 0) (c) : (b * a + c) / b = a + c / b := begin apply le_antisymm, { apply (div_le b0).2, rw [mul_succ, mul_add, add_assoc, add_lt_add_iff_left], apply lt_mul_div_add _ b0 }, { rw [le_div b0, mul_add, add_le_add_iff_left], apply mul_div_le } end theorem div_eq_zero_of_lt {a b : ordinal} (h : a < b) : a / b = 0 := begin rw [←ordinal.le_zero, div_le $ ordinal.pos_iff_ne_zero.1 $ (ordinal.zero_le _).trans_lt h], simpa only [succ_zero, mul_one] using h end @[simp] theorem mul_div_cancel (a) {b : ordinal} (b0 : b ≠ 0) : b * a / b = a := by simpa only [add_zero, zero_div] using mul_add_div a b0 0 @[simp] theorem div_one (a : ordinal) : a / 1 = a := by simpa only [one_mul] using mul_div_cancel a ordinal.one_ne_zero @[simp] theorem div_self {a : ordinal} (h : a ≠ 0) : a / a = 1 := by simpa only [mul_one] using mul_div_cancel 1 h theorem mul_sub (a b c : ordinal) : a * (b - c) = a * b - a * c := if a0 : a = 0 then by simp only [a0, zero_mul, sub_self] else eq_of_forall_ge_iff $ λ d, by rw [sub_le, ← le_div a0, sub_le, ← le_div a0, mul_add_div _ a0] theorem is_limit_add_iff {a b} : is_limit (a + b) ↔ is_limit b ∨ (b = 0 ∧ is_limit a) := begin split; intro h, { by_cases h' : b = 0, { rw [h', add_zero] at h, right, exact ⟨h', h⟩ }, left, rw [←add_sub_cancel a b], apply sub_is_limit h, suffices : a + 0 < a + b, simpa only [add_zero], rwa [add_lt_add_iff_left, ordinal.pos_iff_ne_zero] }, rcases h with h|⟨rfl, h⟩, exact add_is_limit a h, simpa only [add_zero] end theorem dvd_add_iff : ∀ {a b c : ordinal}, a ∣ b → (a ∣ b + c ↔ a ∣ c) | a _ c ⟨b, rfl⟩ := ⟨λ ⟨d, e⟩, ⟨d - b, by rw [mul_sub, ← e, add_sub_cancel]⟩, λ ⟨d, e⟩, by { rw [e, ← mul_add], apply dvd_mul_right }⟩ theorem div_mul_cancel : ∀ {a b : ordinal}, a ≠ 0 → a ∣ b → a * (b / a) = b | a _ a0 ⟨b, rfl⟩ := by rw [mul_div_cancel _ a0] theorem le_of_dvd : ∀ {a b : ordinal}, b ≠ 0 → a ∣ b → a ≤ b | a _ b0 ⟨b, rfl⟩ := by simpa only [mul_one] using mul_le_mul_left' (one_le_iff_ne_zero.2 (λ h : b = 0, by simpa only [h, mul_zero] using b0)) a theorem dvd_antisymm {a b : ordinal} (h₁ : a ∣ b) (h₂ : b ∣ a) : a = b := if a0 : a = 0 then by subst a; exact (eq_zero_of_zero_dvd h₁).symm else if b0 : b = 0 then by subst b; exact eq_zero_of_zero_dvd h₂ else (le_of_dvd b0 h₁).antisymm (le_of_dvd a0 h₂) instance : is_antisymm ordinal (∣) := ⟨@dvd_antisymm⟩ /-- `a % b` is the unique ordinal `o'` satisfying `a = b * o + o'` with `o' < b`. -/ instance : has_mod ordinal := ⟨λ a b, a - b * (a / b)⟩ theorem mod_def (a b : ordinal) : a % b = a - b * (a / b) := rfl @[simp] theorem mod_zero (a : ordinal) : a % 0 = a := by simp only [mod_def, div_zero, zero_mul, sub_zero] theorem mod_eq_of_lt {a b : ordinal} (h : a < b) : a % b = a := by simp only [mod_def, div_eq_zero_of_lt h, mul_zero, sub_zero] @[simp] theorem zero_mod (b : ordinal) : 0 % b = 0 := by simp only [mod_def, zero_div, mul_zero, sub_self] theorem div_add_mod (a b : ordinal) : b * (a / b) + a % b = a := ordinal.add_sub_cancel_of_le $ mul_div_le _ _ theorem mod_lt (a) {b : ordinal} (h : b ≠ 0) : a % b < b := (add_lt_add_iff_left (b * (a / b))).1 $ by rw div_add_mod; exact lt_mul_div_add a h @[simp] theorem mod_self (a : ordinal) : a % a = 0 := if a0 : a = 0 then by simp only [a0, zero_mod] else by simp only [mod_def, div_self a0, mul_one, sub_self] @[simp] theorem mod_one (a : ordinal) : a % 1 = 0 := by simp only [mod_def, div_one, one_mul, sub_self] theorem dvd_of_mod_eq_zero {a b : ordinal} (H : a % b = 0) : b ∣ a := ⟨a / b, by simpa [H] using (div_add_mod a b).symm⟩ theorem mod_eq_zero_of_dvd {a b : ordinal} (H : b ∣ a) : a % b = 0 := begin rcases H with ⟨c, rfl⟩, rcases eq_or_ne b 0 with rfl | hb, { simp }, { simp [mod_def, hb] } end theorem dvd_iff_mod_eq_zero {a b : ordinal} : b ∣ a ↔ a % b = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ /-! ### Families of ordinals There are two kinds of indexed families that naturally arise when dealing with ordinals: those indexed by some type in the appropriate universe, and those indexed by ordinals less than another. The following API allows one to convert from one kind of family to the other. In many cases, this makes it easy to prove claims about one kind of family via the corresponding claim on the other. -/ /-- Converts a family indexed by a `Type u` to one indexed by an `ordinal.{u}` using a specified well-ordering. -/ def bfamily_of_family' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → α) : Π a < type r, α := λ a ha, f (enum r a ha) /-- Converts a family indexed by a `Type u` to one indexed by an `ordinal.{u}` using a well-ordering given by the axiom of choice. -/ def bfamily_of_family {ι : Type u} : (ι → α) → Π a < type (@well_ordering_rel ι), α := bfamily_of_family' well_ordering_rel /-- Converts a family indexed by an `ordinal.{u}` to one indexed by an `Type u` using a specified well-ordering. -/ def family_of_bfamily' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] {o} (ho : type r = o) (f : Π a < o, α) : ι → α := λ i, f (typein r i) (by { rw ←ho, exact typein_lt_type r i }) /-- Converts a family indexed by an `ordinal.{u}` to one indexed by a `Type u` using a well-ordering given by the axiom of choice. -/ def family_of_bfamily (o : ordinal) (f : Π a < o, α) : o.out.α → α := family_of_bfamily' (<) (type_lt o) f @[simp] theorem bfamily_of_family'_typein {ι} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → α) (i) : bfamily_of_family' r f (typein r i) (typein_lt_type r i) = f i := by simp only [bfamily_of_family', enum_typein] @[simp] theorem bfamily_of_family_typein {ι} (f : ι → α) (i) : bfamily_of_family f (typein _ i) (typein_lt_type _ i) = f i := bfamily_of_family'_typein _ f i @[simp] theorem family_of_bfamily'_enum {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] {o} (ho : type r = o) (f : Π a < o, α) (i hi) : family_of_bfamily' r ho f (enum r i (by rwa ho)) = f i hi := by simp only [family_of_bfamily', typein_enum] @[simp] theorem family_of_bfamily_enum (o : ordinal) (f : Π a < o, α) (i hi) : family_of_bfamily o f (enum (<) i (by { convert hi, exact type_lt _ })) = f i hi := family_of_bfamily'_enum _ (type_lt o) f _ _ /-- The range of a family indexed by ordinals. -/ def brange (o : ordinal) (f : Π a < o, α) : set α := {a | ∃ i hi, f i hi = a} theorem mem_brange {o : ordinal} {f : Π a < o, α} {a} : a ∈ brange o f ↔ ∃ i hi, f i hi = a := iff.rfl theorem mem_brange_self {o} (f : Π a < o, α) (i hi) : f i hi ∈ brange o f := ⟨i, hi, rfl⟩ @[simp] theorem range_family_of_bfamily' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] {o} (ho : type r = o) (f : Π a < o, α) : range (family_of_bfamily' r ho f) = brange o f := begin refine set.ext (λ a, ⟨_, _⟩), { rintro ⟨b, rfl⟩, apply mem_brange_self }, { rintro ⟨i, hi, rfl⟩, exact ⟨_, family_of_bfamily'_enum _ _ _ _ _⟩ } end @[simp] theorem range_family_of_bfamily {o} (f : Π a < o, α) : range (family_of_bfamily o f) = brange o f := range_family_of_bfamily' _ _ f @[simp] theorem brange_bfamily_of_family' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → α) : brange _ (bfamily_of_family' r f) = range f := begin refine set.ext (λ a, ⟨_, _⟩), { rintro ⟨i, hi, rfl⟩, apply mem_range_self }, { rintro ⟨b, rfl⟩, exact ⟨_, _, bfamily_of_family'_typein _ _ _⟩ }, end @[simp] theorem brange_bfamily_of_family {ι : Type u} (f : ι → α) : brange _ (bfamily_of_family f) = range f := brange_bfamily_of_family' _ _ @[simp] theorem brange_const {o : ordinal} (ho : o ≠ 0) {c : α} : brange o (λ _ _, c) = {c} := begin rw ←range_family_of_bfamily, exact @set.range_const _ o.out.α (out_nonempty_iff_ne_zero.2 ho) c end theorem comp_bfamily_of_family' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → α) (g : α → β) : (λ i hi, g (bfamily_of_family' r f i hi)) = bfamily_of_family' r (g ∘ f) := rfl theorem comp_bfamily_of_family {ι : Type u} (f : ι → α) (g : α → β) : (λ i hi, g (bfamily_of_family f i hi)) = bfamily_of_family (g ∘ f) := rfl theorem comp_family_of_bfamily' {ι : Type u} (r : ι → ι → Prop) [is_well_order ι r] {o} (ho : type r = o) (f : Π a < o, α) (g : α → β) : g ∘ (family_of_bfamily' r ho f) = family_of_bfamily' r ho (λ i hi, g (f i hi)) := rfl theorem comp_family_of_bfamily {o} (f : Π a < o, α) (g : α → β) : g ∘ (family_of_bfamily o f) = family_of_bfamily o (λ i hi, g (f i hi)) := rfl /-! ### Supremum of a family of ordinals -/ /-- The supremum of a family of ordinals -/ def sup {ι : Type u} (f : ι → ordinal.{max u v}) : ordinal.{max u v} := supr f @[simp] theorem Sup_eq_sup {ι : Type u} (f : ι → ordinal.{max u v}) : Sup (set.range f) = sup f := rfl /-- The range of an indexed ordinal function, whose outputs live in a higher universe than the inputs, is always bounded above. See `ordinal.lsub` for an explicit bound. -/ theorem bdd_above_range {ι : Type u} (f : ι → ordinal.{max u v}) : bdd_above (set.range f) := ⟨(supr (succ ∘ card ∘ f)).ord, begin rintros a ⟨i, rfl⟩, exact le_of_lt (cardinal.lt_ord.2 ((lt_succ _).trans_le (le_csupr (bdd_above_range _) _))) end⟩ theorem le_sup {ι} (f : ι → ordinal) : ∀ i, f i ≤ sup f := λ i, le_cSup (bdd_above_range f) (mem_range_self i) theorem sup_le_iff {ι} {f : ι → ordinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a := (cSup_le_iff' (bdd_above_range f)).trans (by simp) theorem sup_le {ι} {f : ι → ordinal} {a} : (∀ i, f i ≤ a) → sup f ≤ a := sup_le_iff.2 theorem lt_sup {ι} {f : ι → ordinal} {a} : a < sup f ↔ ∃ i, a < f i := by simpa only [not_forall, not_le] using not_congr (@sup_le_iff _ f a) theorem ne_sup_iff_lt_sup {ι} {f : ι → ordinal} : (∀ i, f i ≠ sup f) ↔ ∀ i, f i < sup f := ⟨λ hf _, lt_of_le_of_ne (le_sup _ _) (hf _), λ hf _, ne_of_lt (hf _)⟩ theorem sup_not_succ_of_ne_sup {ι} {f : ι → ordinal} (hf : ∀ i, f i ≠ sup f) {a} (hao : a < sup f) : succ a < sup f := begin by_contra' hoa, exact hao.not_le (sup_le $ λ i, le_of_lt_succ $ (lt_of_le_of_ne (le_sup _ _) (hf i)).trans_le hoa) end @[simp] theorem sup_eq_zero_iff {ι} {f : ι → ordinal} : sup f = 0 ↔ ∀ i, f i = 0 := begin refine ⟨λ h i, _, λ h, le_antisymm (sup_le (λ i, ordinal.le_zero.2 (h i))) (ordinal.zero_le _)⟩, rw [←ordinal.le_zero, ←h], exact le_sup f i end theorem is_normal.sup {f} (H : is_normal f) {ι} (g : ι → ordinal) [nonempty ι] : f (sup g) = sup (f ∘ g) := eq_of_forall_ge_iff $ λ a, by rw [sup_le_iff, comp, H.le_set' set.univ set.univ_nonempty g]; simp [sup_le_iff] @[simp] theorem sup_empty {ι} [is_empty ι] (f : ι → ordinal) : sup f = 0 := csupr_of_empty f @[simp] theorem sup_const {ι} [hι : nonempty ι] (o : ordinal) : sup (λ _ : ι, o) = o := csupr_const @[simp] theorem sup_unique {ι} [unique ι] (f : ι → ordinal) : sup f = f default := supr_unique theorem sup_le_of_range_subset {ι ι'} {f : ι → ordinal} {g : ι' → ordinal} (h : set.range f ⊆ set.range g) : sup.{u (max v w)} f ≤ sup.{v (max u w)} g := sup_le $ λ i, match h (mem_range_self i) with ⟨j, hj⟩ := hj ▸ le_sup _ _ end theorem sup_eq_of_range_eq {ι ι'} {f : ι → ordinal} {g : ι' → ordinal} (h : set.range f = set.range g) : sup.{u (max v w)} f = sup.{v (max u w)} g := (sup_le_of_range_subset h.le).antisymm (sup_le_of_range_subset.{v u w} h.ge) @[simp] theorem sup_sum {α : Type u} {β : Type v} (f : α ⊕ β → ordinal) : sup.{(max u v) w} f = max (sup.{u (max v w)} (λ a, f (sum.inl a))) (sup.{v (max u w)} (λ b, f (sum.inr b))) := begin apply (sup_le_iff.2 _).antisymm (max_le_iff.2 ⟨_, _⟩), { rintro (i|i), { exact le_max_of_le_left (le_sup _ i) }, { exact le_max_of_le_right (le_sup _ i) } }, all_goals { apply sup_le_of_range_subset.{_ (max u v) w}, rintros i ⟨a, rfl⟩, apply mem_range_self } end lemma unbounded_range_of_sup_ge {α β : Type u} (r : α → α → Prop) [is_well_order α r] (f : β → α) (h : type r ≤ sup.{u u} (typein r ∘ f)) : unbounded r (range f) := (not_bounded_iff _).1 $ λ ⟨x, hx⟩, not_lt_of_le h $ lt_of_le_of_lt (sup_le $ λ y, le_of_lt $ (typein_lt_typein r).2 $ hx _ $ mem_range_self y) (typein_lt_type r x) theorem le_sup_shrink_equiv {s : set ordinal.{u}} (hs : small.{u} s) (a) (ha : a ∈ s) : a ≤ sup.{u u} (λ x, ((@equiv_shrink s hs).symm x).val) := by { convert le_sup.{u u} _ ((@equiv_shrink s hs) ⟨a, ha⟩), rw symm_apply_apply } instance small_Iio (o : ordinal.{u}) : small.{u} (set.Iio o) := let f : o.out.α → set.Iio o := λ x, ⟨typein (<) x, typein_lt_self x⟩ in let hf : surjective f := λ b, ⟨enum (<) b.val (by { rw type_lt, exact b.prop }), subtype.ext (typein_enum _ _)⟩ in small_of_surjective hf instance small_Iic (o : ordinal.{u}) : small.{u} (set.Iic o) := by { rw ←Iio_succ, apply_instance } theorem bdd_above_iff_small {s : set ordinal.{u}} : bdd_above s ↔ small.{u} s := ⟨λ ⟨a, h⟩, small_subset $ show s ⊆ Iic a, from λ x hx, h hx, λ h, ⟨sup.{u u} (λ x, ((@equiv_shrink s h).symm x).val), le_sup_shrink_equiv h⟩⟩ theorem bdd_above_of_small (s : set ordinal.{u}) [h : small.{u} s] : bdd_above s := bdd_above_iff_small.2 h theorem sup_eq_Sup {s : set ordinal.{u}} (hs : small.{u} s) : sup.{u u} (λ x, (@equiv_shrink s hs).symm x) = Sup s := let hs' := bdd_above_iff_small.2 hs in ((cSup_le_iff' hs').2 (le_sup_shrink_equiv hs)).antisymm' (sup_le (λ x, le_cSup hs' (subtype.mem _))) theorem Sup_ord {s : set cardinal.{u}} (hs : bdd_above s) : (Sup s).ord = Sup (ord '' s) := eq_of_forall_ge_iff $ λ a, begin rw [cSup_le_iff' (bdd_above_iff_small.2 (@small_image _ _ _ s (cardinal.bdd_above_iff_small.1 hs))), ord_le, cSup_le_iff' hs], simp [ord_le] end theorem supr_ord {ι} {f : ι → cardinal} (hf : bdd_above (range f)) : (supr f).ord = ⨆ i, (f i).ord := by { unfold supr, convert Sup_ord hf, rw range_comp } private theorem sup_le_sup {ι ι' : Type u} (r : ι → ι → Prop) (r' : ι' → ι' → Prop) [is_well_order ι r] [is_well_order ι' r'] {o} (ho : type r = o) (ho' : type r' = o) (f : Π a < o, ordinal) : sup (family_of_bfamily' r ho f) ≤ sup (family_of_bfamily' r' ho' f) := sup_le $ λ i, begin cases typein_surj r' (by { rw [ho', ←ho], exact typein_lt_type r i }) with j hj, simp_rw [family_of_bfamily', ←hj], apply le_sup end theorem sup_eq_sup {ι ι' : Type u} (r : ι → ι → Prop) (r' : ι' → ι' → Prop) [is_well_order ι r] [is_well_order ι' r'] {o : ordinal.{u}} (ho : type r = o) (ho' : type r' = o) (f : Π a < o, ordinal.{max u v}) : sup (family_of_bfamily' r ho f) = sup (family_of_bfamily' r' ho' f) := sup_eq_of_range_eq.{u u v} (by simp) /-- The supremum of a family of ordinals indexed by the set of ordinals less than some `o : ordinal.{u}`. This is a special case of `sup` over the family provided by `family_of_bfamily`. -/ def bsup (o : ordinal.{u}) (f : Π a < o, ordinal.{max u v}) : ordinal.{max u v} := sup (family_of_bfamily o f) @[simp] theorem sup_eq_bsup {o} (f : Π a < o, ordinal) : sup (family_of_bfamily o f) = bsup o f := rfl @[simp] theorem sup_eq_bsup' {o ι} (r : ι → ι → Prop) [is_well_order ι r] (ho : type r = o) (f) : sup (family_of_bfamily' r ho f) = bsup o f := sup_eq_sup r _ ho _ f @[simp] theorem Sup_eq_bsup {o} (f : Π a < o, ordinal) : Sup (brange o f) = bsup o f := by { congr, rw range_family_of_bfamily } @[simp] theorem bsup_eq_sup' {ι} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → ordinal) : bsup _ (bfamily_of_family' r f) = sup f := by simp only [←sup_eq_bsup' r, enum_typein, family_of_bfamily', bfamily_of_family'] theorem bsup_eq_bsup {ι : Type u} (r r' : ι → ι → Prop) [is_well_order ι r] [is_well_order ι r'] (f : ι → ordinal) : bsup _ (bfamily_of_family' r f) = bsup _ (bfamily_of_family' r' f) := by rw [bsup_eq_sup', bsup_eq_sup'] @[simp] theorem bsup_eq_sup {ι} (f : ι → ordinal) : bsup _ (bfamily_of_family f) = sup f := bsup_eq_sup' _ f @[congr] lemma bsup_congr {o₁ o₂ : ordinal} (f : Π a < o₁, ordinal) (ho : o₁ = o₂) : bsup o₁ f = bsup o₂ (λ a h, f a (h.trans_eq ho.symm)) := by subst ho theorem bsup_le_iff {o f a} : bsup.{u v} o f ≤ a ↔ ∀ i h, f i h ≤ a := sup_le_iff.trans ⟨λ h i hi, by { rw ←family_of_bfamily_enum o f, exact h _ }, λ h i, h _ _⟩ theorem bsup_le {o : ordinal} {f : Π b < o, ordinal} {a} : (∀ i h, f i h ≤ a) → bsup.{u v} o f ≤ a := bsup_le_iff.2 theorem le_bsup {o} (f : Π a < o, ordinal) (i h) : f i h ≤ bsup o f := bsup_le_iff.1 le_rfl _ _ theorem lt_bsup {o} (f : Π a < o, ordinal) {a} : a < bsup o f ↔ ∃ i hi, a < f i hi := by simpa only [not_forall, not_le] using not_congr (@bsup_le_iff _ f a) theorem is_normal.bsup {f} (H : is_normal f) {o} : ∀ (g : Π a < o, ordinal) (h : o ≠ 0), f (bsup o g) = bsup o (λ a h, f (g a h)) := induction_on o $ λ α r _ g h, begin resetI, haveI := type_ne_zero_iff_nonempty.1 h, rw [←sup_eq_bsup' r, H.sup, ←sup_eq_bsup' r]; refl end theorem lt_bsup_of_ne_bsup {o : ordinal} {f : Π a < o, ordinal} : (∀ i h, f i h ≠ o.bsup f) ↔ ∀ i h, f i h < o.bsup f := ⟨λ hf _ _, lt_of_le_of_ne (le_bsup _ _ _) (hf _ _), λ hf _ _, ne_of_lt (hf _ _)⟩ theorem bsup_not_succ_of_ne_bsup {o} {f : Π a < o, ordinal} (hf : ∀ {i : ordinal} (h : i < o), f i h ≠ o.bsup f) (a) : a < bsup o f → succ a < bsup o f := by { rw ←sup_eq_bsup at *, exact sup_not_succ_of_ne_sup (λ i, hf _) } @[simp] theorem bsup_eq_zero_iff {o} {f : Π a < o, ordinal} : bsup o f = 0 ↔ ∀ i hi, f i hi = 0 := begin refine ⟨λ h i hi, _, λ h, le_antisymm (bsup_le (λ i hi, ordinal.le_zero.2 (h i hi))) (ordinal.zero_le _)⟩, rw [←ordinal.le_zero, ←h], exact le_bsup f i hi, end theorem lt_bsup_of_limit {o : ordinal} {f : Π a < o, ordinal} (hf : ∀ {a a'} (ha : a < o) (ha' : a' < o), a < a' → f a ha < f a' ha') (ho : ∀ a < o, succ a < o) (i h) : f i h < bsup o f := (hf _ _ $ lt_succ i).trans_le (le_bsup f (succ i) $ ho _ h) theorem bsup_succ_of_mono {o : ordinal} {f : Π a < succ o, ordinal} (hf : ∀ {i j} hi hj, i ≤ j → f i hi ≤ f j hj) : bsup _ f = f o (lt_succ o) := le_antisymm (bsup_le $ λ i hi, hf _ _ $ le_of_lt_succ hi) (le_bsup _ _ _) @[simp] theorem bsup_zero (f : Π a < (0 : ordinal), ordinal) : bsup 0 f = 0 := bsup_eq_zero_iff.2 (λ i hi, (ordinal.not_lt_zero i hi).elim) theorem bsup_const {o : ordinal} (ho : o ≠ 0) (a : ordinal) : bsup o (λ _ _, a) = a := le_antisymm (bsup_le (λ _ _, le_rfl)) (le_bsup _ 0 (ordinal.pos_iff_ne_zero.2 ho)) @[simp] theorem bsup_one (f : Π a < (1 : ordinal), ordinal) : bsup 1 f = f 0 zero_lt_one := by simp_rw [←sup_eq_bsup, sup_unique, family_of_bfamily, family_of_bfamily', typein_one_out] theorem bsup_le_of_brange_subset {o o'} {f : Π a < o, ordinal} {g : Π a < o', ordinal} (h : brange o f ⊆ brange o' g) : bsup.{u (max v w)} o f ≤ bsup.{v (max u w)} o' g := bsup_le $ λ i hi, begin obtain ⟨j, hj, hj'⟩ := h ⟨i, hi, rfl⟩, rw ←hj', apply le_bsup end theorem bsup_eq_of_brange_eq {o o'} {f : Π a < o, ordinal} {g : Π a < o', ordinal} (h : brange o f = brange o' g) : bsup.{u (max v w)} o f = bsup.{v (max u w)} o' g := (bsup_le_of_brange_subset h.le).antisymm (bsup_le_of_brange_subset.{v u w} h.ge) /-- The least strict upper bound of a family of ordinals. -/ def lsub {ι} (f : ι → ordinal) : ordinal := sup (succ ∘ f) @[simp] theorem sup_eq_lsub {ι} (f : ι → ordinal) : sup (succ ∘ f) = lsub f := rfl theorem lsub_le_iff {ι} {f : ι → ordinal} {a} : lsub f ≤ a ↔ ∀ i, f i < a := by { convert sup_le_iff, simp only [succ_le_iff] } theorem lsub_le {ι} {f : ι → ordinal} {a} : (∀ i, f i < a) → lsub f ≤ a := lsub_le_iff.2 theorem lt_lsub {ι} (f : ι → ordinal) (i) : f i < lsub f := succ_le_iff.1 (le_sup _ i) theorem lt_lsub_iff {ι} {f : ι → ordinal} {a} : a < lsub f ↔ ∃ i, a ≤ f i := by simpa only [not_forall, not_lt, not_le] using not_congr (@lsub_le_iff _ f a) theorem sup_le_lsub {ι} (f : ι → ordinal) : sup f ≤ lsub f := sup_le $ λ i, (lt_lsub f i).le theorem lsub_le_sup_succ {ι} (f : ι → ordinal) : lsub f ≤ succ (sup f) := lsub_le $ λ i, lt_succ_iff.2 (le_sup f i) theorem sup_eq_lsub_or_sup_succ_eq_lsub {ι} (f : ι → ordinal) : sup f = lsub f ∨ succ (sup f) = lsub f := begin cases eq_or_lt_of_le (sup_le_lsub f), { exact or.inl h }, { exact or.inr ((succ_le_of_lt h).antisymm (lsub_le_sup_succ f)) } end theorem sup_succ_le_lsub {ι} (f : ι → ordinal) : succ (sup f) ≤ lsub f ↔ ∃ i, f i = sup f := begin refine ⟨λ h, _, _⟩, { by_contra' hf, exact (succ_le_iff.1 h).ne ((sup_le_lsub f).antisymm (lsub_le (ne_sup_iff_lt_sup.1 hf))) }, rintro ⟨_, hf⟩, rw [succ_le_iff, ←hf], exact lt_lsub _ _ end theorem sup_succ_eq_lsub {ι} (f : ι → ordinal) : succ (sup f) = lsub f ↔ ∃ i, f i = sup f := (lsub_le_sup_succ f).le_iff_eq.symm.trans (sup_succ_le_lsub f) theorem sup_eq_lsub_iff_succ {ι} (f : ι → ordinal) : sup f = lsub f ↔ ∀ a < lsub f, succ a < lsub f := begin refine ⟨λ h, _, λ hf, le_antisymm (sup_le_lsub f) (lsub_le (λ i, _))⟩, { rw ←h, exact λ a, sup_not_succ_of_ne_sup (λ i, (lsub_le_iff.1 (le_of_eq h.symm) i).ne) }, by_contra' hle, have heq := (sup_succ_eq_lsub f).2 ⟨i, le_antisymm (le_sup _ _) hle⟩, have := hf _ (by { rw ←heq, exact lt_succ (sup f) }), rw heq at this, exact this.false end theorem sup_eq_lsub_iff_lt_sup {ι} (f : ι → ordinal) : sup f = lsub f ↔ ∀ i, f i < sup f := ⟨λ h i, (by { rw h, apply lt_lsub }), λ h, le_antisymm (sup_le_lsub f) (lsub_le h)⟩ @[simp] lemma lsub_empty {ι} [h : is_empty ι] (f : ι → ordinal) : lsub f = 0 := by { rw [←ordinal.le_zero, lsub_le_iff], exact h.elim } lemma lsub_pos {ι} [h : nonempty ι] (f : ι → ordinal) : 0 < lsub f := h.elim $ λ i, (ordinal.zero_le _).trans_lt (lt_lsub f i) @[simp] theorem lsub_eq_zero_iff {ι} {f : ι → ordinal} : lsub f = 0 ↔ is_empty ι := begin refine ⟨λ h, ⟨λ i, _⟩, λ h, @lsub_empty _ h _⟩, have := @lsub_pos _ ⟨i⟩ f, rw h at this, exact this.false end @[simp] theorem lsub_const {ι} [hι : nonempty ι] (o : ordinal) : lsub (λ _ : ι, o) = succ o := sup_const (succ o) @[simp] theorem lsub_unique {ι} [hι : unique ι] (f : ι → ordinal) : lsub f = succ (f default) := sup_unique _ theorem lsub_le_of_range_subset {ι ι'} {f : ι → ordinal} {g : ι' → ordinal} (h : set.range f ⊆ set.range g) : lsub.{u (max v w)} f ≤ lsub.{v (max u w)} g := sup_le_of_range_subset (by convert set.image_subset _ h; apply set.range_comp) theorem lsub_eq_of_range_eq {ι ι'} {f : ι → ordinal} {g : ι' → ordinal} (h : set.range f = set.range g) : lsub.{u (max v w)} f = lsub.{v (max u w)} g := (lsub_le_of_range_subset h.le).antisymm (lsub_le_of_range_subset.{v u w} h.ge) @[simp] theorem lsub_sum {α : Type u} {β : Type v} (f : α ⊕ β → ordinal) : lsub.{(max u v) w} f = max (lsub.{u (max v w)} (λ a, f (sum.inl a))) (lsub.{v (max u w)} (λ b, f (sum.inr b))) := sup_sum _ theorem lsub_not_mem_range {ι} (f : ι → ordinal) : lsub f ∉ set.range f := λ ⟨i, h⟩, h.not_lt (lt_lsub f i) theorem nonempty_compl_range {ι : Type u} (f : ι → ordinal.{max u v}) : (set.range f)ᶜ.nonempty := ⟨_, lsub_not_mem_range f⟩ @[simp] theorem lsub_typein (o : ordinal) : lsub.{u u} (typein ((<) : o.out.α → o.out.α → Prop)) = o := (lsub_le.{u u} typein_lt_self).antisymm begin by_contra' h, nth_rewrite 0 ←type_lt o at h, simpa [typein_enum] using lt_lsub.{u u} (typein (<)) (enum (<) _ h) end theorem sup_typein_limit {o : ordinal} (ho : ∀ a, a < o → succ a < o) : sup.{u u} (typein ((<) : o.out.α → o.out.α → Prop)) = o := by rw (sup_eq_lsub_iff_succ.{u u} (typein (<))).2; rwa lsub_typein o @[simp] theorem sup_typein_succ {o : ordinal} : sup.{u u} (typein ((<) : (succ o).out.α → (succ o).out.α → Prop)) = o := begin cases sup_eq_lsub_or_sup_succ_eq_lsub.{u u} (typein ((<) : (succ o).out.α → (succ o).out.α → Prop)) with h h, { rw sup_eq_lsub_iff_succ at h, simp only [lsub_typein] at h, exact (h o (lt_succ o)).false.elim }, rw [←succ_eq_succ_iff, h], apply lsub_typein end /-- The least strict upper bound of a family of ordinals indexed by the set of ordinals less than some `o : ordinal.{u}`. This is to `lsub` as `bsup` is to `sup`. -/ def blsub (o : ordinal.{u}) (f : Π a < o, ordinal.{max u v}) : ordinal.{max u v} := o.bsup (λ a ha, succ (f a ha)) @[simp] theorem bsup_eq_blsub (o : ordinal) (f : Π a < o, ordinal) : bsup o (λ a ha, succ (f a ha)) = blsub o f := rfl theorem lsub_eq_blsub' {ι} (r : ι → ι → Prop) [is_well_order ι r] {o} (ho : type r = o) (f) : lsub (family_of_bfamily' r ho f) = blsub o f := sup_eq_bsup' r ho (λ a ha, succ (f a ha)) theorem lsub_eq_lsub {ι ι' : Type u} (r : ι → ι → Prop) (r' : ι' → ι' → Prop) [is_well_order ι r] [is_well_order ι' r'] {o} (ho : type r = o) (ho' : type r' = o) (f : Π a < o, ordinal) : lsub (family_of_bfamily' r ho f) = lsub (family_of_bfamily' r' ho' f) := by rw [lsub_eq_blsub', lsub_eq_blsub'] @[simp] theorem lsub_eq_blsub {o} (f : Π a < o, ordinal) : lsub (family_of_bfamily o f) = blsub o f := lsub_eq_blsub' _ _ _ @[simp] theorem blsub_eq_lsub' {ι} (r : ι → ι → Prop) [is_well_order ι r] (f : ι → ordinal) : blsub _ (bfamily_of_family' r f) = lsub f := bsup_eq_sup' r (succ ∘ f) theorem blsub_eq_blsub {ι : Type u} (r r' : ι → ι → Prop) [is_well_order ι r] [is_well_order ι r'] (f : ι → ordinal) : blsub _ (bfamily_of_family' r f) = blsub _ (bfamily_of_family' r' f) := by rw [blsub_eq_lsub', blsub_eq_lsub'] @[simp] theorem blsub_eq_lsub {ι} (f : ι → ordinal) : blsub _ (bfamily_of_family f) = lsub f := blsub_eq_lsub' _ _ @[congr] lemma blsub_congr {o₁ o₂ : ordinal} (f : Π a < o₁, ordinal) (ho : o₁ = o₂) : blsub o₁ f = blsub o₂ (λ a h, f a (h.trans_eq ho.symm)) := by subst ho theorem blsub_le_iff {o f a} : blsub o f ≤ a ↔ ∀ i h, f i h < a := by { convert bsup_le_iff, simp [succ_le_iff] } theorem blsub_le {o : ordinal} {f : Π b < o, ordinal} {a} : (∀ i h, f i h < a) → blsub o f ≤ a := blsub_le_iff.2 theorem lt_blsub {o} (f : Π a < o, ordinal) (i h) : f i h < blsub o f := blsub_le_iff.1 le_rfl _ _ theorem lt_blsub_iff {o f a} : a < blsub o f ↔ ∃ i hi, a ≤ f i hi := by simpa only [not_forall, not_lt, not_le] using not_congr (@blsub_le_iff _ f a) theorem bsup_le_blsub {o} (f : Π a < o, ordinal) : bsup o f ≤ blsub o f := bsup_le (λ i h, (lt_blsub f i h).le) theorem blsub_le_bsup_succ {o} (f : Π a < o, ordinal) : blsub o f ≤ succ (bsup o f) := blsub_le (λ i h, lt_succ_iff.2 (le_bsup f i h)) theorem bsup_eq_blsub_or_succ_bsup_eq_blsub {o} (f : Π a < o, ordinal) : bsup o f = blsub o f ∨ succ (bsup o f) = blsub o f := by { rw [←sup_eq_bsup, ←lsub_eq_blsub], exact sup_eq_lsub_or_sup_succ_eq_lsub _ } theorem bsup_succ_le_blsub {o} (f : Π a < o, ordinal) : succ (bsup o f) ≤ blsub o f ↔ ∃ i hi, f i hi = bsup o f := begin refine ⟨λ h, _, _⟩, { by_contra' hf, exact ne_of_lt (succ_le_iff.1 h) (le_antisymm (bsup_le_blsub f) (blsub_le (lt_bsup_of_ne_bsup.1 hf))) }, rintro ⟨_, _, hf⟩, rw [succ_le_iff, ←hf], exact lt_blsub _ _ _ end theorem bsup_succ_eq_blsub {o} (f : Π a < o, ordinal) : succ (bsup o f) = blsub o f ↔ ∃ i hi, f i hi = bsup o f := (blsub_le_bsup_succ f).le_iff_eq.symm.trans (bsup_succ_le_blsub f) theorem bsup_eq_blsub_iff_succ {o} (f : Π a < o, ordinal) : bsup o f = blsub o f ↔ ∀ a < blsub o f, succ a < blsub o f := by { rw [←sup_eq_bsup, ←lsub_eq_blsub], apply sup_eq_lsub_iff_succ } theorem bsup_eq_blsub_iff_lt_bsup {o} (f : Π a < o, ordinal) : bsup o f = blsub o f ↔ ∀ i hi, f i hi < bsup o f := ⟨λ h i, (by { rw h, apply lt_blsub }), λ h, le_antisymm (bsup_le_blsub f) (blsub_le h)⟩ theorem bsup_eq_blsub_of_lt_succ_limit {o} (ho : is_limit o) {f : Π a < o, ordinal} (hf : ∀ a ha, f a ha < f (succ a) (ho.2 a ha)) : bsup o f = blsub o f := begin rw bsup_eq_blsub_iff_lt_bsup, exact λ i hi, (hf i hi).trans_le (le_bsup f _ _) end theorem blsub_succ_of_mono {o : ordinal} {f : Π a < succ o, ordinal} (hf : ∀ {i j} hi hj, i ≤ j → f i hi ≤ f j hj) : blsub _ f = succ (f o (lt_succ o)) := bsup_succ_of_mono $ λ i j hi hj h, succ_le_succ (hf hi hj h) @[simp] theorem blsub_eq_zero_iff {o} {f : Π a < o, ordinal} : blsub o f = 0 ↔ o = 0 := by { rw [←lsub_eq_blsub, lsub_eq_zero_iff], exact out_empty_iff_eq_zero } @[simp] lemma blsub_zero (f : Π a < (0 : ordinal), ordinal) : blsub 0 f = 0 := by rwa blsub_eq_zero_iff lemma blsub_pos {o : ordinal} (ho : 0 < o) (f : Π a < o, ordinal) : 0 < blsub o f := (ordinal.zero_le _).trans_lt (lt_blsub f 0 ho) theorem blsub_type (r : α → α → Prop) [is_well_order α r] (f) : blsub (type r) f = lsub (λ a, f (typein r a) (typein_lt_type _ _)) := eq_of_forall_ge_iff $ λ o, by rw [blsub_le_iff, lsub_le_iff]; exact ⟨λ H b, H _ _, λ H i h, by simpa only [typein_enum] using H (enum r i h)⟩ theorem blsub_const {o : ordinal} (ho : o ≠ 0) (a : ordinal) : blsub.{u v} o (λ _ _, a) = succ a := bsup_const.{u v} ho (succ a) @[simp] theorem blsub_one (f : Π a < (1 : ordinal), ordinal) : blsub 1 f = succ (f 0 zero_lt_one) := bsup_one _ @[simp] theorem blsub_id : ∀ o, blsub.{u u} o (λ x _, x) = o := lsub_typein theorem bsup_id_limit {o : ordinal} : (∀ a < o, succ a < o) → bsup.{u u} o (λ x _, x) = o := sup_typein_limit @[simp] theorem bsup_id_succ (o) : bsup.{u u} (succ o) (λ x _, x) = o := sup_typein_succ theorem blsub_le_of_brange_subset {o o'} {f : Π a < o, ordinal} {g : Π a < o', ordinal} (h : brange o f ⊆ brange o' g) : blsub.{u (max v w)} o f ≤ blsub.{v (max u w)} o' g := bsup_le_of_brange_subset $ λ a ⟨b, hb, hb'⟩, begin obtain ⟨c, hc, hc'⟩ := h ⟨b, hb, rfl⟩, simp_rw ←hc' at hb', exact ⟨c, hc, hb'⟩ end theorem blsub_eq_of_brange_eq {o o'} {f : Π a < o, ordinal} {g : Π a < o', ordinal} (h : {o | ∃ i hi, f i hi = o} = {o | ∃ i hi, g i hi = o}) : blsub.{u (max v w)} o f = blsub.{v (max u w)} o' g := (blsub_le_of_brange_subset h.le).antisymm (blsub_le_of_brange_subset.{v u w} h.ge) theorem bsup_comp {o o' : ordinal} {f : Π a < o, ordinal} (hf : ∀ {i j} (hi) (hj), i ≤ j → f i hi ≤ f j hj) {g : Π a < o', ordinal} (hg : blsub o' g = o) : bsup o' (λ a ha, f (g a ha) (by { rw ←hg, apply lt_blsub })) = bsup o f := begin apply le_antisymm; refine bsup_le (λ i hi, _), { apply le_bsup }, { rw [←hg, lt_blsub_iff] at hi, rcases hi with ⟨j, hj, hj'⟩, exact (hf _ _ hj').trans (le_bsup _ _ _) } end theorem blsub_comp {o o' : ordinal} {f : Π a < o, ordinal} (hf : ∀ {i j} (hi) (hj), i ≤ j → f i hi ≤ f j hj) {g : Π a < o', ordinal} (hg : blsub o' g = o) : blsub o' (λ a ha, f (g a ha) (by { rw ←hg, apply lt_blsub })) = blsub o f := @bsup_comp o _ (λ a ha, succ (f a ha)) (λ i j _ _ h, succ_le_succ_iff.2 (hf _ _ h)) g hg theorem is_normal.bsup_eq {f} (H : is_normal f) {o : ordinal} (h : is_limit o) : bsup.{u} o (λ x _, f x) = f o := by { rw [←is_normal.bsup.{u u} H (λ x _, x) h.1, bsup_id_limit h.2] } theorem is_normal.blsub_eq {f} (H : is_normal f) {o : ordinal} (h : is_limit o) : blsub.{u} o (λ x _, f x) = f o := by { rw [←H.bsup_eq h, bsup_eq_blsub_of_lt_succ_limit h], exact (λ a _, H.1 a) } theorem is_normal_iff_lt_succ_and_bsup_eq {f} : is_normal f ↔ (∀ a, f a < f (succ a)) ∧ ∀ o, is_limit o → bsup o (λ x _, f x) = f o := ⟨λ h, ⟨h.1, @is_normal.bsup_eq f h⟩, λ ⟨h₁, h₂⟩, ⟨h₁, λ o ho a, (by {rw ←h₂ o ho, exact bsup_le_iff})⟩⟩ theorem is_normal_iff_lt_succ_and_blsub_eq {f} : is_normal f ↔ (∀ a, f a < f (succ a)) ∧ ∀ o, is_limit o → blsub o (λ x _, f x) = f o := begin rw [is_normal_iff_lt_succ_and_bsup_eq, and.congr_right_iff], intro h, split; intros H o ho; have := H o ho; rwa ←bsup_eq_blsub_of_lt_succ_limit ho (λ a _, h a) at * end theorem is_normal.eq_iff_zero_and_succ {f g : ordinal.{u} → ordinal.{u}} (hf : is_normal f) (hg : is_normal g) : f = g ↔ f 0 = g 0 ∧ ∀ a, f a = g a → f (succ a) = g (succ a) := ⟨λ h, by simp [h], λ ⟨h₁, h₂⟩, funext (λ a, begin apply a.limit_rec_on, assumption', intros o ho H, rw [←is_normal.bsup_eq.{u u} hf ho, ←is_normal.bsup_eq.{u u} hg ho], congr, ext b hb, exact H b hb end)⟩ /-! ### Minimum excluded ordinals -/ /-- The minimum excluded ordinal in a family of ordinals. -/ def mex {ι : Type u} (f : ι → ordinal.{max u v}) : ordinal := Inf (set.range f)ᶜ theorem mex_not_mem_range {ι : Type u} (f : ι → ordinal.{max u v}) : mex f ∉ set.range f := Inf_mem (nonempty_compl_range f) theorem ne_mex {ι} (f : ι → ordinal) : ∀ i, f i ≠ mex f := by simpa using mex_not_mem_range f theorem mex_le_of_ne {ι} {f : ι → ordinal} {a} (ha : ∀ i, f i ≠ a) : mex f ≤ a := cInf_le' (by simp [ha]) theorem exists_of_lt_mex {ι} {f : ι → ordinal} {a} (ha : a < mex f) : ∃ i, f i = a := by { by_contra' ha', exact ha.not_le (mex_le_of_ne ha') } theorem mex_le_lsub {ι} (f : ι → ordinal) : mex f ≤ lsub f := cInf_le' (lsub_not_mem_range f) theorem mex_monotone {α β} {f : α → ordinal} {g : β → ordinal} (h : set.range f ⊆ set.range g) : mex f ≤ mex g := begin refine mex_le_of_ne (λ i hi, _), cases h ⟨i, rfl⟩ with j hj, rw ←hj at hi, exact ne_mex g j hi end theorem mex_lt_ord_succ_mk {ι} (f : ι → ordinal) : mex f < (succ (#ι)).ord := begin by_contra' h, apply (lt_succ (#ι)).not_le, have H := λ a, exists_of_lt_mex ((typein_lt_self a).trans_le h), let g : (succ (#ι)).ord.out.α → ι := λ a, classical.some (H a), have hg : injective g := λ a b h', begin have Hf : ∀ x, f (g x) = typein (<) x := λ a, classical.some_spec (H a), apply_fun f at h', rwa [Hf, Hf, typein_inj] at h' end, convert cardinal.mk_le_of_injective hg, rw cardinal.mk_ord_out end /-- The minimum excluded ordinal of a family of ordinals indexed by the set of ordinals less than some `o : ordinal.{u}`. This is a special case of `mex` over the family provided by `family_of_bfamily`. This is to `mex` as `bsup` is to `sup`. -/ def bmex (o : ordinal) (f : Π a < o, ordinal) : ordinal := mex (family_of_bfamily o f) theorem bmex_not_mem_brange {o : ordinal} (f : Π a < o, ordinal) : bmex o f ∉ brange o f := by { rw ←range_family_of_bfamily, apply mex_not_mem_range } theorem ne_bmex {o : ordinal} (f : Π a < o, ordinal) {i} (hi) : f i hi ≠ bmex o f := begin convert ne_mex _ (enum (<) i (by rwa type_lt)), rw family_of_bfamily_enum end theorem bmex_le_of_ne {o : ordinal} {f : Π a < o, ordinal} {a} (ha : ∀ i hi, f i hi ≠ a) : bmex o f ≤ a := mex_le_of_ne (λ i, ha _ _) theorem exists_of_lt_bmex {o : ordinal} {f : Π a < o, ordinal} {a} (ha : a < bmex o f) : ∃ i hi, f i hi = a := begin cases exists_of_lt_mex ha with i hi, exact ⟨_, typein_lt_self i, hi⟩ end theorem bmex_le_blsub {o : ordinal} (f : Π a < o, ordinal) : bmex o f ≤ blsub o f := mex_le_lsub _ theorem bmex_monotone {o o' : ordinal} {f : Π a < o, ordinal} {g : Π a < o', ordinal} (h : brange o f ⊆ brange o' g) : bmex o f ≤ bmex o' g := mex_monotone (by rwa [range_family_of_bfamily, range_family_of_bfamily]) theorem bmex_lt_ord_succ_card {o : ordinal} (f : Π a < o, ordinal) : bmex o f < (succ o.card).ord := by { rw ←mk_ordinal_out, exact (mex_lt_ord_succ_mk (family_of_bfamily o f)) } end ordinal /-! ### Results about injectivity and surjectivity -/ lemma not_surjective_of_ordinal {α : Type u} (f : α → ordinal.{u}) : ¬ surjective f := λ h, ordinal.lsub_not_mem_range.{u u} f (h _) lemma not_injective_of_ordinal {α : Type u} (f : ordinal.{u} → α) : ¬ injective f := λ h, not_surjective_of_ordinal _ (inv_fun_surjective h) lemma not_surjective_of_ordinal_of_small {α : Type v} [small.{u} α] (f : α → ordinal.{u}) : ¬ surjective f := λ h, not_surjective_of_ordinal _ (h.comp (equiv_shrink _).symm.surjective) lemma not_injective_of_ordinal_of_small {α : Type v} [small.{u} α] (f : ordinal.{u} → α) : ¬ injective f := λ h, not_injective_of_ordinal _ ((equiv_shrink _).injective.comp h) /-- The type of ordinals in universe `u` is not `small.{u}`. This is the type-theoretic analog of the Burali-Forti paradox. -/ theorem not_small_ordinal : ¬ small.{u} ordinal.{max u v} := λ h, @not_injective_of_ordinal_of_small _ h _ (λ a b, ordinal.lift_inj.1) /-! ### Enumerating unbounded sets of ordinals with ordinals -/ namespace ordinal section /-- Enumerator function for an unbounded set of ordinals. -/ def enum_ord (S : set ordinal.{u}) : ordinal → ordinal := lt_wf.fix (λ o f, Inf (S ∩ set.Ici (blsub.{u u} o f))) variables {S : set ordinal.{u}} /-- The equation that characterizes `enum_ord` definitionally. This isn't the nicest expression to work with, so consider using `enum_ord_def` instead. -/ theorem enum_ord_def' (o) : enum_ord S o = Inf (S ∩ set.Ici (blsub.{u u} o (λ a _, enum_ord S a))) := lt_wf.fix_eq _ _ /-- The set in `enum_ord_def'` is nonempty. -/ theorem enum_ord_def'_nonempty (hS : unbounded (<) S) (a) : (S ∩ set.Ici a).nonempty := let ⟨b, hb, hb'⟩ := hS a in ⟨b, hb, le_of_not_gt hb'⟩ private theorem enum_ord_mem_aux (hS : unbounded (<) S) (o) : (enum_ord S o) ∈ S ∩ set.Ici (blsub.{u u} o (λ c _, enum_ord S c)) := by { rw enum_ord_def', exact Inf_mem (enum_ord_def'_nonempty hS _) } theorem enum_ord_mem (hS : unbounded (<) S) (o) : enum_ord S o ∈ S := (enum_ord_mem_aux hS o).left theorem blsub_le_enum_ord (hS : unbounded (<) S) (o) : blsub.{u u} o (λ c _, enum_ord S c) ≤ enum_ord S o := (enum_ord_mem_aux hS o).right theorem enum_ord_strict_mono (hS : unbounded (<) S) : strict_mono (enum_ord S) := λ _ _ h, (lt_blsub.{u u} _ _ h).trans_le (blsub_le_enum_ord hS _) /-- A more workable definition for `enum_ord`. -/ theorem enum_ord_def (o) : enum_ord S o = Inf (S ∩ {b | ∀ c, c < o → enum_ord S c < b}) := begin rw enum_ord_def', congr, ext, exact ⟨λ h a hao, (lt_blsub.{u u} _ _ hao).trans_le h, blsub_le⟩ end /-- The set in `enum_ord_def` is nonempty. -/ lemma enum_ord_def_nonempty (hS : unbounded (<) S) {o} : {x | x ∈ S ∧ ∀ c, c < o → enum_ord S c < x}.nonempty := (⟨_, enum_ord_mem hS o, λ _ b, enum_ord_strict_mono hS b⟩) @[simp] theorem enum_ord_range {f : ordinal → ordinal} (hf : strict_mono f) : enum_ord (range f) = f := funext (λ o, begin apply ordinal.induction o, intros a H, rw enum_ord_def a, have Hfa : f a ∈ range f ∩ {b | ∀ c, c < a → enum_ord (range f) c < b} := ⟨mem_range_self a, λ b hb, (by {rw H b hb, exact hf hb})⟩, refine (cInf_le' Hfa).antisymm ((le_cInf_iff'' ⟨_, Hfa⟩).2 _), rintros _ ⟨⟨c, rfl⟩, hc : ∀ b < a, enum_ord (range f) b < f c⟩, rw hf.le_iff_le, contrapose! hc, exact ⟨c, hc, (H c hc).ge⟩, end) @[simp] theorem enum_ord_univ : enum_ord set.univ = id := by { rw ←range_id, exact enum_ord_range strict_mono_id } @[simp] theorem enum_ord_zero : enum_ord S 0 = Inf S := by { rw enum_ord_def, simp [ordinal.not_lt_zero] } theorem enum_ord_succ_le {a b} (hS : unbounded (<) S) (ha : a ∈ S) (hb : enum_ord S b < a) : enum_ord S (succ b) ≤ a := begin rw enum_ord_def, exact cInf_le' ⟨ha, λ c hc, ((enum_ord_strict_mono hS).monotone (le_of_lt_succ hc)).trans_lt hb⟩ end theorem enum_ord_le_of_subset {S T : set ordinal} (hS : unbounded (<) S) (hST : S ⊆ T) (a) : enum_ord T a ≤ enum_ord S a := begin apply ordinal.induction a, intros b H, rw enum_ord_def, exact cInf_le' ⟨hST (enum_ord_mem hS b), λ c h, (H c h).trans_lt (enum_ord_strict_mono hS h)⟩ end theorem enum_ord_surjective (hS : unbounded (<) S) : ∀ s ∈ S, ∃ a, enum_ord S a = s := λ s hs, ⟨Sup {a | enum_ord S a ≤ s}, begin apply le_antisymm, { rw enum_ord_def, refine cInf_le' ⟨hs, λ a ha, _⟩, have : enum_ord S 0 ≤ s := by { rw enum_ord_zero, exact cInf_le' hs }, rcases exists_lt_of_lt_cSup (by exact ⟨0, this⟩) ha with ⟨b, hb, hab⟩, exact (enum_ord_strict_mono hS hab).trans_le hb }, { by_contra' h, exact (le_cSup ⟨s, λ a, (lt_wf.self_le_of_strict_mono (enum_ord_strict_mono hS) a).trans⟩ (enum_ord_succ_le hS hs h)).not_lt (lt_succ _) } end⟩ /-- An order isomorphism between an unbounded set of ordinals and the ordinals. -/ def enum_ord_order_iso (hS : unbounded (<) S) : ordinal ≃o S := strict_mono.order_iso_of_surjective (λ o, ⟨_, enum_ord_mem hS o⟩) (enum_ord_strict_mono hS) (λ s, let ⟨a, ha⟩ := enum_ord_surjective hS s s.prop in ⟨a, subtype.eq ha⟩) theorem range_enum_ord (hS : unbounded (<) S) : range (enum_ord S) = S := by { rw range_eq_iff, exact ⟨enum_ord_mem hS, enum_ord_surjective hS⟩ } /-- A characterization of `enum_ord`: it is the unique strict monotonic function with range `S`. -/ theorem eq_enum_ord (f : ordinal → ordinal) (hS : unbounded (<) S) : strict_mono f ∧ range f = S ↔ f = enum_ord S := begin split, { rintro ⟨h₁, h₂⟩, rwa [←lt_wf.eq_strict_mono_iff_eq_range h₁ (enum_ord_strict_mono hS), range_enum_ord hS] }, { rintro rfl, exact ⟨enum_ord_strict_mono hS, range_enum_ord hS⟩ } end end /-! ### Ordinal exponential -/ /-- The ordinal exponential, defined by transfinite recursion. -/ instance : has_pow ordinal ordinal := ⟨λ a b, if a = 0 then 1 - b else limit_rec_on b 1 (λ _ IH, IH * a) (λ b _, bsup.{u u} b)⟩ local infixr (name := ordinal.pow) ^ := @pow ordinal ordinal ordinal.has_pow theorem opow_def (a b : ordinal) : a ^ b = if a = 0 then 1 - b else limit_rec_on b 1 (λ _ IH, IH * a) (λ b _, bsup.{u u} b) := rfl theorem zero_opow' (a : ordinal) : 0 ^ a = 1 - a := by simp only [opow_def, if_pos rfl] @[simp] theorem zero_opow {a : ordinal} (a0 : a ≠ 0) : 0 ^ a = 0 := by rwa [zero_opow', ordinal.sub_eq_zero_iff_le, one_le_iff_ne_zero] @[simp] theorem opow_zero (a : ordinal) : a ^ 0 = 1 := by by_cases a = 0; [simp only [opow_def, if_pos h, sub_zero], simp only [opow_def, if_neg h, limit_rec_on_zero]] @[simp] theorem opow_succ (a b : ordinal) : a ^ succ b = a ^ b * a := if h : a = 0 then by subst a; simp only [zero_opow (succ_ne_zero _), mul_zero] else by simp only [opow_def, limit_rec_on_succ, if_neg h] theorem opow_limit {a b : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b = bsup.{u u} b (λ c _, a ^ c) := by simp only [opow_def, if_neg a0]; rw limit_rec_on_limit _ _ _ _ h; refl theorem opow_le_of_limit {a b c : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b ≤ c ↔ ∀ b' < b, a ^ b' ≤ c := by rw [opow_limit a0 h, bsup_le_iff] theorem lt_opow_of_limit {a b c : ordinal} (b0 : b ≠ 0) (h : is_limit c) : a < b ^ c ↔ ∃ c' < c, a < b ^ c' := by rw [← not_iff_not, not_exists]; simp only [not_lt, opow_le_of_limit b0 h, exists_prop, not_and] @[simp] theorem opow_one (a : ordinal) : a ^ 1 = a := by rw [← succ_zero, opow_succ]; simp only [opow_zero, one_mul] @[simp] theorem one_opow (a : ordinal) : 1 ^ a = 1 := begin apply limit_rec_on a, { simp only [opow_zero] }, { intros _ ih, simp only [opow_succ, ih, mul_one] }, refine λ b l IH, eq_of_forall_ge_iff (λ c, _), rw [opow_le_of_limit ordinal.one_ne_zero l], exact ⟨λ H, by simpa only [opow_zero] using H 0 l.pos, λ H b' h, by rwa IH _ h⟩, end theorem opow_pos {a : ordinal} (b) (a0 : 0 < a) : 0 < a ^ b := begin have h0 : 0 < a ^ 0, {simp only [opow_zero, zero_lt_one]}, apply limit_rec_on b, { exact h0 }, { intros b IH, rw [opow_succ], exact mul_pos IH a0 }, { exact λ b l _, (lt_opow_of_limit (ordinal.pos_iff_ne_zero.1 a0) l).2 ⟨0, l.pos, h0⟩ }, end theorem opow_ne_zero {a : ordinal} (b) (a0 : a ≠ 0) : a ^ b ≠ 0 := ordinal.pos_iff_ne_zero.1 $ opow_pos b $ ordinal.pos_iff_ne_zero.2 a0 theorem opow_is_normal {a : ordinal} (h : 1 < a) : is_normal ((^) a) := have a0 : 0 < a, from zero_lt_one.trans h, ⟨λ b, by simpa only [mul_one, opow_succ] using (mul_lt_mul_iff_left (opow_pos b a0)).2 h, λ b l c, opow_le_of_limit (ne_of_gt a0) l⟩ theorem opow_lt_opow_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b < a ^ c ↔ b < c := (opow_is_normal a1).lt_iff theorem opow_le_opow_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b ≤ a ^ c ↔ b ≤ c := (opow_is_normal a1).le_iff theorem opow_right_inj {a b c : ordinal} (a1 : 1 < a) : a ^ b = a ^ c ↔ b = c := (opow_is_normal a1).inj theorem opow_is_limit {a b : ordinal} (a1 : 1 < a) : is_limit b → is_limit (a ^ b) := (opow_is_normal a1).is_limit theorem opow_is_limit_left {a b : ordinal} (l : is_limit a) (hb : b ≠ 0) : is_limit (a ^ b) := begin rcases zero_or_succ_or_limit b with e|⟨b,rfl⟩|l', { exact absurd e hb }, { rw opow_succ, exact mul_is_limit (opow_pos _ l.pos) l }, { exact opow_is_limit l.one_lt l' } end theorem opow_le_opow_right {a b c : ordinal} (h₁ : 0 < a) (h₂ : b ≤ c) : a ^ b ≤ a ^ c := begin cases lt_or_eq_of_le (one_le_iff_pos.2 h₁) with h₁ h₁, { exact (opow_le_opow_iff_right h₁).2 h₂ }, { subst a, simp only [one_opow] } end theorem opow_le_opow_left {a b : ordinal} (c) (ab : a ≤ b) : a ^ c ≤ b ^ c := begin by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, { subst c, simp only [opow_zero] }, { simp only [zero_opow c0, ordinal.zero_le] } }, { apply limit_rec_on c, { simp only [opow_zero] }, { intros c IH, simpa only [opow_succ] using mul_le_mul' IH ab }, { exact λ c l IH, (opow_le_of_limit a0 l).2 (λ b' h, (IH _ h).trans (opow_le_opow_right ((ordinal.pos_iff_ne_zero.2 a0).trans_le ab) h.le)) } } end theorem left_le_opow (a : ordinal) {b : ordinal} (b1 : 0 < b) : a ≤ a ^ b := begin nth_rewrite 0 ←opow_one a, cases le_or_gt a 1 with a1 a1, { cases lt_or_eq_of_le a1 with a0 a1, { rw lt_one_iff_zero at a0, rw [a0, zero_opow ordinal.one_ne_zero], exact ordinal.zero_le _ }, rw [a1, one_opow, one_opow] }, rwa [opow_le_opow_iff_right a1, one_le_iff_pos] end theorem right_le_opow {a : ordinal} (b) (a1 : 1 < a) : b ≤ a ^ b := (opow_is_normal a1).self_le _ theorem opow_lt_opow_left_of_succ {a b c : ordinal} (ab : a < b) : a ^ succ c < b ^ succ c := by { rw [opow_succ, opow_succ], exact (mul_le_mul_right' (opow_le_opow_left c ab.le) a).trans_lt (mul_lt_mul_of_pos_left ab (opow_pos c ((ordinal.zero_le a).trans_lt ab))) } theorem opow_add (a b c : ordinal) : a ^ (b + c) = a ^ b * a ^ c := begin rcases eq_or_ne a 0 with rfl | a0, { rcases eq_or_ne c 0 with rfl | c0, { simp }, have : b + c ≠ 0 := ((ordinal.pos_iff_ne_zero.2 c0).trans_le (le_add_left _ _)).ne', simp only [zero_opow c0, zero_opow this, mul_zero] }, rcases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with rfl | a1, { simp only [one_opow, mul_one] }, apply limit_rec_on c, { simp }, { intros c IH, rw [add_succ, opow_succ, IH, opow_succ, mul_assoc] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((opow_is_normal a1).trans (add_is_normal b)).limit_le l).trans _), dsimp only [function.comp], simp only [IH] {contextual := tt}, exact (((mul_is_normal $ opow_pos b (ordinal.pos_iff_ne_zero.2 a0)).trans (opow_is_normal a1)).limit_le l).symm } end theorem opow_one_add (a b : ordinal) : a ^ (1 + b) = a * a ^ b := by rw [opow_add, opow_one] theorem opow_dvd_opow (a) {b c : ordinal} (h : b ≤ c) : a ^ b ∣ a ^ c := by { rw [← ordinal.add_sub_cancel_of_le h, opow_add], apply dvd_mul_right } theorem opow_dvd_opow_iff {a b c : ordinal} (a1 : 1 < a) : a ^ b ∣ a ^ c ↔ b ≤ c := ⟨λ h, le_of_not_lt $ λ hn, not_le_of_lt ((opow_lt_opow_iff_right a1).2 hn) $ le_of_dvd (opow_ne_zero _ $ one_le_iff_ne_zero.1 $ a1.le) h, opow_dvd_opow _⟩ theorem opow_mul (a b c : ordinal) : a ^ (b * c) = (a ^ b) ^ c := begin by_cases b0 : b = 0, {simp only [b0, zero_mul, opow_zero, one_opow]}, by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, {simp only [c0, mul_zero, opow_zero]}, simp only [zero_opow b0, zero_opow c0, zero_opow (mul_ne_zero b0 c0)] }, cases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 a1, { subst a1, simp only [one_opow] }, apply limit_rec_on c, { simp only [mul_zero, opow_zero] }, { intros c IH, rw [mul_succ, opow_add, IH, opow_succ] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((opow_is_normal a1).trans (mul_is_normal (ordinal.pos_iff_ne_zero.2 b0))).limit_le l).trans _), dsimp only [function.comp], simp only [IH] {contextual := tt}, exact (opow_le_of_limit (opow_ne_zero _ a0) l).symm } end /-! ### Ordinal logarithm -/ /-- The ordinal logarithm is the solution `u` to the equation `x = b ^ u * v + w` where `v < b` and `w < b ^ u`. -/ @[pp_nodot] def log (b : ordinal) (x : ordinal) : ordinal := if h : 1 < b then pred (Inf {o | x < b ^ o}) else 0 /-- The set in the definition of `log` is nonempty. -/ theorem log_nonempty {b x : ordinal} (h : 1 < b) : {o | x < b ^ o}.nonempty := ⟨_, succ_le_iff.1 (right_le_opow _ h)⟩ theorem log_def {b : ordinal} (h : 1 < b) (x : ordinal) : log b x = pred (Inf {o | x < b ^ o}) := by simp only [log, dif_pos h] theorem log_of_not_one_lt_left {b : ordinal} (h : ¬ 1 < b) (x : ordinal) : log b x = 0 := by simp only [log, dif_neg h] theorem log_of_left_le_one {b : ordinal} (h : b ≤ 1) : ∀ x, log b x = 0 := log_of_not_one_lt_left h.not_lt @[simp] lemma log_zero_left : ∀ b, log 0 b = 0 := log_of_left_le_one zero_le_one @[simp] theorem log_zero_right (b : ordinal) : log b 0 = 0 := if b1 : 1 < b then begin rw [log_def b1, ← ordinal.le_zero, pred_le], apply cInf_le', dsimp, rw [succ_zero, opow_one], exact zero_lt_one.trans b1 end else by simp only [log_of_not_one_lt_left b1] @[simp] theorem log_one_left : ∀ b, log 1 b = 0 := log_of_left_le_one le_rfl theorem succ_log_def {b x : ordinal} (hb : 1 < b) (hx : x ≠ 0) : succ (log b x) = Inf {o | x < b ^ o} := begin let t := Inf {o | x < b ^ o}, have : x < b ^ t := Inf_mem (log_nonempty hb), rcases zero_or_succ_or_limit t with h|h|h, { refine ((one_le_iff_ne_zero.2 hx).not_lt _).elim, simpa only [h, opow_zero] }, { rw [show log b x = pred t, from log_def hb x, succ_pred_iff_is_succ.2 h] }, { rcases (lt_opow_of_limit (zero_lt_one.trans hb).ne' h).1 this with ⟨a, h₁, h₂⟩, exact h₁.not_le.elim ((le_cInf_iff'' (log_nonempty hb)).1 le_rfl a h₂) } end theorem lt_opow_succ_log_self {b : ordinal} (hb : 1 < b) (x : ordinal) : x < b ^ succ (log b x) := begin rcases eq_or_ne x 0 with rfl | hx, { apply opow_pos _ (zero_lt_one.trans hb) }, { rw succ_log_def hb hx, exact Inf_mem (log_nonempty hb) } end theorem opow_log_le_self (b) {x : ordinal} (hx : x ≠ 0) : b ^ log b x ≤ x := begin rcases eq_or_ne b 0 with rfl | b0, { rw zero_opow', refine (sub_le_self _ _).trans (one_le_iff_ne_zero.2 hx) }, rcases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with hb | rfl, { refine le_of_not_lt (λ h, (lt_succ (log b x)).not_le _), have := @cInf_le' _ _ {o | x < b ^ o} _ h, rwa ←succ_log_def hb hx at this }, { rwa [one_opow, one_le_iff_ne_zero] } end /-- `opow b` and `log b` (almost) form a Galois connection. -/ theorem opow_le_iff_le_log {b x c : ordinal} (hb : 1 < b) (hx : x ≠ 0) : b ^ c ≤ x ↔ c ≤ log b x := ⟨λ h, le_of_not_lt $ λ hn, (lt_opow_succ_log_self hb x).not_le $ ((opow_le_opow_iff_right hb).2 (succ_le_of_lt hn)).trans h, λ h, ((opow_le_opow_iff_right hb).2 h).trans (opow_log_le_self b hx)⟩ theorem lt_opow_iff_log_lt {b x c : ordinal} (hb : 1 < b) (hx : x ≠ 0) : x < b ^ c ↔ log b x < c := lt_iff_lt_of_le_iff_le (opow_le_iff_le_log hb hx) theorem log_pos {b o : ordinal} (hb : 1 < b) (ho : o ≠ 0) (hbo : b ≤ o) : 0 < log b o := by rwa [←succ_le_iff, succ_zero, ←opow_le_iff_le_log hb ho, opow_one] theorem log_eq_zero {b o : ordinal} (hbo : o < b) : log b o = 0 := begin rcases eq_or_ne o 0 with rfl | ho, { exact log_zero_right b }, cases le_or_lt b 1 with hb hb, { rcases le_one_iff.1 hb with rfl | rfl, { exact log_zero_left o }, { exact log_one_left o } }, { rwa [←ordinal.le_zero, ←lt_succ_iff, succ_zero, ←lt_opow_iff_log_lt hb ho, opow_one] } end @[mono] theorem log_mono_right (b) {x y : ordinal} (xy : x ≤ y) : log b x ≤ log b y := if hx : x = 0 then by simp only [hx, log_zero_right, ordinal.zero_le] else if hb : 1 < b then (opow_le_iff_le_log hb (lt_of_lt_of_le (ordinal.pos_iff_ne_zero.2 hx) xy).ne').1 $ (opow_log_le_self _ hx).trans xy else by simp only [log_of_not_one_lt_left hb, ordinal.zero_le] theorem log_le_self (b x : ordinal) : log b x ≤ x := if hx : x = 0 then by simp only [hx, log_zero_right, ordinal.zero_le] else if hb : 1 < b then (right_le_opow _ hb).trans (opow_log_le_self b hx) else by simp only [log_of_not_one_lt_left hb, ordinal.zero_le] @[simp] theorem log_one_right (b : ordinal) : log b 1 = 0 := if hb : 1 < b then log_eq_zero hb else log_of_not_one_lt_left hb 1 theorem mod_opow_log_lt_self (b : ordinal) {o : ordinal} (ho : o ≠ 0) : o % b ^ log b o < o := begin rcases eq_or_ne b 0 with rfl | hb, { simpa using ordinal.pos_iff_ne_zero.2 ho }, { exact (mod_lt _ $ opow_ne_zero _ hb).trans_le (opow_log_le_self _ ho) } end theorem log_mod_opow_log_lt_log_self {b o : ordinal} (hb : 1 < b) (ho : o ≠ 0) (hbo : b ≤ o) : log b (o % b ^ log b o) < log b o := begin cases eq_or_ne (o % b ^ log b o) 0, { rw [h, log_zero_right], apply log_pos hb ho hbo }, { rw [←succ_le_iff, succ_log_def hb h], apply cInf_le', apply mod_lt, rw ←ordinal.pos_iff_ne_zero, exact opow_pos _ (zero_lt_one.trans hb) } end lemma opow_mul_add_pos {b v : ordinal} (hb : b ≠ 0) (u) (hv : v ≠ 0) (w) : 0 < b ^ u * v + w := (opow_pos u $ ordinal.pos_iff_ne_zero.2 hb).trans_le $ (le_mul_left _ $ ordinal.pos_iff_ne_zero.2 hv).trans $ le_add_right _ _ lemma opow_mul_add_lt_opow_mul_succ {b u w : ordinal} (v : ordinal) (hw : w < b ^ u) : b ^ u * v + w < b ^ u * (succ v) := by rwa [mul_succ, add_lt_add_iff_left] lemma opow_mul_add_lt_opow_succ {b u v w : ordinal} (hvb : v < b) (hw : w < b ^ u) : b ^ u * v + w < b ^ (succ u) := begin convert (opow_mul_add_lt_opow_mul_succ v hw).trans_le (mul_le_mul_left' (succ_le_of_lt hvb) _), exact opow_succ b u end theorem log_opow_mul_add {b u v w : ordinal} (hb : 1 < b) (hv : v ≠ 0) (hvb : v < b) (hw : w < b ^ u) : log b (b ^ u * v + w) = u := begin have hne' := (opow_mul_add_pos (zero_lt_one.trans hb).ne' u hv w).ne', by_contra' hne, cases lt_or_gt_of_ne hne with h h, { rw ←lt_opow_iff_log_lt hb hne' at h, exact h.not_le ((le_mul_left _ (ordinal.pos_iff_ne_zero.2 hv)).trans (le_add_right _ _)) }, { change _ < _ at h, rw [←succ_le_iff, ←opow_le_iff_le_log hb hne'] at h, exact (not_lt_of_le h) (opow_mul_add_lt_opow_succ hvb hw) } end theorem log_opow {b : ordinal} (hb : 1 < b) (x : ordinal) : log b (b ^ x) = x := begin convert log_opow_mul_add hb zero_ne_one.symm hb (opow_pos x (zero_lt_one.trans hb)), rw [add_zero, mul_one] end theorem div_opow_log_lt {b : ordinal} (o : ordinal) (hb : 1 < b) : o / b ^ log b o < b := begin rw [div_lt (opow_pos _ (zero_lt_one.trans hb)).ne', ←opow_succ], exact lt_opow_succ_log_self hb o end theorem add_log_le_log_mul {x y : ordinal} (b : ordinal) (hx : x ≠ 0) (hy : y ≠ 0) : log b x + log b y ≤ log b (x * y) := begin by_cases hb : 1 < b, { rw [←opow_le_iff_le_log hb (mul_ne_zero hx hy), opow_add], exact mul_le_mul' (opow_log_le_self b hx) (opow_log_le_self b hy) }, simp only [log_of_not_one_lt_left hb, zero_add] end /-! ### Casting naturals into ordinals, compatibility with operations -/ @[simp] theorem one_add_nat_cast (m : ℕ) : 1 + (m : ordinal) = succ m := by { rw [←nat.cast_one, ←nat.cast_add, add_comm], refl } @[simp, norm_cast] theorem nat_cast_mul (m : ℕ) : ∀ n : ℕ, ((m * n : ℕ) : ordinal) = m * n | 0 := by simp | (n+1) := by rw [nat.mul_succ, nat.cast_add, nat_cast_mul, nat.cast_succ, mul_add_one] @[simp, norm_cast] theorem nat_cast_opow (m : ℕ) : ∀ n : ℕ, ((pow m n : ℕ) : ordinal) = m ^ n | 0 := by simp | (n+1) := by rw [pow_succ', nat_cast_mul, nat_cast_opow, nat.cast_succ, add_one_eq_succ, opow_succ] @[simp, norm_cast] theorem nat_cast_le {m n : ℕ} : (m : ordinal) ≤ n ↔ m ≤ n := by rw [←cardinal.ord_nat, ←cardinal.ord_nat, cardinal.ord_le_ord, cardinal.nat_cast_le] @[simp, norm_cast] theorem nat_cast_lt {m n : ℕ} : (m : ordinal) < n ↔ m < n := by simp only [lt_iff_le_not_le, nat_cast_le] @[simp, norm_cast] theorem nat_cast_inj {m n : ℕ} : (m : ordinal) = n ↔ m = n := by simp only [le_antisymm_iff, nat_cast_le] @[simp, norm_cast] theorem nat_cast_eq_zero {n : ℕ} : (n : ordinal) = 0 ↔ n = 0 := @nat_cast_inj n 0 theorem nat_cast_ne_zero {n : ℕ} : (n : ordinal) ≠ 0 ↔ n ≠ 0 := not_congr nat_cast_eq_zero @[simp, norm_cast] theorem nat_cast_pos {n : ℕ} : (0 : ordinal) < n ↔ 0 < n := @nat_cast_lt 0 n @[simp, norm_cast] theorem nat_cast_sub (m n : ℕ) : ((m - n : ℕ) : ordinal) = m - n := begin cases le_total m n with h h, { rw [tsub_eq_zero_iff_le.2 h, ordinal.sub_eq_zero_iff_le.2 (nat_cast_le.2 h)], refl }, { apply (add_left_cancel n).1, rw [←nat.cast_add, add_tsub_cancel_of_le h, ordinal.add_sub_cancel_of_le (nat_cast_le.2 h)] } end @[simp, norm_cast] theorem nat_cast_div (m n : ℕ) : ((m / n : ℕ) : ordinal) = m / n := begin rcases eq_or_ne n 0 with rfl | hn, { simp }, { have hn' := nat_cast_ne_zero.2 hn, apply le_antisymm, { rw [le_div hn', ←nat_cast_mul, nat_cast_le, mul_comm], apply nat.div_mul_le_self }, { rw [div_le hn', ←add_one_eq_succ, ←nat.cast_succ, ←nat_cast_mul, nat_cast_lt, mul_comm, ←nat.div_lt_iff_lt_mul (nat.pos_of_ne_zero hn)], apply nat.lt_succ_self } } end @[simp, norm_cast] theorem nat_cast_mod (m n : ℕ) : ((m % n : ℕ) : ordinal) = m % n := by rw [←add_left_cancel, div_add_mod, ←nat_cast_div, ←nat_cast_mul, ←nat.cast_add, nat.div_add_mod] @[simp] theorem lift_nat_cast : ∀ n : ℕ, lift.{u v} n = n | 0 := by simp | (n+1) := by simp [lift_nat_cast n] end ordinal /-! ### Properties of `omega` -/ namespace cardinal open ordinal @[simp] theorem ord_aleph_0 : ord.{u} ℵ₀ = ω := le_antisymm (ord_le.2 $ le_rfl) $ le_of_forall_lt $ λ o h, begin rcases ordinal.lt_lift_iff.1 h with ⟨o, rfl, h'⟩, rw [lt_ord, ←lift_card, ←lift_aleph_0.{0 u}, lift_lt, ←typein_enum (<) h'], exact lt_aleph_0_iff_fintype.2 ⟨set.fintype_lt_nat _⟩ end @[simp] theorem add_one_of_aleph_0_le {c} (h : ℵ₀ ≤ c) : c + 1 = c := begin rw [add_comm, ←card_ord c, ←card_one, ←card_add, one_add_of_omega_le], rwa [←ord_aleph_0, ord_le_ord] end end cardinal namespace ordinal theorem lt_add_of_limit {a b c : ordinal.{u}} (h : is_limit c) : a < b + c ↔ ∃ c' < c, a < b + c' := by rw [←is_normal.bsup_eq.{u u} (add_is_normal b) h, lt_bsup] theorem lt_omega {o : ordinal} : o < ω ↔ ∃ n : ℕ, o = n := by simp_rw [←cardinal.ord_aleph_0, cardinal.lt_ord, lt_aleph_0, card_eq_nat] theorem nat_lt_omega (n : ℕ) : ↑n < ω := lt_omega.2 ⟨_, rfl⟩ theorem omega_pos : 0 < ω := nat_lt_omega 0 theorem omega_ne_zero : ω ≠ 0 := omega_pos.ne' theorem one_lt_omega : 1 < ω := by simpa only [nat.cast_one] using nat_lt_omega 1 theorem omega_is_limit : is_limit ω := ⟨omega_ne_zero, λ o h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e]; exact nat_lt_omega (n+1)⟩ theorem omega_le {o : ordinal} : ω ≤ o ↔ ∀ n : ℕ, ↑n ≤ o := ⟨λ h n, (nat_lt_omega _).le.trans h, λ H, le_of_forall_lt $ λ a h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e, ←succ_le_iff]; exact H (n+1)⟩ @[simp] theorem sup_nat_cast : sup nat.cast = ω := (sup_le $ λ n, (nat_lt_omega n).le).antisymm $ omega_le.2 $ le_sup _ theorem nat_lt_limit {o} (h : is_limit o) : ∀ n : ℕ, ↑n < o | 0 := lt_of_le_of_ne (ordinal.zero_le o) h.1.symm | (n+1) := h.2 _ (nat_lt_limit n) theorem omega_le_of_is_limit {o} (h : is_limit o) : ω ≤ o := omega_le.2 $ λ n, le_of_lt $ nat_lt_limit h n theorem is_limit_iff_omega_dvd {a : ordinal} : is_limit a ↔ a ≠ 0 ∧ ω ∣ a := begin refine ⟨λ l, ⟨l.1, ⟨a / ω, le_antisymm _ (mul_div_le _ _)⟩⟩, λ h, _⟩, { refine (limit_le l).2 (λ x hx, le_of_lt _), rw [←div_lt omega_ne_zero, ←succ_le_iff, le_div omega_ne_zero, mul_succ, add_le_of_limit omega_is_limit], intros b hb, rcases lt_omega.1 hb with ⟨n, rfl⟩, exact (add_le_add_right (mul_div_le _ _) _).trans (lt_sub.1 $ nat_lt_limit (sub_is_limit l hx) _).le }, { rcases h with ⟨a0, b, rfl⟩, refine mul_is_limit_left omega_is_limit (ordinal.pos_iff_ne_zero.2 $ mt _ a0), intro e, simp only [e, mul_zero] } end theorem add_mul_limit_aux {a b c : ordinal} (ba : b + a = a) (l : is_limit c) (IH : ∀ c' < c, (a + b) * succ c' = a * succ c' + b) : (a + b) * c = a * c := le_antisymm ((mul_le_of_limit l).2 $ λ c' h, begin apply (mul_le_mul_left' (le_succ c') _).trans, rw IH _ h, apply (add_le_add_left _ _).trans, { rw ← mul_succ, exact mul_le_mul_left' (succ_le_of_lt $ l.2 _ h) _ }, { apply_instance }, { rw ← ba, exact le_add_right _ _ } end) (mul_le_mul_right' (le_add_right _ _) _) theorem add_mul_succ {a b : ordinal} (c) (ba : b + a = a) : (a + b) * succ c = a * succ c + b := begin apply limit_rec_on c, { simp only [succ_zero, mul_one] }, { intros c IH, rw [mul_succ, IH, ← add_assoc, add_assoc _ b, ba, ← mul_succ] }, { intros c l IH, have := add_mul_limit_aux ba l IH, rw [mul_succ, add_mul_limit_aux ba l IH, mul_succ, add_assoc] } end theorem add_mul_limit {a b c : ordinal} (ba : b + a = a) (l : is_limit c) : (a + b) * c = a * c := add_mul_limit_aux ba l (λ c' _, add_mul_succ c' ba) theorem add_le_of_forall_add_lt {a b c : ordinal} (hb : 0 < b) (h : ∀ d < b, a + d < c) : a + b ≤ c := begin have H : a + (c - a) = c := ordinal.add_sub_cancel_of_le (by {rw ←add_zero a, exact (h _ hb).le}), rw ←H, apply add_le_add_left _ a, by_contra' hb, exact (h _ hb).ne H end theorem is_normal.apply_omega {f : ordinal.{u} → ordinal.{u}} (hf : is_normal f) : sup.{0 u} (f ∘ nat.cast) = f ω := by rw [←sup_nat_cast, is_normal.sup.{0 u u} hf] @[simp] theorem sup_add_nat (o : ordinal) : sup (λ n : ℕ, o + n) = o + ω := (add_is_normal o).apply_omega @[simp] theorem sup_mul_nat (o : ordinal) : sup (λ n : ℕ, o * n) = o * ω := begin rcases eq_zero_or_pos o with rfl | ho, { rw zero_mul, exact sup_eq_zero_iff.2 (λ n, zero_mul n) }, { exact (mul_is_normal ho).apply_omega } end local infixr (name := ordinal.pow) ^ := @pow ordinal ordinal ordinal.has_pow theorem sup_opow_nat {o : ordinal} (ho : 0 < o) : sup (λ n : ℕ, o ^ n) = o ^ ω := begin rcases lt_or_eq_of_le (one_le_iff_pos.2 ho) with ho₁ | rfl, { exact (opow_is_normal ho₁).apply_omega }, { rw one_opow, refine le_antisymm (sup_le (λ n, by rw one_opow)) _, convert le_sup _ 0, rw [nat.cast_zero, opow_zero] } end end ordinal namespace tactic open ordinal positivity /-- Extension for the `positivity` tactic: `ordinal.opow` takes positive values on positive inputs. -/ @[positivity] meta def positivity_opow : expr → tactic strictness | `(@has_pow.pow _ _ %%inst %%a %%b) := do strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``opow_pos [b, p] | _ := failed -- We already know that `0 ≤ x` for all `x : ordinal` end | _ := failed end tactic namespace acc variables {a b : α} /-- The rank of an element `a` accessible under a relation `r` is defined inductively as the smallest ordinal greater than the ranks of all elements below it (i.e. elements `b` such that `r b a`). -/ noncomputable def rank (h : acc r a) : ordinal := acc.rec_on h $ λ a h ih, ordinal.sup $ λ b : {b // r b a}, order.succ $ ih b b.2 lemma rank_eq (h : acc r a) : h.rank = ordinal.sup (λ b : {b // r b a}, order.succ (h.inv b.2).rank) := by { change (acc.intro a $ λ _, h.inv).rank = _, refl } /-- if `r a b` then the rank of `a` is less than the rank of `b`. -/ lemma rank_lt_of_rel (hb : acc r b) (h : r a b) : (hb.inv h).rank < hb.rank := (order.lt_succ _).trans_le $ by { rw hb.rank_eq, refine le_trans _ (ordinal.le_sup _ ⟨a, h⟩), refl } end acc namespace well_founded variables (hwf : well_founded r) {a b : α} include hwf /-- The rank of an element `a` under a well-founded relation `r` is defined inductively as the smallest ordinal greater than the ranks of all elements below it (i.e. elements `b` such that `r b a`). -/ noncomputable def rank (a : α) : ordinal := (hwf.apply a).rank lemma rank_eq : hwf.rank a = ordinal.sup (λ b : {b // r b a}, order.succ $ hwf.rank b) := by { rw [rank, acc.rank_eq], refl } lemma rank_lt_of_rel (h : r a b) : hwf.rank a < hwf.rank b := acc.rank_lt_of_rel _ h omit hwf lemma rank_strict_mono [preorder α] [well_founded_lt α] : strict_mono (rank $ @is_well_founded.wf α (<) _) := λ _ _, rank_lt_of_rel _ lemma rank_strict_anti [preorder α] [well_founded_gt α] : strict_anti (rank $ @is_well_founded.wf α (>) _) := λ _ _, rank_lt_of_rel $ @is_well_founded.wf α (>) _ end well_founded
a63d2a4190cd4a837131d647d5dafb0358922863
b19a1b7dc79c802247fdce4c04708e070863b4d2
/definitions.lean
9ebf122fe13d632fc9622e448f54d400559c49c2
[]
no_license
utanapishtim/promethazine
99a1e80311fb20251a54ba78a534b23852b88c40
08a6f9bd6dd08feb3df8d4697e19ffc8d333b249
refs/heads/master
1,653,595,504,487
1,480,129,933,000
1,480,129,933,000
74,801,596
0
0
null
null
null
null
UTF-8
Lean
false
false
1,380
lean
import data.nat open nat constants (m n : nat) (p q : bool) definition m_plus_n : nat := m + n check m_plus_n print m_plus_n -- lean can infer the type definition m_plus_n' := m + n definition double (x : nat) : nat := x + x print double check double 3 eval double 3 definition square (x : nat) := x * x print square check square 3 eval square 3 definition do_twice (f: nat → nat) (x : nat) : nat := f (f x) eval do_twice double 2 definition do_twice' : (nat → nat) → nat → nat := λ f x, f (f x) eval do_twice' double 2 import data.prod open prod definition curry (A B C : Type) (f : A × B → C) : A → B → C := λ x, (λ y, f (pair x y)) definition fn (x : nat × nat) : ℕ := (pr1 x) + (pr2 x) check fn check ℕ check curry ℕ ℕ ℕ fn 1 2 eval curry ℕ ℕ ℕ fn 1 2 definition uncurry (A B C : Type) (f : A → B → C) : A × B → C := λ x, (f (pr1 x)) (pr2 x) check uncurry definition curriedFn := curry ℕ ℕ ℕ fn check curriedFn check uncurry ℕ ℕ ℕ curriedFn eval uncurry ℕ ℕ ℕ curriedFn (pair 1 2) check let y := n + n in y * y definition t (x : ℕ ) : ℕ := let y := x + x in y * y eval t 3 eval t 2 eval t 1 definition h (n : ℕ ) : ℕ := let y := n + n, z := y + y in z * z eval h 1 eval h 2 definition foo := let a := nat in λ x : a, x + 2 eval foo 1 /- definition bar := (λ a, λ x : a, x + 2) nat -/
7df839f09efd9312709901b9e1e3293b42b8d35a
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/analysis/convex/basic.lean
c76908cc8ee884a361ce6c14b47892be413433c8
[ "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
39,226
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import data.set.intervals.unordered_interval import data.set.intervals.image_preimage import data.complex.module import algebra.pointwise /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ universes u' u v w x variables {E : Type u} {F : Type v} {ι : Type w} {ι' : Type x} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} open set open_locale classical big_operators local notation `I` := (Icc 0 1 : set ℝ) local attribute [instance] set.pointwise_add set.smul_set section sets /-! ### Segment -/ /-- Segments in a vector space -/ def segment (x y : E) : set E := {z : E | ∃ (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} local notation `[`x `, ` y `]` := segment x y lemma segment_symm (x y : E) : [x, y] = [y, x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma left_mem_segment (x y : E) : x ∈ [x, y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x, y] := segment_symm y x ▸ left_mem_segment y x lemma segment_same (x : E) : [x, x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment z z⟩ lemma segment_eq_image (x y : E) : segment x y = (λ (θ : ℝ), (1 - θ) • x + θ • y) '' I := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : segment x y = (λ (θ : ℝ), x + θ • (y - x)) '' I := by { convert segment_eq_image x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image₂ (x y : E) : segment x y = (λ p:ℝ×ℝ, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image'], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_const_add_Icc], simp end lemma segment_eq_Icc' (a b : ℝ) : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_eq_interval (a b : ℝ) : segment a b = interval a b := segment_eq_Icc' _ _ lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b, a + c] ↔ x ∈ [b, c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj] end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, mem_segment_translate a lemma segment_translate_image (a b c: E) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq $ add_left_surjective a /-! ### Convexity of sets -/ /-- Convexity of sets -/ def convex (s : set E) := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s lemma convex_iff_forall_pos : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy (le_of_lt ha) (le_of_lt hb) hab, _⟩, intros h x y hx hy a b ha hb hab, cases eq_or_lt_of_le ha with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases eq_or_lt_of_le hb with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_segment_subset : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x, y] ⊆ s := by simp only [convex, segment_eq_image₂, subset_def, ball_image_iff, prod.forall, mem_set_of_eq, and_imp] lemma convex.segment_subset (h : convex s) {x y:E} (hx : x ∈ s) (hy : y ∈ s) : [x, y] ⊆ s := convex_iff_segment_subset.1 h hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset: convex s ↔ ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintros hA a b ha hb hab w ⟨au, ⟨u, hu, rfl⟩, bv, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_pointwise_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, using division -/ lemma convex_iff_div: convex s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨begin assume h x y hx hy a b ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y hx hy a b ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-! ### Examples of convex sets -/ lemma convex_empty : convex (∅ : set E) := by finish lemma convex_singleton (c : E) : convex ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex_univ : convex (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs: convex s) (ht: convex t) : convex (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s: ι → set E} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.is_linear_image (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩ a b ha hb hab, exact ⟨a • x + b • y, hs hx hy ha hb hab, by simp only [hf.map_add,hf.map_smul]⟩ end lemma convex.linear_image (hs : convex s) (f : E →ₗ[ℝ] F) : convex (image f s) := hs.is_linear_image f.is_linear lemma convex.is_linear_preimage {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (preimage f s) := begin intros x y hx hy a b ha hb hab, convert hs hx hy ha hb hab, simp only [mem_preimage, hf.map_add, hf.map_smul] end lemma convex.linear_preimage {s : set F} (hs : convex s) (f : E →ₗ[ℝ] F) : convex (preimage f s) := hs.is_linear_preimage f.is_linear lemma convex.neg (hs : convex s) : convex ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex s) : convex ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hs : convex s) : convex (c • s) := begin rw smul_set_eq_image, exact hs.is_linear_image (is_linear_map.is_linear_map_smul c) end lemma convex.smul_preimage (c : ℝ) (hs : convex s) : convex ((λ z, c • z) ⁻¹' s) := hs.is_linear_preimage (is_linear_map.is_linear_map_smul c) lemma convex.add {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := by { rw pointwise_add_eq_image, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub {t : set E} (hs : convex s) (ht : convex t) : convex ((λx : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hs : convex s) (z : E) : convex ((λx, z + x) '' s) := begin convert (convex_singleton z).add hs, ext x, simp [set.mem_image, mem_pointwise_add, eq_comm] end lemma convex.affinity (hs : convex s) (z : E) (c : ℝ) : convex ((λx, z + c • x) '' s) := begin convert (hs.smul c).translate z using 1, erw [smul_set_eq_image, ←image_comp] end lemma convex_real_iff {s : set ℝ} : convex s ↔ ∀ {x y}, x ∈ s → y ∈ s → Icc x y ⊆ s := begin simp only [convex_iff_segment_subset, segment_eq_Icc'], split; intros h x y hx hy, { cases le_or_lt x y with hxy hxy, { simpa [hxy] using h hx hy }, { simp [hxy] } }, { apply h; cases le_total x y; simp [*] } end lemma convex_Iio (r : ℝ) : convex (Iio r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_le_of_lt hz.2 hy lemma convex_Ioi (r : ℝ) : convex (Ioi r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_lt_of_le hx hz.1 lemma convex_Iic (r : ℝ) : convex (Iic r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hz.2 hy lemma convex_Ici (r : ℝ) : convex (Ici r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hx hz.1 lemma convex_Ioo (r : ℝ) (s : ℝ) : convex (Ioo r s) := (convex_Ioi _).inter (convex_Iio _) lemma convex_Ico (r : ℝ) (s : ℝ) : convex (Ico r s) := (convex_Ici _).inter (convex_Iio _) lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := (convex_Ioi _).inter (convex_Iic _) lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := (convex_Ici _).inter (convex_Iic _) lemma convex_segment (a b : E) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : E, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image', this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ section submodule open submodule lemma submodule.convex (K : submodule ℝ E) : convex (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ E) : convex (↑K : set E) := K.convex end submodule end sets section functions local notation `[`x `, ` y `]` := segment x y /-! ### Convex functions -/ /-- Convexity of functions -/ def convex_on (s : set E) (f : E → ℝ) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y lemma convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := ⟨hs, by { intros, refl }⟩ lemma convex_on_const (c : ℝ) (hs : convex s) : convex_on s (λ x:E, c) := ⟨hs, by { intros, simp only [← add_mul, *, one_mul] }⟩ variables {t : set E} {f g : E → ℝ} lemma convex_on_iff_div: convex_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) * f x + (b/(a+b)) * f y := and_congr iff.rfl ⟨begin intros h x y hx hy a b ha hb hab, apply h hx hy (div_nonneg ha hab) (div_nonneg hb hab), rw [←add_div], exact div_self (ne_of_gt hab) end, begin intros h x y hx hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a * f x + b * f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y) : convex_on s f := begin use hs, intros x y hx hy a b ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], { exact le_total _ _ }, { cases eq_or_lt_of_le hxy with hxy hxy, by { subst y, rw [← add_smul, ← add_mul, hab, one_smul, one_mul] }, cases eq_or_lt_of_le ha with ha ha, by { subst a, rw [zero_add] at hab, subst b, simp }, cases eq_or_lt_of_le hb with hb hb, by { subst b, rw [add_zero] at hab, subst a, simp }, exact hf hx hy hxy ha hb hab } end /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ lemma convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have A : z - y + (y - x) = z - x, by abel, have B : 0 < z - x, from sub_pos.2 (lt_trans hxy hyz), rw [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, A, ← le_div_iff B, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z)] at this, rw [eq_comm, ← sub_eq_iff_eq_add] at hab; subst a, convert this; symmetry; simp only [div_eq_iff (ne_of_gt B), y]; ring end lemma convex_on.subset (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := begin apply and.intro h_convex, intros x y hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma convex_on.add (hf : convex_on s f) (hg : convex_on s g) : convex_on s (λx, f x + g x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a * f x + b * f y) + (a * g x + b * g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a * f x + a * g x + b * f y + b * g y : by linarith ... = a * (f x + g x) + b * (f y + g y) : by simp [mul_add, add_assoc] end lemma convex_on.smul {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s (λx, c * f x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc c * f (a • x + b • y) ≤ c * (a * f x + b * f y) : mul_le_mul_of_nonneg_left (hf.2 hx hy ha hb hab) hc ... = a * (c * f x) + b * (c * f y) : by rw mul_add; ac_refl end lemma convex_on.le_on_segment' {x y : E} {a b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * max (f x) (f y) + b * max (f x) (f y) : add_le_add (mul_le_mul_of_nonneg_left (le_max_left _ _) ha) (mul_le_mul_of_nonneg_left (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_mul, hab, one_mul] lemma convex_on.le_on_segment (hf : convex_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab lemma convex_on.convex_le (hf : convex_on s f) (r : ℝ) : convex {x ∈ s | f x ≤ r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, ⟨hf.1.segment_subset hx.1 hy.1 hz, le_trans (hf.le_on_segment hx.1 hy.1 hz) $ max_le hx.2 hy.2⟩ lemma convex_on.convex_lt (hf : convex_on s f) (r : ℝ) : convex {x ∈ s | f x < r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, ⟨hf.1.segment_subset hx.1 hy.1 hz, lt_of_le_of_lt (hf.le_on_segment hx.1 hy.1 hz) $ max_lt hx.2 hy.2⟩ lemma convex_on.convex_epigraph (hf : convex_on s f) : convex {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ ⟨hx, hr⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * r + b * t : add_le_add (mul_le_mul_of_nonneg_left hr ha) (mul_le_mul_of_nonneg_left ht hb) end lemma convex_on_iff_convex_epigraph : convex_on s f ↔ convex {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).1 }, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).2 } end end functions section center_mass /-- Center mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (∑ i in t, w i)⁻¹ • (∑ i in t, w i • z i) variables (i j : ι) (c : ℝ) (t : finset ι) (w : ι → ℝ) (z : ι → E) open finset lemma finset.center_mass_empty : (∅ : finset ι).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_pair (hne : i ≠ j) : ({i, j} : finset ι).center_mass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, div_eq_inv_mul] variable {w} lemma finset.center_mass_insert (ha : i ∉ t) (hw : ∑ j in t, w j ≠ 0) : (insert i t).center_mass w z = (w i / (w i + ∑ j in t, w j)) • z i + ((∑ j in t, w j) / (w i + ∑ j in t, w j)) • t.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm], congr' 2, { apply mul_comm }, { rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div_eq_inv] } end lemma finset.center_mass_singleton (hw : w i ≠ 0) : ({i} : finset ι).center_mass w z = z i := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_eq_of_sum_1 (hw : ∑ i in t, w i = 1) : t.center_mass w z = ∑ i in t, w i • z i := by simp only [finset.center_mass, hw, inv_one, one_smul] lemma finset.center_mass_smul : t.center_mass w (λ i, c • z i) = c • t.center_mass w z := by simp only [finset.center_mass, finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ lemma finset.center_mass_segment' (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : ∑ i in s, ws i = 1) (hwt : ∑ i in t, wt i = 1) (a b : ℝ) (hab : a + b = 1): a • s.center_mass ws zs + b • t.center_mass wt zt = (s.image sum.inl ∪ t.image sum.inr).center_mass (sum.elim (λ i, a * ws i) (λ j, b * wt j)) (sum.elim zs zt) := begin rw [s.center_mass_eq_of_sum_1 _ hws, t.center_mass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← finset.sum_sum_elim, finset.center_mass_eq_of_sum_1], { congr, ext ⟨⟩; simp only [sum.elim_inl, sum.elim_inr, mul_smul] }, { rw [sum_sum_elim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] } end /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ lemma finset.center_mass_segment (s : finset ι) (w₁ w₂ : ι → ℝ) (z : ι → E) (hw₁ : ∑ i in s, w₁ i = 1) (hw₂ : ∑ i in s, w₂ i = 1) (a b : ℝ) (hab : a + b = 1): a • s.center_mass w₁ z + b • s.center_mass w₂ z = s.center_mass (λ i, a * w₁ i + b * w₂ i) z := have hw : ∑ i in s, (a * w₁ i + b * w₂ i) = 1, by simp only [mul_sum.symm, sum_add_distrib, mul_one, *], by simp only [finset.center_mass_eq_of_sum_1, smul_sum, sum_add_distrib, add_smul, mul_smul, *] lemma finset.center_mass_ite_eq (hi : i ∈ t) : t.center_mass (λ j, if (i = j) then 1 else 0) z = z i := begin rw [finset.center_mass_eq_of_sum_1], transitivity ∑ j in t, if (i = j) then z i else 0, { congr, ext i, split_ifs, exacts [h ▸ one_smul _ _, zero_smul _ _] }, { rw [sum_ite_eq, if_pos hi] }, { rw [sum_ite_eq, if_pos hi] } end variables {t w} lemma finset.center_mass_subset {t' : finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.center_mass w z = t'.center_mass w z := begin rw [center_mass, sum_subset ht h, smul_sum, center_mass, smul_sum], apply sum_subset ht, assume i hit' hit, rw [h i hit' hit, zero_smul, smul_zero] end lemma finset.center_mass_filter_ne_zero : (t.filter (λ i, w i ≠ 0)).center_mass w z = t.center_mass w z := finset.center_mass_subset z (filter_subset _) $ λ i hit hit', by simpa only [hit, mem_filter, true_and, ne.def, not_not] using hit' variable {z} /-- Center mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem (hs : convex s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i in t, w i) → (∀ i ∈ t, z i ∈ s) → t.center_mass w z ∈ s := begin induction t using finset.induction with i t hi ht, { simp [lt_irrefl] }, intros h₀ hpos hmem, have zi : z i ∈ s, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ j ∈ t, 0 ≤ w j, from λ j hj, h₀ j $ mem_insert_of_mem hj, rw [sum_insert hi] at hpos, by_cases hsum_t : ∑ j in t, w j = 0, { have ws : ∀ j ∈ t, w j = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t, have wz : ∑ j in t, w j • z j = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert hi, wz, hsum_t, add_zero], simp only [hsum_t, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact zi }, { rw [finset.center_mass_insert _ _ _ hi hsum_t], refine convex_iff_div.1 hs zi (ht hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_t) }, { intros j hj, exact hmem j (mem_insert_of_mem hj) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (hs : convex s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : ∑ i in t, w i • z i ∈ s := by simpa only [h₁, center_mass, inv_one, one_smul] using hs.center_mass_mem h₀ (h₁.symm ▸ zero_lt_one) hz lemma convex_iff_sum_mem : convex s ↔ (∀ (t : finset E) (w : E → ℝ), (∀ i ∈ t, 0 ≤ w i) → ∑ i in t, w i = 1 → (∀ x ∈ t, x ∈ s) → ∑ x in t, w x • x ∈ s ) := begin refine ⟨λ hs t w hw₀ hw₁ hts, hs.sum_mem hw₀ hw₁ hts, _⟩, intros h x y hx hy a b ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (hpos : 0 < ∑ i in t, w i) (hmem : ∀ i ∈ t, z i ∈ s) : f (t.center_mass w z) ≤ t.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ t, (z i, (f ∘ z) i) ∈ {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hmem : ∀ i ∈ t, z i ∈ s) : f (∑ i in t, w i • z i) ≤ ∑ i in t, w i * (f (z i)) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- If a function `f` is convex on `s` takes value `y` at the center mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ lemma convex_on.exists_ge_of_center_mass {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) (hz : ∀ i ∈ t, z i ∈ s) : ∃ i ∈ t, f (t.center_mass w z) ≤ f (z i) := begin set y := t.center_mass w z, have : f y ≤ t.center_mass w (f ∘ z) := h.map_center_mass_le hw₀ hws hz, rw ← sum_filter_ne_zero at hws, rw [← finset.center_mass_filter_ne_zero (f ∘ z), center_mass, smul_eq_mul, ← div_eq_inv_mul, le_div_iff hws, mul_sum] at this, replace : ∃ i ∈ t.filter (λ i, w i ≠ 0), f y * w i ≤ w i • (f ∘ z) i := exists_le_of_sum_le (nonempty_of_sum_ne_zero (ne_of_gt hws)) this, rcases this with ⟨i, hi, H⟩, rw [mem_filter] at hi, use [i, hi.1], simp only [smul_eq_mul, mul_comm (w i)] at H, refine (mul_le_mul_right _).1 H, exact lt_of_le_of_ne (hw₀ i hi.1) hi.2.symm end end center_mass section convex_hull variable {t : set E} /-- Convex hull of a set `s` is the minimal convex set that includes `s` -/ def convex_hull (s : set E) : set E := ⋂ (t : set E) (hst : s ⊆ t) (ht : convex t), t variable (s) lemma subset_convex_hull : s ⊆ convex_hull s := set.subset_Inter $ λ t, set.subset_Inter $ λ hst, set.subset_Inter $ λ ht, hst lemma convex_convex_hull : convex (convex_hull s) := convex_Inter $ λ t, convex_Inter $ λ ht, convex_Inter id variable {s} lemma convex_hull_min (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t $ set.Inter_subset_of_subset hst $ set.Inter_subset _ ht lemma convex_hull_mono (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst $ subset_convex_hull t) (convex_convex_hull t) lemma convex.convex_hull_eq {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl _) hs) (subset_convex_hull s) @[simp] lemma convex_hull_singleton {x : E} : convex_hull ({x} : set E) = {x} := (convex_singleton x).convex_hull_eq lemma is_linear_map.image_convex_hull {f : E → F} (hf : is_linear_map ℝ f) : f '' (convex_hull s) = convex_hull (f '' s) := begin refine set.subset.antisymm _ _, { rw [set.image_subset_iff], exact convex_hull_min (set.image_subset_iff.1 $ subset_convex_hull $ f '' s) ((convex_convex_hull (f '' s)).is_linear_preimage hf) }, { exact convex_hull_min (set.image_subset _ $ subset_convex_hull s) ((convex_convex_hull s).is_linear_image hf) } end lemma linear_map.image_convex_hull (f : E →ₗ[ℝ] F) : f '' (convex_hull s) = convex_hull (f '' s) := f.is_linear.image_convex_hull lemma finset.center_mass_mem_convex_hull (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.center_mass w z ∈ convex_hull s := (convex_convex_hull s).center_mass_mem hw₀ hws (λ i hi, subset_convex_hull s $ hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ lemma convex_hull_eq (s : set E) : convex_hull s = {x : E | ∃ (ι : Type u') (t : finset ι) (w : ι → ℝ) (z : ι → E) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hw₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) , t.center_mass w z = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, use [punit, {punit.star}, λ _, 1, λ _, x, λ _ _, zero_le_one, finset.sum_singleton, λ _ _, hx], simp only [finset.center_mass, finset.sum_singleton, inv_one, one_smul] }, { rintros x y ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, _, _, _, _, rfl⟩, { rintros i hi, rw [finset.mem_union, finset.mem_image, finset.mem_image] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [mul_nonneg, hwx₀, hwy₀] }, { simp [finset.sum_sum_elim, finset.mul_sum.symm, *] }, { intros i hi, rw [finset.mem_union, finset.mem_image, finset.mem_image] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [hzx, hzy] } }, { rintros _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, exact t.center_mass_mem_convex_hull hw₀ (hw₁.symm ▸ zero_lt_one) hz } end /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ lemma convex_on.exists_ge_of_mem_convex_hull {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x} (hx : x ∈ convex_hull s) : ∃ y ∈ s, f x ≤ f y := begin rw convex_hull_eq at hx, rcases hx with ⟨α, t, w, z, hw₀, hw₁, hz, rfl⟩, rcases hf.exists_ge_of_center_mass hw₀ (hw₁.symm ▸ zero_lt_one) (λ i hi, subset_convex_hull s (hz i hi)) with ⟨i, hit, Hi⟩, exact ⟨z i, hz i hit, Hi⟩ end lemma finset.convex_hull_eq (s : finset E) : convex_hull ↑s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in s, w y = 1), s.center_mass w id = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, rw [finset.mem_coe] at hx, refine ⟨_, _, _, finset.center_mass_ite_eq _ _ _ hx⟩, { intros, split_ifs, exacts [zero_le_one, le_refl 0] }, { rw [finset.sum_ite_eq, if_pos hx] } }, { rintros x y ⟨wx, hwx₀, hwx₁, rfl⟩ ⟨wy, hwy₀, hwy₁, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, rfl⟩, { rintros i hi, apply_rules [add_nonneg, mul_nonneg, hwx₀, hwy₀], }, { simp only [finset.sum_add_distrib, finset.mul_sum.symm, mul_one, *] } }, { rintros _ ⟨w, hw₀, hw₁, rfl⟩, exact s.center_mass_mem_convex_hull (λ x hx, hw₀ _ hx) (hw₁.symm ▸ zero_lt_one) (λ x hx, hx) } end lemma set.finite.convex_hull_eq {s : set E} (hs : finite s) : convex_hull s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in hs.to_finset, w y = 1), hs.to_finset.center_mass w id = x} := by simpa only [set.finite.coe_to_finset, set.finite.mem_to_finset, exists_prop] using hs.to_finset.convex_hull_eq lemma convex_hull_eq_union_convex_hull_finite_subsets (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s), convex_hull ↑t := begin refine subset.antisymm _ _, { rw [convex_hull_eq.{u}], rintros x ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, simp only [mem_Union], refine ⟨t.image z, _, _⟩, { rw [finset.coe_image, image_subset_iff], exact hz }, { apply t.center_mass_mem_convex_hull hw₀, { simp only [hw₁, zero_lt_one] }, { exact λ i hi, finset.mem_coe.2 (finset.mem_image_of_mem _ hi) } } }, { exact Union_subset (λ i, Union_subset convex_hull_mono), }, end lemma is_linear_map.convex_hull_image {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := set.subset.antisymm (convex_hull_min (image_subset _ (subset_convex_hull s)) $ (convex_convex_hull s).is_linear_image hf) (image_subset_iff.2 $ convex_hull_min (image_subset_iff.1 $ subset_convex_hull _) ((convex_convex_hull _).is_linear_preimage hf)) lemma linear_map.convex_hull_image (f : E →ₗ[ℝ] F) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := f.is_linear.convex_hull_image s end convex_hull /-! ### Simplex -/ section simplex variables (ι) [fintype ι] {f : ι → ℝ} /-- Standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type*) [fintype ι] : set (ι → ℝ) := { f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1 } lemma std_simplex_eq_inter : std_simplex ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex (std_simplex ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:ℝ) 0) ∈ std_simplex ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)] ⟩ /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ lemma convex_hull_basis_eq_std_simplex : convex_hull (range $ λ(i j:ι), if i = j then (1:ℝ) else 0) = std_simplex ι := begin refine subset.antisymm (convex_hull_min _ (convex_std_simplex ι)) _, { rintros _ ⟨i, rfl⟩, exact ite_eq_mem_std_simplex i }, { rintros w ⟨hw₀, hw₁⟩, rw [pi_eq_sum_univ w, ← finset.univ.center_mass_eq_of_sum_1 _ hw₁], exact finset.univ.center_mass_mem_convex_hull (λ i hi, hw₀ i) (hw₁.symm ▸ zero_lt_one) (λ i hi, mem_range_self i) } end variable {ι} /-- Convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ lemma set.finite.convex_hull_eq_image {s : set E} (hs : finite s) : convex_hull s = by haveI := hs.fintype; exact (⇑(∑ x : s, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1)) '' (std_simplex s) := begin rw [← convex_hull_basis_eq_std_simplex, ← linear_map.convex_hull_image, ← range_comp, (∘)], apply congr_arg, convert subtype.range_coe.symm, ext x, simp [linear_map.sum_apply, ite_smul, finset.filter_eq] end /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ lemma mem_Icc_of_mem_std_simplex (hf : f ∈ std_simplex ι) (x) : f x ∈ I := ⟨hf.1 x, hf.2 ▸ finset.single_le_sum (λ y hy, hf.1 y) (finset.mem_univ x)⟩ end simplex
994672dabff44456bce8aa0fffe010f637afc0ba
3ed5a65c1ab3ce5d1a094edce8fa3287980f197b
/src/herstein/ex1_1/Q_04.lean
ff27cf18bf35df462e4cc95e51e9a8c15a2bad40
[]
no_license
group-study-group/herstein
35d32e77158efa2cc303c84e1ee5e3bc80831137
f5a1a72eb56fa19c19ece0cb3ab6cf7ffd161f66
refs/heads/master
1,586,202,191,519
1,548,969,759,000
1,548,969,759,000
157,746,953
0
0
null
1,542,412,901,000
1,542,302,366,000
Lean
UTF-8
Lean
false
false
588
lean
import data.set.basic variable α: Type* variables A B: set α open set -- requires classical logic theorem Q4a: compl (A ∩ B) = (compl A) ∪ (compl B) := ext $ λ x, ⟨λ hi, classical.by_cases (λ ha, or.inr (λ hb, hi ⟨ha, hb⟩)) (λ hn, or.inl hn), λ hu, or.elim hu (λ hac ⟨ha, hb⟩, hac ha) (λ hbc ⟨ha, hb⟩, hbc hb)⟩ theorem Q4b: compl (A ∪ B) = (compl A) ∩ (compl B) := ext $ λ x, ⟨λ hu, ⟨λ ha, hu (or.inl ha), λ hb, hu (or.inr hb)⟩, λ ⟨hac, hbc⟩ hu, or.elim hu (λ ha, hac ha) (λ hb, hbc hb)⟩
5fae6963f3ae011590ba0f28081b5bf7dbf9f047
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/monoidal/of_chosen_finite_products/basic.lean
fb1123060e82a50d7bbfd5298708616478e0dd55
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
12,947
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Simon Hudon -/ import category_theory.monoidal.category import category_theory.limits.shapes.binary_products import category_theory.pempty /-! # The monoidal structure on a category with chosen finite products. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This is a variant of the development in `category_theory.monoidal.of_has_finite_products`, which uses specified choices of the terminal object and binary product, enabling the construction of a cartesian category with specific definitions of the tensor unit and tensor product. (Because the construction in `category_theory.monoidal.of_has_finite_products` uses `has_limit` classes, the actual definitions there are opaque behind `classical.choice`.) We use this in `category_theory.monoidal.types` to construct the monoidal category of types so that the tensor product is the usual cartesian product of types. For now we only do the construction from products, and not from coproducts, which seems less often useful. -/ universes v u noncomputable theory namespace category_theory variables (C : Type u) [category.{v} C] {X Y : C} namespace limits section variables {C} /-- Swap the two sides of a `binary_fan`. -/ def binary_fan.swap {P Q : C} (t : binary_fan P Q) : binary_fan Q P := binary_fan.mk t.snd t.fst @[simp] lemma binary_fan.swap_fst {P Q : C} (t : binary_fan P Q) : t.swap.fst = t.snd := rfl @[simp] lemma binary_fan.swap_snd {P Q : C} (t : binary_fan P Q) : t.swap.snd = t.fst := rfl /-- If a cone `t` over `P Q` is a limit cone, then `t.swap` is a limit cone over `Q P`. -/ @[simps] def is_limit.swap_binary_fan {P Q : C} {t : binary_fan P Q} (I : is_limit t) : is_limit t.swap := { lift := λ s, I.lift (binary_fan.swap s), fac' := λ s, by { rintro ⟨⟨⟩⟩; simp, }, uniq' := λ s m w, begin have h := I.uniq (binary_fan.swap s) m, rw h, rintro ⟨j⟩, specialize w ⟨j.swap⟩, cases j; exact w, end } /-- Construct `has_binary_product Q P` from `has_binary_product P Q`. This can't be an instance, as it would cause a loop in typeclass search. -/ lemma has_binary_product.swap (P Q : C) [has_binary_product P Q] : has_binary_product Q P := has_limit.mk ⟨binary_fan.swap (limit.cone (pair P Q)), (limit.is_limit (pair P Q)).swap_binary_fan⟩ /-- Given a limit cone over `X` and `Y`, and another limit cone over `Y` and `X`, we can construct an isomorphism between the cone points. Relative to some fixed choice of limits cones for every pair, these isomorphisms constitute a braiding. -/ def binary_fan.braiding {X Y : C} {s : binary_fan X Y} (P : is_limit s) {t : binary_fan Y X} (Q : is_limit t) : s.X ≅ t.X := is_limit.cone_point_unique_up_to_iso P Q.swap_binary_fan /-- Given binary fans `sXY` over `X Y`, and `sYZ` over `Y Z`, and `s` over `sXY.X Z`, if `sYZ` is a limit cone we can construct a binary fan over `X sYZ.X`. This is an ingredient of building the associator for a cartesian category. -/ def binary_fan.assoc {X Y Z : C} {sXY : binary_fan X Y} {sYZ : binary_fan Y Z} (Q : is_limit sYZ) (s : binary_fan sXY.X Z) : binary_fan X sYZ.X := binary_fan.mk (s.fst ≫ sXY.fst) (Q.lift (binary_fan.mk (s.fst ≫ sXY.snd) s.snd)) @[simp] lemma binary_fan.assoc_fst {X Y Z : C} {sXY : binary_fan X Y} {sYZ : binary_fan Y Z} (Q : is_limit sYZ) (s : binary_fan sXY.X Z) : (s.assoc Q).fst = s.fst ≫ sXY.fst := rfl @[simp] lemma binary_fan.assoc_snd {X Y Z : C} {sXY : binary_fan X Y} {sYZ : binary_fan Y Z} (Q : is_limit sYZ) (s : binary_fan sXY.X Z) : (s.assoc Q).snd = Q.lift (binary_fan.mk (s.fst ≫ sXY.snd) s.snd) := rfl /-- Given binary fans `sXY` over `X Y`, and `sYZ` over `Y Z`, and `s` over `X sYZ.X`, if `sYZ` is a limit cone we can construct a binary fan over `sXY.X Z`. This is an ingredient of building the associator for a cartesian category. -/ def binary_fan.assoc_inv {X Y Z : C} {sXY : binary_fan X Y} (P : is_limit sXY) {sYZ : binary_fan Y Z} (s : binary_fan X sYZ.X) : binary_fan sXY.X Z := binary_fan.mk (P.lift (binary_fan.mk s.fst (s.snd ≫ sYZ.fst))) (s.snd ≫ sYZ.snd) @[simp] lemma binary_fan.assoc_inv_fst {X Y Z : C} {sXY : binary_fan X Y} (P : is_limit sXY) {sYZ : binary_fan Y Z} (s : binary_fan X sYZ.X) : (s.assoc_inv P).fst = P.lift (binary_fan.mk s.fst (s.snd ≫ sYZ.fst)) := rfl @[simp] lemma binary_fan.assoc_inv_snd {X Y Z : C} {sXY : binary_fan X Y} (P : is_limit sXY) {sYZ : binary_fan Y Z} (s : binary_fan X sYZ.X) : (s.assoc_inv P).snd = s.snd ≫ sYZ.snd := rfl /-- If all the binary fans involved a limit cones, `binary_fan.assoc` produces another limit cone. -/ @[simps] def is_limit.assoc {X Y Z : C} {sXY : binary_fan X Y} (P : is_limit sXY) {sYZ : binary_fan Y Z} (Q : is_limit sYZ) {s : binary_fan sXY.X Z} (R : is_limit s) : is_limit (s.assoc Q) := { lift := λ t, R.lift (binary_fan.assoc_inv P t), fac' := λ t, begin rintro ⟨⟨⟩⟩; simp, apply Q.hom_ext, rintro ⟨⟨⟩⟩; simp, end, uniq' := λ t m w, begin have h := R.uniq (binary_fan.assoc_inv P t) m, rw h, rintro ⟨⟨⟩⟩; simp, apply P.hom_ext, rintro ⟨⟨⟩⟩; simp, { exact w ⟨walking_pair.left⟩, }, { specialize w ⟨walking_pair.right⟩, simp at w, rw [←w], simp, }, { specialize w ⟨walking_pair.right⟩, simp at w, rw [←w], simp, }, end, } /-- Given two pairs of limit cones corresponding to the parenthesisations of `X × Y × Z`, we obtain an isomorphism between the cone points. -/ @[reducible] def binary_fan.associator {X Y Z : C} {sXY : binary_fan X Y} (P : is_limit sXY) {sYZ : binary_fan Y Z} (Q : is_limit sYZ) {s : binary_fan sXY.X Z} (R : is_limit s) {t : binary_fan X sYZ.X} (S : is_limit t) : s.X ≅ t.X := is_limit.cone_point_unique_up_to_iso (is_limit.assoc P Q R) S /-- Given a fixed family of limit data for every pair `X Y`, we obtain an associator. -/ @[reducible] def binary_fan.associator_of_limit_cone (L : Π X Y : C, limit_cone (pair X Y)) (X Y Z : C) : (L (L X Y).cone.X Z).cone.X ≅ (L X (L Y Z).cone.X).cone.X := binary_fan.associator (L X Y).is_limit (L Y Z).is_limit (L (L X Y).cone.X Z).is_limit (L X (L Y Z).cone.X).is_limit local attribute [tidy] tactic.discrete_cases /-- Construct a left unitor from specified limit cones. -/ @[simps] def binary_fan.left_unitor {X : C} {s : cone (functor.empty.{v} C)} (P : is_limit s) {t : binary_fan s.X X} (Q : is_limit t) : t.X ≅ X := { hom := t.snd, inv := Q.lift (binary_fan.mk (P.lift { X := X, π := { app := discrete.rec (pempty.rec _) } }) (𝟙 X) ), hom_inv_id' := by { apply Q.hom_ext, rintro ⟨⟨⟩⟩, { apply P.hom_ext, rintro ⟨⟨⟩⟩, }, { simp, }, }, } /-- Construct a right unitor from specified limit cones. -/ @[simps] def binary_fan.right_unitor {X : C} {s : cone (functor.empty.{v} C)} (P : is_limit s) {t : binary_fan X s.X} (Q : is_limit t) : t.X ≅ X := { hom := t.fst, inv := Q.lift (binary_fan.mk (𝟙 X) (P.lift { X := X, π := { app := discrete.rec (pempty.rec _) } })), hom_inv_id' := by { apply Q.hom_ext, rintro ⟨⟨⟩⟩, { simp, }, { apply P.hom_ext, rintro ⟨⟨⟩⟩, }, }, } end end limits open category_theory.limits section local attribute [tidy] tactic.case_bash variables {C} variables (𝒯 : limit_cone (functor.empty.{v} C)) variables (ℬ : Π (X Y : C), limit_cone (pair X Y)) namespace monoidal_of_chosen_finite_products /-- Implementation of the tensor product for `monoidal_of_chosen_finite_products`. -/ @[reducible] def tensor_obj (X Y : C) : C := (ℬ X Y).cone.X /-- Implementation of the tensor product of morphisms for `monoidal_of_chosen_finite_products`. -/ @[reducible] def tensor_hom {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) : tensor_obj ℬ W Y ⟶ tensor_obj ℬ X Z := (binary_fan.is_limit.lift' (ℬ X Z).is_limit ((ℬ W Y).cone.π.app ⟨walking_pair.left⟩ ≫ f) (((ℬ W Y).cone.π.app ⟨walking_pair.right⟩ : (ℬ W Y).cone.X ⟶ Y) ≫ g)).val lemma tensor_id (X₁ X₂ : C) : tensor_hom ℬ (𝟙 X₁) (𝟙 X₂) = 𝟙 (tensor_obj ℬ X₁ X₂) := begin apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩; { dsimp [tensor_hom], simp, }, end lemma tensor_comp {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂) : tensor_hom ℬ (f₁ ≫ g₁) (f₂ ≫ g₂) = tensor_hom ℬ f₁ f₂ ≫ tensor_hom ℬ g₁ g₂ := begin apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩; { dsimp [tensor_hom], simp, }, end lemma pentagon (W X Y Z : C) : tensor_hom ℬ (binary_fan.associator_of_limit_cone ℬ W X Y).hom (𝟙 Z) ≫ (binary_fan.associator_of_limit_cone ℬ W (tensor_obj ℬ X Y) Z).hom ≫ tensor_hom ℬ (𝟙 W) (binary_fan.associator_of_limit_cone ℬ X Y Z).hom = (binary_fan.associator_of_limit_cone ℬ (tensor_obj ℬ W X) Y Z).hom ≫ (binary_fan.associator_of_limit_cone ℬ W X (tensor_obj ℬ Y Z)).hom := begin dsimp [tensor_hom], apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩, { simp, }, { apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩, { simp, }, apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩, { simp, }, { simp, }, } end lemma triangle (X Y : C) : (binary_fan.associator_of_limit_cone ℬ X 𝒯.cone.X Y).hom ≫ tensor_hom ℬ (𝟙 X) (binary_fan.left_unitor 𝒯.is_limit (ℬ 𝒯.cone.X Y).is_limit).hom = tensor_hom ℬ (binary_fan.right_unitor 𝒯.is_limit (ℬ X 𝒯.cone.X).is_limit).hom (𝟙 Y) := begin dsimp [tensor_hom], apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩; simp, end lemma left_unitor_naturality {X₁ X₂ : C} (f : X₁ ⟶ X₂) : tensor_hom ℬ (𝟙 𝒯.cone.X) f ≫ (binary_fan.left_unitor 𝒯.is_limit (ℬ 𝒯.cone.X X₂).is_limit).hom = (binary_fan.left_unitor 𝒯.is_limit (ℬ 𝒯.cone.X X₁).is_limit).hom ≫ f := begin dsimp [tensor_hom], simp, end lemma right_unitor_naturality {X₁ X₂ : C} (f : X₁ ⟶ X₂) : tensor_hom ℬ f (𝟙 𝒯.cone.X) ≫ (binary_fan.right_unitor 𝒯.is_limit (ℬ X₂ 𝒯.cone.X).is_limit).hom = (binary_fan.right_unitor 𝒯.is_limit (ℬ X₁ 𝒯.cone.X).is_limit).hom ≫ f := begin dsimp [tensor_hom], simp, end lemma associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : tensor_hom ℬ (tensor_hom ℬ f₁ f₂) f₃ ≫ (binary_fan.associator_of_limit_cone ℬ Y₁ Y₂ Y₃).hom = (binary_fan.associator_of_limit_cone ℬ X₁ X₂ X₃).hom ≫ tensor_hom ℬ f₁ (tensor_hom ℬ f₂ f₃) := begin dsimp [tensor_hom], apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩, { simp, }, { apply is_limit.hom_ext (ℬ _ _).is_limit, rintro ⟨⟨⟩⟩, { simp, }, { simp, }, }, end end monoidal_of_chosen_finite_products open monoidal_of_chosen_finite_products /-- A category with a terminal object and binary products has a natural monoidal structure. -/ def monoidal_of_chosen_finite_products : monoidal_category C := { tensor_unit := 𝒯.cone.X, tensor_obj := λ X Y, tensor_obj ℬ X Y, tensor_hom := λ _ _ _ _ f g, tensor_hom ℬ f g, tensor_id' := tensor_id ℬ, tensor_comp' := λ _ _ _ _ _ _ f₁ f₂ g₁ g₂, tensor_comp ℬ f₁ f₂ g₁ g₂, associator := λ X Y Z, binary_fan.associator_of_limit_cone ℬ X Y Z, left_unitor := λ X, binary_fan.left_unitor (𝒯.is_limit) (ℬ 𝒯.cone.X X).is_limit, right_unitor := λ X, binary_fan.right_unitor (𝒯.is_limit) (ℬ X 𝒯.cone.X).is_limit, pentagon' := pentagon ℬ, triangle' := triangle 𝒯 ℬ, left_unitor_naturality' := λ _ _ f, left_unitor_naturality 𝒯 ℬ f, right_unitor_naturality' := λ _ _ f, right_unitor_naturality 𝒯 ℬ f, associator_naturality' := λ _ _ _ _ _ _ f₁ f₂ f₃, associator_naturality ℬ f₁ f₂ f₃, } namespace monoidal_of_chosen_finite_products open monoidal_category /-- A type synonym for `C` carrying a monoidal category structure corresponding to a fixed choice of limit data for the empty functor, and for `pair X Y` for every `X Y : C`. This is an implementation detail for `symmetric_of_chosen_finite_products`. -/ @[derive category, nolint unused_arguments has_nonempty_instance] def monoidal_of_chosen_finite_products_synonym (𝒯 : limit_cone (functor.empty.{v} C)) (ℬ : Π (X Y : C), limit_cone (pair X Y)):= C instance : monoidal_category (monoidal_of_chosen_finite_products_synonym 𝒯 ℬ) := monoidal_of_chosen_finite_products 𝒯 ℬ end monoidal_of_chosen_finite_products end end category_theory
d1b949a6c284a53bd17c1ac990e28e4973b61894
ff5230333a701471f46c57e8c115a073ebaaa448
/library/init/meta/tactic.lean
242fe8cb117d7b4aaa7f0cc77fe4a1a0fa670b69
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
61,882
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.function init.data.option.basic init.util import init.category.combinators init.category.monad init.category.alternative init.category.monad_fail import init.data.nat.div init.meta.exceptional init.meta.format init.meta.environment import init.meta.pexpr init.data.repr init.data.string.basic init.meta.interaction_monad meta constant tactic_state : Type universes u v namespace tactic_state meta constant env : tactic_state → environment /-- Format the given tactic state. If `target_lhs_only` is true and the target is of the form `lhs ~ rhs`, where `~` is a simplification relation, then only the `lhs` is displayed. Remark: the parameter `target_lhs_only` is a temporary hack used to implement the `conv` monad. It will be removed in the future. -/ meta constant to_format (s : tactic_state) (target_lhs_only : bool := ff) : format /-- Format expression with respect to the main goal in the tactic state. If the tactic state does not contain any goals, then format expression using an empty local context. -/ meta constant format_expr : tactic_state → expr → format meta constant get_options : tactic_state → options meta constant set_options : tactic_state → options → tactic_state end tactic_state meta instance : has_to_format tactic_state := ⟨tactic_state.to_format⟩ meta instance : has_to_string tactic_state := ⟨λ s, (to_fmt s).to_string s.get_options⟩ /-- `tactic` is the monad for building tactics. You use this to: - View and modify the local goals and hypotheses in the prover's state. - Invoke type checking and elaboration of terms. - View and modify the environment. - Build new tactics out of existing ones such as `simp` and `rewrite`. -/ @[reducible] meta def tactic := interaction_monad tactic_state @[reducible] meta def tactic_result := interaction_monad.result tactic_state namespace tactic export interaction_monad (hiding failed fail) /-- Cause the tactic to fail with no error message. -/ meta def failed {α : Type} : tactic α := interaction_monad.failed meta def fail {α : Type u} {β : Type v} [has_to_format β] (msg : β) : tactic α := interaction_monad.fail msg end tactic namespace tactic_result export interaction_monad.result end tactic_result open tactic open tactic_result infixl ` >>=[tactic] `:2 := interaction_monad_bind infixl ` >>[tactic] `:2 := interaction_monad_seq meta instance : alternative tactic := { failure := @interaction_monad.failed _, orelse := @interaction_monad_orelse _, ..interaction_monad.monad } meta def {u₁ u₂} tactic.up {α : Type u₂} (t : tactic α) : tactic (ulift.{u₁} α) := λ s, match t s with | success a s' := success (ulift.up a) s' | exception t ref s := exception t ref s end meta def {u₁ u₂} tactic.down {α : Type u₂} (t : tactic (ulift.{u₁} α)) : tactic α := λ s, match t s with | success (ulift.up a) s' := success a s' | exception t ref s := exception t ref s end namespace interactive /-- Typeclass for custom interaction monads, which provides the information required to convert an interactive-mode construction to a `tactic` which can actually be executed. Given a `[monad m]`, `execute_with` explains how to turn a `begin ... end` block, or a `by ...` statement into a `tactic α` which can actually be executed. The `inhabited` first argument facilitates the passing of an optional configuration parameter `config`, using the syntax: ``` begin [custom_monad] with config, ... end ``` -/ meta class executor (m : Type → Type u) [monad m] := (config_type : Type) [inhabited : inhabited config_type] (execute_with : config_type → m unit → tactic unit) attribute [inline] executor.execute_with @[inline] meta def executor.execute_explicit (m : Type → Type u) [monad m] [e : executor m] : m unit → tactic unit := executor.execute_with e.inhabited.default @[inline] meta def executor.execute_with_explicit (m : Type → Type u) [monad m] [executor m] : executor.config_type m → m unit → tactic unit := executor.execute_with /-- Default `executor` instance for `tactic`s themselves -/ meta instance : executor tactic := { config_type := unit, inhabited := ⟨()⟩, execute_with := λ _, id } end interactive namespace tactic variables {α : Type u} meta def try_core (t : tactic α) : tactic (option α) := λ s, result.cases_on (t s) (λ a, success (some a)) (λ e ref s', success none s) /-- Does nothing. -/ meta def skip : tactic unit := success () meta def try (t : tactic α) : tactic unit := try_core t >>[tactic] skip meta def try_lst : list (tactic unit) → tactic unit | [] := failed | (tac :: tacs) := λ s, match tac s with | result.success _ s' := try (try_lst tacs) s' | result.exception e p s' := match try_lst tacs s' with | result.exception _ _ _ := result.exception e p s' | r := r end end meta def fail_if_success {α : Type u} (t : tactic α) : tactic unit := λ s, result.cases_on (t s) (λ a s, mk_exception "fail_if_success combinator failed, given tactic succeeded" none s) (λ e ref s', success () s) meta def success_if_fail {α : Type u} (t : tactic α) : tactic unit := λ s, match t s with | (interaction_monad.result.exception _ _ s') := success () s | (interaction_monad.result.success a s) := mk_exception "success_if_fail combinator failed, given tactic succeeded" none s end open nat /-- (iterate_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def iterate_at_most : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := (do t, iterate_at_most n t) <|> skip /-- (iterate_exactly n t) : execute t n times -/ meta def iterate_exactly : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := do t, iterate_exactly n t /-- Repeat the given tactic forever. -/ meta def iterate : tactic unit → tactic unit := iterate_at_most 100000 meta def returnopt (e : option α) : tactic α := λ s, match e with | (some a) := success a s | none := mk_exception "failed" none s end meta instance opt_to_tac : has_coe (option α) (tactic α) := ⟨returnopt⟩ /-- Decorate t's exceptions with msg. -/ meta def decorate_ex (msg : format) (t : tactic α) : tactic α := λ s, result.cases_on (t s) success (λ opt_thunk, match opt_thunk with | some e := exception (some (λ u, msg ++ format.nest 2 (format.line ++ e u))) | none := exception none end) /-- Set the tactic_state. -/ @[inline] meta def write (s' : tactic_state) : tactic unit := λ s, success () s' /-- Get the tactic_state. -/ @[inline] meta def read : tactic tactic_state := λ s, success s s meta def get_options : tactic options := do s ← read, return s.get_options meta def set_options (o : options) : tactic unit := do s ← read, write (s.set_options o) meta def save_options {α : Type} (t : tactic α) : tactic α := do o ← get_options, a ← t, set_options o, return a meta def returnex {α : Type} (e : exceptional α) : tactic α := λ s, match e with | exceptional.success a := success a s | exceptional.exception ._ f := match get_options s with | success opt _ := exception (some (λ u, f opt)) none s | exception _ _ _ := exception (some (λ u, f options.mk)) none s end end meta instance ex_to_tac {α : Type} : has_coe (exceptional α) (tactic α) := ⟨returnex⟩ end tactic meta def tactic_format_expr (e : expr) : tactic format := do s ← tactic.read, return (tactic_state.format_expr s e) meta class has_to_tactic_format (α : Type u) := (to_tactic_format : α → tactic format) meta instance : has_to_tactic_format expr := ⟨tactic_format_expr⟩ meta def tactic.pp {α : Type u} [has_to_tactic_format α] : α → tactic format := has_to_tactic_format.to_tactic_format open tactic format meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (list α) := ⟨λ l, to_fmt <$> l.mmap pp⟩ meta instance (α : Type u) (β : Type v) [has_to_tactic_format α] [has_to_tactic_format β] : has_to_tactic_format (α × β) := ⟨λ ⟨a, b⟩, to_fmt <$> (prod.mk <$> pp a <*> pp b)⟩ meta def option_to_tactic_format {α : Type u} [has_to_tactic_format α] : option α → tactic format | (some a) := do fa ← pp a, return (to_fmt "(some " ++ fa ++ ")") | none := return "none" meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (option α) := ⟨option_to_tactic_format⟩ meta instance {α} (a : α) : has_to_tactic_format (reflected a) := ⟨λ h, pp h.to_expr⟩ @[priority 10] meta instance has_to_format_to_has_to_tactic_format (α : Type) [has_to_format α] : has_to_tactic_format α := ⟨(λ x, return x) ∘ to_fmt⟩ namespace tactic open tactic_state meta def get_env : tactic environment := do s ← read, return $ env s meta def get_decl (n : name) : tactic declaration := do s ← read, (env s).get n meta def trace {α : Type u} [has_to_tactic_format α] (a : α) : tactic unit := do fmt ← pp a, return $ _root_.trace_fmt fmt (λ u, ()) meta def trace_call_stack : tactic unit := assume state, _root_.trace_call_stack (success () state) meta def timetac {α : Type u} (desc : string) (t : thunk (tactic α)) : tactic α := λ s, timeit desc (t () s) meta def trace_state : tactic unit := do s ← read, trace $ to_fmt s /-- A parameter representing how aggressively definitions should be unfolded when trying to decide if two terms match, unify or are definitionally equal. By default, theorem declarations are never unfolded. - `all` will unfold everything, including macros and theorems. Except projection macros. - `semireducible` will unfold everything except theorems and definitions tagged as irreducible. - `instances` will unfold all class instance definitions and definitions tagged with reducible. - `reducible` will only unfold definitions tagged with the `reducible` attribute. - `none` will never unfold anything. [NOTE] You are not allowed to tag a definition with more than one of `reducible`, `irreducible`, `semireducible` attributes. [NOTE] there is a config flag `m_unfold_lemmas`that will make it unfold theorems. -/ inductive transparency | all | semireducible | instances | reducible | none export transparency (reducible semireducible) /-- (eval_expr α e) evaluates 'e' IF 'e' has type 'α'. -/ meta constant eval_expr (α : Type u) [reflected α] : expr → tactic α /-- Return the partial term/proof constructed so far. Note that the resultant expression may contain variables that are not declarate in the current main goal. -/ meta constant result : tactic expr /-- Display the partial term/proof constructed so far. This tactic is *not* equivalent to `do { r ← result, s ← read, return (format_expr s r) }` because this one will format the result with respect to the current goal, and trace_result will do it with respect to the initial goal. -/ meta constant format_result : tactic format /-- Return target type of the main goal. Fail if tactic_state does not have any goal left. -/ meta constant target : tactic expr meta constant intro_core : name → tactic expr meta constant intron : nat → tactic unit /-- Clear the given local constant. The tactic fails if the given expression is not a local constant. -/ meta constant clear : expr → tactic unit /-- `revert_lst : list expr → tactic nat` is the reverse of `intron`. It takes a local constant `c` and puts it back as bound by a `pi` or `elet` of the main target. If there are other local constants that depend on `c`, these are also reverted. Because of this, the `nat` that is returned is the actual number of reverted local constants. Example: with `x : ℕ, h : P(x) ⊢ T(x)`, `revert_lst [x]` returns `2` and produces the state ` ⊢ Π x, P(x) → T(x)`. -/ meta constant revert_lst : list expr → tactic nat /-- Return `e` in weak head normal form with respect to the given transparency setting. If `unfold_ginductive` is `tt`, then nested and/or mutually recursive inductive datatype constructors and types are unfolded. Recall that nested and mutually recursive inductive datatype declarations are compiled into primitive datatypes accepted by the Kernel. -/ meta constant whnf (e : expr) (md := semireducible) (unfold_ginductive := tt) : tactic expr /-- (head) eta expand the given expression. `f : α → β` head-eta-expands to `λ a, f a`. If `f` isn't a function then it just returns `f`. -/ meta constant head_eta_expand : expr → tactic expr /-- (head) beta reduction. `(λ x, B) c` reduces to `B[x/c]`. -/ meta constant head_beta : expr → tactic expr /-- (head) zeta reduction. Reduction of let bindings at the head of the expression. `let x : a := b in c` reduces to `c[x/b]`. -/ meta constant head_zeta : expr → tactic expr /-- Zeta reduction. Reduction of let bindings. `let x : a := b in c` reduces to `c[x/b]`. -/ meta constant zeta : expr → tactic expr /-- (head) eta reduction. `(λ x, f x)` reduces to `f`. -/ meta constant head_eta : expr → tactic expr /-- Succeeds if `t` and `s` can be unified using the given transparency setting. -/ meta constant unify (t s : expr) (md := semireducible) (approx := ff) : tactic unit /-- Similar to `unify`, but it treats metavariables as constants. -/ meta constant is_def_eq (t s : expr) (md := semireducible) (approx := ff) : tactic unit /-- Infer the type of the given expression. Remark: transparency does not affect type inference -/ meta constant infer_type : expr → tactic expr /-- Get the `local_const` expr for the given `name`. -/ meta constant get_local : name → tactic expr /-- Resolve a name using the current local context, environment, aliases, etc. -/ meta constant resolve_name : name → tactic pexpr /-- Return the hypothesis in the main goal. Fail if tactic_state does not have any goal left. -/ meta constant local_context : tactic (list expr) /-- Get a fresh name that is guaranteed to not be in use in the local context. If `n` is provided and `n` is not in use, then `n` is returned. Otherwise a number `i` is appended to give `"n_i"`. -/ meta constant get_unused_name (n : name := `_x) (i : option nat := none) : tactic name /-- Helper tactic for creating simple applications where some arguments are inferred using type inference. Example, given ``` rel.{l_1 l_2} : Pi (α : Type.{l_1}) (β : α -> Type.{l_2}), (Pi x : α, β x) -> (Pi x : α, β x) -> , Prop nat : Type real : Type vec.{l} : Pi (α : Type l) (n : nat), Type.{l1} f g : Pi (n : nat), vec real n ``` then ``` mk_app_core semireducible "rel" [f, g] ``` returns the application ``` rel.{1 2} nat (fun n : nat, vec real n) f g ``` The unification constraints due to type inference are solved using the transparency `md`. -/ meta constant mk_app (fn : name) (args : list expr) (md := semireducible) : tactic expr /-- Similar to `mk_app`, but allows to specify which arguments are explicit/implicit. Example, given `(a b : nat)` then ``` mk_mapp "ite" [some (a > b), none, none, some a, some b] ``` returns the application ``` @ite.{1} (a > b) (nat.decidable_gt a b) nat a b ``` -/ meta constant mk_mapp (fn : name) (args : list (option expr)) (md := semireducible) : tactic expr /-- (mk_congr_arg h₁ h₂) is a more efficient version of (mk_app `congr_arg [h₁, h₂]) -/ meta constant mk_congr_arg : expr → expr → tactic expr /-- (mk_congr_fun h₁ h₂) is a more efficient version of (mk_app `congr_fun [h₁, h₂]) -/ meta constant mk_congr_fun : expr → expr → tactic expr /-- (mk_congr h₁ h₂) is a more efficient version of (mk_app `congr [h₁, h₂]) -/ meta constant mk_congr : expr → expr → tactic expr /-- (mk_eq_refl h) is a more efficient version of (mk_app `eq.refl [h]) -/ meta constant mk_eq_refl : expr → tactic expr /-- (mk_eq_symm h) is a more efficient version of (mk_app `eq.symm [h]) -/ meta constant mk_eq_symm : expr → tactic expr /-- (mk_eq_trans h₁ h₂) is a more efficient version of (mk_app `eq.trans [h₁, h₂]) -/ meta constant mk_eq_trans : expr → expr → tactic expr /-- (mk_eq_mp h₁ h₂) is a more efficient version of (mk_app `eq.mp [h₁, h₂]) -/ meta constant mk_eq_mp : expr → expr → tactic expr /-- (mk_eq_mpr h₁ h₂) is a more efficient version of (mk_app `eq.mpr [h₁, h₂]) -/ meta constant mk_eq_mpr : expr → expr → tactic expr /- Given a local constant t, if t has type (lhs = rhs) apply substitution. Otherwise, try to find a local constant that has type of the form (t = t') or (t' = t). The tactic fails if the given expression is not a local constant. -/ meta constant subst_core : expr → tactic unit /-- Close the current goal using `e`. Fail is the type of `e` is not definitionally equal to the target type. -/ meta constant exact (e : expr) (md := semireducible) : tactic unit /-- Elaborate the given quoted expression with respect to the current main goal. Note that this means that any implicit arguments for the given `pexpr` will be applied with fresh metavariables. If `allow_mvars` is tt, then metavariables are tolerated and become new goals if `subgoals` is tt. -/ meta constant to_expr (q : pexpr) (allow_mvars := tt) (subgoals := tt) : tactic expr /-- Return true if the given expression is a type class. -/ meta constant is_class : expr → tactic bool /-- Try to create an instance of the given type class. -/ meta constant mk_instance : expr → tactic expr /-- Change the target of the main goal. The input expression must be definitionally equal to the current target. If `check` is `ff`, then the tactic does not check whether `e` is definitionally equal to the current target. If it is not, then the error will only be detected by the kernel type checker. -/ meta constant change (e : expr) (check : bool := tt): tactic unit /-- `assert_core H T`, adds a new goal for T, and change target to `T -> target`. -/ meta constant assert_core : name → expr → tactic unit /-- `assertv_core H T P`, change target to (T -> target) if P has type T. -/ meta constant assertv_core : name → expr → expr → tactic unit /-- `define_core H T`, adds a new goal for T, and change target to `let H : T := ?M in target` in the current goal. -/ meta constant define_core : name → expr → tactic unit /-- `definev_core H T P`, change target to `let H : T := P in target` if P has type T. -/ meta constant definev_core : name → expr → expr → tactic unit /-- Rotate goals to the left. That is, `rotate_left 1` takes the main goal and puts it to the back of the subgoal list. -/ meta constant rotate_left : nat → tactic unit /-- Gets a list of metavariables, one for each goal. -/ meta constant get_goals : tactic (list expr) /-- Replace the current list of goals with the given one. Each expr in the list should be a metavariable. Any assigned metavariables will be ignored.-/ meta constant set_goals : list expr → tactic unit /-- How to order the new goals made from an `apply` tactic. Supposing we were applying `e : ∀ (a:α) (p : P(a)), Q` - `non_dep_first` would produce goals `⊢ P(?m)`, `⊢ α`. It puts the P goal at the front because none of the arguments after `p` in `e` depend on `p`. It doesn't matter what the result `Q` depends on. - `non_dep_only` would produce goal `⊢ P(?m)`. - `all` would produce goals `⊢ α`, `⊢ P(?m)`. -/ inductive new_goals | non_dep_first | non_dep_only | all /-- Configuration options for the `apply` tactic. - `md` sets how aggressively definitions are unfolded. - `new_goals` is the strategy for ordering new goals. - `instances` if `tt`, then `apply` tries to synthesize unresolved `[...]` arguments using type class resolution. - `auto_param` if `tt`, then `apply` tries to synthesize unresolved `(h : p . tac_id)` arguments using tactic `tac_id`. - `opt_param` if `tt`, then `apply` tries to synthesize unresolved `(a : t := v)` arguments by setting them to `v`. - `unify` if `tt`, then `apply` is free to assign existing metavariables in the goal when solving unification constraints. For example, in the goal `|- ?x < succ 0`, the tactic `apply succ_lt_succ` succeeds with the default configuration, but `apply_with succ_lt_succ {unify := ff}` doesn't since it would require Lean to assign `?x` to `succ ?y` where `?y` is a fresh metavariable. -/ structure apply_cfg := (md := semireducible) (approx := tt) (new_goals := new_goals.non_dep_first) (instances := tt) (auto_param := tt) (opt_param := tt) (unify := tt) /-- Apply the expression `e` to the main goal, the unification is performed using the transparency mode in `cfg`. Supposing `e : Π (a₁:α₁) ... (aₙ:αₙ), P(a₁,...,aₙ)` and the target is `Q`, `apply` will attempt to unify `Q` with `P(?a₁,...?aₙ)`. All of the metavariables that are not assigned are added as new metavariables. If `cfg.approx` is `tt`, then fallback to first-order unification, and approximate context during unification. `cfg.new_goals` specifies which unassigned metavariables become new goals, and their order. If `cfg.instances` is `tt`, then use type class resolution to instantiate unassigned meta-variables. The fields `cfg.auto_param` and `cfg.opt_param` are ignored by this tactic (See `tactic.apply`). It returns a list of all introduced meta variables and the parameter name associated with them, even the assigned ones. -/ meta constant apply_core (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) /- Create a fresh meta universe variable. -/ meta constant mk_meta_univ : tactic level /- Create a fresh meta-variable with the given type. The scope of the new meta-variable is the local context of the main goal. -/ meta constant mk_meta_var : expr → tactic expr /-- Return the value assigned to the given universe meta-variable. Fail if argument is not an universe meta-variable or if it is not assigned. -/ meta constant get_univ_assignment : level → tactic level /-- Return the value assigned to the given meta-variable. Fail if argument is not a meta-variable or if it is not assigned. -/ meta constant get_assignment : expr → tactic expr /-- Return true if the given meta-variable is assigned. Fail if argument is not a meta-variable. -/ meta constant is_assigned : expr → tactic bool /-- Make a name that is guaranteed to be unique. Eg `_fresh.1001.4667`. These will be different for each run of the tactic. -/ meta constant mk_fresh_name : tactic name /-- Induction on `h` using recursor `rec`, names for the new hypotheses are retrieved from `ns`. If `ns` does not have sufficient names, then use the internal binder names in the recursor. It returns for each new goal the name of the constructor (if `rec_name` is a builtin recursor), a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The substitutions map internal names to their replacement terms. If the replacement is again a hypothesis the user name stays the same. The internal names are only valid in the original goal, not in the type context of the new goal. Remark: if `rec_name` is not a builtin recursor, we use parameter names of `rec_name` instead of constructor names. If `rec` is none, then the type of `h` is inferred, if it is of the form `C ...`, tactic uses `C.rec` -/ meta constant induction (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Apply `cases_on` recursor, names for the new hypotheses are retrieved from `ns`. `h` must be a local constant. It returns for each new goal the name of the constructor, a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The number of new goals may be smaller than the number of constructors. Some goals may be discarded when the indices to not match. See `induction` for information on the list of substitutions. The `cases` tactic is implemented using this one, and it relaxes the restriction of `h`. -/ meta constant cases_core (h : expr) (ns : list name := []) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Similar to cases tactic, but does not revert/intro/clear hypotheses. -/ meta constant destruct (e : expr) (md := semireducible) : tactic unit /-- Generalizes the target with respect to `e`. -/ meta constant generalize (e : expr) (n : name := `_x) (md := semireducible) : tactic unit /-- instantiate assigned metavariables in the given expression -/ meta constant instantiate_mvars : expr → tactic expr /-- Add the given declaration to the environment -/ meta constant add_decl : declaration → tactic unit /-- Changes the environment to the `new_env`. `new_env` needs to be a descendant from the current environment. -/ meta constant set_env : environment → tactic unit /-- `doc_string env d k` returns the doc string for `d` (if available) -/ meta constant doc_string : name → tactic string /-- Set the docstring for the given declaration. -/ meta constant add_doc_string : name → string → tactic unit /-- Create an auxiliary definition with name `c` where `type` and `value` may contain local constants and meta-variables. This function collects all dependencies (universe parameters, universe metavariables, local constants (aka hypotheses) and metavariables). It updates the environment in the tactic_state, and returns an expression of the form (c.{l_1 ... l_n} a_1 ... a_m) where l_i's and a_j's are the collected dependencies. -/ meta constant add_aux_decl (c : name) (type : expr) (val : expr) (is_lemma : bool) : tactic expr /-- Returns a list of all top-level (`/-!`) docstrings in the active module and imported ones. The returned object is a list of modules, indexed by `(some filename)` for imported modules and `none` for the active one, where each module in the list is paired with a list of `(position_in_file, docstring)` pairs. -/ meta constant olean_doc_strings : tactic (list (option string × (list (pos × string)))) /-- Returns a list of docstrings in the active module. An entry in the list can be either: - a top-level (`/-!`) docstring, represented as `(none, docstring)` - a declaration-specific (`/--`) docstring, represented as `(some decl_name, docstring)` -/ meta def module_doc_strings : tactic (list (option name × string)) := do /- Obtain a list of top-level docs in current module. -/ mod_docs ← olean_doc_strings, let mod_docs: list (list (option name × string)) := mod_docs.filter_map (λ d, if d.1.is_none then some (d.2.map (λ pos_doc, ⟨none, pos_doc.2⟩)) else none), let mod_docs := mod_docs.join, /- Obtain list of declarations in current module. -/ e ← get_env, let decls := environment.fold e ([]: list name) (λ d acc, let n := d.to_name in if (environment.decl_olean e n).is_none then n::acc else acc), /- Map declarations to those which have docstrings. -/ decls ← decls.mfoldl (λa n, (doc_string n >>= λ doc, pure $ (some n, doc) :: a) <|> pure a) [], pure (mod_docs ++ decls) /-- Set attribute `attr_name` for constant `c_name` with the given priority. If the priority is none, then use default -/ meta constant set_basic_attribute (attr_name : name) (c_name : name) (persistent := ff) (prio : option nat := none) : tactic unit /-- `unset_attribute attr_name c_name` -/ meta constant unset_attribute : name → name → tactic unit /-- `has_attribute attr_name c_name` succeeds if the declaration `decl_name` has the attribute `attr_name`. The result is the priority. -/ meta constant has_attribute : name → name → tactic nat /-- `copy_attribute attr_name c_name d_name` copy attribute `attr_name` from `src` to `tgt` if it is defined for `src` -/ meta def copy_attribute (attr_name : name) (src : name) (p : bool) (tgt : name) : tactic unit := try $ do prio ← has_attribute attr_name src, set_basic_attribute attr_name tgt p (some prio) /-- Name of the declaration currently being elaborated. -/ meta constant decl_name : tactic name /-- `save_type_info e ref` save (typeof e) at position associated with ref -/ meta constant save_type_info {elab : bool} : expr → expr elab → tactic unit meta constant save_info_thunk : pos → (unit → format) → tactic unit /-- Return list of currently open namespaces -/ meta constant open_namespaces : tactic (list name) /-- Return tt iff `t` "occurs" in `e`. The occurrence checking is performed using keyed matching with the given transparency setting. We say `t` occurs in `e` by keyed matching iff there is a subterm `s` s.t. `t` and `s` have the same head, and `is_def_eq t s md` The main idea is to minimize the number of `is_def_eq` checks performed. -/ meta constant kdepends_on (e t : expr) (md := reducible) : tactic bool /-- Abstracts all occurrences of the term `t` in `e` using keyed matching. If `unify` is `ff`, then matching is used instead of unification. That is, metavariables occurring in `e` are not assigned. -/ meta constant kabstract (e t : expr) (md := reducible) (unify := tt) : tactic expr /-- Blocks the execution of the current thread for at least `msecs` milliseconds. This tactic is used mainly for debugging purposes. -/ meta constant sleep (msecs : nat) : tactic unit /-- Type check `e` with respect to the current goal. Fails if `e` is not type correct. -/ meta constant type_check (e : expr) (md := semireducible) : tactic unit open list nat /-- A `tag` is a list of `names`. These are attached to goals to help tactics track them.-/ def tag : Type := list name /-- Enable/disable goal tagging. -/ meta constant enable_tags (b : bool) : tactic unit /-- Return tt iff goal tagging is enabled. -/ meta constant tags_enabled : tactic bool /-- Tag goal `g` with tag `t`. It does nothing if goal tagging is disabled. Remark: `set_goal g []` removes the tag -/ meta constant set_tag (g : expr) (t : tag) : tactic unit /-- Return tag associated with `g`. Return `[]` if there is no tag. -/ meta constant get_tag (g : expr) : tactic tag /-- By default, Lean only considers local instances in the header of declarations. This has two main benefits. 1- Results produced by the type class resolution procedure can be easily cached. 2- The set of local instances does not have to be recomputed. This approach has the following disadvantages: 1- Frozen local instances cannot be reverted. 2- Local instances defined inside of a declaration are not considered during type class resolution. This tactic resets the set of local instances. After executing this tactic, the set of local instances will be recomputed and the cache will be frequently reset. Note that, the cache is still used when executing a single tactic that may generate many type class resolution problems (e.g., `simp`). -/ meta constant unfreeze_local_instances : tactic unit /- Return the list of frozen local instances. Return `none` if local instances were not frozen. -/ meta constant frozen_local_instances : tactic (option (list expr)) meta def induction' (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic unit := induction h ns rec md >> return () /-- Remark: set_goals will erase any solved goal -/ meta def cleanup : tactic unit := get_goals >>= set_goals /-- Auxiliary definition used to implement begin ... end blocks -/ meta def step {α : Type u} (t : tactic α) : tactic unit := t >>[tactic] cleanup meta def istep {α : Type u} (line0 col0 : ℕ) (line col : ℕ) (t : tactic α) : tactic unit := λ s, (@scope_trace _ line col (λ _, step t s)).clamp_pos line0 line col meta def is_prop (e : expr) : tactic bool := do t ← infer_type e, return (t = `(Prop)) /-- Return true iff n is the name of declaration that is a proposition. -/ meta def is_prop_decl (n : name) : tactic bool := do env ← get_env, d ← env.get n, t ← return $ d.type, is_prop t meta def is_proof (e : expr) : tactic bool := infer_type e >>= is_prop meta def whnf_no_delta (e : expr) : tactic expr := whnf e transparency.none /-- Return `e` in weak head normal form with respect to the given transparency setting, or `e` head is a generalized constructor or inductive datatype. -/ meta def whnf_ginductive (e : expr) (md := semireducible) : tactic expr := whnf e md ff meta def whnf_target : tactic unit := target >>= whnf >>= change /-- Change the target of the main goal. The input expression must be definitionally equal to the current target. The tactic does not check whether `e` is definitionally equal to the current target. The error will only be detected by the kernel type checker. -/ meta def unsafe_change (e : expr) : tactic unit := change e ff /-- Pi or elet introduction. Given the tactic state `⊢ Π x : α, Y`, ``intro `hello`` will produce the state `hello : α ⊢ Y[x/hello]`. Returns the new local constant. Similarly for `elet` expressions. If the target is not a Pi or elet it will try to put it in WHNF. -/ meta def intro (n : name) : tactic expr := do t ← target, if expr.is_pi t ∨ expr.is_let t then intro_core n else whnf_target >> intro_core n /-- Like `intro` except the name is derived from the bound name in the Π. -/ meta def intro1 : tactic expr := intro `_ /-- Repeatedly apply `intro1` and return the list of new local constants in order of introduction.-/ meta def intros : tactic (list expr) := do t ← target, match t with | expr.pi _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | expr.elet _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | _ := return [] end /-- Same as `intros`, except with the given names for the new hypotheses. Use the name ```_``` to instead use the binder's name.-/ meta def intro_lst : list name → tactic (list expr) | [] := return [] | (n::ns) := do H ← intro n, Hs ← intro_lst ns, return (H :: Hs) /-- Introduces new hypotheses with forward dependencies. -/ meta def intros_dep : tactic (list expr) := do t ← target, let proc (b : expr) := if b.has_var_idx 0 then do h ← intro1, hs ← intros_dep, return (h::hs) else -- body doesn't depend on new hypothesis return [], match t with | expr.pi _ _ _ b := proc b | expr.elet _ _ _ b := proc b | _ := return [] end meta def introv : list name → tactic (list expr) | [] := intros_dep | (n::ns) := do hs ← intros_dep, h ← intro n, hs' ← introv ns, return (hs ++ h :: hs') /-- Returns n fully qualified if it refers to a constant, or else fails. -/ meta def resolve_constant (n : name) : tactic name := do (expr.const n _) ← resolve_name n, pure n meta def to_expr_strict (q : pexpr) : tactic expr := to_expr q /-- Example: with `x : ℕ, h : P(x) ⊢ T(x)`, `revert x` returns `2` and produces the state ` ⊢ Π x, P(x) → T(x)`. -/ meta def revert (l : expr) : tactic nat := revert_lst [l] /- Revert "all" hypotheses. Actually, the tactic only reverts hypotheses occurring after the last frozen local instance. Recall that frozen local instances cannot be reverted. We can use `unfreeze_local_instances` to workaround this limitation. -/ meta def revert_all : tactic nat := do lctx ← local_context, lis ← frozen_local_instances, match lis with | none := revert_lst lctx | some [] := revert_lst lctx /- `hi` is the last local instance. We shoul truncate `lctx` at `hi`. -/ | some (hi::his) := revert_lst $ lctx.foldl (λ r h, if h.local_uniq_name = hi.local_uniq_name then [] else h :: r) [] end meta def clear_lst : list name → tactic unit | [] := skip | (n::ns) := do H ← get_local n, clear H, clear_lst ns meta def match_not (e : expr) : tactic expr := match (expr.is_not e) with | (some a) := return a | none := fail "expression is not a negation" end meta def match_and (e : expr) : tactic (expr × expr) := match (expr.is_and e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a conjunction" end meta def match_or (e : expr) : tactic (expr × expr) := match (expr.is_or e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a disjunction" end meta def match_iff (e : expr) : tactic (expr × expr) := match (expr.is_iff e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an iff" end meta def match_eq (e : expr) : tactic (expr × expr) := match (expr.is_eq e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an equality" end meta def match_ne (e : expr) : tactic (expr × expr) := match (expr.is_ne e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not a disequality" end meta def match_heq (e : expr) : tactic (expr × expr × expr × expr) := do match (expr.is_heq e) with | (some (α, lhs, β, rhs)) := return (α, lhs, β, rhs) | none := fail "expression is not a heterogeneous equality" end meta def match_refl_app (e : expr) : tactic (name × expr × expr) := do env ← get_env, match (environment.is_refl_app env e) with | (some (R, lhs, rhs)) := return (R, lhs, rhs) | none := fail "expression is not an application of a reflexive relation" end meta def match_app_of (e : expr) (n : name) : tactic (list expr) := guard (expr.is_app_of e n) >> return e.get_app_args meta def get_local_type (n : name) : tactic expr := get_local n >>= infer_type meta def trace_result : tactic unit := format_result >>= trace meta def rexact (e : expr) : tactic unit := exact e reducible meta def any_hyp_aux {α : Type} (f : expr → tactic α) : list expr → tactic α | [] := failed | (h :: hs) := f h <|> any_hyp_aux hs meta def any_hyp {α : Type} (f : expr → tactic α) : tactic α := local_context >>= any_hyp_aux f /-- `find_same_type t es` tries to find in es an expression with type definitionally equal to t -/ meta def find_same_type : expr → list expr → tactic expr | e [] := failed | e (H :: Hs) := do t ← infer_type H, (unify e t >> return H) <|> find_same_type e Hs meta def find_assumption (e : expr) : tactic expr := do ctx ← local_context, find_same_type e ctx meta def assumption : tactic unit := do { ctx ← local_context, t ← target, H ← find_same_type t ctx, exact H } <|> fail "assumption tactic failed" meta def save_info (p : pos) : tactic unit := do s ← read, tactic.save_info_thunk p (λ _, tactic_state.to_format s) notation `‹` p `›` := (by assumption : p) /-- Swap first two goals, do nothing if tactic state does not have at least two goals. -/ meta def swap : tactic unit := do gs ← get_goals, match gs with | (g₁ :: g₂ :: rs) := set_goals (g₂ :: g₁ :: rs) | e := skip end /-- `assert h t`, adds a new goal for t, and the hypothesis `h : t` in the current goal. -/ meta def assert (h : name) (t : expr) : tactic expr := do assert_core h t, swap, e ← intro h, swap, return e /-- `assertv h t v`, adds the hypothesis `h : t` in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : tactic expr := assertv_core h t v >> intro h /-- `define h t`, adds a new goal for t, and the hypothesis `h : t := ?M` in the current goal. -/ meta def define (h : name) (t : expr) : tactic expr := do define_core h t, swap, e ← intro h, swap, return e /-- `definev h t v`, adds the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : tactic expr := definev_core h t v >> intro h /-- Add `h : t := pr` to the current goal -/ meta def pose (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, definev h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Add `h : t` to the current goal, given a proof `pr : t` -/ meta def note (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, assertv h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Return the number of goals that need to be solved -/ meta def num_goals : tactic nat := do gs ← get_goals, return (length gs) /-- Rotate the goals to the right by `n`. That is, take the goal at the back and push it to the front `n` times. [NOTE] We have to provide the instance argument `[has_mod nat]` because mod for nat was not defined yet -/ meta def rotate_right (n : nat) [has_mod nat] : tactic unit := do ng ← num_goals, if ng = 0 then skip else rotate_left (ng - n % ng) /-- Rotate the goals to the left by `n`. That is, put the main goal to the back `n` times. -/ meta def rotate : nat → tactic unit := rotate_left private meta def repeat_aux (t : tactic unit) : list expr → list expr → tactic unit | [] r := set_goals r.reverse | (g::gs) r := do ok ← try_core (set_goals [g] >> t), match ok with | none := repeat_aux gs (g::r) | _ := do gs' ← get_goals, repeat_aux (gs' ++ gs) r end /-- This tactic is applied to each goal. If the application succeeds, the tactic is applied recursively to all the generated subgoals until it eventually fails. The recursion stops in a subgoal when the tactic has failed to make progress. The tactic `repeat` never fails. -/ meta def repeat (t : tactic unit) : tactic unit := do gs ← get_goals, repeat_aux t gs [] /-- `first [t_1, ..., t_n]` applies the first tactic that doesn't fail. The tactic fails if all t_i's fail. -/ meta def first {α : Type u} : list (tactic α) → tactic α | [] := fail "first tactic failed, no more alternatives" | (t::ts) := t <|> first ts /-- Applies the given tactic to the main goal and fails if it is not solved. -/ meta def solve1 (tac : tactic unit) : tactic unit := do gs ← get_goals, match gs with | [] := fail "solve1 tactic failed, there isn't any goal left to focus" | (g::rs) := do set_goals [g], tac, gs' ← get_goals, match gs' with | [] := set_goals rs | gs := fail "solve1 tactic failed, focused goal has not been solved" end end /-- `solve [t_1, ... t_n]` applies the first tactic that solves the main goal. -/ meta def solve (ts : list (tactic unit)) : tactic unit := first $ map solve1 ts private meta def focus_aux : list (tactic unit) → list expr → list expr → tactic unit | [] [] rs := set_goals rs | (t::ts) [] rs := fail "focus tactic failed, insufficient number of goals" | tts (g::gs) rs := mcond (is_assigned g) (focus_aux tts gs rs) $ do set_goals [g], t::ts ← pure tts | fail "focus tactic failed, insufficient number of tactics", t, rs' ← get_goals, focus_aux ts gs (rs ++ rs') /-- `focus [t_1, ..., t_n]` applies t_i to the i-th goal. Fails if the number of goals is not n. -/ meta def focus (ts : list (tactic unit)) : tactic unit := do gs ← get_goals, focus_aux ts gs [] meta def focus1 {α} (tac : tactic α) : tactic α := do g::gs ← get_goals, match gs with | [] := tac | _ := do set_goals [g], a ← tac, gs' ← get_goals, set_goals (gs' ++ gs), return a end private meta def all_goals_core (tac : tactic unit) : list expr → list expr → tactic unit | [] ac := set_goals ac | (g :: gs) ac := mcond (is_assigned g) (all_goals_core gs ac) $ do set_goals [g], tac, new_gs ← get_goals, all_goals_core gs (ac ++ new_gs) /-- Apply the given tactic to all goals. -/ meta def all_goals (tac : tactic unit) : tactic unit := do gs ← get_goals, all_goals_core tac gs [] private meta def any_goals_core (tac : tactic unit) : list expr → list expr → bool → tactic unit | [] ac progress := guard progress >> set_goals ac | (g :: gs) ac progress := mcond (is_assigned g) (any_goals_core gs ac progress) $ do set_goals [g], succeeded ← try_core tac, new_gs ← get_goals, any_goals_core gs (ac ++ new_gs) (succeeded.is_some || progress) /-- Apply the given tactic to any goal where it succeeds. The tactic succeeds only if tac succeeds for at least one goal. -/ meta def any_goals (tac : tactic unit) : tactic unit := do gs ← get_goals, any_goals_core tac gs [] ff /-- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : tactic unit) (tac2 : tactic unit) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, all_goals tac2, gs' ← get_goals, set_goals (gs' ++ gs) meta def seq_focus (tac1 : tactic unit) (tacs2 : list (tactic unit)) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, focus tacs2, gs' ← get_goals, set_goals (gs' ++ gs) meta instance andthen_seq : has_andthen (tactic unit) (tactic unit) (tactic unit) := ⟨seq⟩ meta instance andthen_seq_focus : has_andthen (tactic unit) (list (tactic unit)) (tactic unit) := ⟨seq_focus⟩ meta constant is_trace_enabled_for : name → bool /-- Execute tac only if option trace.n is set to true. -/ meta def when_tracing (n : name) (tac : tactic unit) : tactic unit := when (is_trace_enabled_for n = tt) tac /-- Fail if there are no remaining goals. -/ meta def fail_if_no_goals : tactic unit := do n ← num_goals, when (n = 0) (fail "tactic failed, there are no goals to be solved") /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := do n ← num_goals, when (n ≠ 0) (fail "done tactic failed, there are unsolved goals") meta def apply_opt_param : tactic unit := do `(opt_param %%t %%v) ← target, exact v meta def apply_auto_param : tactic unit := do `(auto_param %%type %%tac_name_expr) ← target, change type, tac_name ← eval_expr name tac_name_expr, tac ← eval_expr (tactic unit) (expr.const tac_name []), tac meta def has_opt_auto_param (ms : list expr) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param (cfg : apply_cfg) (ms : list expr) : tactic unit := when (cfg.auto_param || cfg.opt_param) $ mwhen (has_opt_auto_param ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> is_assigned m) $ set_goals [m] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def has_opt_auto_param_for_apply (ms : list (name × expr)) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m.2, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param_for_apply (cfg : apply_cfg) (ms : list (name × expr)) : tactic unit := mwhen (has_opt_auto_param_for_apply ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> (is_assigned m.2)) $ set_goals [m.2] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) := do r ← apply_core e cfg, try_apply_opt_auto_param_for_apply cfg r, return r /-- Same as `apply` but __all__ arguments that weren't inferred are added to goal list. -/ meta def fapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.all} /-- Same as `apply` but only goals that don't depend on other goals are added to goal list. -/ meta def eapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.non_dep_only} /-- Try to solve the main goal using type class resolution. -/ meta def apply_instance : tactic unit := do tgt ← target >>= instantiate_mvars, b ← is_class tgt, if b then mk_instance tgt >>= exact else fail "apply_instance tactic fail, target is not a type class" /-- Create a list of universe meta-variables of the given size. -/ meta def mk_num_meta_univs : nat → tactic (list level) | 0 := return [] | (succ n) := do l ← mk_meta_univ, ls ← mk_num_meta_univs n, return (l::ls) /-- Return `expr.const c [l_1, ..., l_n]` where l_i's are fresh universe meta-variables. -/ meta def mk_const (c : name) : tactic expr := do env ← get_env, decl ← env.get c, let num := decl.univ_params.length, ls ← mk_num_meta_univs num, return (expr.const c ls) /-- Apply the constant `c` -/ meta def applyc (c : name) (cfg : apply_cfg := {}) : tactic unit := do c ← mk_const c, apply c cfg, skip meta def eapplyc (c : name) : tactic unit := do c ← mk_const c, eapply c, skip meta def save_const_type_info (n : name) {elab : bool} (ref : expr elab) : tactic unit := try (do c ← mk_const n, save_type_info c ref) /-- Create a fresh universe `?u`, a metavariable `?T : Type.{?u}`, and return metavariable `?M : ?T`. This action can be used to create a meta-variable when we don't know its type at creation time -/ meta def mk_mvar : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), mk_meta_var t /-- Makes a sorry macro with a meta-variable as its type. -/ meta def mk_sorry : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), return $ expr.mk_sorry t /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := target >>= exact ∘ expr.mk_sorry meta def mk_local' (pp_name : name) (bi : binder_info) (type : expr) : tactic expr := do uniq_name ← mk_fresh_name, return $ expr.local_const uniq_name pp_name bi type meta def mk_local_def (pp_name : name) (type : expr) : tactic expr := mk_local' pp_name binder_info.default type meta def mk_local_pis : expr → tactic (list expr × expr) | (expr.pi n bi d b) := do p ← mk_local' n bi d, (ps, r) ← mk_local_pis (expr.instantiate_var b p), return ((p :: ps), r) | e := return ([], e) private meta def get_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, let l := expr.local_const m n bi d, new_b ← whnf (expr.instantiate_var b l), r ← get_pi_arity_aux new_b, return (r + 1) | e := return 0 /-- Compute the arity of the given (Pi-)type -/ meta def get_pi_arity (type : expr) : tactic nat := whnf type >>= get_pi_arity_aux /-- Compute the arity of the given function -/ meta def get_arity (fn : expr) : tactic nat := infer_type fn >>= get_pi_arity meta def triv : tactic unit := mk_const `trivial >>= exact notation `dec_trivial` := of_as_true (by tactic.triv) meta def by_contradiction (H : option name := none) : tactic expr := do tgt : expr ← target, (match_not tgt >> return ()) <|> (mk_mapp `decidable.by_contradiction [some tgt, none] >>= eapply >> skip) <|> fail "tactic by_contradiction failed, target is not a negation nor a decidable proposition (remark: when 'local attribute classical.prop_decidable [instance]' is used all propositions are decidable)", match H with | some n := intro n | none := intro1 end private meta def generalizes_aux (md : transparency) : list expr → tactic unit | [] := skip | (e::es) := generalize e `x md >> generalizes_aux es meta def generalizes (es : list expr) (md := semireducible) : tactic unit := generalizes_aux md es private meta def kdependencies_core (e : expr) (md : transparency) : list expr → list expr → tactic (list expr) | [] r := return r | (h::hs) r := do type ← infer_type h, d ← kdepends_on type e md, if d then kdependencies_core hs (h::r) else kdependencies_core hs r /-- Return all hypotheses that depends on `e` The dependency test is performed using `kdepends_on` with the given transparency setting. -/ meta def kdependencies (e : expr) (md := reducible) : tactic (list expr) := do ctx ← local_context, kdependencies_core e md ctx [] /-- Revert all hypotheses that depend on `e` -/ meta def revert_kdependencies (e : expr) (md := reducible) : tactic nat := kdependencies e md >>= revert_lst meta def revert_kdeps (e : expr) (md := reducible) := revert_kdependencies e md /-- Similar to `cases_core`, but `e` doesn't need to be a hypothesis. Remark, it reverts dependencies using `revert_kdeps`. Two different transparency modes are used `md` and `dmd`. The mode `md` is used with `cases_core` and `dmd` with `generalize` and `revert_kdeps`. It returns the constructor names associated with each new goal. -/ meta def cases (e : expr) (ids : list name := []) (md := semireducible) (dmd := semireducible) : tactic (list name) := if e.is_local_constant then do r ← cases_core e ids md, return $ r.map (λ t, t.1) else do n ← revert_kdependencies e dmd, x ← get_unused_name, (tactic.generalize e x dmd) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, return ()), h ← tactic.intro1, focus1 (do r ← cases_core h ids md, all_goals (intron n), return $ r.map (λ t, t.1)) /-- The same as `exact` except you can add proof holes. -/ meta def refine (e : pexpr) : tactic unit := do tgt : expr ← target, to_expr ``(%%e : %%tgt) tt >>= exact meta def by_cases (e : expr) (h : name) : tactic unit := do dec_e ← (mk_app `decidable [e] <|> fail "by_cases tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases tactic failed, type of given expression is not decidable"), t ← target, tm ← mk_mapp `dite [some e, some inst, some t], seq (apply tm >> skip) (intro h >> skip) meta def funext_core : list name → bool → tactic unit | [] tt := return () | ids only_ids := try $ do some (lhs, rhs) ← expr.is_eq <$> (target >>= whnf), applyc `funext, id ← if ids.empty ∨ ids.head = `_ then do (expr.lam n _ _ _) ← whnf lhs | pure `_, return n else return ids.head, intro id, funext_core ids.tail only_ids meta def funext : tactic unit := funext_core [] ff meta def funext_lst (ids : list name) : tactic unit := funext_core ids tt private meta def get_undeclared_const (env : environment) (base : name) : ℕ → name | i := let n := base <.> ("_aux_" ++ repr i) in if ¬env.contains n then n else get_undeclared_const (i+1) meta def new_aux_decl_name : tactic name := do env ← get_env, n ← decl_name, return $ get_undeclared_const env n 1 private meta def mk_aux_decl_name : option name → tactic name | none := new_aux_decl_name | (some suffix) := do p ← decl_name, return $ p ++ suffix meta def abstract (tac : tactic unit) (suffix : option name := none) (zeta_reduce := tt) : tactic unit := do fail_if_no_goals, gs ← get_goals, type ← if zeta_reduce then target >>= zeta else target, is_lemma ← is_prop type, m ← mk_meta_var type, set_goals [m], tac, n ← num_goals, when (n ≠ 0) (fail "abstract tactic failed, there are unsolved goals"), set_goals gs, val ← instantiate_mvars m, val ← if zeta_reduce then zeta val else return val, c ← mk_aux_decl_name suffix, e ← add_aux_decl c type val is_lemma, exact e /-- `solve_aux type tac` synthesize an element of 'type' using tactic 'tac' -/ meta def solve_aux {α : Type} (type : expr) (tac : tactic α) : tactic (α × expr) := do m ← mk_meta_var type, gs ← get_goals, set_goals [m], a ← tac, set_goals gs, return (a, m) /-- Return tt iff 'd' is a declaration in one of the current open namespaces -/ meta def in_open_namespaces (d : name) : tactic bool := do ns ← open_namespaces, env ← get_env, return $ ns.any (λ n, n.is_prefix_of d) && env.contains d /-- Execute tac for 'max' "heartbeats". The heartbeat is approx. the maximum number of memory allocations (in thousands) performed by 'tac'. This is a deterministic way of interrupting long running tactics. -/ meta def try_for {α} (max : nat) (tac : tactic α) : tactic α := λ s, match _root_.try_for max (tac s) with | some r := r | none := mk_exception "try_for tactic failed, timeout" none s end meta def updateex_env (f : environment → exceptional environment) : tactic unit := do env ← get_env, env ← returnex $ f env, set_env env /- Add a new inductive datatype to the environment name, universe parameters, number of parameters, type, constructors (name and type), is_meta -/ meta def add_inductive (n : name) (ls : list name) (p : nat) (ty : expr) (is : list (name × expr)) (is_meta : bool := ff) : tactic unit := updateex_env $ λe, e.add_inductive n ls p ty is is_meta meta def add_meta_definition (n : name) (lvls : list name) (type value : expr) : tactic unit := add_decl (declaration.defn n lvls type value reducibility_hints.abbrev ff) /-- `add_defn_equations` adds a definition specified by a list of equations. The arguments: * `lp`: list of universe parameters * `params`: list of parameters (binders before the colon); * `fn`: a local constant giving the name and type of the declaration (with `params` in the local context); * `eqns`: a list of equations, each of which is a list of patterns (constructors applied to new local constants) and the branch expression; * `is_meta`: is the definition meta? `add_defn_equations` can be used as: do my_add ← mk_local_def `my_add `(ℕ → ℕ), a ← mk_local_def `a ℕ, b ← mk_local_def `b ℕ, add_defn_equations [a] my_add [ ([``(nat.zero)], a), ([``(nat.succ %%b)], my_add b) ]) ff -- non-meta to create the following definition: def my_add (a : ℕ) : ℕ → ℕ | nat.zero := a | (nat.succ b) := my_add b -/ meta def add_defn_equations (lp : list name) (params : list expr) (fn : expr) (eqns : list (list pexpr × expr)) (is_meta : bool) : tactic unit := do opt ← get_options, updateex_env $ λ e, e.add_defn_eqns opt lp params fn eqns is_meta meta def rename (curr : name) (new : name) : tactic unit := do h ← get_local curr, n ← revert h, intro new, intron (n - 1) /-- "Replace" hypothesis `h : type` with `h : new_type` where `eq_pr` is a proof that (type = new_type). The tactic actually creates a new hypothesis with the same user facing name, and (tries to) clear `h`. The `clear` step fails if `h` has forward dependencies. In this case, the old `h` will remain in the local context. The tactic returns the new hypothesis. -/ meta def replace_hyp (h : expr) (new_type : expr) (eq_pr : expr) : tactic expr := do h_type ← infer_type h, new_h ← assert h.local_pp_name new_type, mk_eq_mp eq_pr h >>= exact, try $ clear h, return new_h meta def main_goal : tactic expr := do g::gs ← get_goals, return g /- Goal tagging support -/ meta def with_enable_tags {α : Type} (t : tactic α) (b := tt) : tactic α := do old ← tags_enabled, enable_tags b, r ← t, enable_tags old, return r meta def get_main_tag : tactic tag := main_goal >>= get_tag meta def set_main_tag (t : tag) : tactic unit := do g ← main_goal, set_tag g t meta def subst (h : expr) : tactic unit := (do guard h.is_local_constant, some (α, lhs, β, rhs) ← expr.is_heq <$> infer_type h, is_def_eq α β, new_h_type ← mk_app `eq [lhs, rhs], new_h_pr ← mk_app `eq_of_heq [h], new_h ← assertv h.local_pp_name new_h_type new_h_pr, try (clear h), subst_core new_h) <|> subst_core h end tactic notation [parsing_only] `command`:max := tactic unit open tactic namespace list meta def for_each {α} : list α → (α → tactic unit) → tactic unit | [] fn := skip | (e::es) fn := do fn e, for_each es fn meta def any_of {α β} : list α → (α → tactic β) → tactic β | [] fn := failed | (e::es) fn := do opt_b ← try_core (fn e), match opt_b with | some b := return b | none := any_of es fn end end list /- Install monad laws tactic and use it to prove some instances. -/ /-- Try to prove with `iff.refl`.-/ meta def order_laws_tac := whnf_target >> intros >> to_expr ``(iff.refl _) >>= exact meta def monad_from_pure_bind {m : Type u → Type v} (pure : Π {α : Type u}, α → m α) (bind : Π {α β : Type u}, m α → (α → m β) → m β) : monad m := {pure := @pure, bind := @bind} meta instance : monad task := {map := @task.map, bind := @task.bind, pure := @task.pure} namespace tactic meta def mk_id_proof (prop : expr) (pr : expr) : expr := expr.app (expr.app (expr.const ``id [level.zero]) prop) pr meta def mk_id_eq (lhs : expr) (rhs : expr) (pr : expr) : tactic expr := do prop ← mk_app `eq [lhs, rhs], return $ mk_id_proof prop pr meta def replace_target (new_target : expr) (pr : expr) : tactic unit := do t ← target, assert `htarget new_target, swap, ht ← get_local `htarget, locked_pr ← mk_id_eq t new_target pr, mk_eq_mpr locked_pr ht >>= exact end tactic
4acd14ed1063766c64cd76f25d1b14ddf58d9b4f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/ctxopt.lean
f527b739812260492539f77e5f1b641a948ffcf1
[ "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
128
lean
import logic -- definition id {A : Type} (a : A) := a section set_option pp.implicit true check id true end check id true
c17fe342fe4441d2f286b65642049adaa28f8da6
88892181780ff536a81e794003fe058062f06758
/src/100_theorems/t003.lean
2d891218535d7a951e38f1fb64ea528bd0a0d180
[]
no_license
AtnNn/lean-sandbox
fe2c44280444e8bb8146ab8ac391c82b480c0a2e
8c68afbdc09213173aef1be195da7a9a86060a97
refs/heads/master
1,623,004,395,876
1,579,969,507,000
1,579,969,507,000
146,666,368
0
0
null
null
null
null
UTF-8
Lean
false
false
81
lean
import data.rat.denumerable theorem t003 : denumerable ℚ := by apply_instance
7467e134d51327af45b898c8355b3ef3ce5614ff
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/Data/Array/BinSearch.lean
a3d7a488d9e166c2ca7efb2a23d9c791f6fddaa0
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
2,922
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.Data.Array.Basic universe u v -- TODO: CLEANUP namespace Array -- TODO: remove the [Inhabited α] parameters as soon as we have the tactic framework for automating proof generation and using Array.fget -- TODO: remove `partial` using well-founded recursion @[specialize] partial def binSearchAux {α : Type u} {β : Type v} [Inhabited β] (lt : α → α → Bool) (found : Option α → β) (as : Array α) (k : α) : Nat → Nat → β | lo, hi => if lo <= hi then let _ := Inhabited.mk k let m := (lo + hi)/2 let a := as.get! m if lt a k then binSearchAux lt found as k (m+1) hi else if lt k a then if m == 0 then found none else binSearchAux lt found as k lo (m-1) else found (some a) else found none @[inline] def binSearch {α : Type} (as : Array α) (k : α) (lt : α → α → Bool) (lo := 0) (hi := as.size - 1) : Option α := if lo < as.size then let hi := if hi < as.size then hi else as.size - 1 binSearchAux lt id as k lo hi else none @[inline] def binSearchContains {α : Type} (as : Array α) (k : α) (lt : α → α → Bool) (lo := 0) (hi := as.size - 1) : Bool := if lo < as.size then let hi := if hi < as.size then hi else as.size - 1 binSearchAux lt Option.isSome as k lo hi else false @[specialize] private partial def binInsertAux {α : Type u} {m : Type u → Type v} [Monad m] (lt : α → α → Bool) (merge : α → m α) (add : Unit → m α) (as : Array α) (k : α) : Nat → Nat → m (Array α) | lo, hi => let _ := Inhabited.mk k -- as[lo] < k < as[hi] let mid := (lo + hi)/2 let midVal := as.get! mid if lt midVal k then if mid == lo then do let v ← add (); pure <| as.insertAt! (lo+1) v else binInsertAux lt merge add as k mid hi else if lt k midVal then binInsertAux lt merge add as k lo mid else do as.modifyM mid <| fun v => merge v @[specialize] def binInsertM {α : Type u} {m : Type u → Type v} [Monad m] (lt : α → α → Bool) (merge : α → m α) (add : Unit → m α) (as : Array α) (k : α) : m (Array α) := let _ := Inhabited.mk k if as.isEmpty then do let v ← add (); pure <| as.push v else if lt k (as.get! 0) then do let v ← add (); pure <| as.insertAt! 0 v else if !lt (as.get! 0) k then as.modifyM 0 <| merge else if lt as.back k then do let v ← add (); pure <| as.push v else if !lt k as.back then as.modifyM (as.size - 1) <| merge else binInsertAux lt merge add as k 0 (as.size - 1) @[inline] def binInsert {α : Type u} (lt : α → α → Bool) (as : Array α) (k : α) : Array α := Id.run <| binInsertM lt (fun _ => k) (fun _ => k) as k end Array
62977e282e10fc14e81ec0b500bd6ac8e77c926d
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0101.lean
c93204bdb5aaa5ebfb90de392c1bd7754ab10929
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
71
lean
theorem test (p q : Prop) (hp : p) (hq : q) : p ∧ q ∧ p := sorry
924a7fa9b79c7493d6cfdd51fd930b336e0da15b
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/punit_instances.lean
4774141908b37c3616d486bf34efca8c9cbb4329
[ "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,718
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.module.basic import algebra.gcd_monoid.basic import algebra.group_ring_action import group_theory.group_action.defs /-! # Instances on punit This file collects facts about algebraic structures on the one-element type, e.g. that it is a commutative ring. -/ universes u namespace punit variables {R S : Type*} (x y : punit.{u+1}) (s : set punit.{u+1}) @[to_additive] instance : comm_group punit := by refine_struct { mul := λ _ _, star, one := star, inv := λ _, star, div := λ _ _, star, npow := λ _ _, star, zpow := λ _ _, star, .. }; intros; exact subsingleton.elim _ _ @[simp, to_additive] lemma one_eq : (1 : punit) = star := rfl @[simp, to_additive] lemma mul_eq : x * y = star := rfl -- `sub_eq` simplifies `punit.sub_eq`, but the latter is eligible for `dsimp` @[simp, nolint simp_nf, to_additive] lemma div_eq : x / y = star := rfl -- `neg_eq` simplifies `punit.neg_eq`, but the latter is eligible for `dsimp` @[simp, nolint simp_nf, to_additive] lemma inv_eq : x⁻¹ = star := rfl instance : comm_ring punit := by refine { .. punit.comm_group, .. punit.add_comm_group, .. }; intros; exact subsingleton.elim _ _ instance : cancel_comm_monoid_with_zero punit := by refine { .. punit.comm_ring, .. }; intros; exact subsingleton.elim _ _ instance : normalized_gcd_monoid punit := by refine { gcd := λ _ _, star, lcm := λ _ _, star, norm_unit := λ x, 1, gcd_dvd_left := λ _ _, ⟨star, subsingleton.elim _ _⟩, gcd_dvd_right := λ _ _, ⟨star, subsingleton.elim _ _⟩, dvd_gcd := λ _ _ _ _ _, ⟨star, subsingleton.elim _ _⟩, gcd_mul_lcm := λ _ _, ⟨1, subsingleton.elim _ _⟩, .. }; intros; exact subsingleton.elim _ _ @[simp] lemma gcd_eq : gcd x y = star := rfl @[simp] lemma lcm_eq : lcm x y = star := rfl @[simp] lemma norm_unit_eq : norm_unit x = 1 := rfl instance : complete_boolean_algebra punit := by refine { le := λ _ _, true, le_antisymm := λ _ _ _ _, subsingleton.elim _ _, lt := λ _ _, false, lt_iff_le_not_le := λ _ _, iff_of_false not_false (λ H, H.2 trivial), top := star, bot := star, sup := λ _ _, star, inf := λ _ _, star, Sup := λ _, star, Inf := λ _, star, compl := λ _, star, sdiff := λ _ _, star, .. }; intros; trivial <|> simp only [eq_iff_true_of_subsingleton] @[simp] lemma top_eq : (⊤ : punit) = star := rfl @[simp] lemma bot_eq : (⊥ : punit) = star := rfl @[simp] lemma sup_eq : x ⊔ y = star := rfl @[simp] lemma inf_eq : x ⊓ y = star := rfl @[simp] lemma Sup_eq : Sup s = star := rfl @[simp] lemma Inf_eq : Inf s = star := rfl @[simp] lemma compl_eq : xᶜ = star := rfl @[simp] lemma sdiff_eq : x \ y = star := rfl @[simp] protected lemma le : x ≤ y := trivial @[simp] lemma not_lt : ¬(x < y) := not_false instance : canonically_ordered_add_monoid punit := by refine { le_iff_exists_add := λ _ _, iff_of_true _ ⟨star, subsingleton.elim _ _⟩, .. punit.comm_ring, .. punit.complete_boolean_algebra, .. }; intros; trivial instance : linear_ordered_cancel_add_comm_monoid punit := { add_left_cancel := λ _ _ _ _, subsingleton.elim _ _, le_of_add_le_add_left := λ _ _ _ _, trivial, le_total := λ _ _, or.inl trivial, decidable_le := λ _ _, decidable.true, decidable_eq := punit.decidable_eq, decidable_lt := λ _ _, decidable.false, .. punit.canonically_ordered_add_monoid } instance : has_scalar R punit := { smul := λ _ _, star } @[simp] lemma smul_eq (r : R) : r • y = star := rfl instance : is_central_scalar R punit := ⟨λ _ _, rfl⟩ instance : smul_comm_class R S punit := ⟨λ _ _ _, subsingleton.elim _ _⟩ instance [has_scalar R S] : is_scalar_tower R S punit := ⟨λ _ _ _, subsingleton.elim _ _⟩ instance [has_zero R] : smul_with_zero R punit := by refine { ..punit.has_scalar, .. }; intros; exact subsingleton.elim _ _ instance [monoid R] : mul_action R punit := by refine { ..punit.has_scalar, .. }; intros; exact subsingleton.elim _ _ instance [monoid R] : distrib_mul_action R punit := by refine { ..punit.mul_action, .. }; intros; exact subsingleton.elim _ _ instance [monoid R] : mul_distrib_mul_action R punit := by refine { ..punit.mul_action, .. }; intros; exact subsingleton.elim _ _ instance [semiring R] : mul_semiring_action R punit := { ..punit.distrib_mul_action, ..punit.mul_distrib_mul_action } instance [monoid_with_zero R] : mul_action_with_zero R punit := { .. punit.mul_action, .. punit.smul_with_zero } instance [semiring R] : module R punit := by refine { .. punit.distrib_mul_action, .. }; intros; exact subsingleton.elim _ _ end punit
4d51261d0cecd63bc4f54a9109fe1b4be572e237
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/simpStar.lean
c44dc2783deaf2e321219deb04a03e6152901741
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
494
lean
constant f (x y : Nat) : Nat constant g (x : Nat) : Nat theorem ex1 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by simp simp [*] theorem ex2 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by simp [*] axiom g_ax (x : Nat) : g x = 0 theorem ex3 (x y : Nat) (h₁ : f x x = g x) (h₂ : f x x < 5) : f x x + f x x = 0 := by simp [*] at * trace_state have aux₁ : f x x = g x := h₁ have aux₂ : g x < 5 := h₂ simp [g_ax]
8758a4687789c00f6d05cb6ce2d20dcad14b85a0
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/list/basic.lean
051098553ce504e7791a807f369dfd8a4f0f10ef
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
162,088
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import data.nat.order.basic /-! # Basic properties of lists > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open function nat (hiding one_pos) assert_not_exists set.range namespace list universes u v w x variables {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {l₁ l₂ : list α} attribute [inline] list.head /-- There is only one list of an empty type -/ instance unique_of_is_empty [is_empty α] : unique (list α) := { uniq := λ l, match l with | [] := rfl | (a :: l) := is_empty_elim a end, ..list.inhabited α } instance : is_left_id (list α) has_append.append [] := ⟨ nil_append ⟩ instance : is_right_id (list α) has_append.append [] := ⟨ append_nil ⟩ instance : is_associative (list α) has_append.append := ⟨ append_assoc ⟩ theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ []. theorem cons_ne_self (a : α) (l : list α) : a::l ≠ l := mt (congr_arg length) (nat.succ_ne_self _) theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) @[simp] theorem cons_injective {a : α} : injective (cons a) := assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe theorem cons_inj (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' := cons_injective.eq_iff theorem cons_eq_cons {a b : α} {l l' : list α} : a::l = b::l' ↔ a = b ∧ l = l' := ⟨list.cons.inj, λ h, h.1 ▸ h.2 ▸ rfl⟩ lemma singleton_injective : injective (λ a : α, [a]) := λ a b h, (cons_eq_cons.1 h).1 lemma singleton_inj {a b : α} : [a] = [b] ↔ a = b := singleton_injective.eq_iff theorem exists_cons_of_ne_nil {l : list α} (h : l ≠ nil) : ∃ b L, l = b :: L := by { induction l with c l', contradiction, use [c,l'], } lemma set_of_mem_cons (l : list α) (a : α) : {x | x ∈ a :: l} = insert a {x | x ∈ l} := rfl /-! ### mem -/ theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _ theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b := assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, this) (assume : a ∈ [], absurd this (not_mem_nil a)) @[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b := ⟨eq_of_mem_singleton, or.inl⟩ theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (assume : a = b, begin subst a, exact binl end) (assume : a ∈ l, this) theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq α] {a b : α} {l : list α} (h : a ∈ b :: l) : a = b ∨ (a ≠ b ∧ a ∈ l) := decidable.by_cases or.inl $ assume : a ≠ b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩ theorem eq_or_ne_mem_of_mem {a b : α} {l : list α} : a ∈ b :: l → a = b ∨ (a ≠ b ∧ a ∈ l) := by classical; exact decidable.list.eq_or_ne_mem_of_mem theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t := mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩ theorem ne_nil_of_mem {a : α} {l : list α} (h : a ∈ l) : l ≠ [] := by intro e; rw e at h; cases h theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t := begin induction l with b l ih, {cases h}, rcases h with rfl | h, { exact ⟨[], l, rfl⟩ }, { rcases ih h with ⟨s, t, rfl⟩, exact ⟨b::s, t, rfl⟩ } end theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r) theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l := assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2)) theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l := assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p) @[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b := begin -- This proof uses no axioms, that's why it's longer that `induction`; simp [...] induction l with a l ihl, { split, { rintro ⟨_⟩ }, { rintro ⟨a, ⟨_⟩, _⟩ } }, { refine (or_congr eq_comm ihl).trans _, split, { rintro (h|⟨c, hcl, h⟩), exacts [⟨a, or.inl rfl, h⟩, ⟨c, or.inr hcl, h⟩] }, { rintro ⟨c, (hc|hc), h⟩, exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr ⟨c, hc, h⟩] } } end alias mem_map ↔ exists_of_mem_map _ theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l := mem_map.2 ⟨a, h, rfl⟩ theorem mem_map_of_injective {f : α → β} (H : injective f) {a : α} {l : list α} : f a ∈ map f l ↔ a ∈ l := ⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩ @[simp] lemma _root_.function.involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : function.involutive f) (x : α) (l : list α) : (∃ (y : α), y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by { rintro ⟨y, h, rfl⟩, rwa hf y }, λ h, ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : involutive f) {a : α} {l : list α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] lemma forall_mem_map_iff {f : α → β} {l : list α} {P : β → Prop} : (∀ i ∈ l.map f, P i) ↔ ∀ j ∈ l, P (f j) := begin split, { assume H j hj, exact H (f j) (mem_map_of_mem f hj) }, { assume H i hi, rcases mem_map.1 hi with ⟨j, hj, ji⟩, rw ← ji, exact H j hj } end @[simp] lemma map_eq_nil {f : α → β} {l : list α} : list.map f l = [] ↔ l = [] := ⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff], λ h, h.symm ▸ rfl⟩ @[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l | [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩ | (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l := mem_join.1 theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := mem_join.2 ⟨l, lL, al⟩ @[simp] theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a := iff.trans mem_join ⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩, λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩ theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a := mem_bind.1 theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := mem_bind.2 ⟨a, al, h⟩ lemma bind_map {g : α → list β} {f : β → γ} : ∀(l : list α), list.map f (l.bind g) = l.bind (λa, (g a).map f) | [] := rfl | (a::l) := by simp only [cons_bind, map_append, bind_map l] lemma map_bind (g : β → list γ) (f : α → β) : ∀ l : list α, (list.map f l).bind g = l.bind (λ a, g (f a)) | [] := rfl | (a::l) := by simp only [cons_bind, map_cons, map_bind l] /-! ### length -/ theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] := ⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩ @[simp] lemma length_singleton (a : α) : length [a] = 1 := rfl theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l | (b::l) _ := zero_lt_succ _ theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l | (b::l) _ := ⟨b, mem_cons_self _ _⟩ theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l := ⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩ theorem ne_nil_of_length_pos {l : list α} : 0 < length l → l ≠ [] := λ h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1) theorem length_pos_of_ne_nil {l : list α} : l ≠ [] → 0 < length l := λ h, pos_iff_ne_zero.2 $ λ h0, h $ length_eq_zero.1 h0 theorem length_pos_iff_ne_nil {l : list α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ lemma exists_mem_of_ne_nil (l : list α) (h : l ≠ []) : ∃ x, x ∈ l := exists_mem_of_length_pos (length_pos_of_ne_nil h) theorem length_eq_one {l : list α} : length l = 1 ↔ ∃ a, l = [a] := ⟨match l with [a], _ := ⟨a, rfl⟩ end, λ ⟨a, e⟩, e.symm ▸ rfl⟩ lemma exists_of_length_succ {n} : ∀ l : list α, l.length = n + 1 → ∃ h t, l = h :: t | [] H := absurd H.symm $ succ_ne_zero n | (h :: t) H := ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : injective (list.length : list α → ℕ) ↔ subsingleton α := begin split, { intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl }, { intros hα l1 l2 hl, induction l1 generalizing l2; cases l2, { refl }, { cases hl }, { cases hl }, congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl } end @[simp] lemma length_injective [subsingleton α] : injective (length : list α → ℕ) := length_injective_iff.mpr $ by apply_instance lemma length_eq_two {l : list α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨match l with [a, b], _ := ⟨a, b, rfl⟩ end, λ ⟨a, b, e⟩, e.symm ▸ rfl⟩ lemma length_eq_three {l : list α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨match l with [a, b, c], _ := ⟨a, b, c, rfl⟩ end, λ ⟨a, b, c, e⟩, e.symm ▸ rfl⟩ alias length_le_of_sublist ← sublist.length_le /-! ### set-theoretic notation of lists -/ lemma empty_eq : (∅ : list α) = [] := by refl lemma singleton_eq (x : α) : ({x} : list α) = [x] := rfl lemma insert_neg [decidable_eq α] {x : α} {l : list α} (h : x ∉ l) : has_insert.insert x l = x :: l := if_neg h lemma insert_pos [decidable_eq α] {x : α} {l : list α} (h : x ∈ l) : has_insert.insert x l = l := if_pos h lemma doubleton_eq [decidable_eq α] {x y : α} (h : x ≠ y) : ({x, y} : list α) = [x, y] := by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] } /-! ### bounded quantifiers over lists -/ theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x. theorem forall_mem_cons : ∀ {p : α → Prop} {a : α} {l : list α}, (∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x := ball_cons theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem forall_mem_singleton {p : α → Prop} {a : α} : (∀ x ∈ [a], p x) ↔ p a := by simp only [mem_singleton, forall_eq] theorem forall_mem_append {p : α → Prop} {l₁ l₂ : list α} : (∀ x ∈ l₁ ++ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) := by simp only [mem_append, or_imp_distrib, forall_and_distrib] theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x. theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) : ∃ x ∈ a :: l, p x := bex.intro a (mem_cons_self _ _) h theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) : ∃ x ∈ a :: l, p x := bex.elim h (λ x xl px, bex.intro x (mem_cons_of_mem _ xl) px) theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) : p a ∨ ∃ x ∈ l, p x := bex.elim h (λ x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, left, exact px end) (assume : x ∈ l, or.inr (bex.intro x this px))) theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) /-! ### list subset -/ theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl theorem subset_append_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_left _ _ theorem subset_append_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_right _ _ @[simp] theorem cons_subset {a : α} {l m : list α} : a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m := by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] theorem cons_subset_of_subset_of_mem {a : α} {l m : list α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) @[simp] theorem append_subset_iff {l₁ l₂ l : list α} : l₁ ++ l₂ ⊆ l ↔ l₁ ⊆ l ∧ l₂ ⊆ l := begin split, { intro h, simp only [subset_def] at *, split; intros; simp* }, { rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 } end theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = [] | [] s := rfl | (a::l) s := false.elim $ s $ mem_cons_self a l theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l := show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩ theorem map_subset {l₁ l₂ : list α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ := λ x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact λ a h e, ⟨a, H h, e⟩ theorem map_subset_iff {l₁ l₂ : list α} (f : α → β) (h : injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := begin refine ⟨_, map_subset f⟩, intros h2 x hx, rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩, cases h hxx', exact hx' end /-! ### append -/ lemma append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl @[simp] lemma singleton_append {x : α} {l : list α} : [x] ++ l = x :: l := rfl theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction @[simp] lemma append_eq_nil {p q : list α} : (p ++ q) = [] ↔ p = [] ∧ q = [] := by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and] @[simp] lemma nil_eq_append_iff {a b : list α} : [] = a ++ b ↔ a = [] ∧ b = [] := by rw [eq_comm, append_eq_nil] lemma append_eq_cons_iff {a b c : list α} {x : α} : a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) := by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true, true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left'] lemma cons_eq_append_iff {a b c : list α} {x : α} : (x :: c : list α) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) := by rw [eq_comm, append_eq_cons_iff] lemma append_eq_append_iff {a b c d : list α} : a ++ b = c ++ d ↔ (∃a', c = a ++ a' ∧ b = a' ++ d) ∨ (∃c', a = c ++ c' ∧ d = c' ++ b) := begin induction a generalizing c, case nil { rw nil_append, split, { rintro rfl, left, exact ⟨_, rfl, rfl⟩ }, { rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } }, case cons : a as ih { cases c, { simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'], exact eq_comm }, { simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left, exists_and_distrib_left] } } end @[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj : ∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂ | [] [] t₁ t₂ h hl := ⟨rfl, h⟩ | (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl | [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm | (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap, let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩ theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : t₁ = t₂ := (append_inj h hl).right theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : s₁ = s₂ := (append_inj h hl).left theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ ∧ t₁ = t₂ := append_inj h $ @nat.add_right_cancel _ (length t₁) _ $ let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : t₁ = t₂ := (append_inj' h hl).right theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ := (append_inj' h hl).left theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ := append_inj_right h rfl theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ := append_inj_left' h rfl theorem append_right_injective (s : list α) : function.injective (λ t, s ++ t) := λ t₁ t₂, append_left_cancel theorem append_right_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ := (append_right_injective s).eq_iff theorem append_left_injective (t : list α) : function.injective (λ s, s ++ t) := λ s₁ s₂, append_right_cancel theorem append_left_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ := (append_left_injective t).eq_iff theorem map_eq_append_split {f : α → β} {l : list α} {s₁ s₂ : list β} (h : map f l = s₁ ++ s₂) : ∃ l₁ l₂, l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ := begin have := h, rw [← take_append_drop (length s₁) l] at this ⊢, rw map_append at this, refine ⟨_, _, rfl, append_inj this _⟩, rw [length_map, length_take, min_eq_left], rw [← length_map f l, h, length_append], apply nat.le_add_right end /-! ### repeat -/ @[simp] theorem repeat_succ (a : α) (n) : repeat a (n + 1) = a :: repeat a n := rfl theorem mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n ↔ n ≠ 0 ∧ b = a | 0 := by simp | (n + 1) := by simp [mem_repeat] theorem eq_of_mem_repeat {a b : α} {n} (h : b ∈ repeat a n) : b = a := (mem_repeat.1 h).2 theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length | [] H := rfl | (b::l) H := by cases forall_mem_cons.1 H with H₁ H₂; unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem H₂] theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩ theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩, λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩ theorem repeat_add (a : α) (m n) : repeat a (m + n) = repeat a m ++ repeat a n := by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] := λ b h, mem_singleton.2 (eq_of_mem_repeat h) lemma subset_singleton_iff {a : α} : ∀ L : list α, L ⊆ [a] ↔ ∃ n, L = repeat a n | [] := ⟨λ h, ⟨0, by simp⟩, by simp⟩ | (h :: L) := begin refine ⟨λ h, _, λ ⟨k, hk⟩, by simp [hk, repeat_subset_singleton]⟩, rw [cons_subset] at h, obtain ⟨n, rfl⟩ := (subset_singleton_iff L).mp h.2, exact ⟨n.succ, by simp [mem_singleton.mp h.1]⟩ end @[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n := by induction n; [refl, simp only [*, repeat, map]]; split; refl @[simp] theorem tail_repeat (a : α) (n) : tail (repeat a n) = repeat a n.pred := by cases n; refl @[simp] theorem join_repeat_nil (n : ℕ) : join (repeat [] n) = @nil α := by induction n; [refl, simp only [*, repeat, join, append_nil]] lemma repeat_left_injective {n : ℕ} (hn : n ≠ 0) : function.injective (λ a : α, repeat a n) := λ a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩ lemma repeat_left_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : repeat a n = repeat b n ↔ a = b := (repeat_left_injective hn).eq_iff @[simp] lemma repeat_left_inj' {a b : α} : ∀ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b | 0 := by simp | (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or] lemma repeat_right_injective (a : α) : function.injective (repeat a) := function.left_inverse.injective (length_repeat a) @[simp] lemma repeat_right_inj {a : α} {n m : ℕ} : repeat a n = repeat a m ↔ n = m := (repeat_right_injective a).eq_iff /-! ### pure -/ @[simp] theorem mem_pure {α} (x y : α) : x ∈ (pure y : list α) ↔ x = y := by simp! [pure,list.ret] /-! ### bind -/ @[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) : l >>= f = l.bind f := rfl -- TODO: duplicate of a lemma in core theorem bind_append (f : α → list β) (l₁ l₂ : list α) : (l₁ ++ l₂).bind f = l₁.bind f ++ l₂.bind f := append_bind _ _ _ @[simp] theorem bind_singleton (f : α → list β) (x : α) : [x].bind f = f x := append_nil (f x) @[simp] theorem bind_singleton' (l : list α) : l.bind (λ x, [x]) = l := bind_pure l theorem map_eq_bind {α β} (f : α → β) (l : list α) : map f l = l.bind (λ x, [f x]) := by { transitivity, rw [← bind_singleton' l, bind_map], refl } theorem bind_assoc {α β} (l : list α) (f : α → list β) (g : β → list γ) : (l.bind f).bind g = l.bind (λ x, (f x).bind g) := by induction l; simp * /-! ### concat -/ theorem concat_nil (a : α) : concat [] a = [a] := rfl theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] := by induction l; simp only [*, concat]; split; refl theorem init_eq_of_concat_eq {a : α} {l₁ l₂ : list α} : concat l₁ a = concat l₂ a → l₁ = l₂ := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact append_right_cancel h end theorem last_eq_of_concat_eq {a b : α} {l : list α} : concat l a = concat l b → a = b := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact head_eq_of_cons_eq (append_left_cancel h) end theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] := by simp theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ := by simp theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) := by simp only [concat_eq_append, length_append, length] theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a := by simp /-! ### reverse -/ @[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl local attribute [simp] reverse_core @[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] := have aux : ∀ l₁ l₂, reverse_core l₁ l₂ ++ [a] = reverse_core l₁ (l₂ ++ [a]), by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]], (aux l nil).symm theorem reverse_core_eq (l₁ l₂ : list α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ := by induction l₁ generalizing l₂; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]]; refl theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) := by induction s; [rw [nil_append, reverse_nil, append_nil], simp only [*, cons_append, reverse_cons, append_assoc]] theorem reverse_concat (l : list α) (a : α) : reverse (concat l a) = a :: reverse l := by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append] @[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l := by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl @[simp] theorem reverse_involutive : involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : bijective (@reverse α) := reverse_involutive.bijective @[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ := reverse_injective.eq_iff lemma reverse_eq_iff {l l' : list α} : l.reverse = l' ↔ l = l'.reverse := reverse_involutive.eq_iff @[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] := @reverse_inj _ l [] theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] @[simp] theorem length_reverse (l : list α) : length (reverse l) = length l := by induction l; [refl, simp only [*, reverse_cons, length_append, length]] @[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) := by induction l; [refl, simp only [*, map, reverse_cons, map_append]] theorem map_reverse_core (f : α → β) (l₁ l₂ : list α) : map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) := by simp only [reverse_core_eq, map_append, map_reverse] @[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l := by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff, not_mem_nil, false_or, or_false, or_comm]] @[simp] theorem reverse_repeat (a : α) (n) : reverse (repeat a n) = repeat a n := eq_repeat.2 ⟨by simp only [length_reverse, length_repeat], λ b h, eq_of_mem_repeat (mem_reverse.1 h)⟩ /-! ### empty -/ attribute [simp] list.empty lemma empty_iff_eq_nil {l : list α} : l.empty ↔ l = [] := list.cases_on l (by simp) (by simp) /-! ### init -/ @[simp] theorem length_init : ∀ (l : list α), length (init l) = length l - 1 | [] := rfl | [a] := rfl | (a :: b :: l) := begin rw init, simp only [add_left_inj, length, succ_add_sub_one], exact length_init (b :: l) end /-! ### last -/ @[simp] theorem last_cons {a : α} {l : list α} : ∀ (h : l ≠ nil), last (a :: l) (cons_ne_nil a l) = last l h := by {induction l; intros, contradiction, reflexivity} @[simp] theorem last_append_singleton {a : α} (l : list α) : last (l ++ [a]) (append_ne_nil_of_ne_nil_right l _ (cons_ne_nil a _)) = a := by induction l; [refl, simp only [cons_append, last_cons (λ H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]] theorem last_append (l₁ l₂ : list α) (h : l₂ ≠ []) : last (l₁ ++ l₂) (append_ne_nil_of_ne_nil_right l₁ l₂ h) = last l₂ h := begin induction l₁ with _ _ ih, { simp }, { simp only [cons_append], rw list.last_cons, exact ih }, end theorem last_concat {a : α} (l : list α) : last (concat l a) (concat_ne_nil a l) = a := by simp only [concat_eq_append, last_append_singleton] @[simp] theorem last_singleton (a : α) : last [a] (cons_ne_nil a []) = a := rfl @[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) : last (a₁::a₂::l) (cons_ne_nil _ _) = last (a₂::l) (cons_ne_nil a₂ l) := rfl theorem init_append_last : ∀ {l : list α} (h : l ≠ []), init l ++ [last l h] = l | [] h := absurd rfl h | [a] h := rfl | (a::b::l) h := begin rw [init, cons_append, last_cons (cons_ne_nil _ _)], congr, exact init_append_last (cons_ne_nil b l) end theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ theorem last_mem : ∀ {l : list α} (h : l ≠ []), last l h ∈ l | [] h := absurd rfl h | [a] h := or.inl rfl | (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) } lemma last_repeat_succ (a m : ℕ) : (repeat a m.succ).last (ne_nil_of_length_eq_succ (show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a := begin induction m with k IH, { simp }, { simpa only [repeat_succ, last] } end /-! ### last' -/ @[simp] theorem last'_is_none : ∀ {l : list α}, (last' l).is_none ↔ l = [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_none (b::l)] @[simp] theorem last'_is_some : ∀ {l : list α}, l.last'.is_some ↔ l ≠ [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_some (b::l)] theorem mem_last'_eq_last : ∀ {l : list α} {x : α}, x ∈ l.last' → ∃ h, x = last l h | [] x hx := false.elim $ by simpa using hx | [a] x hx := have a = x, by simpa using hx, this ▸ ⟨cons_ne_nil a [], rfl⟩ | (a::b::l) x hx := begin rw last' at hx, rcases mem_last'_eq_last hx with ⟨h₁, h₂⟩, use cons_ne_nil _ _, rwa [last_cons] end theorem last'_eq_last_of_ne_nil : ∀ {l : list α} (h : l ≠ []), l.last' = some (l.last h) | [] h := (h rfl).elim | [a] _ := by {unfold last, unfold last'} | (a::b::l) _ := @last'_eq_last_of_ne_nil (b::l) (cons_ne_nil _ _) theorem mem_last'_cons {x y : α} : ∀ {l : list α} (h : x ∈ l.last'), x ∈ (y :: l).last' | [] _ := by contradiction | (a::l) h := h theorem mem_of_mem_last' {l : list α} {a : α} (ha : a ∈ l.last') : a ∈ l := let ⟨h₁, h₂⟩ := mem_last'_eq_last ha in h₂.symm ▸ last_mem _ theorem init_append_last' : ∀ {l : list α} (a ∈ l.last'), init l ++ [a] = l | [] a ha := (option.not_mem_none a ha).elim | [a] _ rfl := rfl | (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] } theorem ilast_eq_last' [inhabited α] : ∀ l : list α, l.ilast = l.last'.iget | [] := by simp [ilast, arbitrary] | [a] := rfl | [a, b] := rfl | [a, b, c] := rfl | (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)] @[simp] theorem last'_append_cons : ∀ (l₁ : list α) (a : α) (l₂ : list α), last' (l₁ ++ a :: l₂) = last' (a :: l₂) | [] a l₂ := rfl | [b] a l₂ := rfl | (b::c::l₁) a l₂ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons] @[simp] theorem last'_cons_cons (x y : α) (l : list α) : last' (x :: y :: l) = last' (y :: l) := rfl theorem last'_append_of_ne_nil (l₁ : list α) : ∀ {l₂ : list α} (hl₂ : l₂ ≠ []), last' (l₁ ++ l₂) = last' l₂ | [] hl₂ := by contradiction | (b::l₂) _ := last'_append_cons l₁ b l₂ theorem last'_append {l₁ l₂ : list α} {x : α} (h : x ∈ l₂.last') : x ∈ (l₁ ++ l₂).last' := by { cases l₂, { contradiction, }, { rw list.last'_append_cons, exact h } } /-! ### head(') and tail -/ theorem head_eq_head' [inhabited α] (l : list α) : head l = (head' l).iget := by cases l; refl theorem surjective_head [inhabited α] : surjective (@head α _) := λ x, ⟨[x], rfl⟩ theorem surjective_head' : surjective (@head' α) := option.forall.2 ⟨⟨[], rfl⟩, λ x, ⟨[x], rfl⟩⟩ theorem surjective_tail : surjective (@tail α) | [] := ⟨[], rfl⟩ | (a :: l) := ⟨a :: a :: l, rfl⟩ lemma eq_cons_of_mem_head' {x : α} : ∀ {l : list α}, x ∈ l.head' → l = x::tail l | [] h := (option.not_mem_none _ h).elim | (a::l) h := by { simp only [head', option.mem_def] at h, exact h ▸ rfl } theorem mem_of_mem_head' {x : α} {l : list α} (h : x ∈ l.head') : x ∈ l := (eq_cons_of_mem_head' h).symm ▸ mem_cons_self _ _ @[simp] theorem head_cons [inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil α) = [] := rfl @[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl @[simp] theorem head_append [inhabited α] (t : list α) {s : list α} (h : s ≠ []) : head (s ++ t) = head s := by {induction s, contradiction, refl} theorem head'_append {s t : list α} {x : α} (h : x ∈ s.head') : x ∈ (s ++ t).head' := by { cases s, contradiction, exact h } theorem head'_append_of_ne_nil : ∀ (l₁ : list α) {l₂ : list α} (hl₁ : l₁ ≠ []), head' (l₁ ++ l₂) = head' l₁ | [] _ hl₁ := by contradiction | (x::l₁) _ _ := rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : list α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by { induction l, contradiction, rw [tail,cons_append,tail], } theorem cons_head'_tail : ∀ {l : list α} {a : α} (h : a ∈ head' l), a :: tail l = l | [] a h := by contradiction | (b::l) a h := by { simp at h, simp [h] } theorem head_mem_head' [inhabited α] : ∀ {l : list α} (h : l ≠ []), head l ∈ head' l | [] h := by contradiction | (a::l) h := rfl theorem cons_head_tail [inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l := cons_head'_tail (head_mem_head' h) lemma head_mem_self [inhabited α] {l : list α} (h : l ≠ nil) : l.head ∈ l := begin have h' := mem_cons_self l.head l.tail, rwa cons_head_tail h at h', end @[simp] theorem head'_map (f : α → β) (l) : head' (map f l) = (head' l).map f := by cases l; refl lemma tail_append_of_ne_nil (l l' : list α) (h : l ≠ []) : (l ++ l').tail = l.tail ++ l' := begin cases l, { contradiction }, { simp } end @[simp] lemma nth_le_tail (l : list α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := by simpa [←lt_tsub_iff_right] using h) : l.tail.nth_le i h = l.nth_le (i + 1) h' := begin cases l, { cases h, }, { simpa } end lemma nth_le_cons_aux {l : list α} {a : α} {n} (hn : n ≠ 0) (h : n < (a :: l).length) : n - 1 < l.length := begin contrapose! h, rw length_cons, convert succ_le_succ h, exact (nat.succ_pred_eq_of_pos hn.bot_lt).symm end lemma nth_le_cons {l : list α} {a : α} {n} (hl) : (a :: l).nth_le n hl = if hn : n = 0 then a else l.nth_le (n - 1) (nth_le_cons_aux hn hl) := begin split_ifs, { simp [nth_le, h] }, cases l, { rw [length_singleton, lt_succ_iff, nonpos_iff_eq_zero] at hl, contradiction }, cases n, { contradiction }, refl end @[simp] lemma modify_head_modify_head (l : list α) (f g : α → α) : (l.modify_head f).modify_head g = l.modify_head (g ∘ f) := by cases l; simp /-! ### Induction from the right -/ /-- Induction principle from the right for lists: if a property holds for the empty list, and for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ @[elab_as_eliminator] def reverse_rec_on {C : list α → Sort*} (l : list α) (H0 : C []) (H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l := begin rw ← reverse_reverse l, induction reverse l, { exact H0 }, { rw reverse_cons, exact H1 _ _ ih } end /-- Bidirectional induction principle for lists: if a property holds for the empty list, the singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def bidirectional_rec {C : list α → Sort*} (H0 : C []) (H1 : ∀ (a : α), C [a]) (Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : ∀ l, C l | [] := H0 | [a] := H1 a | (a :: b :: l) := let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in have length l' < length (a :: b :: l), by { change _ < length l + 2, simp }, begin rw ←init_append_last (cons_ne_nil b l), have : C l', from bidirectional_rec l', exact Hn a l' b' ‹C l'› end using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf list.length⟩] } /-- Like `bidirectional_rec`, but with the list parameter placed first. -/ @[elab_as_eliminator] def bidirectional_rec_on {C : list α → Sort*} (l : list α) (H0 : C []) (H1 : ∀ (a : α), C [a]) (Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : C l := bidirectional_rec H0 H1 Hn l /-! ### sublists -/ @[simp] theorem nil_sublist : Π (l : list α), [] <+ l | [] := sublist.slnil | (a :: l) := sublist.cons _ _ a (nil_sublist l) @[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l | [] := sublist.slnil | (a :: l) := sublist.cons2 _ _ a (sublist.refl l) @[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ := sublist.rec_on h₂ (λ_ s, s) (λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁)) (λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁ (λ_, nil_sublist _) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons _ _ _ (IH _ h₁) end) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons2 _ _ _ (IH _ h₁) end) rfl) l₁ h₁ @[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l := sublist.cons _ _ _ (sublist.refl l) theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ := sublist.trans (sublist_cons a l₁) theorem sublist.cons_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ := sublist.cons2 _ _ _ s @[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂ | [] l₂ := nil_sublist _ | (a::l₁) l₂ := (sublist_append_left l₁ l₂).cons_cons _ @[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂ | [] l₂ := sublist.refl _ | (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂) theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ := sublist.cons _ _ _ theorem sublist_append_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ := s.trans $ sublist_append_left _ _ theorem sublist_append_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ := s.trans $ sublist_append_right _ _ theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂ | ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s | ._ (sublist.cons2 ._ ._ a s) := s theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ := ⟨sublist_of_cons_sublist_cons, sublist.cons_cons _⟩ @[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂ | [] := iff.rfl | (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l) theorem sublist.append_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l := begin induction h with _ _ a _ ih _ _ a _ ih, { refl }, { apply sublist_cons_of_sublist a ih }, { apply ih.cons_cons a } end theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) : l <+ l₁ ++ l₂ ∨ a ∈ l := begin induction l₁ with b l₁ IH generalizing l, { cases h, { left, exact ‹l <+ l₂› }, { right, apply mem_cons_self } }, { cases h with _ _ _ h _ _ _ h, { exact or.imp_left (sublist_cons_of_sublist _) (IH h) }, { exact (IH h).imp (sublist.cons_cons _) (mem_cons_of_mem _) } } end theorem sublist.reverse {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse := begin induction h with _ _ _ _ ih _ _ a _ ih, {refl}, { rw reverse_cons, exact sublist_append_of_sublist_left ih }, { rw [reverse_cons, reverse_cons], exact ih.append_right [a] } end @[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ := ⟨λ h, l₁.reverse_reverse ▸ l₂.reverse_reverse ▸ h.reverse, sublist.reverse⟩ @[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ := ⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff] using h.reverse, λ h, h.append_right l⟩ theorem sublist.append {l₁ l₂ r₁ r₂ : list α} (hl : l₁ <+ l₂) (hr : r₁ <+ r₂) : l₁ ++ r₁ <+ l₂ ++ r₂ := (hl.append_right _).trans ((append_sublist_append_left _).2 hr) theorem sublist.subset : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂ | ._ ._ sublist.slnil b h := h | ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (sublist.subset s h) | ._ ._ (sublist.cons2 l₁ l₂ a s) b h := match eq_or_mem_of_mem_cons h with | or.inl h := h ▸ mem_cons_self _ _ | or.inr h := mem_cons_of_mem _ (sublist.subset s h) end @[simp] theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l := ⟨λ h, h.subset (mem_singleton_self _), λ h, let ⟨s, t, e⟩ := mem_split h in e.symm ▸ ((nil_sublist _).cons_cons _ ).trans (sublist_append_right _ _)⟩ theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] := eq_nil_of_subset_nil $ s.subset @[simp] theorem sublist_nil_iff_eq_nil {l : list α} : l <+ [] ↔ l = [] := ⟨eq_nil_of_sublist_nil, λ H, H ▸ sublist.refl _⟩ @[simp] theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n := ⟨λ h, by simpa only [length_repeat] using h.length_le, λ h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩ lemma sublist_repeat_iff {l : list α} {a : α} {n : ℕ} : l <+ repeat a n ↔ ∃ (k ≤ n), l = repeat a k := ⟨λ h, ⟨l.length, h.length_le.trans (length_repeat _ _).le, eq_repeat.mpr ⟨rfl, λ b hb, list.eq_of_mem_repeat (h.subset hb)⟩⟩, by { rintro ⟨k, h, rfl⟩, exact (repeat_sublist_repeat _).mpr h }⟩ theorem sublist.eq_of_length : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂ | ._ ._ sublist.slnil h := rfl | ._ ._ (sublist.cons l₁ l₂ a s) h := by cases s.length_le.not_lt (by rw h; apply lt_succ_self) | ._ ._ (sublist.cons2 l₁ l₂ a s) h := by rw [length, length] at h; injection h with h; rw s.eq_of_length h theorem sublist.eq_of_length_le (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) : l₁ = l₂ := s.eq_of_length $ s.length_le.antisymm h lemma sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂) | [] l₂ := is_true $ nil_sublist _ | (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h | (a::l₁) (b::l₂) := if h : a = b then decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $ by rw [← h]; exact ⟨sublist.cons_cons _, sublist_of_cons_sublist_cons⟩ else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂) ⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with | a, l₁, sublist.cons ._ ._ ._ s', h := s' | ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h end⟩ /-! ### index_of -/ section index_of variable [decidable_eq α] @[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl theorem index_of_cons (a b : α) (l : list α) : index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 := assume e, if_pos e @[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 := index_of_cons_eq _ rfl @[simp, priority 990] theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) := assume n, if_neg n theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l := begin induction l with b l ih, { exact iff_of_true rfl (not_mem_nil _) }, simp only [length, mem_cons_iff, index_of_cons], split_ifs, { exact iff_of_false (by rintro ⟨⟩) (λ H, H $ or.inl h) }, { simp only [h, false_or], rw ← ih, exact succ_inj' } end @[simp, priority 980] theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l := index_of_eq_length.2 theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l := begin induction l with b l ih, {refl}, simp only [length, index_of_cons], by_cases h : a = b, {rw if_pos h, exact nat.zero_le _}, rw if_neg h, exact succ_le_succ ih end theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l := ⟨λh, decidable.by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al, λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩ theorem index_of_append_of_mem {a : α} (h : a ∈ l₁) : index_of a (l₁ ++ l₂) = index_of a l₁ := begin induction l₁ with d₁ t₁ ih, { exfalso, exact not_mem_nil a h }, rw list.cons_append, by_cases hh : a = d₁, { iterate 2 { rw index_of_cons_eq _ hh } }, rw [index_of_cons_ne _ hh, index_of_cons_ne _ hh, ih (mem_of_ne_of_mem hh h)], end theorem index_of_append_of_not_mem {a : α} (h : a ∉ l₁) : index_of a (l₁ ++ l₂) = l₁.length + index_of a l₂ := begin induction l₁ with d₁ t₁ ih, { rw [list.nil_append, list.length, zero_add] }, rw [list.cons_append, index_of_cons_ne _ (ne_of_not_mem_cons h), list.length, ih (not_mem_of_not_mem_cons h), nat.succ_add], end end index_of /-! ### nth element -/ theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a | a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩ | a (b :: l) (or.inr m) := let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩ theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h) | (a :: l) 0 h := rfl | (a :: l) (n+1) h := @nth_le_nth l n _ theorem nth_len_le : ∀ {l : list α} {n}, length l ≤ n → nth l n = none | [] n h := rfl | (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h) @[simp] theorem nth_length (l : list α) : l.nth l.length = none := nth_len_le le_rfl theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a := ⟨λ e, have h : n < length l, from lt_of_not_ge $ λ hn, by rw nth_len_le hn at e; contradiction, ⟨h, by rw nth_le_nth h at e; injection e with e; apply nth_le_mem⟩, λ ⟨h, e⟩, e ▸ nth_le_nth _⟩ @[simp] theorem nth_eq_none_iff : ∀ {l : list α} {n}, nth l n = none ↔ length l ≤ n := begin intros, split, { intro h, by_contradiction h', have h₂ : ∃ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩, rw [← nth_eq_some, h] at h₂, cases h₂ }, { solve_by_elim [nth_len_le] }, end theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a := let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩ theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l | (a :: l) 0 h := mem_cons_self _ _ | (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _) theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l := let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _ theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a := ⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩ theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a := mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm lemma nth_zero (l : list α) : l.nth 0 = l.head' := by cases l; refl lemma nth_injective {α : Type u} {xs : list α} {i j : ℕ} (h₀ : i < xs.length) (h₁ : nodup xs) (h₂ : xs.nth i = xs.nth j) : i = j := begin induction xs with x xs generalizing i j, { cases h₀ }, { cases i; cases j, case nat.zero nat.zero { refl }, case nat.succ nat.succ { congr, cases h₁, apply xs_ih; solve_by_elim [lt_of_succ_lt_succ] }, iterate 2 { dsimp at h₂, cases h₁ with _ _ h h', cases h x _ rfl, rw mem_iff_nth, exact ⟨_, h₂.symm⟩ <|> exact ⟨_, h₂⟩ } }, end @[simp] theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f | [] n := rfl | (a :: l) 0 := rfl | (a :: l) (n+1) := nth_map l n theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) := option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl /-- A version of `nth_le_map` that can be used for rewriting. -/ theorem nth_le_map_rev (f : α → β) {l n} (H) : f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm ▸ H) := (nth_le_map f _ _).symm @[simp] theorem nth_le_map' (f : α → β) {l n} (H) : nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) := nth_le_map f _ _ /-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as `hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make such a rewrite, with `rw (nth_le_of_eq h)`. -/ lemma nth_le_of_eq {L L' : list α} (h : L = L') {i : ℕ} (hi : i < L.length) : nth_le L i hi = nth_le L' i (h ▸ hi) := by { congr, exact h} @[simp] lemma nth_le_singleton (a : α) {n : ℕ} (hn : n < 1) : nth_le [a] n hn = a := have hn0 : n = 0 := nat.eq_zero_of_le_zero (le_of_lt_succ hn), by subst hn0; refl lemma nth_le_zero [inhabited α] {L : list α} (h : 0 < L.length) : L.nth_le 0 h = L.head := by { cases L, cases h, simp, } lemma nth_le_append : ∀ {l₁ l₂ : list α} {n : ℕ} (hn₁) (hn₂), (l₁ ++ l₂).nth_le n hn₁ = l₁.nth_le n hn₂ | [] _ n hn₁ hn₂ := (nat.not_lt_zero _ hn₂).elim | (a::l) _ 0 hn₁ hn₂ := rfl | (a::l) _ (n+1) hn₁ hn₂ := by simp only [nth_le, cons_append]; exact nth_le_append _ _ lemma nth_le_append_right_aux {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.length ≤ n) (h₂ : n < (l₁ ++ l₂).length) : n - l₁.length < l₂.length := begin rw list.length_append at h₂, apply lt_of_add_lt_add_right, rwa [nat.sub_add_cancel h₁, nat.add_comm], end lemma nth_le_append_right : ∀ {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.length ≤ n) (h₂), (l₁ ++ l₂).nth_le n h₂ = l₂.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ h₂) | [] _ n h₁ h₂ := rfl | (a :: l) _ (n+1) h₁ h₂ := begin dsimp, conv { to_rhs, congr, skip, rw [nat.add_sub_add_right], }, rw nth_le_append_right (nat.lt_succ_iff.mp h₁), end @[simp] lemma nth_le_repeat (a : α) {n m : ℕ} (h : m < (list.repeat a n).length) : (list.repeat a n).nth_le m h = a := eq_of_mem_repeat (nth_le_mem _ _ _) lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) : (l₁ ++ l₂).nth n = l₁.nth n := have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn (by rw length_append; exact nat.le_add_right _ _), by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append] lemma nth_append_right {l₁ l₂ : list α} {n : ℕ} (hn : l₁.length ≤ n) : (l₁ ++ l₂).nth n = l₂.nth (n - l₁.length) := begin by_cases hl : n < (l₁ ++ l₂).length, { rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] }, { rw [nth_len_le (le_of_not_lt hl), nth_len_le], rw [not_lt, length_append] at hl, exact le_tsub_of_add_le_left hl } end lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []), last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos) | [] h := rfl | [a] h := by rw [last_singleton, nth_le_singleton] | (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)], refl, exact cons_ne_nil b l } lemma nth_le_length_sub_one {l : list α} (h : l.length - 1 < l.length) : l.nth_le (l.length - 1) h = l.last (by { rintro rfl, exact nat.lt_irrefl 0 h }) := (last_eq_nth_le l _).symm @[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a | [] a := rfl | (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length] lemma nth_le_cons_length (x : α) (xs : list α) (n : ℕ) (h : n = xs.length) : (x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) := begin rw last_eq_nth_le, congr, simp [h] end lemma take_one_drop_eq_of_lt_length {l : list α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.nth_le n h] := begin induction l with x l ih generalizing n, { cases h }, { by_cases h₁ : l = [], { subst h₁, rw nth_le_singleton, simp [lt_succ_iff] at h, subst h, simp }, have h₂ := h, rw [length_cons, nat.lt_succ_iff, le_iff_eq_or_lt] at h₂, cases n, { simp }, rw [drop, nth_le], apply ih }, end @[ext] theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂ | [] [] h := rfl | (a::l₁) [] h := by have h0 := h 0; contradiction | [] (a'::l₂) h := by have h0 := h 0; contradiction | (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa; simp only [aa, ext (λn, h (n+1))]; split; refl theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂) (h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ := ext $ λn, if h₁ : n < length l₁ then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])] else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], } @[simp] theorem index_of_nth_le [decidable_eq α] {a : α} : ∀ {l : list α} h, nth_le l (index_of a l) h = a | (b::l) h := by by_cases h' : a = b; simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l] @[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) : nth l (index_of a l) = some a := by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)] theorem nth_le_reverse_aux1 : ∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 | [] r i := λh1 h2, rfl | (a :: l) r i := by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1); exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2) lemma index_of_inj [decidable_eq α] {l : list α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y := ⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) = nth_le l (index_of y l) (index_of_lt_length.2 hy), by simp only [h], by simpa only [index_of_nth_le], λ h, by subst h⟩ theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2), nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 | [] r i h1 h2 := absurd h2 (nat.not_lt_zero _) | (a :: l) r 0 h1 h2 := begin have aux := nth_le_reverse_aux1 l (a :: r) 0, rw zero_add at aux, exact aux _ (zero_lt_succ _) end | (a :: l) r (i+1) h1 h2 := begin have aux := nth_le_reverse_aux2 l (a :: r) i, have heq := calc length (a :: l) - 1 - (i + 1) = length l - (1 + i) : by rw add_comm; refl ... = length l - 1 - i : by rw ← tsub_add_eq_tsub_tsub, rw [← heq] at aux, apply aux end @[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 _ _ _ _ _ lemma nth_le_reverse' (l : list α) (n : ℕ) (hn : n < l.reverse.length) (hn') : l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' := begin rw eq_comm, convert nth_le_reverse l.reverse _ _ _ using 1, { simp }, { simpa } end lemma eq_cons_of_length_one {l : list α} (h : l.length = 1) : l = [l.nth_le 0 (h.symm ▸ zero_lt_one)] := begin refine ext_le (by convert h) (λ n h₁ h₂, _), simp only [nth_le_singleton], congr, exact eq_bot_iff.mpr (nat.lt_succ_iff.mp h₂) end lemma nth_le_eq_iff {l : list α} {n : ℕ} {x : α} {h} : l.nth_le n h = x ↔ l.nth n = some x := by { rw nth_eq_some, tauto } lemma some_nth_le_eq {l : list α} {n : ℕ} {h} : some (l.nth_le n h) = l.nth n := by { symmetry, rw nth_eq_some, tauto } lemma modify_nth_tail_modify_nth_tail {f g : list α → list α} (m : ℕ) : ∀n (l:list α), (l.modify_nth_tail f n).modify_nth_tail g (m + n) = l.modify_nth_tail (λl, (f l).modify_nth_tail g m) n | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l) lemma modify_nth_tail_modify_nth_tail_le {f g : list α → list α} (m n : ℕ) (l : list α) (h : n ≤ m) : (l.modify_nth_tail f n).modify_nth_tail g m = l.modify_nth_tail (λl, (f l).modify_nth_tail g (m - n)) n := begin rcases exists_add_of_le h with ⟨m, rfl⟩, rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail] end lemma modify_nth_tail_modify_nth_tail_same {f g : list α → list α} (n : ℕ) (l:list α) : (l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n := by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl lemma modify_nth_tail_id : ∀n (l:list α), l.modify_nth_tail id n = l | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l) theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _) theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α), update_nth l n a = modify_nth (λ _, a) n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _) theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α), modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := (congr_arg (cons b) (modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m, nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m | n l 0 := by cases l; cases n; refl | n [] (m+1) := by cases n; refl | 0 (a::l) (m+1) := by cases nth l m; refl | (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $ by cases nth l m with b; by_cases n = m; simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj, not_false_iff] theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) : ∀ n l, length (modify_nth_tail f n l) = length l | 0 l := H _ | (n+1) [] := rfl | (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _) @[simp] theorem modify_nth_length (f : α → α) : ∀ n l, length (modify_nth f n l) = length l := modify_nth_tail_length _ (λ l, by cases l; refl) @[simp] theorem update_nth_length (l : list α) (n) (a : α) : length (update_nth l n a) = length l := by simp only [update_nth_eq_modify_nth, modify_nth_length] @[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) : nth (modify_nth f n l) n = f <$> nth l n := by simp only [nth_modify_nth, if_pos] @[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) : nth (modify_nth f m l) n = nth l n := by simp only [nth_modify_nth, if_neg h, id_map'] theorem nth_update_nth_eq (a : α) (n) (l : list α) : nth (update_nth l n a) n = (λ _, a) <$> nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq] theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) : nth (update_nth l n a) n = some a := by rw [nth_update_nth_eq, nth_le_nth h]; refl theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) : nth (update_nth l m a) n = nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h] @[simp] lemma update_nth_nil (n : ℕ) (a : α) : [].update_nth n a = [] := rfl @[simp] lemma update_nth_succ (x : α) (xs : list α) (n : ℕ) (a : α) : (x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl lemma update_nth_comm (a b : α) : Π {n m : ℕ} (l : list α) (h : n ≠ m), (l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a | _ _ [] _ := by simp | 0 0 (x :: t) h := absurd rfl h | (n + 1) 0 (x :: t) h := by simp [list.update_nth] | 0 (m + 1) (x :: t) h := by simp [list.update_nth] | (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true], exact update_nth_comm t (λ h', h $ nat.succ_inj'.mpr h'), } @[simp] lemma nth_le_update_nth_eq (l : list α) (i : ℕ) (a : α) (h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a := by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at * @[simp] lemma nth_le_update_nth_of_ne {l : list α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.update_nth i a).length) : (l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) := by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth] lemma mem_or_eq_of_mem_update_nth : ∀ {l : list α} {n : ℕ} {a b : α} (h : a ∈ l.update_nth n b), a ∈ l ∨ a = b | [] n a b h := false.elim h | (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim or.inr (or.inl ∘ mem_cons_of_mem _) | (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim (λ h, h ▸ or.inl (mem_cons_self _ _)) (λ h, (mem_or_eq_of_mem_update_nth h).elim (or.inl ∘ mem_cons_of_mem _) or.inr) section insert_nth variable {a : α} @[simp] lemma insert_nth_zero (s : list α) (x : α) : insert_nth 0 x s = x :: s := rfl @[simp] lemma insert_nth_succ_nil (n : ℕ) (a : α) : insert_nth (n + 1) a [] = [] := rfl @[simp] lemma insert_nth_succ_cons (s : list α) (hd x : α) (n : ℕ) : insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl lemma length_insert_nth : ∀n as, n ≤ length as → length (insert_nth n a as) = length as + 1 | 0 as h := rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h) lemma remove_nth_insert_nth (n:ℕ) (l : list α) : (l.insert_nth n a).remove_nth n = l := by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same]; from modify_nth_tail_id _ _ lemma insert_nth_remove_nth_of_ge : ∀n m as, n < length as → n ≤ m → insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n | 0 0 [] has _ := (lt_irrefl _ has).elim | 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth] | 0 (m+1) (a::as) has hmn := rfl | (n+1) (m+1) (a::as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_remove_nth_of_le : ∀n m as, n < length as → m ≤ n → insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1) | n 0 (a :: as) has hmn := rfl | (n + 1) (m + 1) (a :: as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_comm (a b : α) : ∀(i j : ℕ) (l : list α) (h : i ≤ j) (hj : j ≤ length l), (l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a | 0 j l := by simp [insert_nth] | (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim | (i + 1) (j+1) [] := by simp | (i + 1) (j+1) (c::l) := assume h₀ h₁, by simp [insert_nth]; exact insert_nth_comm i j l (nat.le_of_succ_le_succ h₀) (nat.le_of_succ_le_succ h₁) lemma mem_insert_nth {a b : α} : ∀ {n : ℕ} {l : list α} (hi : n ≤ l.length), a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l | 0 as h := iff.rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := begin dsimp [list.insert_nth], erw [mem_insert_nth (nat.le_of_succ_le_succ h), ← or.assoc, or_comm (a = a'), or.assoc] end lemma insert_nth_of_length_lt (l : list α) (x : α) (n : ℕ) (h : l.length < n) : insert_nth n x l = l := begin induction l with hd tl IH generalizing n, { cases n, { simpa using h }, { simp } }, { cases n, { simpa using h }, { simp only [nat.succ_lt_succ_iff, length] at h, simpa using IH _ h } } end @[simp] lemma insert_nth_length_self (l : list α) (x : α) : insert_nth l.length x l = l ++ [x] := begin induction l with hd tl IH, { simp }, { simpa using IH } end lemma length_le_length_insert_nth (l : list α) (x : α) (n : ℕ) : l.length ≤ (insert_nth n x l).length := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn, exact (nat.lt_succ_self _).le }, { rw insert_nth_of_length_lt _ _ _ hn } end lemma length_insert_nth_le_succ (l : list α) (x : α) (n : ℕ) : (insert_nth n x l).length ≤ l.length + 1 := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn }, { rw insert_nth_of_length_lt _ _ _ hn, exact (nat.lt_succ_self _).le } end lemma nth_le_insert_nth_of_lt (l : list α) (x : α) (n k : ℕ) (hn : k < n) (hk : k < l.length) (hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)): (insert_nth n x l).nth_le k hk' = l.nth_le k hk := begin induction n with n IH generalizing k l, { simpa using hn }, { cases l with hd tl, { simp }, { cases k, { simp }, { rw nat.succ_lt_succ_iff at hn, simpa using IH _ _ hn _ } } } end @[simp] lemma nth_le_insert_nth_self (l : list α) (x : α) (n : ℕ) (hn : n ≤ l.length) (hn' : n < (insert_nth n x l).length := by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) : (insert_nth n x l).nth_le n hn' = x := begin induction l with hd tl IH generalizing n, { simp only [length, nonpos_iff_eq_zero] at hn, simp [hn] }, { cases n, { simp }, { simp only [nat.succ_le_succ_iff, length] at hn, simpa using IH _ hn } } end lemma nth_le_insert_nth_add_succ (l : list α) (x : α) (n k : ℕ) (hk' : n + k < l.length) (hk : n + k + 1 < (insert_nth n x l).length := by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) : (insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' := begin induction l with hd tl IH generalizing n k, { simpa using hk' }, { cases n, { simpa }, { simpa [succ_add] using IH _ _ _ } } end lemma insert_nth_injective (n : ℕ) (x : α) : function.injective (insert_nth n x) := begin induction n with n IH, { have : insert_nth 0 x = cons x := funext (λ _, rfl), simp [this] }, { rintros (_|⟨a, as⟩) (_|⟨b, bs⟩) h; simpa [IH.eq_iff] using h <|> refl } end end insert_nth /-! ### map -/ @[simp] lemma map_nil (f : α → β) : map f [] = [] := rfl theorem map_eq_foldr (f : α → β) (l : list α) : map f l = foldr (λ a bs, f a :: bs) [] l := by induction l; simp * lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l | [] _ := rfl | (a::l) h := let ⟨h₁, h₂⟩ := forall_mem_cons.1 h in by rw [map, map, h₁, map_congr h₂] lemma map_eq_map_iff {f g : α → β} {l : list α} : map f l = map g l ↔ (∀ x ∈ l, f x = g x) := begin refine ⟨_, map_congr⟩, intros h x hx, rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩, rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h end theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) := by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl @[simp] theorem map_id'' (l : list α) : map (λ x, x) l = l := map_id _ theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l := by simp [show f = id, from funext h] theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil := eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl @[simp] theorem map_join (f : α → β) (L : list (list α)) : map f (join L) = join (map (map f) L) := by induction L; [refl, simp only [*, join, map, map_append]] theorem bind_ret_eq_map (f : α → β) (l : list α) : l.bind (list.ret ∘ f) = map f l := by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *]; split; refl lemma bind_congr {l : list α} {f g : α → list β} (h : ∀ x ∈ l, f x = g x) : list.bind l f = list.bind l g := (congr_arg list.join $ map_congr h : _) @[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl @[simp] theorem map_tail (f : α → β) (l) : map f (tail l) = tail (map f l) := by cases l; refl @[simp] theorem map_injective_iff {f : α → β} : injective (map f) ↔ injective f := begin split; intros h x y hxy, { suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] }, { induction y generalizing x, simpa using hxy, cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] } end /-- A single `list.map` of a composition of functions is equal to composing a `list.map` with another `list.map`, fully applied. This is the reverse direction of `list.map_map`. -/ lemma comp_map (h : β → γ) (g : α → β) (l : list α) : map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm /-- Composing a `list.map` with another `list.map` is equal to a single `list.map` of composed functions. -/ @[simp] lemma map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by { ext l, rw comp_map } theorem map_filter_eq_foldr (f : α → β) (p : α → Prop) [decidable_pred p] (as : list α) : map f (filter p as) = foldr (λ a bs, if p a then f a :: bs else bs) [] as := by { induction as, { refl }, { simp! [*, apply_ite (map f)] } } lemma last_map (f : α → β) {l : list α} (hl : l ≠ []) : (l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) := begin induction l with l_hd l_tl l_ih, { apply (hl rfl).elim }, { cases l_tl, { simp }, { simpa using l_ih } } end lemma map_eq_repeat_iff {l : list α} {f : α → β} {b : β} : l.map f = repeat b l.length ↔ (∀ x ∈ l, f x = b) := begin induction l with x l' ih, { simp only [repeat, length, not_mem_nil, is_empty.forall_iff, implies_true_iff, map_nil, eq_self_iff_true], }, { simp only [map, length, mem_cons_iff, forall_eq_or_imp, repeat_succ, and.congr_right_iff], exact λ _, ih, } end @[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length := map_eq_repeat_iff.mpr (λ x _, rfl) theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := by rw map_const at h; exact eq_of_mem_repeat h /-! ### map₂ -/ theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] := by cases l; refl theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] := by cases l; refl @[simp] theorem map₂_flip (f : α → β → γ) : ∀ as bs, map₂ (flip f) bs as = map₂ f as bs | [] [] := rfl | [] (b :: bs) := rfl | (a :: as) [] := rfl | (a :: as) (b :: bs) := by { simp! [map₂_flip], refl } /-! ### take, drop -/ @[simp] theorem take_zero (l : list α) : take 0 l = [] := rfl @[simp] theorem take_nil : ∀ n, take n [] = ([] : list α) | 0 := rfl | (n+1) := rfl theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl @[simp] theorem take_length : ∀ (l : list α), take (length l) l = l | [] := rfl | (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end theorem take_all_of_le : ∀ {n} {l : list α}, length l ≤ n → take n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: take n l = a :: l, rw [take_all_of_le (le_of_succ_le_succ h)] end @[simp] theorem take_left : ∀ l₁ l₂ : list α, take (length l₁) (l₁ ++ l₂) = l₁ | [] l₂ := rfl | (a::l₁) l₂ := congr_arg (cons a) (take_left l₁ l₂) theorem take_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : take n (l₁ ++ l₂) = l₁ := by rw ← h; apply take_left theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l | n 0 l := by rw [min_zero, take_zero, take_nil] | 0 m l := by rw [zero_min, take_zero, take_zero] | (succ n) (succ m) nil := by simp only [take_nil] | (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl theorem take_repeat (a : α) : ∀ (n m : ℕ), take n (repeat a m) = repeat a (min n m) | n 0 := by simp | 0 m := by simp | (succ n) (succ m) := by simp [min_succ_succ, take_repeat] lemma map_take {α β : Type*} (f : α → β) : ∀ (L : list α) (i : ℕ), (L.take i).map f = (L.map f).take i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_take], } /-- Taking the first `n` elements in `l₁ ++ l₂` is the same as appending the first `n` elements of `l₁` to the first `n - l₁.length` elements of `l₂`. -/ lemma take_append_eq_append_take {l₁ l₂ : list α} {n : ℕ} : take n (l₁ ++ l₂) = take n l₁ ++ take (n - l₁.length) l₂ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma take_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) : (l₁ ++ l₂).take n = l₁.take n := by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h] /-- Taking the first `l₁.length + i` elements in `l₁ ++ l₂` is the same as appending the first `i` elements of `l₂` to `l₁`. -/ lemma take_append {l₁ l₂ : list α} (i : ℕ) : take (l₁.length + i) (l₁ ++ l₂) = l₁ ++ (take i l₂) := by simp [take_append_eq_append_take, take_all_of_le le_self_add] /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_take (L : list α) {i j : ℕ} (hi : i < L.length) (hj : i < j) : nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) := by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ } /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_take' (L : list α) {i j : ℕ} (hi : i < (L.take j).length) : nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) := by { simp at hi, rw nth_le_take L _ hi.1 } lemma nth_take {l : list α} {n m : ℕ} (h : m < n) : (l.take n).nth m = l.nth m := begin induction n with n hn generalizing l m, { simp only [nat.nat_zero_eq_zero] at h, exact absurd h (not_lt_of_le m.zero_le) }, { cases l with hd tl, { simp only [take_nil] }, { cases m, { simp only [nth, take] }, { simpa only using hn (nat.lt_of_succ_lt_succ h) } } }, end @[simp] lemma nth_take_of_succ {l : list α} {n : ℕ} : (l.take (n + 1)).nth n = l.nth n := nth_take (nat.lt_succ_self n) lemma take_succ {l : list α} {n : ℕ} : l.take (n + 1) = l.take n ++ (l.nth n).to_list := begin induction l with hd tl hl generalizing n, { simp only [option.to_list, nth, take_nil, append_nil]}, { cases n, { simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] }, { simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } } end @[simp] lemma take_eq_nil_iff {l : list α} {k : ℕ} : l.take k = [] ↔ l = [] ∨ k = 0 := by { cases l; cases k; simp [nat.succ_ne_zero] } lemma take_eq_take : ∀ {l : list α} {m n : ℕ}, l.take m = l.take n ↔ min m l.length = min n l.length | [] m n := by simp | (x :: xs) 0 0 := by simp | (x :: xs) (m + 1) 0 := by simp | (x :: xs) 0 (n + 1) := by simp [@eq_comm ℕ 0] | (x :: xs) (m + 1) (n + 1) := by simp [nat.min_succ_succ, take_eq_take] lemma take_add (l : list α) (m n : ℕ) : l.take (m + n) = l.take m ++ (l.drop m).take n := begin convert_to take (m + n) (take m l ++ drop m l) = take m l ++ take n (drop m l), { rw take_append_drop }, rw [take_append_eq_append_take, take_all_of_le, append_right_inj], swap, { transitivity m, { apply length_take_le }, { simp }}, simp only [take_eq_take, length_take, length_drop], generalize : l.length = k, by_cases h : m ≤ k, { simp [min_eq_left_iff.mpr h] }, { push_neg at h, simp [nat.sub_eq_zero_of_le (le_of_lt h)] }, end lemma init_eq_take (l : list α) : l.init = l.take l.length.pred := begin cases l with x l, { simp [init] }, { induction l with hd tl hl generalizing x, { simp [init], }, { simp [init, hl] } } end lemma init_take {n : ℕ} {l : list α} (h : n < l.length) : (l.take n).init = l.take n.pred := by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le] @[simp] lemma init_cons_of_ne_nil {α : Type*} {x : α} : ∀ {l : list α} (h : l ≠ []), (x :: l).init = x :: l.init | [] h := false.elim (h rfl) | (a :: l) _ := by simp [init] @[simp] lemma init_append_of_ne_nil {α : Type*} {l : list α} : ∀ (l' : list α) (h : l ≠ []), (l' ++ l).init = l' ++ l.init | [] _ := by simp only [nil_append] | (a :: l') h := by simp [append_ne_nil_of_ne_nil_right l' l h, init_append_of_ne_nil l' h] @[simp] lemma drop_eq_nil_of_le {l : list α} {k : ℕ} (h : l.length ≤ k) : l.drop k = [] := by simpa [←length_eq_zero] using tsub_eq_zero_iff_le.mpr h lemma drop_eq_nil_iff_le {l : list α} {k : ℕ} : l.drop k = [] ↔ l.length ≤ k := begin refine ⟨λ h, _, drop_eq_nil_of_le⟩, induction k with k hk generalizing l, { simp only [drop] at h, simp [h] }, { cases l, { simp }, { simp only [drop] at h, simpa [nat.succ_le_succ_iff] using hk h } } end lemma tail_drop (l : list α) (n : ℕ) : (l.drop n).tail = l.drop (n + 1) := begin induction l with hd tl hl generalizing n, { simp }, { cases n, { simp }, { simp [hl] } } end lemma cons_nth_le_drop_succ {l : list α} {n : ℕ} (hn : n < l.length) : l.nth_le n hn :: l.drop (n + 1) = l.drop n := begin induction l with hd tl hl generalizing n, { exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) }, { cases n, { simp }, { simp only [nat.succ_lt_succ_iff, list.length] at hn, simpa [list.nth_le, list.drop] using hl hn } } end theorem drop_nil : ∀ n, drop n [] = ([] : list α) := λ _, drop_eq_nil_of_le (nat.zero_le _) @[simp] theorem drop_one : ∀ l : list α, drop 1 l = tail l | [] := rfl | (a :: l) := rfl theorem drop_add : ∀ m n (l : list α), drop (m + n) l = drop m (drop n l) | m 0 l := rfl | m (n+1) [] := (drop_nil _).symm | m (n+1) (a::l) := drop_add m n _ @[simp] theorem drop_left : ∀ l₁ l₂ : list α, drop (length l₁) (l₁ ++ l₂) = l₂ | [] l₂ := rfl | (a::l₁) l₂ := drop_left l₁ l₂ theorem drop_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : drop n (l₁ ++ l₂) = l₂ := by rw ← h; apply drop_left theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h, drop n l = nth_le l n h :: drop (n+1) l | 0 (a::l) h := rfl | (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _ @[simp] lemma drop_length (l : list α) : l.drop l.length = [] := calc l.drop l.length = (l ++ []).drop l.length : by simp ... = [] : drop_left _ _ lemma drop_length_cons {l : list α} (h : l ≠ []) (a : α) : (a :: l).drop l.length = [l.last h] := begin induction l with y l ih generalizing a, { cases h rfl }, { simp only [drop, length], by_cases h₁ : l = [], { simp [h₁] }, rw last_cons h₁, exact ih h₁ y }, end /-- Dropping the elements up to `n` in `l₁ ++ l₂` is the same as dropping the elements up to `n` in `l₁`, dropping the elements up to `n - l₁.length` in `l₂`, and appending them. -/ lemma drop_append_eq_append_drop {l₁ l₂ : list α} {n : ℕ} : drop n (l₁ ++ l₂) = drop n l₁ ++ drop (n - l₁.length) l₂ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma drop_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) : (l₁ ++ l₂).drop n = l₁.drop n ++ l₂ := by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h] /-- Dropping the elements up to `l₁.length + i` in `l₁ + l₂` is the same as dropping the elements up to `i` in `l₂`. -/ lemma drop_append {l₁ l₂ : list α} (i : ℕ) : drop (l₁.length + i) (l₁ ++ l₂) = drop i l₂ := by simp [drop_append_eq_append_drop, take_all_of_le le_self_add] lemma drop_sizeof_le [has_sizeof α] (l : list α) : ∀ (n : ℕ), (l.drop n).sizeof ≤ l.sizeof := begin induction l with _ _ lih; intro n, { rw [drop_nil] }, { induction n with n nih, { refl, }, { exact trans (lih _) le_add_self } } end /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_drop (L : list α) {i j : ℕ} (h : i + j < L.length) : nth_le L (i + j) h = nth_le (L.drop i) j begin have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h, rw (take_append_drop i L).symm at h, simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h end := begin have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)], rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right]; simp [A] end /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_drop' (L : list α) {i j : ℕ} (h : j < (L.drop i).length) : nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) ▸ h)) := by rw nth_le_drop lemma nth_drop (L : list α) (i j : ℕ) : nth (L.drop i) j = nth L (i + j) := begin ext, simp only [nth_eq_some, nth_le_drop', option.mem_def], split; exact λ ⟨h, ha⟩, ⟨by simpa [lt_tsub_iff_left] using h, ha⟩ end @[simp] theorem drop_drop (n : ℕ) : ∀ (m) (l : list α), drop n (drop m l) = drop (n + m) l | m [] := by simp | 0 l := by simp | (m+1) (a::l) := calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl ... = drop (n + m) l : drop_drop m l ... = drop (n + (m + 1)) (a :: l) : rfl theorem drop_take : ∀ (m : ℕ) (n : ℕ) (l : list α), drop m (take (m + n) l) = take n (drop m l) | 0 n _ := by simp | (m+1) n nil := by simp | (m+1) n (_::l) := have h: m + 1 + n = (m+n) + 1, by ac_refl, by simpa [take_cons, h] using drop_take m n l lemma map_drop {α β : Type*} (f : α → β) : ∀ (L : list α) (i : ℕ), (L.drop i).map f = (L.map f).drop i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_drop], } theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) : ∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l) | 0 l := rfl | (n+1) [] := H.symm | (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l) theorem modify_nth_eq_take_drop (f : α → α) : ∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop _ rfl theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l := by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n+1) l := by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h] lemma reverse_take {α} {xs : list α} (n : ℕ) (h : n ≤ xs.length) : xs.reverse.take n = (xs.drop (xs.length - n)).reverse := begin induction xs generalizing n; simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil], cases h.lt_or_eq_dec with h' h', { replace h' := le_of_succ_le_succ h', rwa [take_append_of_le_length, xs_ih _ h'], rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop], { rwa [succ_eq_add_one, ← tsub_add_eq_add_tsub] }, { rwa length_reverse } }, { subst h', rw [length, tsub_self, drop], suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length, by rw [this, take_length, reverse_cons], rw [length_append, length_reverse], refl } end @[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] := by cases l; cases n; simp only [update_nth] section take' variable [inhabited α] @[simp] theorem take'_length : ∀ n l, length (@take' α _ n l) = n | 0 l := rfl | (n+1) l := congr_arg succ (take'_length _ _) @[simp] theorem take'_nil : ∀ n, take' n (@nil α) = repeat default n | 0 := rfl | (n+1) := congr_arg (cons _) (take'_nil _) theorem take'_eq_take : ∀ {n} {l : list α}, n ≤ length l → take' n l = take n l | 0 l h := rfl | (n+1) (a::l) h := congr_arg (cons _) $ take'_eq_take $ le_of_succ_le_succ h @[simp] theorem take'_left (l₁ l₂ : list α) : take' (length l₁) (l₁ ++ l₂) = l₁ := (take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _) theorem take'_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : take' n (l₁ ++ l₂) = l₁ := by rw ← h; apply take'_left end take' /-! ### foldl, foldr -/ lemma foldl_ext (f g : α → β → α) (a : α) {l : list β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := begin induction l with hd tl ih generalizing a, {refl}, unfold foldl, rw [ih (λ a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)] end lemma foldr_ext (f g : α → β → β) (b : β) {l : list α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := begin induction l with hd tl ih, {refl}, simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H, simp only [foldr, ih H.2, H.1] end @[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : α → β → α) : ∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂ | a [] l₂ := rfl | a (b::l₁) l₂ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ l₂] @[simp] theorem foldr_append (f : α → β → β) : ∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : Π l : list β, foldl f a l = a | [] := rfl | (b::l) := by rw [foldl_cons, hf b, foldl_fixed' l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : Π l : list α, foldr f b l = b | [] := rfl | (a::l) := by rw [foldr_cons, foldr_fixed' l, hf a] @[simp] theorem foldl_fixed {a : α} : Π l : list β, foldl (λ a b, a) a l = a := foldl_fixed' (λ _, rfl) @[simp] theorem foldr_fixed {b : β} : Π l : list α, foldr (λ a b, b) b l = b := foldr_fixed' (λ _, rfl) @[simp] theorem foldl_join (f : α → β → α) : ∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L | a [] := rfl | a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L] @[simp] theorem foldr_join (f : α → β → β) : ∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L | a [] := rfl | a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons] theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) : foldl f a (reverse l) = foldr (λx y, f y x) a l := by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]] theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) : foldr f a (reverse l) = foldl (λx y, f y x) a l := let t := foldl_reverse (λx y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t @[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l | [] := rfl | (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl @[simp] theorem reverse_foldl {l : list α} : reverse (foldl (λ t h, h :: t) [] l) = l := by rw ←foldr_reverse; simp @[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) : foldl f a (map g l) = foldl (λx y, f x (g y)) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldl]] @[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) : foldr f a (map g l) = foldr (f ∘ g) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldr]] theorem foldl_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β) (a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) : list.foldl f' (g a) (l.map g) = g (list.foldl f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldr_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β) (a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) : list.foldr f' (g a) (l.map g) = g (list.foldr f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldl_hom (l : list γ) (f : α → β) (op : α → γ → α) (op' : β → γ → β) (a : α) (h : ∀a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) := eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] } theorem foldr_hom (l : list γ) (f : α → β) (op : γ → α → α) (op' : γ → β → β) (a : α) (h : ∀x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) := by { revert a, induction l; intros; [refl, simp only [*, foldr]] } lemma foldl_hom₂ (l : list ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := eq.symm $ by { revert a b, induction l; intros; [refl, simp only [*, foldl]] } lemma foldr_hom₂ (l : list ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by { revert a, induction l; intros; [refl, simp only [*, foldr]] } lemma injective_foldl_comp {α : Type*} {l : list (α → α)} {f : α → α} (hl : ∀ f ∈ l, function.injective f) (hf : function.injective f): function.injective (@list.foldl (α → α) (α → α) function.comp f l) := begin induction l generalizing f, { exact hf }, { apply l_ih (λ _ h, hl _ (list.mem_cons_of_mem _ h)), apply function.injective.comp hf, apply hl _ (list.mem_cons_self _ _) } end /-- Induction principle for values produced by a `foldr`: if a property holds for the seed element `b : β` and for all incremental `op : α → β → β` performed on the elements `(a : α) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldr_rec_on {C : β → Sort*} (l : list α) (op : α → β → β) (b : β) (hb : C b) (hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op a b)) : C (foldr op b l) := begin induction l with hd tl IH, { exact hb }, { refine hl _ _ hd (mem_cons_self hd tl), refine IH _, intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) } end /-- Induction principle for values produced by a `foldl`: if a property holds for the seed element `b : β` and for all incremental `op : β → α → β` performed on the elements `(a : α) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldl_rec_on {C : β → Sort*} (l : list α) (op : β → α → β) (b : β) (hb : C b) (hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op b a)) : C (foldl op b l) := begin induction l with hd tl IH generalizing b, { exact hb }, { refine IH _ _ _, { intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) }, { exact hl b hb hd (mem_cons_self hd tl) } } end @[simp] lemma foldr_rec_on_nil {C : β → Sort*} (op : α → β → β) (b) (hb : C b) (hl) : foldr_rec_on [] op b hb hl = hb := rfl @[simp] lemma foldr_rec_on_cons {C : β → Sort*} (x : α) (l : list α) (op : α → β → β) (b) (hb : C b) (hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ (x :: l)), C (op a b)) : foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb (λ b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl @[simp] lemma foldl_rec_on_nil {C : β → Sort*} (op : β → α → β) (b) (hb : C b) (hl) : foldl_rec_on [] op b hb hl = hb := rfl /- scanl -/ section scanl variables {f : β → α → β} {b : β} {a : α} {l : list α} lemma length_scanl : ∀ a l, length (scanl f a l) = l.length + 1 | a [] := rfl | a (x :: l) := by erw [length_cons, length_cons, length_scanl] @[simp] lemma scanl_nil (b : β) : scanl f b nil = [b] := rfl @[simp] lemma scanl_cons : scanl f b (a :: l) = [b] ++ scanl f (f b a) l := by simp only [scanl, eq_self_iff_true, singleton_append, and_self] @[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b := begin cases l, { simp only [nth, scanl_nil] }, { simp only [nth, scanl_cons, singleton_append] } end @[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} : (scanl f b l).nth_le 0 h = b := begin cases l, { simp only [nth_le, scanl_nil] }, { simp only [nth_le, scanl_cons, singleton_append] } end lemma nth_succ_scanl {i : ℕ} : (scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (λ x, (l.nth i).map (λ y, f x y)) := begin induction l with hd tl hl generalizing b i, { symmetry, simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none', scanl_nil, option.not_mem_none, forall_true_iff] }, { simp only [nth, scanl_cons, singleton_append], cases i, { simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] }, { simp only [hl, nth] } } end lemma nth_le_succ_scanl {i : ℕ} {h : i + 1 < (scanl f b l).length} : (scanl f b l).nth_le (i + 1) h = f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h)) (l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) := begin induction i with i hi generalizing b l, { cases l, { simp only [length, zero_add, scanl_nil] at h, exact absurd h (lt_irrefl 1) }, { simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } }, { cases l, { simp only [length, add_lt_iff_neg_right, scanl_nil] at h, exact absurd h (not_lt_of_lt nat.succ_pos') }, { simp_rw scanl_cons, rw nth_le_append_right _, { simpa only [hi, length, succ_add_sub_one] }, { simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } } end end scanl /- scanr -/ @[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α), scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l) | a [] := rfl | a (x::l) := let t := scanr_aux_cons x l in by simp only [scanr, scanr_aux, t, foldr_cons] @[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : scanr f b (a::l) = foldr f b (a::l) :: scanr f b l := by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f) include hassoc theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l) | a b nil := rfl | a b (c :: l) := by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc include hcomm theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := by simp only [foldl_cons]; rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l) end foldl_eq_foldr section foldl_eq_foldlr' variables {f : α → β → α} variables hf : ∀ a b c, f (f a b) c = f (f a c) b include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b::l) = f (foldl f a l) b | a b [] := rfl | a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | a [] := rfl | a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl end foldl_eq_foldlr' section foldl_eq_foldlr' variables {f : α → β → β} variables hf : ∀ a b c, f a (f b c) = f b (f a c) include hf theorem foldr_eq_of_comm' : ∀ a b l, foldr f a (b::l) = foldr f (f b a) l | a b [] := rfl | a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl end foldl_eq_foldlr' section variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op] local notation (name := op) a ` * ` b := op a b local notation (name := foldl) l ` <*> ` a := foldl op a l include ha lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂) | [] a₁ a₂ := rfl | (a :: l) a₁ a₂ := calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp only [foldl_cons, ha.assoc] ... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc, foldl_cons] lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂ | [] a₁ a₂ := rfl | (a :: l) a₁ a₂ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] include hc lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### mfoldl, mfoldr, mmap -/ section mfoldl_mfoldr variables {m : Type v → Type w} [monad m] @[simp] theorem mfoldl_nil (f : β → α → m β) {b} : mfoldl f b [] = pure b := rfl @[simp] theorem mfoldr_nil (f : α → β → m β) {b} : mfoldr f b [] = pure b := rfl @[simp] theorem mfoldl_cons {f : β → α → m β} {b a l} : mfoldl f b (a :: l) = f b a >>= λ b', mfoldl f b' l := rfl @[simp] theorem mfoldr_cons {f : α → β → m β} {b a l} : mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl theorem mfoldr_eq_foldr (f : α → β → m β) (b l) : mfoldr f b l = foldr (λ a mb, mb >>= f a) (pure b) l := by induction l; simp * attribute [simp] mmap mmap' variables [is_lawful_monad m] theorem mfoldl_eq_foldl (f : β → α → m β) (b l) : mfoldl f b l = foldl (λ mb a, mb >>= λ b, f b a) (pure b) l := begin suffices h : ∀ (mb : m β), (mb >>= λ b, mfoldl f b l) = foldl (λ mb a, mb >>= λ b, f b a) mb l, by simp [←h (pure b)], induction l; intro, { simp }, { simp only [mfoldl, foldl, ←l_ih] with functor_norm } end @[simp] theorem mfoldl_append {f : β → α → m β} : ∀ {b l₁ l₂}, mfoldl f b (l₁ ++ l₂) = mfoldl f b l₁ >>= λ x, mfoldl f x l₂ | _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind] | _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc] @[simp] theorem mfoldr_append {f : α → β → m β} : ∀ {b l₁ l₂}, mfoldr f b (l₁ ++ l₂) = mfoldr f b l₂ >>= λ x, mfoldr f x l₁ | _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure] | _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc] end mfoldl_mfoldr /-! ### intersperse -/ @[simp] lemma intersperse_nil {α : Type u} (a : α) : intersperse a [] = [] := rfl @[simp] lemma intersperse_singleton {α : Type u} (a b : α) : intersperse a [b] = [b] := rfl @[simp] lemma intersperse_cons_cons {α : Type u} (a b c : α) (tl : list α) : intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl /-! ### split_at and split_on -/ section split_at_on variables (p : α → Prop) [decidable_pred p] (xs ys : list α) (ls : list (list α)) (f : list α → list α) @[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l) | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop] @[simp] lemma split_on_nil {α : Type u} [decidable_eq α] (a : α) : [].split_on a = [[]] := rfl @[simp] lemma split_on_p_nil : [].split_on_p p = [[]] := rfl /-- An auxiliary definition for proving a specification lemma for `split_on_p`. `split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`, where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`. -/ def split_on_p_aux' {α : Type u} (P : α → Prop) [decidable_pred P] : list α → list α → list (list α) | [] xs := [xs] | (h :: t) xs := if P h then xs :: split_on_p_aux' t [] else split_on_p_aux' t (xs ++ [h]) lemma split_on_p_aux_eq : split_on_p_aux' p xs ys = split_on_p_aux p xs ((++) ys) := begin induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self], split_ifs; rw ih, { refine ⟨rfl, rfl⟩ }, { congr, ext, simp } end lemma split_on_p_aux_nil : split_on_p_aux p xs id = split_on_p_aux' p xs [] := by { rw split_on_p_aux_eq, refl } /-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`, interspersed with the elements `L.filter p`. -/ lemma split_on_p_spec (as : list α) : join (zip_with (++) (split_on_p p as) ((as.filter p).map (λ x, [x]) ++ [[]])) = as := begin rw [split_on_p, split_on_p_aux_nil], suffices : ∀ xs, join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(λ x, [x]) ++ [[]])) = xs ++ as, { rw this, refl }, induction as; intro; simp! only [split_on_p_aux', append_nil], split_ifs; simp [zip_with, join, *], end lemma split_on_p_aux_ne_nil : split_on_p_aux p xs f ≠ [] := begin induction xs with _ _ ih generalizing f, { trivial, }, simp only [split_on_p_aux], split_ifs, { trivial, }, exact ih _, end lemma split_on_p_aux_spec : split_on_p_aux p xs f = (xs.split_on_p p).modify_head f := begin simp only [split_on_p], induction xs with hd tl ih generalizing f, { simp [split_on_p_aux], }, simp only [split_on_p_aux], split_ifs, { simp, }, rw [ih (λ l, f (hd :: l)), ih (λ l, id (hd :: l))], simp, end lemma split_on_p_ne_nil : xs.split_on_p p ≠ [] := split_on_p_aux_ne_nil _ _ id @[simp] lemma split_on_p_cons (x : α) (xs : list α) : (x :: xs).split_on_p p = if p x then [] :: xs.split_on_p p else (xs.split_on_p p).modify_head (cons x) := by { simp only [split_on_p, split_on_p_aux], split_ifs, { simp }, rw split_on_p_aux_spec, refl, } /-- If no element satisfies `p` in the list `xs`, then `xs.split_on_p p = [xs]` -/ lemma split_on_p_eq_single (h : ∀ x ∈ xs, ¬p x) : xs.split_on_p p = [xs] := by { induction xs with hd tl ih, { refl, }, simp [h hd _, ih (λ t ht, h t (or.inr ht))], } /-- When a list of the form `[...xs, sep, ...as]` is split on `p`, the first element is `xs`, assuming no element in `xs` satisfies `p` but `sep` does satisfy `p` -/ lemma split_on_p_first (h : ∀ x ∈ xs, ¬p x) (sep : α) (hsep : p sep) (as : list α) : (xs ++ sep :: as).split_on_p p = xs :: as.split_on_p p := by { induction xs with hd tl ih, { simp [hsep], }, simp [h hd _, ih (λ t ht, h t (or.inr ht))], } /-- `intercalate [x]` is the left inverse of `split_on x` -/ lemma intercalate_split_on (x : α) [decidable_eq α] : [x].intercalate (xs.split_on x) = xs := begin simp only [intercalate, split_on], induction xs with hd tl ih, { simp [join], }, simp only [split_on_p_cons], cases h' : split_on_p (=x) tl with hd' tl', { exact (split_on_p_ne_nil _ tl h').elim, }, rw h' at ih, split_ifs, { subst h, simp [ih, join], }, cases tl'; simpa [join] using ih, end /-- `split_on x` is the left inverse of `intercalate [x]`, on the domain consisting of each nonempty list of lists `ls` whose elements do not contain `x` -/ lemma split_on_intercalate [decidable_eq α] (x : α) (hx : ∀ l ∈ ls, x ∉ l) (hls : ls ≠ []) : ([x].intercalate ls).split_on x = ls := begin simp only [intercalate], induction ls with hd tl ih, { contradiction, }, cases tl, { suffices : hd.split_on x = [hd], { simpa [join], }, refine split_on_p_eq_single _ _ _, intros y hy H, rw H at hy, refine hx hd _ hy, simp, }, { simp only [intersperse_cons_cons, singleton_append, join], specialize ih _ _, { intros l hl, apply hx l, simp at hl ⊢, tauto, }, { trivial, }, have := split_on_p_first (=x) hd _ x rfl _, { simp only [split_on] at ⊢ ih, rw this, rw ih, }, intros y hy H, rw H at hy, exact hx hd (or.inl rfl) hy, } end end split_at_on /-! ### map for partial functions -/ /-- Partial map. If `f : Π a, p a → β` is a partial function defined on `a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β | [] H := [] | (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2 /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id) theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {l : list α} (hx : x ∈ l) : sizeof x < sizeof l := begin induction l with h t ih; cases hx, { rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) }, { exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) } end @[simp] theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) : @pmap _ _ p (λ a _, f a) l H = map f l := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β} (l : list α) {H₁ H₂} (h : ∀ (a ∈ l) h₁ h₂, f a h₁ = g a h₂) : pmap f l H₁ = pmap g l H₂ := begin induction l with _ _ ih, { refl, }, { rw [pmap, pmap, h _ (mem_cons_self _ _), ih (λ a ha, h a (mem_cons_of_mem _ ha))], }, end theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β) (l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_map {p : β → Prop} (g : ∀ b, p b → γ) (f : α → β) (l H) : pmap g (map f l) H = pmap (λ a h, g (f a) h) l (λ a h, H _ (mem_map_of_mem _ h)) := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β) (l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) := by rw [attach, map_pmap]; exact pmap_congr l (λ _ _ _ _, rfl) @[simp] lemma attach_map_coe' (l : list α) (f : α → β) : l.attach.map (λ i, f i) = l.map f := by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _) lemma attach_map_val' (l : list α) (f : α → β) : l.attach.map (λ i, f i.val) = l.map f := attach_map_coe' _ _ @[simp] lemma attach_map_coe (l : list α) : l.attach.map (coe : _ → α) = l := (attach_map_coe' _ _).trans l.map_id lemma attach_map_val (l : list α) : l.attach.map subtype.val = l := attach_map_coe _ @[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ := by have := mem_map.1 (by rw [attach_map_val]; exact h); { rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m } @[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β} {l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b := by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists] @[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β} {l H} : length (pmap f l H) = length l := by induction l; [refl, simp only [*, pmap, length]] @[simp] lemma length_attach (L : list α) : L.attach.length = L.length := length_pmap @[simp] lemma pmap_eq_nil {p : α → Prop} {f : Π a, p a → β} {l H} : pmap f l H = [] ↔ l = [] := by rw [← length_eq_zero, length_pmap, length_eq_zero] @[simp] lemma attach_eq_nil (l : list α) : l.attach = [] ↔ l = [] := pmap_eq_nil lemma last_pmap {α β : Type*} (p : α → Prop) (f : Π a, p a → β) (l : list α) (hl₁ : ∀ a ∈ l, p a) (hl₂ : l ≠ []) : (l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hl₂) = f (l.last hl₂) (hl₁ _ (list.last_mem hl₂)) := begin induction l with l_hd l_tl l_ih, { apply (hl₂ rfl).elim }, { cases l_tl, { simp }, { apply l_ih } } end lemma nth_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) (n : ℕ) : nth (pmap f l h) n = option.pmap f (nth l n) (λ x H, h x (nth_mem H)) := begin induction l with hd tl hl generalizing n, { simp }, { cases n; simp [hl] } end lemma nth_le_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) {n : ℕ} (hn : n < (pmap f l h).length) : nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h ▸ hn)) (h _ (nth_le_mem l n (@length_pmap _ _ p f l h ▸ hn))) := begin induction l with hd tl hl generalizing n, { simp only [length, pmap] at hn, exact absurd hn (not_lt_of_le n.zero_le) }, { cases n, { simp }, { simpa [hl] } } end lemma pmap_append {p : ι → Prop} (f : Π (a : ι), p a → α) (l₁ l₂ : list ι) (h : ∀ a ∈ l₁ ++ l₂, p a) : (l₁ ++ l₂).pmap f h = l₁.pmap f (λ a ha, h a (mem_append_left l₂ ha)) ++ l₂.pmap f (λ a ha, h a (mem_append_right l₁ ha)) := begin induction l₁ with _ _ ih, { refl, }, { dsimp only [pmap, cons_append], rw ih, } end lemma pmap_append' {α β : Type*} {p : α → Prop} (f : Π (a : α), p a → β) (l₁ l₂ : list α) (h₁ : ∀ a ∈ l₁, p a) (h₂ : ∀ a ∈ l₂, p a) : (l₁ ++ l₂).pmap f (λ a ha, (list.mem_append.1 ha).elim (h₁ a) (h₂ a)) = l₁.pmap f h₁ ++ l₂.pmap f h₂ := pmap_append f l₁ l₂ _ /-! ### find -/ section find variables {p : α → Prop} [decidable_pred p] {l : list α} {a : α} @[simp] theorem find_nil (p : α → Prop) [decidable_pred p] : find p [] = none := rfl @[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a := if_pos h @[simp] theorem find_cons_of_neg (l) (h : ¬ p a) : find p (a::l) = find p l := if_neg h @[simp] theorem find_eq_none : find p l = none ↔ ∀ x ∈ l, ¬ p x := begin induction l with a l IH, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases h : p a, { simp only [find_cons_of_pos _ h, h, not_true, false_and] }, { rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] } end theorem find_some (H : find p l = some a) : p a := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, exact h }, { rw find_cons_of_neg _ h at H, exact IH H } end @[simp] theorem find_mem (H : find p l = some a) : a ∈ l := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self }, { rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) } end end find /-! ### lookmap -/ section lookmap variables (f : α → option α) @[simp] theorem lookmap_nil : [].lookmap f = [] := rfl @[simp] theorem lookmap_cons_none {a : α} (l : list α) (h : f a = none) : (a :: l).lookmap f = a :: l.lookmap f := by simp [lookmap, h] @[simp] theorem lookmap_cons_some {a b : α} (l : list α) (h : f a = some b) : (a :: l).lookmap f = b :: l := by simp [lookmap, h] theorem lookmap_some : ∀ l : list α, l.lookmap some = l | [] := rfl | (a::l) := rfl theorem lookmap_none : ∀ l : list α, l.lookmap (λ _, none) = l | [] := rfl | (a::l) := congr_arg (cons a) (lookmap_none l) theorem lookmap_congr {f g : α → option α} : ∀ {l : list α}, (∀ a ∈ l, f a = g a) → l.lookmap f = l.lookmap g | [] H := rfl | (a::l) H := begin cases forall_mem_cons.1 H with H₁ H₂, cases h : g a with b, { simp [h, H₁.trans h, lookmap_congr H₂] }, { simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] } end theorem lookmap_of_forall_not {l : list α} (H : ∀ a ∈ l, f a = none) : l.lookmap f = l := (lookmap_congr H).trans (lookmap_none l) theorem lookmap_map_eq (g : α → β) (h : ∀ a (b ∈ f a), g a = g b) : ∀ l : list α, map g (l.lookmap f) = map g l | [] := rfl | (a::l) := begin cases h' : f a with b, { simp [h', lookmap_map_eq] }, { simp [lookmap_cons_some _ _ h', h _ _ h'] } end theorem lookmap_id' (h : ∀ a (b ∈ f a), a = b) (l : list α) : l.lookmap f = l := by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h theorem length_lookmap (l : list α) : length (l.lookmap f) = length l := by rw [← length_map, lookmap_map_eq _ (λ _, ()), length_map]; simp end lookmap /-! ### filter_map -/ @[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) : filter_map f (a :: l) = filter_map f l := by simp only [filter_map, h] @[simp] theorem filter_map_cons_some (f : α → option β) (a : α) (l : list α) {b : β} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := by simp only [filter_map, h]; split; refl theorem filter_map_cons (f : α → option β) (a : α) (l : list α) : filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) := begin generalize eq : f a = b, cases b, { rw filter_map_cons_none _ _ eq }, { rw filter_map_cons_some _ _ _ eq }, end lemma filter_map_append {α β : Type*} (l l' : list α) (f : α → option β) : filter_map f (l ++ l') = filter_map f l ++ filter_map f l' := begin induction l with hd tl hl generalizing l', { simp }, { rw [cons_append, filter_map, filter_map], cases f hd; simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] } end theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f := begin funext l, induction l with a l IH, {refl}, simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl end theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := begin funext l, induction l with a l IH, {refl}, by_cases pa : p a, { simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl }, { simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] } end theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) : filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l := begin induction l with a l IH, {refl}, cases h : f a with b, { rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH], simp only [h, option.none_bind'] }, rw filter_map_cons_some _ _ _ h, cases h' : g b with c; [ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH], rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ]; simp only [h, h', option.some_bind'] end theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) : map g (filter_map f l) = filter_map (λ x, (f x).map g) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) : filter_map g (map f l) = filter_map (g ∘ f) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) : filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l := by rw [← filter_map_eq_filter, filter_map_filter_map]; refl theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) : filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l := begin rw [← filter_map_eq_filter, filter_map_filter_map], congr, funext x, show (option.guard p x).bind f = ite (p x) (f x) none, by_cases h : p x, { simp only [option.guard, if_pos h, option.some_bind'] }, { simp only [option.guard, if_neg h, option.none_bind'] } end @[simp] theorem filter_map_some (l : list α) : filter_map some l = l := by rw filter_map_eq_map; apply map_id theorem map_filter_map_some_eq_filter_map_is_some (f : α → option β) (l : list α) : (l.filter_map f).map some = (l.map f).filter (λ b, b.is_some) := begin induction l with x xs ih, { simp }, { cases h : f x; rw [list.filter_map_cons, h]; simp [h, ih] }, end @[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} : b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b := begin induction l with a l IH, { split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } }, cases h : f a with b', { have : f a ≠ some b, {rw h, intro, contradiction}, simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] }, { have : f a = some b ↔ b = b', { split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} }, simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, this, exists_eq_left] } end @[simp] theorem filter_map_join (f : α → option β) (L : list (list α)) : filter_map f (join L) = join (map (filter_map f) L) := begin induction L with hd tl ih, { refl }, { rw [map, join, join, filter_map_append, ih] }, end theorem map_filter_map_of_inv (f : α → option β) (g : β → α) (H : ∀ x : α, (f x).map g = some x) (l : list α) : map g (filter_map f l) = l := by simp only [map_filter_map, H, filter_map_some] theorem length_filter_le (p : α → Prop) [decidable_pred p] (l : list α) : (l.filter p).length ≤ l.length := (list.filter_sublist _).length_le theorem length_filter_map_le (f : α → option β) (l : list α) : (list.filter_map f l).length ≤ l.length := begin rw [← list.length_map some, list.map_filter_map_some_eq_filter_map_is_some, ← list.length_map f], apply list.length_filter_le, end theorem sublist.filter_map (f : α → option β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ := by induction s with l₁ l₂ a s IH l₁ l₂ a s IH; simp only [filter_map]; cases f a with b; simp only [filter_map, IH, sublist.cons, sublist.cons2] theorem sublist.map (f : α → β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : map f l₁ <+ map f l₂ := filter_map_eq_map f ▸ s.filter_map _ /-! ### reduce_option -/ @[simp] lemma reduce_option_cons_of_some (x : α) (l : list (option α)) : reduce_option (some x :: l) = x :: l.reduce_option := by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self] @[simp] lemma reduce_option_cons_of_none (l : list (option α)) : reduce_option (none :: l) = l.reduce_option := by simp only [reduce_option, filter_map, id.def] @[simp] lemma reduce_option_nil : @reduce_option α [] = [] := rfl @[simp] lemma reduce_option_map {l : list (option α)} {f : α → β} : reduce_option (map (option.map f) l) = map f (reduce_option l) := begin induction l with hd tl hl, { simp only [reduce_option_nil, map_nil] }, { cases hd; simpa only [true_and, option.map_some', map, eq_self_iff_true, reduce_option_cons_of_some] using hl }, end lemma reduce_option_append (l l' : list (option α)) : (l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option := filter_map_append l l' id lemma reduce_option_length_le (l : list (option α)) : l.reduce_option.length ≤ l.length := begin induction l with hd tl hl, { simp only [reduce_option_nil, length] }, { cases hd, { exact nat.le_succ_of_le hl }, { simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} } end lemma reduce_option_length_eq_iff {l : list (option α)} : l.reduce_option.length = l.length ↔ ∀ x ∈ l, option.is_some x := begin induction l with hd tl hl, { simp only [forall_const, reduce_option_nil, not_mem_nil, forall_prop_of_false, eq_self_iff_true, length, not_false_iff] }, { cases hd, { simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and, reduce_option_cons_of_none, length, option.is_some_none, iff_false], intro H, have := reduce_option_length_le tl, rw H at this, exact absurd (nat.lt_succ_self _) (not_lt_of_le this) }, { simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj, bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } } end lemma reduce_option_length_lt_iff {l : list (option α)} : l.reduce_option.length < l.length ↔ none ∈ l := begin rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff], induction l; simp *, rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or] end lemma reduce_option_singleton (x : option α) : [x].reduce_option = x.to_list := by cases x; refl lemma reduce_option_concat (l : list (option α)) (x : option α) : (l.concat x).reduce_option = l.reduce_option ++ x.to_list := begin induction l with hd tl hl generalizing x, { cases x; simp [option.to_list] }, { simp only [concat_eq_append, reduce_option_append] at hl, cases hd; simp [hl, reduce_option_append] } end lemma reduce_option_concat_of_some (l : list (option α)) (x : α) : (l.concat (some x)).reduce_option = l.reduce_option.concat x := by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some] lemma reduce_option_mem_iff {l : list (option α)} {x : α} : x ∈ l.reduce_option ↔ (some x) ∈ l := by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right] lemma reduce_option_nth_iff {l : list (option α)} {x : α} : (∃ i, l.nth i = some (some x)) ↔ ∃ i, l.reduce_option.nth i = some x := by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff] /-! ### filter -/ section filter variables {p : α → Prop} [decidable_pred p] lemma filter_singleton {a : α} : [a].filter p = if p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Prop) [decidable_pred p] (l : list α) : filter p l = foldr (λ a out, if p a then a :: out else out) [] l := by induction l; simp [*, filter] lemma filter_congr' {p q : α → Prop} [decidable_pred p] [decidable_pred q] : ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l | [] _ := rfl | (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a; [simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr' h.2], simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr' h.2]]; split; refl @[simp] theorem filter_subset (l : list α) : filter p l ⊆ l := (filter_sublist l).subset theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a | (b::l) ain := if pb : p b then have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain, or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, begin rw [← this] at pb, exact pb end) (assume : a ∈ filter p l, of_mem_filter this) else begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l | (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self | (b::l) (or.inr ain) pa := if pb : p b then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa @[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a := ⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩ lemma monotone_filter_left (p : α → Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l ⊆ l') : filter p l ⊆ filter p l' := begin intros x hx, rw [mem_filter] at hx ⊢, exact ⟨h hx.left, hx.right⟩ end theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases p a, { rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] }, { refine iff_of_false (λ hl, h $ of_mem_filter (_ : a ∈ filter p (a :: l))) (mt and.left h), rw hl, exact mem_cons_self _ _ } end theorem filter_length_eq_length {l} : (filter p l).length = l.length ↔ ∀ a ∈ l, p a := iff.trans ⟨l.filter_sublist.eq_of_length, congr_arg list.length⟩ filter_eq_self theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a := by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and] variable (p) theorem sublist.filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ := filter_map_eq_filter p ▸ s.filter_map _ lemma monotone_filter_right (l : list α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≤ q) : l.filter p <+ l.filter q := begin induction l with hd tl IH, { refl }, { by_cases hp : p hd, { rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)], exact IH.cons_cons hd }, { rw filter_cons_of_neg _ hp, by_cases hq : q hd, { rw filter_cons_of_pos _ hq, exact sublist_cons_of_sublist hd IH }, { rw filter_cons_of_neg _ hq, exact IH } } } end theorem map_filter (f : β → α) (l : list β) : filter p (map f l) = map f (filter (p ∘ f) l) := by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl @[simp] theorem filter_filter (q) [decidable_pred q] : ∀ l, filter p (filter q l) = filter (λ a, p a ∧ q a) l | [] := rfl | (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false, true_and, false_and, filter_filter l, eq_self_iff_true] @[simp] lemma filter_true {h : decidable_pred (λ a : α, true)} (l : list α) : @filter α (λ _, true) h l = l := by convert filter_eq_self.2 (λ _ _, trivial) @[simp] lemma filter_false {h : decidable_pred (λ a : α, false)} (l : list α) : @filter α (λ _, false) h l = [] := by convert filter_eq_nil.2 (λ _ _, id) @[simp] theorem span_eq_take_drop : ∀ (l : list α), span p l = (take_while p l, drop_while p l) | [] := rfl | (a::l) := if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while] else by simp only [span, take_while, drop_while, if_neg pa] @[simp] theorem take_while_append_drop : ∀ (l : list α), take_while p l ++ drop_while p l = l | [] := rfl | (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append, take_while_append_drop l] else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append] lemma drop_while_nth_le_zero_not (l : list α) (hl : 0 < (l.drop_while p).length) : ¬ p ((l.drop_while p).nth_le 0 hl) := begin induction l with hd tl IH, { cases hl }, { simp only [drop_while], split_ifs with hp, { exact IH _ }, { simpa using hp } } end variables {p} {l : list α} @[simp] lemma drop_while_eq_nil_iff : drop_while p l = [] ↔ ∀ x ∈ l, p x := begin induction l with x xs IH, { simp [drop_while] }, { by_cases hp : p x; simp [hp, drop_while, IH] } end @[simp] lemma take_while_eq_self_iff : take_while p l = l ↔ ∀ x ∈ l, p x := begin induction l with x xs IH, { simp [take_while] }, { by_cases hp : p x; simp [hp, take_while, IH] } end @[simp] lemma take_while_eq_nil_iff : take_while p l = [] ↔ ∀ (hl : 0 < l.length), ¬ p (l.nth_le 0 hl) := begin induction l with x xs IH, { simp }, { by_cases hp : p x; simp [hp, take_while, IH] } end lemma mem_take_while_imp {x : α} (hx : x ∈ take_while p l) : p x := begin induction l with hd tl IH, { simpa [take_while] using hx }, { simp only [take_while] at hx, split_ifs at hx, { rw mem_cons_iff at hx, rcases hx with rfl|hx, { exact h }, { exact IH hx } }, { simpa using hx } } end lemma take_while_take_while (p q : α → Prop) [decidable_pred p] [decidable_pred q] (l : list α) : take_while p (take_while q l) = take_while (λ a, p a ∧ q a) l := begin induction l with hd tl IH, { simp [take_while] }, { by_cases hp : p hd; by_cases hq : q hd; simp [take_while, hp, hq, IH] } end lemma take_while_idem : take_while p (take_while p l) = take_while p l := by simp_rw [take_while_take_while, and_self] end filter /-! ### erasep -/ section erasep variables {p : α → Prop} [decidable_pred p] @[simp] theorem erasep_nil : [].erasep p = [] := rfl theorem erasep_cons (a : α) (l : list α) : (a :: l).erasep p = if p a then l else a :: l.erasep p := rfl @[simp] theorem erasep_cons_of_pos {a : α} {l : list α} (h : p a) : (a :: l).erasep p = l := by simp [erasep_cons, h] @[simp] theorem erasep_cons_of_neg {a : α} {l : list α} (h : ¬ p a) : (a::l).erasep p = a :: l.erasep p := by simp [erasep_cons, h] theorem erasep_of_forall_not {l : list α} (h : ∀ a ∈ l, ¬ p a) : l.erasep p = l := by induction l with _ _ ih; [refl, simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]] theorem exists_of_erasep {l : list α} {a} (al : a ∈ l) (pa : p a) : ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ := begin induction l with b l IH, {cases al}, by_cases pb : p b, { exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ }, { rcases al with rfl | al, {exact pb.elim pa}, rcases IH al with ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩, exact ⟨c, b::l₁, l₂, forall_mem_cons.2 ⟨pb, h₁⟩, h₂, by rw h₃; refl, by simp [pb, h₄]⟩ } end theorem exists_or_eq_self_of_erasep (p : α → Prop) [decidable_pred p] (l : list α) : l.erasep p = l ∨ ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ := begin by_cases h : ∃ a ∈ l, p a, { rcases h with ⟨a, ha, pa⟩, exact or.inr (exists_of_erasep ha pa) }, { simp at h, exact or.inl (erasep_of_forall_not h) } end @[simp] theorem length_erasep_of_mem {l : list α} {a} (al : a ∈ l) (pa : p a) : length (l.erasep p) = pred (length l) := by rcases exists_of_erasep al pa with ⟨_, l₁, l₂, _, _, e₁, e₂⟩; rw e₂; simp [-add_comm, e₁]; refl @[simp] lemma length_erasep_add_one {l : list α} {a} (al : a ∈ l) (pa : p a) : (l.erasep p).length + 1 = l.length := let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_erasep al pa in by { rw [h₂, h₁, length_append, length_append], refl } theorem erasep_append_left {a : α} (pa : p a) : ∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erasep p = l₁.erasep p ++ l₂ | (x::xs) l₂ h := begin by_cases h' : p x; simp [h'], rw erasep_append_left l₂ (mem_of_ne_of_mem (mt _ h') h), rintro rfl, exact pa end theorem erasep_append_right : ∀ {l₁ : list α} (l₂), (∀ b ∈ l₁, ¬ p b) → (l₁++l₂).erasep p = l₁ ++ l₂.erasep p | [] l₂ h := rfl | (x::xs) l₂ h := by simp [(forall_mem_cons.1 h).1, erasep_append_right _ (forall_mem_cons.1 h).2] theorem erasep_sublist (l : list α) : l.erasep p <+ l := by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩; [rw h, {rw [h₄, h₃], simp}] theorem erasep_subset (l : list α) : l.erasep p ⊆ l := (erasep_sublist l).subset theorem sublist.erasep {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁.erasep p <+ l₂.erasep p := begin induction s, case list.sublist.slnil { refl }, case list.sublist.cons : l₁ l₂ a s IH { by_cases h : p a; simp [h], exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] }, case list.sublist.cons2 : l₁ l₂ a s IH { by_cases h : p a; simp [h], exacts [s, IH.cons2 _ _ _] } end theorem mem_of_mem_erasep {a : α} {l : list α} : a ∈ l.erasep p → a ∈ l := @erasep_subset _ _ _ _ _ @[simp] theorem mem_erasep_of_neg {a : α} {l : list α} (pa : ¬ p a) : a ∈ l.erasep p ↔ a ∈ l := ⟨mem_of_mem_erasep, λ al, begin rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩, { rwa h }, { rw h₄, rw h₃ at al, have : a ≠ c, {rintro rfl, exact pa.elim h₂}, simpa [this] using al } end⟩ theorem erasep_map (f : β → α) : ∀ (l : list β), (map f l).erasep p = map f (l.erasep (p ∘ f)) | [] := rfl | (b::l) := by by_cases p (f b); simp [h, erasep_map l] @[simp] theorem extractp_eq_find_erasep : ∀ l : list α, extractp p l = (find p l, erasep p l) | [] := rfl | (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l] end erasep /-! ### erase -/ section erase variable [decidable_eq α] @[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl theorem erase_cons (a b : α) (l : list α) : (b :: l).erase a = if b = a then l else b :: l.erase a := rfl @[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l := by simp only [erase_cons, if_pos rfl] @[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) : (b::l).erase a = b :: l.erase a := by simp only [erase_cons, if_neg h]; split; refl theorem erase_eq_erasep (a : α) (l : list α) : l.erase a = l.erasep (eq a) := by { induction l with b l, {refl}, by_cases a = b; [simp [h], simp [h, ne.symm h, *]] } @[simp, priority 980] theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l := by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h' theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) : ∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ := by rcases exists_of_erasep h rfl with ⟨_, l₁, l₂, h₁, rfl, h₂, h₃⟩; rw erase_eq_erasep; exact ⟨l₁, l₂, λ h, h₁ _ h rfl, h₂, h₃⟩ @[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) : length (l.erase a) = pred (length l) := by rw erase_eq_erasep; exact length_erasep_of_mem h rfl @[simp] lemma length_erase_add_one {a : α} {l : list α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_erasep, length_erasep_add_one h rfl] theorem erase_append_left {a : α} {l₁ : list α} (l₂) (h : a ∈ l₁) : (l₁++l₂).erase a = l₁.erase a ++ l₂ := by simp [erase_eq_erasep]; exact erasep_append_left (by refl) l₂ h theorem erase_append_right {a : α} {l₁ : list α} (l₂) (h : a ∉ l₁) : (l₁++l₂).erase a = l₁ ++ l₂.erase a := by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right]; rintro b h' rfl; exact h h' theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l := by rw erase_eq_erasep; apply erasep_sublist theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l := (erase_sublist a l).subset theorem sublist.erase (a : α) {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.erase a <+ l₂.erase a := by simp [erase_eq_erasep]; exact sublist.erasep h theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l := @erase_subset _ _ _ _ _ @[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l := by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a := if ab : a = b then by rw ab else if ha : a ∈ l then if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with | ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb := if h₁ : b ∈ l₁ then by rw [erase_append_left _ h₁, erase_append_left _ h₁, erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head] else by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha', erase_cons_tail _ ab, erase_cons_head] end else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)] else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)] theorem map_erase [decidable_eq β] {f : α → β} (finj : injective f) {a : α} (l : list α) : map f (l.erase a) = (map f l).erase (f a) := have this : eq a = eq (f a) ∘ f, { ext b, simp [finj.eq_iff] }, by simp [erase_eq_erasep, erase_eq_erasep, erasep_map, this] theorem map_foldl_erase [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} : map f (foldl list.erase l₁ l₂) = foldl (λ l a, l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁; [refl, simp only [foldl_cons, map_erase finj, *]] end erase /-! ### diff -/ section diff variable [decidable_eq α] @[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl @[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ := if h : a ∈ l₁ then by simp only [list.diff, if_pos h] else by simp only [list.diff, if_neg h, erase_of_not_mem h] lemma diff_cons_right (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.diff l₂).erase a := begin induction l₂ with b l₂ ih generalizing l₁ a, { simp_rw [diff_cons, diff_nil] }, { rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] } end lemma diff_erase (l₁ l₂ : list α) (a : α) : (l₁.diff l₂).erase a = (l₁.erase a).diff l₂ := by rw [← diff_cons_right, diff_cons] @[simp] theorem nil_diff (l : list α) : [].diff l = [] := by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]] lemma cons_diff (a : α) (l₁ l₂ : list α) : (a :: l₁).diff l₂ = if a ∈ l₂ then l₁.diff (l₂.erase a) else a :: l₁.diff l₂ := begin induction l₂ with b l₂ ih, { refl }, rcases eq_or_ne a b with rfl|hne, { simp }, { simp only [mem_cons_iff, *, false_or, diff_cons_right], split_ifs with h₂; simp [diff_erase, list.erase, hne, hne.symm] } end lemma cons_diff_of_mem {a : α} {l₂ : list α} (h : a ∈ l₂) (l₁ : list α) : (a :: l₁).diff l₂ = l₁.diff (l₂.erase a) := by rw [cons_diff, if_pos h] lemma cons_diff_of_not_mem {a : α} {l₂ : list α} (h : a ∉ l₂) (l₁ : list α) : (a :: l₁).diff l₂ = a :: l₁.diff l₂ := by rw [cons_diff, if_neg h] theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂ | l₁ [] := rfl | l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _) @[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ := by simp only [diff_eq_foldl, foldl_append] @[simp] theorem map_diff [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] theorem diff_sublist : ∀ l₁ l₂ : list α, l₁.diff l₂ <+ l₁ | l₁ [] := sublist.refl _ | l₁ (a::l₂) := calc l₁.diff (a :: l₂) = (l₁.erase a).diff l₂ : diff_cons _ _ _ ... <+ l₁.erase a : diff_sublist _ _ ... <+ l₁ : list.erase_sublist _ _ theorem diff_subset (l₁ l₂ : list α) : l₁.diff l₂ ⊆ l₁ := (diff_sublist _ _).subset theorem mem_diff_of_mem {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁ → a ∉ l₂ → a ∈ l₁.diff l₂ | l₁ [] h₁ h₂ := h₁ | l₁ (b::l₂) h₁ h₂ := by rw diff_cons; exact mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons h₂)).2 h₁) (not_mem_of_not_mem_cons h₂) theorem sublist.diff_right : ∀ {l₁ l₂ l₃: list α}, l₁ <+ l₂ → l₁.diff l₃ <+ l₂.diff l₃ | l₁ l₂ [] h := h | l₁ l₂ (a::l₃) h := by simp only [diff_cons, (h.erase _).diff_right] theorem erase_diff_erase_sublist_of_sublist {a : α} : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → (l₂.erase a).diff (l₁.erase a) <+ l₂.diff l₁ | [] l₂ h := erase_sublist _ _ | (b::l₁) l₂ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons] else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons, erase_comm a b l₂] using erase_diff_erase_sublist_of_sublist (h.erase b) end diff /-! ### enum -/ theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l | n [] := rfl | n (a::l) := congr_arg nat.succ (length_enum_from _ _) theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _ @[simp] theorem enum_from_nth : ∀ n (l : list α) m, nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m | n [] m := rfl | n (a :: l) 0 := rfl | n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $ by rw [add_right_comm]; refl @[simp] theorem enum_nth : ∀ (l : list α) n, nth (enum l) n = (λ a, (n, a)) <$> nth l n := by simp only [enum, enum_from_nth, zero_add]; intros; refl @[simp] theorem enum_from_map_snd : ∀ n (l : list α), map prod.snd (enum_from n l) = l | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _) @[simp] theorem enum_map_snd : ∀ (l : list α), map prod.snd (enum l) = l := enum_from_map_snd _ theorem mem_enum_from {x : α} {i : ℕ} : ∀ {j : ℕ} (xs : list α), (i, x) ∈ xs.enum_from j → j ≤ i ∧ i < j + xs.length ∧ x ∈ xs | j [] := by simp [enum_from] | j (y :: ys) := suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys → j ≤ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys), by simpa [enum_from, mem_enum_from ys], begin rintro (h|h), { refine ⟨le_of_eq h.1.symm,h.1 ▸ _,or.inl h.2⟩, apply nat.lt_add_of_pos_right; simp }, { obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h, refine ⟨_, _, _⟩, { exact le_trans (nat.le_succ _) hji }, { convert hijlen using 1, ac_refl }, { simp [hmem] } } end @[simp] lemma enum_nil : enum ([] : list α) = [] := rfl @[simp] lemma enum_from_nil (n : ℕ) : enum_from n ([] : list α) = [] := rfl @[simp] lemma enum_from_cons (x : α) (xs : list α) (n : ℕ) : enum_from n (x :: xs) = (n, x) :: enum_from (n + 1) xs := rfl @[simp] lemma enum_cons (x : α) (xs : list α) : enum (x :: xs) = (0, x) :: enum_from 1 xs := rfl @[simp] lemma enum_from_singleton (x : α) (n : ℕ) : enum_from n [x] = [(n, x)] := rfl @[simp] lemma enum_singleton (x : α) : enum [x] = [(0, x)] := rfl lemma enum_from_append (xs ys : list α) (n : ℕ) : enum_from n (xs ++ ys) = enum_from n xs ++ enum_from (n + xs.length) ys := begin induction xs with x xs IH generalizing ys n, { simp }, { rw [cons_append, enum_from_cons, IH, ←cons_append, ←enum_from_cons, length, add_right_comm, add_assoc] } end lemma enum_append (xs ys : list α) : enum (xs ++ ys) = enum xs ++ enum_from xs.length ys := by simp [enum, enum_from_append] lemma map_fst_add_enum_from_eq_enum_from (l : list α) (n k : ℕ) : map (prod.map (+ n) id) (enum_from k l) = enum_from (n + k) l := begin induction l with hd tl IH generalizing n k, { simp [enum_from] }, { simp only [enum_from, map, zero_add, prod.map_mk, id.def, eq_self_iff_true, true_and], simp [IH, add_comm n k, add_assoc, add_left_comm] } end lemma map_fst_add_enum_eq_enum_from (l : list α) (n : ℕ) : map (prod.map (+ n) id) (enum l) = enum_from n l := map_fst_add_enum_from_eq_enum_from l _ _ lemma nth_le_enum_from (l : list α) (n i : ℕ) (hi' : i < (l.enum_from n).length) (hi : i < l.length := by simpa [length_enum_from] using hi') : (l.enum_from n).nth_le i hi' = (n + i, l.nth_le i hi) := begin rw [←option.some_inj, ←nth_le_nth], simp [enum_from_nth, nth_le_nth hi] end lemma nth_le_enum (l : list α) (i : ℕ) (hi' : i < l.enum.length) (hi : i < l.length := by simpa [length_enum] using hi') : l.enum.nth_le i hi' = (i, l.nth_le i hi) := by { convert nth_le_enum_from _ _ _ hi', exact (zero_add _).symm } section choose variables (p : α → Prop) [decidable_pred p] (l : list α) lemma choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose /-! ### map₂_left' -/ section map₂_left' -- The definitional equalities for `map₂_left'` can already be used by the -- simplifie because `map₂_left'` is marked `@[simp]`. @[simp] theorem map₂_left'_nil_right (f : α → option β → γ) (as) : map₂_left' f as [] = (as.map (λ a, f a none), []) := by cases as; refl end map₂_left' /-! ### map₂_right' -/ section map₂_right' variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem map₂_right'_nil_left : map₂_right' f [] bs = (bs.map (f none), []) := by cases bs; refl @[simp] theorem map₂_right'_nil_right : map₂_right' f as [] = ([], as) := rfl @[simp] theorem map₂_right'_nil_cons : map₂_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) := rfl @[simp] theorem map₂_right'_cons_cons : map₂_right' f (a :: as) (b :: bs) = let rec := map₂_right' f as bs in (f (some a) b :: rec.fst, rec.snd) := rfl end map₂_right' /-! ### zip_left' -/ section zip_left' variables (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem zip_left'_nil_right : zip_left' as ([] : list β) = (as.map (λ a, (a, none)), []) := by cases as; refl @[simp] theorem zip_left'_nil_left : zip_left' ([] : list α) bs = ([], bs) := rfl @[simp] theorem zip_left'_cons_nil : zip_left' (a :: as) ([] : list β) = ((a, none) :: as.map (λ a, (a, none)), []) := rfl @[simp] theorem zip_left'_cons_cons : zip_left' (a :: as) (b :: bs) = let rec := zip_left' as bs in ((a, some b) :: rec.fst, rec.snd) := rfl end zip_left' /-! ### zip_right' -/ section zip_right' variables (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem zip_right'_nil_left : zip_right' ([] : list α) bs = (bs.map (λ b, (none, b)), []) := by cases bs; refl @[simp] theorem zip_right'_nil_right : zip_right' as ([] : list β) = ([], as) := rfl @[simp] theorem zip_right'_nil_cons : zip_right' ([] : list α) (b :: bs) = ((none, b) :: bs.map (λ b, (none, b)), []) := rfl @[simp] theorem zip_right'_cons_cons : zip_right' (a :: as) (b :: bs) = let rec := zip_right' as bs in ((some a, b) :: rec.fst, rec.snd) := rfl end zip_right' /-! ### map₂_left -/ section map₂_left variables (f : α → option β → γ) (as : list α) -- The definitional equalities for `map₂_left` can already be used by the -- simplifier because `map₂_left` is marked `@[simp]`. @[simp] theorem map₂_left_nil_right : map₂_left f as [] = as.map (λ a, f a none) := by cases as; refl theorem map₂_left_eq_map₂_left' : ∀ as bs, map₂_left f as bs = (map₂_left' f as bs).fst | [] bs := by simp! | (a :: as) [] := by simp! | (a :: as) (b :: bs) := by simp! [*] theorem map₂_left_eq_map₂ : ∀ as bs, length as ≤ length bs → map₂_left f as bs = map₂ (λ a b, f a (some b)) as bs | [] [] h := by simp! | [] (b :: bs) h := by simp! | (a :: as) [] h := by { simp at h, contradiction } | (a :: as) (b :: bs) h := by { simp at h, simp! [*] } end map₂_left /-! ### map₂_right -/ section map₂_right variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem map₂_right_nil_left : map₂_right f [] bs = bs.map (f none) := by cases bs; refl @[simp] theorem map₂_right_nil_right : map₂_right f as [] = [] := rfl @[simp] theorem map₂_right_nil_cons : map₂_right f [] (b :: bs) = f none b :: bs.map (f none) := rfl @[simp] theorem map₂_right_cons_cons : map₂_right f (a :: as) (b :: bs) = f (some a) b :: map₂_right f as bs := rfl theorem map₂_right_eq_map₂_right' : map₂_right f as bs = (map₂_right' f as bs).fst := by simp only [map₂_right, map₂_right', map₂_left_eq_map₂_left'] theorem map₂_right_eq_map₂ (h : length bs ≤ length as) : map₂_right f as bs = map₂ (λ a b, f (some a) b) as bs := begin have : (λ a b, flip f a (some b)) = (flip (λ a b, f (some a) b)) := rfl, simp only [map₂_right, map₂_left_eq_map₂, map₂_flip, *] end end map₂_right /-! ### zip_left -/ section zip_left variables (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem zip_left_nil_right : zip_left as ([] : list β) = as.map (λ a, (a, none)) := by cases as; refl @[simp] theorem zip_left_nil_left : zip_left ([] : list α) bs = [] := rfl @[simp] theorem zip_left_cons_nil : zip_left (a :: as) ([] : list β) = (a, none) :: as.map (λ a, (a, none)) := rfl @[simp] theorem zip_left_cons_cons : zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs := rfl theorem zip_left_eq_zip_left' : zip_left as bs = (zip_left' as bs).fst := by simp only [zip_left, zip_left', map₂_left_eq_map₂_left'] end zip_left /-! ### zip_right -/ section zip_right variables (a : α) (as : list α) (b : β) (bs : list β) @[simp] theorem zip_right_nil_left : zip_right ([] : list α) bs = bs.map (λ b, (none, b)) := by cases bs; refl @[simp] theorem zip_right_nil_right : zip_right as ([] : list β) = [] := rfl @[simp] theorem zip_right_nil_cons : zip_right ([] : list α) (b :: bs) = (none, b) :: bs.map (λ b, (none, b)) := rfl @[simp] theorem zip_right_cons_cons : zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs := rfl theorem zip_right_eq_zip_right' : zip_right as bs = (zip_right' as bs).fst := by simp only [zip_right, zip_right', map₂_right_eq_map₂_right'] end zip_right /-! ### to_chunks -/ section to_chunks @[simp] theorem to_chunks_nil (n) : @to_chunks α n [] = [] := by cases n; refl theorem to_chunks_aux_eq (n) : ∀ xs i, @to_chunks_aux α n xs i = (xs.take i, (xs.drop i).to_chunks (n+1)) | [] i := by cases i; refl | (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl | (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons' (n) : ∀ {xs : list α} (h : xs ≠ []), xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1) | [] e := (e rfl).elim | (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons : ∀ {n} {xs : list α} (n0 : n ≠ 0) (x0 : xs ≠ []), xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n | 0 _ e := (e rfl).elim | (n+1) xs _ := to_chunks_eq_cons' _ theorem to_chunks_aux_join {n} : ∀ {xs i l L}, @to_chunks_aux α n xs i = (l, L) → l ++ L.join = xs | [] _ _ _ rfl := rfl | (x::xs) i l L e := begin cases i; [ cases e' : to_chunks_aux n xs n with l L, cases e' : to_chunks_aux n xs i with l L]; { rw [to_chunks_aux, e', to_chunks_aux] at e, cases e, exact (congr_arg (cons x) (to_chunks_aux_join e') : _) } end @[simp] theorem to_chunks_join : ∀ n xs, (@to_chunks α n xs).join = xs | n [] := by cases n; refl | 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil | (n+1) (x::xs) := begin rw to_chunks, cases e : to_chunks_aux n xs n with l L, exact (congr_arg (cons x) (to_chunks_aux_join e) : _), end theorem to_chunks_length_le : ∀ n xs, n ≠ 0 → ∀ l : list α, l ∈ @to_chunks α n xs → l.length ≤ n | 0 _ e _ := (e rfl).elim | (n+1) xs _ l := begin refine (measure_wf length).induction xs _, intros xs IH h, by_cases x0 : xs = [], {subst xs, cases h}, rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h, { apply length_take_le }, { refine IH _ _ h, simp only [measure, inv_image, length_drop], exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) }, end end to_chunks /-! ### all₂ -/ section all₂ variables {p q : α → Prop} {l : list α} @[simp] lemma all₂_cons (p : α → Prop) (x : α) : ∀ (l : list α), all₂ p (x :: l) ↔ p x ∧ all₂ p l | [] := (and_true _).symm | (x :: l) := iff.rfl lemma all₂_iff_forall : ∀ {l : list α}, all₂ p l ↔ ∀ x ∈ l, p x | [] := (iff_true_intro $ ball_nil _).symm | (x :: l) := by rw [ball_cons, all₂_cons, all₂_iff_forall] lemma all₂.imp (h : ∀ x, p x → q x) : ∀ {l : list α}, all₂ p l → all₂ q l | [] := id | (x :: l) := by simpa using and.imp (h x) all₂.imp @[simp] lemma all₂_map_iff {p : β → Prop} (f : α → β) : all₂ p (l.map f) ↔ all₂ (p ∘ f) l := by induction l; simp * instance (p : α → Prop) [decidable_pred p] : decidable_pred (all₂ p) := λ l, decidable_of_iff' _ all₂_iff_forall end all₂ /-! ### Retroattributes The list definitions happen earlier than `to_additive`, so here we tag the few multiplicative definitions that couldn't be tagged earlier. -/ attribute [to_additive] list.prod -- `list.sum` attribute [to_additive] alternating_prod -- `list.alternating_sum` /-! ### Miscellaneous lemmas -/ lemma last_reverse {l : list α} (hl : l.reverse ≠ []) (hl' : 0 < l.length := by { contrapose! hl, simpa [length_eq_zero] using hl }) : l.reverse.last hl = l.nth_le 0 hl' := begin rw [last_eq_nth_le, nth_le_reverse'], { simp, }, { simpa using hl' } end theorem ilast'_mem : ∀ a l, @ilast' α a l ∈ a :: l | a [] := or.inl rfl | a (b::l) := or.inr (ilast'_mem b l) @[simp] lemma nth_le_attach (L : list α) (i) (H : i < L.attach.length) : (L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) := calc (L.attach.nth_le i H).1 = (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map' ... = L.nth_le i _ : by congr; apply attach_map_val @[simp] theorem mem_map_swap (x : α) (y : β) (xs : list (α × β)) : (y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs := begin induction xs with x xs, { simp only [not_mem_nil, map_nil] }, { cases x with a b, simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk, prod.exists, xs_ih, and_comm] }, end lemma slice_eq (xs : list α) (n m : ℕ) : slice n m xs = xs.take n ++ xs.drop (n+m) := begin induction n generalizing xs, { simp [slice] }, { cases xs; simp [slice, *, nat.succ_add], } end lemma sizeof_slice_lt [has_sizeof α] (i j : ℕ) (hj : 0 < j) (xs : list α) (hi : i < xs.length) : sizeof (list.slice i j xs) < sizeof xs := begin induction xs generalizing i j, case list.nil : i j h { cases hi }, case list.cons : x xs xs_ih i j h { cases i; simp only [-slice_eq, list.slice], { cases j, cases h, dsimp only [drop], unfold_wf, apply @lt_of_le_of_lt _ _ _ xs.sizeof, { clear_except, induction xs generalizing j; unfold_wf, case list.nil : j { refl }, case list.cons : xs_hd xs_tl xs_ih j { cases j; unfold_wf, refl, transitivity, apply xs_ih, simp }, }, unfold_wf, }, { unfold_wf, apply xs_ih _ _ h, apply lt_of_succ_lt_succ hi, } }, end /-! ### nthd and inth -/ section nthd variables (l : list α) (x : α) (xs : list α) (d : α) (n : ℕ) @[simp] lemma nthd_nil : nthd d [] n = d := rfl @[simp] lemma nthd_cons_zero : nthd d (x::xs) 0 = x := rfl @[simp] lemma nthd_cons_succ : nthd d (x::xs) (n + 1) = nthd d xs n := rfl lemma nthd_eq_nth_le {n : ℕ} (hn : n < l.length) : l.nthd d n = l.nth_le n hn := begin induction l with hd tl IH generalizing n, { exact absurd hn (not_lt_of_ge (nat.zero_le _)) }, { cases n, { exact nthd_cons_zero _ _ _ }, { exact IH _ } } end lemma nthd_eq_default {n : ℕ} (hn : l.length ≤ n) : l.nthd d n = d := begin induction l with hd tl IH generalizing n, { exact nthd_nil _ _ }, { cases n, { refine absurd (nat.zero_lt_succ _) (not_lt_of_ge hn) }, { exact IH (nat.le_of_succ_le_succ hn) } } end /-- An empty list can always be decidably checked for the presence of an element. Not an instance because it would clash with `decidable_eq α`. -/ def decidable_nthd_nil_ne {α} (a : α) : decidable_pred (λ (i : ℕ), nthd a ([] : list α) i ≠ a) := λ i, is_false $ λ H, H (nthd_nil _ _) @[simp] lemma nthd_singleton_default_eq (n : ℕ) : [d].nthd d n = d := by { cases n; simp } @[simp] lemma nthd_repeat_default_eq (r n : ℕ) : (repeat d r).nthd d n = d := begin induction r with r IH generalizing n, { simp }, { cases n; simp [IH] } end lemma nthd_append (l l' : list α) (d : α) (n : ℕ) (h : n < l.length) (h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm ▸ le_self_add)) : (l ++ l').nthd d n = l.nthd d n := by rw [nthd_eq_nth_le _ _ h', nth_le_append h' h, nthd_eq_nth_le] lemma nthd_append_right (l l' : list α) (d : α) (n : ℕ) (h : l.length ≤ n) : (l ++ l').nthd d n = l'.nthd d (n - l.length) := begin cases lt_or_le _ _ with h' h', { rw [nthd_eq_nth_le _ _ h', nth_le_append_right h h', nthd_eq_nth_le] }, { rw [nthd_eq_default _ _ h', nthd_eq_default], rwa [le_tsub_iff_left h, ←length_append] } end lemma nthd_eq_get_or_else_nth (n : ℕ) : l.nthd d n = (l.nth n).get_or_else d := begin cases lt_or_le _ _ with h h, { rw [nthd_eq_nth_le _ _ h, nth_le_nth h, option.get_or_else_some] }, { rw [nthd_eq_default _ _ h, nth_eq_none_iff.mpr h, option.get_or_else_none] } end end nthd section inth variables [inhabited α] (l : list α) (x : α) (xs : list α) (n : ℕ) @[simp] lemma inth_nil : inth ([] : list α) n = default := rfl @[simp] lemma inth_cons_zero : inth (x::xs) 0 = x := rfl @[simp] lemma inth_cons_succ : inth (x::xs) (n + 1) = inth xs n := rfl lemma inth_eq_nth_le {n : ℕ} (hn : n < l.length) : l.inth n = l.nth_le n hn := nthd_eq_nth_le _ _ _ lemma inth_eq_default {n : ℕ} (hn : l.length ≤ n) : l.inth n = default := nthd_eq_default _ _ hn lemma nthd_default_eq_inth : l.nthd default = l.inth := rfl lemma inth_append (l l' : list α) (n : ℕ) (h : n < l.length) (h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm ▸ le_self_add)) : (l ++ l').inth n = l.inth n := nthd_append _ _ _ _ h h' lemma inth_append_right (l l' : list α) (n : ℕ) (h : l.length ≤ n) : (l ++ l').inth n = l'.inth (n - l.length) := nthd_append_right _ _ _ _ h lemma inth_eq_iget_nth (n : ℕ) : l.inth n = (l.nth n).iget := by rw [←nthd_default_eq_inth, nthd_eq_get_or_else_nth, option.get_or_else_default_eq_iget] lemma inth_zero_eq_head : l.inth 0 = l.head := by { cases l; refl, } end inth end list
374b48dc04f930f7c285b5119dbeb871082ad14a
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/analysis/normed_space/exponential.lean
94a5a79ce6262f34d89a6edc81fdcc9070cd14da
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,825
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import algebra.char_p.algebra import analysis.calculus.deriv import analysis.calculus.fderiv_analytic import analysis.specific_limits import data.complex.exponential import analysis.complex.basic import topology.metric_space.cau_seq_filter /-! # Exponential in a Banach algebra In this file, we define `exp 𝕂 𝔸`, the exponential map in a normed algebra `𝔸` over a nondiscrete normed field `𝕂`. Although the definition doesn't require `𝔸` to be complete, we need to assume it for most results. We then prove basic results, as described below. ## Main result We prove most result for an arbitrary field `𝕂`, and then specialize to `𝕂 = ℝ` or `𝕂 = ℂ`. ### General case - `has_strict_fderiv_at_exp_zero_of_radius_pos` : `exp 𝕂 𝔸` has strict Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero, as long as it converges on a neighborhood of zero (see also `has_strict_deriv_at_exp_zero_of_radius_pos` for the case `𝔸 = 𝕂`) - `exp_add_of_commute_of_lt_radius` : if `𝕂` has characteristic zero, then given two commuting elements `x` and `y` in the disk of convergence, we have `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)` - `exp_add_of_lt_radius` : if `𝕂` has characteristic zero and `𝔸` is commutative, then given two elements `x` and `y` in the disk of convergence, we have `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)` - `has_strict_fderiv_at_exp_of_lt_radius` : if `𝕂` has characteristic zero and `𝔸` is commutative, then given a point `x` in the disk of convergence, `exp 𝕂 𝔸` as strict Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at x (see also `has_strict_deriv_at_exp_of_lt_radius` for the case `𝔸 = 𝕂`) ### `𝕂 = ℝ` or `𝕂 = ℂ` - `exp_series_radius_eq_top` : the `formal_multilinear_series` defining `exp 𝕂 𝔸` has infinite radius of convergence - `has_strict_fderiv_at_exp_zero` : `exp 𝕂 𝔸` has strict Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero (see also `has_strict_deriv_at_exp_zero` for the case `𝔸 = 𝕂`) - `exp_add_of_commute` : given two commuting elements `x` and `y`, we have `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)` - `exp_add` : if `𝔸` is commutative, then we have `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)` for any `x` and `y` - `has_strict_fderiv_at_exp` : if `𝔸` is commutative, then given any point `x`, `exp 𝕂 𝔸` as strict Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at x (see also `has_strict_deriv_at_exp` for the case `𝔸 = 𝕂`) ### Other useful compatibility results - `exp_eq_exp_of_field_extension` : given `𝕂' / 𝕂` a normed field extension (that is, an instance of `normed_algebra 𝕂 𝕂'`) and a normed algebra `𝔸` over both `𝕂` and `𝕂'` then `exp 𝕂 𝔸 = exp 𝕂' 𝔸` - `complex.exp_eq_exp_ℂ_ℂ` : `complex.exp = exp ℂ ℂ` - `real.exp_eq_exp_ℝ_ℝ` : `real.exp = exp ℝ ℝ` -/ open filter is_R_or_C continuous_multilinear_map normed_field asymptotics open_locale nat topological_space big_operators ennreal section any_field_any_algebra variables (𝕂 𝔸 : Type*) [nondiscrete_normed_field 𝕂] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] /-- In a Banach algebra `𝔸` over a normed field `𝕂`, `exp_series 𝕂 𝔸` is the `formal_multilinear_series` whose `n`-th term is the map `(xᵢ) : 𝔸ⁿ ↦ (1/n! : 𝕂) • ∏ xᵢ`. Its sum is the exponential map `exp 𝕂 𝔸 : 𝔸 → 𝔸`. -/ def exp_series : formal_multilinear_series 𝕂 𝔸 𝔸 := λ n, (1/n! : 𝕂) • continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸 /-- In a Banach algebra `𝔸` over a normed field `𝕂`, `exp 𝕂 𝔸 : 𝔸 → 𝔸` is the exponential map determined by the action of `𝕂` on `𝔸`. It is defined as the sum of the `formal_multilinear_series` `exp_series 𝕂 𝔸`. -/ noncomputable def exp (x : 𝔸) : 𝔸 := (exp_series 𝕂 𝔸).sum x variables {𝕂 𝔸} lemma exp_series_apply_eq (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = (1 / n! : 𝕂) • x^n := by simp [exp_series] lemma exp_series_apply_eq' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, (1 / n! : 𝕂) • x^n) := funext (exp_series_apply_eq x) lemma exp_series_apply_eq_field (x : 𝕂) (n : ℕ) : exp_series 𝕂 𝕂 n (λ _, x) = x^n / n! := begin rw [div_eq_inv_mul, ←smul_eq_mul, inv_eq_one_div], exact exp_series_apply_eq x n, end lemma exp_series_apply_eq_field' (x : 𝕂) : (λ n, exp_series 𝕂 𝕂 n (λ _, x)) = (λ n, x^n / n!) := funext (exp_series_apply_eq_field x) lemma exp_series_sum_eq (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), (1 / n! : 𝕂) • x^n := tsum_congr (λ n, exp_series_apply_eq x n) lemma exp_series_sum_eq_field (x : 𝕂) : (exp_series 𝕂 𝕂).sum x = ∑' (n : ℕ), x^n / n! := tsum_congr (λ n, exp_series_apply_eq_field x n) lemma exp_eq_tsum : exp 𝕂 𝔸 = (λ x : 𝔸, ∑' (n : ℕ), (1 / n! : 𝕂) • x^n) := funext exp_series_sum_eq lemma exp_eq_tsum_field : exp 𝕂 𝕂 = (λ x : 𝕂, ∑' (n : ℕ), x^n / n!) := funext exp_series_sum_eq_field lemma exp_zero : exp 𝕂 𝔸 0 = 1 := begin suffices : (λ x : 𝔸, ∑' (n : ℕ), (1 / n! : 𝕂) • x^n) 0 = ∑' (n : ℕ), if n = 0 then 1 else 0, { have key : ∀ n ∉ ({0} : finset ℕ), (if n = 0 then (1 : 𝔸) else 0) = 0, from λ n hn, if_neg (finset.not_mem_singleton.mp hn), rw [exp_eq_tsum, this, tsum_eq_sum key, finset.sum_singleton], simp }, refine tsum_congr (λ n, _), split_ifs with h h; simp [h] end lemma norm_exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := (exp_series 𝕂 𝔸).summable_norm_apply hx lemma norm_exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥(1 / n! : 𝕂) • x^n∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq', exact norm_exp_series_summable_of_mem_ball x hx end lemma norm_exp_series_field_summable_of_mem_ball (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : summable (λ n, ∥x^n / n!∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq_field', exact norm_exp_series_summable_of_mem_ball x hx end section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball x hx) lemma exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, (1 / n! : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) lemma exp_series_field_summable_of_mem_ball [complete_space 𝕂] (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_field_summable_of_mem_ball x hx) lemma exp_series_has_sum_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 𝔸 x) := formal_multilinear_series.has_sum (exp_series 𝕂 𝔸) hx lemma exp_series_has_sum_exp_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, (1 / n! : 𝕂) • x^n) (exp 𝕂 𝔸 x):= begin rw ← exp_series_apply_eq', exact exp_series_has_sum_exp_of_mem_ball x hx end lemma exp_series_field_has_sum_exp_of_mem_ball [complete_space 𝕂] (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : has_sum (λ n, x^n / n!) (exp 𝕂 𝕂 x) := begin rw ← exp_series_apply_eq_field', exact exp_series_has_sum_exp_of_mem_ball x hx end lemma has_fpower_series_on_ball_exp_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_on_ball (exp 𝕂 𝔸) (exp_series 𝕂 𝔸) 0 (exp_series 𝕂 𝔸).radius := (exp_series 𝕂 𝔸).has_fpower_series_on_ball h lemma has_fpower_series_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_at (exp 𝕂 𝔸) (exp_series 𝕂 𝔸) 0 := (has_fpower_series_on_ball_exp_of_radius_pos h).has_fpower_series_at lemma continuous_on_exp : continuous_on (exp 𝕂 𝔸) (emetric.ball 0 (exp_series 𝕂 𝔸).radius) := formal_multilinear_series.continuous_on lemma analytic_at_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : analytic_at 𝕂 (exp 𝕂 𝔸) x:= begin by_cases h : (exp_series 𝕂 𝔸).radius = 0, { rw h at hx, exact (ennreal.not_lt_zero hx).elim }, { have h := pos_iff_ne_zero.mpr h, exact (has_fpower_series_on_ball_exp_of_radius_pos h).analytic_at_of_mem hx } end /-- The exponential in a Banach-algebra `𝔸` over a normed field `𝕂` has strict Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero, as long as it converges on a neighborhood of zero. -/ lemma has_strict_fderiv_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_strict_fderiv_at (exp 𝕂 𝔸) (1 : 𝔸 →L[𝕂] 𝔸) 0 := begin convert (has_fpower_series_at_exp_zero_of_radius_pos h).has_strict_fderiv_at, ext x, change x = exp_series 𝕂 𝔸 1 (λ _, x), simp [exp_series_apply_eq] end /-- The exponential in a Banach-algebra `𝔸` over a normed field `𝕂` has Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero, as long as it converges on a neighborhood of zero. -/ lemma has_fderiv_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fderiv_at (exp 𝕂 𝔸) (1 : 𝔸 →L[𝕂] 𝔸) 0 := (has_strict_fderiv_at_exp_zero_of_radius_pos h).has_fderiv_at /-- In a Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, if `x` and `y` are in the disk of convergence and commute, then `exp 𝕂 𝔸 (x + y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)`. -/ lemma exp_add_of_commute_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hxy : commute x y) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 𝔸 (x + y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y) := begin rw [exp_eq_tsum, tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) (norm_exp_series_summable_of_mem_ball' y hy)], dsimp only, conv_lhs {congr, funext, rw [hxy.add_pow' _, finset.smul_sum]}, refine tsum_congr (λ n, finset.sum_congr rfl $ λ kl hkl, _), rw [nsmul_eq_smul_cast 𝕂, smul_smul, smul_mul_smul, ← (finset.nat.mem_antidiagonal.mp hkl), nat.cast_add_choose, (finset.nat.mem_antidiagonal.mp hkl)], congr' 1, have : (n! : 𝕂) ≠ 0 := nat.cast_ne_zero.mpr n.factorial_ne_zero, field_simp [this] end end complete_algebra end any_field_any_algebra section any_field_comm_algebra variables {𝕂 𝔸 : Type*} [nondiscrete_normed_field 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)` for all `x`, `y` in the disk of convergence. -/ lemma exp_add_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 𝔸 (x + y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y) := exp_add_of_commute_of_mem_ball (commute.all x y) hx hy /-- The exponential map in a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero has Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at any point `x` in the disk of convergence. -/ lemma has_fderiv_at_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_fderiv_at (exp 𝕂 𝔸) (exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸) x := begin have hpos : 0 < (exp_series 𝕂 𝔸).radius := (zero_le _).trans_lt hx, rw has_fderiv_at_iff_is_o_nhds_zero, suffices : (λ h, exp 𝕂 𝔸 x * (exp 𝕂 𝔸 (0 + h) - exp 𝕂 𝔸 0 - continuous_linear_map.id 𝕂 𝔸 h)) =ᶠ[𝓝 0] (λ h, exp 𝕂 𝔸 (x + h) - exp 𝕂 𝔸 x - exp 𝕂 𝔸 x • continuous_linear_map.id 𝕂 𝔸 h), { refine (is_o.const_mul_left _ _).congr' this (eventually_eq.refl _ _), rw ← has_fderiv_at_iff_is_o_nhds_zero, exact has_fderiv_at_exp_zero_of_radius_pos hpos }, have : ∀ᶠ h in 𝓝 (0 : 𝔸), h ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius := emetric.ball_mem_nhds _ hpos, filter_upwards [this], intros h hh, rw [exp_add_of_mem_ball hx hh, exp_zero, zero_add, continuous_linear_map.id_apply, smul_eq_mul], ring end /-- The exponential map in a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero has strict Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at any point `x` in the disk of convergence. -/ lemma has_strict_fderiv_at_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_strict_fderiv_at (exp 𝕂 𝔸) (exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸) x := let ⟨p, hp⟩ := analytic_at_exp_of_mem_ball x hx in hp.has_fderiv_at.unique (has_fderiv_at_exp_of_mem_ball hx) ▸ hp.has_strict_fderiv_at end any_field_comm_algebra section deriv variables {𝕂 : Type*} [nondiscrete_normed_field 𝕂] [complete_space 𝕂] /-- The exponential map in a complete normed field `𝕂` of characteristic zero has strict derivative `exp 𝕂 𝕂 x` at any point `x` in the disk of convergence. -/ lemma has_strict_deriv_at_exp_of_mem_ball [char_zero 𝕂] {x : 𝕂} (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : has_strict_deriv_at (exp 𝕂 𝕂) (exp 𝕂 𝕂 x) x := by simpa using (has_strict_fderiv_at_exp_of_mem_ball hx).has_strict_deriv_at /-- The exponential map in a complete normed field `𝕂` of characteristic zero has derivative `exp 𝕂 𝕂 x` at any point `x` in the disk of convergence. -/ lemma has_deriv_at_exp_of_mem_ball [char_zero 𝕂] {x : 𝕂} (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : has_deriv_at (exp 𝕂 𝕂) (exp 𝕂 𝕂 x) x := (has_strict_deriv_at_exp_of_mem_ball hx).has_deriv_at /-- The exponential map in a complete normed field `𝕂` of characteristic zero has strict derivative `1` at zero, as long as it converges on a neighborhood of zero. -/ lemma has_strict_deriv_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝕂).radius) : has_strict_deriv_at (exp 𝕂 𝕂) 1 0 := (has_strict_fderiv_at_exp_zero_of_radius_pos h).has_strict_deriv_at /-- The exponential map in a complete normed field `𝕂` of characteristic zero has derivative `1` at zero, as long as it converges on a neighborhood of zero. -/ lemma has_deriv_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝕂).radius) : has_deriv_at (exp 𝕂 𝕂) 1 0 := (has_strict_deriv_at_exp_zero_of_radius_pos h).has_deriv_at end deriv section is_R_or_C section any_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] -- This is private because one can use the more general `exp_series_summable_field` intead. private lemma real.summable_pow_div_factorial (x : ℝ) : summable (λ n : ℕ, x^n / n!) := begin by_cases h : x = 0, { refine summable_of_norm_bounded_eventually 0 summable_zero _, filter_upwards [eventually_cofinite_ne 0], intros n hn, rw [h, zero_pow' n hn, zero_div, norm_zero], exact le_refl _ }, { refine summable_of_ratio_test_tendsto_lt_one zero_lt_one (eventually_of_forall $ λ n, div_ne_zero (pow_ne_zero n h) (nat.cast_ne_zero.mpr n.factorial_ne_zero)) _, suffices : ∀ n : ℕ, ∥x^(n+1) / (n+1)!∥ / ∥x^n / n!∥ = ∥x∥ / ∥((n+1 : ℕ) : ℝ)∥, { conv {congr, funext, rw [this, real.norm_coe_nat] }, exact (tendsto_const_div_at_top_nhds_0_nat _).comp (tendsto_add_at_top_nat 1) }, intro n, calc ∥x^(n+1) / (n+1)!∥ / ∥x^n / n!∥ = (∥x∥^n * ∥x∥) * (∥(n! : ℝ)∥⁻¹ * ∥((n+1 : ℕ) : ℝ)∥⁻¹) * ((∥x∥^n)⁻¹ * ∥(n! : ℝ)∥) : by rw [ normed_field.norm_div, normed_field.norm_div, normed_field.norm_pow, normed_field.norm_pow, pow_add, pow_one, div_eq_mul_inv, div_eq_mul_inv, div_eq_mul_inv, mul_inv₀, inv_inv₀, nat.factorial_succ, nat.cast_mul, normed_field.norm_mul, mul_inv_rev₀ ] ... = (∥x∥ * ∥((n+1 : ℕ) : ℝ)∥⁻¹) * (∥x∥^n * (∥x∥^n)⁻¹) * (∥(n! : ℝ)∥ * ∥(n! : ℝ)∥⁻¹) : by linarith --faster than ac_refl ! ... = (∥x∥ * ∥((n+1 : ℕ) : ℝ)∥⁻¹) * 1 * 1 : by rw [mul_inv_cancel (pow_ne_zero _ $ λ h', h $ norm_eq_zero.mp h'), mul_inv_cancel (λ h', n.factorial_ne_zero $ nat.cast_eq_zero.mp $ norm_eq_zero.mp h')]; apply_instance ... = ∥x∥ / ∥((n+1 : ℕ) : ℝ)∥ : by rw [mul_one, mul_one, ← div_eq_mul_inv] } end variables (𝕂 𝔸) /-- In a normed algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, the series defining the exponential map has an infinite radius of convergence. -/ lemma exp_series_radius_eq_top : (exp_series 𝕂 𝔸).radius = ∞ := begin refine (exp_series 𝕂 𝔸).radius_eq_top_of_summable_norm (λ r, _), refine summable_of_norm_bounded_eventually _ (real.summable_pow_div_factorial r) _, filter_upwards [eventually_cofinite_ne 0], intros n hn, rw [norm_mul, norm_norm (exp_series 𝕂 𝔸 n), exp_series, norm_smul, norm_div, norm_one, norm_pow, nnreal.norm_eq, norm_eq_abs, abs_cast_nat, mul_comm, ←mul_assoc, ←mul_div_assoc, mul_one], have : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸∥ ≤ 1 := norm_mk_pi_algebra_fin_le_of_pos (nat.pos_of_ne_zero hn), exact mul_le_of_le_one_right (div_nonneg (pow_nonneg r.coe_nonneg n) n!.cast_nonneg) this end lemma exp_series_radius_pos : 0 < (exp_series 𝕂 𝔸).radius := begin rw exp_series_radius_eq_top, exact with_top.zero_lt_top end variables {𝕂 𝔸} section complete_algebra lemma norm_exp_series_summable (x : 𝔸) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := norm_exp_series_summable_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma norm_exp_series_summable' (x : 𝔸) : summable (λ n, ∥(1 / n! : 𝕂) • x^n∥) := norm_exp_series_summable_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma norm_exp_series_field_summable (x : 𝕂) : summable (λ n, ∥x^n / n!∥) := norm_exp_series_field_summable_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _) variables [complete_space 𝔸] lemma exp_series_summable (x : 𝔸) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable x) lemma exp_series_summable' (x : 𝔸) : summable (λ n, (1 / n! : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable' x) lemma exp_series_field_summable (x : 𝕂) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_field_summable x) lemma exp_series_has_sum_exp (x : 𝔸) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 𝔸 x) := exp_series_has_sum_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_series_has_sum_exp' (x : 𝔸) : has_sum (λ n, (1 / n! : 𝕂) • x^n) (exp 𝕂 𝔸 x):= exp_series_has_sum_exp_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_series_field_has_sum_exp (x : 𝕂) : has_sum (λ n, x^n / n!) (exp 𝕂 𝕂 x):= exp_series_field_has_sum_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _) lemma exp_has_fpower_series_on_ball : has_fpower_series_on_ball (exp 𝕂 𝔸) (exp_series 𝕂 𝔸) 0 ∞ := exp_series_radius_eq_top 𝕂 𝔸 ▸ has_fpower_series_on_ball_exp_of_radius_pos (exp_series_radius_pos _ _) lemma exp_has_fpower_series_at_zero : has_fpower_series_at (exp 𝕂 𝔸) (exp_series 𝕂 𝔸) 0 := exp_has_fpower_series_on_ball.has_fpower_series_at lemma exp_continuous : continuous (exp 𝕂 𝔸) := begin rw [continuous_iff_continuous_on_univ, ← metric.eball_top_eq_univ (0 : 𝔸), ← exp_series_radius_eq_top 𝕂 𝔸], exact continuous_on_exp end lemma exp_analytic (x : 𝔸) : analytic_at 𝕂 (exp 𝕂 𝔸) x := analytic_at_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- The exponential in a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ` has strict Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero. -/ lemma has_strict_fderiv_at_exp_zero : has_strict_fderiv_at (exp 𝕂 𝔸) (1 : 𝔸 →L[𝕂] 𝔸) 0 := has_strict_fderiv_at_exp_zero_of_radius_pos (exp_series_radius_pos 𝕂 𝔸) /-- The exponential in a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ` has Fréchet-derivative `1 : 𝔸 →L[𝕂] 𝔸` at zero. -/ lemma has_fderiv_at_exp_zero : has_fderiv_at (exp 𝕂 𝔸) (1 : 𝔸 →L[𝕂] 𝔸) 0 := has_strict_fderiv_at_exp_zero.has_fderiv_at end complete_algebra local attribute [instance] char_zero_R_or_C /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if `x` and `y` commute, then `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)`. -/ lemma exp_add_of_commute [complete_space 𝔸] {x y : 𝔸} (hxy : commute x y) : exp 𝕂 𝔸 (x + y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y) := exp_add_of_commute_of_mem_ball hxy ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) end any_algebra section comm_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] local attribute [instance] char_zero_R_or_C /-- In a comutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, `exp 𝕂 𝔸 (x+y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y)`. -/ lemma exp_add {x y : 𝔸} : exp 𝕂 𝔸 (x + y) = (exp 𝕂 𝔸 x) * (exp 𝕂 𝔸 y) := exp_add_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- The exponential map in a commutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ` has strict Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at any point `x`. -/ lemma has_strict_fderiv_at_exp {x : 𝔸} : has_strict_fderiv_at (exp 𝕂 𝔸) (exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸) x := has_strict_fderiv_at_exp_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- The exponential map in a commutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ` has Fréchet-derivative `exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸` at any point `x`. -/ lemma has_fderiv_at_exp {x : 𝔸} : has_fderiv_at (exp 𝕂 𝔸) (exp 𝕂 𝔸 x • 1 : 𝔸 →L[𝕂] 𝔸) x := has_strict_fderiv_at_exp.has_fderiv_at end comm_algebra section deriv variables {𝕂 : Type*} [is_R_or_C 𝕂] local attribute [instance] char_zero_R_or_C /-- The exponential map in `𝕂 = ℝ` or `𝕂 = ℂ` has strict derivative `exp 𝕂 𝕂 x` at any point `x`. -/ lemma has_strict_deriv_at_exp {x : 𝕂} : has_strict_deriv_at (exp 𝕂 𝕂) (exp 𝕂 𝕂 x) x := has_strict_deriv_at_exp_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _) /-- The exponential map in `𝕂 = ℝ` or `𝕂 = ℂ` has derivative `exp 𝕂 𝕂 x` at any point `x`. -/ lemma has_deriv_at_exp {x : 𝕂} : has_deriv_at (exp 𝕂 𝕂) (exp 𝕂 𝕂 x) x := has_strict_deriv_at_exp.has_deriv_at /-- The exponential map in `𝕂 = ℝ` or `𝕂 = ℂ` has strict derivative `1` at zero. -/ lemma has_strict_deriv_at_exp_zero : has_strict_deriv_at (exp 𝕂 𝕂) 1 0 := has_strict_deriv_at_exp_zero_of_radius_pos (exp_series_radius_pos 𝕂 𝕂) /-- The exponential map in `𝕂 = ℝ` or `𝕂 = ℂ` has derivative `1` at zero. -/ lemma has_deriv_at_exp_zero : has_deriv_at (exp 𝕂 𝕂) 1 0 := has_strict_deriv_at_exp_zero.has_deriv_at end deriv end is_R_or_C section scalar_tower variables (𝕂 𝕂' 𝔸 : Type*) [nondiscrete_normed_field 𝕂] [nondiscrete_normed_field 𝕂'] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] [normed_algebra 𝕂 𝕂'] [normed_algebra 𝕂' 𝔸] [is_scalar_tower 𝕂 𝕂' 𝔸] (p : ℕ) [char_p 𝕂 p] include p lemma exp_series_eq_exp_series_of_field_extension (n : ℕ) (x : 𝔸) : (exp_series 𝕂 𝔸 n (λ _, x)) = (exp_series 𝕂' 𝔸 n (λ _, x)) := begin haveI : char_p 𝕂' p := char_p_of_injective_algebra_map (algebra_map 𝕂 𝕂').injective p, rw [exp_series, exp_series, smul_apply, mk_pi_algebra_fin_apply, list.of_fn_const, list.prod_repeat, smul_apply, mk_pi_algebra_fin_apply, list.of_fn_const, list.prod_repeat, ←inv_eq_one_div, ←inv_eq_one_div, ← smul_one_smul 𝕂' (_ : 𝕂) (_ : 𝔸)], congr, symmetry, have key : (n! : 𝕂) = 0 ↔ (n! : 𝕂') = 0, { rw [char_p.cast_eq_zero_iff 𝕂' p, char_p.cast_eq_zero_iff 𝕂 p] }, by_cases h : (n! : 𝕂) = 0, { have h' : (n! : 𝕂') = 0 := key.mp h, field_simp [h, h'] }, { have h' : (n! : 𝕂') ≠ 0 := λ hyp, h (key.mpr hyp), suffices : (n! : 𝕂) • (n!⁻¹ : 𝕂') = (n! : 𝕂) • ((n!⁻¹ : 𝕂) • 1), { apply_fun (λ (x : 𝕂'), (n!⁻¹ : 𝕂) • x) at this, rwa [inv_smul_smul₀ h, inv_smul_smul₀ h] at this }, rw [← smul_assoc, ← nsmul_eq_smul_cast, nsmul_eq_smul_cast 𝕂' _ (_ : 𝕂')], field_simp [h, h'] } end /-- Given `𝕂' / 𝕂` a normed field extension (that is, an instance of `normed_algebra 𝕂 𝕂'`) and a normed algebra `𝔸` over both `𝕂` and `𝕂'` then `exp 𝕂 𝔸 = exp 𝕂' 𝔸`. -/ lemma exp_eq_exp_of_field_extension : exp 𝕂 𝔸 = exp 𝕂' 𝔸 := begin ext, rw [exp, exp], refine tsum_congr (λ n, _), rw exp_series_eq_exp_series_of_field_extension 𝕂 𝕂' 𝔸 p n x end end scalar_tower section complex lemma complex.exp_eq_exp_ℂ_ℂ : complex.exp = exp ℂ ℂ := begin refine funext (λ x, _), rw [complex.exp, exp_eq_tsum_field], exact tendsto_nhds_unique x.exp'.tendsto_limit (exp_series_field_summable x).has_sum.tendsto_sum_nat end lemma exp_ℝ_ℂ_eq_exp_ℂ_ℂ : exp ℝ ℂ = exp ℂ ℂ := exp_eq_exp_of_field_extension ℝ ℂ ℂ 0 end complex section real lemma real.exp_eq_exp_ℝ_ℝ : real.exp = exp ℝ ℝ := begin refine funext (λ x, _), rw [real.exp, complex.exp_eq_exp_ℂ_ℂ, ← exp_ℝ_ℂ_eq_exp_ℂ_ℂ, exp_eq_tsum, exp_eq_tsum_field, ← re_to_complex, ← re_clm_apply, re_clm.map_tsum (exp_series_summable' (x : ℂ))], refine tsum_congr (λ n, _), rw [re_clm.map_smul, ← complex.of_real_pow, re_clm_apply, re_to_complex, complex.of_real_re, smul_eq_mul, one_div, mul_comm, div_eq_mul_inv] end end real
97fe3e9d73e53fd3571aa7dbb98449bea07278e5
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/quadratic_form.lean
45bc2716da42bea49aff48cadc14c007330a43eb
[]
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
25,043
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.invertible import Mathlib.linear_algebra.bilinear_form import Mathlib.linear_algebra.determinant import Mathlib.linear_algebra.special_linear_group import Mathlib.PostPort universes u v l u_1 w u_2 u_3 namespace Mathlib /-! # Quadratic forms This file defines quadratic forms over a `R`-module `M`. A quadratic form is a map `Q : M → R` such that (`to_fun_smul`) `Q (a • x) = a * a * Q x` (`polar_...`) The map `polar Q := λ x y, Q (x + y) - Q x - Q y` is bilinear. They come with a scalar multiplication, `(a • Q) x = Q (a • x) = a * a * Q x`, and composition with linear maps `f`, `Q.comp f x = Q (f x)`. ## Main definitions * `quadratic_form.associated`: associated bilinear form * `quadratic_form.pos_def`: positive definite quadratic forms * `quadratic_form.anisotropic`: anisotropic quadratic forms * `quadratic_form.discr`: discriminant of a quadratic form ## Main statements * `quadratic_form.associated_left_inverse`, * `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has an inverse, there is a correspondence between quadratic forms and symmetric bilinear forms ## Notation In this file, the variable `R` is used when a `ring` structure is sufficient and `R₁` is used when specifically a `comm_ring` is required. This allows us to keep `[module R M]` and `[module R₁ M]` assumptions in the variables without confusion between `*` from `ring` and `*` from `comm_ring`. ## References * https://en.wikipedia.org/wiki/Quadratic_form * https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms ## Tags quadratic form, homogeneous polynomial, quadratic polynomial -/ namespace quadratic_form /-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`.d Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization -/ def polar {R : Type u} {M : Type v} [add_comm_group M] [ring R] (f : M → R) (x : M) (y : M) : R := f (x + y) - f x - f y theorem polar_add {R : Type u} {M : Type v} [add_comm_group M] [ring R] (f : M → R) (g : M → R) (x : M) (y : M) : polar (f + g) x y = polar f x y + polar g x y := sorry theorem polar_neg {R : Type u} {M : Type v} [add_comm_group M] [ring R] (f : M → R) (x : M) (y : M) : polar (-f) x y = -polar f x y := sorry theorem polar_smul {R : Type u} {M : Type v} [add_comm_group M] [ring R] (f : M → R) (s : R) (x : M) (y : M) : polar (s • f) x y = s * polar f x y := sorry theorem polar_comm {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] (f : M → R₁) (x : M) (y : M) : polar f x y = polar f y x := sorry end quadratic_form /-- A quadratic form over a module. -/ structure quadratic_form (R : Type u) (M : Type v) [ring R] [add_comm_group M] [module R M] extends quadratic_form.to_fun #1 #0 R M _inst_6 _inst_7 (_inst_8 • to_fun) = _inst_8 * _inst_8 * quadratic_form.to_fun #1 #0 R M _inst_6 _inst_7 to_fun where to_fun : M → R to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x polar_add_left' : ∀ (x x' y : M), quadratic_form.polar to_fun (x + x') y = quadratic_form.polar to_fun x y + quadratic_form.polar to_fun x' y polar_smul_left' : ∀ (a : R) (x y : M), quadratic_form.polar to_fun (a • x) y = a • quadratic_form.polar to_fun x y polar_add_right' : ∀ (x y y' : M), quadratic_form.polar to_fun x (y + y') = quadratic_form.polar to_fun x y + quadratic_form.polar to_fun x y' polar_smul_right' : ∀ (a : R) (x y : M), quadratic_form.polar to_fun x (a • y) = a • quadratic_form.polar to_fun x y namespace quadratic_form protected instance has_coe_to_fun {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : has_coe_to_fun (quadratic_form R M) := has_coe_to_fun.mk (fun (B : quadratic_form R M) => M → R) fun (B : quadratic_form R M) => to_fun B /-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/ @[simp] theorem to_fun_eq_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} : to_fun Q = ⇑Q := rfl @[simp] theorem polar_add_left {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (x' : M) (y : M) : polar (⇑Q) (x + x') y = polar (⇑Q) x y + polar (⇑Q) x' y := polar_add_left' Q x x' y @[simp] theorem polar_smul_left {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (a : R) (x : M) (y : M) : polar (⇑Q) (a • x) y = a * polar (⇑Q) x y := polar_smul_left' Q a x y @[simp] theorem polar_neg_left {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (y : M) : polar (⇑Q) (-x) y = -polar (⇑Q) x y := sorry @[simp] theorem polar_sub_left {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (x' : M) (y : M) : polar (⇑Q) (x - x') y = polar (⇑Q) x y - polar (⇑Q) x' y := sorry @[simp] theorem polar_add_right {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (y : M) (y' : M) : polar (⇑Q) x (y + y') = polar (⇑Q) x y + polar (⇑Q) x y' := polar_add_right' Q x y y' @[simp] theorem polar_smul_right {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (a : R) (x : M) (y : M) : polar (⇑Q) x (a • y) = a * polar (⇑Q) x y := polar_smul_right' Q a x y @[simp] theorem polar_neg_right {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (y : M) : polar (⇑Q) x (-y) = -polar (⇑Q) x y := sorry @[simp] theorem polar_sub_right {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (y : M) (y' : M) : polar (⇑Q) x (y - y') = polar (⇑Q) x y - polar (⇑Q) x y' := sorry theorem map_smul {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (a : R) (x : M) : coe_fn Q (a • x) = a * a * coe_fn Q x := to_fun_smul Q a x theorem map_add_self {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) : coe_fn Q (x + x) = bit0 (bit0 1) * coe_fn Q x := sorry @[simp] theorem map_zero {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} : coe_fn Q 0 = 0 := sorry @[simp] theorem map_neg {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) : coe_fn Q (-x) = coe_fn Q x := sorry theorem map_sub {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} (x : M) (y : M) : coe_fn Q (x - y) = coe_fn Q (y - x) := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn Q (x - y) = coe_fn Q (y - x))) (Eq.symm (neg_sub y x)))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn Q (-(y - x)) = coe_fn Q (y - x))) (map_neg (y - x)))) (Eq.refl (coe_fn Q (y - x)))) theorem ext {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {Q : quadratic_form R M} {Q' : quadratic_form R M} (H : ∀ (x : M), coe_fn Q x = coe_fn Q' x) : Q = Q' := sorry protected instance has_zero {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : HasZero (quadratic_form R M) := { zero := mk (fun (x : M) => 0) sorry sorry sorry sorry sorry } @[simp] theorem zero_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (x : M) : coe_fn 0 x = 0 := rfl protected instance inhabited {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : Inhabited (quadratic_form R M) := { default := 0 } protected instance has_add {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : Add (quadratic_form R M) := { add := fun (Q Q' : quadratic_form R M) => mk (⇑Q + ⇑Q') sorry sorry sorry sorry sorry } @[simp] theorem coe_fn_add {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) (Q' : quadratic_form R M) : ⇑(Q + Q') = ⇑Q + ⇑Q' := rfl @[simp] theorem add_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) (Q' : quadratic_form R M) (x : M) : coe_fn (Q + Q') x = coe_fn Q x + coe_fn Q' x := rfl protected instance has_neg {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : Neg (quadratic_form R M) := { neg := fun (Q : quadratic_form R M) => mk (-⇑Q) sorry sorry sorry sorry sorry } @[simp] theorem coe_fn_neg {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) : ⇑(-Q) = -⇑Q := rfl @[simp] theorem neg_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) (x : M) : coe_fn (-Q) x = -coe_fn Q x := rfl protected instance has_scalar {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] : has_scalar R₁ (quadratic_form R₁ M) := has_scalar.mk fun (a : R₁) (Q : quadratic_form R₁ M) => mk (a • ⇑Q) sorry sorry sorry sorry sorry @[simp] theorem coe_fn_smul {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (a : R₁) (Q : quadratic_form R₁ M) : ⇑(a • Q) = a • ⇑Q := rfl @[simp] theorem smul_apply {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (a : R₁) (Q : quadratic_form R₁ M) (x : M) : coe_fn (a • Q) x = a * coe_fn Q x := rfl protected instance add_comm_group {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] : add_comm_group (quadratic_form R M) := add_comm_group.mk Add.add sorry 0 sorry sorry Neg.neg (add_group.sub._default Add.add sorry 0 sorry sorry Neg.neg) sorry sorry protected instance module {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] : module R₁ (quadratic_form R₁ M) := semimodule.mk sorry sorry /-- Compose the quadratic form with a linear function. -/ def comp {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {N : Type v} [add_comm_group N] [module R N] (Q : quadratic_form R N) (f : linear_map R M N) : quadratic_form R M := mk (fun (x : M) => coe_fn Q (coe_fn f x)) sorry sorry sorry sorry sorry @[simp] theorem comp_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {N : Type v} [add_comm_group N] [module R N] (Q : quadratic_form R N) (f : linear_map R M N) (x : M) : coe_fn (comp Q f) x = coe_fn Q (coe_fn f x) := rfl /-- Create a quadratic form in a commutative ring by proving only one side of the bilinearity. -/ def mk_left {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (f : M → R₁) (to_fun_smul : ∀ (a : R₁) (x : M), f (a • x) = a * a * f x) (polar_add_left : ∀ (x x' y : M), polar f (x + x') y = polar f x y + polar f x' y) (polar_smul_left : ∀ (a : R₁) (x y : M), polar f (a • x) y = a * polar f x y) : quadratic_form R₁ M := mk f to_fun_smul polar_add_left polar_smul_left sorry sorry /-- The product of linear forms is a quadratic form. -/ def lin_mul_lin {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) : quadratic_form R₁ M := mk_left (⇑f * ⇑g) sorry sorry sorry @[simp] theorem lin_mul_lin_apply {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) (x : M) : coe_fn (lin_mul_lin f g) x = coe_fn f x * coe_fn g x := rfl @[simp] theorem add_lin_mul_lin {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) (h : linear_map R₁ M R₁) : lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h := ext fun (x : M) => add_mul (coe_fn f x) (coe_fn g x) (coe_fn h x) @[simp] theorem lin_mul_lin_add {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) (h : linear_map R₁ M R₁) : lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h := ext fun (x : M) => mul_add (coe_fn f x) (coe_fn g x) (coe_fn h x) @[simp] theorem lin_mul_lin_comp {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] {N : Type v} [add_comm_group N] [module R₁ N] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) (h : linear_map R₁ N M) : comp (lin_mul_lin f g) h = lin_mul_lin (linear_map.comp f h) (linear_map.comp g h) := rfl /-- `proj i j` is the quadratic form mapping the vector `x : n → R₁` to `x i * x j` -/ def proj {R₁ : Type u} [comm_ring R₁] {n : Type u_1} (i : n) (j : n) : quadratic_form R₁ (n → R₁) := lin_mul_lin (linear_map.proj i) (linear_map.proj j) @[simp] theorem proj_apply {R₁ : Type u} [comm_ring R₁] {n : Type u_1} (i : n) (j : n) (x : n → R₁) : coe_fn (proj i j) x = x i * x j := rfl end quadratic_form /-! ### Associated bilinear forms Over a commutative ring with an inverse of 2, the theory of quadratic forms is basically identical to that of symmetric bilinear forms. The map from quadratic forms to bilinear forms giving this identification is called the `associated` quadratic form. -/ namespace bilin_form theorem polar_to_quadratic_form {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] {B : bilin_form R M} (x : M) (y : M) : quadratic_form.polar (fun (x : M) => coe_fn B x x) x y = coe_fn B x y + coe_fn B y x := sorry /-- A bilinear form gives a quadratic form by applying the argument twice. -/ def to_quadratic_form {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (B : bilin_form R M) : quadratic_form R M := quadratic_form.mk (fun (x : M) => coe_fn B x x) sorry sorry sorry sorry sorry @[simp] theorem to_quadratic_form_apply {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (B : bilin_form R M) (x : M) : coe_fn (to_quadratic_form B) x = coe_fn B x x := rfl end bilin_form namespace quadratic_form /-- `associated` is the linear map that sends a quadratic form to its associated symmetric bilinear form -/ def associated {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] : linear_map R₁ (quadratic_form R₁ M) (bilin_form R₁ M) := linear_map.mk (fun (Q : quadratic_form R₁ M) => bilin_form.mk (fun (x y : M) => ⅟ * polar (⇑Q) x y) sorry sorry sorry sorry) sorry sorry @[simp] theorem associated_apply {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] {Q : quadratic_form R₁ M} (x : M) (y : M) : coe_fn (coe_fn associated Q) x y = ⅟ * (coe_fn Q (x + y) - coe_fn Q x - coe_fn Q y) := rfl theorem associated_is_sym {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] {Q : quadratic_form R₁ M} : sym_bilin_form.is_sym (coe_fn associated Q) := sorry @[simp] theorem associated_comp {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] {Q : quadratic_form R₁ M} {N : Type v} [add_comm_group N] [module R₁ N] (f : linear_map R₁ N M) : coe_fn associated (comp Q f) = bilin_form.comp (coe_fn associated Q) f f := sorry @[simp] theorem associated_lin_mul_lin {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] (f : linear_map R₁ M R₁) (g : linear_map R₁ M R₁) : coe_fn associated (lin_mul_lin f g) = ⅟ • (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) := sorry theorem associated_to_quadratic_form {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] (B : bilin_form R₁ M) (x : M) (y : M) : coe_fn (coe_fn associated (bilin_form.to_quadratic_form B)) x y = ⅟ * (coe_fn B x y + coe_fn B y x) := sorry theorem associated_left_inverse {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] {B₁ : bilin_form R₁ M} (h : sym_bilin_form.is_sym B₁) : coe_fn associated (bilin_form.to_quadratic_form B₁) = B₁ := sorry theorem associated_right_inverse {M : Type v} [add_comm_group M] {R₁ : Type u} [comm_ring R₁] [module R₁ M] [invertible (bit0 1)] {Q : quadratic_form R₁ M} : bilin_form.to_quadratic_form (coe_fn associated Q) = Q := sorry /-- An anisotropic quadratic form is zero only on zero vectors. -/ def anisotropic {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) := ∀ (x : M), coe_fn Q x = 0 → x = 0 theorem not_anisotropic_iff_exists {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) : ¬anisotropic Q ↔ ∃ (x : M), ∃ (H : x ≠ 0), coe_fn Q x = 0 := sorry /-- A positive definite quadratic form is positive on nonzero vectors. -/ def pos_def {M : Type v} [add_comm_group M] {R₂ : Type u} [ordered_ring R₂] [module R₂ M] (Q₂ : quadratic_form R₂ M) := ∀ (x : M), x ≠ 0 → 0 < coe_fn Q₂ x theorem pos_def.smul {M : Type v} [add_comm_group M] {R : Type u_1} [linear_ordered_comm_ring R] [module R M] {Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a • Q) := fun (x : M) (hx : x ≠ 0) => mul_pos a_pos (h x hx) theorem pos_def.add {M : Type v} [add_comm_group M] {R₂ : Type u} [ordered_ring R₂] [module R₂ M] (Q : quadratic_form R₂ M) (Q' : quadratic_form R₂ M) (hQ : pos_def Q) (hQ' : pos_def Q') : pos_def (Q + Q') := fun (x : M) (hx : x ≠ 0) => add_pos (hQ x hx) (hQ' x hx) theorem lin_mul_lin_self_pos_def {M : Type v} [add_comm_group M] {R : Type u_1} [linear_ordered_comm_ring R] [module R M] (f : linear_map R M R) (hf : linear_map.ker f = ⊥) : pos_def (lin_mul_lin f f) := sorry end quadratic_form /-! ### Quadratic forms and matrices Connect quadratic forms and matrices, in order to explicitly compute with them. The convention is twos out, so there might be a factor 2⁻¹ in the entries of the matrix. The determinant of the matrix is the discriminant of the quadratic form. -/ /-- `M.to_quadratic_form` is the map `λ x, col x ⬝ M ⬝ row x` as a quadratic form. -/ def matrix.to_quadratic_form' {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] (M : matrix n n R₁) : quadratic_form R₁ (n → R₁) := bilin_form.to_quadratic_form (coe_fn matrix.to_bilin' M) /-- A matrix representation of the quadratic form. -/ def quadratic_form.to_matrix' {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] (Q : quadratic_form R₁ (n → R₁)) : matrix n n R₁ := coe_fn bilin_form.to_matrix' (coe_fn quadratic_form.associated Q) theorem quadratic_form.to_matrix'_smul {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] (a : R₁) (Q : quadratic_form R₁ (n → R₁)) : quadratic_form.to_matrix' (a • Q) = a • quadratic_form.to_matrix' Q := sorry namespace quadratic_form @[simp] theorem to_matrix'_comp {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] {m : Type w} [DecidableEq m] [fintype m] (Q : quadratic_form R₁ (m → R₁)) (f : linear_map R₁ (n → R₁) (m → R₁)) : to_matrix' (comp Q f) = matrix.mul (matrix.mul (matrix.transpose (coe_fn linear_map.to_matrix' f)) (to_matrix' Q)) (coe_fn linear_map.to_matrix' f) := sorry /-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/ def discr {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] (Q : quadratic_form R₁ (n → R₁)) : R₁ := matrix.det (to_matrix' Q) theorem discr_smul {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] {Q : quadratic_form R₁ (n → R₁)} (a : R₁) : discr (a • Q) = a ^ fintype.card n * discr Q := sorry theorem discr_comp {R₁ : Type u} [comm_ring R₁] {n : Type w} [fintype n] [DecidableEq n] [invertible (bit0 1)] {Q : quadratic_form R₁ (n → R₁)} (f : linear_map R₁ (n → R₁) (n → R₁)) : discr (comp Q f) = matrix.det (coe_fn linear_map.to_matrix' f) * matrix.det (coe_fn linear_map.to_matrix' f) * discr Q := sorry end quadratic_form namespace quadratic_form /-- An isometry between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`, is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/ structure isometry {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) extends linear_equiv R M₁ M₂ where map_app' : ∀ (m : M₁), coe_fn Q₂ (linear_equiv.to_fun _to_linear_equiv m) = coe_fn Q₁ m /-- Two quadratic forms over a ring `R` are equivalent if there exists an isometry between them: a linear equivalence that transforms one quadratic form into the other. -/ def equivalent {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) := Nonempty (isometry Q₁ Q₂) namespace isometry protected instance linear_equiv.has_coe {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} : has_coe (isometry Q₁ Q₂) (linear_equiv R M₁ M₂) := has_coe.mk to_linear_equiv protected instance has_coe_to_fun {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} : has_coe_to_fun (isometry Q₁ Q₂) := has_coe_to_fun.mk (fun (_x : isometry Q₁ Q₂) => M₁ → M₂) fun (f : isometry Q₁ Q₂) => ⇑↑f @[simp] theorem map_app {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} (f : isometry Q₁ Q₂) (m : M₁) : coe_fn Q₂ (coe_fn f m) = coe_fn Q₁ m := map_app' f m /-- The identity isometry from a quadratic form to itself. -/ def refl {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) : isometry Q Q := mk (linear_equiv.mk (linear_equiv.to_fun (linear_equiv.refl R M)) sorry sorry (linear_equiv.inv_fun (linear_equiv.refl R M)) sorry sorry) sorry /-- The inverse isometry of an isometry between two quadratic forms. -/ def symm {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} (f : isometry Q₁ Q₂) : isometry Q₂ Q₁ := mk (linear_equiv.mk (linear_equiv.to_fun (linear_equiv.symm ↑f)) sorry sorry (linear_equiv.inv_fun (linear_equiv.symm ↑f)) sorry sorry) sorry /-- The composition of two isometries between quadratic forms. -/ def trans {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} {M₃ : Type u_3} [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₃] [module R M₁] [module R M₂] [module R M₃] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} (f : isometry Q₁ Q₂) (g : isometry Q₂ Q₃) : isometry Q₁ Q₃ := mk (linear_equiv.mk (linear_equiv.to_fun (linear_equiv.trans ↑f ↑g)) sorry sorry (linear_equiv.inv_fun (linear_equiv.trans ↑f ↑g)) sorry sorry) sorry end isometry namespace equivalent theorem refl {R : Type u} {M : Type v} [add_comm_group M] [ring R] [module R M] (Q : quadratic_form R M) : equivalent Q Q := Nonempty.intro (isometry.refl Q) theorem symm {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} (h : equivalent Q₁ Q₂) : equivalent Q₂ Q₁ := nonempty.elim h fun (f : isometry Q₁ Q₂) => Nonempty.intro (isometry.symm f) theorem trans {R : Type u} [ring R] {M₁ : Type u_1} {M₂ : Type u_2} {M₃ : Type u_3} [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₃] [module R M₁] [module R M₂] [module R M₃] {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} (h : equivalent Q₁ Q₂) (h' : equivalent Q₂ Q₃) : equivalent Q₁ Q₃ := nonempty.elim h' (nonempty.elim h fun (f : isometry Q₁ Q₂) (g : isometry Q₂ Q₃) => Nonempty.intro (isometry.trans f g))
ce079443bdfac3683408e94f50a5766527c16f85
6214e13b31733dc9aeb4833db6a6466005763162
/src/evaluation.lean
7d81dea20ba3da9d7ac90b3037667394ac882edb
[]
no_license
joshua0pang/esverify-theory
272a250445f3aeea49a7e72d1ab58c2da6618bbe
8565b123c87b0113f83553d7732cd6696c9b5807
refs/heads/master
1,585,873,849,081
1,527,304,393,000
1,527,304,393,000
154,901,199
1
0
null
1,540,593,067,000
1,540,593,067,000
null
UTF-8
Lean
false
false
21,433
lean
-- lemmas about evaluation import .definitions3 -- lemmas lemma binop.eq_of_equal_values {v: value}: binop.apply binop.eq v v = value.true := have binop.apply binop.eq v v = (if v = v then value.true else value.false), by unfold binop.apply, show binop.apply binop.eq v v = value.true, by simp[this] lemma unop.isFunc.inv {v: value}: unop.apply unop.isFunc v = value.true → ∃ (f x: var) (R S: spec) (e: exp) (σ: env), v = value.func f x R S e σ := assume isFunc_is_true: unop.apply unop.isFunc v = value.true, begin cases v with n f x R S e σ, show ∃ (f x: var) (R S: spec) (e: exp) (σ: env), value.true = value.func f x R S e σ, from ( have h1: (unop.apply unop.isFunc value.true = value.true), from isFunc_is_true, have h2: (unop.apply unop.isFunc value.true = value.false), by unfold unop.apply, have some value.true = some value.false, from eq.trans h1.symm h2, have value.true = value.false, from option.some.inj this, false.elim (value._mut_.no_confusion this) ), show ∃ (f x: var) (R S: spec) (e: exp) (σ: env), value.false = value.func f x R S e σ, from ( have h1: (unop.apply unop.isFunc value.false = value.true), from isFunc_is_true, have h2: (unop.apply unop.isFunc value.false = value.false), by unfold unop.apply, have some value.true = some value.false, from eq.trans h1.symm h2, have value.true = value.false, from option.some.inj this, false.elim (value._mut_.no_confusion this) ), show ∃ (f x: var) (R S: spec) (e: exp) (σ: env), value.num n = value.func f x R S e σ, from ( have h1: (unop.apply unop.isFunc (value.num n) = value.true), from isFunc_is_true, have h2: (unop.apply unop.isFunc (value.num n) = value.false), by unfold unop.apply, have some value.true = some value.false, from eq.trans h1.symm h2, have value.true = value.false, from option.some.inj this, false.elim (value._mut_.no_confusion this) ), show ∃ (f_1 x_1: var) (R_1 S_1: spec) (e_1: exp) (σ_1: env), value.func f x R S e σ = value.func f_1 x_1 R_1 S_1 e_1 σ_1, from ( exists.intro f (exists.intro x (exists.intro R (exists.intro S (exists.intro e (exists.intro σ rfl))))) ) end lemma unop.isBool.inv {v: value}: unop.apply unop.isBool v = value.true → (v = value.true) ∨ (v = value.false) := assume isBool_is_true: unop.apply unop.isBool v = value.true, begin cases v with n f x R S e σ, show ((value.true = value.true) ∨ (value.true = value.false)), from ( or.inl rfl ), show ((value.false = value.true) ∨ (value.false = value.false)), from ( or.inr rfl ), show (value.num n = value.true ∨ (value.num n = value.false)), from ( have h1: unop.apply unop.isBool (value.num n) = ↑value.true, from isBool_is_true, have h2: (unop.apply unop.isBool (value.num n) = value.false), by unfold unop.apply, have some value.true = some value.false, from eq.trans h1.symm h2, have value.true = value.false, from option.some.inj this, false.elim (value._mut_.no_confusion this) ), show (value.func f x R S e σ = value.true ∨ (value.func f x R S e σ = value.false)), from ( have h1: unop.apply unop.isBool (value.func f x R S e σ) = ↑value.true, from isBool_is_true, have h2: (unop.apply unop.isBool (value.func f x R S e σ) = value.false), by unfold unop.apply, have some value.true = some value.false, from eq.trans h1.symm h2, have value.true = value.false, from option.some.inj this, false.elim (value._mut_.no_confusion this) ) end lemma binop.eq.inv {v₁ v₂: value}: binop.apply binop.eq v₁ v₂ = value.true → (v₁ = v₂) := assume eq_is_true: binop.apply binop.eq v₁ v₂ = value.true, begin by_cases (v₁ = v₂), from h, unfold binop.apply at eq_is_true, simp[h] at eq_is_true, have h2, from option.some.inj eq_is_true, contradiction end lemma pre_preserved {s s': dstack}: s ⟹* s' → (s.pre = s'.pre) := begin assume h1, induction h1, case trans_dstep.rfl { refl }, case trans_dstep.trans s₁ s₂ s₃ h2 h3 ih { apply eq.trans ih, cases h3, repeat {refl} } end lemma unop_result_not_function {vx vy: value} {op: unop}: (unop.apply op vx = some vy) → (vy = value.true) ∨ (vy = value.false) := begin assume h1, cases op, case unop.not { cases vx, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.true = vy), from option.some.inj h1, left, from h2.symm, unfold unop.apply at h1, contradiction, unfold unop.apply at h1, contradiction }, case unop.isInt { cases vx, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.true = vy), from option.some.inj h1, left, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm }, case unop.isBool { cases vx, unfold unop.apply at h1, have h2: (value.true = vy), from option.some.inj h1, left, from h2.symm, unfold unop.apply at h1, have h2: (value.true = vy), from option.some.inj h1, left, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm }, case unop.isFunc { cases vx, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.false = vy), from option.some.inj h1, right, from h2.symm, unfold unop.apply at h1, have h2: (value.true = vy), from option.some.inj h1, left, from h2.symm } end lemma binop_result_not_function {vx vy vz: value} {op: binop}: (binop.apply op vx vy = some vz) → ((vz = value.true) ∨ (vz = value.false) ∨ (∃n, vz = value.num n)) := begin assume h1, cases op, case binop.plus { cases vx, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, have h2: (value.num (a + a_1) = vz), from option.some.inj h1, right, right, existsi (a + a_1), from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.minus { cases vx, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, have h2: (value.num (a - a_1) = vz), from option.some.inj h1, right, right, existsi (a - a_1), from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.times { cases vx, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, have h2: (value.num (a * a_1) = vz), from option.some.inj h1, right, right, existsi (a * a_1), from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.div { cases vx, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, have h2: (value.num (a / a_1) = vz), from option.some.inj h1, right, right, existsi (a / a_1), from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.and { cases vx, cases vy, unfold binop.apply at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, unfold binop.apply at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm, unfold binop.apply at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.or { cases vx, cases vy, unfold binop.apply at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, unfold binop.apply at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, unfold binop.apply at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction }, case binop.eq { unfold binop.apply at h1, by_cases (vx = vy), simp[h] at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, simp[h] at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm }, case binop.lt { cases vx, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, cases vy, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, by_cases (a < a_1), simp[h] at h1, have h2: (value.true = vz), from option.some.inj h1, left, from h2.symm, simp[h] at h1, have h2: (value.false = vz), from option.some.inj h1, right, left, from h2.symm, unfold binop.apply at h1, contradiction, unfold binop.apply at h1, contradiction } end lemma step_dstep_progress {s s': stack}: (s ⟶ s') → ∀d, stack_equiv_dstack s d → ∃d', (d ⟹ d') := begin assume h1, induction h1, case step.tru { assume d, assume h2, cases h2, existsi (dstack.top R (σ[x↦value.true]) e), apply dstep.tru }, case step.fals { assume d, assume h2, cases h2, existsi (dstack.top R (σ[x↦value.false]) e), apply dstep.fals }, case step.num { assume d, assume h2, cases h2, existsi (dstack.top R (σ[x↦value.num n]) e), apply dstep.num }, case step.closure { assume d, assume h2, cases h2, existsi (dstack.top R_1 (σ[f↦value.func f x R S e₁ σ]) e₂), apply dstep.closure }, case step.unop { assume d, assume h2, cases h2, existsi (dstack.top R (σ[y↦v]) e), apply dstep.unop a a_1 }, case step.binop { assume d, assume h2, cases h2, existsi (dstack.top R (σ[z↦v]) e), apply dstep.binop a a_1 a_2 }, case step.app { assume d, assume h2, cases h2, existsi (dstack.cons (R, (σ'[g↦value.func g z R S e σ'][z↦v]), e) R_1 σ y f x e'), apply dstep.app a a_1 }, case step.ite_true { assume d, assume h2, cases h2, existsi (dstack.top R σ e₁), apply dstep.ite_true a }, case step.ite_false { assume d, assume h2, cases h2, existsi (dstack.top R σ e₂), apply dstep.ite_false a }, case step.ctx s₁ s₂ σ₁ f x y e₁ steps ih { assume d, assume h2, cases h2, have h3, from ih d' a, cases h3 with d'' h4, existsi (dstack.cons d'' R σ₁ y f x e₁), apply dstep.ctx h4 }, case step.return { assume d, assume h2, cases h2, existsi (dstack.top R_1 (σ₂[y↦v]) e'), cases a_3, apply dstep.return a a_1 a_2 } end lemma step_dstep_progress.inv {d d': dstack}: (d ⟹ d') → ∀s, stack_equiv_dstack s d → ∃s', (s ⟶ s') := begin assume h1, induction h1, case dstep.tru { assume s, assume h2, cases h2, existsi (stack.top (σ[x↦value.true]) e), apply step.tru }, case dstep.fals { assume s, assume h2, cases h2, existsi (stack.top (σ[x↦value.false]) e), apply step.fals }, case dstep.num { assume s, assume h2, cases h2, existsi (stack.top (σ[x↦value.num n]) e), apply step.num }, case dstep.closure { assume s, assume h2, cases h2, existsi (stack.top (σ[f↦value.func f x R S e₁ σ]) e₂), apply step.closure, from spec.term value.true }, case dstep.unop { assume s, assume h2, cases h2, existsi (stack.top (σ[y↦v]) e), apply step.unop a a_1 }, case dstep.binop { assume s, assume h2, cases h2, existsi (stack.top (σ[z↦v]) e), apply step.binop a a_1 a_2 }, case dstep.app { assume s, assume h2, cases h2, existsi (stack.cons ((σ'[g↦value.func g z R S e σ'][z↦v]), e) σ y f x e'), apply step.app a a_1 }, case dstep.ite_true { assume s, assume h2, cases h2, existsi (stack.top σ e₁), apply step.ite_true a }, case dstep.ite_false { assume s, assume h2, cases h2, existsi (stack.top σ e₂), apply step.ite_false a }, case dstep.ctx d₁ d₂ R σ₁ f x y e₁ steps ih { assume s, assume h2, cases h2, have h3, from ih s' a, cases h3 with s'' h4, existsi (stack.cons s'' σ₁ y f x e₁), apply step.ctx h4 }, case dstep.return { assume s, assume h2, cases h2, existsi (stack.top (σ₂[y↦v]) e'), cases a_3, apply step.return a a_1 a_2 } end lemma step_dstep_preservation {s: stack}: ∀{s': stack} {d d': dstack}, stack_equiv_dstack s d → (s ⟶ s') → (d ⟹ d') → stack_equiv_dstack s' d' := begin induction s, case stack.top σ e { assume s' d d', assume h1, assume h2, assume h3, cases h1, have h4: (dstack.top R σ e ⟹ d'), from h3, cases h2, case step.tru { cases h4, simp, apply stack_equiv_dstack.top }, case step.fals { cases h4, simp, apply stack_equiv_dstack.top }, case step.num { cases h4, simp, apply stack_equiv_dstack.top }, case step.closure { cases h4, simp, apply stack_equiv_dstack.top }, case step.unop { cases h4, simp, have h5, from eq.trans a.symm a_2, have h6: (v₁ = v₁_1), from option.some.inj h5, rw[h6] at a_1, have h7, from eq.trans a_1.symm a_3, have h8: (v = v_1), from option.some.inj h7, rw[h8], apply stack_equiv_dstack.top }, case step.binop { cases h4, simp, have h5, from eq.trans a.symm a_3, have h6: (v₁ = v₁_1), from option.some.inj h5, rw[h6] at a_2, have h7, from eq.trans a_1.symm a_4, have h8: (v₂ = v₂_1), from option.some.inj h7, rw[h8] at a_2, have h9, from eq.trans a_2.symm a_5, have h10: (v = v_1), from option.some.inj h9, rw[h10], apply stack_equiv_dstack.top }, case step.app { cases h4, apply stack_equiv_dstack.cons, have h5, from eq.trans a.symm a_2, have h6: ((value.func g z R_1 S e_1 σ') = (value.func g_1 z_1 R_2 S_1 e σ'_1)), from option.some.inj h5, injection h6, rw[h_1], rw[h_2], rw[h_3], rw[h_4], rw[h_5], rw[h_6], have h7, from eq.trans a_1.symm a_3, have h8: (v = v_1), from option.some.inj h7, rw[h8], apply stack_equiv_dstack.top }, case step.ite_true { cases h4, simp, apply stack_equiv_dstack.top, have h7, from eq.trans a.symm a_1, have h8: (value.true = value.false), from option.some.inj h7, contradiction }, case step.ite_false { cases h4, simp, have h7, from eq.trans a.symm a_1, have h8: (value.false = value.true), from option.some.inj h7, contradiction, apply stack_equiv_dstack.top, }, }, case stack.cons s'' σ'' f x y e'' ih { assume s' d d', assume h1, assume h2, assume h3, cases h1, cases h2, case step.ctx { cases h3, apply stack_equiv_dstack.cons, from ih a a_1 a_2, simp at a, cases a, cases a_1 }, case step.return { cases h3, simp at a, cases a, cases a_4, simp, cases a, have h5, from eq.trans a_1.symm a_4, have h6: (v = v_1), from option.some.inj h5, rw[h6], apply stack_equiv_dstack.top, } } end lemma step_of_dstep_trans {d d': dstack}: (d ⟹* d') → ∀s, stack_equiv_dstack s d → ∃s', (s ⟶* s') ∧ stack_equiv_dstack s' d' := begin assume h1, induction h1, assume s, assume h2, existsi s, split, from trans_step.rfl, from h2, assume s_1, assume h2, have h3, from ih_1 s_1 h2, cases h3 with s_2 h4, have h5, from step_dstep_progress.inv a_1 s_2 h4.right, cases h5 with s3 h6, existsi s3, split, apply trans_step.trans, from h4.left, from h6, apply step_dstep_preservation h4.right h6 a_1 end lemma dstep_of_step_trans {s s': stack}: (s ⟶* s') → ∀d, stack_equiv_dstack s d → ∃d', (d ⟹* d') ∧ stack_equiv_dstack s' d' := begin assume h1, induction h1, assume d, assume h2, existsi d, split, from trans_dstep.rfl, from h2, assume s_1, assume h2, have h3, from ih_1 s_1 h2, cases h3 with s_2 h4, have h5: ∃ (d' : dstack), s_2⟹d', from step_dstep_progress a_1 s_2 h4.right, cases h5 with s3 h6, existsi s3, split, apply trans_dstep.trans, from h4.left, from h6, apply step_dstep_preservation h4.right a_1 h6 end lemma step_of_dstep {R₁ R₂: spec} {σ₁ σ₂: env} {e₁ e₂: exp}: (R₁, σ₁, e₁) ⟹* (R₂, σ₂, e₂) → ((σ₁, e₁) ⟶* (σ₂, e₂)) := begin assume h1, have h2: stack_equiv_dstack (σ₁, e₁) (R₁, σ₁, e₁), from stack_equiv_dstack.top, have h3, from step_of_dstep_trans h1 (σ₁, e₁) h2, cases h3 with s' h4, cases h4.right, from h4.left end lemma value_or_step_of_dvalue_or_dstep {s: stack} {d: dstack}: stack_equiv_dstack s d → (is_dvalue d ∨ ∃d', d ⟹ d') → (is_value s ∨ ∃s', s ⟶ s') := begin assume h1, assume h2, cases h2 with h3 h3, unfold is_dvalue at h3, cases h3 with R h4, cases h4 with σ h5, cases h5 with x h6, cases h6 with v h7, left, unfold is_value, existsi σ, existsi x, existsi v, split, rw[h7.left] at h1, cases h1, congr, from h7.right, cases h3 with d' h4, have h5, from step_dstep_progress.inv h4 s h1, right, from h5 end
94092fab262212de5f31183b7c0f224d99098f49
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/filter/lift.lean
11faccfa0d540bbe652ae2c0ae7107ddcab93d36
[ "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
19,424
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import order.filter.bases /-! # Lift filters along filter and set functions -/ open set open_locale classical filter namespace filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Sort*} section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. This is essentially a push-forward along a function mapping each set to a filter. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} @[simp] lemma lift_top (g : set α → filter β) : (⊤ : filter α).lift g = g univ := by simp [filter.lift] /-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function `set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type, see `filter.has_basis.lift`. This lemma states the corresponding `mem_iff` statement without using a sigma type. -/ lemma has_basis.mem_lift_iff {ι} {p : ι → Prop} {s : ι → set α} {f : filter α} (hf : f.has_basis p s) {β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ} {g : set α → filter γ} (hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g) {s : set γ} : s ∈ f.lift g ↔ ∃ (i : ι) (hi : p i) (x : β i) (hx : pg i x), sg i x ⊆ s := begin refine (mem_binfi_of_directed _ ⟨univ, univ_sets _⟩).trans _, { intros t₁ ht₁ t₂ ht₂, exact ⟨t₁ ∩ t₂, inter_mem ht₁ ht₂, gm $ inter_subset_left _ _, gm $ inter_subset_right _ _⟩ }, { simp only [← (hg _).mem_iff], exact hf.exists_iff (λ t₁ t₂ ht H, gm ht H) } end /-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function `set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type. See also `filter.has_basis.mem_lift_iff` for the corresponding `mem_iff` statement formulated without using a sigma type. -/ lemma has_basis.lift {ι} {p : ι → Prop} {s : ι → set α} {f : filter α} (hf : f.has_basis p s) {β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ} {g : set α → filter γ} (hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g) : (f.lift g).has_basis (λ i : Σ i, β i, p i.1 ∧ pg i.1 i.2) (λ i : Σ i, β i, sg i.1 i.2) := begin refine ⟨λ t, (hf.mem_lift_iff hg gm).trans _⟩, simp [sigma.exists, and_assoc, exists_and_distrib_left] end lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ f.lift g ↔ ∃t∈f, s ∈ g t := (f.basis_sets.mem_lift_iff (λ s, (g s).basis_sets) hg).trans $ by simp only [id, exists_mem_subset_iff] lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f) (hs : s ∈ g t) : s ∈ f.lift g := le_principal_iff.mp $ show f.lift g ≤ 𝓟 s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h := infi₂_le_of_le s hs hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} (hh : ∀s∈f, h ≤ g s) : h ≤ f.lift g := le_infi₂ hh lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_mono $ λ s, infi_mono' $ λ hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s ∈ f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi₂_mono hg lemma tendsto_lift {m : γ → β} {l : filter γ} : tendsto m l (f.lift g) ↔ ∀ s ∈ f, tendsto m l (g s) := by simp only [filter.lift, tendsto_infi] lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from map_mono.comp hg, filter.ext $ λ s, by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, mem_map, function.comp_app] lemma comap_lift_eq {m : γ → β} : comap m (f.lift g) = f.lift (comap m ∘ g) := by simp only [filter.lift, comap_infi] theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi₂ $ λ s hs, infi₂_le (m ⁻¹' s) ⟨s, hs, subset.rfl⟩) (le_infi₂ $ λ s ⟨s', hs', (h_sub : m ⁻¹' s' ⊆ s)⟩, infi₂_le_of_le s' hs' $ hg h_sub) lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := le_antisymm (infi_mono' $ assume s, ⟨image m s, infi_mono' $ assume hs, ⟨ f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h, le_rfl⟩⟩) (infi_mono' $ assume t, ⟨preimage m t, infi_mono' $ assume ht, ⟨ht, hg $ assume x, assume h : x ∈ m '' preimage m t, let ⟨y, hy, h_eq⟩ := h in show x ∈ t, from h_eq ▸ hy⟩⟩) lemma lift_comm {g : filter β} {h : set α → set β → filter γ} : f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) lemma lift_assoc {h : set β → filter γ} (hg : monotone g) : (f.lift g).lift h = f.lift (λs, (g s).lift h) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_infi $ assume t, le_infi $ assume ht, let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h') lemma lift_lift_same_le_lift {g : set α → set α → filter β} : f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) := le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs lemma lift_lift_same_eq_lift {g : set α → set α → filter β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) : f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) := le_antisymm lift_lift_same_le_lift (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le (s ∩ t) $ infi_le_of_le (inter_mem hs ht) $ calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _) ... ≤ g s t : hg₁ s (inter_subset_right _ _)) lemma lift_principal {s : set α} (hg : monotone g) : (𝓟 s).lift g = g s := le_antisymm (infi_le_of_le s $ infi_le _ $ subset.refl _) (le_infi $ assume t, le_infi $ assume hi, hg hi) theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) := assume a b h, lift_mono (hf h) (hg h) lemma lift_ne_bot_iff (hm : monotone g) : (ne_bot $ f.lift g) ↔ (∀s∈f, ne_bot (g s)) := begin rw [filter.lift, infi_subtype', infi_ne_bot_iff_of_directed', subtype.forall'], { rintros ⟨s, hs⟩ ⟨t, ht⟩, exact ⟨⟨s ∩ t, inter_mem hs ht⟩, hm (inter_subset_left s t), hm (inter_subset_right s t)⟩ } end @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := le_antisymm (lift_le univ_mem $ le_refl g) (le_lift $ assume s hs, le_refl g) @[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} : f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp only [filter.lift, infi_inf_eq, eq_self_iff_true] @[simp] lemma lift_principal2 {f : filter α} : f.lift 𝓟 = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff]) lemma lift_infi_le {f : ι → filter α} {g : set α → filter β} : (infi f).lift g ≤ ⨅ i, (f i).lift g := le_infi $ λ i, lift_mono (infi_le _ _) le_rfl lemma lift_infi [nonempty ι] {f : ι → filter α} {g : set α → filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) : (infi f).lift g = (⨅i, (f i).lift g) := begin refine lift_infi_le.antisymm (λ s, _), have H : ∀ t ∈ infi f, (⨅ i, (f i).lift g) ≤ g t, { intros t ht, refine infi_sets_induct ht _ (λ i s t hs ht, _), { inhabit ι, exact infi₂_le_of_le default univ (infi_le _ univ_mem) }, { rw hg, exact le_inf (infi₂_le_of_le i s $ infi_le _ hs) ht } }, simp only [mem_lift_sets (monotone.of_map_inf hg), exists_imp_distrib], exact λ t ht hs, H t ht hs end lemma lift_infi_of_directed [nonempty ι] {f : ι → filter α} {g : set α → filter β} (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := lift_infi_le.antisymm $ λ s, begin simp only [mem_lift_sets hg, exists_imp_distrib, mem_infi_of_directed hf], exact assume t i ht hs, mem_infi_of_mem i $ mem_lift ht hs end lemma lift_infi_of_map_univ {f : ι → filter α} {g : set α → filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) (hg' : g univ = ⊤) : (infi f).lift g = (⨅i, (f i).lift g) := begin casesI is_empty_or_nonempty ι, { simp [infi_of_empty, hg'] }, { exact lift_infi hg } end end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `map`. This is essentially a push-forward along a function mapping each set to a set. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (𝓟 ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} @[simp] lemma lift'_top (h : set α → set β) : (⊤ : filter α).lift' h = 𝓟 (h univ) := lift_top _ lemma mem_lift' {t : set α} (ht : t ∈ f) : h t ∈ (f.lift' h) := le_principal_iff.mp $ show f.lift' h ≤ 𝓟 (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_rfl lemma tendsto_lift' {m : γ → β} {l : filter γ} : tendsto m l (f.lift' h) ↔ ∀ s ∈ f, ∀ᶠ a in l, m a ∈ h s := by simp only [filter.lift', tendsto_lift, tendsto_principal] lemma has_basis.lift' {ι} {p : ι → Prop} {s} (hf : f.has_basis p s) (hh : monotone h) : (f.lift' h).has_basis p (h ∘ s) := begin refine ⟨λ t, (hf.mem_lift_iff _ (monotone_principal.comp hh)).trans _⟩, show ∀ i, (𝓟 (h (s i))).has_basis (λ j : unit, true) (λ (j : unit), h (s i)), from λ i, has_basis_principal _, simp only [exists_const] end lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h) ↔ (∃t∈f, h t ⊆ s) := mem_lift_sets $ monotone_principal.comp hh lemma eventually_lift'_iff (hh : monotone h) {p : β → Prop} : (∀ᶠ y in f.lift' h, p y) ↔ (∃ t ∈ f, ∀ y ∈ h t, p y) := mem_lift'_sets hh lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : 𝓟 (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf $ assume s, principal_mono.mpr $ hh s lemma lift'_mono' (hh : ∀s∈f, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi₂_mono $ λ s hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm) lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ 𝓟 ∘ h) : map_lift_eq $ monotone_principal.comp hh ... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true] lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 $ monotone_principal.comp hg theorem comap_lift'_eq {m : γ → β} : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := by simp only [filter.lift', comap_lift_eq, (∘), comap_principal] theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 $ monotone_principal.comp hg lemma lift'_principal {s : set α} (hh : monotone h) : (𝓟 s).lift' h = 𝓟 (h s) := lift_principal $ monotone_principal.comp hh lemma lift'_pure {a : α} (hh : monotone h) : (pure a : filter α).lift' h = 𝓟 (h {a}) := by rw [← principal_singleton, lift'_principal hh] lemma lift'_bot (hh : monotone h) : (⊥ : filter α).lift' h = 𝓟 (h ∅) := by rw [← principal_empty, lift'_principal hh] lemma principal_le_lift' {t : set β} (hh : ∀s∈f, t ⊆ h s) : 𝓟 t ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs) theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) := assume a b h, lift'_mono (hf h) (hg h) lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift h = f.lift (λs, h (g s)) := calc (f.lift' g).lift h = f.lift (λs, (𝓟 (g s)).lift h) : lift_assoc (monotone_principal.comp hg) ... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true] lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift' h = f.lift' (λs, h (g s)) := lift_lift'_assoc hg (monotone_principal.comp hh) lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ} (hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) := lift_assoc hg lemma lift_lift'_same_le_lift' {g : set α → set α → set β} : f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) := lift_lift_same_le_lift lemma lift_lift'_same_eq_lift' {g : set α → set α → set β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) : f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) := lift_lift_same_eq_lift (assume s, monotone_principal.comp (hg₁ s)) (assume t, monotone_principal.comp (hg₂ t)) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ 𝓟 s = f.lift' (λt, h t ∩ s) := by simp only [filter.lift', filter.lift, (∘), ← inf_principal, infi_subtype', ← infi_inf] lemma lift'_ne_bot_iff (hh : monotone h) : (ne_bot (f.lift' h)) ↔ (∀s∈f, (h s).nonempty) := calc (ne_bot (f.lift' h)) ↔ (∀s∈f, ne_bot (𝓟 (h s))) : lift_ne_bot_iff (monotone_principal.comp hh) ... ↔ (∀s∈f, (h s).nonempty) : by simp only [principal_ne_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} (h_le : ∀s∈f, h s ∈ g) : g ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, by simpa only [h_le, le_principal_iff, function.comp_app] using h_le s hs lemma lift'_infi [nonempty ι] {f : ι → filter α} {g : set α → set β} (hg : ∀ s t, g (s ∩ t) = g s ∩ g t) : (infi f).lift' g = (⨅ i, (f i).lift' g) := lift_infi $ λ s t, by rw [inf_principal, (∘), ← hg] lemma lift'_infi_of_map_univ {f : ι → filter α} {g : set α → set β} (hg : ∀{s t}, g (s ∩ t) = g s ∩ g t) (hg' : g univ = univ) : (infi f).lift' g = (⨅ i, (f i).lift' g) := lift_infi_of_map_univ (λ s t, by rw [inf_principal, (∘), ← hg]) (by rw [function.comp_app, hg', principal_univ]) lemma lift'_inf (f g : filter α) {s : set α → set β} (hs : ∀ t₁ t₂, s (t₁ ∩ t₂) = s t₁ ∩ s t₂) : (f ⊓ g).lift' s = f.lift' s ⊓ g.lift' s := have (⨅ b : bool, cond b f g).lift' s = ⨅ b : bool, (cond b f g).lift' s := lift'_infi @hs, by simpa only [infi_bool_eq] lemma lift'_inf_le (f g : filter α) (s : set α → set β) : (f ⊓ g).lift' s ≤ f.lift' s ⊓ g.lift' s := le_inf (lift'_mono inf_le_left le_rfl) (lift'_mono inf_le_right le_rfl) theorem comap_eq_lift' {f : filter β} {m : α → β} : comap m f = f.lift' (preimage m) := filter.ext $ λ s, (mem_lift'_sets monotone_preimage).symm end lift' section prod variables {f : filter α} lemma prod_def {f : filter α} {g : filter β} : f ×ᶠ g = (f.lift $ λ s, g.lift' $ λ t, s ×ˢ t) := have ∀(s:set α) (t : set β), 𝓟 (s ×ˢ t) = (𝓟 s).comap prod.fst ⊓ (𝓟 t).comap prod.snd, by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl, begin simp only [filter.lift', function.comp, this, lift_inf, lift_const, lift_inf], rw [← comap_lift_eq, ← comap_lift_eq], simp only [filter.prod, lift_principal2] end lemma prod_same_eq : f ×ᶠ f = f.lift' (λ t : set α, t ×ˢ t) := prod_def.trans $ lift_lift'_same_eq_lift' (λ s, monotone_const.set_prod monotone_id) (λ t, monotone_id.set_prod monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ f ×ᶠ f ↔ (∃t∈f, t ×ˢ t ⊆ s) := by { rw [prod_same_eq, mem_lift'_sets], exact monotone_id.set_prod monotone_id } lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} : filter.tendsto f (x ×ᶠ x) y ↔ ∀ W ∈ y, ∃ U ∈ x, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W := by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self] variables {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} lemma prod_lift_lift {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : (f₁.lift g₁) ×ᶠ (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, g₁ s ×ᶠ g₂ t)) := begin simp only [prod_def, lift_assoc hg₁], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, apply lift'_lift_assoc hg₂ end lemma prod_lift'_lift' {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : f₁.lift' g₁ ×ᶠ f₂.lift' g₂ = f₁.lift (λ s, f₂.lift' (λ t, g₁ s ×ˢ g₂ t)) := calc f₁.lift' g₁ ×ᶠ f₂.lift' g₂ = f₁.lift (λ s, f₂.lift (λ t, 𝓟 (g₁ s) ×ᶠ 𝓟 (g₂ t))) : prod_lift_lift (monotone_principal.comp hg₁) (monotone_principal.comp hg₂) ... = f₁.lift (λ s, f₂.lift (λ t, 𝓟 (g₁ s ×ˢ g₂ t))) : by simp only [prod_principal_principal] end prod end filter
a5aef125830b5e4a2d9acf5dbacbc5d2134f14f9
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/geometry/manifold/instances/circle.lean
03dd3fe83bb7259e856ee853659f73ef9ff2f8e7
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,038
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 geometry.manifold.instances.sphere /-! # The circle This file defines `circle` to be the metric sphere (`metric.sphere`) in `ℂ` centred at `0` of radius `1`. We equip it with the following structure: * a submonoid of `ℂ` * a group * a topological group * a charted space with model space `euclidean_space ℝ (fin 1)` (inherited from `metric.sphere`) * a Lie group with model with corners `𝓡 1` We furthermore define `exp_map_circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`, and show that this map is a group homomorphism and is smooth. ## Implementation notes To borrow the smooth manifold structure cleanly, the circle needs to be defined using `metric.sphere`, and therefore the underlying set is `{z : ℂ | abs (z - 0) = 1}`. This prevents certain algebraic facts from working definitionally -- for example, the circle is not defeq to `{z : ℂ | abs z = 1}`, which is the kernel of `complex.abs` considered as a homomorphism from `ℂ` to `ℝ`, nor is it defeq to `{z : ℂ | norm_sq z = 1}`, which is the kernel of the homomorphism `complex.norm_sq` from `ℂ` to `ℝ`. -/ noncomputable theory open complex finite_dimensional metric open_locale manifold local attribute [instance] findim_real_complex_fact /-- The unit circle in `ℂ`, here given the structure of a submonoid of `ℂ`. -/ def circle : submonoid ℂ := { carrier := sphere (0:ℂ) 1, one_mem' := by simp, mul_mem' := λ a b, begin simp only [norm_eq_abs, mem_sphere_zero_iff_norm], intros ha hb, simp [ha, hb], end } @[simp] lemma mem_circle_iff_abs (z : ℂ) : z ∈ circle ↔ abs z = 1 := mem_sphere_zero_iff_norm lemma circle_def : ↑circle = {z : ℂ | abs z = 1} := by { ext, simp } @[simp] lemma abs_eq_of_mem_circle (z : circle) : abs z = 1 := by { convert z.2, simp } instance : group circle := { inv := λ z, ⟨conj z, by simp⟩, mul_left_inv := λ z, subtype.ext $ by { simp [has_inv.inv, ← norm_sq_eq_conj_mul_self, ← mul_self_abs] }, .. circle.to_monoid } @[simp] lemma coe_inv_circle (z : circle) : ↑(z⁻¹) = conj z := rfl @[simp] lemma coe_div_circle (z w : circle) : ↑(z / w) = ↑z * conj w := rfl -- the following result could instead be deduced from the Lie group structure on the circle using -- `topological_group_of_lie_group`, but that seems a little awkward since one has to first provide -- and then forget the model space instance : topological_group circle := { continuous_mul := let h : continuous (λ x : circle, (x : ℂ)) := continuous_subtype_coe in continuous_induced_rng (continuous_mul.comp (h.prod_map h)), continuous_inv := continuous_induced_rng $ complex.conj_clm.continuous.comp continuous_subtype_coe } /-- The unit circle in `ℂ` is a charted space modelled on `euclidean_space ℝ (fin 1)`. This follows by definition from the corresponding result for `metric.sphere`. -/ instance : charted_space (euclidean_space ℝ (fin 1)) circle := metric.sphere.charted_space /-- The unit circle in `ℂ` is a Lie group. -/ instance : lie_group (𝓡 1) circle := { smooth_mul := begin let c : circle → ℂ := coe, have h₁ : times_cont_mdiff _ _ _ (prod.map c c) := times_cont_mdiff_coe_sphere.prod_map times_cont_mdiff_coe_sphere, have h₂ : times_cont_mdiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ∞ (λ (z : ℂ × ℂ), z.fst * z.snd), { rw times_cont_mdiff_iff, exact ⟨continuous_mul, λ x y, (times_cont_diff_mul.restrict_scalars ℝ).times_cont_diff_on⟩ }, convert (h₂.comp h₁).cod_restrict_sphere _, convert smooth_manifold_with_corners.prod circle circle, { exact metric.sphere.smooth_manifold_with_corners }, { exact metric.sphere.smooth_manifold_with_corners } end, smooth_inv := (complex.conj_clm.times_cont_diff.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _, .. metric.sphere.smooth_manifold_with_corners } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ`. -/ def exp_map_circle (t : ℝ) : circle := ⟨exp (t * I), by simp [exp_mul_I, abs_cos_add_sin_mul_I]⟩ /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ`, considered as a homomorphism of groups. -/ def exp_map_circle_hom : ℝ →+ (additive circle) := { to_fun := exp_map_circle, map_zero' := by { rw exp_map_circle, convert of_mul_one, simp }, map_add' := λ x y, show exp_map_circle (x + y) = (exp_map_circle x) * (exp_map_circle y), from subtype.ext $ by simp [exp_map_circle, exp_add, add_mul] } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ` is smooth. -/ lemma times_cont_mdiff_exp_map_circle : times_cont_mdiff 𝓘(ℝ, ℝ) (𝓡 1) ∞ exp_map_circle := (((times_cont_diff_exp.restrict_scalars ℝ).comp (times_cont_diff_id.smul times_cont_diff_const)).times_cont_mdiff).cod_restrict_sphere _
501f7c5ccbbabba500302822eadfc21894afe52d
367134ba5a65885e863bdc4507601606690974c1
/src/data/fintype/intervals.lean
aa6c98fa36a606b2e10801dc6db58aebec562c10
[ "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
2,029
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.set.intervals import data.set.finite import data.pnat.intervals /-! # fintype instances for intervals We provide `fintype` instances for `Ico l u`, for `l u : ℕ`, and for `l u : ℤ`. -/ namespace set instance Ico_ℕ_fintype (l u : ℕ) : fintype (Ico l u) := fintype.of_finset (finset.Ico l u) $ (λ n, by { simp only [mem_Ico, finset.Ico.mem], }) @[simp] lemma Ico_ℕ_card (l u : ℕ) : fintype.card (Ico l u) = u - l := calc fintype.card (Ico l u) = (finset.Ico l u).card : fintype.card_of_finset _ _ ... = u - l : finset.Ico.card l u instance Ico_pnat_fintype (l u : ℕ+) : fintype (Ico l u) := fintype.of_finset (pnat.Ico l u) $ (λ n, by { simp only [mem_Ico, pnat.Ico.mem], }) @[simp] lemma Ico_pnat_card (l u : ℕ+) : fintype.card (Ico l u) = u - l := calc fintype.card (Ico l u) = (pnat.Ico l u).card : fintype.card_of_finset _ _ ... = u - l : pnat.Ico.card l u instance Ico_ℤ_fintype (l u : ℤ) : fintype (Ico l u) := fintype.of_finset (finset.Ico_ℤ l u) $ (λ n, by { simp only [mem_Ico, finset.Ico_ℤ.mem], }) @[simp] lemma Ico_ℤ_card (l u : ℤ) : fintype.card (Ico l u) = (u - l).to_nat := calc fintype.card (Ico l u) = (finset.Ico_ℤ l u).card : fintype.card_of_finset _ _ ... = (u - l).to_nat : finset.Ico_ℤ.card l u lemma Ico_ℤ_finite (l u : ℤ) : set.finite (Ico l u) := ⟨set.Ico_ℤ_fintype l u⟩ lemma Ioo_ℤ_finite (l u : ℤ) : set.finite (Ioo l u) := Ico_ℤ_finite (l + 1) u lemma Icc_ℤ_finite (l u : ℤ) : set.finite (Icc l u) := begin convert Ico_ℤ_finite l (u + 1), ext, simp only [int.lt_add_one_iff, iff_self, mem_Ico, mem_Icc], end lemma Ioc_ℤ_finite (l u : ℤ) : set.finite (Ioc l u) := Icc_ℤ_finite (l + 1) u -- TODO other useful instances: fin n, zmod? end set
f7e7be67686b7ec14577f466da4962d8eb8d4951
618003631150032a5676f229d13a079ac875ff77
/src/tactic/norm_cast.lean
14dc9252b832ccd1426a4f4ddecef7222089263a
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
25,786
lean
/- Copyright (c) 2019 Paul-Nicolas Madelaine. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Paul-Nicolas Madelaine, Robert Y. Lewis Normalizing casts inside expressions. -/ import tactic.converter.interactive import tactic.hint /-! # A tactic for normalizing casts inside expressions This tactic normalizes casts inside expressions. It can be thought of as a call to the simplifier with a specific set of lemmas to move casts upwards in the expression. It has special handling of numerals and a simple heuristic to help moving casts "past" binary operators. Contrary to simp, it should be safe to use as a non-terminating tactic. The algorithm implemented here is described in the paper <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. ## Important definitions * `tactic.interactive.norm_cast` * `tactic.interactive.push_cast` * `tactic.interactive.exact_mod_cast` * `tactic.interactive.apply_mod_cast` * `tactic.interactive.rw_mod_cast` * `tactic.interactive.assumption_mod_cast` -/ setup_tactic_parser namespace tactic /-- Runs `mk_instance` with a time limit. This is a work around to the fact that in some cases mk_instance times out instead of failing, for example: `has_lift_t ℤ ℕ` `mk_instance_fast` is used when we assume the type class search should end instantly. -/ meta def mk_instance_fast (e : expr) (timeout := 1000) : tactic expr := try_for timeout (mk_instance e) end tactic namespace norm_cast open tactic expr declare_trace norm_cast /-- Output a trace message if `trace.norm_cast` is enabled. -/ meta def trace_norm_cast {α} [has_to_tactic_format α] (msg : string) (a : α) : tactic unit := when_tracing `norm_cast $ do a ← pp a, trace ("[norm_cast] " ++ msg ++ a : format) mk_simp_attribute push_cast "The `push_cast` simp attribute uses `norm_cast` lemmas to move casts toward the leaf nodes of the expression." /-- `label` is a type used to classify `norm_cast` lemmas. * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes -/ @[derive [decidable_eq, has_reflect, inhabited]] inductive label | elim : label | move : label | squash : label namespace label /-- Convert `label` into `string`. -/ protected def to_string : label → string | elim := "elim" | move := "move" | squash := "squash" instance : has_to_string label := ⟨label.to_string⟩ instance : has_repr label := ⟨label.to_string⟩ meta instance : has_to_format label := ⟨λ l, l.to_string⟩ /-- Convert `string` into `label`. -/ def of_string : string -> option label | "elim" := some elim | "move" := some move | "squash" := some squash | _ := none end label open label /-- Count how many coercions are at the top of the expression. -/ meta def count_head_coes : expr → ℕ | `(coe %%e) := count_head_coes e + 1 | `(coe_sort %%e) := count_head_coes e + 1 | `(coe_fn %%e) := count_head_coes e + 1 | _ := 0 /-- Count how many coercions are inside the expression, including the top ones. -/ meta def count_coes : expr → tactic ℕ | `(coe %%e) := (+1) <$> count_coes e | `(coe_sort %%e) := (+1) <$> count_coes e | `(coe_fn %%e) := (+1) <$> count_coes e | (app `(coe_fn %%e) x) := (+) <$> count_coes x <*> (+1) <$> count_coes e | (expr.lam n bi t e) := do l ← mk_local' n bi t, count_coes $ e.instantiate_var l | e := do as ← e.get_simp_args, list.sum <$> as.mmap count_coes /-- Count how many coercions are inside the expression, excluding the top ones. -/ private meta def count_internal_coes (e : expr) : tactic ℕ := do ncoes ← count_coes e, pure $ ncoes - count_head_coes e /-- Classifies a declaration of type `ty` as a `norm_cast` rule. -/ meta def classify_type (ty : expr) : tactic label := do (_, ty) ← mk_local_pis ty, (lhs, rhs) ← match ty with | `(%%lhs = %%rhs) := pure (lhs, rhs) | `(%%lhs ↔ %%rhs) := pure (lhs, rhs) | _ := fail "norm_cast: lemma must be = or ↔" end, lhs_coes ← count_coes lhs, when (lhs_coes = 0) $ fail "norm_cast: badly shaped lemma, lhs must contain at least one coe", let lhs_head_coes := count_head_coes lhs, lhs_internal_coes ← count_internal_coes lhs, let rhs_head_coes := count_head_coes rhs, rhs_internal_coes ← count_internal_coes rhs, if lhs_head_coes = 0 then return elim else if lhs_head_coes = 1 then do when (rhs_head_coes ≠ 0) $ fail "norm_cast: badly shaped lemma, rhs can't start with coe", if rhs_internal_coes = 0 then return squash else return move else if rhs_head_coes < lhs_head_coes then do return squash else do fail "norm_cast: badly shaped shaped squash lemma, rhs must have fewer head coes than lhs" /-- The cache for `norm_cast` attribute stores three `simp_lemma` objects. -/ meta structure norm_cast_cache := (up : simp_lemmas) (down : simp_lemmas) (squash : simp_lemmas) /-- Empty `norm_cast_cache`. -/ meta def empty_cache : norm_cast_cache := { up := simp_lemmas.mk, down := simp_lemmas.mk, squash := simp_lemmas.mk, } meta instance : inhabited norm_cast_cache := ⟨empty_cache⟩ /-- `add_elim cache e` adds `e` as an `elim` lemma to `cache`. -/ meta def add_elim (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_up ← simp_lemmas.add cache.up e, return { up := new_up, down := cache.down, squash := cache.squash, } /-- `add_move cache e` adds `e` as a `move` lemma to `cache`. -/ meta def add_move (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do ty ← infer_type e, new_up ← cache.up.add e tt, new_down ← simp_lemmas.add cache.down e, return { up := new_up, down := new_down, squash := cache.squash, } /-- `add_squash cache e` adds `e` as an `squash` lemma to `cache`. -/ meta def add_squash (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_squash ← simp_lemmas.add cache.squash e, new_down ← simp_lemmas.add cache.down e, return { up := cache.up, down := new_down, squash := new_squash, } /-- The type of the `norm_cast` attribute. The optional label is used to overwrite the classifier. -/ meta def norm_cast_attr_ty : Type := user_attribute norm_cast_cache (option label) /-- Efficient getter for the `@[norm_cast]` attribute parameter that does not call `eval_expr`. See Note [user attribute parameters]. -/ meta def get_label_param (attr : norm_cast_attr_ty) (decl : name) : tactic (option label) := do p ← attr.get_param_untyped decl, match p with | `(none) := pure none | `(some label.elim) := pure label.elim | `(some label.move) := pure label.move | `(some label.squash) := pure label.squash | _ := fail p end /-- `add_lemma cache decl` infers the proper `norm_cast` attribute for `decl` and adds it to `cache`. -/ meta def add_lemma (attr : norm_cast_attr_ty) (cache : norm_cast_cache) (decl : name) : tactic norm_cast_cache := do e ← mk_const decl, param ← get_label_param attr decl, l ← param <|> (infer_type e >>= classify_type), match l with | elim := add_elim cache e | move := add_move cache e | squash := add_squash cache e end -- special lemmas to handle the ≥, > and ≠ operators private lemma ge_from_le {α} [has_le α] : ∀ (x y : α), x ≥ y ↔ y ≤ x := λ _ _, iff.rfl private lemma gt_from_lt {α} [has_lt α] : ∀ (x y : α), x > y ↔ y < x := λ _ _, iff.rfl private lemma ne_from_not_eq {α} : ∀ (x y : α), x ≠ y ↔ ¬(x = y) := λ _ _, iff.rfl /-- `mk_cache names` creates a `norm_cast_cache`. It infers the proper `norm_cast` attributes for names in `names`, and collects the lemmas attributed with specific `norm_cast` attributes. -/ meta def mk_cache (attr : thunk norm_cast_attr_ty) (names : list name) : tactic norm_cast_cache := do -- names has the declarations in reverse order cache ← names.mfoldr (λ name cache, add_lemma (attr ()) cache name) empty_cache, --some special lemmas to handle binary relations let up := cache.up, up ← up.add_simp ``ge_from_le, up ← up.add_simp ``gt_from_lt, up ← up.add_simp ``ne_from_not_eq, let down := cache.down, down ← down.add_simp ``coe_coe, pure { up := up, down := down, squash := cache.squash } /-- The `norm_cast` attribute. -/ @[user_attribute] meta def norm_cast_attr : user_attribute norm_cast_cache (option label) := { name := `norm_cast, descr := "attribute for norm_cast", parser := (do some l ← (label.of_string ∘ to_string) <$> ident, return l) <|> return none, after_set := some (λ decl prio persistent, do param ← get_label_param norm_cast_attr decl, match param with | some l := when (l ≠ elim) $ simp_attr.push_cast.set decl () tt | none := do e ← mk_const decl, ty ← infer_type e, l ← classify_type ty, norm_cast_attr.set decl l persistent prio end), before_unset := some $ λ _ _, tactic.skip, cache_cfg := { mk_cache := mk_cache norm_cast_attr, dependencies := [] } } /-- Classify a declaration as a `norm_cast` rule. -/ meta def make_guess (decl : name) : tactic label := do e ← mk_const decl, ty ← infer_type e, classify_type ty /-- Gets the `norm_cast` classification label for a declaration. Applies the override specified on the attribute, if necessary. -/ meta def get_label (decl : name) : tactic label := do param ← get_label_param norm_cast_attr decl, param <|> make_guess decl end norm_cast namespace tactic.interactive open norm_cast /-- `push_cast` rewrites the expression to move casts toward the leaf nodes. For example, `↑(a + b)` will be written to `↑a + ↑b`. Equivalent to `simp only with push_cast`. Can also be used at hypotheses. `push_cast` can also be used at hypotheses and with extra simp rules. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` -/ meta def push_cast (hs : parse tactic.simp_arg_list) (l : parse location) : tactic unit := tactic.interactive.simp none tt hs [`push_cast] l end tactic.interactive namespace norm_cast open tactic expr /-- Prove `a = b` using the given simp set. -/ meta def prove_eq_using (s : simp_lemmas) (a b : expr) : tactic expr := do (a', a_a') ← simplify s [] a {fail_if_unchanged := ff}, (b', b_b') ← simplify s [] b {fail_if_unchanged := ff}, on_exception (trace_norm_cast "failed: " (to_expr ``(%%a' = %%b') >>= pp)) $ is_def_eq a' b' reducible, b'_b ← mk_eq_symm b_b', mk_eq_trans a_a' b'_b /-- Prove `a = b` by simplifying using move and squash lemmas. -/ meta def prove_eq_using_down (a b : expr) : tactic expr := do cache ← norm_cast_attr.get_cache, trace_norm_cast "proving: " (to_expr ``(%%a = %%b) >>= pp), prove_eq_using cache.down a b /-- This is the main heuristic used alongside the elim and move lemmas. The goal is to help casts move past operators by adding intermediate casts. An expression of the shape: op (↑(x : α) : γ) (↑(y : β) : γ) is rewritten to: op (↑(↑(x : α) : β) : γ) (↑(y : β) : γ) when (↑(↑(x : α) : β) : γ) = (↑(x : α) : γ) can be proven with a squash lemma -/ meta def splitting_procedure : expr → tactic (expr × expr) | (app (app op x) y) := (do `(@coe %%α %%δ %%coe1 %%xx) ← return x, `(@coe %%β %%γ %%coe2 %%yy) ← return y, success_if_fail $ is_def_eq α β, is_def_eq δ γ, (do coe3 ← mk_app `has_lift_t [α, β] >>= mk_instance_fast, new_x ← to_expr ``(@coe %%β %%δ %%coe2 (@coe %%α %%β %%coe3 %%xx)), let new_e := app (app op new_x) y, eq_x ← prove_eq_using_down x new_x, pr ← mk_congr_arg op eq_x, pr ← mk_congr_fun pr y, return (new_e, pr) ) <|> (do coe3 ← mk_app `has_lift_t [β, α] >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%δ %%coe1 (@coe %%β %%α %%coe3 %%yy)), let new_e := app (app op x) new_y, eq_y ← prove_eq_using_down y new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_one.one %%β %%h1) ← return y, h2 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_zero.zero %%β %%h1) ← return y, h2 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@has_one.one %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) <|> (do `(@has_zero.zero %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) | _ := failed /-- Discharging function used during simplification in the "squash" step. TODO: norm_cast takes a list of expressions to use as lemmas for the discharger TODO: a tactic to print the results the discharger fails to proove -/ private meta def prove : tactic unit := assumption /-- Core rewriting function used in the "squash" step, which moves casts upwards and eliminates them. It tries to rewrite an expression using the elim and move lemmas. On failure, it calls the splitting procedure heuristic. -/ meta def upward_and_elim (s : simp_lemmas) (e : expr) : tactic (expr × expr) := (do r ← mcond (is_prop e) (return `iff) (return `eq), (new_e, pr) ← s.rewrite e prove r, pr ← match r with | `iff := mk_app `propext [pr] | _ := return pr end, return (new_e, pr) ) <|> splitting_procedure e /-! The following auxiliary functions are used to handle numerals. -/ /-- If possible, rewrite `(n : α)` to `((n : ℕ) : α)` where `n` is a numeral and `α ≠ ℕ`. Returns a pair of the new expression and proof that they are equal. -/ meta def numeral_to_coe (e : expr) : tactic (expr × expr) := do α ← infer_type e, success_if_fail $ is_def_eq α `(ℕ), n ← e.to_nat, h1 ← mk_app `has_lift_t [`(ℕ), α] >>= mk_instance_fast, let new_e : expr := reflect n, new_e ← to_expr ``(@coe ℕ %%α %%h1 %%new_e), pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- If possible, rewrite `((n : ℕ) : α)` to `(n : α)` where `n` is a numeral. Returns a pair of the new expression and proof that they are equal. -/ meta def coe_to_numeral (e : expr) : tactic (expr × expr) := do `(@coe ℕ %%α %%h1 %%e') ← return e, n ← e'.to_nat, -- replace e' by normalized numeral is_def_eq (reflect n) e' reducible, let e := e.app_fn (reflect n), new_e ← expr.of_nat α n, pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- A local variant on `simplify_top_down`. -/ private meta def simplify_top_down' {α} (a : α) (pre : α → expr → tactic (α × expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (α × expr × expr) := ext_simplify_core a cfg simp_lemmas.mk (λ _, failed) (λ a _ _ _ e, do (new_a, new_e, pr) ← pre a e, guard (¬ new_e =ₐ e), return (new_a, new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e /-- The core simplification routine of `norm_cast`. -/ meta def derive (e : expr) : tactic (expr × expr) := do cache ← norm_cast_attr.get_cache, e ← instantiate_mvars e, let cfg : simp_config := { zeta := ff, beta := ff, eta := ff, proj := ff, iota := ff, iota_eqn := ff, fail_if_unchanged := ff }, let e0 := e, -- step 1: pre-processing of numerals ((), e1, pr1) ← simplify_top_down' () (λ _ e, prod.mk () <$> numeral_to_coe e) e0 cfg, trace_norm_cast "after numeral_to_coe: " e1, -- step 2: casts are moved upwards and eliminated ((), e2, pr2) ← simplify_bottom_up () (λ _ e, prod.mk () <$> upward_and_elim cache.up e) e1 cfg, trace_norm_cast "after upward_and_elim: " e2, -- step 3: casts are squashed (e3, pr3) ← simplify cache.squash [] e2 cfg, trace_norm_cast "after squashing: " e3, -- step 4: post-processing of numerals ((), e4, pr4) ← simplify_top_down' () (λ _ e, prod.mk () <$> coe_to_numeral e) e3 cfg, trace_norm_cast "after coe_to_numeral: " e4, let new_e := e4, guard (¬ new_e =ₐ e), pr ← mk_eq_trans pr1 pr2, pr ← mk_eq_trans pr pr3, pr ← mk_eq_trans pr pr4, return (new_e, pr) end norm_cast namespace tactic open expr norm_cast /-- `aux_mod_cast e` runs `norm_cast` on `e` and returns the result. If `include_goal` is true, it also normalizes the goal. -/ meta def aux_mod_cast (e : expr) (include_goal : bool := tt) : tactic expr := match e with | local_const _ lc _ _ := do e ← get_local lc, replace_at derive [e] include_goal, get_local lc | e := do t ← infer_type e, e ← assertv `this t e, replace_at derive [e] include_goal, get_local `this end /-- `exact_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to use `e` to close the goal. -/ meta def exact_mod_cast (e : expr) : tactic unit := decorate_error "exact_mod_cast failed:" $ do new_e ← aux_mod_cast e, exact new_e /-- `apply_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to apply `e`. -/ meta def apply_mod_cast (e : expr) : tactic (list (name × expr)) := decorate_error "apply_mod_cast failed:" $ do new_e ← aux_mod_cast e, apply new_e /-- `assumption_mod_cast` runs `norm_cast` on the goal. For each local hypothesis `h`, it also normalizes `h` and tries to use that to close the goal. -/ meta def assumption_mod_cast : tactic unit := decorate_error "assumption_mod_cast failed:" $ do let cfg : simp_config := { fail_if_unchanged := ff, canonize_instances := ff, canonize_proofs := ff, proj := ff }, replace_at derive [] tt, ctx ← local_context, try_lst $ ctx.map (λ h, aux_mod_cast h ff >>= tactic.exact) end tactic namespace tactic.interactive open tactic norm_cast /-- Normalize casts at the given locations by moving them "upwards". As opposed to simp, norm_cast can be used without necessarily closing the goal. -/ meta def norm_cast (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← replace_at derive ns loc.include_goal | fail "norm_cast failed to simplify", when loc.include_goal $ try tactic.reflexivity, when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Rewrite with the given rules and normalize casts between steps. -/ meta def rw_mod_cast (rs : parse rw_rules) (loc : parse location) : tactic unit := decorate_error "rw_mod_cast failed:" $ do let cfg_norm : simp_config := {}, let cfg_rw : rewrite_cfg := {}, ns ← loc.get_locals, monad.mapm' (λ r : rw_rule, do save_info r.pos, replace_at derive ns loc.include_goal, rw ⟨[r], none⟩ loc {} ) rs.rules, replace_at derive ns loc.include_goal, skip /-- Normalize the goal and the given expression, then close the goal with exact. -/ meta def exact_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr e <|> do { ty ← target, e ← i_to_expr_strict ``(%%e : %%ty), pty ← pp ty, ptgt ← pp e, fail ("exact_mod_cast failed, expression type not directly " ++ "inferrable. Try:\n\nexact_mod_cast ...\nshow " ++ to_fmt pty ++ ",\nfrom " ++ ptgt : format) }, tactic.exact_mod_cast e /-- Normalize the goal and the given expression, then apply the expression to the goal. -/ meta def apply_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr_for_apply e, concat_tags $ tactic.apply_mod_cast e /-- Normalize the goal and every expression in the local context, then close the goal with assumption. -/ meta def assumption_mod_cast : tactic unit := tactic.assumption_mod_cast end tactic.interactive namespace conv.interactive open conv open norm_cast (derive) /-- the converter version of `norm_cast' -/ meta def norm_cast : conv unit := replace_lhs derive end conv.interactive -- TODO: move this elsewhere? @[norm_cast] lemma ite_cast {α β} [has_lift_t α β] {c : Prop} [decidable c] {a b : α} : ↑(ite c a b) = ite c (↑a : β) (↑b : β) := by by_cases h : c; simp [h] add_hint_tactic "norm_cast at *" /-- The `norm_cast` family of tactics is used to normalize casts inside expressions. It is basically a simp tactic with a specific set of lemmas to move casts upwards in the expression. Therefore it can be used more safely as a non-terminating tactic. It also has special handling of numerals. For instance, given an assumption ```lean a b : ℤ h : ↑a + ↑b < (10 : ℚ) ``` writing `norm_cast at h` will turn `h` into ```lean h : a + b < 10 ``` You can also use `exact_mod_cast`, `apply_mod_cast`, `rw_mod_cast` or `assumption_mod_cast`. Writing `exact_mod_cast h` and `apply_mod_cast h` will normalize the goal and `h` before using `exact h` or `apply h`. Writing `assumption_mod_cast` will normalize the goal and for every expression `h` in the context it will try to normalize `h` and use `exact h`. `rw_mod_cast` acts like the `rw` tactic but it applies `norm_cast` between steps. `push_cast` rewrites the expression to move casts toward the leaf nodes. This uses `norm_cast` lemmas in the forward direction. For example, `↑(a + b)` will be written to `↑a + ↑b`. It is equivalent to `simp only with push_cast`. It can also be used at hypotheses with `push_cast at h` and with extra simp lemmas with `push_cast [int.add_zero]`. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` The implementation and behavior of the `norm_cast` family is described in detail at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast", category := doc_category.tactic, decl_names := [``tactic.interactive.norm_cast, ``tactic.interactive.rw_mod_cast, ``tactic.interactive.apply_mod_cast, ``tactic.interactive.assumption_mod_cast, ``tactic.interactive.exact_mod_cast, ``tactic.interactive.push_cast], tags := ["coercions", "simplification"] } /-- The `norm_cast` attribute should be given to lemmas that describe the behaviour of a coercion in regard to an operator, a relation, or a particular function. It only concerns equality or iff lemmas involving `↑`, `⇑` and `↥`, describing the behavior of the coercion functions. It does not apply to the explicit functions that define the coercions. Examples: ```lean @[norm_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n @[norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 @[norm_cast] theorem cast_id : ∀ n : ℚ, ↑n = n @[norm_cast] theorem coe_nat_add (m n : ℕ) : (↑(m + n) : ℤ) = ↑m + ↑n @[norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) : ((n - m : ℕ) : α) = n - m @[norm_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n @[norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n @[norm_cast] theorem cast_one : ((1 : ℚ) : α) = 1 ``` Lemmas tagged with `@[norm_cast]` are classified into three categories: `move`, `elim`, and `squash`. They are classified roughly as follows: * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes `norm_cast` uses `move` and `elim` lemmas to factor coercions toward the root of an expression and to cancel them from both sides of an equation or relation. It uses `squash` lemmas to clean up the result. Occasionally you may want to override the automatic classification. You can do this by giving an optional `elim`, `move`, or `squash` parameter to the attribute. ```lean @[simp, norm_cast elim] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] ``` Don't do this unless you understand what you are doing. A full description of the tactic, and the use of each lemma category, can be found at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast attributes", category := doc_category.attr, decl_names := [``norm_cast.norm_cast_attr], tags := ["coercions", "simplification"] } -- Lemmas defined in core. attribute [norm_cast] int.nat_abs_of_nat int.coe_nat_sub int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_add -- Lemmas about nat.succ need to get a low priority, so that they are tried last. -- This is because `nat.succ _` matches `1`, `3`, `x+1`, etc. -- Rewriting would then produce really wrong terms. attribute [norm_cast, priority 500] int.coe_nat_succ
7c372afdb89731c4ed7666b5280a68740d850cb8
675b8263050a5d74b89ceab381ac81ce70535688
/src/topology/algebra/ordered.lean
1b231222a790ef9af9c99879150fa9d98358dca3
[ "Apache-2.0" ]
permissive
vozor/mathlib
5921f55235ff60c05f4a48a90d616ea167068adf
f7e728ad8a6ebf90291df2a4d2f9255a6576b529
refs/heads/master
1,675,607,702,231
1,609,023,279,000
1,609,023,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
151,862
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, Yury Kudryashov -/ import tactic.linarith import tactic.tfae import algebra.archimedean import algebra.group.pi import algebra.ordered_ring import order.liminf_limsup import data.set.intervals.image_preimage import data.set.intervals.ord_connected import data.set.intervals.surj_on import data.set.intervals.pi import topology.algebra.group import topology.extend_from_subset import order.filter.interval /-! # Theory of topology on ordered spaces ## Main definitions The order topology on an ordered space is the topology generated by all open intervals (or equivalently by those of the form `(-∞, a)` and `(b, +∞)`). We define it as `preorder.topology α`. However, we do *not* register it as an instance (as many existing ordered types already have topologies, which would be equal but not definitionally equal to `preorder.topology α`). Instead, we introduce a class `order_topology α`(which is a `Prop`, also known as a mixin) saying that on the type `α` having already a topological space structure and a preorder structure, the topological structure is equal to the order topology. We also introduce another (mixin) class `order_closed_topology α` saying that the set of points `(x, y)` with `x ≤ y` is closed in the product space. This is automatically satisfied on a linear order with the order topology. We prove many basic properties of such topologies. ## Main statements This file contains the proofs of the following facts. For exact requirements (`order_closed_topology` vs `order_topology`, `preorder` vs `partial_order` vs `linear_order` etc) see their statements. ### Open / closed sets * `is_open_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open; * `is_open_Iio`, `is_open_Ioi`, `is_open_Ioo` : open intervals are open; * `is_closed_le` : if `f` and `g` are continuous functions, then `{x | f x ≤ g x}` is closed; * `is_closed_Iic`, `is_closed_Ici`, `is_closed_Icc` : closed intervals are closed; * `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x ≤ g x}` and `{x | f x < g x}` are included by `{x | f x = g x}`; * `exists_Ioc_subset_of_mem_nhds`, `exists_Ico_subset_of_mem_nhds` : if `x < y`, then any neighborhood of `x` includes an interval `[x, z)` for some `z ∈ (x, y]`, and any neighborhood of `y` includes an interval `(z, y]` for some `z ∈ [x, y)`. ### Convergence and inequalities * `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually `f x ≤ g x`, then `a ≤ b` * `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x ≤ b` (resp., `b ≤ f x`), then `a ≤ b` (resp., `b ≤ a); we also provide primed versions that assume the inequalities to hold for all `x`. ### Min, max, `Sup` and `Inf` * `continuous.min`, `continuous.max`: pointwise `min`/`max` of two continuous functions is continuous. * `tendsto.min`, `tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise `min`/`max` tend to `min a b` and `max a b`, respectively. * `tendsto_of_tendsto_of_tendsto_of_le_of_le` : theorem known as squeeze theorem, sandwich theorem, theorem of Carabinieri, and two policemen (and a drunk) theorem; if `g` and `h` both converge to `a`, and eventually `g x ≤ f x ≤ h x`, then `f` converges to `a`. ### Connected sets and Intermediate Value Theorem * `is_preconnected_I??` : all intervals `I??` are preconnected, * `is_preconnected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for connected sets and connected spaces, respectively; * `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions on closed intervals. ### Miscellaneous facts * `is_compact.exists_forall_le`, `is_compact.exists_forall_ge` : extreme value theorem, a continuous function on a compact set takes its minimum and maximum values. * `is_closed.Icc_subset_of_forall_mem_nhds_within` : “Continuous induction” principle; if `s ∩ [a, b]` is closed, `a ∈ s`, and for each `x ∈ [a, b) ∩ s` some of its right neighborhoods is included `s`, then `[a, b] ⊆ s`. * `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two other versions of the “continuous induction” principle. ## Implementation We do _not_ register the order topology as an instance on a preorder (or even on a linear order). Indeed, on many such spaces, a topology has already been constructed in a different way (think of the discrete spaces `ℕ` or `ℤ`, or `ℝ` that could inherit a topology as the completion of `ℚ`), and is in general not defeq to the one generated by the intervals. We make it available as a definition `preorder.topology α` though, that can be registered as an instance when necessary, or for specific types. -/ open classical set filter topological_space open function (curry uncurry) open_locale topological_space classical filter universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the set of points `(x, y)` with `x ≤ y` is closed in the product space. We introduce this as a mixin. This property is satisfied for the order topology on a linear order, but it can be satisfied more generally, and suffices to derive many interesting properties relating order and topology. -/ class order_closed_topology (α : Type*) [topological_space α] [preorder α] : Prop := (is_closed_le' : is_closed {p:α×α | p.1 ≤ p.2}) instance : Π [topological_space α], topological_space (order_dual α) := id section order_closed_topology section preorder variables [topological_space α] [preorder α] [t : order_closed_topology α] include t lemma is_closed_le_prod : is_closed {p : α × α | p.1 ≤ p.2} := t.is_closed_le' lemma is_closed_le [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {b | f b ≤ g b} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_le_prod lemma is_closed_le' (a : α) : is_closed {b | b ≤ a} := is_closed_le continuous_id continuous_const lemma is_closed_Iic {a : α} : is_closed (Iic a) := is_closed_le' a lemma is_closed_ge' (a : α) : is_closed {b | a ≤ b} := is_closed_le continuous_const continuous_id lemma is_closed_Ici {a : α} : is_closed (Ici a) := is_closed_ge' a instance : order_closed_topology (order_dual α) := ⟨(@order_closed_topology.is_closed_le' α _ _ _).preimage continuous_swap⟩ lemma is_closed_Icc {a b : α} : is_closed (Icc a b) := is_closed_inter is_closed_Ici is_closed_Iic @[simp] lemma closure_Icc (a b : α) : closure (Icc a b) = Icc a b := is_closed_Icc.closure_eq @[simp] lemma closure_Iic (a : α) : closure (Iic a) = Iic a := is_closed_Iic.closure_eq @[simp] lemma closure_Ici (a : α) : closure (Ici a) = Ici a := is_closed_Ici.closure_eq lemma le_of_tendsto_of_tendsto {f g : β → α} {b : filter β} {a₁ a₂ : α} [ne_bot b] (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) (h : f ≤ᶠ[b] g) : a₁ ≤ a₂ := have tendsto (λb, (f b, g b)) b (𝓝 (a₁, a₂)), by rw [nhds_prod_eq]; exact hf.prod_mk hg, show (a₁, a₂) ∈ {p:α×α | p.1 ≤ p.2}, from t.is_closed_le'.mem_of_tendsto this h lemma le_of_tendsto_of_tendsto' {f g : β → α} {b : filter β} {a₁ a₂ : α} [ne_bot b] (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) (h : ∀ x, f x ≤ g x) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto hf hg (eventually_of_forall h) lemma le_of_tendsto {f : β → α} {a b : α} {x : filter β} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ᶠ c in x, f c ≤ b) : a ≤ b := le_of_tendsto_of_tendsto lim tendsto_const_nhds h lemma le_of_tendsto' {f : β → α} {a b : α} {x : filter β} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ c, f c ≤ b) : a ≤ b := le_of_tendsto lim (eventually_of_forall h) lemma ge_of_tendsto {f : β → α} {a b : α} {x : filter β} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ᶠ c in x, b ≤ f c) : b ≤ a := le_of_tendsto_of_tendsto tendsto_const_nhds lim h lemma ge_of_tendsto' {f : β → α} {a b : α} {x : filter β} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : ∀ c, b ≤ f c) : b ≤ a := ge_of_tendsto lim (eventually_of_forall h) @[simp] lemma closure_le_eq [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : closure {b | f b ≤ g b} = {b | f b ≤ g b} := (is_closed_le hf hg).closure_eq lemma closure_lt_subset_le [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : closure {b | f b < g b} ⊆ {b | f b ≤ g b} := by { rw [←closure_le_eq hf hg], exact closure_mono (λ b, le_of_lt) } lemma continuous_within_at.closure_le [topological_space β] {f g : β → α} {s : set β} {x : β} (hx : x ∈ closure s) (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : ∀ y ∈ s, f y ≤ g y) : f x ≤ g x := show (f x, g x) ∈ {p : α × α | p.1 ≤ p.2}, from order_closed_topology.is_closed_le'.closure_subset ((hf.prod hg).mem_closure hx h) /-- If `s` is a closed set and two functions `f` and `g` are continuous on `s`, then the set `{x ∈ s | f x ≤ g x}` is a closed set. -/ lemma is_closed.is_closed_le [topological_space β] {f g : β → α} {s : set β} (hs : is_closed s) (hf : continuous_on f s) (hg : continuous_on g s) : is_closed {x ∈ s | f x ≤ g x} := (hf.prod hg).preimage_closed_of_closed hs order_closed_topology.is_closed_le' omit t lemma nhds_within_Ici_ne_bot {a b : α} (H₂ : a ≤ b) : ne_bot (𝓝[Ici a] b) := nhds_within_ne_bot_of_mem H₂ @[instance] lemma nhds_within_Ici_self_ne_bot (a : α) : ne_bot (𝓝[Ici a] a) := nhds_within_Ici_ne_bot (le_refl a) lemma nhds_within_Iic_ne_bot {a b : α} (H : a ≤ b) : ne_bot (𝓝[Iic b] a) := nhds_within_ne_bot_of_mem H @[instance] lemma nhds_within_Iic_self_ne_bot (a : α) : ne_bot (𝓝[Iic a] a) := nhds_within_Iic_ne_bot (le_refl a) end preorder section partial_order variables [topological_space α] [partial_order α] [t : order_closed_topology α] include t private lemma is_closed_eq : is_closed {p : α × α | p.1 = p.2} := by simp only [le_antisymm_iff]; exact is_closed_inter t.is_closed_le' (is_closed_le continuous_snd continuous_fst) @[priority 90] -- see Note [lower instance priority] instance order_closed_topology.to_t2_space : t2_space α := { t2 := have is_open {p : α × α | p.1 ≠ p.2}, from is_closed_eq, assume a b h, let ⟨u, v, hu, hv, ha, hb, h⟩ := is_open_prod_iff.mp this a b h in ⟨u, v, hu, hv, ha, hb, set.eq_empty_iff_forall_not_mem.2 $ assume a ⟨h₁, h₂⟩, have a ≠ a, from @h (a, a) ⟨h₁, h₂⟩, this rfl⟩ } end partial_order section linear_order variables [topological_space α] [linear_order α] [order_closed_topology α] lemma is_open_lt_prod : is_open {p : α × α | p.1 < p.2} := by { simp_rw [← is_closed_compl_iff, compl_set_of, not_lt], exact is_closed_le continuous_snd continuous_fst } lemma is_open_lt [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_open {b | f b < g b} := by simp [lt_iff_not_ge, -not_le]; exact is_closed_le hg hf variables {a b : α} lemma is_open_Iio : is_open (Iio a) := is_open_lt continuous_id continuous_const lemma is_open_Ioi : is_open (Ioi a) := is_open_lt continuous_const continuous_id lemma is_open_Ioo : is_open (Ioo a b) := is_open_inter is_open_Ioi is_open_Iio @[simp] lemma interior_Ioi : interior (Ioi a) = Ioi a := is_open_Ioi.interior_eq @[simp] lemma interior_Iio : interior (Iio a) = Iio a := is_open_Iio.interior_eq @[simp] lemma interior_Ioo : interior (Ioo a b) = Ioo a b := is_open_Ioo.interior_eq variables [topological_space γ] /-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions on a preconnected space and `f a ≤ g a` and `g b ≤ f b`, then for some `x` we have `f x = g x`. -/ lemma intermediate_value_univ₂ [preconnected_space γ] {a b : γ} {f g : γ → α} (hf : continuous f) (hg : continuous g) (ha : f a ≤ g a) (hb : g b ≤ f b) : ∃ x, f x = g x := begin obtain ⟨x, h, hfg, hgf⟩ : (univ ∩ {x | f x ≤ g x ∧ g x ≤ f x}).nonempty, from is_preconnected_closed_iff.1 preconnected_space.is_preconnected_univ _ _ (is_closed_le hf hg) (is_closed_le hg hf) (λ x hx, le_total _ _) ⟨a, trivial, ha⟩ ⟨b, trivial, hb⟩, exact ⟨x, le_antisymm hfg hgf⟩ end /-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous on a preconnected set `s` and for some `a b ∈ s` we have `f a ≤ g a` and `g b ≤ f b`, then for some `x ∈ s` we have `f x = g x`. -/ lemma is_preconnected.intermediate_value₂ {s : set γ} (hs : is_preconnected s) {a b : γ} (ha : a ∈ s) (hb : b ∈ s) {f g : γ → α} (hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (hb' : g b ≤ f b) : ∃ x ∈ s, f x = g x := let ⟨x, hx⟩ := @intermediate_value_univ₂ α s _ _ _ _ (subtype.preconnected_space hs) ⟨a, ha⟩ ⟨b, hb⟩ _ _ (continuous_on_iff_continuous_restrict.1 hf) (continuous_on_iff_continuous_restrict.1 hg) ha' hb' in ⟨x, x.2, hx⟩ /-- Intermediate Value Theorem for continuous functions on connected sets. -/ lemma is_preconnected.intermediate_value {s : set γ} (hs : is_preconnected s) {a b : γ} (ha : a ∈ s) (hb : b ∈ s) {f : γ → α} (hf : continuous_on f s) : Icc (f a) (f b) ⊆ f '' s := λ x hx, mem_image_iff_bex.2 $ hs.intermediate_value₂ ha hb hf continuous_on_const hx.1 hx.2 /-- Intermediate Value Theorem for continuous functions on connected spaces. -/ lemma intermediate_value_univ [preconnected_space γ] (a b : γ) {f : γ → α} (hf : continuous f) : Icc (f a) (f b) ⊆ range f := λ x hx, intermediate_value_univ₂ hf continuous_const hx.1 hx.2 /-- Intermediate Value Theorem for continuous functions on connected spaces. -/ lemma mem_range_of_exists_le_of_exists_ge [preconnected_space γ] {c : α} {f : γ → α} (hf : continuous f) (h₁ : ∃ a, f a ≤ c) (h₂ : ∃ b, c ≤ f b) : c ∈ range f := let ⟨a, ha⟩ := h₁, ⟨b, hb⟩ := h₂ in intermediate_value_univ a b hf ⟨ha, hb⟩ /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ lemma is_preconnected.Icc_subset {s : set α} (hs : is_preconnected s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : Icc a b ⊆ s := by simpa only [image_id] using hs.intermediate_value ha hb continuous_on_id /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ lemma is_connected.Icc_subset {s : set α} (hs : is_connected s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : Icc a b ⊆ s := hs.2.Icc_subset ha hb /-- If preconnected set in a linear order space is unbounded below and above, then it is the whole space. -/ lemma is_preconnected.eq_univ_of_unbounded {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s) (ha : ¬bdd_above s) : s = univ := begin refine eq_univ_of_forall (λ x, _), obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := not_bdd_below_iff.1 hb x, obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x, exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ end /-! ### Neighborhoods to the left and to the right on an `order_closed_topology` Limits to the left and to the right of real functions are defined in terms of neighborhoods to the left and to the right, either open or closed, i.e., members of `𝓝[Ioi a] a` and `𝓝[Ici a] a` on the right, and similarly on the left. Here we simply prove that all right-neighborhoods of a point are equal, and we'll prove later other useful characterizations which require the stronger hypothesis `order_topology α` -/ /-! #### Right neighborhoods, point excluded -/ lemma Ioo_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) : Ioo a c ∈ 𝓝[Ioi b] b := mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2, by rw [inter_comm, Ioi_inter_Iio]; exact Ioo_subset_Ioo_left H.1⟩ lemma Ioc_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) : Ioc a c ∈ 𝓝[Ioi b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) : Ico a c ∈ 𝓝[Ioi b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ico_self lemma Icc_mem_nhds_within_Ioi {a b c : α} (H : b ∈ Ico a c) : Icc a c ∈ 𝓝[Ioi b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Icc_self @[simp] lemma nhds_within_Ioc_eq_nhds_within_Ioi {a b : α} (h : a < b) : 𝓝[Ioc a b] a = 𝓝[Ioi a] a := le_antisymm (nhds_within_mono _ Ioc_subset_Ioi_self) $ nhds_within_le_of_mem $ Ioc_mem_nhds_within_Ioi $ left_mem_Ico.2 h @[simp] lemma nhds_within_Ioo_eq_nhds_within_Ioi {a b : α} (h : a < b) : 𝓝[Ioo a b] a = 𝓝[Ioi a] a := le_antisymm (nhds_within_mono _ Ioo_subset_Ioi_self) $ nhds_within_le_of_mem $ Ioo_mem_nhds_within_Ioi $ left_mem_Ico.2 h @[simp] lemma continuous_within_at_Ioc_iff_Ioi [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ioc a b) a ↔ continuous_within_at f (Ioi a) a := by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Ioi h] @[simp] lemma continuous_within_at_Ioo_iff_Ioi [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ioo a b) a ↔ continuous_within_at f (Ioi a) a := by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Ioi h] /-! #### Left neighborhoods, point excluded -/ lemma Ioo_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) : Ioo a c ∈ 𝓝[Iio b] b := by simpa only [dual_Ioo] using @Ioo_mem_nhds_within_Ioi (order_dual α) _ _ _ _ _ _ ⟨H.2, H.1⟩ lemma Ico_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) : Ico a c ∈ 𝓝[Iio b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ico_self lemma Ioc_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[Iio b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ioc_self lemma Icc_mem_nhds_within_Iio {a b c : α} (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[Iio b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Icc_self @[simp] lemma nhds_within_Ico_eq_nhds_within_Iio {a b : α} (h : a < b) : 𝓝[Ico a b] b = 𝓝[Iio b] b := by simpa only [dual_Ioc] using @nhds_within_Ioc_eq_nhds_within_Ioi (order_dual α) _ _ _ _ _ h @[simp] lemma nhds_within_Ioo_eq_nhds_within_Iio {a b : α} (h : a < b) : 𝓝[Ioo a b] b = 𝓝[Iio b] b := by simpa only [dual_Ioo] using @nhds_within_Ioo_eq_nhds_within_Ioi (order_dual α) _ _ _ _ _ h @[simp] lemma continuous_within_at_Ico_iff_Iio [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ico a b) b ↔ continuous_within_at f (Iio b) b := by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Iio h] @[simp] lemma continuous_within_at_Ioo_iff_Iio [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ioo a b) b ↔ continuous_within_at f (Iio b) b := by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Iio h] /-! #### Right neighborhoods, point included -/ lemma Ioo_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ioo a c) : Ioo a c ∈ 𝓝[Ici b] b := mem_nhds_within_of_mem_nhds $ mem_nhds_sets is_open_Ioo H lemma Ioc_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ioo a c) : Ioc a c ∈ 𝓝[Ici b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Ici H) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ico a c) : Ico a c ∈ 𝓝[Ici b] b := mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2, by simp only [inter_comm, Ici_inter_Iio, Ico_subset_Ico_left H.1]⟩ lemma Icc_mem_nhds_within_Ici {a b c : α} (H : b ∈ Ico a c) : Icc a c ∈ 𝓝[Ici b] b := mem_sets_of_superset (Ico_mem_nhds_within_Ici H) Ico_subset_Icc_self @[simp] lemma nhds_within_Icc_eq_nhds_within_Ici {a b : α} (h : a < b) : 𝓝[Icc a b] a = 𝓝[Ici a] a := le_antisymm (nhds_within_mono _ Icc_subset_Ici_self) $ nhds_within_le_of_mem $ Icc_mem_nhds_within_Ici $ left_mem_Ico.2 h @[simp] lemma nhds_within_Ico_eq_nhds_within_Ici {a b : α} (h : a < b) : 𝓝[Ico a b] a = 𝓝[Ici a] a := le_antisymm (nhds_within_mono _ (λ x, and.left)) $ nhds_within_le_of_mem $ Ico_mem_nhds_within_Ici $ left_mem_Ico.2 h @[simp] lemma continuous_within_at_Icc_iff_Ici [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Icc a b) a ↔ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Ici h] @[simp] lemma continuous_within_at_Ico_iff_Ici [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ico a b) a ↔ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Ici h] /-! #### Left neighborhoods, point included -/ lemma Ioo_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioo a c) : Ioo a c ∈ 𝓝[Iic b] b := mem_nhds_within_of_mem_nhds $ mem_nhds_sets is_open_Ioo H lemma Ico_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioo a c) : Ico a c ∈ 𝓝[Iic b] b := mem_sets_of_superset (Ioo_mem_nhds_within_Iic H) Ioo_subset_Ico_self lemma Ioc_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[Iic b] b := by simpa only [dual_Ico] using @Ico_mem_nhds_within_Ici (order_dual α) _ _ _ _ _ _ ⟨H.2, H.1⟩ lemma Icc_mem_nhds_within_Iic {a b c : α} (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[Iic b] b := mem_sets_of_superset (Ioc_mem_nhds_within_Iic H) Ioc_subset_Icc_self @[simp] lemma nhds_within_Icc_eq_nhds_within_Iic {a b : α} (h : a < b) : 𝓝[Icc a b] b = 𝓝[Iic b] b := by simpa only [dual_Icc] using @nhds_within_Icc_eq_nhds_within_Ici (order_dual α) _ _ _ _ _ h @[simp] lemma nhds_within_Ioc_eq_nhds_within_Iic {a b : α} (h : a < b) : 𝓝[Ioc a b] b = 𝓝[Iic b] b := by simpa only [dual_Ico] using @nhds_within_Ico_eq_nhds_within_Ici (order_dual α) _ _ _ _ _ h @[simp] lemma continuous_within_at_Icc_iff_Iic [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Icc a b) b ↔ continuous_within_at f (Iic b) b := by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Iic h] @[simp] lemma continuous_within_at_Ioc_iff_Iic [topological_space β] {a b : α} {f : α → β} (h : a < b) : continuous_within_at f (Ioc a b) b ↔ continuous_within_at f (Iic b) b := by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Iic h] end linear_order section linear_order variables [topological_space α] [linear_order α] [order_closed_topology α] {f g : β → α} section variables [topological_space β] lemma frontier_le_subset_eq (hf : continuous f) (hg : continuous g) : frontier {b | f b ≤ g b} ⊆ {b | f b = g b} := begin rw [frontier_eq_closure_inter_closure, closure_le_eq hf hg], rintros b ⟨hb₁, hb₂⟩, refine le_antisymm hb₁ (closure_lt_subset_le hg hf _), convert hb₂ using 2, simp only [not_le.symm], refl end lemma frontier_lt_subset_eq (hf : continuous f) (hg : continuous g) : frontier {b | f b < g b} ⊆ {b | f b = g b} := by rw ← frontier_compl; convert frontier_le_subset_eq hg hf; simp [ext_iff, eq_comm] @[continuity] lemma continuous.min (hf : continuous f) (hg : continuous g) : continuous (λb, min (f b) (g b)) := have ∀b∈frontier {b | f b ≤ g b}, f b = g b, from assume b hb, frontier_le_subset_eq hf hg hb, continuous_if this hf hg @[continuity] lemma continuous.max (hf : continuous f) (hg : continuous g) : continuous (λb, max (f b) (g b)) := @continuous.min (order_dual α) _ _ _ _ _ _ _ hf hg end lemma continuous_min : continuous (λ p : α × α, min p.1 p.2) := continuous_fst.min continuous_snd lemma continuous_max : continuous (λ p : α × α, max p.1 p.2) := continuous_fst.max continuous_snd lemma tendsto.max {b : filter β} {a₁ a₂ : α} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) : tendsto (λb, max (f b) (g b)) b (𝓝 (max a₁ a₂)) := (continuous_max.tendsto (a₁, a₂)).comp (hf.prod_mk_nhds hg) lemma tendsto.min {b : filter β} {a₁ a₂ : α} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 a₂)) : tendsto (λb, min (f b) (g b)) b (𝓝 (min a₁ a₂)) := (continuous_min.tendsto (a₁, a₂)).comp (hf.prod_mk_nhds hg) end linear_order end order_closed_topology /-- The order topology on an ordered type is the topology generated by open intervals. We register it on a preorder, but it is mostly interesting in linear orders, where it is also order-closed. We define it as a mixin. If you want to introduce the order topology on a preorder, use `preorder.topology`. -/ class order_topology (α : Type*) [t : topological_space α] [preorder α] : Prop := (topology_eq_generate_intervals : t = generate_from {s | ∃a, s = Ioi a ∨ s = Iio a}) /-- (Order) topology on a partial order `α` generated by the subbase of open intervals `(a, ∞) = { x ∣ a < x }, (-∞ , b) = {x ∣ x < b}` for all `a, b` in `α`. We do not register it as an instance as many ordered sets are already endowed with the same topology, most often in a non-defeq way though. Register as a local instance when necessary. -/ def preorder.topology (α : Type*) [preorder α] : topological_space α := generate_from {s : set α | ∃ (a : α), s = {b : α | a < b} ∨ s = {b : α | b < a}} section order_topology instance {α : Type*} [topological_space α] [partial_order α] [order_topology α] : order_topology (order_dual α) := ⟨by convert @order_topology.topology_eq_generate_intervals α _ _ _; conv in (_ ∨ _) { rw or.comm }; refl⟩ section partial_order variables [topological_space α] [partial_order α] [t : order_topology α] include t lemma is_open_iff_generate_intervals {s : set α} : is_open s ↔ generate_open {s | ∃a, s = Ioi a ∨ s = Iio a} s := by rw [t.topology_eq_generate_intervals]; refl lemma is_open_lt' (a : α) : is_open {b:α | a < b} := by rw [@is_open_iff_generate_intervals α _ _ t]; exact generate_open.basic _ ⟨a, or.inl rfl⟩ lemma is_open_gt' (a : α) : is_open {b:α | b < a} := by rw [@is_open_iff_generate_intervals α _ _ t]; exact generate_open.basic _ ⟨a, or.inr rfl⟩ lemma lt_mem_nhds {a b : α} (h : a < b) : ∀ᶠ x in 𝓝 b, a < x := mem_nhds_sets (is_open_lt' _) h lemma le_mem_nhds {a b : α} (h : a < b) : ∀ᶠ x in 𝓝 b, a ≤ x := (𝓝 b).sets_of_superset (lt_mem_nhds h) $ assume b hb, le_of_lt hb lemma gt_mem_nhds {a b : α} (h : a < b) : ∀ᶠ x in 𝓝 a, x < b := mem_nhds_sets (is_open_gt' _) h lemma ge_mem_nhds {a b : α} (h : a < b) : ∀ᶠ x in 𝓝 a, x ≤ b := (𝓝 a).sets_of_superset (gt_mem_nhds h) $ assume b hb, le_of_lt hb lemma nhds_eq_order (a : α) : 𝓝 a = (⨅b ∈ Iio a, 𝓟 (Ioi b)) ⊓ (⨅b ∈ Ioi a, 𝓟 (Iio b)) := by rw [t.topology_eq_generate_intervals, nhds_generate_from]; from le_antisymm (le_inf (le_binfi $ assume b hb, infi_le_of_le {c : α | b < c} $ infi_le _ ⟨hb, b, or.inl rfl⟩) (le_binfi $ assume b hb, infi_le_of_le {c : α | c < b} $ infi_le _ ⟨hb, b, or.inr rfl⟩)) (le_infi $ assume s, le_infi $ assume ⟨ha, b, hs⟩, match s, ha, hs with | _, h, (or.inl rfl) := inf_le_left_of_le $ infi_le_of_le b $ infi_le _ h | _, h, (or.inr rfl) := inf_le_right_of_le $ infi_le_of_le b $ infi_le _ h end) lemma tendsto_order {f : β → α} {a : α} {x : filter β} : tendsto f x (𝓝 a) ↔ (∀ a' < a, ∀ᶠ b in x, a' < f b) ∧ (∀ a' > a, ∀ᶠ b in x, f b < a') := by simp [nhds_eq_order a, tendsto_inf, tendsto_infi, tendsto_principal] instance tendsto_Icc_class_nhds (a : α) : tendsto_Ixx_class Icc (𝓝 a) (𝓝 a) := begin simp only [nhds_eq_order, infi_subtype'], refine ((has_basis_infi_principal_finite _).inf (has_basis_infi_principal_finite _)).tendsto_Ixx_class (λ s hs, _), refine (ord_connected_bInter _).inter (ord_connected_bInter _); intros _ _, exacts [ord_connected_Ioi, ord_connected_Iio] end instance tendsto_Ico_class_nhds (a : α) : tendsto_Ixx_class Ico (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_class_nhds (a : α) : tendsto_Ixx_class Ioc (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_class_nhds (a : α) : tendsto_Ixx_class Ioo (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self) instance tendsto_Ixx_nhds_within (a : α) {s t : set α} {Ixx} [tendsto_Ixx_class Ixx (𝓝 a) (𝓝 a)] [tendsto_Ixx_class Ixx (𝓟 s) (𝓟 t)]: tendsto_Ixx_class Ixx (𝓝[s] a) (𝓝[t] a) := filter.tendsto_Ixx_class_inf /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold eventually for the filter. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le' {f g h : β → α} {b : filter β} {a : α} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : ∀ᶠ b in b, g b ≤ f b) (hfh : ∀ᶠ b in b, f b ≤ h b) : tendsto f b (𝓝 a) := tendsto_order.2 ⟨assume a' h', have ∀ᶠ b in b, a' < g b, from (tendsto_order.1 hg).left a' h', by filter_upwards [this, hgf] assume a, lt_of_lt_of_le, assume a' h', have ∀ᶠ b in b, h b < a', from (tendsto_order.1 hh).right a' h', by filter_upwards [this, hfh] assume a h₁ h₂, lt_of_le_of_lt h₂ h₁⟩ /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold everywhere. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le {f g h : β → α} {b : filter β} {a : α} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : g ≤ f) (hfh : f ≤ h) : tendsto f b (𝓝 a) := tendsto_of_tendsto_of_tendsto_of_le_of_le' hg hh (eventually_of_forall hgf) (eventually_of_forall hfh) lemma nhds_order_unbounded {a : α} (hu : ∃u, a < u) (hl : ∃l, l < a) : 𝓝 a = (⨅l (h₂ : l < a) u (h₂ : a < u), 𝓟 (Ioo l u)) := have ∃ u, u ∈ Ioi a, from hu, have ∃ l, l ∈ Iio a, from hl, by { simp only [nhds_eq_order, inf_binfi, binfi_inf, *, inf_principal, Ioi_inter_Iio], refl } lemma tendsto_order_unbounded {f : β → α} {a : α} {x : filter β} (hu : ∃u, a < u) (hl : ∃l, l < a) (h : ∀l u, l < a → a < u → ∀ᶠ b in x, l < f b ∧ f b < u) : tendsto f x (𝓝 a) := by rw [nhds_order_unbounded hu hl]; from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl, tendsto_infi.2 $ assume u, tendsto_infi.2 $ assume hu, tendsto_principal.2 $ h l u hl hu) end partial_order theorem induced_order_topology' {α : Type u} {β : Type v} [partial_order α] [ta : topological_space β] [partial_order β] [order_topology β] (f : α → β) (hf : ∀ {x y}, f x < f y ↔ x < y) (H₁ : ∀ {a x}, x < f a → ∃ b < a, x ≤ f b) (H₂ : ∀ {a x}, f a < x → ∃ b > a, f b ≤ x) : @order_topology _ (induced f ta) _ := begin letI := induced f ta, refine ⟨eq_of_nhds_eq_nhds (λ a, _)⟩, rw [nhds_induced, nhds_generate_from, nhds_eq_order (f a)], apply le_antisymm, { refine le_infi (λ s, le_infi $ λ hs, le_principal_iff.2 _), rcases hs with ⟨ab, b, rfl|rfl⟩, { exact mem_comap_sets.2 ⟨{x | f b < x}, mem_inf_sets_of_left $ mem_infi_sets _ $ mem_infi_sets (hf.2 ab) $ mem_principal_self _, λ x, hf.1⟩ }, { exact mem_comap_sets.2 ⟨{x | x < f b}, mem_inf_sets_of_right $ mem_infi_sets _ $ mem_infi_sets (hf.2 ab) $ mem_principal_self _, λ x, hf.1⟩ } }, { rw [← map_le_iff_le_comap], refine le_inf _ _; refine le_infi (λ x, le_infi $ λ h, le_principal_iff.2 _); simp, { rcases H₁ h with ⟨b, ab, xb⟩, refine mem_infi_sets _ (mem_infi_sets ⟨ab, b, or.inl rfl⟩ (mem_principal_sets.2 _)), exact λ c hc, lt_of_le_of_lt xb (hf.2 hc) }, { rcases H₂ h with ⟨b, ab, xb⟩, refine mem_infi_sets _ (mem_infi_sets ⟨ab, b, or.inr rfl⟩ (mem_principal_sets.2 _)), exact λ c hc, lt_of_lt_of_le (hf.2 hc) xb } }, end theorem induced_order_topology {α : Type u} {β : Type v} [partial_order α] [ta : topological_space β] [partial_order β] [order_topology β] (f : α → β) (hf : ∀ {x y}, f x < f y ↔ x < y) (H : ∀ {x y}, x < y → ∃ a, x < f a ∧ f a < y) : @order_topology _ (induced f ta) _ := induced_order_topology' f @hf (λ a x xa, let ⟨b, xb, ba⟩ := H xa in ⟨b, hf.1 ba, le_of_lt xb⟩) (λ a x ax, let ⟨b, ab, bx⟩ := H ax in ⟨b, hf.1 ab, le_of_lt bx⟩) /-- On an `ord_connected` subset of a linear order, the order topology for the restriction of the order is the same as the restriction to the subset of the order topology. -/ instance order_topology_of_ord_connected {α : Type u} [ta : topological_space α] [linear_order α] [order_topology α] {t : set α} [ht : ord_connected t] : order_topology t := begin letI := induced (coe : t → α) ta, refine ⟨eq_of_nhds_eq_nhds (λ a, _)⟩, rw [nhds_induced, nhds_generate_from, nhds_eq_order (a : α)], apply le_antisymm, { refine le_infi (λ s, le_infi $ λ hs, le_principal_iff.2 _), rcases hs with ⟨ab, b, rfl|rfl⟩, { refine ⟨Ioi b, _, λ _, id⟩, refine mem_inf_sets_of_left (mem_infi_sets b _), exact mem_infi_sets ab (mem_principal_self (Ioi ↑b)) }, { refine ⟨Iio b, _, λ _, id⟩, refine mem_inf_sets_of_right (mem_infi_sets b _), exact mem_infi_sets ab (mem_principal_self (Iio b)) } }, { rw [← map_le_iff_le_comap], refine le_inf _ _, { refine le_infi (λ x, le_infi $ λ h, le_principal_iff.2 _), by_cases hx : x ∈ t, { refine mem_infi_sets (Ioi ⟨x, hx⟩) (mem_infi_sets ⟨h, ⟨⟨x, hx⟩, or.inl rfl⟩⟩ _), exact λ _, id }, simp only [set_coe.exists, mem_set_of_eq, mem_map], convert univ_sets _, suffices hx' : ∀ (y : t), ↑y ∈ Ioi x, { simp [hx'] }, intros y, revert hx, contrapose!, -- here we use the `ord_connected` hypothesis exact λ hx, ht y.2 a.2 ⟨le_of_not_gt hx, le_of_lt h⟩ }, { refine le_infi (λ x, le_infi $ λ h, le_principal_iff.2 _), by_cases hx : x ∈ t, { refine mem_infi_sets (Iio ⟨x, hx⟩) (mem_infi_sets ⟨h, ⟨⟨x, hx⟩, or.inr rfl⟩⟩ _), exact λ _, id }, simp only [set_coe.exists, mem_set_of_eq, mem_map], convert univ_sets _, suffices hx' : ∀ (y : t), ↑y ∈ Iio x, { simp [hx'] }, intros y, revert hx, contrapose!, -- here we use the `ord_connected` hypothesis exact λ hx, ht a.2 y.2 ⟨le_of_lt h, le_of_not_gt hx⟩ } } end lemma nhds_top_order [topological_space α] [order_top α] [order_topology α] : 𝓝 (⊤:α) = (⨅l (h₂ : l < ⊤), 𝓟 (Ioi l)) := by simp [nhds_eq_order (⊤:α)] lemma nhds_bot_order [topological_space α] [order_bot α] [order_topology α] : 𝓝 (⊥:α) = (⨅l (h₂ : ⊥ < l), 𝓟 (Iio l)) := by simp [nhds_eq_order (⊥:α)] lemma tendsto_nhds_top_mono [topological_space β] [order_top β] [order_topology β] {l : filter α} {f g : α → β} (hf : tendsto f l (𝓝 ⊤)) (hg : f ≤ᶠ[l] g) : tendsto g l (𝓝 ⊤) := begin simp only [nhds_top_order, tendsto_infi, tendsto_principal] at hf ⊢, intros x hx, filter_upwards [hf x hx, hg], exact λ x, lt_of_lt_of_le end lemma tendsto_nhds_bot_mono [topological_space β] [order_bot β] [order_topology β] {l : filter α} {f g : α → β} (hf : tendsto f l (𝓝 ⊥)) (hg : g ≤ᶠ[l] f) : tendsto g l (𝓝 ⊥) := @tendsto_nhds_top_mono α (order_dual β) _ _ _ _ _ _ hf hg lemma tendsto_nhds_top_mono' [topological_space β] [order_top β] [order_topology β] {l : filter α} {f g : α → β} (hf : tendsto f l (𝓝 ⊤)) (hg : f ≤ g) : tendsto g l (𝓝 ⊤) := tendsto_nhds_top_mono hf (eventually_of_forall hg) lemma tendsto_nhds_bot_mono' [topological_space β] [order_bot β] [order_topology β] {l : filter α} {f g : α → β} (hf : tendsto f l (𝓝 ⊥)) (hg : g ≤ f) : tendsto g l (𝓝 ⊥) := tendsto_nhds_bot_mono hf (eventually_of_forall hg) section linear_order variables [topological_space α] [linear_order α] [order_topology α] lemma exists_Ioc_subset_of_mem_nhds' {a : α} {s : set α} (hs : s ∈ 𝓝 a) {l : α} (hl : l < a) : ∃ l' ∈ Ico l a, Ioc l' a ⊆ s := begin rw [nhds_eq_order a] at hs, rcases hs with ⟨t₁, ht₁, t₂, ht₂, hts⟩, -- First we show that `t₂` includes `(-∞, a]`, so it suffices to show `(l', ∞) ⊆ t₁` suffices : ∃ l' ∈ Ico l a, Ioi l' ⊆ t₁, { have A : 𝓟 (Iic a) ≤ ⨅ b ∈ Ioi a, 𝓟 (Iio b), from (le_infi $ λ b, le_infi $ λ hb, principal_mono.2 $ Iic_subset_Iio.2 hb), have B : t₁ ∩ Iic a ⊆ s, from subset.trans (inter_subset_inter_right _ (A ht₂)) hts, from this.imp (λ l', Exists.imp $ λ hl' hl x hx, B ⟨hl hx.1, hx.2⟩) }, clear hts ht₂ t₂, -- Now we find `l` such that `(l', ∞) ⊆ t₁` rw [mem_binfi] at ht₁, { rcases ht₁ with ⟨b, hb, hb'⟩, exact ⟨max b l, ⟨le_max_right _ _, max_lt hb hl⟩, λ x hx, hb' $ Ioi_subset_Ioi (le_max_left _ _) hx⟩ }, { intros b hb b' hb', simp only [mem_Iio] at hb hb', use [max b b', max_lt hb hb'], simp [le_refl] }, exact ⟨l, hl⟩ end lemma exists_Ico_subset_of_mem_nhds' {a : α} {s : set α} (hs : s ∈ 𝓝 a) {u : α} (hu : a < u) : ∃ u' ∈ Ioc a u, Ico a u' ⊆ s := begin convert @exists_Ioc_subset_of_mem_nhds' (order_dual α) _ _ _ _ _ hs _ hu, ext, rw [dual_Ico, dual_Ioc] end lemma exists_Ioc_subset_of_mem_nhds {a : α} {s : set α} (hs : s ∈ 𝓝 a) (h : ∃ l, l < a) : ∃ l < a, Ioc l a ⊆ s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.2, hl.snd⟩ lemma exists_Ico_subset_of_mem_nhds {a : α} {s : set α} (hs : s ∈ 𝓝 a) (h : ∃ u, a < u) : ∃ u (_ : a < u), Ico a u ⊆ s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ico_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.1, hl.snd⟩ lemma order_separated {a₁ a₂ : α} (h : a₁ < a₂) : ∃u v : set α, is_open u ∧ is_open v ∧ a₁ ∈ u ∧ a₂ ∈ v ∧ (∀b₁∈u, ∀b₂∈v, b₁ < b₂) := match dense_or_discrete a₁ a₂ with | or.inl ⟨a, ha₁, ha₂⟩ := ⟨{a' | a' < a}, {a' | a < a'}, is_open_gt' a, is_open_lt' a, ha₁, ha₂, assume b₁ h₁ b₂ h₂, lt_trans h₁ h₂⟩ | or.inr ⟨h₁, h₂⟩ := ⟨{a | a < a₂}, {a | a₁ < a}, is_open_gt' a₂, is_open_lt' a₁, h, h, assume b₁ hb₁ b₂ hb₂, calc b₁ ≤ a₁ : h₂ _ hb₁ ... < a₂ : h ... ≤ b₂ : h₁ _ hb₂⟩ end @[priority 100] -- see Note [lower instance priority] instance order_topology.to_order_closed_topology : order_closed_topology α := { is_closed_le' := is_open_prod_iff.mpr $ assume a₁ a₂ (h : ¬ a₁ ≤ a₂), have h : a₂ < a₁, from lt_of_not_ge h, let ⟨u, v, hu, hv, ha₁, ha₂, h⟩ := order_separated h in ⟨v, u, hv, hu, ha₂, ha₁, assume ⟨b₁, b₂⟩ ⟨h₁, h₂⟩, not_le_of_gt $ h b₂ h₂ b₁ h₁⟩ } lemma order_topology.t2_space : t2_space α := by apply_instance @[priority 100] -- see Note [lower instance priority] instance order_topology.regular_space : regular_space α := { regular := assume s a hs ha, have hs' : sᶜ ∈ 𝓝 a, from mem_nhds_sets hs ha, have ∃t:set α, is_open t ∧ (∀l∈ s, l < a → l ∈ t) ∧ 𝓝[t] a = ⊥, from by_cases (assume h : ∃l, l < a, let ⟨l, hl, h⟩ := exists_Ioc_subset_of_mem_nhds hs' h in match dense_or_discrete l a with | or.inl ⟨b, hb₁, hb₂⟩ := ⟨{a | a < b}, is_open_gt' _, assume c hcs hca, show c < b, from lt_of_not_ge $ assume hbc, h ⟨lt_of_lt_of_le hb₁ hbc, le_of_lt hca⟩ hcs, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_lt' _) hb₂) $ assume x (hx : b < x), show ¬ x < b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, h₂⟩ := ⟨{a' | a' < a}, is_open_gt' _, assume b hbs hba, hba, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_lt' _) hl) $ assume x (hx : l < x), show ¬ x < a, from not_lt.2 $ h₁ _ hx⟩ end) (assume : ¬ ∃l, l < a, ⟨∅, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, nhds_within_empty _⟩), let ⟨t₁, ht₁o, ht₁s, ht₁a⟩ := this in have ∃t:set α, is_open t ∧ (∀u∈ s, u>a → u ∈ t) ∧ 𝓝[t] a = ⊥, from by_cases (assume h : ∃u, u > a, let ⟨u, hu, h⟩ := exists_Ico_subset_of_mem_nhds hs' h in match dense_or_discrete a u with | or.inl ⟨b, hb₁, hb₂⟩ := ⟨{a | b < a}, is_open_lt' _, assume c hcs hca, show c > b, from lt_of_not_ge $ assume hbc, h ⟨le_of_lt hca, lt_of_le_of_lt hbc hb₂⟩ hcs, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_gt' _) hb₁) $ assume x (hx : b > x), show ¬ x > b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, h₂⟩ := ⟨{a' | a' > a}, is_open_lt' _, assume b hbs hba, hba, inf_principal_eq_bot $ (𝓝 a).sets_of_superset (mem_nhds_sets (is_open_gt' _) hu) $ assume x (hx : u > x), show ¬ x > a, from not_lt.2 $ h₂ _ hx⟩ end) (assume : ¬ ∃u, u > a, ⟨∅, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, nhds_within_empty _⟩), let ⟨t₂, ht₂o, ht₂s, ht₂a⟩ := this in ⟨t₁ ∪ t₂, is_open_union ht₁o ht₂o, assume x hx, have x ≠ a, from assume eq, ha $ eq ▸ hx, (ne_iff_lt_or_gt.mp this).imp (ht₁s _ hx) (ht₂s _ hx), by rw [nhds_within_union, ht₁a, ht₂a, bot_sup_eq]⟩, ..order_topology.t2_space } /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`, provided `a` is neither a bottom element nor a top element. -/ lemma mem_nhds_iff_exists_Ioo_subset' {a : α} {s : set α} (hl : ∃ l, l < a) (hu : ∃ u, a < u) : s ∈ 𝓝 a ↔ ∃l u, a ∈ Ioo l u ∧ Ioo l u ⊆ s := begin split, { assume h, rcases exists_Ico_subset_of_mem_nhds h hu with ⟨u, au, hu⟩, rcases exists_Ioc_subset_of_mem_nhds h hl with ⟨l, la, hl⟩, refine ⟨l, u, ⟨la, au⟩, λx hx, _⟩, cases le_total a x with hax hax, { exact hu ⟨hax, hx.2⟩ }, { exact hl ⟨hx.1, hax⟩ } }, { rintros ⟨l, u, ha, h⟩, apply mem_sets_of_superset (mem_nhds_sets is_open_Ioo ha) h } end /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`. -/ lemma mem_nhds_iff_exists_Ioo_subset [no_top_order α] [no_bot_order α] {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃l u, a ∈ Ioo l u ∧ Ioo l u ⊆ s := mem_nhds_iff_exists_Ioo_subset' (no_bot a) (no_top a) lemma nhds_basis_Ioo' {a : α} (hl : ∃l, l < a) (hu : ∃u, a < u) : (𝓝 a).has_basis (λ b : α × α, b.1 < a ∧ a < b.2) (λ b, Ioo b.1 b.2) := ⟨λ s, (mem_nhds_iff_exists_Ioo_subset' hl hu).trans $ by simp⟩ lemma nhds_basis_Ioo [no_top_order α] [no_bot_order α] {a : α} : (𝓝 a).has_basis (λ b : α × α, b.1 < a ∧ a < b.2) (λ b, Ioo b.1 b.2) := nhds_basis_Ioo' (no_bot a) (no_top a) lemma filter.eventually.exists_Ioo_subset [no_top_order α] [no_bot_order α] {a : α} {p : α → Prop} (hp : ∀ᶠ x in 𝓝 a, p x) : ∃ l u, a ∈ Ioo l u ∧ Ioo l u ⊆ {x | p x} := mem_nhds_iff_exists_Ioo_subset.1 hp lemma Iio_mem_nhds {a b : α} (h : a < b) : Iio b ∈ 𝓝 a := mem_nhds_sets is_open_Iio h lemma Ioi_mem_nhds {a b : α} (h : a < b) : Ioi a ∈ 𝓝 b := mem_nhds_sets is_open_Ioi h lemma Iic_mem_nhds {a b : α} (h : a < b) : Iic b ∈ 𝓝 a := mem_sets_of_superset (Iio_mem_nhds h) Iio_subset_Iic_self lemma Ici_mem_nhds {a b : α} (h : a < b) : Ici a ∈ 𝓝 b := mem_sets_of_superset (Ioi_mem_nhds h) Ioi_subset_Ici_self lemma Ioo_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ioo a b ∈ 𝓝 x := mem_nhds_sets is_open_Ioo ⟨ha, hb⟩ lemma Ioc_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ioc a b ∈ 𝓝 x := mem_sets_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ioc_self lemma Ico_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Ico a b ∈ 𝓝 x := mem_sets_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ico_self lemma Icc_mem_nhds {a b x : α} (ha : a < x) (hb : x < b) : Icc a b ∈ 𝓝 x := mem_sets_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Icc_self section pi /-! ### Intervals in `Π i, π i` belong to `𝓝 x` For each leamma `pi_Ixx_mem_nhds` we add a non-dependent version `pi_Ixx_mem_nhds'` because sometimes Lean fails to unify different instances while trying to apply the dependent version to, e.g., `ι → ℝ`. -/ variables {ι : Type*} {π : ι → Type*} [fintype ι] [Π i, linear_order (π i)] [Π i, topological_space (π i)] [∀ i, order_topology (π i)] {a b x : Π i, π i} {a' b' x' : ι → α} lemma pi_Iic_mem_nhds (ha : ∀ i, x i < a i) : Iic a ∈ 𝓝 x := pi_univ_Iic a ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Iic_mem_nhds (ha _)) lemma pi_Iic_mem_nhds' (ha : ∀ i, x' i < a' i) : Iic a' ∈ 𝓝 x' := pi_Iic_mem_nhds ha lemma pi_Ici_mem_nhds (ha : ∀ i, a i < x i) : Ici a ∈ 𝓝 x := pi_univ_Ici a ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Ici_mem_nhds (ha _)) lemma pi_Ici_mem_nhds' (ha : ∀ i, a' i < x' i) : Ici a' ∈ 𝓝 x' := pi_Ici_mem_nhds ha lemma pi_Icc_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Icc a b ∈ 𝓝 x := pi_univ_Icc a b ▸ set_pi_mem_nhds (finite.of_fintype _) (λ i _, Icc_mem_nhds (ha _) (hb _)) lemma pi_Icc_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Icc a' b' ∈ 𝓝 x' := pi_Icc_mem_nhds ha hb variables [nonempty ι] lemma pi_Iio_mem_nhds (ha : ∀ i, x i < a i) : Iio a ∈ 𝓝 x := begin refine mem_sets_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _)) (pi_univ_Iio_subset a), exact Iio_mem_nhds (ha i) end lemma pi_Iio_mem_nhds' (ha : ∀ i, x' i < a' i) : Iio a' ∈ 𝓝 x' := pi_Iio_mem_nhds ha lemma pi_Ioi_mem_nhds (ha : ∀ i, a i < x i) : Ioi a ∈ 𝓝 x := @pi_Iio_mem_nhds ι (λ i, order_dual (π i)) _ _ _ _ _ _ _ ha lemma pi_Ioi_mem_nhds' (ha : ∀ i, a' i < x' i) : Ioi a' ∈ 𝓝 x' := pi_Ioi_mem_nhds ha lemma pi_Ioc_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ioc a b ∈ 𝓝 x := begin refine mem_sets_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _)) (pi_univ_Ioc_subset a b), exact Ioc_mem_nhds (ha i) (hb i) end lemma pi_Ioc_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ioc a' b' ∈ 𝓝 x' := pi_Ioc_mem_nhds ha hb lemma pi_Ico_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ico a b ∈ 𝓝 x := begin refine mem_sets_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _)) (pi_univ_Ico_subset a b), exact Ico_mem_nhds (ha i) (hb i) end lemma pi_Ico_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ico a' b' ∈ 𝓝 x' := pi_Ico_mem_nhds ha hb lemma pi_Ioo_mem_nhds (ha : ∀ i, a i < x i) (hb : ∀ i, x i < b i) : Ioo a b ∈ 𝓝 x := begin refine mem_sets_of_superset (set_pi_mem_nhds (finite.of_fintype _) (λ i _, _)) (pi_univ_Ioo_subset a b), exact Ioo_mem_nhds (ha i) (hb i) end lemma pi_Ioo_mem_nhds' (ha : ∀ i, a' i < x' i) (hb : ∀ i, x' i < b' i) : Ioo a' b' ∈ 𝓝 x' := pi_Ioo_mem_nhds ha hb end pi lemma disjoint_nhds_at_top [no_top_order α] (x : α) : disjoint (𝓝 x) at_top := begin rw filter.disjoint_iff, cases no_top x with a ha, use [Iio a, Iio_mem_nhds ha, Ici a, mem_at_top a], rw [inter_comm, Ici_inter_Iio, Ico_self] end @[simp] lemma inf_nhds_at_top [no_top_order α] (x : α) : 𝓝 x ⊓ at_top = ⊥ := disjoint_iff.1 (disjoint_nhds_at_top x) lemma disjoint_nhds_at_bot [no_bot_order α] (x : α) : disjoint (𝓝 x) at_bot := @disjoint_nhds_at_top (order_dual α) _ _ _ _ x @[simp] lemma inf_nhds_at_bot [no_bot_order α] (x : α) : 𝓝 x ⊓ at_bot = ⊥ := @inf_nhds_at_top (order_dual α) _ _ _ _ x lemma not_tendsto_nhds_of_tendsto_at_top [no_top_order α] {F : filter β} [ne_bot F] {f : β → α} (hf : tendsto f F at_top) (x : α) : ¬ tendsto f F (𝓝 x) := hf.not_tendsto (disjoint_nhds_at_top x).symm lemma not_tendsto_at_top_of_tendsto_nhds [no_top_order α] {F : filter β} [ne_bot F] {f : β → α} {x : α} (hf : tendsto f F (𝓝 x)) : ¬ tendsto f F at_top := hf.not_tendsto (disjoint_nhds_at_top x) lemma not_tendsto_nhds_of_tendsto_at_bot [no_bot_order α] {F : filter β} [ne_bot F] {f : β → α} (hf : tendsto f F at_bot) (x : α) : ¬ tendsto f F (𝓝 x) := hf.not_tendsto (disjoint_nhds_at_bot x).symm lemma not_tendsto_at_bot_of_tendsto_nhds [no_bot_order α] {F : filter β} [ne_bot F] {f : β → α} {x : α} (hf : tendsto f F (𝓝 x)) : ¬ tendsto f F at_bot := hf.not_tendsto (disjoint_nhds_at_bot x) /-! ### Neighborhoods to the left and to the right on an `order_topology` We've seen some properties of left and right neighborhood of a point in an `order_closed_topology`. In an `order_topology`, such neighborhoods can be characterized as the sets containing suitable intervals to the right or to the left of `a`. We give now these characterizations. -/ -- NB: If you extend the list, append to the end please to avoid breaking the API /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `(a, +∞)` 1. `s` is a neighborhood of `a` within `(a, b]` 2. `s` is a neighborhood of `a` within `(a, b)` 3. `s` includes `(a, u)` for some `u ∈ (a, b]` 4. `s` includes `(a, u)` for some `u > a` -/ lemma tfae_mem_nhds_within_Ioi {a b : α} (hab : a < b) (s : set α) : tfae [s ∈ 𝓝[Ioi a] a, -- 0 : `s` is a neighborhood of `a` within `(a, +∞)` s ∈ 𝓝[Ioc a b] a, -- 1 : `s` is a neighborhood of `a` within `(a, b]` s ∈ 𝓝[Ioo a b] a, -- 2 : `s` is a neighborhood of `a` within `(a, b)` ∃ u ∈ Ioc a b, Ioo a u ⊆ s, -- 3 : `s` includes `(a, u)` for some `u ∈ (a, b]` ∃ u ∈ Ioi a, Ioo a u ⊆ s] := -- 4 : `s` includes `(a, u)` for some `u > a` begin tfae_have : 1 ↔ 2, by rw [nhds_within_Ioc_eq_nhds_within_Ioi hab], tfae_have : 1 ↔ 3, by rw [nhds_within_Ioo_eq_nhds_within_Ioi hab], tfae_have : 4 → 5, from λ ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩, tfae_have : 5 → 1, { rintros ⟨u, hau, hu⟩, exact mem_sets_of_superset (Ioo_mem_nhds_within_Ioi ⟨le_refl a, hau⟩) hu }, tfae_have : 1 → 4, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds' va hab with ⟨u, au, hu⟩, refine ⟨u, au, λx hx, _⟩, refine hv ⟨hu ⟨le_of_lt hx.1, hx.2⟩, _⟩, exact hx.1 }, tfae_finish end lemma mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {a u' : α} {s : set α} (hu' : a < u') : s ∈ 𝓝[Ioi a] a ↔ ∃u ∈ Ioc a u', Ioo a u ⊆ s := (tfae_mem_nhds_within_Ioi hu' s).out 0 3 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u < u'`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset' {a u' : α} {s : set α} (hu' : a < u') : s ∈ 𝓝[Ioi a] a ↔ ∃u ∈ Ioi a, Ioo a u ⊆ s := (tfae_mem_nhds_within_Ioi hu' s).out 0 4 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset [no_top_order α] {a : α} {s : set α} : s ∈ 𝓝[Ioi a] a ↔ ∃u ∈ Ioi a, Ioo a u ⊆ s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ioi_iff_exists_Ioo_subset' hu' /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u]` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioc_subset [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Ioi a] a ↔ ∃u ∈ Ioi a, Ioc a u ⊆ s := begin rw mem_nhds_within_Ioi_iff_exists_Ioo_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, λx hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ioo_subset_Ioc_self as⟩ } end /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b)` 1. `s` is a neighborhood of `b` within `[a, b)` 2. `s` is a neighborhood of `b` within `(a, b)` 3. `s` includes `(l, b)` for some `l ∈ [a, b)` 4. `s` includes `(l, b)` for some `l < b` -/ lemma tfae_mem_nhds_within_Iio {a b : α} (h : a < b) (s : set α) : tfae [s ∈ 𝓝[Iio b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b)` s ∈ 𝓝[Ico a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b)` s ∈ 𝓝[Ioo a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b)` ∃ l ∈ Ico a b, Ioo l b ⊆ s, -- 3 : `s` includes `(l, b)` for some `l ∈ [a, b)` ∃ l ∈ Iio b, Ioo l b ⊆ s] := -- 4 : `s` includes `(l, b)` for some `l < b` begin have := @tfae_mem_nhds_within_Ioi (order_dual α) _ _ _ _ _ h s, -- If we call `convert` here, it generates wrong equations, so we need to simplify first simp only [exists_prop] at this ⊢, rw [dual_Ioi, dual_Ioc, dual_Ioo] at this, convert this; ext l; rw [dual_Ioo] end lemma mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {a l' : α} {s : set α} (hl' : l' < a) : s ∈ 𝓝[Iio a] a ↔ ∃l ∈ Ico l' a, Ioo l a ⊆ s := (tfae_mem_nhds_within_Iio hl' s).out 0 3 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset' {a l' : α} {s : set α} (hl' : l' < a) : s ∈ 𝓝[Iio a] a ↔ ∃l ∈ Iio a, Ioo l a ⊆ s := (tfae_mem_nhds_within_Iio hl' s).out 0 4 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset [no_bot_order α] {a : α} {s : set α} : s ∈ 𝓝[Iio a] a ↔ ∃l ∈ Iio a, Ioo l a ⊆ s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iio_iff_exists_Ioo_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `[l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ico_subset [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Iio a] a ↔ ∃l ∈ Iio a, Ico l a ⊆ s := begin convert @mem_nhds_within_Ioi_iff_exists_Ioc_subset (order_dual α) _ _ _ _ _ _ _, simp only [dual_Ioc], refl end /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `[a, +∞)` 1. `s` is a neighborhood of `a` within `[a, b]` 2. `s` is a neighborhood of `a` within `[a, b)` 3. `s` includes `[a, u)` for some `u ∈ (a, b]` 4. `s` includes `[a, u)` for some `u > a` -/ lemma tfae_mem_nhds_within_Ici {a b : α} (hab : a < b) (s : set α) : tfae [s ∈ 𝓝[Ici a] a, -- 0 : `s` is a neighborhood of `a` within `[a, +∞)` s ∈ 𝓝[Icc a b] a, -- 1 : `s` is a neighborhood of `a` within `[a, b]` s ∈ 𝓝[Ico a b] a, -- 2 : `s` is a neighborhood of `a` within `[a, b)` ∃ u ∈ Ioc a b, Ico a u ⊆ s, -- 3 : `s` includes `[a, u)` for some `u ∈ (a, b]` ∃ u ∈ Ioi a, Ico a u ⊆ s] := -- 4 : `s` includes `[a, u)` for some `u > a` begin tfae_have : 1 ↔ 2, by rw [nhds_within_Icc_eq_nhds_within_Ici hab], tfae_have : 1 ↔ 3, by rw [nhds_within_Ico_eq_nhds_within_Ici hab], tfae_have : 4 → 5, from λ ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩, tfae_have : 5 → 1, { rintros ⟨u, hau, hu⟩, exact mem_sets_of_superset (Ico_mem_nhds_within_Ici ⟨le_refl a, hau⟩) hu }, tfae_have : 1 → 4, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds' va hab with ⟨u, au, hu⟩, refine ⟨u, au, λx hx, _⟩, refine hv ⟨hu ⟨hx.1, hx.2⟩, _⟩, exact hx.1 }, tfae_finish end lemma mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset {a u' : α} {s : set α} (hu' : a < u') : s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioc a u', Ico a u ⊆ s := (tfae_mem_nhds_within_Ici hu' s).out 0 3 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u < u'`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset' {a u' : α} {s : set α} (hu' : a < u') : s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, Ico a u ⊆ s := (tfae_mem_nhds_within_Ici hu' s).out 0 4 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset [no_top_order α] {a : α} {s : set α} : s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, Ico a u ⊆ s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ici_iff_exists_Ico_subset' hu' /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Icc_subset' [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Ici a] a ↔ ∃u ∈ Ioi a, Icc a u ⊆ s := begin rw mem_nhds_within_Ici_iff_exists_Ico_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, λx hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ico_subset_Icc_self as⟩ } end /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b]` 1. `s` is a neighborhood of `b` within `[a, b]` 2. `s` is a neighborhood of `b` within `(a, b]` 3. `s` includes `(l, b]` for some `l ∈ [a, b)` 4. `s` includes `(l, b]` for some `l < b` -/ lemma tfae_mem_nhds_within_Iic {a b : α} (h : a < b) (s : set α) : tfae [s ∈ 𝓝[Iic b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b]` s ∈ 𝓝[Icc a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b]` s ∈ 𝓝[Ioc a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b]` ∃ l ∈ Ico a b, Ioc l b ⊆ s, -- 3 : `s` includes `(l, b]` for some `l ∈ [a, b)` ∃ l ∈ Iio b, Ioc l b ⊆ s] := -- 4 : `s` includes `(l, b]` for some `l < b` begin have := @tfae_mem_nhds_within_Ici (order_dual α) _ _ _ _ _ h s, -- If we call `convert` here, it generates wrong equations, so we need to simplify first simp only [exists_prop] at this ⊢, rw [dual_Icc, dual_Ioc, dual_Ioi] at this, convert this; ext l; rw [dual_Ico] end lemma mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset {a l' : α} {s : set α} (hl' : l' < a) : s ∈ 𝓝[Iic a] a ↔ ∃l ∈ Ico l' a, Ioc l a ⊆ s := (tfae_mem_nhds_within_Iic hl' s).out 0 3 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset' {a l' : α} {s : set α} (hl' : l' < a) : s ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio a, Ioc l a ⊆ s := (tfae_mem_nhds_within_Iic hl' s).out 0 4 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset [no_bot_order α] {a : α} {s : set α} : s ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio a, Ioc l a ⊆ s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iic_iff_exists_Ioc_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Icc_subset' [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Iic a] a ↔ ∃l ∈ Iio a, Icc l a ⊆ s := begin convert @mem_nhds_within_Ici_iff_exists_Icc_subset' (order_dual α) _ _ _ _ _ _ _, simp_rw (show ∀ u : order_dual α, @Icc (order_dual α) _ a u = @Icc α _ u a, from λ u, dual_Icc), refl, end /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Icc_subset [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Ici a] a ↔ ∃u, a < u ∧ Icc a u ⊆ s := begin rw mem_nhds_within_Ici_iff_exists_Ico_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, λx hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ico_subset_Icc_self as⟩ } end /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Icc_subset [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ 𝓝[Iic a] a ↔ ∃l, l < a ∧ Icc l a ⊆ s := begin rw mem_nhds_within_Iic_iff_exists_Ioc_subset, split, { rintros ⟨l, la, as⟩, rcases exists_between la with ⟨v, hv⟩, refine ⟨v, hv.2, λx hx, as ⟨lt_of_lt_of_le hv.1 hx.1, hx.2⟩⟩, }, { rintros ⟨l, la, as⟩, exact ⟨l, la, subset.trans Ioc_subset_Icc_self as⟩ } end end linear_order section linear_ordered_add_comm_group variables [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] variables {l : filter β} {f g : β → α} /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_top` then `f + g` tends to `at_top`. -/ lemma filter.tendsto.add_at_top {C : α} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := begin nontriviality α, obtain ⟨C', hC'⟩ : ∃ C', C' < C := no_bot C, refine tendsto_at_top_add_left_of_le' _ C' _ hg, exact (hf.eventually (lt_mem_nhds hC')).mono (λ x, le_of_lt) end /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_bot` then `f + g` tends to `at_bot`. -/ lemma filter.tendsto.add_at_bot {C : α} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @filter.tendsto.add_at_top (order_dual α) _ _ _ _ _ _ _ _ hf hg /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_top` and `g` tends to `C` then `f + g` tends to `at_top`. -/ lemma filter.tendsto.at_top_add {C : α} (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (λ x, f x + g x) l at_top := by { conv in (_ + _) { rw add_comm }, exact hg.add_at_top hf } /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_bot` and `g` tends to `C` then `f + g` tends to `at_bot`. -/ lemma filter.tendsto.at_bot_add {C : α} (hf : tendsto f l at_bot) (hg : tendsto g l (𝓝 C)) : tendsto (λ x, f x + g x) l at_bot := by { conv in (_ + _) { rw add_comm }, exact hg.add_at_bot hf } end linear_ordered_add_comm_group section linear_ordered_field variables [linear_ordered_field α] variables {l : filter β} {f g : β → α} variables [topological_space α] [order_topology α] /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a positive constant `C` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.at_top_mul {C : α} (hC : 0 < C) (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (λ x, (f x * g x)) l at_top := begin refine tendsto_at_top_mono' _ _ (hf.at_top_mul_const (half_pos hC)), filter_upwards [hg.eventually (lt_mem_nhds (half_lt_self hC)), hf.eventually (eventually_ge_at_top 0)], exact λ x hg hf, mul_le_mul_of_nonneg_left hg.le hf end /-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and `g` tends to `at_top` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.mul_at_top {C : α} (hC : 0 < C) (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) : tendsto (λ x, (f x * g x)) l at_top := by simpa only [mul_comm] using hg.at_top_mul hC hf /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a negative constant `C` then `f * g` tends to `at_bot`. -/ lemma filter.tendsto.at_top_mul_neg {C : α} (hC : C < 0) (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (λ x, (f x * g x)) l at_bot := begin rcases exists_between hC with ⟨C', hCC', hC'0⟩, refine tendsto_at_bot_mono' _ _ (hf.at_top_mul_neg_const hC'0), filter_upwards [hg.eventually (gt_mem_nhds hCC'), hf.eventually (eventually_ge_at_top 0)], exact λ x hg hf, mul_le_mul_of_nonneg_left hg.le hf end end linear_ordered_field section linear_ordered_field variables [linear_ordered_field α] [topological_space α] [order_topology α] /-- The function `x ↦ x⁻¹` tends to `+∞` on the right of `0`. -/ lemma tendsto_inv_zero_at_top : tendsto (λx:α, x⁻¹) (𝓝[set.Ioi (0:α)] 0) at_top := begin refine (at_top_basis' 1).tendsto_right_iff.2 (λ b hb, _), have hb' : 0 < b := zero_lt_one.trans_le hb, filter_upwards [Ioc_mem_nhds_within_Ioi ⟨le_rfl, inv_pos.2 hb'⟩], exact λ x hx, (le_inv hx.1 hb').1 hx.2 end /-- The function `r ↦ r⁻¹` tends to `0` on the right as `r → +∞`. -/ lemma tendsto_inv_at_top_zero' : tendsto (λr:α, r⁻¹) at_top (𝓝[set.Ioi (0:α)] 0) := begin refine (has_basis.tendsto_iff at_top_basis ⟨λ s, mem_nhds_within_Ioi_iff_exists_Ioc_subset⟩).2 _, refine λ b hb, ⟨b⁻¹, trivial, λ x hx, _⟩, have : 0 < x := lt_of_lt_of_le (inv_pos.2 hb) hx, exact ⟨inv_pos.2 this, (inv_le this hb).2 hx⟩ end lemma tendsto_inv_at_top_zero : tendsto (λr:α, r⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero'.mono_right inf_le_left variables {l : filter β} {f : β → α} lemma tendsto.inv_tendsto_at_top (h : tendsto f l at_top) : tendsto (f⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp h lemma tendsto.inv_tendsto_zero (h : tendsto f l (𝓝[set.Ioi 0] 0)) : tendsto (f⁻¹) l at_top := tendsto_inv_zero_at_top.comp h /-- The function `x^(-n)` tends to `0` at `+∞` for any positive natural `n`. A version for positive real powers exists as `tendsto_rpow_neg_at_top`. -/ lemma tendsto_pow_neg_at_top {n : ℕ} (hn : 1 ≤ n) : tendsto (λ x : α, x ^ (-(n:ℤ))) at_top (𝓝 0) := tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) (λ x hx, (fpow_neg x n).symm)) (tendsto.inv_tendsto_at_top (tendsto_pow_at_top hn)) end linear_ordered_field lemma preimage_neg [add_group α] : preimage (has_neg.neg : α → α) = image (has_neg.neg : α → α) := (image_eq_preimage_of_inverse neg_neg neg_neg).symm lemma filter.map_neg [add_group α] : map (has_neg.neg : α → α) = comap (has_neg.neg : α → α) := funext $ assume f, map_eq_comap_of_inverse (funext neg_neg) (funext neg_neg) section topological_add_group variables [topological_space α] [ordered_add_comm_group α] [topological_add_group α] lemma neg_preimage_closure {s : set α} : (λr:α, -r) ⁻¹' closure s = closure ((λr:α, -r) '' s) := have (λr:α, -r) ∘ (λr:α, -r) = id, from funext neg_neg, by rw [preimage_neg]; exact (subset.antisymm (image_closure_subset_closure_image continuous_neg) $ calc closure ((λ (r : α), -r) '' s) = (λr, -r) '' ((λr, -r) '' closure ((λ (r : α), -r) '' s)) : by rw [←image_comp, this, image_id] ... ⊆ (λr, -r) '' closure ((λr, -r) '' ((λ (r : α), -r) '' s)) : monotone_image $ image_closure_subset_closure_image continuous_neg ... = _ : by rw [←image_comp, this, image_id]) end topological_add_group section order_topology variables [topological_space α] [topological_space β] [linear_order α] [linear_order β] [order_topology α] [order_topology β] lemma is_lub.nhds_within_ne_bot {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) : ne_bot (𝓝[s] a) := let ⟨a', ha'⟩ := hs in forall_sets_nonempty_iff_ne_bot.mp $ assume t ht, let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := mem_inf_sets.mp ht in by_cases (assume h : a = a', have a ∈ t₁, from mem_of_nhds ht₁, have a ∈ t₂, from ht₂ $ by rwa [h], ⟨a, ht ⟨‹a ∈ t₁›, ‹a ∈ t₂›⟩⟩) (assume : a ≠ a', have a' < a, from lt_of_le_of_ne (ha.left ‹a' ∈ s›) this.symm, let ⟨l, hl, hlt₁⟩ := exists_Ioc_subset_of_mem_nhds ht₁ ⟨a', this⟩ in have ∃a'∈s, l < a', from classical.by_contradiction $ assume : ¬ ∃a'∈s, l < a', have ∀a'∈s, a' ≤ l, from assume a ha, not_lt.1 $ assume ha', this ⟨a, ha, ha'⟩, have ¬ l < a, from not_lt.2 $ ha.right this, this ‹l < a›, let ⟨a', ha', ha'l⟩ := this in have a' ∈ t₁, from hlt₁ ⟨‹l < a'›, ha.left ha'⟩, ⟨a', ht ⟨‹a' ∈ t₁›, ht₂ ‹a' ∈ s›⟩⟩) lemma is_glb.nhds_within_ne_bot : ∀ {a : α} {s : set α}, is_glb s a → s.nonempty → ne_bot (𝓝[s] a) := @is_lub.nhds_within_ne_bot (order_dual α) _ _ _ lemma is_lub_of_mem_nhds {s : set α} {a : α} {f : filter α} (hsa : a ∈ upper_bounds s) (hsf : s ∈ f) [ne_bot (f ⊓ 𝓝 a)] : is_lub s a := ⟨hsa, assume b hb, not_lt.1 $ assume hba, have s ∩ {a | b < a} ∈ f ⊓ 𝓝 a, from inter_mem_inf_sets hsf (mem_nhds_sets (is_open_lt' _) hba), let ⟨x, ⟨hxs, hxb⟩⟩ := nonempty_of_mem_sets this in have b < b, from lt_of_lt_of_le hxb $ hb hxs, lt_irrefl b this⟩ lemma is_glb_of_mem_nhds : ∀ {s : set α} {a : α} {f : filter α}, a ∈ lower_bounds s → s ∈ f → ne_bot (f ⊓ 𝓝 a) → is_glb s a := @is_lub_of_mem_nhds (order_dual α) _ _ _ lemma is_lub_of_is_lub_of_tendsto {f : α → β} {s : set α} {a : α} {b : β} (hf : ∀x∈s, ∀y∈s, x ≤ y → f x ≤ f y) (ha : is_lub s a) (hs : s.nonempty) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : is_lub (f '' s) b := have hnbot : ne_bot (𝓝[s] a), from ha.nhds_within_ne_bot hs, have ∀a'∈s, ¬ b < f a', from assume a' ha' h, have ∀ᶠ x in 𝓝 b, x < f a', from mem_nhds_sets (is_open_gt' _) h, let ⟨t₁, ht₁, t₂, ht₂, hs⟩ := mem_inf_sets.mp (hb this) in by_cases (assume h : a = a', have a ∈ t₁ ∩ t₂, from ⟨mem_of_nhds ht₁, ht₂ $ by rwa [h]⟩, have f a < f a', from hs this, lt_irrefl (f a') $ by rwa [h] at this) (assume h : a ≠ a', have a' < a, from lt_of_le_of_ne (ha.left ha') h.symm, have {x | a' < x} ∈ 𝓝 a, from mem_nhds_sets (is_open_lt' _) this, have {x | a' < x} ∩ t₁ ∈ 𝓝 a, from inter_mem_sets this ht₁, have ({x | a' < x} ∩ t₁) ∩ s ∈ 𝓝[s] a, from inter_mem_inf_sets this (subset.refl s), let ⟨x, ⟨hx₁, hx₂⟩, hx₃⟩ := hnbot.nonempty_of_mem this in have hxa' : f x < f a', from hs ⟨hx₂, ht₂ hx₃⟩, have ha'x : f a' ≤ f x, from hf _ ha' _ hx₃ $ le_of_lt hx₁, lt_irrefl _ (lt_of_le_of_lt ha'x hxa')), and.intro (assume b' ⟨a', ha', h_eq⟩, h_eq ▸ not_lt.1 $ this _ ha') (assume b' hb', by exactI (le_of_tendsto hb $ mem_inf_sets_of_right $ assume x hx, hb' $ mem_image_of_mem _ hx)) lemma is_glb_of_is_glb_of_tendsto {f : α → β} {s : set α} {a : α} {b : β} (hf : ∀x∈s, ∀y∈s, x ≤ y → f x ≤ f y) : is_glb s a → s.nonempty → tendsto f (𝓝[s] a) (𝓝 b) → is_glb (f '' s) b := @is_lub_of_is_lub_of_tendsto (order_dual α) (order_dual β) _ _ _ _ _ _ f s a b (λ x hx y hy, hf y hy x hx) lemma is_glb_of_is_lub_of_tendsto : ∀ {f : α → β} {s : set α} {a : α} {b : β}, (∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) → is_lub s a → s.nonempty → tendsto f (𝓝[s] a) (𝓝 b) → is_glb (f '' s) b := @is_lub_of_is_lub_of_tendsto α (order_dual β) _ _ _ _ _ _ lemma is_lub_of_is_glb_of_tendsto : ∀ {f : α → β} {s : set α} {a : α} {b : β}, (∀x∈s, ∀y∈s, x ≤ y → f y ≤ f x) → is_glb s a → s.nonempty → tendsto f (𝓝[s] a) (𝓝 b) → is_lub (f '' s) b := @is_glb_of_is_glb_of_tendsto α (order_dual β) _ _ _ _ _ _ lemma mem_closure_of_is_lub {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) : a ∈ closure s := by rw closure_eq_cluster_pts; exact ha.nhds_within_ne_bot hs lemma mem_of_is_lub_of_is_closed {a : α} {s : set α} (ha : is_lub s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := by rw ←sc.closure_eq; exact mem_closure_of_is_lub ha hs lemma mem_closure_of_is_glb {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty) : a ∈ closure s := by rw closure_eq_cluster_pts; exact ha.nhds_within_ne_bot hs lemma mem_of_is_glb_of_is_closed {a : α} {s : set α} (ha : is_glb s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := by rw ←sc.closure_eq; exact mem_closure_of_is_glb ha hs /-- A compact set is bounded below -/ lemma is_compact.bdd_below {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] [nonempty α] {s : set α} (hs : is_compact s) : bdd_below s := begin by_contra H, rcases hs.elim_finite_subcover_image (λ x (_ : x ∈ s), @is_open_Ioi _ _ _ _ x) _ with ⟨t, st, ft, ht⟩, { refine H (ft.bdd_below.imp $ λ C hC y hy, _), rcases mem_bUnion_iff.1 (ht hy) with ⟨x, hx, xy⟩, exact le_trans (hC hx) (le_of_lt xy) }, { refine λ x hx, mem_bUnion_iff.2 (not_imp_comm.1 _ H), exact λ h, ⟨x, λ y hy, le_of_not_lt (h.imp $ λ ys, ⟨_, hy, ys⟩)⟩ } end /-- A compact set is bounded above -/ lemma is_compact.bdd_above {α : Type u} [topological_space α] [linear_order α] [order_topology α] : Π [nonempty α] {s : set α}, is_compact s → bdd_above s := @is_compact.bdd_below (order_dual α) _ _ _ end order_topology section linear_order variables [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`, unless `a` is a top element. -/ lemma closure_Ioi' {a b : α} (hab : a < b) : closure (Ioi a) = Ici a := begin apply subset.antisymm, { exact closure_minimal Ioi_subset_Ici_self is_closed_Ici }, { assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_glb is_glb_Ioi ⟨_, hab⟩ }, { exact subset_closure (lt_of_le_of_ne hx (ne.symm h)) } } end /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`. -/ @[simp] lemma closure_Ioi (a : α) [no_top_order α] : closure (Ioi a) = Ici a := let ⟨b, hb⟩ := no_top a in closure_Ioi' hb /-- The closure of the interval `(-∞, a)` is the closed interval `(-∞, a]`, unless `a` is a bottom element. -/ lemma closure_Iio' {a b : α} (hab : b < a) : closure (Iio a) = Iic a := begin apply subset.antisymm, { exact closure_minimal Iio_subset_Iic_self is_closed_Iic }, { assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_lub is_lub_Iio ⟨_, hab⟩ }, { apply subset_closure, by simpa [h] using lt_or_eq_of_le hx } } end /-- The closure of the interval `(-∞, a)` is the interval `(-∞, a]`. -/ @[simp] lemma closure_Iio (a : α) [no_bot_order α] : closure (Iio a) = Iic a := let ⟨b, hb⟩ := no_bot a in closure_Iio' hb /-- The closure of the open interval `(a, b)` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ioo {a b : α} (hab : a < b) : closure (Ioo a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioo_subset_Icc_self is_closed_Icc }, { have hab' : (Ioo a b).nonempty, from nonempty_Ioo.2 hab, assume x hx, by_cases h : x = a, { rw h, exact mem_closure_of_is_glb (is_glb_Ioo hab) hab' }, by_cases h' : x = b, { rw h', refine mem_closure_of_is_lub (is_lub_Ioo hab) hab' }, exact subset_closure ⟨lt_of_le_of_ne hx.1 (ne.symm h), by simpa [h'] using lt_or_eq_of_le hx.2⟩ } end /-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ioc {a b : α} (hab : a < b) : closure (Ioc a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioc_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ioc_self), rw closure_Ioo hab } end /-- The closure of the interval `[a, b)` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ico {a b : α} (hab : a < b) : closure (Ico a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ico_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ico_self), rw closure_Ioo hab } end @[simp] lemma interior_Ici [no_bot_order α] {a : α} : interior (Ici a) = Ioi a := by rw [← compl_Iio, interior_compl, closure_Iio, compl_Iic] @[simp] lemma interior_Iic [no_top_order α] {a : α} : interior (Iic a) = Iio a := by rw [← compl_Ioi, interior_compl, closure_Ioi, compl_Ici] @[simp] lemma interior_Icc [no_bot_order α] [no_top_order α] {a b : α}: interior (Icc a b) = Ioo a b := by rw [← Ici_inter_Iic, interior_inter, interior_Ici, interior_Iic, Ioi_inter_Iio] @[simp] lemma interior_Ico [no_bot_order α] {a b : α} : interior (Ico a b) = Ioo a b := by rw [← Ici_inter_Iio, interior_inter, interior_Ici, interior_Iio, Ioi_inter_Iio] @[simp] lemma interior_Ioc [no_top_order α] {a b : α} : interior (Ioc a b) = Ioo a b := by rw [← Ioi_inter_Iic, interior_inter, interior_Ioi, interior_Iic, Ioi_inter_Iio] @[simp] lemma frontier_Ici [no_bot_order α] {a : α} : frontier (Ici a) = {a} := by simp [frontier] @[simp] lemma frontier_Iic [no_top_order α] {a : α} : frontier (Iic a) = {a} := by simp [frontier] @[simp] lemma frontier_Ioi [no_top_order α] {a : α} : frontier (Ioi a) = {a} := by simp [frontier] @[simp] lemma frontier_Iio [no_bot_order α] {a : α} : frontier (Iio a) = {a} := by simp [frontier] @[simp] lemma frontier_Icc [no_bot_order α] [no_top_order α] {a b : α} (h : a < b) : frontier (Icc a b) = {a, b} := by simp [frontier, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ioo {a b : α} (h : a < b) : frontier (Ioo a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ico [no_bot_order α] {a b : α} (h : a < b) : frontier (Ico a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ioc [no_top_order α] {a b : α} (h : a < b) : frontier (Ioc a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] lemma nhds_within_Ioi_ne_bot' {a b c : α} (H₁ : a < c) (H₂ : a ≤ b) : ne_bot (𝓝[Ioi a] b) := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Ioi' H₁], exact H₂ } lemma nhds_within_Ioi_ne_bot [no_top_order α] {a b : α} (H : a ≤ b) : ne_bot (𝓝[Ioi a] b) := let ⟨c, hc⟩ := no_top a in nhds_within_Ioi_ne_bot' hc H lemma nhds_within_Ioi_self_ne_bot' {a b : α} (H : a < b) : ne_bot (𝓝[Ioi a] a) := nhds_within_Ioi_ne_bot' H (le_refl a) @[instance] lemma nhds_within_Ioi_self_ne_bot [no_top_order α] (a : α) : ne_bot (𝓝[Ioi a] a) := nhds_within_Ioi_ne_bot (le_refl a) lemma nhds_within_Iio_ne_bot' {a b c : α} (H₁ : a < c) (H₂ : b ≤ c) : ne_bot (𝓝[Iio c] b) := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Iio' H₁], exact H₂ } lemma nhds_within_Iio_ne_bot [no_bot_order α] {a b : α} (H : a ≤ b) : ne_bot (𝓝[Iio b] a) := let ⟨c, hc⟩ := no_bot b in nhds_within_Iio_ne_bot' hc H lemma nhds_within_Iio_self_ne_bot' {a b : α} (H : a < b) : ne_bot (𝓝[Iio b] b) := nhds_within_Iio_ne_bot' H (le_refl b) @[instance] lemma nhds_within_Iio_self_ne_bot [no_bot_order α] (a : α) : ne_bot (𝓝[Iio a] a) := nhds_within_Iio_ne_bot (le_refl a) end linear_order section linear_order variables [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a b : α} {s : set α} lemma comap_coe_nhds_within_Iio_of_Ioo_subset (hb : s ⊆ Iio b) (hs : s.nonempty → ∃ a < b, Ioo a b ⊆ s) : comap (coe : s → α) (𝓝[Iio b] b) = at_top := begin nontriviality, haveI : nonempty s := nontrivial_iff_nonempty.1 ‹_›, rcases hs (nonempty_subtype.1 ‹_›) with ⟨a, h, hs⟩, ext u, split, { rintros ⟨t, ht, hts⟩, obtain ⟨x, ⟨hxa : a ≤ x, hxb : x < b⟩, hxt : Ioo x b ⊆ t⟩ := (mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset h).mp ht, obtain ⟨y, hxy, hyb⟩ := exists_between hxb, refine mem_sets_of_superset (mem_at_top ⟨y, hs ⟨hxa.trans_lt hxy, hyb⟩⟩) _, rintros ⟨z, hzs⟩ (hyz : y ≤ z), refine hts (hxt ⟨hxy.trans_le _, hb _⟩); assumption }, { intros hu, obtain ⟨x : s, hx : ∀ z, x ≤ z → z ∈ u⟩ := mem_at_top_sets.1 hu, exact ⟨Ioo x b, Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 $ hb x.2), λ z hz, hx _ hz.1.le⟩ } end lemma comap_coe_nhds_within_Ioi_of_Ioo_subset (ha : s ⊆ Ioi a) (hs : s.nonempty → ∃ b > a, Ioo a b ⊆ s) : comap (coe : s → α) (𝓝[Ioi a] a) = at_bot := begin refine @comap_coe_nhds_within_Iio_of_Ioo_subset (order_dual α) _ _ _ _ _ _ ha (λ h, _), rcases hs h with ⟨b, hab, h⟩, use [b, hab], rwa dual_Ioo end lemma map_coe_at_top_of_Ioo_subset (hb : s ⊆ Iio b) (hs : ∀ a' < b, ∃ a < b, Ioo a b ⊆ s) : map (coe : s → α) at_top = 𝓝[Iio b] b := begin rcases eq_empty_or_nonempty (Iio b) with (hb'|⟨a, ha⟩), { rw [filter_eq_bot_of_not_nonempty at_top, map_bot, hb', nhds_within_empty], exact λ ⟨⟨x, hx⟩⟩, not_nonempty_iff_eq_empty.2 hb' ⟨x, hb hx⟩ }, { rw [← comap_coe_nhds_within_Iio_of_Ioo_subset hb (λ _, hs a ha), map_comap], rw subtype.range_coe, exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' ha).2 (hs a ha) }, end lemma map_coe_at_bot_of_Ioo_subset (ha : s ⊆ Ioi a) (hs : ∀ b' > a, ∃ b > a, Ioo a b ⊆ s) : map (coe : s → α) at_bot = (𝓝[Ioi a] a) := begin refine @map_coe_at_top_of_Ioo_subset (order_dual α) _ _ _ _ a s ha (λ b' hb', _), rcases hs b' hb' with ⟨b, hab, hbs⟩, use [b, hab], rwa dual_Ioo end /-- The `at_top` filter for an open interval `Ioo a b` comes from the left-neighbourhoods filter at the right endpoint in the ambient order. -/ lemma comap_coe_Ioo_nhds_within_Iio (a b : α) : comap (coe : Ioo a b → α) (𝓝[Iio b] b) = at_top := comap_coe_nhds_within_Iio_of_Ioo_subset Ioo_subset_Iio_self $ λ h, ⟨a, nonempty_Ioo.1 h, subset.refl _⟩ /-- The `at_bot` filter for an open interval `Ioo a b` comes from the right-neighbourhoods filter at the left endpoint in the ambient order. -/ lemma comap_coe_Ioo_nhds_within_Ioi (a b : α) : comap (coe : Ioo a b → α) (𝓝[Ioi a] a) = at_bot := comap_coe_nhds_within_Ioi_of_Ioo_subset Ioo_subset_Ioi_self $ λ h, ⟨b, nonempty_Ioo.1 h, subset.refl _⟩ lemma comap_coe_Ioi_nhds_within_Ioi (a : α) : comap (coe : Ioi a → α) (𝓝[Ioi a] a) = at_bot := comap_coe_nhds_within_Ioi_of_Ioo_subset (subset.refl _) $ λ ⟨x, hx⟩, ⟨x, hx, Ioo_subset_Ioi_self⟩ lemma comap_coe_Iio_nhds_within_Iio (a : α) : comap (coe : Iio a → α) (𝓝[Iio a] a) = at_top := @comap_coe_Ioi_nhds_within_Ioi (order_dual α) _ _ _ _ a @[simp] lemma map_coe_Ioo_at_top {a b : α} (h : a < b) : map (coe : Ioo a b → α) at_top = 𝓝[Iio b] b := map_coe_at_top_of_Ioo_subset Ioo_subset_Iio_self $ λ _ _, ⟨_, h, subset.refl _⟩ @[simp] lemma map_coe_Ioo_at_bot {a b : α} (h : a < b) : map (coe : Ioo a b → α) at_bot = 𝓝[Ioi a] a := map_coe_at_bot_of_Ioo_subset Ioo_subset_Ioi_self $ λ _ _, ⟨_, h, subset.refl _⟩ @[simp] lemma map_coe_Ioi_at_bot (a : α) : map (coe : Ioi a → α) at_bot = 𝓝[Ioi a] a := map_coe_at_bot_of_Ioo_subset (subset.refl _) $ λ b hb, ⟨b, hb, Ioo_subset_Ioi_self⟩ @[simp] lemma map_coe_Iio_at_top (a : α) : map (coe : Iio a → α) at_top = 𝓝[Iio a] a := @map_coe_Ioi_at_bot (order_dual α) _ _ _ _ _ variables {l : filter β} {f : α → β} @[simp] lemma tendsto_comp_coe_Ioo_at_top (h : a < b) : tendsto (λ x : Ioo a b, f x) at_top l ↔ tendsto f (𝓝[Iio b] b) l := by rw [← map_coe_Ioo_at_top h, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ioo_at_bot (h : a < b) : tendsto (λ x : Ioo a b, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l := by rw [← map_coe_Ioo_at_bot h, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ioi_at_bot : tendsto (λ x : Ioi a, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l := by rw [← map_coe_Ioi_at_bot, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iio_at_top : tendsto (λ x : Iio a, f x) at_top l ↔ tendsto f (𝓝[Iio a] a) l := by rw [← map_coe_Iio_at_top, tendsto_map'_iff] @[simp] lemma tendsto_Ioo_at_top {f : β → Ioo a b} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l (𝓝[Iio b] b) := by rw [← comap_coe_Ioo_nhds_within_Iio, tendsto_comap_iff] @[simp] lemma tendsto_Ioo_at_bot {f : β → Ioo a b} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l (𝓝[Ioi a] a) := by rw [← comap_coe_Ioo_nhds_within_Ioi, tendsto_comap_iff] @[simp] lemma tendsto_Ioi_at_bot {f : β → Ioi a} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l (𝓝[Ioi a] a) := by rw [← comap_coe_Ioi_nhds_within_Ioi, tendsto_comap_iff] @[simp] lemma tendsto_Iio_at_top {f : β → Iio a} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l (𝓝[Iio a] a) := by rw [← comap_coe_Iio_nhds_within_Iio, tendsto_comap_iff] end linear_order section complete_linear_order variables [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] [nonempty γ] lemma Sup_mem_closure {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : s.nonempty) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_Sup _) hs lemma Inf_mem_closure {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : s.nonempty) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_Inf _) hs lemma is_closed.Sup_mem {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : s.nonempty) (hc : is_closed s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_Sup _) hs hc lemma is_closed.Inf_mem {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : s.nonempty) (hc : is_closed s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_Inf _) hs hc /-- A monotone function continuous at the supremum of a nonempty set sends this supremum to the supremum of the image of this set. -/ lemma map_Sup_of_continuous_at_of_monotone' {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (hs : s.nonempty) : f (Sup s) = Sup (f '' s) := --This is a particular case of the more general is_lub_of_is_lub_of_tendsto (is_lub_of_is_lub_of_tendsto (λ x hx y hy xy, Mf xy) (is_lub_Sup _) hs $ Cf.mono_left inf_le_left).Sup_eq.symm /-- A monotone function `s` sending `bot` to `bot` and continuous at the supremum of a set sends this supremum to the supremum of the image of this set. -/ lemma map_Sup_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (fbot : f ⊥ = ⊥) : f (Sup s) = Sup (f '' s) := begin cases s.eq_empty_or_nonempty with h h, { simp [h, fbot] }, { exact map_Sup_of_continuous_at_of_monotone' Cf Mf h } end /-- A monotone function continuous at the indexed supremum over a nonempty `Sort` sends this indexed supremum to the indexed supremum of the composition. -/ lemma map_supr_of_continuous_at_of_monotone' {ι : Sort*} [nonempty ι] {f : α → β} {g : ι → α} (Cf : continuous_at f (supr g)) (Mf : monotone f) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_Sup_of_continuous_at_of_monotone' Cf Mf (range_nonempty g), ← range_comp, supr] /-- If a monotone function sending `bot` to `bot` is continuous at the indexed supremum over a `Sort`, then it sends this indexed supremum to the indexed supremum of the composition. -/ lemma map_supr_of_continuous_at_of_monotone {ι : Sort*} {f : α → β} {g : ι → α} (Cf : continuous_at f (supr g)) (Mf : monotone f) (fbot : f ⊥ = ⊥) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_Sup_of_continuous_at_of_monotone Cf Mf fbot, ← range_comp, supr] /-- A monotone function continuous at the infimum of a nonempty set sends this infimum to the infimum of the image of this set. -/ lemma map_Inf_of_continuous_at_of_monotone' {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (hs : s.nonempty) : f (Inf s) = Inf (f '' s) := @map_Sup_of_continuous_at_of_monotone' (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf Mf.order_dual hs /-- A monotone function `s` sending `top` to `top` and continuous at the infimum of a set sends this infimum to the infimum of the image of this set. -/ lemma map_Inf_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ftop : f ⊤ = ⊤) : f (Inf s) = Inf (f '' s) := @map_Sup_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf Mf.order_dual ftop /-- A monotone function continuous at the indexed infimum over a nonempty `Sort` sends this indexed infimum to the indexed infimum of the composition. -/ lemma map_infi_of_continuous_at_of_monotone' {ι : Sort*} [nonempty ι] {f : α → β} {g : ι → α} (Cf : continuous_at f (infi g)) (Mf : monotone f) : f (⨅ i, g i) = ⨅ i, f (g i) := @map_supr_of_continuous_at_of_monotone' (order_dual α) (order_dual β) _ _ _ _ _ _ ι _ f g Cf Mf.order_dual /-- If a monotone function sending `top` to `top` is continuous at the indexed infimum over a `Sort`, then it sends this indexed infimum to the indexed infimum of the composition. -/ lemma map_infi_of_continuous_at_of_monotone {ι : Sort*} {f : α → β} {g : ι → α} (Cf : continuous_at f (infi g)) (Mf : monotone f) (ftop : f ⊤ = ⊤) : f (infi g) = infi (f ∘ g) := @map_supr_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ ι f g Cf Mf.order_dual ftop end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] [nonempty γ] lemma cSup_mem_closure {s : set α} (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_cSup hs B) hs lemma cInf_mem_closure {s : set α} (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_cInf hs B) hs lemma is_closed.cSup_mem {s : set α} (hc : is_closed s) (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_cSup hs B) hs hc lemma is_closed.cInf_mem {s : set α} (hc : is_closed s) (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_cInf hs B) hs hc /-- If a monotone function is continuous at the supremum of a nonempty bounded above set `s`, then it sends this supremum to the supremum of the image of `s`. -/ lemma map_cSup_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (ne : s.nonempty) (H : bdd_above s) : f (Sup s) = Sup (f '' s) := begin refine ((is_lub_cSup (ne.image f) (Mf.map_bdd_above H)).unique _).symm, refine is_lub_of_is_lub_of_tendsto (λx hx y hy xy, Mf xy) (is_lub_cSup ne H) ne _, exact Cf.mono_left inf_le_left end /-- If a monotone function is continuous at the indexed supremum of a bounded function on a nonempty `Sort`, then it sends this supremum to the supremum of the composition. -/ lemma map_csupr_of_continuous_at_of_monotone {f : α → β} {g : γ → α} (Cf : continuous_at f (⨆ i, g i)) (Mf : monotone f) (H : bdd_above (range g)) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_cSup_of_continuous_at_of_monotone Cf Mf (range_nonempty _) H, ← range_comp, supr] /-- If a monotone function is continuous at the infimum of a nonempty bounded below set `s`, then it sends this infimum to the infimum of the image of `s`. -/ lemma map_cInf_of_continuous_at_of_monotone {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ne : s.nonempty) (H : bdd_below s) : f (Inf s) = Inf (f '' s) := @map_cSup_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ f s Cf Mf.order_dual ne H /-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally complete linear order, under a boundedness assumption. -/ lemma map_cinfi_of_continuous_at_of_monotone {f : α → β} {g : γ → α} (Cf : continuous_at f (⨅ i, g i)) (Mf : monotone f) (H : bdd_below (range g)) : f (⨅ i, g i) = ⨅ i, f (g i) := @map_csupr_of_continuous_at_of_monotone (order_dual α) (order_dual β) _ _ _ _ _ _ _ _ _ _ Cf Mf.order_dual H /-- A bounded connected subset of a conditionally complete linear order includes the open interval `(Inf s, Sup s)`. -/ lemma is_connected.Ioo_cInf_cSup_subset {s : set α} (hs : is_connected s) (hb : bdd_below s) (ha : bdd_above s) : Ioo (Inf s) (Sup s) ⊆ s := λ x hx, let ⟨y, ys, hy⟩ := (is_glb_lt_iff (is_glb_cInf hs.nonempty hb)).1 hx.1 in let ⟨z, zs, hz⟩ := (lt_is_lub_iff (is_lub_cSup hs.nonempty ha)).1 hx.2 in hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ lemma eq_Icc_cInf_cSup_of_connected_bdd_closed {s : set α} (hc : is_connected s) (hb : bdd_below s) (ha : bdd_above s) (hcl : is_closed s) : s = Icc (Inf s) (Sup s) := subset.antisymm (subset_Icc_cInf_cSup hb ha) $ hc.Icc_subset (hcl.cInf_mem hc.nonempty hb) (hcl.cSup_mem hc.nonempty ha) lemma is_preconnected.Ioi_cInf_subset {s : set α} (hs : is_preconnected s) (hb : bdd_below s) (ha : ¬bdd_above s) : Ioi (Inf s) ⊆ s := begin have sne : s.nonempty := @nonempty_of_not_bdd_above α _ s ⟨Inf ∅⟩ ha, intros x hx, obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := (is_glb_lt_iff (is_glb_cInf sne hb)).1 hx, obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x, exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ end lemma is_preconnected.Iio_cSup_subset {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s) (ha : bdd_above s) : Iio (Sup s) ⊆ s := @is_preconnected.Ioi_cInf_subset (order_dual α) _ _ _ s hs ha hb /-- A preconnected set in a conditionally complete linear order is either one of the intervals `[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +∞)`, `(Inf s, +∞)`, `(-∞, Sup s]`, `(-∞, Sup s)`, `(-∞, +∞)`, or `∅`. The converse statement requires `α` to be densely ordererd. -/ lemma is_preconnected.mem_intervals {s : set α} (hs : is_preconnected s) : s ∈ ({Icc (Inf s) (Sup s), Ico (Inf s) (Sup s), Ioc (Inf s) (Sup s), Ioo (Inf s) (Sup s), Ici (Inf s), Ioi (Inf s), Iic (Sup s), Iio (Sup s), univ, ∅} : set (set α)) := begin rcases s.eq_empty_or_nonempty with rfl|hne, { apply_rules [or.inr, mem_singleton] }, have hs' : is_connected s := ⟨hne, hs⟩, by_cases hb : bdd_below s; by_cases ha : bdd_above s, { rcases mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset (hs'.Ioo_cInf_cSup_subset hb ha) (subset_Icc_cInf_cSup hb ha) with hs|hs|hs|hs, { exact (or.inl hs) }, { exact (or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inr $ or.inl hs) } }, { refine (or.inr $ or.inr $ or.inr $ or.inr _), cases mem_Ici_Ioi_of_subset_of_subset (hs.Ioi_cInf_subset hb ha) (λ x hx, cInf_le hb hx) with hs hs, { exact or.inl hs }, { exact or.inr (or.inl hs) } }, { iterate 6 { apply or.inr }, cases mem_Iic_Iio_of_subset_of_subset (hs.Iio_cSup_subset hb ha) (λ x hx, le_cSup ha hx) with hs hs, { exact or.inl hs }, { exact or.inr (or.inl hs) } }, { iterate 8 { apply or.inr }, exact or.inl (hs.eq_univ_of_unbounded hb ha) } end /-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, or `univ`, or `∅`. The converse statement requires `α` to be densely ordererd. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ lemma set_of_is_preconnected_subset_of_ordered : {s : set α | is_preconnected s} ⊆ -- bounded intervals (range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪ -- unbounded intervals and `univ` (range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) := begin intros s hs, rcases hs.mem_intervals with hs|hs|hs|hs|hs|hs|hs|hs|hs|hs, { exact (or.inl $ or.inl $ or.inl $ or.inl ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inl ⟨Inf s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inr ⟨Inf s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) }, { exact (or.inr $ or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inr hs) } end /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and the set `s ∩ [a, b)` has no maximal point, then `b ∈ s`. -/ lemma is_closed.mem_of_ge_of_forall_exists_gt {a b : α} {s : set α} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hab : a ≤ b) (hgt : ∀ x ∈ s ∩ Ico a b, (s ∩ Ioc x b).nonempty) : b ∈ s := begin let S := s ∩ Icc a b, replace ha : a ∈ S, from ⟨ha, left_mem_Icc.2 hab⟩, have Sbd : bdd_above S, from ⟨b, λ z hz, hz.2.2⟩, let c := Sup (s ∩ Icc a b), have c_mem : c ∈ S, from hs.cSup_mem ⟨_, ha⟩ Sbd, have c_le : c ≤ b, from cSup_le ⟨_, ha⟩ (λ x hx, hx.2.2), cases eq_or_lt_of_le c_le with hc hc, from hc ▸ c_mem.1, exfalso, rcases hgt c ⟨c_mem.1, c_mem.2.1, hc⟩ with ⟨x, xs, cx, xb⟩, exact not_lt_of_le (le_cSup Sbd ⟨xs, le_trans (le_cSup Sbd ha) (le_of_lt cx), xb⟩) cx end /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `a ≤ x < y ≤ b`, `x ∈ s`, the set `s ∩ (x, y]` is not empty, then `[a, b] ⊆ s`. -/ lemma is_closed.Icc_subset_of_forall_exists_gt {a b : α} {s : set α} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : ∀ x ∈ s ∩ Ico a b, ∀ y ∈ Ioi x, (s ∩ Ioc x y).nonempty) : Icc a b ⊆ s := begin assume y hy, have : is_closed (s ∩ Icc a y), { suffices : s ∩ Icc a y = s ∩ Icc a b ∩ Icc a y, { rw this, exact is_closed_inter hs is_closed_Icc }, rw [inter_assoc], congr, exact (inter_eq_self_of_subset_right $ Icc_subset_Icc_right hy.2).symm }, exact is_closed.mem_of_ge_of_forall_exists_gt this ha hy.1 (λ x hx, hgt x ⟨hx.1, Ico_subset_Ico_right hy.2 hx.2⟩ y hx.2.2) end section densely_ordered variables [densely_ordered α] {a b : α} /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `x ∈ s ∩ [a, b)` the set `s` includes some open neighborhood of `x` within `(x, +∞)`, then `[a, b] ⊆ s`. -/ lemma is_closed.Icc_subset_of_forall_mem_nhds_within {a b : α} {s : set α} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : ∀ x ∈ s ∩ Ico a b, s ∈ 𝓝[Ioi x] x) : Icc a b ⊆ s := begin apply hs.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxs, hxab⟩ y hyxb, have : s ∩ Ioc x y ∈ 𝓝[Ioi x] x, from inter_mem_sets (hgt x ⟨hxs, hxab⟩) (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hyxb⟩), exact (nhds_within_Ioi_self_ne_bot' hxab.2).nonempty_of_mem this end /-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/ lemma is_preconnected_Icc : is_preconnected (Icc a b) := is_preconnected_closed_iff.2 begin rintros s t hs ht hab ⟨x, hx⟩ ⟨y, hy⟩, wlog hxy : x ≤ y := le_total x y using [x y s t, y x t s], have xyab : Icc x y ⊆ Icc a b := Icc_subset_Icc hx.1.1 hy.1.2, by_contradiction hst, suffices : Icc x y ⊆ s, from hst ⟨y, xyab $ right_mem_Icc.2 hxy, this $ right_mem_Icc.2 hxy, hy.2⟩, apply (is_closed_inter hs is_closed_Icc).Icc_subset_of_forall_mem_nhds_within hx.2, rintros z ⟨zs, hz⟩, have zt : z ∈ tᶜ, from λ zt, hst ⟨z, xyab $ Ico_subset_Icc_self hz, zs, zt⟩, have : tᶜ ∩ Ioc z y ∈ 𝓝[Ioi z] z, { rw [← nhds_within_Ioc_eq_nhds_within_Ioi hz.2], exact mem_nhds_within.2 ⟨tᶜ, ht, zt, subset.refl _⟩}, apply mem_sets_of_superset this, have : Ioc z y ⊆ s ∪ t, from λ w hw, hab (xyab ⟨le_trans hz.1 (le_of_lt hw.1), hw.2⟩), exact λ w ⟨wt, wzy⟩, (this wzy).elim id (λ h, (wt h).elim) end lemma is_preconnected_interval : is_preconnected (interval a b) := is_preconnected_Icc lemma is_preconnected_iff_ord_connected {s : set α} : is_preconnected s ↔ ord_connected s := ⟨λ h x hx y hy, h.Icc_subset hx hy, λ h, is_preconnected_of_forall_pair $ λ x y hx hy, ⟨interval x y, h.interval_subset hx hy, left_mem_interval, right_mem_interval, is_preconnected_interval⟩⟩ alias is_preconnected_iff_ord_connected ↔ is_preconnected.ord_connected set.ord_connected.is_preconnected lemma is_preconnected_Ici : is_preconnected (Ici a) := ord_connected_Ici.is_preconnected lemma is_preconnected_Iic : is_preconnected (Iic a) := ord_connected_Iic.is_preconnected lemma is_preconnected_Iio : is_preconnected (Iio a) := ord_connected_Iio.is_preconnected lemma is_preconnected_Ioi : is_preconnected (Ioi a) := ord_connected_Ioi.is_preconnected lemma is_preconnected_Ioo : is_preconnected (Ioo a b) := ord_connected_Ioo.is_preconnected lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := ord_connected_Ioc.is_preconnected lemma is_preconnected_Ico : is_preconnected (Ico a b) := ord_connected_Ico.is_preconnected @[priority 100] instance ordered_connected_space : preconnected_space α := ⟨ord_connected_univ.is_preconnected⟩ /-- In a dense conditionally complete linear order, the set of preconnected sets is exactly the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-∞, +∞)`, or `∅`. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ lemma set_of_is_preconnected_eq_of_ordered : {s : set α | is_preconnected s} = -- bounded intervals (range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪ -- unbounded intervals and `univ` (range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) := begin refine subset.antisymm set_of_is_preconnected_subset_of_ordered _, simp only [subset_def, -mem_range, forall_range_iff, uncurry, or_imp_distrib, forall_and_distrib, mem_union, mem_set_of_eq, insert_eq, mem_singleton_iff, forall_eq, forall_true_iff, and_true, is_preconnected_Icc, is_preconnected_Ico, is_preconnected_Ioc, is_preconnected_Ioo, is_preconnected_Ioi, is_preconnected_Iio, is_preconnected_Ici, is_preconnected_Iic, is_preconnected_univ, is_preconnected_empty], end variables {δ : Type*} [linear_order δ] [topological_space δ] [order_closed_topology δ] /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a ≤ t ≤ f b`.-/ lemma intermediate_value_Icc {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) : Icc (f a) (f b) ⊆ f '' (Icc a b) := is_preconnected_Icc.intermediate_value (left_mem_Icc.2 hab) (right_mem_Icc.2 hab) hf /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a ≥ t ≥ f b`.-/ lemma intermediate_value_Icc' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) : Icc (f b) (f a) ⊆ f '' (Icc a b) := is_preconnected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf /-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/ lemma continuous.surjective {f : α → δ} (hf : continuous f) (h_top : tendsto f at_top at_top) (h_bot : tendsto f at_bot at_bot) : function.surjective f := λ p, mem_range_of_exists_le_of_exists_ge hf (h_bot.eventually (eventually_le_at_bot p)).exists (h_top.eventually (eventually_ge_at_top p)).exists /-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/ lemma continuous.surjective' {f : α → δ} (hf : continuous f) (h_top : tendsto f at_bot at_top) (h_bot : tendsto f at_top at_bot) : function.surjective f := @continuous.surjective (order_dual α) _ _ _ _ _ _ _ _ _ hf h_top h_bot /-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_bot : filter β` along `at_bot : filter ↥s` and tends to `at_top : filter β` along `at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ lemma continuous_on.surj_on_of_tendsto {f : α → β} {s : set α} [ord_connected s] (hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_bot) (htop : tendsto (λ x : s, f x) at_top at_top) : surj_on f s univ := by haveI := inhabited_of_nonempty hs.to_subtype; exact (surj_on_iff_surjective.2 $ (continuous_on_iff_continuous_restrict.1 hf).surjective htop hbot) /-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_top : filter β` along `at_bot : filter ↥s` and tends to `at_bot : filter β` along `at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ lemma continuous_on.surj_on_of_tendsto' {f : α → β} {s : set α} [ord_connected s] (hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_top) (htop : tendsto (λ x : s, f x) at_top at_bot) : surj_on f s univ := @continuous_on.surj_on_of_tendsto α (order_dual β) _ _ _ _ _ _ _ _ _ _ hs hf hbot htop end densely_ordered lemma is_compact.Inf_mem {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : Inf s ∈ s := hs.is_closed.cInf_mem ne_s hs.bdd_below lemma is_compact.Sup_mem {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : Sup s ∈ s := @is_compact.Inf_mem (order_dual α) _ _ _ _ hs ne_s lemma is_compact.is_glb_Inf {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : is_glb s (Inf s) := is_glb_cInf ne_s hs.bdd_below lemma is_compact.is_lub_Sup {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : is_lub s (Sup s) := @is_compact.is_glb_Inf (order_dual α) _ _ _ _ hs ne_s lemma is_compact.is_least_Inf {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : is_least s (Inf s) := ⟨hs.Inf_mem ne_s, (hs.is_glb_Inf ne_s).1⟩ lemma is_compact.is_greatest_Sup {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : is_greatest s (Sup s) := @is_compact.is_least_Inf (order_dual α) _ _ _ _ hs ne_s lemma is_compact.exists_is_least {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : ∃ x, is_least s x := ⟨_, hs.is_least_Inf ne_s⟩ lemma is_compact.exists_is_greatest {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : ∃ x, is_greatest s x := ⟨_, hs.is_greatest_Sup ne_s⟩ lemma is_compact.exists_is_glb {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : ∃ x ∈ s, is_glb s x := ⟨_, hs.Inf_mem ne_s, hs.is_glb_Inf ne_s⟩ lemma is_compact.exists_is_lub {s : set α} (hs : is_compact s) (ne_s : s.nonempty) : ∃ x ∈ s, is_lub s x := ⟨_, hs.Sup_mem ne_s, hs.is_lub_Sup ne_s⟩ lemma is_compact.exists_Inf_image_eq {α : Type u} [topological_space α] {s : set α} (hs : is_compact s) (ne_s : s.nonempty) {f : α → β} (hf : continuous_on f s) : ∃ x ∈ s, Inf (f '' s) = f x := let ⟨x, hxs, hx⟩ := (hs.image_of_continuous_on hf).Inf_mem (ne_s.image f) in ⟨x, hxs, hx.symm⟩ lemma is_compact.exists_Sup_image_eq {α : Type u} [topological_space α]: ∀ {s : set α}, is_compact s → s.nonempty → ∀ {f : α → β}, continuous_on f s → ∃ x ∈ s, Sup (f '' s) = f x := @is_compact.exists_Inf_image_eq (order_dual β) _ _ _ _ _ lemma eq_Icc_of_connected_compact {s : set α} (h₁ : is_connected s) (h₂ : is_compact s) : s = Icc (Inf s) (Sup s) := eq_Icc_cInf_cSup_of_connected_bdd_closed h₁ h₂.bdd_below h₂.bdd_above h₂.is_closed /-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/ lemma is_compact.exists_forall_le {α : Type u} [topological_space α] {s : set α} (hs : is_compact s) (ne_s : s.nonempty) {f : α → β} (hf : continuous_on f s) : ∃x∈s, ∀y∈s, f x ≤ f y := begin rcases hs.exists_Inf_image_eq ne_s hf with ⟨x, hxs, hx⟩, refine ⟨x, hxs, λ y hy, _⟩, rw ← hx, exact ((hs.image_of_continuous_on hf).is_glb_Inf (ne_s.image f)).1 (mem_image_of_mem _ hy) end /-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/ lemma is_compact.exists_forall_ge {α : Type u} [topological_space α]: ∀ {s : set α}, is_compact s → s.nonempty → ∀ {f : α → β}, continuous_on f s → ∃x∈s, ∀y∈s, f y ≤ f x := @is_compact.exists_forall_le (order_dual β) _ _ _ _ _ /-- The extreme value theorem: if a continuous function `f` tends to infinity away from compact sets, then it has a global minimum. -/ lemma continuous.exists_forall_le {α : Type*} [topological_space α] [nonempty α] {f : α → β} (hf : continuous f) (hlim : tendsto f (cocompact α) at_top) : ∃ x, ∀ y, f x ≤ f y := begin inhabit α, obtain ⟨s : set α, hsc : is_compact s, hsf : ∀ x ∉ s, f (default α) ≤ f x⟩ := (has_basis_cocompact.tendsto_iff at_top_basis).1 hlim (f $ default α) trivial, obtain ⟨x, -, hx⟩ := (hsc.insert (default α)).exists_forall_le (nonempty_insert _ _) hf.continuous_on, refine ⟨x, λ y, _⟩, by_cases hy : y ∈ s, exacts [hx y (or.inr hy), (hx _ (or.inl rfl)).trans (hsf y hy)] end /-- The extreme value theorem: if a continuous function `f` tends to negative infinity away from compactx sets, then it has a global maximum. -/ lemma continuous.exists_forall_ge {α : Type*} [topological_space α] [nonempty α] {f : α → β} (hf : continuous f) (hlim : tendsto f (cocompact α) at_bot) : ∃ x, ∀ y, f y ≤ f x := @continuous.exists_forall_le (order_dual β) _ _ _ _ _ _ _ hf hlim end conditionally_complete_linear_order section liminf_limsup section order_closed_topology variables [semilattice_sup α] [topological_space α] [order_topology α] lemma is_bounded_le_nhds (a : α) : (𝓝 a).is_bounded (≤) := match forall_le_or_exists_lt_sup a with | or.inl h := ⟨a, eventually_of_forall h⟩ | or.inr ⟨b, hb⟩ := ⟨b, ge_mem_nhds hb⟩ end lemma filter.tendsto.is_bounded_under_le {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≤) u := (is_bounded_le_nhds a).mono h lemma is_cobounded_ge_nhds (a : α) : (𝓝 a).is_cobounded (≥) := (is_bounded_le_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_ge {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≥) u := h.is_bounded_under_le.is_cobounded_flip end order_closed_topology section order_closed_topology variables [semilattice_inf α] [topological_space α] [order_topology α] lemma is_bounded_ge_nhds (a : α) : (𝓝 a).is_bounded (≥) := @is_bounded_le_nhds (order_dual α) _ _ _ a lemma filter.tendsto.is_bounded_under_ge {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≥) u := (is_bounded_ge_nhds a).mono h lemma is_cobounded_le_nhds (a : α) : (𝓝 a).is_cobounded (≤) := (is_bounded_ge_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_le {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≤) u := h.is_bounded_under_ge.is_cobounded_flip end order_closed_topology section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] theorem lt_mem_sets_of_Limsup_lt {f : filter α} {b} (h : f.is_bounded (≤)) (l : f.Limsup < b) : ∀ᶠ a in f, a < b := let ⟨c, (h : ∀ᶠ a in f, a ≤ c), hcb⟩ := exists_lt_of_cInf_lt h l in mem_sets_of_superset h $ assume a hac, lt_of_le_of_lt hac hcb theorem gt_mem_sets_of_Liminf_gt : ∀ {f : filter α} {b}, f.is_bounded (≥) → b < f.Liminf → ∀ᶠ a in f, b < a := @lt_mem_sets_of_Limsup_lt (order_dual α) _ variables [topological_space α] [order_topology α] /-- If the liminf and the limsup of a filter coincide, then this filter converges to their common value, at least if the filter is eventually bounded above and below. -/ theorem le_nhds_of_Limsup_eq_Liminf {f : filter α} {a : α} (hl : f.is_bounded (≤)) (hg : f.is_bounded (≥)) (hs : f.Limsup = a) (hi : f.Liminf = a) : f ≤ 𝓝 a := tendsto_order.2 $ and.intro (assume b hb, gt_mem_sets_of_Liminf_gt hg $ hi.symm ▸ hb) (assume b hb, lt_mem_sets_of_Limsup_lt hl $ hs.symm ▸ hb) theorem Limsup_nhds (a : α) : Limsup (𝓝 a) = a := cInf_intro (is_bounded_le_nhds a) (assume a' (h : {n : α | n ≤ a'} ∈ 𝓝 a), show a ≤ a', from @mem_of_nhds α _ a _ h) (assume b (hba : a < b), show ∃c (h : {n : α | n ≤ c} ∈ 𝓝 a), c < b, from match dense_or_discrete a b with | or.inl ⟨c, hac, hcb⟩ := ⟨c, ge_mem_nhds hac, hcb⟩ | or.inr ⟨_, h⟩ := ⟨a, (𝓝 a).sets_of_superset (gt_mem_nhds hba) h, hba⟩ end) theorem Liminf_nhds : ∀ (a : α), Liminf (𝓝 a) = a := @Limsup_nhds (order_dual α) _ _ _ /-- If a filter is converging, its limsup coincides with its limit. -/ theorem Liminf_eq_of_le_nhds {f : filter α} {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : f.Liminf = a := have hb_ge : is_bounded (≥) f, from (is_bounded_ge_nhds a).mono h, have hb_le : is_bounded (≤) f, from (is_bounded_le_nhds a).mono h, le_antisymm (calc f.Liminf ≤ f.Limsup : Liminf_le_Limsup hb_le hb_ge ... ≤ (𝓝 a).Limsup : Limsup_le_Limsup_of_le h hb_ge.is_cobounded_flip (is_bounded_le_nhds a) ... = a : Limsup_nhds a) (calc a = (𝓝 a).Liminf : (Liminf_nhds a).symm ... ≤ f.Liminf : Liminf_le_Liminf_of_le h (is_bounded_ge_nhds a) hb_le.is_cobounded_flip) /-- If a filter is converging, its liminf coincides with its limit. -/ theorem Limsup_eq_of_le_nhds : ∀ {f : filter α} {a : α} [ne_bot f], f ≤ 𝓝 a → f.Limsup = a := @Liminf_eq_of_le_nhds (order_dual α) _ _ _ /-- If a function has a limit, then its limsup coincides with its limit. -/ theorem filter.tendsto.limsup_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : limsup f u = a := Limsup_eq_of_le_nhds h /-- If a function has a limit, then its liminf coincides with its limit. -/ theorem filter.tendsto.liminf_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : liminf f u = a := Liminf_eq_of_le_nhds h end conditionally_complete_linear_order section complete_linear_order variables [complete_linear_order α] [topological_space α] [order_topology α] -- In complete_linear_order, the above theorems take a simpler form /-- If the liminf and the limsup of a function coincide, then the limit of the function exists and has the same value -/ theorem tendsto_of_liminf_eq_limsup {f : filter β} {u : β → α} {a : α} (hinf : liminf f u = a) (hsup : limsup f u = a) : tendsto u f (𝓝 a) := le_nhds_of_Limsup_eq_Liminf is_bounded_le_of_top is_bounded_ge_of_bot hsup hinf /-- If a number `a` is less than or equal to the `liminf` of a function `f` at some filter and is greater than or equal to the `limsup` of `f`, then `f` tends to `a` along this filter. -/ theorem tendsto_of_le_liminf_of_limsup_le {f : filter β} {u : β → α} {a : α} (hinf : a ≤ liminf f u) (hsup : limsup f u ≤ a) : tendsto u f (𝓝 a) := if hf : f = ⊥ then hf.symm ▸ tendsto_bot else by haveI : ne_bot f := hf; exact tendsto_of_liminf_eq_limsup (le_antisymm (le_trans liminf_le_limsup hsup) hinf) (le_antisymm hsup (le_trans hinf liminf_le_limsup)) end complete_linear_order end liminf_limsup end order_topology section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] [topological_space α] local notation `|` x `|` := abs x lemma nhds_eq_infi_abs_sub [order_topology α] (a : α) : 𝓝 a = (⨅r>0, 𝓟 {b | |a - b| < r}) := begin simp only [le_antisymm_iff, nhds_eq_order, le_inf_iff, le_infi_iff, le_principal_iff, mem_Ioi, mem_Iio, abs_sub_lt_iff, @sub_lt_iff_lt_add _ _ _ _ a, @sub_lt _ _ a, set_of_and], refine ⟨_, _, _⟩, { intros ε ε0, exact inter_mem_inf_sets (mem_infi_sets (a - ε) $ mem_infi_sets (sub_lt_self a ε0) (mem_principal_self _)) (mem_infi_sets (ε + a) $ mem_infi_sets (by simpa) (mem_principal_self _)) }, { intros b hb, exact mem_infi_sets (a - b) (mem_infi_sets (sub_pos.2 hb) (by simp [Ioi])) }, { intros b hb, exact mem_infi_sets (b - a) (mem_infi_sets (sub_pos.2 hb) (by simp [Iio])) } end lemma order_topology_of_nhds_abs (h_nhds : ∀a:α, 𝓝 a = (⨅r>0, 𝓟 {b | |a - b| < r})) : order_topology α := begin refine ⟨eq_of_nhds_eq_nhds $ λ a, _⟩, rw [h_nhds], letI := preorder.topology α, letI : order_topology α := ⟨rfl⟩, exact (nhds_eq_infi_abs_sub a).symm end variables [order_topology α] lemma linear_ordered_add_comm_group.tendsto_nhds {f : β → α} {x : filter β} {a : α} : tendsto f x (𝓝 a) ↔ ∀ ε > (0 : α), ∀ᶠ b in x, |f b - a| < ε := by simp [nhds_eq_infi_abs_sub, abs_sub a] lemma eventually_abs_sub_lt (a : α) {ε : α} (hε : 0 < ε) : ∀ᶠ x in 𝓝 a, |x - a| < ε := (nhds_eq_infi_abs_sub a).symm ▸ mem_infi_sets ε (mem_infi_sets hε $ by simp only [abs_sub, mem_principal_self]) @[priority 100] -- see Note [lower instance priority] instance linear_ordered_add_comm_group.topological_add_group : topological_add_group α := { continuous_add := begin refine continuous_iff_continuous_at.2 _, rintro ⟨a, b⟩, refine linear_ordered_add_comm_group.tendsto_nhds.2 (λ ε ε0, _), rcases dense_or_discrete 0 ε with (⟨δ, δ0, δε⟩|⟨h₁, h₂⟩), { -- If there exists `δ ∈ (0, ε)`, then we choose `δ`-nhd of `a` and `(ε-δ)`-nhd of `b` filter_upwards [prod_mem_nhds_sets (eventually_abs_sub_lt a δ0) (eventually_abs_sub_lt b (sub_pos.2 δε))], rintros ⟨x, y⟩ ⟨hx : |x - a| < δ, hy : |y - b| < ε - δ⟩, rw [add_sub_comm], calc |x - a + (y - b)| ≤ |x - a| + |y - b| : abs_add _ _ ... < δ + (ε - δ) : add_lt_add hx hy ... = ε : add_sub_cancel'_right _ _ }, { -- Otherewise `ε`-nhd of each point `a` is `{a}` have hε : ∀ {x y}, abs (x - y) < ε → x = y, { intros x y h, simpa [sub_eq_zero] using h₂ _ h }, filter_upwards [prod_mem_nhds_sets (eventually_abs_sub_lt a ε0) (eventually_abs_sub_lt b ε0)], rintros ⟨x, y⟩ ⟨hx : |x - a| < ε, hy : |y - b| < ε⟩, simpa [hε hx, hε hy] } end, continuous_neg := continuous_iff_continuous_at.2 $ λ a, linear_ordered_add_comm_group.tendsto_nhds.2 $ λ ε ε0, (eventually_abs_sub_lt a ε0).mono $ λ x hx, by rwa [neg_sub_neg, abs_sub] } @[continuity] lemma continuous_abs : continuous (abs : α → α) := continuous_id.max continuous_neg lemma filter.tendsto.abs {f : β → α} {a : α} {l : filter β} (h : tendsto f l (𝓝 a)) : tendsto (λ x, |f x|) l (𝓝 (|a|)) := (continuous_abs.tendsto _).comp h variables [topological_space β] {f : β → α} {b : β} {a : α} {s : set β} lemma continuous.abs (h : continuous f) : continuous (λ x, |f x|) := continuous_abs.comp h lemma continuous_at.abs (h : continuous_at f b) : continuous_at (λ x, |f x|) b := h.abs lemma continuous_within_at.abs (h : continuous_within_at f s b) : continuous_within_at (λ x, |f x|) s b := h.abs lemma continuous_on.abs (h : continuous_on f s) : continuous_on (λ x, |f x|) s := λ x hx, (h x hx).abs lemma tendsto_abs_nhds_within_zero : tendsto (abs : α → α) (𝓝[{0}ᶜ] 0) (𝓝[Ioi 0] 0) := (continuous_abs.tendsto' (0 : α) 0 abs_zero).inf $ tendsto_principal_principal.2 $ λ x, abs_pos.2 end linear_ordered_add_comm_group /-! Here is a counter-example to a version of the following with `conditionally_complete_lattice α`. Take `α = [0, 1) → ℝ` with the natural lattice structure, `ι = ℕ`. Put `f n x = -x^n`. Then `⨆ n, f n = 0` while none of `f n` is strictly greater than the constant function `-0.5`. -/ lemma tendsto_at_top_csupr {ι α : Type*} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) (hbdd : bdd_above $ range f) : tendsto f at_top (𝓝 (⨆i, f i)) := begin by_cases hi : nonempty ι, { resetI, rw tendsto_order, split, { intros a h, cases exists_lt_of_lt_csupr h with N hN, apply eventually.mono (mem_at_top N), exact λ i hi, lt_of_lt_of_le hN (h_mono hi) }, { exact λ a h, eventually_of_forall (λ n, lt_of_le_of_lt (le_csupr hbdd n) h) } }, { exact tendsto_of_not_nonempty hi } end lemma tendsto_at_top_cinfi {ι α : Type*} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : ∀ ⦃i j⦄, i ≤ j → f j ≤ f i) (hbdd : bdd_below $ range f) : tendsto f at_top (𝓝 (⨅i, f i)) := @tendsto_at_top_csupr _ (order_dual α) _ _ _ _ _ @h_mono hbdd lemma tendsto_at_top_supr {ι α : Type*} [preorder ι] [topological_space α] [complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) : tendsto f at_top (𝓝 (⨆i, f i)) := tendsto_at_top_csupr h_mono (order_top.bdd_above _) lemma tendsto_at_top_infi {ι α : Type*} [preorder ι] [topological_space α] [complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : ∀ ⦃i j⦄, i ≤ j → f j ≤ f i) : tendsto f at_top (𝓝 (⨅i, f i)) := tendsto_at_top_cinfi @h_mono (order_bot.bdd_below _) lemma tendsto_of_monotone {ι α : Type*} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) : tendsto f at_top at_top ∨ (∃ l, tendsto f at_top (𝓝 l)) := if H : bdd_above (range f) then or.inr ⟨_, tendsto_at_top_csupr h_mono H⟩ else or.inl $ tendsto_at_top_at_top_of_monotone' h_mono H lemma supr_eq_of_tendsto {α β} [topological_space α] [complete_linear_order α] [order_topology α] [nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : monotone f) : tendsto f at_top (𝓝 a) → supr f = a := tendsto_nhds_unique (tendsto_at_top_supr hf) lemma infi_eq_of_tendsto {α} [topological_space α] [complete_linear_order α] [order_topology α] [nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : ∀n m, n ≤ m → f m ≤ f n) : tendsto f at_top (𝓝 a) → infi f = a := tendsto_nhds_unique (tendsto_at_top_infi hf) @[to_additive] lemma tendsto_inv_nhds_within_Ioi [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Ioi a] a) (𝓝[Iio (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iio [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Iio a] a) (𝓝[Ioi (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ioi_inv [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Ioi (a⁻¹)] (a⁻¹)) (𝓝[Iio a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ioi _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iio_inv [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Iio (a⁻¹)] (a⁻¹)) (𝓝[Ioi a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iio _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Ici [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Ici a] a) (𝓝[Iic (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iic [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Iic a] a) (𝓝[Ici (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ici_inv [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Ici (a⁻¹)] (a⁻¹)) (𝓝[Iic a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ici _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iic_inv [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : tendsto has_inv.inv (𝓝[Iic (a⁻¹)] (a⁻¹)) (𝓝[Ici a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iic _ _ _ _ (a⁻¹) lemma nhds_left_sup_nhds_right (a : α) [topological_space α] [linear_order α] : 𝓝[Iic a] a ⊔ 𝓝[Ici a] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ici, nhds_within_univ] lemma nhds_left'_sup_nhds_right (a : α) [topological_space α] [linear_order α] : 𝓝[Iio a] a ⊔ 𝓝[Ici a] a = 𝓝 a := by rw [← nhds_within_union, Iio_union_Ici, nhds_within_univ] lemma nhds_left_sup_nhds_right' (a : α) [topological_space α] [linear_order α] : 𝓝[Iic a] a ⊔ 𝓝[Ioi a] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ioi, nhds_within_univ] lemma continuous_at_iff_continuous_left_right [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (Iic a) a ∧ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, continuous_at, ← tendsto_sup, nhds_left_sup_nhds_right] lemma continuous_on_Icc_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a b : α} {la lb : β} (hab : a < b) (hf : continuous_on f (Ioo a b)) (ha : tendsto f (𝓝[Ioi a] a) (𝓝 la)) (hb : tendsto f (𝓝[Iio b] b) (𝓝 lb)) : continuous_on (extend_from (Ioo a b) f) (Icc a b) := begin apply continuous_on_extend_from, { rw closure_Ioo hab, }, { intros x x_in, rcases mem_Ioo_or_eq_endpoints_of_mem_Icc x_in with rfl | rfl | h, { use la, simpa [hab] }, { use lb, simpa [hab] }, { use [f x, hf x h] } } end lemma eq_lim_at_left_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α} {la : β} (hab : a < b) (ha : tendsto f (𝓝[Ioi a] a) (𝓝 la)) : extend_from (Ioo a b) f a = la := begin apply extend_from_eq, { rw closure_Ioo hab, simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] }, { simpa [hab] } end lemma eq_lim_at_right_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α} {lb : β} (hab : a < b) (hb : tendsto f (𝓝[Iio b] b) (𝓝 lb)) : extend_from (Ioo a b) f b = lb := begin apply extend_from_eq, { rw closure_Ioo hab, simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] }, { simpa [hab] } end lemma continuous_on_Ico_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a b : α} {la : β} (hab : a < b) (hf : continuous_on f (Ioo a b)) (ha : tendsto f (𝓝[Ioi a] a) (𝓝 la)) : continuous_on (extend_from (Ioo a b) f) (Ico a b) := begin apply continuous_on_extend_from, { rw [closure_Ioo hab], exact Ico_subset_Icc_self, }, { intros x x_in, rcases mem_Ioo_or_eq_left_of_mem_Ico x_in with rfl | h, { use la, simpa [hab] }, { use [f x, hf x h] } } end lemma continuous_on_Ioc_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a b : α} {lb : β} (hab : a < b) (hf : continuous_on f (Ioo a b)) (hb : tendsto f (𝓝[Iio b] b) (𝓝 lb)) : continuous_on (extend_from (Ioo a b) f) (Ioc a b) := begin have := @continuous_on_Ico_extend_from_Ioo (order_dual α) _ _ _ _ _ _ _ f _ _ _ hab, erw [dual_Ico, dual_Ioi, dual_Ioo] at this, exact this hf hb end lemma continuous_within_at_Ioi_iff_Ici {α β : Type*} [topological_space α] [partial_order α] [topological_space β] {a : α} {f : α → β} : continuous_within_at f (Ioi a) a ↔ continuous_within_at f (Ici a) a := by simp only [← Ici_diff_left, continuous_within_at_diff_self] lemma continuous_within_at_Iio_iff_Iic {α β : Type*} [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_within_at f (Iio a) a ↔ continuous_within_at f (Iic a) a := begin have := @continuous_within_at_Ioi_iff_Ici (order_dual α) _ _ _ _ _ f, erw [dual_Ici, dual_Ioi] at this, exact this, end lemma continuous_at_iff_continuous_left'_right' [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (Iio a) a ∧ continuous_within_at f (Ioi a) a := by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic, continuous_at_iff_continuous_left_right] /-! ### Continuity of monotone functions In this section we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_mono_incr_on_of_image_mem_nhds`, as well as several similar facts. -/ section linear_order variables [linear_order α] [topological_space α] [order_topology α] variables [linear_order β] [topological_space β] [order_topology β] /-- If `f` is a function strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_incr_on.continuous_at_right_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, rw [h_mono.lt_iff_lt has hcs] at hac, filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)], rintros x hx ⟨hax, hxc⟩, exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb } end /-- If `f` is a function monotonically increasing function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_right_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le (h_mono _ has _ hxs hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, have : a < c, from not_le.1 (λ h, hac.not_le $ h_mono _ hcs _ has h), filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)], rintros x hx ⟨hax, hxc⟩, exact (h_mono _ hx _ hcs hxc.le).trans_lt hcb } end /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := begin refine continuous_at_right_of_mono_incr_on_of_exists_between h_mono hs (λ b hb, _), rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩, rcases exists_between hab' with ⟨c', hc'⟩, rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc' with ⟨_, hc, ⟨c, hcs, rfl⟩⟩, exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩ end /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs $ mem_sets_of_superset hfs subset_closure /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (λ x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs (mem_sets_of_superset hfs subset_closure) /-- If a function `f` is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : surj_on f s (Ioi (f a))) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb⟩ := hfs hb in ⟨c, hcs, hcb.symm ▸ hb, hcb.le⟩ /-- If `f` is a function strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_incr_on.continuous_at_left_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If `f` is a function monotonically increasing function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_left_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_mono_incr_on_of_exists_between (order_dual α) (order_dual β) _ _ _ _ _ _ f s a (λ x hx y hy, h_mono y hy x hx) hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ lemma continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (order_dual α) (order_dual β) _ _ _ _ _ _ _ f s a (λ x hx y hy, h_mono y hy x hx) hs hfs /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma continuous_at_left_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs (mem_sets_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs /-- If a function `f` is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : surj_on f s (Iio (f a))) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_surj_on hs hfs /-- If a function `f` is strictly monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l, h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_sets_of_superset hfs subset_closure) /-- If `f` is a function monotonically increasing function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, continuous_at_right_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x ∈ s) (y ∈ s), x ≤ y → f x ≤ f y) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_of_mono_incr_on_of_closure_image_mem_nhds h_mono hs (mem_sets_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ lemma monotone.continuous_of_dense_range [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := continuous_iff_continuous_at.mpr $ λ a, continuous_at_of_mono_incr_on_of_closure_image_mem_nhds (λ x hx y hy hxy, h_mono hxy) univ_mem_sets $ by simp only [image_univ, h_dense.closure_eq, univ_mem_sets] /-- A monotone surjective function with a densely ordered codomain is surjective. -/ lemma monotone.continuous_of_surjective [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := h_mono.continuous_of_dense_range h_surj.dense_range end linear_order /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso variables [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] protected lemma continuous (e : α ≃o β) : continuous e := begin rw [‹order_topology β›.topology_eq_generate_intervals], refine continuous_generated_from (λ s hs, _), rcases hs with ⟨a, rfl|rfl⟩, { rw e.preimage_Ioi, apply is_open_lt' }, { rw e.preimage_Iio, apply is_open_gt' } end /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph (e : α ≃o β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.symm.continuous, .. e } @[simp] lemma coe_to_homeomorph (e : α ≃o β) : ⇑e.to_homeomorph = e := rfl @[simp] lemma coe_to_homeomorph_symm (e : α ≃o β) : ⇑e.to_homeomorph.symm = e.symm := rfl end order_iso section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] [densely_ordered α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] /-- If `f : α → β` is strictly monotone and continuous, and tendsto `at_top` `at_top` and to `at_bot` `at_bot`, then it is a homeomorphism. -/ noncomputable def homeomorph_of_strict_mono_continuous (f : α → β) (h_mono : strict_mono f) (h_cont : continuous f) (h_top : tendsto f at_top at_top) (h_bot : tendsto f at_bot at_bot) : homeomorph α β := (h_mono.order_iso_of_surjective f (h_cont.surjective h_top h_bot)).to_homeomorph @[simp] lemma coe_homeomorph_of_strict_mono_continuous (f : α → β) (h_mono : strict_mono f) (h_cont : continuous f) (h_top : tendsto f at_top at_top) (h_bot : tendsto f at_bot at_bot) : (homeomorph_of_strict_mono_continuous f h_mono h_cont h_top h_bot : α → β) = f := rfl /- Now we prove a relative version of the above result. This (`Ioo` to `univ`) is provided as a sample; there are at least 16 possible variations with open intervals (`univ` to `Ioo`, `Ioi` to `univ`, ...), not to mention the possibilities with closed or half-closed intervals. -/ variables {a b : α} /-- If `f : α → β` is strictly monotone and continuous on the interval `Ioo a b` of `α`, and tends to `at_top` within `𝓝[Iio b] b` and to `at_bot` within `𝓝[Ioi a] a`, then it restricts to a homeomorphism from `Ioo a b` to `β`. -/ noncomputable def homeomorph_of_strict_mono_continuous_Ioo (f : α → β) (h : a < b) (h_mono : ∀ ⦃x y : α⦄, a < x → y < b → x < y → f x < f y) (h_cont : continuous_on f (Ioo a b)) (h_top : tendsto f (𝓝[Iio b] b) at_top) (h_bot : tendsto f (𝓝[Ioi a] a) at_bot) : homeomorph (Ioo a b) β := by haveI : inhabited (Ioo a b) := inhabited_of_nonempty (nonempty_Ioo_subtype h); exact homeomorph_of_strict_mono_continuous (restrict f (Ioo a b)) (λ x y, h_mono x.2.1 y.2.2) (continuous_on_iff_continuous_restrict.mp h_cont) (by rwa [restrict_eq f (Ioo a b), ← tendsto_map'_iff, map_coe_Ioo_at_top h]) (by rwa [restrict_eq f (Ioo a b), ← tendsto_map'_iff, map_coe_Ioo_at_bot h]) @[simp] lemma coe_homeomorph_of_strict_mono_continuous_Ioo (f : α → β) (h : a < b) (h_mono : ∀ ⦃x y : α⦄, a < x → y < b → x < y → f x < f y) (h_cont : continuous_on f (Ioo a b)) (h_top : tendsto f (𝓝[Iio b] b) at_top) (h_bot : tendsto f (𝓝[Ioi a] a) at_bot) : (homeomorph_of_strict_mono_continuous_Ioo f h h_mono h_cont h_top h_bot : Ioo a b → β) = restrict f (Ioo a b) := rfl end conditionally_complete_linear_order
77970360f3b64502f80e5f9b878b2083127118b8
9338c56dfd6ceacc3e5e63e32a7918cfec5d5c69
/src/Kenny/locally_ringed_space_on_opens.lean
cd64c3f1c2340e440b82f567588cd404803c17e9
[]
no_license
Project-Reykjavik/lean-scheme
7322eefce504898ba33737970be89dc751108e2b
6d3ec18fecfd174b79d0ce5c85a783f326dd50f6
refs/heads/master
1,669,426,172,632
1,578,284,588,000
1,578,284,588,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,955
lean
import Kenny.sheaf_of_rings_on_opens universes v w u₁ v₁ u open topological_space lattice structure locally_ringed_space_on_opens (X : Type u) [topological_space X] (U : opens X) : Type (max u (v+1)) := (O : sheaf_of_rings_on_opens.{v} X U) (Hstalks : ∀ x ∈ U, is_local_ring (O.to_sheaf_on_opens.stalk x H)) namespace locally_ringed_space_on_opens variables {X : Type u} [topological_space X] {U : opens X} def res_subset (OX : locally_ringed_space_on_opens X U) (V : opens X) (HVU : V ≤ U) : locally_ringed_space_on_opens X V := { O := OX.O, Hstalks := λ x hv, OX.Hstalks x (HVU hv) } theorem res_res_subset (OX : locally_ringed_space_on_opens X U) (V HVU S HSV T HTV HTS x) : (OX.res_subset V HVU).1.to_sheaf_on_opens.res S HSV T HTV HTS x = OX.1.to_sheaf_on_opens.res S (le_trans HSV HVU) T (le_trans HTV HVU) HTS x := rfl variables {Y : Type u₁} [topological_space Y] {V : opens Y} structure morphism (F : locally_ringed_space_on_opens.{v} X U) (G : locally_ringed_space_on_opens.{v₁} Y V) : Type (max u v u₁ v₁) := (f : X → Y) (hf : continuous f) (hf2 : opens.comap hf V ≤ U) (map : ∀ W ≤ V, F.1.to_sheaf_on_opens.eval (opens.comap hf W) (le_trans (opens.comap_mono _ _ _ H) hf2) → G.1.to_sheaf_on_opens.eval W H) [hom : ∀ W H, is_ring_hom (map W H)] (hlocal : ∀ W H s, is_unit (map W H s) → is_unit s) #exit namespace morphism section variables {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} variables (η : F.morphism G) (V : opens X) (HVU : V ≤ U) (x y : F.eval V HVU) (n : ℕ) @[simp] lemma map_add : η.map V HVU (x + y) = η.map V HVU x + η.map V HVU y := is_ring_hom.map_add _ @[simp] lemma map_zero : η.map V HVU 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma map_neg : η.map V HVU (-x) = -η.map V HVU x := is_ring_hom.map_neg _ @[simp] lemma map_sub : η.map V HVU (x - y) = η.map V HVU x - η.map V HVU y := is_ring_hom.map_sub _ @[simp] lemma map_mul : η.map V HVU (x * y) = η.map V HVU x * η.map V HVU y := is_ring_hom.map_mul _ @[simp] lemma map_one : η.map V HVU 1 = 1 := is_ring_hom.map_one _ @[simp] lemma map_pow : η.map V HVU (x^n) = (η.map V HVU x)^n := is_semiring_hom.map_pow _ x n end def to_sheaf_on_opens {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) : F.to_sheaf_on_opens.morphism G.to_sheaf_on_opens := { .. η } protected def id (F : locally_ringed_space_on_opens.{v} X U) : F.morphism F := { map := λ V HV, id, commutes := λ V HV W HW HWV x, rfl } def comp {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} {H : locally_ringed_space_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) : F.morphism H := { map := λ V HV x, η.map V HV (ξ.map V HV x), commutes := λ V HV W HW HWV x, by rw [ξ.commutes, η.commutes] } @[simp] lemma comp_apply {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} {H : locally_ringed_space_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) (V HV s) : (η.comp ξ).1 V HV s = η.1 V HV (ξ.1 V HV s) := rfl @[extensionality] lemma ext {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} {η ξ : F.morphism G} (H : ∀ V HV x, η.map V HV x = ξ.map V HV x) : η = ξ := by cases η; cases ξ; congr; ext; apply H @[simp] lemma id_comp {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) : (morphism.id G).comp η = η := ext $ λ V HV x, rfl @[simp] lemma comp_id {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) : η.comp (morphism.id F) = η := ext $ λ V HV x, rfl @[simp] lemma comp_assoc {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} {H : locally_ringed_space_on_opens.{u₁} X U} {I : locally_ringed_space_on_opens.{v₁} X U} (η : H.morphism I) (ξ : G.morphism H) (χ : F.morphism G) : (η.comp ξ).comp χ = η.comp (ξ.comp χ) := rfl def res_subset {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (V : opens X) (HVU : V ≤ U) : (F.res_subset V HVU).morphism (G.res_subset V HVU) := { map := λ W HWV, η.map W (le_trans HWV HVU), commutes := λ S HSV T HTV, η.commutes S (le_trans HSV HVU) T (le_trans HTV HVU) } @[simp] lemma res_subset_apply {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (V : opens X) (HVU : V ≤ U) (W HWV s) : (η.res_subset V HVU).1 W HWV s = η.1 W (le_trans HWV HVU) s := rfl @[simp] lemma comp_res_subset {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} {H : locally_ringed_space_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) (V : opens X) (HVU : V ≤ U) : (η.res_subset V HVU).comp (ξ.res_subset V HVU) = (η.comp ξ).res_subset V HVU := rfl @[simp] lemma id_res_subset {F : locally_ringed_space_on_opens.{v} X U} (V : opens X) (HVU : V ≤ U) : (morphism.id F).res_subset V HVU = morphism.id (F.res_subset V HVU) := rfl def stalk {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) (s : F.stalk x hx) : G.stalk x hx := quotient.lift_on s (λ g, ⟦(⟨g.1 ⊓ U, (⟨g.2, hx⟩ : x ∈ g.1 ⊓ U), η.map _ inf_le_right (presheaf.res F.1.1 _ _ (set.inter_subset_left _ _) g.3)⟩ : stalk.elem _ _)⟧) $ λ g₁ g₂ ⟨V, hxV, HV1, HV2, hg⟩, quotient.sound ⟨V ⊓ U, ⟨hxV, hx⟩, set.inter_subset_inter_left _ HV1, set.inter_subset_inter_left _ HV2, calc G.res _ _ (V ⊓ U) inf_le_right (inf_le_inf HV1 (le_refl _)) (η.map (g₁.U ⊓ U) inf_le_right ((F.F).res (g₁.U) (g₁.U ⊓ U) (set.inter_subset_left _ _) (g₁.s))) = η.map (V ⊓ U) inf_le_right ((F.F).res V (V ⊓ U) (set.inter_subset_left _ _) ((F.F).res (g₁.U) V HV1 (g₁.s))) : by rw ← η.3; dsimp only [res, sheaf_on_opens.res, locally_ringed_space_on_opens.to_sheaf_on_opens]; rw [← presheaf.Hcomp', ← presheaf.Hcomp'] ... = G.res _ _ (V ⊓ U) _ _ (η.map (g₂.U ⊓ U) inf_le_right ((F.F).res (g₂.U) (g₂.U ⊓ U) _ (g₂.s))) : by rw [hg, ← η.3]; dsimp only [res, sheaf_on_opens.res, locally_ringed_space_on_opens.to_sheaf_on_opens]; rw [← presheaf.Hcomp', ← presheaf.Hcomp']⟩ @[simp] lemma stalk_to_stalk {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) (V : opens X) (HVU : V ≤ U) (hxV : x ∈ V) (s : F.eval V HVU) : η.stalk x hx (F.to_stalk x hx V hxV HVU s) = G.to_stalk x hx V hxV HVU (η.map V HVU s) := quotient.sound ⟨V, hxV, set.subset_inter (set.subset.refl _) HVU, set.subset.refl _, calc G.res (V ⊓ U) inf_le_right V HVU (le_inf (le_refl V) HVU) (η.map (V ⊓ U) inf_le_right (F.res V HVU (V ⊓ U) inf_le_right inf_le_left s)) = G.res V HVU V HVU (le_refl V) (η.map V HVU s) : by rw [η.3, res_res]⟩ instance is_ring_hom_stalk {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) : is_ring_hom (η.stalk x hx) := { map_one := quotient.sound ⟨U, hx, set.subset_inter (set.subset_univ U.1) (set.subset.refl U.1), set.subset_univ U.1, by dsimp only; erw [_root_.res_one, η.map_one, _root_.res_one, _root_.res_one]⟩, map_mul := λ y z, stalk.induction_on₂ y z $ λ V hxV HVU s t, by rw [stalk_to_stalk, stalk_to_stalk, ← to_stalk_mul, stalk_to_stalk, η.map_mul, to_stalk_mul], map_add := λ y z, stalk.induction_on₂ y z $ λ V hxV HVU s t, by rw [stalk_to_stalk, stalk_to_stalk, ← to_stalk_add, stalk_to_stalk, η.map_add, to_stalk_add] } section variables {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) variables (s t : F.stalk x hx) (n : ℕ) @[simp] lemma stalk_add : η.stalk x hx (s + t) = η.stalk x hx s + η.stalk x hx t := is_ring_hom.map_add _ @[simp] lemma stalk_zero : η.stalk x hx 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma stalk_neg : η.stalk x hx (-s) = -η.stalk x hx s := is_ring_hom.map_neg _ @[simp] lemma stalk_sub : η.stalk x hx (s - t) = η.stalk x hx s - η.stalk x hx t := is_ring_hom.map_sub _ @[simp] lemma stalk_mul : η.stalk x hx (s * t) = η.stalk x hx s * η.stalk x hx t := is_ring_hom.map_mul _ @[simp] lemma stalk_one : η.stalk x hx 1 = 1 := is_ring_hom.map_one _ @[simp] lemma stalk_pow : η.stalk x hx (s^n) = (η.stalk x hx s)^n := is_semiring_hom.map_pow _ s n end end morphism structure equiv (F : locally_ringed_space_on_opens.{v} X U) (G : locally_ringed_space_on_opens.{w} X U) : Type (max u v w) := (to_fun : F.morphism G) (inv_fun : G.to_sheaf_on_opens.morphism F.to_sheaf_on_opens) (left_inv : ∀ V HVU s, inv_fun.1 V HVU (to_fun.1 V HVU s) = s) (right_inv : ∀ V HVU s, to_fun.1 V HVU (inv_fun.1 V HVU s) = s) namespace equiv def to_sheaf_on_opens {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (e : F.equiv G) : F.to_sheaf_on_opens.equiv G.to_sheaf_on_opens := { to_fun := e.1.to_sheaf_on_opens, .. e } def refl (F : locally_ringed_space_on_opens.{v} X U) : equiv F F := ⟨morphism.id F, sheaf_on_opens.morphism.id F.to_sheaf_on_opens, λ _ _ _, rfl, λ _ _ _, rfl⟩ @[simp] lemma refl_apply (F : locally_ringed_space_on_opens.{v} X U) (V HV s) : (refl F).1.1 V HV s = s := rfl def symm {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{v} X U} (e : equiv F G) : equiv G F := ⟨{ hom := λ V HVU, (ring_equiv.symm { to_fun := e.1.1 V HVU, inv_fun := e.2.1 V HVU, left_inv := e.3 V HVU, right_inv := e.4 V HVU, hom := e.1.2 V HVU }).hom, .. e.2 }, e.1.to_sheaf_on_opens, e.4, e.3⟩ def trans {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{v} X U} {H : locally_ringed_space_on_opens.{u₁} X U} (e₁ : equiv F G) (e₂ : equiv G H) : equiv F H := ⟨e₂.1.comp e₁.1, e₁.2.comp e₂.2, λ _ _ _, by rw [morphism.comp_apply, sheaf_on_opens.morphism.comp_apply, e₂.3, e₁.3], λ _ _ _, by rw [morphism.comp_apply, sheaf_on_opens.morphism.comp_apply, e₁.4, e₂.4]⟩ @[simp] lemma trans_apply {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{v} X U} {H : locally_ringed_space_on_opens.{u₁} X U} (e₁ : equiv F G) (e₂ : equiv G H) (V HV s) : (e₁.trans e₂).1.1 V HV s = e₂.1.1 V HV (e₁.1.1 V HV s) := rfl def res_subset {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (e : equiv F G) (V : opens X) (HVU : V ≤ U) : equiv (F.res_subset V HVU) (G.res_subset V HVU) := ⟨e.1.res_subset V HVU, e.2.res_subset V HVU, λ _ _ _, by rw [morphism.res_subset_apply, sheaf_on_opens.morphism.res_subset_apply, e.3], λ _ _ _, by rw [morphism.res_subset_apply, sheaf_on_opens.morphism.res_subset_apply, e.4]⟩ @[simp] lemma res_subset_apply {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (e : equiv F G) (V : opens X) (HVU : V ≤ U) (W HW s) : (e.res_subset V HVU).1.1 W HW s = e.1.1 W (le_trans HW HVU) s := rfl def stalk {F : locally_ringed_space_on_opens.{v} X U} {G : locally_ringed_space_on_opens.{w} X U} (e : equiv F G) (x : X) (hx : x ∈ U) : F.stalk x hx ≃ G.stalk x hx := { to_fun := e.1.stalk x hx, inv_fun := e.2.stalk x hx, left_inv := λ g, stalk.induction_on g $ λ V hxV HVU s, by rw [morphism.stalk_to_stalk, to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, e.3]; refl, right_inv := λ g, stalk.induction_on g $ λ V hxV HVU s, by rw [to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, ← to_stalk, morphism.stalk_to_stalk, e.4]; refl } end equiv def sheaf_glue {I : Type u} (S : I → opens X) (F : Π (i : I), locally_ringed_space_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) : locally_ringed_space_on_opens.{max u v} X (⋃S) := { F := { Fring := λ U, @subtype.comm_ring (Π (i : I), (F i).eval (S i ⊓ U) inf_le_left) _ { f | ∀ (i j : I), (φ i j).1.map (S i ⊓ S j ⊓ U) inf_le_left ((F i).res (S i ⊓ U) inf_le_left (S i ⊓ S j ⊓ U) (le_trans inf_le_left inf_le_left) (le_inf (le_trans inf_le_left inf_le_left) inf_le_right) (f i)) = (F j).res (S j ⊓ U) inf_le_left (S i ⊓ S j ⊓ U) (le_trans inf_le_left inf_le_right) (by rw inf_assoc; exact inf_le_right) (f j) } { add_mem := λ f g hf hg i j, by erw [res_add, morphism.map_add, res_add, hf i j, hg i j], zero_mem := λ i j, by erw [res_zero, morphism.map_zero, res_zero]; refl, neg_mem := λ f hf i j, by erw [res_neg, morphism.map_neg, res_neg, hf i j], one_mem := λ i j, by erw [res_one, morphism.map_one, res_one]; refl, mul_mem := λ f g hf hg i j, by erw [res_mul, morphism.map_mul, res_mul, hf i j, hg i j] }, res_is_ring_hom := λ U V HVU, { map_one := subtype.eq $ funext $ λ i, res_one _ _ _ _ _ _, map_mul := λ f g, subtype.eq $ funext $ λ i, res_mul _ _ _ _ _ _ _ _, map_add := λ f g, subtype.eq $ funext $ λ i, res_add _ _ _ _ _ _ _ _ }, .. (sheaf_on_opens.sheaf_glue S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens)).F } .. sheaf_on_opens.sheaf_glue S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) } @[simp] lemma sheaf_glue_res_val {I : Type u} (S : I → opens X) (F : Π (i : I), locally_ringed_space_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) (U HU V HV HVU s i) : ((sheaf_glue S F φ).res U HU V HV HVU s).1 i = (F i).res _ _ _ _ (inf_le_inf (le_refl _) HVU) (s.1 i) := rfl def universal_property (I : Type u) (S : I → opens X) (F : Π (i : I), locally_ringed_space_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) (Hφ1 : ∀ i V HV s, (φ i i).1.1 V HV s = s) (Hφ2 : ∀ i j k V HV1 HV2 HV3 s, (φ j k).1.1 V HV1 ((φ i j).1.1 V HV2 s) = (φ i k).1.1 V HV3 s) (i : I) : equiv (res_subset (sheaf_glue S F φ) (S i) (le_supr S i)) (F i) := { to_fun := { hom := λ U HU, { map_one := res_one _ _ _ _ _ _, map_mul := λ x y, res_mul _ _ _ _ _ _ _ _, map_add := λ x y, res_add _ _ _ _ _ _ _ _ }, .. (sheaf_on_opens.universal_property I S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) Hφ1 Hφ2 i).1 }, .. sheaf_on_opens.universal_property I S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) Hφ1 Hφ2 i } end locally_ringed_space_on_opens
6eb609904dc998d67e0236fd102908aa42a609b7
9e90bb7eb4d1bde1805f9eb6187c333fdf09588a
/src/lib/util.lean
9fbf6d209c33cf276fe41dbe41aeab6b29a6cf40
[ "Apache-2.0" ]
permissive
alexjbest/stump-learnable
6311d0c3a1a1a0e65ce83edcbb3b4b7cecabb851
f8fd812fc646d2ece312ff6ffc2a19848ac76032
refs/heads/master
1,659,486,805,691
1,590,454,024,000
1,590,454,024,000
266,173,720
0
0
Apache-2.0
1,590,169,884,000
1,590,169,883,000
null
UTF-8
Lean
false
false
9,196
lean
/- Copyright © 2019, Oracle and/or its affiliates. All rights reserved. -/ import data.set import analysis.special_functions.exp_log import .attributed.dvector import lib.basic import topology.constructions local attribute [instance] classical.prop_decidable open nnreal real ennreal set lattice open topological_space open measure_theory lemma lc_nnreal: ∀ a: nnreal, ∀ b: nnreal, a + b = 1 → a = (1:nnreal) - b := assume a b h, h ▸ (nnreal.add_sub_cancel).symm lemma super_dumb: ∀ δ: nnreal, δ > (0:nnreal) → (1:nnreal) - δ ≤ (1: nnreal) := assume h, by simp lemma coe_pmeas: ∀ a: ennreal, ∀ b: ennreal, a ≤ (1:ennreal) → b ≤ (1: ennreal) → a ≤ b → ennreal.to_nnreal a ≤ ennreal.to_nnreal b := begin intros a b h₁ h₂ h₃, rw [← coe_le_coe, coe_to_nnreal,coe_to_nnreal], assumption, rw ←ennreal.lt_top_iff_ne_top, exact lt_of_le_of_lt h₂ (ennreal.lt_top_iff_ne_top.2 one_ne_top), rw ←ennreal.lt_top_iff_ne_top, exact lt_of_le_of_lt h₁ (ennreal.lt_top_iff_ne_top.2 one_ne_top), end lemma pow_preserves_order: ∀ n: ℕ, ∀ a: nnreal, ∀ b: nnreal, a ≤ b → a^n ≤ b^n := begin intros n a b h, induction n with k ih, {by simp}, {simp [pow_succ], exact mul_le_mul h ih (zero_le _) (zero_le _)}, end lemma not_eq_prop: ∀ A: Prop, ∀ B: Prop, (¬ (A ↔ B)) ↔ ((¬ (A → B)) ∨ (¬ (B → A))) := begin intros, split; intros, finish, finish, end lemma nnreal_sub_trans: ∀ a: nnreal, ∀ b: nnreal, b ≤ a → 1 - a ≤ 1 - b := begin intros, rw nnreal.sub_le_iff_le_add, rw add_comm, by_cases (b ≤ 1), { have h: 1 + (a - b) = a + (1 - b), { calc 1 + (a - b) = 1 + a - b : by rw [←nnreal.eq_iff,nnreal.coe_add, (nnreal.coe_sub a_1), ←add_sub_assoc, nnreal.coe_sub _, nnreal.coe_add]; exact le_add_of_le_of_nonneg h (zero_le _) ... = a + 1 - b : by rw add_comm ... = a + (1 - b) : by rw [←nnreal.eq_iff,nnreal.coe_add, (nnreal.coe_sub h), ←add_sub_assoc,nnreal.coe_sub _, nnreal.coe_add];exact le_add_of_le_of_nonneg a_1 (zero_le _), }, rw h.symm, cases b, cases a, dsimp at *, simp at *, }, { simp at h, rw nnreal.sub_eq_zero, swap, exact le_of_lt h, simp, transitivity b, exact le_of_lt h, assumption, }, end lemma prod_rw {α: Type} {β: Type}: ∀ P₁: α → Prop, ∀ P₂: β → Prop, { v : α × β | P₁ v.fst ∧ P₂ v.snd} = set.prod {x: α | P₁ x} {x: β | P₂ x} := begin intros, unfold set.prod, rw ext_iff, intro, repeat {rw mem_set_of_eq}, end lemma dfin_1_projn {α: Type}: ∀ P: α → Prop, ∀ x: vec α 0, (∀ (i: dfin 1), P (kth_projn x i)) ↔ P x := begin intros, split; intros, { simp at *, apply a, }, { simp at *, assumption, }, end lemma dfin_1_projn' {α: Type}: ∀ P: α → Prop, { x: vec α 0 | ∀ (i: dfin 1), P (kth_projn x i)} = {x: α | P x} := begin intros, rw ext_iff, intro, repeat {rw mem_set_of_eq}, rw dfin_1_projn, trivial, end lemma is_measurable_simple_vec {α: Type} [measurable_space α]: ∀ P: α → Prop, is_measurable {x: α | P x} → ∀ n, is_measurable {v: vec α n | ∀ (i: dfin (nat.succ n)), P(kth_projn v i)} := begin intros, induction n; intros, { rw dfin_1_projn', assumption, }, { dunfold vec, conv { congr, congr, funext, rw dfin_succ_prop_iff_fst_and_rst, skip, }, have PROD := prod_rw (λ x, P x) (λ x, ∀ (i: dfin (nat.succ n_n)), P(kth_projn x i)), simp at PROD, rw PROD, clear PROD, apply is_measurable.prod; try {assumption}, }, end /- Move these results back to where vec was defined (../to_mathlib.lean)-/ noncomputable instance vec_topo: ∀ n: ℕ, topological_space (vec nnreal n) := begin intro, induction n, { dunfold vec, apply_instance, }, { unfold vec, have PROD := @prod.topological_space nnreal (vec nnreal n_n) _ n_ih, assumption, }, end instance vec_second_countable : ∀ n:ℕ, second_countable_topology (vec nnreal n) := begin intros n, induction n with k ih, dsimp [vec], apply_instance, dsimp [vec], haveI := second_countable_topology nnreal, apply_instance, end lemma vec.measurable_space_eq_borel (n : ℕ) : (vec.measurable_space n : measurable_space (vec nnreal n)) = borel (vec nnreal n) := begin induction n with k ih, refl, dsimp [vec], sorry;{rw ←borel.prod, rw prod.measurable_space, rw ←ih, refl,}, end lemma test''' : ∀ n: ℕ, ∀ f: nnreal × vec nnreal n → nnreal, ∀ g: nnreal × vec nnreal n → nnreal, continuous f → continuous g → is_measurable {p: nnreal × (vec nnreal n) | f p < g p} := begin intros n f g hf hg, convert is_open.is_measurable _, haveI := vec_second_countable n, change topological_space (vec nnreal (nat.succ n)), apply_instance, swap, exact is_open_lt hf hg,convert prod.opens_measurable_space, apply_instance, { refine {borel_le := _}, sorry, }, apply_instance, apply_instance, end lemma to_nnreal_sub {r₁ r₂ : ennreal} (h₁ : r₁ < ⊤) (h₂ : r₂ < ⊤) : (r₁ - r₂).to_nnreal = r₁.to_nnreal - r₂.to_nnreal := by rw [← ennreal.coe_eq_coe, ennreal.coe_sub, ennreal.coe_to_nnreal (ne_top_of_lt h₂), ennreal.coe_to_nnreal (ne_top_of_lt h₁), ennreal.coe_to_nnreal ((lt_top_iff_ne_top.1 (lt_of_le_of_lt (sub_le_self _ _) h₁)))] lemma Ioi_complement: ∀ x: nnreal, Ioi x = - (Iio x ∪ {x}) := begin intros, rw ext_iff, intro, unfold Ioi, unfold Iio, simp, repeat {rw mem_set_of_eq}, split; intro, { push_neg, split, { by_contradiction, simp at *, rw a_1 at a, have FOO: ¬ (x < x), simp, contradiction, }, { exact le_of_lt a, }, }, { push_neg at a, cases a, by_contradiction, have FOO: ¬ (x ≤ x_1), { simp, simp at a, exact lt_of_le_of_ne a a_left, }, contradiction, }, end lemma Icc_diff_Ioc: ∀ a: nnreal, ∀ b: nnreal, ∀ c: nnreal, a ≤ b → b ≤ c → (Icc a c \ Icc a b) = Ioc b c := begin unfold Icc, unfold Ioc, introv H1 H2, rw ext_iff, intro, rw mem_diff, repeat {rw mem_set_of_eq at *,}, split; intros, { cases a_1, cases a_1_left, finish, }, { cases a_1, split, { split, { transitivity b, assumption, exact le_of_lt a_1_left, }, { assumption, }, }, { simp, intro, assumption, }, }, end lemma mono_simple {μ: probability_measure nnreal}: ∀ a: nnreal, ∀ b: nnreal, a ≤ b → μ (Icc 0 a) ≤ μ (Icc 0 b) := begin intros, apply probability_measure.prob_mono, unfold Icc, rw subset_def, intros, rw mem_set_of_eq at *, cases a_2, split, assumption, transitivity a; assumption, end lemma log_le_log_nnreal: ∀ x: nnreal, ∀ y: nnreal, x > 0 → y > 0 → (x ≤ y ↔ log x ≤ log y) := begin intros, rw log_le_log, refl, assumption, assumption, end lemma log_pow_nnreal: ∀ x: nnreal, x > 0 → ∀ n: ℕ, log(x^n) = n * log(x) := begin intros, apply exp_injective, rw exp_nat_mul, rw exp_log, rw exp_log, assumption, exact pow_pos a n, end lemma pow_coe: ∀ a: nnreal, ∀ n: ℕ, a.val ^ n = (a ^ n).val := begin intros, induction n, simp, have pow_nnreal: ∀ a: nnreal, ∀ n: ℕ, a ^ (nat.succ n) = a * a ^ n, intros, exact rfl, rw pow_nnreal, have mul_coe: ∀ a: nnreal, ∀ b: nnreal, (a * b).val = a.val * b.val, intros, refl, rw mul_coe, rw ← n_ih, refl, end lemma ite_equals_union_interval: ∀ θ > 0, ∀ y: nnreal, {x: nnreal | ite (to_bool(x ≤ y)) x 0 < θ} = Ico 0 θ ∪ Ioi y := begin intros, unfold Ico, unfold Ioi, rw ext_iff, intro, rw mem_union, repeat {rw mem_set_of_eq}, split; intro, { split_ifs at a, { left, split, tidy, }, { right, tidy, }, }, { cases a, { cases a, split_ifs; assumption, }, { split_ifs, { by_contradiction, have GEQ: ¬ (y < x), { simp, simp at h, assumption, }, contradiction, }, { assumption, }, }, }, end
74aa9897b122e4e120db0607093d10d62a46dc04
8e691ffe296a38e3e9dc95845e5b607422c3c973
/src/topology/metric_space/basic.lean
7de964bd34748d0985b38e9d2d98e5fbe8c66fff
[ "Apache-2.0" ]
permissive
jmvlangen/mathlib
8d847f7831f0e8ddebc99568704530af7c9e1a50
82f79a58df3047b8002bbaf90ce50da2a978e114
refs/heads/master
1,588,094,154,678
1,552,412,266,000
1,552,412,266,000
175,439,673
0
0
null
null
null
null
UTF-8
Lean
false
false
56,105
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Metric spaces. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity -/ import data.real.nnreal topology.metric_space.emetric_space topology.algebra.ordered open lattice set filter classical topological_space noncomputable theory local notation `𝓤` := uniformity universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- Construct a uniform structure from a distance function and metric space axioms -/ def uniform_space_of_dist (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space α := uniform_space.of_core { uniformity := (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}), refl := le_infi $ assume ε, le_infi $ by simp [set.subset_def, id_rel, dist_self, (>)] {contextual := tt}, comp := le_infi $ assume ε, le_infi $ assume h, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos_of_pos_of_pos h two_pos) (subset.refl _)) $ have ∀ (a b c : α), dist a c < ε / 2 → dist c b < ε / 2 → dist a b < ε, from assume a b c hac hcb, calc dist a b ≤ dist a c + dist c b : dist_triangle _ _ _ ... < ε / 2 + ε / 2 : add_lt_add hac hcb ... = ε : by rw [div_add_div_same, add_self_div_two], by simpa [comp_rel], symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [dist_comm] } /-- The distance function (given an ambient metric space on `α`), which returns a nonnegative real number `dist x y` given `x y : α`. -/ class has_dist (α : Type*) := (dist : α → α → ℝ) export has_dist (dist) /-- Metric space Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be filled in by default. In the same way, each metric space induces an emetric space structure. It is included in the structure, but filled in by default. When one instantiates a metric space structure, for instance a product structure, this makes it possible to use a uniform structure and an edistance that are exactly the ones for the uniform spaces product and the emetric spaces products, thereby ensuring that everything in defeq in diamonds.-/ class metric_space (α : Type u) extends has_dist α : Type u := (dist_self : ∀ x : α, dist x x = 0) (eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (edist : α → α → ennreal := λx y, ennreal.of_real (dist x y)) (edist_dist : ∀ x y : α, edist x y = ennreal.of_real (dist x y) . control_laws_tac) (to_uniform_space : uniform_space α := uniform_space_of_dist dist dist_self dist_comm dist_triangle) (uniformity_dist : 𝓤 α = ⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε} . control_laws_tac) variables [metric_space α] instance metric_space.to_uniform_space' : uniform_space α := metric_space.to_uniform_space α instance metric_space.to_has_edist : has_edist α := ⟨metric_space.edist⟩ @[simp] theorem dist_self (x : α) : dist x x = 0 := metric_space.dist_self x theorem eq_of_dist_eq_zero {x y : α} : dist x y = 0 → x = y := metric_space.eq_of_dist_eq_zero theorem dist_comm (x y : α) : dist x y = dist y x := metric_space.dist_comm x y theorem edist_dist (x y : α) : edist x y = ennreal.of_real (dist x y) := metric_space.edist_dist _ x y @[simp] theorem dist_eq_zero {x y : α} : dist x y = 0 ↔ x = y := iff.intro eq_of_dist_eq_zero (assume : x = y, this ▸ dist_self _) @[simp] theorem zero_eq_dist {x y : α} : 0 = dist x y ↔ x = y := by rw [eq_comm, dist_eq_zero] theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z := metric_space.dist_triangle x y z theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y := by rw dist_comm z; apply dist_triangle theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z := by rw dist_comm y; apply dist_triangle lemma dist_triangle4 (x y z w : α) : dist x w ≤ dist x y + dist y z + dist z w := calc dist x w ≤ dist x z + dist z w : dist_triangle x z w ... ≤ (dist x y + dist y z) + dist z w : add_le_add_right (metric_space.dist_triangle x y z) _ lemma dist_triangle4_left (x₁ y₁ x₂ y₂ : α) : dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) := by rw [add_left_comm, dist_comm x₁, ← add_assoc]; apply dist_triangle4 lemma dist_triangle4_right (x₁ y₁ x₂ y₂ : α) : dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ := by rw [add_right_comm, dist_comm y₁]; apply dist_triangle4 theorem swap_dist : function.swap (@dist α _) = dist := by funext x y; exact dist_comm _ _ theorem abs_dist_sub_le (x y z : α) : abs (dist x z - dist y z) ≤ dist x y := abs_sub_le_iff.2 ⟨sub_le_iff_le_add.2 (dist_triangle _ _ _), sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩ theorem dist_nonneg {x y : α} : 0 ≤ dist x y := have 2 * dist x y ≥ 0, from calc 2 * dist x y = dist x y + dist y x : by rw [dist_comm x y, two_mul] ... ≥ 0 : by rw ← dist_self x; apply dist_triangle, nonneg_of_mul_nonneg_left this two_pos @[simp] theorem dist_le_zero {x y : α} : dist x y ≤ 0 ↔ x = y := by simpa [le_antisymm_iff, dist_nonneg] using @dist_eq_zero _ _ x y @[simp] theorem dist_pos {x y : α} : 0 < dist x y ↔ x ≠ y := by simpa [-dist_le_zero] using not_congr (@dist_le_zero _ _ x y) @[simp] theorem abs_dist {a b : α} : abs (dist a b) = dist a b := abs_of_nonneg dist_nonneg theorem eq_of_forall_dist_le {x y : α} (h : ∀ε, ε > 0 → dist x y ≤ ε) : x = y := eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h) def nndist (a b : α) : nnreal := ⟨dist a b, dist_nonneg⟩ /--Express `nndist` in terms of `edist`-/ lemma nndist_edist (x y : α) : nndist x y = (edist x y).to_nnreal := by simp [nndist, edist_dist, nnreal.of_real, max_eq_left dist_nonneg, ennreal.of_real] /--Express `edist` in terms of `nndist`-/ lemma edist_nndist (x y : α) : edist x y = ↑(nndist x y) := by simp [nndist, edist_dist, nnreal.of_real, max_eq_left dist_nonneg, ennreal.of_real] /--In a metric space, the extended distance is always finite-/ lemma edist_ne_top (x y : α) : edist x y ≠ ⊤ := by rw [edist_dist x y]; apply ennreal.coe_ne_top /--`nndist x x` vanishes-/ @[simp] lemma nndist_self (a : α) : nndist a a = 0 := (nnreal.coe_eq_zero _).1 (dist_self a) /--Express `dist` in terms of `nndist`-/ lemma dist_nndist (x y : α) : dist x y = ↑(nndist x y) := rfl /--Express `nndist` in terms of `dist`-/ lemma nndist_dist (x y : α) : nndist x y = nnreal.of_real (dist x y) := by rw [dist_nndist, nnreal.of_real_coe] /--Deduce the equality of points with the vanishing of the nonnegative distance-/ theorem eq_of_nndist_eq_zero {x y : α} : nndist x y = 0 → x = y := by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero] theorem nndist_comm (x y : α) : nndist x y = nndist y x := by simpa [nnreal.eq_iff.symm] using dist_comm x y /--Characterize the equality of points with the vanishing of the nonnegative distance-/ @[simp] theorem nndist_eq_zero {x y : α} : nndist x y = 0 ↔ x = y := by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero] @[simp] theorem zero_eq_nndist {x y : α} : 0 = nndist x y ↔ x = y := by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, zero_eq_dist] /--Triangle inequality for the nonnegative distance-/ theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z := by simpa [nnreal.coe_le] using dist_triangle x y z theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y := by simpa [nnreal.coe_le] using dist_triangle_left x y z theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z := by simpa [nnreal.coe_le] using dist_triangle_right x y z /--Express `dist` in terms of `edist`-/ lemma dist_edist (x y : α) : dist x y = (edist x y).to_real := by rw [edist_dist, ennreal.to_real_of_real (dist_nonneg)] namespace metric /- instantiate metric space as a topology -/ variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α} /-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/ def ball (x : α) (ε : ℝ) : set α := {y | dist y x < ε} @[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw dist_comm; refl /-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/ def closed_ball (x : α) (ε : ℝ) := {y | dist y x ≤ ε} @[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ dist y x ≤ ε := iff.rfl theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε := assume y, by simp; intros h; apply le_of_lt h theorem pos_of_mem_ball (hy : y ∈ ball x ε) : ε > 0 := lt_of_le_of_lt dist_nonneg hy theorem mem_ball_self (h : ε > 0) : x ∈ ball x ε := show dist x x < ε, by rw dist_self; assumption theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by simp [dist_comm] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := λ y (yx : _ < ε₁), lt_of_lt_of_le yx h theorem closed_ball_subset_closed_ball {α : Type u} [metric_space α] {ε₁ ε₂ : ℝ} {x : α} (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ := λ y (yx : _ ≤ ε₁), le_trans yx h theorem ball_disjoint (h : ε₁ + ε₂ ≤ dist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ := eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩, not_lt_of_le (dist_triangle_left x y z) (lt_of_lt_of_le (add_lt_add h₁ h₂) h) theorem ball_disjoint_same (h : ε ≤ dist x y / 2) : ball x ε ∩ ball y ε = ∅ := ball_disjoint $ by rwa [← two_mul, ← le_div_iff' two_pos] theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ := λ z zx, by rw ← add_sub_cancel'_right ε₁ ε₂; exact lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h) theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε := ball_subset $ by rw sub_self_div_two; exact le_of_lt h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := ⟨_, sub_pos.2 h, ball_subset $ by rw sub_sub_self⟩ theorem ball_eq_empty_iff_nonpos : ε ≤ 0 ↔ ball x ε = ∅ := (eq_empty_iff_forall_not_mem.trans ⟨λ h, le_of_not_gt $ λ ε0, h _ $ mem_ball_self ε0, λ ε0 y h, not_lt_of_le ε0 $ pos_of_mem_ball h⟩).symm theorem uniformity_dist : 𝓤 α = (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}) := metric_space.uniformity_dist _ theorem uniformity_dist' : 𝓤 α = (⨅ε:{ε:ℝ // ε>0}, principal {p:α×α | dist p.1 p.2 < ε.val}) := by simp [infi_subtype]; exact uniformity_dist theorem mem_uniformity_dist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, dist a b < ε → (a, b) ∈ s) := begin rw [uniformity_dist', mem_infi], simp [subset_def], exact assume ⟨r, hr⟩ ⟨p, hp⟩, ⟨⟨min r p, lt_min hr hp⟩, by simp [lt_min_iff, (≥)] {contextual := tt}⟩, exact ⟨⟨1, zero_lt_one⟩⟩ end theorem dist_mem_uniformity {ε:ℝ} (ε0 : 0 < ε) : {p:α×α | dist p.1 p.2 < ε} ∈ 𝓤 α := mem_uniformity_dist.2 ⟨ε, ε0, λ a b, id⟩ theorem uniform_continuous_iff [metric_space β] {f : α → β} : uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b:α}, dist a b < δ → dist (f a) (f b) < ε := uniform_continuous_def.trans ⟨λ H ε ε0, mem_uniformity_dist.1 $ H _ $ dist_mem_uniformity ε0, λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru, ⟨δ, δ0, hδ⟩ := H _ ε0 in mem_uniformity_dist.2 ⟨δ, δ0, λ a b h, hε (hδ h)⟩⟩ theorem uniform_embedding_iff [metric_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (dist_mem_uniformity δ0), ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 tu in ⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩, λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in ⟨_, dist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩ theorem totally_bounded_iff {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, H _ (dist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru, ⟨t, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ /-- A metric space space is totally bounded if one can reconstruct up to any ε>0 any element of the space from finitely many data. -/ lemma totally_bounded_of_finite_discretization {α : Type u} [metric_space α] {s : set α} (H : ∀ε > (0 : ℝ), ∃ (β : Type u) [fintype β] (F : s → β), ∀x y, F x = F y → dist (x:α) y < ε) : totally_bounded s := begin classical, by_cases hs : s = ∅, { rw hs, exact totally_bounded_empty }, rcases exists_mem_of_ne_empty hs with ⟨x0, hx0⟩, haveI : inhabited s := ⟨⟨x0, hx0⟩⟩, refine totally_bounded_iff.2 (λ ε ε0, _), rcases H ε ε0 with ⟨β, fβ, F, hF⟩, let Finv := function.inv_fun F, refine ⟨range (subtype.val ∘ Finv), finite_range _, λ x xs, _⟩, let x' := Finv (F ⟨x, xs⟩), have : F x' = F ⟨x, xs⟩ := function.inv_fun_eq ⟨⟨x, xs⟩, rfl⟩, simp only [set.mem_Union, set.mem_range], exact ⟨_, ⟨F ⟨x, xs⟩, rfl⟩, hF _ _ this.symm⟩ end protected lemma cauchy_iff {f : filter α} : cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, dist x y < ε := cauchy_iff.trans $ and_congr iff.rfl ⟨λ H ε ε0, let ⟨t, tf, ts⟩ := H _ (dist_mem_uniformity ε0) in ⟨t, tf, λ x y xt yt, @ts (x, y) ⟨xt, yt⟩⟩, λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru, ⟨t, tf, h⟩ := H ε ε0 in ⟨t, tf, λ ⟨x, y⟩ ⟨hx, hy⟩, hε (h x y hx hy)⟩⟩ theorem nhds_eq : nhds x = (⨅ε:{ε:ℝ // ε>0}, principal (ball x ε.val)) := begin rw [nhds_eq_uniformity, uniformity_dist', lift'_infi], { apply congr_arg, funext ε, rw [lift'_principal], { simp [ball, dist_comm] }, { exact monotone_preimage } }, { exact ⟨⟨1, zero_lt_one⟩⟩ }, { intros, refl } end theorem mem_nhds_iff : s ∈ nhds x ↔ ∃ε>0, ball x ε ⊆ s := begin rw [nhds_eq, mem_infi], { simp }, { intros y z, cases y with y hy, cases z with z hz, refine ⟨⟨min y z, lt_min hy hz⟩, _⟩, simp [ball_subset_ball, min_le_left, min_le_right, (≥)] }, { exact ⟨⟨1, zero_lt_one⟩⟩ } end theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s := by simp [is_open_iff_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x ε) := is_open_iff.2 $ λ y, exists_ball_subset_ball theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ nhds x := mem_nhds_sets is_open_ball (mem_ball_self ε0) theorem tendsto_nhds_nhds [metric_space β] {f : α → β} {a b} : tendsto f (nhds a) (nhds b) ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) b < ε := ⟨λ H ε ε0, mem_nhds_iff.1 (H (ball_mem_nhds _ ε0)), λ H s hs, let ⟨ε, ε0, hε⟩ := mem_nhds_iff.1 hs, ⟨δ, δ0, hδ⟩ := H _ ε0 in mem_nhds_iff.2 ⟨δ, δ0, λ x h, hε (hδ h)⟩⟩ theorem continuous_iff [metric_space β] {f : α → β} : continuous f ↔ ∀b (ε > 0), ∃ δ > 0, ∀a, dist a b < δ → dist (f a) (f b) < ε := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_nhds theorem exists_delta_of_continuous [metric_space β] {f : α → β} {ε : ℝ} (hf : continuous f) (hε : ε > 0) (b : α) : ∃ δ > 0, ∀a, dist a b ≤ δ → dist (f a) (f b) < ε := let ⟨δ, δ_pos, hδ⟩ := continuous_iff.1 hf b ε hε in ⟨δ / 2, half_pos δ_pos, assume a ha, hδ a $ lt_of_le_of_lt ha $ div_two_lt_of_pos δ_pos⟩ theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} : tendsto u f (nhds a) ↔ ∀ ε > 0, ∃ n ∈ f, ∀x ∈ n, dist (u x) a < ε := by simp only [metric.nhds_eq, tendsto_infi, subtype.forall, tendsto_principal, mem_ball]; exact forall_congr (assume ε, forall_congr (assume hε, exists_sets_subset_iff.symm)) theorem continuous_iff' [topological_space β] {f : β → α} : continuous f ↔ ∀a (ε > 0), ∃ n ∈ nhds a, ∀b ∈ n, dist (f b) (f a) < ε := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} : tendsto u at_top (nhds a) ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) a < ε := by simp only [metric.nhds_eq, tendsto_infi, subtype.forall, tendsto_at_top_principal]; refl end metric open metric instance metric_space.to_separated : separated α := separated_def.2 $ λ x y h, eq_of_forall_dist_le $ λ ε ε0, le_of_lt (h _ (dist_mem_uniformity ε0)) /-Instantiate a metric space as an emetric space. Before we can state the instance, we need to show that the uniform structure coming from the edistance and the distance coincide. -/ /-- Expressing the uniformity in terms of `edist` -/ protected lemma metric.mem_uniformity_edist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) := begin refine mem_uniformity_dist.trans ⟨_, _⟩; rintro ⟨ε, ε0, Hε⟩, { refine ⟨ennreal.of_real ε, _, λ a b, _⟩, { rwa [gt, ennreal.of_real_pos] }, { rw [edist_dist, ennreal.of_real_lt_of_real_iff ε0], exact Hε } }, { rcases ennreal.lt_iff_exists_real_btwn.1 ε0 with ⟨ε', _, ε0', hε⟩, rw [ennreal.of_real_pos] at ε0', refine ⟨ε', ε0', λ a b h, Hε (lt_trans _ hε)⟩, rwa [edist_dist, ennreal.of_real_lt_of_real_iff ε0'] } end protected theorem metric.uniformity_edist' : 𝓤 α = (⨅ε:{ε:ennreal // ε>0}, principal {p:α×α | edist p.1 p.2 < ε.val}) := begin ext s, rw mem_infi, { simp [metric.mem_uniformity_edist, subset_def] }, { rintro ⟨r, hr⟩ ⟨p, hp⟩, use ⟨min r p, lt_min hr hp⟩, simp [lt_min_iff, (≥)] {contextual := tt} }, { exact ⟨⟨1, ennreal.zero_lt_one⟩⟩ } end theorem uniformity_edist : 𝓤 α = (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}) := by simpa [infi_subtype] using @metric.uniformity_edist' α _ /-- A metric space induces an emetric space -/ instance metric_space.to_emetric_space : emetric_space α := { edist := edist, edist_self := by simp [edist_dist], eq_of_edist_eq_zero := assume x y h, by simpa [edist_dist] using h, edist_comm := by simp only [edist_dist, dist_comm]; simp, edist_triangle := assume x y z, begin simp only [edist_dist, (ennreal.of_real_add _ _).symm, dist_nonneg], rw ennreal.of_real_le_of_real_iff _, { exact dist_triangle _ _ _ }, { simpa using add_le_add (dist_nonneg : 0 ≤ dist x y) dist_nonneg } end, uniformity_edist := uniformity_edist, ..‹metric_space α› } /-- Balls defined using the distance or the edistance coincide -/ lemma metric.emetric_ball {x : α} {ε : ℝ} : emetric.ball x (ennreal.of_real ε) = ball x ε := begin classical, by_cases h : 0 < ε, { ext y, by simp [edist_dist, ennreal.of_real_lt_of_real_iff h] }, { have h' : ε ≤ 0, by simpa using h, have A : ball x ε = ∅, by simpa [ball_eq_empty_iff_nonpos.symm], have B : emetric.ball x (ennreal.of_real ε) = ∅, by simp [ennreal.of_real_eq_zero.2 h', emetric.ball_eq_empty_iff], rwa [A, B] } end /-- Closed balls defined using the distance or the edistance coincide -/ lemma metric.emetric_closed_ball {x : α} {ε : ℝ} (h : 0 ≤ ε) : emetric.closed_ball x (ennreal.of_real ε) = closed_ball x ε := by ext y; simp [edist_dist]; rw ennreal.of_real_le_of_real_iff h def metric_space.replace_uniformity {α} [U : uniform_space α] (m : metric_space α) (H : @uniformity _ U = @uniformity _ (metric_space.to_uniform_space α)) : metric_space α := { dist := @dist _ m.to_has_dist, dist_self := dist_self, eq_of_dist_eq_zero := @eq_of_dist_eq_zero _ _, dist_comm := dist_comm, dist_triangle := dist_triangle, edist := edist, edist_dist := edist_dist, to_uniform_space := U, uniformity_dist := H.trans (metric_space.uniformity_dist α) } /-- One gets a metric space from an emetric space if the edistance is everywhere finite. We set it up so that the edist and the uniformity are defeq in the metric space and the emetric space -/ def emetric_space.to_metric_space {α : Type u} [e : emetric_space α] (h : ∀x y: α, edist x y ≠ ⊤) : metric_space α := let m : metric_space α := { dist := λx y, ennreal.to_real (edist x y), eq_of_dist_eq_zero := λx y hxy, by simpa [dist, ennreal.to_real_eq_zero_iff, h x y] using hxy, dist_self := λx, by simp, dist_comm := λx y, by simp [emetric_space.edist_comm], dist_triangle := λx y z, begin rw [← ennreal.to_real_add (h _ _) (h _ _), ennreal.to_real_le_to_real (h _ _)], { exact edist_triangle _ _ _ }, { simp [ennreal.add_eq_top, h] } end, edist := λx y, edist x y, edist_dist := λx y, by simp [ennreal.of_real_to_real, h] } in metric_space.replace_uniformity m (by rw [uniformity_edist, uniformity_edist']; refl) section real /-- Instantiate the reals as a metric space. -/ instance real.metric_space : metric_space ℝ := { dist := λx y, abs (x - y), dist_self := by simp [abs_zero], eq_of_dist_eq_zero := by simp [add_neg_eq_zero], dist_comm := assume x y, abs_sub _ _, dist_triangle := assume x y z, abs_sub_le _ _ _ } theorem real.dist_eq (x y : ℝ) : dist x y = abs (x - y) := rfl theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = abs x := by simp [real.dist_eq] instance : orderable_topology ℝ := orderable_topology_of_nhds_abs $ λ x, begin simp only [show ∀ r, {b : ℝ | abs (x - b) < r} = ball x r, by simp [-sub_eq_add_neg, abs_sub, ball, real.dist_eq]], apply le_antisymm, { simp [le_infi_iff], exact λ ε ε0, mem_nhds_sets (is_open_ball) (mem_ball_self ε0) }, { intros s h, rcases mem_nhds_iff.1 h with ⟨ε, ε0, ss⟩, exact mem_infi_sets _ (mem_infi_sets ε0 (mem_principal_sets.2 ss)) }, end lemma closed_ball_Icc {x r : ℝ} : closed_ball x r = Icc (x-r) (x+r) := by ext y; rw [mem_closed_ball, dist_comm, real.dist_eq, abs_sub_le_iff, mem_Icc, ← sub_le_iff_le_add', sub_le] lemma squeeze_zero {α} {f g : α → ℝ} {t₀ : filter α} (hf : ∀t, 0 ≤ f t) (hft : ∀t, f t ≤ g t) (g0 : tendsto g t₀ (nhds 0)) : tendsto f t₀ (nhds 0) := begin apply tendsto_of_tendsto_of_tendsto_of_le_of_le (tendsto_const_nhds) g0; simp [*]; exact filter.univ_mem_sets end theorem metric.uniformity_eq_comap_nhds_zero : 𝓤 α = comap (λp:α×α, dist p.1 p.2) (nhds (0 : ℝ)) := begin simp only [uniformity_dist', nhds_eq, comap_infi, comap_principal], congr, funext ε, rw [principal_eq_iff_eq], ext ⟨a, b⟩, simp [real.dist_0_eq_abs] end lemma cauchy_seq_iff_tendsto_dist_at_top_0 [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ tendsto (λ (n : β × β), dist (u n.1) (u n.2)) at_top (nhds 0) := by rw [cauchy_seq_iff_prod_map, metric.uniformity_eq_comap_nhds_zero, ← map_le_iff_le_comap, filter.map_map, tendsto, prod.map_def] end real section cauchy_seq variables [inhabited β] [semilattice_sup β] /-- In a metric space, Cauchy sequences are characterized by the fact that, eventually, the distance between its elements is arbitrarily small -/ theorem metric.cauchy_seq_iff {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, dist (u m) (u n) < ε := begin unfold cauchy_seq, rw metric.cauchy_iff, simp only [true_and, exists_prop, filter.mem_at_top_sets, filter.at_top_ne_bot, filter.mem_map, ne.def, filter.map_eq_bot_iff, not_false_iff, set.mem_set_of_eq], split, { intros H ε εpos, rcases H ε εpos with ⟨t, ⟨N, hN⟩, ht⟩, exact ⟨N, λm n hm hn, ht _ _ (hN _ hm) (hN _ hn)⟩ }, { intros H ε εpos, rcases H (ε/2) (half_pos εpos) with ⟨N, hN⟩, existsi ball (u N) (ε/2), split, { exact ⟨N, λx hx, hN _ _ hx (le_refl N)⟩ }, { exact λx y hx hy, calc dist x y ≤ dist x (u N) + dist y (u N) : dist_triangle_right _ _ _ ... < ε/2 + ε/2 : add_lt_add hx hy ... = ε : add_halves _ } } end /-- A variation around the metric characterization of Cauchy sequences -/ theorem metric.cauchy_seq_iff' {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) (u N) < ε := begin rw metric.cauchy_seq_iff, split, { intros H ε εpos, rcases H ε εpos with ⟨N, hN⟩, exact ⟨N, λn hn, hN _ _ hn (le_refl N)⟩ }, { intros H ε εpos, rcases H (ε/2) (half_pos εpos) with ⟨N, hN⟩, exact ⟨N, λ m n hm hn, calc dist (u m) (u n) ≤ dist (u m) (u N) + dist (u n) (u N) : dist_triangle_right _ _ _ ... < ε/2 + ε/2 : add_lt_add (hN _ hm) (hN _ hn) ... = ε : add_halves _⟩ } end /-- A Cauchy sequence on the natural numbers is bounded. -/ theorem cauchy_seq_bdd {u : ℕ → α} (hu : cauchy_seq u) : ∃ R > 0, ∀ m n, dist (u m) (u n) < R := begin rcases metric.cauchy_seq_iff'.1 hu 1 zero_lt_one with ⟨N, hN⟩, suffices : ∃ R > 0, ∀ n, dist (u n) (u N) < R, { rcases this with ⟨R, R0, H⟩, exact ⟨_, add_pos R0 R0, λ m n, lt_of_le_of_lt (dist_triangle_right _ _ _) (add_lt_add (H m) (H n))⟩ }, let R := finset.sup (finset.range N) (λ n, nndist (u n) (u N)), refine ⟨↑R + 1, add_pos_of_nonneg_of_pos R.2 zero_lt_one, λ n, _⟩, cases le_or_lt N n, { exact lt_of_lt_of_le (hN _ h) (le_add_of_nonneg_left R.2) }, { have : _ ≤ R := finset.le_sup (finset.mem_range.2 h), exact lt_of_le_of_lt this (lt_add_of_pos_right _ zero_lt_one) } end /-- Yet another metric characterization of Cauchy sequences on integers. This one is often the most efficient. -/ lemma cauchy_seq_iff_le_tendsto_0 {s : ℕ → α} : cauchy_seq s ↔ ∃ b : ℕ → ℝ, (∀ n, 0 ≤ b n) ∧ (∀ n m N : ℕ, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) ∧ tendsto b at_top (nhds 0) := ⟨λ hs, begin /- `s` is a Cauchy sequence. The sequence `b` will be constructed by taking the supremum of the distances between `s n` and `s m` for `n m ≥ N`. First, we prove that all these distances are bounded, as otherwise the Sup would not make sense. -/ let S := λ N, (λ(p : ℕ × ℕ), dist (s p.1) (s p.2)) '' {p | p.1 ≥ N ∧ p.2 ≥ N}, have hS : ∀ N, ∃ x, ∀ y ∈ S N, y ≤ x, { rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩, refine λ N, ⟨R, _⟩, rintro _ ⟨⟨m, n⟩, _, rfl⟩, exact le_of_lt (hR m n) }, have bdd : bdd_above (range (λ(p : ℕ × ℕ), dist (s p.1) (s p.2))), { rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩, use R, rintro _ ⟨⟨m, n⟩, rfl⟩, exact le_of_lt (hR m n) }, -- Prove that it bounds the distances of points in the Cauchy sequence have ub : ∀ m n N, N ≤ m → N ≤ n → dist (s m) (s n) ≤ real.Sup (S N) := λ m n N hm hn, real.le_Sup _ (hS N) ⟨⟨_, _⟩, ⟨hm, hn⟩, rfl⟩, have S0m : ∀ n, (0:ℝ) ∈ S n := λ n, ⟨⟨n, n⟩, ⟨le_refl _, le_refl _⟩, dist_self _⟩, have S0 := λ n, real.le_Sup _ (hS n) (S0m n), -- Prove that it tends to `0`, by using the Cauchy property of `s` refine ⟨λ N, real.Sup (S N), S0, ub, metric.tendsto_at_top.2 (λ ε ε0, _)⟩, refine (metric.cauchy_seq_iff.1 hs (ε/2) (half_pos ε0)).imp (λ N hN n hn, _), rw [real.dist_0_eq_abs, abs_of_nonneg (S0 n)], refine lt_of_le_of_lt (real.Sup_le_ub _ ⟨_, S0m _⟩ _) (half_lt_self ε0), rintro _ ⟨⟨m', n'⟩, ⟨hm', hn'⟩, rfl⟩, exact le_of_lt (hN _ _ (le_trans hn hm') (le_trans hn hn')) end, λ ⟨b, _, b_bound, b_lim⟩, metric.cauchy_seq_iff.2 $ λ ε ε0, (metric.tendsto_at_top.1 b_lim ε ε0).imp $ λ N hN m n hm hn, calc dist (s m) (s n) ≤ b N : b_bound m n N hm hn ... ≤ abs (b N) : le_abs_self _ ... = dist (b N) 0 : by rw real.dist_0_eq_abs; refl ... < ε : (hN _ (le_refl N)) ⟩ end cauchy_seq def metric_space.induced {α β} (f : α → β) (hf : function.injective f) (m : metric_space β) : metric_space α := { dist := λ x y, dist (f x) (f y), dist_self := λ x, dist_self _, eq_of_dist_eq_zero := λ x y h, hf (dist_eq_zero.1 h), dist_comm := λ x y, dist_comm _ _, dist_triangle := λ x y z, dist_triangle _ _ _, edist := λ x y, edist (f x) (f y), edist_dist := λ x y, edist_dist _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_dist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, dist (f x) (f y)), refine λ s, mem_comap_sets.trans _, split; intro H, { rcases H with ⟨r, ru, rs⟩, rcases mem_uniformity_dist.1 ru with ⟨ε, ε0, hε⟩, refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h }, { rcases H with ⟨ε, ε0, hε⟩, exact ⟨_, dist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ } end } instance metric_space_subtype {p : α → Prop} [t : metric_space α] : metric_space (subtype p) := metric_space.induced subtype.val (λ x y, subtype.eq) t theorem subtype.dist_eq {p : α → Prop} [t : metric_space α] (x y : subtype p) : dist x y = dist x.1 y.1 := rfl section nnreal instance : metric_space nnreal := by unfold nnreal; apply_instance end nnreal section prod instance prod.metric_space_max [metric_space β] : metric_space (α × β) := { dist := λ x y, max (dist x.1 y.1) (dist x.2 y.2), dist_self := λ x, by simp, eq_of_dist_eq_zero := λ x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ h₂, exact prod.ext_iff.2 ⟨dist_le_zero.1 h₁, dist_le_zero.1 h₂⟩ end, dist_comm := λ x y, by simp [dist_comm], dist_triangle := λ x y z, max_le (le_trans (dist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _))) (le_trans (dist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))), edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2), edist_dist := assume x y, begin have : monotone ennreal.of_real := assume x y h, ennreal.of_real_le_of_real h, rw [edist_dist, edist_dist, (max_distrib_of_monotone this).symm] end, uniformity_dist := begin refine uniformity_prod.trans _, simp [uniformity_dist, comap_infi], rw ← infi_inf_eq, congr, funext, rw ← infi_inf_eq, congr, funext, simp [inf_principal, ext_iff, max_lt_iff] end, to_uniform_space := prod.uniform_space } lemma prod.dist_eq [metric_space β] {x y : α × β} : dist x y = max (dist x.1 y.1) (dist x.2 y.2) := rfl end prod theorem uniform_continuous_dist' : uniform_continuous (λp:α×α, dist p.1 p.2) := metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε/2, half_pos ε0, begin suffices, { intros p q h, cases p with p₁ p₂, cases q with q₁ q₂, cases max_lt_iff.1 h with h₁ h₂, clear h, dsimp at h₁ h₂ ⊢, rw real.dist_eq, refine abs_sub_lt_iff.2 ⟨_, _⟩, { revert p₁ p₂ q₁ q₂ h₁ h₂, exact this }, { apply this; rwa dist_comm } }, intros p₁ p₂ q₁ q₂ h₁ h₂, have := add_lt_add (abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₁ q₁ p₂) h₁)).1 (abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₂ q₂ q₁) h₂)).1, rwa [add_halves, dist_comm p₂, sub_add_sub_cancel, dist_comm q₂] at this end⟩) theorem uniform_continuous_dist [uniform_space β] {f g : β → α} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (λb, dist (f b) (g b)) := (hf.prod_mk hg).comp uniform_continuous_dist' theorem continuous_dist' : continuous (λp:α×α, dist p.1 p.2) := uniform_continuous_dist'.continuous theorem continuous_dist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, dist (f b) (g b)) := (hf.prod_mk hg).comp continuous_dist' theorem tendsto_dist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (nhds a)) (hg : tendsto g x (nhds b)) : tendsto (λx, dist (f x) (g x)) x (nhds (dist a b)) := have tendsto (λp:α×α, dist p.1 p.2) (nhds (a, b)) (nhds (dist a b)), from continuous_iff_continuous_at.mp continuous_dist' (a, b), (hf.prod_mk hg).comp (by rw [nhds_prod_eq] at this; exact this) lemma nhds_comap_dist (a : α) : (nhds (0 : ℝ)).comap (λa', dist a' a) = nhds a := have h₁ : ∀ε, (λa', dist a' a) ⁻¹' ball 0 ε ⊆ ball a ε, by simp [subset_def, real.dist_0_eq_abs], have h₂ : tendsto (λa', dist a' a) (nhds a) (nhds (dist a a)), from tendsto_dist tendsto_id tendsto_const_nhds, le_antisymm (by simp [h₁, nhds_eq, infi_le_infi, principal_mono, -le_principal_iff, -le_infi_iff]) (by simpa [map_le_iff_le_comap.symm, tendsto] using h₂) lemma tendsto_iff_dist_tendsto_zero {f : β → α} {x : filter β} {a : α} : (tendsto f x (nhds a)) ↔ (tendsto (λb, dist (f b) a) x (nhds 0)) := by rw [← nhds_comap_dist a, tendsto_comap_iff] lemma uniform_continuous_nndist' : uniform_continuous (λp:α×α, nndist p.1 p.2) := uniform_continuous_subtype_mk uniform_continuous_dist' _ lemma continuous_nndist' : continuous (λp:α×α, nndist p.1 p.2) := uniform_continuous_nndist'.continuous lemma tendsto_nndist' (a b :α) : tendsto (λp:α×α, nndist p.1 p.2) (filter.prod (nhds a) (nhds b)) (nhds (nndist a b)) := by rw [← nhds_prod_eq]; exact continuous_iff_continuous_at.1 continuous_nndist' _ namespace metric variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α} theorem is_closed_ball : is_closed (closed_ball x ε) := is_closed_le (continuous_dist continuous_id continuous_const) continuous_const /-- ε-characterization of the closure in metric spaces-/ theorem mem_closure_iff' {α : Type u} [metric_space α] {s : set α} {a : α} : a ∈ closure s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε := ⟨begin intros ha ε hε, have A : ball a ε ∩ s ≠ ∅ := mem_closure_iff.1 ha _ is_open_ball (mem_ball_self hε), cases ne_empty_iff_exists_mem.1 A with b hb, simp, exact ⟨b, ⟨hb.2, by have B := hb.1; simpa [mem_ball'] using B⟩⟩ end, begin intros H, apply mem_closure_iff.2, intros o ho ao, rcases is_open_iff.1 ho a ao with ⟨ε, ⟨εpos, hε⟩⟩, rcases H ε εpos with ⟨b, ⟨bs, bdist⟩⟩, have B : b ∈ o ∩ s := ⟨hε (by simpa [dist_comm]), bs⟩, apply ne_empty_of_mem B end⟩ theorem mem_of_closed' {α : Type u} [metric_space α] {s : set α} (hs : is_closed s) {a : α} : a ∈ s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε := by simpa only [closure_eq_of_is_closed hs] using @mem_closure_iff' _ _ s a end metric section pi open finset lattice variables {π : β → Type*} [fintype β] [∀b, metric_space (π b)] instance has_dist_pi : has_dist (Πb, π b) := ⟨λf g, ((finset.sup univ (λb, nndist (f b) (g b)) : nnreal) : ℝ)⟩ lemma dist_pi_def (f g : Πb, π b) : dist f g = (finset.sup univ (λb, nndist (f b) (g b)) : nnreal) := rfl instance metric_space_pi : metric_space (Πb, π b) := { dist := dist, dist_self := assume f, (nnreal.coe_eq_zero _).2 $ bot_unique $ finset.sup_le $ by simp, dist_comm := assume f g, nnreal.eq_iff.2 $ by congr; ext a; exact nndist_comm _ _, dist_triangle := assume f g h, show dist f h ≤ (dist f g) + (dist g h), from begin simp only [dist_pi_def, (nnreal.coe_add _ _).symm, nnreal.coe_le.symm, finset.sup_le_iff], assume b hb, exact le_trans (nndist_triangle _ (g b) _) (add_le_add (le_sup hb) (le_sup hb)) end, eq_of_dist_eq_zero := assume f g eq0, begin simp only [dist_pi_def, nnreal.coe_eq_zero, nnreal.bot_eq_zero.symm, eq_bot_iff, finset.sup_le_iff] at eq0, exact (funext $ assume b, eq_of_nndist_eq_zero $ bot_unique $ eq0 b $ mem_univ b), end, edist := λ f g, finset.sup univ (λb, edist (f b) (g b)), edist_dist := assume x y, begin have A : sup univ (λ (b : β), ((nndist (x b) (y b)) : ennreal)) = ↑(sup univ (λ (b : β), nndist (x b) (y b))), { refine eq.symm (comp_sup_eq_sup_comp _ _ _), exact (assume x y h, ennreal.coe_le_coe.2 h), refl }, simp [dist, edist_nndist, ennreal.of_real, A] end } end pi section compact /-- Any compact set in a metric space can be covered by finitely many balls of a given positive radius -/ lemma finite_cover_balls_of_compact {α : Type u} [metric_space α] {s : set α} (hs : compact s) {e : ℝ} (he : e > 0) : ∃t ⊆ s, finite t ∧ s ⊆ ⋃x∈t, ball x e := begin apply compact_elim_finite_subcover_image hs, { simp [is_open_ball] }, { intros x xs, simp, exact ⟨x, ⟨xs, by simpa⟩⟩ } end end compact section proper_space open metric /-- A metric space is proper if all closed balls are compact. -/ class proper_space (α : Type u) [metric_space α] : Prop := (compact_ball : ∀x:α, ∀r, compact (closed_ball x r)) /- A compact metric space is proper -/ instance proper_of_compact [metric_space α] [compact_space α] : proper_space α := ⟨assume x r, compact_of_is_closed_subset compact_univ is_closed_ball (subset_univ _)⟩ /-- A proper space is locally compact -/ instance locally_compact_of_proper [metric_space α] [proper_space α] : locally_compact_space α := begin apply locally_compact_of_compact_nhds, intros x, existsi closed_ball x 1, split, { apply mem_nhds_iff.2, existsi (1 : ℝ), simp, exact ⟨zero_lt_one, ball_subset_closed_ball⟩ }, { apply proper_space.compact_ball } end /-- A proper space is complete -/ instance complete_of_proper {α : Type u} [metric_space α] [proper_space α] : complete_space α := ⟨begin intros f hf, /- We want to show that the Cauchy filter `f` is converging. It suffices to find a closed ball (therefore compact by properness) where it is nontrivial. -/ have A : ∃ t ∈ f, ∀ x y ∈ t, dist x y < 1 := (metric.cauchy_iff.1 hf).2 1 zero_lt_one, rcases A with ⟨t, ⟨t_fset, ht⟩⟩, rcases inhabited_of_mem_sets hf.1 t_fset with ⟨x, xt⟩, have : t ⊆ closed_ball x 1 := by intros y yt; simp [dist_comm]; apply le_of_lt (ht x y xt yt), have : closed_ball x 1 ∈ f := f.sets_of_superset t_fset this, rcases (compact_iff_totally_bounded_complete.1 (proper_space.compact_ball x 1)).2 f hf (le_principal_iff.2 this) with ⟨y, _, hy⟩, exact ⟨y, hy⟩ end⟩ /-- A proper metric space is separable, and therefore second countable. Indeed, any ball is compact, and therefore admits a countable dense subset. Taking a countable union over the balls centered at a fixed point and with integer radius, one obtains a countable set which is dense in the whole space. -/ instance second_countable_of_proper [metric_space α] [proper_space α] : second_countable_topology α := begin /- We show that the space admits a countable dense subset. The case where the space is empty is special, and trivial. -/ have A : (univ : set α) = ∅ → ∃(s : set α), countable s ∧ closure s = (univ : set α) := assume H, ⟨∅, ⟨by simp, by simp; exact H.symm⟩⟩, have B : (univ : set α) ≠ ∅ → ∃(s : set α), countable s ∧ closure s = (univ : set α) := begin /- When the space is not empty, we take a point `x` in the space, and then a countable set `T r` which is dense in the closed ball `closed_ball x r` for each `r`. Then the set `t = ⋃ T n` (where the union is over all integers `n`) is countable, as a countable union of countable sets, and dense in the space by construction. -/ assume non_empty, rcases ne_empty_iff_exists_mem.1 non_empty with ⟨x, x_univ⟩, choose T a using show ∀ (r:ℝ), ∃ t ⊆ closed_ball x r, (countable (t : set α) ∧ closed_ball x r = closure t), from assume r, emetric.countable_closure_of_compact (proper_space.compact_ball _ _), let t := (⋃n:ℕ, T (n : ℝ)), have T₁ : countable t := by finish [countable_Union], have T₂ : closure t ⊆ univ := by simp, have T₃ : univ ⊆ closure t := begin intros y y_univ, rcases exists_nat_gt (dist y x) with ⟨n, n_large⟩, have h : y ∈ closed_ball x (n : ℝ) := by simp; apply le_of_lt n_large, have h' : closed_ball x (n : ℝ) = closure (T (n : ℝ)) := by finish, have : y ∈ closure (T (n : ℝ)) := by rwa h' at h, show y ∈ closure t, from mem_of_mem_of_subset this (by apply closure_mono; apply subset_Union (λ(n:ℕ), T (n:ℝ))), end, exact ⟨t, ⟨T₁, subset.antisymm T₂ T₃⟩⟩ end, haveI : separable_space α := ⟨by_cases A B⟩, apply emetric.second_countable_of_separable, end end proper_space namespace metric section second_countable open topological_space /-- A metric space is second countable if, for every ε > 0, there is a countable set which is ε-dense. -/ lemma second_countable_of_almost_dense_set (H : ∀ε > (0 : ℝ), ∃ s : set α, countable s ∧ (∀x, ∃y ∈ s, dist x y ≤ ε)) : second_countable_topology α := begin choose T T_dense using H, have I1 : ∀n:ℕ, (n:ℝ) + 1 > 0 := λn, lt_of_lt_of_le zero_lt_one (le_add_of_nonneg_left (nat.cast_nonneg _)), have I : ∀n:ℕ, (n+1 : ℝ)⁻¹ > 0 := λn, inv_pos'.2 (I1 n), let t := ⋃n:ℕ, T (n+1)⁻¹ (I n), have count_t : countable t := by finish [countable_Union], have clos_t : closure t = univ, { refine subset.antisymm (subset_univ _) (λx xuniv, mem_closure_iff'.2 (λε εpos, _)), rcases exists_nat_gt ε⁻¹ with ⟨n, hn⟩, have : ε⁻¹ < n + 1 := lt_of_lt_of_le hn (le_add_of_nonneg_right zero_le_one), have nε : ((n:ℝ)+1)⁻¹ < ε := (inv_lt (I1 n) εpos).2 this, rcases (T_dense (n+1)⁻¹ (I n)).2 x with ⟨y, yT, Dxy⟩, have : y ∈ t := mem_of_mem_of_subset yT (by apply subset_Union (λ (n:ℕ), T (n+1)⁻¹ (I n))), exact ⟨y, this, lt_of_le_of_lt Dxy nε⟩ }, haveI : separable_space α := ⟨⟨t, ⟨count_t, clos_t⟩⟩⟩, exact emetric.second_countable_of_separable α end /-- A metric space space is second countable if one can reconstruct up to any ε>0 any element of the space from countably many data. -/ lemma second_countable_of_countable_discretization {α : Type u} [metric_space α] (H : ∀ε > (0 : ℝ), ∃ (β : Type u) [encodable β] (F : α → β), ∀x y, F x = F y → dist x y ≤ ε) : second_countable_topology α := begin classical, by_cases hs : (univ : set α) = ∅, { haveI : compact_space α := ⟨by rw hs; exact compact_of_finite (set.finite_empty)⟩, by apply_instance }, rcases exists_mem_of_ne_empty hs with ⟨x0, hx0⟩, letI : inhabited α := ⟨x0⟩, refine second_countable_of_almost_dense_set (λε ε0, _), rcases H ε ε0 with ⟨β, fβ, F, hF⟩, let Finv := function.inv_fun F, refine ⟨range Finv, ⟨countable_range _, λx, _⟩⟩, let x' := Finv (F x), have : F x' = F x := function.inv_fun_eq ⟨x, rfl⟩, exact ⟨x', mem_range_self _, hF _ _ this.symm⟩ end end second_countable end metric lemma lebesgue_number_lemma_of_metric {s : set α} {ι} {c : ι → set α} (hs : compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i := let ⟨n, en, hn⟩ := lebesgue_number_lemma hs hc₁ hc₂, ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 en in ⟨δ, δ0, assume x hx, let ⟨i, hi⟩ := hn x hx in ⟨i, assume y hy, hi (hδ (mem_ball'.mp hy))⟩⟩ lemma lebesgue_number_lemma_of_metric_sUnion {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ δ > 0, ∀ x ∈ s, ∃ t ∈ c, ball x δ ⊆ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma_of_metric hs (by simpa) hc₂ namespace metric /-- Boundedness of a subset of a metric space. We formulate the definition to work even in the empty space. -/ def bounded (s : set α) : Prop := ∃C, ∀x y ∈ s, dist x y ≤ C section bounded variables {x : α} {s t : set α} {r : ℝ} @[simp] lemma bounded_empty : bounded (∅ : set α) := ⟨0, by simp⟩ lemma bounded_iff_mem_bounded : bounded s ↔ ∀ x ∈ s, bounded s := ⟨λ h _ _, h, λ H, begin classical, by_cases s = ∅, { subst s, exact ⟨0, by simp⟩ }, { rcases exists_mem_of_ne_empty h with ⟨x, hx⟩, exact H x hx } end⟩ /-- Subsets of a bounded set are also bounded -/ lemma bounded.subset (incl : s ⊆ t) : bounded t → bounded s := Exists.imp $ λ C hC x y hx hy, hC x y (incl hx) (incl hy) /-- Closed balls are bounded -/ lemma bounded_closed_ball : bounded (closed_ball x r) := ⟨r + r, λ y z hy hz, begin simp only [mem_closed_ball] at *, calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... ≤ r + r : add_le_add hy hz end⟩ /-- Open balls are bounded -/ lemma bounded_ball : bounded (ball x r) := bounded_closed_ball.subset ball_subset_closed_ball /-- Given a point, a bounded subset is included in some ball around this point -/ lemma bounded_iff_subset_ball (c : α) : bounded s ↔ ∃r, s ⊆ closed_ball c r := begin split; rintro ⟨C, hC⟩, { classical, by_cases s = ∅, { subst s, exact ⟨0, by simp⟩ }, { rcases exists_mem_of_ne_empty h with ⟨x, hx⟩, exact ⟨C + dist x c, λ y hy, calc dist y c ≤ dist y x + dist x c : dist_triangle _ _ _ ... ≤ C + dist x c : add_le_add_right (hC y x hy hx) _⟩ } }, { exact bounded_closed_ball.subset hC } end /-- The union of two bounded sets is bounded iff each of the sets is bounded -/ @[simp] lemma bounded_union : bounded (s ∪ t) ↔ bounded s ∧ bounded t := ⟨λh, ⟨h.subset (by simp), h.subset (by simp)⟩, begin rintro ⟨hs, ht⟩, refine bounded_iff_mem_bounded.2 (λ x _, _), rw bounded_iff_subset_ball x at hs ht ⊢, rcases hs with ⟨Cs, hCs⟩, rcases ht with ⟨Ct, hCt⟩, exact ⟨max Cs Ct, union_subset (subset.trans hCs $ closed_ball_subset_closed_ball $ le_max_left _ _) (subset.trans hCt $ closed_ball_subset_closed_ball $ le_max_right _ _)⟩, end⟩ /-- A finite union of bounded sets is bounded -/ lemma bounded_bUnion {I : set β} {s : β → set α} (H : finite I) : bounded (⋃i∈I, s i) ↔ ∀i ∈ I, bounded (s i) := finite.induction_on H (by simp) $ λ x I _ _ IH, by simp [or_imp_distrib, forall_and_distrib, IH] /-- A compact set is bounded -/ lemma bounded_of_compact {s : set α} (h : compact s) : bounded s := -- We cover the compact set by finitely many balls of radius 1, -- and then argue that a finite union of bounded sets is bounded let ⟨t, ht, fint, subs⟩ := finite_cover_balls_of_compact h zero_lt_one in bounded.subset subs $ (bounded_bUnion fint).2 $ λ i hi, bounded_ball /-- A finite set is bounded -/ lemma bounded_of_finite {s : set α} (h : finite s) : bounded s := bounded_of_compact $ compact_of_finite h /-- A singleton is bounded -/ lemma bounded_singleton {x : α} : bounded ({x} : set α) := bounded_of_finite $ finite_singleton _ /-- Characterization of the boundedness of the range of a function -/ lemma bounded_range_iff {f : β → α} : bounded (range f) ↔ ∃C, ∀x y, dist (f x) (f y) ≤ C := exists_congr $ λ C, ⟨ λ H x y, H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩, by rintro H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩; exact H x y⟩ /-- In a compact space, all sets are bounded -/ lemma bounded_of_compact_space [compact_space α] : bounded s := (bounded_of_compact compact_univ).subset (subset_univ _) /-- In a proper space, a set is compact if and only if it is closed and bounded -/ lemma compact_iff_closed_bounded [proper_space α] : compact s ↔ is_closed s ∧ bounded s := ⟨λ h, ⟨closed_of_compact _ h, bounded_of_compact h⟩, begin rintro ⟨hc, hb⟩, classical, by_cases s = ∅, {simp [h, compact_empty]}, rcases exists_mem_of_ne_empty h with ⟨x, hx⟩, rcases (bounded_iff_subset_ball x).1 hb with ⟨r, hr⟩, exact compact_of_is_closed_subset (proper_space.compact_ball x r) hc hr end⟩ end bounded section diam variables {s : set α} {x y : α} /-- The diameter of a set in a metric space. To get controllable behavior even when the diameter should be infinite, we express it in terms of the emetric.diameter -/ def diam (s : set α) : ℝ := ennreal.to_real (emetric.diam s) /-- The diameter of a set is always nonnegative -/ lemma diam_nonneg : 0 ≤ diam s := by simp [diam] /-- The empty set has zero diameter -/ @[simp] lemma diam_empty : diam (∅ : set α) = 0 := by simp [diam] /-- A singleton has zero diameter -/ @[simp] lemma diam_singleton : diam ({x} : set α) = 0 := by simp [diam] /-- Characterize the boundedness of a set in terms of the finiteness of its emetric.diameter. -/ lemma bounded_iff_diam_ne_top : bounded s ↔ emetric.diam s ≠ ⊤ := begin classical, by_cases hs : s = ∅, { simp [hs] }, { rcases ne_empty_iff_exists_mem.1 hs with ⟨x, hx⟩, split, { assume bs, rcases (bounded_iff_subset_ball x).1 bs with ⟨r, hr⟩, have r0 : 0 ≤ r := by simpa [closed_ball] using hr hx, have : emetric.diam s < ⊤ := calc emetric.diam s ≤ emetric.diam (emetric.closed_ball x (ennreal.of_real r)) : by rw emetric_closed_ball r0; exact emetric.diam_mono hr ... ≤ 2 * (ennreal.of_real r) : emetric.diam_closed_ball ... < ⊤ : begin apply ennreal.lt_top_iff_ne_top.2, simp [ennreal.mul_eq_top], end, exact ennreal.lt_top_iff_ne_top.1 this }, { assume ds, have : s ⊆ closed_ball x (ennreal.to_real (emetric.diam s)), { rw [← emetric_closed_ball ennreal.to_real_nonneg, ennreal.of_real_to_real ds], exact λy hy, emetric.edist_le_diam_of_mem hy hx }, exact bounded.subset this (bounded_closed_ball) }} end /-- An unbounded set has zero diameter. If you would prefer to get the value ∞, use `emetric.diam`. This lemma makes it possible to avoid side conditions in some situations -/ lemma diam_eq_zero_of_unbounded (h : ¬(bounded s)) : diam s = 0 := begin simp only [bounded_iff_diam_ne_top, not_not, ne.def] at h, simp [diam, h] end /-- If `s ⊆ t`, then the diameter of `s` is bounded by that of `t`, provided `t` is bounded. -/ lemma diam_mono {s t : set α} (h : s ⊆ t) (ht : bounded t) : diam s ≤ diam t := begin unfold diam, rw ennreal.to_real_le_to_real (bounded_iff_diam_ne_top.1 (bounded.subset h ht)) (bounded_iff_diam_ne_top.1 ht), exact emetric.diam_mono h end /-- The distance between two points in a set is controlled by the diameter of the set. -/ lemma dist_le_diam_of_mem (h : bounded s) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s := begin rw [diam, dist_edist], rw ennreal.to_real_le_to_real (edist_ne_top _ _) (bounded_iff_diam_ne_top.1 h), exact emetric.edist_le_diam_of_mem hx hy end /-- If the distance between any two points in a set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_dist_le {d : real} (hd : d ≥ 0) (h : ∀x y ∈ s, dist x y ≤ d) : diam s ≤ d := begin have I : emetric.diam s ≤ ennreal.of_real d, { refine emetric.diam_le_of_forall_edist_le (λx y hx hy, _), rw [edist_dist], exact ennreal.of_real_le_of_real (h x y hx hy) }, have A : emetric.diam s ≠ ⊤ := ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt I (ennreal.lt_top_iff_ne_top.2 (by simp))), rw [← ennreal.to_real_of_real hd, diam, ennreal.to_real_le_to_real A], { exact I }, { simp } end /-- The diameter of a union is controlled by the sum of the diameters, and the distance between any two points in each of the sets. This lemma is true without any side condition, since it is obviously true if `s ∪ t` is unbounded. -/ lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + dist x y + diam t := have I1 : ¬(bounded (s ∪ t)) → diam (s ∪ t) ≤ diam s + dist x y + diam t := λh, calc diam (s ∪ t) = 0 + 0 + 0 : by simp [diam_eq_zero_of_unbounded h] ... ≤ diam s + dist x y + diam t : add_le_add (add_le_add diam_nonneg dist_nonneg) diam_nonneg, have I2 : (bounded (s ∪ t)) → diam (s ∪ t) ≤ diam s + dist x y + diam t := λh, begin have : bounded s := bounded.subset (subset_union_left _ _) h, have : bounded t := bounded.subset (subset_union_right _ _) h, have A : ∀a ∈ s, ∀b ∈ t, dist a b ≤ diam s + dist x y + diam t := λa ha b hb, calc dist a b ≤ dist a x + dist x y + dist y b : dist_triangle4 _ _ _ _ ... ≤ diam s + dist x y + diam t : add_le_add (add_le_add (dist_le_diam_of_mem ‹bounded s› ha xs) (le_refl _)) (dist_le_diam_of_mem ‹bounded t› yt hb), have B : ∀a b ∈ s ∪ t, dist a b ≤ diam s + dist x y + diam t := λa b ha hb, begin cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b, { calc dist a b ≤ diam s : dist_le_diam_of_mem ‹bounded s› h'a h'b ... = diam s + (0 + 0) : by simp ... ≤ diam s + (dist x y + diam t) : add_le_add (le_refl _) (add_le_add dist_nonneg diam_nonneg) ... = diam s + dist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] }, { exact A a h'a b h'b }, { have Z := A b h'b a h'a, rwa [dist_comm] at Z }, { calc dist a b ≤ diam t : dist_le_diam_of_mem ‹bounded t› h'a h'b ... = (0 + 0) + diam t : by simp ... ≤ (diam s + dist x y) + diam t : add_le_add (add_le_add diam_nonneg dist_nonneg) (le_refl _) } end, have C : 0 ≤ diam s + dist x y + diam t := calc 0 = 0 + 0 + 0 : by simp ... ≤ diam s + dist x y + diam t : add_le_add (add_le_add diam_nonneg dist_nonneg) diam_nonneg, exact diam_le_of_forall_dist_le C B end, classical.by_cases I2 I1 /-- If two sets intersect, the diameter of the union is bounded by the sum of the diameters. -/ lemma diam_union' {t : set α} (h : s ∩ t ≠ ∅) : diam (s ∪ t) ≤ diam s + diam t := begin rcases ne_empty_iff_exists_mem.1 h with ⟨x, ⟨xs, xt⟩⟩, simpa using diam_union xs xt end /-- The diameter of a closed ball of radius `r` is at most `2 r`. -/ lemma diam_closed_ball {r : ℝ} (h : r ≥ 0) : diam (closed_ball x r) ≤ 2 * r := diam_le_of_forall_dist_le (mul_nonneg (by norm_num) h) $ λa b ha hb, calc dist a b ≤ dist a x + dist b x : dist_triangle_right _ _ _ ... ≤ r + r : add_le_add ha hb ... = 2 * r : by simp [mul_two, mul_comm] /-- The diameter of a ball of radius `r` is at most `2 r`. -/ lemma diam_ball {r : ℝ} (h : r ≥ 0) : diam (ball x r) ≤ 2 * r := le_trans (diam_mono ball_subset_closed_ball bounded_closed_ball) (diam_closed_ball h) end diam end metric
4562c6c9a398b4883c588c51c3de301a80d906e5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/fail/parser_error.lean
d1f45b399cc54329f04b5a59d30fdd909e9471a0
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
6
lean
lemma
215aa388011d1a54a9298500cec05410c6c0c31a
947b78d97130d56365ae2ec264df196ce769371a
/src/Init/Control/MonadControl.lean
493543a50272ac934072b4c2125e39018a5cbea0
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,760
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich, Leonardo de Moura See: https://lexi-lambda.github.io/blog/2019/09/07/demystifying-monadbasecontrol/ -/ prelude import Init.Control.MonadLift universes u v w class MonadControl (m : Type u → Type v) (n : Type u → Type w) := (stM : Type u → Type u) (liftWith {α : Type u} : ((∀ {β}, n β → m (stM β)) → m α) → n α) (restoreM {α : Type u} : m (stM α) → n α) class MonadControlT (m : Type u → Type v) (n : Type u → Type w) := (stM : Type u → Type u) (liftWith {α : Type u} : ((∀ {β}, n β → m (stM β)) → m α) → n α) (restoreM {α : Type u} : stM α → n α) export MonadControlT (stM liftWith restoreM) instance monadControlTrans (m n o) [MonadControlT m n] [MonadControl n o] : MonadControlT m o := { stM := fun α => stM m n (MonadControl.stM n o α), liftWith := fun α f => MonadControl.liftWith fun x₂ => liftWith fun x₁ => f fun β => x₁ ∘ x₂, restoreM := fun α => MonadControl.restoreM ∘ restoreM } instance monadControlRefl (m : Type u → Type v) [HasPure m] : MonadControlT m m := { stM := fun α => α, liftWith := fun α f => f fun β x => x, restoreM := fun α x => pure x } @[inline] def controlAt (m : Type u → Type v) {n : Type u → Type w} [MonadControlT m n] [HasBind n] {α : Type u} (f : (∀ {β}, n β → m (stM m n β)) → m (stM m n α)) : n α := liftWith f >>= restoreM @[inline] def control {m : Type u → Type v} {n : Type u → Type w} [MonadControlT m n] [HasBind n] {α : Type u} (f : (∀ {β}, n β → m (stM m n β)) → m (stM m n α)) : n α := controlAt m f
de75d76d9304efe15c0f1f763d0c9c8d94cc03cd
626e312b5c1cb2d88fca108f5933076012633192
/src/analysis/calculus/tangent_cone.lean
38976983a7b84f06ce55f1702067f8dc70b44b1b
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,699
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.convex.basic import analysis.specific_limits /-! # Tangent cone In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s` ensuring that, if a function has two derivatives, then they have to coincide. As a direct definition of this fact (quantifying on all target types and all functions) would depend on universes, we use a more intrinsic definition: if all the possible tangent directions to the set `s` at the point `x` span a dense subset of the whole subset, it is easy to check that the derivative has to be unique. Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`, and express `unique_diff_within_at` and `unique_diff_on` in terms of it. One should however think of this definition as an implementation detail: the only reason to introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness of the derivative. This is why their names reflect their uses, and not how they are defined. ## Implementation details Note that this file is imported by `fderiv.lean`. Hence, derivatives are not defined yet. The property of uniqueness of the derivative is therefore proved in `fderiv.lean`, but based on the properties of the tangent cone we prove here. -/ variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] open filter set open_locale topological_space section tangent_cone variables {E : Type*} [add_comm_monoid E] [module 𝕜 E] [topological_space E] /-- The set of all tangent directions to the set `s` at the point `x`. -/ def tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto (λn, ∥c n∥) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} /-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space. The main role of this property is to ensure that the differential within `s` at `x` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `fderiv.lean`. To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which is automatic when `E` is not `0`-dimensional). -/ @[mk_iff] structure unique_diff_within_at (s : set E) (x : E) : Prop := (dense_tangent_cone : dense ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E)) (mem_closure : x ∈ closure s) /-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of the whole space. The main role of this property is to ensure that the differential along `s` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in `fderiv.lean`. -/ def unique_diff_on (s : set E) : Prop := ∀x ∈ s, unique_diff_within_at 𝕜 s x end tangent_cone variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_group G] [normed_space ℝ G] variables {𝕜} {x y : E} {s t : set E} section tangent_cone /- This section is devoted to the properties of the tangent cone. -/ open normed_field lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ := begin refine univ_subset_iff.1 (λy hy, _), rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩, refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem' (λn, mem_univ _), _, _⟩, { simp only [norm_pow], exact tendsto_pow_at_top_at_top_of_one_lt hw }, { convert tendsto_const_nhds, ext n, have : w ^ n * (w ^ n)⁻¹ = 1, { apply mul_inv_cancel, apply pow_ne_zero, simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm }, rw [smul_smul, this, one_smul] } end lemma tangent_cone_mono (h : s ⊆ t) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, exact ⟨c, d, mem_of_superset ds (λn hn, h hn), ctop, clim⟩ end /-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone, the sequence `d` tends to 0 at infinity. -/ lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E} (hc : tendsto (λn, ∥c n∥) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) : tendsto d l (𝓝 0) := begin have A : tendsto (λn, ∥c n∥⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc, have B : tendsto (λn, ∥c n • d n∥) l (𝓝 ∥y∥) := (continuous_norm.tendsto _).comp hd, have C : tendsto (λn, ∥c n∥⁻¹ * ∥c n • d n∥) l (𝓝 (0 * ∥y∥)) := A.mul B, rw zero_mul at C, have : ∀ᶠ n in l, ∥c n∥⁻¹ * ∥c n • d n∥ = ∥d n∥, { apply (eventually_ne_of_tendsto_norm_at_top hc 0).mono (λn hn, _), rw [norm_smul, ← mul_assoc, inv_mul_cancel, one_mul], rwa [ne.def, norm_eq_zero] }, have D : tendsto (λ n, ∥d n∥) l (𝓝 0) := tendsto.congr' this C, rw tendsto_zero_iff_norm_tendsto_zero, exact D end lemma tangent_cone_mono_nhds (h : 𝓝[s] x ≤ 𝓝[t] x) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, refine ⟨c, d, _, ctop, clim⟩, suffices : tendsto (λ n, x + d n) at_top (𝓝[t] x), from tendsto_principal.1 (tendsto_inf.1 this).2, refine (tendsto_inf.2 ⟨_, tendsto_principal.2 ds⟩).mono_right h, simpa only [add_zero] using tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim) end /-- Tangent cone of `s` at `x` depends only on `𝓝[s] x`. -/ lemma tangent_cone_congr (h : 𝓝[s] x = 𝓝[t] x) : tangent_cone_at 𝕜 s x = tangent_cone_at 𝕜 t x := subset.antisymm (tangent_cone_mono_nhds $ le_of_eq h) (tangent_cone_mono_nhds $ le_of_eq h.symm) /-- Intersecting with a neighborhood of the point does not change the tangent cone. -/ lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) : tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x := tangent_cone_congr (nhds_within_restrict' _ ht).symm /-- The tangent cone of a product contains the tangent cone of its left factor. -/ lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) : linear_map.inl 𝕜 E F '' (tangent_cone_at 𝕜 s x) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) := begin rintros _ ⟨v, ⟨c, d, hd, hc, hy⟩, rfl⟩, have : ∀n, ∃d', y + d' ∈ t ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n, { assume n, rcases mem_closure_iff_nhds.1 ht _ (eventually_nhds_norm_smul_sub_lt (c n) y (pow_pos one_half_pos n)) with ⟨z, hz, hzt⟩, exact ⟨z - y, by simpa using hzt, by simpa using hz⟩ }, choose d' hd' using this, refine ⟨c, λn, (d n, d' n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d n, d' n) ∈ set.prod s t, { filter_upwards [hd], assume n hn, simp [hn, (hd' n).1] }, { apply tendsto.prod_mk_nhds hy _, refine squeeze_zero_norm (λn, (hd' n).2.le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } end /-- The tangent cone of a product contains the tangent cone of its right factor. -/ lemma subset_tangent_cone_prod_right {t : set F} {y : F} (hs : x ∈ closure s) : linear_map.inr 𝕜 E F '' (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) := begin rintros _ ⟨w, ⟨c, d, hd, hc, hy⟩, rfl⟩, have : ∀n, ∃d', x + d' ∈ s ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n, { assume n, rcases mem_closure_iff_nhds.1 hs _ (eventually_nhds_norm_smul_sub_lt (c n) x (pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩, exact ⟨z - x, by simpa using hzs, by simpa using hz⟩ }, choose d' hd' using this, refine ⟨c, λn, (d' n, d n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d' n, d n) ∈ set.prod s t, { filter_upwards [hd], assume n hn, simp [hn, (hd' n).1] }, { apply tendsto.prod_mk_nhds _ hy, refine squeeze_zero_norm (λn, (hd' n).2.le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } end /-- The tangent cone of a product contains the tangent cone of each factor. -/ lemma maps_to_tangent_cone_pi {ι : Type*} [decidable_eq ι] {E : ι → Type*} [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] {s : Π i, set (E i)} {x : Π i, E i} {i : ι} (hi : ∀ j ≠ i, x j ∈ closure (s j)) : maps_to (linear_map.single i : E i →ₗ[𝕜] Π j, E j) (tangent_cone_at 𝕜 (s i) (x i)) (tangent_cone_at 𝕜 (set.pi univ s) x) := begin rintros w ⟨c, d, hd, hc, hy⟩, have : ∀ n (j ≠ i), ∃ d', x j + d' ∈ s j ∧ ∥c n • d'∥ < (1 / 2 : ℝ) ^ n, { assume n j hj, rcases mem_closure_iff_nhds.1 (hi j hj) _ (eventually_nhds_norm_smul_sub_lt (c n) (x j) (pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩, exact ⟨z - x j, by simpa using hzs, by simpa using hz⟩ }, choose! d' hd's hcd', refine ⟨c, λ n, function.update (d' n) i (d n), hd.mono (λ n hn j hj', _), hc, tendsto_pi.2 $ λ j, _⟩, { rcases em (j = i) with rfl|hj; simp * }, { rcases em (j = i) with rfl|hj, { simp [hy] }, { suffices : tendsto (λ n, c n • d' n j) at_top (𝓝 0), by simpa [hj], refine squeeze_zero_norm (λ n, (hcd' n j hj).le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } } end /-- If a subset of a real vector space contains a segment, then the direction of this segment belongs to the tangent cone at its endpoints. -/ lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment x y ⊆ s) : y - x ∈ tangent_cone_at ℝ s x := begin let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem' (λn, h _), _, _⟩, show x + d n ∈ segment x y, { rw segment_eq_image, refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩, { rw inv_nonneg, apply pow_nonneg, norm_num }, { apply inv_le_one, apply one_le_pow_of_one_le, norm_num }, { simp only [d, sub_smul, smul_sub, one_smul], abel } }, show filter.tendsto (λ (n : ℕ), ∥c n∥) filter.at_top filter.at_top, { have : (λ (n : ℕ), ∥c n∥) = c, by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) }, rw this, exact tendsto_pow_at_top_at_top_of_one_lt (by norm_num) }, show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)), { have : (λ (n : ℕ), c n • d n) = (λn, y - x), { ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ (by norm_num) }, rw this, apply tendsto_const_nhds } end end tangent_cone section unique_diff /-! ### Properties of `unique_diff_within_at` and `unique_diff_on` This section is devoted to properties of the predicates `unique_diff_within_at` and `unique_diff_on`. -/ lemma unique_diff_on.unique_diff_within_at {s : set E} {x} (hs : unique_diff_on 𝕜 s) (h : x ∈ s) : unique_diff_within_at 𝕜 s x := hs x h lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x := by { rw [unique_diff_within_at_iff, tangent_cone_univ], simp } lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) := λx hx, unique_diff_within_at_univ lemma unique_diff_on_empty : unique_diff_on 𝕜 (∅ : set E) := λ x hx, hx.elim lemma unique_diff_within_at.mono_nhds (h : unique_diff_within_at 𝕜 s x) (st : 𝓝[s] x ≤ 𝓝[t] x) : unique_diff_within_at 𝕜 t x := begin simp only [unique_diff_within_at_iff] at *, rw [mem_closure_iff_nhds_within_ne_bot] at h ⊢, exact ⟨h.1.mono $ submodule.span_mono $ tangent_cone_mono_nhds st, h.2.mono st⟩ end lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) : unique_diff_within_at 𝕜 t x := h.mono_nhds $ nhds_within_mono _ st lemma unique_diff_within_at_congr (st : 𝓝[s] x = 𝓝[t] x) : unique_diff_within_at 𝕜 s x ↔ unique_diff_within_at 𝕜 t x := ⟨λ h, h.mono_nhds $ le_of_eq st, λ h, h.mono_nhds $ le_of_eq st.symm⟩ lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict' _ ht).symm lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter ht).2 hs lemma unique_diff_within_at_inter' (ht : t ∈ 𝓝[s] x) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict'' _ ht).symm lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝[s] x) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter' ht).2 hs lemma unique_diff_within_at_of_mem_nhds (h : s ∈ 𝓝 x) : unique_diff_within_at 𝕜 s x := by simpa only [univ_inter] using unique_diff_within_at_univ.inter h lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x := unique_diff_within_at_of_mem_nhds (is_open.mem_nhds hs xs) lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) := λx hx, (hs x hx.1).inter (is_open.mem_nhds ht hx.2) lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s := λx hx, is_open.unique_diff_within_at hs hx /-- The product of two sets of unique differentiability at points `x` and `y` has unique differentiability at `(x, y)`. -/ lemma unique_diff_within_at.prod {t : set F} {y : F} (hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) : unique_diff_within_at 𝕜 (set.prod s t) (x, y) := begin rw [unique_diff_within_at_iff] at ⊢ hs ht, rw [closure_prod_eq], refine ⟨_, hs.2, ht.2⟩, have : _ ≤ submodule.span 𝕜 (tangent_cone_at 𝕜 (s.prod t) (x, y)) := submodule.span_mono (union_subset (subset_tangent_cone_prod_left ht.2) (subset_tangent_cone_prod_right hs.2)), rw [linear_map.span_inl_union_inr, set_like.le_def] at this, exact (hs.1.prod ht.1).mono this end lemma unique_diff_within_at.univ_pi (ι : Type*) [fintype ι] (E : ι → Type*) [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (x : Π i, E i) (h : ∀ i, unique_diff_within_at 𝕜 (s i) (x i)) : unique_diff_within_at 𝕜 (set.pi univ s) x := begin classical, simp only [unique_diff_within_at_iff, closure_pi_set] at h ⊢, refine ⟨(dense_pi univ (λ i _, (h i).1)).mono _, λ i _, (h i).2⟩, norm_cast, simp only [← submodule.supr_map_single, supr_le_iff, linear_map.map_span, submodule.span_le, ← maps_to'], exact λ i, (maps_to_tangent_cone_pi $ λ j hj, (h j).2).mono subset.rfl submodule.subset_span end lemma unique_diff_within_at.pi (ι : Type*) [fintype ι] (E : ι → Type*) [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (x : Π i, E i) (I : set ι) (h : ∀ i ∈ I, unique_diff_within_at 𝕜 (s i) (x i)) : unique_diff_within_at 𝕜 (set.pi I s) x := begin classical, rw [← set.univ_pi_piecewise], refine unique_diff_within_at.univ_pi _ _ _ _ (λ i, _), by_cases hi : i ∈ I; simp [*, unique_diff_within_at_univ], end /-- The product of two sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) : unique_diff_on 𝕜 (set.prod s t) := λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2) /-- The finite product of a family of sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.pi (ι : Type*) [fintype ι] (E : ι → Type*) [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (I : set ι) (h : ∀ i ∈ I, unique_diff_on 𝕜 (s i)) : unique_diff_on 𝕜 (set.pi I s) := λ x hx, unique_diff_within_at.pi _ _ _ _ _ $ λ i hi, h i hi (x i) (hx i hi) /-- The finite product of a family of sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.univ_pi (ι : Type*) [fintype ι] (E : ι → Type*) [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (h : ∀ i, unique_diff_on 𝕜 (s i)) : unique_diff_on 𝕜 (set.pi univ s) := unique_diff_on.pi _ _ _ _ $ λ i _, h i /-- In a real vector space, a convex set with nonempty interior is a set of unique differentiability. -/ theorem unique_diff_on_convex {s : set G} (conv : convex s) (hs : (interior s).nonempty) : unique_diff_on ℝ s := begin assume x xs, rcases hs with ⟨y, hy⟩, suffices : y - x ∈ interior (tangent_cone_at ℝ s x), { refine ⟨dense.of_closure _, subset_closure xs⟩, simp [(submodule.span ℝ (tangent_cone_at ℝ s x)).eq_top_of_nonempty_interior' ⟨y - x, interior_mono submodule.subset_span this⟩] }, rw [mem_interior_iff_mem_nhds] at hy ⊢, apply mem_of_superset ((is_open_map_sub_right x).image_mem_nhds hy), rintros _ ⟨z, zs, rfl⟩, exact mem_tangent_cone_of_segment_subset (conv.segment_subset xs zs) end lemma unique_diff_on_Ici (a : ℝ) : unique_diff_on ℝ (Ici a) := unique_diff_on_convex (convex_Ici a) $ by simp only [interior_Ici, nonempty_Ioi] lemma unique_diff_on_Iic (a : ℝ) : unique_diff_on ℝ (Iic a) := unique_diff_on_convex (convex_Iic a) $ by simp only [interior_Iic, nonempty_Iio] lemma unique_diff_on_Ioi (a : ℝ) : unique_diff_on ℝ (Ioi a) := is_open_Ioi.unique_diff_on lemma unique_diff_on_Iio (a : ℝ) : unique_diff_on ℝ (Iio a) := is_open_Iio.unique_diff_on lemma unique_diff_on_Icc {a b : ℝ} (hab : a < b) : unique_diff_on ℝ (Icc a b) := unique_diff_on_convex (convex_Icc a b) $ by simp only [interior_Icc, nonempty_Ioo, hab] lemma unique_diff_on_Ico (a b : ℝ) : unique_diff_on ℝ (Ico a b) := if hab : a < b then unique_diff_on_convex (convex_Ico a b) $ by simp only [interior_Ico, nonempty_Ioo, hab] else by simp only [Ico_eq_empty hab, unique_diff_on_empty] lemma unique_diff_on_Ioc (a b : ℝ) : unique_diff_on ℝ (Ioc a b) := if hab : a < b then unique_diff_on_convex (convex_Ioc a b) $ by simp only [interior_Ioc, nonempty_Ioo, hab] else by simp only [Ioc_eq_empty hab, unique_diff_on_empty] lemma unique_diff_on_Ioo (a b : ℝ) : unique_diff_on ℝ (Ioo a b) := is_open_Ioo.unique_diff_on /-- The real interval `[0, 1]` is a set of unique differentiability. -/ lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) := unique_diff_on_Icc zero_lt_one end unique_diff
db55514b516aa4d4c8bb7dd6a9a979fecc9210bf
92e157ec9825b5e4597a6d715a8928703bc8e3b2
/src/mywork/lecture_10.lean
0403c42edae4fc77613fa0c82b95c4a6e1b03f7e
[]
no_license
exb3dg/cs2120f21
9e566bc508762573c023d3e70f83cb839c199ec8
319b8bf0d63bf96437bf17970ce0198d0b3525cd
refs/heads/main
1,692,970,909,568
1,634,584,540,000
1,634,584,540,000
399,947,025
0
0
null
null
null
null
UTF-8
Lean
false
false
9,247
lean
/- In today's class, we'll continue with our exploration of the proposition, "false", its elimination rule, and their vital uses in logical reasoning: especially in - proof of ¬P by negation - proof of P by false elimination Here are the inference rules in display notation: NEGATION INTRODUCTION The first, proof by negation, says that from a proof of P → false you can derive a proof of ¬P. Indeed! It's definitional. Recall: def not (P : Prop) := P → false. (P : Prop) (np : P → false) --------------------------- [by defn'n] (pf : ¬P) This rule is the foundation for "proof by negation." To prove ¬P you first assume P, is true, then you show that in this context you can derive a contradiction. What you have then shown, of course, is P → false. So, to prove ¬P, assume P and show that in this context there is a contradiction. This is proof by negation. It is not to be confused with proof by contradition, which is a different thing entirely. (Proof by contradiction. You can use this approach to a proposition, P, by assuming ¬P and showing that this assumption leads to a contradiction. That proves ¬¬P. Then you use the *indepedent* axiom of negation elimination to infer P from ¬¬P.) FALSE ELIMINATION The second rule says that if you have a proof of false and any other proposition, P, the logic is useless and so you might as well just admit that P is true. Why is the logic useless? Well, if false is true then there's no longer a difference! In this situation, tHere's sense in reasoning any further, and you can just dismiss it. A contradiction makes a logic inconsistent. (P : Prop) (f : false) ---------------------- (false_elim f) (pf : P) -/ /- We covered the relatively simpler notion of negation introduction last time, so today we will start by focusing on false elimination. Understanding why it's not magic and in fact makes perfect sense takes a bit of thought. -/ /- To make sense of false elimination, think in terms of case analysis. Proof by case analysis says that if the truth of some proposition, Y, follows from *any possible form of proof* of X, then you've proved X → Y. If X is a disjunction, P ∨ Q (so now you want to prove P ∨ Q → Y) you must consider two cases: one where P is assumed true and one where Q is assumed true. If X is a proposition with just one form of proof (e.g., the proposition, true), there's just one case to consider. -/ -- two cases example : true ∨ false → true := begin assume h, cases h, assumption, -- context has exact proof contradiction, -- context has contradiction end -- one case example : true → true := begin assume t, cases t, -- just one case exact true.intro, end /- How many cases must you consider if you putatively have a putative proof of false? ZERO! To consider *all* cases you need consider exactly zero cases. Proving all cases when the number of cases is zero is trivial, so the conclusion *follows*. -/ example : false → false := begin assume f, cases f, -- case analysis: there are no cases! end /- In fact, it doesn't matter what your conclusion is: it will always be true in a context in which you have a proof of false. And this makes sense, because if you have a proof of false, then false is true, so whether a given proposition is true or false, it's true, because even if it's false, well, false is true, so it's also true! -/ theorem false_elim : ∀ (P : Prop), false → P := begin assume P, assume f, cases f, end /- This, then, is the general principle for false elimination: ANYTHING FOLLOWS FROM FALSE. This principle gives you a powerful way to prove any proposition is true (conditional on your having a proof that can't exist). The theorem states that if you're given any proposition, P, and a proof, f, of false, then in that context, you can return a proof of P by using false elimination. The only problem with this super-power is that in reality, there is no proof of false, so there's no real danger of any old proposition being automatically true! The rule of false elimination tells you that if you're in a situation that can't happen, then no worries, be happy, you're done (just use false elimination). -/ /- The elimination principle for false is called false.elim in Lean. If you are given or can derive a proof, f, of false, then all you have to do to finish your proof is to say, "this is situation can't happen, so we need not consider it any further." Or, formally, (false.elim f). -/ -- Suppose P is *any* (an arbitrary) proposition axiom P : Prop example : false → P := begin assume f, exact false.elim f, -- Using Lean's version -- P is implicit argument end /- SOME THEOREMS INVOLVING FALSE AND NEGATION -/ theorem no_contradiction : ∀ (P : Prop), ¬(P ∧ ¬P) := begin assume P h, -- assume you have P : Prop and h : P ∧ ¬P have p := and.elim_left h, -- have a proof of P from h have np := and.elim_right h, -- have a proof of ¬P from h have f := np p, -- assume you also have both ¬P and P exact f, -- when you try to exact it'll return false end /- The so-called "law" (it's really an axiom) of the excluded middle says that any proposition is either true or false. There's no middle ground. But in the constructive logic of Lean, this isn't true. To prove P ∨ ¬P, as you recall, we need to have either a proof of P (in this case use or.intro_left) or a proof of ¬P, in which case we use or.intro_right to prove P ∨ ¬P. But what if we don't have a proof either way? There are many important questions in computer science and mathematics where we don't know either way. If you call one of those propositions, P, and try to prove P ∨ ¬P in Lean, you just get stuck. -/ theorem excluded_middle' : ∀ (P : Prop), (P ∨ ¬P) := begin assume P, -- we don't have a proof of either P or of ¬P! end /- Let P be the conjecture, "every even whole number greater than 2 is the sum of two prime numbers." This conjecture, dating (in a slightly different form) to a letter from Goldback to Euler in 1742 is still neither proved nor disproved! Below you will find a placeholder for a proof. Just fill it in (correctly) and you will win $1M and probably a Fields Medal (the "Nobel Prize" in mathematics). -/ axioms (ev : ℕ → Prop) (prime : ℕ → Prop) def goldbach_conjecture := ∀ (n : ℕ), n > 2 → (∃ h k : ℕ, n = h + k ∧ prime h ∧ prime k) theorem goldbach_conjecture_true : goldbach_conjecture := _ theorem goldbach_conjecture_false : ¬goldbach_conjecture := _ example : goldbach_conjecture ∨ ¬goldbach_conjecture := _ /- Our only options are or.intro_left or or.intro_right, but we don't have a required argument (proof) to use either of them! -/ /- The axioms of the constructive logic of Lean are not strong enough to prove the "law of the excluded middle." Rather, if we want to use it, we have to accept it as an additional axiom. We thus have two different logics: one without and one with the law of the excluded middle! -/ axiom excluded_middle : ∀ (P : Prop), (P ∨ ¬P) /- Now, for any proposition whatsoever, P, we can always prove P ∨ ¬P by applying excluded middle to P (using the elimination rule for ∀). What we get in return is a proof of P ∨ ¬P for whatever proposition P is. Here is an example where the proposition is ItsRaining. -/ axiom ItsRaining : Prop theorem example_em : ItsRaining ∨ ¬ItsRaining := begin apply excluded_middle ItsRaining, end /- PROOF BY NEGATION, EXAMPLE -/ /- Next we return to the negation connective, which in logic is pronounced "not." Given any proposition, P, ¬P is also a proposition; and what it means is, exactly, P → false. If P is true, then false is true, and false cannot possibly be true, so P must not be. Thus, to prove ¬P you prove P → false. Another way to read P → false is that, if we assume that P is true, we can derive a proof of false, which cannot exist, so P must not be true. MEMORIZE THIS REASONING. Again, to prove ¬P, *assume P and show that in that context, you can derive a contradiction in the form of a proof of false. This is the strategy call proof by contradiction. -/ /- How about we prove ¬(0 = 1) in English. Proof. By negation. Assume 0 = 1 and show that this leads to a contradiction. That is, show 0 = 1 → false. The rest of the proof is by case analysis on the assumed proof of 0 = 1. The only way to have a proof of equality is by the reflexive property of =. But the reflexive property doesn't imply that there's a proof of 0 = 1. So there can be no proof of 0 = 1, and the assumption that 0 = 1 is a contradiction. We finish the proof by case analysis on the possible proofs of 0 = 1, of which there are zero. So in all (zero!) cases, the conclusion (false) follows. Therefore 0 = 1 → false, which is to say, ¬(0 = 1) is proved and thus true. QED. -/ example : ¬0 = 1 := begin show 0 = 1 → false, -- rewrite goal to a definitionally equal goal assume h, cases h, -- there are *zero* ways to build a proof of 0 = 1 -- case analysis discharges our "proof obligation" end
77f3284e3d51af2bbfd2b6d6fd3ec62ef8d76f2d
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Data/Lsp/Ipc.lean
408999933fb11a216c4008e58487c4c52162026a
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,603
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.System.IO import Lean.Data.Json import Lean.Data.Lsp.Communication import Lean.Data.Lsp.Diagnostics import Lean.Data.Lsp.Extra /-! Provides an IpcM monad for interacting with an external LSP process. Used for testing the Lean server. -/ namespace Lean.Lsp.Ipc open IO open JsonRpc def ipcStdioConfig : Process.StdioConfig where stdin := Process.Stdio.piped stdout := Process.Stdio.piped stderr := Process.Stdio.inherit abbrev IpcM := ReaderT (Process.Child ipcStdioConfig) IO variable [ToJson α] def stdin : IpcM FS.Stream := do FS.Stream.ofHandle (←read).stdin def stdout : IpcM FS.Stream := do FS.Stream.ofHandle (←read).stdout def writeRequest (r : Request α) : IpcM Unit := do (←stdin).writeLspRequest r def writeNotification (n : Notification α) : IpcM Unit := do (←stdin).writeLspNotification n def readMessage : IpcM JsonRpc.Message := do (←stdout).readLspMessage def readResponseAs (expectedID : RequestID) (α) [FromJson α] : IpcM (Response α) := do (←stdout).readLspResponseAs expectedID α def waitForExit : IpcM UInt32 := do (←read).wait /-- Waits for the worker to emit all diagnostics for the current document version and returns them as a list. -/ partial def collectDiagnostics (waitForDiagnosticsId : RequestID := 0) (target : DocumentUri) : IpcM (List (Notification PublishDiagnosticsParams)) := do writeRequest ⟨waitForDiagnosticsId, "textDocument/waitForDiagnostics", WaitForDiagnosticsParam.mk target⟩ let rec loop : IpcM (List (Notification PublishDiagnosticsParams)) := do match ←readMessage with | Message.response id _ => if id == waitForDiagnosticsId then [] else loop | Message.responseError id code msg _ => if id == waitForDiagnosticsId then throw $ userError s!"Waiting for diagnostics failed: {msg}" else loop | Message.notification "textDocument/publishDiagnostics" (some param) => match fromJson? (toJson param) with | some diagnosticParam => ⟨"textDocument/publishDiagnostics", diagnosticParam⟩ :: (←loop) | none => throw $ userError "Cannot decode publishDiagnostics parameters" | _ => loop loop def runWith (cmd : String) (args : Array String := #[]) (test : IpcM α) : IO α := do let proc ← Process.spawn { toStdioConfig := ipcStdioConfig cmd := cmd args := args } ReaderT.run test proc end Lean.Lsp.Ipc
e721297a147d4921dc4c61611cc012e04c0a7d4e
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/record7.lean
64fdd497ba47cd75d8a41b9cb5416ab79f6d0881
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
150
lean
structure point (A : Type) (B : Type) := mk :: (x : A) (y : B) structure point2 (A : Type) (B : Type) extends point A B := make print prefix point2
75d5c5c5edc13332b333a734b210a1a5f686dd61
b32d3853770e6eaf06817a1b8c52064baaed0ef1
/test/clauses_from_env.lean
39c079302b2a5261be2cd899f9af4b3b6231759b
[]
no_license
gebner/super2
4d58b7477b6f7d945d5d866502982466db33ab0b
9bc5256c31750021ab97d6b59b7387773e54b384
refs/heads/master
1,635,021,682,021
1,634,886,326,000
1,634,886,326,000
225,600,688
4
2
null
1,598,209,306,000
1,575,371,550,000
Lean
UTF-8
Lean
false
false
381
lean
import super tactic.core open tactic expr super set_option pp.all true set_option trace.check true -- set_option trace.type_context.is_def_eq true -- set_option trace.type_context.is_def_eq_detail true #eval do e ← get_env, e.get_trusted_decls.mmap' $ λ d, on_exception (trace d.to_name) $ do (prf, ty) ← decl_mk_const d, cls ← clause.of_type_and_proof ty prf, cls.check
9e7751c46630e03cd1d48ad07d5cc3b7b407e5a1
b7b549d2cf38ac9d4e49372b7ad4d37f70449409
/src/LeanLLVM/JITExports.lean
bca730fceb196478fedb5c33251370664ddb06b0
[ "Apache-2.0" ]
permissive
GaloisInc/lean-llvm
7cc196172fe02ff3554edba6cc82f333c30fdc2b
36e2ec604ae22d8ec1b1b66eca0f8887880db6c6
refs/heads/master
1,637,359,020,356
1,629,332,114,000
1,629,402,464,000
146,700,234
29
1
Apache-2.0
1,631,225,695,000
1,535,607,191,000
Lean
UTF-8
Lean
false
false
5,828
lean
#include "llvm_exports.h" #include <lean/io.h> #include <llvm/ExecutionEngine/Orc/CompileUtils.h> #include <llvm/ExecutionEngine/Orc/ExecutionUtils.h> #include <llvm/ExecutionEngine/Orc/IRCompileLayer.h> #include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h> #include <llvm/ExecutionEngine/SectionMemoryManager.h> #include <clang/CodeGen/CodeGenAction.h> #include <clang/Frontend/CompilerInstance.h> using namespace lean; //////////////////////////////////////////////////////////////////////// // Compilation static std::unique_ptr<llvm::Module> compileArgs(llvm::LLVMContext* ctx, b_obj_arg argArrayObj) { size_t argCnt = array_size(argArrayObj); // Copy arguments over const char* args[argCnt]; auto argEnd = args + argCnt; { auto argObjPtr = array_cptr(argArrayObj); for (auto argStrPtr = args; argStrPtr != argEnd; ++argStrPtr, ++argObjPtr) { *argStrPtr = lean::string_cstr(*argObjPtr); } } llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diags(new clang::DiagnosticIDs()); llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts(new clang::DiagnosticOptions()); clang::DiagnosticsEngine diagEngine(diags, diagOpts); std::shared_ptr<clang::CompilerInvocation> invocation(new clang::CompilerInvocation()); clang::CompilerInvocation::CreateFromArgs(*invocation, args, argEnd, diagEngine); // Create the compiler instance clang::CompilerInstance instance; instance.setInvocation(invocation); // Create diagnostics. instance.createDiagnostics(); lean_assert(instance.hasDiagnostics()); // Generate LLVM module using clang. clang::EmitLLVMOnlyAction act(ctx); if (!instance.ExecuteAction(act)) { return 0; } return act.takeModule(); } extern "C" { obj_res lean_llvm_invokeClang(obj_arg ctxObj, b_obj_arg argArrayObj, lean::obj_arg r) { auto ctx = toLLVMContext(ctxObj); auto modPtr = compileArgs(ctx, argArrayObj); if (modPtr == 0) { dec_ref(ctxObj); return set_io_error(r, mk_string("Compilation failed")); } return set_io_result(r, allocModuleObj(ctxObj, std::move(modPtr))); } } //////////////////////////////////////////////////////////////////////// // CompilerSession /** * Provides access to an ORC JIT session. * * The LLVM API makes heavy use of mutable state, and uniquely owned objects that * are not clean Lean objects. We avoid exposing this to Lean by having more code * in the C++ runtime. */ struct CompilerSession { llvm::orc::ExecutionSession es; llvm::orc::RTDyldObjectLinkingLayer objectLayer; llvm::orc::IRCompileLayer compileLayer; llvm::orc::MangleAndInterner mangle; // Context for LLVM modules. llvm::orc::ThreadSafeContext ctx; CompilerSession(const CompilerSession&) = delete; CompilerSession(llvm::orc::JITTargetMachineBuilder& jtmb, llvm::DataLayout& dl) : es(), objectLayer(es, []() { return std::make_unique<llvm::SectionMemoryManager>(); }), compileLayer(es, objectLayer, llvm::orc::ConcurrentIRCompiler(jtmb)), mangle(es, dl), ctx(std::make_unique<llvm::LLVMContext>()) { } }; /** Get compiler session class. */ static external_object_class* compilerSessionClass() { static external_object_class* c = registerDeleteClass<CompilerSession>(); return c; } CompilerSession* getCompilerSession(b_obj_arg o) { lean_assert(external_class(o) == compilerSessionClass()); return static_cast<CompilerSession*>(external_data(o)); } extern "C" { obj_res lean_llvm_newCompilerSession(b_obj_arg tripleObj, obj_arg r) { auto triple = getTriple(tripleObj); llvm::orc::JITTargetMachineBuilder jtmb(*triple); auto tm = jtmb.createTargetMachine(); if (!tm) { return set_io_error(r, errorMsgObj(tm.takeError())); } llvm::DataLayout dl((*tm)->createDataLayout()); auto csession = new CompilerSession(jtmb, dl); auto gen = llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(dl); if (!gen) { return set_io_error(r, errorMsgObj(gen.takeError())); } csession->es.getMainJITDylib().setGenerator(*gen); auto sessObj = alloc_external(compilerSessionClass(), csession); return set_io_result(r, sessObj); } obj_res lean_llvm_addFromClangCompile(b_obj_arg compSessObj, b_obj_arg argArrayObj, lean::obj_arg r) { auto csession = getCompilerSession(compSessObj); llvm::LLVMContext* ctx = csession->ctx.getContext(); auto modPtr = compileArgs(ctx, argArrayObj); if (modPtr == 0) { return set_io_error(r, mk_string("Compilation failed")); } llvm::orc::ExecutionSession& es = csession->es; llvm::orc::JITDylib& dylib = es.getMainJITDylib(); llvm::orc::ThreadSafeModule tsm(std::move(modPtr), csession->ctx); llvm::Error e = csession->compileLayer.add(dylib, std::move(tsm)); if (e) { return set_io_error(r, errorMsgObj(std::move(e))); } return set_io_result(r, box(0)); } obj_res lean_llvm_lookupFn(b_obj_arg compSessObj, b_obj_arg symNameObj, b_obj_arg tpObj, lean::obj_arg r) { auto csession = getCompilerSession(compSessObj); llvm::StringRef symName(string_cstr(symNameObj)); llvm::orc::ExecutionSession& es = csession->es; llvm::orc::MangleAndInterner& mangle = csession->mangle; llvm::orc::SymbolStringPtr symPtr = mangle(symName); llvm::orc::JITDylib* dylib = &es.getMainJITDylib(); dylib->dump(llvm::errs()); llvm::Expected<llvm::JITEvaluatedSymbol> sym = es.lookup({dylib}, symPtr); if (!sym) { return set_io_error(r, errorMsgObj(sym.takeError())); } inc_ref(compSessObj); void* addr = reinterpret_cast<void*>(sym->getAddress()); std::cerr << "Addr " << addr << std::endl; obj_res f = alloc_closure(addr, 2, 0); return set_io_result(r, f); } }
759a12992b96c1fe31d796a11a48bee25147360d
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/algebra/with_zero_topology.lean
a7589975c91ae2099399f0e0da6b411f0490afe4
[ "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
12,121
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import algebra.order.with_zero import topology.algebra.order.basic /-! # The topology on linearly ordered commutative groups with zero Let `Γ₀` be a linearly ordered commutative group to which we have adjoined a zero element. Then `Γ₀` may naturally be endowed with a topology that turns `Γ₀` into a topological monoid. Neighborhoods of zero are sets containing `{γ | γ < γ₀}` for some invertible element `γ₀` and every invertible element is open. In particular the topology is the following: "a subset `U ⊆ Γ₀` is open if `0 ∉ U` or if there is an invertible `γ₀ ∈ Γ₀ such that {γ | γ < γ₀} ⊆ U`", but this fact is not proven here since the neighborhoods description is what is actually useful. We prove this topology is ordered and regular (in addition to be compatible with the monoid structure). All this is useful to extend a valuation to a completion. This is an abstract version of how the absolute value (resp. `p`-adic absolute value) on `ℚ` is extended to `ℝ` (resp. `ℚₚ`). ## Implementation notes This topology is not defined as an instance since it may not be the desired topology on a linearly ordered commutative group with zero. You can locally activate this topology using `local attribute [instance] linear_ordered_comm_group_with_zero.topological_space` All other instances will (`ordered_topology`, `regular_space`, `has_continuous_mul`) then follow. -/ open_locale topological_space open topological_space filter set namespace linear_ordered_comm_group_with_zero variables (Γ₀ : Type*) [linear_ordered_comm_group_with_zero Γ₀] /-- The neighbourhoods around γ ∈ Γ₀, used in the definition of the topology on Γ₀. These neighbourhoods are defined as follows: A set s is a neighbourhood of 0 if there is an invertible γ₀ ∈ Γ₀ such that {γ | γ < γ₀} ⊆ s. If γ ≠ 0, then every set that contains γ is a neighbourhood of γ. -/ def nhds_fun (x : Γ₀) : filter Γ₀ := if x = 0 then ⨅ (γ₀ : Γ₀ˣ), principal {γ | γ < γ₀} else pure x /-- The topology on a linearly ordered commutative group with a zero element adjoined. A subset U is open if 0 ∉ U or if there is an invertible element γ₀ such that {γ | γ < γ₀} ⊆ U. -/ protected def topological_space : topological_space Γ₀ := topological_space.mk_of_nhds (nhds_fun Γ₀) local attribute [instance] linear_ordered_comm_group_with_zero.topological_space /-- The neighbourhoods {γ | γ < γ₀} of 0 form a directed set indexed by the invertible elements γ₀. -/ lemma directed_lt : directed (≥) (λ γ₀ : Γ₀ˣ, principal {γ : Γ₀ | γ < γ₀}) := begin intros γ₁ γ₂, use linear_order.min γ₁ γ₂ ; dsimp only, split ; rw [ge_iff_le, principal_mono] ; intros x x_in, { calc x < ↑(linear_order.min γ₁ γ₂) : x_in ... ≤ γ₁ : min_le_left γ₁ γ₂ }, { calc x < ↑(linear_order.min γ₁ γ₂) : x_in ... ≤ γ₂ : min_le_right γ₁ γ₂ } end -- We need two auxilliary lemmas to show that nhds_fun accurately describes the neighbourhoods -- coming from the topology (that is defined in terms of nhds_fun). /-- At all points of a linearly ordered commutative group with a zero element adjoined, the pure filter is smaller than the filter given by nhds_fun. -/ lemma pure_le_nhds_fun : pure ≤ nhds_fun Γ₀ := λ x, by { by_cases hx : x = 0; simp [hx, nhds_fun] } /-- For every point Γ₀, and every “neighbourhood” s of it (described by nhds_fun), there is a smaller “neighbourhood” t ⊆ s, such that s is a “neighbourhood“ of all the points in t. -/ lemma nhds_fun_ok (x : Γ₀) {s} (s_in : s ∈ nhds_fun Γ₀ x) : (∃ t ∈ nhds_fun Γ₀ x, t ⊆ s ∧ ∀ y ∈ t, s ∈ nhds_fun Γ₀ y) := begin by_cases hx : x = 0, { simp only [hx, nhds_fun, exists_prop, if_true, eq_self_iff_true] at s_in ⊢, cases (mem_infi_of_directed (directed_lt Γ₀) _).mp s_in with γ₀ h, use {γ : Γ₀ | γ < γ₀}, rw mem_principal at h, split, { apply mem_infi_of_mem γ₀, rw mem_principal }, { refine ⟨h, λ y y_in, _⟩, by_cases hy : y = 0, { simp only [hy, if_true, eq_self_iff_true], apply mem_infi_of_mem γ₀, rwa mem_principal }, { simp [hy, h y_in] } } }, { simp only [hx, nhds_fun, exists_prop, if_false, mem_pure] at s_in ⊢, refine ⟨{x}, mem_singleton _, singleton_subset_iff.2 s_in, λ y y_in, _⟩, simpa [mem_singleton_iff.mp y_in, hx] } end variables {Γ₀} /-- The neighbourhood filter of an invertible element consists of all sets containing that element. -/ lemma nhds_coe_units (γ : Γ₀ˣ) : 𝓝 (γ : Γ₀) = pure (γ : Γ₀) := calc 𝓝 (γ : Γ₀) = nhds_fun Γ₀ γ : nhds_mk_of_nhds (nhds_fun Γ₀) γ (pure_le_nhds_fun Γ₀) (nhds_fun_ok Γ₀) ... = pure (γ : Γ₀) : if_neg γ.ne_zero /-- The neighbourhood filter of a nonzero element consists of all sets containing that element. -/ @[simp] lemma nhds_of_ne_zero (γ : Γ₀) (h : γ ≠ 0) : 𝓝 γ = pure γ := nhds_coe_units (units.mk0 _ h) /-- If γ is an invertible element of a linearly ordered group with zero element adjoined, then {γ} is a neighbourhood of γ. -/ lemma singleton_nhds_of_units (γ : Γ₀ˣ) : ({γ} : set Γ₀) ∈ 𝓝 (γ : Γ₀) := by simp /-- If γ is a nonzero element of a linearly ordered group with zero element adjoined, then {γ} is a neighbourhood of γ. -/ lemma singleton_nhds_of_ne_zero (γ : Γ₀) (h : γ ≠ 0) : ({γ} : set Γ₀) ∈ 𝓝 (γ : Γ₀) := by simp [h] /-- If U is a neighbourhood of 0 in a linearly ordered group with zero element adjoined, then there exists an invertible element γ₀ such that {γ | γ < γ₀} ⊆ U. -/ lemma has_basis_nhds_zero : has_basis (𝓝 (0 : Γ₀)) (λ _, true) (λ γ₀ : Γ₀ˣ, {γ : Γ₀ | γ < γ₀}) := ⟨begin intro U, rw nhds_mk_of_nhds (nhds_fun Γ₀) 0 (pure_le_nhds_fun Γ₀) (nhds_fun_ok Γ₀), simp only [nhds_fun, if_true, eq_self_iff_true, exists_true_left], simp_rw [mem_infi_of_directed (directed_lt Γ₀), mem_principal] end⟩ /-- If γ is an invertible element of a linearly ordered group with zero element adjoined, then {x | x < γ} is a neighbourhood of 0. -/ lemma nhds_zero_of_units (γ : Γ₀ˣ) : {x : Γ₀ | x < γ} ∈ 𝓝 (0 : Γ₀) := by { rw has_basis_nhds_zero.mem_iff, use γ, simp } lemma tendsto_zero {α : Type*} {F : filter α} {f : α → Γ₀} : tendsto f F (𝓝 (0 : Γ₀)) ↔ ∀ γ₀ : Γ₀ˣ, { x : α | f x < γ₀ } ∈ F := by simpa using has_basis_nhds_zero.tendsto_right_iff /-- If γ is a nonzero element of a linearly ordered group with zero element adjoined, then {x | x < γ} is a neighbourhood of 0. -/ lemma nhds_zero_of_ne_zero (γ : Γ₀) (h : γ ≠ 0) : {x : Γ₀ | x < γ} ∈ 𝓝 (0 : Γ₀) := nhds_zero_of_units (units.mk0 _ h) lemma has_basis_nhds_units (γ : Γ₀ˣ) : has_basis (𝓝 (γ : Γ₀)) (λ i : unit, true) (λ i, {γ}) := begin rw nhds_of_ne_zero _ γ.ne_zero, exact has_basis_pure γ end lemma has_basis_nhds_of_ne_zero {x : Γ₀} (h : x ≠ 0) : has_basis (𝓝 x) (λ i : unit, true) (λ i, {x}) := has_basis_nhds_units (units.mk0 x h) lemma singleton_mem_nhds_of_ne_zero {x : Γ₀} (h : x ≠ 0) : {x} ∈ 𝓝 x := begin apply (has_basis_nhds_of_ne_zero h).mem_of_mem true.intro, exact unit.star, end lemma tendsto_units {α : Type*} {F : filter α} {f : α → Γ₀} {γ₀ : Γ₀ˣ} : tendsto f F (𝓝 (γ₀ : Γ₀)) ↔ { x : α | f x = γ₀ } ∈ F := begin rw (has_basis_nhds_units γ₀).tendsto_right_iff, simpa end lemma tendsto_of_ne_zero {α : Type*} {F : filter α} {f : α → Γ₀} {γ : Γ₀} (h : γ ≠ 0) : tendsto f F (𝓝 γ) ↔ { x : α | f x = γ } ∈ F := @tendsto_units _ _ _ F f (units.mk0 γ h) variable (Γ₀) /-- The topology on a linearly ordered group with zero element adjoined is compatible with the order structure. -/ @[priority 100] instance ordered_topology : order_closed_topology Γ₀ := { is_closed_le' := begin rw ← is_open_compl_iff, show is_open {p : Γ₀ × Γ₀ | ¬p.fst ≤ p.snd}, simp only [not_le], rw is_open_iff_mem_nhds, rintros ⟨a,b⟩ hab, change b < a at hab, have ha : a ≠ 0 := ne_zero_of_lt hab, rw [nhds_prod_eq, mem_prod_iff], by_cases hb : b = 0, { subst b, use [{a}, singleton_nhds_of_ne_zero _ ha, {x : Γ₀ | x < a}, nhds_zero_of_ne_zero _ ha], intros p p_in, cases mem_prod.1 p_in with h1 h2, rw mem_singleton_iff at h1, change p.2 < p.1, rwa h1 }, { use [{a}, singleton_nhds_of_ne_zero _ ha, {b}, singleton_nhds_of_ne_zero _ hb], intros p p_in, cases mem_prod.1 p_in with h1 h2, rw mem_singleton_iff at h1 h2, change p.2 < p.1, rwa [h1, h2] } end } /-- The topology on a linearly ordered group with zero element adjoined is T₃ (aka regular). -/ @[priority 100] instance regular_space : regular_space Γ₀ := begin haveI : t1_space Γ₀ := t2_space.t1_space, split, intros s x s_closed x_not_in_s, by_cases hx : x = 0, { refine ⟨s, _, subset.rfl, _⟩, { subst x, rw is_open_iff_mem_nhds, intros y hy, by_cases hy' : y = 0, { subst y, contradiction }, simpa [hy'] }, { erw inf_eq_bot_iff, use sᶜ, simp only [exists_prop, mem_principal], exact ⟨s_closed.compl_mem_nhds x_not_in_s, ⟨s, subset.refl s, by simp⟩⟩ } }, { simp only [nhds_within, inf_eq_bot_iff, exists_prop, mem_principal], exact ⟨{x}ᶜ, is_open_compl_iff.mpr is_closed_singleton, by rwa subset_compl_singleton_iff, {x}, singleton_nhds_of_ne_zero x hx, {x}ᶜ, by simp [subset.refl]⟩ } end /-- The topology on a linearly ordered group with zero element adjoined makes it a topological monoid. -/ @[priority 100] instance : has_continuous_mul Γ₀ := ⟨begin have common : ∀ y ≠ (0 : Γ₀), continuous_at (λ (p : Γ₀ × Γ₀), p.fst * p.snd) (0, y), { intros y hy, set γ := units.mk0 y hy, suffices : tendsto (λ (p : Γ₀ × Γ₀), p.fst * p.snd) ((𝓝 0).prod (𝓝 γ)) (𝓝 0), by simpa [continuous_at, nhds_prod_eq], suffices : ∀ (γ' : Γ₀ˣ), ∃ (γ'' : Γ₀ˣ), ∀ (a b : Γ₀), a < γ'' → b = y → a * b < γ', { rw (has_basis_nhds_zero.prod $ has_basis_nhds_units γ).tendsto_iff has_basis_nhds_zero, simpa }, intros γ', use γ⁻¹*γ', rintros a b ha hb, rw [hb, mul_comm], rw [units.coe_mul] at ha, simpa using inv_mul_lt_of_lt_mul₀ ha }, rw continuous_iff_continuous_at, rintros ⟨x, y⟩, by_cases hx : x = 0; by_cases hy : y = 0, { suffices : tendsto (λ (p : Γ₀ × Γ₀), p.fst * p.snd) (𝓝 (0, 0)) (𝓝 0), by simpa [hx, hy, continuous_at], suffices : ∀ (γ : Γ₀ˣ), ∃ (γ' : Γ₀ˣ), ∀ (a b : Γ₀), a < γ' → b < γ' → a * b < γ, by simpa [nhds_prod_eq, has_basis_nhds_zero.prod_self.tendsto_iff has_basis_nhds_zero], intros γ, rcases exists_square_le γ with ⟨γ', h⟩, use γ', intros a b ha hb, calc a*b < γ'*γ' : mul_lt_mul₀ ha hb ... ≤ γ : by exact_mod_cast h }, { rw hx, exact common y hy }, { rw hy, have : (λ (p : Γ₀ × Γ₀), p.fst * p.snd) = (λ (p : Γ₀ × Γ₀), p.fst * p.snd) ∘ (λ p : Γ₀ × Γ₀, (p.2, p.1)), by { ext, rw [mul_comm] }, rw this, apply continuous_at.comp _ continuous_swap.continuous_at, exact common x hx }, { change tendsto _ _ _, rw [nhds_prod_eq], rw ((has_basis_nhds_of_ne_zero hx).prod (has_basis_nhds_of_ne_zero hy)).tendsto_iff (has_basis_nhds_of_ne_zero $ mul_ne_zero hx hy), suffices : ∀ (a b : Γ₀), a = x → b = y → a * b = x * y, by simpa, rintros a b rfl rfl, refl }, end⟩ end linear_ordered_comm_group_with_zero
ce1b9d5c8324e3c3855e6fa81b2da62dc636a4a4
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/char_p/basic.lean
aa3017f32bdee682d4f9e5511833b8c93763c3aa
[ "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
21,907
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Joey van Langen, Casper Putz -/ import algebra.hom.iterate import data.int.modeq import data.nat.choose.dvd import data.nat.choose.sum import data.zmod.defs import group_theory.order_of_element import ring_theory.nilpotent /-! # Characteristic of semirings -/ universes u v variables (R : Type u) /-- The generator of the kernel of the unique homomorphism ℕ → R for a semiring R. *Warning*: for a semiring `R`, `char_p R 0` and `char_zero R` need not coincide. * `char_p R 0` asks that only `0 : ℕ` maps to `0 : R` under the map `ℕ → R`; * `char_zero R` requires an injection `ℕ ↪ R`. For instance, endowing `{0, 1}` with addition given by `max` (i.e. `1` is absorbing), shows that `char_zero {0, 1}` does not hold and yet `char_p {0, 1} 0` does. This example is formalized in `counterexamples/char_p_zero_ne_char_zero`. -/ @[mk_iff] class char_p [add_monoid_with_one R] (p : ℕ) : Prop := (cast_eq_zero_iff [] : ∀ x:ℕ, (x:R) = 0 ↔ p ∣ x) theorem char_p.cast_eq_zero [add_monoid_with_one R] (p : ℕ) [char_p R p] : (p:R) = 0 := (char_p.cast_eq_zero_iff R p p).2 (dvd_refl p) @[simp] lemma char_p.cast_card_eq_zero [add_group_with_one R] [fintype R] : (fintype.card R : R) = 0 := by rw [← nsmul_one, card_nsmul_eq_zero] lemma char_p.add_order_of_one (R) [semiring R] : char_p R (add_order_of (1 : R)) := ⟨λ n, by rw [← nat.smul_one_eq_coe, add_order_of_dvd_iff_nsmul_eq_zero]⟩ lemma char_p.int_cast_eq_zero_iff [add_group_with_one R] (p : ℕ) [char_p R p] (a : ℤ) : (a : R) = 0 ↔ (p:ℤ) ∣ a := begin rcases lt_trichotomy a 0 with h|rfl|h, { rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg], lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }, { simp only [int.cast_zero, eq_self_iff_true, dvd_zero] }, { lift a to ℕ using (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] } end lemma char_p.int_coe_eq_int_coe_iff [add_group_with_one R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = (b : R) ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq_iff_dvd] theorem char_p.eq [add_monoid_with_one R] {p q : ℕ} (c1 : char_p R p) (c2 : char_p R q) : p = q := nat.dvd_antisymm ((char_p.cast_eq_zero_iff R p q).1 (char_p.cast_eq_zero _ _)) ((char_p.cast_eq_zero_iff R q p).1 (char_p.cast_eq_zero _ _)) instance char_p.of_char_zero [add_monoid_with_one R] [char_zero R] : char_p R 0 := ⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩ theorem char_p.exists [non_assoc_semiring R] : ∃ p, char_p R p := by letI := classical.dec_eq R; exact classical.by_cases (assume H : ∀ p:ℕ, (p:R) = 0 → p = 0, ⟨0, ⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; simp⟩⟩⟩) (λ H, ⟨nat.find (not_forall.1 H), ⟨λ x, ⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2, nat.find_min (not_forall.1 H) (nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $ nat.find_spec (not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (not_forall.1 H)), nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul]⟩⟩⟩) theorem char_p.exists_unique [non_assoc_semiring R] : ∃! p, char_p R p := let ⟨c, H⟩ := char_p.exists R in ⟨c, H, λ y H2, char_p.eq R H2 H⟩ theorem char_p.congr {R : Type u} [add_monoid_with_one R] {p : ℕ} (q : ℕ) [hq : char_p R q] (h : q = p) : char_p R p := h ▸ hq /-- Noncomputable function that outputs the unique characteristic of a semiring. -/ noncomputable def ring_char [non_assoc_semiring R] : ℕ := classical.some (char_p.exists_unique R) namespace ring_char variables [non_assoc_semiring R] theorem spec : ∀ x:ℕ, (x:R) = 0 ↔ ring_char R ∣ x := by letI := (classical.some_spec (char_p.exists_unique R)).1; unfold ring_char; exact char_p.cast_eq_zero_iff R (ring_char R) theorem eq (p : ℕ) [C : char_p R p] : ring_char R = p := ((classical.some_spec (char_p.exists_unique R)).2 p C).symm instance char_p : char_p R (ring_char R) := ⟨spec R⟩ variables {R} theorem of_eq {p : ℕ} (h : ring_char R = p) : char_p R p := char_p.congr (ring_char R) h theorem eq_iff {p : ℕ} : ring_char R = p ↔ char_p R p := ⟨of_eq, @eq R _ p⟩ theorem dvd {x : ℕ} (hx : (x : R) = 0) : ring_char R ∣ x := (spec R x).1 hx @[simp] lemma eq_zero [char_zero R] : ring_char R = 0 := eq R 0 @[simp] lemma nat.cast_ring_char : (ring_char R : R) = 0 := by rw ring_char.spec end ring_char theorem add_pow_char_of_commute [semiring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x + y)^p = x^p + y^p := begin rw [commute.add_pow h, finset.sum_range_succ_comm, tsub_self, pow_zero, nat.choose_self], rw [nat.cast_one, mul_one, mul_one], congr' 1, convert finset.sum_eq_single 0 _ _, { simp only [mul_one, one_mul, nat.choose_zero_right, tsub_zero, nat.cast_one, pow_zero] }, { intros b h1 h2, suffices : (p.choose b : R) = 0, { rw this, simp }, rw char_p.cast_eq_zero_iff R p, refine nat.prime.dvd_choose_self (pos_iff_ne_zero.mpr h2) _ (fact.out _), rwa ← finset.mem_range }, { intro h1, contrapose! h1, rw finset.mem_range, exact nat.prime.pos (fact.out _) } end theorem add_pow_char_pow_of_commute [semiring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply add_pow_char_of_commute, apply commute.pow_pow h, end theorem sub_pow_char_of_commute [ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x - y)^p = x^p - y^p := begin rw [eq_sub_iff_add_eq, ← add_pow_char_of_commute _ _ _ (commute.sub_left h rfl)], simp, repeat {apply_instance}, end theorem sub_pow_char_pow_of_commute [ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply sub_pow_char_of_commute, apply commute.pow_pow h, end theorem add_pow_char [comm_semiring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) : (x + y)^p = x^p + y^p := add_pow_char_of_commute _ _ _ (commute.all _ _) theorem add_pow_char_pow [comm_semiring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := add_pow_char_pow_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) : (x - y)^p = x^p - y^p := sub_pow_char_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char_pow [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := sub_pow_char_pow_of_commute _ _ _ (commute.all _ _) lemma eq_iff_modeq_int [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq_iff_dvd] lemma char_p.neg_one_ne_one [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] : (-1 : R) ≠ (1 : R) := begin suffices : (2 : R) ≠ 0, { symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this }, assume h, rw [show (2 : R) = (2 : ℕ), by norm_cast] at h, have := (char_p.cast_eq_zero_iff R p 2).mp h, have := nat.le_of_dvd dec_trivial this, rw fact_iff at *, linarith, end lemma char_p.neg_one_pow_char [comm_ring R] (p : ℕ) [char_p R p] [fact p.prime] : (-1 : R) ^ p = -1 := begin rw eq_neg_iff_add_eq_zero, nth_rewrite 1 ← one_pow p, rw [← add_pow_char, add_left_neg, zero_pow (fact.out (nat.prime p)).pos], end lemma char_p.neg_one_pow_char_pow [comm_ring R] (p n : ℕ) [char_p R p] [fact p.prime] : (-1 : R) ^ p ^ n = -1 := begin rw eq_neg_iff_add_eq_zero, nth_rewrite 1 ← one_pow (p ^ n), rw [← add_pow_char_pow, add_left_neg, zero_pow (pow_pos (fact.out (nat.prime p)).pos _)], end lemma ring_hom.char_p_iff_char_p {K L : Type*} [division_ring K] [semiring L] [nontrivial L] (f : K →+* L) (p : ℕ) : char_p K p ↔ char_p L p := by simp only [char_p_iff, ← f.injective.eq_iff, map_nat_cast f, f.map_zero] section frobenius section comm_semiring variables [comm_semiring R] {S : Type v} [comm_semiring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) /-- The frobenius map that sends x to x^p -/ def frobenius : R →+* R := { to_fun := λ x, x^p, map_one' := one_pow p, map_mul' := λ x y, mul_pow x y p, map_zero' := zero_pow (fact.out (nat.prime p)).pos, map_add' := add_pow_char R } variable {R} theorem frobenius_def : frobenius R p x = x ^ p := rfl theorem iterate_frobenius (n : ℕ) : (frobenius R p)^[n] x = x ^ p ^ n := begin induction n, {simp}, rw [function.iterate_succ', pow_succ', pow_mul, function.comp_apply, frobenius_def, n_ih] end theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := (frobenius R p).map_mul x y theorem frobenius_one : frobenius R p 1 = 1 := one_pow _ theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := f.map_pow x p theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := g.map_pow x p theorem monoid_hom.map_iterate_frobenius (n : ℕ) : f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) := function.semiconj.iterate_right (f.map_frobenius p) n x theorem ring_hom.map_iterate_frobenius (n : ℕ) : g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) := g.to_monoid_hom.map_iterate_frobenius p x n theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ variable (R) theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := map_nat_cast (frobenius R p) n open_locale big_operators variables {R} lemma list_sum_pow_char (l : list R) : l.sum ^ p = (l.map (^ p)).sum := (frobenius R p).map_list_sum _ lemma multiset_sum_pow_char (s : multiset R) : s.sum ^ p = (s.map (^ p)).sum := (frobenius R p).map_multiset_sum _ lemma sum_pow_char {ι : Type*} (s : finset ι) (f : ι → R) : (∑ i in s, f i) ^ p = ∑ i in s, f i ^ p := (frobenius R p).map_sum _ _ end comm_semiring section comm_ring variables [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := (frobenius R p).map_sub x y end comm_ring end frobenius theorem frobenius_inj [comm_ring R] [is_reduced R] (p : ℕ) [fact p.prime] [char_p R p] : function.injective (frobenius R p) := λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact is_reduced.eq_zero _ ⟨_,H⟩ } /-- If `ring_char R = 2`, where `R` is a finite reduced commutative ring, then every `a : R` is a square. -/ lemma is_square_of_char_two' {R : Type*} [finite R] [comm_ring R] [is_reduced R] [char_p R 2] (a : R) : is_square a := by { casesI nonempty_fintype R, exact exists_imp_exists (λ b h, pow_two b ▸ eq.symm h) (((fintype.bijective_iff_injective_and_card _).mpr ⟨frobenius_inj R 2, rfl⟩).surjective a) } namespace char_p section variables [non_assoc_ring R] lemma char_p_to_char_zero (R : Type*) [add_group_with_one R] [char_p R 0] : char_zero R := char_zero_of_inj_zero $ λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff R 0 n).mp h0) lemma cast_eq_mod (p : ℕ) [char_p R p] (k : ℕ) : (k : R) = (k % p : ℕ) := calc (k : R) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div] ... = ↑(k % p) : by simp [cast_eq_zero] /-- The characteristic of a finite ring cannot be zero. -/ theorem char_ne_zero_of_finite (p : ℕ) [char_p R p] [finite R] : p ≠ 0 := begin unfreezingI { rintro rfl }, haveI : char_zero R := char_p_to_char_zero R, casesI nonempty_fintype R, exact absurd nat.cast_injective (not_injective_infinite_finite (coe : ℕ → R)) end lemma ring_char_ne_zero_of_finite [finite R] : ring_char R ≠ 0 := char_ne_zero_of_finite R (ring_char R) end section comm_ring variables [comm_ring R] [is_reduced R] {R} @[simp] lemma pow_prime_pow_mul_eq_one_iff (p k m : ℕ) [fact p.prime] [char_p R p] (x : R) : x ^ (p ^ k * m) = 1 ↔ x ^ m = 1 := begin induction k with k hk, { rw [pow_zero, one_mul] }, { refine ⟨λ h, _, λ h, _⟩, { rw [pow_succ, mul_assoc, pow_mul', ← frobenius_def, ← frobenius_one p] at h, exact hk.1 (frobenius_inj R p h) }, { rw [pow_mul', h, one_pow] } } end end comm_ring section semiring open nat variables [non_assoc_semiring R] theorem char_ne_one [nontrivial R] (p : ℕ) [hc : char_p R p] : p ≠ 1 := assume hp : p = 1, have ( 1 : R) = 0, by simpa using (cast_eq_zero_iff R p 1).mpr (hp ▸ dvd_refl p), absurd this one_ne_zero section no_zero_divisors variable [no_zero_divisors R] theorem char_is_prime_of_two_le (p : ℕ) [hc : char_p R p] (hp : 2 ≤ p) : nat.prime p := suffices ∀d ∣ p, d = 1 ∨ d = p, from nat.prime_def_lt''.mpr ⟨hp, this⟩, assume (d : ℕ) (hdvd : ∃ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : R) = 0, from (cast_eq_zero_iff R p p).mpr (dvd_refl p), have (d : R) * e = 0, from (@cast_mul R _ d e) ▸ (hmul ▸ this), or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (assume hd : (d : R) = 0, have p ∣ d, from (cast_eq_zero_iff R p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : R) = 0, have p ∣ e, from (cast_eq_zero_iff R p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›, have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this)) section nontrivial variables [nontrivial R] theorem char_is_prime_or_zero (p : ℕ) [hc : char_p R p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one R _ _ (1 : ℕ) hc) | (m+2), hc := or.inl (@char_is_prime_of_two_le R _ _ (m+2) hc (nat.le_add_left 2 m)) end lemma char_is_prime_of_pos (p : ℕ) [ne_zero p] [char_p R p] : fact p.prime := ⟨(char_p.char_is_prime_or_zero R _).resolve_right $ ne_zero.ne p⟩ end nontrivial end no_zero_divisors end semiring section ring variables (R) [ring R] [no_zero_divisors R] [nontrivial R] [finite R] theorem char_is_prime (p : ℕ) [char_p R p] : p.prime := or.resolve_right (char_is_prime_or_zero R p) (char_ne_zero_of_finite R p) end ring section char_one variables {R} [non_assoc_semiring R] @[priority 100] -- see Note [lower instance priority] instance [char_p R 1] : subsingleton R := subsingleton.intro $ suffices ∀ (r : R), r = 0, from assume a b, show a = b, by rw [this a, this b], assume r, calc r = 1 * r : by rw one_mul ... = (1 : ℕ) * r : by rw nat.cast_one ... = 0 * r : by rw char_p.cast_eq_zero ... = 0 : by rw zero_mul lemma false_of_nontrivial_of_char_one [nontrivial R] [char_p R 1] : false := false_of_nontrivial_of_subsingleton R lemma ring_char_ne_one [nontrivial R] : ring_char R ≠ 1 := by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], } lemma nontrivial_of_char_ne_one {v : ℕ} (hv : v ≠ 1) [hr : char_p R v] : nontrivial R := ⟨⟨(1 : ℕ), 0, λ h, hv $ by rwa [char_p.cast_eq_zero_iff _ v, nat.dvd_one] at h; assumption ⟩⟩ lemma ring_char_of_prime_eq_zero [nontrivial R] {p : ℕ} (hprime : nat.prime p) (hp0 : (p : R) = 0) : ring_char R = p := or.resolve_left ((nat.dvd_prime hprime).1 (ring_char.dvd hp0)) ring_char_ne_one end char_one end char_p section /-- We have `2 ≠ 0` in a nontrivial ring whose characteristic is not `2`. -/ -- Note: there is `two_ne_zero` (assuming `[ordered_semiring]`) -- and `two_ne_zero'`(assuming `[char_zero]`), which both don't fit the needs here. @[protected] lemma ring.two_ne_zero {R : Type*} [non_assoc_semiring R] [nontrivial R] (hR : ring_char R ≠ 2) : (2 : R) ≠ 0 := begin rw [ne.def, (by norm_cast : (2 : R) = (2 : ℕ)), ring_char.spec, nat.dvd_prime nat.prime_two], exact mt (or_iff_left hR).mp char_p.ring_char_ne_one, end /-- Characteristic `≠ 2` and nontrivial implies that `-1 ≠ 1`. -/ -- We have `char_p.neg_one_ne_one`, which assumes `[ring R] (p : ℕ) [char_p R p] [fact (2 < p)]`. -- This is a version using `ring_char` instead. lemma ring.neg_one_ne_one_of_char_ne_two {R : Type*} [non_assoc_ring R] [nontrivial R] (hR : ring_char R ≠ 2) : (-1 : R) ≠ 1 := λ h, ring.two_ne_zero hR (neg_eq_iff_add_eq_zero.mp h) /-- Characteristic `≠ 2` in a domain implies that `-a = a` iff `a = 0`. -/ lemma ring.eq_self_iff_eq_zero_of_char_ne_two {R : Type*} [non_assoc_ring R] [nontrivial R] [no_zero_divisors R] (hR : ring_char R ≠ 2) {a : R} : -a = a ↔ a = 0 := ⟨λ h, (mul_eq_zero.mp $ (two_mul a).trans $ neg_eq_iff_add_eq_zero.mp h).resolve_left (ring.two_ne_zero hR), λ h, ((congr_arg (λ x, - x) h).trans neg_zero).trans h.symm⟩ end section variables (R) [non_assoc_ring R] [fintype R] (n : ℕ) lemma char_p_of_ne_zero (hn : fintype.card R = n) (hR : ∀ i < n, (i : R) = 0 → i = 0) : char_p R n := { cast_eq_zero_iff := begin have H : (n : R) = 0, by { rw [← hn, char_p.cast_card_eq_zero] }, intro k, split, { intro h, rw [← nat.mod_add_div k n, nat.cast_add, nat.cast_mul, H, zero_mul, add_zero] at h, rw nat.dvd_iff_mod_eq_zero, apply hR _ (nat.mod_lt _ _) h, rw [← hn, fintype.card_pos_iff], exact ⟨0⟩, }, { rintro ⟨k, rfl⟩, rw [nat.cast_mul, H, zero_mul] } end } lemma char_p_of_prime_pow_injective (R) [ring R] [fintype R] (p : ℕ) [hp : fact p.prime] (n : ℕ) (hn : fintype.card R = p ^ n) (hR : ∀ i ≤ n, (p ^ i : R) = 0 → i = n) : char_p R (p ^ n) := begin obtain ⟨c, hc⟩ := char_p.exists R, resetI, have hcpn : c ∣ p ^ n, { rw [← char_p.cast_eq_zero_iff R c, ← hn, char_p.cast_card_eq_zero], }, obtain ⟨i, hi, hc⟩ : ∃ i ≤ n, c = p ^ i, by rwa nat.dvd_prime_pow hp.1 at hcpn, obtain rfl : i = n, { apply hR i hi, rw [← nat.cast_pow, ← hc, char_p.cast_eq_zero] }, rwa ← hc end end section prod variables (S : Type v) [semiring R] [semiring S] (p q : ℕ) [char_p R p] /-- The characteristic of the product of rings is the least common multiple of the characteristics of the two rings. -/ instance [char_p S q] : char_p (R × S) (nat.lcm p q) := { cast_eq_zero_iff := by simp [prod.ext_iff, char_p.cast_eq_zero_iff R p, char_p.cast_eq_zero_iff S q, nat.lcm_dvd_iff] } /-- The characteristic of the product of two rings of the same characteristic is the same as the characteristic of the rings -/ instance prod.char_p [char_p S p] : char_p (R × S) p := by convert nat.lcm.char_p R S p p; simp end prod section /-- If two integers from `{0, 1, -1}` result in equal elements in a ring `R` that is nontrivial and of characteristic not `2`, then they are equal. -/ lemma int.cast_inj_on_of_ring_char_ne_two {R : Type*} [non_assoc_ring R] [nontrivial R] (hR : ring_char R ≠ 2) : ({0, 1, -1} : set ℤ).inj_on (coe : ℤ → R) := begin intros a ha b hb h, apply eq_of_sub_eq_zero, by_contra hf, change a = 0 ∨ a = 1 ∨ a = -1 at ha, change b = 0 ∨ b = 1 ∨ b = -1 at hb, have hh : a - b = 1 ∨ b - a = 1 ∨ a - b = 2 ∨ b - a = 2 := by { rcases ha with ha | ha | ha; rcases hb with hb | hb | hb, swap 5, swap 9, -- move goals with `a = b` to the front iterate 3 { rw [ha, hb, sub_self] at hf, tauto, }, -- 6 goals remain all_goals { rw [ha, hb], norm_num, }, }, have h' : ((a - b : ℤ) : R) = 0 := by exact_mod_cast sub_eq_zero_of_eq h, have h'' : ((b - a : ℤ) : R) = 0 := by exact_mod_cast sub_eq_zero_of_eq h.symm, rcases hh with hh | hh | hh | hh, { rw [hh, (by norm_cast : ((1 : ℤ) : R) = 1)] at h', exact one_ne_zero h', }, { rw [hh, (by norm_cast : ((1 : ℤ) : R) = 1)] at h'', exact one_ne_zero h'', }, { rw [hh, (by norm_cast : ((2 : ℤ) : R) = 2)] at h', exact ring.two_ne_zero hR h', }, { rw [hh, (by norm_cast : ((2 : ℤ) : R) = 2)] at h'', exact ring.two_ne_zero hR h'', }, end end namespace ne_zero variables (R) [add_monoid_with_one R] {r : R} {n p : ℕ} {a : ℕ+} lemma of_not_dvd [char_p R p] (h : ¬ p ∣ n) : ne_zero (n : R) := ⟨(char_p.cast_eq_zero_iff R p n).not.mpr h⟩ lemma not_char_dvd (p : ℕ) [char_p R p] (k : ℕ) [h : ne_zero (k : R)] : ¬ p ∣ k := by rwa [←char_p.cast_eq_zero_iff R p k, ←ne.def, ←ne_zero_iff] end ne_zero
43be585500f5bbef3ae3050f2946d7d91e214000
4727251e0cd73359b15b664c3170e5d754078599
/src/data/finset/default.lean
fb51ca936bb3688770e49fa22e21cb2816127210
[ "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
248
lean
import data.finset.basic import data.finset.fold import data.finset.lattice import data.finset.locally_finite import data.finset.nat_antidiagonal import data.finset.pi import data.finset.powerset import data.finset.sort import data.finset.preimage
5844ebb8fe4e40f3cef91b5975683383562d8aee
9cba98daa30c0804090f963f9024147a50292fa0
/phys_dimensions.lean
b801aa1c17385656df2214d129d6602118388ef9
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
645
lean
/- Our design supports a set of disjoint 1-D affine spaces indexed by natural numbers, each with a standard frame, in terms of which new affine coordinate space "overlays" can be constructed. Our model here in the phys layer is that each physical dimension has its own coordinate-free, disjoint affine space. Currently, time and length are the only two we really support. We're speculating that perhaps mass is in the list as well? (Yet, its algebra isn't affine.) -/ /- Indices to disjoint 1-D "standard" affine spaces, and thus modeling distinct affine spaces of the physical world (construed classically). -/ def TIME := 0 def LENGTH := 1
2ff6d637b1b387c57d9c8cb9e0c935452b94ae2d
367134ba5a65885e863bdc4507601606690974c1
/src/data/pfunctor/univariate/basic.lean
ac6690bba81ec3517c843d01174d8943f6cb568d
[ "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
6,317
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import data.W /-! # Polynomial functors This file defines polynomial functors and the W-type construction as a polynomial functor. (For the M-type construction, see pfunctor/M.lean.) -/ universe u /-- A polynomial functor `P` is given by a type `A` and a family `B` of types over `A`. `P` maps any type `α` to a new type `P.obj α`, which is defined as the sigma type `Σ x, P.B x → α`. An element of `P.obj α` is a pair `⟨a, f⟩`, where `a` is an element of a type `A` and `f : B a → α`. Think of `a` as the shape of the object and `f` as an index to the relevant elements of `α`. -/ structure pfunctor := (A : Type u) (B : A → Type u) namespace pfunctor instance : inhabited pfunctor := ⟨⟨default _, default _⟩⟩ variables (P : pfunctor) {α β : Type u} /-- Applying `P` to an object of `Type` -/ def obj (α : Type*) := Σ x : P.A, P.B x → α /-- Applying `P` to a morphism of `Type` -/ def map {α β : Type*} (f : α → β) : P.obj α → P.obj β := λ ⟨a, g⟩, ⟨a, f ∘ g⟩ instance obj.inhabited [inhabited P.A] [inhabited α] : inhabited (P.obj α) := ⟨ ⟨ default _, λ _, default _ ⟩ ⟩ instance : functor P.obj := {map := @map P} protected theorem map_eq {α β : Type*} (f : α → β) (a : P.A) (g : P.B a → α) : @functor.map P.obj _ _ _ f ⟨a, g⟩ = ⟨a, f ∘ g⟩ := rfl protected theorem id_map {α : Type*} : ∀ x : P.obj α, id <$> x = id x := λ ⟨a, b⟩, rfl protected theorem comp_map {α β γ : Type*} (f : α → β) (g : β → γ) : ∀ x : P.obj α, (g ∘ f) <$> x = g <$> (f <$> x) := λ ⟨a, b⟩, rfl instance : is_lawful_functor P.obj := {id_map := @pfunctor.id_map P, comp_map := @pfunctor.comp_map P} /-- re-export existing definition of W-types and adapt it to a packaged definition of polynomial functor -/ def W := _root_.W_type P.B /- inhabitants of W types is awkward to encode as an instance assumption because there needs to be a value `a : P.A` such that `P.B a` is empty to yield a finite tree -/ attribute [nolint has_inhabited_instance] W variables {P} /-- root element of a W tree -/ def W.head : W P → P.A | ⟨a, f⟩ := a /-- children of the root of a W tree -/ def W.children : Π x : W P, P.B (W.head x) → W P | ⟨a, f⟩ := f /-- destructor for W-types -/ def W.dest : W P → P.obj (W P) | ⟨a, f⟩ := ⟨a, f⟩ /-- constructor for W-types -/ def W.mk : P.obj (W P) → W P | ⟨a, f⟩ := ⟨a, f⟩ @[simp] theorem W.dest_mk (p : P.obj (W P)) : W.dest (W.mk p) = p := by cases p; reflexivity @[simp] theorem W.mk_dest (p : W P) : W.mk (W.dest p) = p := by cases p; reflexivity variables (P) /-- `Idx` identifies a location inside the application of a pfunctor. For `F : pfunctor`, `x : F.obj α` and `i : F.Idx`, `i` can designate one part of `x` or is invalid, if `i.1 ≠ x.1` -/ def Idx := Σ x : P.A, P.B x instance Idx.inhabited [inhabited P.A] [inhabited (P.B (default _))] : inhabited P.Idx := ⟨ ⟨default _, default _⟩ ⟩ variables {P} /-- `x.iget i` takes the component of `x` designated by `i` if any is or returns a default value -/ def obj.iget [decidable_eq P.A] {α} [inhabited α] (x : P.obj α) (i : P.Idx) : α := if h : i.1 = x.1 then x.2 (cast (congr_arg _ h) i.2) else default _ @[simp] lemma fst_map {α β : Type u} (x : P.obj α) (f : α → β) : (f <$> x).1 = x.1 := by { cases x; refl } @[simp] lemma iget_map [decidable_eq P.A] {α β : Type u} [inhabited α] [inhabited β] (x : P.obj α) (f : α → β) (i : P.Idx) (h : i.1 = x.1) : (f <$> x).iget i = f (x.iget i) := by { simp only [obj.iget, fst_map, *, dif_pos, eq_self_iff_true], cases x, refl } end pfunctor /- Composition of polynomial functors. -/ namespace pfunctor /-- functor composition for polynomial functors -/ def comp (P₂ P₁ : pfunctor.{u}) : pfunctor.{u} := ⟨ Σ a₂ : P₂.1, P₂.2 a₂ → P₁.1, λ a₂a₁, Σ u : P₂.2 a₂a₁.1, P₁.2 (a₂a₁.2 u) ⟩ /-- constructor for composition -/ def comp.mk (P₂ P₁ : pfunctor.{u}) {α : Type} (x : P₂.obj (P₁.obj α)) : (comp P₂ P₁).obj α := ⟨ ⟨ x.1, sigma.fst ∘ x.2 ⟩, λ a₂a₁, (x.2 a₂a₁.1).2 a₂a₁.2 ⟩ /-- destructor for composition -/ def comp.get (P₂ P₁ : pfunctor.{u}) {α : Type} (x : (comp P₂ P₁).obj α) : P₂.obj (P₁.obj α) := ⟨ x.1.1, λ a₂, ⟨x.1.2 a₂, λ a₁, x.2 ⟨a₂,a₁⟩ ⟩ ⟩ end pfunctor /- Lifting predicates and relations. -/ namespace pfunctor variables {P : pfunctor.{u}} open functor theorem liftp_iff {α : Type u} (p : α → Prop) (x : P.obj α) : liftp p x ↔ ∃ a f, x = ⟨a, f⟩ ∧ ∀ i, p (f i) := begin split, { rintros ⟨y, hy⟩, cases h : y with a f, refine ⟨a, λ i, (f i).val, _, λ i, (f i).property⟩, rw [←hy, h, pfunctor.map_eq] }, rintros ⟨a, f, xeq, pf⟩, use ⟨a, λ i, ⟨f i, pf i⟩⟩, rw [xeq], reflexivity end theorem liftp_iff' {α : Type u} (p : α → Prop) (a : P.A) (f : P.B a → α) : @liftp.{u} P.obj _ α p ⟨a,f⟩ ↔ ∀ i, p (f i) := begin simp only [liftp_iff, sigma.mk.inj_iff]; split; intro, { casesm* [Exists _, _ ∧ _], subst_vars, assumption }, repeat { constructor <|> assumption } end theorem liftr_iff {α : Type u} (r : α → α → Prop) (x y : P.obj α) : liftr r x y ↔ ∃ a f₀ f₁, x = ⟨a, f₀⟩ ∧ y = ⟨a, f₁⟩ ∧ ∀ i, r (f₀ i) (f₁ i) := begin split, { rintros ⟨u, xeq, yeq⟩, cases h : u with a f, use [a, λ i, (f i).val.fst, λ i, (f i).val.snd], split, { rw [←xeq, h], refl }, split, { rw [←yeq, h], refl }, intro i, exact (f i).property }, rintros ⟨a, f₀, f₁, xeq, yeq, h⟩, use ⟨a, λ i, ⟨(f₀ i, f₁ i), h i⟩⟩, split, { rw [xeq], refl }, rw [yeq], refl end open set theorem supp_eq {α : Type u} (a : P.A) (f : P.B a → α) : @supp.{u} P.obj _ α (⟨a,f⟩ : P.obj α) = f '' univ := begin ext, simp only [supp, image_univ, mem_range, mem_set_of_eq], split; intro h, { apply @h (λ x, ∃ (y : P.B a), f y = x), rw liftp_iff', intro, refine ⟨_,rfl⟩ }, { simp only [liftp_iff'], cases h, subst x, tauto } end end pfunctor
9cc78fc2ef49c4aa72a8ad2feef72c0a451f622d
05b503addd423dd68145d68b8cde5cd595d74365
/src/data/matrix/basic.lean
d8da0d174124860a1722a1082252f2d87e3df764
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,178
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin Matrices -/ import algebra.module algebra.pi_instances import data.fintype.basic universes u v w def matrix (m n : Type u) [fintype m] [fintype n] (α : Type v) : Type (max u v) := m → n → α namespace matrix variables {l m n o : Type u} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : Type v} section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix def col (w : m → α) : matrix m punit α | x y := w x def row (v : n → α) : matrix punit n α | x y := v y instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group @[simp] theorem zero_val [has_zero α] (i j) : (0 : matrix m n α) i j = 0 := rfl @[simp] theorem neg_val [has_neg α] (M : matrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_val [has_add α] (M N : matrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl section diagonal variables [decidable_eq n] def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0 @[simp] theorem diagonal_val_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_val_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_val_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_val_ne h.symm @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by simp [diagonal]; refl section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_val {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_val_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_val_eq i @[simp] theorem one_val_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_val_ne theorem one_val_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_val_ne' end one end diagonal @[simp] theorem diagonal_add [decidable_eq n] [add_monoid α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases i = j; simp [h] protected def mul [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, finset.univ.sum (λ j, M i j * N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_val [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = finset.univ.sum (λ j, M i j * N j k) := rfl local attribute [simp] mul_val instance [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_val' [has_mul α] [add_comm_monoid α] {M N : matrix n n α} {i k} : (M * N) i k = finset.univ.sum (λ j, M i j * N j k) := rfl section semigroup variables [semiring α] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by classical; funext i k; simp [finset.mul_sum, finset.sum_mul, mul_assoc]; rw finset.sum_comm instance : semigroup (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.has_mul } end semigroup @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := by ext i j; by_cases i = j; simp [h] section semiring variables [semiring α] @[simp] protected theorem mul_zero (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by ext i j; simp @[simp] protected theorem zero_mul (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by ext i j; simp protected theorem mul_add (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by ext i j; simp [finset.sum_add_distrib, mul_add] protected theorem add_mul (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by ext i j; simp [finset.sum_add_distrib, add_mul] @[simp] theorem diagonal_mul [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := by simp; rw finset.sum_eq_single i; simp [diagonal_val_ne'] {contextual := tt} @[simp] theorem mul_diagonal [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by simp; rw finset.sum_eq_single j; simp {contextual := tt} @[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [decidable_eq n] : monoid (matrix n n α) := { one_mul := matrix.one_mul, mul_one := matrix.mul_one, ..matrix.has_one, ..matrix.semigroup } instance [decidable_eq n] : semiring (matrix n n α) := { mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, ..matrix.add_comm_monoid, ..matrix.monoid } @[simp] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma is_add_monoid_hom_mul_left (M : matrix l m α) : is_add_monoid_hom (λ x : matrix m n α, M ⬝ x) := { to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ } lemma is_add_monoid_hom_mul_right (M : matrix m n α) : is_add_monoid_hom (λ x : matrix l m α, x ⬝ M) := { to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ } protected lemma sum_mul {β : Type*} (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : s.sum f ⬝ M = s.sum (λ a, f a ⬝ M) := (@finset.sum_hom _ _ _ _ _ s f (λ x, x ⬝ M) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm protected lemma mul_sum {β : Type*} (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ s.sum f = s.sum (λ a, M ⬝ f a) := (@finset.sum_hom _ _ _ _ _ s f (λ x, M ⬝ x) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm end semiring section ring variables [ring α] @[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by ext; simp [matrix.mul] @[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by ext; simp [matrix.mul] end ring instance [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.add_comm_group, ..matrix.semiring } instance [semiring α] : has_scalar α (matrix m n α) := pi.has_scalar instance {β : Type w} [ring α] [add_comm_group β] [module α β] : module α (matrix m n β) := pi.module _ @[simp] lemma smul_val [semiring α] (a : α) (A : matrix m n α) (i : m) (j : n) : (a • A) i j = a * A i j := rfl section comm_ring variables [comm_ring α] lemma smul_eq_diagonal_mul [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_smul (M : matrix m n α) (a : α) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := begin ext i j, unfold matrix.mul has_scalar.smul, rw finset.mul_sum, congr, ext, ac_refl end @[simp] lemma smul_mul (M : matrix m n α) (a : α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := begin ext i j, unfold matrix.mul has_scalar.smul, rw finset.mul_sum, congr, ext, ac_refl end end comm_ring section semiring variables [semiring α] def vec_mul_vec (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y def mul_vec (M : matrix m n α) (v : n → α) : m → α | x := finset.univ.sum (λy:n, M x y * v y) def vec_mul (v : m → α) (M : matrix m n α) : n → α | y := finset.univ.sum (λx:m, v x * M x y) instance mul_vec.is_add_monoid_hom_left (v : n → α) : is_add_monoid_hom (λM:matrix m n α, mul_vec M v) := { map_zero := by ext; simp [mul_vec]; refl, map_add := begin intros x y, ext m, rw pi.add_apply (mul_vec x v) (mul_vec y v) m, simp [mul_vec, finset.sum_add_distrib, right_distrib] end } lemma mul_vec_diagonal [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := begin transitivity, refine finset.sum_eq_single x _ _, { assume b _ ne, simp [diagonal, ne.symm] }, { simp }, { rw [diagonal_val_eq] } end @[simp] lemma mul_vec_one [decidable_eq m] (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } lemma vec_mul_vec_eq (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by simp [matrix.mul]; refl end semiring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_val`, `diagonal_val_eq`, etc. -/ @[simp] lemma transpose_val (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, unfold has_one.one transpose, by_cases i = j, { simp only [h, diagonal_val_eq] }, { simp only [diagonal_val_ne h, diagonal_val_ne (λ p, h (symm p))] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [comm_ring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, unfold matrix.mul transpose, congr, ext, ac_refl end @[simp] lemma transpose_smul [comm_ring α] (c : α)(M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [comm_ring α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl end transpose def minor (A : matrix m n α) (row : l → m) (col : o → n) : matrix l o α := λ i j, A (row i) (col j) @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := minor A id (fin.cast_add r) @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := minor A id (fin.nat_add l) @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := minor A (fin.cast_add d) id @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := minor A (fin.nat_add u) id @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) end matrix
727f05e3fed116c5de774dd390f044c56748313a
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/sum/instances.lean
c0c40438ab706db57cc4d1bf0d4db4ae869dd82e
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
355
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.mk_dec_eq_instance universes u v instance {α : Type u} {β : Type v} [decidable_eq α] [decidable_eq β] : decidable_eq (α ⊕ β) := by tactic.mk_dec_eq_instance
468341e60fcd138b6d4ef1d83cf4114503efe467
367134ba5a65885e863bdc4507601606690974c1
/src/analysis/calculus/fderiv_measurable.lean
507348d4d8396fe9397151be01c3a81f89c5c4dc
[ "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
22,374
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.deriv import measure_theory.borel_space /-! # Derivative is measurable In this file we prove that the derivative of any function with complete codomain is a measurable function. Namely, we prove: * `measurable_set_of_differentiable_at`: the set `{x | differentiable_at 𝕜 f x}` is measurable; * `measurable_fderiv`: the function `fderiv 𝕜 f` is measurable; * `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `λ x, fderiv 𝕜 f x y` is measurable; * `measurable_deriv`: the function `deriv f` is measurable (for `f : 𝕜 → F`). ## Implementation We give a proof that avoids second-countability issues, by expressing the differentiability set as a function of open sets in the following way. Define `A (L, r, ε)` to be the set of points where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the linear map `L`, up to `ε r`. It is an open set. Let also `B (L, r, s, ε) = A (L, r, ε) ∩ A (L, s, ε)`: we require that at two possibly different scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open. We claim that the differentiability set of `f` is exactly `D = ⋂ ε > 0, ⋃ δ > 0, ⋂ r, s < δ, ⋃ L, B (L, r, s, ε)`. In other words, for any `ε > 0`, we require that there is a size `δ` such that, for any two scales below this size, the function is well approximated by a linear map, common to the two scales. The set `⋃ L, B (L, r, s, ε)` is open, as a union of open sets. Converting the intersections and unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the differentiability set is measurable. To prove the claim, there are two inclusions. One is trivial: if the function is differentiable at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the differentiability exactly says that the map is well approximated by `L`). This is proved in `mem_A_of_differentiable` and `differentiable_set_subset_D`. For the other direction, the difficulty is that `L` in the union may depend on `ε, r, s`. The key point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to `A (L, r, ε)` and `A (L', r, ε)`, then `L` and `L'` have to be close on a shell, and thus `∥L - L'∥` is bounded by `ε` (see `norm_sub_le_of_mem_A`). Assume now `x ∈ D`. If one has two maps `L` and `L'` such that `x` belongs to `A (L, r, ε)` and to `A (L', r', ε')`, one deduces that `L` is close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a linear map `L₁` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `ε` (it exists as `x` belongs to `D`). Take also `L₂` that approximates `f` around `x` both at scales `r'` and `s` w.r.t. `ε'`. Then `L₁` is close to `L` (as they are close on a shell of radius `r`), and `L₂` is close to `L₁` (as they are close on a shell of radius `s`), and `L'` is close to `L₂` (as they are close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed. It follows that the different approximating linear maps that show up form a Cauchy sequence when `ε` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`. With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`. To show that the derivative itself is measurable, add in the definition of `B` and `D` a set `K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K` is exactly the set of points where `f` is differentiable with a derivative in `K`. ## Tags derivative, measurable function, Borel σ-algebra -/ noncomputable theory open set metric asymptotics filter continuous_linear_map open topological_space (second_countable_topology) open_locale topological_space namespace continuous_linear_map variables {𝕜 E F : Type*} [nondiscrete_normed_field 𝕜] [normed_group E] [normed_space 𝕜 E] [normed_group F] [normed_space 𝕜 F] instance : measurable_space (E →L[𝕜] F) := borel _ instance : borel_space (E →L[𝕜] F) := ⟨rfl⟩ lemma measurable_apply [measurable_space F] [borel_space F] (x : E) : measurable (λ f : E →L[𝕜] F, f x) := (apply 𝕜 F x).continuous.measurable lemma measurable_apply' [measurable_space E] [opens_measurable_space E] [measurable_space F] [borel_space F] : measurable (λ (x : E) (f : E →L[𝕜] F), f x) := measurable_pi_lambda _ $ λ f, f.measurable lemma measurable_apply₂ [measurable_space E] [opens_measurable_space E] [second_countable_topology E] [second_countable_topology (E →L[𝕜] F)] [measurable_space F] [borel_space F] : measurable (λ p : (E →L[𝕜] F) × E, p.1 p.2) := is_bounded_bilinear_map_apply.continuous.measurable lemma measurable_coe [measurable_space F] [borel_space F] : measurable (λ (f : E →L[𝕜] F) (x : E), f x) := measurable_pi_lambda _ measurable_apply end continuous_linear_map variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {f : E → F} (K : set (E →L[𝕜] F)) namespace fderiv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `L`, up to an error `ε`. We tweak the definition to make sure that this is an open set.-/ def A (f : E → F) (L : E →L[𝕜] F) (r ε : ℝ) : set E := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ ball x r', ∥f z - f y - L (z-y)∥ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a continuous linear map `L` belonging to `K` (a given set of continuous linear maps) that approximates well the function `f` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : E → F) (K : set (E →L[𝕜] F)) (r s ε : ℝ) : set E := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : E → F) (K : set (E →L[𝕜] F)) : set E := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma is_open_A (L : E →L[𝕜] F) (r ε : ℝ) : is_open (A f L r ε) := begin rw metric.is_open_iff, rintros x ⟨r', r'_mem, hr'⟩, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between r'_mem.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le r'_mem.2)⟩, refine ⟨r' - s, by linarith, λ x' hx', ⟨s, this, _⟩⟩, have B : ball x' s ⊆ ball x r' := ball_subset (le_of_lt hx'), assume y z hy hz, exact hr' y z (B hy) (B hz) end lemma is_open_B {K : set (E →L[𝕜] F)} {r s ε : ℝ} : is_open (B f K r s ε) := by simp [B, is_open_Union, is_open_inter, is_open_A] lemma A_mono (L : E →L[𝕜] F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y z hy hz, _⟩, apply le_trans (hr' y z hy hz), apply mul_le_mul_of_nonneg_right h, linarith [mem_ball.1 hy, r'r.2, @dist_nonneg _ _ y x], end lemma le_of_mem_A {r ε : ℝ} {L : E →L[𝕜] F} {x : E} (hx : x ∈ A f L r ε) {y z : E} (hy : y ∈ closed_ball x (r/2)) (hz : z ∈ closed_ball x (r/2)) : ∥f z - f y - L (z-y)∥ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, exact hr' _ _ (lt_of_le_of_lt (mem_closed_ball.1 hy) r'mem.1) (lt_of_le_of_lt (mem_closed_ball.1 hz) r'mem.1) end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : E} (hx : differentiable_at 𝕜 f x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (fderiv 𝕜 f x) r ε := begin have := hx.has_fderiv_at, simp only [has_fderiv_at, has_fderiv_at_filter, is_o_iff] at this, rcases eventually_nhds_iff_ball.1 (this (half_pos hε)) with ⟨R, R_pos, hR⟩, refine ⟨R, R_pos, λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_refl _⟩, refine ⟨r, this, λ y z hy hz, _⟩, calc ∥f z - f y - (fderiv 𝕜 f x) (z - y)∥ = ∥(f z - f x - (fderiv 𝕜 f x) (z - x)) - (f y - f x - (fderiv 𝕜 f x) (y - x))∥ : by { congr' 1, simp only [continuous_linear_map.map_sub], abel } ... ≤ ∥(f z - f x - (fderiv 𝕜 f x) (z - x))∥ + ∥f y - f x - (fderiv 𝕜 f x) (y - x)∥ : norm_sub_le _ _ ... ≤ ε / 2 * ∥z - x∥ + ε / 2 * ∥y - x∥ : add_le_add (hR _ (lt_trans (mem_ball.1 hz) hr.2)) (hR _ (lt_trans (mem_ball.1 hy) hr.2)) ... ≤ ε / 2 * r + ε / 2 * r : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hz)) (le_of_lt (half_pos hε))) (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hy)) (le_of_lt (half_pos hε))) ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {c : 𝕜} (hc : 1 < ∥c∥) {r ε : ℝ} (hε : 0 < ε) (hr : 0 < r) {x : E} {L₁ L₂ : E →L[𝕜] F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ∥L₁ - L₂∥ ≤ 4 * ∥c∥ * ε := begin have : 0 ≤ 4 * ∥c∥ * ε := mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4) (norm_nonneg _)) hε.le, apply op_norm_le_of_shell (half_pos hr) this hc, assume y ley ylt, rw [div_div_eq_div_mul, div_le_iff' (mul_pos (by norm_num : (0 : ℝ) < 2) (zero_lt_one.trans hc))] at ley, calc ∥(L₁ - L₂) y∥ = ∥(f (x + y) - f x - L₂ ((x + y) - x)) - (f (x + y) - f x - L₁ ((x + y) - x))∥ : by simp ... ≤ ∥(f (x + y) - f x - L₂ ((x + y) - x))∥ + ∥(f (x + y) - f x - L₁ ((x + y) - x))∥ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le], } }, { apply le_of_mem_A h₁, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le] } }, end ... = 2 * ε * r : by ring ... ≤ 2 * ε * (2 * ∥c∥ * ∥y∥) : mul_le_mul_of_nonneg_left ley (mul_nonneg (by norm_num) hε.le) ... = 4 * ∥c∥ * ε * ∥y∥ : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_eq], refine ⟨n, λ p hp q hq, ⟨fderiv 𝕜 f x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set (E →L[𝕜] F)} (hK : is_complete K) : D f K ⊆ {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have cpos : 0 < ∥c∥ := lt_trans zero_lt_one hc, assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ∥L e p q - L e' p' q'∥ ≤ 12 * ∥c∥ * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ∥L e p q - L e p r∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A hc P P I1 I2 }, have J2 : ∥L e p r - L e' p' r∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A hc P P I1 (A_mono _ _ I I2) }, have J3 : ∥L e' p' r - L e' p' q'∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A hc P P (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ∥L e p q - L e' p' q'∥ = ∥(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')∥ : by { congr' 1, abel } ... ≤ ∥L e p q - L e p r∥ + ∥L e p r - L e' p' r∥ + ∥L e' p' r - L e' p' q'∥ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * ∥c∥ * (1/2)^e + 4 * ∥c∥ * (1/2)^e + 4 * ∥c∥ * (1/2)^e : by apply_rules [add_le_add] ... = 12 * ∥c∥ * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → (E →L[𝕜] F) := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / (12 * ∥c∥) := exists_pow_lt_of_lt_one (div_pos εpos (mul_pos (by norm_num) cpos)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ∥L0 e - L0 e'∥ ≤ 12 * ∥c∥ * (1/2)^e : M _ _ _ _ _ _ (le_refl _) (le_refl _) (le_refl _) (le_refl _) he' ... < 12 * ∥c∥ * (ε / (12 * ∥c∥)) : mul_lt_mul' (le_refl _) he (le_of_lt P) (mul_pos (by norm_num) cpos) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0), ne_of_gt cpos], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) (le_refl _) (le_refl _)).1) this, have Lf' : ∀ e p, n e ≤ p → ∥L e (n e) p - f'∥ ≤ 12 * ∥c∥ * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ (le_refl _) hp (le_refl _) (le_refl _) he'⟩ }, /- Let us show that `f` has derivative `f'` at `x`. -/ have : has_fderiv_at f f' x, { simp only [has_fderiv_at_iff_is_o_nhds_zero, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole ball of radius `(1/2)^(n e)`. -/ assume ε εpos, have pos : 0 < 4 + 12 * ∥c∥ := add_pos_of_pos_of_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _)), obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / (4 + 12 * ∥c∥) := exists_pow_lt_of_lt_one (div_pos εpos pos) (by norm_num), rw eventually_nhds_iff_ball, refine ⟨(1/2) ^ (n e + 1), P, λ y hy, _⟩, -- We need to show that `f (x + y) - f x - f' y` is small. For this, we will work at scale -- `k` where `k` is chosen with `∥y∥ ∼ 2 ^ (-k)`. by_cases y_pos : y = 0, {simp [y_pos] }, have yzero : 0 < ∥y∥ := norm_pos_iff.mpr y_pos, have y_lt : ∥y∥ < (1/2) ^ (n e + 1), by simpa using mem_ball_iff_norm.1 hy, have yone : ∥y∥ ≤ 1 := le_trans (y_lt.le) (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < ∥y∥ ∧ ∥y∥ ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_trans hk y_lt, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J1 : ∥f (x + y) - f x - L e (n e) m ((x + y) - x)∥ ≤ (1/2) ^ e * (1/2) ^ m, { apply le_of_mem_A (hn e (n e) m (le_refl _) m_ge).2.2, { simp only [mem_closed_ball, dist_self], exact div_nonneg (le_of_lt P) (zero_le_two) }, { simp [dist_eq_norm], convert h'k, field_simp, ring_exp } }, have J2 : ∥f (x + y) - f x - L e (n e) m y∥ ≤ 4 * (1/2) ^ e * ∥y∥ := calc ∥f (x + y) - f x - L e (n e) m y∥ ≤ (1/2) ^ e * (1/2) ^ m : by simpa only [add_sub_cancel'] using J1 ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * ∥y∥ : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)), -- use the previous estimates to see that `f (x + y) - f x - f' y` is small. calc ∥f (x + y) - f x - f' y∥ = ∥(f (x + y) - f x - L e (n e) m y) + (L e (n e) m - f') y∥ : by { congr' 1, simp, abel } ... ≤ ∥f (x + y) - f x - L e (n e) m y∥ + ∥(L e (n e) m - f') y∥ : norm_add_le _ _ ... ≤ 4 * (1/2) ^ e * ∥y∥ + 12 * ∥c∥ * (1/2) ^ e * ∥y∥ : add_le_add J2 (le_trans (le_op_norm _ _) (mul_le_mul_of_nonneg_right (Lf' _ _ m_ge) (norm_nonneg _))) ... = (4 + 12 * ∥c∥) * ∥y∥ * (1/2) ^ e : by ring ... ≤ (4 + 12 * ∥c∥) * ∥y∥ * (ε / (4 + 12 * ∥c∥)) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (add_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _))) (norm_nonneg _)) ... = ε * ∥y∥ : by { field_simp [ne_of_gt pos], ring } }, rw ← this.fderiv at f'K, exact ⟨this.differentiable_at, f'K⟩ end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end fderiv_measurable_aux open fderiv_measurable_aux variables [measurable_space E] [opens_measurable_space E] variables (𝕜 f) /-- The set of differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_at_of_is_complete {K : set (E →L[𝕜] F)} (hK : is_complete K) : measurable_set {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := by simp [differentiable_set_eq_D K hK, D, is_open_B.measurable_set, measurable_set.Inter_Prop, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_at : measurable_set {x | differentiable_at 𝕜 f x} := begin have : is_complete (univ : set (E →L[𝕜] F)) := complete_univ, convert measurable_set_of_differentiable_at_of_is_complete 𝕜 f this, simp end lemma measurable_fderiv : measurable (fderiv 𝕜 f) := begin refine measurable_of_is_closed (λ s hs, _), have : fderiv 𝕜 f ⁻¹' s = {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ s} ∪ {x | (0 : E →L[𝕜] F) ∈ s} ∩ {x | ¬differentiable_at 𝕜 f x} := set.ext (λ x, mem_preimage.trans fderiv_mem_iff), rw this, exact (measurable_set_of_differentiable_at_of_is_complete _ _ hs.is_complete).union ((measurable_set.const _).inter (measurable_set_of_differentiable_at _ _).compl) end lemma measurable_fderiv_apply_const [measurable_space F] [borel_space F] (y : E) : measurable (λ x, fderiv 𝕜 f x y) := (continuous_linear_map.measurable_apply y).comp (measurable_fderiv 𝕜 f) variable {𝕜} lemma measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) : measurable (deriv f) := by simpa only [fderiv_deriv] using measurable_fderiv_apply_const 𝕜 f 1
a56b211a2721e5decdf3d61dc19e4d29dac15973
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/ffi/lib/lean/Main.lean
e763be6b15b7367f214d47c921d1036602830713
[ "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
60
lean
import FFI def main : IO Unit := IO.println <| myAdd 1 2
c573165647ebaff39012864f0f3e4349e475145f
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/algebra/group_with_zero/basic.lean
4a87f163239b01e3519e71ef0b2f315edd4fd4a2
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,038
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import logic.nontrivial import algebra.group.units_hom import algebra.group.inj_surj import algebra.group_with_zero.defs /-! # Groups with an adjoined zero element This file describes structures that are not usually studied on their own right in mathematics, namely a special sort of monoid: apart from a distinguished “zero element” they form a group, or in other words, they are groups with an adjoined zero element. Examples are: * division rings; * the value monoid of a multiplicative valuation; * in particular, the non-negative real numbers. ## Main definitions Various lemmas about `group_with_zero` and `comm_group_with_zero`. To reduce import dependencies, the type-classes themselves are in `algebra.group_with_zero.defs`. ## Implementation details As is usual in mathlib, we extend the inverse function to the zero element, and require `0⁻¹ = 0`. -/ set_option old_structure_cmd true open_locale classical open function variables {M₀ G₀ M₀' G₀' : Type*} mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are division-free." section section mul_zero_class variables [mul_zero_class M₀] {a b : M₀} /-- Pullback a `mul_zero_class` instance along an injective function. -/ protected def function.injective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, zero_mul := λ a, hf $ by simp only [mul, zero, zero_mul], mul_zero := λ a, hf $ by simp only [mul, zero, mul_zero] } /-- Pushforward a `mul_zero_class` instance along an surjective function. -/ protected def function.surjective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, mul_zero := hf.forall.2 $ λ x, by simp only [← zero, ← mul, mul_zero], zero_mul := hf.forall.2 $ λ x, by simp only [← zero, ← mul, zero_mul] } lemma mul_eq_zero_of_left (h : a = 0) (b : M₀) : a * b = 0 := h.symm ▸ zero_mul b lemma mul_eq_zero_of_right (a : M₀) (h : b = 0) : a * b = 0 := h.symm ▸ mul_zero a lemma left_ne_zero_of_mul : a * b ≠ 0 → a ≠ 0 := mt (λ h, mul_eq_zero_of_left h b) lemma right_ne_zero_of_mul : a * b ≠ 0 → b ≠ 0 := mt (mul_eq_zero_of_right a) lemma ne_zero_and_ne_zero_of_mul (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := ⟨left_ne_zero_of_mul h, right_ne_zero_of_mul h⟩ lemma mul_eq_zero_of_ne_zero_imp_eq_zero {a b : M₀} (h : a ≠ 0 → b = 0) : a * b = 0 := if ha : a = 0 then by rw [ha, zero_mul] else by rw [h ha, mul_zero] end mul_zero_class /-- Pushforward a `no_zero_divisors` instance along an injective function. -/ protected lemma function.injective.no_zero_divisors [has_mul M₀] [has_zero M₀] [has_mul M₀'] [has_zero M₀'] [no_zero_divisors M₀'] (f : M₀ → M₀') (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : no_zero_divisors M₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y H, have f x * f y = 0, by rw [← mul, H, zero], (eq_zero_or_eq_zero_of_mul_eq_zero this).imp (λ H, hf $ by rwa zero) (λ H, hf $ by rwa zero) } lemma eq_zero_of_mul_self_eq_zero [has_mul M₀] [has_zero M₀] [no_zero_divisors M₀] {a : M₀} (h : a * a = 0) : a = 0 := (eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id section variables [mul_zero_class M₀] [no_zero_divisors M₀] {a b : M₀} /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem mul_eq_zero : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, o.elim (λ h, mul_eq_zero_of_left h b) (mul_eq_zero_of_right a)⟩ /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem zero_eq_mul : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] /-- If `α` has no zero divisors, then the product of two elements is nonzero iff both of them are nonzero. -/ theorem mul_ne_zero_iff : a * b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr mul_eq_zero).trans not_or_distrib theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mul_ne_zero_iff.2 ⟨ha, hb⟩ /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` equals zero iff so is `b * a`. -/ theorem mul_eq_zero_comm : a * b = 0 ↔ b * a = 0 := mul_eq_zero.trans $ (or_comm _ _).trans mul_eq_zero.symm /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` is nonzero iff so is `b * a`. -/ theorem mul_ne_zero_comm : a * b ≠ 0 ↔ b * a ≠ 0 := not_congr mul_eq_zero_comm lemma mul_self_eq_zero : a * a = 0 ↔ a = 0 := by simp lemma zero_eq_mul_self : 0 = a * a ↔ a = 0 := by simp end end section variables [monoid_with_zero M₀] [nontrivial M₀] {a b : M₀} /-- In a nontrivial monoid with zero, zero and one are different. -/ @[simp] lemma zero_ne_one : 0 ≠ (1:M₀) := begin assume h, rcases exists_pair_ne M₀ with ⟨x, y, hx⟩, apply hx, calc x = 1 * x : by rw [one_mul] ... = 0 : by rw [← h, zero_mul] ... = 1 * y : by rw [← h, zero_mul] ... = y : by rw [one_mul] end @[simp] lemma one_ne_zero : (1:M₀) ≠ 0 := zero_ne_one.symm lemma ne_zero_of_eq_one {a : M₀} (h : a = 1) : a ≠ 0 := calc a = 1 : h ... ≠ 0 : one_ne_zero lemma left_ne_zero_of_mul_eq_one (h : a * b = 1) : a ≠ 0 := left_ne_zero_of_mul $ ne_zero_of_eq_one h lemma right_ne_zero_of_mul_eq_one (h : a * b = 1) : b ≠ 0 := right_ne_zero_of_mul $ ne_zero_of_eq_one h /-- Pullback a `nontrivial` instance along a function sending `0` to `0` and `1` to `1`. -/ protected lemma pullback_nonzero [has_zero M₀'] [has_one M₀'] (f : M₀' → M₀) (zero : f 0 = 0) (one : f 1 = 1) : nontrivial M₀' := ⟨⟨0, 1, mt (congr_arg f) $ by { rw [zero, one], exact zero_ne_one }⟩⟩ end /-- The division operation on a group with zero element. -/ @[priority 100] -- see Note [lower instance priority] instance group_with_zero.has_div {G₀ : Type*} [group_with_zero G₀] : has_div G₀ := ⟨λ g h, g * h⁻¹⟩ section monoid_with_zero /-- Pullback a `monoid_with_zero` class along an injective function. -/ protected def function.injective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : monoid_with_zero M₀' := { .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. -/ protected def function.surjective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : monoid_with_zero M₀' := { .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pullback a `monoid_with_zero` class along an injective function. -/ protected def function.injective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [comm_monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. -/ protected def function.surjective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [comm_monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul } variables [monoid_with_zero M₀] namespace units /-- An element of the unit group of a nonzero monoid with zero represented as an element of the monoid is nonzero. -/ @[simp] lemma ne_zero [nontrivial M₀] (u : units M₀) : (u : M₀) ≠ 0 := left_ne_zero_of_mul_eq_one u.mul_inv -- We can't use `mul_eq_zero` + `units.ne_zero` in the next two lemmas because we don't assume -- `nonzero M₀`. @[simp] lemma mul_left_eq_zero (u : units M₀) {a : M₀} : a * u = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_left h ↑u⁻¹, λ h, mul_eq_zero_of_left h u⟩ @[simp] lemma mul_right_eq_zero (u : units M₀) {a : M₀} : ↑u * a = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_right ↑u⁻¹ h, mul_eq_zero_of_right u⟩ end units namespace is_unit lemma ne_zero [nontrivial M₀] {a : M₀} (ha : is_unit a) : a ≠ 0 := let ⟨u, hu⟩ := ha in hu ▸ u.ne_zero lemma mul_right_eq_zero {a b : M₀} (ha : is_unit a) : a * b = 0 ↔ b = 0 := let ⟨u, hu⟩ := ha in hu ▸ u.mul_right_eq_zero lemma mul_left_eq_zero {a b : M₀} (hb : is_unit b) : a * b = 0 ↔ a = 0 := let ⟨u, hu⟩ := hb in hu ▸ u.mul_left_eq_zero end is_unit /-- In a monoid with zero, if zero equals one, then zero is the only element. -/ lemma eq_zero_of_zero_eq_one (h : (0 : M₀) = 1) (a : M₀) : a = 0 := by rw [← mul_one a, ← h, mul_zero] /-- In a monoid with zero, if zero equals one, then zero is the unique element. Somewhat arbitrarily, we define the default element to be `0`. All other elements will be provably equal to it, but not necessarily definitionally equal. -/ def unique_of_zero_eq_one (h : (0 : M₀) = 1) : unique M₀ := { default := 0, uniq := eq_zero_of_zero_eq_one h } /-- In a monoid with zero, zero equals one if and only if all elements of that semiring are equal. -/ theorem subsingleton_iff_zero_eq_one : (0 : M₀) = 1 ↔ subsingleton M₀ := ⟨λ h, @unique.subsingleton _ (unique_of_zero_eq_one h), λ h, @subsingleton.elim _ h _ _⟩ alias subsingleton_iff_zero_eq_one ↔ subsingleton_of_zero_eq_one _ lemma eq_of_zero_eq_one (h : (0 : M₀) = 1) (a b : M₀) : a = b := @subsingleton.elim _ (subsingleton_of_zero_eq_one h) a b @[simp] theorem is_unit_zero_iff : is_unit (0 : M₀) ↔ (0:M₀) = 1 := ⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0, λ h, @is_unit_of_subsingleton _ _ (subsingleton_of_zero_eq_one h) 0⟩ @[simp] theorem not_is_unit_zero [nontrivial M₀] : ¬ is_unit (0 : M₀) := mt is_unit_zero_iff.1 zero_ne_one variable (M₀) /-- In a monoid with zero, either zero and one are nonequal, or zero is the only element. -/ lemma zero_ne_one_or_forall_eq_0 : (0 : M₀) ≠ 1 ∨ (∀a:M₀, a = 0) := not_or_of_imp eq_zero_of_zero_eq_one end monoid_with_zero section cancel_monoid_with_zero variables [cancel_monoid_with_zero M₀] {a b c : M₀} @[priority 10] -- see Note [lower instance priority] instance comm_cancel_monoid_with_zero.no_zero_divisors : no_zero_divisors M₀ := ⟨λ a b ab0, by { by_cases a = 0, { left, exact h }, right, apply cancel_monoid_with_zero.mul_left_cancel_of_ne_zero h, rw [ab0, mul_zero], }⟩ lemma mul_left_inj' (hc : c ≠ 0) : a * c = b * c ↔ a = b := ⟨mul_right_cancel' hc, λ h, h ▸ rfl⟩ lemma mul_right_inj' (ha : a ≠ 0) : a * b = a * c ↔ b = c := ⟨mul_left_cancel' ha, λ h, h ▸ rfl⟩ @[simp] lemma mul_eq_mul_right_iff : a * c = b * c ↔ a = b ∨ c = 0 := by by_cases hc : c = 0; [simp [hc], simp [mul_left_inj', hc]] @[simp] lemma mul_eq_mul_left_iff : a * b = a * c ↔ b = c ∨ a = 0 := by by_cases ha : a = 0; [simp [ha], simp [mul_right_inj', ha]] /-- Pullback a `monoid_with_zero` class along an injective function. -/ protected def function.injective.cancel_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : cancel_monoid_with_zero M₀' := { mul_left_cancel_of_ne_zero := λ x y z hx H, hf $ mul_left_cancel' ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, mul_right_cancel_of_ne_zero := λ x y z hx H, hf $ mul_right_cancel' ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- An element of a `cancel_monoid_with_zero` fixed by right multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_right (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_left_cancel' ha $ h₂.symm ▸ (mul_one a).symm /-- An element of a `cancel_monoid_with_zero` fixed by left multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_left (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_right_cancel' ha $ h₂.symm ▸ (one_mul a).symm end cancel_monoid_with_zero section group_with_zero variables [group_with_zero G₀] lemma div_eq_mul_inv {a b : G₀} : a / b = a * b⁻¹ := rfl alias div_eq_mul_inv ← division_def /-- Pullback a `group_with_zero` class along an injective function. -/ protected def function.injective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : group_with_zero G₀' := { inv := has_inv.inv, inv_zero := hf $ by erw [inv, zero, inv_zero], mul_inv_cancel := λ x hx, hf $ by erw [one, mul, inv, mul_inv_cancel ((hf.ne_iff' zero).2 hx)], .. hf.monoid_with_zero f zero one mul, .. pullback_nonzero f zero one } /-- Pushforward a `group_with_zero` class along an surjective function. -/ protected def function.surjective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : group_with_zero G₀' := { inv := has_inv.inv, inv_zero := by erw [← zero, ← inv, inv_zero], mul_inv_cancel := hf.forall.2 $ λ x hx, by erw [← inv, ← mul, mul_inv_cancel (mt (congr_arg f) $ trans_rel_left ne hx zero.symm)]; exact one, exists_pair_ne := ⟨0, 1, h01⟩, .. hf.monoid_with_zero f zero one mul } @[simp] lemma mul_inv_cancel_right' {b : G₀} (h : b ≠ 0) (a : G₀) : (a * b) * b⁻¹ = a := calc (a * b) * b⁻¹ = a * (b * b⁻¹) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma mul_inv_cancel_left' {a : G₀} (h : a ≠ 0) (b : G₀) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = (a * a⁻¹) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] lemma inv_ne_zero {a : G₀} (h : a ≠ 0) : a⁻¹ ≠ 0 := assume a_eq_0, by simpa [a_eq_0] using mul_inv_cancel h @[simp] lemma inv_mul_cancel {a : G₀} (h : a ≠ 0) : a⁻¹ * a = 1 := calc a⁻¹ * a = (a⁻¹ * a) * a⁻¹ * a⁻¹⁻¹ : by simp [inv_ne_zero h] ... = a⁻¹ * a⁻¹⁻¹ : by simp [h] ... = 1 : by simp [inv_ne_zero h] @[simp] lemma inv_mul_cancel_right' {b : G₀} (h : b ≠ 0) (a : G₀) : (a * b⁻¹) * b = a := calc (a * b⁻¹) * b = a * (b⁻¹ * b) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma inv_mul_cancel_left' {a : G₀} (h : a ≠ 0) (b : G₀) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = (a⁻¹ * a) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] @[simp] lemma inv_one : 1⁻¹ = (1:G₀) := calc 1⁻¹ = 1 * 1⁻¹ : by rw [one_mul] ... = (1:G₀) : by simp @[simp] lemma inv_inv' (a : G₀) : a⁻¹⁻¹ = a := begin classical, by_cases h : a = 0, { simp [h] }, calc a⁻¹⁻¹ = a * (a⁻¹ * a⁻¹⁻¹) : by simp [h] ... = a : by simp [inv_ne_zero h] end /-- Multiplying `a` by itself and then by its inverse results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_self_mul_inv (a : G₀) : a * a * a⁻¹ = a := begin classical, by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_assoc, mul_inv_cancel h, mul_one] } end /-- Multiplying `a` by its inverse and then by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_inv_mul_self (a : G₀) : a * a⁻¹ * a = a := begin classical, by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_inv_cancel h, one_mul] } end /-- Multiplying `a⁻¹` by `a` twice results in `a` (whether or not `a` is zero). -/ @[simp] lemma inv_mul_mul_self (a : G₀) : a⁻¹ * a * a = a := begin classical, by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [inv_mul_cancel h, one_mul] } end /-- Multiplying `a` by itself and then dividing by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_self_div_self (a : G₀) : a * a / a = a := mul_self_mul_inv a /-- Dividing `a` by itself and then multiplying by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma div_self_mul_self (a : G₀) : a / a * a = a := mul_inv_mul_self a lemma inv_involutive' : function.involutive (has_inv.inv : G₀ → G₀) := inv_inv' lemma eq_inv_of_mul_right_eq_one {a b : G₀} (h : a * b = 1) : b = a⁻¹ := by rw [← inv_mul_cancel_left' (left_ne_zero_of_mul_eq_one h) b, h, mul_one] lemma eq_inv_of_mul_left_eq_one {a b : G₀} (h : a * b = 1) : a = b⁻¹ := by rw [← mul_inv_cancel_right' (right_ne_zero_of_mul_eq_one h) a, h, one_mul] lemma inv_injective' : function.injective (@has_inv.inv G₀ _) := inv_involutive'.injective @[simp] lemma inv_inj' {g h : G₀} : g⁻¹ = h⁻¹ ↔ g = h := inv_injective'.eq_iff lemma inv_eq_iff {g h : G₀} : g⁻¹ = h ↔ h⁻¹ = g := by rw [← inv_inj', eq_comm, inv_inv'] @[simp] lemma inv_eq_one' {g : G₀} : g⁻¹ = 1 ↔ g = 1 := by rw [inv_eq_iff, inv_one, eq_comm] end group_with_zero namespace units variables [group_with_zero G₀] variables {a b : G₀} /-- Embed a non-zero element of a `group_with_zero` into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : G₀) (ha : a ≠ 0) : units G₀ := ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩ @[simp] lemma coe_mk0 {a : G₀} (h : a ≠ 0) : (mk0 a h : G₀) = a := rfl @[simp] lemma mk0_coe (u : units G₀) (h : (u : G₀) ≠ 0) : mk0 (u : G₀) h = u := units.ext rfl @[simp, norm_cast] lemma coe_inv' (u : units G₀) : ((u⁻¹ : units G₀) : G₀) = u⁻¹ := eq_inv_of_mul_left_eq_one u.inv_mul @[simp] lemma mul_inv' (u : units G₀) : (u : G₀) * u⁻¹ = 1 := mul_inv_cancel u.ne_zero @[simp] lemma inv_mul' (u : units G₀) : (u⁻¹ : G₀) * u = 1 := inv_mul_cancel u.ne_zero @[simp] lemma mk0_inj {a b : G₀} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ @[simp] lemma exists_iff_ne_zero {x : G₀} : (∃ u : units G₀, ↑u = x) ↔ x ≠ 0 := ⟨λ ⟨u, hu⟩, hu ▸ u.ne_zero, assume hx, ⟨mk0 x hx, rfl⟩⟩ end units section group_with_zero variables [group_with_zero G₀] lemma is_unit.mk0 (x : G₀) (hx : x ≠ 0) : is_unit x := is_unit_unit (units.mk0 x hx) lemma is_unit_iff_ne_zero {x : G₀} : is_unit x ↔ x ≠ 0 := units.exists_iff_ne_zero @[priority 10] -- see Note [lower instance priority] instance group_with_zero.no_zero_divisors : no_zero_divisors G₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin classical, contrapose! h, exact ((units.mk0 a h.1) * (units.mk0 b h.2)).ne_zero end, .. (‹_› : group_with_zero G₀) } @[priority 10] -- see Note [lower instance priority] instance group_with_zero.cancel_monoid_with_zero : cancel_monoid_with_zero G₀ := { mul_left_cancel_of_ne_zero := λ x y z hx h, by rw [← inv_mul_cancel_left' hx y, h, inv_mul_cancel_left' hx z], mul_right_cancel_of_ne_zero := λ x y z hy h, by rw [← mul_inv_cancel_right' hy x, h, mul_inv_cancel_right' hy z], .. (‹_› : group_with_zero G₀) } lemma mul_inv_rev' (x y : G₀) : (x * y)⁻¹ = y⁻¹ * x⁻¹ := begin classical, by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, symmetry, apply eq_inv_of_mul_left_eq_one, simp [mul_assoc, hx, hy] end @[simp] lemma div_self {a : G₀} (h : a ≠ 0) : a / a = 1 := mul_inv_cancel h @[simp] lemma div_one (a : G₀) : a / 1 = a := by simp [div_eq_mul_inv] @[simp] lemma one_div (a : G₀) : 1 / a = a⁻¹ := one_mul _ @[simp] lemma zero_div (a : G₀) : 0 / a = 0 := zero_mul _ @[simp] lemma div_zero (a : G₀) : a / 0 = 0 := show a * 0⁻¹ = 0, by rw [inv_zero, mul_zero] @[simp] lemma div_mul_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a / b * b = a := inv_mul_cancel_right' h a lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a) @[simp] lemma mul_div_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a * b / b = a := mul_inv_cancel_right' h a lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a) lemma mul_div_assoc {a b c : G₀} : a * b / c = a * (b / c) := mul_assoc _ _ _ local attribute [simp] div_eq_mul_inv mul_comm mul_assoc mul_left_comm lemma div_eq_mul_one_div (a b : G₀) : a / b = a * (1 / b) := by simp lemma mul_one_div_cancel {a : G₀} (h : a ≠ 0) : a * (1 / a) = 1 := by simp [h] lemma one_div_mul_cancel {a : G₀} (h : a ≠ 0) : (1 / a) * a = 1 := by simp [h] lemma one_div_one : 1 / 1 = (1:G₀) := div_self (ne.symm zero_ne_one) lemma one_div_ne_zero {a : G₀} (h : a ≠ 0) : 1 / a ≠ 0 := by simpa only [one_div] using inv_ne_zero h lemma eq_one_div_of_mul_eq_one {a b : G₀} (h : a * b = 1) : b = 1 / a := by simpa only [one_div] using eq_inv_of_mul_right_eq_one h lemma eq_one_div_of_mul_eq_one_left {a b : G₀} (h : b * a = 1) : b = 1 / a := by simpa only [one_div] using eq_inv_of_mul_left_eq_one h @[simp] lemma one_div_div (a b : G₀) : 1 / (a / b) = b / a := by rw [one_div, div_eq_mul_inv, mul_inv_rev', inv_inv', div_eq_mul_inv] lemma one_div_one_div (a : G₀) : 1 / (1 / a) = a := by simp lemma eq_of_one_div_eq_one_div {a b : G₀} (h : 1 / a = 1 / b) : a = b := by rw [← one_div_one_div a, h, one_div_one_div] variables {a b c : G₀} @[simp] lemma inv_eq_zero {a : G₀} : a⁻¹ = 0 ↔ a = 0 := by rw [inv_eq_iff, inv_zero, eq_comm] @[simp] lemma zero_eq_inv {a : G₀} : 0 = a⁻¹ ↔ 0 = a := eq_comm.trans $ inv_eq_zero.trans eq_comm lemma one_div_mul_one_div_rev (a b : G₀) : (1 / a) * (1 / b) = 1 / (b * a) := by simp only [div_eq_mul_inv, one_mul, mul_inv_rev'] theorem divp_eq_div (a : G₀) (u : units G₀) : a /ₚ u = a / u := congr_arg _ $ u.coe_inv' @[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma inv_div : (a / b)⁻¹ = b / a := (mul_inv_rev' _ _).trans (by rw inv_inv'; refl) lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ := (mul_inv_rev' _ _).symm lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := mul_ne_zero ha (inv_ne_zero hb) @[simp] lemma div_eq_zero_iff : a / b = 0 ↔ a = 0 ∨ b = 0:= by simp [div_eq_mul_inv] lemma div_ne_zero_iff : a / b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr div_eq_zero_iff).trans not_or_distrib lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_left_inj] lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := ⟨λ h, by rw [← h, div_mul_cancel _ hb], λ h, by rw [← h, mul_div_cancel _ hb]⟩ lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b := by rw [eq_comm, div_eq_iff_mul_eq hc] lemma div_eq_of_eq_mul {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : y = z * x) : y / x = z := (div_eq_iff_mul_eq hx).2 h.symm lemma eq_div_of_mul_eq {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : z * x = y) : z = y / x := eq.symm $ div_eq_of_eq_mul hx h.symm lemma eq_of_div_eq_one (h : a / b = 1) : a = b := begin classical, by_cases hb : b = 0, { rw [hb, div_zero] at h, exact eq_of_zero_eq_one h a b }, { rwa [div_eq_iff_mul_eq hb, one_mul, eq_comm] at h } end lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, λ h, h.symm ▸ div_self hb⟩ lemma div_mul_left {a b : G₀} (hb : b ≠ 0) : b / (a * b) = 1 / a := by simp only [div_eq_mul_inv, mul_inv_rev', mul_inv_cancel_left' hb, one_mul] lemma mul_div_mul_right (a b : G₀) {c : G₀} (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev', mul_assoc, mul_inv_cancel_left' hc] lemma mul_mul_div (a : G₀) {b : G₀} (hb : b ≠ 0) : a = a * b * (1 / b) := by simp [hb] end group_with_zero section comm_group_with_zero -- comm variables [comm_group_with_zero G₀] {a b c : G₀} @[priority 10] -- see Note [lower instance priority] instance comm_group_with_zero.comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero G₀ := { ..group_with_zero.cancel_monoid_with_zero, ..comm_group_with_zero.to_comm_monoid_with_zero G₀ } /-- Pullback a `comm_group_with_zero` class along an injective function. -/ protected def function.injective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : comm_group_with_zero G₀' := { .. hf.group_with_zero f zero one mul inv, .. hf.comm_semigroup f mul } /-- Pushforward a `comm_group_with_zero` class along an surjective function. -/ protected def function.surjective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : comm_group_with_zero G₀' := { .. hf.group_with_zero h01 f zero one mul inv, .. hf.comm_semigroup f mul } lemma mul_inv' : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev', mul_comm] lemma one_div_mul_one_div (a b : G₀) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [one_div_mul_one_div_rev, mul_comm b] lemma div_mul_right {a : G₀} (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := by rw [mul_comm, div_mul_left ha] lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b := by rw [mul_comm, mul_div_cancel_of_imp h] lemma mul_div_cancel_left {a : G₀} (b : G₀) (ha : a ≠ 0) : a * b / a = b := mul_div_cancel_left_of_imp $ λ h, (ha h).elim lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a := by rw [mul_comm, div_mul_cancel_of_imp h] lemma mul_div_cancel' (a : G₀) {b : G₀} (hb : b ≠ 0) : b * (a / b) = a := by rw [mul_comm, (div_mul_cancel _ hb)] local attribute [simp] mul_assoc mul_comm mul_left_comm lemma div_mul_div (a b c d : G₀) : (a / b) * (c / d) = (a * c) / (b * d) := by simp [div_eq_mul_inv, mul_inv'] lemma mul_div_mul_left (a b : G₀) {c : G₀} (hc : c ≠ 0) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, mul_div_mul_right _ _ hc] @[field_simps] lemma div_mul_eq_mul_div (a b c : G₀) : (b / c) * a = (b * a) / c := by simp [div_eq_mul_inv] lemma div_mul_eq_mul_div_comm (a b c : G₀) : (b / c) * a = b * (a / c) := by rw [div_mul_eq_mul_div, ← one_mul c, ← div_mul_div, div_one, one_mul] lemma mul_eq_mul_of_div_eq_div (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [← mul_one (a*d), mul_assoc, mul_comm d, ← mul_assoc, ← div_self hb, ← div_mul_eq_mul_div_comm, h, div_mul_eq_mul_div, div_mul_cancel _ hd] @[field_simps] lemma div_div_eq_mul_div (a b c : G₀) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, one_div_div, ← mul_div_assoc] @[field_simps] lemma div_div_eq_div_mul (a b c : G₀) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, div_mul_div, mul_one] lemma div_div_div_div_eq (a : G₀) {b c d : G₀} : (a / b) / (c / d) = (a * d) / (b * c) := by rw [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul] lemma div_mul_eq_div_mul_one_div (a b c : G₀) : a / (b * c) = (a / b) * (1 / c) := by rw [← div_div_eq_div_mul, ← div_eq_mul_one_div] /-- Dividing `a` by the result of dividing `a` by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma div_div_self (a : G₀) : a / (a / a) = a := begin rw div_div_eq_mul_div, exact mul_self_div_self a end lemma ne_zero_of_one_div_ne_zero {a : G₀} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, div_zero] at h, contradiction end lemma eq_zero_of_one_div_eq_zero {a : G₀} (h : 1 / a = 0) : a = 0 := classical.by_cases (assume ha, ha) (assume ha, ((one_div_ne_zero ha) h).elim) lemma div_helper {a : G₀} (b : G₀) (h : a ≠ 0) : (1 / (a * b)) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] end comm_group_with_zero section comm_group_with_zero variables [comm_group_with_zero G₀] {a b c d : G₀} lemma div_eq_inv_mul : a / b = b⁻¹ * a := mul_comm _ _ lemma mul_div_right_comm (a b c : G₀) : (a * b) / c = (a / c) * b := by rw [div_eq_mul_inv, mul_assoc, mul_comm b, ← mul_assoc]; refl lemma mul_comm_div' (a b c : G₀) : (a / b) * c = a * (c / b) := by rw [← mul_div_assoc, mul_div_right_comm] lemma div_mul_comm' (a b c : G₀) : (a / b) * c = (c / b) * a := by rw [div_mul_eq_mul_div, mul_comm, mul_div_right_comm] lemma mul_div_comm (a b c : G₀) : a * (b / c) = b * (a / c) := by rw [← mul_div_assoc, mul_comm, mul_div_assoc] lemma div_right_comm (a : G₀) : (a / b) / c = (a / c) / b := by rw [div_div_eq_div_mul, div_div_eq_div_mul, mul_comm] lemma div_div_div_cancel_right (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [div_div_eq_mul_div, div_mul_cancel _ hc] lemma div_mul_div_cancel (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] @[field_simps] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := calc a / b = c / d ↔ a / b * (b * d) = c / d * (b * d) : by rw [mul_left_inj' (mul_ne_zero hb hd)] ... ↔ a * d = c * b : by rw [← mul_assoc, div_mul_cancel _ hb, ← mul_assoc, mul_right_comm, div_mul_cancel _ hd] @[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff _ _ a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff _ _ c 1 a b one_ne_zero hb lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := by rw [div_eq_mul_inv, inv_div, mul_div_cancel' _ ha] end comm_group_with_zero namespace semiconj_by @[simp] lemma zero_right [mul_zero_class G₀] (a : G₀) : semiconj_by a 0 0 := by simp only [semiconj_by, mul_zero, zero_mul] @[simp] lemma zero_left [mul_zero_class G₀] (x y : G₀) : semiconj_by 0 x y := by simp only [semiconj_by, mul_zero, zero_mul] variables [group_with_zero G₀] {a x y x' y' : G₀} @[simp] lemma inv_symm_left_iff' : semiconj_by a⁻¹ x y ↔ semiconj_by a y x := classical.by_cases (λ ha : a = 0, by simp only [ha, inv_zero, semiconj_by.zero_left]) (λ ha, @units_inv_symm_left_iff _ _ (units.mk0 a ha) _ _) lemma inv_symm_left' (h : semiconj_by a x y) : semiconj_by a⁻¹ y x := semiconj_by.inv_symm_left_iff'.2 h lemma inv_right' (h : semiconj_by a x y) : semiconj_by a x⁻¹ y⁻¹ := begin classical, by_cases ha : a = 0, { simp only [ha, zero_left] }, by_cases hx : x = 0, { subst x, simp only [semiconj_by, mul_zero, @eq_comm _ _ (y * a), mul_eq_zero] at h, simp [h.resolve_right ha] }, { have := mul_ne_zero ha hx, rw [h.eq, mul_ne_zero_iff] at this, exact @units_inv_right _ _ _ (units.mk0 x hx) (units.mk0 y this.1) h }, end @[simp] lemma inv_right_iff' : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := ⟨λ h, inv_inv' x ▸ inv_inv' y ▸ h.inv_right', inv_right'⟩ lemma div_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x / x') (y / y') := h.mul_right h'.inv_right' end semiconj_by namespace commute @[simp] theorem zero_right [mul_zero_class G₀] (a : G₀) :commute a 0 := semiconj_by.zero_right a @[simp] theorem zero_left [mul_zero_class G₀] (a : G₀) : commute 0 a := semiconj_by.zero_left a a variables [group_with_zero G₀] {a b c : G₀} @[simp] theorem inv_left_iff' : commute a⁻¹ b ↔ commute a b := semiconj_by.inv_symm_left_iff' theorem inv_left' (h : commute a b) : commute a⁻¹ b := inv_left_iff'.2 h @[simp] theorem inv_right_iff' : commute a b⁻¹ ↔ commute a b := semiconj_by.inv_right_iff' theorem inv_right' (h : commute a b) : commute a b⁻¹ := inv_right_iff'.2 h theorem inv_inv' (h : commute a b) : commute a⁻¹ b⁻¹ := h.inv_left'.inv_right' @[simp] theorem div_right (hab : commute a b) (hac : commute a c) : commute a (b / c) := hab.div_right hac @[simp] theorem div_left (hac : commute a c) (hbc : commute b c) : commute (a / b) c := hac.mul_left hbc.inv_left' end commute namespace monoid_with_zero_hom variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero M₀] [nontrivial M₀] section monoid_with_zero variables (f : monoid_with_zero_hom G₀ M₀) {a : G₀} lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 := ⟨λ hfa ha, hfa $ ha.symm ▸ f.map_zero, λ ha, ((is_unit.mk0 a ha).map f.to_monoid_hom).ne_zero⟩ lemma map_eq_zero : f a = 0 ↔ a = 0 := by { classical, exact not_iff_not.1 f.map_ne_zero } end monoid_with_zero section group_with_zero variables (f : monoid_with_zero_hom G₀ G₀') (a b : G₀) /-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/ lemma map_inv' : f a⁻¹ = (f a)⁻¹ := begin classical, by_cases h : a = 0, by simp [h], apply eq_inv_of_mul_left_eq_one, rw [← f.map_mul, inv_mul_cancel h, f.map_one] end lemma map_div : f (a / b) = f a / f b := (f.map_mul _ _).trans $ _root_.congr_arg _ $ f.map_inv' b end group_with_zero end monoid_with_zero_hom @[simp] lemma monoid_hom.map_units_inv {M G₀ : Type*} [monoid M] [group_with_zero G₀] (f : M →* G₀) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ := by rw [← units.coe_map, ← units.coe_map, ← units.coe_inv', monoid_hom.map_inv]
db24f3ce50cf638a76a5f62ba3519139d6486467
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/5.8.1-4.6.5.lean
bb471e1eb40d203cf0634c295158ee7be724a1f7
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,415
lean
open classical lemma imp_or : ∀ p q: Prop, (p → q) → (¬p ∨ q) := begin intros p q h, apply by_cases, { assume: p, right, exact (h this) }, { assume: ¬ p, left, assumption } end #check imp_or lemma nall_en: ∀ α: Type, ∀ p: α -> Prop, (¬ ∀ x, p x) -> (∃ x, ¬ p x) := begin intros, apply by_contradiction, { intro hc, have: ∀ x, p x, { intro, apply by_contradiction, { intro hnpx, have: ∃ x, ¬ p x, from ⟨x, hnpx⟩, contradiction } }, contradiction } end #check nall_en variables (α : Type) (p q : α → Prop) variable a : α variable r : Prop example : (∃ x : α, r) → r := begin intro h, cases h, assumption end include a example : r → (∃ x : α, r) := begin intro hr, exact ⟨a, hr⟩ end example : (∃ x, p x ∧ r) ↔ (∃ x, p x) ∧ r := begin apply iff.intro, { intro h, cases h, constructor, exact ⟨h_w, h_h.1⟩, exact h_h.2 }, { intro h, cases h.1, existsi w, constructor, exact h_1, exact h.2 } end example : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := begin apply iff.intro, { intro h, cases h, cases h_h, left, exact ⟨h_w, h_h⟩, right, exact ⟨h_w, h_h⟩ }, { intro h, cases h, cases h, existsi h_w, left, assumption, cases h, existsi h_w, right, assumption, } end example : (∀ x, p x) ↔ ¬ (∃ x, ¬ p x) := begin apply iff.intro, { intro h, intro h1, cases h1 with w hnpw, exact hnpw (h w) }, { intro h, intro x, apply by_contradiction, { intro hnpx, have h2: (∃ x, ¬ p x), from ⟨x, hnpx⟩, contradiction } } end example : (∃ x, p x) ↔ ¬ (∀ x, ¬ p x) := begin apply iff.intro, { intro h, cases h, intro hnpx, exact hnpx h_w h_h }, { intro h, apply by_contradiction, { intro hc, have: ∀ x, ¬ p x, { intro x, intro hpx, have: ∃ x, p x, from ⟨x, hpx⟩, contradiction }, contradiction } } end example : (¬ ∃ x, p x) ↔ (∀ x, ¬ p x) := begin apply iff.intro, { intros h x, assume: p x, exact h ⟨x, this⟩ }, { intros h hnxp, cases hnxp with w pw, have: ¬ p w, from h w, contradiction } end example : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := begin apply iff.intro, { intro h, exact nall_en α p h, }, { intro h, cases h, intro hpx, have: p h_w, from hpx h_w, contradiction } end example : (∀ x, p x → r) ↔ (∃ x, p x) → r := begin apply iff.intro, { intros h he, cases he, exact h he_w he_h }, { intro h, intros x hpx, exact h ⟨x, hpx⟩ } end example : (∃ x, p x → r) ↔ (∀ x, p x) → r := begin apply iff.intro, { intros h hpx, cases h, have: p h_w, from hpx h_w, exact h_h this }, { intro h, have: (¬ ∀ x, p x) ∨ r, from imp_or (∀ x, p x) r h, cases this, { have: ∃ x, ¬ p x, from nall_en α p this, cases this with w hnpw, existsi w, intro, contradiction }, { existsi a, intro, assumption } } end example : (∃ x, r → p x) ↔ (r → ∃ x, p x) := begin apply iff.intro, { intros h hr, cases h, exact ⟨h_w, h_h hr⟩ }, { intro h, apply by_cases, { assume hr: r, cases (h hr) with w pw, existsi w, intro, assumption }, { assume: ¬ r, existsi a, intro hr, contradiction } } end
2596c0d163a85338962583822b6c8c64e1ab90c2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/integral/vitali_caratheodory.lean
61f2381d4b3af2a06dcdd1f5d3bfcf56399a5cb8
[ "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
29,653
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.measure.regular import topology.semicontinuous import measure_theory.integral.bochner import topology.instances.ereal /-! # Vitali-Carathéodory theorem Vitali-Carathéodory theorem asserts the following. Consider an integrable function `f : α → ℝ` on a space with a regular measure. Then there exists a function `g : α → ereal` such that `f x < g x` everywhere, `g` is lower semicontinuous, and the integral of `g` is arbitrarily close to that of `f`. This theorem is proved in this file, as `exists_lt_lower_semicontinuous_integral_lt`. Symmetrically, there exists `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. It follows from the previous statement applied to `-f`. It is formalized under the name `exists_upper_semicontinuous_lt_integral_gt`. The most classical version of Vitali-Carathéodory theorem only ensures a large inequality `f x ≤ g x`. For applications to the fundamental theorem of calculus, though, the strict inequality `f x < g x` is important. Therefore, we prove the stronger version with strict inequalities in this file. There is a price to pay: we require that the measure is `σ`-finite, which is not necessary for the classical Vitali-Carathéodory theorem. Since this is satisfied in all applications, this is not a real problem. ## Sketch of proof Decomposing `f` as the difference of its positive and negative parts, it suffices to show that a positive function can be bounded from above by a lower semicontinuous function, and from below by an upper semicontinuous function, with integrals close to that of `f`. For the bound from above, write `f` as a series `∑' n, cₙ * indicator (sₙ)` of simple functions. Then, approximate `sₙ` by a larger open set `uₙ` with measure very close to that of `sₙ` (this is possible by regularity of the measure), and set `g = ∑' n, cₙ * indicator (uₙ)`. It is lower semicontinuous as a series of lower semicontinuous functions, and its integral is arbitrarily close to that of `f`. For the bound from below, use finitely many terms in the series, and approximate `sₙ` from inside by a closed set `Fₙ`. Then `∑ n < N, cₙ * indicator (Fₙ)` is bounded from above by `f`, it is upper semicontinuous as a finite sum of upper semicontinuous functions, and its integral is arbitrarily close to that of `f`. The main pain point in the implementation is that one needs to jump between the spaces `ℝ`, `ℝ≥0`, `ℝ≥0∞` and `ereal` (and be careful that addition is not well behaved on `ereal`), and between `lintegral` and `integral`. We first show the bound from above for simple functions and the nonnegative integral (this is the main nontrivial mathematical point), then deduce it for general nonnegative functions, first for the nonnegative integral and then for the Bochner integral. Then we follow the same steps for the lower bound. Finally, we glue them together to obtain the main statement `exists_lt_lower_semicontinuous_integral_lt`. ## Related results Are you looking for a result on approximation by continuous functions (not just semicontinuous)? See result `measure_theory.Lp.continuous_map_dense`, in the file `measure_theory.continuous_map_dense`. ## References [Rudin, *Real and Complex Analysis* (Theorem 2.24)][rudin2006real] -/ open_locale ennreal nnreal open measure_theory measure_theory.measure variables {α : Type*} [topological_space α] [measurable_space α] [borel_space α] (μ : measure α) [weakly_regular μ] namespace measure_theory local infixr ` →ₛ `:25 := simple_func /-! ### Lower semicontinuous upper bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_le_lower_semicontinuous_lintegral_ge (f : α →ₛ ℝ≥0) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin induction f using measure_theory.simple_func.induction with c s hs f₁ f₂ H h₁ h₂ generalizing ε, { let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases h : ∫⁻ x, f x ∂μ = ⊤, { refine ⟨λ x, c, λ x, _, lower_semicontinuous_const, by simp only [ennreal.top_add, le_top, h]⟩, simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_self _ _ _ }, by_cases hc : c = 0, { refine ⟨λ x, 0, _, lower_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [lintegral_const, zero_mul, zero_le, ennreal.coe_zero] } }, have : μ s < μ s + ε / c, { have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨ε0, ennreal.coe_ne_top⟩, simpa using ennreal.add_lt_add_left _ this, simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, or_false, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and, restrict_apply] using h }, obtain ⟨u, su, u_open, μu⟩ : ∃ u ⊇ s, is_open u ∧ μ u < μ s + ε / c := s.exists_is_open_lt_of_lt _ this, refine ⟨set.indicator u (λ x, c), λ x, _, u_open.lower_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset su (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ u ≤ c * μ s + ε, by simpa only [hs, u_open.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ u ≤ c * (μ s + ε / c) : ennreal.mul_le_mul le_rfl μu.le ... = c * μ s + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { rcases h₁ (ennreal.half_pos ε0).ne' with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.half_pos ε0).ne' with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end open simple_func (eapprox_diff tsum_eapprox_diff) /-- Given a measurable function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_le_lower_semicontinuous_lintegral_ge (f : α → ℝ≥0∞) (hf : measurable f) {ε : ℝ≥0∞} (εpos : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases ennreal.exists_pos_sum_of_encodable' εpos ℕ with ⟨δ, δpos, hδ⟩, have : ∀ n, ∃ g : α → ℝ≥0, (∀ x, simple_func.eapprox_diff f n x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, simple_func.eapprox_diff f n x ∂μ + δ n) := λ n, simple_func.exists_le_lower_semicontinuous_lintegral_ge μ (simple_func.eapprox_diff f n) (δpos n).ne', choose g f_le_g gcont hg using this, refine ⟨λ x, (∑' n, g n x), λ x, _, _, _⟩, { rw ← tsum_eapprox_diff f hf, exact ennreal.tsum_le_tsum (λ n, ennreal.coe_le_coe.2 (f_le_g n x)) }, { apply lower_semicontinuous_tsum (λ n, _), exact ennreal.continuous_coe.comp_lower_semicontinuous (gcont n) (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { calc ∫⁻ x, ∑' (n : ℕ), g n x ∂μ = ∑' n, ∫⁻ x, g n x ∂μ : by rw lintegral_tsum (λ n, (gcont n).measurable.coe_nnreal_ennreal) ... ≤ ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ + δ n) : ennreal.tsum_le_tsum hg ... = ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ) + ∑' n, δ n : ennreal.tsum_add ... ≤ ∫⁻ (x : α), f x ∂μ + ε : begin refine add_le_add _ hδ.le, rw [← lintegral_tsum], { simp_rw [tsum_eapprox_diff f hf, le_refl] }, { assume n, exact (simple_func.measurable _).coe_nnreal_ennreal } end } end /-- Given a measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge [sigma_finite μ] (f : α → ℝ≥0) (fmeas : measurable f) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin have : ε / 2 ≠ 0 := (ennreal.half_pos ε0).ne', rcases exists_pos_lintegral_lt_of_sigma_finite μ this with ⟨w, wpos, wmeas, wint⟩, let f' := λ x, ((f x + w x : ℝ≥0) : ℝ≥0∞), rcases exists_le_lower_semicontinuous_lintegral_ge μ f' (fmeas.add wmeas).coe_nnreal_ennreal this with ⟨g, le_g, gcont, gint⟩, refine ⟨g, λ x, _, gcont, _⟩, { calc (f x : ℝ≥0∞) < f' x : by simpa [← ennreal.coe_lt_coe] using add_lt_add_left (wpos x) (f x) ... ≤ g x : le_g x }, { calc ∫⁻ (x : α), g x ∂μ ≤ ∫⁻ (x : α), f x + w x ∂μ + ε / 2 : gint ... = ∫⁻ (x : α), f x ∂ μ + ∫⁻ (x : α), w x ∂ μ + (ε / 2) : by rw lintegral_add fmeas.coe_nnreal_ennreal wmeas.coe_nnreal_ennreal ... ≤ ∫⁻ (x : α), f x ∂ μ + ε / 2 + ε / 2 : add_le_add_right (add_le_add_left wint.le _) _ ... = ∫⁻ (x : α), f x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] }, end /-- Given an almost everywhere measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable [sigma_finite μ] (f : α → ℝ≥0) (fmeas : ae_measurable f μ) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin have : ε / 2 ≠ 0 := (ennreal.half_pos ε0).ne', rcases exists_lt_lower_semicontinuous_lintegral_ge μ (fmeas.mk f) fmeas.measurable_mk this with ⟨g0, f_lt_g0, g0_cont, g0_int⟩, rcases exists_measurable_superset_of_null fmeas.ae_eq_mk with ⟨s, hs, smeas, μs⟩, rcases exists_le_lower_semicontinuous_lintegral_ge μ (s.indicator (λ x, ∞)) (measurable_const.indicator smeas) this with ⟨g1, le_g1, g1_cont, g1_int⟩, refine ⟨λ x, g0 x + g1 x, λ x, _, g0_cont.add g1_cont, _⟩, { by_cases h : x ∈ s, { have := le_g1 x, simp only [h, set.indicator_of_mem, top_le_iff] at this, simp [this] }, { have : f x = fmeas.mk f x, by { rw set.compl_subset_comm at hs, exact hs h }, rw this, exact (f_lt_g0 x).trans_le le_self_add } }, { calc ∫⁻ x, g0 x + g1 x ∂μ = ∫⁻ x, g0 x ∂μ + ∫⁻ x, g1 x ∂μ : lintegral_add g0_cont.measurable g1_cont.measurable ... ≤ (∫⁻ x, f x ∂μ + ε / 2) + (0 + ε / 2) : begin refine add_le_add _ _, { convert g0_int using 2, exact lintegral_congr_ae (fmeas.ae_eq_mk.fun_comp _) }, { convert g1_int, simp only [smeas, μs, lintegral_const, set.univ_inter, measurable_set.univ, lintegral_indicator, mul_zero, restrict_apply] } end ... = ∫⁻ x, f x ∂μ + ε : by simp only [add_assoc, ennreal.add_halves, zero_add] } end variable {μ} /-- Given an integrable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_integral_gt_nnreal [sigma_finite μ] (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (integrable (λ x, (g x).to_real) μ) ∧ (∫ x, (g x).to_real ∂μ < ∫ x, f x ∂μ + ε) := begin have fmeas : ae_measurable f μ, by { convert fint.ae_measurable.real_to_nnreal, ext1 x, simp only [real.to_nnreal_coe] }, lift ε to ℝ≥0 using εpos.le, obtain ⟨δ, δpos, hδε⟩ : ∃ δ : ℝ≥0, 0 < δ ∧ δ < ε, from exists_between εpos, have int_f_ne_top : ∫⁻ (a : α), (f a) ∂μ ≠ ∞ := (has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral).ne, rcases exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable μ f fmeas (ennreal.coe_ne_zero.2 δpos.ne') with ⟨g, f_lt_g, gcont, gint⟩, have gint_ne : ∫⁻ (x : α), g x ∂μ ≠ ∞ := ne_top_of_le_ne_top (by simpa) gint, have g_lt_top : ∀ᵐ (x : α) ∂μ, g x < ∞ := ae_lt_top gcont.measurable gint_ne, have Ig : ∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ = ∫⁻ (a : α), g a ∂μ, { apply lintegral_congr_ae, filter_upwards [g_lt_top] with _ hx, simp only [hx.ne, ennreal.of_real_to_real, ne.def, not_false_iff], }, refine ⟨g, f_lt_g, gcont, g_lt_top, _, _⟩, { refine ⟨gcont.measurable.ennreal_to_real.ae_measurable, _⟩, simp only [has_finite_integral_iff_norm, real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], convert gint_ne.lt_top using 1 }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { calc ennreal.to_real (∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ) = ennreal.to_real (∫⁻ (a : α), g a ∂μ) : by congr' 1 ... ≤ ennreal.to_real (∫⁻ (a : α), f a ∂μ + δ) : begin apply ennreal.to_real_mono _ gint, simpa using int_f_ne_top, end ... = ennreal.to_real (∫⁻ (a : α), f a ∂μ) + δ : by rw [ennreal.to_real_add int_f_ne_top ennreal.coe_ne_top, ennreal.coe_to_real] ... < ennreal.to_real (∫⁻ (a : α), f a ∂μ) + ε : add_lt_add_left hδε _ ... = (∫⁻ (a : α), ennreal.of_real ↑(f a) ∂μ).to_real + ε : by simp }, { apply filter.eventually_of_forall (λ x, _), simp }, { exact fmeas.coe_nnreal_real, }, { apply filter.eventually_of_forall (λ x, _), simp }, { apply gcont.measurable.ennreal_to_real.ae_measurable } } end /-! ### Upper semicontinuous lower bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_upper_semicontinuous_le_lintegral_le (f : α →ₛ ℝ≥0) (int_f : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin induction f using measure_theory.simple_func.induction with c s hs f₁ f₂ H h₁ h₂ generalizing ε, { let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases hc : c = 0, { refine ⟨λ x, 0, _, upper_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [hc, set.indicator_zero', lintegral_const, zero_mul, pi.zero_apply, simple_func.const_zero, zero_add, zero_le', simple_func.coe_zero, set.piecewise_eq_indicator, ennreal.coe_zero, simple_func.coe_piecewise, zero_le] } }, have μs_lt_top : μ s < ∞, by simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, or_false, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, restrict_apply measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, function.const_apply, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and] using int_f, have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨ε0, ennreal.coe_ne_top⟩, obtain ⟨F, Fs, F_closed, μF⟩ : ∃ F ⊆ s, is_closed F ∧ μ s < μ F + ε / c := hs.exists_is_closed_lt_add μs_lt_top.ne this.ne', refine ⟨set.indicator F (λ x, c), λ x, _, F_closed.upper_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset Fs (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ s ≤ c * μ F + ε, by simpa only [hs, F_closed.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ s ≤ c * (μ F + ε / c) : ennreal.mul_le_mul le_rfl μF.le ... = c * μ F + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { have A : ∫⁻ (x : α), f₁ x ∂μ + ∫⁻ (x : α), f₂ x ∂μ ≠ ⊤, by rwa ← lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal , rcases h₁ (ennreal.add_ne_top.1 A).1 (ennreal.half_pos ε0).ne' with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.add_ne_top.1 A).2 (ennreal.half_pos ε0).ne' with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_lintegral_le (f : α → ℝ≥0) (int_f : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin obtain ⟨fs, fs_le_f, int_fs⟩ : ∃ (fs : α →ₛ ℝ≥0), (∀ x, fs x ≤ f x) ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε/2) := begin have := ennreal.lt_add_right int_f (ennreal.half_pos ε0).ne', conv_rhs at this { rw lintegral_eq_nnreal (λ x, (f x : ℝ≥0∞)) μ }, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp only [lt_supr_iff] at this, rcases this with ⟨fs, fs_le_f, int_fs⟩, refine ⟨fs, λ x, by simpa only [ennreal.coe_le_coe] using fs_le_f x, _⟩, convert int_fs.le, rw ← simple_func.lintegral_eq_lintegral, refl end, have int_fs_lt_top : ∫⁻ x, fs x ∂μ ≠ ∞, { apply ne_top_of_le_ne_top int_f (lintegral_mono (λ x, _)), simpa only [ennreal.coe_le_coe] using fs_le_f x }, obtain ⟨g, g_le_fs, gcont, gint⟩ : ∃ g : α → ℝ≥0, (∀ x, g x ≤ fs x) ∧ upper_semicontinuous g ∧ (∫⁻ x, fs x ∂μ ≤ ∫⁻ x, g x ∂μ + ε/2) := fs.exists_upper_semicontinuous_le_lintegral_le int_fs_lt_top (ennreal.half_pos ε0).ne', refine ⟨g, λ x, (g_le_fs x).trans (fs_le_f x), gcont, _⟩, calc ∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε / 2 : int_fs ... ≤ (∫⁻ x, g x ∂μ + ε / 2) + ε / 2 : add_le_add gint le_rfl ... = ∫⁻ x, g x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_integral_le (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, (g x : ℝ)) μ) ∧ (∫ x, (f x : ℝ) ∂μ - ε ≤ ∫ x, g x ∂μ) := begin lift ε to ℝ≥0 using εpos.le, rw [nnreal.coe_pos, ← ennreal.coe_pos] at εpos, have If : ∫⁻ x, f x ∂ μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_upper_semicontinuous_le_lintegral_le f If.ne εpos.ne' with ⟨g, gf, gcont, gint⟩, have Ig : ∫⁻ x, g x ∂ μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) If, simpa using gf x }, refine ⟨g, gf, gcont, _, _⟩, { refine integrable.mono fint gcont.measurable.coe_nnreal_real.ae_measurable _, exact filter.eventually_of_forall (λ x, by simp [gf x]) }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { rw sub_le_iff_le_add, convert ennreal.to_real_mono _ gint, { simp, }, { rw ennreal.to_real_add Ig.ne ennreal.coe_ne_top, simp }, { simpa using Ig.ne } }, { apply filter.eventually_of_forall, simp }, { exact gcont.measurable.coe_nnreal_real.ae_measurable }, { apply filter.eventually_of_forall, simp }, { exact fint.ae_measurable } } end /-! ### Vitali-Carathéodory theorem -/ /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g > f` which is lower semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_lt_lower_semicontinuous_integral_lt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (f x : ereal) < g x) ∧ lower_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (∫ x, ereal.to_real (g x) ∂μ < ∫ x, f x ∂μ + ε) := begin let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, let fp : α → ℝ≥0 := λ x, real.to_nnreal (f x), have int_fp : integrable (λ x, (fp x : ℝ)) μ := hf.real_to_nnreal, rcases exists_lt_lower_semicontinuous_integral_gt_nnreal fp int_fp δpos with ⟨gp, fp_lt_gp, gpcont, gp_lt_top, gp_integrable, gpint⟩, let fm : α → ℝ≥0 := λ x, real.to_nnreal (-f x), have int_fm : integrable (λ x, (fm x : ℝ)) μ := hf.neg.real_to_nnreal, rcases exists_upper_semicontinuous_le_integral_le fm int_fm δpos with ⟨gm, gm_le_fm, gmcont, gm_integrable, gmint⟩, let g : α → ereal := λ x, (gp x : ereal) - (gm x), have ae_g : ∀ᵐ x ∂ μ, (g x).to_real = (gp x : ereal).to_real - (gm x : ereal).to_real, { filter_upwards [gp_lt_top] with _ hx, rw ereal.to_real_sub; simp [hx.ne], }, refine ⟨g, _, _, _, _, _⟩, show integrable (λ x, ereal.to_real (g x)) μ, { rw integrable_congr ae_g, convert gp_integrable.sub gm_integrable, ext x, simp }, show ∫ (x : α), (g x).to_real ∂μ < ∫ (x : α), f x ∂μ + ε, from calc ∫ (x : α), (g x).to_real ∂μ = ∫ (x : α), ereal.to_real (gp x) - ereal.to_real (gm x) ∂μ : integral_congr_ae ae_g ... = ∫ (x : α), ereal.to_real (gp x) ∂ μ - ∫ (x : α), gm x ∂μ : begin simp only [ereal.to_real_coe_ennreal, ennreal.coe_to_real, coe_coe], exact integral_sub gp_integrable gm_integrable, end ... < ∫ (x : α), ↑(fp x) ∂μ + ↑δ - ∫ (x : α), gm x ∂μ : begin apply sub_lt_sub_right, convert gpint, simp only [ereal.to_real_coe_ennreal], end ... ≤ ∫ (x : α), ↑(fp x) ∂μ + ↑δ - (∫ (x : α), fm x ∂μ - δ) : sub_le_sub_left gmint _ ... = ∫ (x : α), f x ∂μ + 2 * δ : by { simp_rw [integral_eq_integral_pos_part_sub_integral_neg_part hf, fp, fm], ring } ... = ∫ (x : α), f x ∂μ + ε : by { congr' 1, field_simp [δ, mul_comm] }, show ∀ᵐ (x : α) ∂μ, g x < ⊤, { filter_upwards [gp_lt_top] with _ hx, simp [g, ereal.sub_eq_add_neg, lt_top_iff_ne_top, lt_top_iff_ne_top.1 hx], }, show ∀ x, (f x : ereal) < g x, { assume x, rw ereal.coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (f x), refine ereal.sub_lt_sub_of_lt_of_le _ _ _ _, { simp only [ereal.coe_ennreal_lt_coe_ennreal_iff, coe_coe], exact (fp_lt_gp x) }, { simp only [ennreal.coe_le_coe, ereal.coe_ennreal_le_coe_ennreal_iff, coe_coe], exact (gm_le_fm x) }, { simp only [ereal.coe_ennreal_ne_bot, ne.def, not_false_iff, coe_coe] }, { simp only [ereal.coe_nnreal_ne_top, ne.def, not_false_iff, coe_coe] } }, show lower_semicontinuous g, { apply lower_semicontinuous.add', { exact continuous_coe_ennreal_ereal.comp_lower_semicontinuous gpcont (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy) }, { apply ereal.continuous_neg.comp_upper_semicontinuous_antitone _ (λ x y hxy, ereal.neg_le_neg_iff.2 hxy), dsimp, apply continuous_coe_ennreal_ereal.comp_upper_semicontinuous _ (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy), exact ennreal.continuous_coe.comp_upper_semicontinuous gmcont (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { assume x, exact ereal.continuous_at_add (by simp) (by simp) } } end /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_upper_semicontinuous_lt_integral_gt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (g x : ereal) < f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂μ, ⊥ < g x) ∧ (∫ x, f x ∂μ < ∫ x, ereal.to_real (g x) ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_integral_lt (λ x, - f x) hf.neg εpos with ⟨g, g_lt_f, gcont, g_integrable, g_lt_top, gint⟩, refine ⟨λ x, - g x, _, _, _, _, _⟩, { exact λ x, ereal.neg_lt_iff_neg_lt.1 (by simpa only [ereal.coe_neg] using g_lt_f x) }, { exact ereal.continuous_neg.comp_lower_semicontinuous_antitone gcont (λ x y hxy, ereal.neg_le_neg_iff.2 hxy) }, { convert g_integrable.neg, ext x, simp }, { simpa [bot_lt_iff_ne_bot, lt_top_iff_ne_top] using g_lt_top }, { simp_rw [integral_neg, lt_neg_add_iff_add_lt] at gint, rw add_comm at gint, simpa [integral_neg] using gint } end end measure_theory
4823b1c8639761f7f7dc1f9c36db1aba687228c5
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/envExtensionSealed.lean
22ed3e1018fa3dd3238cb5ec27d5fd7ed5436a57
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
239
lean
import Lean namespace Lean def ex1 : CoreM Nat := do env ← getEnv; pure $ privateExt.getState env #eval ex1 def ex2 : CoreM Nat := do env ← getEnv; pure $ { privateExt with idx := 3 }.getState env -- Error -- #eval ex2 end Lean
e07ec231781272d17acedb931d1b7b6bff0d330b
5719a16e23dfc08cdea7a5bf035b81690f307965
/src/Init/Lean/Data/Json/Parser.lean
d4967cbd08609390a89dc7536a6e5eb2f2c2b11e
[ "Apache-2.0" ]
permissive
postmasters/lean4
488b03969a371e1507e1e8a4df9ebf63c7cbe7ac
f3976fc53a883ac7606fc59357d43f4b51016ca7
refs/heads/master
1,655,582,707,480
1,588,682,595,000
1,588,682,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,250
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ prelude import Init.Lean.Data.Json.Basic import Init.Control.Except namespace Lean inductive Quickparse.Result (α : Type) | success {} (pos : String.Iterator) (res : α) : Quickparse.Result | error {} (pos : String.Iterator) (err : String) : Quickparse.Result def Quickparse (α : Type) : Type := String.Iterator → Quickparse.Result α instance Quickparse.inhabited (α : Type) : Inhabited (Quickparse α) := ⟨fun it => Quickparse.Result.error it ""⟩ namespace Quickparse open Result partial def skipWs : String.Iterator → String.Iterator | it => if it.hasNext then let c := it.curr; if c = '\u0009' ∨ c = '\u000a' ∨ c = '\u000d' ∨ c = '\u0020' then skipWs it.next else it else it @[inline] protected def pure {α : Type} (a : α) : Quickparse α | it => success it a @[inline] protected def bind {α β : Type} (f : Quickparse α) (g : α → Quickparse β) : Quickparse β | it => match f it with | success rem a => g a rem | error pos msg => error pos msg @[inline] def fail {α : Type} (msg : String) : Quickparse α | it => error it msg @[inline] instance : Monad Quickparse := { pure := @Quickparse.pure, bind := @Quickparse.bind } def unexpectedEndOfInput := "unexpected end of input" @[inline] def peek? : Quickparse (Option Char) | it => if it.hasNext then success it it.curr else success it none @[inline] def peek! : Quickparse Char := do some c <- peek? | fail unexpectedEndOfInput; pure c @[inline] def skip : Quickparse Unit | it => success it.next () @[inline] def next : Quickparse Char := do c ← peek!; skip; pure c def expect (s : String) : Quickparse Unit | it => if it.extract (it.forward s.length) = s then success (it.forward s.length) () else error it ("expected: " ++ s) @[inline] def ws : Quickparse Unit | it => success (skipWs it) () def expectedEndOfInput := "expected end of input" @[inline] def eoi : Quickparse Unit | it => if it.hasNext then error it expectedEndOfInput else success it () end Quickparse namespace Json.Parser open Quickparse @[inline] def hexChar : Quickparse Nat := do c ← next; if '0' ≤ c ∧ c ≤ '9' then pure $ c.val.toNat - '0'.val.toNat else if 'a' ≤ c ∧ c ≤ 'f' then pure $ c.val.toNat - 'a'.val.toNat else if 'A' ≤ c ∧ c ≤ 'F' then pure $ c.val.toNat - 'A'.val.toNat else fail "invalid hex character" def escapedChar : Quickparse Char := do c ← next; match c with | '\\' => pure '\\' | '"' => pure '"' | '/' => pure '/' | 'b' => pure '\x08' | 'f' => pure '\x0c' | 'n' => pure '\n' | 'r' => pure '\x0d' | 't' => pure '\t' | 'u' => do u1 ← hexChar; u2 ← hexChar; u3 ← hexChar; u4 ← hexChar; pure $ Char.ofNat $ 4096*u1 + 256*u2 + 16*u3 + u4 | _ => fail "illegal \\u escape" partial def strCore : String → Quickparse String | acc => do c ← peek!; if c = '"' then do -- " skip; pure acc else do c ← next; ec ← if c = '\\' then escapedChar -- as to whether c.val > 0xffff should be split up and encoded with multiple \u, -- the JSON standard is not definite: both directly printing the character -- and encoding it with multiple \u is allowed. we choose the former. else if 0x0020 ≤ c.val ∧ c.val ≤ 0x10ffff then pure c else fail "unexpected character in string"; strCore (acc.push ec) def str : Quickparse String := strCore "" partial def natCore : Nat → Nat → Quickparse (Nat × Nat) | acc, digits => do some c ← peek? | pure (acc, digits); if '0' ≤ c ∧ c ≤ '9' then do skip; let acc' := 10*acc + (c.val.toNat - '0'.val.toNat); natCore acc' (digits+1) else pure (acc, digits) @[inline] def lookahead (p : Char → Prop) (desc : String) [DecidablePred p] : Quickparse Unit := do c <- peek!; if p c then pure () else fail $ "expected " ++ desc @[inline] def natNonZero : Quickparse Nat := do lookahead (fun c => '1' ≤ c ∧ c ≤ '9') "1-9"; (n, _) ← natCore 0 0; pure n @[inline] def natNumDigits : Quickparse (Nat × Nat) := do lookahead (fun c => '0' ≤ c ∧ c ≤ '9') "digit"; natCore 0 0 @[inline] def natMaybeZero : Quickparse Nat := do (n, _) ← natNumDigits; pure n def num : Quickparse JsonNumber := do c ← peek!; sign ← if c = '-' then do skip; pure (-1 : Int) else pure 1; c ← peek!; res ← if c = '0' then do skip; pure 0 else natNonZero; let res := JsonNumber.fromInt (sign * res); c? ← peek?; res ← if c? = some '.' then do skip; (n, d) ← natNumDigits; if d > usizeSz then do fail "too many decimals" else pure (); let mantissa' := res.mantissa * (10^d : Nat) + n; let exponent' := res.exponent + d; pure $ JsonNumber.mk mantissa' exponent' else pure res; c? ← peek?; if c? = some 'e' ∨ c? = some 'E' then do skip; c ← peek!; if c = '-' then do skip; n ← natMaybeZero; pure (res.shiftr n) else do if c = '+' then skip else pure (); n ← natMaybeZero; if n > usizeSz then do fail "exp too large" else pure (); pure (res.shiftl n) else pure res partial def arrayCore (anyCore : Unit → Quickparse Json) : Array Json → Quickparse (Array Json) | acc => do hd ← anyCore (); let acc' := acc.push hd; c ← next; if c = ']' then do ws; pure acc' else if c = ',' then do ws; arrayCore acc' else fail "unexpected character in array" partial def objectCore : (Unit → Quickparse Json) → Quickparse (RBNode String (fun _ => Json)) | anyCore => do lookahead (fun c => c = '"') "\""; skip; -- " k ← strCore ""; ws; lookahead (fun c => c = ':') ":"; skip; ws; v ← anyCore (); c ← next; if c = '}' then do ws; pure (RBNode.singleton k v) else if c = ',' then do ws; kvs ← objectCore anyCore; pure (kvs.insert strLt k v) else fail "unexpected character in object" -- takes a unit parameter so that -- we can use the equation compiler and recursion partial def anyCore : Unit → Quickparse Json | () => do c ← peek!; if c = '[' then do skip; ws; c ← peek!; if c = ']' then do skip; ws; pure (Json.arr (Array.mkEmpty 0)) else do a ← arrayCore anyCore (Array.mkEmpty 4); pure (Json.arr a) else if c = '{' then do skip; ws; c ← peek!; if c = '}' then do skip; ws; pure (Json.obj (RBNode.leaf)) else do kvs ← objectCore anyCore; pure (Json.obj kvs) else if c = '"' then do --" skip; s ← strCore ""; ws; pure (Json.str s) else if c = 'f' then do expect "false"; ws; pure (Json.bool false) else if c = 't' then do expect "true"; ws; pure (Json.bool true) else if c = 'n' then do expect "null"; ws; pure Json.null else if c = '-' ∨ ('0' ≤ c ∧ c ≤ '9') then do n ← num; ws; pure (Json.num n) else fail "unexpected input" def any : Quickparse Json := do ws; res ← anyCore (); eoi; pure res end Json.Parser namespace Json def parse (s : String) : Except String Lean.Json := match Json.Parser.any s.mkIterator with | Result.success _ res => Except.ok res | Result.error it err => Except.error ("offset " ++ it.i.repr ++ ": " ++ err) end Json end Lean
b847a66f1eebaf04765680ca806a0d61dd38025e
618003631150032a5676f229d13a079ac875ff77
/src/tactic/trunc_cases.lean
0a29981f47e1b0aacc5ce980cc2818bf0957d4b5
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
4,423
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.chain import data.quot namespace tactic /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_subsingleton (e : expr) (ids : list name) : tactic expr := do -- When the target is a subsingleton, -- we can just use induction along `trunc.rec_on_subsingleton`, -- generating just a single goal. [(_, [e], _)] ← tactic.induction e ids `trunc.rec_on_subsingleton, return e /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_nondependent (e : expr) (ids : list name) : tactic expr := do -- We may as well just use `trunc.lift_on`. -- (It would be nice if we could use the `induction` tactic with non-dependent recursors, too?) -- (In fact, the general strategy works just as well here, -- except that it leaves a beta redex in the invariance goal.) to_expr ``(trunc.lift_on %%e) >>= tactic.fapply, -- Replace the hypothesis `e` with the unboxed version. tactic.clear e, e ← tactic.intro e.local_pp_name, -- In the invariance goal, introduce the two arguments using the specified identifiers tactic.swap, match ids.nth 1 with | some n := tactic.intro n | none := tactic.intro1 end, match ids.nth 2 with | some n := tactic.intro n | none := tactic.intro1 end, tactic.swap, return e /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_dependent (e : expr) (ids : list name) : tactic expr := do -- If all else fails, just use the general induction principle. [(_, [e], _), (_, [e_a, e_b, e_p], _)] ← tactic.induction e ids, -- However even now we can do something useful: -- the invariance goal has a useless `e_p : true` hypothesis, -- and after casing on that we may be able to simplify away -- the `eq.rec`. swap, (tactic.cases e_p >> `[try { simp only [eq_rec_constant] }]), swap, return e namespace interactive open interactive open interactive.types open tactic /-- `trunc_cases e` performs case analysis on a `trunc` expression `e`, attempting the following strategies: 1. when the goal is a subsingleton, calling `induction e using trunc.rec_on_subsingleton`, 2. when the goal does not depend on `e`, calling `fapply trunc.lift_on e`, and using `intro` and `clear` afterwards to make the goals look like we used `induction`, 3. otherwise, falling through to `trunc.rec_on`, and in the new invariance goal calling `cases h_p` on the useless `h_p : true` hypothesis, and then attempting to simplify the `eq.rec`. `trunc_cases e with h` names the new hypothesis `h`. If `e` is a local hypothesis already, `trunc_cases` defaults to reusing the same name. `trunc_cases e with h h_a h_b` will use the names `h_a` and `h_b` for the new hypothesis in the invariance goal if `trunc_cases` uses `trunc.lift_on` or `trunc.rec_on`. Finally, if the new hypothesis from inside the `trunc` is a type class, `trunc_cases` resets the instance cache so that it is immediately available. -/ meta def trunc_cases (e : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← to_expr e, -- If `ids = []` and `e` is a local constant, we'll want to give -- the new unboxed hypothesis the same name. let ids := if ids = [] ∧ e.is_local_constant then [e.local_pp_name] else ids, -- Make a note of the expr `e`, or reuse `e` if it is already a local constant. e ← if e.is_local_constant then return e else (do n ← match ids.nth 0 with | some n := pure n | none := mk_fresh_name end, note n none e), -- Now check if the target is a subsingleton. tgt ← target, ss ← succeeds (mk_app `subsingleton [tgt] >>= mk_instance), -- In each branch here, we're going to capture the name of the new unboxed hypothesis -- so that we can later check if it's a typeclass and if so unfreeze local instances. e ← if ss then trunc_cases_subsingleton e ids else if e.occurs tgt then trunc_cases_dependent e ids else trunc_cases_nondependent e ids, c ← infer_type e >>= is_class, when c unfreeze_local_instances end interactive end tactic add_tactic_doc { name := "trunc_cases", category := doc_category.tactic, decl_names := [`tactic.interactive.trunc_cases], tags := ["case bashing"] }
6b86bd5130c4f0d028f2b7969db2f48d485b33f2
947b78d97130d56365ae2ec264df196ce769371a
/src/Std/Data/PersistentArray.lean
a842fe4e4a2091ed086ac4ff2fec00fe2aff5777
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,488
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 -/ universes u v w namespace Std inductive PersistentArrayNode (α : Type u) | node (cs : Array PersistentArrayNode) : PersistentArrayNode | leaf (vs : Array α) : PersistentArrayNode namespace PersistentArrayNode instance {α : Type u} : Inhabited (PersistentArrayNode α) := ⟨leaf #[]⟩ def isNode {α} : PersistentArrayNode α → Bool | node _ => true | leaf _ => false end PersistentArrayNode abbrev PersistentArray.initShift : USize := 5 abbrev PersistentArray.branching : USize := USize.ofNat (2 ^ PersistentArray.initShift.toNat) structure PersistentArray (α : Type u) := /- Recall that we run out of memory if we have more than `usizeSz/8` elements. So, we can stop adding elements at `root` after `size > usizeSz`, and keep growing the `tail`. This modification allow us to use `USize` instead of `Nat` when traversing `root`. -/ (root : PersistentArrayNode α := PersistentArrayNode.node (Array.mkEmpty PersistentArray.branching.toNat)) (tail : Array α := Array.mkEmpty PersistentArray.branching.toNat) (size : Nat := 0) (shift : USize := PersistentArray.initShift) (tailOff : Nat := 0) abbrev PArray (α : Type u) := PersistentArray α namespace PersistentArray /- TODO: use proofs for showing that array accesses are not out of bounds. We can do it after we reimplement the tactic framework. -/ variables {α : Type u} open Std.PersistentArrayNode def empty : PersistentArray α := {} def isEmpty (a : PersistentArray α) : Bool := a.size == 0 instance : Inhabited (PersistentArray α) := ⟨{}⟩ def mkEmptyArray : Array α := Array.mkEmpty branching.toNat abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1) partial def getAux [Inhabited α] : PersistentArrayNode α → USize → USize → α | node cs, i, shift => getAux (cs.get! (div2Shift i shift).toNat) (mod2Shift i shift) (shift - initShift) | leaf cs, i, _ => cs.get! i.toNat def get! [Inhabited α] (t : PersistentArray α) (i : Nat) : α := if i >= t.tailOff then t.tail.get! (i - t.tailOff) else getAux t.root (USize.ofNat i) t.shift def getOp [Inhabited α] (self : PersistentArray α) (idx : Nat) : α := self.get! idx partial def setAux : PersistentArrayNode α → USize → USize → α → PersistentArrayNode α | node cs, i, shift, a => let j := div2Shift i shift; let i := mod2Shift i shift; let shift := shift - initShift; node $ cs.modify j.toNat $ fun c => setAux c i shift a | leaf cs, i, _, a => leaf (cs.set! i.toNat a) def set (t : PersistentArray α) (i : Nat) (a : α) : PersistentArray α := if i >= t.tailOff then { t with tail := t.tail.set! (i - t.tailOff) a } else { t with root := setAux t.root (USize.ofNat i) t.shift a } @[specialize] partial def modifyAux [Inhabited α] (f : α → α) : PersistentArrayNode α → USize → USize → PersistentArrayNode α | node cs, i, shift => let j := div2Shift i shift; let i := mod2Shift i shift; let shift := shift - initShift; node $ cs.modify j.toNat $ fun c => modifyAux c i shift | leaf cs, i, _ => leaf (cs.modify i.toNat f) @[specialize] def modify [Inhabited α] (t : PersistentArray α) (i : Nat) (f : α → α) : PersistentArray α := if i >= t.tailOff then { t with tail := t.tail.modify (i - t.tailOff) f } else { t with root := modifyAux f t.root (USize.ofNat i) t.shift } partial def mkNewPath : USize → Array α → PersistentArrayNode α | shift, a => if shift == 0 then leaf a else node (mkEmptyArray.push (mkNewPath (shift - initShift) a)) partial def insertNewLeaf : PersistentArrayNode α → USize → USize → Array α → PersistentArrayNode α | node cs, i, shift, a => if i < branching then node (cs.push (leaf a)) else let j := div2Shift i shift; let i := mod2Shift i shift; let shift := shift - initShift; if j.toNat < cs.size then node $ cs.modify j.toNat $ fun c => insertNewLeaf c i shift a else node $ cs.push $ mkNewPath shift a | n, _, _, _ => n -- unreachable def mkNewTail (t : PersistentArray α) : PersistentArray α := if t.size <= (mul2Shift 1 (t.shift + initShift)).toNat then { t with tail := mkEmptyArray, root := insertNewLeaf t.root (USize.ofNat (t.size - 1)) t.shift t.tail, tailOff := t.size } else { t with tail := #[], root := let n := mkEmptyArray.push t.root; node (n.push (mkNewPath t.shift t.tail)), shift := t.shift + initShift, tailOff := t.size } def tooBig : Nat := usizeSz / 8 def push (t : PersistentArray α) (a : α) : PersistentArray α := let r := { t with tail := t.tail.push a, size := t.size + 1 }; if r.tail.size < branching.toNat || t.size >= tooBig then r else mkNewTail r private def emptyArray {α : Type u} : Array (PersistentArrayNode α) := Array.mkEmpty PersistentArray.branching.toNat partial def popLeaf : PersistentArrayNode α → Option (Array α) × Array (PersistentArrayNode α) | n@(node cs) => if h : cs.size ≠ 0 then let idx : Fin cs.size := ⟨cs.size - 1, Nat.predLt h⟩; let last := cs.get idx; let cs := cs.set idx (arbitrary _); match popLeaf last with | (none, _) => (none, emptyArray) | (some l, newLast) => if newLast.size == 0 then let cs := cs.pop; if cs.isEmpty then (some l, emptyArray) else (some l, cs) else (some l, cs.set idx (node newLast)) else (none, emptyArray) | leaf vs => (some vs, emptyArray) def pop (t : PersistentArray α) : PersistentArray α := if t.tail.size > 0 then { t with tail := t.tail.pop, size := t.size - 1 } else match popLeaf t.root with | (none, _) => t | (some last, newRoots) => let last := last.pop; let newSize := t.size - 1; let newTailOff := newSize - last.size; if newRoots.size == 1 && (newRoots.get! 0).isNode then { root := newRoots.get! 0, shift := t.shift - initShift, size := newSize, tail := last, tailOff := newTailOff } else { t with root := node newRoots, size := newSize, tail := last, tailOff := newTailOff } section variables {m : Type v → Type w} [Monad m] variable {β : Type v} @[specialize] partial def foldlMAux (f : β → α → m β) : PersistentArrayNode α → β → m β | node cs, b => cs.foldlM (fun b c => foldlMAux c b) b | leaf vs, b => vs.foldlM f b @[specialize] def foldlM (t : PersistentArray α) (f : β → α → m β) (b : β) : m β := do b ← foldlMAux f t.root b; t.tail.foldlM f b @[specialize] partial def findSomeMAux (f : α → m (Option β)) : PersistentArrayNode α → m (Option β) | node cs => cs.findSomeM? (fun c => findSomeMAux c) | leaf vs => vs.findSomeM? f @[specialize] def findSomeM? (t : PersistentArray α) (f : α → m (Option β)) : m (Option β) := do b ← findSomeMAux f t.root; match b with | none => t.tail.findSomeM? f | some b => pure (some b) @[specialize] partial def findSomeRevMAux (f : α → m (Option β)) : PersistentArrayNode α → m (Option β) | node cs => cs.findSomeRevM? (fun c => findSomeRevMAux c) | leaf vs => vs.findSomeRevM? f @[specialize] def findSomeRevM? (t : PersistentArray α) (f : α → m (Option β)) : m (Option β) := do b ← t.tail.findSomeRevM? f; match b with | none => findSomeRevMAux f t.root | some b => pure (some b) partial def foldlFromMAux (f : β → α → m β) : PersistentArrayNode α → USize → USize → β → m β | node cs, i, shift, b => do let j := (div2Shift i shift).toNat; b ← foldlFromMAux (cs.get! j) (mod2Shift i shift) (shift - initShift) b; cs.foldlFromM (fun b c => foldlMAux f c b) b (j+1) | leaf vs, i, _, b => vs.foldlFromM f b i.toNat def foldlFromM (t : PersistentArray α) (f : β → α → m β) (b : β) (ini : Nat) : m β := if ini >= t.tailOff then t.tail.foldlFromM f b (ini - t.tailOff) else do b ← foldlFromMAux f t.root (USize.ofNat ini) t.shift b; t.tail.foldlM f b @[specialize] partial def forMAux (f : α → m PUnit) : PersistentArrayNode α → m PUnit | node cs => cs.forM (fun c => forMAux c) | leaf vs => vs.forM f @[specialize] def forM (t : PersistentArray α) (f : α → m PUnit) : m PUnit := forMAux f t.root *> t.tail.forM f end @[inline] def foldl {β} (t : PersistentArray α) (f : β → α → β) (b : β) : β := Id.run (t.foldlM f b) @[inline] def filter (as : PersistentArray α) (p : α → Bool) : PersistentArray α := as.foldl (fun asNew a => if p a then asNew.push a else asNew) {} def toArray (t : PersistentArray α) : Array α := t.foldl Array.push #[] def append (t₁ t₂ : PersistentArray α) : PersistentArray α := if t₁.isEmpty then t₂ else t₂.foldl PersistentArray.push t₁ instance : HasAppend (PersistentArray α) := ⟨append⟩ @[inline] def findSome? {β} (t : PersistentArray α) (f : α → (Option β)) : Option β := Id.run (t.findSomeM? f) @[inline] def findSomeRev? {β} (t : PersistentArray α) (f : α → (Option β)) : Option β := Id.run (t.findSomeRevM? f) @[inline] def foldlFrom {β} (t : PersistentArray α) (f : β → α → β) (b : β) (ini : Nat) : β := Id.run (t.foldlFromM f b ini) def toList (t : PersistentArray α) : List α := (t.foldl (fun xs x => x :: xs) []).reverse section variables {m : Type → Type w} [Monad m] @[specialize] partial def anyMAux (p : α → m Bool) : PersistentArrayNode α → m Bool | node cs => cs.anyM (fun c => anyMAux c) | leaf vs => vs.anyM p @[specialize] def anyM (t : PersistentArray α) (p : α → m Bool) : m Bool := anyMAux p t.root <||> t.tail.anyM p @[inline] def allM (a : PersistentArray α) (p : α → m Bool) : m Bool := do b ← anyM a (fun v => do b ← p v; pure (not b)); pure (not b) end @[inline] def any (a : PersistentArray α) (p : α → Bool) : Bool := Id.run $ anyM a p @[inline] def all (a : PersistentArray α) (p : α → Bool) : Bool := !any a (fun v => !p v) section variables {m : Type u → Type v} [Monad m] variable {β : Type u} @[specialize] partial def mapMAux (f : α → m β) : PersistentArrayNode α → m (PersistentArrayNode β) | node cs => node <$> cs.mapM (fun c => mapMAux c) | leaf vs => leaf <$> vs.mapM f @[specialize] def mapM (f : α → m β) (t : PersistentArray α) : m (PersistentArray β) := do root ← mapMAux f t.root; tail ← t.tail.mapM f; pure { t with tail := tail, root := root } end @[inline] def map {β} (f : α → β) (t : PersistentArray α) : PersistentArray β := Id.run (t.mapM f) structure Stats := (numNodes : Nat) (depth : Nat) (tailSize : Nat) partial def collectStats : PersistentArrayNode α → Stats → Nat → Stats | node cs, s, d => cs.foldl (fun s c => collectStats c s (d+1)) { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth } | leaf vs, s, d => { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth } def stats (r : PersistentArray α) : Stats := collectStats r.root { numNodes := 0, depth := 0, tailSize := r.tail.size } 0 def Stats.toString (s : Stats) : String := "{nodes := " ++ toString s.numNodes ++ ", depth := " ++ toString s.depth ++ ", tail size := " ++ toString s.tailSize ++ "}" instance : HasToString Stats := ⟨Stats.toString⟩ end PersistentArray def mkPersistentArray {α : Type u} (n : Nat) (v : α) : PArray α := n.fold (fun i p => p.push v) PersistentArray.empty @[inline] def mkPArray {α : Type u} (n : Nat) (v : α) : PArray α := mkPersistentArray n v end Std open Std (PersistentArray PersistentArray.empty) def List.toPersistentArrayAux {α : Type u} : List α → PersistentArray α → PersistentArray α | [], t => t | x::xs, t => List.toPersistentArrayAux xs (t.push x) def List.toPersistentArray {α : Type u} (xs : List α) : PersistentArray α := xs.toPersistentArrayAux {} def Array.toPersistentArray {α : Type u} (xs : Array α) : PersistentArray α := xs.foldl (fun p x => p.push x) PersistentArray.empty @[inline] def Array.toPArray {α : Type u} (xs : Array α) : PersistentArray α := xs.toPersistentArray
02ed6bb6107fc5ddd70f113d70da0db9616e0b96
c780ac8c1c0dd4de0fb63981226f2b53b62fe298
/src/ring_theory.lean
92939893b98798c07414e984b5e598ee65260443
[]
no_license
jmvlangen/finite_fields
0672670245ccf62b2967d9c5edc2f60c53fc141f
757bd91636e0b3508ee21c92be775eb9d908391e
refs/heads/master
1,586,987,952,433
1,549,629,122,000
1,549,629,122,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,955
lean
import ring_theory.ideal_operations import data.equiv.algebra universes u v namespace ideal variable {α : Type u} variable [comm_ring α] lemma eq_bot (I : ideal α) : I = ⊥ ↔ ∀ x : α, x ∈ I → x = 0 := begin rw [lattice.eq_bot_iff, submodule.le_def], apply forall_congr, intro x, apply imp_congr_right, intro h, exact submodule.mem_bot end lemma bot_ne_top {α : Type u} [nonzero_comm_ring α] : (⊥ : ideal α) ≠ (⊤ : ideal α) := by rw[ideal.ne_top_iff_one, submodule.mem_bot]; exact one_ne_zero lemma mem_of_not_bot (I : ideal α) : I ≠ (⊥ : ideal α) → ∃ x ∈ I, (x : α) ≠ 0 := assume h : I ≠ ⊥, have ¬ (∀ x : α, x ∈ I → x = 0), by rw ←eq_bot; assumption, have ∃ x : α, ¬ (x ∈ I → x = 0), from classical.not_forall.mp this, let ⟨x, h₁⟩ := this in have x ∈ I, from classical.not_not.mp (not_not_of_not_imp h₁), have x ≠ 0, from not_of_not_imp h₁, show ∃ x ∈ I, (x : α) ≠ 0, from ⟨x, ‹x ∈ I›, ‹x ≠ 0›⟩ instance bot_is_prime {α : Type u} [integral_domain α] : is_prime (⊥ : ideal α) := begin apply and.intro bot_ne_top, intros x y h, repeat {rw[submodule.mem_bot]}, exact no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero x y (submodule.mem_bot.mp h) end end ideal namespace is_ring_hom open function ideal ideal.quotient variables {α : Type u} {β : Type v} variables [comm_ring α] [comm_ring β] def ker (f : α → β) [is_ring_hom f] : ideal α := comap f ⊥ variables {f : α → β} [is_ring_hom f] lemma mem_ker {x : α} : x ∈ ker f ↔ f x = 0 := submodule.mem_bot theorem ker_eq_bot : ker f = ⊥ ↔ injective f := begin rw [is_add_group_hom.injective_iff f, eq_bot], apply forall_congr, intro, rw mem_ker, end lemma le_ker {I : ideal α} : I ≤ ker f ↔ ∀ x : α, x ∈ I → f x = 0 := begin apply forall_congr, intro x, apply imp_congr_right, intro h, exact mem_ker end variable {I : ideal α} lemma map_mk_eq_bot : map_mk I I = ⊥ := suffices ∀ x : quotient I, x ∈ map_mk I I → x = 0, from (eq_bot (map_mk I I)).mpr this, assume x : quotient I, assume hx : x ∈ map_mk I I, have ∃ y : α, y ∈ I ∧ ideal.quotient.mk I y = x, from (set.mem_image (ideal.quotient.mk I) I x).mp hx, let ⟨y, hy, heq⟩ := this in have y - 0 ∈ I, from eq.symm (sub_zero y) ▸ hy, have x = ideal.quotient.mk I 0, from heq ▸ ideal.quotient.eq.mpr this, show x = 0, from eq.symm this ▸ mk_zero I /-- The homomorphism theorem for rings --/ def factor (f : α → β) [is_ring_hom f] (I : ideal α) (h : I ≤ ker f) : quotient I → β := lift I f (le_ker.mp h) lemma factor_to_ring_hom' {h : I ≤ ker f} : is_ring_hom (factor f I h) := ideal.quotient.is_ring_hom instance factor_to_ring_hom {h : I ≤ ker f} : is_ring_hom (factor f I h) := ideal.quotient.is_ring_hom theorem factor_commutes (h : I ≤ ker f) {x : α} : (factor f I h) (ideal.quotient.mk I x) = f x := lift_mk lemma ker_factor (h : I ≤ ker f) : ker (factor f I h) = map_mk I (ker f) := suffices ∀ x : quotient I, x ∈ ker (factor f I h) ↔ x ∈ map_mk I (ker f), from ext this, assume x : quotient I, suffices ∀ y : α, ideal.quotient.mk I y ∈ ker (factor f I h) ↔ ideal.quotient.mk I y ∈ map_mk I (ker f), from quotient.induction_on' x this, assume y : α, suffices y ∈ ker f ↔ ideal.quotient.mk I y ∈ map_mk I (ker f), by rw [mem_ker, factor_commutes h]; rw mem_ker at this; assumption, iff.intro (assume h0 : y ∈ ker f, show ideal.quotient.mk I y ∈ map_mk I (ker f), from set.mem_image_of_mem (ideal.quotient.mk I) h0) (assume h0 : ideal.quotient.mk I y ∈ map_mk I (ker f), have ∃ y' : α, y' ∈ ker f ∧ ideal.quotient.mk I y' = ideal.quotient.mk I y, from (set.mem_image (ideal.quotient.mk I) (ker f) (ideal.quotient.mk I y)).mp h0, let ⟨y', hy', heq⟩ := this in have y - y' ∈ I, from ideal.quotient.eq.mp (eq.symm heq), have y - y' ∈ ker f, from h this, show y ∈ ker f, from sub_add_cancel y y' ▸ (ker f).add this hy') /-- The first isomorphism theorem for rings --/ theorem factor_injective : injective (factor f (ker f) (le_refl (ker f))) := begin apply ker_eq_bot.mp, rw ker_factor, exact map_mk_eq_bot end theorem factor_surjective (h : I ≤ ker f) (hf : surjective f) : surjective (factor f I h) := assume y : β, have ∃ x : α, f x = y, from hf y, let ⟨x, hfx⟩ := this in have (factor f I h) (ideal.quotient.mk I x) = y, from hfx ▸ factor_commutes h, show ∃ x' : quotient I, (factor f I h) x' = y, from ⟨ideal.quotient.mk I x, this⟩ theorem factor_bijective (hf : surjective f) : bijective (factor f (ker f) (le_refl (ker f))) := ⟨factor_injective, factor_surjective (le_refl (ker f)) hf⟩ noncomputable theorem factor_iso (hf : surjective f) : quotient (ker f) ≃r β := ring_equiv.mk (equiv.of_bijective (factor_bijective hf)) factor_to_ring_hom' end is_ring_hom
f1858639ee804cb4d1c9563015b37a3f8f3ad0c9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/calculus/extend_deriv.lean
4e07fb7c59b692aa80aae4fdf4efd23363d5945a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
11,609
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.mean_value /-! # Extending differentiability to the boundary We investigate how differentiable functions inside a set extend to differentiable functions on the boundary. For this, it suffices that the function and its derivative admit limits there. A general version of this statement is given in `has_fderiv_at_boundary_of_tendsto_fderiv`. One-dimensional versions, in which one wants to obtain differentiability at the left endpoint or the right endpoint of an interval, are given in `has_deriv_at_interval_left_endpoint_of_tendsto_deriv` and `has_deriv_at_interval_right_endpoint_of_tendsto_deriv`. These versions are formulated in terms of the one-dimensional derivative `deriv ℝ f`. -/ variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] open filter set metric continuous_linear_map open_locale topological_space local attribute [mono] prod_mono /-- If a function `f` is differentiable in a convex open set and continuous on its closure, and its derivative converges to a limit `f'` at a point on the boundary, then `f` is differentiable there with derivative `f'`. -/ theorem has_fderiv_at_boundary_of_tendsto_fderiv {f : E → F} {s : set E} {x : E} {f' : E →L[ℝ] F} (f_diff : differentiable_on ℝ f s) (s_conv : convex ℝ s) (s_open : is_open s) (f_cont : ∀y ∈ closure s, continuous_within_at f s y) (h : tendsto (λy, fderiv ℝ f y) (𝓝[s] x) (𝓝 f')) : has_fderiv_within_at f f' (closure s) x := begin classical, -- one can assume without loss of generality that `x` belongs to the closure of `s`, as the -- statement is empty otherwise by_cases hx : x ∉ closure s, { rw ← closure_closure at hx, exact has_fderiv_within_at_of_not_mem_closure hx }, push_neg at hx, rw [has_fderiv_within_at, has_fderiv_at_filter, asymptotics.is_o_iff], /- One needs to show that `‖f y - f x - f' (y - x)‖ ≤ ε ‖y - x‖` for `y` close to `x` in `closure s`, where `ε` is an arbitrary positive constant. By continuity of the functions, it suffices to prove this for nearby points inside `s`. In a neighborhood of `x`, the derivative of `f` is arbitrarily close to `f'` by assumption. The mean value inequality completes the proof. -/ assume ε ε_pos, obtain ⟨δ, δ_pos, hδ⟩ : ∃ δ > 0, ∀ y ∈ s, dist y x < δ → ‖fderiv ℝ f y - f'‖ < ε, by simpa [dist_zero_right] using tendsto_nhds_within_nhds.1 h ε ε_pos, set B := ball x δ, suffices : ∀ y ∈ B ∩ (closure s), ‖f y - f x - (f' y - f' x)‖ ≤ ε * ‖y - x‖, from mem_nhds_within_iff.2 ⟨δ, δ_pos, λy hy, by simpa using this y hy⟩, suffices : ∀ p : E × E, p ∈ closure ((B ∩ s) ×ˢ (B ∩ s)) → ‖f p.2 - f p.1 - (f' p.2 - f' p.1)‖ ≤ ε * ‖p.2 - p.1‖, { rw closure_prod_eq at this, intros y y_in, apply this ⟨x, y⟩, have : B ∩ closure s ⊆ closure (B ∩ s), from is_open_ball.inter_closure, exact ⟨this ⟨mem_ball_self δ_pos, hx⟩, this y_in⟩ }, have key : ∀ p : E × E, p ∈ (B ∩ s) ×ˢ (B ∩ s) → ‖f p.2 - f p.1 - (f' p.2 - f' p.1)‖ ≤ ε * ‖p.2 - p.1‖, { rintros ⟨u, v⟩ ⟨u_in, v_in⟩, have conv : convex ℝ (B ∩ s) := (convex_ball _ _).inter s_conv, have diff : differentiable_on ℝ f (B ∩ s) := f_diff.mono (inter_subset_right _ _), have bound : ∀ z ∈ (B ∩ s), ‖fderiv_within ℝ f (B ∩ s) z - f'‖ ≤ ε, { intros z z_in, convert le_of_lt (hδ _ z_in.2 z_in.1), have op : is_open (B ∩ s) := is_open_ball.inter s_open, rw differentiable_at.fderiv_within _ (op.unique_diff_on z z_in), exact (diff z z_in).differentiable_at (is_open.mem_nhds op z_in) }, simpa using conv.norm_image_sub_le_of_norm_fderiv_within_le' diff bound u_in v_in }, rintros ⟨u, v⟩ uv_in, refine continuous_within_at.closure_le uv_in _ _ key, have f_cont' : ∀y ∈ closure s, continuous_within_at (f - f') s y, { intros y y_in, exact tendsto.sub (f_cont y y_in) (f'.cont.continuous_within_at) }, all_goals { -- common start for both continuity proofs have : (B ∩ s) ×ˢ (B ∩ s) ⊆ s ×ˢ s, by mono ; exact inter_subset_right _ _, obtain ⟨u_in, v_in⟩ : u ∈ closure s ∧ v ∈ closure s, by simpa [closure_prod_eq] using closure_mono this uv_in, apply continuous_within_at.mono _ this, simp only [continuous_within_at] }, rw nhds_within_prod_eq, { have : ∀ u v, f v - f u - (f' v - f' u) = f v - f' v - (f u - f' u) := by { intros, abel }, simp only [this], exact tendsto.comp continuous_norm.continuous_at ((tendsto.comp (f_cont' v v_in) tendsto_snd).sub $ tendsto.comp (f_cont' u u_in) tendsto_fst) }, { apply tendsto_nhds_within_of_tendsto_nhds, rw nhds_prod_eq, exact tendsto_const_nhds.mul (tendsto.comp continuous_norm.continuous_at $ tendsto_snd.sub tendsto_fst) }, end /-- If a function is differentiable on the right of a point `a : ℝ`, continuous at `a`, and its derivative also converges at `a`, then `f` is differentiable on the right at `a`. -/ lemma has_deriv_at_interval_left_endpoint_of_tendsto_deriv {s : set ℝ} {e : E} {a : ℝ} {f : ℝ → E} (f_diff : differentiable_on ℝ f s) (f_lim : continuous_within_at f s a) (hs : s ∈ 𝓝[>] a) (f_lim' : tendsto (λx, deriv f x) (𝓝[>] a) (𝓝 e)) : has_deriv_within_at f e (Ici a) a := begin /- This is a specialization of `has_fderiv_at_boundary_of_tendsto_fderiv`. To be in the setting of this theorem, we need to work on an open interval with closure contained in `s ∪ {a}`, that we call `t = (a, b)`. Then, we check all the assumptions of this theorem and we apply it. -/ obtain ⟨b, ab : a < b, sab : Ioc a b ⊆ s⟩ := mem_nhds_within_Ioi_iff_exists_Ioc_subset.1 hs, let t := Ioo a b, have ts : t ⊆ s := subset.trans Ioo_subset_Ioc_self sab, have t_diff : differentiable_on ℝ f t := f_diff.mono ts, have t_conv : convex ℝ t := convex_Ioo a b, have t_open : is_open t := is_open_Ioo, have t_closure : closure t = Icc a b := closure_Ioo ab.ne, have t_cont : ∀y ∈ closure t, continuous_within_at f t y, { rw t_closure, assume y hy, by_cases h : y = a, { rw h, exact f_lim.mono ts }, { have : y ∈ s := sab ⟨lt_of_le_of_ne hy.1 (ne.symm h), hy.2⟩, exact (f_diff.continuous_on y this).mono ts } }, have t_diff' : tendsto (λx, fderiv ℝ f x) (𝓝[t] a) (𝓝 (smul_right 1 e)), { simp only [deriv_fderiv.symm], exact tendsto.comp (is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map ℝ _) .continuous_right.continuous_at (tendsto_nhds_within_mono_left Ioo_subset_Ioi_self f_lim'), }, -- now we can apply `has_fderiv_at_boundary_of_differentiable` have : has_deriv_within_at f e (Icc a b) a, { rw [has_deriv_within_at_iff_has_fderiv_within_at, ← t_closure], exact has_fderiv_at_boundary_of_tendsto_fderiv t_diff t_conv t_open t_cont t_diff' }, exact this.nhds_within (Icc_mem_nhds_within_Ici $ left_mem_Ico.2 ab) end /-- If a function is differentiable on the left of a point `a : ℝ`, continuous at `a`, and its derivative also converges at `a`, then `f` is differentiable on the left at `a`. -/ lemma has_deriv_at_interval_right_endpoint_of_tendsto_deriv {s : set ℝ} {e : E} {a : ℝ} {f : ℝ → E} (f_diff : differentiable_on ℝ f s) (f_lim : continuous_within_at f s a) (hs : s ∈ 𝓝[<] a) (f_lim' : tendsto (λx, deriv f x) (𝓝[<] a) (𝓝 e)) : has_deriv_within_at f e (Iic a) a := begin /- This is a specialization of `has_fderiv_at_boundary_of_differentiable`. To be in the setting of this theorem, we need to work on an open interval with closure contained in `s ∪ {a}`, that we call `t = (b, a)`. Then, we check all the assumptions of this theorem and we apply it. -/ obtain ⟨b, ba, sab⟩ : ∃ b ∈ Iio a, Ico b a ⊆ s := mem_nhds_within_Iio_iff_exists_Ico_subset.1 hs, let t := Ioo b a, have ts : t ⊆ s := subset.trans Ioo_subset_Ico_self sab, have t_diff : differentiable_on ℝ f t := f_diff.mono ts, have t_conv : convex ℝ t := convex_Ioo b a, have t_open : is_open t := is_open_Ioo, have t_closure : closure t = Icc b a := closure_Ioo (ne_of_lt ba), have t_cont : ∀y ∈ closure t, continuous_within_at f t y, { rw t_closure, assume y hy, by_cases h : y = a, { rw h, exact f_lim.mono ts }, { have : y ∈ s := sab ⟨hy.1, lt_of_le_of_ne hy.2 h⟩, exact (f_diff.continuous_on y this).mono ts } }, have t_diff' : tendsto (λx, fderiv ℝ f x) (𝓝[t] a) (𝓝 (smul_right 1 e)), { simp only [deriv_fderiv.symm], exact tendsto.comp (is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map ℝ _) .continuous_right.continuous_at (tendsto_nhds_within_mono_left Ioo_subset_Iio_self f_lim'), }, -- now we can apply `has_fderiv_at_boundary_of_differentiable` have : has_deriv_within_at f e (Icc b a) a, { rw [has_deriv_within_at_iff_has_fderiv_within_at, ← t_closure], exact has_fderiv_at_boundary_of_tendsto_fderiv t_diff t_conv t_open t_cont t_diff' }, exact this.nhds_within (Icc_mem_nhds_within_Iic $ right_mem_Ioc.2 ba) end /-- If a real function `f` has a derivative `g` everywhere but at a point, and `f` and `g` are continuous at this point, then `g` is also the derivative of `f` at this point. -/ lemma has_deriv_at_of_has_deriv_at_of_ne {f g : ℝ → E} {x : ℝ} (f_diff : ∀ y ≠ x, has_deriv_at f (g y) y) (hf : continuous_at f x) (hg : continuous_at g x) : has_deriv_at f (g x) x := begin have A : has_deriv_within_at f (g x) (Ici x) x, { have diff : differentiable_on ℝ f (Ioi x) := λy hy, (f_diff y (ne_of_gt hy)).differentiable_at.differentiable_within_at, -- next line is the nontrivial bit of this proof, appealing to differentiability -- extension results. apply has_deriv_at_interval_left_endpoint_of_tendsto_deriv diff hf.continuous_within_at self_mem_nhds_within, have : tendsto g (𝓝[>] x) (𝓝 (g x)) := tendsto_inf_left hg, apply this.congr' _, apply mem_of_superset self_mem_nhds_within (λy hy, _), exact (f_diff y (ne_of_gt hy)).deriv.symm }, have B : has_deriv_within_at f (g x) (Iic x) x, { have diff : differentiable_on ℝ f (Iio x) := λy hy, (f_diff y (ne_of_lt hy)).differentiable_at.differentiable_within_at, -- next line is the nontrivial bit of this proof, appealing to differentiability -- extension results. apply has_deriv_at_interval_right_endpoint_of_tendsto_deriv diff hf.continuous_within_at self_mem_nhds_within, have : tendsto g (𝓝[<] x) (𝓝 (g x)) := tendsto_inf_left hg, apply this.congr' _, apply mem_of_superset self_mem_nhds_within (λy hy, _), exact (f_diff y (ne_of_lt hy)).deriv.symm }, simpa using B.union A end /-- If a real function `f` has a derivative `g` everywhere but at a point, and `f` and `g` are continuous at this point, then `g` is the derivative of `f` everywhere. -/ lemma has_deriv_at_of_has_deriv_at_of_ne' {f g : ℝ → E} {x : ℝ} (f_diff : ∀ y ≠ x, has_deriv_at f (g y) y) (hf : continuous_at f x) (hg : continuous_at g x) (y : ℝ) : has_deriv_at f (g y) y := begin rcases eq_or_ne y x with rfl|hne, { exact has_deriv_at_of_has_deriv_at_of_ne f_diff hf hg }, { exact f_diff y hne } end
0e9434749eeaf8c676d121843183278d806dd1eb
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/algebra/algebra/basic.lean
593c13c3e4403413133d0d89fb16ffe680cb9e5c
[ "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
50,767
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import tactic.nth_rewrite import data.matrix.basic import data.equiv.ring_aut import linear_algebra.tensor_product import ring_theory.subring import deprecated.subring import algebra.opposites /-! # Algebra over Commutative Semiring In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`, algebra equivalences `alg_equiv`. We also define usual operations on `alg_hom`s (`id`, `comp`). `subalgebra`s are defined in `algebra.algebra.subalgebra`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. For the category of `R`-algebras, denoted `Algebra R`, see the file `algebra/category/Algebra/basic.lean`. ## Notations * `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`. * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ universes u v w u₁ v₁ open_locale tensor_product big_operators section prio -- We set this priority to 0 later in this file set_option extends_priority 200 /- control priority of `instance [algebra R A] : has_scalar R A` -/ /-- Given a commutative (semi)ring `R`, an `R`-algebra is a (possibly noncommutative) (semi)ring `A` endowed with a morphism of rings `R →+* A` which lands in the center of `A`. For convenience, this typeclass extends `has_scalar R A` where the scalar action must agree with left multiplication by the image of the structure morphism. Given an `algebra R A` instance, the structure morphism `R →+* A` is denoted `algebra_map R A`. -/ @[nolint has_inhabited_instance] class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] extends has_scalar R A, R →+* A := (commutes' : ∀ r x, to_fun r * x = x * to_fun r) (smul_def' : ∀ r x, r • x = to_fun r * x) end prio /-- Embedding `R →+* A` given by `algebra` structure. -/ def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A := algebra.to_ring_hom /-- Creating an algebra from a morphism to the center of a semiring. -/ def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S) (h : ∀ c x, i c * x = x * i c) : algebra R S := { smul := λ c x, i c * x, commutes' := h, smul_def' := λ c x, rfl, to_ring_hom := i} /-- Creating an algebra from a morphism to a commutative semiring. -/ def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : algebra R S := i.to_algebra' $ λ _, mul_comm _ lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : @algebra_map R S _ _ i.to_algebra = i := rfl namespace algebra variables {R : Type u} {S : Type v} {A : Type w} {B : Type*} /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule' [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x) (h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A := { to_fun := λ r, r • 1, map_one' := one_smul _ _, map_mul' := λ r₁ r₂, by rw [h₁, mul_smul], map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul r₁ r₂ 1, commutes' := λ r x, by simp only [h₁, h₂], smul_def' := λ r x, by simp only [h₁] } /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y)) (h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A := of_semimodule' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one]) section semiring variables [comm_semiring R] [comm_semiring S] variables [semiring A] [algebra R A] [semiring B] [algebra R B] lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x /-- To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree, it suffices to check the `algebra_map`s agree. -/ -- We'll later use this to show `algebra ℤ M` is a subsingleton. @[ext] lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A) (w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } = by { haveI := Q, exact algebra_map R A r }) : P = Q := begin unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ }, congr, { funext r a, replace w := congr_arg (λ s, s * a) (w r), simp only [←algebra.smul_def''] at w, apply w, }, { ext r, exact w r, }, { apply proof_irrel_heq, }, { apply proof_irrel_heq, }, end @[priority 200] -- see Note [lower instance priority] instance to_semimodule : semimodule R A := { one_smul := by simp [smul_def''], mul_smul := by simp [smul_def'', mul_assoc], smul_add := by simp [smul_def'', mul_add], smul_zero := by simp [smul_def''], add_smul := by simp [smul_def'', add_mul], zero_smul := by simp [smul_def''] } -- from now on, we don't want to use the following instance anymore attribute [instance, priority 0] algebra.to_has_scalar lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 := calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm ... = r • 1 : (algebra.smul_def r 1).symm theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) := by rw [← mul_assoc, ← commutes, mul_assoc] @[simp] lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := by rw [smul_def, smul_def, left_comm] @[simp] lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := by rw [smul_def, smul_def, mul_assoc] section variables {r : R} {a : A} @[simp] lemma bit0_smul_one : bit0 r • (1 : A) = r • 2 := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit1_smul_one : bit1 r • (1 : A) = r • 2 + 1 := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a := by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel } end variables (R A) /-- The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`, packaged as an `R`-linear map. -/ protected def linear_map : R →ₗ[R] A := { map_smul' := λ x y, by simp [algebra.smul_def], ..algebra_map R A } @[simp] lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl instance id : algebra R R := (ring_hom.id R).to_algebra variables {R A} namespace id @[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl @[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl end id section prod variables (R A B) instance : algebra R (A × B) := { commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] }, smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] }, .. prod.semimodule, .. ring_hom.prod (algebra_map R A) (algebra_map R B) } variables {R A B} @[simp] lemma algebra_map_prod_apply (r : R) : algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl end prod /-- Algebra over a subsemiring. -/ instance of_subsemiring (S : subsemiring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subsemiring.subtype S) } /-- Algebra over a subring. -/ instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : subring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subring.subtype S) } lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S →+* R) = subring.subtype S := rfl lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S → R) = subtype.val := rfl lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) : algebra_map S R x = x := rfl section local attribute [instance] subset.comm_ring /-- Algebra over a set that is closed under the ring operations. -/ local attribute [instance] def of_is_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : set R) [is_subring S] : algebra S A := algebra.of_subring S.to_subring lemma is_subring_coe_algebra_map_hom {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S →+* R) = is_subring.subtype S := rfl lemma is_subring_coe_algebra_map {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S → R) = subtype.val := rfl lemma is_subring_algebra_map_apply {R : Type*} [comm_ring R] (S : set R) [is_subring S] (x : S) : algebra_map S R x = x := rfl lemma set_range_subset {R : Type*} [comm_ring R] {T₁ T₂ : set R} [is_subring T₁] (hyp : T₁ ⊆ T₂) : set.range (algebra_map T₁ R) ⊆ T₂ := begin rintros x ⟨⟨t, ht⟩, rfl⟩, exact hyp ht, end end /-- Explicit characterization of the submonoid map in the case of an algebra. `S` is made explicit to help with type inference -/ def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S] (M : submonoid R) : (submonoid S) := submonoid.map (algebra_map R S : R →* S) M lemma mem_algebra_map_submonoid_of_mem [algebra R S] {M : submonoid R} (x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M := set.mem_image_of_mem (algebra_map R S) x.2 end semiring section ring variables [comm_ring R] variables (R) /-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure. -/ def semiring_to_ring [semiring A] [algebra R A] : ring A := { ..semimodule.add_comm_monoid_to_add_comm_group R, ..(infer_instance : semiring A) } variables {R} lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) : x * (x - algebra_map R A r) = (x - algebra_map R A r) * x := by rw [mul_sub, ←commutes, sub_mul] lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) : x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x := begin induction n with n ih, { simp }, { rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc], } end end ring end algebra namespace opposite variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] instance : algebra R Aᵒᵖ := { to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _, smul_def' := λ c x, unop_injective $ by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] }, commutes' := λ r, op_induction $ λ x, by dsimp; simp only [← op_mul, algebra.commutes], ..opposite.has_scalar A R } @[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵒᵖ c = op (algebra_map R A c) := rfl end opposite namespace module variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [semimodule R M] instance endomorphism_algebra : algebra R (M →ₗ[R] M) := { to_fun := λ r, r • linear_map.id, map_one' := one_smul _ _, map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul _ _ _, map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] }, commutes' := by { intros, ext, simp }, smul_def' := by { intros, ext, simp } } lemma algebra_map_End_eq_smul_id (a : R) : (algebra_map R (End R M)) a = a • linear_map.id := rfl @[simp] lemma algebra_map_End_apply (a : R) (m : M) : (algebra_map R (End R M)) a m = a • m := rfl @[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v) [field K] [add_comm_group V] [vector_space K V] (a : K) (ha : a ≠ 0) : ((algebra_map K (End K V)) a).ker = ⊥ := linear_map.ker_smul _ _ ha end module instance matrix_algebra (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_semiring R] : algebra R (matrix n n R) := { commutes' := by { intros, simp [matrix.scalar], }, smul_def' := by { intros, simp [matrix.scalar], }, ..(matrix.scalar n) } set_option old_structure_cmd true /-- Defining the homomorphism in the category R-Alg. -/ @[nolint has_inhabited_instance] structure alg_hom (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`" infixr ` →ₐ `:25 := alg_hom _ notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} section semiring variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩ initialize_simps_projections alg_hom (to_fun → apply) instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩ instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩ instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩ @[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) : ⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl @[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl variables (φ : A →ₐ[R] B) theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ := by { cases φ₁, cases φ₂, congr, exact H } theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) := λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B), from congr_arg _ H theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) := ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) := ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := H ▸ rfl protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := h ▸ rfl @[ext] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := coe_fn_inj $ funext H theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := ⟨alg_hom.congr_fun, ext⟩ @[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) : (⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl @[simp] theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext $ φ.commutes @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := φ.to_ring_hom.map_add r s @[simp] lemma map_zero : φ 0 = 0 := φ.to_ring_hom.map_zero @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := φ.to_ring_hom.map_mul x y @[simp] lemma map_one : φ 1 = 1 := φ.to_ring_hom.map_one @[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x := by simp only [algebra.smul_def, map_mul, commutes] @[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n := φ.to_ring_hom.map_pow x n lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) : φ (∑ x in s, f x) = ∑ x in s, φ (f x) := φ.to_ring_hom.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.sum g) = f.sum (λ i a, φ (g i a)) := φ.map_sum _ _ @[simp] lemma map_nat_cast (n : ℕ) : φ n = n := φ.to_ring_hom.map_nat_cast n @[simp] lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := φ.to_ring_hom.map_bit0 x @[simp] lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := φ.to_ring_hom.map_bit1 x /-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/ def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B := { to_fun := f, commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one], .. f } @[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl section variables (R A) /-- Identity map as an `alg_hom`. -/ protected def id : A →ₐ[R] A := { commutes' := λ _, rfl, ..ring_hom.id A } end @[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl /-- Composition of algebra homeomorphisms. -/ def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C := { commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl, .. φ₁.to_ring_hom.comp ↑φ₂ } @[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, map_add' := φ.map_add, map_smul' := φ.map_smul } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H @[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) : φ (∏ x in s, f x) = ∏ x in s, φ (f x) := φ.to_ring_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.prod g) = f.prod (λ i a, φ (g i a)) := φ.map_prod _ _ end comm_semiring section ring variables [comm_semiring R] [ring A] [ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_neg (x) : φ (-x) = -φ x := φ.to_ring_hom.map_neg x @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := φ.to_ring_hom.map_sub x y @[simp] lemma map_int_cast (n : ℤ) : φ n = n := φ.to_ring_hom.map_int_cast n end ring section division_ring variables [comm_ring R] [division_ring A] [division_ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ := φ.to_ring_hom.map_inv x @[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y := φ.to_ring_hom.map_div x y end division_ring theorem injective_iff {R A B : Type*} [comm_semiring R] [ring A] [semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : function.injective f ↔ (∀ x, f x = 0 → x = 0) := ring_hom.injective_iff (f : A →+* B) end alg_hom set_option old_structure_cmd true /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure alg_equiv (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) attribute [nolint doc_blame] alg_equiv.to_ring_equiv attribute [nolint doc_blame] alg_equiv.to_equiv attribute [nolint doc_blame] alg_equiv.to_add_equiv attribute [nolint doc_blame] alg_equiv.to_mul_equiv notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A' namespace alg_equiv variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁} section semiring variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] variables (e : A₁ ≃ₐ[R] A₂) instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩ @[ext] lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} : Π {x x' : A₁}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := h ▸ rfl lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) := begin intros f g w, ext, exact congr_fun w a, end instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩ @[simp] lemma mk_apply {to_fun inv_fun left_inv right_inv map_mul map_add commutes a} : (⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) a = to_fun a := rfl @[simp] lemma to_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₁} : e.to_fun a = e a := rfl @[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) := begin intros f g w, ext, replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) := congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w, exact congr_fun w a, end @[simp] lemma map_add : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add @[simp] lemma map_zero : e 0 = 0 := e.to_add_equiv.map_zero @[simp] lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul @[simp] lemma map_one : e 1 = 1 := e.to_mul_equiv.map_one @[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r := e.commutes' lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∑ x in s, f x) = ∑ x in s, e (f x) := e.to_add_equiv.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum (λ i b, e (g i b)) := e.map_sum _ _ /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to_*_hom` projections. The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/ def to_alg_hom : A₁ →ₐ[R] A₂ := { map_one' := e.map_one, map_zero' := e.map_zero, ..e } instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) := ⟨to_alg_hom⟩ @[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl @[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e := rfl @[simp] lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow lemma injective : function.injective e := e.to_equiv.injective lemma surjective : function.surjective e := e.to_equiv.surjective lemma bijective : function.bijective e := e.to_equiv.bijective instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩ instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩ /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := 1 @[simp] lemma coe_refl : (@refl R A₁ _ _ _ : A₁ →ₐ[R] A₁) = alg_hom.id R A₁ := alg_hom.ext (λ x, rfl) /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr, change _ = e _, rw e.commutes, }, ..e.to_ring_equiv.symm, } /-- See Note [custom simps projection] -/ def simps.inv_fun (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply) @[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl @[simp] lemma symm_symm {e : A₁ ≃ₐ[R] A₂} : e.symm.symm = e := by { ext, refl, } /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'], ..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), } @[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ := by { ext, simp } @[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ := by { ext, simp } /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') := { to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom, inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom, left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp], simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] }, right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm], simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } } lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply], congr, exact (e₂.symm_apply_apply _).symm } @[simp] lemma arrow_congr_refl : arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) := by { ext, refl } @[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := by { ext, refl } @[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := by { ext, refl } /-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/ def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ := { inv_fun := g, left_inv := alg_hom.ext_iff.1 h₂, right_inv := alg_hom.ext_iff.1 h₁, ..f } /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ := { .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f } /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { to_fun := e.to_fun, map_add' := λ x y, by simp, map_smul' := λ r x, by simp [algebra.smul_def''], inv_fun := e.symm.to_fun, left_inv := e.left_inv, right_inv := e.right_inv, } @[simp] lemma to_linear_equiv_apply (e : A₁ ≃ₐ[R] A₂) (x : A₁) : e.to_linear_equiv x = e x := rfl theorem to_linear_equiv_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_equiv = e₂.to_linear_equiv) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_equiv x = e₂.to_linear_equiv x, by rw H /-- Interpret an algebra equivalence as a linear map. -/ def to_linear_map : A₁ →ₗ[R] A₂ := e.to_alg_hom.to_linear_map @[simp] lemma to_alg_hom_to_linear_map : (e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_equiv_to_linear_map : e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl theorem to_linear_map_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_map = e₂.to_linear_map) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_map x = e₂.to_linear_map x, by rw H @[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl instance aut : group (A₁ ≃ₐ[R] A₁) := { mul := λ ϕ ψ, ψ.trans ϕ, mul_assoc := λ ϕ ψ χ, rfl, one := 1, one_mul := λ ϕ, by { ext, refl }, mul_one := λ ϕ, by { ext, refl }, inv := symm, mul_left_inv := λ ϕ, by { ext, exact symm_apply_apply ϕ a } } end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∏ x in s, f x) = ∏ x in s, e (f x) := e.to_alg_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod (λ i a, e (g i a)) := e.to_alg_hom.map_finsupp_prod f g end comm_semiring section ring variables [comm_ring R] [ring A₁] [ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_neg (x) : e (-x) = -e x := e.to_alg_hom.map_neg x @[simp] lemma map_sub (x y) : e (x - y) = e x - e y := e.to_alg_hom.map_sub x y end ring section division_ring variables [comm_ring R] [division_ring A₁] [division_ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ := e.to_alg_hom.map_inv x @[simp] lemma map_div (x y) : e (x / y) = e x / e y := e.to_alg_hom.map_div x y end division_ring end alg_equiv namespace matrix /-! ### `matrix` section Specialize `matrix.one_map` and `matrix.zero_map` to `alg_hom` and `alg_equiv`. TODO: there should be a way to avoid restating these for each `foo_hom`. -/ variables {R A₁ A₂ n : Type*} [fintype n] section semiring variables [comm_semiring R] [semiring A₁] [algebra R A₁] [semiring A₂] [algebra R A₂] /-- A version of `matrix.one_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_one [decidable_eq n] (f : A₁ →ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.one_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_one [decidable_eq n] (f : A₁ ≃ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.zero_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_zero (f : A₁ →ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero /-- A version of `matrix.zero_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_zero (f : A₁ ≃ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero end semiring end matrix namespace algebra variables (R : Type u) (S : Type v) (A : Type w) include R S A /-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it when `algebra R S` and `algebra S A`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. -/ /- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and `algebra ?m_1 A -/ /- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the appropriate type classes -/ @[nolint unused_arguments] def comap : Type w := A instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h instance comap.semiring [h : semiring A] : semiring (comap R S A) := h instance comap.ring [h : ring A] : ring (comap R S A) := h instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] : algebra S (comap R S A) := h /-- Identity homomorphism `A →ₐ[S] comap R S A`. -/ def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] : A →ₐ[S] comap R S A := alg_hom.id S A /-- Identity homomorphism `comap R S A →ₐ[S] A`. -/ def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] : comap R S A →ₐ[S] A := alg_hom.id S A variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] /-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map R S r • x : A), commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _, .. (algebra_map S A).comp (algebra_map R S) } /-- Embedding of `S` into `comap R S A`. -/ def to_comap : S →ₐ[R] comap R S A := { commutes' := λ r, rfl, .. algebra_map S A } theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl end algebra namespace alg_hom variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B) include R /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } end alg_hom namespace ring_hom variables {R S : Type*} /-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/ def to_nat_alg_hom [semiring R] [semiring S] [algebra ℕ R] [algebra ℕ S] (f : R →+* S) : R →ₐ[ℕ] S := { to_fun := f, commutes' := λ n, by simp, .. f } /-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/ def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) : R →ₐ[ℤ] S := { commutes' := λ n, by simp, .. f } @[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r := ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r /-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/ def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) : R →ₐ[ℚ] S := { commutes' := f.map_rat_algebra_map, .. f } end ring_hom namespace rat instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α := (rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x end rat namespace algebra open module variables (R : Type u) (A : Type v) variables [comm_semiring R] [semiring A] [algebra R A] /-- `algebra_map` as an `alg_hom`. -/ def of_id : R →ₐ[R] A := { commutes' := λ _, rfl, .. algebra_map R A } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₐ[R] (End R A) := { map_one' := by { ext a, exact one_mul a }, map_mul' := by { intros a b, ext c, exact mul_assoc a b c }, map_zero' := by { ext a, exact zero_mul a }, commutes' := by { intro r, ext a, dsimp, rw [smul_def] }, .. (show A →ₗ[R] A →ₗ[R] A, from linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm])) } variables {A} /-- The multiplication on the left in an algebra is a linear map. -/ def lmul_left (r : A) : A →ₗ A := lmul R A r /-- The multiplication on the right in an algebra is a linear map. -/ def lmul_right (r : A) : A →ₗ A := (lmul R A).to_linear_map.flip r /-- Simultaneous multiplication on the left and right is a linear map. -/ def lmul_left_right (vw: A × A) : A →ₗ[R] A := (lmul_right R vw.2).comp (lmul_left R vw.1) /-- The multiplication map on an algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/ def lmul' : A ⊗[R] A →ₗ[R] A := tensor_product.lift (lmul R A).to_linear_map variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R p q = q * p := rfl @[simp] lemma lmul_left_right_apply (vw : A × A) (p : A) : lmul_left_right R vw p = vw.1 * p * vw.2 := rfl @[simp] lemma lmul_left_one : lmul_left R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, one_mul, id.def, lmul_left_apply] } @[simp] lemma lmul_left_mul (a b : A) : lmul_left R (a * b) = (lmul_left R a).comp (lmul_left R b) := by { ext, simp only [lmul_left_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul_right_one : lmul_right R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, mul_one, id.def, lmul_right_apply] } @[simp] lemma lmul_right_mul (a b : A) : lmul_right R (a * b) = (lmul_right R b).comp (lmul_right R a) := by { ext, simp only [lmul_right_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul'_apply {x y : A} : lmul' R (x ⊗ₜ y) = x * y := by simp only [algebra.lmul', tensor_product.lift.tmul, alg_hom.to_linear_map_apply, lmul_apply] instance linear_map.semimodule' (R : Type u) [comm_semiring R] (M : Type v) [add_comm_monoid M] [semimodule R M] (S : Type w) [comm_semiring S] [algebra R S] : semimodule S (M →ₗ[R] S) := { smul := λ s f, linear_map.llcomp _ _ _ _ (algebra.lmul R S s) f, one_smul := λ f, linear_map.ext $ λ x, one_mul _, mul_smul := λ s₁ s₂ f, linear_map.ext $ λ x, mul_assoc _ _ _, smul_add := λ s f g, linear_map.map_add _ _ _, smul_zero := λ s, linear_map.map_zero _, add_smul := λ s₁ s₂ f, linear_map.ext $ λ x, add_mul _ _ _, zero_smul := λ f, linear_map.ext $ λ x, zero_mul _ } end algebra section nat variables (R : Type*) [semiring R] /-- Semiring ⥤ ℕ-Alg -/ instance algebra_nat : algebra ℕ R := { commutes' := nat.cast_commute, smul_def' := λ _ _, nsmul_eq_mul _ _, to_ring_hom := nat.cast_ring_hom R } section span_nat open submodule lemma span_nat_eq_add_group_closure (s : set R) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := eq.symm $ add_submonoid.closure_eq_of_le subset_span $ λ x hx, span_induction hx (λ x hx, add_submonoid.subset_closure hx) (add_submonoid.zero_mem _) (λ _ _, add_submonoid.add_mem _) (λ _ _ _, add_submonoid.nsmul_mem _ ‹_› _) @[simp] lemma span_nat_eq (s : add_submonoid R) : (span ℕ (s : set R)).to_add_submonoid = s := by rw [span_nat_eq_add_group_closure, s.closure_eq] end span_nat end nat section int variables (R : Type*) [ring R] /-- Ring ⥤ ℤ-Alg -/ instance algebra_int : algebra ℤ R := { commutes' := int.cast_commute, smul_def' := λ _ _, gsmul_eq_mul _ _, to_ring_hom := int.cast_ring_hom R } variables {R} section variables {S : Type*} [ring S] instance int_algebra_subsingleton : subsingleton (algebra ℤ S) := ⟨λ P Q, by { ext, simp, }⟩ end section variables {S : Type*} [semiring S] instance nat_algebra_subsingleton : subsingleton (algebra ℕ S) := ⟨λ P Q, by { ext, simp, }⟩ end section span_int open submodule lemma span_int_eq_add_group_closure (s : set R) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _) @[simp] lemma span_int_eq (s : add_subgroup R) : (span ℤ (s : set R)).to_add_subgroup = s := by rw [span_int_eq_add_group_closure, s.closure_eq] end span_int end int /-! The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra. We couldn't set this up back in `algebra.pi_instances` because this file imports it. -/ namespace pi variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) variables (I f) instance algebra (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] : algebra α (Π i : I, f i) := { commutes' := λ a f, begin ext, simp [algebra.commutes], end, smul_def' := λ a f, begin ext, simp [algebra.smul_def''], end, ..pi.ring_hom (λ i, algebra_map α (f i)) } @[simp] lemma algebra_map_apply (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] (a : α) (i : I) : algebra_map α (Π i, f i) a i = algebra_map α (f i) a := rfl -- One could also build a `Π i, R i`-algebra structure on `Π i, A i`, -- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful. end pi section is_scalar_tower variables {R : Type*} [comm_semiring R] variables (A : Type*) [semiring A] [algebra R A] variables {M : Type*} [add_comm_monoid M] [semimodule A M] [semimodule R M] [is_scalar_tower R A M] variables {N : Type*} [add_comm_monoid N] [semimodule A N] [semimodule R N] [is_scalar_tower R A N] lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m := by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul] @[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m := (algebra_compatible_smul A r m).symm variable {A} @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M := ⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul, ←algebra_compatible_smul]⟩ @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M := smul_comm_class.symm _ _ _ lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m := smul_comm _ _ _ namespace linear_map instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) := ⟨restrict_scalars R⟩ variables (R) {A M N} @[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) : (f.restrict_scalars R : M → N) = f := rfl @[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) : ((f : M →ₗ[R] N) : M → N) = f := rfl /-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over a commutative semiring `R` and `M` a semimodule over `R`. -/ def lto_fun (R : Type u) (M : Type v) (A : Type w) [comm_semiring R] [add_comm_monoid M] [semimodule R M] [comm_ring A] [algebra R A] : (M →ₗ[R] A) →ₗ[A] (M → A) := { to_fun := linear_map.to_fun, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } end linear_map end is_scalar_tower section restrict_scalars /- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then `S`-modules are also `R`-modules. -/ section type_synonym variables (R A M : Type*) /-- Warning: use this type synonym judiciously! The preferred way of working with an `A`-module `M` as `R`-module (where `A` is an `R`-algebra), is by `[module R M] [module A M] [is_scalar_tower R A M]`. When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`, provided as a type synonym `module.restrict_scalars R A M := M`. -/ @[nolint unused_arguments] def restrict_scalars (R A M : Type*) : Type* := M instance [I : inhabited M] : inhabited (restrict_scalars R A M) := I instance [I : add_comm_monoid M] : add_comm_monoid (restrict_scalars R A M) := I instance [I : add_comm_group M] : add_comm_group (restrict_scalars R A M) := I instance restrict_scalars.module_orig [semiring A] [add_comm_monoid M] [I : semimodule A M] : semimodule A (restrict_scalars R A M) := I variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [semimodule A M] /-- When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`. The preferred way of setting this up is `[module R M] [module A M] [is_scalar_tower R A M]`. -/ instance : semimodule R (restrict_scalars R A M) := { smul := λ c x, (algebra_map R A c) • x, one_smul := by simp, mul_smul := by simp [mul_smul], smul_add := by simp [smul_add], smul_zero := by simp [smul_zero], add_smul := by simp [add_smul], zero_smul := by simp [zero_smul] } lemma restrict_scalars_smul_def (c : R) (x : restrict_scalars R A M) : c • x = ((algebra_map R A c) • x : M) := rfl instance : is_scalar_tower R A (restrict_scalars R A M) := ⟨λ r A M, by { rw [algebra.smul_def, mul_smul], refl }⟩ instance submodule.restricted_module (V : submodule A M) : semimodule R V := restrict_scalars.semimodule R A V instance submodule.restricted_module_is_scalar_tower (V : submodule A M) : is_scalar_tower R A V := restrict_scalars.is_scalar_tower R A V end type_synonym section semimodule open semimodule variables (R A M N : Type*) [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [semimodule R M] [semimodule A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [semimodule R N] [semimodule A N] [is_scalar_tower R A N] variables {A M N} namespace submodule /-- `V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars, corresponding to `V`, an `S`-submodule of the original `S`-module. -/ @[simps] def restrict_scalars (V : submodule A M) : submodule R M := { carrier := V.carrier, zero_mem' := V.zero_mem, smul_mem' := λ c m h, by { rw algebra_compatible_smul A c m, exact V.smul_mem _ h }, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma restrict_scalars_mem (V : submodule A M) (m : M) : m ∈ V.restrict_scalars R ↔ m ∈ V := iff.refl _ variables (R A M) lemma restrict_scalars_injective : function.injective (restrict_scalars R : submodule A M → submodule R M) := λ V₁ V₂ h, ext $ by convert set.ext_iff.1 (ext'_iff.1 h); refl @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule A M} : restrict_scalars R V₁ = restrict_scalars R V₂ ↔ V₁ = V₂ := ⟨λ h, restrict_scalars_injective R _ _ h, congr_arg _⟩ @[simp] lemma restrict_scalars_bot : restrict_scalars R (⊥ : submodule A M) = ⊥ := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊤ : submodule A M) = ⊤ := rfl end submodule @[simp] lemma linear_map.ker_restrict_scalars (f : M →ₗ[A] N) : (f.restrict_scalars R).ker = submodule.restrict_scalars R f.ker := rfl end semimodule end restrict_scalars namespace linear_map section extend_scalars /-! When `V` is an `R`-module and `W` is an `S`-module, where `S` is an algebra over `R`, then the collection of `R`-linear maps from `V` to `W` admits an `S`-module structure, given by multiplication in the target. -/ variables (R : Type*) [comm_semiring R] (S : Type*) [semiring S] [algebra R S] (V : Type*) [add_comm_monoid V] [semimodule R V] (W : Type*) [add_comm_monoid W] [semimodule R W] [semimodule S W] [is_scalar_tower R S W] instance is_scalar_tower_extend_scalars : is_scalar_tower R S (V →ₗ[R] W) := { smul_assoc := λ r s f, by simp only [(•), coe_mk, smul_assoc] } variables {R S V W} /-- When `f` is a linear map taking values in `S`, then `λb, f b • x` is a linear map. -/ def smul_algebra_right (f : V →ₗ[R] S) (x : W) : V →ₗ[R] W := { to_fun := λb, f b • x, map_add' := by simp [add_smul], map_smul' := λ b y, by { simp [algebra.smul_def, ← smul_smul], } } @[simp] theorem smul_algebra_right_apply (f : V →ₗ[R] S) (x : W) (c : V) : smul_algebra_right f x c = f c • x := rfl end extend_scalars end linear_map
64b7be944a774a8d567c7d6f2f360538d050739c
618003631150032a5676f229d13a079ac875ff77
/src/algebra/group/conj.lean
74614791a18cb3519a8cdcd3afa0468e25931230
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
1,939
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Chris Hughes, Michael Howes -/ import algebra.group.hom import tactic.rewrite /-! # Conjugacy of group elements -/ universes u v variables {α : Type u} {β : Type v} variables [group α] [group β] def is_conj (a b : α) := ∃ c : α, c * a * c⁻¹ = b @[refl] lemma is_conj_refl (a : α) : is_conj a a := ⟨1, by rw [one_mul, one_inv, mul_one]⟩ @[symm] lemma is_conj_symm {a b : α} : is_conj a b → is_conj b a | ⟨c, hc⟩ := ⟨c⁻¹, by rw [← hc, mul_assoc, mul_inv_cancel_right, inv_mul_cancel_left]⟩ @[trans] lemma is_conj_trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c | ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, by rw [← hc₂, ← hc₁, mul_inv_rev]; simp only [mul_assoc]⟩ @[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 := ⟨by simp [is_conj, is_conj_refl] {contextual := tt}, by simp [is_conj_refl] {contextual := tt}⟩ @[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 := calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj_symm, is_conj_symm⟩ ... ↔ a = 1 : is_conj_one_right @[simp] lemma conj_inv {a b : α} : (b * a * b⁻¹)⁻¹ = b * a⁻¹ * b⁻¹ := begin rw [mul_inv_rev _ b⁻¹, mul_inv_rev b _, inv_inv, mul_assoc], end @[simp] lemma conj_mul {a b c : α} : (b * a * b⁻¹) * (b * c * b⁻¹) = b * (a * c) * b⁻¹ := begin assoc_rw inv_mul_cancel_right, repeat {rw mul_assoc}, end @[simp] lemma is_conj_iff_eq {α : Type*} [comm_group α] {a b : α} : is_conj a b ↔ a = b := ⟨λ ⟨c, hc⟩, by rw [← hc, mul_right_comm, mul_inv_self, one_mul], λ h, by rw h⟩ protected lemma monoid_hom.map_is_conj (f : α →* β) {a b : α} : is_conj a b → is_conj (f a) (f b) | ⟨c, hc⟩ := ⟨f c, by rw [← f.map_mul, ← f.map_inv, ← f.map_mul, hc]⟩
97df372ad5893694a051451e95f04860301b342a
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/e9.lean
797227b782bb5cd71dbdb64e2224c68191186149
[ "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
650
lean
precedence `+`:65 namespace nat constant nat : Type.{1} constant add : nat → nat → nat infixl + := add end nat namespace int open nat (nat) constant int : Type.{1} constant add : int → int → int infixl + := add constant of_nat : nat → int end int namespace int_coercions coercion [persistent] int.of_nat end int_coercions -- Open "only" the notation and declarations from the namespaces nat and int open nat open int constants n m : nat constants i j : int check n + m check i + j section -- Temporarily use the int_coercions open int_coercions check n + i end -- The following one is an error -- check n + i
1953b18879cc9a34ced0e712086fb18dcf74c4c8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/opposites.lean
b65f71078046081ccccb046b36c1ff704cb796d9
[ "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
17,595
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison -/ import category_theory.equivalence /-! # Opposite categories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We provide a category instance on `Cᵒᵖ`. The morphisms `X ⟶ Y` are defined to be the morphisms `unop Y ⟶ unop X` in `C`. Here `Cᵒᵖ` is an irreducible typeclass synonym for `C` (it is the same one used in the algebra library). We also provide various mechanisms for constructing opposite morphisms, functors, and natural transformations. Unfortunately, because we do not have a definitional equality `op (op X) = X`, there are quite a few variations that are needed in practice. -/ universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. open opposite variables {C : Type u₁} section quiver variables [quiver.{v₁} C] lemma quiver.hom.op_inj {X Y : C} : function.injective (quiver.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) := λ _ _ H, congr_arg quiver.hom.unop H lemma quiver.hom.unop_inj {X Y : Cᵒᵖ} : function.injective (quiver.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) := λ _ _ H, congr_arg quiver.hom.op H @[simp] lemma quiver.hom.unop_op {X Y : C} (f : X ⟶ Y) : f.op.unop = f := rfl @[simp] lemma quiver.hom.op_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : f.unop.op = f := rfl end quiver namespace category_theory variables [category.{v₁} C] /-- The opposite category. See <https://stacks.math.columbia.edu/tag/001M>. -/ instance category.opposite : category.{v₁} Cᵒᵖ := { comp := λ _ _ _ f g, (g.unop ≫ f.unop).op, id := λ X, (𝟙 (unop X)).op } @[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).op = g.op ≫ f.op := rfl @[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl @[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).unop = g.unop ≫ f.unop := rfl @[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl @[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl @[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl section variables (C) /-- The functor from the double-opposite of a category to the underlying category. -/ @[simps] def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C := { obj := λ X, unop (unop X), map := λ X Y f, f.unop.unop } /-- The functor from a category to its double-opposite. -/ @[simps] def unop_unop : C ⥤ Cᵒᵖᵒᵖ := { obj := λ X, op (op X), map := λ X Y f, f.op.op } /-- The double opposite category is equivalent to the original. -/ @[simps] def op_op_equivalence : Cᵒᵖᵒᵖ ≌ C := { functor := op_op C, inverse := unop_unop C, unit_iso := iso.refl (𝟭 Cᵒᵖᵒᵖ), counit_iso := iso.refl (unop_unop C ⋙ op_op C) } end /-- If `f` is an isomorphism, so is `f.op` -/ instance is_iso_op {X Y : C} (f : X ⟶ Y) [is_iso f] : is_iso f.op := ⟨⟨(inv f).op, ⟨quiver.hom.unop_inj (by tidy), quiver.hom.unop_inj (by tidy)⟩⟩⟩ /-- If `f.op` is an isomorphism `f` must be too. (This cannot be an instance as it would immediately loop!) -/ lemma is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f := ⟨⟨(inv (f.op)).unop, ⟨quiver.hom.op_inj (by simp), quiver.hom.op_inj (by simp)⟩⟩⟩ lemma is_iso_op_iff {X Y : C} (f : X ⟶ Y) : is_iso f.op ↔ is_iso f := ⟨λ hf, by exactI is_iso_of_op _, λ hf, by exactI infer_instance⟩ lemma is_iso_unop_iff {X Y : Cᵒᵖ} (f : X ⟶ Y) : is_iso f.unop ↔ is_iso f := by rw [← is_iso_op_iff f.unop, quiver.hom.op_unop] instance is_iso_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso f] : is_iso f.unop := (is_iso_unop_iff _).2 infer_instance @[simp] lemma op_inv {X Y : C} (f : X ⟶ Y) [is_iso f] : (inv f).op = inv f.op := by { ext, rw [← op_comp, is_iso.inv_hom_id, op_id] } @[simp] lemma unop_inv {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso f] : (inv f).unop = inv f.unop := by { ext, rw [← unop_comp, is_iso.inv_hom_id, unop_id] } namespace functor section variables {D : Type u₂} [category.{v₂} D] variables {C D} /-- The opposite of a functor, i.e. considering a functor `F : C ⥤ D` as a functor `Cᵒᵖ ⥤ Dᵒᵖ`. In informal mathematics no distinction is made between these. -/ @[simps] protected def op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (unop X)), map := λ X Y f, (F.map f.unop).op } /-- Given a functor `F : Cᵒᵖ ⥤ Dᵒᵖ` we can take the "unopposite" functor `F : C ⥤ D`. In informal mathematics no distinction is made between these. -/ @[simps] protected def unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D := { obj := λ X, unop (F.obj (op X)), map := λ X Y f, (F.map f.op).unop } /-- The isomorphism between `F.op.unop` and `F`. -/ @[simps] def op_unop_iso (F : C ⥤ D) : F.op.unop ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- The isomorphism between `F.unop.op` and `F`. -/ @[simps] def unop_op_iso (F : Cᵒᵖ ⥤ Dᵒᵖ) : F.unop.op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) variables (C D) /-- Taking the opposite of a functor is functorial. -/ @[simps] def op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) := { obj := λ F, (unop F).op, map := λ F G α, { app := λ X, (α.unop.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj (α.unop.naturality f.unop).symm } } /-- Take the "unopposite" of a functor is functorial. -/ @[simps] def op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ := { obj := λ F, op F.unop, map := λ F G α, quiver.hom.op { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj $ (α.naturality f.op).symm } } variables {C D} /-- Another variant of the opposite of functor, turning a functor `C ⥤ Dᵒᵖ` into a functor `Cᵒᵖ ⥤ D`. In informal mathematics no distinction is made. -/ @[simps] protected def left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D := { obj := λ X, unop (F.obj (unop X)), map := λ X Y f, (F.map f.unop).unop } /-- Another variant of the opposite of functor, turning a functor `Cᵒᵖ ⥤ D` into a functor `C ⥤ Dᵒᵖ`. In informal mathematics no distinction is made. -/ @[simps] protected def right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (op X)), map := λ X Y f, (F.map f.op).op } instance {F : C ⥤ D} [full F] : full F.op := { preimage := λ X Y f, (F.preimage f.unop).op } instance {F : C ⥤ D} [faithful F] : faithful F.op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj $ by simpa using map_injective F (quiver.hom.op_inj h) } /-- If F is faithful then the right_op of F is also faithful. -/ instance right_op_faithful {F : Cᵒᵖ ⥤ D} [faithful F] : faithful F.right_op := { map_injective' := λ X Y f g h, quiver.hom.op_inj (map_injective F (quiver.hom.op_inj h)) } /-- If F is faithful then the left_op of F is also faithful. -/ instance left_op_faithful {F : C ⥤ Dᵒᵖ} [faithful F] : faithful F.left_op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj (map_injective F (quiver.hom.unop_inj h)) } /-- The isomorphism between `F.left_op.right_op` and `F`. -/ @[simps] def left_op_right_op_iso (F : C ⥤ Dᵒᵖ) : F.left_op.right_op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- The isomorphism between `F.right_op.left_op` and `F`. -/ @[simps] def right_op_left_op_iso (F : Cᵒᵖ ⥤ D) : F.right_op.left_op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- Whenever possible, it is advisable to use the isomorphism `right_op_left_op_iso` instead of this equality of functors. -/ lemma right_op_left_op_eq (F : Cᵒᵖ ⥤ D) : F.right_op.left_op = F := by { cases F, refl, } end end functor namespace nat_trans variables {D : Type u₂} [category.{v₂} D] section variables {F G : C ⥤ D} /-- The opposite of a natural transformation. -/ @[simps] protected def op (α : F ⟶ G) : G.op ⟶ F.op := { app := λ X, (α.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj (by simp) } @[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl /-- The "unopposite" of a natural transformation. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) : G.unop ⟶ F.unop := { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj (by simp) } @[simp] lemma unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.unop (𝟙 F) = 𝟙 (F.unop) := rfl /-- Given a natural transformation `α : F.op ⟶ G.op`, we can take the "unopposite" of each component obtaining a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_op (α : F.op ⟶ G.op) : G ⟶ F := { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj $ by simpa only [functor.op_map] using (α.naturality f.op).symm } @[simp] lemma remove_op_id (F : C ⥤ D) : nat_trans.remove_op (𝟙 F.op) = 𝟙 F := rfl /-- Given a natural transformation `α : F.unop ⟶ G.unop`, we can take the opposite of each component obtaining a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F.unop ⟶ G.unop) : G ⟶ F := { app := λ X, (α.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj $ by simpa only [functor.unop_map] using (α.naturality f.unop).symm } @[simp] lemma remove_unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.remove_unop (𝟙 F.unop) = 𝟙 F := rfl end section variables {F G H : C ⥤ Dᵒᵖ} /-- Given a natural transformation `α : F ⟶ G`, for `F G : C ⥤ Dᵒᵖ`, taking `unop` of each component gives a natural transformation `G.left_op ⟶ F.left_op`. -/ @[simps] protected def left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op := { app := λ X, (α.app (unop X)).unop, naturality' := λ X Y f, quiver.hom.op_inj (by simp) } @[simp] lemma left_op_id : (𝟙 F : F ⟶ F).left_op = 𝟙 F.left_op := rfl @[simp] lemma left_op_comp (α : F ⟶ G) (β : G ⟶ H) : (α ≫ β).left_op = β.left_op ≫ α.left_op := rfl /-- Given a natural transformation `α : F.left_op ⟶ G.left_op`, for `F G : C ⥤ Dᵒᵖ`, taking `op` of each component gives a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_left_op (α : F.left_op ⟶ G.left_op) : G ⟶ F := { app := λ X, (α.app (op X)).op, naturality' := λ X Y f, quiver.hom.unop_inj $ by simpa only [functor.left_op_map] using (α.naturality f.op).symm } @[simp] lemma remove_left_op_id : nat_trans.remove_left_op (𝟙 F.left_op) = 𝟙 F := rfl end section variables {F G H : Cᵒᵖ ⥤ D} /-- Given a natural transformation `α : F ⟶ G`, for `F G : Cᵒᵖ ⥤ D`, taking `op` of each component gives a natural transformation `G.right_op ⟶ F.right_op`. -/ @[simps] protected def right_op (α : F ⟶ G) : G.right_op ⟶ F.right_op := { app := λ X, (α.app _).op, naturality' := λ X Y f, quiver.hom.unop_inj (by simp) } @[simp] lemma right_op_id : (𝟙 F : F ⟶ F).right_op = 𝟙 F.right_op := rfl @[simp] lemma right_op_comp (α : F ⟶ G) (β : G ⟶ H) : (α ≫ β).right_op = β.right_op ≫ α.right_op := rfl /-- Given a natural transformation `α : F.right_op ⟶ G.right_op`, for `F G : Cᵒᵖ ⥤ D`, taking `unop` of each component gives a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_right_op (α : F.right_op ⟶ G.right_op) : G ⟶ F := { app := λ X, (α.app X.unop).unop, naturality' := λ X Y f, quiver.hom.op_inj $ by simpa only [functor.right_op_map] using (α.naturality f.unop).symm } @[simp] lemma remove_right_op_id : nat_trans.remove_right_op (𝟙 F.right_op) = 𝟙 F := rfl end end nat_trans namespace iso variables {X Y : C} /-- The opposite isomorphism. -/ @[simps] protected def op (α : X ≅ Y) : op Y ≅ op X := { hom := α.hom.op, inv := α.inv.op, hom_inv_id' := quiver.hom.unop_inj α.inv_hom_id, inv_hom_id' := quiver.hom.unop_inj α.hom_inv_id } /-- The isomorphism obtained from an isomorphism in the opposite category. -/ @[simps] def unop {X Y : Cᵒᵖ} (f : X ≅ Y) : Y.unop ≅ X.unop := { hom := f.hom.unop, inv := f.inv.unop, hom_inv_id' := by simp only [← unop_comp, f.inv_hom_id, unop_id], inv_hom_id' := by simp only [← unop_comp, f.hom_inv_id, unop_id] } @[simp] lemma unop_op {X Y : Cᵒᵖ} (f : X ≅ Y) : f.unop.op = f := by ext; refl @[simp] lemma op_unop {X Y : C} (f : X ≅ Y) : f.op.unop = f := by ext; refl end iso namespace nat_iso variables {D : Type u₂} [category.{v₂} D] variables {F G : C ⥤ D} /-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def op (α : F ≅ G) : G.op ≅ F.op := { hom := nat_trans.op α.hom, inv := nat_trans.op α.inv, hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism between the opposite functors `F.op ≅ G.op`. -/ @[simps] protected def remove_op (α : F.op ≅ G.op) : G ≅ F := { hom := nat_trans.remove_op α.hom, inv := nat_trans.remove_op α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G.unop ≅ F.unop` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) : G.unop ≅ F.unop := { hom := nat_trans.unop α.hom, inv := nat_trans.unop α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } end nat_iso namespace equivalence variables {D : Type u₂} [category.{v₂} D] /-- An equivalence between categories gives an equivalence between the opposite categories. -/ @[simps] def op (e : C ≌ D) : Cᵒᵖ ≌ Dᵒᵖ := { functor := e.functor.op, inverse := e.inverse.op, unit_iso := (nat_iso.op e.unit_iso).symm, counit_iso := (nat_iso.op e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.unop_inj, dsimp, simp, }, } /-- An equivalence between opposite categories gives an equivalence between the original categories. -/ @[simps] def unop (e : Cᵒᵖ ≌ Dᵒᵖ) : C ≌ D := { functor := e.functor.unop, inverse := e.inverse.unop, unit_iso := (nat_iso.unop e.unit_iso).symm, counit_iso := (nat_iso.unop e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.op_inj, dsimp, simp, }, } end equivalence /-- The equivalence between arrows of the form `A ⟶ B` and `B.unop ⟶ A.unop`. Useful for building adjunctions. Note that this (definitionally) gives variants ``` def op_equiv' (A : C) (B : Cᵒᵖ) : (opposite.op A ⟶ B) ≃ (B.unop ⟶ A) := op_equiv _ _ def op_equiv'' (A : Cᵒᵖ) (B : C) : (A ⟶ opposite.op B) ≃ (B ⟶ A.unop) := op_equiv _ _ def op_equiv''' (A B : C) : (opposite.op A ⟶ opposite.op B) ≃ (B ⟶ A) := op_equiv _ _ ``` -/ @[simps] def op_equiv (A B : Cᵒᵖ) : (A ⟶ B) ≃ (B.unop ⟶ A.unop) := { to_fun := λ f, f.unop, inv_fun := λ g, g.op, left_inv := λ _, rfl, right_inv := λ _, rfl } instance subsingleton_of_unop (A B : Cᵒᵖ) [subsingleton (unop B ⟶ unop A)] : subsingleton (A ⟶ B) := (op_equiv A B).subsingleton instance decidable_eq_of_unop (A B : Cᵒᵖ) [decidable_eq (unop B ⟶ unop A)] : decidable_eq (A ⟶ B) := (op_equiv A B).decidable_eq /-- The equivalence between isomorphisms of the form `A ≅ B` and `B.unop ≅ A.unop`. Note this is definitionally the same as the other three variants: * `(opposite.op A ≅ B) ≃ (B.unop ≅ A)` * `(A ≅ opposite.op B) ≃ (B ≅ A.unop)` * `(opposite.op A ≅ opposite.op B) ≃ (B ≅ A)` -/ @[simps] def iso_op_equiv (A B : Cᵒᵖ) : (A ≅ B) ≃ (B.unop ≅ A.unop) := { to_fun := λ f, f.unop, inv_fun := λ g, g.op, left_inv := λ _, by { ext, refl, }, right_inv := λ _, by { ext, refl, } } namespace functor variables (C) variables (D : Type u₂) [category.{v₂} D] /-- The equivalence of functor categories induced by `op` and `unop`. -/ @[simps] def op_unop_equiv : (C ⥤ D)ᵒᵖ ≌ Cᵒᵖ ⥤ Dᵒᵖ := { functor := op_hom _ _, inverse := op_inv _ _, unit_iso := nat_iso.of_components (λ F, F.unop.op_unop_iso.op) begin intros F G f, dsimp [op_unop_iso], rw [(show f = f.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, tidy, end, counit_iso := nat_iso.of_components (λ F, F.unop_op_iso) (by tidy) }. /-- The equivalence of functor categories induced by `left_op` and `right_op`. -/ @[simps] def left_op_right_op_equiv : (Cᵒᵖ ⥤ D)ᵒᵖ ≌ (C ⥤ Dᵒᵖ) := { functor := { obj := λ F, F.unop.right_op, map := λ F G η, η.unop.right_op }, inverse := { obj := λ F, op F.left_op, map := λ F G η, η.left_op.op }, unit_iso := nat_iso.of_components (λ F, F.unop.right_op_left_op_iso.op) begin intros F G η, dsimp, rw [(show η = η.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, tidy, end, counit_iso := nat_iso.of_components (λ F, F.left_op_right_op_iso) (by tidy) } end functor end category_theory
f4c314d06bf9a54daad088e48931c12296c379c5
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Compiler/IR/CtorLayout.lean
63e472a3b474f8fe6fc7114fb609bbf9c9698726
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
956
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.Environment import Lean.Compiler.IR.Format namespace Lean namespace IR inductive CtorFieldInfo where | irrelevant | object (i : Nat) | usize (i : Nat) | scalar (sz : Nat) (offset : Nat) (type : IRType) namespace CtorFieldInfo def format : CtorFieldInfo → Format | irrelevant => "◾" | object i => f!"obj@{i}" | usize i => f!"usize@{i}" | scalar sz offset type => f!"scalar#{sz}@{offset}:{type}" instance : ToFormat CtorFieldInfo := ⟨format⟩ end CtorFieldInfo structure CtorLayout where cidx : Nat fieldInfo : List CtorFieldInfo numObjs : Nat numUSize : Nat scalarSize : Nat @[extern "lean_ir_get_ctor_layout"] constant getCtorLayout (env : @& Environment) (ctorName : @& Name) : Except String CtorLayout end IR end Lean
63497d61b9be0af555a34d36191fcf3d42430e1a
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/lang.lean
4141f880e36fe277adf93237809521ceed3897f7
[]
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
2,710
lean
import .expressions.bool_expr import .expressions.time_series.geom3d_expr import .expressions.geom1d_expr import .expressions.geom3d_expr import .expressions.time_expr open lang.time open lang.geom1d open lang.geom3d open lang.series.geom3d open lang.bool_expr #check time structure constrained (T : Type 1) (P : T → Prop) := (val : T) (holds : P val) (fails : ¬P val) @[class] structure has_norm_constraint (T : Type*) := (to_norm_constraint : scalar_expr → T → Prop) notation `norm<`s := has_norm_constraint.to_norm_constraint s instance {tf : time_frame_expr} {ts : time_space_expr tf} : has_norm_constraint (duration_expr ts) := ⟨λs, λd, ∥d.value.to_vectr∥ < s.value⟩ @[class] structure has_time_range_constraint (T : Type*) := (to_range_constraint : scalar_expr → scalar_expr → T → Prop) notation `time ``range `x` to `y := has_time_range_constraint.to_range_constraint x y instance {tf : time_frame_expr} {ts : time_space_expr tf} : has_time_range_constraint (time_expr ts) := ⟨λs1 s2, λt, t.value.coord > s1.value ∧ t.value.coord < s2.value⟩ instance {tf : time_frame_expr} {ts : time_space_expr tf} {gf : geom3d_frame_expr } {gs : geom3d_space_expr gf} : has_time_range_constraint (timestamped_pose3d_expr ts gs) := ⟨λs1 s2, λt, t.value.timestamp.coord > s1.value ∧ t.value.timestamp.coord < s2.value⟩ instance {tf : time_frame_expr} {ts : time_space_expr tf} {gf : geom3d_frame_expr } {gs : geom3d_space_expr gf} : has_time_range_constraint (pose3d_series_expr ts gs) := ⟨λs1 s2, λte, ∀t : time ts.value, t∈te.value.domain → t.coord > s1.value ∧ t.coord < s2.value⟩ /- def time_expr.range_constraint {tf : time_frame_expr} {ts : time_space_expr tf} (te : time_expr ts) : scalar → scalar → Prop := λs1 s2, te.value.coord > s1 ∧ te.value.coord < s2 def timestamped_pose3d_expr.range_constraint {tf : time_frame_expr} {ts : time_space_expr tf} {gf : geom3d_frame_expr } {gs : geom3d_space_expr gf} (s1 s2 : scalar) : timestamped_pose3d_expr ts gs → Prop := λts, ts.value.timestamp.coord > s1 ∧ ts.value.timestamp.coord < s2 def timestamped_pose3d_expr.range_constraint {tf : time_frame_expr} {ts : time_space_expr tf} {gf : geom3d_frame_expr } {gs : geom3d_space_expr gf} (s1 s2 : scalar) : timestamped_pose3d_expr ts gs → Prop := λts, ts.value.timestamp.coord > s1 ∧ ts.value.timestamp.coord < s2 def pose3d_series_expr.range_constraint {tf : time_frame_expr} {ts : time_space_expr tf} {gf : geom3d_frame_expr } {gs : geom3d_space_expr gf} (s1 s2 : scalar) : pose3d_series_expr ts gs → Prop := λte, ∀t : time ts.value, t∈te.value.domain → t.coord > s1 ∧ t.coord < s2 -/
15976ec61307b911a8b038c154269cd2c66fe5df
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/complex/upper_half_plane/functions_bounded_at_infty.lean
53538b4fa044d711595989bd49ebf25437c649e8
[ "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
3,646
lean
/- Copyright (c) 2022 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck, David Loeffler -/ import algebra.module.submodule.basic import analysis.complex.upper_half_plane.basic import order.filter.zero_and_bounded_at_filter /-! # Bounded at infinity For complex valued functions on the upper half plane, this file defines the filter `at_im_infty` required for defining when functions are bounded at infinity and zero at infinity. Both of which are relevant for defining modular forms. -/ open complex filter open_locale topology upper_half_plane noncomputable theory namespace upper_half_plane /-- Filter for approaching `i∞`. -/ def at_im_infty := filter.at_top.comap upper_half_plane.im lemma at_im_infty_basis : (at_im_infty).has_basis (λ _, true) (λ (i : ℝ), im ⁻¹' set.Ici i) := filter.has_basis.comap upper_half_plane.im filter.at_top_basis lemma at_im_infty_mem (S : set ℍ) : S ∈ at_im_infty ↔ (∃ A : ℝ, ∀ z : ℍ, A ≤ im z → z ∈ S) := begin simp only [at_im_infty, filter.mem_comap', filter.mem_at_top_sets, ge_iff_le, set.mem_set_of_eq, upper_half_plane.coe_im], refine ⟨λ ⟨a, h⟩, ⟨a, (λ z hz, h (im z) hz rfl)⟩, _⟩, rintro ⟨A, h⟩, refine ⟨A, λ b hb x hx, h x _⟩, rwa hx, end /-- A function ` f : ℍ → α` is bounded at infinity if it is bounded along `at_im_infty`. -/ def is_bounded_at_im_infty {α : Type*} [has_norm α] (f : ℍ → α) : Prop := bounded_at_filter at_im_infty f /-- A function ` f : ℍ → α` is zero at infinity it is zero along `at_im_infty`. -/ def is_zero_at_im_infty {α : Type*} [has_zero α] [topological_space α] (f : ℍ → α) : Prop := zero_at_filter at_im_infty f lemma zero_form_is_bounded_at_im_infty {α : Type*} [normed_field α] : is_bounded_at_im_infty (0 : ℍ → α) := const_bounded_at_filter at_im_infty (0:α) /-- Module of functions that are zero at infinity. -/ def zero_at_im_infty_submodule (α : Type*) [normed_field α] : submodule α (ℍ → α) := zero_at_filter_submodule at_im_infty /-- ubalgebra of functions that are bounded at infinity. -/ def bounded_at_im_infty_subalgebra (α : Type*) [normed_field α] : subalgebra α (ℍ → α) := bounded_filter_subalgebra at_im_infty lemma is_bounded_at_im_infty.mul {f g : ℍ → ℂ} (hf : is_bounded_at_im_infty f) (hg : is_bounded_at_im_infty g) : is_bounded_at_im_infty (f * g) := by simpa only [pi.one_apply, mul_one, norm_eq_abs] using hf.mul hg lemma bounded_mem (f : ℍ → ℂ) : is_bounded_at_im_infty f ↔ ∃ (M A : ℝ), ∀ z : ℍ, A ≤ im z → abs (f z) ≤ M := by simp [is_bounded_at_im_infty, bounded_at_filter, asymptotics.is_O_iff, filter.eventually, at_im_infty_mem] lemma zero_at_im_infty (f : ℍ → ℂ) : is_zero_at_im_infty f ↔ ∀ ε : ℝ, 0 < ε → ∃ A : ℝ, ∀ z : ℍ, A ≤ im z → abs (f z) ≤ ε := begin rw [is_zero_at_im_infty, zero_at_filter, tendsto_iff_forall_eventually_mem], split, { simp_rw [filter.eventually, at_im_infty_mem], intros h ε hε, simpa using (h (metric.closed_ball (0 : ℂ) ε) (metric.closed_ball_mem_nhds (0 : ℂ) hε))}, { simp_rw metric.mem_nhds_iff, intros h s hs, simp_rw [filter.eventually, at_im_infty_mem], obtain ⟨ε, h1, h2⟩ := hs, have h11 : 0 < (ε/2), by {linarith,}, obtain ⟨A, hA⟩ := (h (ε/2) h11), use A, intros z hz, have hzs : f z ∈ s, { apply h2, simp only [mem_ball_zero_iff, norm_eq_abs], apply lt_of_le_of_lt (hA z hz), linarith }, apply hzs,} end end upper_half_plane
7514b8d4e736467a4d59aac0cef39842ca602601
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/src/Init/Data/Array/Basic.lean
e5c08dc3484109c30f222d7709a3d49b9e52570b
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
27,590
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Nat.Basic import Init.Data.Fin.Basic import Init.Data.UInt import Init.Data.Repr import Init.Data.ToString.Basic import Init.Util universes u v w namespace Array variable {α : Type u} @[extern "lean_mk_array"] def mkArray {α : Type u} (n : Nat) (v : α) : Array α := { data := List.replicate n v } @[simp] theorem size_mkArray (n : Nat) (v : α) : (mkArray n v).size = n := List.length_replicate .. instance : EmptyCollection (Array α) := ⟨Array.empty⟩ instance : Inhabited (Array α) where default := Array.empty def isEmpty (a : Array α) : Bool := a.size = 0 def singleton (v : α) : Array α := mkArray 1 v /- Low-level version of `fget` which is as fast as a C array read. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `fget` may be slightly slower than `uget`. -/ @[extern "lean_array_uget"] def uget (a : @& Array α) (i : USize) (h : i.toNat < a.size) : α := a.get ⟨i.toNat, h⟩ def back [Inhabited α] (a : Array α) : α := a.get! (a.size - 1) def get? (a : Array α) (i : Nat) : Option α := if h : i < a.size then some (a.get ⟨i, h⟩) else none def back? (a : Array α) : Option α := a.get? (a.size - 1) -- auxiliary declaration used in the equation compiler when pattern matching array literals. abbrev getLit {α : Type u} {n : Nat} (a : Array α) (i : Nat) (h₁ : a.size = n) (h₂ : i < n) : α := a.get ⟨i, h₁.symm ▸ h₂⟩ @[simp] theorem size_set (a : Array α) (i : Fin a.size) (v : α) : (set a i v).size = a.size := List.length_set .. @[simp] theorem size_push (a : Array α) (v : α) : (push a v).size = a.size + 1 := List.length_concat .. /- Low-level version of `fset` which is as fast as a C array fset. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `fset` may be slightly slower than `uset`. -/ @[extern "lean_array_uset"] def uset (a : Array α) (i : USize) (v : α) (h : i.toNat < a.size) : Array α := a.set ⟨i.toNat, h⟩ v @[extern "lean_array_fswap"] def swap (a : Array α) (i j : @& Fin a.size) : Array α := let v₁ := a.get i let v₂ := a.get j let a' := a.set i v₂ a'.set (size_set a i v₂ ▸ j) v₁ @[extern "lean_array_swap"] def swap! (a : Array α) (i j : @& Nat) : Array α := if h₁ : i < a.size then if h₂ : j < a.size then swap a ⟨i, h₁⟩ ⟨j, h₂⟩ else panic! "index out of bounds" else panic! "index out of bounds" @[inline] def swapAt (a : Array α) (i : Fin a.size) (v : α) : α × Array α := let e := a.get i let a := a.set i v (e, a) @[inline] def swapAt! (a : Array α) (i : Nat) (v : α) : α × Array α := if h : i < a.size then swapAt a ⟨i, h⟩ v else have : Inhabited α := ⟨v⟩ panic! ("index " ++ toString i ++ " out of bounds") @[extern "lean_array_pop"] def pop (a : Array α) : Array α := { data := a.data.dropLast } def shrink (a : Array α) (n : Nat) : Array α := let rec loop | 0, a => a | n+1, a => loop n a.pop loop (a.size - n) a @[inline] def modifyM [Monad m] [Inhabited α] (a : Array α) (i : Nat) (f : α → m α) : m (Array α) := do if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩ let v := a.get idx let a' := a.set idx arbitrary let v ← f v pure <| a'.set (size_set a .. ▸ idx) v else pure a @[inline] def modify [Inhabited α] (a : Array α) (i : Nat) (f : α → α) : Array α := Id.run <| a.modifyM i f @[inline] def modifyOp [Inhabited α] (self : Array α) (idx : Nat) (f : α → α) : Array α := self.modify idx f /- We claim this unsafe implementation is correct because an array cannot have more than `usizeSz` elements in our runtime. This kind of low level trick can be removed with a little bit of compiler support. For example, if the compiler simplifies `as.size < usizeSz` to true. -/ @[inline] unsafe def forInUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : α → β → m (ForInStep β)) : m β := let sz := USize.ofNat as.size let rec @[specialize] loop (i : USize) (b : β) : m β := do if i < sz then let a := as.uget i lcProof match (← f a b) with | ForInStep.done b => pure b | ForInStep.yield b => loop (i+1) b else pure b loop 0 b -- Move? private theorem zeroLtOfLt : {a b : Nat} → a < b → 0 < b | 0, _, h => h | a+1, b, h => have : a < b := Nat.ltTrans (Nat.ltSuccSelf _) h zeroLtOfLt this /- Reference implementation for `forIn` -/ @[implementedBy Array.forInUnsafe] protected def forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : α → β → m (ForInStep β)) : m β := let rec loop (i : Nat) (h : i ≤ as.size) (b : β) : m β := do match i, h with | 0, _ => pure b | i+1, h => have h' : i < as.size := Nat.ltOfLtOfLe (Nat.ltSuccSelf i) h have : as.size - 1 < as.size := Nat.subLt (zeroLtOfLt h') (by decide) have : as.size - 1 - i < as.size := Nat.ltOfLeOfLt (Nat.subLe (as.size - 1) i) this match (← f (as.get ⟨as.size - 1 - i, this⟩) b) with | ForInStep.done b => pure b | ForInStep.yield b => loop i (Nat.leOfLt h') b loop as.size (Nat.leRefl _) b instance : ForIn m (Array α) α where forIn := Array.forIn /- See comment at forInUnsafe -/ @[inline] unsafe def foldlMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Array α) (start := 0) (stop := as.size) : m β := let rec @[specialize] fold (i : USize) (stop : USize) (b : β) : m β := do if i == stop then pure b else fold (i+1) stop (← f b (as.uget i lcProof)) if start < stop then if stop ≤ as.size then fold (USize.ofNat start) (USize.ofNat stop) init else pure init else pure init /- Reference implementation for `foldlM` -/ @[implementedBy foldlMUnsafe] def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Array α) (start := 0) (stop := as.size) : m β := let fold (stop : Nat) (h : stop ≤ as.size) := let rec loop (i : Nat) (j : Nat) (b : β) : m β := do if hlt : j < stop then match i with | 0 => pure b | i'+1 => loop i' (j+1) (← f b (as.get ⟨j, Nat.ltOfLtOfLe hlt h⟩)) else pure b loop (stop - start) start init if h : stop ≤ as.size then fold stop h else fold as.size (Nat.leRefl _) /- See comment at forInUnsafe -/ @[inline] unsafe def foldrMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Array α) (start := as.size) (stop := 0) : m β := let rec @[specialize] fold (i : USize) (stop : USize) (b : β) : m β := do if i == stop then pure b else fold (i-1) stop (← f (as.uget (i-1) lcProof) b) if start ≤ as.size then if stop < start then fold (USize.ofNat start) (USize.ofNat stop) init else pure init else if stop < as.size then fold (USize.ofNat as.size) (USize.ofNat stop) init else pure init /- Reference implementation for `foldrM` -/ @[implementedBy foldrMUnsafe] def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Array α) (start := as.size) (stop := 0) : m β := let rec fold (i : Nat) (h : i ≤ as.size) (b : β) : m β := do if i == stop then pure b else match i, h with | 0, _ => pure b | i+1, h => have : i < as.size := Nat.ltOfLtOfLe (Nat.ltSuccSelf _) h fold i (Nat.leOfLt this) (← f (as.get ⟨i, this⟩) b) if h : start ≤ as.size then if stop < start then fold start h init else pure init else if stop < as.size then fold as.size (Nat.leRefl _) init else pure init /- See comment at forInUnsafe -/ @[inline] unsafe def mapMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → m β) (as : Array α) : m (Array β) := let sz := USize.ofNat as.size let rec @[specialize] map (i : USize) (r : Array NonScalar) : m (Array PNonScalar.{v}) := do if i < sz then let v := r.uget i lcProof let r := r.uset i arbitrary lcProof let vNew ← f (unsafeCast v) map (i+1) (r.uset i (unsafeCast vNew) lcProof) else pure (unsafeCast r) unsafeCast <| map 0 (unsafeCast as) /- Reference implementation for `mapM` -/ @[implementedBy mapMUnsafe] def mapM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → m β) (as : Array α) : m (Array β) := as.foldlM (fun bs a => do let b ← f a; pure (bs.push b)) (mkEmpty as.size) @[inline] def mapIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : Fin as.size → α → m β) : m (Array β) := let rec @[specialize] map (i : Nat) (j : Nat) (inv : i + j = as.size) (bs : Array β) : m (Array β) := do match i, inv with | 0, _ => pure bs | i+1, inv => have : j < as.size := by rw [← inv, Nat.add_assoc, Nat.add_comm 1 j, Nat.add_left_comm]; apply Nat.leAddRight let idx : Fin as.size := ⟨j, this⟩ have : i + (j + 1) = as.size := by rw [← inv, Nat.add_comm j 1, Nat.add_assoc] map i (j+1) this (bs.push (← f idx (as.get idx))) map as.size 0 rfl (mkEmpty as.size) @[inline] def findSomeM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := do for a in as do match (← f a) with | some b => return b | _ => pure ⟨⟩ return none @[inline] def findM? {α : Type} {m : Type → Type} [Monad m] (as : Array α) (p : α → m Bool) : m (Option α) := do for a in as do if (← p a) then return a return none @[inline] def findIdxM? [Monad m] (as : Array α) (p : α → m Bool) : m (Option Nat) := do let mut i := 0 for a in as do if (← p a) then return some i i := i + 1 return none @[inline] unsafe def anyMUnsafe {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := let rec @[specialize] any (i : USize) (stop : USize) : m Bool := do if i == stop then pure false else if (← p (as.uget i lcProof)) then pure true else any (i+1) stop if start < stop then if stop ≤ as.size then any (USize.ofNat start) (USize.ofNat stop) else pure false else pure false @[implementedBy anyMUnsafe] def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := let any (stop : Nat) (h : stop ≤ as.size) := let rec loop (i : Nat) (j : Nat) : m Bool := do if hlt : j < stop then match i with | 0 => pure false | i'+1 => if (← p (as.get ⟨j, Nat.ltOfLtOfLe hlt h⟩)) then pure true else loop i' (j+1) else pure false loop (stop - start) start if h : stop ≤ as.size then any stop h else any as.size (Nat.leRefl _) @[inline] def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := return !(← as.anyM fun v => return !(← p v)) @[inline] def findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := let rec @[specialize] find : (i : Nat) → i ≤ as.size → m (Option β) | 0, h => pure none | i+1, h => do have : i < as.size := Nat.ltOfLtOfLe (Nat.ltSuccSelf _) h let r ← f (as.get ⟨i, this⟩) match r with | some v => pure r | none => have : i ≤ as.size := Nat.leOfLt this find i this find as.size (Nat.leRefl _) @[inline] def findRevM? {α : Type} {m : Type → Type w} [Monad m] (as : Array α) (p : α → m Bool) : m (Option α) := as.findSomeRevM? fun a => return if (← p a) then some a else none @[inline] def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Array α) (start := 0) (stop := as.size) : m PUnit := as.foldlM (fun _ => f) ⟨⟩ start stop @[inline] def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Array α) (start := as.size) (stop := 0) : m PUnit := as.foldrM (fun a _ => f a) ⟨⟩ start stop @[inline] def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Array α) (start := 0) (stop := as.size) : β := Id.run <| as.foldlM f init start stop @[inline] def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Array α) (start := as.size) (stop := 0) : β := Id.run <| as.foldrM f init start stop @[inline] def map {α : Type u} {β : Type v} (f : α → β) (as : Array α) : Array β := Id.run <| as.mapM f @[inline] def mapIdx {α : Type u} {β : Type v} (as : Array α) (f : Fin as.size → α → β) : Array β := Id.run <| as.mapIdxM f @[inline] def find? {α : Type} (as : Array α) (p : α → Bool) : Option α := Id.run <| as.findM? p @[inline] def findSome? {α : Type u} {β : Type v} (as : Array α) (f : α → Option β) : Option β := Id.run <| as.findSomeM? f @[inline] def findSome! {α : Type u} {β : Type v} [Inhabited β] (a : Array α) (f : α → Option β) : β := match findSome? a f with | some b => b | none => panic! "failed to find element" @[inline] def findSomeRev? {α : Type u} {β : Type v} (as : Array α) (f : α → Option β) : Option β := Id.run <| as.findSomeRevM? f @[inline] def findRev? {α : Type} (as : Array α) (p : α → Bool) : Option α := Id.run <| as.findRevM? p @[inline] def findIdx? {α : Type u} (as : Array α) (p : α → Bool) : Option Nat := let rec loop (i : Nat) (j : Nat) (inv : i + j = as.size) : Option Nat := if hlt : j < as.size then match i, inv with | 0, inv => by apply False.elim rw [Nat.zero_add] at inv rw [inv] at hlt exact absurd hlt (Nat.ltIrrefl _) | i+1, inv => if p (as.get ⟨j, hlt⟩) then some j else have : i + (j+1) = as.size := by rw [← inv, Nat.add_comm j 1, Nat.add_assoc] loop i (j+1) this else none loop as.size 0 rfl def getIdx? [BEq α] (a : Array α) (v : α) : Option Nat := a.findIdx? fun a => a == v @[inline] def any (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool := Id.run <| as.anyM p start stop @[inline] def all (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool := Id.run <| as.allM p start stop def contains [BEq α] (as : Array α) (a : α) : Bool := as.any fun b => a == b def elem [BEq α] (a : α) (as : Array α) : Bool := as.contains a -- TODO(Leo): justify termination using wf-rec, and use `swap` partial def reverse (as : Array α) : Array α := let n := as.size let mid := n / 2 let rec rev (as : Array α) (i : Nat) := if i < mid then rev (as.swap! i (n - i - 1)) (i+1) else as rev as 0 @[inline] def getEvenElems (as : Array α) : Array α := (·.2) <| as.foldl (init := (true, Array.empty)) fun (even, r) a => if even then (false, r.push a) else (true, r) @[export lean_array_to_list] def toList (as : Array α) : List α := as.foldr List.cons [] instance {α : Type u} [Repr α] : Repr (Array α) where reprPrec a _ := if a.size == 0 then "#[]" else Std.Format.bracketFill "#[" (@Std.Format.joinSep _ ⟨repr⟩ (toList a) ("," ++ Std.Format.line)) "]" instance [ToString α] : ToString (Array α) where toString a := "#" ++ toString a.toList protected def append (as : Array α) (bs : Array α) : Array α := bs.foldl (init := as) fun r v => r.push v instance : Append (Array α) := ⟨Array.append⟩ protected def appendList (as : Array α) (bs : List α) : Array α := bs.foldl (init := as) fun r v => r.push v instance : HAppend (Array α) (List α) (Array α) := ⟨Array.appendList⟩ @[inline] def concatMapM [Monad m] (f : α → m (Array β)) (as : Array α) : m (Array β) := as.foldlM (init := empty) fun bs a => do return bs ++ (← f a) @[inline] def concatMap (f : α → Array β) (as : Array α) : Array β := as.foldl (init := empty) fun bs a => bs ++ f a end Array export Array (mkArray) syntax "#[" sepBy(term, ", ") "]" : term macro_rules | `(#[ $elems,* ]) => `(List.toArray [ $elems,* ]) namespace Array -- TODO(Leo): cleanup @[specialize] partial def isEqvAux (a b : Array α) (hsz : a.size = b.size) (p : α → α → Bool) (i : Nat) : Bool := if h : i < a.size then let aidx : Fin a.size := ⟨i, h⟩; let bidx : Fin b.size := ⟨i, hsz ▸ h⟩; match p (a.get aidx) (b.get bidx) with | true => isEqvAux a b hsz p (i+1) | false => false else true @[inline] def isEqv (a b : Array α) (p : α → α → Bool) : Bool := if h : a.size = b.size then isEqvAux a b h p 0 else false instance [BEq α] : BEq (Array α) := ⟨fun a b => isEqv a b BEq.beq⟩ @[inline] def filter (p : α → Bool) (as : Array α) (start := 0) (stop := as.size) : Array α := as.foldl (init := #[]) (start := start) (stop := stop) fun r a => if p a then r.push a else r @[inline] def filterM [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m (Array α) := as.foldlM (init := #[]) (start := start) (stop := stop) fun r a => do if (← p a) then r.push a else r @[specialize] def filterMapM [Monad m] (f : α → m (Option β)) (as : Array α) (start := 0) (stop := as.size) : m (Array β) := as.foldlM (init := #[]) (start := start) (stop := stop) fun bs a => do match (← f a) with | some b => pure (bs.push b) | none => pure bs @[inline] def filterMap (f : α → Option β) (as : Array α) (start := 0) (stop := as.size) : Array β := Id.run <| as.filterMapM f (start := start) (stop := stop) @[specialize] def getMax? (as : Array α) (lt : α → α → Bool) : Option α := if h : 0 < as.size then let a0 := as.get ⟨0, h⟩ some <| as.foldl (init := a0) (start := 1) fun best a => if lt best a then a else best else none @[inline] def partition (p : α → Bool) (as : Array α) : Array α × Array α := do let mut bs := #[] let mut cs := #[] for a in as do if p a then bs := bs.push a else cs := cs.push a return (bs, cs) theorem ext (a b : Array α) (h₁ : a.size = b.size) (h₂ : (i : Nat) → (hi₁ : i < a.size) → (hi₂ : i < b.size) → a.get ⟨i, hi₁⟩ = b.get ⟨i, hi₂⟩) : a = b := by let rec extAux (a b : List α) (h₁ : a.length = b.length) (h₂ : (i : Nat) → (hi₁ : i < a.length) → (hi₂ : i < b.length) → a.get i hi₁ = b.get i hi₂) : a = b := by induction a generalizing b with | nil => cases b with | nil => rfl | cons b bs => rw [List.length_cons] at h₁; injection h₁ | cons a as ih => cases b with | nil => rw [List.length_cons] at h₁; injection h₁ | cons b bs => have hz₁ : 0 < (a::as).length := by rw [List.length_cons]; apply Nat.zeroLtSucc have hz₂ : 0 < (b::bs).length := by rw [List.length_cons]; apply Nat.zeroLtSucc have headEq : a = b := h₂ 0 hz₁ hz₂ have h₁' : as.length = bs.length := by rw [List.length_cons, List.length_cons] at h₁; injection h₁; assumption have h₂' : (i : Nat) → (hi₁ : i < as.length) → (hi₂ : i < bs.length) → as.get i hi₁ = bs.get i hi₂ := by intro i hi₁ hi₂ have hi₁' : i+1 < (a::as).length := by rw [List.length_cons]; apply Nat.succ_lt_succ; assumption have hi₂' : i+1 < (b::bs).length := by rw [List.length_cons]; apply Nat.succ_lt_succ; assumption have : (a::as).get (i+1) hi₁' = (b::bs).get (i+1) hi₂' := h₂ (i+1) hi₁' hi₂' apply this have tailEq : as = bs := ih bs h₁' h₂' rw [headEq, tailEq] cases a; cases b apply congrArg apply extAux assumption assumption theorem extLit {n : Nat} (a b : Array α) (hsz₁ : a.size = n) (hsz₂ : b.size = n) (h : (i : Nat) → (hi : i < n) → a.getLit i hsz₁ hi = b.getLit i hsz₂ hi) : a = b := Array.ext a b (hsz₁.trans hsz₂.symm) fun i hi₁ hi₂ => h i (hsz₁ ▸ hi₁) end Array -- CLEANUP the following code namespace Array partial def indexOfAux [BEq α] (a : Array α) (v : α) : Nat → Option (Fin a.size) | i => if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩; if a.get idx == v then some idx else indexOfAux a v (i+1) else none def indexOf? [BEq α] (a : Array α) (v : α) : Option (Fin a.size) := indexOfAux a v 0 partial def eraseIdxAux : Nat → Array α → Array α | i, a => if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩; let idx1 : Fin a.size := ⟨i - 1, by exact Nat.ltOfLeOfLt (Nat.predLe i) h⟩; eraseIdxAux (i+1) (a.swap idx idx1) else a.pop def feraseIdx (a : Array α) (i : Fin a.size) : Array α := eraseIdxAux (i.val + 1) a def eraseIdx (a : Array α) (i : Nat) : Array α := if i < a.size then eraseIdxAux (i+1) a else a @[simp] theorem size_swap (a : Array α) (i j : Fin a.size) : (a.swap i j).size = a.size := by show ((a.set i (a.get j)).set (size_set a i _ ▸ j) (a.get i)).size = a.size rw [size_set, size_set] @[simp] theorem size_pop (a : Array α) : a.pop.size = a.size - 1 := List.length_dropLast .. section /- Instance for justifying `partial` declaration. We should be able to delete it as soon as we restore support for well-founded recursion. -/ instance eraseIdxSzAuxInstance (a : Array α) : Inhabited { r : Array α // r.size = a.size - 1 } where default := ⟨a.pop, size_pop a⟩ partial def eraseIdxSzAux (a : Array α) : ∀ (i : Nat) (r : Array α), r.size = a.size → { r : Array α // r.size = a.size - 1 } | i, r, heq => if h : i < r.size then let idx : Fin r.size := ⟨i, h⟩; let idx1 : Fin r.size := ⟨i - 1, by exact Nat.ltOfLeOfLt (Nat.predLe i) h⟩; eraseIdxSzAux a (i+1) (r.swap idx idx1) ((size_swap r idx idx1).trans heq) else ⟨r.pop, (size_pop r).trans (heq ▸ rfl)⟩ end def eraseIdx' (a : Array α) (i : Fin a.size) : { r : Array α // r.size = a.size - 1 } := eraseIdxSzAux a (i.val + 1) a rfl def erase [BEq α] (as : Array α) (a : α) : Array α := match as.indexOf? a with | none => as | some i => as.feraseIdx i partial def insertAtAux (i : Nat) : Array α → Nat → Array α | as, j => if i == j then as else let as := as.swap! (j-1) j; insertAtAux i as (j-1) /-- Insert element `a` at position `i`. Pre: `i < as.size` -/ def insertAt (as : Array α) (i : Nat) (a : α) : Array α := if i > as.size then panic! "invalid index" else let as := as.push a; as.insertAtAux i as.size def toListLitAux (a : Array α) (n : Nat) (hsz : a.size = n) : ∀ (i : Nat), i ≤ a.size → List α → List α | 0, hi, acc => acc | (i+1), hi, acc => toListLitAux a n hsz i (Nat.leOfSuccLe hi) (a.getLit i hsz (Nat.ltOfLtOfEq (Nat.ltOfLtOfLe (Nat.ltSuccSelf i) hi) hsz) :: acc) def toArrayLit (a : Array α) (n : Nat) (hsz : a.size = n) : Array α := List.toArray <| toListLitAux a n hsz n (hsz ▸ Nat.leRefl _) [] theorem toArrayLitEq (a : Array α) (n : Nat) (hsz : a.size = n) : a = toArrayLit a n hsz := -- TODO: this is painful to prove without proper automation sorry /- First, we need to prove ∀ i j acc, i ≤ a.size → (toListLitAux a n hsz (i+1) hi acc).index j = if j < i then a.getLit j hsz _ else acc.index (j - i) by induction Base case is trivial (j : Nat) (acc : List α) (hi : 0 ≤ a.size) |- (toListLitAux a n hsz 0 hi acc).index j = if j < 0 then a.getLit j hsz _ else acc.index (j - 0) ... |- acc.index j = acc.index j Induction (j : Nat) (acc : List α) (hi : i+1 ≤ a.size) |- (toListLitAux a n hsz (i+1) hi acc).index j = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) ... |- (toListLitAux a n hsz i hi' (a.getLit i hsz _ :: acc)).index j = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) * by def ... |- if j < i then a.getLit j hsz _ else (a.getLit i hsz _ :: acc).index (j-i) * by induction hypothesis = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) If j < i, then both are a.getLit j hsz _ If j = i, then lhs reduces else-branch to (a.getLit i hsz _) and rhs is then-brachn (a.getLit i hsz _) If j >= i + 1, we use - j - i >= 1 > 0 - (a::as).index k = as.index (k-1) If k > 0 - j - (i + 1) = (j - i) - 1 Then lhs = (a.getLit i hsz _ :: acc).index (j-i) = acc.index (j-i-1) = acc.index (j-(i+1)) = rhs With this proof, we have ∀ j, j < n → (toListLitAux a n hsz n _ []).index j = a.getLit j hsz _ We also need - (toListLitAux a n hsz n _ []).length = n - j < n -> (List.toArray as).getLit j _ _ = as.index j Then using Array.extLit, we have that a = List.toArray <| toListLitAux a n hsz n _ [] -/ partial def isPrefixOfAux [BEq α] (as bs : Array α) (hle : as.size ≤ bs.size) : Nat → Bool | i => if h : i < as.size then let a := as.get ⟨i, h⟩; let b := bs.get ⟨i, Nat.ltOfLtOfLe h hle⟩; if a == b then isPrefixOfAux as bs hle (i+1) else false else true /- Return true iff `as` is a prefix of `bs` -/ def isPrefixOf [BEq α] (as bs : Array α) : Bool := if h : as.size ≤ bs.size then isPrefixOfAux as bs h 0 else false private def allDiffAuxAux [BEq α] (as : Array α) (a : α) : forall (i : Nat), i < as.size → Bool | 0, h => true | i+1, h => have : i < as.size := Nat.ltTrans (Nat.ltSuccSelf _) h; a != as.get ⟨i, this⟩ && allDiffAuxAux as a i this private partial def allDiffAux [BEq α] (as : Array α) : Nat → Bool | i => if h : i < as.size then allDiffAuxAux as (as.get ⟨i, h⟩) i h && allDiffAux as (i+1) else true def allDiff [BEq α] (as : Array α) : Bool := allDiffAux as 0 @[specialize] partial def zipWithAux (f : α → β → γ) (as : Array α) (bs : Array β) : Nat → Array γ → Array γ | i, cs => if h : i < as.size then let a := as.get ⟨i, h⟩; if h : i < bs.size then let b := bs.get ⟨i, h⟩; zipWithAux f as bs (i+1) <| cs.push <| f a b else cs else cs @[inline] def zipWith (as : Array α) (bs : Array β) (f : α → β → γ) : Array γ := zipWithAux f as bs 0 #[] def zip (as : Array α) (bs : Array β) : Array (α × β) := zipWith as bs Prod.mk def unzip (as : Array (α × β)) : Array α × Array β := as.foldl (init := (#[], #[])) fun (as, bs) (a, b) => (as.push a, bs.push b) def split (as : Array α) (p : α → Bool) : Array α × Array α := as.foldl (init := (#[], #[])) fun (as, bs) a => if p a then (as.push a, bs) else (as, bs.push a) end Array
4e67f841315ee3b8280394d788c6b377df13d1cb
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/int/basic.lean
e1d468b759609e3fc8a241ca9647c0a9b877f02f
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
55,348
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import data.nat.pow import order.min_max /-! # Basic operations on the integers This file contains: * instances on `ℤ`. The stronger one is `int.linear_ordered_comm_ring`. * some basic lemmas about integers ## Recursors * `int.rec`: Sign disjunction. Something is true/defined on `ℤ` if it's true/defined for nonnegative and for negative values. * `int.bit_cases_on`: Parity disjunction. Something is true/defined on `ℤ` if it's true/defined for even and for odd values. * `int.induction_on`: Simple growing induction on positive numbers, plus simple decreasing induction on negative numbers. Note that this recursor is currently only `Prop`-valued. * `int.induction_on'`: Simple growing induction for numbers greater than `b`, plus simple decreasing induction on numbers less than `b`. -/ open nat namespace int instance : inhabited ℤ := ⟨int.zero⟩ instance : nontrivial ℤ := ⟨⟨0, 1, int.zero_ne_one⟩⟩ instance : comm_ring int := { add := int.add, add_assoc := int.add_assoc, zero := int.zero, zero_add := int.zero_add, add_zero := int.add_zero, neg := int.neg, add_left_neg := int.add_left_neg, add_comm := int.add_comm, mul := int.mul, mul_assoc := int.mul_assoc, one := int.one, one_mul := int.one_mul, mul_one := int.mul_one, sub := int.sub, left_distrib := int.distrib_left, right_distrib := int.distrib_right, mul_comm := int.mul_comm, gsmul := (*), gsmul_zero' := int.zero_mul, gsmul_succ' := λ n x, by rw [succ_eq_one_add, of_nat_add, int.distrib_right, of_nat_one, int.one_mul], gsmul_neg' := λ n x, neg_mul_eq_neg_mul_symm (n.succ : ℤ) x } /-! ### Extra instances to short-circuit type class resolution -/ -- instance : has_sub int := by apply_instance -- This is in core instance : add_comm_monoid int := by apply_instance instance : add_monoid int := by apply_instance instance : monoid int := by apply_instance instance : comm_monoid int := by apply_instance instance : comm_semigroup int := by apply_instance instance : semigroup int := by apply_instance instance : add_comm_semigroup int := by apply_instance instance : add_semigroup int := by apply_instance instance : comm_semiring int := by apply_instance instance : semiring int := by apply_instance instance : ring int := by apply_instance instance : distrib int := by apply_instance instance : linear_ordered_comm_ring int := { add_le_add_left := @int.add_le_add_left, mul_pos := @int.mul_pos, zero_le_one := le_of_lt int.zero_lt_one, .. int.comm_ring, .. int.linear_order, .. int.nontrivial } instance : linear_ordered_add_comm_group int := by apply_instance @[simp] lemma add_neg_one (i : ℤ) : i + -1 = i - 1 := rfl theorem abs_eq_nat_abs : ∀ a : ℤ, |a| = nat_abs a | (n : ℕ) := abs_of_nonneg $ coe_zero_le _ | -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _ theorem nat_abs_abs (a : ℤ) : nat_abs (|a|) = nat_abs a := by rw [abs_eq_nat_abs]; refl theorem sign_mul_abs (a : ℤ) : sign a * |a| = a := by rw [abs_eq_nat_abs, sign_mul_nat_abs] @[simp] lemma default_eq_zero : default ℤ = 0 := rfl meta instance : has_to_format ℤ := ⟨λ z, to_string z⟩ meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ attribute [simp] int.of_nat_eq_coe int.bodd @[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl @[simp] lemma neg_succ_not_nonneg (n : ℕ) : 0 ≤ -[1+ n] ↔ false := by { simp only [not_le, iff_false], exact int.neg_succ_lt_zero n, } @[simp] lemma neg_succ_not_pos (n : ℕ) : 0 < -[1+ n] ↔ false := by simp only [not_lt, iff_false] @[simp] lemma neg_succ_sub_one (n : ℕ) : -[1+ n] - 1 = -[1+ (n+1)] := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp, norm_cast] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := not_congr coe_nat_eq_zero @[simp] lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n) @[simp, norm_cast] theorem coe_nat_abs (n : ℕ) : |(n : ℤ)| = n := abs_of_nonneg (coe_nat_nonneg n) /-! ### succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : ℤ) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : ℤ) := a - 1 theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : ℤ) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : ℤ) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : ℤ) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b := add_le_add_iff_right _ @[simp] lemma succ_coe_nat_pos (n : ℕ) : 0 < (n : ℤ) + 1 := lt_add_one_iff.mpr (by simp) @[norm_cast] lemma coe_pred_of_pos {n : ℕ} (h : 0 < n) : ((n - 1 : ℕ) : ℤ) = (n : ℤ) - 1 := by { cases n, cases h, simp, } lemma le_add_one {a b : ℤ} (h : a ≤ b) : a ≤ b + 1 := le_of_lt (int.lt_add_one_iff.mpr h) theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b := le_sub_iff_add_le @[simp] lemma eq_zero_iff_abs_lt_one {a : ℤ} : |a| < 1 ↔ a = 0 := ⟨λ a0, let ⟨hn, hp⟩ := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn, λ a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_one⟩ @[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop} (i : ℤ) (hz : p 0) (hp : ∀ i : ℕ, p i → p (i + 1)) (hn : ∀ i : ℕ, p (-i) → p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : ∀ n:ℕ, p (- n), { intro n, induction n, { simp [hz] }, { convert hn _ n_ih using 1, simp [sub_eq_neg_add] } }, exact this (i + 1) } end /-- Inductively define a function on `ℤ` by defining it at `b`, for the `succ` of a number greater than `b`, and the `pred` of a number less than `b`. -/ protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) : C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z := λ H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b) with n n, { induction n with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction n with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /-! ### nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := begin have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b), { refine (λ a b : ℕ, sub_nat_nat_elim a b.succ (λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ.inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end lemma nat_abs_sub_le (a b : ℤ) : nat_abs (a - b) ≤ nat_abs a + nat_abs b := by { rw [sub_eq_add_neg, ← int.nat_abs_neg b], apply nat_abs_add_le } theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [← int.mul_def, int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] lemma nat_abs_mul_nat_abs_eq {a b : ℤ} {c : ℕ} (h : a * b = (c : ℤ)) : a.nat_abs * b.nat_abs = c := by rw [← nat_abs_mul, h, nat_abs_of_nat] @[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq, sub_eq_neg_add] lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 := λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h @[simp] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 := ⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩ lemma nat_abs_ne_zero {a : ℤ} : a.nat_abs ≠ 0 ↔ a ≠ 0 := not_congr int.nat_abs_eq_zero lemma nat_abs_lt_nat_abs_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) : a.nat_abs < b.nat_abs := begin lift b to ℕ using le_trans w₁ (le_of_lt w₂), lift a to ℕ using w₁, simpa using w₂, end lemma nat_abs_eq_nat_abs_iff {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a = b ∨ a = -b := begin split; intro h, { cases int.nat_abs_eq a with h₁ h₁; cases int.nat_abs_eq b with h₂ h₂; rw [h₁, h₂]; simp [h], }, { cases h; rw h, rw int.nat_abs_neg, }, end lemma nat_abs_eq_iff {a : ℤ} {n : ℕ} : a.nat_abs = n ↔ a = n ∨ a = -n := by rw [←int.nat_abs_eq_nat_abs_iff, int.nat_abs_of_nat] lemma nat_abs_eq_iff_mul_self_eq {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a * a = b * b := begin rw [← abs_eq_iff_mul_self_eq, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_inj'.symm end lemma nat_abs_lt_iff_mul_self_lt {a b : ℤ} : a.nat_abs < b.nat_abs ↔ a * a < b * b := begin rw [← abs_lt_iff_mul_self_lt, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_lt.symm end lemma nat_abs_le_iff_mul_self_le {a b : ℤ} : a.nat_abs ≤ b.nat_abs ↔ a * a ≤ b * b := begin rw [← abs_le_iff_mul_self_le, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_le.symm end lemma nat_abs_eq_iff_sq_eq {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a ^ 2 = b ^ 2 := by { rw [sq, sq], exact nat_abs_eq_iff_mul_self_eq } lemma nat_abs_lt_iff_sq_lt {a b : ℤ} : a.nat_abs < b.nat_abs ↔ a ^ 2 < b ^ 2 := by { rw [sq, sq], exact nat_abs_lt_iff_mul_self_lt } lemma nat_abs_le_iff_sq_le {a b : ℤ} : a.nat_abs ≤ b.nat_abs ↔ a ^ 2 ≤ b ^ 2 := by { rw [sq, sq], exact nat_abs_le_iff_mul_self_le } /-! ### `/` -/ @[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp, norm_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end -- Will be generalized to Euclidean domains. local attribute [simp] protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0 | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := show of_nat _ = _, by simp | -[1+ n] := show -of_nat _ = _, by simp local attribute [simp] -- Will be generalized to Euclidean domains. protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0 | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := show of_nat _ = _, by simp | -[1+ n] := rfl @[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b) | (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl | (m : ℕ) (n+1:ℕ) := rfl | 0 -[1+ n] := by simp | (m+1:ℕ) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < |b|) : a / b = 0 := match b, |b|, abs_eq_nat_abs b, H2 with | (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_injective $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) : (a + b * c) / c = a / c + b := have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from λ k n a, match a with | (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ = n - (m / k.succ + 1 : ℕ), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : ℕ) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from λ a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : ℕ) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj.1 $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] protected theorem add_div_of_dvd_right {a b c : ℤ} (H : c ∣ b) : (a + b) / c = a / c + b / c := begin by_cases h1 : c = 0, { simp [h1] }, cases H with k hk, rw hk, change c ≠ 0 at h1, rw [mul_comm c k, int.add_mul_div_right _ _ h1, ←zero_add (k * c), int.add_mul_div_right _ _ h1, int.zero_div, zero_add] end protected theorem add_div_of_dvd_left {a b c : ℤ} (H : c ∣ a) : (a + b) / c = a / c + b / c := by rw [add_comm, int.add_div_of_dvd_right H, add_comm] @[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /-! ### mod -/ theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl @[simp, norm_cast] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : 0 < b) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b | (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : ℤ) : a % (|b|) = a % b := abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _) local attribute [simp] -- Will be generalized to Euclidean domains. theorem zero_mod (b : ℤ) : 0 % b = 0 := rfl local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_zero : ∀ (a : ℤ), a % 0 = a | (m : ℕ) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_one : ∀ (a : ℤ), a % 1 = 0 | (m : ℕ) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b | (m : ℕ) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < |b| := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H) theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm end theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a | (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := by simp | (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem div_add_mod (a b : ℤ) : b * (a / b) + a % b = a := (add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : ℤ) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : ℤ) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : ℤ) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : ℤ} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] lemma mul_mod (a b n : ℤ) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc, add_mul_mod_self] } end @[simp] lemma neg_mod_two (i : ℤ) : (-i) % 2 = i % 2 := begin apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr, convert int.mul_mod_right 2 (-i), simp only [two_mul, sub_eq_add_neg] end local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_self {a : ℤ} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod_of_dvd (n : ℤ) {m k : ℤ} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} lemma sub_mod (a b n : ℤ) : (a - b) % n = ((a % n) - (b % n)) % n := begin apply (mod_add_cancel_right b).mp, rw [sub_add_cancel, ← add_mod_mod, sub_add_cancel, mod_mod] end /-! ### properties of `/` and `%` -/ @[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b / (a * c) = b / c := suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, λ m k b, match b, k with | (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≤ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (H : 0 < b) (c : ℤ) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : ℤ} (H : 0 < a) (b c : ℤ) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b := by { rw [add_mul, one_mul, mul_comm, ← sub_lt_iff_lt_add', ← mod_def], exact mod_lt_of_pos _ H } theorem abs_div_le_abs : ∀ (a b : ℤ), |a / b| ≤ |a| := suffices ∀ (a : ℤ) (n : ℕ), |a / n| ≤ |a|, from λ a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : ℕ), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial, have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : ℕ) := λ _ _, or.inl rfl | (1 : ℕ) := λ _ _, or.inr rfl | (k + 2 : ℕ) := λ h _, absurd h dec_trivial | -[1+ a] := λ _ h₁, absurd h₁ dec_trivial end /-! ### dvd -/ @[norm_cast] theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (λm0, by simp [m0] at ae; simp [ae, m0]) (λm0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : ℤ} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ /-- If `a % b = c` then `b` divides `a - c`. -/ lemma dvd_sub_of_mod_eq {a b c : ℤ} (h : a % b = c) : b ∣ a - c := begin have hx : a % b % b = c % b, { rw h }, rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx, exact dvd_of_mod_eq_zero hx end theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd, ← e]) theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg, ← e]) instance decidable_dvd : @decidable_rel ℤ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] protected theorem mul_div_assoc' (b : ℤ) {a c : ℤ} (h : c ∣ a) : a * b / c = a / c * b := by rw [mul_comm, int.mul_div_assoc _ h, mul_comm] theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem eq_div_of_mul_eq_right {a b c : ℤ} (H1 : a ≠ 0) (H2 : a * b = c) : b = c / a := eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) protected lemma eq_zero_of_div_eq_zero {d n : ℤ} (h : d ∣ n) (H : n / d = 0) : n = 0 := by rw [← int.mul_div_cancel' h, H, mul_zero] theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma sub_div_of_dvd (a : ℤ) {b c : ℤ} (hcb : c ∣ b) : (a - b) / c = a / c - b / c := begin rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)], congr, exact neg_div_of_dvd hcb, end lemma sub_div_of_dvd_sub {a b c : ℤ} (hcab : c ∣ (a - b)) : (a - b) / c = a / c - b / c := by rw [eq_sub_iff_add_eq, ← int.add_div_of_dvd_left hcab, sub_add_cancel] theorem div_sign : ∀ a b, a / sign b = a * sign b | a (n+1:ℕ) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:ℕ) (n+1:ℕ) := rfl | (m+1:ℕ) -[1+ n] := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / |a| := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az) (sign_mul_abs _).symm).symm theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i | (n+1:ℕ) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ @[simp] theorem sign_pow_bit1 (k : ℕ) : ∀ n : ℤ, n.sign ^ (bit1 k) = n.sign | (n+1:ℕ) := one_pow (bit1 k) | 0 := zero_pow (nat.zero_lt_bit1 k) | -[1+ n] := (neg_pow_bit1 1 k).trans (congr_arg (λ x, -x) (one_pow (bit1 k))) theorem le_of_dvd {a b : ℤ} (bpos : 0 < b) (H : a ∣ b) : a ≤ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : ℤ} (H : 0 ≤ a) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : 0 ≤ a) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : 0 ≤ b) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, change -[1+k] with -(↑(k+1) : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv, end lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)` for some `k`. -/ lemma exists_lt_and_lt_iff_not_dvd (m : ℤ) {n : ℤ} (hn : 0 < n) : (∃ k, n * k < m ∧ m < n * (k + 1)) ↔ ¬ n ∣ m := begin split, { rintro ⟨k, h1k, h2k⟩ ⟨l, rfl⟩, rw [mul_lt_mul_left hn] at h1k h2k, rw [lt_add_one_iff, ← not_lt] at h2k, exact h2k h1k }, { intro h, rw [dvd_iff_mod_eq_zero, ← ne.def] at h, have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm, simp only [← mod_add_div m n] {single_pass := tt}, refine ⟨m / n, lt_add_of_pos_left _ this, _⟩, rw [add_comm _ (1 : ℤ), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ } end /-! ### `/` and ordering -/ protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b := le_trans (decidable.mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b := by rw [← int.div_mul_cancel H2]; exact decidable.mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw [int.mul_div_cancel_left _ hb], rw mul_assoc at h, apply mul_left_cancel₀ hb h end /-- If an integer with larger absolute value divides an integer, it is zero. -/ lemma eq_zero_of_dvd_of_nat_abs_lt_nat_abs {a b : ℤ} (w : a ∣ b) (h : nat_abs b < nat_abs a) : b = 0 := begin rw [←nat_abs_dvd, ←dvd_nat_abs, coe_nat_dvd] at w, rw ←nat_abs_eq_zero, exact eq_zero_of_dvd_of_lt w h end lemma eq_zero_of_dvd_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) (h : b ∣ a) : a = 0 := eq_zero_of_dvd_of_nat_abs_lt_nat_abs h (nat_abs_lt_nat_abs_of_nonneg_of_lt w₁ w₂) /-- If two integers are congruent to a sufficiently large modulus, they are equal. -/ lemma eq_of_mod_eq_of_nat_abs_sub_lt_nat_abs {a b c : ℤ} (h1 : a % b = c) (h2 : nat_abs (a - c) < nat_abs b) : a = c := eq_of_sub_eq_zero (eq_zero_of_dvd_of_nat_abs_lt_nat_abs (dvd_sub_of_mod_eq h1) h2) theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ} (h : n.succ ≤ m) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply nat.sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /-! ### to_nat -/ theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0 | (n : ℕ) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] lemma to_nat_zero : (0 : ℤ).to_nat = 0 := rfl @[simp] lemma to_nat_one : (1 : ℤ).to_nat = 1 := rfl @[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] lemma to_nat_sub_of_le {a b : ℤ} (h : b ≤ a) : (to_nat (a - b) : ℤ) = a - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) @[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl @[simp] lemma to_nat_coe_nat_add_one {n : ℕ} : ((n : ℤ) + 1).to_nat = n + 1 := rfl theorem le_to_nat (a : ℤ) : a ≤ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h lemma to_nat_add {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : (a + b).to_nat = a.to_nat + b.to_nat := begin lift a to ℕ using ha, lift b to ℕ using hb, norm_cast, end lemma to_nat_add_nat {a : ℤ} (ha : 0 ≤ a) (n : ℕ) : (a + n).to_nat = a.to_nat + n := begin lift a to ℕ using ha, norm_cast, end @[simp] lemma pred_to_nat : ∀ (i : ℤ), (i - 1).to_nat = i.to_nat - 1 | (0:ℕ) := rfl | (n+1:ℕ) := by simp | -[1+ n] := rfl @[simp] lemma to_nat_pred_coe_of_pos {i : ℤ} (h : 0 < i) : ((i.to_nat - 1 : ℕ) : ℤ) = i - 1 := by simp [h, le_of_lt h] with push_cast @[simp] lemma to_nat_sub_to_nat_neg : ∀ (n : ℤ), ↑n.to_nat - ↑((-n).to_nat) = n | (0 : ℕ) := rfl | (n+1 : ℕ) := show ↑(n+1) - (0:ℤ) = n+1, from sub_zero _ | -[1+ n] := show 0 - (n+1 : ℤ) = _, from zero_sub _ @[simp] lemma to_nat_add_to_nat_neg_eq_nat_abs : ∀ (n : ℤ), (n.to_nat) + ((-n).to_nat) = n.nat_abs | (0 : ℕ) := rfl | (n+1 : ℕ) := show (n+1) + 0 = n+1, from add_zero _ | -[1+ n] := show 0 + (n+1) = n+1, from zero_add _ /-- If `n : ℕ`, then `int.to_nat' n = some n`, if `n : ℤ` is negative, then `int.to_nat' n = none`. -/ def to_nat' : ℤ → option ℕ | (n : ℕ) := some n | -[1+ n] := none theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n | (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h lemma to_nat_of_nonpos : ∀ {z : ℤ}, z ≤ 0 → z.to_nat = 0 | (0 : ℕ) := λ _, rfl | (n + 1 : ℕ) := λ h, (h.not_lt (by { exact_mod_cast nat.succ_pos n })).elim | (-[1+ n]) := λ _, rfl /-! ### units -/ @[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 := by simpa only [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma is_unit_eq_one_or {a : ℤ} : is_unit a → a = 1 ∨ a = -1 | ⟨x, hx⟩ := hx ▸ (units_eq_one_or _).imp (congr_arg coe) (congr_arg coe) lemma is_unit_iff {a : ℤ} : is_unit a ↔ a = 1 ∨ a = -1 := begin refine ⟨λ h, is_unit_eq_one_or h, λ h, _⟩, rcases h with rfl | rfl, { exact is_unit_one }, { exact is_unit_one.neg } end theorem is_unit_iff_nat_abs_eq {n : ℤ} : is_unit n ↔ n.nat_abs = 1 := by simp [nat_abs_eq_iff, is_unit_iff] lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) @[simp] lemma units_mul_self (u : units ℤ) : u * u = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) -- `units.coe_mul` is a "wrong turn" for the simplifier, this undoes it and simplifies further @[simp] lemma units_coe_mul_self (u : units ℤ) : (u * u : ℤ) = 1 := by rw [←units.coe_mul, units_mul_self, units.coe_one] @[simp] lemma neg_one_pow_ne_zero {n : ℕ} : (-1 : ℤ)^n ≠ 0 := pow_ne_zero _ (abs_pos.mp trivial) /-! ### bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl lemma bodd_two : bodd 2 = ff := rfl @[simp, norm_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := rfl @[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros; simp; cases i.bodd; simp @[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n := by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, -of_nat_eq_coe, bool.bxor_comm] @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; simp [← int.mul_def, int.mul, -of_nat_eq_coe, bool.bxor_comm] theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : ℕ) := by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : ∀ n, div2 n = n / 2 | (n : ℕ) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ /-- Defines a function from `ℤ` conditionally, if it is defined for odd and even integers separately using `bit`. -/ def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma bodd_bit0 (n : ℤ) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n : ℤ) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add], cases b, { simp }, { show of_nat _ = _, rw nat.div_eq_zero; simp }, { cc } end lemma bit0_ne_bit1 (m n : ℤ) : bit0 m ≠ bit1 n := mt (congr_arg bodd) $ by simp lemma bit1_ne_bit0 (m n : ℤ) : bit1 m ≠ bit0 n := (bit0_ne_bit1 _ _).symm lemma bit1_ne_zero (m : ℤ) : bit1 m ≠ 0 := by simpa only [bit0_zero] using bit1_ne_bit0 m 0 @[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (λ a b, a && bnot b) n m = nat.bitwise (λ a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]}, apply congr_arg (λ f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : ℕ) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : ℕ) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k | (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (λ i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (λ i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k | (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n) | (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n) | (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (pow_pos dec_trivial _) end lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0 | (n : ℕ) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ end int attribute [irreducible] int.nonneg
e495dccd037e591701e3759e953fd2e0c77a4e65
6f1049e897f569e5c47237de40321e62f0181948
/src/exercises/07_first_negations.lean
dbe6da650b950a66445f441e72f347c8bbf271bf
[ "Apache-2.0" ]
permissive
anrddh/tutorials
f654a0807b9523608544836d9a81939f8e1dceb8
3ba43804e7b632201c494cdaa8da5406f1a255f9
refs/heads/master
1,655,542,921,827
1,588,846,595,000
1,588,846,595,000
262,330,134
0
0
null
1,588,944,346,000
1,588,944,345,000
null
UTF-8
Lean
false
false
6,724
lean
import tuto_lib import data.int.parity /- Negations, proof by contradiction and contraposition This file introduces the logical rules and tactics related to negation: exfalso, by_contradiction, contrapose, by_cases and push_neg. There is a special statement denoted by false which, by definition, has no proof. So false implies everything. Indeed `false → P` means any proof of false could be turned into a proof of P. This fact is known by its latin name "ex falso quod libet" (from false follows whatever you want). Hence Lean's tactic to invoke this is called `exfalso`. -/ example : false → 0 = 1 := begin intro h, exfalso, exact h, end /- The preceding example suggest the definition of false isn't very useful. But actually it allows to define the negation of a statement P as "P implies False" that we can read as "if P were true, we would get a contradiction". Lean denote this by `¬ P`. One can prove that (¬ P) ↔ (P ↔ false). But in practice we directly use the definition of ¬ P. -/ example {x : ℝ} : ¬ x < x := begin intro hyp, rw lt_iff_le_and_ne at hyp, cases hyp with hyp_inf hyp_non, clear hyp_inf, -- we won't use that one, so let's discard it change x = x → false at hyp_non, -- Lean doesn't need this psychological line apply hyp_non, refl, end open int -- 0045 example (n : ℤ) (h_pair : even n) (h_non_pair : ¬ even n) : 0 = 1 := begin sorry end -- 0046 example (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q := begin sorry end /- The definition of negation easily implies that, for every statement P, P → ¬ ¬ P The excluded middle axiom, which asserts P ∨ ¬ P allows to prove the converse implication. Together those implications form the principle of double negation elimination. not_not {P : Prop} : (¬ ¬ P) ↔ P The implication ¬ ¬ P → P is the basic for proofs by contradiction: in order to prove P, it suffices to prove ¬¬ P, ie ¬ P → false. Of course there is no need to keep explaining all this. The tactic by_contradiction Hyp, transform any goal P into false and adds Hyp : ¬ P to the local context. Let's return to a proof of the 5th file: uniqueness of limits for a sequence. This cannot be proved without using some version of the excluded middle axiom. We used it secretely in eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y (we'll prove a variation on this lemma below). -/ example (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' := begin intros hl hl', by_contradiction H, change l ≠ l' at H, -- Lean does not need this line have ineg : |l-l'| > 0, exact abs_pos_of_ne_zero (sub_ne_zero_of_ne H), cases hl ( |l-l'|/4 ) (by linarith) with N hN, cases hl' ( |l-l'|/4 ) (by linarith) with N' hN', let N₀ := max N N', -- this is a new tactic, whose effect should be clear specialize hN N₀ (le_max_left _ _), specialize hN' N₀ (le_max_right _ _), have clef : |l-l'| < |l-l'|, calc |l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring ... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add ... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub ... < |l-l'| : by linarith, linarith, -- linarith can also found easy numerical contradictions end /- Another incarnation of the excluded middle axiom is the principle of contraposition: in order to prove P ⇒ Q, it suffices to prove non Q ⇒ non P. -/ -- Using a proof by contradiction, let's prove the contraposition principle -- 0047 example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q := begin sorry end /- Again Lean doesn't need to be explained this principle, we can use the contrapose tactic. -/ example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q := begin contrapose, exact h, end /- In the next exercise, we'll use odd n : ∃ k, n = 2*k + 1 not_even_iff_odd : ¬ even n ↔ odd n, -/ -- 0048 example (n : ℤ) : even (n^2) ↔ even n := begin sorry end /- As a last step of our excluded middle tour, let's notice that, especially in pure logic exercises, it can sometimes be useful to use the excluded middle axiom in its original form: classical.em : ∀ P, P ∨ ¬ P Instead of applying this lemma and then using the cases tactic, we have the shortcut by_cases h : P, combining both steps to create two proof branches: one assuming h : P, and the other assuming h : ¬ P For instance, let's prove a reformulation of this implication relation, which is sometimes used as a definition in other logical foundations, especially those based on truth tables (hence very strongly using excluded middle from the very beginning). -/ variables (P Q : Prop) example : (P → Q) ↔ (¬ P ∨ Q) := begin split, { intro h, by_cases hP : P, { right, exact h hP }, { left, exact hP } }, { intros h hP, cases h with hnP hQ, { exfalso, exact hnP hP }, { exact hQ } }, end -- 0049 example : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q := begin sorry end /- It is crucial to understand negation of quantifiers. Let's do it by hand for a little while. In the first exercise, only the definition of negation is needed. -/ -- 0050 example (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k := begin sorry end /- Contrary to begation of the existential quantifier, negation of the universal quantifier requires excluded middle for the first implication. In order to prove this, we can use either * a double proof by contradiction * a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction. -/ def even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x -- 0051 example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x := begin sorry end /- Of course we can't keep repeating the above proofs, especially the second one. So we use the `push_neg` tactic. -/ example : ¬ even_fun (λ x, 2*x) := begin unfold even_fun, -- Here unfolding is important because push_neg won't do it. push_neg, use 42, linarith, end -- 0052 example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x := begin sorry end def bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M example : ¬ bounded_above (λ x, x) := begin unfold bounded_above, push_neg, intro M, use M + 1, linarith, end -- Let's contrapose -- 0053 example (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 := begin sorry end /- The "contrapose, push_neg" combo is so common that we can abreviate it to `contrapose!` Let's use this trick, together with: eq_or_lt_of_le : a ≤ b → a = b ∨ a < b -/ -- 0054 example (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) := begin sorry end
3dd2f55a113fd33a943d18bfca69cc051c884682
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/monad/equiv_mon.lean
4c35b16c796670b7d06be3ede3fa7c8dd137ac00
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
4,213
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.monad.basic import category_theory.monoidal.End import category_theory.monoidal.Mon_ /-! # The equivalence between `Monad C` and `Mon_ (C ⥤ C)`. A monad "is just" a monoid in the category of endofunctors. # Definitions/Theorems 1. `to_Mon` associates a monoid object in `C ⥤ C` to any monad on `C`. 2. `Monad_to_Mon` is the functorial version of `to_Mon`. 3. `of_Mon` associates a monad on `C` to any monoid object in `C ⥤ C`. 4. `Monad_Mon_equiv` is the equivalence between `Monad C` and `Mon_ (C ⥤ C)`. -/ namespace category_theory open category universes v u -- morphism levels before object levels. See note [category_theory universes]. variables {C : Type u} [category.{v} C] namespace Monad local attribute [instance, reducible] endofunctor_monoidal_category /-- To every `Monad C` we associated a monoid object in `C ⥤ C`.-/ @[simps] def to_Mon : monad C → Mon_ (C ⥤ C) := λ M, { X := (M : C ⥤ C), one := M.η, mul := M.μ, one_mul' := by { ext, simp }, -- `obviously` provides this, but slowly mul_one' := by { ext, simp }, -- `obviously` provides this, but slowly mul_assoc' := by { ext, dsimp, simp [M.assoc] } } variable (C) /-- Passing from `Monad C` to `Mon_ (C ⥤ C)` is functorial. -/ @[simps] def Monad_to_Mon : monad C ⥤ Mon_ (C ⥤ C) := { obj := to_Mon, map := λ _ _ f, { hom := f.to_nat_trans }, map_id' := by { intros X, refl }, -- `obviously` provides this, but slowly map_comp' := by { intros X Y Z f g, refl, } } variable {C} /-- To every monoid object in `C ⥤ C` we associate a `Monad C`. -/ @[simps] def of_Mon : Mon_ (C ⥤ C) → monad C := λ M, { to_functor := M.X, η' := M.one, μ' := M.mul, left_unit' := λ X, by { rw [←M.one.id_hcomp_app, ←nat_trans.comp_app, M.mul_one], refl }, right_unit' := λ X, by { rw [←M.one.hcomp_id_app, ←nat_trans.comp_app, M.one_mul], refl }, assoc' := λ X, by { rw [←nat_trans.hcomp_id_app, ←nat_trans.comp_app], simp } } variable (C) /-- Passing from `Mon_ (C ⥤ C)` to `Monad C` is functorial. -/ @[simps] def Mon_to_Monad : Mon_ (C ⥤ C) ⥤ monad C := { obj := of_Mon, map := λ _ _ f, { app_η' := begin intro X, erw [←nat_trans.comp_app, f.one_hom], refl, end, app_μ' := begin intro X, erw [←nat_trans.comp_app, f.mul_hom], -- `finish` closes this goal simpa only [nat_trans.naturality, nat_trans.hcomp_app, assoc, nat_trans.comp_app, of_Mon_μ], end, ..f.hom } } namespace Monad_Mon_equiv variable {C} /-- Isomorphism of functors used in `Monad_Mon_equiv` -/ @[simps {rhs_md := semireducible}] def counit_iso : Mon_to_Monad C ⋙ Monad_to_Mon C ≅ 𝟭 _ := { hom := { app := λ _, { hom := 𝟙 _ } }, inv := { app := λ _, { hom := 𝟙 _ } }, hom_inv_id' := by { ext, simp }, -- `obviously` provides these, but slowly inv_hom_id' := by { ext, simp } } /-- Auxiliary definition for `Monad_Mon_equiv` -/ @[simps] def unit_iso_hom : 𝟭 _ ⟶ Monad_to_Mon C ⋙ Mon_to_Monad C := { app := λ _, { app := λ _, 𝟙 _ } } /-- Auxiliary definition for `Monad_Mon_equiv` -/ @[simps] def unit_iso_inv : Monad_to_Mon C ⋙ Mon_to_Monad C ⟶ 𝟭 _ := { app := λ _, { app := λ _, 𝟙 _ } } /-- Isomorphism of functors used in `Monad_Mon_equiv` -/ @[simps] def unit_iso : 𝟭 _ ≅ Monad_to_Mon C ⋙ Mon_to_Monad C := { hom := unit_iso_hom, inv := unit_iso_inv, hom_inv_id' := by { ext, simp }, -- `obviously` provides these, but slowly inv_hom_id' := by { ext, simp } } end Monad_Mon_equiv open Monad_Mon_equiv /-- Oh, monads are just monoids in the category of endofunctors (equivalence of categories). -/ @[simps] def Monad_Mon_equiv : (monad C) ≌ (Mon_ (C ⥤ C)) := { functor := Monad_to_Mon _, inverse := Mon_to_Monad _, unit_iso := unit_iso, counit_iso := counit_iso, functor_unit_iso_comp' := by { intros X, ext, dsimp, simp } } -- `obviously`, slowly -- Sanity check example (A : monad C) {X : C} : ((Monad_Mon_equiv C).unit_iso.app A).hom.app X = 𝟙 _ := rfl end Monad end category_theory