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
2478b63b9f4e6b5f9402df9f3f94f95b52a1ca9b
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/subsemiring.lean
2820b7c220d3b8154c428dc9c97aaed427c42bc5
[ "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
27,013
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.ring.prod import group_theory.submonoid import data.equiv.ring /-! # Bundled subsemirings We define bundled subsemirings and some standard constructions: `complete_lattice` structure, `subtype` and `inclusion` ring homomorphisms, subsemiring `map`, `comap` and range (`srange`) of a `ring_hom` etc. -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [semiring R] [semiring S] [semiring T] set_option old_structure_cmd true /-- A subsemiring of a semiring `R` is a subset `s` that is both a multiplicative and an additive submonoid. -/ structure subsemiring (R : Type u) [semiring R] extends submonoid R, add_submonoid R /-- Reinterpret a `subsemiring` as a `submonoid`. -/ add_decl_doc subsemiring.to_submonoid /-- Reinterpret a `subsemiring` as an `add_submonoid`. -/ add_decl_doc subsemiring.to_add_submonoid namespace subsemiring instance : has_coe (subsemiring R) (set R) := ⟨subsemiring.carrier⟩ instance : has_mem R (subsemiring R) := ⟨λ m S, m ∈ (S:set R)⟩ instance : has_coe_to_sort (subsemiring R) := ⟨Type*, λ S, {x : R // x ∈ S}⟩ /-- Construct a `subsemiring R` from a set `s`, a submonoid `sm`, and an additive submonoid `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (hm : ↑sm = s) (sa : add_submonoid R) (ha : ↑sa = s) : subsemiring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) : (subsemiring.mk' s sm hm sa ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) {x : R} : x ∈ subsemiring.mk' s sm hm sa ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) : (subsemiring.mk' s sm hm sa ha).to_submonoid = sm := submonoid.ext' hm.symm @[simp] lemma mk'_to_add_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa =s) : (subsemiring.mk' s sm hm sa ha).to_add_submonoid = sa := add_submonoid.ext' ha.symm end subsemiring protected lemma subsemiring.exists {s : subsemiring R} {p : s → Prop} : (∃ x : s, p x) ↔ ∃ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.exists protected lemma subsemiring.forall {s : subsemiring R} {p : s → Prop} : (∀ x : s, p x) ↔ ∀ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.forall namespace subsemiring variables (s : subsemiring R) /-- Two subsemirings are equal if the underlying subsets are equal. -/ theorem ext' ⦃s t : subsemiring R⦄ (h : (s : set R) = t) : s = t := by cases s; cases t; congr' /-- Two subsemirings are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {s t : subsemiring R} : s = t ↔ (s : set R) = t := ⟨λ h, h ▸ rfl, λ h, ext' h⟩ /-- Two subsemirings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subsemiring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h /-- A subsemiring contains the semiring's 1. -/ theorem one_mem : (1 : R) ∈ s := s.one_mem' /-- A subsemiring contains the semiring's 0. -/ theorem zero_mem : (0 : R) ∈ s := s.zero_mem' /-- A subsemiring is closed under multiplication. -/ theorem mul_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem' /-- A subsemiring is closed under addition. -/ theorem add_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem' /-- Product of a list of elements in a `subsemiring` is in the `subsemiring`. -/ lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := s.to_submonoid.list_prod_mem /-- Sum of a list of elements in a `subsemiring` is in the `subsemiring`. -/ lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := s.to_add_submonoid.list_sum_mem /-- Product of a multiset of elements in a `subsemiring` of a `comm_semiring` is in the `subsemiring`. -/ lemma multiset_prod_mem {R} [comm_semiring R] (s : subsemiring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := s.to_submonoid.multiset_prod_mem m /-- Sum of a multiset of elements in a `subsemiring` of a `semiring` is in the `add_subsemiring`. -/ lemma multiset_sum_mem {R} [semiring R] (s : subsemiring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := s.to_add_submonoid.multiset_sum_mem m /-- Product of elements of a subsemiring of a `comm_semiring` indexed by a `finset` is in the subsemiring. -/ lemma prod_mem {R : Type*} [comm_semiring R] (s : subsemiring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := s.to_submonoid.prod_mem h /-- Sum of elements in an `subsemiring` of an `semiring` indexed by a `finset` is in the `add_subsemiring`. -/ lemma sum_mem {R : Type*} [semiring R] (s : subsemiring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := s.to_add_submonoid.sum_mem h lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n lemma nsmul_mem {x : R} (hx : x ∈ s) (n : ℕ) : n •ℕ x ∈ s := s.to_add_submonoid.nsmul_mem hx n lemma coe_nat_mem (n : ℕ) : (n : R) ∈ s := by simp only [← nsmul_one, nsmul_mem, one_mem] /-- A subsemiring of a semiring inherits a semiring structure -/ instance to_semiring : semiring s := { mul_zero := λ x, subtype.eq $ mul_zero x, zero_mul := λ x, subtype.eq $ zero_mul x, right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_monoid, .. s.to_add_submonoid.to_add_comm_monoid } instance nontrivial [nontrivial R] : nontrivial s := nontrivial_of_ne 0 1 $ λ H, zero_ne_one (congr_arg subtype.val H) instance no_zero_divisors [no_zero_divisors R] : no_zero_divisors s := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y h, or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ subtype.ext_iff.mp h) (λ h, or.inl $ subtype.eq h) (λ h, or.inr $ subtype.eq h) } @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl /-- A subsemiring of a `comm_semiring` is a `comm_semiring`. -/ instance to_comm_semiring {R} [comm_semiring R] (s : subsemiring R) : comm_semiring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..s.to_semiring} /-- The natural ring hom from a subsemiring of semiring `R` to `R`. -/ def subtype : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_submonoid.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl /-- A subsemiring of an `ordered_semiring` is an `ordered_semiring`. -/ instance to_ordered_semiring {R} [ordered_semiring R] (s : subsemiring R) : ordered_semiring s := subtype.coe_injective.ordered_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-- A subsemiring of an `ordered_comm_semiring` is an `ordered_comm_semiring`. -/ instance to_ordered_comm_semiring {R} [ordered_comm_semiring R] (s : subsemiring R) : ordered_comm_semiring s := subtype.coe_injective.ordered_comm_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-- A subsemiring of a `linear_ordered_semiring` is a `linear_ordered_semiring`. -/ instance to_linear_ordered_semiring {R} [linear_ordered_semiring R] (s : subsemiring R) : linear_ordered_semiring s := subtype.coe_injective.linear_ordered_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-! Note: currently, there is no `linear_ordered_comm_semiring`. -/ instance : partial_order (subsemiring R) := { le := λ s t, ∀ ⦃x⦄, x ∈ s → x ∈ t, .. partial_order.lift (coe : subsemiring R → set R) ext' } lemma le_def {s t : subsemiring R} : s ≤ t ↔ ∀ ⦃x : R⦄, x ∈ s → x ∈ t := iff.rfl @[simp, norm_cast] lemma coe_subset_coe {s t : subsemiring R} : (s : set R) ⊆ t ↔ s ≤ t := iff.rfl @[simp, norm_cast] lemma coe_ssubset_coe {s t : subsemiring R} : (s : set R) ⊂ t ↔ s < t := iff.rfl @[simp, norm_cast] lemma mem_coe {S : subsemiring R} {m : R} : m ∈ (S : set R) ↔ m ∈ S := iff.rfl @[simp, norm_cast] lemma coe_coe (s : subsemiring R) : ↥(s : set R) = s := rfl @[simp] lemma mem_to_submonoid {s : subsemiring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subsemiring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_submonoid {s : subsemiring R} {x : R} : x ∈ s.to_add_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_submonoid (s : subsemiring R) : (s.to_add_submonoid : set R) = s := rfl /-- The subsemiring `R` of the semiring `R`. -/ instance : has_top (subsemiring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_submonoid R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subsemiring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subsemiring R) : set R) = set.univ := rfl /-- The preimage of a subsemiring along a ring homomorphism is a subsemiring. -/ def comap (f : R →+* S) (s : subsemiring S) : subsemiring R := { carrier := f ⁻¹' s, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_submonoid.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subsemiring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subsemiring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subsemiring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-- The image of a subsemiring along a ring homomorphism is a subsemiring. -/ def map (f : R →+* S) (s : subsemiring R) : subsemiring S := { carrier := f '' s, .. s.to_submonoid.map (f : R →* S), .. s.to_add_submonoid.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subsemiring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subsemiring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := ext' $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subsemiring R} {t : subsemiring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap end subsemiring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-- The range of a ring homomorphism is a subsemiring. -/ def srange : subsemiring S := (⊤ : subsemiring R).map f @[simp] lemma coe_srange : (f.srange : set S) = set.range f := set.image_univ @[simp] lemma mem_srange {f : R →+* S} {y : S} : y ∈ f.srange ↔ ∃ x, f x = y := by simp [srange] lemma mem_srange_self (f : R →+* S) (x : R) : f x ∈ f.srange := mem_srange.mpr ⟨x, rfl⟩ lemma map_srange : f.srange.map g = (g.comp f).srange := (⊤ : subsemiring R).map_map g f end ring_hom namespace subsemiring instance : has_bot (subsemiring R) := ⟨(nat.cast_ring_hom R).srange⟩ instance : inhabited (subsemiring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subsemiring R) : set R) = set.range (coe : ℕ → R) := (nat.cast_ring_hom R).coe_srange lemma mem_bot {x : R} : x ∈ (⊥ : subsemiring R) ↔ ∃ n : ℕ, ↑n=x := ring_hom.mem_srange /-- The inf of two subsemirings is their intersection. -/ instance : has_inf (subsemiring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_submonoid ⊓ t.to_add_submonoid }⟩ @[simp] lemma coe_inf (p p' : subsemiring R) : ((p ⊓ p' : subsemiring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subsemiring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subsemiring R) := ⟨λ s, subsemiring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subsemiring.to_submonoid t) (by simp) (⨅ t ∈ s, subsemiring.to_add_submonoid t) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subsemiring R)) : ((Inf S : subsemiring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subsemiring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] lemma Inf_to_submonoid (s : set (subsemiring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subsemiring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_submonoid (s : set (subsemiring R)) : (Inf s).to_add_submonoid = ⨅ t ∈ s, subsemiring.to_add_submonoid t := mk'_to_add_submonoid _ _ /-- Subsemirings of a semiring form a complete lattice. -/ instance : complete_lattice (subsemiring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ s.coe_nat_mem n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subsemiring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from coe_subset_coe) is_glb_binfi)} /-- The `subsemiring` generated by a set. -/ def closure (s : set R) : subsemiring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subsemiring R, s ⊆ S → x ∈ S := mem_Inf /-- The subsemiring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx /-- A subsemiring `S` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subsemiring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subsemiring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subsemiring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul, H0, Hadd⟩).2 Hs h lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_submonoid.closure (submonoid.closure s : set R) := ⟨λ h, closure_induction h (λ x hx, add_submonoid.subset_closure $ submonoid.subset_closure hx) (add_submonoid.zero_mem _) (add_submonoid.subset_closure $ submonoid.one_mem $ submonoid.closure s) (λ _ _, add_submonoid.add_mem _) (λ x y ihx ihy, add_submonoid.closure_induction ihy (λ q hq, add_submonoid.closure_induction ihx (λ p hp, add_submonoid.subset_closure $ (submonoid.closure s).mul_mem hp hq) ((zero_mul q).symm ▸ add_submonoid.zero_mem _) (λ p₁ p₂ ihp₁ ihp₂, (add_mul p₁ p₂ q).symm ▸ add_submonoid.add_mem _ ihp₁ ihp₂)) ((mul_zero x).symm ▸ add_submonoid.zero_mem _) (λ q₁ q₂ ihq₁ ihq₂, (mul_add x q₁ q₂).symm ▸ add_submonoid.add_mem _ ihq₁ ihq₂)), λ h, add_submonoid.closure_induction h (λ x hx, submonoid.closure_induction hx subset_closure (one_mem _) (λ _ _, mul_mem _)) (zero_mem _) (λ _ _, add_mem _)⟩ lemma mem_closure_iff_exists_list {s : set R} {x} : x ∈ closure s ↔ ∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s) ∧ (L.map list.prod).sum = x := ⟨λ hx, add_submonoid.closure_induction (mem_closure_iff.1 hx) (λ x hx, suffices ∃ t : list R, (∀ y ∈ t, y ∈ s) ∧ t.prod = x, from let ⟨t, ht1, ht2⟩ := this in ⟨[t], list.forall_mem_singleton.2 ht1, by rw [list.map_singleton, list.sum_singleton, ht2]⟩, submonoid.closure_induction hx (λ x hx, ⟨[x], list.forall_mem_singleton.2 hx, one_mul x⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x y ⟨t, ht1, ht2⟩ ⟨u, hu1, hu2⟩, ⟨t ++ u, list.forall_mem_append.2 ⟨ht1, hu1⟩, by rw [list.prod_append, ht2, hu2]⟩)) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x y ⟨L, HL1, HL2⟩ ⟨M, HM1, HM2⟩, ⟨L ++ M, list.forall_mem_append.2 ⟨HL1, HM1⟩, by rw [list.map_append, list.sum_append, HL2, HM2]⟩), λ ⟨L, HL1, HL2⟩, HL2 ▸ list_sum_mem _ (λ r hr, let ⟨t, ht1, ht2⟩ := list.mem_map.1 hr in ht2 ▸ list_prod_mem _ (λ y hy, subset_closure $ HL1 t ht1 y hy))⟩ variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subsemiring `S` equals `S`. -/ lemma closure_eq (s : subsemiring R) : closure (s : set R) = s := (subsemiring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subsemiring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subsemiring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subsemiring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subsemiring.gi R).gc.l_Sup lemma map_sup (s t : subsemiring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subsemiring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subsemiring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subsemiring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subsemiring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subsemiring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subsemiring`s `s`, `t` of semirings `R`, `S` respectively, `s.prod t` is `s × t` as a subsemiring of `R × S`. -/ def prod (s : subsemiring R) (t : subsemiring S) : subsemiring (R × S) := { carrier := (s : set R).prod t, .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_submonoid.prod t.to_add_submonoid} @[norm_cast] lemma coe_prod (s : subsemiring R) (t : subsemiring S) : (s.prod t : set (R × S)) = (s : set R).prod (t : set S) := rfl lemma mem_prod {s : subsemiring R} {t : subsemiring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subsemiring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subsemiring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subsemiring R) : monotone (λ t : subsemiring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subsemiring S) : monotone (λ s : subsemiring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subsemiring R) : s.prod (⊤ : subsemiring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subsemiring S) : (⊤ : subsemiring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subsemiring R).prod (⊤ : subsemiring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subsemirings is isomorphic to their product as monoids. -/ def prod_equiv (s : subsemiring R) (t : subsemiring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subsemiring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩, let U : subsemiring R := subsemiring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (⨆ i, (S i).to_add_submonoid) (add_submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subsemiring R} (hS : directed (≤) S) : ((⨆ i, S i : subsemiring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] end subsemiring namespace ring_hom variables [semiring T] {s : subsemiring R} open subsemiring /-- Restriction of a ring homomorphism to a subsemiring of the domain. -/ def srestrict (f : R →+* S) (s : subsemiring R) : s →+* S := f.comp s.subtype @[simp] lemma srestrict_apply (f : R →+* S) (x : s) : f.srestrict s x = f x := rfl /-- Restriction of a ring homomorphism to a subsemiring of the codomain. -/ def cod_srestrict (f : R →+* S) (s : subsemiring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ n, ⟨f n, h n⟩, .. (f : R →* S).cod_mrestrict s.to_submonoid h, .. (f : R →+ S).cod_mrestrict s.to_add_submonoid h } /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `set.range_factorization`. -/ def srange_restrict (f : R →+* S) : R →+* f.srange := f.cod_srestrict f.srange f.mem_srange_self @[simp] lemma coe_srange_restrict (f : R →+* S) (x : R) : (f.srange_restrict x : S) = f x := rfl lemma srange_restrict_surjective (f : R →+* S) : function.surjective f.srange_restrict := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_srange.mp hy in ⟨x, subtype.ext hx⟩ lemma srange_top_iff_surjective {f : R →+* S} : f.srange = (⊤ : subsemiring S) ↔ function.surjective f := subsemiring.ext'_iff.trans $ iff.trans (by rw [coe_srange, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma srange_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.srange = (⊤ : subsemiring S) := srange_top_iff_surjective.2 hf /-- The subsemiring of elements `x : R` such that `f x = g x` -/ def eq_slocus (f g : R →+* S) : subsemiring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_mlocus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subsemiring closure. -/ lemma eq_on_sclosure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_slocus g, from closure_le.2 h lemma eq_of_eq_on_stop {f g : R →+* S} (h : set.eq_on f g (⊤ : subsemiring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_sdense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_stop $ hs ▸ eq_on_sclosure h lemma sclosure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subsemiring generated by a set equals the subsemiring generated by the image of the set. -/ lemma map_sclosure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (sclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subsemiring open ring_hom /-- The ring homomorphism associated to an inclusion of subsemirings. -/ def inclusion {S T : subsemiring R} (h : S ≤ T) : S →* T := S.subtype.cod_srestrict _ (λ x, h x.2) @[simp] lemma srange_subtype (s : subsemiring R) : s.subtype.srange = s := ext' $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subsemiring R) (t : subsemiring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨mem_coe.2 $ one_mem ⊥, hp.2⟩) end subsemiring namespace ring_equiv variables {s t : subsemiring R} /-- Makes the identity isomorphism from a proof two subsemirings of a multiplicative monoid are equal. -/ def subsemiring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ subsemiring.ext'_iff.1 h } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `ring_hom.srange`. -/ def sof_left_inverse {g : S → R} {f : R →+* S} (h : function.left_inverse g f) : R ≃+* f.srange := { to_fun := λ x, f.srange_restrict x, inv_fun := λ x, (g ∘ f.srange.subtype) x, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := ring_hom.mem_srange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.srange_restrict } @[simp] lemma sof_left_inverse_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : R) : ↑(sof_left_inverse h x) = f x := rfl @[simp] lemma sof_left_inverse_symm_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : f.srange) : (sof_left_inverse h).symm x = g x := rfl end ring_equiv
03a8bd3bbf4887a34558177fbb160e81c8a49f4f
94637389e03c919023691dcd05bd4411b1034aa5
/src/zzz_junk/has_monoid/monoid.lean
6361cc1e2f878af9b913f88a8648bedca229fbf2
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
664
lean
import ..semigroup.semigroup import ..has_mul.has_mul import ..has_one.has_one /- A monoid is a semigroup with an element 1 such that 1 * a = a * 1 = a. -/ namespace hidden universe u @[class] structure has_monoid (α : Type u) extends (hidden.semigroup α), (hidden.has_one α) : Type u -- mul -- from semigroup extends has_mul (not accessible?) -- mul_assoc -- from semigroup (uses mul) -- one -- from this structures extending from has_one instance has_monoid_bool [semigroup bool] [has_one bool] : hidden.has_monoid bool := ⟨ ⟩ instance has_monoid_nat [semigroup nat] [has_one nat] : has_monoid nat := ⟨ ⟩ end hidden
025f917f26beada05dda5ae80b1ad28ece3a0251
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/metric_space/algebra.lean
3ed29eb490197a6b4baa81318c9809c4fba3022b
[ "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
7,934
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 topology.algebra.mul_action import topology.metric_space.lipschitz /-! # Compatibility of algebraic operations with metric space structures In this file we define mixin typeclasses `has_lipschitz_mul`, `has_lipschitz_add`, `has_bounded_smul` expressing compatibility of multiplication, addition and scalar-multiplication operations with an underlying metric space structure. The intended use case is to abstract certain properties shared by normed groups and by `R≥0`. ## Implementation notes We deduce a `has_continuous_mul` instance from `has_lipschitz_mul`, etc. In principle there should be an intermediate typeclass for uniform spaces, but the algebraic hierarchy there (see `uniform_group`) is structured differently. -/ open_locale nnreal noncomputable theory variables (α β : Type*) [pseudo_metric_space α] [pseudo_metric_space β] section has_lipschitz_mul /-- Class `has_lipschitz_add M` says that the addition `(+) : X × X → X` is Lipschitz jointly in the two arguments. -/ class has_lipschitz_add [add_monoid β] : Prop := ( lipschitz_add : ∃ C, lipschitz_with C (λ p : β × β, p.1 + p.2) ) /-- Class `has_lipschitz_mul M` says that the multiplication `(*) : X × X → X` is Lipschitz jointly in the two arguments. -/ @[to_additive] class has_lipschitz_mul [monoid β] : Prop := ( lipschitz_mul : ∃ C, lipschitz_with C (λ p : β × β, p.1 * p.2) ) variables [monoid β] /-- The Lipschitz constant of a monoid `β` satisfying `has_lipschitz_mul` -/ @[to_additive "The Lipschitz constant of an `add_monoid` `β` satisfying `has_lipschitz_add`"] def has_lipschitz_mul.C [_i : has_lipschitz_mul β] : ℝ≥0 := classical.some _i.lipschitz_mul variables {β} @[to_additive] lemma lipschitz_with_lipschitz_const_mul_edist [_i : has_lipschitz_mul β] : lipschitz_with (has_lipschitz_mul.C β) (λ p : β × β, p.1 * p.2) := classical.some_spec _i.lipschitz_mul variables [has_lipschitz_mul β] @[to_additive] lemma lipschitz_with_lipschitz_const_mul : ∀ p q : β × β, dist (p.1 * p.2) (q.1 * q.2) ≤ (has_lipschitz_mul.C β) * dist p q := begin rw ← lipschitz_with_iff_dist_le_mul, exact lipschitz_with_lipschitz_const_mul_edist, end @[to_additive, priority 100] -- see Note [lower instance priority] instance has_lipschitz_mul.has_continuous_mul : has_continuous_mul β := ⟨ lipschitz_with_lipschitz_const_mul_edist.continuous ⟩ @[to_additive] instance submonoid.has_lipschitz_mul (s : submonoid β) : has_lipschitz_mul s := { lipschitz_mul := ⟨has_lipschitz_mul.C β, begin rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, convert lipschitz_with_lipschitz_const_mul_edist ⟨(x₁:β), x₂⟩ ⟨y₁, y₂⟩ using 1 end⟩ } @[to_additive] instance mul_opposite.has_lipschitz_mul : has_lipschitz_mul βᵐᵒᵖ := { lipschitz_mul := ⟨has_lipschitz_mul.C β, λ ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, (lipschitz_with_lipschitz_const_mul_edist ⟨x₂.unop, x₁.unop⟩ ⟨y₂.unop, y₁.unop⟩).trans_eq (congr_arg _ $ max_comm _ _)⟩ } -- this instance could be deduced from `normed_group.has_lipschitz_add`, but we prove it separately -- here so that it is available earlier in the hierarchy instance real.has_lipschitz_add : has_lipschitz_add ℝ := { lipschitz_add := ⟨2, begin rw lipschitz_with_iff_dist_le_mul, intros p q, simp only [real.dist_eq, prod.dist_eq, prod.fst_sub, prod.snd_sub, nnreal.coe_one, nnreal.coe_bit0], convert le_trans (abs_add (p.1 - q.1) (p.2 - q.2)) _ using 2, { abel }, have := le_max_left (|p.1 - q.1|) (|p.2 - q.2|), have := le_max_right (|p.1 - q.1|) (|p.2 - q.2|), linarith, end⟩ } -- this instance has the same proof as `add_submonoid.has_lipschitz_add`, but the former can't -- directly be applied here since `ℝ≥0` is a subtype of `ℝ`, not an additive submonoid. instance nnreal.has_lipschitz_add : has_lipschitz_add ℝ≥0 := { lipschitz_add := ⟨has_lipschitz_add.C ℝ, begin rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, convert lipschitz_with_lipschitz_const_add_edist ⟨(x₁:ℝ), x₂⟩ ⟨y₁, y₂⟩ using 1 end⟩ } end has_lipschitz_mul section has_bounded_smul variables [has_zero α] [has_zero β] [has_smul α β] /-- Mixin typeclass on a scalar action of a metric space `α` on a metric space `β` both with distinguished points `0`, requiring compatibility of the action in the sense that `dist (x • y₁) (x • y₂) ≤ dist x 0 * dist y₁ y₂` and `dist (x₁ • y) (x₂ • y) ≤ dist x₁ x₂ * dist y 0`. -/ class has_bounded_smul : Prop := ( dist_smul_pair' : ∀ x : α, ∀ y₁ y₂ : β, dist (x • y₁) (x • y₂) ≤ dist x 0 * dist y₁ y₂ ) ( dist_pair_smul' : ∀ x₁ x₂ : α, ∀ y : β, dist (x₁ • y) (x₂ • y) ≤ dist x₁ x₂ * dist y 0 ) variables {α β} [has_bounded_smul α β] lemma dist_smul_pair (x : α) (y₁ y₂ : β) : dist (x • y₁) (x • y₂) ≤ dist x 0 * dist y₁ y₂ := has_bounded_smul.dist_smul_pair' x y₁ y₂ lemma dist_pair_smul (x₁ x₂ : α) (y : β) : dist (x₁ • y) (x₂ • y) ≤ dist x₁ x₂ * dist y 0 := has_bounded_smul.dist_pair_smul' x₁ x₂ y /-- The typeclass `has_bounded_smul` on a metric-space scalar action implies continuity of the action. -/ @[priority 100] -- see Note [lower instance priority] instance has_bounded_smul.has_continuous_smul : has_continuous_smul α β := { continuous_smul := begin rw metric.continuous_iff, rintros ⟨a, b⟩ ε hε, have : 0 ≤ dist a 0 := dist_nonneg, have : 0 ≤ dist b 0 := dist_nonneg, let δ : ℝ := min 1 ((dist a 0 + dist b 0 + 2)⁻¹ * ε), have hδ_pos : 0 < δ, { refine lt_min_iff.mpr ⟨by norm_num, mul_pos _ hε⟩, rw inv_pos, linarith }, refine ⟨δ, hδ_pos, _⟩, rintros ⟨a', b'⟩ hab', calc _ ≤ _ : dist_triangle _ (a • b') _ ... ≤ δ * (dist a 0 + dist b 0 + δ) : _ ... < ε : _, { have : 0 ≤ dist a' a := dist_nonneg, have := dist_triangle b' b 0, have := dist_comm (a • b') (a' • b'), have := dist_comm a a', have : dist a' a ≤ dist (a', b') (a, b) := le_max_left _ _, have : dist b' b ≤ dist (a', b') (a, b) := le_max_right _ _, have := dist_smul_pair a b' b, have := dist_pair_smul a a' b', nlinarith }, { have : δ ≤ _ := min_le_right _ _, have : δ ≤ _ := min_le_left _ _, have : (dist a 0 + dist b 0 + 2)⁻¹ * (ε * (dist a 0 + dist b 0 + δ)) < ε, { rw inv_mul_lt_iff; nlinarith }, nlinarith } end } -- this instance could be deduced from `normed_space.has_bounded_smul`, but we prove it separately -- here so that it is available earlier in the hierarchy instance real.has_bounded_smul : has_bounded_smul ℝ ℝ := { dist_smul_pair' := λ x y₁ y₂, by simpa [real.dist_eq, mul_sub] using (abs_mul x (y₁ - y₂)).le, dist_pair_smul' := λ x₁ x₂ y, by simpa [real.dist_eq, sub_mul] using (abs_mul (x₁ - x₂) y).le } instance nnreal.has_bounded_smul : has_bounded_smul ℝ≥0 ℝ≥0 := { dist_smul_pair' := λ x y₁ y₂, by convert dist_smul_pair (x:ℝ) (y₁:ℝ) y₂ using 1, dist_pair_smul' := λ x₁ x₂ y, by convert dist_pair_smul (x₁:ℝ) x₂ (y:ℝ) using 1 } /-- If a scalar is central, then its right action is bounded when its left action is. -/ instance has_bounded_smul.op [has_smul αᵐᵒᵖ β] [is_central_scalar α β] : has_bounded_smul αᵐᵒᵖ β := { dist_smul_pair' := mul_opposite.rec $ λ x y₁ y₂, by simpa only [op_smul_eq_smul] using dist_smul_pair x y₁ y₂, dist_pair_smul' := mul_opposite.rec $ λ x₁, mul_opposite.rec $ λ x₂ y, by simpa only [op_smul_eq_smul] using dist_pair_smul x₁ x₂ y } end has_bounded_smul
ce3aeadb21f04b44cf68901b3fafe4dbb211c239
98000ddc36f1ec26addea0df1853c69fa0e71d67
/examples.lean
2aa2f426dc48b6096b9f565f930ed2ce16946ed3
[]
no_license
SCRK16/Intuitionism
50b7b5ed780a75c4ff1339c3d36f1133b8acdfe3
a3d9920ae056b39a66e37d1d0e03d246bca1e961
refs/heads/master
1,670,199,869,808
1,598,045,061,000
1,598,045,061,000
254,657,585
1
0
null
1,590,754,909,000
1,586,529,604,000
Lean
UTF-8
Lean
false
false
1,408
lean
import ..Intuitionism.reckless example (P Q : Prop) : (P ∨ Q) → ¬(¬P ∧ ¬Q) := begin intros h₁ h₂, cases h₁ with hp hq, { exact h₂.elim_left hp, }, { exact h₂.elim_right hq, } end lemma LEM_equiv_double_not : (∀ P : Prop, P ∨ ¬P) ↔ ∀ Q : Prop, ¬¬Q → Q := begin split, { intros h Q nnq, cases h Q with hq nq, { exact hq, }, { exfalso, exact nnq nq, } }, { intros h P, apply h (P ∨ ¬P), exact reckless.not_not_or P, } end example (P Q : Prop) (lem : ∀ P : Prop, P ∨ ¬P) : ¬(¬P ∧ ¬Q) → P ∨ Q := begin intro h, cases lem P with hp np, {-- case: P left, exact hp, }, {-- case: ¬P right, have nn := LEM_equiv_double_not.mp lem, apply nn Q, intro nq, exact h (and.intro np nq), } end -- For the proof that the contrapositive of this is reckless, see reckless.lean example (P Q : Prop) : (¬P ∨ Q) → (P → Q) := begin intros h hp, cases h with np hq, {-- case: ¬P exfalso, exact np hp, }, {-- case: Q exact hq, } end example (α : Type) (P : α → Prop) : (¬∃ x, P x) → ∀ x, ¬P x := begin intros h x hpx, apply h, use x, exact hpx, end
a4d2853773dc559621523326e35e114fdfa703e8
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/taylor.lean
0ad33d0d2830954829946f7e08c9c2666c3c160d
[ "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
18,587
lean
/- Copyright (c) 2022 Moritz Doll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Moritz Doll -/ import analysis.calculus.iterated_deriv import analysis.calculus.mean_value import data.polynomial.module /-! # Taylor's theorem This file defines the Taylor polynomial of a real function `f : ℝ → E`, where `E` is a normed vector space over `ℝ` and proves Taylor's theorem, which states that if `f` is sufficiently smooth, then `f` can be approximated by the Taylor polynomial up to an explicit error term. ## Main definitions * `taylor_coeff_within`: the Taylor coefficient using `deriv_within` * `taylor_within`: the Taylor polynomial using `deriv_within` ## Main statements * `taylor_mean_remainder`: Taylor's theorem with the general form of the remainder term * `taylor_mean_remainder_lagrange`: Taylor's theorem with the Lagrange remainder * `taylor_mean_remainder_cauchy`: Taylor's theorem with the Cauchy remainder * `exists_taylor_mean_remainder_bound`: Taylor's theorem for vector valued functions with a polynomial bound on the remainder ## TODO * the Peano form of the remainder * the integral form of the remainder * Generalization to higher dimensions ## Tags Taylor polynomial, Taylor's theorem -/ open_locale big_operators interval topology nat open set variables {𝕜 E F : Type*} variables [normed_add_comm_group E] [normed_space ℝ E] /-- The `k`th coefficient of the Taylor polynomial. -/ noncomputable def taylor_coeff_within (f : ℝ → E) (k : ℕ) (s : set ℝ) (x₀ : ℝ) : E := (k! : ℝ)⁻¹ • (iterated_deriv_within k f s x₀) /-- The Taylor polynomial with derivatives inside of a set `s`. The Taylor polynomial is given by $$∑_{k=0}^n \frac{(x - x₀)^k}{k!} f^{(k)}(x₀),$$ where $f^{(k)}(x₀)$ denotes the iterated derivative in the set `s`. -/ noncomputable def taylor_within (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : polynomial_module ℝ E := (finset.range (n+1)).sum (λ k, polynomial_module.comp (polynomial.X - polynomial.C x₀) (polynomial_module.single ℝ k (taylor_coeff_within f k s x₀))) /-- The Taylor polynomial with derivatives inside of a set `s` considered as a function `ℝ → E`-/ noncomputable def taylor_within_eval (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : E := polynomial_module.eval x (taylor_within f n s x₀) lemma taylor_within_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : taylor_within f (n+1) s x₀ = taylor_within f n s x₀ + polynomial_module.comp (polynomial.X - polynomial.C x₀) (polynomial_module.single ℝ (n+1) (taylor_coeff_within f (n+1) s x₀)) := begin dunfold taylor_within, rw finset.sum_range_succ, end @[simp] lemma taylor_within_eval_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f (n+1) s x₀ x = taylor_within_eval f n s x₀ x + (((n + 1 : ℝ) * n!)⁻¹ * (x - x₀)^(n+1)) • iterated_deriv_within (n + 1) f s x₀ := begin simp_rw [taylor_within_eval, taylor_within_succ, linear_map.map_add, polynomial_module.comp_eval], congr, simp only [polynomial.eval_sub, polynomial.eval_X, polynomial.eval_C, polynomial_module.eval_single, mul_inv_rev], dunfold taylor_coeff_within, rw [←mul_smul, mul_comm, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one, mul_inv_rev], end /-- The Taylor polynomial of order zero evaluates to `f x`. -/ @[simp] lemma taylor_within_zero_eval (f : ℝ → E) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f 0 s x₀ x = f x₀ := begin dunfold taylor_within_eval, dunfold taylor_within, dunfold taylor_coeff_within, simp, end /-- Evaluating the Taylor polynomial at `x = x₀` yields `f x`. -/ @[simp] lemma taylor_within_eval_self (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : taylor_within_eval f n s x₀ x₀ = f x₀ := begin induction n with k hk, { exact taylor_within_zero_eval _ _ _ _}, simp [hk] end lemma taylor_within_apply (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f n s x₀ x = ∑ k in finset.range (n+1), ((k! : ℝ)⁻¹ * (x - x₀)^k) • iterated_deriv_within k f s x₀ := begin induction n with k hk, { simp }, rw [taylor_within_eval_succ, finset.sum_range_succ, hk], simp, end /-- If `f` is `n` times continuous differentiable on a set `s`, then the Taylor polynomial `taylor_within_eval f n s x₀ x` is continuous in `x₀`. -/ lemma continuous_on_taylor_within_eval {f : ℝ → E} {x : ℝ} {n : ℕ} {s : set ℝ} (hs : unique_diff_on ℝ s) (hf : cont_diff_on ℝ n f s) : continuous_on (λ t, taylor_within_eval f n s t x) s := begin simp_rw taylor_within_apply, refine continuous_on_finset_sum (finset.range (n+1)) (λ i hi, _), refine (continuous_on_const.mul ((continuous_on_const.sub continuous_on_id).pow _)).smul _, rw cont_diff_on_iff_continuous_on_differentiable_on_deriv hs at hf, cases hf, specialize hf_left i, simp only [finset.mem_range] at hi, refine (hf_left _), simp only [with_top.coe_le_coe], exact nat.lt_succ_iff.mp hi, end /-- Helper lemma for calculating the derivative of the monomial that appears in Taylor expansions.-/ lemma monomial_has_deriv_aux (t x : ℝ) (n : ℕ) : has_deriv_at (λ y, (x - y)^(n+1)) (-(n+1) * (x - t)^n) t := begin simp_rw sub_eq_neg_add, rw [←neg_one_mul, mul_comm (-1 : ℝ), mul_assoc, mul_comm (-1 : ℝ), ←mul_assoc], convert @has_deriv_at.pow _ _ _ _ _ (n+1) ((has_deriv_at_id t).neg.add_const x), simp only [nat.cast_add, nat.cast_one], end lemma has_deriv_within_at_taylor_coeff_within {f : ℝ → E} {x y : ℝ} {k : ℕ} {s s' : set ℝ} (hs'_unique : unique_diff_within_at ℝ s' y) (hs' : s' ∈ 𝓝[s] y) (hy : y ∈ s') (h : s' ⊆ s) (hf' : differentiable_on ℝ (iterated_deriv_within (k+1) f s) s') : has_deriv_within_at (λ t, (((k+1 : ℝ) * k!)⁻¹ * (x - t)^(k+1)) • iterated_deriv_within (k+1) f s t) ((((k+1 : ℝ) * k!)⁻¹ * (x - y)^(k+1)) • iterated_deriv_within (k+2) f s y - ((k! : ℝ)⁻¹ * (x - y)^k) • iterated_deriv_within (k+1) f s y) s' y := begin have hf'' : has_deriv_within_at (λ t, iterated_deriv_within (k+1) f s t) (iterated_deriv_within (k+2) f s y) s' y := begin convert (hf' y hy).has_deriv_within_at, rw iterated_deriv_within_succ (hs'_unique.mono h), refine (deriv_within_subset h hs'_unique _).symm, exact (hf' y hy).antimono h hs', end, have : has_deriv_within_at (λ t, (((k+1 : ℝ) * k!)⁻¹ * (x - t)^(k+1))) (-((k! : ℝ)⁻¹ * (x - y)^k)) s' y := begin -- Commuting the factors: have : (-((k! : ℝ)⁻¹ * (x - y)^k)) = (((k+1 : ℝ) * k!)⁻¹ * (-(k+1) *(x - y)^k)) := by { field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k], ring_nf }, rw this, exact (monomial_has_deriv_aux y x _).has_deriv_within_at.const_mul _, end, convert this.smul hf'', field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k], rw [neg_div, neg_smul, sub_eq_add_neg], end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for arbitrary sets -/ lemma has_deriv_within_at_taylor_within_eval {f : ℝ → E} {x y : ℝ} {n : ℕ} {s s' : set ℝ} (hs'_unique : unique_diff_within_at ℝ s' y) (hs_unique : unique_diff_on ℝ s) (hs' : s' ∈ 𝓝[s] y) (hy : y ∈ s') (h : s' ⊆ s) (hf : cont_diff_on ℝ n f s) (hf' : differentiable_on ℝ (iterated_deriv_within n f s) s') : has_deriv_within_at (λ t, taylor_within_eval f n s t x) (((n! : ℝ)⁻¹ * (x - y)^n) • (iterated_deriv_within (n+1) f s y)) s' y := begin induction n with k hk, { simp only [taylor_within_zero_eval, nat.factorial_zero, nat.cast_one, inv_one, pow_zero, mul_one, zero_add, one_smul], simp only [iterated_deriv_within_zero] at hf', rw iterated_deriv_within_one hs_unique (h hy), refine has_deriv_within_at.mono _ h, refine differentiable_within_at.has_deriv_within_at _, exact (hf' y hy).antimono h hs' }, simp_rw [nat.add_succ, taylor_within_eval_succ], simp only [add_zero, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one], have hdiff : differentiable_on ℝ (iterated_deriv_within k f s) s' := begin have coe_lt_succ : (k : with_top ℕ) < k.succ := by { rw [with_top.coe_lt_coe], exact lt_add_one k }, refine differentiable_on.mono _ h, exact hf.differentiable_on_iterated_deriv_within coe_lt_succ hs_unique, end, specialize hk (cont_diff_on.of_succ hf) hdiff, convert hk.add (has_deriv_within_at_taylor_coeff_within hs'_unique hs' hy h hf'), exact (add_sub_cancel'_right _ _).symm, end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for open intervals -/ lemma taylor_within_eval_has_deriv_at_Ioo {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ} (hx : a < b) (ht : t ∈ Ioo a b) (hf : cont_diff_on ℝ n f (Icc a b)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Ioo a b)) : has_deriv_at (λ y, taylor_within_eval f n (Icc a b) y x) (((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) t := begin have h_nhds := is_open.mem_nhds is_open_Ioo ht, exact (has_deriv_within_at_taylor_within_eval (unique_diff_within_at_Ioo ht) (unique_diff_on_Icc hx) (nhds_within_le_nhds h_nhds) ht Ioo_subset_Icc_self hf hf') .has_deriv_at h_nhds, end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for closed intervals -/ lemma has_deriv_within_taylor_within_eval_at_Icc {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ} (hx : a < b) (ht : t ∈ Icc a b) (hf : cont_diff_on ℝ n f (Icc a b)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b)) : has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x) (((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) (Icc a b) t := has_deriv_within_at_taylor_within_eval (unique_diff_on_Icc hx t ht) (unique_diff_on_Icc hx) self_mem_nhds_within ht rfl.subset hf hf' /-! ### Taylor's theorem with mean value type remainder estimate -/ /-- **Taylor's theorem** with the general mean value form of the remainder. We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`, and `g` is a differentiable function on `Ioo x₀ x` and continuous on `Icc x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{(x - x')^n}{n!} \frac{g(x) - g(x₀)}{g' x'},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$. -/ lemma taylor_mean_remainder {f : ℝ → ℝ} {g g' : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) (gcont : continuous_on g (Icc x₀ x)) (gdiff : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at g (g' x_1) x_1) (g'_ne : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → g' x_1 ≠ 0) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = ((x - x')^n /n! * (g x - g x₀) / g' x') • (iterated_deriv_within (n+1) f (Icc x₀ x) x') := begin -- We apply the mean value theorem rcases exists_ratio_has_deriv_at_eq_ratio_slope (λ t, taylor_within_eval f n (Icc x₀ x) t x) (λ t, ((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc x₀ x) t)) hx (continuous_on_taylor_within_eval (unique_diff_on_Icc hx) hf) (λ _ hy, taylor_within_eval_has_deriv_at_Ioo x hx hy hf hf') g g' gcont gdiff with ⟨y, hy, h⟩, use [y, hy], -- The rest is simplifications and trivial calculations simp only [taylor_within_eval_self] at h, rw [mul_comm, ←div_left_inj' (g'_ne y hy), mul_div_cancel _ (g'_ne y hy)] at h, rw ←h, field_simp [g'_ne y hy, n.factorial_ne_zero], ring, end /-- **Taylor's theorem** with the Lagrange form of the remainder. We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x₀)^{n+1}}{(n+1)!},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated derivative. -/ lemma taylor_mean_remainder_lagrange {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = (iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x₀)^(n+1) /(n+1)! := begin have gcont : continuous_on (λ (t : ℝ), (x - t) ^ (n + 1)) (Icc x₀ x) := by { refine continuous.continuous_on _, continuity }, have xy_ne : ∀ (y : ℝ), y ∈ Ioo x₀ x → (x - y)^n ≠ 0 := begin intros y hy, refine pow_ne_zero _ _, rw [mem_Ioo] at hy, rw sub_ne_zero, exact hy.2.ne.symm, end, have hg' : ∀ (y : ℝ), y ∈ Ioo x₀ x → -(↑n + 1) * (x - y) ^ n ≠ 0 := λ y hy, mul_ne_zero (neg_ne_zero.mpr (nat.cast_add_one_ne_zero n)) (xy_ne y hy), -- We apply the general theorem with g(t) = (x - t)^(n+1) rcases taylor_mean_remainder hx hf hf' gcont (λ y _, monomial_has_deriv_aux y x _) hg' with ⟨y, hy, h⟩, use [y, hy], simp only [sub_self, zero_pow', ne.def, nat.succ_ne_zero, not_false_iff, zero_sub, mul_neg] at h, rw [h, neg_div, ←div_neg, neg_mul, neg_neg], field_simp [n.cast_add_one_ne_zero, n.factorial_ne_zero, xy_ne y hy], ring, end /-- **Taylor's theorem** with the Cauchy form of the remainder. We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x')^n (x-x₀)}{n!},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated derivative. -/ lemma taylor_mean_remainder_cauchy {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = (iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x')^n /n! * (x - x₀) := begin have gcont : continuous_on id (Icc x₀ x) := continuous.continuous_on (by continuity), have gdiff : (∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at id ((λ (t : ℝ), (1 : ℝ)) x_1) x_1) := λ _ _, has_deriv_at_id _, -- We apply the general theorem with g = id rcases taylor_mean_remainder hx hf hf' gcont gdiff (λ _ _, by simp) with ⟨y, hy, h⟩, use [y, hy], rw h, field_simp [n.factorial_ne_zero], ring, end /-- **Taylor's theorem** with a polynomial bound on the remainder We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`. The difference of `f` and its `n`-th Taylor polynomial can be estimated by `C * (x - a)^(n+1) / n!` where `C` is a bound for the `n+1`-th iterated derivative of `f`. -/ lemma taylor_mean_remainder_bound {f : ℝ → E} {a b C x : ℝ} {n : ℕ} (hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) (hx : x ∈ Icc a b) (hC : ∀ y ∈ Icc a b, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖ ≤ C) : ‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) / n! := begin rcases eq_or_lt_of_le hab with rfl|h, { rw [Icc_self, mem_singleton_iff] at hx, simp [hx] }, -- The nth iterated derivative is differentiable have hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b) := hf.differentiable_on_iterated_deriv_within (with_top.coe_lt_coe.mpr n.lt_succ_self) (unique_diff_on_Icc h), -- We can uniformly bound the derivative of the Taylor polynomial have h' : ∀ (y : ℝ) (hy : y ∈ Ico a x), ‖((n! : ℝ)⁻¹ * (x - y) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) y‖ ≤ (n! : ℝ)⁻¹ * |(x - a)|^n * C, { rintro y ⟨hay, hyx⟩, rw [norm_smul, real.norm_eq_abs], -- Estimate the iterated derivative by `C` refine mul_le_mul _ (hC y ⟨hay, hyx.le.trans hx.2⟩) (by positivity) (by positivity), -- The rest is a trivial calculation rw [abs_mul, abs_pow, abs_inv, nat.abs_cast], mono* with [0 ≤ (n! : ℝ)⁻¹], any_goals { positivity }, linarith [hx.1, hyx] }, -- Apply the mean value theorem for vector valued functions: have A : ∀ t ∈ Icc a x, has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x) (((↑n!)⁻¹ * (x - t) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) t) (Icc a x) t, { assume t ht, have I : Icc a x ⊆ Icc a b := Icc_subset_Icc_right hx.2, exact (has_deriv_within_taylor_within_eval_at_Icc x h (I ht) hf.of_succ hf').mono I }, have := norm_image_sub_le_of_norm_deriv_le_segment' A h' x (right_mem_Icc.2 hx.1), simp only [taylor_within_eval_self] at this, refine this.trans_eq _, -- The rest is a trivial calculation rw [abs_of_nonneg (sub_nonneg.mpr hx.1)], ring_exp, end /-- **Taylor's theorem** with a polynomial bound on the remainder We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`. There exists a constant `C` such that for all `x ∈ Icc a b` the difference of `f` and its `n`-th Taylor polynomial can be estimated by `C * (x - a)^(n+1)`. -/ lemma exists_taylor_mean_remainder_bound {f : ℝ → E} {a b : ℝ} {n : ℕ} (hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) : ∃ C, ∀ x ∈ Icc a b, ‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) := begin rcases eq_or_lt_of_le hab with rfl|h, { refine ⟨0, λ x hx, _⟩, have : a = x, by simpa [← le_antisymm_iff] using hx, simp [← this] }, -- We estimate by the supremum of the norm of the iterated derivative let g : ℝ → ℝ := λ y, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖, use [has_Sup.Sup (g '' Icc a b) / n!], intros x hx, rw div_mul_eq_mul_div₀, refine taylor_mean_remainder_bound hab hf hx (λ y, _), exact (hf.continuous_on_iterated_deriv_within rfl.le $ unique_diff_on_Icc h) .norm.le_Sup_image_Icc, end
6f813885a5863967a7cdc8f2c0870a2434bc038b
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world6/level9.lean
c0ce2355c05368e019bbdc9185b9a1a74e7a82cc
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,007
lean
/- # Proposition world. ## Level 9: a big maze. Lean's "congruence closure" tactic `cc` is good at mazes. Perhaps I should have mentioned it earlier. -/ /- Lemma : no-side-bar There is a way through the following maze. -/ example (A B C D E F G H I J K L : Prop) (f1 : A → B) (f2 : B → E) (f3 : E → D) (f4 : D → A) (f5 : E → F) (f6 : F → C) (f7 : B → C) (f8 : F → G) (f9 : G → J) (f10 : I → J) (f11 : J → I) (f12 : I → H) (f13 : E → H) (f14 : H → K) (f15 : I → L) : A → L := begin cc, end /- Now move onto advanced proposition world, where you will see how to prove goals such as `P ∧ Q` ($P$ and $Q$), `P ∨ Q` ($P$ or $Q$), `P ↔ Q` ($P\iff Q$). You will need to learn five more tactics: `split`, `cases`, `left`, `right`, and `exfalso`, but they are all straightforward, and furthermore they are essentially the last tactics you need to learn in order to complete all the levels of the Natural Number Game, including all 30 levels of Inequality World. -/
959f42359f9688f401f2cb98393bab4e8d9f5df0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/reverse-ffi/lib/lakefile.lean
8c56421b938f2f02316ff4ece500b759ba709cba
[ "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
128
lean
import Lake open System Lake DSL package rffi @[default_target] lean_lib RFFI where defaultFacets := #[LeanLib.sharedFacet]
881a1448d7ac5d46a427fb633a8aa471c41fb658
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/group/arithmetic.lean
a2ede97a8e05a29d5b25614cb2f3c5080d1b9b75
[ "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
33,099
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.measure.ae_measurable /-! # Typeclasses for measurability of operations In this file we define classes `has_measurable_mul` etc and prove dot-style lemmas (`measurable.mul`, `ae_measurable.mul` etc). For binary operations we define two typeclasses: - `has_measurable_mul` says that both left and right multiplication are measurable; - `has_measurable_mul₂` says that `λ p : α × α, p.1 * p.2` is measurable, and similarly for other binary operations. The reason for introducing these classes is that in case of topological space `α` equipped with the Borel `σ`-algebra, instances for `has_measurable_mul₂` etc require `α` to have a second countable topology. We define separate classes for `has_measurable_div`/`has_measurable_sub` because on some types (e.g., `ℕ`, `ℝ≥0∞`) division and/or subtraction are not defined as `a * b⁻¹` / `a + (-b)`. For instances relating, e.g., `has_continuous_mul` to `has_measurable_mul` see file `measure_theory.borel_space`. ## Implementation notes For the heuristics of `@[to_additive]` it is important that the type with a multiplication (or another multiplicative operations) is the first (implicit) argument of all declarations. ## Tags measurable function, arithmetic operator ## Todo * Uniformize the treatment of `pow` and `smul`. * Use `@[to_additive]` to send `has_measurable_pow` to `has_measurable_smul₂`. * This might require changing the definition (swapping the arguments in the function that is in the conclusion of `measurable_smul`.) -/ universes u v open_locale big_operators pointwise measure_theory open measure_theory /-! ### Binary operations: `(+)`, `(*)`, `(-)`, `(/)` -/ /-- We say that a type `has_measurable_add` if `((+) c)` and `(+ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (+)` see `has_measurable_add₂`. -/ class has_measurable_add (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_const_add : ∀ c : M, measurable ((+) c)) (measurable_add_const : ∀ c : M, measurable (+ c)) export has_measurable_add (measurable_const_add measurable_add_const) /-- We say that a type `has_measurable_add` if `uncurry (+)` is a measurable functions. For a typeclass assuming measurability of `((+) c)` and `(+ c)` see `has_measurable_add`. -/ class has_measurable_add₂ (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_add : measurable (λ p : M × M, p.1 + p.2)) export has_measurable_add₂ (measurable_add) has_measurable_add (measurable_const_add measurable_add_const) /-- We say that a type `has_measurable_mul` if `((*) c)` and `(* c)` are measurable functions. For a typeclass assuming measurability of `uncurry (*)` see `has_measurable_mul₂`. -/ @[to_additive] class has_measurable_mul (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_const_mul : ∀ c : M, measurable ((*) c)) (measurable_mul_const : ∀ c : M, measurable (* c)) export has_measurable_mul (measurable_const_mul measurable_mul_const) /-- We say that a type `has_measurable_mul` if `uncurry (*)` is a measurable functions. For a typeclass assuming measurability of `((*) c)` and `(* c)` see `has_measurable_mul`. -/ @[to_additive has_measurable_add₂] class has_measurable_mul₂ (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_mul : measurable (λ p : M × M, p.1 * p.2)) export has_measurable_mul₂ (measurable_mul) section mul variables {M α : Type*} [measurable_space M] [has_mul M] {m : measurable_space α} {f g : α → M} {μ : measure α} include m @[measurability, to_additive] lemma measurable.const_mul [has_measurable_mul M] (hf : measurable f) (c : M) : measurable (λ x, c * f x) := (measurable_const_mul c).comp hf @[measurability, to_additive] lemma ae_measurable.const_mul [has_measurable_mul M] (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c * f x) μ := (has_measurable_mul.measurable_const_mul c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.mul_const [has_measurable_mul M] (hf : measurable f) (c : M) : measurable (λ x, f x * c) := (measurable_mul_const c).comp hf @[measurability, to_additive] lemma ae_measurable.mul_const [has_measurable_mul M] (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x * c) μ := (measurable_mul_const c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.mul' [has_measurable_mul₂ M] (hf : measurable f) (hg : measurable g) : measurable (f * g) := measurable_mul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma measurable.mul [has_measurable_mul₂ M] (hf : measurable f) (hg : measurable g) : measurable (λ a, f a * g a) := measurable_mul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.mul' [has_measurable_mul₂ M] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f * g) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.mul [has_measurable_mul₂ M] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a * g a) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100, to_additive] instance has_measurable_mul₂.to_has_measurable_mul [has_measurable_mul₂ M] : has_measurable_mul M := ⟨λ c, measurable_const.mul measurable_id, λ c, measurable_id.mul measurable_const⟩ @[to_additive] instance pi.has_measurable_mul {ι : Type*} {α : ι → Type*} [∀ i, has_mul (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_mul (α i)] : has_measurable_mul (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_mul _, λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).mul_const _⟩ @[to_additive pi.has_measurable_add₂] instance pi.has_measurable_mul₂ {ι : Type*} {α : ι → Type*} [∀ i, has_mul (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_mul₂ (α i)] : has_measurable_mul₂ (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, measurable_fst.eval.mul measurable_snd.eval⟩ attribute [measurability] measurable.add' measurable.add ae_measurable.add ae_measurable.add' measurable.const_add ae_measurable.const_add measurable.add_const ae_measurable.add_const end mul /-- A version of `measurable_div_const` that assumes `has_measurable_mul` instead of `has_measurable_div`. This can be nice to avoid unnecessary type-class assumptions. -/ @[to_additive /-" A version of `measurable_sub_const` that assumes `has_measurable_add` instead of `has_measurable_sub`. This can be nice to avoid unnecessary type-class assumptions. "-/] lemma measurable_div_const' {G : Type*} [div_inv_monoid G] [measurable_space G] [has_measurable_mul G] (g : G) : measurable (λ h, h / g) := by simp_rw [div_eq_mul_inv, measurable_mul_const] /-- This class assumes that the map `β × γ → β` given by `(x, y) ↦ x ^ y` is measurable. -/ class has_measurable_pow (β γ : Type*) [measurable_space β] [measurable_space γ] [has_pow β γ] := (measurable_pow : measurable (λ p : β × γ, p.1 ^ p.2)) export has_measurable_pow (measurable_pow) /-- `monoid.has_pow` is measurable. -/ instance monoid.has_measurable_pow (M : Type*) [monoid M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_pow M ℕ := ⟨measurable_from_prod_countable $ λ n, begin induction n with n ih, { simp only [pow_zero, ←pi.one_def, measurable_one] }, { simp only [pow_succ], exact measurable_id.mul ih } end⟩ section pow variables {β γ α : Type*} [measurable_space β] [measurable_space γ] [has_pow β γ] [has_measurable_pow β γ] {m : measurable_space α} {μ : measure α} {f : α → β} {g : α → γ} include m @[measurability] lemma measurable.pow (hf : measurable f) (hg : measurable g) : measurable (λ x, f x ^ g x) := measurable_pow.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.pow (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x ^ g x) μ := measurable_pow.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma measurable.pow_const (hf : measurable f) (c : γ) : measurable (λ x, f x ^ c) := hf.pow measurable_const @[measurability] lemma ae_measurable.pow_const (hf : ae_measurable f μ) (c : γ) : ae_measurable (λ x, f x ^ c) μ := hf.pow ae_measurable_const @[measurability] lemma measurable.const_pow (hg : measurable g) (c : β) : measurable (λ x, c ^ g x) := measurable_const.pow hg @[measurability] lemma ae_measurable.const_pow (hg : ae_measurable g μ) (c : β) : ae_measurable (λ x, c ^ g x) μ := ae_measurable_const.pow hg omit m end pow /-- We say that a type `has_measurable_sub` if `(λ x, c - x)` and `(λ x, x - c)` are measurable functions. For a typeclass assuming measurability of `uncurry (-)` see `has_measurable_sub₂`. -/ class has_measurable_sub (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_const_sub : ∀ c : G, measurable (λ x, c - x)) (measurable_sub_const : ∀ c : G, measurable (λ x, x - c)) export has_measurable_sub (measurable_const_sub measurable_sub_const) /-- We say that a type `has_measurable_sub` if `uncurry (-)` is a measurable functions. For a typeclass assuming measurability of `((-) c)` and `(- c)` see `has_measurable_sub`. -/ class has_measurable_sub₂ (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_sub : measurable (λ p : G × G, p.1 - p.2)) export has_measurable_sub₂ (measurable_sub) /-- We say that a type `has_measurable_div` if `((/) c)` and `(/ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (/)` see `has_measurable_div₂`. -/ @[to_additive] class has_measurable_div (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_const_div : ∀ c : G₀, measurable ((/) c)) (measurable_div_const : ∀ c : G₀, measurable (/ c)) export has_measurable_div (measurable_const_div measurable_div_const) /-- We say that a type `has_measurable_div` if `uncurry (/)` is a measurable functions. For a typeclass assuming measurability of `((/) c)` and `(/ c)` see `has_measurable_div`. -/ @[to_additive has_measurable_sub₂] class has_measurable_div₂ (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_div : measurable (λ p : G₀× G₀, p.1 / p.2)) export has_measurable_div₂ (measurable_div) section div variables {G α : Type*} [measurable_space G] [has_div G] {m : measurable_space α} {f g : α → G} {μ : measure α} include m @[measurability, to_additive] lemma measurable.const_div [has_measurable_div G] (hf : measurable f) (c : G) : measurable (λ x, c / f x) := (has_measurable_div.measurable_const_div c).comp hf @[measurability, to_additive] lemma ae_measurable.const_div [has_measurable_div G] (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, c / f x) μ := (has_measurable_div.measurable_const_div c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.div_const [has_measurable_div G] (hf : measurable f) (c : G) : measurable (λ x, f x / c) := (has_measurable_div.measurable_div_const c).comp hf @[measurability, to_additive] lemma ae_measurable.div_const [has_measurable_div G] (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, f x / c) μ := (has_measurable_div.measurable_div_const c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.div' [has_measurable_div₂ G] (hf : measurable f) (hg : measurable g) : measurable (f / g) := measurable_div.comp (hf.prod_mk hg) @[measurability, to_additive] lemma measurable.div [has_measurable_div₂ G] (hf : measurable f) (hg : measurable g) : measurable (λ a, f a / g a) := measurable_div.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.div' [has_measurable_div₂ G] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f / g) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.div [has_measurable_div₂ G] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a / g a) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) attribute [measurability] measurable.sub measurable.sub' ae_measurable.sub ae_measurable.sub' measurable.const_sub ae_measurable.const_sub measurable.sub_const ae_measurable.sub_const omit m @[priority 100, to_additive] instance has_measurable_div₂.to_has_measurable_div [has_measurable_div₂ G] : has_measurable_div G := ⟨λ c, measurable_const.div measurable_id, λ c, measurable_id.div measurable_const⟩ @[to_additive] instance pi.has_measurable_div {ι : Type*} {α : ι → Type*} [∀ i, has_div (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_div (α i)] : has_measurable_div (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_div _, λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).div_const _⟩ @[to_additive pi.has_measurable_sub₂] instance pi.has_measurable_div₂ {ι : Type*} {α : ι → Type*} [∀ i, has_div (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_div₂ (α i)] : has_measurable_div₂ (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, measurable_fst.eval.div measurable_snd.eval⟩ @[measurability] lemma measurable_set_eq_fun {m : measurable_space α} {E} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] {f g : α → E} (hf : measurable f) (hg : measurable g) : measurable_set {x | f x = g x} := begin suffices h_set_eq : {x : α | f x = g x} = {x | (f-g) x = (0 : E)}, { rw h_set_eq, exact (hf.sub hg) measurable_set_eq, }, ext, simp_rw [set.mem_set_of_eq, pi.sub_apply, sub_eq_zero], end lemma measurable_set_eq_fun_of_countable {m : measurable_space α} {E} [measurable_space E] [measurable_singleton_class E] [countable E] {f g : α → E} (hf : measurable f) (hg : measurable g) : measurable_set {x | f x = g x} := begin have : {x | f x = g x} = ⋃ j, {x | f x = j} ∩ {x | g x = j}, { ext1 x, simp only [set.mem_set_of_eq, set.mem_Union, set.mem_inter_iff, exists_eq_right'], }, rw this, refine measurable_set.Union (λ j, measurable_set.inter _ _), { exact hf (measurable_set_singleton j), }, { exact hg (measurable_set_singleton j), }, end lemma ae_eq_trim_of_measurable {α E} {m m0 : measurable_space α} {μ : measure α} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] (hm : m ≤ m0) {f g : α → E} (hf : measurable[m] f) (hg : measurable[m] g) (hfg : f =ᵐ[μ] g) : f =ᶠ[@measure.ae α m (μ.trim hm)] g := begin rwa [filter.eventually_eq, ae_iff, trim_measurable_set_eq hm _], exact (@measurable_set.compl α _ m (@measurable_set_eq_fun α m E _ _ _ _ _ _ hf hg)), end end div /-- We say that a type `has_measurable_neg` if `x ↦ -x` is a measurable function. -/ class has_measurable_neg (G : Type*) [has_neg G] [measurable_space G] : Prop := (measurable_neg : measurable (has_neg.neg : G → G)) /-- We say that a type `has_measurable_inv` if `x ↦ x⁻¹` is a measurable function. -/ @[to_additive] class has_measurable_inv (G : Type*) [has_inv G] [measurable_space G] : Prop := (measurable_inv : measurable (has_inv.inv : G → G)) export has_measurable_inv (measurable_inv) has_measurable_neg (measurable_neg) @[priority 100, to_additive] instance has_measurable_div_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul G] [has_measurable_inv G] : has_measurable_div G := { measurable_const_div := λ c, by { convert (measurable_inv.const_mul c), ext1, apply div_eq_mul_inv }, measurable_div_const := λ c, by { convert (measurable_id.mul_const c⁻¹), ext1, apply div_eq_mul_inv } } section inv variables {G α : Type*} [has_inv G] [measurable_space G] [has_measurable_inv G] {m : measurable_space α} {f : α → G} {μ : measure α} include m @[measurability, to_additive] lemma measurable.inv (hf : measurable f) : measurable (λ x, (f x)⁻¹) := measurable_inv.comp hf @[measurability, to_additive] lemma ae_measurable.inv (hf : ae_measurable f μ) : ae_measurable (λ x, (f x)⁻¹) μ := measurable_inv.comp_ae_measurable hf attribute [measurability] measurable.neg ae_measurable.neg @[simp, to_additive] lemma measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp, to_additive] lemma ae_measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp] lemma measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp] lemma ae_measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ omit m @[to_additive] instance pi.has_measurable_inv {ι : Type*} {α : ι → Type*} [∀ i, has_inv (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_inv (α i)] : has_measurable_inv (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).inv⟩ @[to_additive] lemma measurable_set.inv {s : set G} (hs : measurable_set s) : measurable_set s⁻¹ := measurable_inv hs end inv /-- `div_inv_monoid.has_pow` is measurable. -/ instance div_inv_monoid.has_measurable_zpow (G : Type u) [div_inv_monoid G] [measurable_space G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_pow G ℤ := ⟨measurable_from_prod_countable $ λ n, begin cases n with n n, { simp_rw zpow_of_nat, exact measurable_id.pow_const _ }, { simp_rw zpow_neg_succ_of_nat, exact (measurable_id.pow_const (n + 1)).inv } end⟩ @[priority 100, to_additive] instance has_measurable_div₂_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_div₂ G := ⟨by { simp only [div_eq_mul_inv], exact measurable_fst.mul measurable_snd.inv }⟩ /-- We say that the action of `M` on `α` `has_measurable_vadd` if for each `c` the map `x ↦ c +ᵥ x` is a measurable function and for each `x` the map `c ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_vadd : ∀ c : M, measurable ((+ᵥ) c : α → α)) (measurable_vadd_const : ∀ x : α, measurable (λ c : M, c +ᵥ x)) /-- We say that the action of `M` on `α` `has_measurable_smul` if for each `c` the map `x ↦ c • x` is a measurable function and for each `x` the map `c ↦ c • x` is a measurable function. -/ @[to_additive] class has_measurable_smul (M α : Type*) [has_smul M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_smul : ∀ c : M, measurable ((•) c : α → α)) (measurable_smul_const : ∀ x : α, measurable (λ c : M, c • x)) /-- We say that the action of `M` on `α` `has_measurable_vadd₂` if the map `(c, x) ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd₂ (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_vadd : measurable (function.uncurry (+ᵥ) : M × α → α)) /-- We say that the action of `M` on `α` `has_measurable_smul₂` if the map `(c, x) ↦ c • x` is a measurable function. -/ @[to_additive has_measurable_vadd₂] class has_measurable_smul₂ (M α : Type*) [has_smul M α] [measurable_space M] [measurable_space α] : Prop := (measurable_smul : measurable (function.uncurry (•) : M × α → α)) export has_measurable_smul (measurable_const_smul measurable_smul_const) export has_measurable_smul₂ (measurable_smul) export has_measurable_vadd (measurable_const_vadd measurable_vadd_const) export has_measurable_vadd₂ (measurable_vadd) @[to_additive] instance has_measurable_smul_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul M M := ⟨measurable_id.const_mul, measurable_id.mul_const⟩ @[to_additive] instance has_measurable_smul₂_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ M M := ⟨measurable_mul⟩ @[to_additive] instance submonoid.has_measurable_smul {M α} [measurable_space M] [measurable_space α] [monoid M] [mul_action M α] [has_measurable_smul M α] (s : submonoid M) : has_measurable_smul s α := ⟨λ c, by simpa only using measurable_const_smul (c : M), λ x, (measurable_smul_const x : measurable (λ c : M, c • x)).comp measurable_subtype_coe⟩ @[to_additive] instance subgroup.has_measurable_smul {G α} [measurable_space G] [measurable_space α] [group G] [mul_action G α] [has_measurable_smul G α] (s : subgroup G) : has_measurable_smul s α := s.to_submonoid.has_measurable_smul section smul variables {M β α : Type*} [measurable_space M] [measurable_space β] [has_smul M β] {m : measurable_space α} {f : α → M} {g : α → β} include m @[measurability, to_additive] lemma measurable.smul [has_measurable_smul₂ M β] (hf : measurable f) (hg : measurable g) : measurable (λ x, f x • g x) := measurable_smul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.smul [has_measurable_smul₂ M β] {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x • g x) μ := has_measurable_smul₂.measurable_smul.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100, to_additive] instance has_measurable_smul₂.to_has_measurable_smul [has_measurable_smul₂ M β] : has_measurable_smul M β := ⟨λ c, measurable_const.smul measurable_id, λ y, measurable_id.smul measurable_const⟩ include m variables [has_measurable_smul M β] {μ : measure α} @[measurability, to_additive] lemma measurable.smul_const (hf : measurable f) (y : β) : measurable (λ x, f x • y) := (has_measurable_smul.measurable_smul_const y).comp hf @[measurability, to_additive] lemma ae_measurable.smul_const (hf : ae_measurable f μ) (y : β) : ae_measurable (λ x, f x • y) μ := (has_measurable_smul.measurable_smul_const y).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.const_smul' (hg : measurable g) (c : M) : measurable (λ x, c • g x) := (has_measurable_smul.measurable_const_smul c).comp hg @[measurability, to_additive] lemma measurable.const_smul (hg : measurable g) (c : M) : measurable (c • g) := hg.const_smul' c @[measurability, to_additive] lemma ae_measurable.const_smul' (hg : ae_measurable g μ) (c : M) : ae_measurable (λ x, c • g x) μ := (has_measurable_smul.measurable_const_smul c).comp_ae_measurable hg @[measurability, to_additive] lemma ae_measurable.const_smul (hf : ae_measurable g μ) (c : M) : ae_measurable (c • g) μ := hf.const_smul' c omit m @[to_additive] instance pi.has_measurable_smul {ι : Type*} {α : ι → Type*} [∀ i, has_smul M (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_smul M (α i)] : has_measurable_smul M (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_smul _, λ g, measurable_pi_iff.mpr $ λ i, measurable_smul_const _⟩ /-- `add_monoid.has_smul_nat` is measurable. -/ instance add_monoid.has_measurable_smul_nat₂ (M : Type*) [add_monoid M] [measurable_space M] [has_measurable_add₂ M] : has_measurable_smul₂ ℕ M := ⟨begin suffices : measurable (λ p : M × ℕ, p.2 • p.1), { apply this.comp measurable_swap, }, refine measurable_from_prod_countable (λ n, _), induction n with n ih, { simp only [zero_smul, ←pi.zero_def, measurable_zero] }, { simp only [succ_nsmul], exact measurable_id.add ih } end⟩ /-- `sub_neg_monoid.has_smul_int` is measurable. -/ instance sub_neg_monoid.has_measurable_smul_int₂ (M : Type*) [sub_neg_monoid M] [measurable_space M] [has_measurable_add₂ M] [has_measurable_neg M] : has_measurable_smul₂ ℤ M := ⟨begin suffices : measurable (λ p : M × ℤ, p.2 • p.1), { apply this.comp measurable_swap, }, refine measurable_from_prod_countable (λ n, _), induction n with n n ih, { simp only [of_nat_zsmul], exact measurable_const_smul _, }, { simp only [zsmul_neg_succ_of_nat], exact (measurable_const_smul _).neg } end⟩ end smul section mul_action variables {M β α : Type*} [measurable_space M] [measurable_space β] [monoid M] [mul_action M β] [has_measurable_smul M β] [measurable_space α] {f : α → β} {μ : measure α} variables {G : Type*} [group G] [measurable_space G] [mul_action G β] [has_measurable_smul G β] @[to_additive] lemma measurable_const_smul_iff (c : G) : measurable (λ x, c • f x) ↔ measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] lemma ae_measurable_const_smul_iff (c : G) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] instance : measurable_space Mˣ := measurable_space.comap (coe : Mˣ → M) ‹_› @[to_additive] instance units.has_measurable_smul : has_measurable_smul Mˣ β := { measurable_const_smul := λ c, (measurable_const_smul (c : M) : _), measurable_smul_const := λ x, (measurable_smul_const x : measurable (λ c : M, c • x)).comp measurable_space.le_map_comap, } @[to_additive] lemma is_unit.measurable_const_smul_iff {c : M} (hc : is_unit c) : measurable (λ x, c • f x) ↔ measurable f := let ⟨u, hu⟩ := hc in hu ▸ measurable_const_smul_iff u @[to_additive] lemma is_unit.ae_measurable_const_smul_iff {c : M} (hc : is_unit c) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := let ⟨u, hu⟩ := hc in hu ▸ ae_measurable_const_smul_iff u variables {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [mul_action G₀ β] [has_measurable_smul G₀ β] lemma measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : measurable (λ x, c • f x) ↔ measurable f := (is_unit.mk0 c hc).measurable_const_smul_iff lemma ae_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := (is_unit.mk0 c hc).ae_measurable_const_smul_iff end mul_action /-! ### Opposite monoid -/ section opposite open mul_opposite @[to_additive] instance {α : Type*} [h : measurable_space α] : measurable_space αᵐᵒᵖ := measurable_space.map op h @[to_additive] lemma measurable_mul_op {α : Type*} [measurable_space α] : measurable (op : α → αᵐᵒᵖ) := λ s, id @[to_additive] lemma measurable_mul_unop {α : Type*} [measurable_space α] : measurable (unop : αᵐᵒᵖ → α) := λ s, id @[to_additive] instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_mul Mᵐᵒᵖ := ⟨λ c, measurable_mul_op.comp (measurable_mul_unop.mul_const _), λ c, measurable_mul_op.comp (measurable_mul_unop.const_mul _)⟩ @[to_additive] instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_mul₂ Mᵐᵒᵖ := ⟨measurable_mul_op.comp ((measurable_mul_unop.comp measurable_snd).mul (measurable_mul_unop.comp measurable_fst))⟩ /-- If a scalar is central, then its right action is measurable when its left action is. -/ instance has_measurable_smul.op {M α} [measurable_space M] [measurable_space α] [has_smul M α] [has_smul Mᵐᵒᵖ α] [is_central_scalar M α] [has_measurable_smul M α] : has_measurable_smul Mᵐᵒᵖ α := ⟨ mul_opposite.rec $ λ c, show measurable (λ x, op c • x), by simpa only [op_smul_eq_smul] using measurable_const_smul c, λ x, show measurable (λ c, op (unop c) • x), by simpa only [op_smul_eq_smul] using (measurable_smul_const x).comp measurable_mul_unop⟩ /-- If a scalar is central, then its right action is measurable when its left action is. -/ instance has_measurable_smul₂.op {M α} [measurable_space M] [measurable_space α] [has_smul M α] [has_smul Mᵐᵒᵖ α] [is_central_scalar M α] [has_measurable_smul₂ M α] : has_measurable_smul₂ Mᵐᵒᵖ α := ⟨show measurable (λ x : Mᵐᵒᵖ × α, op (unop x.1) • x.2), begin simp_rw op_smul_eq_smul, refine (measurable_mul_unop.comp measurable_fst).smul measurable_snd, end⟩ @[to_additive] instance has_measurable_smul_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul Mᵐᵒᵖ M := ⟨λ c, measurable_mul_const (unop c), λ x, measurable_mul_unop.const_mul x⟩ @[to_additive] instance has_measurable_smul₂_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ Mᵐᵒᵖ M := ⟨measurable_snd.mul (measurable_mul_unop.comp measurable_fst)⟩ end opposite /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M α : Type*} [monoid M] [measurable_space M] [has_measurable_mul₂ M] {m : measurable_space α} {μ : measure α} include m @[measurability, to_additive] lemma list.measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := begin induction l with f l ihl, { exact measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[measurability, to_additive] lemma list.ae_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := begin induction l with f l ihl, { exact ae_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[measurability, to_additive] lemma list.measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.measurable_prod' hl @[measurability, to_additive] lemma list.ae_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_measurable_prod' hl omit m end monoid section comm_monoid variables {M ι α : Type*} [comm_monoid M] [measurable_space M] [has_measurable_mul₂ M] {m : measurable_space α} {μ : measure α} {f : ι → α → M} include m @[measurability, to_additive] lemma multiset.measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.measurable_prod' (by simpa using hl) } @[measurability, to_additive] lemma multiset.ae_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_measurable_prod' (by simpa using hl) } @[measurability, to_additive] lemma multiset.measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, measurable f) : measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.measurable_prod' hs @[measurability, to_additive] lemma multiset.ae_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_measurable f μ) : ae_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_measurable_prod' hs @[measurability, to_additive] lemma finset.measurable_prod' (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ _ _, measurable.mul) (@measurable_one M _ _ _ _) hf @[measurability, to_additive] lemma finset.measurable_prod (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.measurable_prod' hf @[measurability, to_additive] lemma finset.ae_measurable_prod' (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (∏ i in s, f i) μ := multiset.ae_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[measurability, to_additive] lemma finset.ae_measurable_prod (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_measurable_prod' hf omit m end comm_monoid
d51aca746ec94841525730759c749f76c8f36cd0
0ec6b5eb2131429a4464b8e6c6f09897abba3de1
/src/algebra_tensor.lean
59e50183c4796218605ba93b184614e9bcdd00e5
[]
no_license
kckennylau/local-langlands-abelian
ba1b86e9d956778cd28ac900ffec02b0f0ba172a
ee22666898357dab800a0432214a22c519ed26a9
refs/heads/master
1,584,671,060,846
1,545,151,246,000
1,545,151,246,000
137,111,892
8
0
null
null
null
null
UTF-8
Lean
false
false
10,086
lean
import .algebra noncomputable theory set_option eqn_compiler.zeta true universes u v w u₁ v₁ w₁ infix ` ⊗ `:100 := tensor_product variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring C] [comm_ring D] variables (iA : algebra R A) (iB : algebra R B) (iC : algebra R C) (iD : algebra R D) namespace tensor_product open linear_map set_option class.instance_max_depth 200 def lmul : iA.mod ⊗ iB.mod →ₗ iA.mod ⊗ iB.mod →ₗ iA.mod ⊗ iB.mod := lift $ linear_map.compr₂ ((linear_map.lflip _ _ _ _).comp $ linear_map.compr₂ (curry $ curry $ map (lift iA.lmul) $ lift iB.lmul) (lcurry _ _ _)) (uncurry _ _ _) theorem lmul_tmul (p : iA.mod) (q : iB.mod) (r : iA.mod) (s : iB.mod) : lmul iA iB (p ⊗ₜ q) (r ⊗ₜ s) = (p * r) ⊗ₜ (q * s) := by rw [lmul, lift.tmul, compr₂_apply, uncurry_apply, comp_apply, lflip_apply, compr₂_apply, lcurry_apply, curry_apply, curry_apply, map_tmul, lift.tmul, lift.tmul]; refl instance : comm_ring (iA.mod ⊗ iB.mod) := { mul := λ x y, lmul iA iB x y, mul_assoc := begin intros x y z, show lmul iA iB (lmul iA iB x y) z = lmul iA iB x (lmul iA iB y z), refine tensor_product.induction_on _ _ z _ (λ z₁ z₂, _) (λ z₁ z₂ ih₁ ih₂, _), { simp only [map_zero] }, { refine tensor_product.induction_on _ _ y _ (λ y₁ y₂, _) (λ y₁ y₂ ih₁ ih₂, _), { rw [map_zero₂, map_zero, map_zero₂] }, { refine tensor_product.induction_on _ _ x _ (λ x₁ x₂, _) (λ x₁ x₂ ih₁ ih₂, _), { rw [map_zero₂, map_zero₂, map_zero₂] }, { iterate 4 { rw [lmul_tmul] }, iterate 2 { rw mul_assoc } }, { rw [map_add₂, map_add₂, map_add₂], congr' 1, exacts [ih₁, ih₂] } }, { rw [map_add₂, map_add, map_add₂, map_add], congr' 1, exacts [ih₁, ih₂] } }, { rw [map_add, map_add, map_add], congr' 1, exacts [ih₁, ih₂] } end, one := 1 ⊗ₜ 1, one_mul := λ x, show lmul iA iB (1 ⊗ₜ 1) x = x, from tensor_product.induction_on _ _ x (map_zero _) (λ x y, (lmul_tmul _ _ _ _ _ _).trans $ by rw [one_mul, one_mul]) (λ x y ihx ihy, (map_add _ _ _).trans $ by rw [ihx, ihy]), mul_one := λ x, show lmul iA iB x (1 ⊗ₜ 1) = x, from tensor_product.induction_on _ _ x (map_zero₂ _ _) (λ x y, (lmul_tmul _ _ _ _ _ _).trans $ by rw [mul_one, mul_one]) (λ x y ihx ihy, (map_add₂ _ _ _ _).trans $ by rw [ihx, ihy]), left_distrib := λ _, map_add _, right_distrib := map_add₂ _, mul_comm := λ x y, show lmul iA iB x y = lmul iA iB y x, from tensor_product.induction_on _ _ x (by rw [map_zero₂, map_zero]) (λ x₁ x₂, tensor_product.induction_on _ _ y (by rw [map_zero₂, map_zero]) (λ y₁ y₂, by rw [lmul_tmul, lmul_tmul, mul_comm x₁ y₁, mul_comm x₂ y₂]) (λ y₁ y₂ ih₁ ih₂, by rw [map_add₂, map_add, ih₁, ih₂])) (λ x₁ x₂ ih₁ ih₂, by rw [map_add₂, map_add, ih₁, ih₂]), .. tensor_product.add_comm_group _ _ } set_option class.instance_max_depth 32 theorem mul_def (p : iA.mod) (q : iB.mod) (r : iA.mod) (s : iB.mod) : (p ⊗ₜ q) * (r ⊗ₜ s) = (p * r) ⊗ₜ (q * s) := lmul_tmul _ _ _ _ _ _ theorem one_def : (1 : iA.mod ⊗ iB.mod) = 1 ⊗ₜ 1 := rfl end tensor_product namespace algebra open tensor_product linear_map def tensor_product : algebra R (iA.mod ⊗ iB.mod) := { to_fun := λ r, iA r ⊗ₜ 1, hom := ⟨by rw iA.map_one; refl, λ x y, by rw [iA.map_mul, mul_def, mul_one], λ x y, by rw [iA.map_add, add_tmul]⟩, smul_def' := λ r x, tensor_product.induction_on _ _ x (by rw [smul_zero, mul_zero]) (λ x y, by rw [← tmul_smul, ← smul_tmul, mul_def, iA.smul_def, one_mul]) (λ x y ihx ihy, by rw [smul_add, mul_add, ihx, ihy]) } def inl : iA →ₐ iA.tensor_product iB := { to_fun := λ x, x ⊗ₜ 1, hom := ⟨rfl, λ x y, by rw [mul_def, mul_one], λ x y, add_tmul _ _ _⟩, commutes' := λ r, rfl } theorem inl_def (p : iA.mod) : iA.inl iB p = p ⊗ₜ 1 := rfl def inr : iB →ₐ iA.tensor_product iB := { to_fun := λ x, 1 ⊗ₜ x, hom := ⟨rfl, λ x y, by rw [mul_def, mul_one], λ x y, tmul_add _ _ _⟩, commutes' := λ r, by rw [← mul_one (iB r), ← iB.smul_def, ← smul_tmul, iA.smul_def, mul_one]; refl } theorem inr_def (q : iB.mod) : iA.inr iB q = 1 ⊗ₜ q := rfl namespace tensor_product variables {iA iB iC} set_option class.instance_max_depth 100 @[elab_with_expected_type] def arec (f : iA →ₐ iC) (g : iB →ₐ iC) : iA.tensor_product iB →ₐ iC := { to_fun := (tensor_product.lift iC.lmul).comp $ map f.to_linear_map g.to_linear_map, hom := ⟨by rw [one_def, comp_apply, map_tmul, lift.tmul]; show f 1 * g 1 = 1; rw [f.map_one, g.map_one, mul_one], λ x y, tensor_product.induction_on _ _ x (by rw [zero_mul, linear_map.map_zero]; exact (zero_mul _).symm) (λ x₁ x₂, tensor_product.induction_on _ _ y (by rw [mul_zero, linear_map.map_zero]; exact (mul_zero _).symm) (λ y₁ y₂, by simp only [mul_def, comp_apply, map_tmul, lift.tmul]; change f _ * g _ = (f _ * g _) * (f _ * g _); rw [f.map_mul, g.map_mul, mul_assoc, mul_assoc, mul_left_comm (f y₁)]) (λ y₁ y₂ ih₁ ih₂, by simp only [mul_add, linear_map.map_add, ih₁, ih₂])) (λ x₁ x₂ ih₁ ih₂, by simp only [add_mul, linear_map.map_add, ih₁, ih₂]), linear_map.map_add _⟩, commutes' := λ r, show lift iC.lmul (map f.to_linear_map g.to_linear_map (iA r ⊗ₜ 1)) = _, by rw [map_tmul, lift.tmul]; change f _ * g 1 = _; rw [f.commutes, g.map_one, mul_one] } set_option class.instance_max_depth 32 theorem arec_tmul (f : iA →ₐ iC) (g : iB →ₐ iC) (p : iA.mod) (q : iB.mod) : arec f g (p ⊗ₜ q) = f p * g q := lift.tmul _ _ variables (iA iB iC) def UMP : ((iA →ₐ iC) × (iB →ₐ iC)) ≃ (iA.tensor_product iB →ₐ iC) := { to_fun := λ φ, arec φ.1 φ.2, inv_fun := λ φ, (φ.comp (iA.inl iB), φ.comp (iA.inr iB)), left_inv := λ ⟨φ₁, φ₂⟩, prod.ext (by ext p; change (arec φ₁ φ₂) (p ⊗ₜ 1) = φ₁ p; rw [arec_tmul, φ₂.map_one', mul_one]) (by ext q; change (arec φ₁ φ₂) (1 ⊗ₜ q) = φ₂ q; rw [arec_tmul, φ₁.map_one', one_mul]), right_inv := λ φ, alg_hom.to_linear_map_inj $ tensor_product.ext $ λ p q, by dsimp only [alg_hom.to_linear_map_apply]; rw [arec_tmul, alg_hom.comp_apply, alg_hom.comp_apply, inl_def, inr_def, ← φ.map_mul, mul_def, mul_one, one_mul] } variables {iA iB iC iD} def amap (f : iA →ₐ iC) (g : iB →ₐ iD) : (iA.tensor_product iB) →ₐ (iC.tensor_product iD) := arec ((iC.inl iD).comp f) ((iC.inr iD).comp g) variables (iA iB iC iD) @[simp] lemma amap_tmul (f : iA →ₐ iC) (g : iB →ₐ iD) (x : A) (y : B) : amap f g (x ⊗ₜ y) = f x ⊗ₜ g y := by rw amap; simp only [arec_tmul, alg_hom.comp_apply, inl_def, inr_def, mul_def, mul_one, one_mul] def aassoc : (iA.tensor_product iB).tensor_product iC →ₐ iA.tensor_product (iB.tensor_product iC) := arec (arec (iA.inl $ iB.tensor_product iC) ((iA.inr $ iB.tensor_product iC).comp $ iB.inl iC)) ((iA.inr $ iB.tensor_product iC).comp $ iB.inr iC) @[simp] lemma aassoc_tmul (x y z) : aassoc iA iB iC (x ⊗ₜ y ⊗ₜ z) = x ⊗ₜ (y ⊗ₜ z) := by rw aassoc; simp only [arec_tmul, inl_def, inr_def, alg_hom.comp_apply, mul_def, mul_one, one_mul] def id_tensor : (algebra.id R).tensor_product iA →ₐ iA := arec iA.of_id (alg_hom.id iA) @[simp] lemma id_tensor_tmul (r x) : id_tensor iA (r ⊗ₜ x) = ((r : R) • x : iA.mod) := by rw id_tensor; simp only [arec_tmul, alg_hom.id_apply, of_id_apply, iA.smul_def] def tensor_id : iA.tensor_product (algebra.id R) →ₐ iA := arec (alg_hom.id iA) iA.of_id @[simp] lemma tensor_id_tmul (r x) : tensor_id iA (x ⊗ₜ r) = ((r : R) • x : iA.mod) := by rw tensor_id; simp only [arec_tmul, alg_hom.id_apply, of_id_apply, iA.smul_def, mul_comm] def base_change_left : algebra iA.mod (iA.mod ⊗ iB.mod) := algebra.of_core { to_fun := iA.inl iB } theorem base_change_left_apply (r : iA.mod) : base_change_left iA iB r = r ⊗ₜ 1 := rfl set_option class.instance_max_depth 100 def base_change_left_rec {D : Type v₁} [comm_ring D] (iD : algebra iA.mod D) (φ : iB →ₐ iA.comap iD) : base_change_left iA iB →ₐ iD := { to_fun := (tensor_product.lift (iA.comap iD).lmul).comp (map (iA.to_comap iD).to_linear_map φ.to_linear_map), hom := ⟨by simp only [comp_apply, one_def, map_tmul, alg_hom.to_linear_map_apply, lmul_apply, lift.tmul, φ.map_one', (to_comap iA iD).map_one']; apply mul_one, λ x y, tensor_product.induction_on _ _ x (by rw [zero_mul, linear_map.map_zero]; exact (zero_mul _).symm) (λ x₁ x₂, tensor_product.induction_on _ _ y (by rw [mul_zero, linear_map.map_zero]; exact (mul_zero _).symm) (λ y₁ y₂, by simp only [mul_def, comp_apply, map_tmul, alg_hom.to_linear_map_apply, lmul_apply, lift.tmul, (iA.to_comap iD).map_mul, φ.map_mul, mul_assoc]; rw mul_left_comm ((iA.to_comap iD) y₁)) (λ y₁ y₂ ih₁ ih₂, by rw [mul_add, linear_map.map_add, ih₁, ih₂, linear_map.map_add, mul_add])) (λ x₁ x₂ ih₁ ih₂, by rw [add_mul, linear_map.map_add, ih₁, ih₂, linear_map.map_add, add_mul]), linear_map.map_add _⟩, commutes' := λ x, by rw [comp_apply, base_change_left_apply, map_tmul, alg_hom.to_linear_map_apply, alg_hom.to_linear_map_apply, to_comap_apply, lift.tmul, lmul_apply, φ.map_one']; exact mul_one _ } set_option class.instance_max_depth 32 def base_change_right : algebra B (iA.mod ⊗ iB.mod) := algebra.of_core { to_fun := iA.inr iB } end tensor_product end algebra
4f4b55755cb2a65160bc82c9965fc4d8be4aa618
bb31430994044506fa42fd667e2d556327e18dfe
/src/group_theory/submonoid/basic.lean
a503eca7573f67251c2948fd16a4350d1ba9aac6
[ "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
22,069
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import algebra.hom.group -- Only needed for notation import algebra.group.units import group_theory.subsemigroup.basic /-! # Submonoids: definition and `complete_lattice` structure > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines bundled multiplicative and additive submonoids. We also define a `complete_lattice` structure on `submonoid`s, define the closure of a set as the minimal submonoid that includes this set, and prove a few results about extending properties from a dense set (i.e. a set with `closure s = ⊤`) to the whole monoid, see `submonoid.dense_induction` and `monoid_hom.of_mclosure_eq_top_left`/`monoid_hom.of_mclosure_eq_top_right`. ## Main definitions * `submonoid M`: the type of bundled submonoids of a monoid `M`; the underlying set is given in the `carrier` field of the structure, and should be accessed through coercion as in `(S : set M)`. * `add_submonoid M` : the type of bundled submonoids of an additive monoid `M`. For each of the following definitions in the `submonoid` namespace, there is a corresponding definition in the `add_submonoid` namespace. * `submonoid.copy` : copy of a submonoid with `carrier` replaced by a set that is equal but possibly not definitionally equal to the carrier of the original `submonoid`. * `submonoid.closure` : monoid closure of a set, i.e., the least submonoid that includes the set. * `submonoid.gi` : `closure : set M → submonoid M` and coercion `coe : submonoid M → set M` form a `galois_insertion`; * `monoid_hom.eq_mlocus`: the submonoid of elements `x : M` such that `f x = g x`; * `monoid_hom.of_mclosure_eq_top_right`: if a map `f : M → N` between two monoids satisfies `f 1 = 1` and `f (x * y) = f x * f y` for `y` from some dense set `s`, then `f` is a monoid homomorphism. E.g., if `f : ℕ → M` satisfies `f 0 = 0` and `f (x + 1) = f x + f 1`, then `f` is an additive monoid homomorphism. ## Implementation notes Submonoid inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a submonoid's underlying set. Note that `submonoid M` does not actually require `monoid M`, instead requiring only the weaker `mul_one_class M`. This file is designed to have very few dependencies. In particular, it should not use natural numbers. `submonoid` is implemented by extending `subsemigroup` requiring `one_mem'`. ## Tags submonoid, submonoids -/ variables {M : Type*} {N : Type*} variables {A : Type*} section non_assoc variables [mul_one_class M] {s : set M} variables [add_zero_class A] {t : set A} /-- `one_mem_class S M` says `S` is a type of subsets `s ≤ M`, such that `1 ∈ s` for all `s`. -/ class one_mem_class (S : Type*) (M : out_param $ Type*) [has_one M] [set_like S M] : Prop := (one_mem : ∀ (s : S), (1 : M) ∈ s) export one_mem_class (one_mem) /-- `zero_mem_class S M` says `S` is a type of subsets `s ≤ M`, such that `0 ∈ s` for all `s`. -/ class zero_mem_class (S : Type*) (M : out_param $ Type*) [has_zero M] [set_like S M] : Prop := (zero_mem : ∀ (s : S), (0 : M) ∈ s) export zero_mem_class (zero_mem) attribute [to_additive] one_mem_class section set_option old_structure_cmd true /-- A submonoid of a monoid `M` is a subset containing 1 and closed under multiplication. -/ @[ancestor subsemigroup] structure submonoid (M : Type*) [mul_one_class M] extends subsemigroup M := (one_mem' : (1 : M) ∈ carrier) end /-- A submonoid of a monoid `M` can be considered as a subsemigroup of that monoid. -/ add_decl_doc submonoid.to_subsemigroup /-- `submonoid_class S M` says `S` is a type of subsets `s ≤ M` that contain `1` and are closed under `(*)` -/ class submonoid_class (S : Type*) (M : out_param $ Type*) [mul_one_class M] [set_like S M] extends mul_mem_class S M, one_mem_class S M : Prop section set_option old_structure_cmd true /-- An additive submonoid of an additive monoid `M` is a subset containing 0 and closed under addition. -/ @[ancestor add_subsemigroup] structure add_submonoid (M : Type*) [add_zero_class M] extends add_subsemigroup M := (zero_mem' : (0 : M) ∈ carrier) end /-- An additive submonoid of an additive monoid `M` can be considered as an additive subsemigroup of that additive monoid. -/ add_decl_doc add_submonoid.to_add_subsemigroup /-- `add_submonoid_class S M` says `S` is a type of subsets `s ≤ M` that contain `0` and are closed under `(+)` -/ class add_submonoid_class (S : Type*) (M : out_param $ Type*) [add_zero_class M] [set_like S M] extends add_mem_class S M, zero_mem_class S M : Prop attribute [to_additive] submonoid submonoid_class @[to_additive] lemma pow_mem {M} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] {S : A} {x : M} (hx : x ∈ S) : ∀ (n : ℕ), x ^ n ∈ S | 0 := by { rw pow_zero, exact one_mem_class.one_mem S } | (n + 1) := by { rw pow_succ, exact mul_mem_class.mul_mem hx (pow_mem n) } namespace submonoid @[to_additive] instance : set_like (submonoid M) M := { coe := submonoid.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } @[to_additive] instance : submonoid_class (submonoid M) M := { one_mem := submonoid.one_mem', mul_mem := submonoid.mul_mem' } /-- See Note [custom simps projection] -/ @[to_additive " See Note [custom simps projection]"] def simps.coe (S : submonoid M) : set M := S initialize_simps_projections submonoid (carrier → coe) initialize_simps_projections add_submonoid (carrier → coe) @[simp, to_additive] lemma mem_carrier {s : submonoid M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma mem_mk {s : set M} {x : M} (h_one) (h_mul) : x ∈ mk s h_one h_mul ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma coe_set_mk {s : set M} (h_one) (h_mul) : (mk s h_one h_mul : set M) = s := rfl @[simp, to_additive] lemma mk_le_mk {s t : set M} (h_one) (h_mul) (h_one') (h_mul') : mk s h_one h_mul ≤ mk t h_one' h_mul' ↔ s ⊆ t := iff.rfl /-- Two submonoids are equal if they have the same elements. -/ @[ext, to_additive "Two `add_submonoid`s are equal if they have the same elements."] theorem ext {S T : submonoid M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy a submonoid replacing `carrier` with a set that is equal to it. -/ @[to_additive "Copy an additive submonoid replacing `carrier` with a set that is equal to it."] protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := λ _ _, hs.symm ▸ S.mul_mem' } variable {S : submonoid M} @[simp, to_additive] lemma coe_copy {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl @[to_additive] lemma copy_eq {s : set M} (hs : s = S) : S.copy s hs = S := set_like.coe_injective hs variable (S) /-- A submonoid contains the monoid's 1. -/ @[to_additive "An `add_submonoid` contains the monoid's 0."] protected theorem one_mem : (1 : M) ∈ S := one_mem S /-- A submonoid is closed under multiplication. -/ @[to_additive "An `add_submonoid` is closed under addition."] protected theorem mul_mem {x y : M} : x ∈ S → y ∈ S → x * y ∈ S := mul_mem /-- The submonoid `M` of the monoid `M`. -/ @[to_additive "The additive submonoid `M` of the `add_monoid M`."] instance : has_top (submonoid M) := ⟨{ carrier := set.univ, one_mem' := set.mem_univ 1, mul_mem' := λ _ _ _ _, set.mem_univ _ }⟩ /-- The trivial submonoid `{1}` of an monoid `M`. -/ @[to_additive "The trivial `add_submonoid` `{0}` of an `add_monoid` `M`."] instance : has_bot (submonoid M) := ⟨{ carrier := {1}, one_mem' := set.mem_singleton 1, mul_mem' := λ a b ha hb, by { simp only [set.mem_singleton_iff] at *, rw [ha, hb, mul_one] }}⟩ @[to_additive] instance : inhabited (submonoid M) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : M} : x ∈ (⊥ : submonoid M) ↔ x = 1 := set.mem_singleton_iff @[simp, to_additive] lemma mem_top (x : M) : x ∈ (⊤ : submonoid M) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : submonoid M) : set M) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : submonoid M) : set M) = {1} := rfl /-- The inf of two submonoids is their intersection. -/ @[to_additive "The inf of two `add_submonoid`s is their intersection."] instance : has_inf (submonoid M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, one_mem' := ⟨S₁.one_mem, S₂.one_mem⟩, mul_mem' := λ _ _ ⟨hx, hx'⟩ ⟨hy, hy'⟩, ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩ @[simp, to_additive] lemma coe_inf (p p' : submonoid M) : ((p ⊓ p' : submonoid M) : set M) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : submonoid M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (submonoid M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, one_mem' := set.mem_bInter $ λ i h, i.one_mem, mul_mem' := λ x y hx hy, set.mem_bInter $ λ i h, i.mul_mem (by apply set.mem_Inter₂.1 hx i h) (by apply set.mem_Inter₂.1 hy i h) }⟩ @[simp, norm_cast, to_additive] lemma coe_Inf (S : set (submonoid M)) : ((Inf S : submonoid M) : set M) = ⋂ s ∈ S, ↑s := rfl @[to_additive] lemma mem_Inf {S : set (submonoid M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → submonoid M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast, to_additive] lemma coe_infi {ι : Sort*} {S : ι → submonoid M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Submonoids of a monoid form a complete lattice. -/ @[to_additive "The `add_submonoid`s of an `add_monoid` form a complete lattice."] instance : complete_lattice (submonoid M) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (submonoid M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from set_like.coe_subset_coe) is_glb_binfi } @[simp, to_additive] lemma subsingleton_iff : subsingleton (submonoid M) ↔ subsingleton M := ⟨ λ h, by exactI ⟨λ x y, have ∀ i : M, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : submonoid M) ⊥ ▸ mem_top i, (this x).trans (this y).symm⟩, λ h, by exactI ⟨λ x y, submonoid.ext $ λ i, subsingleton.elim 1 i ▸ by simp [submonoid.one_mem]⟩⟩ @[simp, to_additive] lemma nontrivial_iff : nontrivial (submonoid M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton M] : unique (submonoid M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mpr ‹_›) a _⟩ @[to_additive] instance [nontrivial M] : nontrivial (submonoid M) := nontrivial_iff.mpr ‹_› /-- The `submonoid` generated by a set. -/ @[to_additive "The `add_submonoid` generated by a set"] def closure (s : set M) : submonoid M := Inf {S | s ⊆ S} @[to_additive] lemma mem_closure {x : M} : x ∈ closure s ↔ ∀ S : submonoid M, s ⊆ S → x ∈ S := mem_Inf /-- The submonoid generated by a set includes the set. -/ @[simp, to_additive "The `add_submonoid` generated by a set includes the set."] lemma subset_closure : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx @[to_additive] lemma not_mem_of_not_mem_closure {P : M} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) variable {S} open set /-- A submonoid `S` includes `closure s` if and only if it includes `s`. -/ @[simp, to_additive "An additive submonoid `S` includes `closure s` if and only if it includes `s`"] lemma closure_le : closure s ≤ S ↔ s ⊆ S := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ /-- Submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ @[to_additive "Additive submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`"] lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ subset.trans h subset_closure @[to_additive] lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure s) : closure s = S := le_antisymm (closure_le.2 h₁) h₂ variable (S) /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the additive closure of `s`."] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, Hmul, H1⟩).2 Hs h /-- A dependent version of `submonoid.closure_induction`. -/ @[elab_as_eliminator, to_additive "A dependent version of `add_submonoid.closure_induction`. "] lemma closure_induction' (s : set M) {p : Π x, x ∈ closure s → Prop} (Hs : ∀ x (h : x ∈ s), p x (subset_closure h)) (H1 : p 1 (one_mem _)) (Hmul : ∀ x hx y hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) {x} (hx : x ∈ closure s) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p x hx), hc), exact closure_induction hx (λ x hx, ⟨_, Hs x hx⟩) ⟨_, H1⟩ (λ x y ⟨hx', hx⟩ ⟨hy', hy⟩, ⟨_, Hmul _ _ _ _ hx hy⟩), end /-- An induction principle for closure membership for predicates with two arguments. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership for predicates with two arguments."] lemma closure_induction₂ {p : M → M → Prop} {x} {y : M} (hx : x ∈ closure s) (hy : y ∈ closure s) (Hs : ∀ (x ∈ s) (y ∈ s), p x y) (H1_left : ∀ x, p 1 x) (H1_right : ∀ x, p x 1) (Hmul_left : ∀ x y z, p x z → p y z → p (x * y) z) (Hmul_right : ∀ x y z, p z x → p z y → p z (x * y)) : p x y := closure_induction hx (λ x xs, closure_induction hy (Hs x xs) (H1_right x) (λ z y h₁ h₂, Hmul_right z _ _ h₁ h₂)) (H1_left y) (λ x z h₁ h₂, Hmul_left _ _ _ h₁ h₂) /-- If `s` is a dense set in a monoid `M`, `submonoid.closure s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, verify `p 1`, and verify that `p x` and `p y` imply `p (x * y)`. -/ @[elab_as_eliminator, to_additive "If `s` is a dense set in an additive monoid `M`, `add_submonoid.closure s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, verify `p 0`, and verify that `p x` and `p y` imply `p (x + y)`."] lemma dense_induction {p : M → Prop} (x : M) {s : set M} (hs : closure s = ⊤) (Hs : ∀ x ∈ s, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := have ∀ x ∈ closure s, p x, from λ x hx, closure_induction hx Hs H1 Hmul, by simpa [hs] using this x variable (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure M _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {M} /-- Closure of a submonoid `S` equals `S`. -/ @[simp, to_additive "Additive closure of an additive submonoid `S` equals `S`"] lemma closure_eq : closure (S : set M) = S := (submonoid.gi M).l_u_eq S @[simp, to_additive] lemma closure_empty : closure (∅ : set M) = ⊥ := (submonoid.gi M).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set M) = ⊤ := @coe_top M _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set M) : closure (s ∪ t) = closure s ⊔ closure t := (submonoid.gi M).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set M) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (submonoid.gi M).gc.l_supr @[simp, to_additive] lemma closure_singleton_le_iff_mem (m : M) (p : submonoid M) : closure {m} ≤ p ↔ m ∈ p := by rw [closure_le, singleton_subset_iff, set_like.mem_coe] @[to_additive] lemma mem_supr {ι : Sort*} (p : ι → submonoid M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← closure_singleton_le_iff_mem, le_supr_iff], simp only [closure_singleton_le_iff_mem], end @[to_additive] lemma supr_eq_closure {ι : Sort*} (p : ι → submonoid M) : (⨆ i, p i) = submonoid.closure (⋃ i, (p i : set M)) := by simp_rw [submonoid.closure_Union, submonoid.closure_eq] @[to_additive] lemma disjoint_def {p₁ p₂ : submonoid M} : disjoint p₁ p₂ ↔ ∀ {x : M}, x ∈ p₁ → x ∈ p₂ → x = 1 := by simp_rw [disjoint_iff_inf_le, set_like.le_def, mem_inf, and_imp, mem_bot] @[to_additive] lemma disjoint_def' {p₁ p₂ : submonoid M} : disjoint p₁ p₂ ↔ ∀ {x y : M}, x ∈ p₁ → y ∈ p₂ → x = y → x = 1 := disjoint_def.trans ⟨λ h x y hx hy hxy, h hx $ hxy.symm ▸ hy, λ h x hx hx', h hx hx' rfl⟩ end submonoid namespace monoid_hom variables [mul_one_class N] open submonoid /-- The submonoid of elements `x : M` such that `f x = g x` -/ @[to_additive "The additive submonoid of elements `x : M` such that `f x = g x`"] def eq_mlocus (f g : M →* N) : submonoid M := { carrier := {x | f x = g x}, one_mem' := by rw [set.mem_set_of_eq, f.map_one, g.map_one], mul_mem' := λ x y (hx : _ = _) (hy : _ = _), by simp [*] } @[simp, to_additive] lemma eq_mlocus_same (f : M →* N) : f.eq_mlocus f = ⊤ := set_like.ext $ λ _, eq_self_iff_true _ /-- If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure. -/ @[to_additive "If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure."] lemma eq_on_mclosure {f g : M →* N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_mlocus g, from closure_le.2 h @[to_additive] lemma eq_of_eq_on_mtop {f g : M →* N} (h : set.eq_on f g (⊤ : submonoid M)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_mdense {s : set M} (hs : closure s = ⊤) {f g : M →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_mtop $ hs ▸ eq_on_mclosure h end monoid_hom end non_assoc section assoc variables [monoid M] [monoid N] {s : set M} section is_unit /-- The submonoid consisting of the units of a monoid -/ @[to_additive "The additive submonoid consisting of the additive units of an additive monoid"] def is_unit.submonoid (M : Type*) [monoid M] : submonoid M := { carrier := set_of is_unit, one_mem' := by simp only [is_unit_one, set.mem_set_of_eq], mul_mem' := by { intros a b ha hb, rw set.mem_set_of_eq at *, exact is_unit.mul ha hb } } @[to_additive] lemma is_unit.mem_submonoid_iff {M : Type*} [monoid M] (a : M) : a ∈ is_unit.submonoid M ↔ is_unit a := begin change a ∈ set_of is_unit ↔ is_unit a, rw set.mem_set_of_eq end end is_unit namespace monoid_hom open submonoid /-- Let `s` be a subset of a monoid `M` such that the closure of `s` is the whole monoid. Then `monoid_hom.of_mclosure_eq_top_left` defines a monoid homomorphism from `M` asking for a proof of `f (x * y) = f x * f y` only for `x ∈ s`. -/ @[to_additive "/-- Let `s` be a subset of an additive monoid `M` such that the closure of `s` is the whole monoid. Then `add_monoid_hom.of_mclosure_eq_top_left` defines an additive monoid homomorphism from `M` asking for a proof of `f (x + y) = f x + f y` only for `x ∈ s`. -/"] def of_mclosure_eq_top_left {M N} [monoid M] [monoid N] {s : set M} (f : M → N) (hs : closure s = ⊤) (h1 : f 1 = 1) (hmul : ∀ (x ∈ s) y, f (x * y) = f x * f y) : M →* N := { to_fun := f, map_one' := h1, map_mul' := λ x, dense_induction x hs hmul (λ y, by rw [one_mul, h1, one_mul]) $ λ a b ha hb y, by rw [mul_assoc, ha, ha, hb, mul_assoc] } @[simp, norm_cast, to_additive] lemma coe_of_mclosure_eq_top_left (f : M → N) (hs : closure s = ⊤) (h1 hmul) : ⇑(of_mclosure_eq_top_left f hs h1 hmul) = f := rfl /-- Let `s` be a subset of a monoid `M` such that the closure of `s` is the whole monoid. Then `monoid_hom.of_mclosure_eq_top_right` defines a monoid homomorphism from `M` asking for a proof of `f (x * y) = f x * f y` only for `y ∈ s`. -/ @[to_additive "/-- Let `s` be a subset of an additive monoid `M` such that the closure of `s` is the whole monoid. Then `add_monoid_hom.of_mclosure_eq_top_right` defines an additive monoid homomorphism from `M` asking for a proof of `f (x + y) = f x + f y` only for `y ∈ s`. -/"] def of_mclosure_eq_top_right {M N} [monoid M] [monoid N] {s : set M} (f : M → N) (hs : closure s = ⊤) (h1 : f 1 = 1) (hmul : ∀ x (y ∈ s), f (x * y) = f x * f y) : M →* N := { to_fun := f, map_one' := h1, map_mul' := λ x y, dense_induction y hs (λ y hy x, hmul x y hy) (by simp [h1]) (λ y₁ y₂ h₁ h₂ x, by simp only [← mul_assoc, h₁, h₂]) x } @[simp, norm_cast, to_additive] lemma coe_of_mclosure_eq_top_right (f : M → N) (hs : closure s = ⊤) (h1 hmul) : ⇑(of_mclosure_eq_top_right f hs h1 hmul) = f := rfl end monoid_hom end assoc
333a796b0fb95fe8a294a17f6e81824cd6701ddf
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/data/complex/is_R_or_C.lean
cbabb05343039a56443b16a4c33a4167d08ed23e
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
28,842
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import data.real.sqrt import field_theory.tower import analysis.normed_space.finite_dimension /-! # `is_R_or_C`: a typeclass for ℝ or ℂ This file defines the typeclass `is_R_or_C` intended to have only two instances: ℝ and ℂ. It is meant for definitions and theorems which hold for both the real and the complex case, and in particular when the real case follows directly from the complex case by setting `re` to `id`, `im` to zero and so on. Its API follows closely that of ℂ. Possible applications include defining inner products and Hilbert spaces for both the real and complex case. One would produce the definitions and proof for an arbitrary field of this typeclass, which basically amounts to doing the complex case, and the two cases then fall out immediately from the two instances of the class. The instance for `ℝ` is registered in this file. The instance for `ℂ` is declared in `analysis.complex.basic`. ## Implementation notes The coercion from reals into an `is_R_or_C` field is done by registering `algebra_map ℝ K` as a `has_coe_t`. For this to work, we must proceed carefully to avoid problems involving circular coercions in the case `K=ℝ`; in particular, we cannot use the plain `has_coe` and must set priorities carefully. This problem was already solved for `ℕ`, and we copy the solution detailed in `data/nat/cast`. See also Note [coercion into rings] for more details. In addition, several lemmas need to be set at priority 900 to make sure that they do not override their counterparts in `complex.lean` (which causes linter errors). -/ open_locale big_operators section local notation `𝓚` := algebra_map ℝ _ /-- This typeclass captures properties shared by ℝ and ℂ, with an API that closely matches that of ℂ. -/ class is_R_or_C (K : Type*) extends nondiscrete_normed_field K, normed_algebra ℝ K, complete_space K := (re : K →+ ℝ) (im : K →+ ℝ) (conj : K →+* K) (I : K) -- Meant to be set to 0 for K=ℝ (I_re_ax : re I = 0) (I_mul_I_ax : I = 0 ∨ I * I = -1) (re_add_im_ax : ∀ (z : K), 𝓚 (re z) + 𝓚 (im z) * I = z) (of_real_re_ax : ∀ r : ℝ, re (𝓚 r) = r) (of_real_im_ax : ∀ r : ℝ, im (𝓚 r) = 0) (mul_re_ax : ∀ z w : K, re (z * w) = re z * re w - im z * im w) (mul_im_ax : ∀ z w : K, im (z * w) = re z * im w + im z * re w) (conj_re_ax : ∀ z : K, re (conj z) = re z) (conj_im_ax : ∀ z : K, im (conj z) = -(im z)) (conj_I_ax : conj I = -I) (norm_sq_eq_def_ax : ∀ (z : K), ∥z∥^2 = (re z) * (re z) + (im z) * (im z)) (mul_im_I_ax : ∀ (z : K), (im z) * im I = im z) (inv_def_ax : ∀ (z : K), z⁻¹ = conj z * 𝓚 ((∥z∥^2)⁻¹)) (div_I_ax : ∀ (z : K), z / I = -(z * I)) end namespace is_R_or_C variables {K : Type*} [is_R_or_C K] local postfix `†`:100 := @is_R_or_C.conj K _ /- The priority must be set at 900 to ensure that coercions are tried in the right order. See Note [coercion into rings], or `data/nat/cast.lean` for more details. -/ @[priority 900] noncomputable instance algebra_map_coe : has_coe_t ℝ K := ⟨algebra_map ℝ K⟩ lemma of_real_alg (x : ℝ) : (x : K) = x • (1 : K) := algebra.algebra_map_eq_smul_one x lemma algebra_map_eq_of_real : ⇑(algebra_map ℝ K) = coe := rfl @[simp] lemma re_add_im (z : K) : ((re z) : K) + (im z) * I = z := is_R_or_C.re_add_im_ax z @[simp, norm_cast] lemma of_real_re : ∀ r : ℝ, re (r : K) = r := is_R_or_C.of_real_re_ax @[simp, norm_cast] lemma of_real_im : ∀ r : ℝ, im (r : K) = 0 := is_R_or_C.of_real_im_ax @[simp] lemma mul_re : ∀ z w : K, re (z * w) = re z * re w - im z * im w := is_R_or_C.mul_re_ax @[simp] lemma mul_im : ∀ z w : K, im (z * w) = re z * im w + im z * re w := is_R_or_C.mul_im_ax theorem inv_def (z : K) : z⁻¹ = conj z * ((∥z∥^2)⁻¹:ℝ) := is_R_or_C.inv_def_ax z theorem ext_iff : ∀ {z w : K}, z = w ↔ re z = re w ∧ im z = im w := λ z w, { mp := by { rintro rfl, cc }, mpr := by { rintro ⟨h₁,h₂⟩, rw [←re_add_im z, ←re_add_im w, h₁, h₂] } } theorem ext : ∀ {z w : K}, re z = re w → im z = im w → z = w := by { simp_rw ext_iff, cc } @[simp, norm_cast, priority 900] lemma of_real_zero : ((0 : ℝ) : K) = 0 := by rw [of_real_alg, zero_smul] @[simp] lemma zero_re' : re (0 : K) = (0 : ℝ) := re.map_zero @[simp, norm_cast, priority 900] lemma of_real_one : ((1 : ℝ) : K) = 1 := by rw [of_real_alg, one_smul] @[simp] lemma one_re : re (1 : K) = 1 := by rw [←of_real_one, of_real_re] @[simp] lemma one_im : im (1 : K) = 0 := by rw [←of_real_one, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_inj {z w : ℝ} : (z : K) = (w : K) ↔ z = w := { mp := λ h, by { convert congr_arg re h; simp only [of_real_re] }, mpr := λ h, by rw h } @[simp] lemma bit0_re (z : K) : re (bit0 z) = bit0 (re z) := by simp [bit0] @[simp] lemma bit1_re (z : K) : re (bit1 z) = bit1 (re z) := by simp only [bit1, add_monoid_hom.map_add, bit0_re, add_right_inj, one_re] @[simp] lemma bit0_im (z : K) : im (bit0 z) = bit0 (im z) := by simp [bit0] @[simp] lemma bit1_im (z : K) : im (bit1 z) = bit0 (im z) := by simp only [bit1, add_right_eq_self, add_monoid_hom.map_add, bit0_im, one_im] @[simp, priority 900] theorem of_real_eq_zero {z : ℝ} : (z : K) = 0 ↔ z = 0 := by rw [←of_real_zero]; exact of_real_inj @[simp, norm_cast, priority 900] lemma of_real_add ⦃r s : ℝ⦄ : ((r + s : ℝ) : K) = r + s := by { apply (@is_R_or_C.ext_iff K _ ((r + s : ℝ) : K) (r + s)).mpr, simp } @[simp, norm_cast, priority 900] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : K) = bit0 (r : K) := ext_iff.2 $ by simp [bit0] @[simp, norm_cast, priority 900] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : K) = bit1 (r : K) := ext_iff.2 $ by simp [bit1] /- Note: This can be proven by `norm_num` once K is proven to be of characteristic zero below. -/ lemma two_ne_zero : (2 : K) ≠ 0 := begin intro h, rw [(show (2 : K) = ((2 : ℝ) : K), by norm_num), ←of_real_zero, of_real_inj] at h, linarith, end @[simp, norm_cast, priority 900] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : K) = -r := ext_iff.2 $ by simp @[simp, norm_cast, priority 900] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : K) = r * s := ext_iff.2 $ by simp lemma of_real_mul_re (r : ℝ) (z : K) : re (↑r * z) = r * re z := by simp only [mul_re, of_real_im, zero_mul, of_real_re, sub_zero] lemma smul_re (r : ℝ) (z : K) : re (↑r * z) = r * (re z) := by simp only [of_real_im, zero_mul, of_real_re, sub_zero, mul_re] lemma smul_im (r : ℝ) (z : K) : im (↑r * z) = r * (im z) := by simp only [add_zero, of_real_im, zero_mul, of_real_re, mul_im] lemma smul_re' : ∀ (r : ℝ) (z : K), re (r • z) = r * (re z) := λ r z, by { rw algebra.smul_def, apply smul_re } lemma smul_im' : ∀ (r : ℝ) (z : K), im (r • z) = r * (im z) := λ r z, by { rw algebra.smul_def, apply smul_im } /-- The real part in a `is_R_or_C` field, as a linear map. -/ noncomputable def re_lm : K →ₗ[ℝ] ℝ := { map_smul' := smul_re', .. re } @[simp] lemma re_lm_coe : (re_lm : K → ℝ) = re := rfl /-! ### The imaginary unit, `I` -/ /-- The imaginary unit. -/ @[simp] lemma I_re : re (I : K) = 0 := I_re_ax @[simp] lemma I_im (z : K) : im z * im (I : K) = im z := mul_im_I_ax z @[simp] lemma I_im' (z : K) : im (I : K) * im z = im z := by rw [mul_comm, I_im _] lemma I_mul_re (z : K) : re (I * z) = - im z := by simp only [I_re, zero_sub, I_im', zero_mul, mul_re] lemma I_mul_I : (I : K) = 0 ∨ (I : K) * I = -1 := I_mul_I_ax @[simp] lemma conj_re (z : K) : re (conj z) = re z := is_R_or_C.conj_re_ax z @[simp] lemma conj_im (z : K) : im (conj z) = -(im z) := is_R_or_C.conj_im_ax z @[simp] lemma conj_I : conj (I : K) = -I := is_R_or_C.conj_I_ax @[simp] lemma conj_of_real (r : ℝ) : conj (r : K) = (r : K) := by { rw ext_iff, simp only [of_real_im, conj_im, eq_self_iff_true, conj_re, and_self, neg_zero] } @[simp] lemma conj_bit0 (z : K) : conj (bit0 z) = bit0 (conj z) := by simp [bit0, ext_iff] @[simp] lemma conj_bit1 (z : K) : conj (bit1 z) = bit1 (conj z) := by simp [bit0, ext_iff] @[simp] lemma conj_neg_I : conj (-I) = (I : K) := by simp [ext_iff] @[simp] lemma conj_conj (z : K) : conj (conj z) = z := by simp [ext_iff] lemma conj_involutive : @function.involutive K is_R_or_C.conj := conj_conj lemma conj_bijective : @function.bijective K K is_R_or_C.conj := conj_involutive.bijective lemma conj_inj (z w : K) : conj z = conj w ↔ z = w := conj_bijective.1.eq_iff lemma conj_eq_zero {z : K} : conj z = 0 ↔ z = 0 := ring_hom.map_eq_zero conj lemma eq_conj_iff_real {z : K} : conj z = z ↔ ∃ r : ℝ, z = (r : K) := begin split, { intro h, suffices : im z = 0, { use (re z), rw ← add_zero (coe _), convert (re_add_im z).symm, simp [this] }, contrapose! h, rw ← re_add_im z, simp only [conj_of_real, ring_hom.map_add, ring_hom.map_mul, conj_I_ax], rw [add_left_cancel_iff, ext_iff], simpa [neg_eq_iff_add_eq_zero, add_self_eq_zero] }, { rintros ⟨r, rfl⟩, apply conj_of_real } end variables (K) /-- Conjugation as a ring equivalence. This is used to convert the inner product into a sesquilinear product. -/ def conj_to_ring_equiv : K ≃+* Kᵒᵖ := { to_fun := opposite.op ∘ conj, inv_fun := conj ∘ opposite.unop, left_inv := λ x, by simp only [conj_conj, function.comp_app, opposite.unop_op], right_inv := λ x, by simp only [conj_conj, opposite.op_unop, function.comp_app], map_mul' := λ x y, by simp [mul_comm], map_add' := λ x y, by simp } variables {K} @[simp] lemma ring_equiv_apply {x : K} : (conj_to_ring_equiv K x).unop = x† := rfl lemma eq_conj_iff_re {z : K} : conj z = z ↔ ((re z) : K) = z := eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, λ h, ⟨_, h.symm⟩⟩ /-- The norm squared function. -/ def norm_sq : monoid_with_zero_hom K ℝ := { to_fun := λ z, re z * re z + im z * im z, map_zero' := by simp, map_one' := by simp, map_mul' := λ z w, by { simp, ring } } lemma norm_sq_eq_def {z : K} : ∥z∥^2 = (re z) * (re z) + (im z) * (im z) := norm_sq_eq_def_ax z lemma norm_sq_eq_def' (z : K) : norm_sq z = ∥z∥^2 := by { rw norm_sq_eq_def, refl } @[simp] lemma norm_sq_of_real (r : ℝ) : ∥(r : K)∥^2 = r * r := by simp [norm_sq_eq_def] lemma norm_sq_zero : norm_sq (0 : K) = 0 := norm_sq.map_zero lemma norm_sq_one : norm_sq (1 : K) = 1 := norm_sq.map_one lemma norm_sq_nonneg (z : K) : 0 ≤ norm_sq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) @[simp] lemma norm_sq_eq_zero {z : K} : norm_sq z = 0 ↔ z = 0 := by { rw [norm_sq_eq_def'], simp [pow_two] } @[simp] lemma norm_sq_pos {z : K} : 0 < norm_sq z ↔ z ≠ 0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [norm_sq_nonneg] @[simp] lemma norm_sq_neg (z : K) : norm_sq (-z) = norm_sq z := by simp [norm_sq_eq_def'] @[simp] lemma norm_sq_conj (z : K) : norm_sq (conj z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_mul (z w : K) : norm_sq (z * w) = norm_sq z * norm_sq w := norm_sq.map_mul z w lemma norm_sq_add (z w : K) : norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (re (z * conj w)) := by simp [norm_sq, pow_two]; ring lemma re_sq_le_norm_sq (z : K) : re z * re z ≤ norm_sq z := le_add_of_nonneg_right (mul_self_nonneg _) lemma im_sq_le_norm_sq (z : K) : im z * im z ≤ norm_sq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : K) : z * conj z = ((norm_sq z) : K) := by simp [ext_iff, norm_sq, mul_comm, sub_eq_neg_add, add_comm] theorem add_conj (z : K) : z + conj z = 2 * (re z) := by simp [ext_iff, two_mul] /-- The pseudo-coercion `of_real` as a `ring_hom`. -/ noncomputable def of_real_hom : ℝ →+* K := algebra_map ℝ K /-- The coercion from reals as a `ring_hom`. -/ noncomputable def coe_hom : ℝ →+* K := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩ @[simp, norm_cast, priority 900] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : K) = r - s := ext_iff.2 $ by simp @[simp, norm_cast, priority 900] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : K) = r ^ n := by induction n; simp [*, of_real_mul, pow_succ] theorem sub_conj (z : K) : z - conj z = (2 * im z) * I := by simp [ext_iff, two_mul, sub_eq_add_neg, add_mul, mul_im_I_ax] lemma norm_sq_sub (z w : K) : norm_sq (z - w) = norm_sq z + norm_sq w - 2 * re (z * conj w) := by simp [-mul_re, norm_sq_add, add_comm, add_left_comm, sub_eq_add_neg] lemma sqrt_norm_sq_eq_norm {z : K} : real.sqrt (norm_sq z) = ∥z∥ := begin have h₂ : ∥z∥ = real.sqrt (∥z∥^2) := (real.sqrt_sqr (norm_nonneg z)).symm, rw [h₂], exact congr_arg real.sqrt (norm_sq_eq_def' z) end /-! ### Inversion -/ @[simp] lemma inv_re (z : K) : re (z⁻¹) = re z / norm_sq z := by simp [inv_def, norm_sq_eq_def, norm_sq, division_def] @[simp] lemma inv_im (z : K) : im (z⁻¹) = im (-z) / norm_sq z := by simp [inv_def, norm_sq_eq_def, norm_sq, division_def] @[simp, norm_cast, priority 900] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : K) = r⁻¹ := begin rw ext_iff, by_cases r = 0, { simp [h] }, { simp; field_simp [h, norm_sq] }, end protected lemma inv_zero : (0⁻¹ : K) = 0 := by rw [← of_real_zero, ← of_real_inv, inv_zero] protected theorem mul_inv_cancel {z : K} (h : z ≠ 0) : z * z⁻¹ = 1 := by rw [inv_def, ←mul_assoc, mul_conj, ←of_real_mul, ←norm_sq_eq_def', mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one] lemma div_re (z w : K) : re (z / w) = re z * re w / norm_sq w + im z * im w / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg] lemma div_im (z w : K) : im (z / w) = im z * re w / norm_sq w - re z * im w / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm] @[simp, norm_cast, priority 900] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : K) = r / s := (@is_R_or_C.coe_hom K _).map_div r s lemma div_re_of_real {z : K} {r : ℝ} : re (z / r) = re z / r := begin by_cases h : r = 0, { simp [h, of_real_zero] }, { change r ≠ 0 at h, rw [div_eq_mul_inv, ←of_real_inv, div_eq_mul_inv], simp [norm_sq, div_mul_eq_div_mul_one_div, div_self h] } end @[simp, norm_cast, priority 900] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : K) = r ^ n := (@is_R_or_C.coe_hom K _).map_fpow r n lemma I_mul_I_of_nonzero : (I : K) ≠ 0 → (I : K) * I = -1 := by { have := I_mul_I_ax, tauto } @[simp] lemma div_I (z : K) : z / I = -(z * I) := begin by_cases h : (I : K) = 0, { simp [h] }, { field_simp [mul_assoc, I_mul_I_of_nonzero h] } end @[simp] lemma inv_I : (I : K)⁻¹ = -I := by { by_cases h : (I : K) = 0; field_simp [h] } @[simp] lemma norm_sq_inv (z : K) : norm_sq z⁻¹ = (norm_sq z)⁻¹ := (@norm_sq K _).map_inv' z @[simp] lemma norm_sq_div (z w : K) : norm_sq (z / w) = norm_sq z / norm_sq w := (@norm_sq K _).map_div z w lemma norm_conj {z : K} : ∥conj z∥ = ∥z∥ := by simp only [←sqrt_norm_sq_eq_norm, norm_sq_conj] lemma conj_inv {z : K} : conj (z⁻¹) = (conj z)⁻¹ := by simp only [inv_def, norm_conj, ring_hom.map_mul, conj_of_real] lemma conj_div {z w : K} : conj (z / w) = (conj z) / (conj w) := by rw [div_eq_inv_mul, div_eq_inv_mul, ring_hom.map_mul]; simp only [conj_inv] /-! ### Cast lemmas -/ @[simp, norm_cast, priority 900] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : K) = n := of_real_hom.map_nat_cast n @[simp, norm_cast] lemma nat_cast_re (n : ℕ) : re (n : K) = n := by rw [← of_real_nat_cast, of_real_re] @[simp, norm_cast] lemma nat_cast_im (n : ℕ) : im (n : K) = 0 := by rw [← of_real_nat_cast, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_int_cast (n : ℤ) : ((n : ℝ) : K) = n := of_real_hom.map_int_cast n @[simp, norm_cast] lemma int_cast_re (n : ℤ) : re (n : K) = n := by rw [← of_real_int_cast, of_real_re] @[simp, norm_cast] lemma int_cast_im (n : ℤ) : im (n : K) = 0 := by rw [← of_real_int_cast, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_rat_cast (n : ℚ) : ((n : ℝ) : K) = n := (@is_R_or_C.of_real_hom K _).map_rat_cast n @[simp, norm_cast] lemma rat_cast_re (q : ℚ) : re (q : K) = q := by rw [← of_real_rat_cast, of_real_re] @[simp, norm_cast] lemma rat_cast_im (q : ℚ) : im (q : K) = 0 := by rw [← of_real_rat_cast, of_real_im] /-! ### Characteristic zero -/ -- TODO: I think this can be instance, because it is a `Prop` /-- ℝ and ℂ are both of characteristic zero. Note: This is not registered as an instance to avoid having multiple instances on ℝ and ℂ. -/ lemma char_zero_R_or_C : char_zero K := char_zero_of_inj_zero $ λ n h, by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h theorem re_eq_add_conj (z : K) : ↑(re z) = (z + conj z) / 2 := begin haveI : char_zero K := char_zero_R_or_C, rw [add_conj, mul_div_cancel_left ((re z):K) two_ne_zero'], end theorem im_eq_conj_sub (z : K) : ↑(im z) = I * (conj z - z) / 2 := begin rw [← neg_inj, ← of_real_neg, ← I_mul_re, re_eq_add_conj], simp [mul_add, sub_eq_add_neg, neg_div'] end /-! ### Absolute value -/ /-- The complex absolute value function, defined as the square root of the norm squared. -/ @[pp_nodot] noncomputable def abs (z : K) : ℝ := (norm_sq z).sqrt local notation `abs'` := _root_.abs local notation `absK` := @abs K _ @[simp, norm_cast] lemma abs_of_real (r : ℝ) : absK r = abs' r := by simp [abs, norm_sq, norm_sq_of_real, real.sqrt_mul_self_eq_abs] lemma norm_eq_abs (z : K) : ∥z∥ = absK z := by simp [abs, norm_sq_eq_def'] lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : absK r = r := (abs_of_real _).trans (abs_of_nonneg h) lemma abs_of_nat (n : ℕ) : absK n = n := by { rw [← of_real_nat_cast], exact abs_of_nonneg (nat.cast_nonneg n) } lemma mul_self_abs (z : K) : abs z * abs z = norm_sq z := real.mul_self_sqrt (norm_sq_nonneg _) @[simp] lemma abs_zero : absK 0 = 0 := by simp [abs] @[simp] lemma abs_one : absK 1 = 1 := by simp [abs] @[simp] lemma abs_two : absK 2 = 2 := calc absK 2 = absK (2 : ℝ) : by rw [of_real_bit0, of_real_one] ... = (2 : ℝ) : abs_of_nonneg (by norm_num) lemma abs_nonneg (z : K) : 0 ≤ absK z := real.sqrt_nonneg _ @[simp] lemma abs_eq_zero {z : K} : absK z = 0 ↔ z = 0 := (real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero lemma abs_ne_zero {z : K} : abs z ≠ 0 ↔ z ≠ 0 := not_congr abs_eq_zero @[simp] lemma abs_conj (z : K) : abs (conj z) = abs z := by simp [abs] @[simp] lemma abs_mul (z w : K) : abs (z * w) = abs z * abs w := by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl lemma abs_re_le_abs (z : K) : abs' (re z) ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (re z)) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply re_sq_le_norm_sq lemma abs_im_le_abs (z : K) : abs' (im z) ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (im z)) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply im_sq_le_norm_sq lemma re_le_abs (z : K) : re z ≤ abs z := (abs_le.1 (abs_re_le_abs _)).2 lemma im_le_abs (z : K) : im z ≤ abs z := (abs_le.1 (abs_im_le_abs _)).2 lemma im_eq_zero_of_le {a : K} (h : abs a ≤ re a) : im a = 0 := begin rw ← zero_eq_mul_self, have : re a * re a = re a * re a + im a * im a, { convert is_R_or_C.mul_self_abs a; linarith [re_le_abs a] }, linarith end lemma re_eq_self_of_le {a : K} (h : abs a ≤ re a) : (re a : K) = a := by { rw ← re_add_im a, simp [im_eq_zero_of_le h] } lemma abs_add (z w : K) : abs (z + w) ≤ abs z + abs w := (mul_self_le_mul_self_iff (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $ begin rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs, add_right_comm, norm_sq_add, add_le_add_iff_left, mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)], simpa [-mul_re] using re_le_abs (z * conj w) end instance : is_absolute_value absK := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value @[simp] lemma abs_abs (z : K) : abs' (abs z) = abs z := _root_.abs_of_nonneg (abs_nonneg _) @[simp] lemma abs_pos {z : K} : 0 < abs z ↔ z ≠ 0 := abv_pos abs @[simp] lemma abs_neg : ∀ z : K, abs (-z) = abs z := abv_neg abs lemma abs_sub : ∀ z w : K, abs (z - w) = abs (w - z) := abv_sub abs lemma abs_sub_le : ∀ a b c : K, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs @[simp] theorem abs_inv : ∀ z : K, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs @[simp] theorem abs_div : ∀ z w : K, abs (z / w) = abs z / abs w := abv_div abs lemma abs_abs_sub_le_abs_sub : ∀ z w : K, abs' (abs z - abs w) ≤ abs (z - w) := abs_abv_sub_le_abv_sub abs lemma abs_re_div_abs_le_one (z : K) : abs' (re z / abs z) ≤ 1 := begin by_cases hz : z = 0, { simp [hz, zero_le_one] }, { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] } end lemma abs_im_div_abs_le_one (z : K) : abs' (im z / abs z) ≤ 1 := begin by_cases hz : z = 0, { simp [hz, zero_le_one] }, { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] } end @[simp, norm_cast] lemma abs_cast_nat (n : ℕ) : abs (n : K) = n := by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)] lemma norm_sq_eq_abs (x : K) : norm_sq x = abs x ^ 2 := by rw [abs, pow_two, real.mul_self_sqrt (norm_sq_nonneg _)] lemma re_eq_abs_of_mul_conj (x : K) : re (x * (conj x)) = abs (x * (conj x)) := by rw [mul_conj, of_real_re, abs_of_real, norm_sq_eq_abs, pow_two, _root_.abs_mul, abs_abs] lemma abs_sqr_re_add_conj (x : K) : (abs (x + x†))^2 = (re (x + x†))^2 := by simp [pow_two, ←norm_sq_eq_abs, norm_sq] lemma abs_sqr_re_add_conj' (x : K) : (abs (x† + x))^2 = (re (x† + x))^2 := by simp [pow_two, ←norm_sq_eq_abs, norm_sq] lemma conj_mul_eq_norm_sq_left (x : K) : x† * x = ((norm_sq x) : K) := begin rw ext_iff, refine ⟨by simp [of_real_re, mul_re, conj_re, conj_im, norm_sq],_⟩, simp [of_real_im, mul_im, conj_im, conj_re, mul_comm], end /-- The real part in a `is_R_or_C` field, as a continuous linear map. -/ noncomputable def re_clm : K →L[ℝ] ℝ := re_lm.mk_continuous 1 $ by { simp only [norm_eq_abs, re_lm_coe, one_mul], exact abs_re_le_abs } @[simp] lemma norm_re_clm : ∥(re_clm : K →L[ℝ] ℝ)∥ = 1 := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _), convert continuous_linear_map.ratio_le_op_norm _ (1 : K), simp, end @[simp, norm_cast] lemma re_clm_coe : ((re_clm : K →L[ℝ] ℝ) : K →ₗ[ℝ] ℝ) = re_lm := rfl @[simp] lemma re_clm_apply : ((re_clm : K →L[ℝ] ℝ) : K → ℝ) = re := rfl /-! ### Cauchy sequences -/ theorem is_cau_seq_re (f : cau_seq K abs) : is_cau_seq abs' (λ n, re (f n)) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij) theorem is_cau_seq_im (f : cau_seq K abs) : is_cau_seq abs' (λ n, im (f n)) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij) /-- The real part of a K Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_re (f : cau_seq K abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_re f⟩ /-- The imaginary part of a K Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_im (f : cau_seq K abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_im f⟩ lemma is_cau_seq_abs {f : ℕ → K} (hf : is_cau_seq abs f) : is_cau_seq abs' (abs ∘ f) := λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩ @[simp, norm_cast, priority 900] lemma of_real_prod {α : Type*} (s : finset α) (f : α → ℝ) : ((∏ i in s, f i : ℝ) : K) = ∏ i in s, (f i : K) := ring_hom.map_prod _ _ _ @[simp, norm_cast, priority 900] lemma of_real_sum {α : Type*} (s : finset α) (f : α → ℝ) : ((∑ i in s, f i : ℝ) : K) = ∑ i in s, (f i : K) := ring_hom.map_sum _ _ _ @[simp, norm_cast] lemma of_real_finsupp_sum {α M : Type*} [has_zero M] (f : α →₀ M) (g : α → M → ℝ) : ((f.sum (λ a b, g a b) : ℝ) : K) = f.sum (λ a b, ((g a b) : K)) := ring_hom.map_finsupp_sum _ f g @[simp, norm_cast] lemma of_real_finsupp_prod {α M : Type*} [has_zero M] (f : α →₀ M) (g : α → M → ℝ) : ((f.prod (λ a b, g a b) : ℝ) : K) = f.prod (λ a b, ((g a b) : K)) := ring_hom.map_finsupp_prod _ f g end is_R_or_C namespace finite_dimensional variables {K : Type*} [is_R_or_C K] open_locale classical open is_R_or_C /-- This instance generates a type-class problem with a metavariable `?m` that should satisfy `is_R_or_C ?m`. Since this can only be satisfied by `ℝ` or `ℂ`, this does not cause problems. -/ library_note "is_R_or_C instance" /-- An `is_R_or_C` field is finite-dimensional over `ℝ`, since it is spanned by `{1, I}`. -/ @[nolint dangerous_instance] instance is_R_or_C_to_real : finite_dimensional ℝ K := finite_dimensional.iff_fg.mpr ⟨{1, I}, begin rw eq_top_iff, intros a _, rw [finset.coe_insert, finset.coe_singleton, submodule.mem_span_insert], refine ⟨re a, (im a) • I, _, _⟩, { rw submodule.mem_span_singleton, use im a }, simp [re_add_im a, algebra.smul_def, algebra_map_eq_of_real] end⟩ /-- Over an `is_R_or_C` field, we can register the properness of finite-dimensional normed spaces as an instance. -/ @[priority 900, nolint dangerous_instance] instance proper_is_R_or_C -- note [is_R_or_C instance] {E : Type*} [normed_group E] [normed_space K E] [finite_dimensional K E] : proper_space E := begin letI : normed_space ℝ E := restrict_scalars.normed_space ℝ K E, letI : is_scalar_tower ℝ K E := restrict_scalars.is_scalar_tower _ _ _, letI : finite_dimensional ℝ E := finite_dimensional.trans ℝ K E, apply_instance end end finite_dimensional section instances noncomputable instance real.is_R_or_C : is_R_or_C ℝ := { re := add_monoid_hom.id ℝ, im := 0, conj := ring_hom.id ℝ, I := 0, I_re_ax := by simp only [add_monoid_hom.map_zero], I_mul_I_ax := or.intro_left _ rfl, re_add_im_ax := λ z, by unfold_coes; simp [add_zero, id.def, mul_zero], of_real_re_ax := λ r, by simp only [add_monoid_hom.id_apply, algebra.id.map_eq_self], of_real_im_ax := λ r, by simp only [add_monoid_hom.zero_apply], mul_re_ax := λ z w, by simp only [sub_zero, mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply], mul_im_ax := λ z w, by simp only [add_zero, zero_mul, mul_zero, add_monoid_hom.zero_apply], conj_re_ax := λ z, by simp only [ring_hom.id_apply], conj_im_ax := λ z, by simp only [neg_zero, add_monoid_hom.zero_apply], conj_I_ax := by simp only [ring_hom.map_zero, neg_zero], norm_sq_eq_def_ax := λ z, by simp only [pow_two, norm, ←abs_mul, abs_mul_self z, add_zero, mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply], mul_im_I_ax := λ z, by simp only [mul_zero, add_monoid_hom.zero_apply], inv_def_ax := λ z, by simp [pow_two, real.norm_eq_abs, abs_mul_abs_self, ← div_eq_mul_inv], div_I_ax := λ z, by simp only [div_zero, mul_zero, neg_zero]} end instances namespace is_R_or_C section cleanup_lemmas local notation `reR` := @is_R_or_C.re ℝ _ local notation `imR` := @is_R_or_C.im ℝ _ local notation `conjR` := @is_R_or_C.conj ℝ _ local notation `IR` := @is_R_or_C.I ℝ _ local notation `absR` := @is_R_or_C.abs ℝ _ local notation `norm_sqR` := @is_R_or_C.norm_sq ℝ _ @[simp] lemma re_to_real {x : ℝ} : reR x = x := rfl @[simp] lemma im_to_real {x : ℝ} : imR x = 0 := rfl @[simp] lemma conj_to_real {x : ℝ} : conjR x = x := rfl @[simp] lemma I_to_real : IR = 0 := rfl @[simp] lemma norm_sq_to_real {x : ℝ} : norm_sqR x = x*x := by simp [is_R_or_C.norm_sq] @[simp] lemma abs_to_real {x : ℝ} : absR x = _root_.abs x := by simp [is_R_or_C.abs, abs, real.sqrt_mul_self_eq_abs] @[simp] lemma coe_real_eq_id : @coe ℝ ℝ _ = id := rfl end cleanup_lemmas end is_R_or_C section normalization variables {K : Type*} [is_R_or_C K] variables {E : Type*} [normed_group E] [normed_space K E] open is_R_or_C /- Note: one might think the following lemma belongs in `analysis.normed_space.basic`. But it can't be placed there, because that file is an import of `data.complex.is_R_or_C`! -/ /-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to unit length. -/ @[simp] lemma norm_smul_inv_norm {x : E} (hx : x ≠ 0) : ∥(∥x∥⁻¹ : K) • x∥ = 1 := begin have h : ∥(∥x∥ : K)∥ = ∥x∥, { rw norm_eq_abs, exact abs_of_nonneg (norm_nonneg _) }, have : ∥x∥ ≠ 0 := by simp [hx], field_simp [norm_smul, h] end end normalization
596976dc55aca05070728af2db8ca73d1efebb39
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/group_action/group.lean
02971812f57845f0427542d10694697d01baba87
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
11,659
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.hom.aut import group_theory.group_action.units /-! # Group actions applied to various types of group > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ open function universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action /-- `monoid.to_mul_action` is faithful on cancellative monoids. -/ @[to_additive /-" `add_monoid.to_add_action` is faithful on additive cancellative monoids. "-/] instance right_cancel_monoid.to_has_faithful_smul [right_cancel_monoid α] : has_faithful_smul α α := ⟨λ x y h, mul_right_cancel (h 1)⟩ section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive, simps] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm /-- `mul_action.to_perm` is injective on faithful actions. -/ @[to_additive "`add_action.to_perm` is injective on faithful actions."] lemma mul_action.to_perm_injective [has_faithful_smul α β] : function.injective (mul_action.to_perm : α → equiv.perm β) := (show function.injective (equiv.to_fun ∘ mul_action.to_perm), from smul_left_injective').of_comp variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } /-- The tautological action by `equiv.perm α` on `α`. This generalizes `function.End.apply_mul_action`.-/ instance equiv.perm.apply_mul_action (α : Type*) : mul_action (equiv.perm α) α := { smul := λ f a, f a, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma equiv.perm.smul_def {α : Type*} (f : equiv.perm α) (a : α) : f • a = f a := rfl /-- `equiv.perm.apply_mul_action` is faithful. -/ instance equiv.perm.apply_has_faithful_smul (α : Type*) : has_faithful_smul (equiv.perm α) α := ⟨λ x y, equiv.ext⟩ variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] lemma smul_zpow [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) (p : ℤ) : (c • x) ^ p = c ^ p • x ^ p := by { cases p; simp [smul_pow, smul_inv] } @[simp] lemma commute.smul_right_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute a (r • b) ↔ commute a b := ⟨λ h, inv_smul_smul r b ▸ h.smul_right r⁻¹, λ h, h.smul_right r⟩ @[simp] lemma commute.smul_left_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute (r • a) b ↔ commute a b := by rw [commute.symm_iff, commute.smul_right_iff, commute.symm_iff] @[to_additive] protected lemma mul_action.bijective (g : α) : bijective ((•) g : β → β) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : injective ((•) g : β → β) := (mul_action.bijective g).injective @[to_additive] protected lemma mul_action.surjective (g : α) : surjective ((•) g : β → β) := (mul_action.bijective g).surjective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group /-- `monoid.to_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_smul [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_smul α α := ⟨λ x y h, mul_left_injective₀ one_ne_zero (h 1)⟩ section gwz variables [group_with_zero α] [mul_action α β] {a : α} @[simp] lemma inv_smul_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply @[simp] lemma commute.smul_right_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute a (c • b) ↔ commute a b := commute.smul_right_iff (units.mk0 c hc) @[simp] lemma commute.smul_left_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute (c • a) b ↔ commute a b := commute.smul_left_iff (units.mk0 c hc) protected lemma mul_action.bijective₀ (ha : a ≠ 0) : bijective ((•) a : β → β) := mul_action.bijective $ units.mk0 a ha protected lemma mul_action.injective₀ (ha : a ≠ 0) : injective ((•) a : β → β) := (mul_action.bijective₀ ha).injective protected lemma mul_action.surjective₀ (ha : a ≠ 0) : surjective ((•) a : β → β) := (mul_action.bijective₀ ha).surjective end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] variables (β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def distrib_mul_action.to_add_equiv (x : α) : β ≃+ β := { .. distrib_mul_action.to_add_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def distrib_mul_action.to_add_aut : α →* add_aut β := { to_fun := distrib_mul_action.to_add_equiv β, map_one' := add_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, add_equiv.ext (mul_smul _ _) } variables {α β} theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := show units.mk0 a ha • x = 0 ↔ x = 0, from smul_eq_zero_iff_eq _ theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := show units.mk0 a ha • x ≠ 0 ↔ x ≠ 0, from smul_ne_zero_iff_ne _ end gwz end distrib_mul_action section mul_distrib_mul_action variables [group α] [monoid β] [mul_distrib_mul_action α β] variables (β) /-- Each element of the group defines a multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def mul_distrib_mul_action.to_mul_equiv (x : α) : β ≃* β := { .. mul_distrib_mul_action.to_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def mul_distrib_mul_action.to_mul_aut : α →* mul_aut β := { to_fun := mul_distrib_mul_action.to_mul_equiv β, map_one' := mul_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, mul_equiv.ext (mul_smul _ _) } variables {α β} end mul_distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[to_additive arrow_add_action "If `G` acts on `A`, then it acts also on `A → B`, by `(g +ᵥ F) a = F (g⁻¹ +ᵥ a)`", simps] def arrow_action {G A B : Type*} [division_monoid G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [inv_one, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- When `B` is a monoid, `arrow_action` is additionally a `mul_distrib_mul_action`. -/ def arrow_mul_distrib_mul_action {G A B : Type*} [group G] [mul_action G A] [monoid B] : mul_distrib_mul_action G (A → B) := { smul_one := λ g, rfl, smul_mul := λ g f₁ f₂, rfl } local attribute [instance] arrow_mul_distrib_mul_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [monoid H] : G →* mul_aut (A → H) := mul_distrib_mul_action.to_mul_aut _ _ end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ show u • x = 0 ↔ x = 0, from smul_eq_zero_iff_eq u end distrib_mul_action end is_unit section smul variables [group α] [monoid β] @[simp] lemma is_unit_smul_iff [mul_action α β] [smul_comm_class α β β] [is_scalar_tower α β β] (g : α) (m : β) : is_unit (g • m) ↔ is_unit m := ⟨λ h, inv_smul_smul g m ▸ h.smul g⁻¹, is_unit.smul g⟩ lemma is_unit.smul_sub_iff_sub_inv_smul [add_group β] [distrib_mul_action α β] [is_scalar_tower α β β] [smul_comm_class α β β] (r : α) (a : β) : is_unit (r • 1 - a) ↔ is_unit (1 - r⁻¹ • a) := by rw [←is_unit_smul_iff r (1 - r⁻¹ • a), smul_sub, smul_inv_smul] end smul
71846aba3615a0c6e94220328fe43b73e3c66ad4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/W/cardinal.lean
2d2db0f54efc2212588f24e58211eefffd07cb6c
[ "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
2,655
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.W.basic import set_theory.cardinal.ordinal /-! # Cardinality of W-types This file proves some theorems about the cardinality of W-types. The main result is `cardinal_mk_le_max_aleph_0_of_fintype` which says that if for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `ℵ₀`. This can be used to prove theorems about the cardinality of algebraic constructions such as polynomials. There is a surjection from a `W_type` to `mv_polynomial` for example, and this surjection can be used to put an upper bound on the cardinality of `mv_polynomial`. ## Tags W, W type, cardinal, first order -/ universe u variables {α : Type u} {β : α → Type u} noncomputable theory namespace W_type open_locale cardinal open cardinal lemma cardinal_mk_eq_sum : #(W_type β) = sum (λ a : α, #(W_type β) ^ #(β a)) := begin simp only [cardinal.power_def, ← cardinal.mk_sigma], exact mk_congr (equiv_sigma β) end /-- `#(W_type β)` is the least cardinal `κ` such that `sum (λ a : α, κ ^ #(β a)) ≤ κ` -/ lemma cardinal_mk_le_of_le {κ : cardinal.{u}} (hκ : sum (λ a : α, κ ^ #(β a)) ≤ κ) : #(W_type β) ≤ κ := begin induction κ using cardinal.induction_on with γ, simp only [cardinal.power_def, ← cardinal.mk_sigma, cardinal.le_def] at hκ, cases hκ, exact cardinal.mk_le_of_injective (elim_injective _ hκ.1 hκ.2) end /-- If, for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `ℵ₀` -/ lemma cardinal_mk_le_max_aleph_0_of_finite [∀ a, finite (β a)] : #(W_type β) ≤ max (#α) ℵ₀ := (is_empty_or_nonempty α).elim (begin introI h, rw [cardinal.mk_eq_zero (W_type β)], exact zero_le _ end) $ λ hn, let m := max (#α) ℵ₀ in cardinal_mk_le_of_le $ calc cardinal.sum (λ a, m ^ #(β a)) ≤ #α * ⨆ a, m ^ #(β a) : cardinal.sum_le_supr _ ... ≤ m * ⨆ a, m ^ #(β a) : mul_le_mul' (le_max_left _ _) le_rfl ... = m : mul_eq_left.{u} (le_max_right _ _) (csupr_le' $ λ i, pow_le (le_max_right _ _) (lt_aleph_0_of_finite _)) $ pos_iff_ne_zero.1 $ order.succ_le_iff.1 begin rw succ_zero, obtain ⟨a⟩ : nonempty α, from hn, refine le_trans _ (le_csupr (bdd_above_range.{u u} _) a), rw ←power_zero, exact power_le_power_left (pos_iff_ne_zero.1 (aleph_0_pos.trans_le (le_max_right _ _))) (zero_le _) end end W_type
0197eb150234394214f9b83222605114e76bf40d
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/module/pi.lean
da7bf5be5c6715cd4a4df2cc50e0eaeaa3a029a9
[ "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,456
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.module.basic import algebra.ring.pi /-! # Pi instances for module and multiplicative actions This file defines instances for module, mul_action and related structures on Pi Types -/ namespace pi universes u v w 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) instance has_scalar {α : Type*} [Π i, has_scalar α $ f i] : has_scalar α (Π i : I, f i) := ⟨λ s x, λ i, s • (x i)⟩ @[simp] lemma smul_apply {α : Type*} [Π i, has_scalar α $ f i] (s : α) : (s • x) i = s • x i := rfl instance has_scalar' {g : I → Type*} [Π i, has_scalar (f i) (g i)] : has_scalar (Π i, f i) (Π i : I, g i) := ⟨λ s x, λ i, (s i) • (x i)⟩ @[simp] lemma smul_apply' {g : I → Type*} [∀ i, has_scalar (f i) (g i)] (s : Π i, f i) (x : Π i, g i) : (s • x) i = s i • x i := rfl instance is_scalar_tower {α β : Type*} [has_scalar α β] [Π i, has_scalar β $ f i] [Π i, has_scalar α $ f i] [Π i, is_scalar_tower α β (f i)] : is_scalar_tower α β (Π i : I, f i) := ⟨λ x y z, funext $ λ i, smul_assoc x y (z i)⟩ instance is_scalar_tower' {g : I → Type*} {α : Type*} [Π i, has_scalar α $ f i] [Π i, has_scalar (f i) (g i)] [Π i, has_scalar α $ g i] [Π i, is_scalar_tower α (f i) (g i)] : is_scalar_tower α (Π i : I, f i) (Π i : I, g i) := ⟨λ x y z, funext $ λ i, smul_assoc x (y i) (z i)⟩ instance is_scalar_tower'' {g : I → Type*} {h : I → Type*} [Π i, has_scalar (f i) (g i)] [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)] [Π i, is_scalar_tower (f i) (g i) (h i)] : is_scalar_tower (Π i, f i) (Π i, g i) (Π i, h i) := ⟨λ x y z, funext $ λ i, smul_assoc (x i) (y i) (z i)⟩ instance smul_comm_class {α β : Type*} [Π i, has_scalar α $ f i] [Π i, has_scalar β $ f i] [∀ i, smul_comm_class α β (f i)] : smul_comm_class α β (Π i : I, f i) := ⟨λ x y z, funext $ λ i, smul_comm x y (z i)⟩ instance smul_comm_class' {g : I → Type*} {α : Type*} [Π i, has_scalar α $ g i] [Π i, has_scalar (f i) (g i)] [∀ i, smul_comm_class α (f i) (g i)] : smul_comm_class α (Π i : I, f i) (Π i : I, g i) := ⟨λ x y z, funext $ λ i, smul_comm x (y i) (z i)⟩ instance smul_comm_class'' {g : I → Type*} {h : I → Type*} [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)] [∀ i, smul_comm_class (f i) (g i) (h i)] : smul_comm_class (Π i, f i) (Π i, g i) (Π i, h i) := ⟨λ x y z, funext $ λ i, smul_comm (x i) (y i) (z i)⟩ instance mul_action (α) {m : monoid α} [Π i, mul_action α $ f i] : @mul_action α (Π i : I, f i) m := { smul := (•), mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _, one_smul := λ f, funext $ λ i, one_smul α _ } instance mul_action' {g : I → Type*} {m : Π i, monoid (f i)} [Π i, mul_action (f i) (g i)] : @mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) := { smul := (•), mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _, one_smul := λ f, funext $ λ i, one_smul _ _ } instance distrib_mul_action (α) {m : monoid α} {n : ∀ i, add_monoid $ f i} [∀ i, distrib_mul_action α $ f i] : @distrib_mul_action α (Π i : I, f i) m (@pi.add_monoid I f n) := { smul_zero := λ c, funext $ λ i, smul_zero _, smul_add := λ c f g, funext $ λ i, smul_add _ _ _, ..pi.mul_action _ } instance distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, add_monoid $ g i} [Π i, distrib_mul_action (f i) (g i)] : @distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.add_monoid I g n) := { smul_add := by { intros, ext x, apply smul_add }, smul_zero := by { intros, ext x, apply smul_zero } } lemma single_smul {α} [monoid α] [Π i, add_monoid $ f i] [Π i, distrib_mul_action α $ f i] [decidable_eq I] (i : I) (r : α) (x : f i) : single i (r • x) = r • single i x := single_op (λ i : I, ((•) r : f i → f i)) (λ j, smul_zero _) _ _ lemma single_smul' {g : I → Type*} [Π i, monoid_with_zero (f i)] [Π i, add_monoid (g i)] [Π i, distrib_mul_action (f i) (g i)] [decidable_eq I] (i : I) (r : f i) (x : g i) : single i (r • x) = single i r • single i x := single_op₂ (λ i : I, ((•) : f i → g i → g i)) (λ j, smul_zero _) _ _ _ variables (I f) instance semimodule (α) {r : semiring α} {m : ∀ i, add_comm_monoid $ f i} [∀ i, semimodule α $ f i] : @semimodule α (Π i : I, f i) r (@pi.add_comm_monoid I f m) := { add_smul := λ c f g, funext $ λ i, add_smul _ _ _, zero_smul := λ f, funext $ λ i, zero_smul α _, ..pi.distrib_mul_action _ } variables {I f} instance semimodule' {g : I → Type*} {r : Π i, semiring (f i)} {m : Π i, add_comm_monoid (g i)} [Π i, semimodule (f i) (g i)] : semimodule (Π i, f i) (Π i, g i) := { add_smul := by { intros, ext1, apply add_smul }, zero_smul := by { intros, ext1, apply zero_smul } } instance (α) {r : semiring α} {m : Π i, add_comm_monoid $ f i} [Π i, semimodule α $ f i] [∀ i, no_zero_smul_divisors α $ f i] : no_zero_smul_divisors α (Π i : I, f i) := ⟨λ c x h, or_iff_not_imp_left.mpr (λ hc, funext (λ i, (smul_eq_zero.mp (congr_fun h i)).resolve_left hc))⟩ end pi
c53b0a7bcec9b3f533d577cb157edae97f4896ae
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/list/range.lean
db50721f434e165d9a20a050478d3d8cb27a5d46
[ "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
12,286
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 import data.list.zip /-! # Ranges of naturals as lists This file shows basic results about `list.iota`, `list.range`, `list.range'` (all defined in `data.list.defs`) and defines `list.fin_range`. `fin_range n` is the list of elements of `fin n`. `iota n = [n, n - 1, ..., 1]` and `range n = [0, ..., n - 1]` are basic list constructions used for tactics. `range' a b = [a, ..., a + b - 1]` is there to help prove properties about them. Actual maths should use `list.Ico` instead. -/ universe u open nat namespace list 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 range'_eq_nil {s n : ℕ} : range' s n = [] ↔ n = 0 := by rw [← length_eq_zero, 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 (nat.le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@decidable.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.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 h.length_le, λ h, by rw [← tsub_add_cancel_of_le 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 ⟨nat.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 @[simp] lemma nth_le_range' {n m} (i) (H : i < (range' n m).length) : nth_le (range' n m) i H = n + i := option.some.inj $ by rw [←nth_le_nth _, nth_range' _ (by simpa using H)] 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'] @[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_range] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp only [range_eq_range', pairwise_lt_range'] theorem pairwise_le_range (n : ℕ) : pairwise (≤) (range n) := pairwise.imp (@le_of_lt ℕ _) (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_succ (n : ℕ) : range (succ n) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] @[simp] lemma range_zero : range 0 = [] := rfl theorem chain'_range_succ (r : ℕ → ℕ → Prop) (n : ℕ) : chain' r (range n.succ) ↔ ∀ m < n, r m m.succ := begin rw range_succ, induction n with n hn, { simp }, { rw range_succ, simp only [append_assoc, singleton_append, chain'_append_cons_cons, chain'_singleton, and_true], rw [hn, forall_lt_succ] } end theorem chain_range_succ (r : ℕ → ℕ → Prop) (n a : ℕ) : chain r a (range n.succ) ↔ r a 0 ∧ ∀ m < n, r m m.succ := begin rw [range_succ_eq_map, chain_cons, and.congr_right_iff, ←chain'_range_succ, range_succ_eq_map], exact λ _, iff.rfl end lemma range_add (a : ℕ) : ∀ b, range (a + b) = range a ++ (range b).map (λ x, a + x) | 0 := by rw [add_zero, range_zero, map_nil, append_nil] | (b + 1) := by rw [nat.add_succ, range_succ, range_add b, range_succ, map_append, map_singleton, append_assoc] 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, tsub_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`. The corresponding finset is `finset.univ`. -/ def fin_range (n : ℕ) : list (fin n) := (range n).pmap fin.mk (λ _, list.mem_range.1) @[simp] lemma fin_range_zero : fin_range 0 = [] := rfl @[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_range _).pmap $ λ _ _ _ _, fin.veq_of_eq @[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n := by rw [fin_range, length_pmap, length_range] @[simp] lemma fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_fin_range] @[simp] lemma map_coe_fin_range (n : ℕ) : (fin_range n).map coe = list.range n := begin simp_rw [fin_range, map_pmap, fin.coe_mk, pmap_eq_map], exact list.map_id _ end lemma fin_range_succ_eq_map (n : ℕ) : fin_range n.succ = 0 :: (fin_range n).map fin.succ := begin apply map_injective_iff.mpr fin.coe_injective, rw [map_cons, map_coe_fin_range, range_succ_eq_map, fin.coe_zero, ←map_coe_fin_range, map_map, map_map, function.comp, function.comp], congr' 2 with x, exact (fin.coe_succ _).symm, end @[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_succ, 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'] lemma enum_eq_zip_range (l : list α) : l.enum = (range l.length).zip l := zip_of_prod (enum_map_fst _) (enum_map_snd _) @[simp] lemma unzip_enum_eq_prod (l : list α) : l.enum.unzip = (range l.length, l) := by simp only [enum_eq_zip_range, unzip_zip, length_range] lemma enum_from_eq_zip_range' (l : list α) {n : ℕ} : l.enum_from n = (range' n l.length).zip l := zip_of_prod (enum_from_map_fst _ _) (enum_from_map_snd _ _) @[simp] lemma unzip_enum_from_eq_prod (l : list α) {n : ℕ} : (l.enum_from n).unzip = (range' n l.length, l) := by simp only [enum_from_eq_zip_range', unzip_zip, length_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)] @[simp] lemma nth_le_fin_range {n : ℕ} {i : ℕ} (h) : (fin_range n).nth_le i h = ⟨i, length_fin_range n ▸ h⟩ := by simp only [fin_range, nth_le_range, nth_le_pmap] @[simp] lemma map_nth_le (l : list α) : (fin_range l.length).map (λ n, l.nth_le n n.2) = l := ext_le (by rw [length_map, length_fin_range]) $ λ n _ h, by { rw ← nth_le_map_rev, congr, { rw nth_le_fin_range, refl }, { rw length_fin_range, exact 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⟩, -subtype.val_eq_coe] }) theorem of_fn_id (n) : of_fn id = fin_range n := of_fn_eq_pmap theorem of_fn_eq_map {α n} {f : fin n → α} : of_fn f = (fin_range n).map f := by rw [← of_fn_id, map_of_fn, function.right_id] theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := by { rw of_fn_eq_pmap, exact (nodup_range n).pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) } end list
f00634488f69a791ca03a1e069fca40d2b1c4f29
7cdf3413c097e5d36492d12cdd07030eb991d394
/world_experiments/world7/level15.lean
b79e46240d4fc04daf559a9d704a8605aff0aab1
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
527
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level14 -- hide namespace mynat -- hide /- # World 2 -- Addition World ## Level 15 -- `eq_zero_of_add_right_eq_self` We have * `succ_eq_add_one (n : mynat) : succ n = n + 1` but sometimes the other way is also convenient. -/ /- Theorem For any natural number $d$, we have $$ d+1 = \operatorname{succ}(d). $$ -/ theorem add_one_eq_succ (d : mynat) : d + 1 = succ d := begin [less_leaky] rw succ_eq_add_one, refl, end end mynat -- hide
441e7085f0c82bb3451ac175d3dcd0f47c0e1205
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/matrix/basic.lean
d44e796938c247586708f06a4a6a93b35403661e
[ "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
80,215
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, Lu-Ming Zhang -/ import algebra.algebra.basic import algebra.big_operators.pi import algebra.big_operators.ring import algebra.module.linear_map import algebra.module.pi import algebra.ring.equiv import algebra.star.module import algebra.star.pi import data.fintype.card /-! # Matrices This file defines basic properties of matrices. Matrices with rows indexed by `m`, columns indexed by `n`, and entries of type `α` are represented with `matrix m n α`. For the typical approach of counting rows and columns, `matrix (fin m) (fin n) α` can be used. ## Notation The locale `matrix` gives the following notation: * `⬝ᵥ` for `matrix.dot_product` * `⬝` for `matrix.mul` * `ᵀ` for `matrix.transpose` * `ᴴ` for `matrix.conj_transpose` ## Implementation notes For convenience, `matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the form `λ i j, _` or even `(λ i j, _ : matrix m n α)`, as these are not recognized by lean as having the right type. Instead, `matrix.of` should be used. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ universes u u' v w open_locale big_operators /-- `matrix m n R` is the type of matrices with entries in `R`, whose rows are indexed by `m` and whose columns are indexed by `n`. -/ def matrix (m : Type u) (n : Type u') (α : Type v) : Type (max u u' v) := m → n → α variables {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variables {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace matrix 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 /-- Cast a function into a matrix. The two sides of the equivalence are definitionally equal types. We want to use an explicit cast to distinguish the types because `matrix` has different instances to pi types (such as `pi.has_mul`, which performs elementwise multiplication, vs `matrix.has_mul`). If you are defining a matrix, in terms of its entries, either use `of (λ i j, _)`, or use pattern matching in a definition as `| i j := _` (which can only be unfolded when fully-applied). The purpose of this approach is to ensure that terms of the form `(λ i j, _) * (λ i j, _)` do not appear, as the type of `*` can be misleading. -/ def of : (m → n → α) ≃ matrix m n α := equiv.refl _ @[simp] lemma of_apply (f : m → n → α) (i j) : of f i j = f i j := rfl @[simp] lemma of_symm_apply (f : matrix m n α) (i j) : of.symm f i j = f i j := rfl /-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`. This is available in bundled forms as: * `add_monoid_hom.map_matrix` * `linear_map.map_matrix` * `ring_hom.map_matrix` * `alg_hom.map_matrix` * `equiv.map_matrix` * `add_equiv.map_matrix` * `linear_equiv.map_matrix` * `ring_equiv.map_matrix` * `alg_equiv.map_matrix` -/ def map (M : matrix m n α) (f : α → β) : matrix m n β := of (λ i j, f (M i j)) @[simp] lemma map_apply {M : matrix m n α} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) := rfl @[simp] lemma map_id (M : matrix m n α) : M.map id = M := by { ext, refl, } @[simp] lemma map_map {M : matrix m n α} {β γ : Type*} {f : α → β} {g : β → γ} : (M.map f).map g = M.map (g ∘ f) := by { ext, refl, } lemma map_injective {f : α → β} (hf : function.injective f) : function.injective (λ M : matrix m n α, M.map f) := λ M N h, ext $ λ i j, hf $ ext_iff.mpr h i j /-- The transpose of a matrix. -/ def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix (name := matrix.transpose) `ᵀ`:1500 := matrix.transpose" in matrix /-- The conjugate transpose of a matrix defined in term of `star`. -/ def conj_transpose [has_star α] (M : matrix m n α) : matrix n m α := M.transpose.map star localized "postfix (name := matrix.conj_transpose) `ᴴ`:1500 := matrix.conj_transpose" in matrix /-- `matrix.col u` is the column matrix whose entries are given by `u`. -/ def col (w : m → α) : matrix m unit α | x y := w x /-- `matrix.row u` is the row matrix whose entries are given by `u`. -/ def row (v : n → α) : matrix unit 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_zero_class α] : add_zero_class (matrix m n α) := pi.add_zero_class 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 [has_sub α] : has_sub (matrix m n α) := pi.has_sub 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 instance [unique α] : unique (matrix m n α) := pi.unique instance [subsingleton α] : subsingleton (matrix m n α) := pi.subsingleton instance [nonempty m] [nonempty n] [nontrivial α] : nontrivial (matrix m n α) := function.nontrivial instance [has_smul R α] : has_smul R (matrix m n α) := pi.has_smul instance [has_smul R α] [has_smul S α] [smul_comm_class R S α] : smul_comm_class R S (matrix m n α) := pi.smul_comm_class instance [has_smul R S] [has_smul R α] [has_smul S α] [is_scalar_tower R S α] : is_scalar_tower R S (matrix m n α) := pi.is_scalar_tower instance [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] : is_central_scalar R (matrix m n α) := pi.is_central_scalar instance [monoid R] [mul_action R α] : mul_action R (matrix m n α) := pi.mul_action _ instance [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R (matrix m n α) := pi.distrib_mul_action _ instance [semiring R] [add_comm_monoid α] [module R α] : module R (matrix m n α) := pi.module _ _ _ /-! simp-normal form pulls `of` to the outside. -/ @[simp] lemma of_zero [has_zero α] : of (0 : m → n → α) = 0 := rfl @[simp] lemma of_add_of [has_add α] (f g : m → n → α) : of f + of g = of (f + g) := rfl @[simp] lemma of_sub_of [has_sub α] (f g : m → n → α) : of f - of g = of (f - g) := rfl @[simp] lemma neg_of [has_neg α] (f : m → n → α) : -of f = of (-f) := rfl @[simp] lemma smul_of [has_smul R α] (r : R) (f : m → n → α) : r • of f = of (r • f) := rfl @[simp] protected lemma map_zero [has_zero α] [has_zero β] (f : α → β) (h : f 0 = 0) : (0 : matrix m n α).map f = 0 := by { ext, simp [h], } protected lemma map_add [has_add α] [has_add β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ + a₂) = f a₁ + f a₂) (M N : matrix m n α) : (M + N).map f = M.map f + N.map f := ext $ λ _ _, hf _ _ protected lemma map_sub [has_sub α] [has_sub β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ - a₂) = f a₁ - f a₂) (M N : matrix m n α) : (M - N).map f = M.map f - N.map f := ext $ λ _ _, hf _ _ lemma map_smul [has_smul R α] [has_smul R β] (f : α → β) (r : R) (hf : ∀ a, f (r • a) = r • f a) (M : matrix m n α) : (r • M).map f = r • (M.map f) := ext $ λ _ _, hf _ /-- The scalar action via `has_mul.to_has_smul` is transformed by the same map as the elements of the matrix, when `f` preserves multiplication. -/ lemma map_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α) (hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) : (r • A).map f = f r • A.map f := ext $ λ _ _, hf _ _ /-- The scalar action via `has_mul.to_has_opposite_smul` is transformed by the same map as the elements of the matrix, when `f` preserves multiplication. -/ lemma map_op_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α) (hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) : (mul_opposite.op r • A).map f = mul_opposite.op (f r) • A.map f := ext $ λ _ _, hf _ _ lemma _root_.is_smul_regular.matrix [has_smul R S] {k : R} (hk : is_smul_regular S k) : is_smul_regular (matrix m n S) k := is_smul_regular.pi $ λ _, is_smul_regular.pi $ λ _, hk lemma _root_.is_left_regular.matrix [has_mul α] {k : α} (hk : is_left_regular k) : is_smul_regular (matrix m n α) k := hk.is_smul_regular.matrix instance subsingleton_of_empty_left [is_empty m] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim i }⟩ instance subsingleton_of_empty_right [is_empty n] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim j }⟩ end matrix open_locale matrix namespace matrix section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. Note that bundled versions exist as: * `matrix.diagonal_add_monoid_hom` * `matrix.diagonal_linear_map` * `matrix.diagonal_ring_hom` * `matrix.diagonal_alg_hom` -/ def diagonal [has_zero α] (d : n → α) : matrix n n α | i j := if i = j then d i else 0 @[simp] theorem diagonal_apply_eq [has_zero α] (d : n → α) (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_apply_ne [has_zero α] (d : n → α) {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_apply_ne' [has_zero α] (d : n → α) {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_apply_ne d h.symm @[simp] theorem diagonal_eq_diagonal_iff [has_zero α] {d₁ d₂ : n → α} : diagonal d₁ = diagonal d₂ ↔ ∀ i, d₁ i = d₂ i := ⟨λ h i, by simpa using congr_arg (λ m : matrix n n α, m i i) h, λ h, by rw show d₁ = d₂, from funext h⟩ lemma diagonal_injective [has_zero α] : function.injective (diagonal : (n → α) → matrix n n α) := λ d₁ d₂ h, funext $ λ i, by simpa using matrix.ext_iff.mpr h i i @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by { ext, simp [diagonal] } @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_apply_ne' _ h] } end @[simp] theorem diagonal_add [add_zero_class α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] @[simp] theorem diagonal_smul [monoid R] [add_monoid α] [distrib_mul_action R α] (r : R) (d : n → α) : diagonal (r • d) = r • diagonal d := by ext i j; by_cases h : i = j; simp [h] variables (n α) /-- `matrix.diagonal` as an `add_monoid_hom`. -/ @[simps] def diagonal_add_monoid_hom [add_zero_class α] : (n → α) →+ matrix n n α := { to_fun := diagonal, map_zero' := diagonal_zero, map_add' := λ x y, (diagonal_add x y).symm,} variables (R) /-- `matrix.diagonal` as a `linear_map`. -/ @[simps] def diagonal_linear_map [semiring R] [add_comm_monoid α] [module R α] : (n → α) →ₗ[R] matrix n n α := { map_smul' := diagonal_smul, .. diagonal_add_monoid_hom n α,} variables {n α R} @[simp] lemma diagonal_map [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} : (diagonal d).map f = diagonal (λ m, f (d m)) := by { ext, simp only [diagonal, map_apply], split_ifs; simp [h], } @[simp] lemma diagonal_conj_transpose [add_monoid α] [star_add_monoid α] (v : n → α) : (diagonal v)ᴴ = diagonal (star v) := begin rw [conj_transpose, diagonal_transpose, diagonal_map (star_zero _)], refl, end 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_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq _ i @[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_apply_ne _ theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_apply_ne' _ @[simp] lemma map_one [has_zero β] [has_one β] (f : α → β) (h₀ : f 0 = 0) (h₁ : f 1 = 1) : (1 : matrix n n α).map f = (1 : matrix n n β) := by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], } lemma one_eq_pi_single {i j} : (1 : matrix n n α) i j = pi.single i 1 j := by simp only [one_apply, pi.single_apply, eq_comm]; congr -- deal with decidable_eq end one section numeral @[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_zero_class α] [has_one α] lemma bit1_apply (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_apply_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_apply] @[simp] lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_apply, h] end numeral end diagonal section diag /-- The diagonal of a square matrix. -/ @[simp] def diag (A : matrix n n α) (i : n) : α := A i i @[simp] lemma diag_diagonal [decidable_eq n] [has_zero α] (a : n → α) : diag (diagonal a) = a := funext $ @diagonal_apply_eq _ _ _ _ a @[simp] lemma diag_transpose (A : matrix n n α) : diag Aᵀ = diag A := rfl @[simp] theorem diag_zero [has_zero α] : diag (0 : matrix n n α) = 0 := rfl @[simp] theorem diag_add [has_add α] (A B : matrix n n α) : diag (A + B) = diag A + diag B := rfl @[simp] theorem diag_sub [has_sub α] (A B : matrix n n α) : diag (A - B) = diag A - diag B := rfl @[simp] theorem diag_neg [has_neg α] (A : matrix n n α) : diag (-A) = -diag A := rfl @[simp] theorem diag_smul [has_smul R α] (r : R) (A : matrix n n α) : diag (r • A) = r • diag A := rfl @[simp] theorem diag_one [decidable_eq n] [has_zero α] [has_one α] : diag (1 : matrix n n α) = 1 := diag_diagonal _ variables (n α) /-- `matrix.diag` as an `add_monoid_hom`. -/ @[simps] def diag_add_monoid_hom [add_zero_class α] : matrix n n α →+ (n → α) := { to_fun := diag, map_zero' := diag_zero, map_add' := diag_add,} variables (R) /-- `matrix.diag` as a `linear_map`. -/ @[simps] def diag_linear_map [semiring R] [add_comm_monoid α] [module R α] : matrix n n α →ₗ[R] (n → α) := { map_smul' := diag_smul, .. diag_add_monoid_hom n α,} variables {n α R} lemma diag_map {f : α → β} {A : matrix n n α} : diag (A.map f) = f ∘ diag A := rfl @[simp] lemma diag_conj_transpose [add_monoid α] [star_add_monoid α] (A : matrix n n α) : diag Aᴴ = star (diag A) := rfl @[simp] lemma diag_list_sum [add_monoid α] (l : list (matrix n n α)) : diag l.sum = (l.map diag).sum := map_list_sum (diag_add_monoid_hom n α) l @[simp] lemma diag_multiset_sum [add_comm_monoid α] (s : multiset (matrix n n α)) : diag s.sum = (s.map diag).sum := map_multiset_sum (diag_add_monoid_hom n α) s @[simp] lemma diag_sum {ι} [add_comm_monoid α] (s : finset ι) (f : ι → matrix n n α) : diag (∑ i in s, f i) = ∑ i in s, diag (f i) := map_sum (diag_add_monoid_hom n α) f s end diag section dot_product variables [fintype m] [fintype n] /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i /- The precedence of 72 comes immediately after ` • ` for `has_smul.smul`, so that `r₁ • a ⬝ᵥ r₂ • b` is parsed as `(r₁ • a) ⬝ᵥ (r₂ • b)` here. -/ localized "infix (name := matrix.dot_product) ` ⬝ᵥ `:72 := matrix.dot_product" in matrix lemma dot_product_assoc [non_unital_semiring α] (u : m → α) (w : n → α) (v : matrix m n α) : (λ j, u ⬝ᵥ (λ i, v i j)) ⬝ᵥ w = u ⬝ᵥ (λ i, (v i) ⬝ᵥ w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [add_comm_monoid α] [comm_semigroup α] (v w : m → α) : v ⬝ᵥ w = w ⬝ᵥ v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : v ⬝ᵥ w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] (u v w : m → α) (x y : n → α) @[simp] lemma dot_product_zero : v ⬝ᵥ 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' : v ⬝ᵥ (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product : 0 ⬝ᵥ v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' : (λ _, (0 : α)) ⬝ᵥ v = 0 := zero_dot_product v @[simp] lemma add_dot_product : (u + v) ⬝ᵥ w = u ⬝ᵥ w + v ⬝ᵥ w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add : u ⬝ᵥ (v + w) = u ⬝ᵥ v + u ⬝ᵥ w := by simp [dot_product, mul_add, finset.sum_add_distrib] @[simp] lemma sum_elim_dot_product_sum_elim : (sum.elim u x) ⬝ᵥ (sum.elim v y) = u ⬝ᵥ v + x ⬝ᵥ y := by simp [dot_product] end non_unital_non_assoc_semiring section non_unital_non_assoc_semiring_decidable variables [decidable_eq m] [non_unital_non_assoc_semiring α] (u v w : m → α) @[simp] lemma diagonal_dot_product (i : m) : diagonal v i ⬝ᵥ w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal (i : m) : v ⬝ᵥ diagonal w i = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal' (i : m) : v ⬝ᵥ (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma single_dot_product (x : α) (i : m) : pi.single i x ⬝ᵥ v = x * v i := have ∀ j ≠ i, pi.single i x j * v j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_single (x : α) (i : m) : v ⬝ᵥ pi.single i x = v i * x := have ∀ j ≠ i, v j * pi.single i x j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp end non_unital_non_assoc_semiring_decidable section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] (u v w : m → α) @[simp] lemma neg_dot_product : -v ⬝ᵥ w = - (v ⬝ᵥ w) := by simp [dot_product] @[simp] lemma dot_product_neg : v ⬝ᵥ -w = - (v ⬝ᵥ w) := by simp [dot_product] @[simp] lemma sub_dot_product : (u - v) ⬝ᵥ w = u ⬝ᵥ w - v ⬝ᵥ w := by simp [sub_eq_add_neg] @[simp] lemma dot_product_sub : u ⬝ᵥ (v - w) = u ⬝ᵥ v - u ⬝ᵥ w := by simp [sub_eq_add_neg] end non_unital_non_assoc_ring section distrib_mul_action variables [monoid R] [has_mul α] [add_comm_monoid α] [distrib_mul_action R α] @[simp] lemma smul_dot_product [is_scalar_tower R α α] (x : R) (v w : m → α) : (x • v) ⬝ᵥ w = x • (v ⬝ᵥ w) := by simp [dot_product, finset.smul_sum, smul_mul_assoc] @[simp] lemma dot_product_smul [smul_comm_class R α α] (x : R) (v w : m → α) : v ⬝ᵥ (x • w) = x • (v ⬝ᵥ w) := by simp [dot_product, finset.smul_sum, mul_smul_comm] end distrib_mul_action section star_ring variables [non_unital_semiring α] [star_ring α] (v w : m → α) lemma star_dot_product_star : star v ⬝ᵥ star w = star (w ⬝ᵥ v) := by simp [dot_product] lemma star_dot_product : star v ⬝ᵥ w = star (star w ⬝ᵥ v) := by simp [dot_product] lemma dot_product_star : v ⬝ᵥ star w = star (w ⬝ᵥ star v) := by simp [dot_product] end star_ring end dot_product open_locale matrix /-- `M ⬝ N` is the usual product of matrices `M` and `N`, i.e. we have that `(M ⬝ N) i k` is the dot product of the `i`-th row of `M` by the `k`-th column of `N`. This is currently only defined when `m` is finite. -/ protected def mul [fintype m] [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, (λ j, M i j) ⬝ᵥ (λ j, N j k) localized "infixl (name := matrix.mul) ` ⬝ `:75 := matrix.mul" in matrix theorem mul_apply [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [fintype n] [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [fintype n] [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_apply' [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = (λ j, M i j) ⬝ᵥ (λ j, N j k) := rfl @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := ((diagonal_add_monoid_hom n α).map_neg d).symm lemma sum_apply [add_comm_monoid α] (i : m) (j : n) (s : finset β) (g : β → matrix m n α) : (∑ c in s, g c) i j = ∑ c in s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) lemma two_mul_expl {R : Type*} [comm_ring R] (A B : matrix (fin 2) (fin 2) R) : (A * B) 0 0 = A 0 0 * B 0 0 + A 0 1 * B 1 0 ∧ (A * B) 0 1 = A 0 0 * B 0 1 + A 0 1 * B 1 1 ∧ (A * B) 1 0 = A 1 0 * B 0 0 + A 1 1 * B 1 0 ∧ (A * B) 1 1 = A 1 0 * B 0 1 + A 1 1 * B 1 1 := begin split, work_on_goal 2 {split}, work_on_goal 3 {split}, all_goals {simp only [matrix.mul_eq_mul], rw [matrix.mul_apply, finset.sum_fin_eq_sum_range, finset.sum_range_succ, finset.sum_range_succ], simp}, end section add_comm_monoid variables [add_comm_monoid α] [has_mul α] @[simp] lemma smul_mul [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (M : matrix m n α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } @[simp] lemma mul_smul [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] (M : matrix m n α) (a : R) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } end add_comm_monoid section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] @[simp] protected theorem mul_zero [fintype n] (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul [fintype m] (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add [fintype n] (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul [fintype m] (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } instance [fintype n] : non_unital_non_assoc_semiring (matrix n n α) := { mul := (*), add := (+), zero := 0, mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, .. matrix.add_comm_monoid} @[simp] theorem diagonal_mul [fintype m] [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [fintype n] [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] theorem diagonal_mul_diagonal [fintype n] [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' [fintype n] [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma smul_eq_diagonal_mul [fintype m] [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } @[simp] lemma diag_col_mul_row (a b : n → α) : diag (col a ⬝ row b) = a * b := by { ext, simp [matrix.mul_apply, col, row] } /-- Left multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_left [fintype m] (M : matrix l m α) : matrix m n α →+ matrix l n α := { to_fun := λ x, M ⬝ x, map_zero' := matrix.mul_zero _, map_add' := matrix.mul_add _ } /-- Right multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_right [fintype m] (M : matrix m n α) : matrix l m α →+ matrix l n α := { to_fun := λ x, x ⬝ M, map_zero' := matrix.zero_mul _, map_add' := λ _ _, matrix.add_mul _ _ _ } protected lemma sum_mul [fintype m] (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (add_monoid_hom_mul_right M : matrix l m α →+ _).map_sum f s protected lemma mul_sum [fintype m] (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (add_monoid_hom_mul_left M : matrix m n α →+ _).map_sum f s /-- This instance enables use with `smul_mul_assoc`. -/ instance semiring.is_scalar_tower [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] : is_scalar_tower R (matrix n n α) (matrix n n α) := ⟨λ r m n, matrix.smul_mul r m n⟩ /-- This instance enables use with `mul_smul_comm`. -/ instance semiring.smul_comm_class [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] : smul_comm_class R (matrix n n α) (matrix n n α) := ⟨λ r m n, (matrix.mul_smul m r n).symm⟩ end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring α] @[simp] protected theorem one_mul [fintype m] [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 [fintype n] [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 [fintype n] [decidable_eq n] : non_assoc_semiring (matrix n n α) := { one := 1, one_mul := matrix.one_mul, mul_one := matrix.mul_one, nat_cast := λ n, diagonal (λ _, n), nat_cast_zero := by ext; simp [nat.cast], nat_cast_succ := λ n, by ext; by_cases i = j; simp [nat.cast, *], .. matrix.non_unital_non_assoc_semiring } @[simp] lemma map_mul [fintype n] {L : matrix m n α} {M : matrix n o α} [non_assoc_semiring β] {f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f := by { ext, simp [mul_apply, ring_hom.map_sum], } variables (α n) /-- `matrix.diagonal` as a `ring_hom`. -/ @[simps] def diagonal_ring_hom [fintype n] [decidable_eq n] : (n → α) →+* matrix n n α := { to_fun := diagonal, map_one' := diagonal_one, map_mul' := λ _ _, (diagonal_mul_diagonal' _ _).symm, .. diagonal_add_monoid_hom n α } end non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] [fintype m] [fintype n] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : non_unital_semiring (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.non_unital_non_assoc_semiring } end non_unital_semiring section semiring variables [semiring α] instance [fintype n] [decidable_eq n] : semiring (matrix n n α) := { ..matrix.non_unital_semiring, ..matrix.non_assoc_semiring } end semiring section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] [fintype n] @[simp] protected theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] protected theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } protected theorem sub_mul (M M' : matrix m n α) (N : matrix n o α) : (M - M') ⬝ N = M ⬝ N - M' ⬝ N := by rw [sub_eq_add_neg, matrix.add_mul, matrix.neg_mul, sub_eq_add_neg] protected theorem mul_sub (M : matrix m n α) (N N' : matrix n o α) : M ⬝ (N - N') = M ⬝ N - M ⬝ N' := by rw [sub_eq_add_neg, matrix.mul_add, matrix.mul_neg, sub_eq_add_neg] instance : non_unital_non_assoc_ring (matrix n n α) := { ..matrix.non_unital_non_assoc_semiring, ..matrix.add_comm_group } end non_unital_non_assoc_ring instance [fintype n] [non_unital_ring α] : non_unital_ring (matrix n n α) := { ..matrix.non_unital_semiring, ..matrix.add_comm_group } instance [fintype n] [decidable_eq n] [non_assoc_ring α] : non_assoc_ring (matrix n n α) := { ..matrix.non_assoc_semiring, ..matrix.add_comm_group } instance [fintype n] [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } section semiring variables [semiring α] lemma diagonal_pow [fintype n] [decidable_eq n] (v : n → α) (k : ℕ) : diagonal v ^ k = diagonal (v ^ k) := (map_pow (diagonal_ring_hom n α) v k).symm @[simp] lemma mul_mul_left [fintype n] (M : matrix m n α) (N : matrix n o α) (a : α) : of (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) := smul_mul a M N /-- The ring homomorphism `α →+* matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α := { to_fun := λ a, a • 1, map_one' := by simp, map_mul' := by { intros, ext, simp [mul_assoc], }, .. (smul_add_hom α _).flip (1 : matrix n n α) } section scalar variables [decidable_eq n] [fintype n] @[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl lemma scalar_apply_eq (a : α) (i : n) : scalar n a i i = a := by simp only [coe_scalar, smul_eq_mul, mul_one, one_apply_eq, pi.smul_apply] lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) : scalar n a i j = 0 := by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, pi.smul_apply, smul_zero] lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := begin split, { intro h, inhabit n, rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] }, { rintro rfl, refl } end end scalar end semiring section comm_semiring variables [comm_semiring α] [fintype n] 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_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) : M ⬝ of (λ i j, a * N i j) = a • (M ⬝ N) := mul_smul M a N lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M := by simp [commute, semiconj_by] end comm_semiring section algebra variables [fintype n] [decidable_eq n] variables [comm_semiring R] [semiring α] [semiring β] [algebra R α] [algebra R β] instance : algebra R (matrix n n α) := { commutes' := λ r x, begin ext, simp [matrix.scalar, matrix.mul_apply, matrix.one_apply, algebra.commutes, smul_ite], end, smul_def' := λ r x, begin ext, simp [matrix.scalar, algebra.smul_def r], end, ..((matrix.scalar n).comp (algebra_map R α)) } lemma algebra_map_matrix_apply {r : R} {i j : n} : algebra_map R (matrix n n α) r i j = if i = j then algebra_map R α r else 0 := begin dsimp [algebra_map, algebra.to_ring_hom, matrix.scalar], split_ifs with h; simp [h, matrix.one_apply_ne], end lemma algebra_map_eq_diagonal (r : R) : algebra_map R (matrix n n α) r = diagonal (algebra_map R (n → α) r) := matrix.ext $ λ i j, algebra_map_matrix_apply @[simp] lemma algebra_map_eq_smul (r : R) : algebra_map R (matrix n n R) r = r • (1 : matrix n n R) := rfl lemma algebra_map_eq_diagonal_ring_hom : algebra_map R (matrix n n α) = (diagonal_ring_hom n α).comp (algebra_map R _) := ring_hom.ext algebra_map_eq_diagonal @[simp] lemma map_algebra_map (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebra_map R α r) = algebra_map R β r) : (algebra_map R (matrix n n α) r).map f = algebra_map R (matrix n n β) r := begin rw [algebra_map_eq_diagonal, algebra_map_eq_diagonal, diagonal_map hf], congr' 1 with x, simp only [hf₂, pi.algebra_map_apply] end variables (R) /-- `matrix.diagonal` as an `alg_hom`. -/ @[simps] def diagonal_alg_hom : (n → α) →ₐ[R] matrix n n α := { to_fun := diagonal, commutes' := λ r, (algebra_map_eq_diagonal r).symm, .. diagonal_ring_hom n α } end algebra end matrix /-! ### Bundled versions of `matrix.map` -/ namespace equiv /-- The `equiv` between spaces of matrices induced by an `equiv` between their coefficients. This is `matrix.map` as an `equiv`. -/ @[simps apply] def map_matrix (f : α ≃ β) : matrix m n α ≃ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, left_inv := λ M, matrix.ext $ λ _ _, f.symm_apply_apply _, right_inv := λ M, matrix.ext $ λ _ _, f.apply_symm_apply _, } @[simp] lemma map_matrix_refl : (equiv.refl α).map_matrix = equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ β) (g : β ≃ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ _) := rfl end equiv namespace add_monoid_hom variables [add_zero_class α] [add_zero_class β] [add_zero_class γ] /-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their coefficients. This is `matrix.map` as an `add_monoid_hom`. -/ @[simps] def map_matrix (f : α →+ β) : matrix m n α →+ matrix m n β := { to_fun := λ M, M.map f, map_zero' := matrix.map_zero f f.map_zero, map_add' := matrix.map_add f f.map_add } @[simp] lemma map_matrix_id : (add_monoid_hom.id α).map_matrix = add_monoid_hom.id (matrix m n α) := rfl @[simp] lemma map_matrix_comp (f : β →+ γ) (g : α →+ β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →+ _) := rfl end add_monoid_hom namespace add_equiv variables [has_add α] [has_add β] [has_add γ] /-- The `add_equiv` between spaces of matrices induced by an `add_equiv` between their coefficients. This is `matrix.map` as an `add_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+ β) : matrix m n α ≃+ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, map_add' := matrix.map_add f f.map_add, .. f.to_equiv.map_matrix } @[simp] lemma map_matrix_refl : (add_equiv.refl α).map_matrix = add_equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃+ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃+ _) := rfl end add_equiv namespace linear_map variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_map` between spaces of matrices induced by a `linear_map` between their coefficients. This is `matrix.map` as a `linear_map`. -/ @[simps] def map_matrix (f : α →ₗ[R] β) : matrix m n α →ₗ[R] matrix m n β := { to_fun := λ M, M.map f, map_add' := matrix.map_add f f.map_add, map_smul' := λ r, matrix.map_smul f r (f.map_smul r), } @[simp] lemma map_matrix_id : linear_map.id.map_matrix = (linear_map.id : matrix m n α →ₗ[R] _) := rfl @[simp] lemma map_matrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →ₗ[R] _) := rfl end linear_map namespace linear_equiv variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_equiv` between spaces of matrices induced by an `linear_equiv` between their coefficients. This is `matrix.map` as an `linear_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₗ[R] β) : matrix m n α ≃ₗ[R] matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_equiv.map_matrix, .. f.to_linear_map.map_matrix } @[simp] lemma map_matrix_refl : (linear_equiv.refl R α).map_matrix = linear_equiv.refl R (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₗ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ₗ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ₗ[R] _) := rfl end linear_equiv namespace ring_hom variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_hom` between spaces of square matrices induced by a `ring_hom` between their coefficients. This is `matrix.map` as a `ring_hom`. -/ @[simps] def map_matrix (f : α →+* β) : matrix m m α →+* matrix m m β := { to_fun := λ M, M.map f, map_one' := by simp, map_mul' := λ L M, matrix.map_mul, .. f.to_add_monoid_hom.map_matrix } @[simp] lemma map_matrix_id : (ring_hom.id α).map_matrix = ring_hom.id (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →+* γ) (g : α →+* β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →+* _) := rfl end ring_hom namespace ring_equiv variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_equiv` between spaces of square matrices induced by a `ring_equiv` between their coefficients. This is `matrix.map` as a `ring_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+* β) : matrix m m α ≃+* matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_ring_hom.map_matrix, .. f.to_add_equiv.map_matrix } @[simp] lemma map_matrix_refl : (ring_equiv.refl α).map_matrix = ring_equiv.refl (matrix m m α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+* β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃+* _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃+* _) := rfl end ring_equiv namespace alg_hom variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_hom` between spaces of square matrices induced by a `alg_hom` between their coefficients. This is `matrix.map` as a `alg_hom`. -/ @[simps] def map_matrix (f : α →ₐ[R] β) : matrix m m α →ₐ[R] matrix m m β := { to_fun := λ M, M.map f, commutes' := λ r, matrix.map_algebra_map r f f.map_zero (f.commutes r), .. f.to_ring_hom.map_matrix } @[simp] lemma map_matrix_id : (alg_hom.id R α).map_matrix = alg_hom.id R (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →ₐ[R] _) := rfl end alg_hom namespace alg_equiv variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_equiv` between spaces of square matrices induced by a `alg_equiv` between their coefficients. This is `matrix.map` as a `alg_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₐ[R] β) : matrix m m α ≃ₐ[R] matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_alg_hom.map_matrix, .. f.to_ring_equiv.map_matrix } @[simp] lemma map_matrix_refl : alg_equiv.refl.map_matrix = (alg_equiv.refl : matrix m m α ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₐ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃ₐ[R] _) := rfl end alg_equiv open_locale matrix namespace matrix /-- For two vectors `w` and `v`, `vec_mul_vec w v i j` is defined to be `w i * v j`. Put another way, `vec_mul_vec w v` is exactly `col w ⬝ row v`. -/ def vec_mul_vec [has_mul α] (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y lemma vec_mul_vec_eq [has_mul α] [add_comm_monoid α] (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp only [vec_mul_vec, mul_apply, fintype.univ_punit, finset.sum_singleton], refl } section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] /-- `mul_vec M v` is the matrix-vector product of `M` and `v`, where `v` is seen as a column matrix. Put another way, `mul_vec M v` is the vector whose entries are those of `M ⬝ col v` (see `col_mul_vec`). -/ def mul_vec [fintype n] (M : matrix m n α) (v : n → α) : m → α | i := (λ j, M i j) ⬝ᵥ v /-- `vec_mul v M` is the vector-matrix product of `v` and `M`, where `v` is seen as a row matrix. Put another way, `vec_mul v M` is the vector whose entries are those of `row v ⬝ M` (see `row_vec_mul`). -/ def vec_mul [fintype m] (v : m → α) (M : matrix m n α) : n → α | j := v ⬝ᵥ (λ i, M i j) /-- Left multiplication by a matrix, as an `add_monoid_hom` from vectors to vectors. -/ @[simps] def mul_vec.add_monoid_hom_left [fintype n] (v : n → α) : matrix m n α →+ m → α := { to_fun := λ M, mul_vec M v, map_zero' := by ext; simp [mul_vec]; refl, map_add' := λ x y, by { ext m, apply add_dot_product } } lemma mul_vec_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x /-- Associate the dot product of `mul_vec` to the left. -/ lemma dot_product_mul_vec [fintype n] [fintype m] [non_unital_semiring R] (v : m → R) (A : matrix m n R) (w : n → R) : v ⬝ᵥ mul_vec A w = vec_mul v A ⬝ᵥ w := by simp only [dot_product, vec_mul, mul_vec, finset.mul_sum, finset.sum_mul, mul_assoc]; exact finset.sum_comm @[simp] lemma mul_vec_zero [fintype n] (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma zero_vec_mul [fintype m] (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma zero_mul_vec [fintype n] (v : n → α) : mul_vec (0 : matrix m n α) v = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero [fintype m] (v : m → α) : vec_mul v (0 : matrix m n α) = 0 := by { ext, simp [vec_mul] } lemma smul_mul_vec_assoc [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (A : matrix m n α) (b : n → α) : (a • A).mul_vec b = a • (A.mul_vec b) := by { ext, apply smul_dot_product, } lemma mul_vec_add [fintype n] (A : matrix m n α) (x y : n → α) : A.mul_vec (x + y) = A.mul_vec x + A.mul_vec y := by { ext, apply dot_product_add } lemma add_mul_vec [fintype n] (A B : matrix m n α) (x : n → α) : (A + B).mul_vec x = A.mul_vec x + B.mul_vec x := by { ext, apply add_dot_product } lemma vec_mul_add [fintype m] (A B : matrix m n α) (x : m → α) : vec_mul x (A + B) = vec_mul x A + vec_mul x B := by { ext, apply dot_product_add } lemma add_vec_mul [fintype m] (A : matrix m n α) (x y : m → α) : vec_mul (x + y) A = vec_mul x A + vec_mul y A := by { ext, apply add_dot_product } lemma vec_mul_smul [fintype n] [monoid R] [non_unital_non_assoc_semiring S] [distrib_mul_action R S] [is_scalar_tower R S S] (M : matrix n m S) (b : R) (v : n → S) : M.vec_mul (b • v) = b • M.vec_mul v := by { ext i, simp only [vec_mul, dot_product, finset.smul_sum, pi.smul_apply, smul_mul_assoc] } lemma mul_vec_smul [fintype n] [monoid R] [non_unital_non_assoc_semiring S] [distrib_mul_action R S] [smul_comm_class R S S] (M : matrix m n S) (b : R) (v : n → S) : M.mul_vec (b • v) = b • M.mul_vec v := by { ext i, simp only [mul_vec, dot_product, finset.smul_sum, pi.smul_apply, mul_smul_comm] } @[simp] lemma mul_vec_single [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (M : matrix m n R) (j : n) (x : R) : M.mul_vec (pi.single j x) = (λ i, M i j * x) := funext $ λ i, dot_product_single _ _ _ @[simp] lemma single_vec_mul [fintype m] [decidable_eq m] [non_unital_non_assoc_semiring R] (M : matrix m n R) (i : m) (x : R) : vec_mul (pi.single i x) M = (λ j, x * M i j) := funext $ λ i, single_dot_product _ _ _ @[simp] lemma diagonal_mul_vec_single [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (v : n → R) (j : n) (x : R) : (diagonal v).mul_vec (pi.single j x) = pi.single j (v j * x) := begin ext i, rw mul_vec_diagonal, exact pi.apply_single (λ i x, v i * x) (λ i, mul_zero _) j x i, end @[simp] lemma single_vec_mul_diagonal [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (v : n → R) (j : n) (x : R) : vec_mul (pi.single j x) (diagonal v) = pi.single j (x * v j) := begin ext i, rw vec_mul_diagonal, exact pi.apply_single (λ i x, x * v i) (λ i, zero_mul _) j x i, end end non_unital_non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] @[simp] lemma vec_mul_vec_mul [fintype n] [fintype m] (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec [fintype n] [fintype o] (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } lemma star_mul_vec [fintype n] [star_ring α] (M : matrix m n α) (v : n → α) : star (M.mul_vec v) = vec_mul (star v) (Mᴴ) := funext $ λ i, (star_dot_product_star _ _).symm lemma star_vec_mul [fintype m] [star_ring α] (M : matrix m n α) (v : m → α) : star (M.vec_mul v) = (Mᴴ).mul_vec (star v) := funext $ λ i, (star_dot_product_star _ _).symm lemma mul_vec_conj_transpose [fintype m] [star_ring α] (A : matrix m n α) (x : m → α) : mul_vec Aᴴ x = star (vec_mul (star x) A) := funext $ λ i, star_dot_product _ _ lemma vec_mul_conj_transpose [fintype n] [star_ring α] (A : matrix m n α) (x : n → α) : vec_mul x Aᴴ = star (mul_vec A (star x)) := funext $ λ i, dot_product_star _ _ lemma mul_mul_apply [fintype n] (A B C : matrix n n α) (i j : n) : (A ⬝ B ⬝ C) i j = A i ⬝ᵥ (B.mul_vec (Cᵀ j)) := by { rw matrix.mul_assoc, simpa only [mul_apply, dot_product, mul_vec] } end non_unital_semiring section non_assoc_semiring variables [fintype m] [decidable_eq m] [non_assoc_semiring α] @[simp] lemma one_mul_vec (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } end non_assoc_semiring section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] lemma neg_vec_mul [fintype m] (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg [fintype m] (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec [fintype n] (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg [fintype n] (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } lemma sub_mul_vec [fintype n] (A B : matrix m n α) (x : n → α) : mul_vec (A - B) x = mul_vec A x - mul_vec B x := by simp [sub_eq_add_neg, add_mul_vec, neg_mul_vec] lemma vec_mul_sub [fintype m] (A B : matrix m n α) (x : m → α) : vec_mul x (A - B) = vec_mul x A - vec_mul x B := by simp [sub_eq_add_neg, vec_mul_add, vec_mul_neg] end non_unital_non_assoc_ring section non_unital_comm_semiring variables [non_unital_comm_semiring α] lemma mul_vec_transpose [fintype m] (A : matrix m n α) (x : m → α) : mul_vec Aᵀ x = vec_mul x A := by { ext, apply dot_product_comm } lemma vec_mul_transpose [fintype n] (A : matrix m n α) (x : n → α) : vec_mul x Aᵀ = mul_vec A x := by { ext, apply dot_product_comm } lemma mul_vec_vec_mul [fintype n] [fintype o] (A : matrix m n α) (B : matrix o n α) (x : o → α) : mul_vec A (vec_mul x B) = mul_vec (A ⬝ Bᵀ) x := by rw [← mul_vec_mul_vec, mul_vec_transpose] lemma vec_mul_mul_vec [fintype m] [fintype n] (A : matrix m n α) (B : matrix m o α) (x : n → α) : vec_mul (mul_vec A x) B = vec_mul x (Aᵀ ⬝ B) := by rw [← vec_mul_vec_mul, vec_mul_transpose] end non_unital_comm_semiring section comm_semiring variables [comm_semiring α] lemma mul_vec_smul_assoc [fintype n] (A : matrix m n α) (b : n → α) (a : α) : A.mul_vec (a • b) = a • (A.mul_vec b) := by { ext, apply dot_product_smul } end comm_semiring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma transpose_apply (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_apply_eq] }, { simp only [diagonal_apply_ne _ h, diagonal_apply_ne' _ h] } 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_sub [has_sub α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᵀ = Mᵀ - Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [add_comm_monoid α] [comm_semigroup α] [fintype n] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul {R : Type*} [has_smul R α] (c : R) (M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [has_neg α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl lemma transpose_map {f : α → β} {M : matrix m n α} : Mᵀ.map f = (M.map f)ᵀ := by { ext, refl } variables (m n α) /-- `matrix.transpose` as an `add_equiv` -/ @[simps apply] def transpose_add_equiv [has_add α] : matrix m n α ≃+ matrix n m α := { to_fun := transpose, inv_fun := transpose, left_inv := transpose_transpose, right_inv := transpose_transpose, map_add' := transpose_add } @[simp] lemma transpose_add_equiv_symm [has_add α] : (transpose_add_equiv m n α).symm = transpose_add_equiv n m α := rfl variables {m n α} lemma transpose_list_sum [add_monoid α] (l : list (matrix m n α)) : l.sumᵀ = (l.map transpose).sum := (transpose_add_equiv m n α).to_add_monoid_hom.map_list_sum l lemma transpose_multiset_sum [add_comm_monoid α] (s : multiset (matrix m n α)) : s.sumᵀ = (s.map transpose).sum := (transpose_add_equiv m n α).to_add_monoid_hom.map_multiset_sum s lemma transpose_sum [add_comm_monoid α] {ι : Type*} (s : finset ι) (M : ι → matrix m n α) : (∑ i in s, M i)ᵀ = ∑ i in s, (M i)ᵀ := (transpose_add_equiv m n α).to_add_monoid_hom.map_sum _ s variables (m n R α) /-- `matrix.transpose` as a `linear_map` -/ @[simps apply] def transpose_linear_equiv [semiring R] [add_comm_monoid α] [module R α] : matrix m n α ≃ₗ[R] matrix n m α := { map_smul' := transpose_smul, ..transpose_add_equiv m n α} @[simp] lemma transpose_linear_equiv_symm [semiring R] [add_comm_monoid α] [module R α] : (transpose_linear_equiv m n R α).symm = transpose_linear_equiv n m R α := rfl variables {m n R α} variables (m α) /-- `matrix.transpose` as a `ring_equiv` to the opposite ring -/ @[simps] def transpose_ring_equiv [add_comm_monoid α] [comm_semigroup α] [fintype m] : matrix m m α ≃+* (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᵀ), inv_fun := λ M, M.unopᵀ, map_mul' := λ M N, (congr_arg mul_opposite.op (transpose_mul M N)).trans (mul_opposite.op_mul _ _), ..(transpose_add_equiv m m α).trans mul_opposite.op_add_equiv } variables {m α} @[simp] lemma transpose_pow [comm_semiring α] [fintype m] [decidable_eq m] (M : matrix m m α) (k : ℕ) : (M ^ k)ᵀ = Mᵀ ^ k := mul_opposite.op_injective $ map_pow (transpose_ring_equiv m α) M k lemma transpose_list_prod [comm_semiring α] [fintype m] [decidable_eq m] (l : list (matrix m m α)) : l.prodᵀ = (l.map transpose).reverse.prod := (transpose_ring_equiv m α).unop_map_list_prod l variables (R m α) /-- `matrix.transpose` as an `alg_equiv` to the opposite ring -/ @[simps] def transpose_alg_equiv [comm_semiring R] [comm_semiring α] [fintype m] [decidable_eq m] [algebra R α] : matrix m m α ≃ₐ[R] (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᵀ), commutes' := λ r, by simp only [algebra_map_eq_diagonal, diagonal_transpose, mul_opposite.algebra_map_apply], ..(transpose_add_equiv m m α).trans mul_opposite.op_add_equiv, ..transpose_ring_equiv m α } variables {R m α} end transpose section conj_transpose open_locale matrix /-- Tell `simp` what the entries are in a conjugate transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma conj_transpose_apply [has_star α] (M : matrix m n α) (i j) : M.conj_transpose j i = star (M i j) := rfl @[simp] lemma conj_transpose_conj_transpose [has_involutive_star α] (M : matrix m n α) : Mᴴᴴ = M := matrix.ext $ by simp @[simp] lemma conj_transpose_zero [add_monoid α] [star_add_monoid α] : (0 : matrix m n α)ᴴ = 0 := matrix.ext $ by simp @[simp] lemma conj_transpose_one [decidable_eq n] [semiring α] [star_ring α]: (1 : matrix n n α)ᴴ = 1 := by simp [conj_transpose] @[simp] lemma conj_transpose_add [add_monoid α] [star_add_monoid α] (M N : matrix m n α) : (M + N)ᴴ = Mᴴ + Nᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_sub [add_group α] [star_add_monoid α] (M N : matrix m n α) : (M - N)ᴴ = Mᴴ - Nᴴ := matrix.ext $ by simp /-- Note that `star_module` is quite a strong requirement; as such we also provide the following variants which this lemma would not apply to: * `matrix.conj_transpose_smul_non_comm` * `matrix.conj_transpose_nsmul` * `matrix.conj_transpose_zsmul` * `matrix.conj_transpose_nat_cast_smul` * `matrix.conj_transpose_int_cast_smul` * `matrix.conj_transpose_inv_nat_cast_smul` * `matrix.conj_transpose_inv_int_cast_smul` * `matrix.conj_transpose_rat_smul` * `matrix.conj_transpose_rat_cast_smul` -/ @[simp] lemma conj_transpose_smul [has_star R] [has_star α] [has_smul R α] [star_module R α] (c : R) (M : matrix m n α) : (c • M)ᴴ = star c • Mᴴ := matrix.ext $ λ i j, star_smul _ _ @[simp] lemma conj_transpose_smul_non_comm [has_star R] [has_star α] [has_smul R α] [has_smul Rᵐᵒᵖ α] (c : R) (M : matrix m n α) (h : ∀ (r : R) (a : α), star (r • a) = mul_opposite.op (star r) • star a) : (c • M)ᴴ = mul_opposite.op (star c) • Mᴴ := matrix.ext $ by simp [h] @[simp] lemma conj_transpose_smul_self [semigroup α] [star_semigroup α] (c : α) (M : matrix m n α) : (c • M)ᴴ = mul_opposite.op (star c) • Mᴴ := conj_transpose_smul_non_comm c M star_mul @[simp] lemma conj_transpose_nsmul [add_monoid α] [star_add_monoid α] (c : ℕ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_zsmul [add_group α] [star_add_monoid α] (c : ℤ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_nat_cast_smul [semiring R] [add_comm_monoid α] [star_add_monoid α] [module R α] (c : ℕ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_int_cast_smul [ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℤ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_inv_nat_cast_smul [division_ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℕ) (M : matrix m n α) : ((c : R)⁻¹ • M)ᴴ = (c : R)⁻¹ • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_inv_int_cast_smul [division_ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℤ) (M : matrix m n α) : ((c : R)⁻¹ • M)ᴴ = (c : R)⁻¹ • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_rat_cast_smul [division_ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℚ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_rat_smul [add_comm_group α] [star_add_monoid α] [module ℚ α] (c : ℚ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_mul [fintype n] [non_unital_semiring α] [star_ring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᴴ = Nᴴ ⬝ Mᴴ := matrix.ext $ by simp [mul_apply] @[simp] lemma conj_transpose_neg [add_group α] [star_add_monoid α] (M : matrix m n α) : (- M)ᴴ = - Mᴴ := matrix.ext $ by simp lemma conj_transpose_map [has_star α] [has_star β] {A : matrix m n α} (f : α → β) (hf : function.semiconj f star star) : Aᴴ.map f = (A.map f)ᴴ := matrix.ext $ λ i j, hf _ variables (m n α) /-- `matrix.conj_transpose` as an `add_equiv` -/ @[simps apply] def conj_transpose_add_equiv [add_monoid α] [star_add_monoid α] : matrix m n α ≃+ matrix n m α := { to_fun := conj_transpose, inv_fun := conj_transpose, left_inv := conj_transpose_conj_transpose, right_inv := conj_transpose_conj_transpose, map_add' := conj_transpose_add } @[simp] lemma conj_transpose_add_equiv_symm [add_monoid α] [star_add_monoid α] : (conj_transpose_add_equiv m n α).symm = conj_transpose_add_equiv n m α := rfl variables {m n α} lemma conj_transpose_list_sum [add_monoid α] [star_add_monoid α] (l : list (matrix m n α)) : l.sumᴴ = (l.map conj_transpose).sum := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_list_sum l lemma conj_transpose_multiset_sum [add_comm_monoid α] [star_add_monoid α] (s : multiset (matrix m n α)) : s.sumᴴ = (s.map conj_transpose).sum := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_multiset_sum s lemma conj_transpose_sum [add_comm_monoid α] [star_add_monoid α] {ι : Type*} (s : finset ι) (M : ι → matrix m n α) : (∑ i in s, M i)ᴴ = ∑ i in s, (M i)ᴴ := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_sum _ s variables (m n R α) /-- `matrix.conj_transpose` as a `linear_map` -/ @[simps apply] def conj_transpose_linear_equiv [comm_semiring R] [star_ring R] [add_comm_monoid α] [star_add_monoid α] [module R α] [star_module R α] : matrix m n α ≃ₗ⋆[R] matrix n m α := { map_smul' := conj_transpose_smul, ..conj_transpose_add_equiv m n α} @[simp] lemma conj_transpose_linear_equiv_symm [comm_semiring R] [star_ring R] [add_comm_monoid α] [star_add_monoid α] [module R α] [star_module R α] : (conj_transpose_linear_equiv m n R α).symm = conj_transpose_linear_equiv n m R α := rfl variables {m n R α} variables (m α) /-- `matrix.conj_transpose` as a `ring_equiv` to the opposite ring -/ @[simps] def conj_transpose_ring_equiv [semiring α] [star_ring α] [fintype m] : matrix m m α ≃+* (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᴴ), inv_fun := λ M, M.unopᴴ, map_mul' := λ M N, (congr_arg mul_opposite.op (conj_transpose_mul M N)).trans (mul_opposite.op_mul _ _), ..(conj_transpose_add_equiv m m α).trans mul_opposite.op_add_equiv } variables {m α} @[simp] lemma conj_transpose_pow [semiring α] [star_ring α] [fintype m] [decidable_eq m] (M : matrix m m α) (k : ℕ) : (M ^ k)ᴴ = Mᴴ ^ k := mul_opposite.op_injective $ map_pow (conj_transpose_ring_equiv m α) M k lemma conj_transpose_list_prod [semiring α] [star_ring α] [fintype m] [decidable_eq m] (l : list (matrix m m α)) : l.prodᴴ = (l.map conj_transpose).reverse.prod := (conj_transpose_ring_equiv m α).unop_map_list_prod l end conj_transpose section star /-- When `α` has a star operation, square matrices `matrix n n α` have a star operation equal to `matrix.conj_transpose`. -/ instance [has_star α] : has_star (matrix n n α) := {star := conj_transpose} lemma star_eq_conj_transpose [has_star α] (M : matrix m m α) : star M = Mᴴ := rfl @[simp] lemma star_apply [has_star α] (M : matrix n n α) (i j) : (star M) i j = star (M j i) := rfl instance [has_involutive_star α] : has_involutive_star (matrix n n α) := { star_involutive := conj_transpose_conj_transpose } /-- When `α` is a `*`-additive monoid, `matrix.has_star` is also a `*`-additive monoid. -/ instance [add_monoid α] [star_add_monoid α] : star_add_monoid (matrix n n α) := { star_add := conj_transpose_add } /-- When `α` is a `*`-(semi)ring, `matrix.has_star` is also a `*`-(semi)ring. -/ instance [fintype n] [semiring α] [star_ring α] : star_ring (matrix n n α) := { star_add := conj_transpose_add, star_mul := conj_transpose_mul, } /-- A version of `star_mul` for `⬝` instead of `*`. -/ lemma star_mul [fintype n] [non_unital_semiring α] [star_ring α] (M N : matrix n n α) : star (M ⬝ N) = star N ⬝ star M := conj_transpose_mul _ _ end star /-- Given maps `(r_reindex : l → m)` and `(c_reindex : o → n)` reindexing the rows and columns of a matrix `M : matrix m n α`, the matrix `M.submatrix r_reindex c_reindex : matrix l o α` is defined by `(M.submatrix r_reindex c_reindex) i j = M (r_reindex i) (c_reindex j)` for `(i,j) : l × o`. Note that the total number of row and columns does not have to be preserved. -/ def submatrix (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : matrix l o α := of $ λ i j, A (r_reindex i) (c_reindex j) @[simp] lemma submatrix_apply (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) (i j) : A.submatrix r_reindex c_reindex i j = A (r_reindex i) (c_reindex j) := rfl @[simp] lemma submatrix_id_id (A : matrix m n α) : A.submatrix id id = A := ext $ λ _ _, rfl @[simp] lemma submatrix_submatrix {l₂ o₂ : Type*} (A : matrix m n α) (r₁ : l → m) (c₁ : o → n) (r₂ : l₂ → l) (c₂ : o₂ → o) : (A.submatrix r₁ c₁).submatrix r₂ c₂ = A.submatrix (r₁ ∘ r₂) (c₁ ∘ c₂) := ext $ λ _ _, rfl @[simp] lemma transpose_submatrix (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.submatrix r_reindex c_reindex)ᵀ = Aᵀ.submatrix c_reindex r_reindex := ext $ λ _ _, rfl @[simp] lemma conj_transpose_submatrix [has_star α] (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.submatrix r_reindex c_reindex)ᴴ = Aᴴ.submatrix c_reindex r_reindex := ext $ λ _ _, rfl lemma submatrix_add [has_add α] (A B : matrix m n α) : ((A + B).submatrix : (l → m) → (o → n) → matrix l o α) = A.submatrix + B.submatrix := rfl lemma submatrix_neg [has_neg α] (A : matrix m n α) : ((-A).submatrix : (l → m) → (o → n) → matrix l o α) = -A.submatrix := rfl lemma submatrix_sub [has_sub α] (A B : matrix m n α) : ((A - B).submatrix : (l → m) → (o → n) → matrix l o α) = A.submatrix - B.submatrix := rfl @[simp] lemma submatrix_zero [has_zero α] : ((0 : matrix m n α).submatrix : (l → m) → (o → n) → matrix l o α) = 0 := rfl lemma submatrix_smul {R : Type*} [has_smul R α] (r : R) (A : matrix m n α) : ((r • A : matrix m n α).submatrix : (l → m) → (o → n) → matrix l o α) = r • A.submatrix := rfl lemma submatrix_map (f : α → β) (e₁ : l → m) (e₂ : o → n) (A : matrix m n α) : (A.map f).submatrix e₁ e₂ = (A.submatrix e₁ e₂).map f := rfl /-- Given a `(m × m)` diagonal matrix defined by a map `d : m → α`, if the reindexing map `e` is injective, then the resulting matrix is again diagonal. -/ lemma submatrix_diagonal [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l → m) (he : function.injective e) : (diagonal d).submatrix e e = diagonal (d ∘ e) := ext $ λ i j, begin rw submatrix_apply, by_cases h : i = j, { rw [h, diagonal_apply_eq, diagonal_apply_eq], }, { rw [diagonal_apply_ne _ h, diagonal_apply_ne _ (he.ne h)], }, end lemma submatrix_one [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l → m) (he : function.injective e) : (1 : matrix m m α).submatrix e e = 1 := submatrix_diagonal _ e he lemma submatrix_mul [fintype n] [fintype o] [has_mul α] [add_comm_monoid α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o → n) (e₃ : q → p) (he₂ : function.bijective e₂) : (M ⬝ N).submatrix e₁ e₃ = (M.submatrix e₁ e₂) ⬝ (N.submatrix e₂ e₃) := ext $ λ _ _, (he₂.sum_comp _).symm lemma diag_submatrix (A : matrix m m α) (e : l → m) : diag (A.submatrix e e) = A.diag ∘ e := rfl /-! `simp` lemmas for `matrix.submatrix`s interaction with `matrix.diagonal`, `1`, and `matrix.mul` for when the mappings are bundled. -/ @[simp] lemma submatrix_diagonal_embedding [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ↪ m) : (diagonal d).submatrix e e = diagonal (d ∘ e) := submatrix_diagonal d e e.injective @[simp] lemma submatrix_diagonal_equiv [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ≃ m) : (diagonal d).submatrix e e = diagonal (d ∘ e) := submatrix_diagonal d e e.injective @[simp] lemma submatrix_one_embedding [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ↪ m) : (1 : matrix m m α).submatrix e e = 1 := submatrix_one e e.injective @[simp] lemma submatrix_one_equiv [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ≃ m) : (1 : matrix m m α).submatrix e e = 1 := submatrix_one e e.injective @[simp] lemma submatrix_mul_equiv [fintype n] [fintype o] [add_comm_monoid α] [has_mul α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o ≃ n) (e₃ : q → p) : (M.submatrix e₁ e₂) ⬝ (N.submatrix e₂ e₃) = (M ⬝ N).submatrix e₁ e₃ := (submatrix_mul M N e₁ e₂ e₃ e₂.bijective).symm lemma mul_submatrix_one [fintype n] [fintype o] [non_assoc_semiring α] [decidable_eq o] (e₁ : n ≃ o) (e₂ : l → o) (M : matrix m n α) : M ⬝ (1 : matrix o o α).submatrix e₁ e₂ = submatrix M id (e₁.symm ∘ e₂) := begin let A := M.submatrix id e₁.symm, have : M = A.submatrix id e₁, { simp only [submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], }, rw [this, submatrix_mul_equiv], simp only [matrix.mul_one, submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], end lemma one_submatrix_mul [fintype m] [fintype o] [non_assoc_semiring α] [decidable_eq o] (e₁ : l → o) (e₂ : m ≃ o) (M : matrix m n α) : ((1 : matrix o o α).submatrix e₁ e₂).mul M = submatrix M (e₂.symm ∘ e₁) id := begin let A := M.submatrix e₂.symm id, have : M = A.submatrix e₂ id, { simp only [submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], }, rw [this, submatrix_mul_equiv], simp only [matrix.one_mul, submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], end /-- The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence. -/ def reindex (eₘ : m ≃ l) (eₙ : n ≃ o) : matrix m n α ≃ matrix l o α := { to_fun := λ M, M.submatrix eₘ.symm eₙ.symm, inv_fun := λ M, M.submatrix eₘ eₙ, left_inv := λ M, by simp, right_inv := λ M, by simp, } @[simp] lemma reindex_apply (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : reindex eₘ eₙ M = M.submatrix eₘ.symm eₙ.symm := rfl @[simp] lemma reindex_refl_refl (A : matrix m n α) : reindex (equiv.refl _) (equiv.refl _) A = A := A.submatrix_id_id @[simp] lemma reindex_symm (eₘ : m ≃ l) (eₙ : n ≃ o) : (reindex eₘ eₙ).symm = (reindex eₘ.symm eₙ.symm : matrix l o α ≃ _) := rfl @[simp] lemma reindex_trans {l₂ o₂ : Type*} (eₘ : m ≃ l) (eₙ : n ≃ o) (eₘ₂ : l ≃ l₂) (eₙ₂ : o ≃ o₂) : (reindex eₘ eₙ).trans (reindex eₘ₂ eₙ₂) = (reindex (eₘ.trans eₘ₂) (eₙ.trans eₙ₂) : matrix m n α ≃ _) := equiv.ext $ λ A, (A.submatrix_submatrix eₘ.symm eₙ.symm eₘ₂.symm eₙ₂.symm : _) lemma transpose_reindex (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᵀ = (reindex eₙ eₘ Mᵀ) := rfl lemma conj_transpose_reindex [has_star α] (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᴴ = (reindex eₙ eₘ Mᴴ) := rfl @[simp] lemma submatrix_mul_transpose_submatrix [fintype m] [fintype n] [add_comm_monoid α] [has_mul α] (e : m ≃ n) (M : matrix m n α) : (M.submatrix id e) ⬝ (Mᵀ).submatrix e id = M ⬝ Mᵀ := by rw [submatrix_mul_equiv, submatrix_id_id] /-- The left `n × l` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := submatrix A id (fin.cast_add r) /-- The right `n × r` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := submatrix A id (fin.nat_add l) /-- The top `u × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := submatrix A (fin.cast_add d) id /-- The bottom `d × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := submatrix A (fin.nat_add u) id /-- The top-right `u × r` part of a `(u+d) × (l+r)` matrix. -/ @[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) /-- The bottom-right `d × r` part of a `(u+d) × (l+r)` matrix. -/ @[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) /-- The top-left `u × l` part of a `(u+d) × (l+r)` matrix. -/ @[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) /-- The bottom-left `d × l` part of a `(u+d) × (l+r)` matrix. -/ @[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) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [has_add α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [has_smul R α] (x : R) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [has_add α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [has_smul R α] (x : R) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma col_apply (v : m → α) (i j) : matrix.col v i j = v i := rfl @[simp] lemma row_apply (v : m → α) (i j) : matrix.row v i j = v j := rfl @[simp] lemma transpose_col (v : m → α) : (matrix.col v)ᵀ = matrix.row v := by { ext, refl } @[simp] lemma transpose_row (v : m → α) : (matrix.row v)ᵀ = matrix.col v := by { ext, refl } @[simp] lemma conj_transpose_col [has_star α] (v : m → α) : (col v)ᴴ = row (star v) := by { ext, refl } @[simp] lemma conj_transpose_row [has_star α] (v : m → α) : (row v)ᴴ = col (star v) := by { ext, refl } lemma row_vec_mul [fintype m] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [fintype m] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [fintype n] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [fintype n] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} @[simp] lemma row_mul_col_apply [fintype m] [has_mul α] [add_comm_monoid α] (v w : m → α) (i j) : (row v ⬝ col w) i j = v ⬝ᵥ w := rfl end row_col section update /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row [decidable_eq m] (M : matrix m n α) (i : m) (b : n → α) : matrix m n α := function.update M i b /-- Update, i.e. replace the `j`th column of matrix `A` with the values in `b`. -/ def update_column [decidable_eq n] (M : matrix m n α) (j : n) (b : m → α) : matrix m n α := λ i, function.update (M i) j (b i) variables {M : matrix m n α} {i : m} {j : n} {b : n → α} {c : m → α} @[simp] lemma update_row_self [decidable_eq m] : update_row M i b i = b := function.update_same i b M @[simp] lemma update_column_self [decidable_eq n] : update_column M j c i j = c i := function.update_same j (c i) (M i) @[simp] lemma update_row_ne [decidable_eq m] {i' : m} (i_ne : i' ≠ i) : update_row M i b i' = M i' := function.update_noteq i_ne b M @[simp] lemma update_column_ne [decidable_eq n] {j' : n} (j_ne : j' ≠ j) : update_column M j c i j' = M i j' := function.update_noteq j_ne (c i) (M i) lemma update_row_apply [decidable_eq m] {i' : m} : update_row M i b i' j = if i' = i then b j else M i' j := begin by_cases i' = i, { rw [h, update_row_self, if_pos rfl] }, { rwa [update_row_ne h, if_neg h] } end lemma update_column_apply [decidable_eq n] {j' : n} : update_column M j c i j' = if j' = j then c i else M i j' := begin by_cases j' = j, { rw [h, update_column_self, if_pos rfl] }, { rwa [update_column_ne h, if_neg h] } end @[simp] lemma update_column_subsingleton [subsingleton n] (A : matrix m n R) (i : n) (b : m → R) : A.update_column i b = (col b).submatrix id (function.const n ()) := begin ext x y, simp [update_column_apply, subsingleton.elim i y] end @[simp] lemma update_row_subsingleton [subsingleton m] (A : matrix m n R) (i : m) (b : n → R) : A.update_row i b = (row b).submatrix (function.const m ()) id := begin ext x y, simp [update_column_apply, subsingleton.elim i x] end lemma map_update_row [decidable_eq m] (f : α → β) : map (update_row M i b) f = update_row (M.map f) i (f ∘ b) := begin ext i' j', rw [update_row_apply, map_apply, map_apply, update_row_apply], exact apply_ite f _ _ _, end lemma map_update_column [decidable_eq n] (f : α → β) : map (update_column M j c) f = update_column (M.map f) j (f ∘ c) := begin ext i' j', rw [update_column_apply, map_apply, map_apply, update_column_apply], exact apply_ite f _ _ _, end lemma update_row_transpose [decidable_eq n] : update_row Mᵀ j c = (update_column M j c)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_column_transpose [decidable_eq m] : update_column Mᵀ i b = (update_row M i b)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_row_conj_transpose [decidable_eq n] [has_star α] : update_row Mᴴ j (star c) = (update_column M j c)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_row_transpose, map_update_column], refl, end lemma update_column_conj_transpose [decidable_eq m] [has_star α] : update_column Mᴴ i (star b) = (update_row M i b)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_column_transpose, map_update_row], refl, end @[simp] lemma update_row_eq_self [decidable_eq m] (A : matrix m n α) (i : m) : A.update_row i (A i) = A := function.update_eq_self i A @[simp] lemma update_column_eq_self [decidable_eq n] (A : matrix m n α) (i : n) : A.update_column i (λ j, A j i) = A := funext $ λ j, function.update_eq_self i (A j) lemma diagonal_update_column_single [decidable_eq n] [has_zero α] (v : n → α) (i : n) (x : α): (diagonal v).update_column i (pi.single i x) = diagonal (function.update v i x) := begin ext j k, obtain rfl | hjk := eq_or_ne j k, { rw [diagonal_apply_eq], obtain rfl | hji := eq_or_ne j i, { rw [update_column_self, pi.single_eq_same, function.update_same], }, { rw [update_column_ne hji, diagonal_apply_eq, function.update_noteq hji], } }, { rw [diagonal_apply_ne _ hjk], obtain rfl | hki := eq_or_ne k i, { rw [update_column_self, pi.single_eq_of_ne hjk] }, { rw [update_column_ne hki, diagonal_apply_ne _ hjk] } } end lemma diagonal_update_row_single [decidable_eq n] [has_zero α] (v : n → α) (i : n) (x : α): (diagonal v).update_row i (pi.single i x) = diagonal (function.update v i x) := by rw [←diagonal_transpose, update_row_transpose, diagonal_update_column_single, diagonal_transpose] end update end matrix namespace ring_hom variables [fintype n] [non_assoc_semiring α] [non_assoc_semiring β] lemma map_matrix_mul (M : matrix m n α) (N : matrix n o α) (i : m) (j : o) (f : α →+* β) : f (matrix.mul M N i j) = matrix.mul (M.map f) (N.map f) i j := by simp [matrix.mul_apply, ring_hom.map_sum] lemma map_dot_product [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (v w : n → R) : f (v ⬝ᵥ w) = (f ∘ v) ⬝ᵥ (f ∘ w) := by simp only [matrix.dot_product, f.map_sum, f.map_mul] lemma map_vec_mul [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (M : matrix n m R) (v : n → R) (i : m) : f (M.vec_mul v i) = ((M.map f).vec_mul (f ∘ v) i) := by simp only [matrix.vec_mul, matrix.map_apply, ring_hom.map_dot_product] lemma map_mul_vec [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (M : matrix m n R) (v : n → R) (i : m) : f (M.mul_vec v i) = ((M.map f).mul_vec (f ∘ v) i) := by simp only [matrix.mul_vec, matrix.map_apply, ring_hom.map_dot_product] end ring_hom
28c65dea1e159447851419da6071568466efa4ea
94e33a31faa76775069b071adea97e86e218a8ee
/src/linear_algebra/special_linear_group.lean
8db2dd55e160b252dec4c304245be10b15aee51e
[ "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
8,537
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import linear_algebra.matrix.adjugate import linear_algebra.matrix.to_lin /-! # The Special Linear group $SL(n, R)$ This file defines the elements of the Special Linear group `special_linear_group n R`, consisting of all square `R`-matrices with determinant `1` on the fintype `n` by `n`. In addition, we define the group structure on `special_linear_group n R` and the embedding into the general linear group `general_linear_group R (n → R)`. ## Main definitions * `matrix.special_linear_group` is the type of matrices with determinant 1 * `matrix.special_linear_group.group` gives the group structure (under multiplication) * `matrix.special_linear_group.to_GL` is the embedding `SLₙ(R) → GLₙ(R)` ## Notation For `m : ℕ`, we introduce the notation `SL(m,R)` for the special linear group on the fintype `n = fin m`, in the locale `matrix_groups`. ## Implementation notes The inverse operation in the `special_linear_group` is defined to be the adjugate matrix, so that `special_linear_group n R` has a group structure for all `comm_ring R`. We define the elements of `special_linear_group` to be matrices, since we need to compute their determinant. This is in contrast with `general_linear_group R M`, which consists of invertible `R`-linear maps on `M`. We provide `matrix.special_linear_group.has_coe_to_fun` for convenience, but do not state any lemmas about it, and use `matrix.special_linear_group.coe_fn_eq_coe` to eliminate it `⇑` in favor of a regular `↑` coercion. ## References * https://en.wikipedia.org/wiki/Special_linear_group ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map section variables (n : Type u) [decidable_eq n] [fintype n] (R : Type v) [comm_ring R] /-- `special_linear_group n R` is the group of `n` by `n` `R`-matrices with determinant equal to 1. -/ def special_linear_group := { A : matrix n n R // A.det = 1 } end localized "notation `SL(` n `,` R `)`:= matrix.special_linear_group (fin n) R" in matrix_groups namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_matrix : has_coe (special_linear_group n R) (matrix n n R) := ⟨λ A, A.val⟩ /- In this file, Lean often has a hard time working out the values of `n` and `R` for an expression like `det ↑A`. Rather than writing `(A : matrix n n R)` everywhere in this file which is annoyingly verbose, or `A.val` which is not the simp-normal form for subtypes, we create a local notation `↑ₘA`. This notation references the local `n` and `R` variables, so is not valid as a global notation. -/ local prefix `↑ₘ`:1024 := @coe _ (matrix n n R) _ lemma ext_iff (A B : special_linear_group n R) : A = B ↔ (∀ i j, ↑ₘA i j = ↑ₘB i j) := subtype.ext_iff.trans matrix.ext_iff.symm @[ext] lemma ext (A B : special_linear_group n R) : (∀ i j, ↑ₘA i j = ↑ₘB i j) → A = B := (special_linear_group.ext_iff A B).mpr instance has_inv : has_inv (special_linear_group n R) := ⟨λ A, ⟨adjugate A, by rw [det_adjugate, A.prop, one_pow]⟩⟩ instance has_mul : has_mul (special_linear_group n R) := ⟨λ A B, ⟨A.1 ⬝ B.1, by erw [det_mul, A.2, B.2, one_mul]⟩⟩ instance has_one : has_one (special_linear_group n R) := ⟨⟨1, det_one⟩⟩ instance : has_pow (special_linear_group n R) ℕ := { pow := λ x n, ⟨x ^ n, (det_pow _ _).trans $ x.prop.symm ▸ one_pow _⟩} instance : inhabited (special_linear_group n R) := ⟨1⟩ section coe_lemmas variables (A B : special_linear_group n R) @[simp] lemma coe_mk (A : matrix n n R) (h : det A = 1) : ↑(⟨A, h⟩ : special_linear_group n R) = A := rfl @[simp] lemma coe_inv : ↑ₘ(A⁻¹) = adjugate A := rfl @[simp] lemma coe_mul : ↑ₘ(A * B) = ↑ₘA ⬝ ↑ₘB := rfl @[simp] lemma coe_one : ↑ₘ(1 : special_linear_group n R) = (1 : matrix n n R) := rfl @[simp] lemma det_coe : det ↑ₘA = 1 := A.2 @[simp] lemma coe_pow (m : ℕ) : ↑ₘ(A ^ m) = ↑ₘA ^ m := rfl lemma det_ne_zero [nontrivial R] (g : special_linear_group n R) : det ↑ₘg ≠ 0 := by { rw g.det_coe, norm_num } lemma row_ne_zero [nontrivial R] (g : special_linear_group n R) (i : n): ↑ₘg i ≠ 0 := λ h, g.det_ne_zero $ det_eq_zero_of_row_eq_zero i $ by simp [h] end coe_lemmas instance : monoid (special_linear_group n R) := function.injective.monoid coe subtype.coe_injective coe_one coe_mul coe_pow instance : group (special_linear_group n R) := { mul_left_inv := λ A, by { ext1, simp [adjugate_mul] }, ..special_linear_group.monoid, ..special_linear_group.has_inv } /-- A version of `matrix.to_lin' A` that produces linear equivalences. -/ def to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R) := { to_fun := λ A, linear_equiv.of_linear (matrix.to_lin' ↑ₘA) (matrix.to_lin' ↑ₘ(A⁻¹)) (by rw [←to_lin'_mul, ←coe_mul, mul_right_inv, coe_one, to_lin'_one]) (by rw [←to_lin'_mul, ←coe_mul, mul_left_inv, coe_one, to_lin'_one]), map_one' := linear_equiv.to_linear_map_injective matrix.to_lin'_one, map_mul' := λ A B, linear_equiv.to_linear_map_injective $ matrix.to_lin'_mul A B } lemma to_lin'_apply (A : special_linear_group n R) (v : n → R) : special_linear_group.to_lin' A v = matrix.to_lin' ↑ₘA v := rfl lemma to_lin'_to_linear_map (A : special_linear_group n R) : ↑(special_linear_group.to_lin' A) = matrix.to_lin' ↑ₘA := rfl lemma to_lin'_symm_apply (A : special_linear_group n R) (v : n → R) : A.to_lin'.symm v = matrix.to_lin' ↑ₘ(A⁻¹) v := rfl lemma to_lin'_symm_to_linear_map (A : special_linear_group n R) : ↑(A.to_lin'.symm) = matrix.to_lin' ↑ₘ(A⁻¹) := rfl lemma to_lin'_injective : function.injective ⇑(to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R)) := λ A B h, subtype.coe_injective $ matrix.to_lin'.injective $ linear_equiv.to_linear_map_injective.eq_iff.mpr h /-- `to_GL` is the map from the special linear group to the general linear group -/ def to_GL : special_linear_group n R →* general_linear_group R (n → R) := (general_linear_group.general_linear_equiv _ _).symm.to_monoid_hom.comp to_lin' lemma coe_to_GL (A : special_linear_group n R) : ↑A.to_GL = A.to_lin'.to_linear_map := rfl variables {S : Type*} [comm_ring S] /-- A ring homomorphism from `R` to `S` induces a group homomorphism from `special_linear_group n R` to `special_linear_group n S`. -/ @[simps] def map (f : R →+* S) : special_linear_group n R →* special_linear_group n S := { to_fun := λ g, ⟨f.map_matrix ↑g, by { rw ← f.map_det, simp [g.2] }⟩, map_one' := subtype.ext $ f.map_matrix.map_one, map_mul' := λ x y, subtype.ext $ f.map_matrix.map_mul x y } section cast /-- Coercion of SL `n` `ℤ` to SL `n` `R` for a commutative ring `R`. -/ instance : has_coe (special_linear_group n ℤ) (special_linear_group n R) := ⟨λ x, map (int.cast_ring_hom R) x⟩ @[simp] lemma coe_matrix_coe (g : special_linear_group n ℤ) : ↑(g : special_linear_group n R) = (↑g : matrix n n ℤ).map (int.cast_ring_hom R) := map_apply_coe (int.cast_ring_hom R) g end cast section has_neg variables [fact (even (fintype.card n))] /-- Formal operation of negation on special linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (special_linear_group n R) := ⟨λ g, ⟨- g, by simpa [(fact.out $ even $ fintype.card n).neg_one_pow, g.det_coe] using det_smul ↑ₘg (-1)⟩⟩ @[simp] lemma coe_neg (g : special_linear_group n R) : ↑(- g) = - (g : matrix n n R) := rfl instance : has_distrib_neg (special_linear_group n R) := function.injective.has_distrib_neg _ subtype.coe_injective coe_neg coe_mul @[simp] lemma coe_int_neg (g : special_linear_group n ℤ) : ↑(-g) = (-↑g : special_linear_group n R) := subtype.ext $ (@ring_hom.map_matrix n _ _ _ _ _ _ (int.cast_ring_hom R)).map_neg ↑g end has_neg -- this section should be last to ensure we do not use it in lemmas section coe_fn_instance /-- This instance is here for convenience, but is not the simp-normal form. -/ instance : has_coe_to_fun (special_linear_group n R) (λ _, n → n → R) := { coe := λ A, A.val } @[simp] lemma coe_fn_eq_coe (s : special_linear_group n R) : ⇑s = ↑ₘs := rfl end coe_fn_instance end special_linear_group end matrix
87f299118e478225e1170a3eb0a00c6a9227e6b0
500f65bb93c499cd35c3254d894d762208cae042
/src/topology/uniform_space/cauchy.lean
7058b0c845db0d2d23e070f58bb86b9409b11c8a
[ "Apache-2.0" ]
permissive
PatrickMassot/mathlib
c39dc0ff18bbde42f1c93a1642f6e429adad538c
45df75b3c9da159fe3192fa7f769dfbec0bd6bda
refs/heads/master
1,623,168,646,390
1,566,940,765,000
1,566,940,765,000
115,220,590
0
1
null
1,514,061,524,000
1,514,061,524,000
null
UTF-8
Lean
false
false
16,554
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ import topology.uniform_space.basic open filter topological_space lattice set classical local attribute [instance, priority 0] prop_decidable variables {α : Type*} {β : Type*} [uniform_space α] universe u local notation `𝓤` := uniformity /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := f ≠ ⊥ ∧ filter.prod f f ≤ (𝓤 α) def is_complete (s : set α) := ∀f, cauchy f → f ≤ principal s → ∃x∈s, f ≤ nhds x lemma cauchy_iff {f : filter α} : cauchy f ↔ (f ≠ ⊥ ∧ (∀ s ∈ 𝓤 α, ∃t∈f.sets, set.prod t t ⊆ s)) := and_congr (iff.refl _) $ forall_congr $ assume s, forall_congr $ assume hs, mem_prod_same_iff lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (l ≠ ⊥ ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l.prod l) (𝓤 α)) := by rw [cauchy, (≠), map_eq_bot_iff, prod_map_map_eq]; refl lemma cauchy_downwards {f g : filter α} (h_c : cauchy f) (hg : g ≠ ⊥) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy_nhds {a : α} : cauchy (nhds a) := ⟨nhds_neq_bot, calc filter.prod (nhds a) (nhds a) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel 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_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ 𝓤 α : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_downwards cauchy_nhds (show principal {a} ≠ ⊥, by simp) (pure_le_nhds a) lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : f ⊓ nhds x ≠ ⊥) : f ≤ nhds x := have ∀s∈f.sets, x ∈ closure s, begin intros s hs, simp [closure_eq_nhds, inf_comm], exact assume h', adhs $ bot_unique $ h' ▸ inf_le_inf (by simp; exact hs) (le_refl _) end, calc f ≤ f.lift' (λs:set α, {y | x ∈ closure s ∧ y ∈ closure s}) : le_infi $ assume s, le_infi $ assume hs, begin rw [←forall_sets_neq_empty_iff_neq_bot] at adhs, simp [this s hs], exact mem_sets_of_superset hs subset_closure end ... ≤ f.lift' (λs:set α, {y | (x, y) ∈ closure (set.prod s s)}) : by simp [closure_prod_eq]; exact le_refl _ ... = (filter.prod f f).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : begin rw [prod_same_eq], rw [lift'_lift'_assoc], exact monotone_prod monotone_id monotone_id, exact monotone_comp (assume s t h x h', closure_mono h h') monotone_preimage end ... ≤ (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : lift'_mono hf.right (le_refl _) ... = ((𝓤 α).lift' closure).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : begin rw [lift'_lift'_assoc], exact assume s t h, closure_mono h, exact monotone_preimage end ... = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : by rw [←uniformity_eq_uniformity_closure] ... = nhds x : by rw [nhds_eq_uniformity] lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ nhds x ↔ f ⊓ nhds x ≠ ⊥ := ⟨assume h, (inf_of_le_left h).symm ▸ hf.left, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy_map [uniform_space β] {f : filter α} {m : α → β} (hm : uniform_continuous m) (hf : cauchy f) : cauchy (map m f) := ⟨have f ≠ ⊥, from hf.left, by simp; assumption, calc filter.prod (map m f) (map m f) = map (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy_comap [uniform_space β] {f : filter β} {m : α → β} (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hf : cauchy f) (hb : comap m f ≠ ⊥) : cauchy (comap m f) := ⟨hb, calc filter.prod (comap m f) (comap m f) = comap (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [inhabited β] [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq_iff_prod_map [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ map (prod.map u u) at_top ≤ 𝓤 α := iff.trans (and_iff_right (map_ne_bot at_top_ne_bot)) (prod_map_at_top_eq u u ▸ iff.rfl) /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ nhds x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, by simp, hx⟩ end lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} : cauchy f → cauchy g → cauchy (filter.prod f g) | ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_neq_bot.2 ⟨f_proper, g_proper⟩, let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β, by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap_comp, (∘), lattice.inf_assoc, lattice.inf_comm, lattice.inf_left_comm], lattice.inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩ instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ cauchy_map uniform_continuous_fst hf in let ⟨x2, hx2⟩ := complete_space.complete $ cauchy_map uniform_continuous_snd hf in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem_sets H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma cauchy_iff_exists_le_nhds [uniform_space α] [complete_space α] {l : filter α} (hl : l ≠ ⊥) : cauchy l ↔ (∃x, l ≤ nhds x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_downwards cauchy_nhds hl hx⟩ lemma cauchy_map_iff_exists_tendsto [uniform_space α] [complete_space α] {l : filter β} {f : β → α} (hl : l ≠ ⊥) : cauchy (l.map f) ↔ (∃x, tendsto f l (nhds x)) := cauchy_iff_exists_le_nhds (map_ne_bot hl) /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [inhabited β] [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (nhds x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [inhabited β] [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (nhds v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_sets_iff.2 ⟨univ, univ_mem_sets, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem le_nhds_lim_of_cauchy {α} [uniform_space α] [complete_space α] [inhabited α] {f : filter α} (hf : cauchy f) : f ≤ nhds (lim f) := lim_spec (complete_space.complete hf) lemma is_complete_of_is_closed [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_nhds.mp h x (neq_bot_of_le_neq_bot cf.left (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := finite_subset fk (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_subset [uniform_space α] {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty [uniform_space α] : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ lemma totally_bounded_closure [uniform_space α] {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : closure_eq_of_is_closed $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ lemma totally_bounded_image [uniform_space α] [uniform_space β] {f : α → β} {s : set α} (hf : uniform_continuous f) (hs : totally_bounded s) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, finite_image f hfc, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp [-mem_image], exact let ⟨i, hi, ht⟩ := hct x hx in ⟨f i, mem_image_of_mem f hi, ht⟩ end⟩ lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α} (hs : totally_bounded s) (hf : is_ultrafilter f) (h : f ≤ principal s) : cauchy f := ⟨hf.left, assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets, from mem_sets_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets, from mem_of_finite_Union_ultrafilter hf hi this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, (filter.prod f f).sets_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c) := ⟨assume : totally_bounded s, assume f hf hs, ⟨ultrafilter_of f, ultrafilter_of_le, cauchy_of_totally_bounded_of_ultrafilter this (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hs)⟩, assume h : ∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c, assume d hd, classical.by_contradiction $ assume hs, have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}), by simpa using hs, let f := ⨅t:{t : set α // finite t}, principal (s \ (⋃y∈t.val, {x | (x,y) ∈ d})), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h, hd_cover finite_empty $ h.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, finite_union ht₁ ht₂⟩, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inl, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inr⟩) (assume ⟨t, ht⟩, by simp [diff_eq_empty]; exact hd_cover ht), have f ≤ principal s, from infi_le_of_le ⟨∅, finite_empty⟩ $ by simp; exact subset.refl s, let ⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this, ⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α c d).mp $ hc₂.right hd in have c ≤ principal s, from le_trans ‹c ≤ f› this, have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this, let ⟨y, hym, hys⟩ := inhabited_of_mem_sets hc₂.left this in let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in have m ⊆ ys, from assume y' hy', show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}), by simp; exact @hmd (y', y) ⟨hy', hym⟩, have c ≤ principal (s - ys), from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_singleton _⟩ $ le_refl _, have (s - ys) ∩ (m ∩ s) ∈ c.sets, from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›, have ∅ ∈ c.sets, from c.sets_of_superset this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm, hc₂.left $ empty_in_sets_eq_bot.mp this⟩ lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f, is_ultrafilter f → f ≤ principal s → cauchy f) := ⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs, assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs, have cauchy (ultrafilter_of f), from h (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs), ⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩ lemma compact_iff_totally_bounded_complete {s : set α} : compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf1 hf2, let ⟨x, xs, fx⟩ := compact_iff_ultrafilter_le_nhds.1 hs f hf1 hf2 in cauchy_downwards (cauchy_nhds) (hf1.1) fx), λ f fc fs, let ⟨a, as, fa⟩ := hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, compact_iff_ultrafilter_le_nhds.2 (λf hf hfs, hc _ (totally_bounded_iff_ultrafilter.1 ht _ hf hfs) hfs)⟩ instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa [principal_univ] using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, is_complete_of_is_closed hc⟩
42a872b27d039dbdfccf84c2c357cd6e77d44a7d
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/finset/comb.lean
e03d3889d9841ecec267783af93b0e6ccfc3288b
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,886
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad Combinators for finite sets. -/ import data.finset.basic logic.identities open list quot subtype decidable perm function namespace finset /- image (corresponds to map on list) -/ section image variables {A B : Type} variable [h : decidable_eq B] include h definition image (f : A → B) (s : finset A) : finset B := quot.lift_on s (λ l, to_finset (list.map f (elt_of l))) (λ l₁ l₂ p, quot.sound (perm_erase_dup_of_perm (perm_map _ p))) notation [priority finset.prio] f `'[`:max a `]` := image f a theorem image_empty (f : A → B) : image f ∅ = ∅ := rfl theorem mem_image_of_mem (f : A → B) {s : finset A} {a : A} : a ∈ s → f a ∈ image f s := quot.induction_on s (take l, assume H : a ∈ elt_of l, mem_to_finset (mem_map f H)) theorem mem_image {f : A → B} {s : finset A} {a : A} {b : B} (H1 : a ∈ s) (H2 : f a = b) : b ∈ image f s := eq.subst H2 (mem_image_of_mem f H1) theorem exists_of_mem_image {f : A → B} {s : finset A} {b : B} : b ∈ image f s → ∃a, a ∈ s ∧ f a = b := quot.induction_on s (take l, assume H : b ∈ erase_dup (list.map f (elt_of l)), exists_of_mem_map (mem_of_mem_erase_dup H)) theorem mem_image_iff (f : A → B) {s : finset A} {y : B} : y ∈ image f s ↔ ∃x, x ∈ s ∧ f x = y := iff.intro exists_of_mem_image (assume H, obtain x (H₁ : x ∈ s) (H₂ : f x = y), from H, mem_image H₁ H₂) theorem mem_image_eq (f : A → B) {s : finset A} {y : B} : y ∈ image f s = ∃x, x ∈ s ∧ f x = y := propext (mem_image_iff f) theorem mem_image_of_mem_image_of_subset {f : A → B} {s t : finset A} {y : B} (H1 : y ∈ image f s) (H2 : s ⊆ t) : y ∈ image f t := obtain x (H3: x ∈ s) (H4 : f x = y), from exists_of_mem_image H1, have H5 : x ∈ t, from mem_of_subset_of_mem H2 H3, show y ∈ image f t, from mem_image H5 H4 theorem image_insert [h' : decidable_eq A] (f : A → B) (s : finset A) (a : A) : image f (insert a s) = insert (f a) (image f s) := ext (take y, iff.intro (assume H : y ∈ image f (insert a s), obtain x (H1l : x ∈ insert a s) (H1r :f x = y), from exists_of_mem_image H, have x = a ∨ x ∈ s, from eq_or_mem_of_mem_insert H1l, or.elim this (suppose x = a, have f a = y, from eq.subst this H1r, show y ∈ insert (f a) (image f s), from eq.subst this !mem_insert) (suppose x ∈ s, have f x ∈ image f s, from mem_image_of_mem f this, show y ∈ insert (f a) (image f s), from eq.subst H1r (mem_insert_of_mem _ this))) (suppose y ∈ insert (f a) (image f s), have y = f a ∨ y ∈ image f s, from eq_or_mem_of_mem_insert this, or.elim this (suppose y = f a, have f a ∈ image f (insert a s), from mem_image_of_mem f !mem_insert, show y ∈ image f (insert a s), from eq.subst (eq.symm `y = f a`) this) (suppose y ∈ image f s, show y ∈ image f (insert a s), from mem_image_of_mem_image_of_subset this !subset_insert))) lemma image_compose {C : Type} [deceqC : decidable_eq C] {f : B → C} {g : A → B} {s : finset A} : image (f∘g) s = image f (image g s) := ext (take z, iff.intro (suppose z ∈ image (f∘g) s, obtain x (Hx : x ∈ s) (Hgfx : f (g x) = z), from exists_of_mem_image this, by rewrite -Hgfx; apply mem_image_of_mem _ (mem_image_of_mem _ Hx)) (suppose z ∈ image f (image g s), obtain y (Hy : y ∈ image g s) (Hfy : f y = z), from exists_of_mem_image this, obtain x (Hx : x ∈ s) (Hgx : g x = y), from exists_of_mem_image Hy, mem_image Hx (by esimp; rewrite [Hgx, Hfy]))) lemma image_subset {a b : finset A} (f : A → B) (H : a ⊆ b) : f '[a] ⊆ f '[b] := subset_of_forall (take y, assume Hy : y ∈ f '[a], obtain x (Hx₁ : x ∈ a) (Hx₂ : f x = y), from exists_of_mem_image Hy, mem_image (mem_of_subset_of_mem H Hx₁) Hx₂) theorem image_union [h' : decidable_eq A] (f : A → B) (s t : finset A) : image f (s ∪ t) = image f s ∪ image f t := ext (take y, iff.intro (assume H : y ∈ image f (s ∪ t), obtain x [(xst : x ∈ s ∪ t) (fxy : f x = y)], from exists_of_mem_image H, or.elim (mem_or_mem_of_mem_union xst) (assume xs, mem_union_l (mem_image xs fxy)) (assume xt, mem_union_r (mem_image xt fxy))) (assume H : y ∈ image f s ∪ image f t, or.elim (mem_or_mem_of_mem_union H) (assume yifs : y ∈ image f s, obtain x [(xs : x ∈ s) (fxy : f x = y)], from exists_of_mem_image yifs, mem_image (mem_union_l xs) fxy) (assume yift : y ∈ image f t, obtain x [(xt : x ∈ t) (fxy : f x = y)], from exists_of_mem_image yift, mem_image (mem_union_r xt) fxy))) end image /- separation and set-builder notation -/ section sep variables {A : Type} [deceq : decidable_eq A] include deceq variables (p : A → Prop) [decp : decidable_pred p] (s : finset A) {x : A} include decp definition sep : finset A := quot.lift_on s (λl, to_finset_of_nodup (list.filter p (subtype.elt_of l)) (list.nodup_filter p (subtype.has_property l))) (λ l₁ l₂ u, quot.sound (perm.perm_filter u)) notation [priority finset.prio] `{` binder ` ∈ ` s ` | ` r:(scoped:1 p, sep p s) `}` := r theorem sep_empty : sep p ∅ = ∅ := rfl variables {p s} theorem of_mem_sep : x ∈ sep p s → p x := quot.induction_on s (take l, list.of_mem_filter) theorem mem_of_mem_sep : x ∈ sep p s → x ∈ s := quot.induction_on s (take l, list.mem_of_mem_filter) theorem mem_sep_of_mem {x : A} : x ∈ s → p x → x ∈ sep p s := quot.induction_on s (take l, list.mem_filter_of_mem) variables (p s) theorem mem_sep_iff : x ∈ sep p s ↔ x ∈ s ∧ p x := iff.intro (assume H, and.intro (mem_of_mem_sep H) (of_mem_sep H)) (assume H, mem_sep_of_mem (and.left H) (and.right H)) theorem mem_sep_eq : x ∈ sep p s = (x ∈ s ∧ p x) := propext !mem_sep_iff variable t : finset A theorem mem_sep_union_iff : x ∈ sep p (s ∪ t) ↔ x ∈ sep p s ∨ x ∈ sep p t := by rewrite [*mem_sep_iff, mem_union_iff, and.right_distrib] end sep section variables {A : Type} [deceqA : decidable_eq A] include deceqA theorem eq_sep_of_subset {s t : finset A} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := ext (take x, iff.intro (suppose x ∈ s, mem_sep_of_mem (mem_of_subset_of_mem ssubt this) this) (suppose x ∈ {x ∈ t | x ∈ s}, of_mem_sep this)) theorem mem_singleton_eq' (x a : A) : x ∈ '{a} = (x = a) := by rewrite [mem_insert_eq, mem_empty_eq, or_false] end /- set difference -/ section diff variables {A : Type} [deceq : decidable_eq A] include deceq definition diff (s t : finset A) : finset A := {x ∈ s | x ∉ t} infix [priority finset.prio] ` \ `:70 := diff theorem mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∈ s := mem_of_mem_sep H theorem not_mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∉ t := of_mem_sep H theorem mem_diff {s t : finset A} {x : A} (H1 : x ∈ s) (H2 : x ∉ t) : x ∈ s \ t := mem_sep_of_mem H1 H2 theorem mem_diff_iff (s t : finset A) (x : A) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.intro (assume H, and.intro (mem_of_mem_diff H) (not_mem_of_mem_diff H)) (assume H, mem_diff (and.left H) (and.right H)) theorem mem_diff_eq (s t : finset A) (x : A) : x ∈ s \ t = (x ∈ s ∧ x ∉ t) := propext !mem_diff_iff theorem union_diff_cancel {s t : finset A} (H : s ⊆ t) : s ∪ (t \ s) = t := ext (take x, iff.intro (suppose x ∈ s ∪ (t \ s), or.elim (mem_or_mem_of_mem_union this) (suppose x ∈ s, mem_of_subset_of_mem H this) (suppose x ∈ t \ s, mem_of_mem_diff this)) (suppose x ∈ t, decidable.by_cases (suppose x ∈ s, mem_union_left _ this) (suppose x ∉ s, mem_union_right _ (mem_diff `x ∈ t` this)))) theorem diff_union_cancel {s t : finset A} (H : s ⊆ t) : (t \ s) ∪ s = t := eq.subst !union.comm (!union_diff_cancel H) end diff /- set complement -/ section complement variables {A : Type} [deceqA : decidable_eq A] [h : fintype A] include deceqA h definition complement (s : finset A) : finset A := univ \ s prefix [priority finset.prio] - := complement theorem mem_complement {s : finset A} {x : A} (H : x ∉ s) : x ∈ -s := mem_diff !mem_univ H theorem not_mem_of_mem_complement {s : finset A} {x : A} (H : x ∈ -s) : x ∉ s := not_mem_of_mem_diff H theorem mem_complement_iff (s : finset A) (x : A) : x ∈ -s ↔ x ∉ s := iff.intro not_mem_of_mem_complement mem_complement section open classical theorem union_eq_comp_comp_inter_comp (s t : finset A) : s ∪ t = -(-s ∩ -t) := ext (take x, by rewrite [mem_union_iff, mem_complement_iff, mem_inter_iff, *mem_complement_iff, or_iff_not_and_not]) theorem inter_eq_comp_comp_union_comp (s t : finset A) : s ∩ t = -(-s ∪ -t) := ext (take x, by rewrite [mem_inter_iff, mem_complement_iff, mem_union_iff, *mem_complement_iff, and_iff_not_or_not]) end end complement /- all -/ section all variables {A : Type} definition all (s : finset A) (p : A → Prop) : Prop := quot.lift_on s (λ l, all (elt_of l) p) (λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !and.left_comm) p true) theorem all_empty (p : A → Prop) : all ∅ p = true := rfl theorem of_mem_of_all {p : A → Prop} {a : A} {s : finset A} : a ∈ s → all s p → p a := quot.induction_on s (λ l i h, list.of_mem_of_all i h) theorem forall_of_all {p : A → Prop} {s : finset A} (H : all s p) : ∀{a}, a ∈ s → p a := λ a H', of_mem_of_all H' H theorem all_of_forall {p : A → Prop} {s : finset A} : (∀a, a ∈ s → p a) → all s p := quot.induction_on s (λ l H, list.all_of_forall H) theorem all_iff_forall (p : A → Prop) (s : finset A) : all s p ↔ (∀a, a ∈ s → p a) := iff.intro forall_of_all all_of_forall definition decidable_all [instance] (p : A → Prop) [h : decidable_pred p] (s : finset A) : decidable (all s p) := quot.rec_on_subsingleton s (λ l, list.decidable_all p (elt_of l)) theorem all_implies {p q : A → Prop} {s : finset A} : all s p → (∀ x, p x → q x) → all s q := quot.induction_on s (λ l h₁ h₂, list.all_implies h₁ h₂) variable [h : decidable_eq A] include h theorem all_union {p : A → Prop} {s₁ s₂ : finset A} : all s₁ p → all s₂ p → all (s₁ ∪ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a₁ a₂, all_union a₁ a₂) theorem all_of_all_union_left {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₁ p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_left a) theorem all_of_all_union_right {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₂ p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_right a) theorem all_insert_of_all {p : A → Prop} {a : A} {s : finset A} : p a → all s p → all (insert a s) p := quot.induction_on s (λ l h₁ h₂, list.all_insert_of_all h₁ h₂) theorem all_erase_of_all {p : A → Prop} (a : A) {s : finset A}: all s p → all (erase a s) p := quot.induction_on s (λ l h, list.all_erase_of_all a h) theorem all_inter_of_all_left {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₁ p → all (s₁ ∩ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_left _ h) theorem all_inter_of_all_right {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₂ p → all (s₁ ∩ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_right _ h) theorem subset_iff_all (s t : finset A) : s ⊆ t ↔ all s (λ x, x ∈ t) := iff.intro (suppose s ⊆ t, all_of_forall (take x, suppose x ∈ s, mem_of_subset_of_mem `s ⊆ t` `x ∈ s`)) (suppose all s (λ x, x ∈ t), subset_of_forall (take x, suppose x ∈ s, of_mem_of_all `x ∈ s` `all s (λ x, x ∈ t)`)) definition decidable_subset [instance] (s t : finset A) : decidable (s ⊆ t) := decidable_of_decidable_of_iff _ (iff.symm !subset_iff_all) end all /- any -/ section any variables {A : Type} definition any (s : finset A) (p : A → Prop) : Prop := quot.lift_on s (λ l, any (elt_of l) p) (λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !or.left_comm) p false) theorem any_empty (p : A → Prop) : any ∅ p = false := rfl theorem exists_of_any {p : A → Prop} {s : finset A} : any s p → ∃a, a ∈ s ∧ p a := quot.induction_on s (λ l H, list.exists_of_any H) theorem any_of_mem {p : A → Prop} {s : finset A} {a : A} : a ∈ s → p a → any s p := quot.induction_on s (λ l H1 H2, list.any_of_mem H1 H2) theorem any_of_exists {p : A → Prop} {s : finset A} (H : ∃a, a ∈ s ∧ p a) : any s p := obtain a H₁ H₂, from H, any_of_mem H₁ H₂ theorem any_iff_exists (p : A → Prop) (s : finset A) : any s p ↔ (∃a, a ∈ s ∧ p a) := iff.intro exists_of_any any_of_exists theorem any_of_insert [h : decidable_eq A] {p : A → Prop} (s : finset A) {a : A} (H : p a) : any (insert a s) p := any_of_mem (mem_insert a s) H theorem any_of_insert_right [h : decidable_eq A] {p : A → Prop} {s : finset A} (a : A) (H : any s p) : any (insert a s) p := obtain b (H₁ : b ∈ s) (H₂ : p b), from exists_of_any H, any_of_mem (mem_insert_of_mem a H₁) H₂ definition decidable_any [instance] (p : A → Prop) [h : decidable_pred p] (s : finset A) : decidable (any s p) := quot.rec_on_subsingleton s (λ l, list.decidable_any p (elt_of l)) end any section product variables {A B : Type} definition product (s₁ : finset A) (s₂ : finset B) : finset (A × B) := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, to_finset_of_nodup (product (elt_of l₁) (elt_of l₂)) (nodup_product (has_property l₁) (has_property l₂))) (λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_product p₁ p₂)) infix [priority finset.prio] * := product theorem empty_product (s : finset B) : @empty A * s = ∅ := quot.induction_on s (λ l, rfl) theorem mem_product {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : a ∈ s₁ → b ∈ s₂ → (a, b) ∈ s₁ * s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i₁ i₂, list.mem_product i₁ i₂) theorem mem_of_mem_product_left {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : (a, b) ∈ s₁ * s₂ → a ∈ s₁ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_left i) theorem mem_of_mem_product_right {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : (a, b) ∈ s₁ * s₂ → b ∈ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_right i) theorem product_empty (s : finset A) : s * @empty B = ∅ := ext (λ p, match p with | (a, b) := iff.intro (λ i, absurd (mem_of_mem_product_right i) !not_mem_empty) (λ i, absurd i !not_mem_empty) end) end product /- powerset -/ section powerset variables {A : Type} [deceqA : decidable_eq A] include deceqA section list_powerset open list definition list_powerset : list A → finset (finset A) | [] := '{∅} | (a :: l) := list_powerset l ∪ image (insert a) (list_powerset l) end list_powerset private theorem image_insert_comm (a b : A) (s : finset (finset A)) : image (insert a) (image (insert b) s) = image (insert b) (image (insert a) s) := have aux' : ∀ a b : A, ∀ x : finset A, x ∈ image (insert a) (image (insert b) s) → x ∈ image (insert b) (image (insert a) s), from begin intros [a, b, x, H], cases (exists_of_mem_image H) with [y, Hy], cases Hy with [Hy1, Hy2], cases (exists_of_mem_image Hy1) with [z, Hz], cases Hz with [Hz1, Hz2], substvars, rewrite insert.comm, repeat (apply mem_image_of_mem), assumption end, ext (take x, iff.intro (aux' a b x) (aux' b a x)) theorem list_powerset_eq_list_powerset_of_perm {l₁ l₂ : list A} (p : l₁ ~ l₂) : list_powerset l₁ = list_powerset l₂ := perm.induction_on p rfl (λ x l₁ l₂ p ih, by rewrite [↑list_powerset, ih]) (λ x y l, by rewrite [↑list_powerset, ↑list_powerset, *image_union, image_insert_comm, *union.assoc, union.left_comm (finset.image (finset.insert x) _)]) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) definition powerset (s : finset A) : finset (finset A) := quot.lift_on s (λ l, list_powerset (elt_of l)) (λ l₁ l₂ p, list_powerset_eq_list_powerset_of_perm p) prefix [priority finset.prio] `𝒫`:100 := powerset theorem powerset_empty : 𝒫 (∅ : finset A) = '{∅} := rfl theorem powerset_insert {a : A} {s : finset A} : a ∉ s → 𝒫 (insert a s) = 𝒫 s ∪ image (insert a) (𝒫 s) := quot.induction_on s (λ l, assume H : a ∉ quot.mk l, calc 𝒫 (insert a (quot.mk l)) = list_powerset (list.insert a (elt_of l)) : rfl ... = list_powerset (#list a :: elt_of l) : by rewrite [list.insert_eq_of_not_mem H] ... = 𝒫 (quot.mk l) ∪ image (insert a) (𝒫 (quot.mk l)) : rfl) theorem mem_powerset_iff_subset (s : finset A) : ∀ x, x ∈ 𝒫 s ↔ x ⊆ s := begin induction s with a s nains ih, intro x, rewrite powerset_empty, show x ∈ '{∅} ↔ x ⊆ ∅, by rewrite [mem_singleton_eq', subset_empty_iff], intro x, rewrite [powerset_insert nains, mem_union_iff, ih, mem_image_iff], exact (iff.intro (assume H, or.elim H (suppose x ⊆ s, subset.trans this !subset_insert) (suppose ∃ y, y ∈ 𝒫 s ∧ insert a y = x, obtain y [yps iay], from this, show x ⊆ insert a s, begin rewrite [-iay], apply insert_subset_insert, rewrite -ih, apply yps end)) (assume H : x ⊆ insert a s, assert H' : erase a x ⊆ s, from erase_subset_of_subset_insert H, decidable.by_cases (suppose a ∈ x, or.inr (exists.intro (erase a x) (and.intro (show erase a x ∈ 𝒫 s, by rewrite ih; apply H') (show insert a (erase a x) = x, from insert_erase this)))) (suppose a ∉ x, or.inl (show x ⊆ s, by rewrite [(erase_eq_of_not_mem this) at H']; apply H')))) end theorem subset_of_mem_powerset {s t : finset A} (H : s ∈ 𝒫 t) : s ⊆ t := iff.mp (mem_powerset_iff_subset t s) H theorem mem_powerset_of_subset {s t : finset A} (H : s ⊆ t) : s ∈ 𝒫 t := iff.mpr (mem_powerset_iff_subset t s) H theorem empty_mem_powerset (s : finset A) : ∅ ∈ 𝒫 s := mem_powerset_of_subset (empty_subset s) end powerset end finset
a340b10a8f9557ab66371ae568edc465ff6a7e72
592f865bb4e2e537ae3552cb83c32fb99c8d4c68
/src/to_qShannon_theory_maybe/state.lean
fa723cd550326f4dbb758824bae327eaf35f501e
[]
no_license
BassemSafieldeen/Entropy_and_reversible_catalysis
250dbb9446690ab89d89a6a512c8f888e09e8596
5dd6ee062f61e26bbcf254477e3e24aa3fc489af
refs/heads/master
1,678,747,213,156
1,615,540,586,000
1,615,540,586,000
322,216,014
0
0
null
null
null
null
UTF-8
Lean
false
false
1,427
lean
import linear_algebra.eigenspace import to_mathlib_maybe.Hilbert_space open_locale tensor_product big_operators variables {ℋ : Type} [complex_hilbert_space ℋ] /-- A quantum state is a linear map from a Hilbert space to itself -- a.k.a., a linear operator, a.k.a, a module endomorphism -- with trace one and only non-negative eigenvalues. Definition 4.1.3 here (https://arxiv.org/pdf/1106.1445.pdf). -/ class quantum_state (ρ : module.End ℂ ℋ) := (trace_one : Tr ρ = 1) (pos_semidef : ∀ e, ρ.has_eigenvalue e → (e.im = 0) ∧ (e.re ≥ 0)) -- Alternatively, we could define this more abstractly -- (hermitian : ∀ x y : ℋ, inner x (ρ y) = inner (ρ x) y) -- (pos_semidef : ∀ x : ℋ, inner x (ρ x) ≥ 0) /-- The list of eigenvalues of a quantum state. -/ def spectrum (ρ : module.End ℂ ℋ) [quantum_state ρ] : list ℝ := sorry /-- Trace distance -/ def trace_distance (ρ : module.End ℂ ℋ) (σ : module.End ℂ ℋ) : ℝ := 1/2 * ∑ e ∈ spectrum (ρ-σ), abs e notation `D(`ρ`,`σ`)` := trace_distance ρ σ universe x variables {ι : Type x} -- indexing type [fintype ι] -- tell Lean that the set of all elements ι is finite. (ρ : module.End ℂ ℋ) {e : ι → ℂ} [∀ i, ρ.has_eigenvalue (e i)] /-- If a linear operator has trace one then its eigenvalues sum to 1. -/ lemma sum_eigenvalues_eq_one_of_trace_one : Tr ρ = 1 → ∑ i, e i = 1 := begin sorry end
202aaff2dd12e533683ef7ecb839aa0db11e78b0
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/measure_theory/l1_space.lean
a3826e109b7a74ce85cc0945c929fa7280b058cb
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
28,704
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.ae_eq_fun /-! # Integrable functions and `L¹` space In the first part of this file, the predicate `integrable` is defined and basic properties of integrable functions are proved. In the second part, the space `L¹` of equivalence classes of integrable functions under the relation of being almost everywhere equal is defined as a subspace of the space `L⁰`. See the file `src/measure_theory/ae_eq_fun.lean` for information on `L⁰` space. ## Notation * `α →₁ β` is the type of `L¹` space, where `α` is a `measure_space` and `β` is a `normed_group` with a `second_countable_topology`. `f : α →ₘ β` is a "function" in `L¹`. In comments, `[f]` is also used to denote an `L¹` function. `₁` can be typed as `\1`. ## Main definitions * Let `f : α → β` be a function, where `α` is a `measure_space` and `β` a `normed_group`. Then `f` is called `integrable` if `(∫⁻ a, nnnorm (f a)) < ⊤` holds. * The space `L¹` is defined as a subspace of `L⁰` : An `ae_eq_fun` `[f] : α →ₘ β` is in the space `L¹` if `edist [f] 0 < ⊤`, which means `(∫⁻ a, edist (f a) 0) < ⊤` if we expand the definition of `edist` in `L⁰`. ## Main statements `L¹`, as a subspace, inherits most of the structures of `L⁰`. ## Implementation notes Maybe `integrable f` should be mean `(∫⁻ a, edist (f a) 0) < ⊤`, so that `integrable` and `ae_eq_fun.integrable` are more aligned. But in the end one can use the lemma `lintegral_nnnorm_eq_lintegral_edist : (∫⁻ a, nnnorm (f a)) = (∫⁻ a, edist (f a) 0)` to switch the two forms. ## Tags integrable, function space, l1 -/ noncomputable theory open_locale classical topological_space namespace measure_theory open set filter topological_space ennreal emetric universes u v w variables {α : Type u} [measure_space α] variables {β : Type v} [normed_group β] {γ : Type w} [normed_group γ] /-- A function is `integrable` if the integral of its pointwise norm is less than infinity. -/ def integrable (f : α → β) : Prop := (∫⁻ a, nnnorm (f a)) < ⊤ lemma integrable_iff_norm (f : α → β) : integrable f ↔ (∫⁻ a, ennreal.of_real ∥f a∥) < ⊤ := by simp only [integrable, of_real_norm_eq_coe_nnnorm] lemma integrable_iff_edist (f : α → β) : integrable f ↔ (∫⁻ a, edist (f a) 0) < ⊤ := have eq : (λa, edist (f a) 0) = (λa, (nnnorm(f a) : ennreal)), by { funext, rw edist_eq_coe_nnnorm }, iff.intro (by { rw eq, exact λh, h }) $ by { rw eq, exact λh, h } lemma integrable_iff_of_real {f : α → ℝ} (h : ∀ₘ a, 0 ≤ f a) : integrable f ↔ (∫⁻ a, ennreal.of_real (f a)) < ⊤ := have lintegral_eq : (∫⁻ a, ennreal.of_real ∥f a∥) = (∫⁻ a, ennreal.of_real (f a)) := begin apply lintegral_congr_ae, filter_upwards [h], simp only [mem_set_of_eq], assume a h, rw [real.norm_eq_abs, abs_of_nonneg], exact h end, by rw [integrable_iff_norm, lintegral_eq] lemma integrable_of_ae_eq {f g : α → β} (hf : integrable f) (h : ∀ₘ a, f a = g a) : integrable g := begin simp only [integrable] at *, have : (∫⁻ (a : α), ↑(nnnorm (f a))) = (∫⁻ (a : α), ↑(nnnorm (g a))), { apply lintegral_congr_ae, filter_upwards [h], assume a, simp only [mem_set_of_eq], assume h, rw h }, rwa ← this end lemma integrable_congr_ae {f g : α → β} (h : ∀ₘ a, f a = g a) : integrable f ↔ integrable g := iff.intro (λhf, integrable_of_ae_eq hf h) (λhg, integrable_of_ae_eq hg (all_ae_eq_symm h)) lemma integrable_of_le_ae {f : α → β} {g : α → γ} (h : ∀ₘ a, ∥f a∥ ≤ ∥g a∥) (hg : integrable g) : integrable f := begin simp only [integrable_iff_norm] at *, calc (∫⁻ a, ennreal.of_real ∥f a∥) ≤ (∫⁻ (a : α), ennreal.of_real ∥g a∥) : lintegral_le_lintegral_ae (by { filter_upwards [h], assume a h, exact of_real_le_of_real h }) ... < ⊤ : hg end lemma integrable_of_le {f : α → β} {g : α → γ} (h : ∀a, ∥f a∥ ≤ ∥g a∥) (hg : integrable g) : integrable f := integrable_of_le_ae (all_ae_of_all h) hg lemma lintegral_nnnorm_eq_lintegral_edist (f : α → β) : (∫⁻ a, nnnorm (f a)) = ∫⁻ a, edist (f a) 0 := by simp only [edist_eq_coe_nnnorm] lemma lintegral_norm_eq_lintegral_edist (f : α → β) : (∫⁻ a, ennreal.of_real ∥f a∥) = ∫⁻ a, edist (f a) 0 := by simp only [of_real_norm_eq_coe_nnnorm, edist_eq_coe_nnnorm] lemma lintegral_edist_triangle [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g h : α → β} (hf : measurable f) (hg : measurable g) (hh : measurable h) : (∫⁻ a, edist (f a) (g a)) ≤ (∫⁻ a, edist (f a) (h a)) + ∫⁻ a, edist (g a) (h a) := begin rw ← lintegral_add (hf.edist hh) (hg.edist hh), apply lintegral_mono, assume a, have := edist_triangle (f a) (h a) (g a), convert this, rw edist_comm (h a) (g a), end lemma lintegral_edist_lt_top [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) : (∫⁻ a, edist (f a) (g a)) < ⊤ := lt_of_le_of_lt (lintegral_edist_triangle hfm hgm (measurable_const : measurable (λa, (0 : β)))) (ennreal.add_lt_top.2 $ by { split; rw ← integrable_iff_edist; assumption }) lemma lintegral_nnnorm_zero : (∫⁻ a : α, nnnorm (0 : β)) = 0 := by simp variables (α β) @[simp] lemma integrable_zero : integrable (λa:α, (0:β)) := by simp [integrable] variables {α β} lemma lintegral_nnnorm_add [measurable_space β] [opens_measurable_space β] [measurable_space γ] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : (∫⁻ a, nnnorm (f a) + nnnorm (g a)) = (∫⁻ a, nnnorm (f a)) + ∫⁻ a, nnnorm (g a) := lintegral_add hf.ennnorm hg.ennnorm lemma integrable.add [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) : integrable (λa, f a + g a) := calc (∫⁻ (a : α), ↑(nnnorm ((f + g) a))) ≤ ∫⁻ (a : α), ↑(nnnorm (f a)) + ↑(nnnorm (g a)) : lintegral_mono (assume a, by { simp only [← coe_add, coe_le_coe], exact nnnorm_add_le _ _ }) ... = _ : lintegral_nnnorm_add hfm hgm ... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩ lemma integrable_finset_sum {ι} [measurable_space β] [borel_space β] [second_countable_topology β] (s : finset ι) {f : ι → α → β} (hfm : ∀ i, measurable (f i)) (hfi : ∀ i, integrable (f i)) : integrable (λ a, s.sum (λ i, f i a)) := begin refine finset.induction_on s _ _, { simp only [finset.sum_empty, integrable_zero] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], refine (hfi _).add (hfm _) (s.measurable_sum hfm) ih } end lemma lintegral_nnnorm_neg {f : α → β} : (∫⁻ (a : α), ↑(nnnorm ((-f) a))) = ∫⁻ (a : α), ↑(nnnorm ((f) a)) := by simp only [pi.neg_apply, nnnorm_neg] lemma integrable.neg {f : α → β} : integrable f → integrable (λa, -f a) := assume hfi, calc _ = _ : lintegral_nnnorm_neg ... < ⊤ : hfi @[simp] lemma integrable_neg_iff (f : α → β) : integrable (λa, -f a) ↔ integrable f := begin split, { assume h, simpa only [_root_.neg_neg] using h.neg }, exact integrable.neg end lemma integrable.sub [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) : integrable (λa, f a - g a) := calc (∫⁻ (a : α), ↑(nnnorm ((f - g) a))) ≤ ∫⁻ (a : α), ↑(nnnorm (f a)) + ↑(nnnorm (-g a)) : lintegral_mono (assume a, by { simp only [← coe_add, coe_le_coe], exact nnnorm_add_le _ _ }) ... = _ : by { simp only [nnnorm_neg], exact lintegral_nnnorm_add hfm hgm } ... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩ lemma integrable.norm {f : α → β} (hfi : integrable f) : integrable (λa, ∥f a∥) := have eq : (λa, (nnnorm ∥f a∥ : ennreal)) = λa, (nnnorm (f a) : ennreal), by { funext, rw nnnorm_norm }, by { rwa [integrable, eq] } lemma integrable_norm_iff (f : α → β) : integrable (λa, ∥f a∥) ↔ integrable f := have eq : (λa, (nnnorm ∥f a∥ : ennreal)) = λa, (nnnorm (f a) : ennreal), by { funext, rw nnnorm_norm }, by { rw [integrable, integrable, eq] } lemma integrable_of_integrable_bound {f : α → β} {bound : α → ℝ} (h : integrable bound) (h_bound : ∀ₘ a, ∥f a∥ ≤ bound a) : integrable f := have h₁ : ∀ₘ a, (nnnorm (f a) : ennreal) ≤ ennreal.of_real (bound a), begin filter_upwards [h_bound], simp only [mem_set_of_eq], assume a h, calc (nnnorm (f a) : ennreal) = ennreal.of_real (∥f a∥) : by rw of_real_norm_eq_coe_nnnorm ... ≤ ennreal.of_real (bound a) : ennreal.of_real_le_of_real h end, calc (∫⁻ a, nnnorm (f a)) ≤ (∫⁻ a, ennreal.of_real (bound a)) : by { apply lintegral_le_lintegral_ae, exact h₁ } ... ≤ (∫⁻ a, ennreal.of_real ∥bound a∥) : lintegral_mono $ by { assume a, apply ennreal.of_real_le_of_real, exact le_max_left (bound a) (-bound a) } ... < ⊤ : by { rwa [integrable_iff_norm] at h } section dominated_convergence variables {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} lemma all_ae_of_real_F_le_bound (h : ∀ n, ∀ₘ a, ∥F n a∥ ≤ bound a) : ∀ n, ∀ₘ a, ennreal.of_real ∥F n a∥ ≤ ennreal.of_real (bound a) := λn, by filter_upwards [h n] λ a h, ennreal.of_real_le_of_real h lemma all_ae_tendsto_of_real_norm (h : ∀ₘ a, tendsto (λ n, F n a) at_top $ 𝓝 $ f a) : ∀ₘ a, tendsto (λn, ennreal.of_real ∥F n a∥) at_top $ 𝓝 $ ennreal.of_real ∥f a∥ := by filter_upwards [h] λ a h, tendsto_of_real $ tendsto.comp (continuous.tendsto continuous_norm _) h lemma all_ae_of_real_f_le_bound (h_bound : ∀ n, ∀ₘ a, ∥F n a∥ ≤ bound a) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (𝓝 (f a))) : ∀ₘ a, ennreal.of_real ∥f a∥ ≤ ennreal.of_real (bound a) := begin have F_le_bound := all_ae_of_real_F_le_bound h_bound, rw ← all_ae_all_iff at F_le_bound, apply F_le_bound.mp ((all_ae_tendsto_of_real_norm h_lim).mono _), assume a tendsto_norm F_le_bound, exact le_of_tendsto' at_top_ne_bot tendsto_norm (F_le_bound) end lemma integrable_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (bound_integrable : integrable bound) (h_bound : ∀ n, ∀ₘ a, ∥F n a∥ ≤ bound a) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (𝓝 (f a))) : integrable f := /- `∥F n a∥ ≤ bound a` and `∥F n a∥ --> ∥f a∥` implies `∥f a∥ ≤ bound a`, and so `∫ ∥f∥ ≤ ∫ bound < ⊤` since `bound` is integrable -/ begin rw integrable_iff_norm, calc (∫⁻ a, (ennreal.of_real ∥f a∥)) ≤ ∫⁻ a, ennreal.of_real (bound a) : lintegral_le_lintegral_ae $ all_ae_of_real_f_le_bound h_bound h_lim ... < ⊤ : begin rw ← integrable_iff_of_real, { exact bound_integrable }, filter_upwards [h_bound 0] λ a h, le_trans (norm_nonneg _) h, end end lemma tendsto_lintegral_norm_of_dominated_convergence [measurable_space β] [borel_space β] [second_countable_topology β] {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (F_measurable : ∀ n, measurable (F n)) (f_measurable : measurable f) (bound_integrable : integrable bound) (h_bound : ∀ n, ∀ₘ a, ∥F n a∥ ≤ bound a) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0) := let b := λa, 2 * ennreal.of_real (bound a) in /- `∥F n a∥ ≤ bound a` and `F n a --> f a` implies `∥f a∥ ≤ bound a`, and thus by the triangle inequality, have `∥F n a - f a∥ ≤ 2 * (bound a). -/ have hb : ∀ n, ∀ₘ a, ennreal.of_real ∥F n a - f a∥ ≤ b a, begin assume n, filter_upwards [all_ae_of_real_F_le_bound h_bound n, all_ae_of_real_f_le_bound h_bound h_lim], assume a h₁ h₂, calc ennreal.of_real ∥F n a - f a∥ ≤ (ennreal.of_real ∥F n a∥) + (ennreal.of_real ∥f a∥) : begin rw [← ennreal.of_real_add], apply of_real_le_of_real, { apply norm_sub_le }, { exact norm_nonneg _ }, { exact norm_nonneg _ } end ... ≤ (ennreal.of_real (bound a)) + (ennreal.of_real (bound a)) : add_le_add' h₁ h₂ ... = b a : by rw ← two_mul end, /- On the other hand, `F n a --> f a` implies that `∥F n a - f a∥ --> 0` -/ have h : ∀ₘ a, tendsto (λ n, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0), begin suffices h : ∀ₘ a, tendsto (λ n, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 $ ennreal.of_real 0), { rwa ennreal.of_real_zero at h }, filter_upwards [h_lim], assume a h, refine tendsto.comp (continuous.tendsto continuous_of_real _) _, rw ← tendsto_iff_norm_tendsto_zero, exact h end, /- Therefore, by the dominated convergence theorem for nonnegative integration, have ` ∫ ∥f a - F n a∥ --> 0 ` -/ begin suffices h : tendsto (λn, ∫⁻ a, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 (∫⁻ (a:α), 0)), { rwa lintegral_zero at h }, -- Using the dominated convergence theorem. refine tendsto_lintegral_of_dominated_convergence _ _ hb _ _, -- Show `λa, ∥f a - F n a∥` is measurable for all `n` { exact λn, measurable_of_real.comp ((F_measurable n).sub f_measurable).norm }, -- Show `2 * bound` is integrable { rw integrable_iff_of_real at bound_integrable, { calc (∫⁻ a, b a) = 2 * (∫⁻ a, ennreal.of_real (bound a)) : by { rw lintegral_const_mul', exact coe_ne_top } ... < ⊤ : mul_lt_top (coe_lt_top) bound_integrable }, filter_upwards [h_bound 0] λ a h, le_trans (norm_nonneg _) h }, -- Show `∥f a - F n a∥ --> 0` { exact h } end end dominated_convergence section pos_part /-! Lemmas used for defining the positive part of a `L¹` function -/ lemma integrable.max_zero {f : α → ℝ} (hf : integrable f) : integrable (λa, max (f a) 0) := begin simp only [integrable_iff_norm] at *, calc (∫⁻ a, ennreal.of_real ∥max (f a) 0∥) ≤ (∫⁻ (a : α), ennreal.of_real ∥f a∥) : lintegral_mono begin assume a, apply of_real_le_of_real, simp only [real.norm_eq_abs], calc abs (max (f a) 0) = max (f a) 0 : by { rw abs_of_nonneg, apply le_max_right } ... ≤ abs (f a) : max_le (le_abs_self _) (abs_nonneg _) end ... < ⊤ : hf end lemma integrable.min_zero {f : α → ℝ} (hf : integrable f) : integrable (λa, min (f a) 0) := begin have : (λa, min (f a) 0) = (λa, - max (-f a) 0), { funext, rw [min_eq_neg_max_neg_neg, neg_zero] }, rw this, exact (integrable.max_zero hf.neg).neg, end end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul (c : 𝕜) {f : α → β} : integrable f → integrable (λa, c • f a) := begin simp only [integrable], assume hfi, calc (∫⁻ (a : α), nnnorm ((c • f) a)) = (∫⁻ (a : α), (nnnorm c) * nnnorm (f a)) : begin apply lintegral_congr_ae, filter_upwards [], assume a, simp only [nnnorm_smul, set.mem_set_of_eq, pi.smul_apply, ennreal.coe_mul] end ... < ⊤ : begin rw lintegral_const_mul', apply mul_lt_top, { exact coe_lt_top }, { exact hfi }, { simp only [ennreal.coe_ne_top, ne.def, not_false_iff] } end end lemma integrable_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : integrable (λa, c • f a) ↔ integrable f := begin split, { assume h, simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.smul c⁻¹ }, exact integrable.smul _ end end normed_space variables [second_countable_topology β] namespace ae_eq_fun variable [measurable_space β] section variable [opens_measurable_space β] /-- An almost everywhere equal function is `integrable` if it has a finite distance to the origin. Should mean the same thing as the predicate `integrable` over functions. -/ def integrable (f : α →ₘ β) : Prop := f ∈ ball (0 : α →ₘ β) ⊤ lemma integrable_mk {f : α → β} (hf : measurable f) : (integrable (mk f hf)) ↔ measure_theory.integrable f := by simp [integrable, zero_def, edist_mk_mk', measure_theory.integrable, nndist_eq_nnnorm] lemma integrable_to_fun (f : α →ₘ β) : integrable f ↔ (measure_theory.integrable f.to_fun) := by conv_lhs { rw [self_eq_mk f, integrable_mk] } local attribute [simp] integrable_mk lemma integrable_zero : integrable (0 : α →ₘ β) := mem_ball_self coe_lt_top end section variable [borel_space β] lemma integrable.add : ∀ {f g : α →ₘ β}, integrable f → integrable g → integrable (f + g) := begin rintros ⟨f, hf⟩ ⟨g, hg⟩, simp only [mem_ball, zero_def, mk_add_mk, integrable_mk, quot_mk_eq_mk], assume hfi hgi, exact hfi.add hf hg hgi end lemma integrable.neg : ∀ {f : α →ₘ β}, integrable f → integrable (-f) := begin rintros ⟨f, hfm⟩ hfi, exact (integrable_mk _).2 ((integrable_mk hfm).1 hfi).neg end lemma integrable.sub : ∀ {f g : α →ₘ β}, integrable f → integrable g → integrable (f - g) := begin rintros ⟨f, hfm⟩ ⟨g, hgm⟩, simp only [quot_mk_eq_mk, integrable_mk, mk_sub_mk], exact λ hfi hgi, hfi.sub hfm hgm hgi end protected lemma is_add_subgroup : is_add_subgroup (ball (0 : α →ₘ β) ⊤) := { zero_mem := integrable_zero, add_mem := λ _ _, integrable.add, neg_mem := λ _, integrable.neg } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul : ∀ {c : 𝕜} {f : α →ₘ β}, integrable f → integrable (c • f) := begin rintros c ⟨f, hfm⟩, simp only [quot_mk_eq_mk, integrable_mk, smul_mk], exact λ hfi, hfi.smul c end end normed_space end end ae_eq_fun section variables (α β) [measurable_space β] [opens_measurable_space β] /-- The space of equivalence classes of integrable (and measurable) functions, where two integrable functions are equivalent if they agree almost everywhere, i.e., they differ on a set of measure `0`. -/ def l1 : Type (max u v) := subtype (@ae_eq_fun.integrable α _ β _ _ _ _) infixr ` →₁ `:25 := l1 end namespace l1 open ae_eq_fun local attribute [instance] ae_eq_fun.is_add_subgroup variables [measurable_space β] section variable [opens_measurable_space β] instance : has_coe (α →₁ β) (α →ₘ β) := ⟨subtype.val⟩ protected lemma eq {f g : α →₁ β} : (f : α →ₘ β) = (g : α →ₘ β) → f = g := subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁ β} : (f : α →ₘ β) = (g : α →ₘ β) ↔ f = g := iff.intro (l1.eq) (congr_arg coe) /- TODO : order structure of l1-/ /-- `L¹` space forms a `emetric_space`, with the emetric being inherited from almost everywhere functions, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (α →₁ β) := subtype.emetric_space /-- `L¹` space forms a `metric_space`, with the metric being inherited from almost everywhere functions, i.e., `edist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a))`. -/ instance : metric_space (α →₁ β) := metric_space_emetric_ball 0 ⊤ end variable [borel_space β] instance : add_comm_group (α →₁ β) := subtype.add_comm_group instance : inhabited (α →₁ β) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁ β) : α →ₘ β) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁ β) : ((f + g : α →₁ β) : α →ₘ β) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁ β) : ((-f : α →₁ β) : α →ₘ β) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁ β) : ((f - g : α →₁ β) : α →ₘ β) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁ β) : edist f g = edist (f : α →ₘ β) (g : α →ₘ β) := rfl lemma dist_eq (f g : α →₁ β) : dist f g = ennreal.to_real (edist (f : α →ₘ β) (g : α →ₘ β)) := rfl /-- The norm on `L¹` space is defined to be `∥f∥ = ∫⁻ a, edist (f a) 0`. -/ instance : has_norm (α →₁ β) := ⟨λ f, dist f 0⟩ lemma norm_eq (f : α →₁ β) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ β) 0) := rfl instance : normed_group (α →₁ β) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add], rw edist_eq_add_add } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] instance : has_scalar 𝕜 (α →₁ β) := ⟨λ x f, ⟨x • (f : α →ₘ β), ae_eq_fun.integrable.smul f.2⟩⟩ @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁ β) : ((c • f : α →₁ β) : α →ₘ β) = c • (f : α →ₘ β) := rfl instance : semimodule 𝕜 (α →₁ β) := { one_smul := λf, l1.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, l1.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, l1.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, l1.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, l1.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, l1.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } instance : module 𝕜 (α →₁ β) := { .. l1.semimodule } instance : vector_space 𝕜 (α →₁ β) := { .. l1.semimodule } instance : normed_space 𝕜 (α →₁ β) := ⟨ begin rintros x ⟨f, hf⟩, show ennreal.to_real (edist (x • f) 0) = ∥x∥ * ennreal.to_real (edist f 0), rw [edist_smul, to_real_of_real_mul], exact norm_nonneg _ end ⟩ end normed_space section of_fun /-- Construct the equivalence class `[f]` of a measurable and integrable function `f`. -/ def of_fun (f : α → β) (hfm : measurable f) (hfi : integrable f) : (α →₁ β) := ⟨mk f hfm, by { rw integrable_mk, exact hfi }⟩ lemma of_fun_eq_mk (f : α → β) (hfm hfi) : (of_fun f hfm hfi : α →ₘ β) = mk f hfm := rfl lemma of_fun_eq_of_fun (f g : α → β) (hfm hfi hgm hgi) : of_fun f hfm hfi = of_fun g hgm hgi ↔ ∀ₘ a, f a = g a := by { rw ← l1.eq_iff, simp only [of_fun_eq_mk, mk_eq_mk] } lemma of_fun_zero : of_fun (λa:α, (0:β)) (@measurable_const _ _ _ _ (0:β)) (integrable_zero α β) = 0 := rfl lemma of_fun_add (f g : α → β) (hfm hfi hgm hgi) : of_fun (λa, f a + g a) (measurable.add hfm hgm) (integrable.add hfm hfi hgm hgi) = of_fun f hfm hfi + of_fun g hgm hgi := rfl lemma of_fun_neg (f : α → β) (hfm hfi) : of_fun (λa, - f a) (measurable.neg hfm) (integrable.neg hfi) = - of_fun f hfm hfi := rfl lemma of_fun_sub (f g : α → β) (hfm hfi hgm hgi) : of_fun (λa, f a - g a) (measurable.sub hfm hgm) (integrable.sub hfm hfi hgm hgi) = of_fun f hfm hfi - of_fun g hgm hgi := rfl lemma norm_of_fun (f : α → β) (hfm hfi) : ∥of_fun f hfm hfi∥ = ennreal.to_real (∫⁻ a, edist (f a) 0) := rfl lemma norm_of_fun_eq_lintegral_norm (f : α → β) (hfm hfi) : ∥of_fun f hfm hfi∥ = ennreal.to_real (∫⁻ a, ennreal.of_real ∥f a∥) := by { rw [norm_of_fun, lintegral_norm_eq_lintegral_edist] } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma of_fun_smul (f : α → β) (hfm : measurable f) (hfi : integrable f) (k : 𝕜) : of_fun (λa, k • f a) (hfm.const_smul _) (hfi.smul _) = k • of_fun f hfm hfi := rfl end of_fun section to_fun /-- Find a representative of an `L¹` function [f] -/ @[reducible] protected def to_fun (f : α →₁ β) : α → β := (f : α →ₘ β).to_fun protected lemma measurable (f : α →₁ β) : measurable f.to_fun := f.1.measurable protected lemma integrable (f : α →₁ β) : integrable f.to_fun := by { rw [l1.to_fun, ← integrable_to_fun], exact f.2 } lemma of_fun_to_fun (f : α →₁ β) : of_fun (f.to_fun) f.measurable f.integrable = f := begin rcases f with ⟨f, hfi⟩, rw [of_fun, subtype.mk_eq_mk], exact (self_eq_mk f).symm end lemma mk_to_fun (f : α →₁ β) : mk (f.to_fun) f.measurable = f := by { rw ← of_fun_eq_mk, rw l1.eq_iff, exact of_fun_to_fun f } lemma to_fun_of_fun (f : α → β) (hfm hfi) : ∀ₘ a, (of_fun f hfm hfi).to_fun a = f a := (all_ae_mk_to_fun f hfm).mono $ assume a, id variables (α β) lemma zero_to_fun : ∀ₘ a, (0 : α →₁ β).to_fun a = 0 := ae_eq_fun.zero_to_fun variables {α β} lemma add_to_fun (f g : α →₁ β) : ∀ₘ a, (f + g).to_fun a = f.to_fun a + g.to_fun a := ae_eq_fun.add_to_fun _ _ lemma neg_to_fun (f : α →₁ β) : ∀ₘ a, (-f).to_fun a = -f.to_fun a := ae_eq_fun.neg_to_fun _ lemma sub_to_fun (f g : α →₁ β) : ∀ₘ a, (f - g).to_fun a = f.to_fun a - g.to_fun a := ae_eq_fun.sub_to_fun _ _ lemma dist_to_fun (f g : α →₁ β) : dist f g = ennreal.to_real (∫⁻ x, edist (f.to_fun x) (g.to_fun x)) := by { simp only [dist_eq, edist_to_fun] } lemma norm_eq_nnnorm_to_fun (f : α →₁ β) : ∥f∥ = ennreal.to_real (∫⁻ a, nnnorm (f.to_fun a)) := by { rw [lintegral_nnnorm_eq_lintegral_edist, ← edist_zero_to_fun], refl } lemma norm_eq_norm_to_fun (f : α →₁ β) : ∥f∥ = ennreal.to_real (∫⁻ a, ennreal.of_real ∥f.to_fun a∥) := by { rw norm_eq_nnnorm_to_fun, congr, funext, rw of_real_norm_eq_coe_nnnorm } lemma lintegral_edist_to_fun_lt_top (f g : α →₁ β) : (∫⁻ a, edist (f.to_fun a) (g.to_fun a)) < ⊤ := begin apply lintegral_edist_lt_top, exact f.measurable, exact f.integrable, exact g.measurable, exact g.integrable end variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma smul_to_fun (c : 𝕜) (f : α →₁ β) : ∀ₘ a, (c • f).to_fun a = c • f.to_fun a := ae_eq_fun.smul_to_fun _ _ end to_fun section pos_part /-- Positive part of a function in `L¹` space. -/ def pos_part (f : α →₁ ℝ) : α →₁ ℝ := ⟨ ae_eq_fun.pos_part f, begin rw [ae_eq_fun.integrable_to_fun, integrable_congr_ae (pos_part_to_fun _)], exact integrable.max_zero f.integrable end ⟩ /-- Negative part of a function in `L¹` space. -/ def neg_part (f : α →₁ ℝ) : α →₁ ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ ℝ) : (f.pos_part : α →ₘ ℝ) = (f : α →ₘ ℝ).pos_part := rfl lemma pos_part_to_fun (f : α →₁ ℝ) : ∀ₘ a, (pos_part f).to_fun a = max (f.to_fun a) 0 := ae_eq_fun.pos_part_to_fun _ lemma neg_part_to_fun_eq_max (f : α →₁ ℝ) : ∀ₘ a, (neg_part f).to_fun a = max (- f.to_fun a) 0 := begin rw neg_part, filter_upwards [pos_part_to_fun (-f), neg_to_fun f], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂] end lemma neg_part_to_fun_eq_min (f : α →₁ ℝ) : ∀ₘ a, (neg_part f).to_fun a = - min (f.to_fun a) 0 := begin filter_upwards [neg_part_to_fun_eq_max f], simp only [mem_set_of_eq], assume a h, rw [h, min_eq_neg_max_neg_neg, _root_.neg_neg, neg_zero], end lemma norm_le_norm_of_ae_le {f g : α →₁ β} (h : ∀ₘ a, ∥f.to_fun a∥ ≤ ∥g.to_fun a∥) : ∥f∥ ≤ ∥g∥ := begin simp only [l1.norm_eq_norm_to_fun], rw to_real_le_to_real, { apply lintegral_le_lintegral_ae, filter_upwards [h], simp only [mem_set_of_eq], assume a h, exact of_real_le_of_real h }, { rw [← lt_top_iff_ne_top, ← integrable_iff_norm], exact f.integrable }, { rw [← lt_top_iff_ne_top, ← integrable_iff_norm], exact g.integrable } end lemma continuous_pos_part : continuous $ λf : α →₁ ℝ, pos_part f := begin simp only [metric.continuous_iff], assume g ε hε, use ε, use hε, simp only [dist_eq_norm], assume f hfg, refine lt_of_le_of_lt (norm_le_norm_of_ae_le _) hfg, filter_upwards [l1.sub_to_fun f g, l1.sub_to_fun (pos_part f) (pos_part g), pos_part_to_fun f, pos_part_to_fun g], simp only [mem_set_of_eq], assume a h₁ h₂ h₃ h₄, simp only [real.norm_eq_abs, h₁, h₂, h₃, h₄], exact abs_max_sub_max_le_abs _ _ _ end lemma continuous_neg_part : continuous $ λf : α →₁ ℝ, neg_part f := have eq : (λf : α →₁ ℝ, neg_part f) = (λf : α →₁ ℝ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part /- TODO: l1 is a complete space -/ end l1 end measure_theory
c4151eb9869502b0df4bbb35aad2edb8e25856ba
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/borrowBug.lean
6528df998996610c607be9656644d0e93c71a05f
[ "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
373
lean
new_frontend @[noinline] def g (x : Nat) : Nat × Nat := (x, x) @[noinline] def p (x : Nat) : Bool := x > 10 set_option trace.compiler.ir.rc true def f (x : Nat) (y : Nat) : Bool := let jp (x : Nat) : Bool := -- x must be owned p x || p (x+1); match x with | 0 => let z := g y; jp z.1 | _ => let z := g x; jp z.1 def h (x : Nat) : Bool := -- x must be borrowed x > 5
cc5901ee7a52747b3298726dfa8392779f5186b7
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/lie/submodule.lean
769bdc77c14b509bfadf6e0088fa758e827de6c5
[ "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
28,729
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.subalgebra import ring_theory.noetherian /-! # Lie submodules of a Lie algebra In this file we define Lie submodules and Lie ideals, we construct the lattice structure on Lie submodules and we use it to define various important operations, notably the Lie span of a subset of a Lie module. ## Main definitions * `lie_submodule` * `lie_submodule.well_founded_of_noetherian` * `lie_submodule.lie_span` * `lie_submodule.map` * `lie_submodule.comap` * `lie_ideal` * `lie_ideal.map` * `lie_ideal.comap` ## Tags lie algebra, lie submodule, lie ideal, lattice structure -/ universes u v w w₁ w₂ section lie_submodule variables (R : Type u) (L : Type v) (M : Type w) variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] set_option old_structure_cmd true /-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie module. -/ structure lie_submodule extends submodule R M := (lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → ⁅x, m⁆ ∈ carrier) attribute [nolint doc_blame] lie_submodule.to_submodule namespace lie_submodule variables {R L M} (N N' : lie_submodule R L M) /-- The zero module is a Lie submodule of any Lie module. -/ instance : has_zero (lie_submodule R L M) := ⟨{ lie_mem := λ x m h, by { rw ((submodule.mem_bot R).1 h), apply lie_zero, }, ..(0 : submodule R M)}⟩ instance : inhabited (lie_submodule R L M) := ⟨0⟩ instance coe_submodule : has_coe (lie_submodule R L M) (submodule R M) := ⟨to_submodule⟩ @[norm_cast] lemma coe_to_submodule : ((N : submodule R M) : set M) = N := rfl instance has_mem : has_mem M (lie_submodule R L M) := ⟨λ x N, x ∈ (N : set M)⟩ @[simp] lemma mem_carrier {x : M} : x ∈ N.carrier ↔ x ∈ (N : set M) := iff.rfl @[simp] lemma mem_coe_submodule {x : M} : x ∈ (N : submodule R M) ↔ x ∈ N := iff.rfl lemma mem_coe {x : M} : x ∈ (N : set M) ↔ x ∈ N := iff.rfl @[simp] lemma zero_mem : (0 : M) ∈ N := (N : submodule R M).zero_mem @[simp] lemma coe_to_set_mk (S : set M) (h₁ h₂ h₃ h₄) : ((⟨S, h₁, h₂, h₃, h₄⟩ : lie_submodule R L M) : set M) = S := rfl @[simp] lemma coe_to_submodule_mk (p : submodule R M) (h) : (({lie_mem := h, ..p} : lie_submodule R L M) : submodule R M) = p := by { cases p, refl, } @[ext] lemma ext (h : ∀ m, m ∈ N ↔ m ∈ N') : N = N' := by { cases N, cases N', simp only [], ext m, exact h m, } @[simp] lemma coe_to_submodule_eq_iff : (N : submodule R M) = (N' : submodule R M) ↔ N = N' := begin split; intros h, { ext, rw [← mem_coe_submodule, h], simp, }, { rw h, }, end instance : lie_ring_module L N := { bracket := λ (x : L) (m : N), ⟨⁅x, m.val⁆, N.lie_mem m.property⟩, add_lie := by { intros x y m, apply set_coe.ext, apply add_lie, }, lie_add := by { intros x m n, apply set_coe.ext, apply lie_add, }, leibniz_lie := by { intros x y m, apply set_coe.ext, apply leibniz_lie, }, } instance : lie_module R L N := { lie_smul := by { intros t x y, apply set_coe.ext, apply lie_smul, }, smul_lie := by { intros t x y, apply set_coe.ext, apply smul_lie, }, } end lie_submodule section lie_ideal variables (L) /-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/ abbreviation lie_ideal := lie_submodule R L L lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I := by { rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, } /-- An ideal of a Lie algebra is a Lie subalgebra. -/ def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L := { lie_mem' := by { intros x y hx hy, apply lie_mem_right, exact hy, }, ..I.to_submodule, } instance : has_coe (lie_ideal R L) (lie_subalgebra R L) := ⟨λ I, lie_ideal_subalgebra R L I⟩ @[norm_cast] lemma lie_ideal.coe_to_subalgebra (I : lie_ideal R L) : ((I : lie_subalgebra R L) : set L) = I := rfl @[norm_cast] lemma lie_ideal.coe_to_lie_subalgebra_to_submodule (I : lie_ideal R L) : ((I : lie_subalgebra R L) : submodule R L) = I := rfl end lie_ideal variables {R M} lemma submodule.exists_lie_submodule_coe_eq_iff (p : submodule R M) : (∃ (N : lie_submodule R L M), ↑N = p) ↔ ∀ (x : L) (m : M), m ∈ p → ⁅x, m⁆ ∈ p := begin split, { rintros ⟨N, rfl⟩, exact N.lie_mem, }, { intros h, use { lie_mem := h, ..p }, exact lie_submodule.coe_to_submodule_mk p _, }, end namespace lie_subalgebra variables {L} lemma exists_lie_ideal_coe_eq_iff (K : lie_subalgebra R L): (∃ (I : lie_ideal R L), ↑I = K) ↔ ∀ (x y : L), y ∈ K → ⁅x, y⁆ ∈ K := begin simp only [← coe_to_submodule_eq_iff, lie_ideal.coe_to_lie_subalgebra_to_submodule, submodule.exists_lie_submodule_coe_eq_iff L], exact iff.rfl, end lemma exists_nested_lie_ideal_coe_eq_iff {K K' : lie_subalgebra R L} (h : K ≤ K') : (∃ (I : lie_ideal R K'), ↑I = of_le h) ↔ ∀ (x y : L), x ∈ K' → y ∈ K → ⁅x, y⁆ ∈ K := begin simp only [exists_lie_ideal_coe_eq_iff, coe_bracket, mem_of_le], split, { intros h' x y hx hy, exact h' ⟨x, hx⟩ ⟨y, h hy⟩ hy, }, { rintros h' ⟨x, hx⟩ ⟨y, hy⟩ hy', exact h' x y hx hy', }, end end lie_subalgebra end lie_submodule namespace lie_submodule variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] variables (N N' : lie_submodule R L M) (I J : lie_ideal R L) section lattice_structure open set lemma coe_injective : function.injective (coe : lie_submodule R L M → set M) := λ N N' h, by { cases N, cases N', simp only, exact h, } lemma coe_submodule_injective : function.injective (coe : lie_submodule R L M → submodule R M) := λ N N' h, by { ext, rw [← mem_coe_submodule, h], refl, } instance : partial_order (lie_submodule R L M) := { le := λ N N', ∀ ⦃x⦄, x ∈ N → x ∈ N', -- Overriding `le` like this gives a better defeq. ..partial_order.lift (coe : lie_submodule R L M → set M) coe_injective } lemma le_def : N ≤ N' ↔ (N : set M) ⊆ N' := iff.rfl @[simp, norm_cast] lemma coe_submodule_le_coe_submodule : (N : submodule R M) ≤ N' ↔ N ≤ N' := iff.rfl instance : has_bot (lie_submodule R L M) := ⟨0⟩ @[simp] lemma bot_coe : ((⊥ : lie_submodule R L M) : set M) = {0} := rfl @[simp] lemma bot_coe_submodule : ((⊥ : lie_submodule R L M) : submodule R M) = ⊥ := rfl @[simp] lemma mem_bot (x : M) : x ∈ (⊥ : lie_submodule R L M) ↔ x = 0 := mem_singleton_iff instance : has_top (lie_submodule R L M) := ⟨{ lie_mem := λ x m h, mem_univ ⁅x, m⁆, ..(⊤ : submodule R M) }⟩ @[simp] lemma top_coe : ((⊤ : lie_submodule R L M) : set M) = univ := rfl @[simp] lemma top_coe_submodule : ((⊤ : lie_submodule R L M) : submodule R M) = ⊤ := rfl @[simp] lemma mem_top (x : M) : x ∈ (⊤ : lie_submodule R L M) := mem_univ x instance : has_inf (lie_submodule R L M) := ⟨λ N N', { lie_mem := λ x m h, mem_inter (N.lie_mem h.1) (N'.lie_mem h.2), ..(N ⊓ N' : submodule R M) }⟩ instance : has_Inf (lie_submodule R L M) := ⟨λ S, { lie_mem := λ x m h, by { simp only [submodule.mem_carrier, mem_Inter, submodule.Inf_coe, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib] at *, intros N hN, apply N.lie_mem (h N hN), }, ..Inf {(s : submodule R M) | s ∈ S} }⟩ @[simp] theorem inf_coe : (↑(N ⊓ N') : set M) = N ∩ N' := rfl @[simp] lemma Inf_coe_to_submodule (S : set (lie_submodule R L M)) : (↑(Inf S) : submodule R M) = Inf {(s : submodule R M) | s ∈ S} := rfl @[simp] lemma Inf_coe (S : set (lie_submodule R L M)) : (↑(Inf S) : set M) = ⋂ s ∈ S, (s : set M) := begin rw [← lie_submodule.coe_to_submodule, Inf_coe_to_submodule, submodule.Inf_coe], ext m, simpa only [mem_Inter, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib], end lemma Inf_glb (S : set (lie_submodule R L M)) : is_glb S (Inf S) := begin have h : ∀ (N N' : lie_submodule R L M), (N : set M) ≤ N' ↔ N ≤ N', { intros, apply iff.rfl, }, simp only [is_glb.of_image h, Inf_coe, is_glb_binfi], end /-- The set of Lie submodules of a Lie module form a complete lattice. We provide explicit values for the fields `bot`, `top`, `inf` to get more convenient definitions than we would otherwise obtain from `complete_lattice_of_Inf`. -/ instance : complete_lattice (lie_submodule R L M) := { bot := ⊥, bot_le := λ N _ h, by { rw mem_bot at h, rw h, exact N.zero_mem', }, top := ⊤, le_top := λ _ _ _, trivial, inf := (⊓), le_inf := λ N₁ N₂ N₃ h₁₂ h₁₃ m hm, ⟨h₁₂ hm, h₁₃ hm⟩, inf_le_left := λ _ _ _, and.left, inf_le_right := λ _ _ _, and.right, ..complete_lattice_of_Inf _ Inf_glb } instance : add_comm_monoid (lie_submodule R L M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm, } @[simp] lemma add_eq_sup : N + N' = N ⊔ N' := rfl @[norm_cast, simp] lemma sup_coe_to_submodule : (↑(N ⊔ N') : submodule R M) = (N : submodule R M) ⊔ (N' : submodule R M) := begin have aux : ∀ (x : L) m, m ∈ (N ⊔ N' : submodule R M) → ⁅x,m⁆ ∈ (N ⊔ N' : submodule R M), { simp only [submodule.mem_sup], rintro x m ⟨y, hy, z, hz, rfl⟩, refine ⟨⁅x, y⁆, N.lie_mem hy, ⁅x, z⁆, N'.lie_mem hz, (lie_add _ _ _).symm⟩ }, refine le_antisymm (Inf_le ⟨{ lie_mem := aux, ..(N ⊔ N' : submodule R M) }, _⟩) _, { simp only [exists_prop, and_true, mem_set_of_eq, eq_self_iff_true, coe_to_submodule_mk, ← coe_submodule_le_coe_submodule, and_self, le_sup_left, le_sup_right] }, { simp, }, end @[norm_cast, simp] lemma inf_coe_to_submodule : (↑(N ⊓ N') : submodule R M) = (N : submodule R M) ⊓ (N' : submodule R M) := rfl @[simp] lemma mem_inf (x : M) : x ∈ N ⊓ N' ↔ x ∈ N ∧ x ∈ N' := by rw [← mem_coe_submodule, ← mem_coe_submodule, ← mem_coe_submodule, inf_coe_to_submodule, submodule.mem_inf] lemma mem_sup (x : M) : x ∈ N ⊔ N' ↔ ∃ (y ∈ N) (z ∈ N'), y + z = x := by { rw [← mem_coe_submodule, sup_coe_to_submodule, submodule.mem_sup], exact iff.rfl, } lemma eq_bot_iff : N = ⊥ ↔ ∀ (m : M), m ∈ N → m = 0 := by { rw eq_bot_iff, exact iff.rfl, } -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_bot : subsingleton (lie_submodule R L ↥(⊥ : lie_submodule R L M)) := begin apply subsingleton_of_bot_eq_top, ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx, simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot], end instance : is_modular_lattice (lie_submodule R L M) := { sup_inf_le_assoc_of_le := λ N₁ N₂ N₃, by { simp only [← coe_submodule_le_coe_submodule, sup_coe_to_submodule, inf_coe_to_submodule], exact is_modular_lattice.sup_inf_le_assoc_of_le ↑N₂, }, } variables (R L M) lemma well_founded_of_noetherian [is_noetherian R M] : well_founded ((>) : lie_submodule R L M → lie_submodule R L M → Prop) := begin let f : ((>) : lie_submodule R L M → lie_submodule R L M → Prop) →r ((>) : submodule R M → submodule R M → Prop) := { to_fun := coe, map_rel' := λ N N' h, h, }, apply f.well_founded, rw ← is_noetherian_iff_well_founded, apply_instance, end variables {R L M} section inclusion_maps /-- The inclusion of a Lie submodule into its ambient space is a morphism of Lie modules. -/ def incl : N →ₗ⁅R,L⁆ M := { map_lie' := λ x m, rfl, ..submodule.subtype (N : submodule R M) } @[simp] lemma incl_apply (m : N) : N.incl m = m := rfl lemma incl_eq_val : (N.incl : N → M) = subtype.val := rfl variables {N N'} (h : N ≤ N') /-- Given two nested Lie submodules `N ⊆ N'`, the inclusion `N ↪ N'` is a morphism of Lie modules.-/ def hom_of_le : N →ₗ⁅R,L⁆ N' := { map_lie' := λ x m, rfl, ..submodule.of_le h } @[simp] lemma coe_hom_of_le (m : N) : (hom_of_le h m : M) = m := rfl lemma hom_of_le_apply (m : N) : hom_of_le h m = ⟨m.1, h m.2⟩ := rfl lemma hom_of_le_injective : function.injective (hom_of_le h) := λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, submodule.coe_eq_coe, subtype.val_eq_coe] end inclusion_maps section lie_span variables (R L) (s : set M) /-- The `lie_span` of a set `s ⊆ M` is the smallest Lie submodule of `M` that contains `s`. -/ def lie_span : lie_submodule R L M := Inf {N | s ⊆ N} variables {R L s} lemma mem_lie_span {x : M} : x ∈ lie_span R L s ↔ ∀ N : lie_submodule R L M, s ⊆ N → x ∈ N := by { change x ∈ (lie_span R L s : set M) ↔ _, erw Inf_coe, exact mem_bInter_iff, } lemma subset_lie_span : s ⊆ lie_span R L s := by { intros m hm, erw mem_lie_span, intros N hN, exact hN hm, } lemma submodule_span_le_lie_span : submodule.span R s ≤ lie_span R L s := by { rw submodule.span_le, apply subset_lie_span, } lemma lie_span_le {N} : lie_span R L s ≤ N ↔ s ⊆ N := begin split, { exact subset.trans subset_lie_span, }, { intros hs m hm, rw mem_lie_span at hm, exact hm _ hs, }, end lemma lie_span_mono {t : set M} (h : s ⊆ t) : lie_span R L s ≤ lie_span R L t := by { rw lie_span_le, exact subset.trans h subset_lie_span, } lemma lie_span_eq : lie_span R L (N : set M) = N := le_antisymm (lie_span_le.mpr rfl.subset) subset_lie_span lemma coe_lie_span_submodule_eq_iff {p : submodule R M} : (lie_span R L (p : set M) : submodule R M) = p ↔ ∃ (N : lie_submodule R L M), ↑N = p := begin rw p.exists_lie_submodule_coe_eq_iff L, split; intros h, { intros x m hm, rw [← h, mem_coe_submodule], exact lie_mem _ (subset_lie_span hm), }, { rw [← coe_to_submodule_mk p h, coe_to_submodule, coe_to_submodule_eq_iff, lie_span_eq], }, end end lie_span end lattice_structure end lie_submodule section lie_submodule_map_and_comap variables {R : Type u} {L : Type v} {L' : Type w₂} {M : Type w} {M' : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] namespace lie_submodule /-- A morphism of Lie modules `f : M → M'` pushes forward Lie submodules of `M` to Lie submodules of `M'`. -/ def map (f : M →ₗ⁅R,L⁆ M') (N : lie_submodule R L M) : lie_submodule R L M' := { lie_mem := λ x m' h, by { rcases h with ⟨m, hm, hfm⟩, use ⁅x, m⁆, split, { apply N.lie_mem hm, }, { norm_cast at hfm, simp [hfm], }, }, ..(N : submodule R M).map (f : M →ₗ[R] M') } /-- A morphism of Lie modules `f : M → M'` pulls back Lie submodules of `M'` to Lie submodules of `M`. -/ def comap (f : M →ₗ⁅R,L⁆ M') (N : lie_submodule R L M') : lie_submodule R L M := { lie_mem := λ x m h, by { suffices : ⁅x, f m⁆ ∈ N, { simp [this], }, apply N.lie_mem h, }, ..(N : submodule R M').comap (f : M →ₗ[R] M') } lemma map_le_iff_le_comap {f : M →ₗ⁅R,L⁆ M'} {N : lie_submodule R L M} {N' : lie_submodule R L M'} : map f N ≤ N' ↔ N ≤ comap f N' := set.image_subset_iff lemma gc_map_comap (f : M →ₗ⁅R,L⁆ M') : galois_connection (map f) (comap f) := λ N N', map_le_iff_le_comap end lie_submodule namespace lie_ideal variables (f : L →ₗ⁅R⁆ L') (I : lie_ideal R L) (J : lie_ideal R L') @[simp] lemma top_coe_lie_subalgebra : ((⊤ : lie_ideal R L) : lie_subalgebra R L) = ⊤ := rfl /-- A morphism of Lie algebras `f : L → L'` pushes forward Lie ideals of `L` to Lie ideals of `L'`. Note that unlike `lie_submodule.map`, we must take the `lie_span` of the image. Mathematically this is because although `f` makes `L'` into a Lie module over `L`, in general the `L` submodules of `L'` are not the same as the ideals of `L'`. -/ def map : lie_ideal R L' := lie_submodule.lie_span R L' $ (I : submodule R L).map (f : L →ₗ[R] L') /-- A morphism of Lie algebras `f : L → L'` pulls back Lie ideals of `L'` to Lie ideals of `L`. Note that `f` makes `L'` into a Lie module over `L` (turning `f` into a morphism of Lie modules) and so this is a special case of `lie_submodule.comap` but we do not exploit this fact. -/ def comap : lie_ideal R L := { lie_mem := λ x y h, by { suffices : ⁅f x, f y⁆ ∈ J, { simp [this], }, apply J.lie_mem h, }, ..(J : submodule R L').comap (f : L →ₗ[R] L') } @[simp] lemma map_coe_submodule (h : ↑(map f I) = f '' I) : (map f I : submodule R L') = (I : submodule R L).map f := by { rw [submodule.ext'_iff, lie_submodule.coe_to_submodule, h, submodule.map_coe], refl, } @[simp] lemma comap_coe_submodule : (comap f J : submodule R L) = (J : submodule R L').comap f := rfl lemma map_le : map f I ≤ J ↔ f '' I ⊆ J := lie_submodule.lie_span_le variables {f I J} lemma mem_map {x : L} (hx : x ∈ I) : f x ∈ map f I := by { apply lie_submodule.subset_lie_span, use x, exact ⟨hx, rfl⟩, } @[simp] lemma mem_comap {x : L} : x ∈ comap f J ↔ f x ∈ J := iff.rfl lemma map_le_iff_le_comap : map f I ≤ J ↔ I ≤ comap f J := by { rw map_le, exact set.image_subset_iff, } lemma gc_map_comap : galois_connection (map f) (comap f) := λ I I', map_le_iff_le_comap lemma map_comap_le : map f (comap f J) ≤ J := by { rw map_le_iff_le_comap, apply le_refl _, } /-- See also `lie_ideal.map_comap_eq`. -/ lemma comap_map_le : I ≤ comap f (map f I) := by { rw ← map_le_iff_le_comap, apply le_refl _, } @[mono] lemma map_mono : monotone (map f) := λ I₁ I₂ h, by { rw lie_submodule.le_def at h, apply lie_submodule.lie_span_mono (set.image_subset ⇑f h), } @[mono] lemma comap_mono : monotone (comap f) := λ J₁ J₂ h, by { rw lie_submodule.le_def at h ⊢, exact set.preimage_mono h, } lemma map_of_image (h : f '' I = J) : I.map f = J := begin apply le_antisymm, { erw [lie_submodule.lie_span_le, submodule.map_coe, h], }, { rw [lie_submodule.le_def, ← h], exact lie_submodule.subset_lie_span, }, end /-- Note that this is not a special case of `lie_submodule.subsingleton_of_bot`. Indeed, given `I : lie_ideal R L`, in general the two lattices `lie_ideal R I` and `lie_submodule R L I` are different (though the latter does naturally inject into the former). In other words, in general, ideals of `I`, regarded as a Lie algebra in its own right, are not the same as ideals of `L` contained in `I`. -/ -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_bot : subsingleton (lie_ideal R ↥(⊥ : lie_ideal R L)) := begin apply subsingleton_of_bot_eq_top, ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx, simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot], end end lie_ideal namespace lie_hom variables (f : L →ₗ⁅R⁆ L') (I : lie_ideal R L) (J : lie_ideal R L') /-- The kernel of a morphism of Lie algebras, as an ideal in the domain. -/ def ker : lie_ideal R L := lie_ideal.comap f ⊥ /-- The range of a morphism of Lie algebras as an ideal in the codomain. -/ def ideal_range : lie_ideal R L' := lie_ideal.map f ⊤ lemma ideal_range_eq_lie_span_range : f.ideal_range = lie_submodule.lie_span R L' f.range := rfl /-- The condition that the image of a morphism of Lie algebras is an ideal. -/ def is_ideal_morphism : Prop := (f.ideal_range : lie_subalgebra R L') = f.range @[simp] lemma is_ideal_morphism_def : f.is_ideal_morphism ↔ (f.ideal_range : lie_subalgebra R L') = f.range := iff.rfl lemma is_ideal_morphism_iff : f.is_ideal_morphism ↔ ∀ (x : L') (y : L), ∃ (z : L), ⁅x, f y⁆ = f z := begin simp only [is_ideal_morphism_def, ideal_range_eq_lie_span_range, ← lie_subalgebra.coe_to_submodule_eq_iff, ← f.range.coe_to_submodule, lie_ideal.coe_to_lie_subalgebra_to_submodule, lie_submodule.coe_lie_span_submodule_eq_iff, lie_subalgebra.mem_coe_submodule, mem_range, exists_imp_distrib, submodule.exists_lie_submodule_coe_eq_iff], split, { intros h x y, obtain ⟨z, hz⟩ := h x (f y) y rfl, use z, exact hz.symm, }, { intros h x y z hz, obtain ⟨w, hw⟩ := h x z, use w, rw [← hw, hz], }, end lemma range_subset_ideal_range : (f.range : set L') ⊆ f.ideal_range := lie_submodule.subset_lie_span lemma map_le_ideal_range : I.map f ≤ f.ideal_range := lie_ideal.map_mono le_top lemma ker_le_comap : f.ker ≤ J.comap f := lie_ideal.comap_mono bot_le @[simp] lemma ker_coe_submodule : (ker f : submodule R L) = (f : L →ₗ[R] L').ker := rfl @[simp] lemma mem_ker {x : L} : x ∈ ker f ↔ f x = 0 := show x ∈ (f.ker : submodule R L) ↔ _, by simp only [ker_coe_submodule, linear_map.mem_ker, coe_to_linear_map] lemma mem_ideal_range {x : L} : f x ∈ ideal_range f := lie_ideal.mem_map (lie_submodule.mem_top x) @[simp] lemma mem_ideal_range_iff (h : is_ideal_morphism f) {y : L'} : y ∈ ideal_range f ↔ ∃ (x : L), f x = y := begin rw f.is_ideal_morphism_def at h, rw [← lie_submodule.mem_coe, ← lie_ideal.coe_to_subalgebra, h, f.range_coe, set.mem_range], end lemma le_ker_iff : I ≤ f.ker ↔ ∀ x, x ∈ I → f x = 0 := begin split; intros h x hx, { specialize h hx, rw mem_ker at h, exact h, }, { rw mem_ker, apply h x hx, }, end lemma ker_eq_bot : f.ker = ⊥ ↔ function.injective f := by rw [← lie_submodule.coe_to_submodule_eq_iff, ker_coe_submodule, lie_submodule.bot_coe_submodule, linear_map.ker_eq_bot, coe_to_linear_map] @[simp] lemma range_coe_submodule : (f.range : submodule R L') = (f : L →ₗ[R] L').range := rfl lemma range_eq_top : f.range = ⊤ ↔ function.surjective f := begin rw [← lie_subalgebra.coe_to_submodule_eq_iff, range_coe_submodule, lie_subalgebra.top_coe_submodule], exact linear_map.range_eq_top, end @[simp] lemma ideal_range_eq_top_of_surjective (h : function.surjective f) : f.ideal_range = ⊤ := begin rw ← f.range_eq_top at h, rw [ideal_range_eq_lie_span_range, h, ← lie_subalgebra.coe_to_submodule, ← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule, lie_subalgebra.top_coe_submodule, lie_submodule.coe_lie_span_submodule_eq_iff], use ⊤, exact lie_submodule.top_coe_submodule, end lemma is_ideal_morphism_of_surjective (h : function.surjective f) : f.is_ideal_morphism := by rw [is_ideal_morphism_def, f.ideal_range_eq_top_of_surjective h, f.range_eq_top.mpr h, lie_ideal.top_coe_lie_subalgebra] end lie_hom namespace lie_ideal variables {f : L →ₗ⁅R⁆ L'} {I : lie_ideal R L} {J : lie_ideal R L'} @[simp] lemma map_eq_bot_iff : I.map f = ⊥ ↔ I ≤ f.ker := by { rw ← le_bot_iff, exact lie_ideal.map_le_iff_le_comap } lemma coe_map_of_surjective (h : function.surjective f) : (I.map f : submodule R L') = (I : submodule R L).map f := begin let J : lie_ideal R L' := { lie_mem := λ x y hy, begin have hy' : ∃ (x : L), x ∈ I ∧ f x = y, { simpa [hy], }, obtain ⟨z₂, hz₂, rfl⟩ := hy', obtain ⟨z₁, rfl⟩ := h x, simp only [lie_hom.coe_to_linear_map, submodule.mem_coe, set.mem_image, lie_submodule.mem_coe_submodule, submodule.mem_carrier, submodule.map_coe], use ⁅z₁, z₂⁆, exact ⟨I.lie_mem hz₂, f.map_lie z₁ z₂⟩, end, ..(I : submodule R L).map (f : L →ₗ[R] L'), }, erw lie_submodule.coe_lie_span_submodule_eq_iff, use J, apply lie_submodule.coe_to_submodule_mk, end lemma mem_map_of_surjective {y : L'} (h₁ : function.surjective f) (h₂ : y ∈ I.map f) : ∃ (x : I), f x = y := begin rw [← lie_submodule.mem_coe_submodule, coe_map_of_surjective h₁, submodule.mem_map] at h₂, obtain ⟨x, hx, rfl⟩ := h₂, use ⟨x, hx⟩, refl, end lemma bot_of_map_eq_bot {I : lie_ideal R L} (h₁ : function.injective f) (h₂ : I.map f = ⊥) : I = ⊥ := begin rw ← f.ker_eq_bot at h₁, change comap f ⊥ = ⊥ at h₁, rw [eq_bot_iff, map_le_iff_le_comap, h₁] at h₂, rw eq_bot_iff, exact h₂, end /-- Given two nested Lie ideals `I₁ ⊆ I₂`, the inclusion `I₁ ↪ I₂` is a morphism of Lie algebras. -/ def hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) : I₁ →ₗ⁅R⁆ I₂ := { map_lie' := λ x y, rfl, ..submodule.of_le h, } @[simp] lemma coe_hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) : (hom_of_le h x : L) = x := rfl lemma hom_of_le_apply {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) : hom_of_le h x = ⟨x.1, h x.2⟩ := rfl lemma hom_of_le_injective {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) : function.injective (hom_of_le h) := λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, submodule.coe_eq_coe, subtype.val_eq_coe] @[simp] lemma map_sup_ker_eq_map : lie_ideal.map f (I ⊔ f.ker) = lie_ideal.map f I := begin suffices : lie_ideal.map f (I ⊔ f.ker) ≤ lie_ideal.map f I, { exact le_antisymm this (lie_ideal.map_mono le_sup_left), }, apply lie_submodule.lie_span_mono, rintros x ⟨y, hy₁, hy₂⟩, rw ← hy₂, erw lie_submodule.mem_sup at hy₁, obtain ⟨z₁, hz₁, z₂, hz₂, hy⟩ := hy₁, rw ← hy, rw [f.coe_to_linear_map, f.map_add, f.mem_ker.mp hz₂, add_zero], exact ⟨z₁, hz₁, rfl⟩, end @[simp] lemma map_comap_eq (h : f.is_ideal_morphism) : map f (comap f J) = f.ideal_range ⊓ J := begin apply le_antisymm, { rw le_inf_iff, exact ⟨f.map_le_ideal_range _, map_comap_le⟩, }, { rw f.is_ideal_morphism_def at h, rw [lie_submodule.le_def, lie_submodule.inf_coe, ← coe_to_subalgebra, h], rintros y ⟨⟨x, h₁, h₂⟩, h₃⟩, rw ← h₂ at h₃ ⊢, exact mem_map h₃, }, end @[simp] lemma comap_map_eq (h : ↑(map f I) = f '' I) : comap f (map f I) = I ⊔ f.ker := by rw [← lie_submodule.coe_to_submodule_eq_iff, comap_coe_submodule, I.map_coe_submodule f h, lie_submodule.sup_coe_to_submodule, f.ker_coe_submodule, linear_map.comap_map_eq] variables (f I J) /-- Regarding an ideal `I` as a subalgebra, the inclusion map into its ambient space is a morphism of Lie algebras. -/ def incl : I →ₗ⁅R⁆ L := (I : lie_subalgebra R L).incl @[simp] lemma incl_range : I.incl.range = I := (I : lie_subalgebra R L).incl_range @[simp] lemma incl_apply (x : I) : I.incl x = x := rfl @[simp] lemma incl_coe : (I.incl : I →ₗ[R] L) = (I : submodule R L).subtype := rfl @[simp] lemma comap_incl_self : comap I.incl I = ⊤ := by { rw ← lie_submodule.coe_to_submodule_eq_iff, exact submodule.comap_subtype_self _, } @[simp] lemma ker_incl : I.incl.ker = ⊥ := by rw [← lie_submodule.coe_to_submodule_eq_iff, I.incl.ker_coe_submodule, lie_submodule.bot_coe_submodule, incl_coe, submodule.ker_subtype] @[simp] lemma incl_ideal_range : I.incl.ideal_range = I := begin rw [lie_hom.ideal_range_eq_lie_span_range, ← lie_subalgebra.coe_to_submodule, ← lie_submodule.coe_to_submodule_eq_iff, incl_range, coe_to_lie_subalgebra_to_submodule, lie_submodule.coe_lie_span_submodule_eq_iff], use I, end lemma incl_is_ideal_morphism : I.incl.is_ideal_morphism := begin rw [I.incl.is_ideal_morphism_def, incl_ideal_range], exact (I : lie_subalgebra R L).incl_range.symm, end end lie_ideal end lie_submodule_map_and_comap section top_equiv_self variables {R : Type u} {L : Type v} variables [comm_ring R] [lie_ring L] [lie_algebra R L] /-- The natural equivalence between the 'top' Lie subalgebra and the enclosing Lie algebra. -/ def lie_subalgebra.top_equiv_self : (⊤ : lie_subalgebra R L) ≃ₗ⁅R⁆ L := { inv_fun := λ x, ⟨x, set.mem_univ x⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, rfl, ..(⊤ : lie_subalgebra R L).incl, } @[simp] lemma lie_subalgebra.top_equiv_self_apply (x : (⊤ : lie_subalgebra R L)) : lie_subalgebra.top_equiv_self x = x := rfl /-- The natural equivalence between the 'top' Lie ideal and the enclosing Lie algebra. -/ def lie_ideal.top_equiv_self : (⊤ : lie_ideal R L) ≃ₗ⁅R⁆ L := lie_subalgebra.top_equiv_self @[simp] lemma lie_ideal.top_equiv_self_apply (x : (⊤ : lie_ideal R L)) : lie_ideal.top_equiv_self x = x := rfl end top_equiv_self
5b2db11455d2a80953081a6018c1e803432470df
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/logic/function/basic.lean
d76e65e62658a45d633ac9e71e6d9551a46b2ad2
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,152
lean
/- Copyright (c) 2016 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import logic.basic import data.option.defs /-! # Miscellaneous function constructions and lemmas -/ universes u v w namespace function section variables {α β γ : Sort*} {f : α → β} /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `function.eval x : (Π x, β x) → β x`. -/ @[reducible] def eval {β : α → Sort*} (x : α) (f : Π x, β x) : β x := f x @[simp] lemma eval_apply {β : α → Sort*} (x : α) (f : Π x, β x) : eval x f = f x := rfl lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl lemma const_def {y : β} : (λ x : α, y) = const α y := rfl @[simp] lemma const_apply {y : β} {x : α} : const α y x = y := rfl @[simp] lemma const_comp {f : α → β} {c : γ} : const β c ∘ f = const α c := rfl @[simp] lemma comp_const {f : β → γ} {b : β} : f ∘ const α b = const α (f b) := rfl lemma id_def : @id α = λ x, x := rfl lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a} (hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' := begin subst hα, have : ∀a, f a == f' a, { intro a, exact h a a (heq.refl a) }, have : β = β', { funext a, exact type_eq_of_heq (this a) }, subst this, apply heq_of_eq, funext a, exact eq_of_heq (this a) end lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) := iff.intro (assume h a, h ▸ rfl) funext protected lemma bijective.injective {f : α → β} (hf : bijective f) : injective f := hf.1 protected lemma bijective.surjective {f : α → β} (hf : bijective f) : surjective f := hf.2 theorem injective.eq_iff (I : injective f) {a b : α} : f a = f b ↔ a = b := ⟨@I _ _, congr_arg f⟩ theorem injective.eq_iff' (I : injective f) {a b : α} {c : β} (h : f b = c) : f a = c ↔ a = b := h ▸ I.eq_iff lemma injective.ne (hf : injective f) {a₁ a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ := mt (assume h, hf h) lemma injective.ne_iff (hf : injective f) {x y : α} : f x ≠ f y ↔ x ≠ y := ⟨mt $ congr_arg f, hf.ne⟩ lemma injective.ne_iff' (hf : injective f) {x y : α} {z : β} (h : f y = z) : f x ≠ z ↔ x ≠ y := h ▸ hf.ne_iff /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α := λ a b, decidable_of_iff _ I.eq_iff lemma injective.of_comp {g : γ → α} (I : injective (f ∘ g)) : injective g := λ x y h, I $ show f (g x) = f (g y), from congr_arg f h lemma injective.of_comp_iff {f : α → β} (hf : injective f) (g : γ → α) : injective (f ∘ g) ↔ injective g := ⟨injective.of_comp, hf.comp⟩ lemma injective.of_comp_iff' (f : α → β) {g : γ → α} (hg : bijective g) : injective (f ∘ g) ↔ injective f := ⟨ λ h x y, let ⟨x', hx⟩ := hg.surjective x, ⟨y', hy⟩ := hg.surjective y in hx ▸ hy ▸ λ hf, h hf ▸ rfl, λ h, h.comp hg.injective⟩ lemma injective_of_subsingleton [subsingleton α] (f : α → β) : injective f := λ a b ab, subsingleton.elim _ _ lemma injective.dite (p : α → Prop) [decidable_pred p] {f : {a : α // p a} → β} {f' : {a : α // ¬ p a} → β} (hf : injective f) (hf' : injective f') (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬ p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) : function.injective (λ x, if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩) := λ x₁ x₂ h, begin dsimp only at h, by_cases h₁ : p x₁; by_cases h₂ : p x₂, { rw [dif_pos h₁, dif_pos h₂] at h, injection (hf h), }, { rw [dif_pos h₁, dif_neg h₂] at h, exact (im_disj h).elim, }, { rw [dif_neg h₁, dif_pos h₂] at h, exact (im_disj h.symm).elim, }, { rw [dif_neg h₁, dif_neg h₂] at h, injection (hf' h), }, end lemma surjective.of_comp {g : γ → α} (S : surjective (f ∘ g)) : surjective f := λ y, let ⟨x, h⟩ := S y in ⟨g x, h⟩ lemma surjective.of_comp_iff (f : α → β) {g : γ → α} (hg : surjective g) : surjective (f ∘ g) ↔ surjective f := ⟨surjective.of_comp, λ h, h.comp hg⟩ lemma surjective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : surjective (f ∘ g) ↔ surjective g := ⟨λ h x, let ⟨x', hx'⟩ := h (f x) in ⟨x', hf.injective hx'⟩, hf.surjective.comp⟩ instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*) [Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp) | f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm theorem surjective.forall {f : α → β} (hf : surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x) := ⟨λ h x, h (f x), λ h y, let ⟨x, hx⟩ := hf y in hx ▸ h x⟩ theorem surjective.forall₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) := hf.forall.trans $ forall_congr $ λ x, hf.forall theorem surjective.forall₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.forall.trans $ forall_congr $ λ x, hf.forall₂ theorem surjective.exists {f : α → β} (hf : surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x) := ⟨λ ⟨y, hy⟩, let ⟨x, hx⟩ := hf y in ⟨x, hx.symm ▸ hy⟩, λ ⟨x, hx⟩, ⟨f x, hx⟩⟩ theorem surjective.exists₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) := hf.exists.trans $ exists_congr $ λ x, hf.exists theorem surjective.exists₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.exists.trans $ exists_congr $ λ x, hf.exists₂ lemma bijective_iff_exists_unique (f : α → β) : bijective f ↔ ∀ b : β, ∃! (a : α), f a = b := ⟨ λ hf b, let ⟨a, ha⟩ := hf.surjective b in ⟨a, ha, λ a' ha', hf.injective (ha'.trans ha.symm)⟩, λ he, ⟨ λ a a' h, unique_of_exists_unique (he (f a')) h rfl, λ b, exists_of_exists_unique (he b) ⟩⟩ /-- Shorthand for using projection notation with `function.bijective_iff_exists_unique`. -/ lemma bijective.exists_unique {f : α → β} (hf : bijective f) (b : β) : ∃! (a : α), f a = b := (bijective_iff_exists_unique f).mp hf b lemma bijective.of_comp_iff (f : α → β) {g : γ → α} (hg : bijective g) : bijective (f ∘ g) ↔ bijective f := and_congr (injective.of_comp_iff' _ hg) (surjective.of_comp_iff _ hg.surjective) lemma bijective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : function.bijective (f ∘ g) ↔ function.bijective g := and_congr (injective.of_comp_iff hf.injective _) (surjective.of_comp_iff' hf _) /-- **Cantor's diagonal argument** implies that there are no surjective functions from `α` to `set α`. -/ theorem cantor_surjective {α} (f : α → set α) : ¬ function.surjective f | h := let ⟨D, e⟩ := h (λ a, ¬ f a a) in (iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D) /-- **Cantor's diagonal argument** implies that there are no injective functions from `set α` to `α`. -/ theorem cantor_injective {α : Type*} (f : (set α) → α) : ¬ function.injective f | i := cantor_surjective (λ a b, ∀ U, a = f U → U b) $ right_inverse.surjective (λ U, funext $ λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩) /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f := λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem left_inverse_iff_comp {f : α → β} {g : β → α} : left_inverse f g ↔ f ∘ g = id := ⟨left_inverse.comp_eq_id, congr_fun⟩ theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem right_inverse_iff_comp {f : α → β} {g : β → α} : right_inverse f g ↔ g ∘ f = id := ⟨right_inverse.comp_eq_id, congr_fun⟩ theorem left_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a] theorem right_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf theorem left_inverse.right_inverse {f : α → β} {g : β → α} (h : left_inverse g f) : right_inverse f g := h theorem right_inverse.left_inverse {f : α → β} {g : β → α} (h : right_inverse g f) : left_inverse f g := h theorem left_inverse.surjective {f : α → β} {g : β → α} (h : left_inverse f g) : surjective f := h.right_inverse.surjective theorem right_inverse.injective {f : α → β} {g : β → α} (h : right_inverse f g) : injective f := h.left_inverse.injective theorem left_inverse.eq_right_inverse {f : α → β} {g₁ g₂ : β → α} (h₁ : left_inverse g₁ f) (h₂ : right_inverse g₂ f) : g₁ = g₂ := calc g₁ = g₁ ∘ f ∘ g₂ : by rw [h₂.comp_eq_id, comp.right_id] ... = g₂ : by rw [← comp.assoc, h₁.comp_eq_id, comp.left_id] local attribute [instance, priority 10] classical.prop_decidable /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α := if h : ∃ a, f a = b then some (classical.some h) else none theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) | a b := ⟨λ h, if h' : ∃ a, f a = b then begin rw [partial_inv, dif_pos h'] at h, injection h with h, subst h, apply classical.some_spec h' end else by rw [partial_inv, dif_neg h'] at h; contradiction, λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩, (dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩ theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) end section inv_fun variables {α : Type u} [n : nonempty α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β} include n local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. For a computable version, see `function.injective.inv_of_mem_range`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice n theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_eq' (h : ∀ (x ∈ s) (y ∈ s), f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h _ (inv_fun_on_mem this) _ ha (inv_fun_on_eq this) theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice n := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b := inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩ lemma inv_fun_neg (h : ¬ ∃ a, f a = b) : inv_fun f b = classical.choice n := by refine inv_fun_on_neg (mt _ h); exact assume ⟨a, _, ha⟩, ⟨a, ha⟩ theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext $ assume b, hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f := assume b, inv_fun_eq $ hf b lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f := assume b, have f (inv_fun f (f b)) = f b, from inv_fun_eq ⟨b, rfl⟩, hf this lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) := (left_inverse_inv_fun hf).surjective lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf end inv_fun section inv_fun variables {α : Type u} [i : nonempty α] {β : Sort v} {f : α → β} include i lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f := ⟨inv_fun f, left_inverse_inv_fun hf⟩ lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f := ⟨injective.has_left_inverse, has_left_inverse.injective⟩ end inv_fun section surj_inv variables {α : Sort u} {β : Sort v} {f : α → β} /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b) lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f := right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2) lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f := ⟨_, right_inverse_surj_inv hf⟩ lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f := ⟨surjective.has_right_inverse, has_right_inverse.surjective⟩ lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f := ⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩, λ ⟨g, gl, gr⟩, ⟨gl.injective, gr.surjective⟩⟩ lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) := (right_inverse_surj_inv h).injective lemma surjective_to_subsingleton [na : nonempty α] [subsingleton β] (f : α → β) : surjective f := λ y, let ⟨a⟩ := na in ⟨a, subsingleton.elim _ _⟩ end surj_inv section update variables {α : Sort u} {β : α → Sort v} {α' : Sort w} [decidable_eq α] [decidable_eq α'] /-- Replacing the value of a function at a given point by a given value. -/ def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a := if h : a = a' then eq.rec v h.symm else f a /-- On non-dependent functions, `function.update` can be expressed as an `ite` -/ lemma update_apply {β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = if a = a' then b else f a := begin dunfold update, congr, funext, rw eq_rec_constant, end @[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v := dif_pos rfl lemma update_injective (f : Πa, β a) (a' : α) : injective (update f a') := λ v v' h, have _ := congr_fun h a', by rwa [update_same, update_same] at this @[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a := dif_neg h lemma forall_update_iff (f : Π a, β a) {a : α} {b : β a} (p : Π a, β a → Prop) : (∀ x, p x (update f a b x)) ↔ p a b ∧ ∀ x ≠ a, p x (f x) := by { rw [← and_forall_ne a, update_same], simp { contextual := tt } } lemma update_eq_iff {a : α} {b : β a} {f g : Π a, β a} : update f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x := funext_iff.trans $ forall_update_iff _ (λ x y, y = g x) lemma eq_update_iff {a : α} {b : β a} {f g : Π a, β a} : g = update f a b ↔ g a = b ∧ ∀ x ≠ a, g x = f x := funext_iff.trans $ forall_update_iff _ (λ x y, g x = y) @[simp] lemma update_eq_self (a : α) (f : Πa, β a) : update f a (f a) = f := update_eq_iff.2 ⟨rfl, λ _ _, rfl⟩ lemma update_comp_eq_of_forall_ne' {α'} (g : Π a, β a) {f : α' → α} {i : α} (a : β i) (h : ∀ x, f x ≠ i) : (λ j, (update g i a) (f j)) = (λ j, g (f j)) := funext $ λ x, update_noteq (h _) _ _ /-- Non-dependent version of `function.update_comp_eq_of_forall_ne'` -/ lemma update_comp_eq_of_forall_ne {α β : Sort*} (g : α' → β) {f : α → α'} {i : α'} (a : β) (h : ∀ x, f x ≠ i) : (update g i a) ∘ f = g ∘ f := update_comp_eq_of_forall_ne' g a h lemma update_comp_eq_of_injective' (g : Π a, β a) {f : α' → α} (hf : function.injective f) (i : α') (a : β (f i)) : (λ j, update g (f i) a (f j)) = update (λ i, g (f i)) i a := eq_update_iff.2 ⟨update_same _ _ _, λ j hj, update_noteq (hf.ne hj) _ _⟩ /-- Non-dependent version of `function.update_comp_eq_of_injective'` -/ lemma update_comp_eq_of_injective {β : Sort*} (g : α' → β) {f : α → α'} (hf : function.injective f) (i : α) (a : β) : (function.update g (f i) a) ∘ f = function.update (g ∘ f) i a := update_comp_eq_of_injective' g hf i a lemma apply_update {ι : Sort*} [decidable_eq ι] {α β : ι → Sort*} (f : Π i, α i → β i) (g : Π i, α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (λ k, f k (g k)) i (f i v) j := begin by_cases h : j = i, { subst j, simp }, { simp [h] } end lemma comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ (update g i v) = update (f ∘ g) i (f v) := funext $ apply_update _ _ _ _ theorem update_comm {α} [decidable_eq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b) (f : Πa, β a) : update (update f a v) b w = update (update f b w) a v := begin funext c, simp only [update], by_cases h₁ : c = b; by_cases h₂ : c = a; try {simp [h₁, h₂]}, cases h (h₂.symm.trans h₁), end @[simp] theorem update_idem {α} [decidable_eq α] {β : α → Sort*} {a : α} (v w : β a) (f : Πa, β a) : update (update f a v) a w = update f a w := by {funext b, by_cases b = a; simp [update, h]} end update section extend noncomputable theory local attribute [instance, priority 10] classical.prop_decidable variables {α β γ : Type*} {f : α → β} /-- `extend f g e'` extends a function `g : α → γ` along a function `f : α → β` to a function `β → γ`, by using the values of `g` on the range of `f` and the values of an auxiliary function `e' : β → γ` elsewhere. Mostly useful when `f` is injective. -/ def extend (f : α → β) (g : α → γ) (e' : β → γ) : β → γ := λ b, if h : ∃ a, f a = b then g (classical.some h) else e' b lemma extend_def (f : α → β) (g : α → γ) (e' : β → γ) (b : β) [decidable (∃ a, f a = b)] : extend f g e' b = if h : ∃ a, f a = b then g (classical.some h) else e' b := by { unfold extend, congr } @[simp] lemma extend_apply (hf : injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a := begin simp only [extend_def, dif_pos, exists_apply_eq_apply], exact congr_arg g (hf $ classical.some_spec (exists_apply_eq_apply f a)) end @[simp] lemma extend_comp (hf : injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g := funext $ λ a, extend_apply hf g e' a end extend lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := rfl @[simp] lemma uncurry_apply_pair {α β γ} (f : α → β → γ) (x : α) (y : β) : uncurry f (x, y) = f x y := rfl @[simp] lemma curry_apply {α β γ} (f : α × β → γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl section bicomp variables {α β γ δ ε : Type*} /-- Compose a binary function `f` with a pair of unary functions `g` and `h`. If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/ def bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b) := f (g a) (h b) /-- Compose an unary function `f` with a binary function `g`. -/ def bicompr (f : γ → δ) (g : α → β → γ) (a b) := f (g a b) -- Suggested local notation: local notation f `∘₂` g := bicompr f g lemma uncurry_bicompr (f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = (g ∘ uncurry f) := rfl lemma uncurry_bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = (uncurry f) ∘ (prod.map g h) := rfl end bicomp section uncurry variables {α β γ δ : Type*} /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type*) (β : out_param Type*) (γ : out_param Type*) := (uncurry : α → (β → γ)) /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps.-/ add_decl_doc has_uncurry.uncurry notation `↿`:max x:max := has_uncurry.uncurry x instance has_uncurry_base : has_uncurry (α → β) α β := ⟨id⟩ instance has_uncurry_induction [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := ⟨λ f p, ↿(f p.1) p.2⟩ end uncurry /-- A function is involutive, if `f ∘ f = id`. -/ def involutive {α} (f : α → α) : Prop := ∀ x, f (f x) = x lemma involutive_iff_iter_2_eq_id {α} {f : α → α} : involutive f ↔ (f^[2] = id) := funext_iff.symm namespace involutive variables {α : Sort u} {f : α → α} (h : involutive f) include h @[simp] lemma comp_self : f ∘ f = id := funext h protected lemma left_inverse : left_inverse f f := h protected lemma right_inverse : right_inverse f f := h protected lemma injective : injective f := h.left_inverse.injective protected lemma surjective : surjective f := λ x, ⟨f x, h x⟩ protected lemma bijective : bijective f := ⟨h.injective, h.surjective⟩ /-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/ protected lemma ite_not (P : Prop) [decidable P] (x : α) : f (ite P x (f x)) = ite (¬ P) x (f x) := by rw [apply_ite f, h, ite_not] /-- An involution commutes across an equality. Compare to `function.injective.eq_iff`. -/ protected lemma eq_iff {x y : α} : f x = y ↔ x = f y := h.injective.eq_iff' (h y) end involutive /-- The property of a binary function `f : α → β → γ` being injective. Mathematically this should be thought of as the corresponding function `α × β → γ` being injective. -/ @[reducible] def injective2 {α β γ} (f : α → β → γ) : Prop := ∀ ⦃a₁ a₂ b₁ b₂⦄, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂ namespace injective2 variables {α β γ : Type*} (f : α → β → γ) protected lemma left (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : a₁ = a₂ := (hf h).1 protected lemma right (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : b₁ = b₂ := (hf h).2 lemma eq_iff (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ : f a₁ b₁ = f a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ h, hf h, λ⟨h1, h2⟩, congr_arg2 f h1 h2⟩ end injective2 section sometimes local attribute [instance, priority 10] classical.prop_decidable /-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially interesting in the case where `α` is a proposition, in which case `f` is necessarily a constant function, so that `sometimes f = f a` for all `a`. -/ noncomputable def sometimes {α β} [nonempty β] (f : α → β) : β := if h : nonempty α then f (classical.choice h) else classical.choice ‹_› theorem sometimes_eq {p : Prop} {α} [nonempty α] (f : p → α) (a : p) : sometimes f = f a := dif_pos ⟨a⟩ theorem sometimes_spec {p : Prop} {α} [nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f) := by rwa sometimes_eq end sometimes end function /-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/ def set.piecewise {α : Type u} {β : α → Sort v} (s : set α) (f g : Πi, β i) [∀j, decidable (j ∈ s)] : Πi, β i := λi, if i ∈ s then f i else g i /-! ### Bijectivity of `eq.rec`, `eq.mp`, `eq.mpr`, and `cast` -/ lemma eq_rec_on_bijective {α : Sort*} {C : α → Sort*} : ∀ {a a' : α} (h : a = a'), function.bijective (@eq.rec_on _ _ C _ h) | _ _ rfl := ⟨λ x y, id, λ x, ⟨x, rfl⟩⟩ lemma eq_mp_bijective {α β : Sort*} (h : α = β) : function.bijective (eq.mp h) := eq_rec_on_bijective h lemma eq_mpr_bijective {α β : Sort*} (h : α = β) : function.bijective (eq.mpr h) := eq_rec_on_bijective h.symm lemma cast_bijective {α β : Sort*} (h : α = β) : function.bijective (cast h) := eq_rec_on_bijective h /-! Note these lemmas apply to `Type*` not `Sort*`, as the latter interferes with `simp`, and is trivial anyway.-/ @[simp] lemma eq_rec_inj {α : Sort*} {a a' : α} (h : a = a') {C : α → Type*} (x y : C a) : (eq.rec x h : C a') = eq.rec y h ↔ x = y := (eq_rec_on_bijective h).injective.eq_iff @[simp] lemma cast_inj {α β : Type*} (h : α = β) {x y : α} : cast h x = cast h y ↔ x = y := (cast_bijective h).injective.eq_iff /-- A set of functions "separates points" if for each pair of distinct points there is a function taking different values on them. -/ def set.separates_points {α β : Type*} (A : set (α → β)) : Prop := ∀ ⦃x y : α⦄, x ≠ y → ∃ f ∈ A, (f x : β) ≠ f y lemma is_symm_op.flip_eq {α β} (op) [is_symm_op α β op] : flip op = op := funext $ λ a, funext $ λ b, (is_symm_op.symm_op a b).symm
3ddf7916a500c131de729a3200638f1c3cf1e427
be30445afb85fcb2041b437059d21f9a84dff894
/diagrams.hlean
d209884c422780196d1de4e57ab9f62bfa7b0d9a
[ "Apache-2.0" ]
permissive
EgbertRijke/GraphModel
784efde97299c4f3cb1760369e8a260e02caafd5
2a473880764093dd151554a913292ed465e80e62
refs/heads/master
1,609,477,731,119
1,476,299,056,000
1,476,299,056,000
58,213,801
6
0
null
null
null
null
UTF-8
Lean
false
false
2,389
hlean
import types graphmodel open is_trunc eq sigma sigma.ops equiv is_equiv function sum graph namespace diagram structure ctx extends graph.ctx structure fam (Γ : ctx) : Type := ( pts : ctx.pts Γ → Type.{u} ) ( edg : Π {i j : ctx.pts Γ}, (ctx.edg Γ i j) → pts i → pts j) structure tm {Γ : ctx} (A : fam Γ) : Type := ( pts : Π (i : ctx.pts Γ), fam.pts A i ) ( edg : Π {i j : ctx.pts Γ} (e : ctx.edg Γ i j), fam.edg A e (pts i) = pts j ) end diagram definition diagram_ctx_from_graph_ctx : graph.ctx → diagram.ctx := λ Γ, diagram.ctx.mk _ (graph.ctx.edg Γ) structure is_diagram [class] {Γ : graph.ctx} (A : graph.fam Γ) : Type := ( witness : Π {i j : graph.ctx.pts Γ} (e : graph.ctx.edg Γ i j) (x : graph.fam.pts A i), is_contr (Σ (y : graph.fam.pts A j), graph.fam.edg A e x y)) attribute is_diagram.witness [instance] definition diagram_fam_from_graph_fam_from_is_diagram {Γ : graph.ctx} : Π (A : graph.fam Γ), is_diagram A → diagram.fam (diagram_ctx_from_graph_ctx Γ) := begin intros A H, fapply diagram.fam.mk, exact (@graph.fam.pts Γ A), intros i j e x, refine sigma.pr1 (@is_trunc.center (sigma (graph.fam.edg A e x)) _), exact is_diagram.witness e x end namespace diagram_tm_from_graph_tm_from_is_diagram variables {Γ : graph.ctx} {A : graph.fam Γ} [is_diagram A] (t : graph.tm A) definition pts : Π (i : ctx.pts Γ), fam.pts A i := graph.tm.pts t definition eq : Π {i j : ctx.pts Γ} (e : ctx.edg Γ i j), (center (Σ (a : fam.pts A j), fam.edg A e (tm.pts t i) a)).1 = tm.pts t j := begin intros i j e, have p : center _ = dpair (tm.pts t j) (tm.edg t e), from !center_eq, exact ap pr1 p, end end diagram_tm_from_graph_tm_from_is_diagram definition diagram_tm_from_graph_tm_from_is_diagram {Γ : graph.ctx} (A : graph.fam Γ) [H : is_diagram A] : graph.tm A → diagram.tm (diagram_fam_from_graph_fam_from_is_diagram A H) := begin intro t, fapply diagram.tm.mk, intro i, exact diagram_tm_from_graph_tm_from_is_diagram.pts t i, intro i j e, exact diagram_tm_from_graph_tm_from_is_diagram.eq t e, end ----------------------------------------------------------------------------------------------------
2b7453159e056948dbec9b0db985987aaca3496f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/polynomial/dickson.lean
d28c781076a683202eedba1bdb58b9ddf7576a24
[ "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
10,511
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import ring_theory.polynomial.chebyshev import ring_theory.localization import data.zmod.basic import algebra.char_p.invertible /-! # Dickson polynomials The (generalised) Dickson polynomials are a family of polynomials indexed by `ℕ × ℕ`, with coefficients in a commutative ring `R` depending on an element `a∈R`. More precisely, the they satisfy the recursion `dickson k a (n + 2) = X * (dickson k a n + 1) - a * (dickson k a n)` with starting values `dickson k a 0 = 3 - k` and `dickson k a 1 = X`. In the literature, `dickson k a n` is called the `n`-th Dickson polynomial of the `k`-th kind associated to the parameter `a : R`. They are closely related to the Chebyshev polynomials in the case that `a=1`. When `a=0` they are just the family of monomials `X ^ n`. ## Main definition * `polynomial.dickson`: the generalised Dickson polynomials. ## Main statements * `polynomial.dickson_one_one_mul`, the `(m * n)`-th Dickson polynomial of the first kind for parameter `1 : R` is the composition of the `m`-th and `n`-th Dickson polynomials of the first kind for `1 : R`. * `polynomial.dickson_one_one_char_p`, for a prime number `p`, the `p`-th Dickson polynomial of the first kind associated to parameter `1 : R` is congruent to `X ^ p` modulo `p`. ## References * [R. Lidl, G. L. Mullen and G. Turnwald, _Dickson polynomials_][MR1237403] ## TODO * Redefine `dickson` in terms of `linear_recurrence`. * Show that `dickson 2 1` is equal to the characteristic polynomial of the adjacency matrix of a type A Dynkin diagram. * Prove that the adjacency matrices of simply laced Dynkin diagrams are precisely the adjacency matrices of simple connected graphs which annihilate `dickson 2 1`. -/ noncomputable theory namespace polynomial variables {R S : Type*} [comm_ring R] [comm_ring S] (k : ℕ) (a : R) /-- `dickson` is the `n`the (generalised) Dickson polynomial of the `k`-th kind associated to the element `a ∈ R`. -/ noncomputable def dickson : ℕ → polynomial R | 0 := 3 - k | 1 := X | (n + 2) := X * dickson (n + 1) - (C a) * dickson n @[simp] lemma dickson_zero : dickson k a 0 = 3 - k := rfl @[simp] lemma dickson_one : dickson k a 1 = X := rfl lemma dickson_two : dickson k a 2 = X ^ 2 - C a * (3 - k) := by simp only [dickson, sq] @[simp] lemma dickson_add_two (n : ℕ) : dickson k a (n + 2) = X * dickson k a (n + 1) - C a * dickson k a n := by rw dickson lemma dickson_of_two_le {n : ℕ} (h : 2 ≤ n) : dickson k a n = X * dickson k a (n - 1) - C a * dickson k a (n - 2) := begin obtain ⟨n, rfl⟩ := nat.exists_eq_add_of_le h, rw add_comm, exact dickson_add_two k a n end variables {R S k a} lemma map_dickson (f : R →+* S) : ∀ (n : ℕ), map f (dickson k a n) = dickson k (f a) n | 0 := by simp only [dickson_zero, map_sub, map_nat_cast, bit1, bit0, map_add, map_one] | 1 := by simp only [dickson_one, map_X] | (n + 2) := begin simp only [dickson_add_two, map_sub, map_mul, map_X, map_C], rw [map_dickson, map_dickson] end variable {R} @[simp] lemma dickson_two_zero : ∀ (n : ℕ), dickson 2 (0 : R) n = X ^ n | 0 := by { simp only [dickson_zero, pow_zero], norm_num } | 1 := by simp only [dickson_one, pow_one] | (n + 2) := begin simp only [dickson_add_two, C_0, zero_mul, sub_zero], rw [dickson_two_zero, pow_add X (n + 1) 1, mul_comm, pow_one] end section dickson /-! ### A Lambda structure on `polynomial ℤ` Mathlib doesn't currently know what a Lambda ring is. But once it does, we can endow `polynomial ℤ` with a Lambda structure in terms of the `dickson 1 1` polynomials defined below. There is exactly one other Lambda structure on `polynomial ℤ` in terms of binomial polynomials. -/ variables {R} lemma dickson_one_one_eval_add_inv (x y : R) (h : x * y = 1) : ∀ n, (dickson 1 (1 : R) n).eval (x + y) = x ^ n + y ^ n | 0 := by { simp only [bit0, eval_one, eval_add, pow_zero, dickson_zero], norm_num } | 1 := by simp only [eval_X, dickson_one, pow_one] | (n + 2) := begin simp only [eval_sub, eval_mul, dickson_one_one_eval_add_inv, eval_X, dickson_add_two, C_1, eval_one], conv_lhs { simp only [pow_succ, add_mul, mul_add, h, ← mul_assoc, mul_comm y x, one_mul] }, ring_exp end variables (R) lemma dickson_one_one_eq_chebyshev_T [invertible (2 : R)] : ∀ n, dickson 1 (1 : R) n = 2 * (chebyshev.T R n).comp (C (⅟2) * X) | 0 := by { simp only [chebyshev.T_zero, mul_one, one_comp, dickson_zero], norm_num } | 1 := by rw [dickson_one, chebyshev.T_one, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul, mul_inv_of_self, C_1, one_mul] | (n + 2) := begin simp only [dickson_add_two, chebyshev.T_add_two, dickson_one_one_eq_chebyshev_T (n + 1), dickson_one_one_eq_chebyshev_T n, sub_comp, mul_comp, add_comp, X_comp, bit0_comp, one_comp], simp only [← C_1, ← C_bit0, ← mul_assoc, ← C_mul, mul_inv_of_self], rw [C_1, one_mul], ring end lemma chebyshev_T_eq_dickson_one_one [invertible (2 : R)] (n : ℕ) : chebyshev.T R n = C (⅟2) * (dickson 1 1 n).comp (2 * X) := begin rw dickson_one_one_eq_chebyshev_T, simp only [comp_assoc, mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul], rw [inv_of_mul_self, C_1, one_mul, one_mul, comp_X] end /-- The `(m * n)`-th Dickson polynomial of the first kind is the composition of the `m`-th and `n`-th. -/ lemma dickson_one_one_mul (m n : ℕ) : dickson 1 (1 : R) (m * n) = (dickson 1 1 m).comp (dickson 1 1 n) := begin have h : (1 : R) = int.cast_ring_hom R (1), simp only [ring_hom.eq_int_cast, int.cast_one], rw h, simp only [← map_dickson (int.cast_ring_hom R), ← map_comp], congr' 1, apply map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_dickson, map_comp, ring_hom.eq_int_cast, int.cast_one, dickson_one_one_eq_chebyshev_T, chebyshev.T_mul, two_mul, ← add_comp], simp only [← two_mul, ← comp_assoc], apply eval₂_congr rfl rfl, rw [comp_assoc], apply eval₂_congr rfl _ rfl, rw [mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul, inv_of_mul_self, C_1, one_mul] end lemma dickson_one_one_comp_comm (m n : ℕ) : (dickson 1 (1 : R) m).comp (dickson 1 1 n) = (dickson 1 1 n).comp (dickson 1 1 m) := by rw [← dickson_one_one_mul, mul_comm, dickson_one_one_mul] lemma dickson_one_one_zmod_p (p : ℕ) [fact p.prime] : dickson 1 (1 : zmod p) p = X ^ p := begin -- Recall that `dickson_eval_add_inv` characterises `dickson 1 1 p` -- as a polynomial that maps `x + x⁻¹` to `x ^ p + (x⁻¹) ^ p`. -- Since `X ^ p` also satisfies this property in characteristic `p`, -- we can use a variant on `polynomial.funext` to conclude that these polynomials are equal. -- For this argument, we need an arbitrary infinite field of characteristic `p`. obtain ⟨K, _, _, H⟩ : ∃ (K : Type) (_ : field K), by exactI ∃ (_ : char_p K p), infinite K, { let K := fraction_ring (polynomial (zmod p)), let f : zmod p →+* K := (algebra_map _ (fraction_ring _)).comp C, haveI : char_p K p, { rw ← f.char_p_iff_char_p, apply_instance }, haveI : infinite K := infinite.of_injective (algebra_map (polynomial (zmod p)) (fraction_ring (polynomial (zmod p)))) (is_fraction_ring.injective _ _), refine ⟨K, _, _, _⟩; apply_instance }, resetI, apply map_injective (zmod.cast_hom (dvd_refl p) K) (ring_hom.injective _), rw [map_dickson, map_pow, map_X], apply eq_of_infinite_eval_eq, -- The two polynomials agree on all `x` of the form `x = y + y⁻¹`. apply @set.infinite.mono _ {x : K | ∃ y, x = y + y⁻¹ ∧ y ≠ 0}, { rintro _ ⟨x, rfl, hx⟩, simp only [eval_X, eval_pow, set.mem_set_of_eq, @add_pow_char K _ p, dickson_one_one_eval_add_inv _ _ (mul_inv_cancel hx), inv_pow₀, zmod.cast_hom_apply, zmod.cast_one'] }, -- Now we need to show that the set of such `x` is infinite. -- If the set is finite, then we will show that `K` is also finite. { intro h, rw ← set.infinite_univ_iff at H, apply H, -- To each `x` of the form `x = y + y⁻¹` -- we `bind` the set of `y` that solve the equation `x = y + y⁻¹`. -- For every `x`, that set is finite (since it is governed by a quadratic equation). -- For the moment, we claim that all these sets together cover `K`. suffices : (set.univ : set K) = {x : K | ∃ (y : K), x = y + y⁻¹ ∧ y ≠ 0} >>= (λ x, {y | x = y + y⁻¹ ∨ y = 0}), { rw this, clear this, refine h.bUnion (λ x hx, _), -- The following quadratic polynomial has as solutions the `y` for which `x = y + y⁻¹`. let φ : polynomial K := X ^ 2 - C x * X + 1, have hφ : φ ≠ 0, { intro H, have : φ.eval 0 = 0, by rw [H, eval_zero], simpa [eval_X, eval_one, eval_pow, eval_sub, sub_zero, eval_add, eval_mul, mul_zero, sq, zero_add, one_ne_zero] }, classical, convert (φ.roots ∪ {0}).to_finset.finite_to_set using 1, ext1 y, simp only [multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe, multiset.mem_union, mem_roots hφ, is_root, eval_add, eval_sub, eval_pow, eval_mul, eval_X, eval_C, eval_one, multiset.mem_singleton], by_cases hy : y = 0, { simp only [hy, eq_self_iff_true, or_true] }, apply or_congr _ iff.rfl, rw [← mul_left_inj' hy, eq_comm, ← sub_eq_zero, add_mul, inv_mul_cancel hy], apply eq_iff_eq_cancel_right.mpr, ring }, -- Finally, we prove the claim that our finite union of finite sets covers all of `K`. { apply (set.eq_univ_of_forall _).symm, intro x, simp only [exists_prop, set.mem_Union, set.bind_def, ne.def, set.mem_set_of_eq], by_cases hx : x = 0, { simp only [hx, and_true, eq_self_iff_true, inv_zero, or_true], exact ⟨_, 1, rfl, one_ne_zero⟩ }, { simp only [hx, or_false, exists_eq_right], exact ⟨_, rfl, hx⟩ } } } end lemma dickson_one_one_char_p (p : ℕ) [fact p.prime] [char_p R p] : dickson 1 (1 : R) p = X ^ p := begin have h : (1 : R) = zmod.cast_hom (dvd_refl p) R (1), simp only [zmod.cast_hom_apply, zmod.cast_one'], rw [h, ← map_dickson (zmod.cast_hom (dvd_refl p) R), dickson_one_one_zmod_p, map_pow, map_X] end end dickson end polynomial
a84dd20255d0ea3e2f129622ae5cb56dd49f1e1c
42610cc2e5db9c90269470365e6056df0122eaa0
/hott/types/equiv.hlean
725e2cfbed5f53259932bd2e7e9ea82fb4d287be
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
11,891
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about the types equiv and is_equiv -/ import .fiber .arrow arity ..prop_trunc cubical.square open eq is_trunc sigma sigma.ops pi fiber function equiv namespace is_equiv variables {A B : Type} (f : A → B) [H : is_equiv f] include H /- is_equiv f is a mere proposition -/ definition is_contr_fiber_of_is_equiv [instance] (b : B) : is_contr (fiber f b) := is_contr.mk (fiber.mk (f⁻¹ b) (right_inv f b)) (λz, fiber.rec_on z (λa p, fiber_eq ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) (calc right_inv f b = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ((ap (f ∘ f⁻¹) p) ⬝ right_inv f b) : by rewrite inv_con_cancel_left ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (right_inv f (f a) ⬝ p) : by rewrite ap_con_eq_con ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (ap f (left_inv f a) ⬝ p) : by rewrite [adj f] ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite con.assoc ... = (ap f (ap f⁻¹ p))⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_compose ... = ap f (ap f⁻¹ p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_inv ... = ap f ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) ⬝ p : by rewrite ap_con))) definition is_contr_right_inverse : is_contr (Σ(g : B → A), f ∘ g ~ id) := begin fapply is_trunc_equiv_closed, {apply sigma_equiv_sigma_right, intro g, apply eq_equiv_homotopy}, fapply is_trunc_equiv_closed, {apply fiber.sigma_char}, fapply is_contr_fiber_of_is_equiv, apply (to_is_equiv (arrow_equiv_arrow_right B (equiv.mk f H))), end definition is_contr_right_coherence (u : Σ(g : B → A), f ∘ g ~ id) : is_contr (Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) := begin fapply is_trunc_equiv_closed, {apply equiv.symm, apply sigma_pi_equiv_pi_sigma}, fapply is_trunc_equiv_closed, {apply pi_equiv_pi_right, intro a, apply (fiber_eq_equiv (fiber.mk (u.1 (f a)) (u.2 (f a))) (fiber.mk a idp))}, end omit H protected definition sigma_char : (is_equiv f) ≃ (Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a)) := equiv.MK (λH, ⟨inv f, right_inv f, left_inv f, adj f⟩) (λp, is_equiv.mk f p.1 p.2.1 p.2.2.1 p.2.2.2) (λp, begin induction p with p1 p2, induction p2 with p21 p22, induction p22 with p221 p222, reflexivity end) (λH, by induction H; reflexivity) protected definition sigma_char' : (is_equiv f) ≃ (Σ(u : Σ(g : B → A), f ∘ g ~ id) (η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) := calc (is_equiv f) ≃ (Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a)) : is_equiv.sigma_char ... ≃ (Σ(u : Σ(g : B → A), f ∘ g ~ id), Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) : sigma_assoc_equiv (λu, Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) local attribute is_contr_right_inverse [instance] [priority 1600] local attribute is_contr_right_coherence [instance] [priority 1600] theorem is_prop_is_equiv [instance] : is_prop (is_equiv f) := is_prop_of_imp_is_contr (λ(H : is_equiv f), is_trunc_equiv_closed -2 (equiv.symm !is_equiv.sigma_char')) definition inv_eq_inv {A B : Type} {f f' : A → B} {Hf : is_equiv f} {Hf' : is_equiv f'} (p : f = f') : f⁻¹ = f'⁻¹ := apd011 inv p !is_prop.elim /- contractible fibers -/ definition is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f := is_contr_fiber_of_is_equiv f definition is_prop_is_contr_fun (f : A → B) : is_prop (is_contr_fun f) := _ definition is_equiv_of_is_contr_fun [H : is_contr_fun f] : is_equiv f := adjointify _ (λb, point (center (fiber f b))) (λb, point_eq (center (fiber f b))) (λa, ap point (center_eq (fiber.mk a idp))) definition is_equiv_of_imp_is_equiv (H : B → is_equiv f) : is_equiv f := @is_equiv_of_is_contr_fun _ _ f (λb, @is_contr_fiber_of_is_equiv _ _ _ (H b) _) definition is_equiv_equiv_is_contr_fun : is_equiv f ≃ is_contr_fun f := equiv_of_is_prop _ (λH, !is_equiv_of_is_contr_fun) end is_equiv /- Moving equivalences around in homotopies -/ namespace is_equiv variables {A B C : Type} (f : A → B) [Hf : is_equiv f] include Hf section pre_compose variables (α : A → C) (β : B → C) -- homotopy_inv_of_homotopy_pre is in init.equiv protected definition inv_homotopy_of_homotopy_pre.is_equiv : is_equiv (inv_homotopy_of_homotopy_pre f α β) := adjointify _ (homotopy_of_inv_homotopy_pre f α β) abstract begin intro q, apply eq_of_homotopy, intro b, unfold inv_homotopy_of_homotopy_pre, unfold homotopy_of_inv_homotopy_pre, apply inverse, apply eq_bot_of_square, apply eq_hconcat (ap02 α (adj_inv f b)), apply eq_hconcat (ap_compose α f⁻¹ (right_inv f b))⁻¹, apply natural_square_tr q (right_inv f b) end end abstract begin intro p, apply eq_of_homotopy, intro a, unfold inv_homotopy_of_homotopy_pre, unfold homotopy_of_inv_homotopy_pre, apply trans (con.assoc (ap α (left_inv f a))⁻¹ (p (f⁻¹ (f a))) (ap β (right_inv f (f a))))⁻¹, apply inverse, apply eq_bot_of_square, refine hconcat_eq _ (ap02 β (adj f a))⁻¹, refine hconcat_eq _ (ap_compose β f (left_inv f a)), apply natural_square_tr p (left_inv f a) end end end pre_compose section post_compose variables (α : C → A) (β : C → B) -- homotopy_inv_of_homotopy_post is in init.equiv protected definition inv_homotopy_of_homotopy_post.is_equiv : is_equiv (inv_homotopy_of_homotopy_post f α β) := adjointify _ (homotopy_of_inv_homotopy_post f α β) abstract begin intro q, apply eq_of_homotopy, intro c, unfold inv_homotopy_of_homotopy_post, unfold homotopy_of_inv_homotopy_post, apply trans (whisker_right (ap_con f⁻¹ (right_inv f (β c))⁻¹ (ap f (q c)) ⬝ whisker_right (ap_inv f⁻¹ (right_inv f (β c))) (ap f⁻¹ (ap f (q c)))) (left_inv f (α c))), apply inverse, apply eq_bot_of_square, apply eq_hconcat (adj_inv f (β c))⁻¹, apply eq_vconcat (ap_compose f⁻¹ f (q c))⁻¹, refine vconcat_eq _ (ap_id (q c)), apply natural_square (left_inv f) (q c) end end abstract begin intro p, apply eq_of_homotopy, intro c, unfold inv_homotopy_of_homotopy_post, unfold homotopy_of_inv_homotopy_post, apply trans (whisker_left (right_inv f (β c))⁻¹ (ap_con f (ap f⁻¹ (p c)) (left_inv f (α c)))), apply trans (con.assoc (right_inv f (β c))⁻¹ (ap f (ap f⁻¹ (p c))) (ap f (left_inv f (α c))))⁻¹, apply inverse, apply eq_bot_of_square, refine hconcat_eq _ (adj f (α c)), apply eq_vconcat (ap_compose f f⁻¹ (p c))⁻¹, refine vconcat_eq _ (ap_id (p c)), apply natural_square (right_inv f) (p c) end end end post_compose end is_equiv namespace is_equiv /- Theorem 4.7.7 -/ variables {A : Type} {P Q : A → Type} variable (f : Πa, P a → Q a) definition is_fiberwise_equiv [reducible] := Πa, is_equiv (f a) definition is_equiv_total_of_is_fiberwise_equiv [H : is_fiberwise_equiv f] : is_equiv (total f) := is_equiv_sigma_functor id f definition is_fiberwise_equiv_of_is_equiv_total [H : is_equiv (total f)] : is_fiberwise_equiv f := begin intro a, apply is_equiv_of_is_contr_fun, intro q, apply @is_contr_equiv_closed _ _ (fiber_total_equiv f q) end end is_equiv namespace equiv open is_equiv variables {A B C : Type} definition equiv_mk_eq {f f' : A → B} [H : is_equiv f] [H' : is_equiv f'] (p : f = f') : equiv.mk f H = equiv.mk f' H' := apd011 equiv.mk p !is_prop.elim definition equiv_eq' {f f' : A ≃ B} (p : to_fun f = to_fun f') : f = f' := by (cases f; cases f'; apply (equiv_mk_eq p)) definition equiv_eq {f f' : A ≃ B} (p : to_fun f ~ to_fun f') : f = f' := by apply equiv_eq'; apply eq_of_homotopy p definition trans_symm (f : A ≃ B) (g : B ≃ C) : (f ⬝e g)⁻¹ᵉ = g⁻¹ᵉ ⬝e f⁻¹ᵉ :> (C ≃ A) := equiv_eq' idp definition symm_symm (f : A ≃ B) : f⁻¹ᵉ⁻¹ᵉ = f :> (A ≃ B) := equiv_eq' idp protected definition equiv.sigma_char [constructor] (A B : Type) : (A ≃ B) ≃ Σ(f : A → B), is_equiv f := begin fapply equiv.MK, {intro F, exact ⟨to_fun F, to_is_equiv F⟩}, {intro p, cases p with f H, exact (equiv.mk f H)}, {intro p, cases p, exact idp}, {intro F, cases F, exact idp}, end definition equiv_eq_char (f f' : A ≃ B) : (f = f') ≃ (to_fun f = to_fun f') := calc (f = f') ≃ (to_fun !equiv.sigma_char f = to_fun !equiv.sigma_char f') : eq_equiv_fn_eq (to_fun !equiv.sigma_char) ... ≃ ((to_fun !equiv.sigma_char f).1 = (to_fun !equiv.sigma_char f').1 ) : equiv_subtype ... ≃ (to_fun f = to_fun f') : equiv.rfl definition is_equiv_ap_to_fun (f f' : A ≃ B) : is_equiv (ap to_fun : f = f' → to_fun f = to_fun f') := begin fapply adjointify, {intro p, cases f with f H, cases f' with f' H', cases p, apply ap (mk f'), apply is_prop.elim}, {intro p, cases f with f H, cases f' with f' H', cases p, apply @concat _ _ (ap to_fun (ap (equiv.mk f') (is_prop.elim H H'))), {apply idp}, generalize is_prop.elim H H', intro q, cases q, apply idp}, {intro p, cases p, cases f with f H, apply ap (ap (equiv.mk f)), apply is_set.elim} end definition equiv_pathover {A : Type} {a a' : A} (p : a = a') {B : A → Type} {C : A → Type} (f : B a ≃ C a) (g : B a' ≃ C a') (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p] g := begin fapply pathover_of_fn_pathover_fn, { intro a, apply equiv.sigma_char}, { fapply sigma_pathover, esimp, apply arrow_pathover, exact r, apply is_prop.elimo} end definition is_contr_equiv (A B : Type) [HA : is_contr A] [HB : is_contr B] : is_contr (A ≃ B) := begin apply @is_contr_of_inhabited_prop, apply is_prop.mk, intro x y, cases x with fx Hx, cases y with fy Hy, generalize Hy, apply (eq_of_homotopy (λ a, !eq_of_is_contr)) ▸ (λ Hy, !is_prop.elim ▸ rfl), apply equiv_of_is_contr_of_is_contr end definition is_trunc_succ_equiv (n : trunc_index) (A B : Type) [HA : is_trunc n.+1 A] [HB : is_trunc n.+1 B] : is_trunc n.+1 (A ≃ B) := @is_trunc_equiv_closed _ _ n.+1 (equiv.symm !equiv.sigma_char) (@is_trunc_sigma _ _ _ _ (λ f, !is_trunc_succ_of_is_prop)) definition is_trunc_equiv (n : trunc_index) (A B : Type) [HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A ≃ B) := by cases n; apply !is_contr_equiv; apply !is_trunc_succ_equiv definition eq_of_fn_eq_fn'_idp {A B : Type} (f : A → B) [is_equiv f] (x : A) : eq_of_fn_eq_fn' f (idpath (f x)) = idpath x := !con.left_inv definition eq_of_fn_eq_fn'_con {A B : Type} (f : A → B) [is_equiv f] {x y z : A} (p : f x = f y) (q : f y = f z) : eq_of_fn_eq_fn' f (p ⬝ q) = eq_of_fn_eq_fn' f p ⬝ eq_of_fn_eq_fn' f q := begin unfold eq_of_fn_eq_fn', refine _ ⬝ !con.assoc, apply whisker_right, refine _ ⬝ !con.assoc⁻¹ ⬝ !con.assoc⁻¹, apply whisker_left, refine !ap_con ⬝ _, apply whisker_left, refine !con_inv_cancel_left⁻¹ end end equiv
b74f3486daa452ca267bebd6191852fe337002be
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/continuous_function/stone_weierstrass.lean
af6f6ce19e9e1c44212cb83c2f6b2deb0ef6adc8
[ "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
19,785
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Heather Macbeth -/ import topology.continuous_function.weierstrass import analysis.complex.basic /-! # The Stone-Weierstrass theorem If a subalgebra `A` of `C(X, ℝ)`, where `X` is a compact topological space, separates points, then it is dense. We argue as follows. * In any subalgebra `A` of `C(X, ℝ)`, if `f ∈ A`, then `abs f ∈ A.topological_closure`. This follows from the Weierstrass approximation theorem on `[-∥f∥, ∥f∥]` by approximating `abs` uniformly thereon by polynomials. * This ensures that `A.topological_closure` is actually a sublattice: if it contains `f` and `g`, then it contains the pointwise supremum `f ⊔ g` and the pointwise infimum `f ⊓ g`. * Any nonempty sublattice `L` of `C(X, ℝ)` which separates points is dense, by a nice argument approximating a given `f` above and below using separating functions. For each `x y : X`, we pick a function `g x y ∈ L` so `g x y x = f x` and `g x y y = f y`. By continuity these functions remain close to `f` on small patches around `x` and `y`. We use compactness to identify a certain finitely indexed infimum of finitely indexed supremums which is then close to `f` everywhere, obtaining the desired approximation. * Finally we put these pieces together. `L = A.topological_closure` is a nonempty sublattice which separates points since `A` does, and so is dense (in fact equal to `⊤`). We then prove the complex version for self-adjoint subalgebras `A`, by separately approximating the real and imaginary parts using the real subalgebra of real-valued functions in `A` (which still separates points, by taking the norm-square of a separating function). ## Future work Extend to cover the case of subalgebras of the continuous functions vanishing at infinity, on non-compact spaces. -/ noncomputable theory namespace continuous_map variables {X : Type*} [topological_space X] [compact_space X] /-- Turn a function `f : C(X, ℝ)` into a continuous map into `set.Icc (-∥f∥) (∥f∥)`, thereby explicitly attaching bounds. -/ def attach_bound (f : C(X, ℝ)) : C(X, set.Icc (-∥f∥) (∥f∥)) := { to_fun := λ x, ⟨f x, ⟨neg_norm_le_apply f x, apply_le_norm f x⟩⟩ } @[simp] lemma attach_bound_apply_coe (f : C(X, ℝ)) (x : X) : ((attach_bound f) x : ℝ) = f x := rfl lemma polynomial_comp_attach_bound (A : subalgebra ℝ C(X, ℝ)) (f : A) (g : polynomial ℝ) : (g.to_continuous_map_on (set.Icc (-∥f∥) ∥f∥)).comp (f : C(X, ℝ)).attach_bound = polynomial.aeval f g := begin ext, simp only [continuous_map.comp_coe, function.comp_app, continuous_map.attach_bound_apply_coe, polynomial.to_continuous_map_on_to_fun, polynomial.aeval_subalgebra_coe, polynomial.aeval_continuous_map_apply, polynomial.to_continuous_map_to_fun], end /-- Given a continuous function `f` in a subalgebra of `C(X, ℝ)`, postcomposing by a polynomial gives another function in `A`. This lemma proves something slightly more subtle than this: we take `f`, and think of it as a function into the restricted target `set.Icc (-∥f∥) ∥f∥)`, and then postcompose with a polynomial function on that interval. This is in fact the same situation as above, and so also gives a function in `A`. -/ lemma polynomial_comp_attach_bound_mem (A : subalgebra ℝ C(X, ℝ)) (f : A) (g : polynomial ℝ) : (g.to_continuous_map_on (set.Icc (-∥f∥) ∥f∥)).comp (f : C(X, ℝ)).attach_bound ∈ A := begin rw polynomial_comp_attach_bound, apply set_like.coe_mem, end theorem comp_attach_bound_mem_closure (A : subalgebra ℝ C(X, ℝ)) (f : A) (p : C(set.Icc (-∥f∥) (∥f∥), ℝ)) : p.comp (attach_bound f) ∈ A.topological_closure := begin -- `p` itself is in the closure of polynomials, by the Weierstrass theorem, have mem_closure : p ∈ (polynomial_functions (set.Icc (-∥f∥) (∥f∥))).topological_closure := continuous_map_mem_polynomial_functions_closure _ _ p, -- and so there are polynomials arbitrarily close. have frequently_mem_polynomials := mem_closure_iff_frequently.mp mem_closure, -- To prove `p.comp (attached_bound f)` is in the closure of `A`, -- we show there are elements of `A` arbitrarily close. apply mem_closure_iff_frequently.mpr, -- To show that, we pull back the polynomials close to `p`, refine ((comp_right_continuous_map ℝ (attach_bound (f : C(X, ℝ)))).continuous_at p).tendsto .frequently_map _ _ frequently_mem_polynomials, -- but need to show that those pullbacks are actually in `A`. rintros _ ⟨g, ⟨-,rfl⟩⟩, simp only [set_like.mem_coe, alg_hom.coe_to_ring_hom, comp_right_continuous_map_apply, polynomial.to_continuous_map_on_alg_hom_apply], apply polynomial_comp_attach_bound_mem, end theorem abs_mem_subalgebra_closure (A : subalgebra ℝ C(X, ℝ)) (f : A) : (f : C(X, ℝ)).abs ∈ A.topological_closure := begin let M := ∥f∥, let f' := attach_bound (f : C(X, ℝ)), let abs : C(set.Icc (-∥f∥) (∥f∥), ℝ) := { to_fun := λ x : set.Icc (-∥f∥) (∥f∥), |(x : ℝ)| }, change (abs.comp f') ∈ A.topological_closure, apply comp_attach_bound_mem_closure, end theorem inf_mem_subalgebra_closure (A : subalgebra ℝ C(X, ℝ)) (f g : A) : (f : C(X, ℝ)) ⊓ (g : C(X, ℝ)) ∈ A.topological_closure := begin rw inf_eq, refine A.topological_closure.smul_mem (A.topological_closure.sub_mem (A.topological_closure.add_mem (A.subalgebra_topological_closure f.property) (A.subalgebra_topological_closure g.property)) _) _, exact_mod_cast abs_mem_subalgebra_closure A _, end theorem inf_mem_closed_subalgebra (A : subalgebra ℝ C(X, ℝ)) (h : is_closed (A : set C(X, ℝ))) (f g : A) : (f : C(X, ℝ)) ⊓ (g : C(X, ℝ)) ∈ A := begin convert inf_mem_subalgebra_closure A f g, apply set_like.ext', symmetry, erw closure_eq_iff_is_closed, exact h, end theorem sup_mem_subalgebra_closure (A : subalgebra ℝ C(X, ℝ)) (f g : A) : (f : C(X, ℝ)) ⊔ (g : C(X, ℝ)) ∈ A.topological_closure := begin rw sup_eq, refine A.topological_closure.smul_mem (A.topological_closure.add_mem (A.topological_closure.add_mem (A.subalgebra_topological_closure f.property) (A.subalgebra_topological_closure g.property)) _) _, exact_mod_cast abs_mem_subalgebra_closure A _, end theorem sup_mem_closed_subalgebra (A : subalgebra ℝ C(X, ℝ)) (h : is_closed (A : set C(X, ℝ))) (f g : A) : (f : C(X, ℝ)) ⊔ (g : C(X, ℝ)) ∈ A := begin convert sup_mem_subalgebra_closure A f g, apply set_like.ext', symmetry, erw closure_eq_iff_is_closed, exact h, end open_locale topological_space -- Here's the fun part of Stone-Weierstrass! theorem sublattice_closure_eq_top (L : set C(X, ℝ)) (nA : L.nonempty) (inf_mem : ∀ f g ∈ L, f ⊓ g ∈ L) (sup_mem : ∀ f g ∈ L, f ⊔ g ∈ L) (sep : L.separates_points_strongly) : closure L = ⊤ := begin -- We start by boiling down to a statement about close approximation. apply eq_top_iff.mpr, rintros f -, refine filter.frequently.mem_closure ((filter.has_basis.frequently_iff metric.nhds_basis_ball).mpr (λ ε pos, _)), simp only [exists_prop, metric.mem_ball], -- It will be helpful to assume `X` is nonempty later, -- so we get that out of the way here. by_cases nX : nonempty X, swap, exact ⟨nA.some, (dist_lt_iff pos).mpr (λ x, false.elim (nX ⟨x⟩)), nA.some_spec⟩, /- The strategy now is to pick a family of continuous functions `g x y` in `A` with the property that `g x y x = f x` and `g x y y = f y` (this is immediate from `h : separates_points_strongly`) then use continuity to see that `g x y` is close to `f` near both `x` and `y`, and finally using compactness to produce the desired function `h` as a maximum over finitely many `x` of a minimum over finitely many `y` of the `g x y`. -/ dsimp [set.separates_points_strongly] at sep, let g : X → X → L := λ x y, (sep f x y).some, have w₁ : ∀ x y, g x y x = f x := λ x y, (sep f x y).some_spec.1, have w₂ : ∀ x y, g x y y = f y := λ x y, (sep f x y).some_spec.2, -- For each `x y`, we define `U x y` to be `{z | f z - ε < g x y z}`, -- and observe this is a neighbourhood of `y`. let U : X → X → set X := λ x y, {z | f z - ε < g x y z}, have U_nhd_y : ∀ x y, U x y ∈ 𝓝 y, { intros x y, refine is_open.mem_nhds _ _, { apply is_open_lt; continuity, }, { rw [set.mem_set_of_eq, w₂], exact sub_lt_self _ pos, }, }, -- Fixing `x` for a moment, we have a family of functions `λ y, g x y` -- which on different patches (the `U x y`) are greater than `f z - ε`. -- Taking the supremum of these functions -- indexed by a finite collection of patches which cover `X` -- will give us an element of `A` that is globally greater than `f z - ε` -- and still equal to `f x` at `x`. -- Since `X` is compact, for every `x` there is some finset `ys t` -- so the union of the `U x y` for `y ∈ ys x` still covers everything. let ys : Π x, finset X := λ x, (compact_space.elim_nhds_subcover (U x) (U_nhd_y x)).some, let ys_w : ∀ x, (⋃ y ∈ ys x, U x y) = ⊤ := λ x, (compact_space.elim_nhds_subcover (U x) (U_nhd_y x)).some_spec, have ys_nonempty : ∀ x, (ys x).nonempty := λ x, set.nonempty_of_union_eq_top_of_nonempty _ _ nX (ys_w x), -- Thus for each `x` we have the desired `h x : A` so `f z - ε < h x z` everywhere -- and `h x x = f x`. let h : Π x, L := λ x, ⟨(ys x).sup' (ys_nonempty x) (λ y, (g x y : C(X, ℝ))), finset.sup'_mem _ sup_mem _ _ _ (λ y _, (g x y).2)⟩, have lt_h : ∀ x z, f z - ε < h x z, { intros x z, obtain ⟨y, ym, zm⟩ := set.exists_set_mem_of_union_eq_top _ _ (ys_w x) z, dsimp [h], simp only [coe_fn_coe_base', subtype.coe_mk, sup'_coe, finset.sup'_apply, finset.lt_sup'_iff], exact ⟨y, ym, zm⟩ }, have h_eq : ∀ x, h x x = f x, { intro x, simp only [coe_fn_coe_base'] at w₁, simp [coe_fn_coe_base', w₁], }, -- For each `x`, we define `W x` to be `{z | h x z < f z + ε}`, let W : Π x, set X := λ x, {z | h x z < f z + ε}, -- This is still a neighbourhood of `x`. have W_nhd : ∀ x, W x ∈ 𝓝 x, { intros x, refine is_open.mem_nhds _ _, { apply is_open_lt; continuity, }, { dsimp only [W, set.mem_set_of_eq], rw h_eq, exact lt_add_of_pos_right _ pos}, }, -- Since `X` is compact, there is some finset `ys t` -- so the union of the `W x` for `x ∈ xs` still covers everything. let xs : finset X := (compact_space.elim_nhds_subcover W W_nhd).some, let xs_w : (⋃ x ∈ xs, W x) = ⊤ := (compact_space.elim_nhds_subcover W W_nhd).some_spec, have xs_nonempty : xs.nonempty := set.nonempty_of_union_eq_top_of_nonempty _ _ nX xs_w, -- Finally our candidate function is the infimum over `x ∈ xs` of the `h x`. -- This function is then globally less than `f z + ε`. let k : (L : Type*) := ⟨xs.inf' xs_nonempty (λ x, (h x : C(X, ℝ))), finset.inf'_mem _ inf_mem _ _ _ (λ x _, (h x).2)⟩, refine ⟨k.1, _, k.2⟩, -- We just need to verify the bound, which we do pointwise. rw dist_lt_iff pos, intro z, -- We rewrite into this particular form, -- so that simp lemmas about inequalities involving `finset.inf'` can fire. rw [(show ∀ a b ε : ℝ, dist a b < ε ↔ a < b + ε ∧ b - ε < a, by { intros, simp only [← metric.mem_ball, real.ball_eq_Ioo, set.mem_Ioo, and_comm], })], fsplit, { dsimp [k], simp only [finset.inf'_lt_iff, continuous_map.inf'_apply], exact set.exists_set_mem_of_union_eq_top _ _ xs_w z, }, { dsimp [k], simp only [finset.lt_inf'_iff, continuous_map.inf'_apply], intros x xm, apply lt_h, }, end /-- The **Stone-Weierstrass Approximation Theorem**, that a subalgebra `A` of `C(X, ℝ)`, where `X` is a compact topological space, is dense if it separates points. -/ theorem subalgebra_topological_closure_eq_top_of_separates_points (A : subalgebra ℝ C(X, ℝ)) (w : A.separates_points) : A.topological_closure = ⊤ := begin -- The closure of `A` is closed under taking `sup` and `inf`, -- and separates points strongly (since `A` does), -- so we can apply `sublattice_closure_eq_top`. apply set_like.ext', let L := A.topological_closure, have n : set.nonempty (L : set C(X, ℝ)) := ⟨(1 : C(X, ℝ)), A.subalgebra_topological_closure A.one_mem⟩, convert sublattice_closure_eq_top (L : set C(X, ℝ)) n (λ f fm g gm, inf_mem_closed_subalgebra L A.is_closed_topological_closure ⟨f, fm⟩ ⟨g, gm⟩) (λ f fm g gm, sup_mem_closed_subalgebra L A.is_closed_topological_closure ⟨f, fm⟩ ⟨g, gm⟩) (subalgebra.separates_points.strongly (subalgebra.separates_points_monotone (A.subalgebra_topological_closure) w)), { simp, }, end /-- An alternative statement of the Stone-Weierstrass theorem. If `A` is a subalgebra of `C(X, ℝ)` which separates points (and `X` is compact), every real-valued continuous function on `X` is a uniform limit of elements of `A`. -/ theorem continuous_map_mem_subalgebra_closure_of_separates_points (A : subalgebra ℝ C(X, ℝ)) (w : A.separates_points) (f : C(X, ℝ)) : f ∈ A.topological_closure := begin rw subalgebra_topological_closure_eq_top_of_separates_points A w, simp, end /-- An alternative statement of the Stone-Weierstrass theorem, for those who like their epsilons. If `A` is a subalgebra of `C(X, ℝ)` which separates points (and `X` is compact), every real-valued continuous function on `X` is within any `ε > 0` of some element of `A`. -/ theorem exists_mem_subalgebra_near_continuous_map_of_separates_points (A : subalgebra ℝ C(X, ℝ)) (w : A.separates_points) (f : C(X, ℝ)) (ε : ℝ) (pos : 0 < ε) : ∃ (g : A), ∥(g : C(X, ℝ)) - f∥ < ε := begin have w := mem_closure_iff_frequently.mp (continuous_map_mem_subalgebra_closure_of_separates_points A w f), rw metric.nhds_basis_ball.frequently_iff at w, obtain ⟨g, H, m⟩ := w ε pos, rw [metric.mem_ball, dist_eq_norm] at H, exact ⟨⟨g, m⟩, H⟩, end /-- An alternative statement of the Stone-Weierstrass theorem, for those who like their epsilons and don't like bundled continuous functions. If `A` is a subalgebra of `C(X, ℝ)` which separates points (and `X` is compact), every real-valued continuous function on `X` is within any `ε > 0` of some element of `A`. -/ theorem exists_mem_subalgebra_near_continuous_of_separates_points (A : subalgebra ℝ C(X, ℝ)) (w : A.separates_points) (f : X → ℝ) (c : continuous f) (ε : ℝ) (pos : 0 < ε) : ∃ (g : A), ∀ x, ∥g x - f x∥ < ε := begin obtain ⟨g, b⟩ := exists_mem_subalgebra_near_continuous_map_of_separates_points A w ⟨f, c⟩ ε pos, use g, rwa norm_lt_iff _ pos at b, end end continuous_map section complex open complex -- Redefine `X`, since for the next few lemmas it need not be compact variables {X : Type*} [topological_space X] namespace continuous_map /-- A real subalgebra of `C(X, ℂ)` is `conj_invariant`, if it contains all its conjugates. -/ def conj_invariant_subalgebra (A : subalgebra ℝ C(X, ℂ)) : Prop := A.map (conj_ae.to_alg_hom.comp_left_continuous ℝ conj_cle.continuous) ≤ A lemma mem_conj_invariant_subalgebra {A : subalgebra ℝ C(X, ℂ)} (hA : conj_invariant_subalgebra A) {f : C(X, ℂ)} (hf : f ∈ A) : (conj_ae.to_alg_hom.comp_left_continuous ℝ conj_cle.continuous) f ∈ A := hA ⟨f, hf, rfl⟩ end continuous_map open continuous_map /-- If a conjugation-invariant subalgebra of `C(X, ℂ)` separates points, then the real subalgebra of its purely real-valued elements also separates points. -/ lemma subalgebra.separates_points.complex_to_real {A : subalgebra ℂ C(X, ℂ)} (hA : A.separates_points) (hA' : conj_invariant_subalgebra (A.restrict_scalars ℝ)) : ((A.restrict_scalars ℝ).comap' (of_real_am.comp_left_continuous ℝ continuous_of_real)).separates_points := begin intros x₁ x₂ hx, -- Let `f` in the subalgebra `A` separate the points `x₁`, `x₂` obtain ⟨_, ⟨f, hfA, rfl⟩, hf⟩ := hA hx, let F : C(X, ℂ) := f - const (f x₂), -- Subtract the constant `f x₂` from `f`; this is still an element of the subalgebra have hFA : F ∈ A, { refine A.sub_mem hfA _, convert A.smul_mem A.one_mem (f x₂), ext1, simp }, -- Consider now the function `λ x, |f x - f x₂| ^ 2` refine ⟨_, ⟨(⟨complex.norm_sq, continuous_norm_sq⟩ : C(ℂ, ℝ)).comp F, _, rfl⟩, _⟩, { -- This is also an element of the subalgebra, and takes only real values rw [set_like.mem_coe, subalgebra.mem_comap], convert (A.restrict_scalars ℝ).mul_mem (mem_conj_invariant_subalgebra hA' hFA) hFA, ext1, exact complex.norm_sq_eq_conj_mul_self }, { -- And it also separates the points `x₁`, `x₂` have : f x₁ - f x₂ ≠ 0 := sub_ne_zero.mpr hf, simpa using this }, end variables [compact_space X] /-- The Stone-Weierstrass approximation theorem, complex version, that a subalgebra `A` of `C(X, ℂ)`, where `X` is a compact topological space, is dense if it is conjugation-invariant and separates points. -/ theorem continuous_map.subalgebra_complex_topological_closure_eq_top_of_separates_points (A : subalgebra ℂ C(X, ℂ)) (hA : A.separates_points) (hA' : conj_invariant_subalgebra (A.restrict_scalars ℝ)) : A.topological_closure = ⊤ := begin rw algebra.eq_top_iff, -- Let `I` be the natural inclusion of `C(X, ℝ)` into `C(X, ℂ)` let I : C(X, ℝ) →ₗ[ℝ] C(X, ℂ) := of_real_clm.comp_left_continuous ℝ X, -- The main point of the proof is that its range (i.e., every real-valued function) is contained -- in the closure of `A` have key : I.range ≤ (A.to_submodule.restrict_scalars ℝ).topological_closure, { -- Let `A₀` be the subalgebra of `C(X, ℝ)` consisting of `A`'s purely real elements; it is the -- preimage of `A` under `I`. In this argument we only need its submodule structure. let A₀ : submodule ℝ C(X, ℝ) := (A.to_submodule.restrict_scalars ℝ).comap I, -- By `subalgebra.separates_points.complex_to_real`, this subalgebra also separates points, so -- we may apply the real Stone-Weierstrass result to it. have SW : A₀.topological_closure = ⊤, { have := subalgebra_topological_closure_eq_top_of_separates_points _ (hA.complex_to_real hA'), exact congr_arg subalgebra.to_submodule this }, rw [← submodule.map_top, ← SW], -- So it suffices to prove that the image under `I` of the closure of `A₀` is contained in the -- closure of `A`, which follows by abstract nonsense have h₁ := A₀.topological_closure_map (of_real_clm.comp_left_continuous_compact X), have h₂ := (A.to_submodule.restrict_scalars ℝ).map_comap_le I, exact h₁.trans (submodule.topological_closure_mono h₂) }, -- In particular, for a function `f` in `C(X, ℂ)`, the real and imaginary parts of `f` are in the -- closure of `A` intros f, let f_re : C(X, ℝ) := (⟨complex.re, complex.re_clm.continuous⟩ : C(ℂ, ℝ)).comp f, let f_im : C(X, ℝ) := (⟨complex.im, complex.im_clm.continuous⟩ : C(ℂ, ℝ)).comp f, have h_f_re : I f_re ∈ A.topological_closure := key ⟨f_re, rfl⟩, have h_f_im : I f_im ∈ A.topological_closure := key ⟨f_im, rfl⟩, -- So `f_re + complex.I • f_im` is in the closure of `A` convert A.topological_closure.add_mem h_f_re (A.topological_closure.smul_mem h_f_im complex.I), -- And this, of course, is just `f` ext; simp [I] end end complex
c303bb7ecb87486dce42fa7db42666fd2b6b3bc4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/bounds/order_iso.lean
b90ea907458e5448f521e4104b2096d8c11b8d53
[ "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
2,090
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import order.bounds.basic import order.hom.set /-! # Order isomorhpisms and bounds. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α β : Type*} open set namespace order_iso variables [preorder α] [preorder β] (f : α ≃o β) lemma upper_bounds_image {s : set α} : upper_bounds (f '' s) = f '' upper_bounds s := subset.antisymm (λ x hx, ⟨f.symm x, λ y hy, f.le_symm_apply.2 (hx $ mem_image_of_mem _ hy), f.apply_symm_apply x⟩) f.monotone.image_upper_bounds_subset_upper_bounds_image lemma lower_bounds_image {s : set α} : lower_bounds (f '' s) = f '' lower_bounds s := @upper_bounds_image αᵒᵈ βᵒᵈ _ _ f.dual _ @[simp] lemma is_lub_image {s : set α} {x : β} : is_lub (f '' s) x ↔ is_lub s (f.symm x) := ⟨λ h, is_lub.of_image (λ _ _, f.le_iff_le) ((f.apply_symm_apply x).symm ▸ h), λ h, is_lub.of_image (λ _ _, f.symm.le_iff_le) $ (f.symm_image_image s).symm ▸ h⟩ lemma is_lub_image' {s : set α} {x : α} : is_lub (f '' s) (f x) ↔ is_lub s x := by rw [is_lub_image, f.symm_apply_apply] @[simp] lemma is_glb_image {s : set α} {x : β} : is_glb (f '' s) x ↔ is_glb s (f.symm x) := f.dual.is_lub_image lemma is_glb_image' {s : set α} {x : α} : is_glb (f '' s) (f x) ↔ is_glb s x := f.dual.is_lub_image' @[simp] lemma is_lub_preimage {s : set β} {x : α} : is_lub (f ⁻¹' s) x ↔ is_lub s (f x) := by rw [← f.symm_symm, ← image_eq_preimage, is_lub_image] lemma is_lub_preimage' {s : set β} {x : β} : is_lub (f ⁻¹' s) (f.symm x) ↔ is_lub s x := by rw [is_lub_preimage, f.apply_symm_apply] @[simp] lemma is_glb_preimage {s : set β} {x : α} : is_glb (f ⁻¹' s) x ↔ is_glb s (f x) := f.dual.is_lub_preimage lemma is_glb_preimage' {s : set β} {x : β} : is_glb (f ⁻¹' s) (f.symm x) ↔ is_glb s x := f.dual.is_lub_preimage' end order_iso
530ee920dce64fb9c4016b83865588d8d9019c11
4fa161becb8ce7378a709f5992a594764699e268
/src/data/set/finite.lean
fa8d7c6f5bd2a81a7a809d6e6812589d9ed18de9
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
24,300
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.fintype.basic import algebra.big_operators /-! # Finite sets This file defines predicates `finite : set α → Prop` and `infinite : set α → Prop` and proves some basic facts about finite sets. -/ open set function open_locale big_operators universes u v w x variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x} namespace set /-- A set is finite if the subtype is a fintype, i.e. there is a list that enumerates its members. -/ def finite (s : set α) : Prop := nonempty (fintype s) /-- A set is infinite if it is not finite. -/ def infinite (s : set α) : Prop := ¬ finite s /-- The subtype corresponding to a finite set is a finite type. Note that because `finite` isn't a typeclass, this will not fire if it is made into an instance -/ noncomputable def finite.fintype {s : set α} (h : finite s) : fintype s := classical.choice h /-- Get a finset from a finite set -/ noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α := @set.to_finset _ _ h.fintype @[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s := @mem_to_finset _ _ h.fintype _ @[simp] lemma finite.coe_to_finset {α} {s : set α} (h : finite s) : ↑h.to_finset = s := @set.coe_to_finset _ s h.fintype theorem finite.exists_finset {s : set α} : finite s → ∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s | ⟨h⟩ := by exactI ⟨to_finset s, λ _, mem_to_finset⟩ theorem finite.exists_finset_coe {s : set α} (hs : finite s) : ∃ s' : finset α, ↑s' = s := ⟨hs.to_finset, hs.coe_to_finset⟩ /-- Finite sets can be lifted to finsets. -/ instance : can_lift (set α) (finset α) := { coe := coe, cond := finite, prf := λ s hs, hs.exists_finset_coe } theorem finite_mem_finset (s : finset α) : finite {a | a ∈ s} := ⟨fintype.of_finset s (λ _, iff.rfl)⟩ theorem finite.of_fintype [fintype α] (s : set α) : finite s := by classical; exact ⟨set_fintype s⟩ /-- Membership of a subset of a finite type is decidable. Using this as an instance leads to potential loops with `subtype.fintype` under certain decidability assumptions, so it should only be declared a local instance. -/ def decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) := decidable_of_iff _ mem_to_finset instance fintype_empty : fintype (∅ : set α) := fintype.of_finset ∅ $ by simp theorem empty_card : fintype.card (∅ : set α) = 0 := rfl @[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} : @fintype.card (∅ : set α) h = 0 := eq.trans (by congr) empty_card @[simp] theorem finite_empty : @finite α ∅ := ⟨set.fintype_empty⟩ /-- A `fintype` structure on `insert a s`. -/ def fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : fintype (insert a s : set α) := fintype.of_finset ⟨a :: s.to_finset.1, multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp theorem card_fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : @fintype.card _ (fintype_insert' s h) = fintype.card s + 1 := by rw [fintype_insert', fintype.card_of_finset]; simp [finset.card, to_finset]; refl @[simp] theorem card_insert {a : α} (s : set α) [fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} : @fintype.card _ d = fintype.card s + 1 := by rw ← card_fintype_insert' s h; congr lemma card_image_of_inj_on {s : set α} [fintype s] {f : α → β} [fintype (f '' s)] (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : fintype.card (f '' s) = fintype.card s := by haveI := classical.prop_decidable; exact calc fintype.card (f '' s) = (s.to_finset.image f).card : fintype.card_of_finset' _ (by simp) ... = s.to_finset.card : finset.card_image_of_inj_on (λ x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy) ... = fintype.card s : (fintype.card_of_finset' _ (λ a, mem_to_finset)).symm lemma card_image_of_injective (s : set α) [fintype s] {f : α → β} [fintype (f '' s)] (H : function.injective f) : fintype.card (f '' s) = fintype.card s := card_image_of_inj_on $ λ _ _ _ _ h, H h section local attribute [instance] decidable_mem_of_fintype instance fintype_insert [decidable_eq α] (a : α) (s : set α) [fintype s] : fintype (insert a s : set α) := if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)] else fintype_insert' _ h end @[simp] theorem finite_insert (a : α) {s : set α} : finite s → finite (insert a s) | ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq α) _ _ h⟩ lemma to_finset_insert [decidable_eq α] {a : α} {s : set α} (hs : finite s) : (finite_insert a hs).to_finset = insert a hs.to_finset := finset.ext $ by simp @[elab_as_eliminator] theorem finite.induction_on {C : set α → Prop} {s : set α} (h : finite s) (H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → finite s → C s → C (insert a s)) : C s := let ⟨t⟩ := h in by exactI match s.to_finset, @mem_to_finset _ s _ with | ⟨l, nd⟩, al := begin change ∀ a, a ∈ l ↔ a ∈ s at al, clear _let_match _match t h, revert s nd al, refine multiset.induction_on l _ (λ a l IH, _); intros s nd al, { rw show s = ∅, from eq_empty_iff_forall_not_mem.2 (by simpa using al), exact H0 }, { rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al), cases multiset.nodup_cons.1 nd with m nd', refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (λ _, iff.rfl)), exact m } end end @[elab_as_eliminator] theorem finite.dinduction_on {C : ∀s:set α, finite s → Prop} {s : set α} (h : finite s) (H0 : C ∅ finite_empty) (H1 : ∀ {a s}, a ∉ s → ∀h:finite s, C s h → C (insert a s) (finite_insert a h)) : C s h := have ∀h:finite s, C s h, from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)), this h instance fintype_singleton (a : α) : fintype ({a} : set α) := unique.fintype @[simp] theorem card_singleton (a : α) : fintype.card ({a} : set α) = 1 := fintype.card_of_subsingleton _ @[simp] theorem finite_singleton (a : α) : finite ({a} : set α) := ⟨set.fintype_singleton _⟩ instance fintype_pure : ∀ a : α, fintype (pure a : set α) := set.fintype_singleton theorem finite_pure (a : α) : finite (pure a : set α) := ⟨set.fintype_pure a⟩ instance fintype_univ [fintype α] : fintype (@univ α) := fintype.of_equiv α $ (equiv.set.univ α).symm theorem finite_univ [fintype α] : finite (@univ α) := ⟨set.fintype_univ⟩ theorem infinite_univ_iff : (@univ α).infinite ↔ _root_.infinite α := ⟨λ h₁, ⟨λ h₂, h₁ $ @finite_univ α h₂⟩, λ ⟨h₁⟩ ⟨h₂⟩, h₁ $ @fintype.of_equiv _ _ h₂ $ equiv.set.univ _⟩ theorem infinite_univ [h : _root_.infinite α] : infinite (@univ α) := infinite_univ_iff.2 h instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] : fintype (s ∪ t : set α) := fintype.of_finset (s.to_finset ∪ t.to_finset) $ by simp theorem finite_union {s t : set α} : finite s → finite t → finite (s ∪ t) | ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq α) _ _ hs ht⟩ instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] : fintype ({a ∈ s | p a} : set α) := fintype.of_finset (s.to_finset.filter p) $ by simp instance fintype_inter (s t : set α) [fintype s] [decidable_pred t] : fintype (s ∩ t : set α) := set.fintype_sep s t def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred t] (h : t ⊆ s) : fintype t := by rw ← inter_eq_self_of_subset_right h; apply_instance theorem finite_subset {s : set α} : finite s → ∀ {t : set α}, t ⊆ s → finite t | ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩ instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) := fintype.of_finset (s.to_finset.image f) $ by simp instance fintype_range [decidable_eq β] (f : α → β) [fintype α] : fintype (range f) := fintype.of_finset (finset.univ.image f) $ by simp [range] theorem finite_range (f : α → β) [fintype α] : finite (range f) := by haveI := classical.dec_eq β; exact ⟨by apply_instance⟩ theorem finite_image {s : set α} (f : α → β) : finite s → finite (f '' s) | ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq β) _ _ h⟩ lemma finite_dependent_image {s : set α} (hs : finite s) {F : Π i ∈ s, β} {t : set β} (H : ∀ y ∈ t, ∃ x (hx : x ∈ s), y = F x hx) : set.finite t := begin let G : s → β := λ x, F x.1 x.2, have A : t ⊆ set.range G, { assume y hy, rcases H y hy with ⟨x, hx, xy⟩, refine ⟨⟨x, hx⟩, xy.symm⟩ }, letI : fintype s := finite.fintype hs, exact finite_subset (finite_range G) A end instance fintype_map {α β} [decidable_eq β] : ∀ (s : set α) (f : α → β) [fintype s], fintype (f <$> s) := set.fintype_image theorem finite_map {α β} {s : set α} : ∀ (f : α → β), finite s → finite (f <$> s) := finite_image def fintype_of_fintype_image (s : set α) {f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s := fintype.of_finset ⟨_, @multiset.nodup_filter_map β α g _ (@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ λ a, begin suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s, by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc], rw exists_swap, suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]}, simp [I _, (injective_of_partial_inv I).eq_iff] end theorem finite_of_finite_image_on {s : set α} {f : α → β} (hi : set.inj_on f s) : finite (f '' s) → finite s | ⟨h⟩ := ⟨@fintype.of_injective _ _ h (λa:s, ⟨f a.1, mem_image_of_mem f a.2⟩) $ assume a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext.1 eq⟩ theorem finite_image_iff_on {s : set α} {f : α → β} (hi : inj_on f s) : finite (f '' s) ↔ finite s := ⟨finite_of_finite_image_on hi, finite_image _⟩ theorem finite_of_finite_image {s : set α} {f : α → β} (I : set.inj_on f s) : finite (f '' s) → finite s := finite_of_finite_image_on I theorem finite_preimage {s : set β} {f : α → β} (I : set.inj_on f (f⁻¹' s)) (h : finite s) : finite (f ⁻¹' s) := finite_of_finite_image I (finite_subset h (image_preimage_subset f s)) instance fintype_Union [decidable_eq α] {ι : Type*} [fintype ι] (f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) := fintype.of_finset (finset.univ.bind (λ i, (f i).to_finset)) $ by simp theorem finite_Union {ι : Type*} [fintype ι] {f : ι → set α} (H : ∀i, finite (f i)) : finite (⋃ i, f i) := ⟨@set.fintype_Union _ (classical.dec_eq α) _ _ _ (λ i, finite.fintype (H i))⟩ def fintype_bUnion [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) (H : ∀ i ∈ s, fintype (f i)) : fintype (⋃ i ∈ s, f i) := by rw bUnion_eq_Union; exact @set.fintype_Union _ _ _ _ _ (by rintro ⟨i, hi⟩; exact H i hi) instance fintype_bUnion' [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) [H : ∀ i, fintype (f i)] : fintype (⋃ i ∈ s, f i) := fintype_bUnion _ (λ i _, H i) theorem finite_sUnion {s : set (set α)} (h : finite s) (H : ∀t∈s, finite t) : finite (⋃₀ s) := by rw sUnion_eq_Union; haveI := finite.fintype h; apply finite_Union; simpa using H theorem finite_bUnion {α} {ι : Type*} {s : set ι} {f : ι → set α} : finite s → (∀i, finite (f i)) → finite (⋃ i∈s, f i) | ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (λ i, h _) theorem finite_bUnion' {α} {ι : Type*} {s : set ι} (f : ι → set α) : finite s → (∀i ∈ s, finite (f i)) → finite (⋃ i∈s, f i) | ⟨hs⟩ h := by { rw [bUnion_eq_Union], exactI finite_Union (λ i, h i.1 i.2) } instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} := fintype.of_finset (finset.range n) $ by simp instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} := by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1) lemma finite_le_nat (n : ℕ) : finite {i | i ≤ n} := ⟨set.fintype_le_nat _⟩ lemma finite_lt_nat (n : ℕ) : finite {i | i < n} := ⟨set.fintype_lt_nat _⟩ instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] : fintype (set.prod s t) := fintype.of_finset (s.to_finset.product t.to_finset) $ by simp lemma finite_prod {s : set α} {t : set β} : finite s → finite t → finite (set.prod s t) | ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩ def fintype_bind {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) (H : ∀ a ∈ s, fintype (f a)) : fintype (s >>= f) := set.fintype_bUnion _ H instance fintype_bind' {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) [H : ∀ a, fintype (f a)] : fintype (s >>= f) := fintype_bind _ _ (λ i _, H i) theorem finite_bind {α β} {s : set α} {f : α → set β} : finite s → (∀ a ∈ s, finite (f a)) → finite (s >>= f) | ⟨hs⟩ H := ⟨@fintype_bind _ _ (classical.dec_eq β) _ hs _ (λ a ha, (H a ha).fintype)⟩ instance fintype_seq {α β : Type u} [decidable_eq β] (f : set (α → β)) (s : set α) [fintype f] [fintype s] : fintype (f <*> s) := by rw seq_eq_bind_map; apply set.fintype_bind' theorem finite_seq {α β : Type u} {f : set (α → β)} {s : set α} : finite f → finite s → finite (f <*> s) | ⟨hf⟩ ⟨hs⟩ := by { haveI := classical.dec_eq β, exactI ⟨set.fintype_seq _ _⟩ } /-- There are finitely many subsets of a given finite set -/ lemma finite_subsets_of_finite {α : Type u} {a : set α} (h : finite a) : finite {b | b ⊆ a} := begin -- we just need to translate the result, already known for finsets, -- to the language of finite sets let s : set (set α) := coe '' (↑(finset.powerset (finite.to_finset h)) : set (finset α)), have : finite s := finite_image _ (finite_mem_finset _), apply finite_subset this, refine λ b hb, ⟨(finite_subset h hb).to_finset, _, finite.coe_to_finset _⟩, simpa [finset.subset_iff] end lemma exists_min_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f a ≤ f b | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_min_image f ⟨x, finite.mem_to_finset.2 hx⟩ lemma exists_max_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f b ≤ f a | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_max_image f ⟨x, finite.mem_to_finset.2 hx⟩ end set namespace finset variables [decidable_eq β] variables {s : finset α} lemma finite_to_set (s : finset α) : set.finite (↑s : set α) := set.finite_mem_finset s @[simp] lemma coe_bind {f : α → finset β} : ↑(s.bind f) = (⋃x ∈ (↑s : set α), ↑(f x) : set β) := by simp [set.ext_iff] @[simp] lemma finite_to_set_to_finset {α : Type*} (s : finset α) : (finite_to_set s).to_finset = s := by { ext, rw [set.finite.mem_to_finset, mem_coe] } end finset namespace set lemma finite_subset_Union {s : set α} (hs : finite s) {ι} {t : ι → set α} (h : s ⊆ ⋃ i, t i) : ∃ I : set ι, finite I ∧ s ⊆ ⋃ i ∈ I, t i := begin unfreezeI, cases hs, choose f hf using show ∀ x : s, ∃ i, x.1 ∈ t i, {simpa [subset_def] using h}, refine ⟨range f, finite_range f, _⟩, rintro x hx, simp, exact ⟨x, ⟨hx, hf _⟩⟩, end lemma eq_finite_Union_of_finite_subset_Union {ι} {s : ι → set α} {t : set α} (tfin : finite t) (h : t ⊆ ⋃ i, s i) : ∃ I : set ι, (finite I) ∧ ∃ σ : {i | i ∈ I} → set α, (∀ i, finite (σ i)) ∧ (∀ i, σ i ⊆ s i) ∧ t = ⋃ i, σ i := let ⟨I, Ifin, hI⟩ := finite_subset_Union tfin h in ⟨I, Ifin, λ x, s x ∩ t, λ i, finite_subset tfin (inter_subset_right _ _), λ i, inter_subset_left _ _, begin ext x, rw mem_Union, split, { intro x_in, rcases mem_Union.mp (hI x_in) with ⟨i, _, ⟨hi, rfl⟩, H⟩, use [i, hi, H, x_in] }, { rintros ⟨i, hi, H⟩, exact H } end⟩ instance nat.fintype_Iio (n : ℕ) : fintype (Iio n) := fintype.of_finset (finset.range n) $ by simp /-- If `P` is some relation between terms of `γ` and sets in `γ`, such that every finite set `t : set γ` has some `c : γ` related to it, then there is a recursively defined sequence `u` in `γ` so `u n` is related to the image of `{0, 1, ..., n-1}` under `u`. (We use this later to show sequentially compact sets are totally bounded.) -/ lemma seq_of_forall_finite_exists {γ : Type*} {P : γ → set γ → Prop} (h : ∀ t, finite t → ∃ c, P c t) : ∃ u : ℕ → γ, ∀ n, P (u n) (u '' Iio n) := ⟨λ n, @nat.strong_rec_on' (λ _, γ) n $ λ n ih, classical.some $ h (range $ λ m : Iio n, ih m.1 m.2) (finite_range _), λ n, begin classical, refine nat.strong_rec_on' n (λ n ih, _), rw nat.strong_rec_on_beta', convert classical.some_spec (h _ _), ext x, split, { rintros ⟨m, hmn, rfl⟩, exact ⟨⟨m, hmn⟩, rfl⟩ }, { rintros ⟨⟨m, hmn⟩, rfl⟩, exact ⟨m, hmn, rfl⟩ } end⟩ lemma finite_range_ite {p : α → Prop} [decidable_pred p] {f g : α → β} (hf : finite (range f)) (hg : finite (range g)) : finite (range (λ x, if p x then f x else g x)) := finite_subset (finite_union hf hg) range_ite_subset lemma finite_range_const {c : β} : finite (range (λ x : α, c)) := finite_subset (finite_singleton c) range_const_subset lemma range_find_greatest_subset {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ}: range (λ x, nat.find_greatest (P x) b) ⊆ ↑(finset.range (b + 1)) := by { rw range_subset_iff, assume x, simp [nat.lt_succ_iff, nat.find_greatest_le] } lemma finite_range_find_greatest {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ} : finite (range (λ x, nat.find_greatest (P x) b)) := finite_subset (finset.finite_to_set $ finset.range (b + 1)) range_find_greatest_subset lemma card_lt_card {s t : set α} [fintype s] [fintype t] (h : s ⊂ t) : fintype.card s < fintype.card t := begin rw [← s.coe_to_finset, ← t.coe_to_finset, finset.coe_ssubset] at h, rw [fintype.card_of_finset' _ (λ x, mem_to_finset), fintype.card_of_finset' _ (λ x, mem_to_finset)], exact finset.card_lt_card h, end lemma card_le_of_subset {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) : fintype.card s ≤ fintype.card t := calc fintype.card s = s.to_finset.card : fintype.card_of_finset' _ (by simp) ... ≤ t.to_finset.card : finset.card_le_of_subset (λ x hx, by simp [set.subset_def, *] at *) ... = fintype.card t : eq.symm (fintype.card_of_finset' _ (by simp)) lemma eq_of_subset_of_card_le {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) (hcard : fintype.card t ≤ fintype.card s) : s = t := (eq_or_ssubset_of_subset hsub).elim id (λ h, absurd hcard $ not_le_of_lt $ card_lt_card h) lemma card_range_of_injective [fintype α] {f : α → β} (hf : injective f) [fintype (range f)] : fintype.card (range f) = fintype.card α := eq.symm $ fintype.card_congr $ equiv.set.range f hf lemma finite.exists_maximal_wrt [partial_order β] (f : α → β) (s : set α) (h : set.finite s) : s.nonempty → ∃a∈s, ∀a'∈s, f a ≤ f a' → f a = f a' := begin classical, refine h.induction_on _ _, { assume h, exact absurd h empty_not_nonempty }, assume a s his _ ih _, cases s.eq_empty_or_nonempty with h h, { use a, simp [h] }, rcases ih h with ⟨b, hb, ih⟩, by_cases f b ≤ f a, { refine ⟨a, set.mem_insert _ _, assume c hc hac, le_antisymm hac _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { refl }, { rwa [← ih c hcs (le_trans h hac)] } }, { refine ⟨b, set.mem_insert_of_mem _ hb, assume c hc hbc, _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { exact (h hbc).elim }, { exact ih c hcs hbc } } end lemma finite.card_to_finset {s : set α} [fintype s] (h : s.finite) : h.to_finset.card = fintype.card s := by { rw [← finset.card_attach, finset.attach_eq_univ, ← fintype.card], congr' 2, funext, rw set.finite.mem_to_finset } section local attribute [instance, priority 1] classical.prop_decidable lemma to_finset_inter {α : Type*} [fintype α] (s t : set α) : (s ∩ t).to_finset = s.to_finset ∩ t.to_finset := by ext; simp end section variables [semilattice_sup α] [nonempty α] {s : set α} /--A finite set is bounded above.-/ lemma bdd_above_finite (hs : finite s) : bdd_above s := finite.induction_on hs bdd_above_empty $ λ a s _ _ h, h.insert a /--A finite union of sets which are all bounded above is still bounded above.-/ lemma bdd_above_finite_union {I : set β} {S : β → set α} (H : finite I) : (bdd_above (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_above (S i)) := finite.induction_on H (by simp only [bUnion_empty, bdd_above_empty, ball_empty_iff]) (λ a s ha _ hs, by simp only [bUnion_insert, ball_insert_iff, bdd_above_union, hs]) end section variables [semilattice_inf α] [nonempty α] {s : set α} /--A finite set is bounded below.-/ lemma bdd_below_finite (hs : finite s) : bdd_below s := finite.induction_on hs bdd_below_empty $ λ a s _ _ h, h.insert a /--A finite union of sets which are all bounded below is still bounded below.-/ lemma bdd_below_finite_union {I : set β} {S : β → set α} (H : finite I) : (bdd_below (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_below (S i)) := @bdd_above_finite_union (order_dual α) _ _ _ _ _ H end end set namespace finset section preimage /-- Preimage of `s : finset β` under a map `f` injective of `f ⁻¹' s` as a `finset`. -/ noncomputable def preimage {f : α → β} (s : finset β) (hf : set.inj_on f (f ⁻¹' ↑s)) : finset α := (set.finite_preimage hf (set.finite_mem_finset s)).to_finset @[simp] lemma mem_preimage {f : α → β} {s : finset β} {hf : set.inj_on f (f ⁻¹' ↑s)} {x : α} : x ∈ preimage s hf ↔ f x ∈ s := set.finite.mem_to_finset @[simp, norm_cast] lemma coe_preimage {f : α → β} (s : finset β) (hf : set.inj_on f (f ⁻¹' ↑s)) : (↑(preimage s hf) : set α) = f ⁻¹' ↑s := set.finite.coe_to_finset _ lemma image_preimage [decidable_eq β] (f : α → β) (s : finset β) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) : image f (preimage s hf.inj_on) = s := finset.coe_inj.1 $ suffices f '' (f ⁻¹' ↑s) = ↑s, by simpa, (set.subset.antisymm (image_preimage_subset _ _) hf.surj_on) end preimage @[to_additive] lemma prod_preimage [comm_monoid β] (f : α → γ) (s : finset γ) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) (g : γ → β) : ∏ x in preimage s hf.inj_on, g (f x) = ∏ x in s, g x := by classical; calc ∏ x in preimage s hf.inj_on, g (f x) = ∏ x in image f (preimage s hf.inj_on), g x : begin rw prod_image, intros x hx y hy hxy, apply hf.inj_on, repeat { try { rw mem_preimage at hx hy, rw [set.mem_preimage, mem_coe] }, assumption }, end ... = ∏ x in s, g x : by rw [image_preimage] /-- A finset is bounded above. -/ lemma bdd_above [semilattice_sup α] [nonempty α] (s : finset α) : bdd_above (↑s : set α) := set.bdd_above_finite (finset.finite_to_set s) /-- A finset is bounded below. -/ lemma bdd_below [semilattice_inf α] [nonempty α] (s : finset α) : bdd_below (↑s : set α) := set.bdd_below_finite (finset.finite_to_set s) end finset lemma fintype.exists_max [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x ≤ f x₀ := begin rcases set.finite_univ.exists_maximal_wrt f _ univ_nonempty with ⟨x, _, hx⟩, exact ⟨x, λ y, (le_total (f x) (f y)).elim (λ h, ge_of_eq $ hx _ trivial h) id⟩ end
103a304faf6077f36dfdef0d67469bdae106293d
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Server.lean
62f9ba41e6ce621ea9be28dbb70604fff9f8a409
[ "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
10,283
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 Std.Data.RBMap import Lean.Environment import Lean.Server.Snapshots import Lean.Data.Lsp import Lean.Data.Json.FromToJson namespace Lean namespace Server open Lsp /-- A document editable in the sense that we track the environment and parser state after each command so that edits can be applied without recompiling code appearing earlier in the file. -/ structure EditableDocument := (version : Nat) (text : FileMap) /- The first snapshot is that after the header. -/ (header : Snapshots.Snapshot) /- Subsequent snapshots occur after each command. -/ -- TODO(WN): These should probably be asynchronous Tasks (snapshots : List Snapshots.Snapshot) namespace EditableDocument open Elab /-- Compiles the contents of a Lean file. -/ def compileDocument (version : Nat) (text : FileMap) : IO (MessageLog × EditableDocument) := do headerSnap ← Snapshots.compileHeader text.source; (cmdSnaps, msgLog) ← Snapshots.compileCmdsAfter text.source headerSnap; let docOut : EditableDocument := ⟨version, text, headerSnap, cmdSnaps⟩; pure (msgLog, docOut) /-- Given `changePos`, the UTF-8 offset of a change into the pre-change source, and the new document, updates editable doc state. -/ def updateDocument (doc : EditableDocument) (changePos : String.Pos) (newVersion : Nat) (newText : FileMap) : IO (MessageLog × EditableDocument) := do let recompileEverything := (do -- TODO free compacted regions compileDocument newVersion newText); /- If the change occurred before the first command or there are no commands yet, recompile everything. -/ match doc.snapshots.head? with | none => recompileEverything | some firstSnap => if firstSnap.beginPos > changePos then recompileEverything else do -- NOTE(WN): endPos is greedy in that it consumes input until the next token, -- so a change on some whitespace after a command recompiles it. We could -- be more precise. let validSnaps := doc.snapshots.filter (fun snap => snap.endPos < changePos); -- The lowest-in-the-file snapshot which is still valid. let lastSnap := validSnaps.getLastD doc.header; (snaps, msgLog) ← Snapshots.compileCmdsAfter newText.source lastSnap; let newDoc := { version := newVersion, header := doc.header, text := newText, snapshots := validSnaps ++ snaps : EditableDocument }; pure (msgLog, newDoc) end EditableDocument open EditableDocument open IO open Std (RBMap RBMap.empty) open JsonRpc abbrev DocumentMap := RBMap DocumentUri EditableDocument (fun a b => Decidable.decide (a < b)) structure ServerContext := (hIn hOut : FS.Stream) (openDocumentsRef : IO.Ref DocumentMap) -- TODO (requestsInFlight : IO.Ref (Array (Task (Σ α, Response α)))) abbrev ServerM := ReaderT ServerContext IO def findOpenDocument (key : DocumentUri) : ServerM EditableDocument := fun st => do openDocuments ← st.openDocumentsRef.get; match openDocuments.find? key with | some doc => pure doc | none => throw (userError $ "got unknown document URI (" ++ key ++ ")") def updateOpenDocuments (key : DocumentUri) (val : EditableDocument) : ServerM Unit := fun st => st.openDocumentsRef.modify (fun documents => documents.insert key val) def readLspMessage : ServerM JsonRpc.Message := fun st => monadLift $ readLspMessage st.hIn def readLspRequestAs (expectedMethod : String) (α : Type*) [HasFromJson α] : ServerM (Request α) := fun st => monadLift $ readLspRequestAs st.hIn expectedMethod α def readLspNotificationAs (expectedMethod : String) (α : Type*) [HasFromJson α] : ServerM α := fun st => monadLift $ readLspNotificationAs st.hIn expectedMethod α def writeLspNotification {α : Type*} [HasToJson α] (method : String) (params : α) : ServerM Unit := fun st => monadLift $ writeLspNotification st.hOut method params def writeLspResponse {α : Type*} [HasToJson α] (id : RequestID) (params : α) : ServerM Unit := fun st => monadLift $ writeLspResponse st.hOut id params /-- Clears diagnostics for the document version 'version'. -/ -- TODO(WN): how to clear all diagnostics? Sending version 'none' doesn't seem to work def clearDiagnostics (uri : DocumentUri) (version : Nat) : ServerM Unit := writeLspNotification "textDocument/publishDiagnostics" { uri := uri, version? := version, diagnostics := #[] : PublishDiagnosticsParams } def sendDiagnostics (uri : DocumentUri) (doc : EditableDocument) (log : MessageLog) : ServerM Unit := do diagnostics ← log.msgs.mapM fun msg => liftM $ msgToDiagnostic doc.text msg; writeLspNotification "textDocument/publishDiagnostics" { uri := uri, version? := doc.version, diagnostics := diagnostics.toArray : PublishDiagnosticsParams } def handleDidOpen (p : DidOpenTextDocumentParams) : ServerM Unit := do let doc := p.textDocument; -- NOTE(WN): `toFileMap` marks line beginnings as immediately following -- "\n", which should be enough to handle both LF and CRLF correctly. -- This is because LSP always refers to characters by (line, column), -- so if we get the line number correct it shouldn't matter that there -- is a CR there. let text := doc.text.toFileMap; (msgLog, newDoc) ← monadLift $ compileDocument doc.version text; updateOpenDocuments doc.uri newDoc; sendDiagnostics doc.uri newDoc msgLog private def replaceLspRange (text : FileMap) (r : Lsp.Range) (newText : String) : FileMap := let start := text.lspPosToUtf8Pos r.start; let «end» := text.lspPosToUtf8Pos r.«end»; let pre := text.source.extract 0 start; let post := text.source.extract «end» text.source.bsize; (pre ++ newText ++ post).toFileMap def handleDidChange (p : DidChangeTextDocumentParams) : ServerM Unit := do let docId := p.textDocument; let changes := p.contentChanges; oldDoc ← findOpenDocument docId.uri; some newVersion ← pure docId.version? | throw (userError "expected version number"); if newVersion <= oldDoc.version then do throw (userError "got outdated version number") else changes.forM $ fun change => match change with | TextDocumentContentChangeEvent.rangeChange (range : Range) (newText : String) => do let startOff := oldDoc.text.lspPosToUtf8Pos range.start; let newDocText := replaceLspRange oldDoc.text range newText; (msgLog, newDoc) ← monadLift $ updateDocument oldDoc startOff newVersion newDocText; updateOpenDocuments docId.uri newDoc; -- Clients don't have to clear diagnostics, so we clear them -- for the *previous* version here. clearDiagnostics docId.uri oldDoc.version; sendDiagnostics docId.uri newDoc msgLog | TextDocumentContentChangeEvent.fullChange (text : String) => throw (userError "TODO impl computing the diff of two sources.") def handleDidClose (p : DidCloseTextDocumentParams) : ServerM Unit := do -- TODO free compacted regions fun st => st.openDocumentsRef.modify (fun openDocuments => openDocuments.erase p.textDocument.uri) def handleHover (p : HoverParams) : ServerM Json := pure Json.null def parseParams (paramType : Type*) [HasFromJson paramType] (params : Json) : ServerM paramType := match fromJson? params with | some parsed => pure parsed | none => throw (userError "got param with wrong structure") def handleNotification (method : String) (params : Json) : ServerM Unit := do let h := (fun paramType [HasFromJson paramType] (handler : paramType → ServerM Unit) => parseParams paramType params >>= handler); match method with | "textDocument/didOpen" => h DidOpenTextDocumentParams handleDidOpen | "textDocument/didChange" => h DidChangeTextDocumentParams handleDidChange | "textDocument/didClose" => h DidCloseTextDocumentParams handleDidClose | "$/cancelRequest" => pure () -- TODO when we're async | _ => throw (userError "got unsupported notification method") def handleRequest (id : RequestID) (method : String) (params : Json) : ServerM Unit := do let h := (fun paramType responseType [HasFromJson paramType] [HasToJson responseType] (handler : paramType → ServerM responseType) => parseParams paramType params >>= handler >>= writeLspResponse id); match method with | "textDocument/hover" => h HoverParams Json handleHover | _ => throw (userError $ "got unsupported request: " ++ method ++ "; params: " ++ toString params) partial def mainLoop : Unit → ServerM Unit | () => do msg ← readLspMessage; match msg with | Message.request id "shutdown" _ => writeLspResponse id (Json.null) | Message.request id method (some params) => do handleRequest id method (toJson params); mainLoop () | Message.notification method (some params) => do handleNotification method (toJson params); mainLoop () | _ => throw (userError "got invalid JSON-RPC message") def mkLeanServerCapabilities : ServerCapabilities := { textDocumentSync? := some { openClose := true, change := TextDocumentSyncKind.incremental, willSave := false, willSaveWaitUntil := false, save? := none, }, hoverProvider := true, } def initAndRunServer (i o : FS.Stream) : IO Unit := do openDocumentsRef ← IO.mkRef (RBMap.empty : DocumentMap); runReader (do -- ignore InitializeParams for MWE r ← readLspRequestAs "initialize" InitializeParams; writeLspResponse r.id { capabilities := mkLeanServerCapabilities, serverInfo? := some { name := "Lean 4 server", version? := "0.0.1" } : InitializeResult }; _ ← readLspNotificationAs "initialized" InitializedParams; mainLoop (); Message.notification "exit" none ← readLspMessage | throw (userError "Expected an Exit Notification."); pure ()) (⟨i, o, openDocumentsRef⟩ : ServerContext) namespace Test def runWithInputFile (fn : String) (searchPath : Option String) : IO Unit := do o ← IO.getStdout; e ← IO.getStderr; FS.withFile fn FS.Mode.read (fun hFile => do Lean.initSearchPath searchPath; catch (Lean.Server.initAndRunServer (FS.Stream.ofHandle hFile) o) (fun err => e.putStrLn (toString err))) end Test end Server end Lean
3ffcf2019063a7e18c7413059fcc5c173b226591
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/prod.lean
b3deca10fe95475a6a00648279d57e3d0e50d02e
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
850
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 -/ prelude import init.logic universe variables u v instance {α : Type u} {β : Type v} [inhabited α] [inhabited β] : inhabited (prod α β) := ⟨(default α, default β)⟩ instance {α : Type u} {β : Type v} [h₁ : decidable_eq α] [h₂ : decidable_eq β] : decidable_eq (α × β) | (a, b) (a', b') := match (h₁ a a') with | (is_true e₁) := match (h₂ b b') with | (is_true e₂) := is_true (eq.rec_on e₁ (eq.rec_on e₂ rfl)) | (is_false n₂) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₂' n₂)) end | (is_false n₁) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₁' n₁)) end
2cefbad90aa8330393ff8c681ebb928b0a30ec15
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/big_operators/nat_antidiagonal.lean
af55a195409c3763ca5f6568236b7744fb235a8f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,453
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.finset.nat_antidiagonal import Mathlib.algebra.big_operators.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Big operators for `nat_antidiagonal` This file contains theorems relevant to big operators over `finset.nat.antidiagonal`. -/ namespace finset namespace nat theorem prod_antidiagonal_succ {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.prod (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (0, n + 1) * finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p + 1, prod.snd p) := sorry theorem sum_antidiagonal_succ {N : Type u_2} [add_comm_monoid N] {n : ℕ} {f : ℕ × ℕ → N} : (finset.sum (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (0, n + 1) + finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p + 1, prod.snd p) := prod_antidiagonal_succ theorem sum_antidiagonal_swap {M : Type u_1} [add_comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.swap p)) = finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f p := sorry theorem prod_antidiagonal_succ' {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.prod (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (n + 1, 0) * finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p, prod.snd p + 1) := sorry theorem sum_antidiagonal_succ' {N : Type u_2} [add_comm_monoid N] {n : ℕ} {f : ℕ × ℕ → N} : (finset.sum (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (n + 1, 0) + finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p, prod.snd p + 1) := prod_antidiagonal_succ' theorem prod_antidiagonal_subst {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → ℕ → M} : (finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f p n) = finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f p (prod.fst p + prod.snd p) := sorry theorem prod_antidiagonal_eq_prod_range_succ {M : Type u_1} [comm_monoid M] (f : ℕ → ℕ → M) (n : ℕ) : (finset.prod (antidiagonal n) fun (ij : ℕ × ℕ) => f (prod.fst ij) (prod.snd ij)) = finset.prod (range (Nat.succ n)) fun (k : ℕ) => f k (n - k) := sorry
920bf1d59183f6d57f09a9129db1dc8dd33f24f4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/star/free.lean
fca485ba142037f0844f4b04f1ed8f81f673a012
[ "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
2,365
lean
/- Copyright (c) 2020 Eric Weiser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Weiser -/ import algebra.star.basic import algebra.free_algebra /-! # A *-algebra structure on the free algebra. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Reversing words gives a *-structure on the free monoid or on the free algebra on a type. ## Implementation note We have this in a separate file, rather than in `algebra.free_monoid` and `algebra.free_algebra`, to avoid importing `algebra.star.basic` into the entire hierarchy. -/ namespace free_monoid variables {α : Type*} instance : star_semigroup (free_monoid α) := { star := list.reverse, star_involutive := list.reverse_reverse, star_mul := list.reverse_append, } @[simp] lemma star_of (x : α) : star (of x) = of x := rfl /-- Note that `star_one` is already a global simp lemma, but this one works with dsimp too -/ @[simp] lemma star_one : star (1 : free_monoid α) = 1 := rfl end free_monoid namespace free_algebra variables {R : Type*} [comm_semiring R] {X : Type*} /-- The star ring formed by reversing the elements of products -/ instance : star_ring (free_algebra R X) := { star := mul_opposite.unop ∘ lift R (mul_opposite.op ∘ ι R), star_involutive := λ x, by { unfold has_star.star, simp only [function.comp_apply], refine free_algebra.induction R X _ _ _ _ x, { intros, simp only [alg_hom.commutes, mul_opposite.algebra_map_apply, mul_opposite.unop_op] }, { intros, simp only [lift_ι_apply, mul_opposite.unop_op] }, { intros, simp only [*, map_mul, mul_opposite.unop_mul] }, { intros, simp only [*, map_add, mul_opposite.unop_add] } }, star_mul := λ a b, by simp only [function.comp_app, map_mul, mul_opposite.unop_mul], star_add := λ a b, by simp only [function.comp_app, map_add, mul_opposite.unop_add]} @[simp] lemma star_ι (x : X) : star (ι R x) = (ι R x) := by simp [star, has_star.star] @[simp] lemma star_algebra_map (r : R) : star (algebra_map R (free_algebra R X) r) = (algebra_map R _ r) := by simp [star, has_star.star] /-- `star` as an `alg_equiv` -/ def star_hom : free_algebra R X ≃ₐ[R] (free_algebra R X)ᵐᵒᵖ := { commutes' := λ r, by simp [star_algebra_map], ..star_ring_equiv } end free_algebra
700c6876428ee58281a95eeae7b4a55d8edb3cf7
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/tactic/interactive.lean
8147ac85a13013c911fa6992f4791696cc829f3c
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
28,446
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Simon Hudon, Sebastien Gouezel, Scott Morrison -/ import data.dlist data.dlist.basic data.prod category.basic tactic.basic tactic.rcases tactic.generalize_proofs tactic.split_ifs logic.basic tactic.ext tactic.tauto tactic.replacer tactic.simpa tactic.squeeze tactic.library_search open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic namespace interactive open interactive interactive.types expr /-- The `rcases` tactic is the same as `cases`, but with more flexibility in the `with` pattern syntax to allow for recursive case splitting. The pattern syntax uses the following recursive grammar: ``` patt ::= (patt_list "|")* patt_list patt_list ::= id | "_" | "⟨" (patt ",")* patt "⟩" ``` A pattern like `⟨a, b, c⟩ | ⟨d, e⟩` will do a split over the inductive datatype, naming the first three parameters of the first constructor as `a,b,c` and the first two of the second constructor `d,e`. If the list is not as long as the number of arguments to the constructor or the number of constructors, the remaining variables will be automatically named. If there are nested brackets such as `⟨⟨a⟩, b | c⟩ | d` then these will cause more case splits as necessary. If there are too many arguments, such as `⟨a, b, c⟩` for splitting on `∃ x, ∃ y, p x`, then it will be treated as `⟨a, ⟨b, c⟩⟩`, splitting the last parameter as necessary. `rcases` also has special support for quotient types: quotient induction into Prop works like matching on the constructor `quot.mk`. `rcases? e` will perform case splits on `e` in the same way as `rcases e`, but rather than accepting a pattern, it does a maximal cases and prints the pattern that would produce this case splitting. The default maximum depth is 5, but this can be modified with `rcases? e : n`. -/ meta def rcases : parse rcases_parse → tactic unit | (p, sum.inl ids) := tactic.rcases p ids | (p, sum.inr depth) := do patt ← tactic.rcases_hint p depth, pe ← pp p, trace $ ↑"snippet: rcases " ++ pe ++ " with " ++ to_fmt patt /-- The `rintro` tactic is a combination of the `intros` tactic with `rcases` to allow for destructuring patterns while introducing variables. See `rcases` for a description of supported patterns. For example, `rintros (a | ⟨b, c⟩) ⟨d, e⟩` will introduce two variables, and then do case splits on both of them producing two subgoals, one with variables `a d e` and the other with `b c d e`. `rintro?` will introduce and case split on variables in the same way as `rintro`, but will also print the `rintro` invocation that would have the same result. Like `rcases?`, `rintro? : n` allows for modifying the depth of splitting; the default is 5. -/ meta def rintro : parse rintro_parse → tactic unit | (sum.inl []) := intros [] | (sum.inl l) := tactic.rintro l | (sum.inr depth) := do ps ← tactic.rintro_hint depth, trace $ ↑"snippet: rintro" ++ format.join (ps.map $ λ p, format.space ++ format.group (p.format tt)) /-- Alias for `rintro`. -/ meta def rintros := rintro /-- `try_for n { tac }` executes `tac` for `n` ticks, otherwise uses `sorry` to close the goal. Never fails. Useful for debugging. -/ meta def try_for (max : parse parser.pexpr) (tac : itactic) : tactic unit := do max ← i_to_expr_strict max >>= tactic.eval_expr nat, λ s, match _root_.try_for max (tac s) with | some r := r | none := (tactic.trace "try_for timeout, using sorry" >> admit) s end /-- Multiple subst. `substs x y z` is the same as `subst x, subst y, subst z`. -/ meta def substs (l : parse ident*) : tactic unit := l.mmap' (λ h, get_local h >>= tactic.subst) >> try (tactic.reflexivity reducible) /-- Unfold coercion-related definitions -/ meta def unfold_coes (loc : parse location) : tactic unit := unfold [ ``coe, ``coe_t, ``has_coe_t.coe, ``coe_b,``has_coe.coe, ``lift, ``has_lift.lift, ``lift_t, ``has_lift_t.lift, ``coe_fn, ``has_coe_to_fun.coe, ``coe_sort, ``has_coe_to_sort.coe] loc /-- Unfold auxiliary definitions associated with the current declaration. -/ meta def unfold_aux : tactic unit := do tgt ← target, name ← decl_name, let to_unfold := (tgt.list_names_with_prefix name), guard (¬ to_unfold.empty), -- should we be using simp_lemmas.mk_default? simp_lemmas.mk.dsimplify to_unfold.to_list tgt >>= tactic.change /-- For debugging only. This tactic checks the current state for any missing dropped goals and restores them. Useful when there are no goals to solve but "result contains meta-variables". -/ meta def recover : tactic unit := metavariables >>= tactic.set_goals /-- Like `try { tac }`, but in the case of failure it continues from the failure state instead of reverting to the original state. -/ meta def continue (tac : itactic) : tactic unit := λ s, result.cases_on (tac s) (λ a, result.success ()) (λ e ref, result.success ()) /-- Move goal `n` to the front. -/ meta def swap (n := 2) : tactic unit := do gs ← get_goals, match gs.nth (n-1) with | (some g) := set_goals (g :: gs.remove_nth (n-1)) | _ := skip end /-- Generalize proofs in the goal, naming them with the provided list. -/ meta def generalize_proofs : parse ident_* → tactic unit := tactic.generalize_proofs /-- Clear all hypotheses starting with `_`, like `_match` and `_let_match`. -/ meta def clear_ : tactic unit := tactic.repeat $ do l ← local_context, l.reverse.mfirst $ λ h, do name.mk_string s p ← return $ local_pp_name h, guard (s.front = '_'), cl ← infer_type h >>= is_class, guard (¬ cl), tactic.clear h meta def apply_iff_congr_core : tactic unit := applyc ``iff_of_eq meta def congr_core' : tactic unit := do tgt ← target, apply_eq_congr_core tgt <|> apply_heq_congr_core <|> apply_iff_congr_core <|> fail "congr tactic failed" /-- Same as the `congr` tactic, but takes an optional argument which gives the depth of recursive applications. This is useful when `congr` is too aggressive in breaking down the goal. For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y` and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. -/ meta def congr' : parse (with_desc "n" small_nat)? → tactic unit | (some 0) := failed | o := focus1 (assumption <|> (congr_core' >> all_goals (reflexivity <|> `[apply proof_irrel_heq] <|> `[apply proof_irrel] <|> try (congr' (nat.pred <$> o))))) /-- Acts like `have`, but removes a hypothesis with the same name as this one. For example if the state is `h : p ⊢ goal` and `f : p → q`, then after `replace h := f h` the goal will be `h : q ⊢ goal`, where `have h := f h` would result in the state `h : p, h : q ⊢ goal`. This can be used to simulate the `specialize` and `apply at` tactics of Coq. -/ meta def replace (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do let h := h.get_or_else `this, old ← try_core (get_local h), «have» h q₁ q₂, match old, q₂ with | none, _ := skip | some o, some _ := tactic.clear o | some o, none := swap >> tactic.clear o >> swap end /-- `apply_assumption` looks for an assumption of the form `... → ∀ _, ... → head` where `head` matches the current goal. alternatively, when encountering an assumption of the form `sg₀ → ¬ sg₁`, after the main approach failed, the goal is dismissed and `sg₀` and `sg₁` are made into the new goal. optional arguments: - asms: list of rules to consider instead of the local constants - tac: a tactic to run on each subgoals after applying an assumption; if this tactic fails, the corresponding assumption will be rejected and the next one will be attempted. -/ meta def apply_assumption (asms : tactic (list expr) := local_context) (tac : tactic unit := return ()) : tactic unit := tactic.apply_assumption asms tac open nat meta def mk_assumption_set (no_dflt : bool) (hs : list simp_arg_type) (attr : list name): tactic (list expr) := do (hs, gex, hex, all_hyps) ← decode_simp_arg_list hs, hs ← hs.mmap i_to_expr_for_apply, l ← attr.mmap $ λ a, attribute.get_instances a, let l := l.join, m ← list.mmap mk_const l, let hs := (hs ++ m).filter $ λ h, expr.const_name h ∉ gex, hs ← if no_dflt then return hs else do { congr_fun ← mk_const `congr_fun, congr_arg ← mk_const `congr_arg, return (congr_fun :: congr_arg :: hs) }, if ¬ no_dflt ∨ all_hyps then do ctx ← local_context, return $ hs.append (ctx.filter (λ h, h.local_uniq_name ∉ hex)) -- remove local exceptions else return hs /-- `solve_by_elim` calls `apply_assumption` on the main goal to find an assumption whose head matches and then repeatedly calls `apply_assumption` on the generated subgoals until no subgoals remain, performing at most `max_rep` recursive steps. `solve_by_elim` discharges the current goal or fails `solve_by_elim` performs back-tracking if `apply_assumption` chooses an unproductive assumption By default, the assumptions passed to apply_assumption are the local context, `congr_fun` and `congr_arg`. `solve_by_elim [h₁, h₂, ..., hᵣ]` also applies the named lemmas. `solve_by_elim with attr₁ ... attrᵣ also applied all lemmas tagged with the specified attributes. `solve_by_elim only [h₁, h₂, ..., hᵣ]` does not include the local context, `congr_fun`, or `congr_arg` unless they are explicitly included. `solve_by_elim [-id]` removes a specified assumption. `solve_by_elim*` tries to solve all goals together, using backtracking if a solution for one goal makes other goals impossible. optional arguments: - discharger: a subsidiary tactic to try at each step (e.g. `cc` may be helpful) - max_rep: number of attempts at discharging generated sub-goals -/ meta def solve_by_elim (all_goals : parse $ (tk "*")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (opt : by_elim_opt := { }) : tactic unit := do asms ← mk_assumption_set no_dflt hs attr_names, tactic.solve_by_elim { all_goals := all_goals.is_some, assumptions := return asms, ..opt } /-- `tautology` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim` -/ meta def tautology (c : parse $ (tk "!")?) := tactic.tautology c.is_some /-- Shorter name for the tactic `tautology`. -/ meta def tauto (c : parse $ (tk "!")?) := tautology c /-- Make every propositions in the context decidable -/ meta def classical := tactic.classical private meta def generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def generalize_arg_p : parser (pexpr × name) := with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux lemma {u} generalize_a_aux {α : Sort u} (h : ∀ x : Sort u, (α → x) → x) : α := h α id /-- Like `generalize` but also considers assumptions specified by the user. The user can also specify to omit the goal. -/ meta def generalize_hyp (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) (l : parse location) : tactic unit := do h' ← get_unused_name `h, x' ← get_unused_name `x, g ← if ¬ l.include_goal then do refine ``(generalize_a_aux _), some <$> (prod.mk <$> tactic.intro x' <*> tactic.intro h') else pure none, n ← l.get_locals >>= tactic.revert_lst, generalize h () p, intron n, match g with | some (x',h') := do tactic.apply h', tactic.clear h', tactic.clear x' | none := return () end /-- Similar to `refine` but generates equality proof obligations for every discrepancy between the goal and the type of the rule. -/ meta def convert (sym : parse (with_desc "←" (tk "<-")?)) (r : parse texpr) (n : parse (tk "using" *> small_nat)?) : tactic unit := do v ← mk_mvar, if sym.is_some then refine ``(eq.mp %%v %%r) else refine ``(eq.mpr %%v %%r), gs ← get_goals, set_goals [v], congr' n, gs' ← get_goals, set_goals $ gs' ++ gs meta def clean_ids : list name := [``id, ``id_rhs, ``id_delta, ``hidden] /-- Remove identity functions from a term. These are normally automatically generated with terms like `show t, from p` or `(p : t)` which translate to some variant on `@id t p` in order to retain the type. -/ meta def clean (q : parse texpr) : tactic unit := do tgt : expr ← target, e ← i_to_expr_strict ``(%%q : %%tgt), tactic.exact $ e.replace (λ e n, match e with | (app (app (const n _) _) e') := if n ∈ clean_ids then some e' else none | (app (lam _ _ _ (var 0)) e') := some e' | _ := none end) meta def source_fields (missing : list name) (e : pexpr) : tactic (list (name × pexpr)) := do e ← to_expr e, t ← infer_type e, let struct_n : name := t.get_app_fn.const_name, fields ← expanded_field_list struct_n, let exp_fields := fields.filter (λ x, x.2 ∈ missing), exp_fields.mmap $ λ ⟨p,n⟩, (prod.mk n ∘ to_pexpr) <$> mk_mapp (n.update_prefix p) [none,some e] meta def collect_struct' : pexpr → state_t (list $ expr×structure_instance_info) tactic pexpr | e := do some str ← pure (e.get_structure_instance_info) | e.traverse collect_struct', v ← monad_lift mk_mvar, modify (list.cons (v,str)), pure $ to_pexpr v meta def collect_struct (e : pexpr) : tactic $ pexpr × list (expr×structure_instance_info) := prod.map id list.reverse <$> (collect_struct' e).run [] meta def refine_one (str : structure_instance_info) : tactic $ list (expr×structure_instance_info) := do tgt ← target, let struct_n : name := tgt.get_app_fn.const_name, exp_fields ← expanded_field_list struct_n, let missing_f := exp_fields.filter (λ f, (f.2 : name) ∉ str.field_names), (src_field_names,src_field_vals) ← (@list.unzip name _ ∘ list.join) <$> str.sources.mmap (source_fields $ missing_f.map prod.snd), let provided := exp_fields.filter (λ f, (f.2 : name) ∈ str.field_names), let missing_f' := missing_f.filter (λ x, x.2 ∉ src_field_names), vs ← mk_mvar_list missing_f'.length, (field_values,new_goals) ← list.unzip <$> (str.field_values.mmap collect_struct : tactic _), e' ← to_expr $ pexpr.mk_structure_instance { struct := some struct_n , field_names := str.field_names ++ missing_f'.map prod.snd ++ src_field_names , field_values := field_values ++ vs.map to_pexpr ++ src_field_vals }, tactic.exact e', gs ← with_enable_tags ( mzip_with (λ (n : name × name) v, do set_goals [v], try (interactive.unfold (provided.map $ λ ⟨s,f⟩, f.update_prefix s) (loc.ns [none])), apply_auto_param <|> apply_opt_param <|> (set_main_tag [`_field,n.2,n.1]), get_goals) missing_f' vs), set_goals gs.join, return new_goals.join meta def refine_recursively : expr × structure_instance_info → tactic (list expr) | (e,str) := do set_goals [e], rs ← refine_one str, gs ← get_goals, gs' ← rs.mmap refine_recursively, return $ gs'.join ++ gs /-- `refine_struct { .. }` acts like `refine` but works only with structure instance literals. It creates a goal for each missing field and tags it with the name of the field so that `have_field` can be used to generically refer to the field currently being refined. As an example, we can use `refine_struct` to automate the construction semigroup instances: ``` refine_struct ( { .. } : semigroup α ), -- case semigroup, mul -- α : Type u, -- ⊢ α → α → α -- case semigroup, mul_assoc -- α : Type u, -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) ``` -/ meta def refine_struct : parse texpr → tactic unit | e := do (x,xs) ← collect_struct e, refine x, gs ← get_goals, xs' ← xs.mmap refine_recursively, set_goals (xs'.join ++ gs) /-- `guard_hyp h := t` fails if the hypothesis `h` does not have type `t`. We use this tactic for writing tests. Fixes `guard_hyp` by instantiating meta variables -/ meta def guard_hyp' (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_eq h p /-- `guard_expr_strict t := e` fails if the expr `t` is not equal to `e`. By contrast to `guard_expr`, this tests strict (syntactic) equality. We use this tactic for writing tests. -/ meta def guard_expr_strict (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (t = e) /-- `guard_target_strict t` fails if the target of the main goal is not syntactically `t`. We use this tactic for writing tests. -/ meta def guard_target_strict (p : parse texpr) : tactic unit := do t ← target, guard_expr_strict t p /-- `guard_hyp_strict h := t` fails if the hypothesis `h` does not have type syntactically equal to `t`. We use this tactic for writing tests. -/ meta def guard_hyp_strict (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_strict h p meta def guard_hyp_nums (n : ℕ) : tactic unit := do k ← local_context, guard (n = k.length) <|> fail format!"{k.length} hypotheses found" meta def guard_tags (tags : parse ident*) : tactic unit := do (t : list name) ← get_main_tag, guard (t = tags) meta def get_current_field : tactic name := do [_,field,str] ← get_main_tag, expr.const_name <$> resolve_name (field.update_prefix str) meta def field (n : parse ident) (tac : itactic) : tactic unit := do gs ← get_goals, ts ← gs.mmap get_tag, ([g],gs') ← pure $ (list.zip gs ts).partition (λ x, x.snd.nth 1 = some n), set_goals [g.1], tac, done, set_goals $ gs'.map prod.fst /-- `have_field`, used after `refine_struct _` poses `field` as a local constant with the type of the field of the current goal: ``` refine_struct ({ .. } : semigroup α), { have_field, ... }, { have_field, ... }, ``` behaves like ``` refine_struct ({ .. } : semigroup α), { have field := @semigroup.mul, ... }, { have field := @semigroup.mul_assoc, ... }, ``` -/ meta def have_field : tactic unit := propagate_tags $ get_current_field >>= mk_const >>= note `field none >> return () /-- `apply_field` functions as `have_field, apply field, clear field` -/ meta def apply_field : tactic unit := propagate_tags $ get_current_field >>= applyc /--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the first goal and the resulting subgoals, iteratively, at most `n` times. `n` is 50 by default. `hs` can contain user attributes: in this case all theorems with this attribute are added to the list of rules. example, with or without user attribute: ``` @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right lemma my_test {a b c d e : real} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules mono_rules -- any of the following lines would also work: -- add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 -- by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] -- by apply_rules [mono_rules] ``` -/ meta def apply_rules (hs : parse pexpr_list_or_texpr) (n : nat := 50) : tactic unit := tactic.apply_rules hs n meta def return_cast (f : option expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) (e x x' eq_h : expr) : tactic (option (expr × expr) × list (expr × expr × expr)) := (do guard (¬ e.has_var), unify x x', u ← mk_meta_univ, f ← f <|> mk_mapp ``_root_.id [(expr.sort u : expr)], t' ← infer_type e, some (f',t) ← pure t | return (some (f,t'), (e,x',eq_h) :: es), infer_type e >>= is_def_eq t, unify f f', return (some (f,t), (e,x',eq_h) :: es)) <|> return (t, es) meta def list_cast_of_aux (x : expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) : expr → tactic (option (expr × expr) × list (expr × expr × expr)) | e@`(cast %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mp %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mpr %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast none t es e x x' | e@`(@eq.subst %%α %%p %%a %%b %%eq_h %%x') := return_cast p t es e x x' eq_h | e@`(@eq.substr %%α %%p %%a %%b %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast p t es e x x' | e@`(@eq.rec %%α %%a %%f %%x' _ %%eq_h) := return_cast f t es e x x' eq_h | e@`(@eq.rec_on %%α %%a %%f %%b %%eq_h %%x') := return_cast f t es e x x' eq_h | e := return (t,es) meta def list_cast_of (x tgt : expr) : tactic (list (expr × expr × expr)) := (list.reverse ∘ prod.snd) <$> tgt.mfold (none, []) (λ e i es, list_cast_of_aux x es.1 es.2 e) private meta def h_generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `heq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def h_generalize_arg_p : parser (pexpr × name) := with_desc "expr == id" $ parser.pexpr 0 >>= h_generalize_arg_p_aux /-- `h_generalize Hx : e == x` matches on `cast _ e` in the goal and replaces it with `x`. It also adds `Hx : e == x` as an assumption. If `cast _ e` appears multiple times (not necessarily with the same proof), they are all replaced by `x`. `cast` `eq.mp`, `eq.mpr`, `eq.subst`, `eq.substr`, `eq.rec` and `eq.rec_on` are all treated as casts. `h_generalize Hx : e == x with h` adds hypothesis `α = β` with `e : α, x : β`. `h_generalize Hx : e == x with _` chooses automatically chooses the name of assumption `α = β`. `h_generalize! Hx : e == x` reverts `Hx`. when `Hx` is omitted, assumption `Hx : e == x` is not added. -/ meta def h_generalize (rev : parse (tk "!")?) (h : parse ident_?) (_ : parse (tk ":")) (arg : parse h_generalize_arg_p) (eqs_h : parse ( (tk "with" >> pure <$> ident_) <|> pure [])) : tactic unit := do let (e,n) := arg, let h' := if h = `_ then none else h, h' ← (h' : tactic name) <|> get_unused_name ("h" ++ n.to_string : string), e ← to_expr e, tgt ← target, ((e,x,eq_h)::es) ← list_cast_of e tgt | fail "no cast found", interactive.generalize h' () (to_pexpr e, n), asm ← get_local h', v ← get_local n, hs ← es.mmap (λ ⟨e,_⟩, mk_app `eq [e,v]), (eqs_h.zip [e]).mmap' (λ ⟨h,e⟩, do h ← if h ≠ `_ then pure h else get_unused_name `h, () <$ note h none eq_h ), hs.mmap' (λ h, do h' ← assert `h h, tactic.exact asm, try (rewrite_target h'), tactic.clear h' ), when h.is_some (do (to_expr ``(heq_of_eq_rec_left %%eq_h %%asm) <|> to_expr ``(heq_of_eq_mp %%eq_h %%asm)) >>= note h' none >> pure ()), tactic.clear asm, when rev.is_some (interactive.revert [n]) /-- `choose a b h using hyp` takes an hypothesis `hyp` of the form `∀ (x : X) (y : Y), ∃ (a : A) (b : B), P x y a b` for some `P : X → Y → A → B → Prop` and outputs into context a function `a : X → Y → A`, `b : X → Y → B` and a proposition `h` stating `∀ (x : X) (y : Y), P x y (a x y) (b x y)`. It presumably also works with dependent versions. Example: ```lean example (h : ∀n m : ℕ, ∃i j, m = n + i ∨ m + j = n) : true := begin choose i j h using h, guard_hyp i := ℕ → ℕ → ℕ, guard_hyp j := ℕ → ℕ → ℕ, guard_hyp h := ∀ (n m : ℕ), m = n + i n m ∨ m + j n m = n, trivial end ``` -/ meta def choose (first : parse ident) (names : parse ident*) (tgt : parse (tk "using" *> texpr)?) : tactic unit := do tgt ← match tgt with | none := get_local `this | some e := tactic.i_to_expr_strict e end, tactic.choose tgt (first :: names), try (tactic.clear tgt) meta def guard_expr_eq' (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, is_def_eq t e /-- `guard_target t` fails if the target of the main goal is not `t`. We use this tactic for writing tests. -/ meta def guard_target' (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq' t p /-- a weaker version of `trivial` that tries to solve the goal by reflexivity or by reducing it to true, unfolding only `reducible` constants. -/ meta def triv : tactic unit := tactic.triv' <|> tactic.reflexivity reducible <|> tactic.contradiction <|> fail "triv tactic failed" /-- Similar to `existsi`. `use x` will instantiate the first term of an `∃` or `Σ` goal with `x`. Unlike `existsi`, `x` is elaborated with respect to the expected type. `use` will alternatively take a list of terms `[x0, ..., xn]`. `use` will work with constructors of arbitrary inductive types. Examples: example (α : Type) : ∃ S : set α, S = S := by use ∅ example : ∃ x : ℤ, x = x := by use 42 example : ∃ a b c : ℤ, a + b + c = 6 := by use [1, 2, 3] example : ∃ p : ℤ × ℤ, p.1 = 1 := by use ⟨1, 42⟩ example : Σ x y : ℤ, (ℤ × ℤ) × ℤ := by use [1, 2, 3, 4, 5] inductive foo | mk : ℕ → bool × ℕ → ℕ → foo example : foo := by use [100, tt, 4, 3] -/ meta def use (l : parse pexpr_list_or_texpr) : tactic unit := tactic.use l >> try triv /-- `clear_aux_decl` clears every `aux_decl` in the local context for the current goal. This includes the induction hypothesis when using the equation compiler and `_let_match` and `_fun_match`. It is useful when using a tactic such as `finish`, `simp *` or `subst` that may use these auxiliary declarations, and produce an error saying the recursion is not well founded. -/ meta def clear_aux_decl : tactic unit := tactic.clear_aux_decl meta def loc.get_local_pp_names : loc → tactic (list name) | loc.wildcard := list.map expr.local_pp_name <$> local_context | (loc.ns l) := return l.reduce_option meta def loc.get_local_uniq_names (l : loc) : tactic (list name) := list.map expr.local_uniq_name <$> l.get_locals /-- The logic of `change x with y at l` fails when there are dependencies. `change'` mimics the behavior of `change`, except in the case of `change x with y at l`. In this case, it will correctly replace occurences of `x` with `y` at all possible hypotheses in `l`. As long as `x` and `y` are defeq, it should never fail. -/ meta def change' (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do l' ← loc.get_local_pp_names l, l'.mmap' (λ e, try (change_with_at q w e)), when l.include_goal $ change q w (loc.ns [none]) private meta def opt_dir_with : parser (option (bool × name)) := (do tk "with", arrow ← (tk "<-")?, h ← ident, return (arrow.is_some, h)) <|> return none /-- `set a := t with h` is a variant of `let a := t`. It adds the hypothesis `h : a = t` to the local context and replaces `t` with `a` everywhere it can. `set a := t with ←h` will add `h : t = a` instead. `set! a := t with h` does not do any replacing. -/ meta def set (h_simp : parse (tk "!")?) (a : parse ident) (tp : parse ((tk ":") >> texpr)?) (_ : parse (tk ":=")) (pv : parse texpr) (rev_name : parse opt_dir_with) := do let vt := match tp with | some t := t | none := pexpr.mk_placeholder end, let pv := ``(%%pv : %%vt), v ← to_expr pv, tp ← infer_type v, definev a tp v, when h_simp.is_none $ change' pv (some (expr.const a [])) loc.wildcard, match rev_name with | some (flip, id) := do nv ← get_local a, pf ← to_expr (cond flip ``(%%pv = %%nv) ``(%%nv = %%pv)) >>= assert id, reflexivity | none := skip end end interactive end tactic
d1b96c53aafe9fc42e5f409ce742777c91f3f1fe
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/AdditiveUnital.lean
b0a730845e7c38fbb101b8a2e5e54b5ba8f67858
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
7,394
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section AdditiveUnital structure AdditiveUnital (A : Type) : Type := (zero : A) (plus : (A → (A → A))) (lunit_zero : (∀ {x : A} , (plus zero x) = x)) (runit_zero : (∀ {x : A} , (plus x zero) = x)) open AdditiveUnital structure Sig (AS : Type) : Type := (zeroS : AS) (plusS : (AS → (AS → AS))) structure Product (A : Type) : Type := (zeroP : (Prod A A)) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (lunit_0P : (∀ {xP : (Prod A A)} , (plusP zeroP xP) = xP)) (runit_0P : (∀ {xP : (Prod A A)} , (plusP xP zeroP) = xP)) structure Hom {A1 : Type} {A2 : Type} (Ad1 : (AdditiveUnital A1)) (Ad2 : (AdditiveUnital A2)) : Type := (hom : (A1 → A2)) (pres_zero : (hom (zero Ad1)) = (zero Ad2)) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Ad1) x1 x2)) = ((plus Ad2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ad1 : (AdditiveUnital A1)) (Ad2 : (AdditiveUnital A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_zero : (interp (zero Ad1) (zero Ad2))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Ad1) x1 x2) ((plus Ad2) y1 y2)))))) inductive AdditiveUnitalTerm : Type | zeroL : AdditiveUnitalTerm | plusL : (AdditiveUnitalTerm → (AdditiveUnitalTerm → AdditiveUnitalTerm)) open AdditiveUnitalTerm inductive ClAdditiveUnitalTerm (A : Type) : Type | sing : (A → ClAdditiveUnitalTerm) | zeroCl : ClAdditiveUnitalTerm | plusCl : (ClAdditiveUnitalTerm → (ClAdditiveUnitalTerm → ClAdditiveUnitalTerm)) open ClAdditiveUnitalTerm inductive OpAdditiveUnitalTerm (n : ℕ) : Type | v : ((fin n) → OpAdditiveUnitalTerm) | zeroOL : OpAdditiveUnitalTerm | plusOL : (OpAdditiveUnitalTerm → (OpAdditiveUnitalTerm → OpAdditiveUnitalTerm)) open OpAdditiveUnitalTerm inductive OpAdditiveUnitalTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpAdditiveUnitalTerm2) | sing2 : (A → OpAdditiveUnitalTerm2) | zeroOL2 : OpAdditiveUnitalTerm2 | plusOL2 : (OpAdditiveUnitalTerm2 → (OpAdditiveUnitalTerm2 → OpAdditiveUnitalTerm2)) open OpAdditiveUnitalTerm2 def simplifyCl {A : Type} : ((ClAdditiveUnitalTerm A) → (ClAdditiveUnitalTerm A)) | (plusCl zeroCl x) := x | (plusCl x zeroCl) := x | zeroCl := zeroCl | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpAdditiveUnitalTerm n) → (OpAdditiveUnitalTerm n)) | (plusOL zeroOL x) := x | (plusOL x zeroOL) := x | zeroOL := zeroOL | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpAdditiveUnitalTerm2 n A) → (OpAdditiveUnitalTerm2 n A)) | (plusOL2 zeroOL2 x) := x | (plusOL2 x zeroOL2) := x | zeroOL2 := zeroOL2 | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((AdditiveUnital A) → (AdditiveUnitalTerm → A)) | Ad zeroL := (zero Ad) | Ad (plusL x1 x2) := ((plus Ad) (evalB Ad x1) (evalB Ad x2)) def evalCl {A : Type} : ((AdditiveUnital A) → ((ClAdditiveUnitalTerm A) → A)) | Ad (sing x1) := x1 | Ad zeroCl := (zero Ad) | Ad (plusCl x1 x2) := ((plus Ad) (evalCl Ad x1) (evalCl Ad x2)) def evalOpB {A : Type} {n : ℕ} : ((AdditiveUnital A) → ((vector A n) → ((OpAdditiveUnitalTerm n) → A))) | Ad vars (v x1) := (nth vars x1) | Ad vars zeroOL := (zero Ad) | Ad vars (plusOL x1 x2) := ((plus Ad) (evalOpB Ad vars x1) (evalOpB Ad vars x2)) def evalOp {A : Type} {n : ℕ} : ((AdditiveUnital A) → ((vector A n) → ((OpAdditiveUnitalTerm2 n A) → A))) | Ad vars (v2 x1) := (nth vars x1) | Ad vars (sing2 x1) := x1 | Ad vars zeroOL2 := (zero Ad) | Ad vars (plusOL2 x1 x2) := ((plus Ad) (evalOp Ad vars x1) (evalOp Ad vars x2)) def inductionB {P : (AdditiveUnitalTerm → Type)} : ((P zeroL) → ((∀ (x1 x2 : AdditiveUnitalTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → (∀ (x : AdditiveUnitalTerm) , (P x)))) | p0l pplusl zeroL := p0l | p0l pplusl (plusL x1 x2) := (pplusl _ _ (inductionB p0l pplusl x1) (inductionB p0l pplusl x2)) def inductionCl {A : Type} {P : ((ClAdditiveUnitalTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P zeroCl) → ((∀ (x1 x2 : (ClAdditiveUnitalTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → (∀ (x : (ClAdditiveUnitalTerm A)) , (P x))))) | psing p0cl ppluscl (sing x1) := (psing x1) | psing p0cl ppluscl zeroCl := p0cl | psing p0cl ppluscl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing p0cl ppluscl x1) (inductionCl psing p0cl ppluscl x2)) def inductionOpB {n : ℕ} {P : ((OpAdditiveUnitalTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P zeroOL) → ((∀ (x1 x2 : (OpAdditiveUnitalTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → (∀ (x : (OpAdditiveUnitalTerm n)) , (P x))))) | pv p0ol pplusol (v x1) := (pv x1) | pv p0ol pplusol zeroOL := p0ol | pv p0ol pplusol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv p0ol pplusol x1) (inductionOpB pv p0ol pplusol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpAdditiveUnitalTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P zeroOL2) → ((∀ (x1 x2 : (OpAdditiveUnitalTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → (∀ (x : (OpAdditiveUnitalTerm2 n A)) , (P x)))))) | pv2 psing2 p0ol2 pplusol2 (v2 x1) := (pv2 x1) | pv2 psing2 p0ol2 pplusol2 (sing2 x1) := (psing2 x1) | pv2 psing2 p0ol2 pplusol2 zeroOL2 := p0ol2 | pv2 psing2 p0ol2 pplusol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 p0ol2 pplusol2 x1) (inductionOp pv2 psing2 p0ol2 pplusol2 x2)) def stageB : (AdditiveUnitalTerm → (Staged AdditiveUnitalTerm)) | zeroL := (Now zeroL) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClAdditiveUnitalTerm A) → (Staged (ClAdditiveUnitalTerm A))) | (sing x1) := (Now (sing x1)) | zeroCl := (Now zeroCl) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpAdditiveUnitalTerm n) → (Staged (OpAdditiveUnitalTerm n))) | (v x1) := (const (code (v x1))) | zeroOL := (Now zeroOL) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpAdditiveUnitalTerm2 n A) → (Staged (OpAdditiveUnitalTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | zeroOL2 := (Now zeroOL2) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (zeroT : (Repr A)) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) end AdditiveUnital
2223044ec082c690d65aa14ad6a4f9c3e05bbcf8
69d4931b605e11ca61881fc4f66db50a0a875e39
/test/monotonicity.lean
bbdb091a803c197d55589b2649f07a8126813b3b
[ "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
8,581
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.monotonicity import tactic.norm_num import algebra.ordered_ring import measure_theory.lebesgue_measure import data.list.defs open list tactic tactic.interactive set example (h : 3 + 6 ≤ 4 + 5) : 1 + 3 + 2 + 6 ≤ 4 + 2 + 1 + 5 := begin ac_mono, end example (h : 3 ≤ (4 : ℤ)) (h' : 5 ≤ (6 : ℤ)) : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 := begin ac_mono, mono, end example (h : 3 ≤ (4 : ℤ)) (h' : 5 ≤ (6 : ℤ)) : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 := begin transitivity (1 + 3 + 2 - 5 : ℤ), { ac_mono }, { ac_mono }, end example (x y z k : ℤ) (h : 3 ≤ (4 : ℤ)) (h' : z ≤ y) : (k + 3 + x) - y ≤ (k + 4 + x) - z := begin mono, norm_num end example (x y z a b : ℤ) (h : a ≤ (b : ℤ)) (h' : z ≤ y) : (1 + a + x) - y ≤ (1 + b + x) - z := begin transitivity (1 + a + x - z), { mono, }, { mono, mono, mono }, end example (x y z : ℤ) (h' : z ≤ y) : (1 + 3 + x) - y ≤ (1 + 4 + x) - z := begin transitivity (1 + 3 + x - z), { mono }, { mono, mono, norm_num }, end example (x y z : ℤ) (h : 3 ≤ (4 : ℤ)) (h' : z ≤ y) : (1 + 3 + x) - y ≤ (1 + 4 + x) - z := begin ac_mono, mono* end @[simp] def list.le' {α : Type*} [has_le α] : list α → list α → Prop | (x::xs) (y::ys) := x ≤ y ∧ list.le' xs ys | [] [] := true | _ _ := false @[simp] instance list_has_le {α : Type*} [has_le α] : has_le (list α) := ⟨ list.le' ⟩ lemma list.le_refl {α : Type*} [preorder α] {xs : list α} : xs ≤ xs := begin induction xs with x xs, { trivial }, { simp [has_le.le,list.le], split, apply le_refl, apply xs_ih } end -- @[trans] lemma list.le_trans {α : Type*} [preorder α] {xs zs : list α} (ys : list α) (h : xs ≤ ys) (h' : ys ≤ zs) : xs ≤ zs := begin revert ys zs, induction xs with x xs ; intros ys zs h h' ; cases ys with y ys ; cases zs with z zs ; try { cases h ; cases h' ; done }, { apply list.le_refl }, { simp [has_le.le,list.le], split, apply le_trans h.left h'.left, apply xs_ih _ h.right h'.right, } end @[mono] lemma list_le_mono_left {α : Type*} [preorder α] {xs ys zs : list α} (h : xs ≤ ys) : xs ++ zs ≤ ys ++ zs := begin revert ys, induction xs with x xs ; intros ys h, { cases ys, apply list.le_refl, cases h }, { cases ys with y ys, cases h, simp [has_le.le,list.le] at *, revert h, apply and.imp_right, apply xs_ih } end @[mono] lemma list_le_mono_right {α : Type*} [preorder α] {xs ys zs : list α} (h : xs ≤ ys) : zs ++ xs ≤ zs ++ ys := begin revert ys zs, induction xs with x xs ; intros ys zs h, { cases ys, { simp, apply list.le_refl }, cases h }, { cases ys with y ys, cases h, simp [has_le.le,list.le] at *, suffices : list.le' ((zs ++ [x]) ++ xs) ((zs ++ [y]) ++ ys), { refine cast _ this, simp, }, apply list.le_trans (zs ++ [y] ++ xs), { apply list_le_mono_left, induction zs with z zs, { simp [has_le.le,list.le], apply h.left }, { simp [has_le.le,list.le], split, apply le_refl, apply zs_ih, } }, { apply xs_ih h.right, } } end lemma bar_bar' (h : [] ++ [3] ++ [2] ≤ [1] ++ [5] ++ [4]) : [] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) := begin ac_mono, end lemma bar_bar'' (h : [3] ++ [2] ++ [2] ≤ [5] ++ [4] ++ []) : [1] ++ ([3] ++ [2]) ++ [2] ≤ [1] ++ [5] ++ ([4] ++ []) := begin ac_mono, end lemma bar_bar (h : [3] ++ [2] ≤ [5] ++ [4]) : [1] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) := begin ac_mono, end def P (x : ℕ) := 7 ≤ x def Q (x : ℕ) := x ≤ 7 @[mono] lemma P_mono {x y : ℕ} (h : x ≤ y) : P x → P y := by { intro h', apply le_trans h' h } @[mono] lemma Q_mono {x y : ℕ} (h : y ≤ x) : Q x → Q y := by apply le_trans h example (x y z : ℕ) (h : x ≤ y) : P (x + z) → P (z + y) := begin ac_mono, ac_mono, end example (x y z : ℕ) (h : y ≤ x) : Q (x + z) → Q (z + y) := begin ac_mono, ac_mono, end example (x y z k m n : ℤ) (h₀ : z ≤ 0) (h₁ : y ≤ x) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, ac_mono, ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, ac_mono, ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, -- ⊢ (m + x + n) * z ≤ z * (y + n + m) ac_mono, -- ⊢ m + x + n ≤ y + n + m ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := by { ac_mono* := h₁ } example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : m + x + n ≤ y + n + m) : (m + x + n) * z + k ≤ z * (y + n + m) + k := by { ac_mono* := h₁ } example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : n + x + m ≤ y + n + m) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono* : m + x + n ≤ y + n + m, transitivity ; [ skip , apply h₁ ], apply le_of_eq, ac_refl, end example (x y z k m n : ℤ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono, success_if_fail { ac_mono }, admit }, trivial end example (x y z k m n : ℕ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono*, change 0 ≤ z, apply nat.zero_le, }, trivial end example (x y z k m n : ℕ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono, change (m + x + n) * z ≤ z * (y + n + m), admit }, trivial, end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : (m + x + n + i) * z + k = z * (j + n + m + y) + k := begin ac_mono^3, cc end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : z * (x + i + n + m) + k = z * (y + j + n + m) + k := begin congr, simp [h₁], end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : (m + x + n + i) * z + k = z * (j + n + m + y) + k := begin ac_mono*, cc, end example (x y : ℕ) (h : x ≤ y) : true := begin (do v ← mk_mvar, p ← to_expr ```(%%v + x ≤ y + %%v), assert `h' p), ac_mono := h, trivial, exact 1, end example {x y z : ℕ} : true := begin have : y + x ≤ y + z, { mono, guard_target' x ≤ z, admit }, trivial end example {x y z : ℕ} : true := begin suffices : x + y ≤ z + y, trivial, mono, guard_target' x ≤ z, admit, end example {x y z w : ℕ} : true := begin have : x + y ≤ z + w, { mono, guard_target' x ≤ z, admit, guard_target' y ≤ w, admit }, trivial end example {x y z w : ℕ} : true := begin have : x * y ≤ z * w, { mono with [0 ≤ z,0 ≤ y], { guard_target 0 ≤ z, admit }, { guard_target 0 ≤ y, admit }, guard_target' x ≤ z, admit, guard_target' y ≤ w, admit }, trivial end example {x y z w : Prop} : true := begin have : x ∧ y → z ∧ w, { mono, guard_target' x → z, admit, guard_target' y → w, admit }, trivial end example {x y z w : Prop} : true := begin have : x ∨ y → z ∨ w, { mono, guard_target' x → z, admit, guard_target' y → w, admit }, trivial end example {x y z w : ℤ} : true := begin suffices : x + y < w + z, trivial, have : x < w, admit, have : y ≤ z, admit, mono right, end example {x y z w : ℤ} : true := begin suffices : x * y < w * z, trivial, have : x < w, admit, have : y ≤ z, admit, mono right, { guard_target' 0 < y, admit }, { guard_target' 0 ≤ w, admit }, end open tactic example (x y : ℕ) (h : x ≤ y) : true := begin (do v ← mk_mvar, p ← to_expr ```(%%v + x ≤ y + %%v), assert `h' p), ac_mono := h, trivial, exact 3 end example {α} [linear_order α] (a b c d e : α) : max a b ≤ e → b ≤ e := by { mono, apply le_max_right } example (a b c d e : Prop) (h : d → a) (h' : c → e) : (a ∧ b → c) ∨ d → (d ∧ b → e) ∨ a := begin mono, mono, mono, end example : ∫ x in Icc 0 1, real.exp x ≤ ∫ x in Icc 0 1, real.exp (x+1) := begin mono, { exact real.continuous_exp.integrable_on_compact compact_Icc }, { exact (real.continuous_exp.comp $ continuous_add_right 1).integrable_on_compact compact_Icc }, intro x, dsimp only, mono, linarith end
f5ee64aa9d3657e86e697b31e126dc228e7e939b
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/data/finset.lean
f0754f688432d4e16373f839aac7ce9a8ebaeea6
[ "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
89,279
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro Finite sets. -/ import logic.embedding algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /- set coercion -/ /-- Convert a finset to a set in the natural way. -/ def to_set (s : finset α) : set α := {x | x ∈ s} instance : has_lift (finset α) (set α) := ⟨to_set⟩ @[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl /- extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[extensionality] theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext.2 @[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := (set.ext_iff _ _).trans ext.symm lemma to_set_injective {α} : function.injective (finset.to_set : finset α → set α) := λ s t, coe_inj.1 /- subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_iff_subset_not_subset, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff /- empty -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ | e := not_mem_empty a $ e ▸ h @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem exists_mem_iff_ne_empty {s : finset α} : (∃ a : α, a ∈ s) ↔ ¬s = ∅ := ⟨λ ⟨a, ha⟩, ne_empty_of_mem ha, exists_mem_of_ne_empty⟩ @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl lemma nonempty_iff_ne_empty (s : finset α) : nonempty (↑s : set α) ↔ s ≠ ∅ := begin rw [set.coe_nonempty_iff_ne_empty, ←coe_empty], apply not_congr, apply function.injective.eq_iff, exact to_set_injective end /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩ local prefix `ι`:90 := singleton @[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := not_iff_not_of_iff mem_singleton theorem mem_singleton_self (a : α) : a ∈ ι a := or.inl rfl theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := rfl /- insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ @[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext.2 $ λ x, by simp only [finset.mem_insert, or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext.2 $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume ⟨h₁, h₂⟩, have ∃a ∈ t, a ∉ s, by simpa only [finset.subset_iff, classical.not_forall] using h₂, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s @[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl @[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a := insert_eq_of_mem $ mem_singleton_self _ /- union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by rw [mem_union, not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ @[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext.2 $ λ x, by simp only [mem_union, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext.2 $ λ x, by simp only [mem_union, or_assoc] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ λ _, mem_union.trans $ or_self _ instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext.2 $ λ _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext.2 $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)] @[simp] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext.2 $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext.2 $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] /- inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ λ _, by simp only [mem_inter, and_assoc] @[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ λ _, by simp only [mem_inter, and.left_comm] @[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ λ _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ λ _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ λ _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ λ _, mem_inter.trans $ false_and _ @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext.2 $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext.2 $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : ι a ∩ s = ι a := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : ι a ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /- lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.lattice.semilattice_inf_bot, ..finset.lattice.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right /- erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2 @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext.2 $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext.2 $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /- sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 @[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext.2 $ λ a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) @[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := (inter_comm _ _).trans (inter_sdiff_self _ _) theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] lemma to_set_sdiff (s t : finset α) : (s \ t).to_set = s.to_set \ t.to_set := by apply finset.coe_sdiff end decidable_eq /- attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /- filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext.2 $ assume a, by simp only [mem_filter, and_comm, and.left_comm] @[simp] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext.2 $ assume a, by simp only [mem_filter, and_false]; refl lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext.2 $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext.2 $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) := by {ext, simp [and_assoc], rw [and.left_comm] } theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext.2 $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext.2 $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ λ _, by simp only [mem_sdiff, mem_filter] theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset s)] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} [decidable_pred (∈ t₁)] (h : ↑s ⊆ t₁ ∪ t₂) : ∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, classical.or_not] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter(eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, insert_empty_eq_singleton, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end end filter /- range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := finset.induction_on s ⟨0, empty_subset _⟩ $ λ a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa only [mem_range] using le_max_left (a+1) n, subset.trans hn (by simpa only [range_subset] using le_max_right (a+1) n)⟩⟩ end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := finset.singleton a end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = finset.singleton a := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /- erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext' $ by simp @[simp] lemma to_finset_smul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (add_monoid.smul n s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, add_monoid.one_smul] }, { rw [add_monoid.add_smul, to_finset_add, add_monoid.one_smul, to_finset_smul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext' $ by simp theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset section map open function def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_inj f.2 @[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := by simp only [subset.antisymm_iff, map_subset_map] def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext.2 $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext.2 $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext.2 $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩ @[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) := ext.2 $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, insert_empty_eq_singleton, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ_inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] @[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans $ by simp only [exists_prop]; refl theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] @[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext.2 $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext.2 $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton (f : α → β) (a : α) : (singleton a).image f = singleton (f a) := ext.2 $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, insert_empty_eq_singleton, image_singleton, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext.2 $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (assume h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (assume h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const {s : finset α} (h : s ≠ ∅) (b : β) : s.image (λa, b) = singleton b := ext.2 $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, exists_mem_of_ne_empty h, true_and, mem_singleton, eq_comm] protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subset_image_iff [decidable_eq α] {f : α → β} {s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s := begin split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { exact ⟨∅, set.empty_subset _, finset.image_empty _⟩ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi] end end image /- card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = finset.singleton a := by cases s; simp [multiset.card_eq_one, finset.singleton, finset.card] @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} : card (erase s a) ≤ card s := card_erase_le @[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa only [ext, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on [decidable_eq β] {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext.2 $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) (λ a, by by_cases a ∈ s; simp * {contextual := tt}) lemma card_union_le [decidable_eq α] (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ le_add_right _ _ lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : card t ≤ card s) : (∀ b ∈ t, ∃ a ha, b = f a ha) := by haveI := classical.dec_eq β; exact λ b hb, have h : card (image (λ (a : {a // a ∈ s}), f (a.val) a.2) (attach s)) = card s, from @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (λ a : {a // a ∈ s}, f a.1 a.2) s.attach = t := eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩, exact ⟨a, a.2, ha₂.symm⟩, end end card section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext.2 $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext.2 $ λ x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a := show (insert a ∅ : finset α).bind t = t a, from bind_insert.trans $ union_empty _ theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bind f ∩ t = s.bind (λ x, f x ∩ t) := by { ext x, simp, exact ⟨λ ⟨xt, y, ys, xf⟩, ⟨y, ys, xt, xf⟩, λ ⟨y, ys, xt, xf⟩, ⟨xt, y, ys, xf⟩⟩ } theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bind f = s.bind (λ x, t ∩ f x) := by rw [inter_comm, bind_inter]; simp theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [image_insert, bind_insert, ih]) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [bind_insert, image_union, ih]) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext.2 $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop] lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop] lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext.2 $ λ x, by simp only [mem_bind, mem_image, insert_empty_eq_singleton, mem_singleton, eq_comm] lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bind (λa, s.filter $ (λc, g c = a)) = s := begin ext b, simp, split, { rintros ⟨a, ⟨b', _, _⟩, hb, _⟩, exact hb }, { rintros hb, exact ⟨g b, ⟨b, hb, rfl⟩, hb, rfl⟩ } end end bind section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext.2 $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} (H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ := λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext.2 $ λ ⟨x, y⟩, by simp only [mem_sigma, mem_bind, mem_image, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, heq_iff_eq, exists_eq_right] end sigma section pi variables {δ : α → Type*} [decidable_eq α] def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ def pi.empty (β : α → Sort*) (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa only [mem_cons, mem_insert] using h) = pi.cons s a b e₂ e (by simpa only [mem_cons, mem_insert] using h), by rw [eq], this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end end pi section powerset def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) end powerset section powerset_len def powerset_len (n : ℕ) (s : finset α) : finset (finset α) := ⟨(s.1.powerset_len n).pmap finset.mk (λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset_len s.2)⟩ theorem mem_powerset_len {n} {s t : finset α} : s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n := by cases s; simp [powerset_len, val_le_iff.symm]; refl @[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) : powerset_len n s ⊆ powerset_len n t := λ u h', mem_powerset_len.2 $ and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h') @[simp] theorem card_powerset_len (n : ℕ) (s : finset α) : card (powerset_len n s) = nat.choose (card s) n := (card_pmap _ _ _).trans (card_powerset_len n s.1) end powerset_len section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by unfold fold; rw [insert_val, ndinsert_of_not_mem h, map_cons, fold_cons_left] @[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b := rfl @[simp] theorem fold_map {g : γ ↪ α} {s : finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, map, multiset.map_map] @[simp] theorem fold_image [decidable_eq α] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, image_val_of_inj_on H, multiset.map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp only [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp only [map_cons, fold_cons_left, fold] end fold section sup variables [semilattice_sup_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_val : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton [decidable_eq β] {b : β} : ({b} : finset β).sup f = f b := calc _ = f b ⊔ (∅:finset β).sup f : sup_insert ... = f b : sup_bot_eq lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_refl _) (λ a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le_sup H.1 (ih H.2)) lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := by letI := classical.dec_eq β; from calc f b ≤ f b ⊔ s.sup f : le_sup_left ... = (insert b s).sup f : sup_insert.symm ... = s.sup f : by rw [insert_eq_of_mem hb] lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := by letI := classical.dec_eq β; from finset.induction_on s (λ _, bot_le) (λ n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le H.1 (ih H.2)) lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := iff.intro (assume h b hb, le_trans (le_sup hb) h) sup_le lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) lemma sup_lt [is_total α (≤)] {a : α} : (⊥ < a) → (∀b ∈ s, f b < a) → s.sup f < a := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) lemma comp_sup_eq_sup_comp [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ] (g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := have A : ∀x y, g (x ⊔ y) = g x ⊔ g y := begin assume x y, cases (is_total.total (≤) x y) with h, { simp [sup_of_le_right h, sup_of_le_right (mono_g h)] }, { simp [sup_of_le_left h, sup_of_le_left (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [bot]) (by simp [A] {contextual := tt}) end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) section inf variables [semilattice_inf_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_val : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton [decidable_eq β] {b : β} : ({b} : finset β).inf f = f b := calc _ = f b ⊓ (∅:finset β).inf f : inf_insert ... = f b : inf_top_eq lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by rw [empty_union, inf_empty, top_inf_eq]) $ λ a s has ih, by rw [insert_union, inf_insert, inf_insert, ih, inf_assoc] theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_refl _) (λ a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact inf_le_inf H.1 (ih H.2)) lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := by letI := classical.dec_eq β; from calc f b ≥ f b ⊓ s.inf f : inf_le_left ... = (insert b s).inf f : inf_insert.symm ... = s.inf f : by rw [insert_eq_of_mem hb] lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_top) (λ n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact le_inf H.1 (ih H.2)) lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ (∀b ∈ s, a ≤ f b) := iff.intro (assume h b hb, le_trans h (inf_le hb)) le_inf lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma lt_inf [is_total α (≤)] {a : α} : (a < ⊤) → (∀b ∈ s, a < f b) → a < s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) lemma comp_inf_eq_inf_comp [is_total α (≤)] {γ : Type} [semilattice_inf_top γ] (g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := have A : ∀x y, g (x ⊓ y) = g x ⊓ g y := begin assume x y, cases (is_total.total (≤) x y) with h, { simp [inf_of_le_left h, inf_of_le_left (mono_g h)] }, { simp [inf_of_le_right h, inf_of_le_right (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [top]) (by simp [A] {contextual := tt}) end inf lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) := le_antisymm (le_infi $ assume a, le_infi $ assume ha, inf_le ha) (finset.le_inf $ assume a ha, infi_le_of_le a $ infi_le _ ha) /- max and min of finite sets -/ section max_min variables [decidable_linear_order α] protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := max_insert @[simp] theorem max_singleton' {a : α} : finset.max (singleton a) = some a := max_singleton theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, by_contradiction $ λ hs, let ⟨a, ha⟩ := max_of_ne_empty hs in by rw [h] at ha; cases ha, λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := min_insert theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, by_contradiction $ λ hs, let ⟨a, ha⟩ := min_of_ne_empty hs in by rw [h] at ha; cases ha, λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (λ _ H, by cases H) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; rw [min_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption lemma exists_min (s : finset β) (f : β → α) (h : nonempty ↥(↑s : set β)) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := begin have : s.image f ≠ ∅, rwa [ne, image_eq_empty, ← ne.def, ← nonempty_iff_ne_empty], cases min_of_ne_empty this with y hy, rcases mem_image.mp (mem_of_min hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', min_le_of_mem (mem_image_of_mem f hx') hy⟩ end end max_min section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ @[simp] theorem length_sort {s : finset α} : (sort r s).length = s.card := multiset.length_sort _ end sort section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_bind_left {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin refine s.induction _ _, { simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] } end lemma disjoint_bind_right {ι : Type*} [decidable_eq ι] (s : finset α) (t : finset ι) (f : ι → finset α) : disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bind_left t f s @[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) : card (s ∪ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : (∀x, p x → ¬ q x) → disjoint (s.filter p) (s.filter q) := assume h, by simp only [disjoint_iff_ne, mem_filter]; rintros a ⟨_, ha⟩ b ⟨_, hb⟩ eq; rw [eq] at ha; exact h _ ha hb end disjoint theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp 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 theorem lt_wf {α} [decidable_eq α] : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image (<) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf section decidable_linear_order variables {α} [decidable_linear_order α] def min' (S : finset α) (H : S ≠ ∅) : α := @option.get _ S.min $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] def max' (S : finset α) (H : S ≠ ∅) : α := @option.get _ S.max $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (S : finset α) (H : S ≠ ∅) theorem min'_mem : S.min' H ∈ S := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ S) : S.min' H ≤ x := min_le_of_mem H2 $ option.get_mem _ theorem le_min' (x) (H2 : ∀ y ∈ S, x ≤ y) : x ≤ S.min' H := H2 _ $ min'_mem _ _ theorem max'_mem : S.max' H ∈ S := mem_of_max $ by simp [max'] theorem le_max' (x) (H2 : x ∈ S) : x ≤ S.max' H := le_max_of_mem H2 $ option.get_mem _ theorem max'_le (x) (H2 : ∀ y ∈ S, y ≤ x) : S.max' H ≤ x := H2 _ $ max'_mem _ _ theorem min'_lt_max' {i j} (H1 : i ∈ S) (H2 : j ∈ S) (H3 : i ≠ j) : S.min' H < S.max' H := begin rcases lt_trichotomy i j with H4 | H4 | H4, { have H5 := min'_le S H i H1, have H6 := le_max' S H j H2, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 }, { cc }, { have H5 := min'_le S H j H2, have H6 := le_max' S H i H1, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 } end end decidable_linear_order /- Ico (a closed openinterval) -/ variables {n m l : ℕ} /-- `Ico n m` is the set of natural numbers `n ≤ k < m`. -/ def Ico (n m : ℕ) : finset ℕ := ⟨_, Ico.nodup n m⟩ namespace Ico @[simp] theorem val (n m : ℕ) : (Ico n m).1 = multiset.Ico n m := rfl @[simp] theorem to_finset (n m : ℕ) : (multiset.Ico n m).to_finset = Ico n m := (multiset.to_finset_eq _).symm theorem image_add (n m k : ℕ) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] theorem image_sub (n m k : ℕ) (h : k ≤ n) : (Ico n m).image (λ x, x - k) = Ico (n - k) (m - k) := begin dsimp [image], rw [multiset.Ico.map_sub _ _ _ h, ←multiset.to_finset_eq], refl, end theorem zero_bot (n : ℕ) : Ico 0 n = range n := eq_of_veq $ multiset.Ico.zero_bot _ @[simp] theorem card (n m : ℕ) : (Ico n m).card = m - n := multiset.Ico.card _ _ @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := multiset.Ico.mem theorem eq_empty_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = ∅ := eq_of_veq $ multiset.Ico.eq_zero_of_le h @[simp] theorem self_eq_empty {n : ℕ} : Ico n n = ∅ := eq_empty_of_le $ le_refl n @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = ∅ ↔ m ≤ n := iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff lemma union_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ∪ Ico m l = Ico n l := by rw [← to_finset, ← to_finset, ← multiset.to_finset_add, multiset.Ico.add_consecutive hnm hml, to_finset] @[simp] lemma inter_consecutive {n m l : ℕ} : Ico n m ∩ Ico m l = ∅ := begin rw [← to_finset, ← to_finset, ← multiset.to_finset_inter, multiset.Ico.inter_consecutive], simp, end @[simp] theorem succ_singleton {n : ℕ} : Ico n (n+1) = {n} := eq_of_veq $ multiset.Ico.succ_singleton theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = insert m (Ico n m) := by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset] theorem succ_top' {n m : ℕ} (h : n < m) : Ico n m = insert (m - 1) (Ico n (m - 1)) := begin have w : m = m - 1 + 1 := (nat.sub_add_cancel (nat.one_le_of_lt h)).symm, conv { to_lhs, rw w }, rw succ_top, exact nat.le_pred_of_lt h end theorem eq_cons {n m : ℕ} (h : n < m) : Ico n m = insert n (Ico (n + 1) m) := by rw [← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : ℕ} (h : 0 < m) : Ico (m - 1) m = {m - 1} := eq_of_veq $ multiset.Ico.pred_singleton h @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := multiset.Ico.not_mem_top lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = ∅ := eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := eq_of_veq $ multiset.Ico.filter_lt n m l lemma filter_le_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, l ≤ x) = Ico n m := eq_of_veq $ multiset.Ico.filter_le_of_le_bot hln lemma filter_le_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, l ≤ x) = ∅ := eq_of_veq $ multiset.Ico.filter_le_of_top_le hml lemma filter_le_of_le {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, l ≤ x) = Ico l m := eq_of_veq $ multiset.Ico.filter_le_of_le hnl @[simp] lemma filter_le (n m l : ℕ) : (Ico n m).filter (λ x, l ≤ x) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_le n m l @[simp] lemma diff_left (l n m : ℕ) : (Ico n m) \ (Ico n l) = Ico (max n l) m := by ext k; by_cases n ≤ k; simp [h, and_comm] @[simp] lemma diff_right (l n m : ℕ) : (Ico n m) \ (Ico l m) = Ico n (min m l) := have ∀k, (k < m ∧ (l ≤ k → m ≤ k)) ↔ (k < m ∧ k < l) := assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk], by ext k; by_cases n ≤ k; simp [h, this] end Ico end finset namespace multiset lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end end multiset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h end list namespace lattice variables {ι : Sort*} [complete_lattice α] [decidable_eq ι] lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset (plift ι), ⨆i∈t, s (plift.down i)) := le_antisymm (supr_le $ assume b, le_supr_of_le {plift.up b} $ le_supr_of_le (plift.up b) $ le_supr_of_le (by simp) $ le_refl _) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset (plift ι), ⨅i∈t, s (plift.down i)) := le_antisymm (le_infi $ assume t, le_infi $ assume b, le_infi $ assume hb, infi_le _ _) (le_infi $ assume b, infi_le_of_le {plift.up b} $ infi_le_of_le (plift.up b) $ infi_le_of_le (by simp) $ le_refl _) end lattice namespace set variables {ι : Sort*} [decidable_eq ι] lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset (plift ι), ⋃i∈t, s (plift.down i)) := lattice.supr_eq_supr_finset s lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset (plift ι), ⋂i∈t, s (plift.down i)) := lattice.infi_eq_infi_finset s end set namespace finset namespace nat /-- The antidiagonal of a natural number `n` is the finset of pairs `(i,j)` such that `i+j = n`. -/ def antidiagonal (n : ℕ) : finset (ℕ × ℕ) := (multiset.nat.antidiagonal n).to_finset /-- 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 := by rw [antidiagonal, multiset.mem_to_finset, multiset.nat.mem_antidiagonal] /-- The cardinality of the antidiagonal of `n` is `n+1`. -/ @[simp] lemma card_antidiagonal (n : ℕ) : (antidiagonal n).card = n+1 := by simpa using list.to_finset_card_of_nodup (list.nat.nodup_antidiagonal n) /-- The antidiagonal of `0` is the list `[(0,0)]` -/ @[simp] lemma antidiagonal_zero : antidiagonal 0 = {(0, 0)} := by { rw [antidiagonal, multiset.nat.antidiagonal_zero], refl } end nat end finset
3a4fa565f89de294de0fa32387d13999658fc6da
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Lean/Elab/Level.lean
d487c480300d1bc6d474accc267e04d5fa9a30c8
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,296
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.LevelDefEq import Lean.Elab.Exception import Lean.Elab.Log import Lean.Elab.AutoBound namespace Lean.Elab.Level structure Context where options : Options ref : Syntax autoBoundImplicit : Bool structure State where ngen : NameGenerator mctx : MetavarContext levelNames : List Name abbrev LevelElabM := ReaderT Context (EStateM Exception State) instance : MonadOptions LevelElabM where getOptions := return (← read).options instance : MonadRef LevelElabM where getRef := return (← read).ref withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : AddMessageContext LevelElabM where addMessageContext msg := pure msg instance : MonadNameGenerator LevelElabM where getNGen := return (← get).ngen setNGen ngen := modify fun s => { s with ngen := ngen } def mkFreshLevelMVar : LevelElabM Level := do let mvarId ← mkFreshId modify fun s => { s with mctx := s.mctx.addLevelMVarDecl mvarId } return mkLevelMVar mvarId register_builtin_option maxUniverseOffset : Nat := { defValue := 32 descr := "maximum universe level offset" } private def checkUniverseOffset [Monad m] [MonadError m] [MonadOptions m] (n : Nat) : m Unit := do let max := maxUniverseOffset.get (← getOptions) unless n <= max do throwError! "maximum universe level offset threshold ({max}) has been reached, you can increase the limit using option `set_option maxUniverseOffset <limit>`, but you are probably misusing universe levels since offsets are usually small natural numbers" partial def elabLevel (stx : Syntax) : LevelElabM Level := withRef stx do let kind := stx.getKind if kind == `Lean.Parser.Level.paren then elabLevel (stx.getArg 1) else if kind == `Lean.Parser.Level.max then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.imax then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelIMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.hole then mkFreshLevelMVar else if kind == numLitKind then match stx.isNatLit? with | some val => checkUniverseOffset val; return Level.ofNat val | none => throwIllFormedSyntax else if kind == identKind then let paramName := stx.getId unless (← get).levelNames.contains paramName do if (← read).autoBoundImplicit && isValidAutoBoundLevelName paramName then modify fun s => { s with levelNames := paramName :: s.levelNames } else throwError! "unknown universe level '{paramName}'" return mkLevelParam paramName else if kind == `Lean.Parser.Level.addLit then let lvl ← elabLevel (stx.getArg 0) match stx.getArg 2 |>.isNatLit? with | some val => checkUniverseOffset val; return lvl.addOffset val | none => throwIllFormedSyntax else throwError "unexpected universe level syntax kind" end Lean.Elab.Level
e200beffd6487a4cd54ef4c873a61cfda911e000
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/ForEachExpr.lean
08f41d776ee2e4e9dc23e1654c484597ef3e634f
[ "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
2,096
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.Expr import Lean.Util.MonadCache import Lean.Meta.Basic namespace Lean.Meta namespace ForEachExpr abbrev M := MonadCacheT Expr Unit MetaM mutual private partial def visitBinder (fn : Expr → MetaM Bool) : Array Expr → Nat → Expr → M Unit | fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.forallE n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars; visit fn t; let v := v.instantiateRevRange j fvars.size fvars; visit fn v; withLetDecl n t v fun x => visitBinder fn (fvars.push x) j b | fvars, j, e => visit fn $ e.instantiateRevRange j fvars.size fvars partial def visit (fn : Expr → MetaM Bool) (e : Expr) : M Unit := checkCache e fun _ => do if (← liftM (fn e)) then match e with | Expr.forallE _ _ _ _ => visitBinder fn #[] 0 e | Expr.lam _ _ _ _ => visitBinder fn #[] 0 e | Expr.letE _ _ _ _ _ => visitBinder fn #[] 0 e | Expr.app f a _ => visit fn f; visit fn a | Expr.mdata _ b _ => visit fn b | Expr.proj _ _ b _ => visit fn b | _ => pure () end end ForEachExpr /-- Similar to `Expr.forEach'`, but creates free variables whenever going inside of a binder. -/ def forEachExpr' (e : Expr) (f : Expr → MetaM Bool) : MetaM Unit := ForEachExpr.visit f e |>.run /-- Similar to `Expr.forEach`, but creates free variables whenever going inside of a binder. -/ def forEachExpr (e : Expr) (f : Expr → MetaM Unit) : MetaM Unit := forEachExpr' e fun e => do f e pure true end Lean.Meta
06934df2c7d201548f09d356aebd22626568a10c
618003631150032a5676f229d13a079ac875ff77
/src/tactic/lean_core_docs.lean
dbae033cfc5be7ccb50b410cb2d92b7a0062d43e
[ "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
22,856
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Bryan Gin-ge Chen, Robert Y. Lewis, Scott Morrison -/ import tactic.doc_commands /-! # Core tactic documentation This file adds the majority of the interactive tactics from core Lean (i.e. pre-mathlib) to the API documentation. ## TODO * Make a PR to core changing core docstrings to the docstrings below, and also changing the docstrings of `cc`, `simp` and `conv` to the ones already in the API docs. * SMT tactics are currently not documented. * `rsimp` and `constructor_matching` are currently not documented. * `dsimp` deserves better documentation. -/ add_tactic_doc { name := "abstract", category := doc_category.tactic, decl_names := [`tactic.interactive.abstract], tags := ["core", "proof extraction"] } /-- Proves a goal of the form `s = t` when `s` and `t` are expressions built up out of a binary operation, and equality can be proved using associativity and commutativity of that operation. -/ add_tactic_doc { name := "ac_refl", category := doc_category.tactic, decl_names := [`tactic.interactive.ac_refl, `tactic.interactive.ac_reflexivity], tags := ["core", "lemma application", "finishing"] } add_tactic_doc { name := "all_goals", category := doc_category.tactic, decl_names := [`tactic.interactive.all_goals], tags := ["core", "goal management"] } add_tactic_doc { name := "any_goals", category := doc_category.tactic, decl_names := [`tactic.interactive.any_goals], tags := ["core", "goal management"] } add_tactic_doc { name := "apply", category := doc_category.tactic, decl_names := [`tactic.interactive.apply], tags := ["core", "basic", "lemma application"] } add_tactic_doc { name := "apply_auto_param", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_auto_param], tags := ["core", "lemma application"] } add_tactic_doc { name := "apply_instance", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_instance], tags := ["core", "type class"] } add_tactic_doc { name := "apply_opt_param", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_opt_param], tags := ["core", "lemma application"] } add_tactic_doc { name := "apply_with", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_with], tags := ["core", "lemma application"] } add_tactic_doc { name := "assume", category := doc_category.tactic, decl_names := [`tactic.interactive.assume], tags := ["core", "logic"] } add_tactic_doc { name := "assumption", category := doc_category.tactic, decl_names := [`tactic.interactive.assumption], tags := ["core", "basic", "finishing"] } add_tactic_doc { name := "assumption'", category := doc_category.tactic, decl_names := [`tactic.interactive.assumption'], tags := ["core", "goal management"] } add_tactic_doc { name := "async", category := doc_category.tactic, decl_names := [`tactic.interactive.async], tags := ["core", "goal management", "combinator", "proof extraction"] } /-- `by_cases p` splits the main goal into two cases, assuming `h : p` in the first branch, and `h : ¬ p` in the second branch. You can specify the name of the new hypothesis using the syntax `by_cases h : p`. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `open_locale classical` (or `local attribute [instance, priority 10] classical.prop_decidable` if you are not using mathlib). -/ add_tactic_doc { name := "by_cases", category := doc_category.tactic, decl_names := [`tactic.interactive.by_cases], tags := ["core", "basic", "logic", "case bashing"] } /-- If the target of the main goal is a proposition `p`, `by_contra h` reduces the goal to proving `false` using the additional hypothesis `h : ¬ p`. If `h` is omitted, a name is generated automatically. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `open_locale classical` (or `local attribute [instance, priority 10] classical.prop_decidable` if you are not using mathlib). -/ add_tactic_doc { name := "by_contra / by_contradiction", category := doc_category.tactic, decl_names := [`tactic.interactive.by_contra, `tactic.interactive.by_contradiction], tags := ["core", "logic"] } add_tactic_doc { name := "case", category := doc_category.tactic, decl_names := [`tactic.interactive.case], tags := ["core", "goal management"] } add_tactic_doc { name := "cases", category := doc_category.tactic, decl_names := [`tactic.interactive.cases], tags := ["core", "basic", "induction"] } /-- `cases_matching p` applies the `cases` tactic to a hypothesis `h : type` if `type` matches the pattern `p`. `cases_matching [p_1, ..., p_n]` applies the `cases` tactic to a hypothesis `h : type` if `type` matches one of the given patterns. `cases_matching* p` is a more efficient and compact version of `focus1 { repeat { cases_matching p } }`. It is more efficient because the pattern is compiled once. `casesm` is shorthand for `cases_matching`. Example: The following tactic destructs all conjunctions and disjunctions in the current context. ``` cases_matching* [_ ∨ _, _ ∧ _] ``` -/ add_tactic_doc { name := "cases_matching / casesm", category := doc_category.tactic, decl_names := [`tactic.interactive.cases_matching, `tactic.interactive.casesm], tags := ["core", "induction", "context management"] } /-- * `cases_type I` applies the `cases` tactic to a hypothesis `h : (I ...)` * `cases_type I_1 ... I_n` applies the `cases` tactic to a hypothesis `h : (I_1 ...)` or ... or `h : (I_n ...)` * `cases_type* I` is shorthand for `focus1 { repeat { cases_type I } }` * `cases_type! I` only applies `cases` if the number of resulting subgoals is <= 1. Example: The following tactic destructs all conjunctions and disjunctions in the current context. ``` cases_type* or and ``` -/ add_tactic_doc { name := "cases_type", category := doc_category.tactic, decl_names := [`tactic.interactive.cases_type], tags := ["core", "induction", "context management"] } add_tactic_doc { name := "change", category := doc_category.tactic, decl_names := [`tactic.interactive.change], tags := ["core", "basic", "renaming"] } add_tactic_doc { name := "clear", category := doc_category.tactic, decl_names := [`tactic.interactive.clear], tags := ["core", "context management"] } /-- Close goals of the form `n ≠ m` when `n` and `m` have type `nat`, `char`, `string`, `int` or `fin sz`, and they are literals. It also closes goals of the form `n < m`, `n > m`, `n ≤ m` and `n ≥ m` for `nat`. If the goal is of the form `n = m`, then it tries to close it using reflexivity. In mathlib, consider using `norm_num` instead for numeric types. -/ add_tactic_doc { name := "comp_val", category := doc_category.tactic, decl_names := [`tactic.interactive.comp_val], tags := ["core", "arithmetic"] } /-- The `congr` tactic attempts to identify both sides of an equality goal `A = B`, leaving as new goals the subterms of `A` and `B` which are not definitionally equal. Example: suppose the goal is `x * f y = g w * f z`. Then `congr` will produce two goals: `x = g w` and `y = z`. Note that `congr` can be over-aggressive at times; the `congr'` tactic in mathlib provides a more refined approach, by taking a parameter that limits the recursion depth. -/ add_tactic_doc { name := "congr", category := doc_category.tactic, decl_names := [`tactic.interactive.congr], tags := ["core", "congruence"] } add_tactic_doc { name := "constructor", category := doc_category.tactic, decl_names := [`tactic.interactive.constructor], tags := ["core", "logic"] } add_tactic_doc { name := "contradiction", category := doc_category.tactic, decl_names := [`tactic.interactive.contradiction], tags := ["core", "basic", "finishing"] } add_tactic_doc { name := "delta", category := doc_category.tactic, decl_names := [`tactic.interactive.delta], tags := ["core", "simplification"] } add_tactic_doc { name := "destruct", category := doc_category.tactic, decl_names := [`tactic.interactive.destruct], tags := ["core", "induction"] } add_tactic_doc { name := "done", category := doc_category.tactic, decl_names := [`tactic.interactive.done], tags := ["core", "goal management"] } add_tactic_doc { name := "dsimp", category := doc_category.tactic, decl_names := [`tactic.interactive.dsimp], tags := ["core", "simplification"] } add_tactic_doc { name := "dunfold", category := doc_category.tactic, decl_names := [`tactic.interactive.dunfold], tags := ["core", "simplification"] } add_tactic_doc { name := "eapply", category := doc_category.tactic, decl_names := [`tactic.interactive.eapply], tags := ["core", "lemma application"] } add_tactic_doc { name := "econstructor", category := doc_category.tactic, decl_names := [`tactic.interactive.econstructor], tags := ["core", "logic"] } /-- A variant of `rw` that uses the unifier more aggressively, unfolding semireducible definitions. -/ add_tactic_doc { name := "erewrite / erw", category := doc_category.tactic, decl_names := [`tactic.interactive.erewrite, `tactic.interactive.erw], tags := ["core", "rewriting"] } add_tactic_doc { name := "exact", category := doc_category.tactic, decl_names := [`tactic.interactive.exact], tags := ["core", "basic", "finishing"] } add_tactic_doc { name := "exacts", category := doc_category.tactic, decl_names := [`tactic.interactive.exacts], tags := ["core", "finishing"] } add_tactic_doc { name := "exfalso", category := doc_category.tactic, decl_names := [`tactic.interactive.exfalso], tags := ["core", "basic", "logic"] } /-- `existsi e` will instantiate an existential quantifier in the target with `e` and leave the instantiated body as the new target. More generally, it applies to any inductive type with one constructor and at least two arguments, applying the constructor with `e` as the first argument and leaving the remaining arguments as goals. `existsi [e₁, ..., eₙ]` iteratively does the same for each expression in the list. Note: in mathlib, the `use` tactic is an equivalent tactic which sometimes is smarter with unification. -/ add_tactic_doc { name := "existsi", category := doc_category.tactic, decl_names := [`tactic.interactive.existsi], tags := ["core", "logic"] } add_tactic_doc { name := "fail_if_success", category := doc_category.tactic, decl_names := [`tactic.interactive.fail_if_success], tags := ["core", "testing", "combinator"] } add_tactic_doc { name := "fapply", category := doc_category.tactic, decl_names := [`tactic.interactive.fapply], tags := ["core", "lemma application"] } add_tactic_doc { name := "focus", category := doc_category.tactic, decl_names := [`tactic.interactive.focus], tags := ["core", "goal management", "combinator"] } add_tactic_doc { name := "from", category := doc_category.tactic, decl_names := [`tactic.interactive.from], tags := ["core", "finishing"] } /-- Apply function extensionality and introduce new hypotheses. The tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible to ``` |- ((fun x, ...) = (fun x, ...)) ``` The variant `funext h₁ ... hₙ` applies `funext` `n` times, and uses the given identifiers to name the new hypotheses. Note also the mathlib tactic `ext`, which applies as many extensionality lemmas as possible. -/ add_tactic_doc { name := "funext", category := doc_category.tactic, decl_names := [`tactic.interactive.funext], tags := ["core", "logic"] } add_tactic_doc { name := "generalize", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize], tags := ["core", "context management"] } add_tactic_doc { name := "guard_hyp", category := doc_category.tactic, decl_names := [`tactic.interactive.guard_hyp], tags := ["core", "testing", "context management"] } add_tactic_doc { name := "guard_target", category := doc_category.tactic, decl_names := [`tactic.interactive.guard_target], tags := ["core", "testing", "goal management"] } add_tactic_doc { name := "have", category := doc_category.tactic, decl_names := [`tactic.interactive.have], tags := ["core", "basic", "context management"] } add_tactic_doc { name := "induction", category := doc_category.tactic, decl_names := [`tactic.interactive.induction], tags := ["core", "basic", "induction"] } add_tactic_doc { name := "injection", category := doc_category.tactic, decl_names := [`tactic.interactive.injection], tags := ["core", "structures", "induction"] } add_tactic_doc { name := "injections", category := doc_category.tactic, decl_names := [`tactic.interactive.injections], tags := ["core", "structures", "induction"] } /-- If the current goal is a Pi/forall `∀ x : t, u` (resp. `let x := t in u`) then `intro` puts `x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`. If the goal is an arrow `t → u`, then it puts `h : t` in the local context and the new goal target is `u`. If the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the tactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter case, the tactic fails. The variant `intro z` uses the identifier `z` to name the new hypothesis. The variant `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h₁ ... hₙ` introduces `n` new hypotheses using the given identifiers to name them. -/ add_tactic_doc { name := "intro / intros", category := doc_category.tactic, decl_names := [`tactic.interactive.intro, `tactic.interactive.intros], tags := ["core", "basic", "logic"] } add_tactic_doc { name := "introv", category := doc_category.tactic, decl_names := [`tactic.interactive.introv], tags := ["core", "logic"] } add_tactic_doc { name := "iterate", category := doc_category.tactic, decl_names := [`tactic.interactive.iterate], tags := ["core", "combinator"] } /-- `left` applies the first constructor when the type of the target is an inductive data type with two constructors. Similarly, `right` applies the second constructor. -/ add_tactic_doc { name := "left / right", category := doc_category.tactic, decl_names := [`tactic.interactive.left, `tactic.interactive.right], tags := ["core", "basic", "logic"] } /-- `let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. Note the related mathlib tactic `set a := t with h`, which adds the hypothesis `h : a = t` to the local context and replaces `t` with `a` everywhere it can. -/ add_tactic_doc { name := "let", category := doc_category.tactic, decl_names := [`tactic.interactive.let], tags := ["core", "basic", "logic", "context management"] } add_tactic_doc { name := "mapply", category := doc_category.tactic, decl_names := [`tactic.interactive.mapply], tags := ["core", "lemma application"] } add_tactic_doc { name := "match_target", category := doc_category.tactic, decl_names := [`tactic.interactive.match_target], tags := ["core", "testing", "goal management"] } add_tactic_doc { name := "refine", category := doc_category.tactic, decl_names := [`tactic.interactive.refine], tags := ["core", "basic", "lemma application"] } /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation, that is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ add_tactic_doc { name := "refl / reflexivity", category := doc_category.tactic, decl_names := [`tactic.interactive.refl, `tactic.interactive.reflexivity], tags := ["core", "basic", "finishing"] } add_tactic_doc { name := "rename", category := doc_category.tactic, decl_names := [`tactic.interactive.rename], tags := ["core", "renaming"] } add_tactic_doc { name := "repeat", category := doc_category.tactic, decl_names := [`tactic.interactive.repeat], tags := ["core", "combinator"] } add_tactic_doc { name := "revert", category := doc_category.tactic, decl_names := [`tactic.interactive.revert], tags := ["core", "context management", "goal management"] } /-- `rw e` applies an equation or iff `e` as a rewrite rule to the main goal. If `e` is preceded by left arrow (`←` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined constant, then the equational lemmas associated with `e` are used. This provides a convenient way to unfold `e`. `rw [e₁, ..., eₙ]` applies the given rules sequentially. `rw e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses in the local context. In the latter case, a turnstile `⊢` or `|-` can also be used, to signify the target of the goal. `rewrite` is synonymous with `rw`. -/ add_tactic_doc { name := "rw / rewrite", category := doc_category.tactic, decl_names := [`tactic.interactive.rw, `tactic.interactive.rewrite], tags := ["core", "basic", "rewriting"] } add_tactic_doc { name := "rwa", category := doc_category.tactic, decl_names := [`tactic.interactive.rwa], tags := ["core", "rewriting"] } add_tactic_doc { name := "show", category := doc_category.tactic, decl_names := [`tactic.interactive.show], tags := ["core", "goal management", "renaming"] } add_tactic_doc { name := "simp_intros", category := doc_category.tactic, decl_names := [`tactic.interactive.simp_intros], tags := ["core", "simplification"] } add_tactic_doc { name := "skip", category := doc_category.tactic, decl_names := [`tactic.interactive.skip], tags := ["core", "combinator"] } add_tactic_doc { name := "solve1", category := doc_category.tactic, decl_names := [`tactic.interactive.solve1], tags := ["core", "combinator", "goal management"] } add_tactic_doc { name := "sorry / admit", category := doc_category.tactic, decl_names := [`tactic.interactive.sorry, `tactic.interactive.admit], inherit_description_from := `tactic.interactive.sorry, tags := ["core", "testing", "debugging"] } add_tactic_doc { name := "specialize", category := doc_category.tactic, decl_names := [`tactic.interactive.specialize], tags := ["core", "hypothesis management", "lemma application"] } add_tactic_doc { name := "split", category := doc_category.tactic, decl_names := [`tactic.interactive.split], tags := ["core", "basic", "logic"] } add_tactic_doc { name := "subst", category := doc_category.tactic, decl_names := [`tactic.interactive.subst], tags := ["core", "rewriting"] } add_tactic_doc { name := "subst_vars", category := doc_category.tactic, decl_names := [`tactic.interactive.subst_vars], tags := ["core", "rewriting"] } add_tactic_doc { name := "success_if_fail", category := doc_category.tactic, decl_names := [`tactic.interactive.success_if_fail], tags := ["core", "testing", "combinator"] } add_tactic_doc { name := "suffices", category := doc_category.tactic, decl_names := [`tactic.interactive.suffices], tags := ["core", "basic", "goal management"] } add_tactic_doc { name := "symmetry", category := doc_category.tactic, decl_names := [`tactic.interactive.symmetry], tags := ["core", "basic", "lemma application"] } add_tactic_doc { name := "trace", category := doc_category.tactic, decl_names := [`tactic.interactive.trace], tags := ["core", "debugging", "testing"] } add_tactic_doc { name := "trace_simp_set", category := doc_category.tactic, decl_names := [`tactic.interactive.trace_simp_set], tags := ["core", "debugging", "testing"] } add_tactic_doc { name := "trace_state", category := doc_category.tactic, decl_names := [`tactic.interactive.trace_state], tags := ["core", "debugging", "testing"] } add_tactic_doc { name := "transitivity", category := doc_category.tactic, decl_names := [`tactic.interactive.transitivity], tags := ["core", "lemma application"] } add_tactic_doc { name := "trivial", category := doc_category.tactic, decl_names := [`tactic.interactive.trivial], tags := ["core", "finishing"] } add_tactic_doc { name := "try", category := doc_category.tactic, decl_names := [`tactic.interactive.try], tags := ["core", "combinator"] } add_tactic_doc { name := "type_check", category := doc_category.tactic, decl_names := [`tactic.interactive.type_check], tags := ["core", "debugging", "testing"] } add_tactic_doc { name := "unfold", category := doc_category.tactic, decl_names := [`tactic.interactive.unfold], tags := ["core", "basic", "rewriting"] } add_tactic_doc { name := "unfold1", category := doc_category.tactic, decl_names := [`tactic.interactive.unfold1], tags := ["core", "rewriting"] } add_tactic_doc { name := "unfold_projs", category := doc_category.tactic, decl_names := [`tactic.interactive.unfold_projs], tags := ["core", "rewriting"] } add_tactic_doc { name := "with_cases", category := doc_category.tactic, decl_names := [`tactic.interactive.with_cases], tags := ["core", "combinator"] }
9b7366a2a36354e2a825831060cff7ab48b7f81c
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/euclidean_domain.lean
2c620dc21a53a1dfe154f00e2fa03f38505bf517
[ "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
17,980
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro -/ import data.int.basic import algebra.field.basic /-! # Euclidean domains This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise, a slightly more general version is provided which is sometimes called a transfinite Euclidean domain and differs in the fact that the degree function need not take values in `ℕ` but can take values in any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which don't satisfy the classical notion were provided independently by Hiblot and Nagata. ## Main definitions * `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`. * `gcd`: defines the greatest common divisors of two elements of a Euclidean domain. * `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that `x * a + y * b = gcd a b`. * `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as `a * b / (gcd a b)` ## Main statements * `gcd_eq_gcd_ab`: states Bézout's lemma for Euclidean domains. * `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain. * `field.to_euclidean_domain`: shows that any field is a Euclidean domain. ## Notation `≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than the degree of `q`. ## Implementation details Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute value of `j`. ## References * [Th. Motzkin, *The Euclidean algorithm*][MR32592] * [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*] [MR399081] * [M. Nagata, *On Euclid algorithm*][MR541021] ## Tags Euclidean domain, transfinite Euclidean domain, Bézout's lemma -/ universe u /-- A `euclidean_domain` is an non-trivial commutative ring with a division and a remainder, satisfying `b * (a / b) + a % b = a`. The definition of a euclidean domain usually includes a valuation function `R → ℕ`. This definition is slightly generalised to include a well founded relation `r` with the property that `r (a % b) b`, instead of a valuation. -/ @[protect_proj without mul_left_not_lt r_well_founded] class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R := (quotient : R → R → R) (quotient_zero : ∀ a, quotient a 0 = 0) (remainder : R → R → R) (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : R → R → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {R : Type u} variables [euclidean_domain R] local infix ` ≺ `:50 := euclidean_domain.r @[priority 70] -- see Note [lower instance priority] instance : has_div R := ⟨euclidean_domain.quotient⟩ @[priority 70] -- see Note [lower instance priority] instance : has_mod R := ⟨euclidean_domain.remainder⟩ theorem div_add_mod (a b : R) : b * (a / b) + a % b = a := euclidean_domain.quotient_mul_add_remainder_eq _ _ lemma mod_add_div (a b : R) : a % b + b * (a / b) = a := (add_comm _ _).trans (div_add_mod _ _) lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b := euclidean_domain.remainder_lt theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by { rw mul_comm, exact mul_left_not_lt b h } lemma mul_div_cancel_left {a : R} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : R} (b0 : b ≠ 0) : a * b / b = a := by { rw mul_comm, exact mul_div_cancel_left a b0 } @[simp] lemma mod_zero (a : R) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : R} : a % b = 0 ↔ b ∣ a := ⟨λ h, by { rw [← div_add_mod a b, h, add_zero], exact dvd_mul_right _ _ }, λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0, { simp only [b0, zero_mul] }, { rw [mul_div_cancel_left _ b0] } end⟩ @[simp] lemma mod_self (a : R) : a % a = 0 := mod_eq_zero.2 dvd_rfl lemma dvd_mod_iff {a b c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (h.mul_right _), div_add_mod] lemma lt_one (a : R) : a ≺ (1:R) → a = 0 := by { haveI := classical.dec, exact not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) } lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha) @[simp] lemma mod_one (a : R) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : R) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 := euclidean_domain.quotient_zero a @[simp, priority 900] lemma zero_div {a : R} : 0 / a = 0 := classical.by_cases (λ a0 : a = 0, a0.symm ▸ div_zero 0) (λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0) @[simp, priority 900] lemma div_self {a : R} (a0 : a ≠ 0) : a / a = 1 := by simpa only [one_mul] using mul_div_cancel 1 a0 lemma eq_div_of_mul_eq_left {a b c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b := by rw [← h, mul_div_cancel _ hb] lemma eq_div_of_mul_eq_right {a b c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a := by rw [← h, mul_div_cancel_left _ ha] theorem mul_div_assoc (x : R) {y z : R} (h : z ∣ y) : x * y / z = x * (y / z) := begin classical, by_cases hz : z = 0, { subst hz, rw [div_zero, div_zero, mul_zero] }, rcases h with ⟨p, rfl⟩, rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz] end @[simp, priority 900] -- This generalizes `int.div_one`, see note [simp-normal form] lemma div_one (p : R) : p / 1 = p := (euclidean_domain.eq_div_of_mul_eq_left (@one_ne_zero R _ _) (mul_one p)).symm lemma div_dvd_of_dvd {p q : R} (hpq : q ∣ p) : p / q ∣ p := begin by_cases hq : q = 0, { rw [hq, zero_dvd_iff] at hpq, rw hpq, exact dvd_zero _ }, use q, rw [mul_comm, ← euclidean_domain.mul_div_assoc _ hpq, mul_comm, euclidean_domain.mul_div_cancel _ hq] end section open_locale classical @[elab_as_eliminator] theorem gcd.induction {P : R → R → Prop} : ∀ a b : R, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} end section gcd variable [decidable_eq R] /-- `gcd a b` is a (non-unique) element such that `gcd a b ∣ a` `gcd a b ∣ b`, and for any element `c` such that `c ∣ a` and `c ∣ b`, then `c ∣ gcd a b` -/ def gcd : R → R → R | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a := by { rw gcd, exact if_pos rfl } @[simp] theorem gcd_zero_right (a : R) : gcd a 0 = a := by { rw gcd, split_ifs; simp only [h, zero_mod, gcd_zero_left] } theorem gcd_val (a b : R) : gcd a b = gcd (b % a) a := by { rw gcd, split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]} theorem gcd_dvd (a b : R) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by { rw gcd_zero_left, exact ⟨dvd_zero _, dvd_rfl⟩ }) (λ a b aneq ⟨IH₁, IH₂⟩, by { rw gcd_val, exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩ }) theorem gcd_dvd_left (a b : R) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : R) : gcd a b ∣ b := (gcd_dvd a b).right protected theorem gcd_eq_zero_iff {a b : R} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ h, by simpa [h] using gcd_dvd a b, by { rintro ⟨rfl, rfl⟩, exact gcd_zero_right _ }⟩ theorem dvd_gcd {a b c : R} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (λ _ _ H, by simpa only [gcd_zero_left] using H) (λ a b a0 IH ca cb, by { rw gcd_val, exact IH ((dvd_mod_iff ca).2 cb) ca }) theorem gcd_eq_left {a b : R} : gcd a b = a ↔ a ∣ b := ⟨λ h, by {rw ← h, apply gcd_dvd_right }, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : R) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : R) : gcd a a = a := gcd_eq_left.2 dvd_rfl /-- An implementation of the extended GCD algorithm. At each step we are computing a triple `(r, s, t)`, where `r` is the next value of the GCD algorithm, to compute the greatest common divisor of the input (say `x` and `y`), and `s` and `t` are the coefficients in front of `x` and `y` to obtain `r` (i.e. `r = s * x + t * y`). The function `xgcd_aux` takes in two triples, and from these recursively computes the next triple: ``` xgcd_aux (r, s, t) (r', s', t') = xgcd_aux (r' % r, s' - (r' / r) * s, t' - (r' / r) * t) (r, s, t) ``` -/ def xgcd_aux : R → R → R → R → R → R → R × R × R | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by { unfold xgcd_aux, exact if_pos rfl } theorem xgcd_aux_rec {r s t r' s' t' : R} (h : r ≠ 0) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h} /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : R) : R := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : R) : R := (xgcd x y).2 @[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 := by { unfold gcd_a, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 := by { unfold gcd_b, rw [xgcd, xgcd_zero_left] } @[simp] theorem xgcd_aux_fst (x y : R) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by { intros, rw [xgcd_zero_left, gcd_zero_left] }) (λ x y h IH s t s' t', by { simp only [xgcd_aux_rec h, if_neg h, IH], rw ← gcd_val }) theorem xgcd_aux_val (x y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta] theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) := prod.mk.eta.symm private def P (a b : R) : R × R × R → Prop | (r, s, t) := (r : R) = a * s + b * t theorem xgcd_aux_P (a b : R) {r r' : R} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by { intros, simpa only [xgcd_zero_left] }) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢, rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub, mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p, mod_eq_sub_mul_div] end /-- An explicit version of **Bézout's lemma** for Euclidean domains. -/ theorem gcd_eq_gcd_ab (a b : R) : (gcd a b : R) = a * gcd_a a b + b * gcd_b a b := by { have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]), rwa [xgcd_aux_val, xgcd_val] at this } @[priority 70] -- see Note [lower instance priority] instance (R : Type*) [e : euclidean_domain R] : is_domain R := by { haveI := classical.dec_eq R, exact { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, (or_iff_not_and_not.2 $ λ h0, h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div]), ..e }} end gcd section lcm variables [decidable_eq R] /-- `lcm a b` is a (non-unique) element such that `a ∣ lcm a b` `b ∣ lcm a b`, and for any element `c` such that `a ∣ c` and `b ∣ c`, then `lcm a b ∣ c` -/ def lcm (x y : R) : R := x * y / gcd x y theorem dvd_lcm_left (x y : R) : x ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $ by rw [mul_right_comm, mul_assoc, ← hz]⟩) theorem dvd_lcm_right (x y : R) : y ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $ by rw [← mul_assoc, mul_right_comm, ← hz]⟩) theorem lcm_dvd {x y z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z := begin rw lcm, by_cases hxy : gcd x y = 0, { rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, suffices : x * y ∣ z * gcd x y, { cases this with p hp, use p, generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs, rw [mul_left_comm, mul_div_cancel_left _ hxy, ← mul_left_inj' hxy, hp], rw [← mul_assoc], simp only [mul_right_comm] }, rw [gcd_eq_gcd_ab, mul_add], apply dvd_add, { rw mul_left_comm, exact mul_dvd_mul_left _ (hyz.mul_right _) }, { rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (hxz.mul_right _) } end @[simp] lemma lcm_dvd_iff {x y z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z := ⟨λ hz, ⟨(dvd_lcm_left _ _).trans hz, (dvd_lcm_right _ _).trans hz⟩, λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩ @[simp] lemma lcm_zero_left (x : R) : lcm 0 x = 0 := by rw [lcm, zero_mul, zero_div] @[simp] lemma lcm_zero_right (x : R) : lcm x 0 = 0 := by rw [lcm, mul_zero, zero_div] @[simp] lemma lcm_eq_zero_iff {x y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 := begin split, { intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy, apply or_of_or_of_imp_right hxy, intro hy, by_cases hgxy : gcd x y = 0, { rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 }, { rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs, rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } }, rintro (hx | hy), { rw [hx, lcm_zero_left] }, { rw [hy, lcm_zero_right] } end @[simp] lemma gcd_mul_lcm (x y : R) : gcd x y * lcm x y = x * y := begin rw lcm, by_cases h : gcd x y = 0, { rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at h hr ⊢, subst hr, rw [mul_assoc, mul_div_cancel_left _ h] end end lcm section div lemma mul_div_mul_cancel {a b c : R} (ha : a ≠ 0) (hcb : c ∣ b) : a * b / (a * c) = b / c := begin by_cases hc : c = 0, { simp [hc] }, refine eq_div_of_mul_eq_right hc (mul_left_cancel₀ ha _), rw [← mul_assoc, ← mul_div_assoc _ (mul_dvd_mul_left a hcb), mul_div_cancel_left _ (mul_ne_zero ha hc)] end end div end euclidean_domain instance int.euclidean_domain : euclidean_domain ℤ := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), quotient_zero := int.div_zero, remainder := (%), quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs], exact int.mod_lt _ b0 }, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by {rw [← mul_one a.nat_abs, int.nat_abs_mul], exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }, .. int.comm_ring, .. int.nontrivial } @[priority 100] -- see Note [lower instance priority] instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), remainder := λ a b, a - a * b / b, quotient_zero := div_zero, quotient_mul_add_remainder_eq := λ a b, by { classical, by_cases b = 0; simp [h, mul_div_cancel'] }, r := λ a b, a = 0 ∧ b ≠ 0, r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩, acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb, remainder_lt := λ a b hnb, by simp [hnb], mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb, .. ‹field K› }
9a3922822b71098e9e927087e77e9fac2a32d39f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/builtin_attr/UserAttr.lean
28b59b34724068b78659ec1cd3359c1a1dd78b3c
[ "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
20
lean
import UserAttr.Tst
c96ebaff73b68185192a487dcee912d4ffa5a342
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/dslope.lean
48b5af6941003e2635fa76c263adbcda7b20d612
[ "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
6,258
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.deriv /-! # Slope of a differentiable function Given a function `f : 𝕜 → E` from a nontrivially normed field to a normed space over this field, `dslope f a b` is defined as `slope f a b = (b - a)⁻¹ • (f b - f a)` for `a ≠ b` and as `deriv f a` for `a = b`. In this file we define `dslope` and prove some basic lemmas about its continuity and differentiability. -/ open_locale classical topology filter open function set filter variables {𝕜 E : Type*} [nontrivially_normed_field 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] /-- `dslope f a b` is defined as `slope f a b = (b - a)⁻¹ • (f b - f a)` for `a ≠ b` and `deriv f a` for `a = b`. -/ noncomputable def dslope (f : 𝕜 → E) (a : 𝕜) : 𝕜 → E := update (slope f a) a (deriv f a) @[simp] lemma dslope_same (f : 𝕜 → E) (a : 𝕜) : dslope f a a = deriv f a := update_same _ _ _ variables {f : 𝕜 → E} {a b : 𝕜} {s : set 𝕜} lemma dslope_of_ne (f : 𝕜 → E) (h : b ≠ a) : dslope f a b = slope f a b := update_noteq h _ _ lemma continuous_linear_map.dslope_comp {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] (f : E →L[𝕜] F) (g : 𝕜 → E) (a b : 𝕜) (H : a = b → differentiable_at 𝕜 g a) : dslope (f ∘ g) a b = f (dslope g a b) := begin rcases eq_or_ne b a with rfl|hne, { simp only [dslope_same], exact (f.has_fderiv_at.comp_has_deriv_at b (H rfl).has_deriv_at).deriv }, { simpa only [dslope_of_ne _ hne] using f.to_linear_map.slope_comp g a b } end lemma eq_on_dslope_slope (f : 𝕜 → E) (a : 𝕜) : eq_on (dslope f a) (slope f a) {a}ᶜ := λ b, dslope_of_ne f lemma dslope_eventually_eq_slope_of_ne (f : 𝕜 → E) (h : b ≠ a) : dslope f a =ᶠ[𝓝 b] slope f a := (eq_on_dslope_slope f a).eventually_eq_of_mem (is_open_ne.mem_nhds h) lemma dslope_eventually_eq_slope_punctured_nhds (f : 𝕜 → E) : dslope f a =ᶠ[𝓝[≠] a] slope f a := (eq_on_dslope_slope f a).eventually_eq_of_mem self_mem_nhds_within @[simp] lemma sub_smul_dslope (f : 𝕜 → E) (a b : 𝕜) : (b - a) • dslope f a b = f b - f a := by rcases eq_or_ne b a with rfl | hne; simp [dslope_of_ne, *] lemma dslope_sub_smul_of_ne (f : 𝕜 → E) (h : b ≠ a) : dslope (λ x, (x - a) • f x) a b = f b := by rw [dslope_of_ne _ h, slope_sub_smul _ h.symm] lemma eq_on_dslope_sub_smul (f : 𝕜 → E) (a : 𝕜) : eq_on (dslope (λ x, (x - a) • f x) a) f {a}ᶜ := λ b, dslope_sub_smul_of_ne f lemma dslope_sub_smul [decidable_eq 𝕜] (f : 𝕜 → E) (a : 𝕜) : dslope (λ x, (x - a) • f x) a = update f a (deriv (λ x, (x - a) • f x) a) := eq_update_iff.2 ⟨dslope_same _ _, eq_on_dslope_sub_smul f a⟩ @[simp] lemma continuous_at_dslope_same : continuous_at (dslope f a) a ↔ differentiable_at 𝕜 f a := by simp only [dslope, continuous_at_update_same, ← has_deriv_at_deriv_iff, has_deriv_at_iff_tendsto_slope] lemma continuous_within_at.of_dslope (h : continuous_within_at (dslope f a) s b) : continuous_within_at f s b := have continuous_within_at (λ x, (x - a) • dslope f a x + f a) s b, from ((continuous_within_at_id.sub continuous_within_at_const).smul h).add continuous_within_at_const, by simpa only [sub_smul_dslope, sub_add_cancel] using this lemma continuous_at.of_dslope (h : continuous_at (dslope f a) b) : continuous_at f b := (continuous_within_at_univ _ _).1 h.continuous_within_at.of_dslope lemma continuous_on.of_dslope (h : continuous_on (dslope f a) s) : continuous_on f s := λ x hx, (h x hx).of_dslope lemma continuous_within_at_dslope_of_ne (h : b ≠ a) : continuous_within_at (dslope f a) s b ↔ continuous_within_at f s b := begin refine ⟨continuous_within_at.of_dslope, λ hc, _⟩, simp only [dslope, continuous_within_at_update_of_ne h], exact ((continuous_within_at_id.sub continuous_within_at_const).inv₀ (sub_ne_zero.2 h)).smul (hc.sub continuous_within_at_const) end lemma continuous_at_dslope_of_ne (h : b ≠ a) : continuous_at (dslope f a) b ↔ continuous_at f b := by simp only [← continuous_within_at_univ, continuous_within_at_dslope_of_ne h] lemma continuous_on_dslope (h : s ∈ 𝓝 a) : continuous_on (dslope f a) s ↔ continuous_on f s ∧ differentiable_at 𝕜 f a := begin refine ⟨λ hc, ⟨hc.of_dslope, continuous_at_dslope_same.1 $ hc.continuous_at h⟩, _⟩, rintro ⟨hc, hd⟩ x hx, rcases eq_or_ne x a with rfl | hne, exacts [(continuous_at_dslope_same.2 hd).continuous_within_at, (continuous_within_at_dslope_of_ne hne).2 (hc x hx)] end lemma differentiable_within_at.of_dslope (h : differentiable_within_at 𝕜 (dslope f a) s b) : differentiable_within_at 𝕜 f s b := by simpa only [id, sub_smul_dslope f a, sub_add_cancel] using ((differentiable_within_at_id.sub_const a).smul h).add_const (f a) lemma differentiable_at.of_dslope (h : differentiable_at 𝕜 (dslope f a) b) : differentiable_at 𝕜 f b := differentiable_within_at_univ.1 h.differentiable_within_at.of_dslope lemma differentiable_on.of_dslope (h : differentiable_on 𝕜 (dslope f a) s) : differentiable_on 𝕜 f s := λ x hx, (h x hx).of_dslope lemma differentiable_within_at_dslope_of_ne (h : b ≠ a) : differentiable_within_at 𝕜 (dslope f a) s b ↔ differentiable_within_at 𝕜 f s b := begin refine ⟨differentiable_within_at.of_dslope, λ hd, _⟩, refine (((differentiable_within_at_id.sub_const a).inv (sub_ne_zero.2 h)).smul (hd.sub_const (f a))).congr_of_eventually_eq _ (dslope_of_ne _ h), refine (eq_on_dslope_slope _ _).eventually_eq_of_mem _, exact mem_nhds_within_of_mem_nhds (is_open_ne.mem_nhds h) end lemma differentiable_on_dslope_of_nmem (h : a ∉ s) : differentiable_on 𝕜 (dslope f a) s ↔ differentiable_on 𝕜 f s := forall_congr $ λ x, forall_congr $ λ hx, differentiable_within_at_dslope_of_ne $ ne_of_mem_of_not_mem hx h lemma differentiable_at_dslope_of_ne (h : b ≠ a) : differentiable_at 𝕜 (dslope f a) b ↔ differentiable_at 𝕜 f b := by simp only [← differentiable_within_at_univ, differentiable_within_at_dslope_of_ne h]
44050b48ba31e537651f808ea7e9d68c6a151e48
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/order/boolean_algebra.lean
0ae658512b673a516a0242fb204575e860a58ddb
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,857
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, Bryan Gin-ge Chen -/ import order.bounded_lattice /-! # (Generalized) Boolean algebras A Boolean algebra is a bounded distributive lattice with a complement operator. Boolean algebras generalize the (classical) logic of propositions and the lattice of subsets of a set. Generalized Boolean algebras may be less familiar, but they are essentially Boolean algebras which do not necessarily have a top element (`⊤`) (and hence not all elements may have complements). One example in mathlib is `finset α`, the type of all finite subsets of an arbitrary (not-necessarily-finite) type `α`. `generalized_boolean_algebra α` is defined to be a distributive lattice with bottom (`⊥`) admitting a *relative* complement operator, written using "set difference" notation as `x \ y` (`sdiff x y`). For convenience, the `boolean_algebra` type class is defined to extend `generalized_boolean_algebra` so that it is also bundled with a `\` operator. (A terminological point: `x \ y` is the complement of `y` relative to the interval `[⊥, x]`. We do not yet have relative complements for arbitrary intervals, as we do not even have lattice intervals.) ## Main declarations * `has_compl`: a type class for the complement operator * `generalized_boolean_algebra`: a type class for generalized Boolean algebras * `boolean_algebra.core`: a type class with the minimal assumptions for a Boolean algebras * `boolean_algebra`: the main type class for Boolean algebras; it extends both `generalized_boolean_algebra` and `boolean_algebra.core`. An instance of `boolean_algebra` can be obtained from one of `boolean_algebra.core` using `boolean_algebra.of_core`. * `Prop.boolean_algebra`: the Boolean algebra instance on `Prop` ## Implementation notes The `sup_inf_sdiff` and `inf_inf_sdiff` axioms for the relative complement operator in `generalized_boolean_algebra` are taken from [Wikipedia](https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations). [Stone's paper introducing generalized Boolean algebras][Stone1935] does not define a relative complement operator `a \ b` for all `a`, `b`. Instead, the postulates there amount to an assumption that for all `a, b : α` where `a ≤ b`, the equations `x ⊔ a = b` and `x ⊓ a = ⊥` have a solution `x`. `disjoint.sdiff_unique` proves that this `x` is in fact `b \ a`. ## Notations * `xᶜ` is notation for `compl x` * `x \ y` is notation for `sdiff x y`. ## References * <https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations> * [*Postulates for Boolean Algebras and Generalized Boolean Algebras*, M.H. Stone][Stone1935] * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] ## Tags generalized Boolean algebras, Boolean algebras, lattices, sdiff, compl -/ set_option old_structure_cmd true universes u v variables {α : Type u} {w x y z : α} /-! ### Generalized Boolean algebras Some of the lemmas in this section are from: * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] * <https://ncatlab.org/nlab/show/relative+complement> * <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf> -/ export has_sdiff (sdiff) /-- A generalized Boolean algebra is a distributive lattice with `⊥` and a relative complement operation `\` (called `sdiff`, after "set difference") satisfying `(a ⊓ b) ⊔ (a \ b) = a` and `(a ⊓ b) ⊓ (a \ b) = b`, i.e. `a \ b` is the complement of `b` in `a`. This is a generalization of Boolean algebras which applies to `finset α` for arbitrary (not-necessarily-`fintype`) `α`. -/ class generalized_boolean_algebra (α : Type u) extends distrib_lattice_bot α, has_sdiff α := (sup_inf_sdiff : ∀a b:α, (a ⊓ b) ⊔ (a \ b) = a) (inf_inf_sdiff : ∀a b:α, (a ⊓ b) ⊓ (a \ b) = ⊥) -- We might want a `is_compl_of` predicate (for relative complements) generalizing `is_compl`, -- however we'd need another type class for lattices with bot, and all the API for that. section generalized_boolean_algebra variables [generalized_boolean_algebra α] @[simp] theorem sup_inf_sdiff (x y : α) : (x ⊓ y) ⊔ (x \ y) = x := generalized_boolean_algebra.sup_inf_sdiff _ _ @[simp] theorem inf_inf_sdiff (x y : α) : (x ⊓ y) ⊓ (x \ y) = ⊥ := generalized_boolean_algebra.inf_inf_sdiff _ _ @[simp] theorem sup_sdiff_inf (x y : α) : (x \ y) ⊔ (x ⊓ y) = x := by rw [sup_comm, sup_inf_sdiff] @[simp] theorem inf_sdiff_inf (x y : α) : (x \ y) ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff] theorem disjoint_inf_sdiff : disjoint (x ⊓ y) (x \ y) := (inf_inf_sdiff x y).le -- TODO: in distributive lattices, relative complements are unique when they exist theorem sdiff_unique (s : (x ⊓ y) ⊔ z = x) (i : (x ⊓ y) ⊓ z = ⊥) : x \ y = z := begin conv_rhs at s { rw [←sup_inf_sdiff x y, sup_comm] }, rw sup_comm at s, conv_rhs at i { rw [←inf_inf_sdiff x y, inf_comm] }, rw inf_comm at i, exact (eq_of_inf_eq_sup_eq i s).symm, end theorem sdiff_symm (hy : y ≤ x) (hz : z ≤ x) (H : x \ y = z) : x \ z = y := have hyi : x ⊓ y = y := inf_eq_right.2 hy, have hzi : x ⊓ z = z := inf_eq_right.2 hz, eq_of_inf_eq_sup_eq (begin have ixy := inf_inf_sdiff x y, rw [H, hyi] at ixy, have ixz := inf_inf_sdiff x z, rwa [hzi, inf_comm, ←ixy] at ixz, end) (begin have sxz := sup_inf_sdiff x z, rw [hzi, sup_comm] at sxz, rw sxz, symmetry, have sxy := sup_inf_sdiff x y, rwa [H, hyi] at sxy, end) lemma sdiff_le : x \ y ≤ x := calc x \ y ≤ (x ⊓ y) ⊔ (x \ y) : le_sup_right ... = x : sup_inf_sdiff x y @[simp] lemma bot_sdiff : ⊥ \ x = ⊥ := le_bot_iff.1 sdiff_le lemma inf_sdiff_right : x ⊓ (x \ y) = x \ y := by rw [inf_of_le_right (@sdiff_le _ x y _)] lemma inf_sdiff_left : (x \ y) ⊓ x = x \ y := by rw [inf_comm, inf_sdiff_right] -- cf. `is_compl_top_bot` @[simp] lemma sdiff_self : x \ x = ⊥ := by rw [←inf_inf_sdiff, inf_idem, inf_of_le_right (@sdiff_le _ x x _)] @[simp] theorem sup_sdiff_self_right : x ⊔ (y \ x) = x ⊔ y := calc x ⊔ (y \ x) = (x ⊔ (x ⊓ y)) ⊔ (y \ x) : by rw sup_inf_self ... = x ⊔ ((y ⊓ x) ⊔ (y \ x)) : by ac_refl ... = x ⊔ y : by rw sup_inf_sdiff @[simp] theorem sup_sdiff_self_left : (y \ x) ⊔ x = y ⊔ x := by rw [sup_comm, sup_sdiff_self_right, sup_comm] lemma sup_sdiff_symm : x ⊔ (y \ x) = y ⊔ (x \ y) := by rw [sup_sdiff_self_right, sup_sdiff_self_right, sup_comm] lemma sup_sdiff_of_le (h : x ≤ y) : x ⊔ (y \ x) = y := by conv_rhs { rw [←sup_inf_sdiff y x, inf_eq_right.2 h] } @[simp] lemma sup_sdiff_left : x ⊔ (x \ y) = x := by { rw sup_eq_left, exact sdiff_le } lemma sup_sdiff_right : (x \ y) ⊔ x = x := by rw [sup_comm, sup_sdiff_left] @[simp] lemma sdiff_inf_sdiff : x \ y ⊓ (y \ x) = ⊥ := eq.symm $ calc ⊥ = (x ⊓ y) ⊓ (x \ y) : by rw inf_inf_sdiff ... = (x ⊓ (y ⊓ x ⊔ y \ x)) ⊓ (x \ y) : by rw sup_inf_sdiff ... = (x ⊓ (y ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_sup_left ... = (y ⊓ (x ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by ac_refl ... = (y ⊓ x ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_idem ... = (x ⊓ y ⊓ (x \ y)) ⊔ (x ⊓ (y \ x) ⊓ (x \ y)) : by rw [inf_sup_right, @inf_comm _ _ x y] ... = x ⊓ (y \ x) ⊓ (x \ y) : by rw [inf_inf_sdiff, bot_sup_eq] ... = x ⊓ (x \ y) ⊓ (y \ x) : by ac_refl ... = (x \ y) ⊓ (y \ x) : by rw inf_sdiff_right lemma disjoint_sdiff_sdiff : disjoint (x \ y) (y \ x) := sdiff_inf_sdiff.le theorem le_sup_sdiff : y ≤ x ⊔ (y \ x) := by { rw [sup_sdiff_self_right], exact le_sup_right } theorem le_sdiff_sup : y ≤ (y \ x) ⊔ x := by { rw [sup_comm], exact le_sup_sdiff } @[simp] theorem inf_sdiff_self_right : x ⊓ (y \ x) = ⊥ := calc x ⊓ (y \ x) = ((x ⊓ y) ⊔ (x \ y)) ⊓ (y \ x) : by rw sup_inf_sdiff ... = (x ⊓ y) ⊓ (y \ x) ⊔ (x \ y) ⊓ (y \ x) : by rw inf_sup_right ... = ⊥ : by rw [@inf_comm _ _ x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq] @[simp] theorem inf_sdiff_self_left : (y \ x) ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right] theorem disjoint_sdiff_self_left : disjoint (y \ x) x := inf_sdiff_self_left.le theorem disjoint_sdiff_self_right : disjoint x (y \ x) := inf_sdiff_self_right.le lemma disjoint.disjoint_sdiff_left (h : disjoint x y) : disjoint (x \ z) y := h.mono_left sdiff_le lemma disjoint.disjoint_sdiff_right (h : disjoint x y) : disjoint x (y \ z) := h.mono_right sdiff_le /- TODO: if we had a typeclass for distributive lattices with `⊥`, we could make an alternative constructor for `generalized_boolean_algebra` using `disjoint x (y \ x)` and `x ⊔ (y \ x) = y` as axioms. -/ theorem disjoint.sdiff_eq_of_sup_eq (hi : disjoint x z) (hs : x ⊔ z = y) : y \ x = z := have h : y ⊓ x = x := inf_eq_right.2 $ le_sup_left.trans hs.le, sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot]) lemma disjoint.sup_sdiff_cancel_left (h : disjoint x y) : (x ⊔ y) \ x = y := h.sdiff_eq_of_sup_eq rfl lemma disjoint.sup_sdiff_cancel_right (h : disjoint x y) : (x ⊔ y) \ y = x := h.symm.sdiff_eq_of_sup_eq sup_comm protected theorem disjoint.sdiff_unique (hd : disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) : y \ x = z := sdiff_unique (begin rw ←inf_eq_right at hs, rwa [sup_inf_right, inf_sup_right, @sup_comm _ _ x, inf_sup_self, inf_comm, @sup_comm _ _ z, hs, sup_eq_left], end) (by rw [inf_assoc, hd.eq_bot, inf_bot_eq]) -- cf. `is_compl.disjoint_left_iff` and `is_compl.disjoint_right_iff` lemma disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : disjoint z (y \ x) ↔ z ≤ x := ⟨λ H, le_of_inf_le_sup_le (le_trans H bot_le) (begin rw sup_sdiff_of_le hx, refine le_trans (sup_le_sup_left sdiff_le z) _, rw sup_eq_right.2 hz, end), λ H, disjoint_sdiff_self_right.mono_left H⟩ -- cf. `is_compl.le_left_iff` and `is_compl.le_right_iff` lemma le_iff_disjoint_sdiff (hz : z ≤ y) (hx : x ≤ y) : z ≤ x ↔ disjoint z (y \ x) := (disjoint_sdiff_iff_le hz hx).symm -- cf. `is_compl.inf_left_eq_bot_iff` and `is_compl.inf_right_eq_bot_iff` lemma inf_sdiff_eq_bot_iff (hz : z ≤ y) (hx : x ≤ y) : z ⊓ (y \ x) = ⊥ ↔ z ≤ x := by { rw ←disjoint_iff, exact disjoint_sdiff_iff_le hz hx } -- cf. `is_compl.left_le_iff` and `is_compl.right_le_iff` lemma le_iff_eq_sup_sdiff (hz : z ≤ y) (hx : x ≤ y) : x ≤ z ↔ y = z ⊔ (y \ x) := ⟨λ H, begin apply le_antisymm, { conv_lhs { rw ←sup_inf_sdiff y x, }, apply sup_le_sup_right, rwa inf_eq_right.2 hx, }, { apply le_trans, { apply sup_le_sup_right hz, }, { rw sup_sdiff_left, } } end, λ H, begin conv_lhs at H { rw ←sup_sdiff_of_le hx, }, refine le_of_inf_le_sup_le _ H.le, rw inf_sdiff_self_right, exact bot_le, end⟩ -- cf. `set.union_diff_cancel'` lemma sup_sdiff_cancel' (hx : x ≤ z) (hz : z ≤ y) : z ⊔ (y \ x) = y := ((le_iff_eq_sup_sdiff hz (hx.trans hz)).1 hx).symm -- cf. `is_compl.sup_inf` lemma sdiff_sup : y \ (x ⊔ z) = (y \ x) ⊓ (y \ z) := sdiff_unique (calc y ⊓ (x ⊔ z) ⊔ y \ x ⊓ (y \ z) = (y ⊓ (x ⊔ z) ⊔ y \ x) ⊓ (y ⊓ (x ⊔ z) ⊔ (y \ z)) : by rw sup_inf_left ... = (y ⊓ x ⊔ y ⊓ z ⊔ y \ x) ⊓ (y ⊓ x ⊔ y ⊓ z ⊔ (y \ z)) : by rw @inf_sup_left _ _ y ... = (y ⊓ z ⊔ (y ⊓ x ⊔ y \ x)) ⊓ (y ⊓ x ⊔ (y ⊓ z ⊔ (y \ z))) : by ac_refl ... = (y ⊓ z ⊔ y) ⊓ (y ⊓ x ⊔ y) : by rw [sup_inf_sdiff, sup_inf_sdiff] ... = (y ⊔ y ⊓ z) ⊓ (y ⊔ y ⊓ x) : by ac_refl ... = y : by rw [sup_inf_self, sup_inf_self, inf_idem]) (calc y ⊓ (x ⊔ z) ⊓ ((y \ x) ⊓ (y \ z)) = (y ⊓ x ⊔ y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z)) : by rw inf_sup_left ... = ((y ⊓ x) ⊓ ((y \ x) ⊓ (y \ z))) ⊔ ((y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z))) : by rw inf_sup_right ... = ((y ⊓ x) ⊓ (y \ x) ⊓ (y \ z)) ⊔ ((y \ x) ⊓ ((y \ z) ⊓ (y ⊓ z))) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, bot_inf_eq, bot_sup_eq, @inf_comm _ _ (y \ z), inf_inf_sdiff, inf_bot_eq]) -- cf. `is_compl.inf_sup` lemma sdiff_inf : y \ (x ⊓ z) = y \ x ⊔ y \ z := sdiff_unique (calc y ⊓ (x ⊓ z) ⊔ (y \ x ⊔ y \ z) = (z ⊓ (y ⊓ x)) ⊔ (y \ x ⊔ y \ z) : by ac_refl ... = (z ⊔ (y \ x ⊔ y \ z)) ⊓ ((y ⊓ x) ⊔ (y \ x ⊔ y \ z)) : by rw sup_inf_right ... = (y \ x ⊔ (y \ z ⊔ z)) ⊓ (y ⊓ x ⊔ (y \ x ⊔ y \ z)) : by ac_refl ... = (y ⊔ z) ⊓ ((y ⊓ x) ⊔ (y \ x ⊔ y \ z)) : by rw [sup_sdiff_self_left, ←sup_assoc, sup_sdiff_right] ... = (y ⊔ z) ⊓ y : by rw [←sup_assoc, sup_inf_sdiff, sup_sdiff_left] ... = y : by rw [inf_comm, inf_sup_self]) (calc y ⊓ (x ⊓ z) ⊓ ((y \ x) ⊔ (y \ z)) = (y ⊓ (x ⊓ z) ⊓ (y \ x)) ⊔ (y ⊓ (x ⊓ z) ⊓ (y \ z)) : by rw inf_sup_left ... = z ⊓ (y ⊓ x ⊓ (y \ x)) ⊔ z ⊓ (y ⊓ x) ⊓ (y \ z) : by ac_refl ... = z ⊓ (y ⊓ x) ⊓ (y \ z) : by rw [inf_inf_sdiff, inf_bot_eq, bot_sup_eq] ... = x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) @[simp] lemma sdiff_inf_self_right : y \ (x ⊓ y) = y \ x := by rw [sdiff_inf, sdiff_self, sup_bot_eq] @[simp] lemma sdiff_inf_self_left : y \ (y ⊓ x) = y \ x := by rw [inf_comm, sdiff_inf_self_right] lemma sdiff_eq_sdiff_iff_inf_eq_inf : y \ x = y \ z ↔ y ⊓ x = y ⊓ z := ⟨λ h, eq_of_inf_eq_sup_eq (by rw [inf_inf_sdiff, h, inf_inf_sdiff]) (by rw [sup_inf_sdiff, h, sup_inf_sdiff]), λ h, by rw [←sdiff_inf_self_right, ←@sdiff_inf_self_right _ z y, inf_comm, h, inf_comm]⟩ theorem disjoint.sdiff_eq_left (h : disjoint x y) : x \ y = x := by conv_rhs { rw [←sup_inf_sdiff x y, h.eq_bot, bot_sup_eq] } theorem disjoint.sdiff_eq_right (h : disjoint x y) : y \ x = y := h.symm.sdiff_eq_left -- cf. `is_compl_bot_top` @[simp] theorem sdiff_bot : x \ ⊥ = x := disjoint_bot_right.sdiff_eq_left theorem sdiff_eq_self_iff_disjoint : x \ y = x ↔ disjoint y x := calc x \ y = x ↔ x \ y = x \ ⊥ : by rw sdiff_bot ... ↔ x ⊓ y = x ⊓ ⊥ : sdiff_eq_sdiff_iff_inf_eq_inf ... ↔ disjoint y x : by rw [inf_bot_eq, inf_comm, disjoint_iff] theorem sdiff_eq_self_iff_disjoint' : x \ y = x ↔ disjoint x y := by rw [sdiff_eq_self_iff_disjoint, disjoint.comm] lemma sdiff_lt (hx : y ≤ x) (hy : y ≠ ⊥) : x \ y < x := begin refine sdiff_le.lt_of_ne (λ h, hy _), rw [sdiff_eq_self_iff_disjoint', disjoint_iff] at h, rw [←h, inf_eq_right.mpr hx], end -- cf. `is_compl.antitone` lemma sdiff_le_sdiff_self (h : z ≤ x) : w \ x ≤ w \ z := le_of_inf_le_sup_le (calc (w \ x) ⊓ (w ⊓ z) ≤ (w \ x) ⊓ (w ⊓ x) : inf_le_inf le_rfl (inf_le_inf le_rfl h) ... = ⊥ : by rw [inf_comm, inf_inf_sdiff] ... ≤ (w \ z) ⊓ (w ⊓ z) : bot_le) (calc w \ x ⊔ (w ⊓ z) ≤ w \ x ⊔ (w ⊓ x) : sup_le_sup le_rfl (inf_le_inf le_rfl h) ... ≤ w : by rw [sup_comm, sup_inf_sdiff] ... = (w \ z) ⊔ (w ⊓ z) : by rw [sup_comm, sup_inf_sdiff]) lemma sdiff_le_iff : y \ x ≤ z ↔ y ≤ x ⊔ z := ⟨λ h, le_of_inf_le_sup_le (le_of_eq (calc y ⊓ (y \ x) = y \ x : inf_sdiff_right ... = (x ⊓ (y \ x)) ⊔ (z ⊓ (y \ x)) : by rw [inf_eq_right.2 h, inf_sdiff_self_right, bot_sup_eq] ... = (x ⊔ z) ⊓ (y \ x) : inf_sup_right.symm)) (calc y ⊔ y \ x = y : sup_sdiff_left ... ≤ y ⊔ (x ⊔ z) : le_sup_left ... = ((y \ x) ⊔ x) ⊔ z : by rw [←sup_assoc, ←@sup_sdiff_self_left _ x y] ... = x ⊔ z ⊔ y \ x : by ac_refl), λ h, le_of_inf_le_sup_le (calc y \ x ⊓ x = ⊥ : inf_sdiff_self_left ... ≤ z ⊓ x : bot_le) (calc y \ x ⊔ x = y ⊔ x : sup_sdiff_self_left ... ≤ (x ⊔ z) ⊔ x : sup_le_sup_right h x ... ≤ z ⊔ x : by rw [sup_assoc, sup_comm, sup_assoc, sup_idem])⟩ lemma sdiff_eq_bot_iff : y \ x = ⊥ ↔ y ≤ x := by rw [←le_bot_iff, sdiff_le_iff, sup_bot_eq] lemma sdiff_le_comm : x \ y ≤ z ↔ x \ z ≤ y := by rw [sdiff_le_iff, sup_comm, sdiff_le_iff] lemma sdiff_le_self_sdiff (h : w ≤ y) : w \ x ≤ y \ x := le_of_inf_le_sup_le (calc (w \ x) ⊓ (w ⊓ x) = ⊥ : by rw [inf_comm, inf_inf_sdiff] ... ≤ (y \ x) ⊓ (w ⊓ x) : bot_le) (calc w \ x ⊔ (w ⊓ x) = w : by rw [sup_comm, sup_inf_sdiff] ... ≤ (y ⊓ (y \ x)) ⊔ w : le_sup_right ... = (y ⊓ (y \ x)) ⊔ (y ⊓ w) : by rw inf_eq_right.2 h ... = y ⊓ ((y \ x) ⊔ w) : by rw inf_sup_left ... = ((y \ x) ⊔ (y ⊓ x)) ⊓ ((y \ x) ⊔ w) : by rw [@sup_comm _ _ (y \ x) (y ⊓ x), sup_inf_sdiff] ... = (y \ x) ⊔ ((y ⊓ x) ⊓ w) : by rw ←sup_inf_left ... = (y \ x) ⊔ ((w ⊓ y) ⊓ x) : by ac_refl ... = (y \ x) ⊔ (w ⊓ x) : by rw inf_eq_left.2 h) theorem sdiff_le_sdiff (h₁ : w ≤ y) (h₂ : z ≤ x) : w \ x ≤ y \ z := calc w \ x ≤ w \ z : sdiff_le_sdiff_self h₂ ... ≤ y \ z : sdiff_le_self_sdiff h₁ lemma sup_inf_inf_sdiff : (x ⊓ y) ⊓ z ⊔ (y \ z) = (x ⊓ y) ⊔ (y \ z) := calc (x ⊓ y) ⊓ z ⊔ (y \ z) = x ⊓ (y ⊓ z) ⊔ (y \ z) : by rw inf_assoc ... = (x ⊔ (y \ z)) ⊓ y : by rw [sup_inf_right, sup_inf_sdiff] ... = (x ⊓ y) ⊔ (y \ z) : by rw [inf_sup_right, inf_sdiff_left] @[simp] lemma inf_sdiff_sup_left : (x \ z) ⊓ (x ⊔ y) = x \ z := by rw [inf_sup_left, inf_sdiff_left, sup_inf_self] @[simp] lemma inf_sdiff_sup_right : (x \ z) ⊓ (y ⊔ x) = x \ z := by rw [sup_comm, inf_sdiff_sup_left] lemma sdiff_sdiff_right : x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) := begin rw [sup_comm, inf_comm, ←inf_assoc, sup_inf_inf_sdiff], apply sdiff_unique, { calc x ⊓ (y \ z) ⊔ (z ⊓ x ⊔ x \ y) = (x ⊔ (z ⊓ x ⊔ x \ y)) ⊓ (y \ z ⊔ (z ⊓ x ⊔ x \ y)) : by rw sup_inf_right ... = (x ⊔ x ⊓ z ⊔ x \ y) ⊓ (y \ z ⊔ (x ⊓ z ⊔ x \ y)) : by ac_refl ... = x ⊓ (y \ z ⊔ x ⊓ z ⊔ x \ y) : by rw [sup_inf_self, sup_sdiff_left, ←sup_assoc] ... = x ⊓ (y \ z ⊓ (z ⊔ y) ⊔ x ⊓ (z ⊔ y) ⊔ x \ y) : by rw [sup_inf_left, sup_sdiff_self_left, inf_sup_right, @sup_comm _ _ y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ x ⊓ y) ⊔ x \ y) : by rw [inf_sdiff_sup_right, @inf_sup_left _ _ x z y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ (x ⊓ y ⊔ x \ y))) : by ac_refl ... = x ⊓ (y \ z ⊔ (x ⊔ x ⊓ z)) : by rw [sup_inf_sdiff, @sup_comm _ _ (x ⊓ z)] ... = x : by rw [sup_inf_self, sup_comm, inf_sup_self] }, { calc x ⊓ (y \ z) ⊓ (z ⊓ x ⊔ x \ y) = x ⊓ (y \ z) ⊓ (z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by rw inf_sup_left ... = x ⊓ (y \ z ⊓ z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by ac_refl ... = x ⊓ (y \ z) ⊓ (x \ y) : by rw [inf_sdiff_self_left, bot_inf_eq, inf_bot_eq, bot_sup_eq] ... = x ⊓ (y \ z ⊓ y) ⊓ (x \ y) : by conv_lhs { rw ←inf_sdiff_left } ... = x ⊓ (y \ z ⊓ (y ⊓ (x \ y))) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_right' : x \ (y \ z) = (x \ y) ⊔ (x ⊓ z) := calc x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) : sdiff_sdiff_right ... = z ⊓ x ⊓ y ⊔ (x \ y) : by ac_refl ... = (x \ y) ⊔ (x ⊓ z) : by rw [sup_inf_inf_sdiff, sup_comm, inf_comm] @[simp] lemma sdiff_sdiff_right_self : x \ (x \ y) = x ⊓ y := by rw [sdiff_sdiff_right, inf_idem, sdiff_self, bot_sup_eq] lemma sdiff_sdiff_eq_self (h : y ≤ x) : x \ (x \ y) = y := by rw [sdiff_sdiff_right_self, inf_of_le_right h] lemma sdiff_sdiff_left : (x \ y) \ z = x \ (y ⊔ z) := begin rw sdiff_sup, apply sdiff_unique, { rw [←inf_sup_left, sup_sdiff_self_right, inf_sdiff_sup_right] }, { rw [inf_assoc, @inf_comm _ _ z, inf_assoc, inf_sdiff_self_left, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_left' : (x \ y) \ z = (x \ y) ⊓ (x \ z) := by rw [sdiff_sdiff_left, sdiff_sup] lemma sdiff_sdiff_comm : (x \ y) \ z = (x \ z) \ y := by rw [sdiff_sdiff_left, sup_comm, sdiff_sdiff_left] @[simp] lemma sdiff_idem : x \ y \ y = x \ y := by rw [sdiff_sdiff_left, sup_idem] @[simp] lemma sdiff_sdiff_self : x \ y \ x = ⊥ := by rw [sdiff_sdiff_comm, sdiff_self, bot_sdiff] lemma sdiff_sdiff_sup_sdiff : z \ (x \ y ⊔ y \ x) = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) := calc z \ (x \ y ⊔ y \ x) = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sup, sdiff_sdiff_right, sdiff_sdiff_right] ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sup_inf_left, sup_comm, sup_inf_sdiff] ... = z ⊓ (z \ x ⊔ y) ⊓ (z ⊓ (z \ y ⊔ x)) : by rw [sup_inf_left, @sup_comm _ _ (z \ y), sup_inf_sdiff] ... = z ⊓ z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by ac_refl ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by rw inf_idem lemma sdiff_sdiff_sup_sdiff' : z \ (x \ y ⊔ y \ x) = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) := calc z \ (x \ y ⊔ y \ x) = z \ (x \ y) ⊓ (z \ (y \ x)) : sdiff_sup ... = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sdiff_right, sdiff_sdiff_right] ... = (z \ x ⊔ z ⊓ y ⊓ x) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by ac_refl ... = (z \ x) ⊓ (z \ y) ⊔ z ⊓ y ⊓ x : sup_inf_right.symm ... = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) : by ac_refl lemma sup_sdiff : (x ⊔ y) \ z = (x \ z) ⊔ (y \ z) := sdiff_unique (calc (x ⊔ y) ⊓ z ⊔ (x \ z ⊔ y \ z) = (x ⊓ z ⊔ y ⊓ z) ⊔ (x \ z ⊔ y \ z) : by rw inf_sup_right ... = x ⊓ z ⊔ x \ z ⊔ y \ z ⊔ y ⊓ z : by ac_refl ... = x ⊔ (y ⊓ z ⊔ y \ z) : by rw [sup_inf_sdiff, sup_assoc, @sup_comm _ _ (y \ z)] ... = x ⊔ y : by rw sup_inf_sdiff) (calc (x ⊔ y) ⊓ z ⊓ (x \ z ⊔ y \ z) = (x ⊓ z ⊔ y ⊓ z) ⊓ (x \ z ⊔ y \ z) : by rw inf_sup_right ... = (x ⊓ z ⊔ y ⊓ z) ⊓ (x \ z) ⊔ ((x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z)) : by rw [@inf_sup_left _ _ (x ⊓ z ⊔ y ⊓ z)] ... = (y ⊓ z ⊓ (x \ z)) ⊔ ((x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z)) : by rw [inf_sup_right, inf_inf_sdiff, bot_sup_eq] ... = (x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z) : by rw [inf_assoc, inf_sdiff_self_right, inf_bot_eq, bot_sup_eq] ... = x ⊓ z ⊓ (y \ z) : by rw [inf_sup_right, inf_inf_sdiff, sup_bot_eq] ... = ⊥ : by rw [inf_assoc, inf_sdiff_self_right, inf_bot_eq]) lemma sup_sdiff_right_self : (x ⊔ y) \ y = x \ y := by rw [sup_sdiff, sdiff_self, sup_bot_eq] lemma sup_sdiff_left_self : (x ⊔ y) \ x = y \ x := by rw [sup_comm, sup_sdiff_right_self] lemma inf_sdiff : (x ⊓ y) \ z = (x \ z) ⊓ (y \ z) := sdiff_unique (calc (x ⊓ y) ⊓ z ⊔ ((x \ z) ⊓ (y \ z)) = ((x ⊓ y) ⊓ z ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊓ z ⊔ (y \ z)) : by rw [sup_inf_left] ... = (x ⊓ y ⊓ (z ⊔ x) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by rw [sup_inf_right, sup_sdiff_self_right, inf_sup_right, inf_sdiff_sup_right] ... = (y ⊓ (x ⊓ (x ⊔ z)) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by ac_refl ... = ((y ⊓ x) ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊔ (y \ z)) : by rw [inf_sup_self, sup_inf_inf_sdiff] ... = (x ⊓ y) ⊔ ((x \ z) ⊓ (y \ z)) : by rw [@inf_comm _ _ y, sup_inf_left] ... = x ⊓ y : sup_eq_left.2 (inf_le_inf sdiff_le sdiff_le)) (calc (x ⊓ y) ⊓ z ⊓ ((x \ z) ⊓ (y \ z)) = x ⊓ y ⊓ (z ⊓ (x \ z)) ⊓ (y \ z) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, bot_inf_eq]) lemma inf_sdiff_assoc : (x ⊓ y) \ z = x ⊓ (y \ z) := sdiff_unique (calc x ⊓ y ⊓ z ⊔ x ⊓ (y \ z) = x ⊓ (y ⊓ z) ⊔ x ⊓ (y \ z) : by rw inf_assoc ... = x ⊓ ((y ⊓ z) ⊔ y \ z) : inf_sup_left.symm ... = x ⊓ y : by rw sup_inf_sdiff) (calc x ⊓ y ⊓ z ⊓ (x ⊓ (y \ z)) = x ⊓ x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) lemma sup_eq_sdiff_sup_sdiff_sup_inf : x ⊔ y = (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) := eq.symm $ calc (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) = ((x \ y) ⊔ (y \ x) ⊔ x) ⊓ ((x \ y) ⊔ (y \ x) ⊔ y) : by rw sup_inf_left ... = ((x \ y) ⊔ x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ ((y \ x) ⊔ y)) : by ac_refl ... = (x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ y) : by rw [sup_sdiff_right, sup_sdiff_right] ... = x ⊔ y : by rw [sup_sdiff_self_right, sup_sdiff_self_left, inf_idem] instance pi.generalized_boolean_algebra {α : Type u} {β : Type v} [generalized_boolean_algebra β] : generalized_boolean_algebra (α → β) := by pi_instance end generalized_boolean_algebra /-! ### Boolean algebras -/ /-- Set / lattice complement -/ @[notation_class] class has_compl (α : Type*) := (compl : α → α) export has_compl (compl) postfix `ᶜ`:(max+1) := compl /-- This class contains the core axioms of a Boolean algebra. The `boolean_algebra` class extends both this class and `generalized_boolean_algebra`, see Note [forgetful inheritance]. -/ class boolean_algebra.core (α : Type u) extends bounded_distrib_lattice α, has_compl α := (inf_compl_le_bot : ∀x:α, x ⊓ xᶜ ≤ ⊥) (top_le_sup_compl : ∀x:α, ⊤ ≤ x ⊔ xᶜ) section boolean_algebra_core variables [boolean_algebra.core α] @[simp] theorem inf_compl_eq_bot : x ⊓ xᶜ = ⊥ := bot_unique $ boolean_algebra.core.inf_compl_le_bot x @[simp] theorem compl_inf_eq_bot : xᶜ ⊓ x = ⊥ := eq.trans inf_comm inf_compl_eq_bot @[simp] theorem sup_compl_eq_top : x ⊔ xᶜ = ⊤ := top_unique $ boolean_algebra.core.top_le_sup_compl x @[simp] theorem compl_sup_eq_top : xᶜ ⊔ x = ⊤ := eq.trans sup_comm sup_compl_eq_top theorem is_compl_compl : is_compl x xᶜ := is_compl.of_eq inf_compl_eq_bot sup_compl_eq_top theorem is_compl.eq_compl (h : is_compl x y) : x = yᶜ := h.left_unique is_compl_compl.symm theorem is_compl.compl_eq (h : is_compl x y) : xᶜ = y := (h.right_unique is_compl_compl).symm theorem eq_compl_iff_is_compl : x = yᶜ ↔ is_compl x y := ⟨λ h, by { rw h, exact is_compl_compl.symm }, is_compl.eq_compl⟩ theorem compl_eq_iff_is_compl : xᶜ = y ↔ is_compl x y := ⟨λ h, by { rw ←h, exact is_compl_compl }, is_compl.compl_eq⟩ theorem disjoint_compl_right : disjoint x xᶜ := is_compl_compl.disjoint theorem disjoint_compl_left : disjoint xᶜ x := disjoint_compl_right.symm theorem compl_unique (i : x ⊓ y = ⊥) (s : x ⊔ y = ⊤) : xᶜ = y := (is_compl.of_eq i s).compl_eq @[simp] theorem compl_top : ⊤ᶜ = (⊥:α) := is_compl_top_bot.compl_eq @[simp] theorem compl_bot : ⊥ᶜ = (⊤:α) := is_compl_bot_top.compl_eq @[simp] theorem compl_compl (x : α) : xᶜᶜ = x := is_compl_compl.symm.compl_eq @[simp] theorem compl_involutive : function.involutive (compl : α → α) := compl_compl theorem compl_bijective : function.bijective (compl : α → α) := compl_involutive.bijective theorem compl_injective : function.injective (compl : α → α) := compl_involutive.injective @[simp] theorem compl_inj_iff : xᶜ = yᶜ ↔ x = y := compl_injective.eq_iff theorem is_compl.compl_eq_iff (h : is_compl x y) : zᶜ = y ↔ z = x := h.compl_eq ▸ compl_inj_iff @[simp] theorem compl_eq_top : xᶜ = ⊤ ↔ x = ⊥ := is_compl_bot_top.compl_eq_iff @[simp] theorem compl_eq_bot : xᶜ = ⊥ ↔ x = ⊤ := is_compl_top_bot.compl_eq_iff @[simp] theorem compl_inf : (x ⊓ y)ᶜ = xᶜ ⊔ yᶜ := (is_compl_compl.inf_sup is_compl_compl).compl_eq @[simp] theorem compl_sup : (x ⊔ y)ᶜ = xᶜ ⊓ yᶜ := (is_compl_compl.sup_inf is_compl_compl).compl_eq theorem compl_le_compl (h : y ≤ x) : xᶜ ≤ yᶜ := is_compl_compl.antitone is_compl_compl h @[simp] theorem compl_le_compl_iff_le : yᶜ ≤ xᶜ ↔ x ≤ y := ⟨assume h, by have h := compl_le_compl h; simp at h; assumption, compl_le_compl⟩ theorem le_compl_of_le_compl (h : y ≤ xᶜ) : x ≤ yᶜ := by simpa only [compl_compl] using compl_le_compl h theorem compl_le_of_compl_le (h : yᶜ ≤ x) : xᶜ ≤ y := by simpa only [compl_compl] using compl_le_compl h theorem le_compl_iff_le_compl : y ≤ xᶜ ↔ x ≤ yᶜ := ⟨le_compl_of_le_compl, le_compl_of_le_compl⟩ theorem compl_le_iff_compl_le : xᶜ ≤ y ↔ yᶜ ≤ x := ⟨compl_le_of_compl_le, compl_le_of_compl_le⟩ namespace boolean_algebra @[priority 100] instance : is_complemented α := ⟨λ x, ⟨xᶜ, is_compl_compl⟩⟩ end boolean_algebra end boolean_algebra_core /-- A Boolean algebra is a bounded distributive lattice with a complement operator `ᶜ` such that `x ⊓ xᶜ = ⊥` and `x ⊔ xᶜ = ⊤`. For convenience, it must also provide a set difference operation `\` satisfying `x \ y = x ⊓ yᶜ`. This is a generalization of (classical) logic of propositions, or the powerset lattice. -/ -- Lean complains about metavariables in the type if the universe is not specified class boolean_algebra (α : Type u) extends generalized_boolean_algebra α, boolean_algebra.core α := (sdiff_eq : ∀x y:α, x \ y = x ⊓ yᶜ) -- TODO: is there a way to automatically fill in the proofs of sup_inf_sdiff and inf_inf_sdiff given -- everything in `boolean_algebra.core` and `sdiff_eq`? The following doesn't work: -- (sup_inf_sdiff := λ a b, by rw [sdiff_eq, ←inf_sup_left, sup_compl_eq_top, inf_top_eq]) /-- Create a `boolean_algebra` instance from a `boolean_algebra.core` instance, defining `x \ y` to be `x ⊓ yᶜ`. For some types, it may be more convenient to create the `boolean_algebra` instance by hand in order to have a simpler `sdiff` operation. -/ def boolean_algebra.of_core (B : boolean_algebra.core α) : boolean_algebra α := { sdiff := λ x y, x ⊓ yᶜ, sdiff_eq := λ _ _, rfl, sup_inf_sdiff := λ a b, by rw [←inf_sup_left, sup_compl_eq_top, inf_top_eq], inf_inf_sdiff := λ a b, by rw [inf_left_right_swap, @inf_assoc _ _ a, compl_inf_eq_bot, inf_bot_eq, bot_inf_eq], ..B } section boolean_algebra variables [boolean_algebra α] theorem sdiff_eq : x \ y = x ⊓ yᶜ := boolean_algebra.sdiff_eq x y theorem sdiff_compl : x \ yᶜ = x ⊓ y := by rw [sdiff_eq, compl_compl] theorem top_sdiff : ⊤ \ x = xᶜ := by rw [sdiff_eq, top_inf_eq] @[simp] theorem sdiff_top : x \ ⊤ = ⊥ := by rw [sdiff_eq, compl_top, inf_bot_eq] @[simp] lemma sup_inf_inf_compl : (x ⊓ y) ⊔ (x ⊓ yᶜ) = x := by rw [← sdiff_eq, sup_inf_sdiff _ _] end boolean_algebra instance Prop.boolean_algebra : boolean_algebra Prop := boolean_algebra.of_core { compl := not, inf_compl_le_bot := λ p ⟨Hp, Hpc⟩, Hpc Hp, top_le_sup_compl := λ p H, classical.em p, .. Prop.bounded_distrib_lattice } instance pi.boolean_algebra {ι : Type u} {α : ι → Type v} [∀ i, boolean_algebra (α i)] : boolean_algebra (Π i, α i) := by refine_struct { sdiff := λ x y i, x i \ y i, compl := λ x i, (x i)ᶜ, .. pi.bounded_lattice }; tactic.pi_instance_derive_field
fc40011faaf6b0e174ef4c50818927f995965af0
49bd2218ae088932d847f9030c8dbff1c5607bb7
/src/linear_algebra/basic.lean
7012f87fff682e0e221a73fd5ff2841d60753a15
[ "Apache-2.0" ]
permissive
FredericLeRoux/mathlib
e8f696421dd3e4edc8c7edb3369421c8463d7bac
3645bf8fb426757e0a20af110d1fdded281d286e
refs/heads/master
1,607,062,870,732
1,578,513,186,000
1,578,513,186,000
231,653,181
0
0
Apache-2.0
1,578,080,327,000
1,578,080,326,000
null
UTF-8
Lean
false
false
72,869
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard -/ import algebra.pi_instances data.finsupp data.equiv.algebra order.order_iso /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. If `p` and `q` are submodules of a module, `p ≤ q` means that `p ⊆ q`. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module.lean`. ## Main definitions * Many constructors for linear maps, including `pair` and `copair` * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * If `p` is a submodule of `M`, `submodule.quotient p` is the quotient of `M` with respect to `p`: that is, elements of `M` are identified if their difference is in `p`. This is itself a module. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * `linear_equiv M M₂`, the type of linear equivalences between `M` and `M₂`, is a structure that extends `linear_map` and `equiv`. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first and second isomorphism laws for modules are proved as `quot_ker_equiv_range` and `sup_quotient_equiv_quotient_inf`. ## Notations * We continue to use the notation `M →ₗ[R] M₂` for the type of linear maps from `M` to `M₂` over the ring `R`. * We introduce the notations `M ≃ₗ M₂` and `M ≃ₗ[R] M₂` for `linear_equiv M M₂`. In the first, the ring `R` is implicit. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`pair`, `copair`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function lattice reserve infix ` ≃ₗ `:25 universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} namespace finset lemma smul_sum {α : Type u} {M : Type v} {R : Type w} [ring R] [add_comm_group M] [module R M] {s : finset α} {a : R} {f : α → M} : a • (s.sum f) = s.sum (λc, a • f c) := (s.sum_hom ((•) a)).symm lemma smul_sum' {α : Type u} {M : Type v} {R : Type w} [ring R] [add_comm_group M] [module R M] {s : finset α} {f : α → R} {x : M} : (s.sum f) • x = s.sum (λa, (f a) • x) := begin -- TODO : where should I put this instance? haveI : is_add_monoid_hom (λ (r : R), r • x) := { map_add := λ a b, add_smul _ _ _, map_zero := zero_smul _ _ }, exact (s.sum_hom (λ (r : R), r • x)).symm end end finset namespace finsupp lemma smul_sum {α : Type u} {β : Type v} {R : Type w} {M : Type y} [has_zero β] [ring R] [add_comm_group M] [module R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp section open_locale classical /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type u} [fintype ι] {R : Type v} [semiring R] (x : ι → R) : x = finset.sum finset.univ (λi:ι, x i • (λj, if i = j then 1 else 0)) := begin ext k, rw pi.finset_sum_apply, have : finset.sum finset.univ (λ (x_1 : ι), x x_1 * ite (k = x_1) 1 0) = x k, by { have := finset.sum_mul_boole finset.univ x k, rwa if_pos (finset.mem_univ _) at this }, rw ← this, apply finset.sum_congr rfl (λl hl, _), simp only [smul_eq_mul, mul_ite, pi.smul_apply], conv_lhs { rw eq_comm } end end namespace linear_map section variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables [module R M] [module R M₂] [module R M₃] [module R M₄] variables (f g : M →ₗ[R] M₂) include R @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl theorem comp_assoc (g : M₂ →ₗ[R] M₃) (h : M₃ →ₗ[R] M₄) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h : ∀c, f c ∈ p) : M₂ →ₗ[R] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R M) (f : M₂ →ₗ[R] M) {h} (x : M₂) : (cod_restrict p f h x : M) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) (g : M₃ →ₗ[R] M) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- If a function `g` is a left and right inverse of a linear map `f`, then `g` is linear itself. -/ def inverse (g : M₂ → M) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : M₂ →ₗ[R] M := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ /-- The constant 0 map is linear. -/ instance : has_zero (M →ₗ[R] M₂) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₗ[R] M₂) x = 0 := rfl /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₗ[R] M₂) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₗ[R] M₂) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : M) : (f + g) x = f x + g x := rfl /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp instance linear_map.is_add_group_hom : is_add_group_hom f := { map_add := f.add } instance linear_map_apply_is_add_group_hom (a : M) : is_add_group_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a } lemma sum_apply (t : finset ι) (f : ι → M →ₗ[R] M₂) (b : M) : t.sum f b = t.sum (λd, f d b) := (t.sum_hom (λ g : M →ₗ[R] M₂, g b)).symm @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl /-- `λb, f b • x` is a linear map. -/ def smul_right (f : M₂ →ₗ[R] R) (x : M) : M₂ →ₗ[R] M := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_right f x : M₂ → M) c = f c • x := rfl instance : has_one (M →ₗ[R] M) := ⟨linear_map.id⟩ instance : has_mul (M →ₗ[R] M) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : M) : (1 : M →ₗ[R] M) x = x := rfl @[simp] lemma mul_app (A B : M →ₗ[R] M) (x : M) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : M₃ →ₗ[R] M) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : M₂ →ₗ[R] M₃).comp f = 0 := rfl section variables (R M) include M instance endomorphism_ring : ring (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp } end section open_locale classical /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = finset.sum finset.univ (λi:ι, x i • (f (λj, if i = j then 1 else 0))) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw f.map_smul end end section variables (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl /-- The pair of two linear maps is a linear map. -/ def pair (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : M →ₗ[R] M₂ × M₃ := ⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩ @[simp] theorem pair_apply (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (x : M) : pair f g x = (f x, g x) := rfl @[simp] theorem fst_pair (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (pair f g) = f := by ext; refl @[simp] theorem snd_pair (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (pair f g) = g := by ext; refl @[simp] theorem pair_fst_snd : pair (fst R M M₂) (snd R M M₂) = linear_map.id := by ext; refl section variables (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl /-- The copair function `λ x : M × M₂, f x.1 + g x.2` is a linear map. -/ def copair (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := ⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩ @[simp] theorem copair_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M) (y : M₂) : copair f g (x, y) = f x + g y := rfl @[simp] theorem copair_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (copair f g).comp (inl R M M₂) = f := by ext; simp @[simp] theorem copair_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (copair f g).comp (inr R M M₂) = g := by ext; simp @[simp] theorem copair_inl_inr : copair (inl R M M₂) (inr R M M₂) = linear_map.id := by ext ⟨x, y⟩; simp theorem fst_eq_copair : fst R M M₂ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_copair : snd R M M₂ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_pair : inl R M M₂ = pair linear_map.id 0 := rfl theorem inr_eq_pair : inr R M M₂ = pair 0 linear_map.id := rfl end section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f g : M →ₗ[R] M₂) include R instance : has_scalar R (M →ₗ[R] M₂) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : R) (x : M) : (a • f) x = a • f x := rfl instance : module R (M →ₗ[R] M₂) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def congr_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ theorem smul_comp (g : M₂ →ₗ[R] M₃) (a : R) : (a • g).comp f = a • (g.comp f) := rfl theorem comp_smul (g : M₂ →ₗ[R] M₃) (a : R) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl end comm_ring end linear_map namespace submodule variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set lattice instance : partial_order (submodule R M) := partial_order.lift (coe : submodule R M → set M) (λ a b, ext') (by apply_instance) lemma le_def {p p' : submodule R M} : p ≤ p' ↔ (p : set M) ⊆ p' := iff.rfl lemma le_def' {p p' : submodule R M} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl /-- If two submodules p and p' satisfy p ⊆ p', then `of_le p p'` is the linear map version of this inclusion. -/ def of_le {p p' : submodule R M} (h : p ≤ p') : p →ₗ[R] p' := linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx @[simp] theorem of_le_apply {p p' : submodule R M} (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : (submodule.subtype q).comp (of_le h) = submodule.subtype p := by ext ⟨b, hb⟩; simp /-- The set `{0}` is the bottom element of the lattice of submodules. -/ instance : has_bot (submodule R M) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ @[simp] lemma bot_coe : ((⊥ : submodule R M) : set M) = {0} := rfl section variables (R) @[simp] lemma mem_bot : x ∈ (⊥ : submodule R M) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule R M) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } /-- The universal set is the top element of the lattice of submodules. -/ instance : has_top (submodule R M) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule R M) : set M) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule R M) := trivial lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : p = ⊥ := by ext x; simp [semimodule.eq_zero_of_zero_eq_one _ x zero_eq_one] instance : order_top (submodule R M) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule R M) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule R M)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule R M)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule R M) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule R M) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.lattice.order_top, ..submodule.lattice.order_bot } instance : add_comm_monoid (submodule R M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (p q : submodule R M) : p + q = p ⊔ q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl lemma eq_top_iff' {p : submodule R M} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set M) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule R M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule R M)) : (↑(Inf P) : set M) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule R M) : (↑⨅ i, p i : set M) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule R M) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:M) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set M)) ↔ _, by simp /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : M →ₗ[R] M₂) (p : submodule R M) : submodule R M₂ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } lemma map_coe (f : M →ₗ[R] M₂) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl @[simp] lemma mem_map {f : M →ₗ[R] M₂} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M →ₗ[R] M₂} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M →ₗ[R] M₂} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M →ₗ[R] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : M →ₗ[R] M₂) (p : submodule R M₂) : submodule R M := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : M →ₗ[R] M₂) (p : submodule R M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M₃) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : M →ₗ[R] M₂} {q q' : submodule R M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : M →ₗ[R] M₂} {p : submodule R M} {q : submodule R M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : M →ₗ[R] M₂) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M →ₗ[R] M₂) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M →ₗ[R] M₂) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : M →ₗ[R] M₂) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : M →ₗ[R] M₂) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M →ₗ[R] M₂) q = ⊤ := ext $ by simp lemma map_comap_le (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : M →ₗ[R] M₂) (p : submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : M →ₗ[R] M₂} {p : submodule R M} {p' : submodule R M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr @[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι) (S : ι → submodule R M) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), rw [show supr S = ⨆ i, span R (S i), by simp, ← span_Union], unfreezeI, refine λ x hx, span_induction hx (λ _, id) _ _ _, { cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ }, { simp, intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_supr_of_mem {ι : Sort*} {b : M} (p : ι → submodule R M) (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h @[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι) (S : ι → submodule R M) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hzs : z ∈ Sup s) (x ∈ s) (hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) : ∃ y ∈ s, z ∈ y := begin haveI := classical.dec, rw Sup_eq_supr at hzs, have : ∃ (i : submodule R M), z ∈ ⨆ (H : i ∈ s), i, { refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs, by_cases his : i ∈ s; by_cases hjs : j ∈ s, { rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩, exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik), le_supr_of_le hks (supr_le $ λ _, hjk)⟩ }, { exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ }, { exact ⟨j, supr_le $ his.elim, le_refl _⟩ }, { exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } }, cases this with N hzn, by_cases hns : N ∈ s, { have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _), exact ⟨N, hns, this hzn⟩ }, { have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim, cases (mem_bot R).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ } end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ end lemma mem_span_singleton {y : M} : x ∈ span R ({y} : set M) ↔ ∃ a:R, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : M) : (span R ({y} : set M) : set M) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin rw [← union_singleton, span_union, mem_sup], simp [mem_span_singleton], split, { rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ }, { rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ } end lemma mem_span_insert' {y} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ } end lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ lemma span_singleton_eq_bot : span R ({x} : set M) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : M →ₗ[R] M₂) : span R (f '' s) = map f (span R s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma linear_eq_on (s : set M) {f g : M →ₗ[R] M₂} (H : ∀x∈s, f x = g x) {x} (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M₂) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (M × M₂)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M₂} {x : M × M₂} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M₂) : span R (set.prod s t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M₂)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M₂)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M₂} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup /-- The equivalence relation associated to a submodule `p`, defined by `x ≈ y` iff `y - x ∈ p`. -/ def quotient_rel : setoid M := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩ /-- The quotient of a module `M` by a submodule `p ⊆ M`. -/ def quotient : Type* := quotient (quotient_rel p) namespace quotient /-- Map associating to an element of `M` the corresponding element of `M/p`, when `p` is a submodule of `M`. -/ def mk {p : submodule R M} : M → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule R M} (x : M) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule R M} (x : M) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule R M} (x : M) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : M} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm] instance : has_scalar R (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module R (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] end quotient end submodule namespace submodule variables [discrete_field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end set_option class.instance_max_depth 40 lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by by_cases a = 0; simp [h, map_smul] end submodule namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] include R open submodule @[simp] lemma finsupp_sum {R M M₂ γ} [ring R] [add_comm_group M] [module R M] [add_comm_group M₂] [module R M₂] [has_zero γ] (f : M →ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. -/ def range (f : M →ₗ[R] M₂) : submodule R M₂ := map f ⊤ theorem range_coe (f : M →ₗ[R] M₂) : (range f : set M₂) = set.range f := set.image_univ @[simp] theorem mem_range {f : M →ₗ[R] M₂} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := (set.ext_iff _ _).1 (range_coe f). @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := map_id _ theorem range_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : M →ₗ[R] M₂} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M →ₗ[R] M₂} {p : submodule R M} : map f p ≤ range f := map_mono le_top lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ := begin refine eq_top_iff'.2 (λ x, mem_sup.2 _), rcases x with ⟨x₁, x₂⟩ , have h₁ : prod.mk x₁ (0 : M₂) ∈ (inl R M M₂).range, by simp, have h₂ : prod.mk (0 : M) x₂ ∈ (inr R M M₂).range, by simp, use [⟨x₁, 0⟩, h₁, ⟨0, x₂⟩, h₂], simp end /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M →ₗ[R] M₂) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : M →ₗ[R] M₂} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl theorem ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : M →ₗ[R] M₂} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} {s : set M} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl theorem ker_eq_bot {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot' {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := have h : (∀ m ∈ (⊤ : submodule R M), f m = 0 → m = 0) ↔ (∀ m, f m = 0 → m = 0), from ⟨λ h m, h m mem_top, λ h m _, h m⟩, by simpa [h, disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : M →ₗ[R] M₂} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : M →ₗ[R] M₂} {q : submodule R M₂} (h : q ≤ range f) : map f (comap f q) = q := by rw [map_comap_eq, inf_of_le_right h] lemma comap_map_eq (f : M →ₗ[R] M₂) (p : submodule R M) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : M →ₗ[R] M₂} {p : submodule R M} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] @[simp] theorem ker_zero : ker (0 : M →ₗ[R] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : M →ₗ[R] M₂) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : M →ₗ[R] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : M →ₗ[R] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem map_le_map_iff {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : M →ₗ[R] M₂} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : M →ₗ[R] M₂} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_copair_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : submodule R M) (q : submodule R M₂) : map (copair f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_pair_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : submodule R M₂) (q : submodule R M₃) : comap (pair f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) : p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) : p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) := by rw [← map_copair_prod, copair_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set M₂} : span R (prod.inl '' s ∪ prod.inr '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_pair (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (pair f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_pair_prod]; refl end linear_map namespace linear_map variables [discrete_field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end linear_map namespace is_linear_map lemma is_linear_map_add {R M : Type*} [ring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [ring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp }, { intros x y, simp [smul_add] } end end is_linear_map namespace submodule variables {T : ring R} [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map @[simp] theorem map_top (f : M →ₗ[R] M₂) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : M →ₗ[R] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] lemma disjoint_iff_comap_eq_bot (p q : submodule R M) : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff p.ker_subtype, map_bot, map_comap_subtype]; refl /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule R p → submodule R p → Prop) ≃o ((≤) : {p' : submodule R M // p' ≤ p} → {p' : submodule R M // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of M. -/ def map_subtype.le_order_embedding : ((≤) : submodule R p → submodule R p → Prop) ≼o ((≤) : submodule R M → submodule R M → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of M. -/ def map_subtype.lt_order_embedding : ((<) : submodule R p → submodule R p → Prop) ≼o ((<) : submodule R M → submodule R M → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] /-- The map from a module `M` to the quotient of `M` by a submodule `p` as a linear map. -/ def mkq : M →ₗ[R] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- The map from the quotient of `M` by a submodule `p` to `M₂` induced by a linear map `f : M → M₂` vanishing on `p`, as a linear map. -/ def liftq (f : M →ₗ[R] M₂) (h : p ≤ f.ker) : p.quotient →ₗ[R] M₂ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : M →ₗ[R] M₂) {h} (x : M) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : M →ₗ[R] M₂) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule R p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] /-- The map from the quotient of `M` by submodule `p` to the quotient of `M₂` by submodule `q` along `f : M → M₂` is linear. -/ def mapq (f : M →ₗ[R] M₂) (h : p ≤ comap f q) : p.quotient →ₗ[R] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : M →ₗ[R] M₂) {h} (x : M) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : M →ₗ[R] M₂) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : M →ₗ[R] M₂) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : M →ₗ[R] M₂) (h) (q : submodule R (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : M →ₗ[R] M₂) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : M →ₗ[R] M₂) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : M →ₗ[R] M₂) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- The correspondence theorem for modules: there is an order isomorphism between submodules of the quotient of `M` by `p`, and submodules of `M` larger than `p`. -/ def comap_mkq.order_iso : ((≤) : submodule R p.quotient → submodule R p.quotient → Prop) ≃o ((≤) : {p' : submodule R M // p ≤ p'} → {p' : submodule R M // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq], ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.le_order_embedding : ((≤) : submodule R p.quotient → submodule R p.quotient → Prop) ≼o ((≤) : submodule R M → submodule R M → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.lt_order_embedding : ((<) : submodule R p.quotient → submodule R p.quotient → Prop) ≼o ((<) : submodule R M → submodule R M → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ structure linear_equiv (R : Type u) (M : Type v) (M₂ : Type w) [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] extends M →ₗ[R] M₂, M ≃ M₂ end infix ` ≃ₗ ` := linear_equiv _ notation M ` ≃ₗ[`:50 R `] ` M₂ := linear_equiv R M M₂ namespace linear_equiv section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] include R instance : has_coe (M ≃ₗ[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩ @[simp] theorem coe_apply (e : M ≃ₗ[R] M₂) (b : M) : (e : M →ₗ[R] M₂) b = e b := rfl lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₗ[R] M₂) → M ≃ M₂) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[ext] lemma ext {f g : M ≃ₗ[R] M₂} (h : (f : M → M₂) = g) : f = g := to_equiv_injective (equiv.eq_of_to_fun_eq h) section variable (M) /-- The identity map is a linear equivalence. -/ @[refl] def refl : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end /-- Linear equivalences are symmetric. -/ @[symm] def symm (e : M ≃ₗ[R] M₂) : M₂ ≃ₗ[R] M := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } /-- Linear equivalences are transitive. -/ @[trans] def trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : M ≃ₗ[R] M₃ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } /-- A linear equivalence is an additive equivalence. -/ def to_add_equiv (e : M ≃ₗ[R] M₂) : M ≃+ M₂ := { map_add' := e.add, .. e } @[simp] theorem apply_symm_apply (e : M ≃ₗ[R] M₂) (c : M₂) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (e : M ≃ₗ[R] M₂) (b : M) : e.symm (e b) = b := e.5 b @[simp] theorem map_add (e : M ≃ₗ[R] M₂) (a b : M) : e (a + b) = e a + e b := e.add a b @[simp] theorem map_zero (e : M ≃ₗ[R] M₂) : e 0 = 0 := e.to_linear_map.map_zero @[simp] theorem map_neg (e : M ≃ₗ[R] M₂) (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (e : M ≃ₗ[R] M₂) (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b @[simp] theorem map_smul (e : M ≃ₗ[R] M₂) (c : R) (x : M) : e (c • x) = c • e x := e.smul c x @[simp] theorem map_eq_zero_iff (e : M ≃ₗ[R] M₂) {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff @[simp] theorem map_ne_zero_iff (e : M ≃ₗ[R] M₂) {x : M} : e x ≠ 0 ↔ x ≠ 0 := e.to_add_equiv.map_ne_zero_iff /-- A bijective linear map is a linear equivalence. Here, bijectivity is described by saying that the kernel of `f` is `{0}` and the range is the universal set. -/ noncomputable def of_bijective (f : M →ₗ[R] M₂) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : M ≃ₗ[R] M₂ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply (f : M →ₗ[R] M₂) {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₗ[R] M₂ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem ker (f : M ≃ₗ[R] M₂) : (f : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot.2 f.to_equiv.injective @[simp] protected theorem range (f : M ≃ₗ[R] M₂) : (f : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 f.to_equiv.surjective /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (p : submodule R M) (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply (p : submodule R M) {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply (p : submodule R M) {h} (x : M) : ↑((of_top p h).symm x) = x := rfl lemma eq_bot_of_equiv (p : submodule R M) (e : p ≃ₗ[R] (⊥ : submodule R M₂)) : p = ⊥ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot R).2 _), have := e.symm_apply_apply ⟨b, hb⟩, rw [← e.coe_apply, submodule.eq_zero_of_bot_submodule ((e : p →ₗ[R] (⊥ : submodule R M₂)) ⟨b, hb⟩), ← e.symm.coe_apply, linear_map.map_zero] at this, exact congr_arg (coe : p → M) this.symm end end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] include R open linear_map set_option class.instance_max_depth 39 /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) • 1 : M →ₗ M) (((a⁻¹ : units R) : R) • 1 : M →ₗ M) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f, e₂.to_linear_map.comp $ f.comp e₁.symm.to_linear_map, inv_fun := λ f, e₂.symm.to_linear_map.comp $ f.comp e₁.to_linear_map, left_inv := λ f, by { ext x, unfold_coes, change e₂.inv_fun (e₂.to_fun $ f.to_fun $ e₁.inv_fun $ e₁.to_fun x) = _, rw [e₁.left_inv, e₂.left_inv] }, right_inv := λ f, by { ext x, unfold_coes, change e₂.to_fun (e₂.inv_fun $ f.to_fun $ e₁.to_fun $ e₁.inv_fun x) = _, rw [e₁.right_inv, e₂.right_inv] }, add := λ f g, by { ext x, change e₂.to_fun ((f + g) (e₁.inv_fun x)) = _, rw [linear_map.add_apply, e₂.add], refl }, smul := λ c f, by { ext x, change e₂.to_fun ((c • f) (e₁.inv_fun x)) = _, rw [linear_map.smul_apply, e₂.smul], refl } } /-- If M₂ and M₃ are linearly isomorphic then the two spaces of linear maps from M into M₂ and M into M₃ are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ (M →ₗ M₃) := arrow_congr (linear_equiv.refl M) f /-- If M and M₂ are linearly isomorphic then the two spaces of linear maps from M and M₂ to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (M →ₗ[R] M) ≃ₗ[R] (M₂ →ₗ[R] M₂) := arrow_congr e e end comm_ring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variable (M) open linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha end field end linear_equiv namespace equiv variables [ring R] [add_comm_group M] [module R M] [add_comm_group M₂] [module R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { add := h.add, smul := h.smul, .. e} end equiv namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f : M →ₗ[R] M₂) /-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly equivalent to the range of `f`. -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[R] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[R] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule /-- Canonical linear map from the quotient p/(p ∩ p') to (p+p')/p', mapping x + (p ∩ p') to x + p', where p and p' are submodules of an ambient module. -/ def sup_quotient_to_quotient_inf (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient →ₗ[R] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end set_option class.instance_max_depth 41 /-- Second Isomorphism Law : the canonical map from p/(p ∩ p') to (p+p')/p' as a linear isomorphism. -/ noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[R] (comap (p ⊔ p').subtype p').quotient := { .. sup_quotient_to_quotient_inf p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin constructor, { rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } section prod /-- The cartesian product of two linear maps as a linear map. -/ def prod {R M M₂ M₃ : Type*} [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] (f₁ : M →ₗ[R] M₂) (f₂ : M →ₗ[R] M₃) : M →ₗ[R] (M₂ × M₃) := { to_fun := λx, (f₁ x, f₂ x), add := λx y, begin change (f₁ (x + y), f₂ (x+y)) = (f₁ x, f₂ x) + (f₁ y, f₂ y), simp only [linear_map.map_add], refl end, smul := λc x, by simp only [linear_map.map_smul] } lemma is_linear_map_prod_iso {R M M₂ M₃ : Type*} [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] : is_linear_map R (λ(p : (M →ₗ[R] M₂) × (M →ₗ[R] M₃)), (linear_map.prod p.1 p.2 : (M →ₗ[R] (M₂ × M₃)))) := ⟨λu v, rfl, λc u, rfl⟩ end prod section pi universe i variables {φ : ι → Type i} variables [∀i, add_comm_group (φ i)] [∀i, module R (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) := ⟨λc i, f i c, assume c d, funext $ assume i, (f i).add _ _, assume c d, funext $ assume i, (f i).smul _ _⟩ @[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := ⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := bot_unique $ submodule.le_def'.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end section variables (R φ) /-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.val_prop'], ext b ⟨j, hj⟩, refl }, { ext ⟨b, hb⟩, apply subtype.coe_ext.2, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { rw [dif_pos h], refl }, { rw [dif_neg h], exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[R] φ j := @function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end section variable [decidable_eq ι] variables (R φ) /-- The standard basis of the product of `φ`. -/ def std_basis (i : ι) : φ i →ₗ[R] (Πi, φ i) := pi (diag i) lemma std_basis_apply (i : ι) (b : φ i) : std_basis R φ i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis R φ i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis R φ i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ι) : ker (std_basis R φ i) = ⊥ := ker_eq_bot.2 $ assume f g hfg, have std_basis R φ i f i = std_basis R φ i g i := hfg ▸ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis R φ j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis R φ i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis R φ j) = 0 := by ext b; simp [std_basis_ne R φ _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis R φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩, rw [proj_std_basis_ne R φ j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis R φ i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show I.sum (λi, std_basis R φ i (b i)) = b, { ext i, rw [pi.finset_sum_apply, ← std_basis_same R φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem _ i $ mem_supr_of_mem _ hiI $ linear_map.mem_range.2 ⟨_, rfl⟩) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis R φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [finset.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis R φ this) (supr_le_supr $ assume i, _), rw [← finset.mem_coe, finset.coe_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis R φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis R φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis R φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis R φ i)) (⨆i∈J, range (std_basis R φ i)) := begin refine disjoint_mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end lemma std_basis_eq_single {a : R} : (λ (i : ι), (std_basis R (λ _ : ι, R) i) a) = λ (i : ι), (finsupp.single i a) := begin ext i j, rw [std_basis_apply, finsupp.single_apply], split_ifs, { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h)], refl }, end end end pi variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl M, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } instance automorphism_group.to_linear_map_is_monoid_hom : is_monoid_hom (linear_equiv.to_linear_map : (M ≃ₗ[R] M) → (M →ₗ[R] M)) := { map_one := rfl, map_mul := λ f g, rfl } /-- The group of invertible linear maps from `M` to itself -/ def general_linear_group := units (M →ₗ[R] M) namespace general_linear_group variables {R M} instance : group (general_linear_group R M) := by delta general_linear_group; apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { inv_fun := f.inv.to_fun, left_inv := λ m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := λ m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := f.symm, val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, begin delta to_linear_equiv of_linear_equiv, cases f with f f_inv, cases f, cases f_inv, congr end, right_inv := λ f, begin delta to_linear_equiv of_linear_equiv, cases f, congr end, map_mul' := λ x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : ((general_linear_equiv R M).to_equiv f).to_linear_map = f.val := by {ext, refl} end general_linear_group end linear_map
90cfcfeaebc69d7101c84b2f0de0e1d6f905e04e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/mv_polynomial/comm_ring.lean
ec1b5b6d175388034d69d13026723791a78fdc40
[]
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,797
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, Johan Commelin, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.mv_polynomial.variables import Mathlib.PostPort universes u u_1 v namespace Mathlib /-! # Multivariate polynomials over a ring Many results about polynomials hold when the coefficient ring is a commutative semiring. Some stronger results can be derived when we assume this semiring is a ring. This file does not define any new operations, but proves some of these stronger results. ## Notation As in other polynomial files, we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_ring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` -/ namespace mv_polynomial protected instance comm_ring {R : Type u} {σ : Type u_1} [comm_ring R] : comm_ring (mv_polynomial σ R) := add_monoid_algebra.comm_ring protected instance C.is_ring_hom {R : Type u} {σ : Type u_1} [comm_ring R] : is_ring_hom ⇑C := is_ring_hom.of_semiring ⇑C @[simp] theorem C_sub {R : Type u} (σ : Type u_1) (a : R) (a' : R) [comm_ring R] : coe_fn C (a - a') = coe_fn C a - coe_fn C a' := is_ring_hom.map_sub ⇑C @[simp] theorem C_neg {R : Type u} (σ : Type u_1) (a : R) [comm_ring R] : coe_fn C (-a) = -coe_fn C a := is_ring_hom.map_neg ⇑C @[simp] theorem coeff_neg {R : Type u} (σ : Type u_1) [comm_ring R] (m : σ →₀ ℕ) (p : mv_polynomial σ R) : coeff m (-p) = -coeff m p := finsupp.neg_apply @[simp] theorem coeff_sub {R : Type u} (σ : Type u_1) [comm_ring R] (m : σ →₀ ℕ) (p : mv_polynomial σ R) (q : mv_polynomial σ R) : coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply protected instance coeff.is_add_group_hom {R : Type u} (σ : Type u_1) [comm_ring R] (m : σ →₀ ℕ) : is_add_group_hom (coeff m) := is_add_group_hom.mk theorem degrees_neg {R : Type u} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) : degrees (-p) = degrees p := sorry theorem degrees_sub {R : Type u} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) (q : mv_polynomial σ R) : degrees (p - q) ≤ degrees p ⊔ degrees q := sorry @[simp] theorem vars_neg {R : Type u} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) : vars (-p) = vars p := sorry theorem vars_sub_subset {R : Type u} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) (q : mv_polynomial σ R) : vars (p - q) ⊆ vars p ∪ vars q := sorry @[simp] theorem vars_sub_of_disjoint {R : Type u} {σ : Type u_1} [comm_ring R] {p : mv_polynomial σ R} {q : mv_polynomial σ R} (hpq : disjoint (vars p) (vars q)) : vars (p - q) = vars p ∪ vars q := sorry @[simp] theorem eval₂_sub {R : Type u} {S : Type v} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) {q : mv_polynomial σ R} [comm_ring S] (f : R →+* S) (g : σ → S) : eval₂ f g (p - q) = eval₂ f g p - eval₂ f g q := ring_hom.map_sub (eval₂_hom f g) p q @[simp] theorem eval₂_neg {R : Type u} {S : Type v} {σ : Type u_1} [comm_ring R] (p : mv_polynomial σ R) [comm_ring S] (f : R →+* S) (g : σ → S) : eval₂ f g (-p) = -eval₂ f g p := ring_hom.map_neg (eval₂_hom f g) p theorem hom_C {S : Type v} {σ : Type u_1} [comm_ring S] (f : mv_polynomial σ ℤ → S) [is_ring_hom f] (n : ℤ) : f (coe_fn C n) = ↑n := ring_hom.eq_int_cast (ring_hom.comp (ring_hom.of f) (ring_hom.of ⇑C)) n /-- A ring homomorphism f : Z[X_1, X_2, ...] → R is determined by the evaluations f(X_1), f(X_2), ... -/ @[simp] theorem eval₂_hom_X {S : Type v} [comm_ring S] {R : Type u} (c : ℤ →+* S) (f : mv_polynomial R ℤ →+* S) (x : mv_polynomial R ℤ) : eval₂ c (⇑f ∘ X) x = coe_fn f x := sorry /-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as functions out of the type `σ`, -/ def hom_equiv {S : Type v} {σ : Type u_1} [comm_ring S] : (mv_polynomial σ ℤ →+* S) ≃ (σ → S) := equiv.mk (fun (f : mv_polynomial σ ℤ →+* S) => ⇑f ∘ X) (fun (f : σ → S) => eval₂_hom (int.cast_ring_hom S) f) sorry sorry @[simp] theorem total_degree_neg {R : Type u} {σ : Type u_1} [comm_ring R] (a : mv_polynomial σ R) : total_degree (-a) = total_degree a := sorry theorem total_degree_sub {R : Type u} {σ : Type u_1} [comm_ring R] (a : mv_polynomial σ R) (b : mv_polynomial σ R) : total_degree (a - b) ≤ max (total_degree a) (total_degree b) := sorry
ce0a0fe03a0fe607654a472b294d73bc5b2748af
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/set/intervals/iso_Ioo.lean
a3cfa15ccf2366410f70b5cf88130bf8d8022946
[ "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
1,582
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.monotone.odd import tactic.field_simp /-! # Order isomorphism between a linear ordered field and `(-1, 1)` In this file we provide an order isomorphism `order_iso_Ioo_neg_one_one` between the open interval `(-1, 1)` in a linear ordered field and the whole field. -/ open set /-- In a linear ordered field, the whole field is order isomorphic to the open interval `(-1, 1)`. We consider the actual implementation to be a "black box", so it is irreducible. -/ @[irreducible] def order_iso_Ioo_neg_one_one (k : Type*) [linear_ordered_field k] : k ≃o Ioo (-1 : k) 1 := begin refine strict_mono.order_iso_of_right_inverse _ _ (λ x, x / (1 - |x|)) _, { refine cod_restrict (λ x, x / (1 + |x|)) _ (λ x, abs_lt.1 _), have H : 0 < 1 + |x|, from (abs_nonneg x).trans_lt (lt_one_add _), calc |x / (1 + |x|)| = |x| / (1 + |x|) : by rw [abs_div, abs_of_pos H] ... < 1 : (div_lt_one H).2 (lt_one_add _) }, { refine (strict_mono_of_odd_strict_mono_on_nonneg _ _).cod_restrict _, { intro x, simp only [abs_neg, neg_div] }, { rintros x (hx : 0 ≤ x) y (hy : 0 ≤ y) hxy, simp [abs_of_nonneg, mul_add, mul_comm x y, div_lt_div_iff, hx.trans_lt (lt_one_add _), hy.trans_lt (lt_one_add _), *] } }, { refine λ x, subtype.ext _, have : 0 < 1 - |(x : k)|, from sub_pos.2 (abs_lt.2 x.2), field_simp [abs_div, this.ne', abs_of_pos this] } end
7fac3a4c766567d711cbefe0d7f442ef6daa4015
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/def_brec2.lean
a2f67416e51ff4c18a2d42e48cf6ac1856d5dff4
[ "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
232
lean
set_option trace.eqn_compiler true definition fib : nat → nat | 0 := 1 | 1 := 1 | (n+2) := fib n + fib (n+1) example : fib 0 = 1 := rfl example : fib 1 = 1 := rfl example (n : nat) : fib (n+2) = fib n + fib (n+1) := rfl
d52e92e9b45b4540a8c1e582852d277bda624d49
46125763b4dbf50619e8846a1371029346f4c3db
/src/data/equiv/denumerable.lean
035ca1a1d3986f77c2f64c1b92d9b8a7bc4c7bf7
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
9,052
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Denumerable (countably infinite) types, as a typeclass extending encodable. This is used to provide explicit encode/decode functions from nat, where the functions are known inverses of each other. -/ import data.equiv.encodable data.sigma data.fintype data.list.min_max open nat section prio set_option default_priority 100 -- see Note [default priority] /-- A denumerable type is one which is (constructively) bijective with ℕ. Although we already have a name for this property, namely `α ≃ ℕ`, we are here interested in using it as a typeclass. -/ class denumerable (α : Type*) extends encodable α := (decode_inv : ∀ n, ∃ a ∈ decode n, encode a = n) end prio namespace denumerable section variables {α : Type*} {β : Type*} [denumerable α] [denumerable β] open encodable theorem decode_is_some (α) [denumerable α] (n : ℕ) : (decode α n).is_some := option.is_some_iff_exists.2 $ (decode_inv α n).imp $ λ a, Exists.fst def of_nat (α) [f : denumerable α] (n : ℕ) : α := option.get (decode_is_some α n) @[simp, priority 900] theorem decode_eq_of_nat (α) [denumerable α] (n : ℕ) : decode α n = some (of_nat α n) := option.eq_some_of_is_some _ @[simp] theorem of_nat_of_decode {n b} (h : decode α n = some b) : of_nat α n = b := option.some.inj $ (decode_eq_of_nat _ _).symm.trans h @[simp] theorem encode_of_nat (n) : encode (of_nat α n) = n := let ⟨a, h, e⟩ := decode_inv α n in by rwa [of_nat_of_decode h] @[simp] theorem of_nat_encode (a) : of_nat α (encode a) = a := of_nat_of_decode (encodek _) def eqv (α) [denumerable α] : α ≃ ℕ := ⟨encode, of_nat α, of_nat_encode, encode_of_nat⟩ def mk' {α} (e : α ≃ ℕ) : denumerable α := { encode := e, decode := some ∘ e.symm, encodek := λ a, congr_arg some (e.symm_apply_apply _), decode_inv := λ n, ⟨_, rfl, e.apply_symm_apply _⟩ } def of_equiv (α) {β} [denumerable α] (e : β ≃ α) : denumerable β := { decode_inv := λ n, by simp, ..encodable.of_equiv _ e } @[simp] theorem of_equiv_of_nat (α) {β} [denumerable α] (e : β ≃ α) (n) : @of_nat β (of_equiv _ e) n = e.symm (of_nat α n) := by apply of_nat_of_decode; show option.map _ _ = _; simp def equiv₂ (α β) [denumerable α] [denumerable β] : α ≃ β := (eqv α).trans (eqv β).symm instance nat : denumerable nat := ⟨λ n, ⟨_, rfl, rfl⟩⟩ @[simp] theorem of_nat_nat (n) : of_nat ℕ n = n := rfl instance option : denumerable (option α) := ⟨λ n, by cases n; simp⟩ instance sum : denumerable (α ⊕ β) := ⟨λ n, begin suffices : ∃ a ∈ @decode_sum α β _ _ n, encode_sum a = bit (bodd n) (div2 n), {simpa [bit_decomp]}, simp [decode_sum]; cases bodd n; simp [decode_sum, bit, encode_sum] end⟩ section sigma variables {γ : α → Type*} [∀ a, denumerable (γ a)] instance sigma : denumerable (sigma γ) := ⟨λ n, by simp [decode_sigma]; exact ⟨_, _, ⟨rfl, heq.rfl⟩, by simp⟩⟩ @[simp] theorem sigma_of_nat_val (n : ℕ) : of_nat (sigma γ) n = ⟨of_nat α (unpair n).1, of_nat (γ _) (unpair n).2⟩ := option.some.inj $ by rw [← decode_eq_of_nat, decode_sigma_val]; simp; refl end sigma instance prod : denumerable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem prod_of_nat_val (n : ℕ) : of_nat (α × β) n = (of_nat α (unpair n).1, of_nat β (unpair n).2) := by simp; refl @[simp] theorem prod_nat_of_nat : of_nat (ℕ × ℕ) = unpair := by funext; simp instance int : denumerable ℤ := denumerable.mk' equiv.int_equiv_nat instance pnat : denumerable ℕ+ := denumerable.mk' equiv.pnat_equiv_nat instance ulift : denumerable (ulift α) := of_equiv _ equiv.ulift instance plift : denumerable (plift α) := of_equiv _ equiv.plift def pair : α × α ≃ α := equiv₂ _ _ end end denumerable namespace nat.subtype open function encodable lattice variables {s : set ℕ} [decidable_pred s] [infinite s] lemma exists_succ (x : s) : ∃ n, x.1 + n + 1 ∈ s := classical.by_contradiction $ λ h, have ∀ (a : ℕ) (ha : a ∈ s), a < x.val.succ, from λ a ha, lt_of_not_ge (λ hax, h ⟨a - (x.1 + 1), by rwa [add_right_comm, nat.add_sub_cancel' hax]⟩), infinite.not_fintype ⟨(((multiset.range x.1.succ).filter (∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), subtype.mk y hy) (by simp [-multiset.range_succ])).to_finset, by simpa [subtype.ext, multiset.mem_filter, -multiset.range_succ]⟩ def succ (x : s) : s := have h : ∃ m, x.1 + m + 1 ∈ s, from exists_succ x, ⟨x.1 + nat.find h + 1, nat.find_spec h⟩ lemma succ_le_of_lt {x y : s} (h : y < x) : succ y ≤ x := have hx : ∃ m, y.1 + m + 1 ∈ s, from exists_succ _, let ⟨k, hk⟩ := nat.exists_eq_add_of_lt h in have nat.find hx ≤ k, from nat.find_min' _ (hk ▸ x.2), show y.1 + nat.find hx + 1 ≤ x.1, by rw hk; exact add_le_add_right (add_le_add_left this _) _ lemma le_succ_of_forall_lt_le {x y : s} (h : ∀ z < x, z ≤ y) : x ≤ succ y := have hx : ∃ m, y.1 + m + 1 ∈ s, from exists_succ _, show x.1 ≤ y.1 + nat.find hx + 1, from le_of_not_gt $ λ hxy, have y.1 + nat.find hx + 1 ≤ y.1 := h ⟨_, nat.find_spec hx⟩ hxy, not_lt_of_le this $ calc y.1 ≤ y.1 + nat.find hx : le_add_of_nonneg_right (nat.zero_le _) ... < y.1 + nat.find hx + 1 : nat.lt_succ_self _ lemma lt_succ_self (x : s) : x < succ x := calc x.1 ≤ x.1 + _ : le_add_right (le_refl _) ... < succ x : nat.lt_succ_self (x.1 + _) lemma lt_succ_iff_le {x y : s} : x < succ y ↔ x ≤ y := ⟨λ h, le_of_not_gt (λ h', not_le_of_gt h (succ_le_of_lt h')), λ h, lt_of_le_of_lt h (lt_succ_self _)⟩ def of_nat (s : set ℕ) [decidable_pred s] [infinite s] : ℕ → s | 0 := ⊥ | (n+1) := succ (of_nat n) lemma of_nat_surjective_aux : ∀ {x : ℕ} (hx : x ∈ s), ∃ n, of_nat s n = ⟨x, hx⟩ | x := λ hx, let t : list s := ((list.range x).filter (λ y, y ∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), ⟨y, hy⟩) (by simp) in have hmt : ∀ {y : s}, y ∈ t ↔ y < ⟨x, hx⟩, by simp [list.mem_filter, subtype.ext, t]; intros; refl, have wf : ∀ m : s, list.maximum t = m → m.1 < x, from λ m hmax, by simpa [hmt] using list.maximum_mem hmax, begin cases hmax : list.maximum t with m, { exact ⟨0, le_antisymm (@bot_le s _ _) (le_of_not_gt (λ h, list.not_mem_nil (⊥ : s) $ by rw [← list.maximum_eq_none.1 hmax, hmt]; exact h))⟩ }, { cases of_nat_surjective_aux m.2 with a ha, exact ⟨a + 1, le_antisymm (by rw of_nat; exact succ_le_of_lt (by rw ha; exact wf _ hmax)) $ by rw of_nat; exact le_succ_of_forall_lt_le (λ z hz, by rw ha; cases m; exact list.le_maximum_of_mem (hmt.2 hz) hmax)⟩ } end using_well_founded {dec_tac := `[tauto]} lemma of_nat_surjective : surjective (of_nat s) := λ ⟨x, hx⟩, of_nat_surjective_aux hx private def to_fun_aux (x : s) : ℕ := (list.range x).countp s private lemma to_fun_aux_eq (x : s) : to_fun_aux x = ((finset.range x).filter s).card := by rw [to_fun_aux, list.countp_eq_length_filter]; refl open finset private lemma right_inverse_aux : ∀ n, to_fun_aux (of_nat s n) = n | 0 := begin rw [to_fun_aux_eq, card_eq_zero, eq_empty_iff_forall_not_mem], assume n, rw [mem_filter, of_nat, mem_range], assume h, exact not_lt_of_le bot_le (show (⟨n, h.2⟩ : s) < ⊥, from h.1) end | (n+1) := have ih : to_fun_aux (of_nat s n) = n, from right_inverse_aux n, have h₁ : (of_nat s n : ℕ) ∉ (range (of_nat s n)).filter s, by simp, have h₂ : (range (succ (of_nat s n))).filter s = insert (of_nat s n) ((range (of_nat s n)).filter s), begin simp only [finset.ext, mem_insert, mem_range, mem_filter], assume m, exact ⟨λ h, by simp only [h.2, and_true]; exact or.symm (lt_or_eq_of_le ((@lt_succ_iff_le _ _ _ ⟨m, h.2⟩ _).1 h.1)), λ h, h.elim (λ h, h.symm ▸ ⟨lt_succ_self _, subtype.property _⟩) (λ h, ⟨lt_of_le_of_lt (le_of_lt h.1) (lt_succ_self _), h.2⟩)⟩ end, begin clear_aux_decl, simp only [to_fun_aux_eq, of_nat, range_succ] at *, conv {to_rhs, rw [← ih, ← card_insert_of_not_mem h₁, ← h₂] }, end def denumerable (s : set ℕ) [decidable_pred s] [infinite s] : denumerable s := denumerable.of_equiv ℕ { to_fun := to_fun_aux, inv_fun := of_nat s, left_inv := left_inverse_of_surjective_of_right_inverse of_nat_surjective right_inverse_aux, right_inv := right_inverse_aux } end nat.subtype namespace denumerable open encodable def of_encodable_of_infinite (α : Type*) [encodable α] [infinite α] : denumerable α := begin letI := @decidable_range_encode α _; letI : infinite (set.range (@encode α _)) := infinite.of_injective _ (equiv.set.range _ encode_injective).injective, letI := nat.subtype.denumerable (set.range (@encode α _)), exact denumerable.of_equiv (set.range (@encode α _)) (equiv_range_encode α) end end denumerable
98b8bcf6180d8a566f17db637bc71dde76029ba2
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/logic/examples/negative.lean
2e8d07afeed85d712bb0e7a44a7035524afa3d62
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
783
lean
/- This example demonstrates why allowing types such as inductive D : Type := | intro : (D → D) → D would make the system inconsistent -/ /- If we were allowed to form the inductive type inductive D : Type := | intro : (D → D) → D we would get the following -/ universe l -- The new type A axiom D : Type.{l} -- The constructor axiom introD : (D → D) → D -- The eliminator axiom recD : Π {C : D → Type}, (Π (f : D → D) (r : Π d, C (f d)), C (introD f)) → (Π (d : D), C d) -- We would also get a computational rule for the eliminator, but we don't need it for deriving the inconsistency. noncomputable definition id : D → D := λd, d noncomputable definition v : D := introD id theorem inconsistent : false := recD (λ f ih, ih v) v
ac6f6abf60cbb13a09e4db330ffafcadce975887
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/autoboundIssues.lean
95d1233f96e069c6cbf5e7bf09018259817f9886
[ "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
222
lean
example : n.succ = 1 → n = 0 := by intros h; injection h example (h : n.succ = 1) : n = 0 := by injection h opaque T : Type opaque T.Pred : T → T → Prop example {ρ} (hρ : ρ.Pred σ) : T.Pred ρ ρ := sorry
5f125416c35fcf0ab33ad0f7ba50fd83f80449e0
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/tst8.lean
5d32c19ef1794c7203defe41ea588c2776d51687
[ "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
232
lean
import Int check fun (A : Type) (a : A), let b := a in b variable g : forall A : Type, A -> A definition f (A: Type) (a : A) : A := let b := g A a, c := g A b in c print f _ 10. print f _ (- 10).
e583f755a283f501ec61459990e79afc0ce3ca76
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/group/ulift.lean
6f74e32584af4174f70d859ac26c2424cfb24b8c
[ "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
6,623
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 data.int.cast.defs import algebra.hom.equiv /-! # `ulift` instances for groups and monoids This file defines instances for group, monoid, semigroup and related structures on `ulift` types. (Recall `ulift α` is just a "copy" of a type `α` in a higher universe.) We use `tactic.pi_instance_derive_field`, even though it wasn't intended for this purpose, which seems to work fine. We also provide `ulift.mul_equiv : ulift R ≃* R` (and its additive analogue). -/ universes u v variables {α : Type u} {β : Type*} {x y : ulift.{v} α} namespace ulift @[to_additive] instance has_one [has_one α] : has_one (ulift α) := ⟨⟨1⟩⟩ @[simp, to_additive] lemma one_down [has_one α] : (1 : ulift α).down = 1 := rfl @[to_additive] instance has_mul [has_mul α] : has_mul (ulift α) := ⟨λ f g, ⟨f.down * g.down⟩⟩ @[simp, to_additive] lemma mul_down [has_mul α] : (x * y).down = x.down * y.down := rfl @[to_additive] instance has_div [has_div α] : has_div (ulift α) := ⟨λ f g, ⟨f.down / g.down⟩⟩ @[simp, to_additive] lemma div_down [has_div α] : (x / y).down = x.down / y.down := rfl @[to_additive] instance has_inv [has_inv α] : has_inv (ulift α) := ⟨λ f, ⟨f.down⁻¹⟩⟩ @[simp, to_additive] lemma inv_down [has_inv α] : x⁻¹.down = (x.down)⁻¹ := rfl @[to_additive] instance has_smul [has_smul α β] : has_smul α (ulift β) := ⟨λ n x, up (n • x.down)⟩ @[simp, to_additive] lemma smul_down [has_smul α β] (a : α) (b : ulift.{v} β) : (a • b).down = a • b.down := rfl @[to_additive has_smul, to_additive_reorder 1] instance has_pow [has_pow α β] : has_pow (ulift α) β := ⟨λ x n, up (x.down ^ n)⟩ @[simp, to_additive smul_down, to_additive_reorder 1] lemma pow_down [has_pow α β] (a : ulift.{v} α) (b : β) : (a ^ b).down = a.down ^ b := rfl /-- The multiplicative equivalence between `ulift α` and `α`. -/ @[to_additive "The additive equivalence between `ulift α` and `α`."] def _root_.mul_equiv.ulift [has_mul α] : ulift α ≃* α := { map_mul' := λ x y, rfl, .. equiv.ulift } @[to_additive] instance semigroup [semigroup α] : semigroup (ulift α) := mul_equiv.ulift.injective.semigroup _ $ λ x y, rfl @[to_additive] instance comm_semigroup [comm_semigroup α] : comm_semigroup (ulift α) := equiv.ulift.injective.comm_semigroup _ $ λ x y, rfl @[to_additive] instance mul_one_class [mul_one_class α] : mul_one_class (ulift α) := equiv.ulift.injective.mul_one_class _ rfl $ λ x y, rfl instance mul_zero_one_class [mul_zero_one_class α] : mul_zero_one_class (ulift α) := equiv.ulift.injective.mul_zero_one_class _ rfl rfl $ λ x y, rfl @[to_additive] instance monoid [monoid α] : monoid (ulift α) := equiv.ulift.injective.monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance add_monoid_with_one [add_monoid_with_one α] : add_monoid_with_one (ulift α) := { nat_cast := λ n, ⟨n⟩, nat_cast_zero := congr_arg ulift.up nat.cast_zero, nat_cast_succ := λ n, congr_arg ulift.up (nat.cast_succ _), .. ulift.has_one, .. ulift.add_monoid } @[simp] lemma nat_cast_down [add_monoid_with_one α] (n : ℕ) : (n : ulift α).down = n := rfl @[to_additive] instance comm_monoid [comm_monoid α] : comm_monoid (ulift α) := equiv.ulift.injective.comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance monoid_with_zero [monoid_with_zero α] : monoid_with_zero (ulift α) := equiv.ulift.injective.monoid_with_zero _ rfl rfl (λ _ _, rfl) (λ _ _, rfl) instance comm_monoid_with_zero [comm_monoid_with_zero α] : comm_monoid_with_zero (ulift α) := equiv.ulift.injective.comm_monoid_with_zero _ rfl rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive] instance div_inv_monoid [div_inv_monoid α] : div_inv_monoid (ulift α) := equiv.ulift.injective.div_inv_monoid _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive] instance group [group α] : group (ulift α) := equiv.ulift.injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance add_group_with_one [add_group_with_one α] : add_group_with_one (ulift α) := { int_cast := λ n, ⟨n⟩, int_cast_of_nat := λ n, congr_arg ulift.up (int.cast_of_nat _), int_cast_neg_succ_of_nat := λ n, congr_arg ulift.up (int.cast_neg_succ_of_nat _), .. ulift.add_monoid_with_one, .. ulift.add_group } @[simp] lemma int_cast_down [add_group_with_one α] (n : ℤ) : (n : ulift α).down = n := rfl @[to_additive] instance comm_group [comm_group α] : comm_group (ulift α) := equiv.ulift.injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance group_with_zero [group_with_zero α] : group_with_zero (ulift α) := equiv.ulift.injective.group_with_zero _ rfl rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance comm_group_with_zero [comm_group_with_zero α] : comm_group_with_zero (ulift α) := equiv.ulift.injective.comm_group_with_zero _ rfl rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_left_cancel_semigroup] instance left_cancel_semigroup [left_cancel_semigroup α] : left_cancel_semigroup (ulift α) := equiv.ulift.injective.left_cancel_semigroup _ (λ _ _, rfl) @[to_additive add_right_cancel_semigroup] instance right_cancel_semigroup [right_cancel_semigroup α] : right_cancel_semigroup (ulift α) := equiv.ulift.injective.right_cancel_semigroup _ (λ _ _, rfl) @[to_additive add_left_cancel_monoid] instance left_cancel_monoid [left_cancel_monoid α] : left_cancel_monoid (ulift α) := equiv.ulift.injective.left_cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_right_cancel_monoid] instance right_cancel_monoid [right_cancel_monoid α] : right_cancel_monoid (ulift α) := equiv.ulift.injective.right_cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_cancel_monoid] instance cancel_monoid [cancel_monoid α] : cancel_monoid (ulift α) := equiv.ulift.injective.cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_cancel_monoid] instance cancel_comm_monoid [cancel_comm_monoid α] : cancel_comm_monoid (ulift α) := equiv.ulift.injective.cancel_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance nontrivial [nontrivial α] : nontrivial (ulift α) := equiv.ulift.symm.injective.nontrivial -- TODO we don't do `ordered_cancel_comm_monoid` or `ordered_comm_group` -- We'd need to add instances for `ulift` in `order.basic`. end ulift
19873ef03dd924a33870176eb30c9370e93c9edb
827a8a5c2041b1d7f55e128581f583dfbd65ecf6
/crazy_hits.hlean
5dab3c48406e9ef1bf662cbd73cded2fa2e78042
[ "Apache-2.0" ]
permissive
fpvandoorn/leansnippets
6af0499f6f3fd2c07e4b580734d77b67574e7c27
601bafbe07e9534af76f60994d6bdf741996ef93
refs/heads/master
1,590,063,910,882
1,545,093,878,000
1,545,093,878,000
36,044,957
2
2
null
1,442,619,708,000
1,432,256,875,000
Lean
UTF-8
Lean
false
false
6,220
hlean
import homotopy.circle open circle eq int nat prod sum equiv function is_trunc /--------------------------------------------------------------------------------------------------- We use the Licata trick to define HIT X := | b : X | s : (S¹ → X) → X | p : b = b -------- this is a W-suspension X with A = bool B ff = empty B tt = S¹ one equality: sup ff _ = sup ff _ -------- Note: we don't (and can't in Lean) actually hide the recursor for the "wrong" datatype X, so we have to be careful not to use X.rec or induction x for (x : X) ---------------------------------------------------------------------------------------------------/ namespace hide inductive X := | b : X | s : (S¹ → X) → X open X axiom p : b = b definition rec [unfold 5] {P : X → Type} (Pb : P b) (Ps : Π(f : S¹ → X), (Π(x : S¹), P (f x)) → P (s f)) (Pp : Pb =[p] Pb) (x : X) : P x := X.rec_on x Pb Ps axiom rec_p {P : X → Type} (Pb : P b) (Ps : Π(f : S¹ → X), (Π(x : S¹), P (f x)) → P (s f)) (Pp : Pb =[p] Pb) : apd (rec Pb Ps Pp) p = Pp definition elim [unfold 5] {P : Type} (Pb : P) (Ps : Π(f : S¹ → X), (S¹ → P) → P) (Pp : Pb = Pb) (x : X) : P := rec Pb Ps (pathover_of_eq _ Pp) x theorem elim_p {P : Type} (Pb : P) (Ps : Π(f : S¹ → X), (S¹ → P) → P) (Pp : Pb = Pb) : ap (elim Pb Ps Pp) p = Pp := sorry -- easy to prove -- abbreviation Y := ℕ × S¹ ⊎ ℕ × ℤ × ℕ inductive Ztree := | leaf : Ztree | node : ℤ → Ztree → Ztree abbreviation W := Ztree × S¹ /- need: (S¹ → Y) ≃ (Σ(y : Y), y = y) (Σ(y : X + Y), y = y) ≃ (Σ(x : X), x = x) + (Σ(y : Y), y = y) (Σ(x : X), x = x) ≃ X if x is set (Σ(y : X × Y), y = y) ≃ (Σ(x : X), x = x) × (Σ(y : Y), y = y) ? (Σ(y : S¹), y = y) ≃ S¹ × ℤ? -/ -- definition φ (x : X) : Y := -- begin -- refine (elim _ _ _ x), -- { exact inl (0, base)}, -- { intros f g, -- /- -- g is constant (inl (n, x)) ↦ inl (n+1, x) -- g is constant (inr (n, m, k)) ↦ inr (n, m, k+1) -- g base = inl (n, x) and ap f loop = 'loop'ᵐ ↦ (n, m, 0) -- -/ }, -- { exact ap inl (prod_eq idp loop)}, -- end -- definition ψ₁ (n : ℕ) : X := -- begin -- induction n with n x, -- exact b, -- exact s (λz, x) -- end -- definition pψ (n : ℕ) : ψ₁ n = ψ₁ n := -- begin -- induction n with n q, -- exact p, -- exact ap (λx, s (λz, x)) q, -- end definition px (x : X) : x = x := begin refine rec _ _ _ x, { exact p}, { intro f q, exact ap s (eq_of_homotopy q)}, { apply eq_pathover, rewrite +ap_id, exact square_of_eq idp} end -- definition ψY (y : Y) : X := -- begin -- induction y with y y, -- { induction y with n z, induction z, -- { exact ψ₁ n}, -- { exact pψ n}}, -- { induction y with n y, induction y with m k, -- induction k with k x, -- { apply s, intro z, induction z, exact ψ₁ n, apply pψ}, -- { apply s, intro z, exact x}} -- end section open equiv sigma sigma.ops definition foo (A : Type) : (S¹ → A) ≃ Σ(a : A), a = a := begin fapply equiv.MK, { intro f, exact ⟨f base, ap f loop⟩}, { intro q z, induction z, exact q.1, exact q.2}, { intro q, fapply sigma_eq, reflexivity, esimp, apply pathover_idp_of_eq, apply elim_loop}, { intro f, esimp, /-not fully simplified-/ apply eq_of_homotopy, intro z, esimp, induction z, reflexivity, esimp, apply eq_pathover, apply hdeg_square, esimp, apply elim_loop}, end end definition ψ (w : W) : X := begin induction w with w z, induction w with n zz x, { induction z, exact b, exact p}, { induction z, { apply s, intro z, induction z, { exact x}, { apply power !px n}}, { apply px}} end definition φ (x : X) : W := begin refine (elim _ _ _ x), { exact (Ztree.leaf, base)}, { intros f g, clear f, constructor, { apply Ztree.node, { apply to_fun (eq_equiv_Z (pr2 (g base))), apply ap (pr2 ∘ g) loop}, { exact pr1 (g base)}}, { exact pr2 (g base)}}, { exact prod_eq idp loop}, end definition ψnode (zz : Ztree) (n : ℤ) : ψ (Ztree.node n zz, base) = s (circle.elim (ψ (zz, base)) (power (px (ψ (zz, base))) n)) := idp definition φs (f : S¹ → X) : φ (s f) = (Ztree.node (to_fun (eq_equiv_Z (pr2 (φ (f base)))) (ap (pr2 ∘ φ ∘ f) loop)) (pr1 (φ (f base))), pr2 (φ (f base))) := idp definition ψφ (x : X) : ψ (φ x) = x := begin refine (rec _ _ _ x), { reflexivity}, { intros f q, rewrite [↑[φ]/-,↓φ (f base) FAILS-/], apply sorry}, { apply eq_pathover, apply hdeg_square, rewrite [ap_id, ap_compose ψ φ,↑φ,elim_p], apply sorry} end -- definition bar {A B : Type} {P : A → Type} (f : Π{a}, P a → B) {a a' : A} (p : a = a') : f definition φψ (w : W) : φ (ψ w) = w := begin induction w with w z, induction w with n zz q, { esimp [ψ], induction z, { reflexivity}, { apply eq_pathover, apply hdeg_square, rewrite [ap_compose φ,elim_loop,↑φ,elim_p]}}, { induction z, { rewrite [ψnode,φs], apply prod_eq, { esimp, apply ap011 Ztree.node, { exact sorry}, rewrite q}, { esimp, rewrite q}}, { exact sorry}} end end hide /--------------------------------------------------------------------------------------------------- Question: Is HIT ntree := | leaf : ntree | node : (ℕ → ntree) → ntree | nirr : Π(f : ℕ ≃ ℕ) (g : ℕ → ntree), node (g ∘ f) = node g | tru : is_set ntree the set-truncation of HIT tree := | leaf : tree | node : (ℕ → tree) → tree | irr : Π(f : ℕ ≃ ℕ) (g : ℕ → tree), node (g ∘ f) = node g ---------------------------------------------------------------------------------------------------/ namespace hide2 inductive ntree := | leaf : ntree | node : (ℕ → ntree) → ntree open ntree constant nirr : Π(f : ℕ ≃ ℕ) (g : ℕ → ntree), node (g ∘ f) = node g constant tru : is_set ntree end hide2
d186407c2345d4c59035997df932dfa2b25e71c3
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/data/buffer/parser.lean
06b9475d014bc9bc324ff0efecfbbd750eea6000
[ "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
6,828
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import data.buffer data.dlist universes u v inductive parse_result (α : Type u) | done (pos : ℕ) (result : α) : parse_result | fail (pos : ℕ) (expected : dlist string) : parse_result def parser (α : Type u) := ∀ (input : char_buffer) (start : ℕ), parse_result α namespace parser -- Type polymorphism is restricted here because of monad.bind variables {α β γ : Type} protected def bind (p : parser α) (f : α → parser β) : parser β := λ input pos, match p input pos with | parse_result.done pos a := f a input pos | parse_result.fail ._ pos expected := parse_result.fail β pos expected end protected def pure (a : α) : parser α := λ input pos, parse_result.done pos a private lemma id_map (p : parser α) : parser.bind p parser.pure = p := begin apply funext, intro input, apply funext, intro pos, dunfold parser.bind, cases (p input pos); exact rfl end private lemma bind_assoc (p : parser α) (q : α → parser β) (r : β → parser γ) : parser.bind (parser.bind p q) r = parser.bind p (λ a, parser.bind (q a) r) := begin apply funext, intro input, apply funext, intro pos, dunfold parser.bind, cases (p input pos); dunfold bind, cases (q result input pos_1); dunfold bind, all_goals {refl} end protected def fail (msg : string) : parser α := λ _ pos, parse_result.fail α pos (dlist.singleton msg) instance : monad_fail parser := { pure := @parser.pure, bind := @parser.bind, fail := @parser.fail, id_map := @id_map, pure_bind := λ _ _ _ _, rfl, bind_assoc := @bind_assoc } protected def failure : parser α := λ _ pos, parse_result.fail α pos dlist.empty protected def orelse (p q : parser α) : parser α := λ input pos, match p input pos with | parse_result.fail ._ pos₁ expected₁ := if pos₁ ≠ pos then parse_result.fail _ pos₁ expected₁ else match q input pos with | parse_result.fail ._ pos₂ expected₂ := if pos₁ < pos₂ then parse_result.fail _ pos₁ expected₁ else if pos₂ < pos₁ then parse_result.fail _ pos₂ expected₂ else -- pos₁ = pos₂ parse_result.fail _ pos₁ (expected₁ ++ expected₂) | ok := ok end | ok := ok end instance : alternative parser := { parser.monad_fail with failure := @parser.failure, orelse := @parser.orelse } instance : inhabited (parser α) := ⟨parser.failure⟩ /-- Overrides the expected token name, and does not consume input on failure. -/ def decorate_errors (msgs : thunk (list string)) (p : parser α) : parser α := λ input pos, match p input pos with | parse_result.fail ._ _ expected := parse_result.fail _ pos (dlist.lazy_of_list (msgs ())) | ok := ok end /-- Overrides the expected token name, and does not consume input on failure. -/ def decorate_error (msg : thunk string) (p : parser α) : parser α := decorate_errors [msg ()] p /-- Matches a single character satisfying the given predicate. -/ def sat (p : char → Prop) [decidable_pred p] : parser char := λ input pos, if h : pos < input.size then let c := input.read ⟨pos, h⟩ in if p c then parse_result.done (pos+1) $ input.read ⟨pos, h⟩ else parse_result.fail _ pos dlist.empty else parse_result.fail _ pos dlist.empty /-- Matches the empty word. -/ def eps : parser unit := return () /-- Matches the given character. -/ def ch (c : char) : parser unit := decorate_error c.to_string $ sat (= c) >> eps /-- Matches a whole char_buffer. Does not consume input in case of failure. -/ def char_buf (s : char_buffer) : parser unit := decorate_error s.to_string $ monad.for' s.to_list ch /-- Matches one out of a list of characters. -/ def one_of (cs : list char) : parser char := decorate_errors (do c ← cs, return c.to_string) $ sat (∈ cs) def one_of' (cs : list char) : parser unit := one_of cs >> eps /-- Matches a string. Does not consume input in case of failure. -/ def str (s : string) : parser unit := decorate_error s $ monad.for' s.to_list ch /-- Number of remaining input characters. -/ def remaining : parser ℕ := λ input pos, parse_result.done pos (input.size - pos) /-- Matches the end of the input. -/ def eof : parser unit := decorate_error "<end-of-file>" $ do rem ← remaining, guard $ rem = 0 def many_core (p : parser α) : ∀ (reps : ℕ), parser (list α) | 0 := failure | (reps+1) := (do x ← p, xs ← many_core reps, return (x::xs)) <|> return [] /-- Matches zero or more occurrences of `p`. -/ def many (p : parser α) : parser (list α) := λ input pos, many_core p (input.size - pos + 1) input pos def many_char (p : parser char) : parser string := list.as_string <$> many p /-- Matches zero or more occurrences of `p`. -/ def many' (p : parser α) : parser unit := many p >> eps /-- Matches one or more occurrences of `p`. -/ def many1 (p : parser α) : parser (list α) := list.cons <$> p <*> many p def many_char1 (p : parser char) : parser string := list.as_string <$> many1 p /-- Matches one or more occurrences of `p`, separated by `sep`. -/ def sep_by1 (sep : parser unit) (p : parser α) : parser (list α) := list.cons <$> p <*> many (sep >> p) /-- Matches zero or more occurrences of `p`, separated by `sep`. -/ def sep_by (sep : parser unit) (p : parser α) : parser (list α) := sep_by1 sep p <|> return [] def fix_core (F : parser α → parser α) : ∀ (max_depth : ℕ), parser α | 0 := failure | (max_depth+1) := F (fix_core max_depth) /-- Fixpoint combinator satisfying `fix F = F (fix F)`. -/ def fix (F : parser α → parser α) : parser α := λ input pos, fix_core F (input.size - pos + 1) input pos private def make_monospaced : char → char | '\n' := ' ' | '\t' := ' ' | c := c def mk_error_msg (input : char_buffer) (pos : ℕ) (expected : dlist string) : char_buffer := let left_ctx := (input.taken pos).taken_right 10, right_ctx := (input.dropn pos).taken 10 in left_ctx.map make_monospaced ++ right_ctx.map make_monospaced ++ "\n".to_char_buffer ++ left_ctx.map (λ _, ' ') ++ "^\n".to_char_buffer ++ "\n".to_char_buffer ++ "expected: ".to_char_buffer ++ string.to_char_buffer (" | ".intercalate expected.to_list) ++ "\n".to_char_buffer /-- Runs a parser on the given input. The parser needs to match the complete input. -/ def run (p : parser α) (input : char_buffer) : sum string α := match (p <* eof) input 0 with | parse_result.done pos res := sum.inr res | parse_result.fail ._ pos expected := sum.inl $ buffer.to_string $ mk_error_msg input pos expected end /-- Runs a parser on the given input. The parser needs to match the complete input. -/ def run_string (p : parser α) (input : string) : sum string α := run p input.to_char_buffer end parser
d14960301de8935bbebfc52b129d8e75c446b8cb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/nondepArrow.lean
739c44cd7c86d52a30351cc8dec222f00daa44ce
[ "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
70
lean
#check _ → _ -- second metavariable should not depend on first one
a3caf8799663b004d64e3ae0420fc1c37c06abec
07c76fbd96ea1786cc6392fa834be62643cea420
/hott/homotopy/LES_of_homotopy_groups.hlean
4bd1cb63e0de4ad6ebba9b814b04fe7a978fe26f
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
32,895
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn We define the fiber sequence of a pointed map f : X →* Y. We mostly follow the proof in section 8.4 of the book. PART 1: We define a sequence fiber_sequence as in Definition 8.4.3. It has types X(n) : Type* X(0) := Y, X(1) := X, X(n+1) := fiber (f(n)) with functions f(n) : X(n+1) →* X(n) f(0) := f f(n+1) := point (f(n)) [this is the first projection] We prove that this is an exact sequence. Then we prove Lemma 8.4.3, by showing that X(n+3) ≃* Ω(X(n)) and that this equivalence sends the pointed map f(n+3) to -Ω(f(n)), i.e. the composition of Ω(f(n)) with path inversion. Using this equivalence we get a boundary_map : Ω(Y) → pfiber f. PART 2: Now we can define a new fiber sequence X'(n) : Type*, and here we slightly diverge from the book. We define it as X'(0) := Y, X'(1) := X, X'(2) := fiber f X'(n+3) := Ω(X'(n)) with maps f'(n) : X'(n+1) →* X'(n) f'(0) := f f'(1) := point f f'(2) := boundary_map f'(n+3) := Ω(f'(n)) This sequence is not equivalent to the previous sequence. The difference is in the signs. The sequence f has negative signs (i.e. is composed with the inverse maps) for n ≡ 3, 4, 5 mod 6. This sign information is captured by e : X'(n) ≃* X'(n) such that e(k) := 1 for k = 0,1,2,3 e(k+3) := Ω(e(k)) ∘ (-)⁻¹ for k > 0 Now the sequence (X', f' ∘ e) is equivalent to (X, f), Hence (X', f' ∘ e) is an exact sequence. We then prove that (X', f') is an exact sequence by using that there are other equivalences eₗ and eᵣ such that f' = eᵣ ∘ f' ∘ e f' ∘ eₗ = e ∘ f'. (this fact is type_chain_complex_cancel_aut and is_exact_at_t_cancel_aut in the file chain_complex) eₗ and eᵣ are almost the same as e, except that the places where the inverse is taken is slightly shifted: eᵣ = (-)⁻¹ for n ≡ 3, 4, 5 mod 6 and eᵣ = 1 otherwise e = (-)⁻¹ for n ≡ 4, 5, 6 mod 6 (except for n = 0) and e = 1 otherwise eₗ = (-)⁻¹ for n ≡ 5, 6, 7 mod 6 (except for n = 0, 1) and eₗ = 1 otherwise PART 3: We change the type over which the sequence of types and maps are indexed from ℕ to ℕ × 3 (where 3 is the finite type with 3 elements). The reason is that we have that X'(3n) = Ωⁿ(Y), but this equality is not definitionally true. Hence we cannot even state whether f'(3n) = Ωⁿ(f) without using transports. This gets ugly. However, if we use as index type ℕ × 3, we can do this. We can define Y : ℕ × 3 → Type* as Y(n, 0) := Ωⁿ(Y) Y(n, 1) := Ωⁿ(X) Y(n, 2) := Ωⁿ(fiber f) with maps g(n) : Y(S n) →* Y(n) (where the successor is defined in the obvious way) g(n, 0) := Ωⁿ(f) g(n, 1) := Ωⁿ(point f) g(n, 2) := Ωⁿ(boundary_map) ∘ cast Here "cast" is the transport over the equality Ωⁿ⁺¹(Y) = Ωⁿ(Ω(Y)). We show that the sequence (ℕ, X', f') is equivalent to (ℕ × 3, Y, g). PART 4: We get the long exact sequence of homotopy groups by taking the set-truncation of (Y, g). -/ import .chain_complex algebra.homotopy_group eq2 open eq pointed sigma fiber equiv is_equiv is_trunc nat trunc algebra function /-------------- PART 1 --------------/ namespace chain_complex section open sigma.ops definition fiber_sequence_helper [constructor] (v : Σ(X Y : Type*), X →* Y) : Σ(Z X : Type*), Z →* X := ⟨pfiber v.2.2, v.1, ppoint v.2.2⟩ definition fiber_sequence_helpern (v : Σ(X Y : Type*), X →* Y) (n : ℕ) : Σ(Z X : Type*), Z →* X := iterate fiber_sequence_helper n v end section open sigma.ops universe variable u parameters {X Y : pType.{u}} (f : X →* Y) include f definition fiber_sequence_carrier (n : ℕ) : Type* := (fiber_sequence_helpern ⟨X, Y, f⟩ n).2.1 definition fiber_sequence_fun (n : ℕ) : fiber_sequence_carrier (n + 1) →* fiber_sequence_carrier n := (fiber_sequence_helpern ⟨X, Y, f⟩ n).2.2 /- Definition 8.4.3 -/ definition fiber_sequence : type_chain_complex.{0 u} +ℕ := begin fconstructor, { exact fiber_sequence_carrier }, { exact fiber_sequence_fun }, { intro n x, cases n with n, { exact point_eq x }, { exact point_eq x }} end definition is_exact_fiber_sequence : is_exact_t fiber_sequence := λn x p, fiber.mk (fiber.mk x p) rfl /- (generalization of) Lemma 8.4.4(i)(ii) -/ definition fiber_sequence_carrier_pequiv (n : ℕ) : fiber_sequence_carrier (n+3) ≃* Ω(fiber_sequence_carrier n) := pfiber_ppoint_pequiv (fiber_sequence_fun n) definition fiber_sequence_carrier_pequiv_eq (n : ℕ) (x : fiber_sequence_carrier (n+1)) (p : fiber_sequence_fun n x = pt) (q : fiber_sequence_fun (n+1) (fiber.mk x p) = pt) : fiber_sequence_carrier_pequiv n (fiber.mk (fiber.mk x p) q) = !respect_pt⁻¹ ⬝ ap (fiber_sequence_fun n) q⁻¹ ⬝ p := pfiber_ppoint_equiv_eq p q definition fiber_sequence_carrier_pequiv_inv_eq (n : ℕ) (p : Ω(fiber_sequence_carrier n)) : (fiber_sequence_carrier_pequiv n)⁻¹ᵉ* p = fiber.mk (fiber.mk pt (respect_pt (fiber_sequence_fun n) ⬝ p)) idp := pfiber_ppoint_equiv_inv_eq (fiber_sequence_fun n) p /- TODO: prove naturality of pfiber_ppoint_pequiv in general -/ /- Lemma 8.4.4(iii) -/ definition fiber_sequence_fun_eq_helper (n : ℕ) (p : Ω(fiber_sequence_carrier (n + 1))) : fiber_sequence_carrier_pequiv n (fiber_sequence_fun (n + 3) ((fiber_sequence_carrier_pequiv (n + 1))⁻¹ᵉ* p)) = Ω→ (fiber_sequence_fun n) p⁻¹ := begin refine ap (λx, fiber_sequence_carrier_pequiv n (fiber_sequence_fun (n + 3) x)) (fiber_sequence_carrier_pequiv_inv_eq (n+1) p) ⬝ _, /- the following three lines are rewriting some reflexivities: -/ -- replace (n + 3) with (n + 2 + 1), -- refine ap (fiber_sequence_carrier_pequiv n) -- (fiber_sequence_fun_eq1 (n+2) _ idp) ⬝ _, refine fiber_sequence_carrier_pequiv_eq n pt (respect_pt (fiber_sequence_fun n)) _ ⬝ _, esimp, apply whisker_right, apply whisker_left, apply ap02, apply inverse2, apply idp_con, end theorem fiber_sequence_carrier_pequiv_eq_point_eq_idp (n : ℕ) : fiber_sequence_carrier_pequiv_eq n (Point (fiber_sequence_carrier (n+1))) (respect_pt (fiber_sequence_fun n)) (respect_pt (fiber_sequence_fun (n + 1))) = idp := begin apply con_inv_eq_idp, refine ap (λx, whisker_left _ (_ ⬝ x)) _ ⬝ _, { reflexivity}, { reflexivity}, refine ap (whisker_left _) (eq_transport_Fl_idp_left (fiber_sequence_fun n) (respect_pt (fiber_sequence_fun n))) ⬝ _, apply whisker_left_idp_con_eq_assoc end theorem fiber_sequence_fun_phomotopy_helper (n : ℕ) : (fiber_sequence_carrier_pequiv n ∘* fiber_sequence_fun (n + 3)) ∘* (fiber_sequence_carrier_pequiv (n + 1))⁻¹ᵉ* ~* Ω→ (fiber_sequence_fun n) ∘* !pinverse := begin fapply phomotopy.mk, { exact chain_complex.fiber_sequence_fun_eq_helper f n}, { esimp, rewrite [idp_con], refine _ ⬝ whisker_left _ !idp_con⁻¹, apply whisker_right, apply whisker_left, exact chain_complex.fiber_sequence_carrier_pequiv_eq_point_eq_idp f n} end theorem fiber_sequence_fun_eq (n : ℕ) : Π(x : fiber_sequence_carrier (n + 4)), fiber_sequence_carrier_pequiv n (fiber_sequence_fun (n + 3) x) = Ω→ (fiber_sequence_fun n) (fiber_sequence_carrier_pequiv (n + 1) x)⁻¹ := begin refine @(homotopy_of_inv_homotopy_pre (fiber_sequence_carrier_pequiv (n + 1))) !pequiv.to_is_equiv _ _ _, apply fiber_sequence_fun_eq_helper n end theorem fiber_sequence_fun_phomotopy (n : ℕ) : fiber_sequence_carrier_pequiv n ∘* fiber_sequence_fun (n + 3) ~* (Ω→ (fiber_sequence_fun n) ∘* !pinverse) ∘* fiber_sequence_carrier_pequiv (n + 1) := begin apply phomotopy_of_pinv_right_phomotopy, apply fiber_sequence_fun_phomotopy_helper end definition boundary_map : Ω Y →* pfiber f := fiber_sequence_fun 2 ∘* (fiber_sequence_carrier_pequiv 0)⁻¹ᵉ* /-------------- PART 2 --------------/ /- Now we are ready to define the long exact sequence of loop spaces. First we define its carrier -/ definition loop_spaces : ℕ → Type* | 0 := Y | 1 := X | 2 := pfiber f | (k+3) := Ω (loop_spaces k) /- The maps between the homotopy groups -/ definition loop_spaces_fun : Π(n : ℕ), loop_spaces (n+1) →* loop_spaces n | 0 := proof f qed | 1 := proof ppoint f qed | 2 := proof boundary_map qed | (k+3) := proof Ω→ (loop_spaces_fun k) qed definition loop_spaces_fun_add3 [unfold_full] (n : ℕ) : loop_spaces_fun (n + 3) = Ω→ (loop_spaces_fun n) := idp definition fiber_sequence_pequiv_loop_spaces : Πn, fiber_sequence_carrier n ≃* loop_spaces n | 0 := by reflexivity | 1 := by reflexivity | 2 := by reflexivity | (k+3) := begin refine fiber_sequence_carrier_pequiv k ⬝e* _, apply loop_pequiv_loop, exact fiber_sequence_pequiv_loop_spaces k end definition fiber_sequence_pequiv_loop_spaces_add3 (n : ℕ) : fiber_sequence_pequiv_loop_spaces (n + 3) = Ω→ (fiber_sequence_pequiv_loop_spaces n) ∘* fiber_sequence_carrier_pequiv n := by reflexivity definition fiber_sequence_pequiv_loop_spaces_3_phomotopy : fiber_sequence_pequiv_loop_spaces 3 ~* fiber_sequence_carrier_pequiv 0 := begin refine pwhisker_right _ ap1_pid ⬝* _, apply pid_pcompose end definition pid_or_pinverse : Π(n : ℕ), loop_spaces n ≃* loop_spaces n | 0 := pequiv.rfl | 1 := pequiv.rfl | 2 := pequiv.rfl | 3 := pequiv.rfl | (k+4) := !pequiv_pinverse ⬝e* loop_pequiv_loop (pid_or_pinverse (k+1)) definition pid_or_pinverse_add4 (n : ℕ) : pid_or_pinverse (n + 4) = !pequiv_pinverse ⬝e* loop_pequiv_loop (pid_or_pinverse (n + 1)) := by reflexivity definition pid_or_pinverse_add4_rev (n : ℕ) : pid_or_pinverse (n + 4) ~* !pinverse ∘* Ω→(pid_or_pinverse (n + 1)) := !pinverse_natural theorem fiber_sequence_phomotopy_loop_spaces : Π(n : ℕ), fiber_sequence_pequiv_loop_spaces n ∘* fiber_sequence_fun n ~* (loop_spaces_fun n ∘* pid_or_pinverse (n + 1)) ∘* fiber_sequence_pequiv_loop_spaces (n + 1) | 0 := proof proof phomotopy.rfl qed ⬝* pwhisker_right _ !pcompose_pid⁻¹* qed | 1 := by reflexivity | 2 := begin refine !pid_pcompose ⬝* _, replace loop_spaces_fun 2 with boundary_map, refine _ ⬝* pwhisker_left _ fiber_sequence_pequiv_loop_spaces_3_phomotopy⁻¹*, apply phomotopy_of_pinv_right_phomotopy, exact !pcompose_pid⁻¹* end | (k+3) := begin replace (k + 3 + 1) with (k + 1 + 3), rewrite [fiber_sequence_pequiv_loop_spaces_add3 k, fiber_sequence_pequiv_loop_spaces_add3 (k+1)], refine !passoc ⬝* _, refine pwhisker_left _ (fiber_sequence_fun_phomotopy k) ⬝* _, refine !passoc⁻¹* ⬝* _ ⬝* !passoc, apply pwhisker_right, replace (k + 1 + 3) with (k + 4), xrewrite [loop_spaces_fun_add3, pid_or_pinverse_add4, to_pmap_pequiv_trans], refine _ ⬝* !passoc⁻¹*, refine _ ⬝* pwhisker_left _ !passoc⁻¹*, refine _ ⬝* pwhisker_left _ (pwhisker_left _ !pinverse_natural), refine !passoc⁻¹* ⬝* _ ⬝* !passoc ⬝* !passoc, apply pwhisker_right, refine !ap1_pcompose⁻¹* ⬝* _ ⬝* !ap1_pcompose ⬝* pwhisker_right _ !ap1_pcompose, apply ap1_phomotopy, exact fiber_sequence_phomotopy_loop_spaces k end definition pid_or_pinverse_right : Π(n : ℕ), loop_spaces n →* loop_spaces n | 0 := !pid | 1 := !pid | 2 := !pid | (k+3) := Ω→(pid_or_pinverse_right k) ∘* !pinverse definition pid_or_pinverse_left : Π(n : ℕ), loop_spaces n →* loop_spaces n | 0 := pequiv.rfl | 1 := pequiv.rfl | 2 := pequiv.rfl | 3 := pequiv.rfl | 4 := pequiv.rfl | (k+5) := Ω→(pid_or_pinverse_left (k+2)) ∘* !pinverse definition pid_or_pinverse_right_add3 (n : ℕ) : pid_or_pinverse_right (n + 3) = Ω→(pid_or_pinverse_right n) ∘* !pinverse := by reflexivity definition pid_or_pinverse_left_add5 (n : ℕ) : pid_or_pinverse_left (n + 5) = Ω→(pid_or_pinverse_left (n+2)) ∘* !pinverse := by reflexivity theorem pid_or_pinverse_commute_right : Π(n : ℕ), loop_spaces_fun n ~* pid_or_pinverse_right n ∘* loop_spaces_fun n ∘* pid_or_pinverse (n + 1) | 0 := proof !pcompose_pid⁻¹* ⬝* !pid_pcompose⁻¹* qed | 1 := proof !pcompose_pid⁻¹* ⬝* !pid_pcompose⁻¹* qed | 2 := proof !pcompose_pid⁻¹* ⬝* !pid_pcompose⁻¹* qed | (k+3) := begin replace (k + 3 + 1) with (k + 4), rewrite [pid_or_pinverse_right_add3, loop_spaces_fun_add3], refine _ ⬝* pwhisker_left _ (pwhisker_left _ !pid_or_pinverse_add4_rev⁻¹*), refine ap1_phomotopy (pid_or_pinverse_commute_right k) ⬝* _, refine !ap1_pcompose ⬝* _ ⬝* !passoc⁻¹*, apply pwhisker_left, refine !ap1_pcompose ⬝* _ ⬝* !passoc ⬝* !passoc, apply pwhisker_right, refine _ ⬝* pwhisker_right _ !pinverse_natural, refine _ ⬝* !passoc⁻¹*, refine !pcompose_pid⁻¹* ⬝* pwhisker_left _ _, symmetry, apply pinverse_pinverse end theorem pid_or_pinverse_commute_left : Π(n : ℕ), loop_spaces_fun n ∘* pid_or_pinverse_left (n + 1) ~* pid_or_pinverse n ∘* loop_spaces_fun n | 0 := proof !pcompose_pid ⬝* !pid_pcompose⁻¹* qed | 1 := proof !pcompose_pid ⬝* !pid_pcompose⁻¹* qed | 2 := proof !pcompose_pid ⬝* !pid_pcompose⁻¹* qed | 3 := proof !pcompose_pid ⬝* !pid_pcompose⁻¹* qed | (k+4) := begin replace (k + 4 + 1) with (k + 5), rewrite [pid_or_pinverse_left_add5, pid_or_pinverse_add4], replace (k + 4) with (k + 1 + 3), rewrite [loop_spaces_fun_add3], refine !passoc⁻¹* ⬝* _ ⬝* !passoc⁻¹*, refine _ ⬝* pwhisker_left _ !pinverse_natural, refine _ ⬝* !passoc, apply pwhisker_right, refine !ap1_pcompose⁻¹* ⬝* _ ⬝* !ap1_pcompose, exact ap1_phomotopy (pid_or_pinverse_commute_left (k+1)) end definition LES_of_loop_spaces' [constructor] : type_chain_complex +ℕ := transfer_type_chain_complex fiber_sequence (λn, loop_spaces_fun n ∘* pid_or_pinverse (n + 1)) fiber_sequence_pequiv_loop_spaces fiber_sequence_phomotopy_loop_spaces definition LES_of_loop_spaces [constructor] : type_chain_complex +ℕ := type_chain_complex_cancel_aut LES_of_loop_spaces' loop_spaces_fun pid_or_pinverse pid_or_pinverse_right (λn x, idp) pid_or_pinverse_commute_right definition is_exact_LES_of_loop_spaces : is_exact_t LES_of_loop_spaces := begin intro n, refine is_exact_at_t_cancel_aut n pid_or_pinverse_left _ _ pid_or_pinverse_commute_left _, apply is_exact_at_t_transfer, apply is_exact_fiber_sequence end open prod succ_str fin /-------------- PART 3 --------------/ definition fibration_sequence [unfold 4] : fin 3 → Type* | (fin.mk 0 H) := Y | (fin.mk 1 H) := X | (fin.mk 2 H) := pfiber f | (fin.mk (n+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition loop_spaces2 [reducible] : +3ℕ → Type* | (n, m) := Ω[n] (fibration_sequence m) definition loop_spaces2_add1 (n : ℕ) : Π(x : fin 3), loop_spaces2 (n+1, x) = Ω (loop_spaces2 (n, x)) | (fin.mk 0 H) := by reflexivity | (fin.mk 1 H) := by reflexivity | (fin.mk 2 H) := by reflexivity | (fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition loop_spaces_fun2 : Π(n : +3ℕ), loop_spaces2 (S n) →* loop_spaces2 n | (n, fin.mk 0 H) := proof Ω→[n] f qed | (n, fin.mk 1 H) := proof Ω→[n] (ppoint f) qed | (n, fin.mk 2 H) := proof Ω→[n] boundary_map ∘* loopn_succ_in n Y qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition loop_spaces_fun2_add1_0 (n : ℕ) (H : 0 < succ 2) : loop_spaces_fun2 (n+1, fin.mk 0 H) ~* cast proof idp qed ap1 (loop_spaces_fun2 (n, fin.mk 0 H)) := by reflexivity definition loop_spaces_fun2_add1_1 (n : ℕ) (H : 1 < succ 2) : loop_spaces_fun2 (n+1, fin.mk 1 H) ~* cast proof idp qed ap1 (loop_spaces_fun2 (n, fin.mk 1 H)) := by reflexivity definition loop_spaces_fun2_add1_2 (n : ℕ) (H : 2 < succ 2) : loop_spaces_fun2 (n+1, fin.mk 2 H) ~* cast proof idp qed ap1 (loop_spaces_fun2 (n, fin.mk 2 H)) := proof !ap1_pcompose⁻¹* qed definition nat_of_str [unfold 2] [reducible] {n : ℕ} : ℕ × fin (succ n) → ℕ := λx, succ n * pr1 x + val (pr2 x) definition str_of_nat {n : ℕ} : ℕ → ℕ × fin (succ n) := λm, (m / (succ n), mk_mod n m) definition nat_of_str_3S [unfold 2] [reducible] : Π(x : stratified +ℕ 2), nat_of_str x + 1 = nat_of_str (@S (stratified +ℕ 2) x) | (n, fin.mk 0 H) := by reflexivity | (n, fin.mk 1 H) := by reflexivity | (n, fin.mk 2 H) := by reflexivity | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition fin_prod_nat_equiv_nat [constructor] (n : ℕ) : ℕ × fin (succ n) ≃ ℕ := equiv.MK nat_of_str str_of_nat abstract begin intro m, unfold [nat_of_str, str_of_nat, mk_mod], refine _ ⬝ (eq_div_mul_add_mod m (succ n))⁻¹, rewrite [mul.comm] end end abstract begin intro x, cases x with m k, cases k with k H, apply prod_eq: esimp [str_of_nat], { rewrite [add.comm, add_mul_div_self_left _ _ (!zero_lt_succ), ▸*, div_eq_zero_of_lt H, zero_add]}, { apply eq_of_veq, esimp [mk_mod], rewrite [add.comm, add_mul_mod_self_left, ▸*, mod_eq_of_lt H]} end end /- note: in the following theorem the (n+1) case is 3 times the same, so maybe this can be simplified -/ definition loop_spaces2_pequiv' : Π(n : ℕ) (x : fin (nat.succ 2)), loop_spaces (nat_of_str (n, x)) ≃* loop_spaces2 (n, x) | 0 (fin.mk 0 H) := by reflexivity | 0 (fin.mk 1 H) := by reflexivity | 0 (fin.mk 2 H) := by reflexivity | (n+1) (fin.mk 0 H) := begin apply loop_pequiv_loop, rexact loop_spaces2_pequiv' n (fin.mk 0 H) end | (n+1) (fin.mk 1 H) := begin apply loop_pequiv_loop, rexact loop_spaces2_pequiv' n (fin.mk 1 H) end | (n+1) (fin.mk 2 H) := begin apply loop_pequiv_loop, rexact loop_spaces2_pequiv' n (fin.mk 2 H) end | n (fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition loop_spaces2_pequiv : Π(x : +3ℕ), loop_spaces (nat_of_str x) ≃* loop_spaces2 x | (n, x) := loop_spaces2_pequiv' n x local attribute loop_pequiv_loop [reducible] /- all cases where n>0 are basically the same -/ definition loop_spaces_fun2_phomotopy (x : +3ℕ) : loop_spaces2_pequiv x ∘* loop_spaces_fun (nat_of_str x) ~* (loop_spaces_fun2 x ∘* loop_spaces2_pequiv (S x)) ∘* pcast (ap (loop_spaces) (nat_of_str_3S x)) := begin cases x with n x, cases x with k H, do 3 (cases k with k; rotate 1), { /-k≥3-/ exfalso, apply lt_le_antisymm H, apply le_add_left}, { /-k=0-/ induction n with n IH, { refine !pid_pcompose ⬝* _ ⬝* !pcompose_pid⁻¹* ⬝* !pcompose_pid⁻¹*, reflexivity}, { refine _ ⬝* !pcompose_pid⁻¹*, refine _ ⬝* pwhisker_right _ !loop_spaces_fun2_add1_0⁻¹*, refine !ap1_pcompose⁻¹* ⬝* _ ⬝* !ap1_pcompose, apply ap1_phomotopy, exact IH ⬝* !pcompose_pid}}, { /-k=1-/ induction n with n IH, { refine !pid_pcompose ⬝* _ ⬝* !pcompose_pid⁻¹* ⬝* !pcompose_pid⁻¹*, reflexivity}, { refine _ ⬝* !pcompose_pid⁻¹*, refine _ ⬝* pwhisker_right _ !loop_spaces_fun2_add1_1⁻¹*, refine !ap1_pcompose⁻¹* ⬝* _ ⬝* !ap1_pcompose, apply ap1_phomotopy, exact IH ⬝* !pcompose_pid}}, { /-k=2-/ induction n with n IH, { refine !pid_pcompose ⬝* _ ⬝* !pcompose_pid⁻¹*, refine !pcompose_pid⁻¹* ⬝* pconcat2 _ _, { exact (pcompose_pid (chain_complex.boundary_map f))⁻¹*}, { refine !loop_pequiv_loop_rfl⁻¹* }}, { refine _ ⬝* !pcompose_pid⁻¹*, refine _ ⬝* pwhisker_right _ !loop_spaces_fun2_add1_2⁻¹*, refine !ap1_pcompose⁻¹* ⬝* _ ⬝* !ap1_pcompose, apply ap1_phomotopy, exact IH ⬝* !pcompose_pid}}, end definition LES_of_loop_spaces2 [constructor] : type_chain_complex +3ℕ := transfer_type_chain_complex2 LES_of_loop_spaces !fin_prod_nat_equiv_nat nat_of_str_3S @loop_spaces_fun2 @loop_spaces2_pequiv begin intro m x, refine loop_spaces_fun2_phomotopy m x ⬝ _, apply ap (loop_spaces_fun2 m), apply ap (loop_spaces2_pequiv (S m)), esimp, exact ap010 cast !ap_compose⁻¹ x end definition is_exact_LES_of_loop_spaces2 : is_exact_t LES_of_loop_spaces2 := begin intro n, apply is_exact_at_t_transfer2, apply is_exact_LES_of_loop_spaces end definition LES_of_homotopy_groups' [constructor] : chain_complex +3ℕ := trunc_chain_complex LES_of_loop_spaces2 /-------------- PART 4 --------------/ open prod.ops definition homotopy_groups [reducible] : +3ℕ → Set* := λnm, π[nm.1] (fibration_sequence nm.2) definition homotopy_groups_pequiv_loop_spaces2 [reducible] : Π(n : +3ℕ), ptrunc 0 (loop_spaces2 n) ≃* homotopy_groups n | (n, fin.mk 0 H) := by reflexivity | (n, fin.mk 1 H) := by reflexivity | (n, fin.mk 2 H) := by reflexivity | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition homotopy_groups_fun : Π(n : +3ℕ), homotopy_groups (S n) →* homotopy_groups n | (n, fin.mk 0 H) := proof π→[n] f qed | (n, fin.mk 1 H) := proof π→[n] (ppoint f) qed | (n, fin.mk 2 H) := proof π→[n] boundary_map ∘* homotopy_group_succ_in n Y qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition homotopy_groups_fun_phomotopy_loop_spaces_fun2 [reducible] : Π(n : +3ℕ), homotopy_groups_pequiv_loop_spaces2 n ∘* ptrunc_functor 0 (loop_spaces_fun2 n) ~* homotopy_groups_fun n ∘* homotopy_groups_pequiv_loop_spaces2 (S n) | (n, fin.mk 0 H) := by reflexivity | (n, fin.mk 1 H) := by reflexivity | (n, fin.mk 2 H) := begin refine !pid_pcompose ⬝* _ ⬝* !pcompose_pid⁻¹*, refine !ptrunc_functor_pcompose end | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition LES_of_homotopy_groups [constructor] : chain_complex +3ℕ := transfer_chain_complex LES_of_homotopy_groups' homotopy_groups_fun homotopy_groups_pequiv_loop_spaces2 homotopy_groups_fun_phomotopy_loop_spaces_fun2 definition is_exact_LES_of_homotopy_groups : is_exact LES_of_homotopy_groups := begin intro n, apply is_exact_at_transfer, apply is_exact_at_trunc, apply is_exact_LES_of_loop_spaces2 end variable (n : ℕ) /- the carrier of the fiber sequence is definitionally what we want (as pointed sets) -/ example : LES_of_homotopy_groups (str_of_nat 6) = π[2] Y :> Set* := by reflexivity example : LES_of_homotopy_groups (str_of_nat 7) = π[2] X :> Set* := by reflexivity example : LES_of_homotopy_groups (str_of_nat 8) = π[2] (pfiber f) :> Set* := by reflexivity example : LES_of_homotopy_groups (str_of_nat 9) = π[3] Y :> Set* := by reflexivity example : LES_of_homotopy_groups (str_of_nat 10) = π[3] X :> Set* := by reflexivity example : LES_of_homotopy_groups (str_of_nat 11) = π[3] (pfiber f) :> Set* := by reflexivity definition LES_of_homotopy_groups_0 : LES_of_homotopy_groups (n, 0) = π[n] Y := by reflexivity definition LES_of_homotopy_groups_1 : LES_of_homotopy_groups (n, 1) = π[n] X := by reflexivity definition LES_of_homotopy_groups_2 : LES_of_homotopy_groups (n, 2) = π[n] (pfiber f) := by reflexivity /- the functions of the fiber sequence is definitionally what we want (as pointed function). -/ definition LES_of_homotopy_groups_fun_0 : cc_to_fn LES_of_homotopy_groups (n, 0) = π→[n] f := by reflexivity definition LES_of_homotopy_groups_fun_1 : cc_to_fn LES_of_homotopy_groups (n, 1) = π→[n] (ppoint f) := by reflexivity definition LES_of_homotopy_groups_fun_2 : cc_to_fn LES_of_homotopy_groups (n, 2) = π→[n] boundary_map ∘* homotopy_group_succ_in n Y := by reflexivity open group definition group_LES_of_homotopy_groups (n : ℕ) [is_succ n] (x : fin (succ 2)) : group (LES_of_homotopy_groups (n, x)) := group_homotopy_group n (fibration_sequence x) definition pgroup_LES_of_homotopy_groups (n : ℕ) [H : is_succ n] (x : fin (succ 2)) : pgroup (LES_of_homotopy_groups (n, x)) := by induction H with n; exact @pgroup_of_group _ (group_LES_of_homotopy_groups (n+1) x) idp definition ab_group_LES_of_homotopy_groups (n : ℕ) [is_at_least_two n] (x : fin (succ 2)) : ab_group (LES_of_homotopy_groups (n, x)) := ab_group_homotopy_group n (fibration_sequence x) definition Group_LES_of_homotopy_groups (n : +3ℕ) : Group.{u} := πg[n.1+1] (fibration_sequence n.2) definition AbGroup_LES_of_homotopy_groups (n : +3ℕ) : AbGroup.{u} := πag[n.1+2] (fibration_sequence n.2) definition homomorphism_LES_of_homotopy_groups_fun : Π(k : +3ℕ), Group_LES_of_homotopy_groups (S k) →g Group_LES_of_homotopy_groups k | (k, fin.mk 0 H) := proof homomorphism.mk (cc_to_fn LES_of_homotopy_groups (k + 1, 0)) (homotopy_group_functor_mul _ _) qed | (k, fin.mk 1 H) := proof homomorphism.mk (cc_to_fn LES_of_homotopy_groups (k + 1, 1)) (homotopy_group_functor_mul _ _) qed | (k, fin.mk 2 H) := begin apply homomorphism.mk (cc_to_fn LES_of_homotopy_groups (k + 1, 2)), exact abstract begin rewrite [LES_of_homotopy_groups_fun_2], refine homomorphism.struct ((π→g[k+1] boundary_map) ∘g ghomotopy_group_succ_in k Y), end end end | (k, fin.mk (l+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition LES_is_equiv_of_trivial (n : ℕ) (x : fin (succ 2)) [H : is_succ n] (HX1 : is_contr (LES_of_homotopy_groups (stratified_pred snat' (n, x)))) (HX2 : is_contr (LES_of_homotopy_groups (stratified_pred snat' (n+1, x)))) : is_equiv (cc_to_fn LES_of_homotopy_groups (n, x)) := begin induction H with n, induction x with m H, cases m with m, { rexact @is_equiv_of_trivial +3ℕ LES_of_homotopy_groups (n, 2) (is_exact_LES_of_homotopy_groups (n, 2)) proof (is_exact_LES_of_homotopy_groups (n+1, 0)) qed HX1 proof HX2 qed proof pgroup_LES_of_homotopy_groups (n+1) 0 qed proof pgroup_LES_of_homotopy_groups (n+1) 1 qed proof homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun (n, 0)) qed }, cases m with m, { rexact @is_equiv_of_trivial +3ℕ LES_of_homotopy_groups (n+1, 0) (is_exact_LES_of_homotopy_groups (n+1, 0)) proof (is_exact_LES_of_homotopy_groups (n+1, 1)) qed HX1 proof HX2 qed proof pgroup_LES_of_homotopy_groups (n+1) 1 qed proof pgroup_LES_of_homotopy_groups (n+1) 2 qed proof homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun (n, 1)) qed }, cases m with m, { rexact @is_equiv_of_trivial +3ℕ LES_of_homotopy_groups (n+1, 1) (is_exact_LES_of_homotopy_groups (n+1, 1)) proof (is_exact_LES_of_homotopy_groups (n+1, 2)) qed HX1 proof HX2 qed proof pgroup_LES_of_homotopy_groups (n+1) 2 qed proof pgroup_LES_of_homotopy_groups (n+2) 0 qed proof homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun (n, 2)) qed }, exfalso, apply lt_le_antisymm H, apply le_add_left end definition LES_isomorphism_of_trivial_cod (n : ℕ) [H : is_succ n] (HX1 : is_contr (πg[n] Y)) (HX2 : is_contr (πg[n+1] Y)) : πg[n] (pfiber f) ≃g πg[n] X := begin induction H with n, refine isomorphism.mk (homomorphism_LES_of_homotopy_groups_fun (n, 1)) _, apply LES_is_equiv_of_trivial, apply HX1, apply HX2 end definition LES_isomorphism_of_trivial_dom (n : ℕ) [H : is_succ n] (HX1 : is_contr (πg[n] X)) (HX2 : is_contr (πg[n+1] X)) : πg[n+1] Y ≃g πg[n] (pfiber f) := begin induction H with n, refine isomorphism.mk (homomorphism_LES_of_homotopy_groups_fun (n, 2)) _, apply LES_is_equiv_of_trivial, apply HX1, apply HX2 end definition LES_isomorphism_of_trivial_pfiber (n : ℕ) (HX1 : is_contr (π[n] (pfiber f))) (HX2 : is_contr (πg[n+1] (pfiber f))) : πg[n+1] X ≃g πg[n+1] Y := begin refine isomorphism.mk (homomorphism_LES_of_homotopy_groups_fun (n, 0)) _, apply LES_is_equiv_of_trivial, apply HX1, apply HX2 end definition LES_is_contr_of_is_embedding_of_is_surjective (n : ℕ) (H : is_embedding (π→[n] f)) (H2 : is_surjective (π→[n+1] f)) : is_contr (π[n] (pfiber f)) := begin rexact @is_contr_of_is_embedding_of_is_surjective +3ℕ LES_of_homotopy_groups (n, 0) (is_exact_LES_of_homotopy_groups _) proof H qed proof H2 qed end definition is_contr_homotopy_group_fiber {n : ℕ} (H1 : is_embedding (π→[n] f)) (H2 : is_surjective (π→g[n+1] f)) : is_contr (π[n] (pfiber f)) := begin apply @is_contr_of_is_embedding_of_is_surjective +3ℕ LES_of_homotopy_groups (n, 0), exact is_exact_LES_of_homotopy_groups (n, 1), exact H1, exact H2 end definition is_contr_homotopy_group_fiber_of_is_equiv {n : ℕ} (H1 : is_equiv (π→[n] f)) (H2 : is_equiv (π→g[n+1] f)) : is_contr (π[n] (pfiber f)) := is_contr_homotopy_group_fiber (is_embedding_of_is_equiv _) (is_surjective_of_is_equiv _) end /- Fibration sequences This is a similar construction, but with as input data two pointed maps, and a pointed equivalence between the domain of the second map and the fiber of the first map, and a pointed homotopy. -/ section universe variable u parameters {F X Y : pType.{u}} (f : X →* Y) (g : F →* X) (e : pfiber f ≃* F) (p : ppoint f ~* g ∘* e) include f p open succ_str prod nat definition fibration_sequence_car [reducible] : +3ℕ → Type* | (n, fin.mk 0 H) := Ω[n] Y | (n, fin.mk 1 H) := Ω[n] X | (n, fin.mk k H) := Ω[n] F definition fibration_sequence_fun : Π(n : +3ℕ), fibration_sequence_car (S n) →* fibration_sequence_car n | (n, fin.mk 0 H) := proof Ω→[n] f qed | (n, fin.mk 1 H) := proof Ω→[n] g qed | (n, fin.mk 2 H) := proof Ω→[n] (e ∘* boundary_map f) ∘* loopn_succ_in n Y qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition fibration_sequence_pequiv : Π(x : +3ℕ), loop_spaces2 f x ≃* fibration_sequence_car x | (n, fin.mk 0 H) := by reflexivity | (n, fin.mk 1 H) := by reflexivity | (n, fin.mk 2 H) := loopn_pequiv_loopn n e | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition fibration_sequence_fun_phomotopy : Π(x : +3ℕ), fibration_sequence_pequiv x ∘* loop_spaces_fun2 f x ~* (fibration_sequence_fun x ∘* fibration_sequence_pequiv (S x)) | (n, fin.mk 0 H) := by reflexivity | (n, fin.mk 1 H) := begin refine !pid_pcompose ⬝* _, refine apn_phomotopy n p ⬝* _, refine !apn_pcompose ⬝* _, reflexivity end | (n, fin.mk 2 H) := begin refine !passoc⁻¹* ⬝* _ ⬝* !pcompose_pid⁻¹*, apply pwhisker_right, refine _ ⬝* !apn_pcompose⁻¹*, reflexivity end | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition type_LES_fibration_sequence [constructor] : type_chain_complex +3ℕ := transfer_type_chain_complex (LES_of_loop_spaces2 f) fibration_sequence_fun fibration_sequence_pequiv fibration_sequence_fun_phomotopy definition is_exact_type_fibration_sequence : is_exact_t type_LES_fibration_sequence := begin intro n, apply is_exact_at_t_transfer, apply is_exact_LES_of_loop_spaces2 end definition LES_fibration_sequence [constructor] : chain_complex +3ℕ := trunc_chain_complex type_LES_fibration_sequence end end chain_complex
bd2308b28e7e71a8a67ec2bff58adbc83ac4aa04
94e33a31faa76775069b071adea97e86e218a8ee
/src/order/liminf_limsup.lean
f135e1fe1c8ed427f4dd4d689688fc1932b1ced8
[ "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
29,083
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl, Rémy Degenne -/ import order.filter.cofinite /-! # liminfs and limsups of functions and filters Defines the Liminf/Limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `f.Limsup` (`f.Liminf`) where `f` is a filter taking values in a conditionally complete lattice. `f.Limsup` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `f.Liminf`). To work with the Limsup along a function `u` use `(f.map u).Limsup`. Usually, one defines the Limsup as `Inf (Sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `Inf_n (Sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `Limsup (λx, 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `Sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `Inf Sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ open filter set open_locale filter variables {α β γ ι : Type*} namespace filter section relation /-- `f.is_bounded (≺)`: the filter `f` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. `r` will be usually instantiated with `≤` or `≥`. -/ def is_bounded (r : α → α → Prop) (f : filter α) := ∃ b, ∀ᶠ x in f, r x b /-- `f.is_bounded_under (≺) u`: the image of the filter `f` under `u` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. -/ def is_bounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_bounded r variables {r : α → α → Prop} {f g : filter α} /-- `f` is eventually bounded if and only if, there exists an admissible set on which it is bounded. -/ lemma is_bounded_iff : f.is_bounded r ↔ (∃s∈f.sets, ∃b, s ⊆ {x | r x b}) := iff.intro (assume ⟨b, hb⟩, ⟨{a | r a b}, hb, b, subset.refl _⟩) (assume ⟨s, hs, b, hb⟩, ⟨b, mem_of_superset hs hb⟩) /-- A bounded function `u` is in particular eventually bounded. -/ lemma is_bounded_under_of {f : filter β} {u : β → α} : (∃b, ∀x, r (u x) b) → f.is_bounded_under r u | ⟨b, hb⟩ := ⟨b, show ∀ᶠ x in f, r (u x) b, from eventually_of_forall hb⟩ lemma is_bounded_bot : is_bounded r ⊥ ↔ nonempty α := by simp [is_bounded, exists_true_iff_nonempty] lemma is_bounded_top : is_bounded r ⊤ ↔ (∃t, ∀x, r x t) := by simp [is_bounded, eq_univ_iff_forall] lemma is_bounded_principal (s : set α) : is_bounded r (𝓟 s) ↔ (∃t, ∀x∈s, r x t) := by simp [is_bounded, subset_def] lemma is_bounded_sup [is_trans α r] (hr : ∀b₁ b₂, ∃b, r b₁ b ∧ r b₂ b) : is_bounded r f → is_bounded r g → is_bounded r (f ⊔ g) | ⟨b₁, h₁⟩ ⟨b₂, h₂⟩ := let ⟨b, rb₁b, rb₂b⟩ := hr b₁ b₂ in ⟨b, eventually_sup.mpr ⟨h₁.mono (λ x h, trans h rb₁b), h₂.mono (λ x h, trans h rb₂b)⟩⟩ lemma is_bounded.mono (h : f ≤ g) : is_bounded r g → is_bounded r f | ⟨b, hb⟩ := ⟨b, h hb⟩ lemma is_bounded_under.mono {f g : filter β} {u : β → α} (h : f ≤ g) : g.is_bounded_under r u → f.is_bounded_under r u := λ hg, hg.mono (map_mono h) lemma is_bounded_under.mono_le [preorder β] {l : filter α} {u v : α → β} (hu : is_bounded_under (≤) l u) (hv : v ≤ᶠ[l] u) : is_bounded_under (≤) l v := hu.imp $ λ b hb, (eventually_map.1 hb).mp $ hv.mono $ λ x, le_trans lemma is_bounded_under.mono_ge [preorder β] {l : filter α} {u v : α → β} (hu : is_bounded_under (≥) l u) (hv : u ≤ᶠ[l] v) : is_bounded_under (≥) l v := @is_bounded_under.mono_le α βᵒᵈ _ _ _ _ hu hv lemma is_bounded.is_bounded_under {q : β → β → Prop} {u : α → β} (hf : ∀a₀ a₁, r a₀ a₁ → q (u a₀) (u a₁)) : f.is_bounded r → f.is_bounded_under q u | ⟨b, h⟩ := ⟨u b, show ∀ᶠ x in f, q (u x) (u b), from h.mono (λ x, hf x b)⟩ lemma not_is_bounded_under_of_tendsto_at_top [preorder β] [no_max_order β] {f : α → β} {l : filter α} [l.ne_bot] (hf : tendsto f l at_top) : ¬ is_bounded_under (≤) l f := begin rintro ⟨b, hb⟩, rw eventually_map at hb, obtain ⟨b', h⟩ := exists_gt b, have hb' := (tendsto_at_top.mp hf) b', have : {x : α | f x ≤ b} ∩ {x : α | b' ≤ f x} = ∅ := eq_empty_of_subset_empty (λ x hx, (not_le_of_lt h) (le_trans hx.2 hx.1)), exact (nonempty_of_mem (hb.and hb')).ne_empty this end lemma not_is_bounded_under_of_tendsto_at_bot [preorder β] [no_min_order β] {f : α → β} {l : filter α} [l.ne_bot](hf : tendsto f l at_bot) : ¬ is_bounded_under (≥) l f := @not_is_bounded_under_of_tendsto_at_top α βᵒᵈ _ _ _ _ _ hf lemma is_bounded_under.bdd_above_range_of_cofinite [semilattice_sup β] {f : α → β} (hf : is_bounded_under (≤) cofinite f) : bdd_above (range f) := begin rcases hf with ⟨b, hb⟩, haveI : nonempty β := ⟨b⟩, rw [← image_univ, ← union_compl_self {x | f x ≤ b}, image_union, bdd_above_union], exact ⟨⟨b, ball_image_iff.2 $ λ x, id⟩, (hb.image f).bdd_above⟩ end lemma is_bounded_under.bdd_below_range_of_cofinite [semilattice_inf β] {f : α → β} (hf : is_bounded_under (≥) cofinite f) : bdd_below (range f) := @is_bounded_under.bdd_above_range_of_cofinite α βᵒᵈ _ _ hf lemma is_bounded_under.bdd_above_range [semilattice_sup β] {f : ℕ → β} (hf : is_bounded_under (≤) at_top f) : bdd_above (range f) := by { rw ← nat.cofinite_eq_at_top at hf, exact hf.bdd_above_range_of_cofinite } lemma is_bounded_under.bdd_below_range [semilattice_inf β] {f : ℕ → β} (hf : is_bounded_under (≥) at_top f) : bdd_below (range f) := @is_bounded_under.bdd_above_range βᵒᵈ _ _ hf /-- `is_cobounded (≺) f` states that the filter `f` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. There is a subtlety in this definition: we want `f.is_cobounded` to hold for any `f` in the case of complete lattices. This will be relevant to deduce theorems on complete lattices from their versions on conditionally complete lattices with additional assumptions. We have to be careful in the edge case of the trivial filter containing the empty set: the other natural definition `¬ ∀ a, ∀ᶠ n in f, a ≤ n` would not work as well in this case. -/ def is_cobounded (r : α → α → Prop) (f : filter α) := ∃b, ∀a, (∀ᶠ x in f, r x a) → r b a /-- `is_cobounded_under (≺) f u` states that the image of the filter `f` under the map `u` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. -/ def is_cobounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_cobounded r /-- To check that a filter is frequently bounded, it suffices to have a witness which bounds `f` at some point for every admissible set. This is only an implication, as the other direction is wrong for the trivial filter.-/ lemma is_cobounded.mk [is_trans α r] (a : α) (h : ∀s∈f, ∃x∈s, r a x) : f.is_cobounded r := ⟨a, assume y s, let ⟨x, h₁, h₂⟩ := h _ s in trans h₂ h₁⟩ /-- A filter which is eventually bounded is in particular frequently bounded (in the opposite direction). At least if the filter is not trivial. -/ lemma is_bounded.is_cobounded_flip [is_trans α r] [ne_bot f] : f.is_bounded r → f.is_cobounded (flip r) | ⟨a, ha⟩ := ⟨a, assume b hb, let ⟨x, rxa, rbx⟩ := (ha.and hb).exists in show r b a, from trans rbx rxa⟩ lemma is_bounded.is_cobounded_ge [preorder α] [ne_bot f] (h : f.is_bounded (≤)) : f.is_cobounded (≥) := h.is_cobounded_flip lemma is_bounded.is_cobounded_le [preorder α] [ne_bot f] (h : f.is_bounded (≥)) : f.is_cobounded (≤) := h.is_cobounded_flip lemma is_cobounded_bot : is_cobounded r ⊥ ↔ (∃b, ∀x, r b x) := by simp [is_cobounded] lemma is_cobounded_top : is_cobounded r ⊤ ↔ nonempty α := by simp [is_cobounded, eq_univ_iff_forall, exists_true_iff_nonempty] {contextual := tt} lemma is_cobounded_principal (s : set α) : (𝓟 s).is_cobounded r ↔ (∃b, ∀a, (∀x∈s, r x a) → r b a) := by simp [is_cobounded, subset_def] lemma is_cobounded.mono (h : f ≤ g) : f.is_cobounded r → g.is_cobounded r | ⟨b, hb⟩ := ⟨b, assume a ha, hb a (h ha)⟩ end relation lemma is_cobounded_le_of_bot [preorder α] [order_bot α] {f : filter α} : f.is_cobounded (≤) := ⟨⊥, assume a h, bot_le⟩ lemma is_cobounded_ge_of_top [preorder α] [order_top α] {f : filter α} : f.is_cobounded (≥) := ⟨⊤, assume a h, le_top⟩ lemma is_bounded_le_of_top [preorder α] [order_top α] {f : filter α} : f.is_bounded (≤) := ⟨⊤, eventually_of_forall $ λ _, le_top⟩ lemma is_bounded_ge_of_bot [preorder α] [order_bot α] {f : filter α} : f.is_bounded (≥) := ⟨⊥, eventually_of_forall $ λ _, bot_le⟩ @[simp] lemma _root_.order_iso.is_bounded_under_le_comp [preorder α] [preorder β] (e : α ≃o β) {l : filter γ} {u : γ → α} : is_bounded_under (≤) l (λ x, e (u x)) ↔ is_bounded_under (≤) l u := e.surjective.exists.trans $ exists_congr $ λ a, by simp only [eventually_map, e.le_iff_le] @[simp] lemma _root_.order_iso.is_bounded_under_ge_comp [preorder α] [preorder β] (e : α ≃o β) {l : filter γ} {u : γ → α} : is_bounded_under (≥) l (λ x, e (u x)) ↔ is_bounded_under (≥) l u := e.dual.is_bounded_under_le_comp @[simp, to_additive] lemma is_bounded_under_le_inv [ordered_comm_group α] {l : filter β} {u : β → α} : is_bounded_under (≤) l (λ x, (u x)⁻¹) ↔ is_bounded_under (≥) l u := (order_iso.inv α).is_bounded_under_ge_comp @[simp, to_additive] lemma is_bounded_under_ge_inv [ordered_comm_group α] {l : filter β} {u : β → α} : is_bounded_under (≥) l (λ x, (u x)⁻¹) ↔ is_bounded_under (≤) l u := (order_iso.inv α).is_bounded_under_le_comp lemma is_bounded_under.sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) u → f.is_bounded_under (≤) v → f.is_bounded_under (≤) (λa, u a ⊔ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≤ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≤ bv)⟩ := ⟨bu ⊔ bv, show ∀ᶠ x in f, u x ⊔ v x ≤ bu ⊔ bv, by filter_upwards [hu, hv] with _ using sup_le_sup⟩ @[simp] lemma is_bounded_under_le_sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) (λ a, u a ⊔ v a) ↔ f.is_bounded_under (≤) u ∧ f.is_bounded_under (≤) v := ⟨λ h, ⟨h.mono_le $ eventually_of_forall $ λ _, le_sup_left, h.mono_le $ eventually_of_forall $ λ _, le_sup_right⟩, λ h, h.1.sup h.2⟩ lemma is_bounded_under.inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) u → f.is_bounded_under (≥) v → f.is_bounded_under (≥) (λa, u a ⊓ v a) := @is_bounded_under.sup αᵒᵈ β _ _ _ _ @[simp] lemma is_bounded_under_ge_inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) (λ a, u a ⊓ v a) ↔ f.is_bounded_under (≥) u ∧ f.is_bounded_under (≥) v := @is_bounded_under_le_sup αᵒᵈ _ _ _ _ _ lemma is_bounded_under_le_abs [linear_ordered_add_comm_group α] {f : filter β} {u : β → α} : f.is_bounded_under (≤) (λ a, |u a|) ↔ f.is_bounded_under (≤) u ∧ f.is_bounded_under (≥) u := is_bounded_under_le_sup.trans $ and_congr iff.rfl is_bounded_under_le_neg /-- Filters are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `is_bounded_default` in the statements, in the form `(hf : f.is_bounded (≥) . is_bounded_default)`. -/ meta def is_bounded_default : tactic unit := tactic.applyc ``is_cobounded_le_of_bot <|> tactic.applyc ``is_cobounded_ge_of_top <|> tactic.applyc ``is_bounded_le_of_top <|> tactic.applyc ``is_bounded_ge_of_bot section conditionally_complete_lattice variables [conditionally_complete_lattice α] /-- The `Limsup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def Limsup (f : filter α) : α := Inf { a | ∀ᶠ n in f, n ≤ a } /-- The `Liminf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def Liminf (f : filter α) : α := Sup { a | ∀ᶠ n in f, a ≤ n } /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup (f : filter β) (u : β → α) : α := (f.map u).Limsup /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf (f : filter β) (u : β → α) : α := (f.map u).Liminf section variables {f : filter β} {u : β → α} theorem limsup_eq : f.limsup u = Inf { a | ∀ᶠ n in f, u n ≤ a } := rfl theorem liminf_eq : f.liminf u = Sup { a | ∀ᶠ n in f, a ≤ u n } := rfl end theorem Limsup_le_of_le {f : filter α} {a} (hf : f.is_cobounded (≤) . is_bounded_default) (h : ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ a := cInf_le hf h theorem le_Liminf_of_le {f : filter α} {a} (hf : f.is_cobounded (≥) . is_bounded_default) (h : ∀ᶠ n in f, a ≤ n) : a ≤ f.Liminf := le_cSup hf h theorem le_Limsup_of_le {f : filter α} {a} (hf : f.is_bounded (≤) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, n ≤ b) → a ≤ b) : a ≤ f.Limsup := le_cInf hf h theorem Liminf_le_of_le {f : filter α} {a} (hf : f.is_bounded (≥) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, b ≤ n) → b ≤ a) : f.Liminf ≤ a := cSup_le hf h theorem Liminf_le_Limsup {f : filter α} [ne_bot f] (h₁ : f.is_bounded (≤) . is_bounded_default) (h₂ : f.is_bounded (≥) . is_bounded_default) : f.Liminf ≤ f.Limsup := Liminf_le_of_le h₂ $ assume a₀ ha₀, le_Limsup_of_le h₁ $ assume a₁ ha₁, show a₀ ≤ a₁, from let ⟨b, hb₀, hb₁⟩ := (ha₀.and ha₁).exists in le_trans hb₀ hb₁ lemma Liminf_le_Liminf {f g : filter α} (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) (h : ∀ a, (∀ᶠ n in f, a ≤ n) → ∀ᶠ n in g, a ≤ n) : f.Liminf ≤ g.Liminf := cSup_le_cSup hg hf h lemma Limsup_le_Limsup {f g : filter α} (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) (h : ∀ a, (∀ᶠ n in g, n ≤ a) → ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ g.Limsup := cInf_le_cInf hf hg h lemma Limsup_le_Limsup_of_le {f g : filter α} (h : f ≤ g) (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) : f.Limsup ≤ g.Limsup := Limsup_le_Limsup hf hg (assume a ha, h ha) lemma Liminf_le_Liminf_of_le {f g : filter α} (h : g ≤ f) (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) : f.Liminf ≤ g.Liminf := Liminf_le_Liminf hf hg (assume a ha, h ha) lemma limsup_le_limsup {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : u ≤ᶠ[f] v) (hu : f.is_cobounded_under (≤) u . is_bounded_default) (hv : f.is_bounded_under (≤) v . is_bounded_default) : f.limsup u ≤ f.limsup v := Limsup_le_Limsup hu hv $ assume b, h.trans lemma liminf_le_liminf {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a ≤ v a) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hv : f.is_cobounded_under (≥) v . is_bounded_default) : f.liminf u ≤ f.liminf v := @limsup_le_limsup βᵒᵈ α _ _ _ _ h hv hu lemma limsup_le_limsup_of_le {α β} [conditionally_complete_lattice β] {f g : filter α} (h : f ≤ g) {u : α → β} (hf : f.is_cobounded_under (≤) u . is_bounded_default) (hg : g.is_bounded_under (≤) u . is_bounded_default) : f.limsup u ≤ g.limsup u := Limsup_le_Limsup_of_le (map_mono h) hf hg lemma liminf_le_liminf_of_le {α β} [conditionally_complete_lattice β] {f g : filter α} (h : g ≤ f) {u : α → β} (hf : f.is_bounded_under (≥) u . is_bounded_default) (hg : g.is_cobounded_under (≥) u . is_bounded_default) : f.liminf u ≤ g.liminf u := Liminf_le_Liminf_of_le (map_mono h) hf hg theorem Limsup_principal {s : set α} (h : bdd_above s) (hs : s.nonempty) : (𝓟 s).Limsup = Sup s := by simp [Limsup]; exact cInf_upper_bounds_eq_cSup h hs theorem Liminf_principal {s : set α} (h : bdd_below s) (hs : s.nonempty) : (𝓟 s).Liminf = Inf s := @Limsup_principal αᵒᵈ _ s h hs lemma limsup_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : limsup f u = limsup f v := begin rw limsup_eq, congr' with b, exact eventually_congr (h.mono $ λ x hx, by simp [hx]) end lemma liminf_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : liminf f u = liminf f v := @limsup_congr βᵒᵈ _ _ _ _ _ h lemma limsup_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : limsup f (λ x, b) = b := by simpa only [limsup_eq, eventually_const] using cInf_Ici lemma liminf_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : liminf f (λ x, b) = b := @limsup_const βᵒᵈ α _ f _ b lemma liminf_le_limsup {f : filter β} [ne_bot f] {u : β → α} (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : liminf f u ≤ limsup f u := Liminf_le_Limsup h h' end conditionally_complete_lattice section complete_lattice variables [complete_lattice α] @[simp] theorem Limsup_bot : (⊥ : filter α).Limsup = ⊥ := bot_unique $ Inf_le $ by simp @[simp] theorem Liminf_bot : (⊥ : filter α).Liminf = ⊤ := top_unique $ le_Sup $ by simp @[simp] theorem Limsup_top : (⊤ : filter α).Limsup = ⊤ := top_unique $ le_Inf $ by simp [eq_univ_iff_forall]; exact assume b hb, (top_unique $ hb _) @[simp] theorem Liminf_top : (⊤ : filter α).Liminf = ⊥ := bot_unique $ Sup_le $ by simp [eq_univ_iff_forall]; exact assume b hb, (bot_unique $ hb _) /-- Same as limsup_const applied to `⊥` but without the `ne_bot f` assumption -/ lemma limsup_const_bot {f : filter β} : limsup f (λ x : β, (⊥ : α)) = (⊥ : α) := begin rw [limsup_eq, eq_bot_iff], exact Inf_le (eventually_of_forall (λ x, le_rfl)), end /-- Same as limsup_const applied to `⊤` but without the `ne_bot f` assumption -/ lemma liminf_const_top {f : filter β} : liminf f (λ x : β, (⊤ : α)) = (⊤ : α) := @limsup_const_bot αᵒᵈ β _ _ theorem has_basis.Limsup_eq_infi_Sup {ι} {p : ι → Prop} {s} {f : filter α} (h : f.has_basis p s) : f.Limsup = ⨅ i (hi : p i), Sup (s i) := le_antisymm (le_infi₂ $ λ i hi, Inf_le $ h.eventually_iff.2 ⟨i, hi, λ x, le_Sup⟩) (le_Inf $ assume a ha, let ⟨i, hi, ha⟩ := h.eventually_iff.1 ha in infi₂_le_of_le _ hi $ Sup_le ha) theorem has_basis.Liminf_eq_supr_Inf {p : ι → Prop} {s : ι → set α} {f : filter α} (h : f.has_basis p s) : f.Liminf = ⨆ i (hi : p i), Inf (s i) := @has_basis.Limsup_eq_infi_Sup αᵒᵈ _ _ _ _ _ h theorem Limsup_eq_infi_Sup {f : filter α} : f.Limsup = ⨅ s ∈ f, Sup s := f.basis_sets.Limsup_eq_infi_Sup theorem Liminf_eq_supr_Inf {f : filter α} : f.Liminf = ⨆ s ∈ f, Inf s := @Limsup_eq_infi_Sup αᵒᵈ _ _ /-- In a complete lattice, the limsup of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem limsup_eq_infi_supr {f : filter β} {u : β → α} : f.limsup u = ⨅ s ∈ f, ⨆ a ∈ s, u a := (f.basis_sets.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] lemma limsup_eq_infi_supr_of_nat {u : ℕ → α} : limsup at_top u = ⨅ n : ℕ, ⨆ i ≥ n, u i := (at_top_basis.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, infi_const]; refl lemma limsup_eq_infi_supr_of_nat' {u : ℕ → α} : limsup at_top u = ⨅ n : ℕ, ⨆ i : ℕ, u (i + n) := by simp only [limsup_eq_infi_supr_of_nat, supr_ge_eq_supr_nat_add] theorem has_basis.limsup_eq_infi_supr {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : f.limsup u = ⨅ i (hi : p i), ⨆ a ∈ s i, u a := (h.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] /-- In a complete lattice, the liminf of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem liminf_eq_supr_infi {f : filter β} {u : β → α} : f.liminf u = ⨆ s ∈ f, ⨅ a ∈ s, u a := @limsup_eq_infi_supr αᵒᵈ β _ _ _ lemma liminf_eq_supr_infi_of_nat {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i ≥ n, u i := @limsup_eq_infi_supr_of_nat αᵒᵈ _ u lemma liminf_eq_supr_infi_of_nat' {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i : ℕ, u (i + n) := @limsup_eq_infi_supr_of_nat' αᵒᵈ _ _ theorem has_basis.liminf_eq_supr_infi {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : f.liminf u = ⨆ i (hi : p i), ⨅ a ∈ s i, u a := @has_basis.limsup_eq_infi_supr αᵒᵈ _ _ _ _ _ _ _ h @[simp] lemma liminf_nat_add (f : ℕ → α) (k : ℕ) : at_top.liminf (λ i, f (i + k)) = at_top.liminf f := by { simp_rw liminf_eq_supr_infi_of_nat, exact supr_infi_ge_nat_add f k } @[simp] lemma limsup_nat_add (f : ℕ → α) (k : ℕ) : at_top.limsup (λ i, f (i + k)) = at_top.limsup f := @liminf_nat_add αᵒᵈ _ f k lemma liminf_le_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, u a ≤ x) : f.liminf u ≤ x := begin rw liminf_eq, refine Sup_le (λ b hb, _), have hbx : ∃ᶠ a in f, b ≤ x, { revert h, rw [←not_imp_not, not_frequently, not_frequently], exact λ h, hb.mp (h.mono (λ a hbx hba hax, hbx (hba.trans hax))), }, exact hbx.exists.some_spec, end lemma le_limsup_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, x ≤ u a) : x ≤ f.limsup u := @liminf_le_of_frequently_le' _ βᵒᵈ _ _ _ _ h end complete_lattice section conditionally_complete_linear_order lemma eventually_lt_of_lt_liminf {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : b < liminf f u) (hu : f.is_bounded_under (≥) u . is_bounded_default) : ∀ᶠ a in f, b < u a := begin obtain ⟨c, hc, hbc⟩ : ∃ (c : β) (hc : c ∈ {c : β | ∀ᶠ (n : α) in f, c ≤ u n}), b < c := exists_lt_of_lt_cSup hu h, exact hc.mono (λ x hx, lt_of_lt_of_le hbc hx) end lemma eventually_lt_of_limsup_lt {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : limsup f u < b) (hu : f.is_bounded_under (≤) u . is_bounded_default) : ∀ᶠ a in f, u a < b := @eventually_lt_of_lt_liminf _ βᵒᵈ _ _ _ _ h hu lemma le_limsup_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, b ≤ u x) (hu : f.is_bounded_under (≤) u . is_bounded_default) : b ≤ f.limsup u := begin revert hu_le, rw [←not_imp_not, not_frequently], simp_rw ←lt_iff_not_ge, exact λ h, eventually_lt_of_limsup_lt h hu, end lemma liminf_le_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, u x ≤ b) (hu : f.is_bounded_under (≥) u . is_bounded_default) : f.liminf u ≤ b := @le_limsup_of_frequently_le _ βᵒᵈ _ f u b hu_le hu lemma frequently_lt_of_lt_limsup {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu : f.is_cobounded_under (≤) u . is_bounded_default) (h : b < f.limsup u) : ∃ᶠ x in f, b < u x := begin contrapose! h, apply Limsup_le_of_le hu, simpa using h, end lemma frequently_lt_of_liminf_lt {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu : f.is_cobounded_under (≥) u . is_bounded_default) (h : f.liminf u < b) : ∃ᶠ x in f, u x < b := @frequently_lt_of_lt_limsup _ βᵒᵈ _ f u b hu h end conditionally_complete_linear_order end filter section order open filter lemma monotone.is_bounded_under_le_comp [nonempty β] [linear_order β] [preorder γ] [no_max_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : monotone g) (hg' : tendsto g at_top at_top) : is_bounded_under (≤) l (g ∘ f) ↔ is_bounded_under (≤) l f := begin refine ⟨_, λ h, h.is_bounded_under hg⟩, rintro ⟨c, hc⟩, rw eventually_map at hc, obtain ⟨b, hb⟩ : ∃ b, ∀ a ≥ b, c < g a := eventually_at_top.1 (hg'.eventually_gt_at_top c), exact ⟨b, hc.mono $ λ x hx, not_lt.1 (λ h, (hb _ h.le).not_le hx)⟩ end lemma monotone.is_bounded_under_ge_comp [nonempty β] [linear_order β] [preorder γ] [no_min_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : monotone g) (hg' : tendsto g at_bot at_bot) : is_bounded_under (≥) l (g ∘ f) ↔ is_bounded_under (≥) l f := hg.dual.is_bounded_under_le_comp hg' lemma antitone.is_bounded_under_le_comp [nonempty β] [linear_order β] [preorder γ] [no_max_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : antitone g) (hg' : tendsto g at_bot at_top) : is_bounded_under (≤) l (g ∘ f) ↔ is_bounded_under (≥) l f := hg.dual_right.is_bounded_under_ge_comp hg' lemma antitone.is_bounded_under_ge_comp [nonempty β] [linear_order β] [preorder γ] [no_min_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : antitone g) (hg' : tendsto g at_top at_bot) : is_bounded_under (≥) l (g ∘ f) ↔ is_bounded_under (≤) l f := hg.dual_right.is_bounded_under_le_comp hg' lemma galois_connection.l_limsup_le [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {v : α → β} {l : β → γ} {u : γ → β} (gc : galois_connection l u) (hlv : f.is_bounded_under (≤) (λ x, l (v x)) . is_bounded_default) (hv_co : f.is_cobounded_under (≤) v . is_bounded_default) : l (f.limsup v) ≤ f.limsup (λ x, l (v x)) := begin refine le_Limsup_of_le hlv (λ c hc, _), rw filter.eventually_map at hc, simp_rw (gc _ _) at hc ⊢, exact Limsup_le_of_le hv_co hc, end lemma order_iso.limsup_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≤) u . is_bounded_default) (hu_co : f.is_cobounded_under (≤) u . is_bounded_default) (hgu : f.is_bounded_under (≤) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≤) (λ x, g (u x)) . is_bounded_default) : g (f.limsup u) = f.limsup (λ x, g (u x)) := begin refine le_antisymm (g.to_galois_connection.l_limsup_le hgu hu_co) _, rw [←(g.symm.symm_apply_apply (f.limsup (λ (x : α), g (u x)))), g.symm_symm], refine g.monotone _, have hf : u = λ i, g.symm (g (u i)), from funext (λ i, (g.symm_apply_apply (u i)).symm), nth_rewrite 0 hf, refine g.symm.to_galois_connection.l_limsup_le _ hgu_co, simp_rw g.symm_apply_apply, exact hu, end lemma order_iso.liminf_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hu_co : f.is_cobounded_under (≥) u . is_bounded_default) (hgu : f.is_bounded_under (≥) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≥) (λ x, g (u x)) . is_bounded_default) : g (f.liminf u) = f.liminf (λ x, g (u x)) := @order_iso.limsup_apply α βᵒᵈ γᵒᵈ _ _ f u g.dual hu hu_co hgu hgu_co end order
f05da301105a1076ca0983627ed28c2ce9251d0b
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/graded_object.lean
394d339fe27a8831284320a122e333a748db9914
[ "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
6,806
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 data.int.basic import algebra.group_power.lemmas import category_theory.pi.basic import category_theory.shift import category_theory.concrete_category.basic /-! # The category of graded objects For any type `β`, a `β`-graded object over some category `C` is just a function `β → C` into the objects of `C`. We put the "pointwise" category structure on these, as the non-dependent specialization of `category_theory.pi`. We describe the `comap` functors obtained by precomposing with functions `β → γ`. As a consequence a fixed element (e.g. `1`) in an additive group `β` provides a shift functor on `β`-graded objects When `C` has coproducts we construct the `total` functor `graded_object β C ⥤ C`, show that it is faithful, and deduce that when `C` is concrete so is `graded_object β C`. -/ open category_theory.pi open category_theory.limits namespace category_theory universes w v u /-- A type synonym for `β → C`, used for `β`-graded objects in a category `C`. -/ def graded_object (β : Type w) (C : Type u) : Type (max w u) := β → C -- Satisfying the inhabited linter... instance inhabited_graded_object (β : Type w) (C : Type u) [inhabited C] : inhabited (graded_object β C) := ⟨λ b, inhabited.default⟩ /-- A type synonym for `β → C`, used for `β`-graded objects in a category `C` with a shift functor given by translation by `s`. -/ @[nolint unused_arguments] -- `s` is here to distinguish type synonyms asking for different shifts abbreviation graded_object_with_shift {β : Type w} [add_comm_group β] (s : β) (C : Type u) : Type (max w u) := graded_object β C namespace graded_object variables {C : Type u} [category.{v} C] instance category_of_graded_objects (β : Type w) : category.{(max w v)} (graded_object β C) := category_theory.pi (λ _, C) /-- The projection of a graded object to its `i`-th component. -/ @[simps] def eval {β : Type w} (b : β) : graded_object β C ⥤ C := { obj := λ X, X b, map := λ X Y f, f b, } section variable (C) /-- The natural isomorphism comparing between pulling back along two propositionally equal functions. -/ @[simps] def comap_eq {β γ : Type w} {f g : β → γ} (h : f = g) : comap (λ _, C) f ≅ comap (λ _, C) g := { hom := { app := λ X b, eq_to_hom begin dsimp [comap], subst h, end }, inv := { app := λ X b, eq_to_hom begin dsimp [comap], subst h, end }, } lemma comap_eq_symm {β γ : Type w} {f g : β → γ} (h : f = g) : comap_eq C h.symm = (comap_eq C h).symm := by tidy lemma comap_eq_trans {β γ : Type w} {f g h : β → γ} (k : f = g) (l : g = h) : comap_eq C (k.trans l) = comap_eq C k ≪≫ comap_eq C l := begin ext X b, simp, end @[simp] lemma eq_to_hom_apply {β : Type w} {X Y : Π b : β, C} (h : X = Y) (b : β) : (eq_to_hom h : X ⟶ Y) b = eq_to_hom (by subst h) := by { subst h, refl } /-- The equivalence between β-graded objects and γ-graded objects, given an equivalence between β and γ. -/ @[simps] def comap_equiv {β γ : Type w} (e : β ≃ γ) : (graded_object β C) ≌ (graded_object γ C) := { functor := comap (λ _, C) (e.symm : γ → β), inverse := comap (λ _, C) (e : β → γ), counit_iso := (comap_comp (λ _, C) _ _).trans (comap_eq C (by { ext, simp } )), unit_iso := (comap_eq C (by { ext, simp } )).trans (comap_comp _ _ _).symm, functor_unit_iso_comp' := λ X, by { ext b, dsimp, simp, }, } -- See note [dsimp, simp]. end instance has_shift {β : Type*} [add_comm_group β] (s : β) : has_shift (graded_object_with_shift s C) ℤ := has_shift_mk _ _ { F := λ n, comap (λ _, C) $ λ (b : β), b + n • s, ε := (comap_id β (λ _, C)).symm ≪≫ (comap_eq C (by { ext, simp })), μ := λ m n, comap_comp _ _ _ ≪≫ comap_eq C (by { ext, simp [add_zsmul, add_comm] }), left_unitality := by { introv, ext, dsimp, simpa }, right_unitality := by { introv, ext, dsimp, simpa }, associativity := by { introv, ext, dsimp, simp } } @[simp] lemma shift_functor_obj_apply {β : Type*} [add_comm_group β] (s : β) (X : β → C) (t : β) (n : ℤ) : (shift_functor (graded_object_with_shift s C) n).obj X t = X (t + n • s) := rfl @[simp] lemma shift_functor_map_apply {β : Type*} [add_comm_group β] (s : β) {X Y : graded_object_with_shift s C} (f : X ⟶ Y) (t : β) (n : ℤ) : (shift_functor (graded_object_with_shift s C) n).map f t = f (t + n • s) := rfl instance has_zero_morphisms [has_zero_morphisms C] (β : Type w) : has_zero_morphisms.{(max w v)} (graded_object β C) := { has_zero := λ X Y, { zero := λ b, 0 } } @[simp] lemma zero_apply [has_zero_morphisms C] (β : Type w) (X Y : graded_object β C) (b : β) : (0 : X ⟶ Y) b = 0 := rfl section open_locale zero_object instance has_zero_object [has_zero_object C] [has_zero_morphisms C] (β : Type w) : has_zero_object.{(max w v)} (graded_object β C) := by { refine ⟨⟨λ b, 0, λ X, ⟨⟨⟨λ b, 0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨λ b, 0⟩, λ f, _⟩⟩⟩⟩; ext, } end end graded_object namespace graded_object -- The universes get a little hairy here, so we restrict the universe level for the grading to 0. -- Since we're typically interested in grading by ℤ or a finite group, this should be okay. -- If you're grading by things in higher universes, have fun! variables (β : Type) variables (C : Type u) [category.{v} C] variables [has_coproducts C] /-- The total object of a graded object is the coproduct of the graded components. -/ noncomputable def total : graded_object β C ⥤ C := { obj := λ X, ∐ (λ i : ulift.{v} β, X i.down), map := λ X Y f, limits.sigma.map (λ i, f i.down) }. variables [has_zero_morphisms C] /-- The `total` functor taking a graded object to the coproduct of its graded components is faithful. To prove this, we need to know that the coprojections into the coproduct are monomorphisms, which follows from the fact we have zero morphisms and decidable equality for the grading. -/ instance : faithful (total β C) := { map_injective' := λ X Y f g w, begin classical, ext i, replace w := sigma.ι (λ i : ulift.{v} β, X i.down) ⟨i⟩ ≫= w, erw [colimit.ι_map, colimit.ι_map] at w, exact mono.right_cancellation _ _ w, end } end graded_object namespace graded_object noncomputable theory variables (β : Type) variables (C : Type (u+1)) [large_category C] [concrete_category C] [has_coproducts C] [has_zero_morphisms C] instance : concrete_category (graded_object β C) := { forget := total β C ⋙ forget C } instance : has_forget₂ (graded_object β C) C := { forget₂ := total β C } end graded_object end category_theory
6d60024d11e64c43d416d0d15611a3ad69bd0117
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/doc/demo/subst.lean
a13a42f69bde86b61e0b4e8ef42c0bc23b4ad762
[ "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
229
lean
variable q : Nat → Bool variable f : Nat → Nat → Nat theorem T1 (a b : Nat) (H1 : a = b) (H2 : q (f (f a a) (f a a))) : q (f (f a b) (f a a)) := subst H2 H1 check @subst set_option pp::implicit true print environment 1
6733505344d11dbc08f94e7dc34256b285a3ed0d
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/eval_expr_partial.lean
f02925ed454865e851cd1eda4192743c11eacd88
[ "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
75
lean
open tactic run_cmd to_expr ``(bit0 1) >>= eval_expr nat >>= tactic.trace
d61d0d921faf26cf2836f1dfac8639dbfc57af58
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/13_More_Tactics.org.3.lean
01a5c70347ed7a02a96789bd06dc8447340a0695
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
579
lean
import standard import data.finset data.nat open finset nat variables (A : Type) [deceqA : decidable_eq A] include deceqA -- BEGIN theorem card_add_card (s₁ s₂ : finset A) : card s₁ + card s₂ = card (s₁ ∪ s₂) + card (s₁ ∩ s₂) := begin induction s₂ using finset.induction with a s₂ hs₂ ih, show card s₁ + card (∅:finset A) = card (s₁ ∪ ∅) + card (s₁ ∩ ∅), by rewrite [union_empty, card_empty, inter_empty], show card s₁ + card (insert a s₂) = card (s₁ ∪ (insert a s₂)) + card (s₁ ∩ (insert a s₂)), from sorry end -- END
c6dd042820ac9bf02d8c794560aa90654f87ce81
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/tests/tactics.lean
9543b67e3bae653cc242606923ced478328ea86a
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
15,021
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic data.set.lattice data.prod tactic.rewrite section solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := begin apply_assumption, apply_assumption, end example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y := by solve_by_elim example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y := begin apply_assumption, end end solve_by_elim section tauto₀ variables p q r : Prop variables h : p ∧ q ∨ p ∧ r include h example : p ∧ p := by tauto end tauto₀ section tauto₁ variables α : Type variables p q r : α → Prop variables h : (∃ x, p x ∧ q x) ∨ (∃ x, p x ∧ r x) include h example : ∃ x, p x := by tauto end tauto₁ section tauto₂ variables α : Type variables x : α variables p q r : α → Prop variables h₀ : (∀ x, p x → q x → r x) ∨ r x variables h₁ : p x variables h₂ : q x include h₀ h₁ h₂ example : ∃ x, r x := by tauto end tauto₂ section tauto₃ example (p : Prop) : p ∧ true ↔ p := by tauto example (p : Prop) : p ∨ false ↔ p := by tauto example (p q r : Prop) [decidable p] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q r : Prop) [decidable q] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ p) : q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : p) : ¬ q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : q) : ¬ p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) : p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) (h'' : ¬ p) : false := by tauto example (p q r : Prop) [decidable q] [decidable p] (h : p ↔ q) (h' : r ↔ q) (h'' : ¬ r) : ¬ p := by tauto example (p q r : Prop) [decidable q] [decidable p] (h : p ↔ q) (h' : r ↔ q) : p ↔ r := by tauto example (p q r : Prop) [decidable p] [decidable q] [decidable r] (h : ¬ p = q) (h' : r = q) : p ↔ ¬ r := by tauto section modulo_symmetry variables {p q r : Prop} {α : Type} {x y : α} [decidable_eq α] variables [decidable p] [decidable q] [decidable r] variables (h : x = y) variables (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q)) include h include h'' example (h' : ¬ y = x) : p ∧ q := by tauto example (h' : p ∧ ¬ y = x) : p ∧ q := by tauto example : y = x := by tauto example (h' : ¬ x = y) : p ∧ q := by tauto example : x = y := by tauto end modulo_symmetry end tauto₃ section wlog example {x y : ℕ} (a : x = 1) : true := begin suffices : false, trivial, wlog h : x = y, { guard_target x = y ∨ y = x, admit }, { guard_hyp h := x = y, guard_hyp a := x = 1, admit } end example {x y : ℕ} : true := begin suffices : false, trivial, wlog h : x ≤ y, { guard_hyp h := x ≤ y, guard_target false, admit } end example {x y z : ℕ} : true := begin suffices : false, trivial, wlog h : x ≤ y + z, { guard_target x ≤ y + z ∨ x ≤ z + y, admit }, { guard_hyp h := x ≤ y + z, guard_target false, admit } end example {x y z : ℕ} : true := begin suffices : false, trivial, wlog : x ≤ y + z using x y, { guard_target x ≤ y + z ∨ y ≤ x + z, admit }, { guard_hyp case := x ≤ y + z, guard_target false, admit }, end example {x : ℕ} (S₀ S₁ : set ℕ) (P : ℕ → Prop) (h : x ∈ S₀ ∪ S₁) : true := begin suffices : false, trivial, wlog h' : x ∈ S₀ using S₀ S₁, { guard_target x ∈ S₀ ∨ x ∈ S₁, admit }, { guard_hyp h := x ∈ S₀ ∪ S₁, guard_hyp h' := x ∈ S₀, admit } end example {n m i : ℕ} {p : ℕ → ℕ → ℕ → Prop} : true := begin suffices : false, trivial, wlog : p n m i using [n m i, n i m, i n m], { guard_target p n m i ∨ p n i m ∨ p i n m, admit }, { guard_hyp case := p n m i, admit } end example {n m i : ℕ} {p : ℕ → Prop} : true := begin suffices : false, trivial, wlog : p n using [n m i, m n i, i n m], { guard_target p n ∨ p m ∨ p i, admit }, { guard_hyp case := p n, admit } end example {n m i : ℕ} {p : ℕ → ℕ → Prop} {q : ℕ → ℕ → ℕ → Prop} : true := begin suffices : q n m i, trivial, have h : p n i ∨ p i m ∨ p m i, from sorry, wlog : p n i := h using n m i, { guard_hyp h := p n i, guard_target q n m i, admit }, { guard_hyp h := p i m, guard_hyp this := q i m n, guard_target q n m i, admit }, { guard_hyp h := p m i, guard_hyp this := q m i n, guard_target q n m i, admit }, end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, cases hyp, wlog x_in : x ∈ B using B C, { assumption }, { exact or.inl ⟨hyp_left, x_in⟩ } }, { intro hyp, wlog x_in : x ∈ A ∩ B using B C, { assumption }, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, wlog x_in : x ∈ B := hyp.2 using B C, { exact or.inl ⟨hyp.1, x_in⟩ } }, { intro hyp, wlog x_in : x ∈ A ∩ B := hyp using B C, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, cases hyp, wlog x_in : x ∈ B := hyp_right using B C, { exact or.inl ⟨hyp_left, x_in⟩ }, }, { intro hyp, wlog x_in : x ∈ A ∩ B := hyp using B C, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end end wlog example (m n p q : nat) (h : m + n = p) : true := begin have : m + n = q, { generalize_hyp h' : m + n = x at h, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target m + n = q, admit }, have : m + n = q, { generalize_hyp h' : m + n = x at h ⊢, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target x = q, admit }, trivial end example (α : Sort*) (L₁ L₂ L₃ : list α) (H : L₁ ++ L₂ = L₃) : true := begin have : L₁ ++ L₂ = L₂, { generalize_hyp h : L₁ ++ L₂ = L at H, induction L with hd tl ih, case list.nil { tactic.cleanup, change list.nil = L₃ at H, admit }, case list.cons { change hd :: tl = L₃ at H, admit } }, trivial end section convert open set variables {α β : Type} local attribute [simp] private lemma singleton_inter_singleton_eq_empty {x y : α} : ({x} ∩ {y} = (∅ : set α)) ↔ x ≠ y := by simp [singleton_inter_eq_empty] example {f : β → α} {x y : α} (h : x ≠ y) : f ⁻¹' {x} ∩ f ⁻¹' {y} = ∅ := begin have : {x} ∩ {y} = (∅ : set α) := by simpa using h, convert preimage_empty, rw [←preimage_inter,this], end end convert section rcases universe u variables {α β γ : Type u} example (x : α × β × γ) : true := begin rcases x with ⟨a, b, c⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : α × β × γ) : true := begin rcases x with ⟨a, ⟨b, c⟩⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : (α × β) × γ) : true := begin rcases x with ⟨⟨a, b⟩, c⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : inhabited α × option β ⊕ γ) : true := begin rcases x with ⟨⟨a⟩, _ | b⟩ | c, { guard_hyp a := α, trivial }, { guard_hyp a := α, guard_hyp b := β, trivial }, { guard_hyp c := γ, trivial } end example (x y : ℕ) (h : x = y) : true := begin rcases x with _|⟨⟩|z, { guard_hyp h := nat.zero = y, trivial }, { guard_hyp h := nat.succ nat.zero = y, trivial }, { guard_hyp z := ℕ, guard_hyp h := z.succ.succ = y, trivial }, end -- from equiv.sum_empty example (s : α ⊕ empty) : true := begin rcases s with _ | ⟨⟨⟩⟩, { guard_hyp s := α, trivial } end end rcases section ext example (x y : ℕ) : true := begin have : x = y, { ext <|> admit }, have : x = y, { ext i <|> admit }, have : x = y, { ext 1 <|> admit }, trivial end example (X Y : ℕ × ℕ) (h : X.1 = Y.1) (h : X.2 = Y.2) : X = Y := begin ext ; assumption end example (X Y : (ℕ → ℕ) × ℕ) (h : ∀ i, X.1 i = Y.1 i) (h : X.2 = Y.2) : X = Y := begin ext x ; solve_by_elim, end example (X Y : ℕ → ℕ × ℕ) (h : ∀ i, X i = Y i) : true := begin have : X = Y, { ext 1 with i, guard_target X i = Y i, admit }, have : X = Y, { ext i, guard_target (X i).fst = (Y i).fst, admit, guard_target (X i).snd = (Y i).snd, admit, }, have : X = Y, { ext 1, guard_target X x = Y x, admit }, trivial, end def my_foo {α} (x : semigroup α) (y : group α) : true := trivial example {α : Type} : true := begin have : true, { refine_struct (@my_foo α { .. } { .. } ), -- 9 goals guard_tags _field mul semigroup, admit, -- case semigroup, mul -- α : Type -- ⊢ α → α → α guard_tags _field mul_assoc semigroup, admit, -- case semigroup, mul_assoc -- α : Type -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) guard_tags _field mul group, admit, -- case group, mul -- α : Type -- ⊢ α → α → α guard_tags _field mul_assoc group, admit, -- case group, mul_assoc -- α : Type -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) guard_tags _field one group, admit, -- case group, one -- α : Type -- ⊢ α guard_tags _field one_mul group, admit, -- case group, one_mul -- α : Type -- ⊢ ∀ (a : α), 1 * a = a guard_tags _field mul_one group, admit, -- case group, mul_one -- α : Type -- ⊢ ∀ (a : α), a * 1 = a guard_tags _field inv group, admit, -- case group, inv -- α : Type -- ⊢ α → α guard_tags _field mul_left_inv group, admit, -- case group, mul_left_inv -- α : Type -- ⊢ ∀ (a : α), a⁻¹ * a = 1 }, trivial end def my_bar {α} (x : semigroup α) (y : group α) (i j : α) : α := i example {α : Type} : true := begin have : monoid α, { refine_struct { mul := my_bar { .. } { .. } }, guard_tags _field mul semigroup, admit, guard_tags _field mul_assoc semigroup, admit, guard_tags _field mul group, admit, guard_tags _field mul_assoc group, admit, guard_tags _field one group, admit, guard_tags _field one_mul group, admit, guard_tags _field mul_one group, admit, guard_tags _field inv group, admit, guard_tags _field mul_left_inv group, admit, guard_tags _field mul_assoc monoid, admit, guard_tags _field one monoid, admit, guard_tags _field one_mul monoid, admit, guard_tags _field mul_one monoid, admit, }, trivial end end ext section apply_rules example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [mono_rules] example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules mono_rules end apply_rules section h_generalize variables {α β γ φ ψ : Type} (f : α → α → α → φ → γ) (x y : α) (a b : β) (z : φ) (h₀ : β = α) (h₁ : β = α) (h₂ : φ = β) (hx : x == a) (hy : y == b) (hz : z == a) include f x y z a b hx hy hz example : f x y x z = f (eq.rec_on h₀ a) (cast h₀ b) (eq.mpr h₁.symm a) (eq.mpr h₂ a) := begin guard_hyp_nums 16, h_generalize hp : a == p with hh, guard_hyp_nums 19, guard_hyp' hh := β = α, guard_target f x y x z = f p (cast h₀ b) p (eq.mpr h₂ a), h_generalize hq : _ == q, guard_hyp_nums 21, guard_target f x y x z = f p q p (eq.mpr h₂ a), h_generalize _ : _ == r, guard_hyp_nums 23, guard_target f x y x z = f p q p r, casesm* [_ == _, _ = _], refl end end h_generalize section h_generalize variables {α β γ φ ψ : Type} (f : list α → list α → γ) (x : list α) (a : list β) (z : φ) (h₀ : β = α) (h₁ : list β = list α) (hx : x == a) include f x z a hx h₀ h₁ example : true := begin have : f x x = f (eq.rec_on h₀ a) (cast h₁ a), { guard_hyp_nums 11, h_generalize : a == p with _, guard_hyp_nums 13, guard_hyp' h := β = α, guard_target f x x = f p (cast h₁ a), h_generalize! : a == q , guard_hyp_nums 13, guard_target ∀ q, f x x = f p q, casesm* [_ == _, _ = _], success_if_fail { refl }, admit }, trivial end end h_generalize section assoc_rw open tactic example : ∀ x y z a b c : ℕ, true := begin intros, have : x + (y + z) = 3 + y, admit, have : a + (b + x) + y + (z + b + c) ≤ 0, (do this ← get_local `this, tgt ← to_expr ```(a + (b + x) + y + (z + b + c)), assoc ← mk_mapp ``add_monoid.add_assoc [`(ℕ),none], (l,p) ← assoc_rewrite_intl assoc this tgt, note `h none p ), erw h, guard_target a + b + 3 + y + b + c ≤ 0, admit, trivial end example : ∀ x y z a b c : ℕ, true := begin intros, have : ∀ y, x + (y + z) = 3 + y, admit, have : a + (b + x) + y + (z + b + c) ≤ 0, (do this ← get_local `this, tgt ← to_expr ```(a + (b + x) + y + (z + b + c)), assoc_rewrite_target this ), guard_target a + b + 3 + y + b + c ≤ 0, admit, trivial end variables x y z a b c : ℕ variables h₀ : ∀ (y : ℕ), x + (y + z) = 3 + y variables h₁ : a + (b + x) + y + (z + b + a) ≤ 0 variables h₂ : y + b + c = y + b + a include h₀ h₁ h₂ example : a + (b + x) + y + (z + b + c) ≤ 0 := by { assoc_rw [h₀,h₂] at *, guard_hyp _inst := is_associative ℕ has_add.add, -- keep a local instance of is_associative to cache -- type class queries exact h₁ } end assoc_rw
040544597a75edf488ab96914d3fd266311892ff
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/multiset/range_auto.lean
39e879c5273378275ae8e53e81c666e06347d208
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,130
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.multiset.basic import Mathlib.data.list.range import Mathlib.PostPort namespace Mathlib namespace multiset /- range -/ /-- `range n` is the multiset lifted from the list `range n`, that is, the set `{0, 1, ..., n-1}`. -/ def range (n : ℕ) : multiset ℕ := ↑(list.range n) @[simp] theorem range_zero : range 0 = 0 := rfl @[simp] theorem range_succ (n : ℕ) : range (Nat.succ n) = n ::ₘ range n := sorry @[simp] theorem card_range (n : ℕ) : coe_fn card (range n) = n := list.length_range n theorem range_subset {m : ℕ} {n : ℕ} : range m ⊆ range n ↔ m ≤ n := list.range_subset @[simp] theorem mem_range {m : ℕ} {n : ℕ} : m ∈ range n ↔ m < n := list.mem_range @[simp] theorem not_mem_range_self {n : ℕ} : ¬n ∈ range n := list.not_mem_range_self theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := list.self_mem_range_succ n end Mathlib
cd6dc9596c4ea5f123b6a28d632549b3d879ecff
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/limits/cones.lean
bc04d5549954f1b7da45c85f58fc3f88c388b336
[ "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
13,269
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, Floris van Doorn -/ import category_theory.const import category_theory.yoneda universes v u u' -- declare the `v`'s first; see `category_theory.category` for an explanation open category_theory -- There is an awkward difficulty with universes here. -- If we allowed `J` to be a small category in `Prop`, we'd run into trouble -- because `yoneda.obj (F : (J ⥤ C)ᵒᵖ)` will be a functor into `Sort (max v 1)`, -- not into `Sort v`. -- So we don't allow this case; it's not particularly useful anyway. variables {J : Type v} [small_category J] variables {C : Type u} [category.{v} C] open category_theory open category_theory.category open category_theory.functor open opposite namespace category_theory namespace functor variables {J C} (F : J ⥤ C) /-- `F.cones` is the functor assigning to an object `X` the type of natural transformations from the constant functor with value `X` to `F`. An object representing this functor is a limit of `F`. -/ def cones : Cᵒᵖ ⥤ Type v := (const J).op ⋙ (yoneda.obj F) lemma cones_obj (X : Cᵒᵖ) : F.cones.obj X = ((const J).obj (unop X) ⟶ F) := rfl @[simp] lemma cones_map_app {X₁ X₂ : Cᵒᵖ} (f : X₁ ⟶ X₂) (t : F.cones.obj X₁) (j : J) : (F.cones.map f t).app j = f.unop ≫ t.app j := rfl /-- `F.cocones` is the functor assigning to an object `X` the type of natural transformations from `F` to the constant functor with value `X`. An object corepresenting this functor is a colimit of `F`. -/ def cocones : C ⥤ Type v := const J ⋙ coyoneda.obj (op F) lemma cocones_obj (X : C) : F.cocones.obj X = (F ⟶ (const J).obj X) := rfl @[simp] lemma cocones_map_app {X₁ X₂ : C} (f : X₁ ⟶ X₂) (t : F.cocones.obj X₁) (j : J) : (F.cocones.map f t).app j = t.app j ≫ f := rfl end functor section variables (J C) /-- Functorially associated to each functor `J ⥤ C`, we have the `C`-presheaf consisting of cones with a given cone point. -/ @[simps] def cones : (J ⥤ C) ⥤ (Cᵒᵖ ⥤ Type v) := { obj := functor.cones, map := λ F G f, whisker_left (const J).op (yoneda.map f) } /-- Contravariantly associated to each functor `J ⥤ C`, we have the `C`-copresheaf consisting of cocones with a given cocone point. -/ @[simps] def cocones : (J ⥤ C)ᵒᵖ ⥤ (C ⥤ Type v) := { obj := λ F, functor.cocones (unop F), map := λ F G f, whisker_left (const J) (coyoneda.map f) } end namespace limits /-- A `c : cone F` is: * an object `c.X` and * a natural transformation `c.π : c.X ⟶ F` from the constant `c.X` functor to `F`. `cone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cones.obj X`. -/ structure cone (F : J ⥤ C) := (X : C) (π : (const J).obj X ⟶ F) @[simp] lemma cone.w {F : J ⥤ C} (c : cone F) {j j' : J} (f : j ⟶ j') : c.π.app j ≫ F.map f = c.π.app j' := by convert ←(c.π.naturality f).symm; apply id_comp /-- A `c : cocone F` is * an object `c.X` and * a natural transformation `c.ι : F ⟶ c.X` from `F` to the constant `c.X` functor. `cocone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cocones.obj X`. -/ structure cocone (F : J ⥤ C) := (X : C) (ι : F ⟶ (const J).obj X) @[simp] lemma cocone.w {F : J ⥤ C} (c : cocone F) {j j' : J} (f : j ⟶ j') : F.map f ≫ c.ι.app j' = c.ι.app j := by convert ←(c.ι.naturality f); apply comp_id variables {F : J ⥤ C} namespace cone def equiv (F : J ⥤ C) : cone F ≅ Σ X, F.cones.obj X := { hom := λ c, ⟨op c.X, c.π⟩, inv := λ c, { X := unop c.1, π := c.2 }, hom_inv_id' := begin ext, cases x, refl, end, inv_hom_id' := begin ext, cases x, refl, end } @[simp] def extensions (c : cone F) : yoneda.obj c.X ⟶ F.cones := { app := λ X f, ((const J).map f) ≫ c.π } /-- A map to the vertex of a cone induces a cone by composition. -/ @[simp] def extend (c : cone F) {X : C} (f : X ⟶ c.X) : cone F := { X := X, π := c.extensions.app (op X) f } @[simp] lemma extend_π (c : cone F) {X : Cᵒᵖ} (f : unop X ⟶ c.X) : (extend c f).π = c.extensions.app X f := rfl @[simps] def whisker {K : Type v} [small_category K] (E : K ⥤ J) (c : cone F) : cone (E ⋙ F) := { X := c.X, π := whisker_left E c.π } end cone namespace cocone def equiv (F : J ⥤ C) : cocone F ≅ Σ X, F.cocones.obj X := { hom := λ c, ⟨c.X, c.ι⟩, inv := λ c, { X := c.1, ι := c.2 }, hom_inv_id' := begin ext, cases x, refl, end, inv_hom_id' := begin ext, cases x, refl, end } @[simp] def extensions (c : cocone F) : coyoneda.obj (op c.X) ⟶ F.cocones := { app := λ X f, c.ι ≫ (const J).map f } /-- A map from the vertex of a cocone induces a cocone by composition. -/ @[simp] def extend (c : cocone F) {X : C} (f : c.X ⟶ X) : cocone F := { X := X, ι := c.extensions.app X f } @[simp] lemma extend_ι (c : cocone F) {X : C} (f : c.X ⟶ X) : (extend c f).ι = c.extensions.app X f := rfl @[simps] def whisker {K : Type v} [small_category K] (E : K ⥤ J) (c : cocone F) : cocone (E ⋙ F) := { X := c.X, ι := whisker_left E c.ι } end cocone @[ext] structure cone_morphism (A B : cone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, hom ≫ B.π.app j = A.π.app j . obviously) restate_axiom cone_morphism.w' attribute [simp] cone_morphism.w @[simps] instance cone.category : category.{v} (cone F) := { hom := λ A B, cone_morphism A B, comp := λ X Y Z f g, { hom := f.hom ≫ g.hom, w' := by intro j; rw [assoc, g.w, f.w] }, id := λ B, { hom := 𝟙 B.X } } namespace cones /-- To give an isomorphism between cones, it suffices to give an isomorphism between their vertices which commutes with the cone maps. -/ @[ext, simps] def ext {c c' : cone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.π.app j = φ.hom ≫ c'.π.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.inv_comp_eq.mpr (w j) } } @[simps] def postcompose {G : J ⥤ C} (α : F ⟶ G) : cone F ⥤ cone G := { obj := λ c, { X := c.X, π := c.π ≫ α }, map := λ c₁ c₂ f, { hom := f.hom, w' := by intro; erw ← category.assoc; simp [-category.assoc] } } def postcompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : postcompose (α ≫ β) ≅ postcompose α ⋙ postcompose β := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) def postcompose_id : postcompose (𝟙 F) ≅ 𝟭 (cone F) := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) @[simps] def postcompose_equivalence {G : J ⥤ C} (α : F ≅ G) : cone F ≌ cone G := { functor := postcompose α.hom, inverse := postcompose α.inv, unit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) } section variable (F) @[simps] def forget : cone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables {D : Type u'} [category.{v} D] @[simps] def functoriality (G : C ⥤ D) : cone F ⥤ cone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, π := { app := λ j, G.map (A.π.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ X Y f, { hom := G.map f.hom, w' := by intros; rw [←functor.map_comp, f.w] } } end end cones @[ext] structure cocone_morphism (A B : cocone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, A.ι.app j ≫ hom = B.ι.app j . obviously) restate_axiom cocone_morphism.w' attribute [simp] cocone_morphism.w @[simps] instance cocone.category : category.{v} (cocone F) := { hom := λ A B, cocone_morphism A B, comp := λ _ _ _ f g, { hom := f.hom ≫ g.hom, w' := by intro j; rw [←assoc, f.w, g.w] }, id := λ B, { hom := 𝟙 B.X } } namespace cocones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[ext, simps] def ext {c c' : cocone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.ι.app j ≫ φ.hom = c'.ι.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.comp_inv_eq.mpr (w j).symm } } @[simps] def precompose {G : J ⥤ C} (α : G ⟶ F) : cocone F ⥤ cocone G := { obj := λ c, { X := c.X, ι := α ≫ c.ι }, map := λ c₁ c₂ f, { hom := f.hom } } def precompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : precompose (α ≫ β) ≅ precompose β ⋙ precompose α := by { fapply nat_iso.of_components, { intro s, fapply ext, refl, obviously }, obviously } def precompose_id : precompose (𝟙 F) ≅ 𝟭 (cocone F) := by { fapply nat_iso.of_components, { intro s, fapply ext, refl, obviously }, obviously } def precompose_equivalence {G : J ⥤ C} (α : G ≅ F) : cocone F ≌ cocone G := begin refine equivalence.mk (precompose α.hom) (precompose α.inv) _ _, { symmetry, refine (precompose_comp _ _).symm.trans _, rw [iso.inv_hom_id], exact precompose_id }, { refine (precompose_comp _ _).symm.trans _, rw [iso.hom_inv_id], exact precompose_id } end section variable (F) @[simps] def forget : cocone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables {D : Type u'} [category.{v} D] @[simps] def functoriality (G : C ⥤ D) : cocone F ⥤ cocone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, ι := { app := λ j, G.map (A.ι.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ _ _ f, { hom := G.map f.hom, w' := by intros; rw [←functor.map_comp, cocone_morphism.w] } } end end cocones end limits namespace functor variables {D : Type u'} [category.{v} D] variables {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D) open category_theory.limits /-- The image of a cone in C under a functor G : C ⥤ D is a cone in D. -/ def map_cone (c : cone F) : cone (F ⋙ H) := (cones.functoriality F H).obj c /-- The image of a cocone in C under a functor G : C ⥤ D is a cocone in D. -/ def map_cocone (c : cocone F) : cocone (F ⋙ H) := (cocones.functoriality F H).obj c @[simp] lemma map_cone_X (c : cone F) : (H.map_cone c).X = H.obj c.X := rfl @[simp] lemma map_cocone_X (c : cocone F) : (H.map_cocone c).X = H.obj c.X := rfl @[simps] def map_cone_inv [is_equivalence H] (c : cone (F ⋙ H)) : cone F := let t := (inv H).map_cone c in let α : (F ⋙ H) ⋙ inv H ⟶ F := ((whisker_left F is_equivalence.unit_iso.inv) : F ⋙ (H ⋙ inv H) ⟶ _) ≫ (functor.right_unitor _).hom in { X := t.X, π := ((category_theory.cones J C).map α).app (op t.X) t.π } def map_cone_morphism {c c' : cone F} (f : c ⟶ c') : (H.map_cone c) ⟶ (H.map_cone c') := (cones.functoriality F H).map f def map_cocone_morphism {c c' : cocone F} (f : c ⟶ c') : (H.map_cocone c) ⟶ (H.map_cocone c') := (cocones.functoriality F H).map f @[simp] lemma map_cone_π (c : cone F) (j : J) : (map_cone H c).π.app j = H.map (c.π.app j) := rfl @[simp] lemma map_cocone_ι (c : cocone F) (j : J) : (map_cocone H c).ι.app j = H.map (c.ι.app j) := rfl /-- `map_cone` is the left inverse to `map_cone_inv`. -/ def map_cone_map_cone_inv {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cone (F ⋙ H)) : map_cone H (map_cone_inv H c) ≅ c := begin apply cones.ext _ (λ j, _), { exact H.inv_fun_id.app c.X }, { dsimp, erw [comp_id, ← H.inv_fun_id.hom.naturality (c.π.app j), comp_map, H.map_comp], congr' 1, erw [← cancel_epi (H.inv_fun_id.inv.app (H.obj (F.obj j))), nat_iso.inv_hom_id_app, ← (functor.as_equivalence H).functor_unit _, ← H.map_comp, nat_iso.hom_inv_id_app, H.map_id], refl } end end functor end category_theory namespace category_theory.limits variables {F : J ⥤ Cᵒᵖ} -- Here and below we only automatically generate the `@[simp]` lemma for the `X` field, -- as we can be a simpler `rfl` lemma for the components of the natural transformation by hand. @[simps X] def cone_of_cocone_left_op (c : cocone F.left_op) : cone F := { X := op c.X, π := nat_trans.right_op (c.ι ≫ (const.op_obj_unop (op c.X)).hom) } @[simp] lemma cone_of_cocone_left_op_π_app (c : cocone F.left_op) (j) : (cone_of_cocone_left_op c).π.app j = (c.ι.app (op j)).op := by { dsimp [cone_of_cocone_left_op], simp } @[simps X] def cocone_left_op_of_cone (c : cone F) : cocone (F.left_op) := { X := unop c.X, ι := nat_trans.left_op c.π } @[simp] lemma cocone_left_op_of_cone_ι_app (c : cone F) (j) : (cocone_left_op_of_cone c).ι.app j = (c.π.app (unop j)).unop := by { dsimp [cocone_left_op_of_cone], simp } @[simps X] def cocone_of_cone_left_op (c : cone F.left_op) : cocone F := { X := op c.X, ι := nat_trans.right_op ((const.op_obj_unop (op c.X)).hom ≫ c.π) } @[simp] lemma cocone_of_cone_left_op_ι_app (c : cone F.left_op) (j) : (cocone_of_cone_left_op c).ι.app j = (c.π.app (op j)).op := by { dsimp [cocone_of_cone_left_op], simp } @[simps X] def cone_left_op_of_cocone (c : cocone F) : cone (F.left_op) := { X := unop c.X, π := nat_trans.left_op c.ι } @[simp] lemma cone_left_op_of_cocone_π_app (c : cocone F) (j) : (cone_left_op_of_cocone c).π.app j = (c.ι.app (unop j)).unop := by { dsimp [cone_left_op_of_cocone], simp } end category_theory.limits
62a97480add94b25ca07221c9c3c7e84becc11da
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/vset.lean
a387154492cc9b3176e088f8448ab2f1aaad8c76
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
2,620
lean
/- This files contains the `vset` type class definition and instances. -/ import data.fresh universes u v namespace acie ----------------------------------------------------------------- /- The type class `vset V S` defines the requirements for a finite set `S` of variable names allocated from the infinite set of variable names `V` with decidable equality. -/ class vset (S : Type u → Type v) (V : Type u) [decidable_eq V] extends has_fresh V S , has_emptyc (S V) , has_insert V (S V) , has_subset (S V) := (prop_insert_self : ∀ (a : V) (s : S V), a ∈ insert a s) (prop_insert : ∀ {a : V} (b : V) {s : S V}, a ∈ s → a ∈ insert b s) (prop_insert_idem : ∀ (a : V) (s : S V), insert a (insert a s) = insert a s) (prop_insert_comm : ∀ (a b : V) (s : S V), insert a (insert b s) = insert b (insert a s)) (prop_rm_insert_if_ne : ∀ {a b : V} {s : S V}, b ∈ insert a s → b ≠ a → b ∈ s) (prop_mem_of_subset : ∀ {a : V} {s₁ s₂ : S V}, s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂) (prop_ne_if_mem_and_not_mem : ∀ {a b : V} {s : S V}, a ∈ s → b ∉ s → a ≠ b) (prop_subset_refl : ∀ (s : S V), s ⊆ s) (prop_subset_trans : ∀ {s₁ s₂ s₃ : S V}, s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃) (prop_insert_of_subset : ∀ (a : V) {s₁ s₂ : S V}, s₁ ⊆ s₂ → insert a s₁ ⊆ insert a s₂) (prop_subset_insert_self : ∀ (a : V) (s : S V), s ⊆ insert a s) namespace vset ----------------------------------------------------------------- variables {V : Type} [decidable_eq V] -- Type of variable names variables {S : Type → Type} [vset S V] -- Type of variable name sets variables {a b : V} -- Variable names -- Make a new constraint for another free variable set if the given variable -- name equals the inserted one. theorem prop_insert_self_if_eq (s : S V) (p : a = b) : a ∈ insert b s := by rw [p]; exact vset.prop_insert_self b s theorem prop_subset_of_insert_comm (a b : V) (s : S V) : insert a (insert b s) ⊆ insert b (insert a s) := calc insert a (insert b s) = insert b (insert a s) : prop_insert_comm a b s ... ⊆ insert b (insert a s) : prop_subset_refl (insert b (insert a s)) theorem prop_subset_of_insert_comm_refl (a : V) (s : S V) : prop_subset_of_insert_comm a a s = prop_subset_refl (insert a (insert a s)) := rfl end /- namespace -/ vset ------------------------------------------------------- end /- namespace -/ acie -------------------------------------------------------
0cca2374bcfcbfbc9e4303fe097e6eae09679f89
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/module/finite_dimension.lean
fe65f4c3628e89db1e3929bef697d70580f96a10
[ "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
22,268
lean
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Anatole Dedecker -/ import analysis.locally_convex.balanced_core_hull import linear_algebra.free_module.finite.matrix import topology.algebra.module.simple import topology.algebra.module.determinant /-! # Finite dimensional topological vector spaces over complete fields > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Let `𝕜` be a complete nontrivially normed field, and `E` a topological vector space (TVS) over `𝕜` (i.e we have `[add_comm_group E] [module 𝕜 E] [topological_space E] [topological_add_group E]` and `[has_continuous_smul 𝕜 E]`). If `E` is finite dimensional and Hausdorff, then all linear maps from `E` to any other TVS are continuous. When `E` is a normed space, this gets us the equivalence of norms in finite dimension. ## Main results : * `linear_map.continuous_iff_is_closed_ker` : a linear form is continuous if and only if its kernel is closed. * `linear_map.continuous_of_finite_dimensional` : a linear map on a finite-dimensional Hausdorff space over a complete field is continuous. ## TODO Generalize more of `analysis/normed_space/finite_dimension` to general TVSs. ## Implementation detail The main result from which everything follows is the fact that, if `ξ : ι → E` is a finite basis, then `ξ.equiv_fun : E →ₗ (ι → 𝕜)` is continuous. However, for technical reasons, it is easier to prove this when `ι` and `E` live ine the same universe. So we start by doing that as a private lemma, then we deduce `linear_map.continuous_of_finite_dimensional` from it, and then the general result follows as `continuous_equiv_fun_basis`. -/ universes u v w x noncomputable theory open set finite_dimensional topological_space filter open_locale big_operators section field variables {𝕜 E F : Type*} [field 𝕜] [topological_space 𝕜] [add_comm_group E] [module 𝕜 E] [topological_space E] [add_comm_group F] [module 𝕜 F] [topological_space F] [topological_add_group F] [has_continuous_smul 𝕜 F] /-- The space of continuous linear maps between finite-dimensional spaces is finite-dimensional. -/ instance [finite_dimensional 𝕜 E] [finite_dimensional 𝕜 F] : finite_dimensional 𝕜 (E →L[𝕜] F) := finite_dimensional.of_injective (continuous_linear_map.coe_lm 𝕜 : (E →L[𝕜] F) →ₗ[𝕜] (E →ₗ[𝕜] F)) continuous_linear_map.coe_injective end field section normed_field variables {𝕜 : Type u} [hnorm : nontrivially_normed_field 𝕜] {E : Type v} [add_comm_group E] [module 𝕜 E] [topological_space E] [topological_add_group E] [has_continuous_smul 𝕜 E] {F : Type w} [add_comm_group F] [module 𝕜 F] [topological_space F] [topological_add_group F] [has_continuous_smul 𝕜 F] {F' : Type x} [add_comm_group F'] [module 𝕜 F'] [topological_space F'] [topological_add_group F'] [has_continuous_smul 𝕜 F'] include hnorm /-- If `𝕜` is a nontrivially normed field, any T2 topology on `𝕜` which makes it a topological vector space over itself (with the norm topology) is *equal* to the norm topology. -/ lemma unique_topology_of_t2 {t : topological_space 𝕜} (h₁ : @topological_add_group 𝕜 t _) (h₂ : @has_continuous_smul 𝕜 𝕜 _ hnorm.to_uniform_space.to_topological_space t) (h₃ : @t2_space 𝕜 t) : t = hnorm.to_uniform_space.to_topological_space := begin -- Let `𝓣₀` denote the topology on `𝕜` induced by the norm, and `𝓣` be any T2 vector -- topology on `𝕜`. To show that `𝓣₀ = 𝓣`, it suffices to show that they have the same -- neighborhoods of 0. refine topological_add_group.ext h₁ infer_instance (le_antisymm _ _), { -- To show `𝓣 ≤ 𝓣₀`, we have to show that closed balls are `𝓣`-neighborhoods of 0. rw metric.nhds_basis_closed_ball.ge_iff, -- Let `ε > 0`. Since `𝕜` is nontrivially normed, we have `0 < ‖ξ₀‖ < ε` for some `ξ₀ : 𝕜`. intros ε hε, rcases normed_field.exists_norm_lt 𝕜 hε with ⟨ξ₀, hξ₀, hξ₀ε⟩, -- Since `ξ₀ ≠ 0` and `𝓣` is T2, we know that `{ξ₀}ᶜ` is a `𝓣`-neighborhood of 0. have : {ξ₀}ᶜ ∈ @nhds 𝕜 t 0 := is_open.mem_nhds is_open_compl_singleton (ne.symm $ norm_ne_zero_iff.mp hξ₀.ne.symm), -- Thus, its balanced core `𝓑` is too. Let's show that the closed ball of radius `ε` contains -- `𝓑`, which will imply that the closed ball is indeed a `𝓣`-neighborhood of 0. have : balanced_core 𝕜 {ξ₀}ᶜ ∈ @nhds 𝕜 t 0 := balanced_core_mem_nhds_zero this, refine mem_of_superset this (λ ξ hξ, _), -- Let `ξ ∈ 𝓑`. We want to show `‖ξ‖ < ε`. If `ξ = 0`, this is trivial. by_cases hξ0 : ξ = 0, { rw hξ0, exact metric.mem_closed_ball_self hε.le }, { rw [mem_closed_ball_zero_iff], -- Now suppose `ξ ≠ 0`. By contradiction, let's assume `ε < ‖ξ‖`, and show that -- `ξ₀ ∈ 𝓑 ⊆ {ξ₀}ᶜ`, which is a contradiction. by_contra' h, suffices : (ξ₀ * ξ⁻¹) • ξ ∈ balanced_core 𝕜 {ξ₀}ᶜ, { rw [smul_eq_mul 𝕜, mul_assoc, inv_mul_cancel hξ0, mul_one] at this, exact not_mem_compl_iff.mpr (mem_singleton ξ₀) ((balanced_core_subset _) this) }, -- For that, we use that `𝓑` is balanced : since `‖ξ₀‖ < ε < ‖ξ‖`, we have `‖ξ₀ / ξ‖ ≤ 1`, -- hence `ξ₀ = (ξ₀ / ξ) • ξ ∈ 𝓑` because `ξ ∈ 𝓑`. refine (balanced_core_balanced _).smul_mem _ hξ, rw [norm_mul, norm_inv, mul_inv_le_iff (norm_pos_iff.mpr hξ0), mul_one], exact (hξ₀ε.trans h).le } }, { -- Finally, to show `𝓣₀ ≤ 𝓣`, we simply argue that `id = (λ x, x • 1)` is continuous from -- `(𝕜, 𝓣₀)` to `(𝕜, 𝓣)` because `(•) : (𝕜, 𝓣₀) × (𝕜, 𝓣) → (𝕜, 𝓣)` is continuous. calc (@nhds 𝕜 hnorm.to_uniform_space.to_topological_space 0) = map id (@nhds 𝕜 hnorm.to_uniform_space.to_topological_space 0) : map_id.symm ... = map (λ x, id x • 1) (@nhds 𝕜 hnorm.to_uniform_space.to_topological_space 0) : by conv_rhs {congr, funext, rw [smul_eq_mul, mul_one]}; refl ... ≤ (@nhds 𝕜 t ((0 : 𝕜) • 1)) : @tendsto.smul_const _ _ _ hnorm.to_uniform_space.to_topological_space t _ _ _ _ _ tendsto_id (1 : 𝕜) ... = (@nhds 𝕜 t 0) : by rw zero_smul } end /-- Any linear form on a topological vector space over a nontrivially normed field is continuous if its kernel is closed. -/ lemma linear_map.continuous_of_is_closed_ker (l : E →ₗ[𝕜] 𝕜) (hl : is_closed (l.ker : set E)) : continuous l := begin -- `l` is either constant or surjective. If it is constant, the result is trivial. by_cases H : finrank 𝕜 l.range = 0, { rw [finrank_eq_zero, linear_map.range_eq_bot] at H, rw H, exact continuous_zero }, { -- In the case where `l` is surjective, we factor it as `φ : (E ⧸ l.ker) ≃ₗ[𝕜] 𝕜`. Note that -- `E ⧸ l.ker` is T2 since `l.ker` is closed. have : finrank 𝕜 l.range = 1, from le_antisymm (finrank_self 𝕜 ▸ l.range.finrank_le) (zero_lt_iff.mpr H), have hi : function.injective (l.ker.liftq l (le_refl _)), { rw [← linear_map.ker_eq_bot], exact submodule.ker_liftq_eq_bot _ _ _ (le_refl _) }, have hs : function.surjective (l.ker.liftq l (le_refl _)), { rw [← linear_map.range_eq_top, submodule.range_liftq], exact eq_top_of_finrank_eq ((finrank_self 𝕜).symm ▸ this) }, let φ : (E ⧸ l.ker) ≃ₗ[𝕜] 𝕜 := linear_equiv.of_bijective (l.ker.liftq l (le_refl _)) ⟨hi, hs⟩, have hlφ : (l : E → 𝕜) = φ ∘ l.ker.mkq, by ext; refl, -- Since the quotient map `E →ₗ[𝕜] (E ⧸ l.ker)` is continuous, the continuity of `l` will follow -- form the continuity of `φ`. suffices : continuous φ.to_equiv, { rw hlφ, exact this.comp continuous_quot_mk }, -- The pullback by `φ.symm` of the quotient topology is a T2 topology on `𝕜`, because `φ.symm` -- is injective. Since `φ.symm` is linear, it is also a vector space topology. -- Hence, we know that it is equal to the topology induced by the norm. have : induced φ.to_equiv.symm infer_instance = hnorm.to_uniform_space.to_topological_space, { refine unique_topology_of_t2 (topological_add_group_induced φ.symm.to_linear_map) (has_continuous_smul_induced φ.symm.to_linear_map) _, rw t2_space_iff, exact λ x y hxy, @separated_by_continuous _ _ (induced _ _) _ _ _ continuous_induced_dom _ _ (φ.to_equiv.symm.injective.ne hxy) }, -- Finally, the pullback by `φ.symm` is exactly the pushforward by `φ`, so we have to prove -- that `φ` is continuous when `𝕜` is endowed with the pushforward by `φ` of the quotient -- topology, which is trivial by definition of the pushforward. rw [this.symm, equiv.induced_symm], exact continuous_coinduced_rng } end /-- Any linear form on a topological vector space over a nontrivially normed field is continuous if and only if its kernel is closed. -/ lemma linear_map.continuous_iff_is_closed_ker (l : E →ₗ[𝕜] 𝕜) : continuous l ↔ is_closed (l.ker : set E) := ⟨λ h, is_closed_singleton.preimage h, l.continuous_of_is_closed_ker⟩ /-- Over a nontrivially normed field, any linear form which is nonzero on a nonempty open set is automatically continuous. -/ lemma linear_map.continuous_of_nonzero_on_open (l : E →ₗ[𝕜] 𝕜) (s : set E) (hs₁ : is_open s) (hs₂ : s.nonempty) (hs₃ : ∀ x ∈ s, l x ≠ 0) : continuous l := begin refine l.continuous_of_is_closed_ker (l.is_closed_or_dense_ker.resolve_right $ λ hl, _), rcases hs₂ with ⟨x, hx⟩, have : x ∈ interior (l.ker : set E)ᶜ, { rw mem_interior_iff_mem_nhds, exact mem_of_superset (hs₁.mem_nhds hx) hs₃ }, rwa hl.interior_compl at this end variables [complete_space 𝕜] /-- This version imposes `ι` and `E` to live in the same universe, so you should instead use `continuous_equiv_fun_basis` which gives the same result without universe restrictions. -/ private lemma continuous_equiv_fun_basis_aux [ht2 : t2_space E] {ι : Type v} [fintype ι] (ξ : basis ι 𝕜 E) : continuous ξ.equiv_fun := begin letI : uniform_space E := topological_add_group.to_uniform_space E, letI : uniform_add_group E := topological_add_comm_group_is_uniform, letI : separated_space E := separated_iff_t2.mpr ht2, unfreezingI { induction hn : fintype.card ι with n IH generalizing ι E }, { rw fintype.card_eq_zero_iff at hn, exact continuous_of_const (λ x y, funext hn.elim) }, { haveI : finite_dimensional 𝕜 E := of_fintype_basis ξ, -- first step: thanks to the induction hypothesis, any n-dimensional subspace is equivalent -- to a standard space of dimension n, hence it is complete and therefore closed. have H₁ : ∀s : submodule 𝕜 E, finrank 𝕜 s = n → is_closed (s : set E), { assume s s_dim, letI : uniform_add_group s := s.to_add_subgroup.uniform_add_group, let b := basis.of_vector_space 𝕜 s, have U : uniform_embedding b.equiv_fun.symm.to_equiv, { have : fintype.card (basis.of_vector_space_index 𝕜 s) = n, by { rw ← s_dim, exact (finrank_eq_card_basis b).symm }, have : continuous b.equiv_fun := IH b this, exact b.equiv_fun.symm.uniform_embedding b.equiv_fun.symm.to_linear_map.continuous_on_pi this }, have : is_complete (s : set E), from complete_space_coe_iff_is_complete.1 ((complete_space_congr U).1 (by apply_instance)), exact this.is_closed }, -- second step: any linear form is continuous, as its kernel is closed by the first step have H₂ : ∀f : E →ₗ[𝕜] 𝕜, continuous f, { assume f, by_cases H : finrank 𝕜 f.range = 0, { rw [finrank_eq_zero, linear_map.range_eq_bot] at H, rw H, exact continuous_zero }, { have : finrank 𝕜 f.ker = n, { have Z := f.finrank_range_add_finrank_ker, rw [finrank_eq_card_basis ξ, hn] at Z, have : finrank 𝕜 f.range = 1, from le_antisymm (finrank_self 𝕜 ▸ f.range.finrank_le) (zero_lt_iff.mpr H), rw [this, add_comm, nat.add_one] at Z, exact nat.succ.inj Z }, have : is_closed (f.ker : set E), from H₁ _ this, exact linear_map.continuous_of_is_closed_ker f this } }, rw continuous_pi_iff, intros i, change continuous (ξ.coord i), exact H₂ (ξ.coord i) }, end /-- Any linear map on a finite dimensional space over a complete field is continuous. -/ theorem linear_map.continuous_of_finite_dimensional [t2_space E] [finite_dimensional 𝕜 E] (f : E →ₗ[𝕜] F') : continuous f := begin -- for the proof, go to a model vector space `b → 𝕜` thanks to `continuous_equiv_fun_basis`, and -- argue that all linear maps there are continuous. let b := basis.of_vector_space 𝕜 E, have A : continuous b.equiv_fun := continuous_equiv_fun_basis_aux b, have B : continuous (f.comp (b.equiv_fun.symm : (basis.of_vector_space_index 𝕜 E → 𝕜) →ₗ[𝕜] E)) := linear_map.continuous_on_pi _, have : continuous ((f.comp (b.equiv_fun.symm : (basis.of_vector_space_index 𝕜 E → 𝕜) →ₗ[𝕜] E)) ∘ b.equiv_fun) := B.comp A, convert this, ext x, dsimp, rw [basis.equiv_fun_symm_apply, basis.sum_repr] end instance linear_map.continuous_linear_map_class_of_finite_dimensional [t2_space E] [finite_dimensional 𝕜 E] : continuous_linear_map_class (E →ₗ[𝕜] F') 𝕜 E F' := { map_continuous := λ f, f.continuous_of_finite_dimensional, ..linear_map.semilinear_map_class } /-- In finite dimensions over a non-discrete complete normed field, the canonical identification (in terms of a basis) with `𝕜^n` (endowed with the product topology) is continuous. This is the key fact wich makes all linear maps from a T2 finite dimensional TVS over such a field continuous (see `linear_map.continuous_of_finite_dimensional`), which in turn implies that all norms are equivalent in finite dimensions. -/ theorem continuous_equiv_fun_basis [t2_space E] {ι : Type*} [fintype ι] (ξ : basis ι 𝕜 E) : continuous ξ.equiv_fun := begin haveI : finite_dimensional 𝕜 E := of_fintype_basis ξ, exact ξ.equiv_fun.to_linear_map.continuous_of_finite_dimensional end namespace linear_map variables [t2_space E] [finite_dimensional 𝕜 E] /-- The continuous linear map induced by a linear map on a finite dimensional space -/ def to_continuous_linear_map : (E →ₗ[𝕜] F') ≃ₗ[𝕜] E →L[𝕜] F' := { to_fun := λ f, ⟨f, f.continuous_of_finite_dimensional⟩, inv_fun := coe, map_add' := λ f g, rfl, map_smul' := λ c f, rfl, left_inv := λ f, rfl, right_inv := λ f, continuous_linear_map.coe_injective rfl } @[simp] lemma coe_to_continuous_linear_map' (f : E →ₗ[𝕜] F') : ⇑f.to_continuous_linear_map = f := rfl @[simp] lemma coe_to_continuous_linear_map (f : E →ₗ[𝕜] F') : (f.to_continuous_linear_map : E →ₗ[𝕜] F') = f := rfl @[simp] lemma coe_to_continuous_linear_map_symm : ⇑(to_continuous_linear_map : (E →ₗ[𝕜] F') ≃ₗ[𝕜] E →L[𝕜] F').symm = coe := rfl @[simp] lemma det_to_continuous_linear_map (f : E →ₗ[𝕜] E) : f.to_continuous_linear_map.det = f.det := rfl @[simp] lemma ker_to_continuous_linear_map (f : E →ₗ[𝕜] F') : ker f.to_continuous_linear_map = ker f := rfl @[simp] lemma range_to_continuous_linear_map (f : E →ₗ[𝕜] F') : range f.to_continuous_linear_map = range f := rfl /-- A surjective linear map `f` with finite dimensional codomain is an open map. -/ lemma is_open_map_of_finite_dimensional (f : F →ₗ[𝕜] E) (hf : function.surjective f) : is_open_map f := begin rcases f.exists_right_inverse_of_surjective (linear_map.range_eq_top.2 hf) with ⟨g, hg⟩, refine is_open_map.of_sections (λ x, ⟨λ y, g (y - f x) + x, _, _, λ y, _⟩), { exact ((g.continuous_of_finite_dimensional.comp $ continuous_id.sub continuous_const).add continuous_const).continuous_at }, { rw [sub_self, map_zero, zero_add] }, { simp only [map_sub, map_add, ← comp_apply f g, hg, id_apply, sub_add_cancel] } end instance can_lift_continuous_linear_map : can_lift (E →ₗ[𝕜] F) (E →L[𝕜] F) coe (λ _, true) := ⟨λ f _, ⟨f.to_continuous_linear_map, rfl⟩⟩ end linear_map section variables [t2_space E] [t2_space F] [finite_dimensional 𝕜 E] namespace linear_equiv /-- The continuous linear equivalence induced by a linear equivalence on a finite dimensional space. -/ def to_continuous_linear_equiv (e : E ≃ₗ[𝕜] F) : E ≃L[𝕜] F := { continuous_to_fun := e.to_linear_map.continuous_of_finite_dimensional, continuous_inv_fun := begin haveI : finite_dimensional 𝕜 F := e.finite_dimensional, exact e.symm.to_linear_map.continuous_of_finite_dimensional end, ..e } @[simp] lemma coe_to_continuous_linear_equiv (e : E ≃ₗ[𝕜] F) : (e.to_continuous_linear_equiv : E →ₗ[𝕜] F) = e := rfl @[simp] lemma coe_to_continuous_linear_equiv' (e : E ≃ₗ[𝕜] F) : (e.to_continuous_linear_equiv : E → F) = e := rfl @[simp] lemma coe_to_continuous_linear_equiv_symm (e : E ≃ₗ[𝕜] F) : (e.to_continuous_linear_equiv.symm : F →ₗ[𝕜] E) = e.symm := rfl @[simp] lemma coe_to_continuous_linear_equiv_symm' (e : E ≃ₗ[𝕜] F) : (e.to_continuous_linear_equiv.symm : F → E) = e.symm := rfl @[simp] lemma to_linear_equiv_to_continuous_linear_equiv (e : E ≃ₗ[𝕜] F) : e.to_continuous_linear_equiv.to_linear_equiv = e := by { ext x, refl } @[simp] lemma to_linear_equiv_to_continuous_linear_equiv_symm (e : E ≃ₗ[𝕜] F) : e.to_continuous_linear_equiv.symm.to_linear_equiv = e.symm := by { ext x, refl } instance can_lift_continuous_linear_equiv : can_lift (E ≃ₗ[𝕜] F) (E ≃L[𝕜] F) continuous_linear_equiv.to_linear_equiv (λ _, true) := ⟨λ f _, ⟨_, f.to_linear_equiv_to_continuous_linear_equiv⟩⟩ end linear_equiv variable [finite_dimensional 𝕜 F] /-- Two finite-dimensional topological vector spaces over a complete normed field are continuously linearly equivalent if they have the same (finite) dimension. -/ theorem finite_dimensional.nonempty_continuous_linear_equiv_of_finrank_eq (cond : finrank 𝕜 E = finrank 𝕜 F) : nonempty (E ≃L[𝕜] F) := (nonempty_linear_equiv_of_finrank_eq cond).map linear_equiv.to_continuous_linear_equiv /-- Two finite-dimensional topological vector spaces over a complete normed field are continuously linearly equivalent if and only if they have the same (finite) dimension. -/ theorem finite_dimensional.nonempty_continuous_linear_equiv_iff_finrank_eq : nonempty (E ≃L[𝕜] F) ↔ finrank 𝕜 E = finrank 𝕜 F := ⟨ λ ⟨h⟩, h.to_linear_equiv.finrank_eq, λ h, finite_dimensional.nonempty_continuous_linear_equiv_of_finrank_eq h ⟩ /-- A continuous linear equivalence between two finite-dimensional topological vector spaces over a complete normed field of the same (finite) dimension. -/ def continuous_linear_equiv.of_finrank_eq (cond : finrank 𝕜 E = finrank 𝕜 F) : E ≃L[𝕜] F := (linear_equiv.of_finrank_eq E F cond).to_continuous_linear_equiv end namespace basis variables {ι : Type*} [fintype ι] [t2_space E] /-- Construct a continuous linear map given the value at a finite basis. -/ def constrL (v : basis ι 𝕜 E) (f : ι → F) : E →L[𝕜] F := by haveI : finite_dimensional 𝕜 E := finite_dimensional.of_fintype_basis v; exact (v.constr 𝕜 f).to_continuous_linear_map @[simp, norm_cast] lemma coe_constrL (v : basis ι 𝕜 E) (f : ι → F) : (v.constrL f : E →ₗ[𝕜] F) = v.constr 𝕜 f := rfl /-- The continuous linear equivalence between a vector space over `𝕜` with a finite basis and functions from its basis indexing type to `𝕜`. -/ def equiv_funL (v : basis ι 𝕜 E) : E ≃L[𝕜] (ι → 𝕜) := { continuous_to_fun := begin haveI : finite_dimensional 𝕜 E := finite_dimensional.of_fintype_basis v, exact v.equiv_fun.to_linear_map.continuous_of_finite_dimensional, end, continuous_inv_fun := begin change continuous v.equiv_fun.symm.to_fun, exact v.equiv_fun.symm.to_linear_map.continuous_of_finite_dimensional, end, ..v.equiv_fun } @[simp] lemma constrL_apply (v : basis ι 𝕜 E) (f : ι → F) (e : E) : (v.constrL f) e = ∑ i, (v.equiv_fun e i) • f i := v.constr_apply_fintype 𝕜 _ _ @[simp] lemma constrL_basis (v : basis ι 𝕜 E) (f : ι → F) (i : ι) : (v.constrL f) (v i) = f i := v.constr_basis 𝕜 _ _ end basis namespace continuous_linear_map variables [t2_space E] [finite_dimensional 𝕜 E] /-- Builds a continuous linear equivalence from a continuous linear map on a finite-dimensional vector space whose determinant is nonzero. -/ def to_continuous_linear_equiv_of_det_ne_zero (f : E →L[𝕜] E) (hf : f.det ≠ 0) : E ≃L[𝕜] E := ((f : E →ₗ[𝕜] E).equiv_of_det_ne_zero hf).to_continuous_linear_equiv @[simp] lemma coe_to_continuous_linear_equiv_of_det_ne_zero (f : E →L[𝕜] E) (hf : f.det ≠ 0) : (f.to_continuous_linear_equiv_of_det_ne_zero hf : E →L[𝕜] E) = f := by { ext x, refl } @[simp] lemma to_continuous_linear_equiv_of_det_ne_zero_apply (f : E →L[𝕜] E) (hf : f.det ≠ 0) (x : E) : f.to_continuous_linear_equiv_of_det_ne_zero hf x = f x := rfl lemma _root_.matrix.to_lin_fin_two_prod_to_continuous_linear_map (a b c d : 𝕜) : (matrix.to_lin (basis.fin_two_prod 𝕜) (basis.fin_two_prod 𝕜) !![a, b; c, d]).to_continuous_linear_map = (a • continuous_linear_map.fst 𝕜 𝕜 𝕜 + b • continuous_linear_map.snd 𝕜 𝕜 𝕜).prod (c • continuous_linear_map.fst 𝕜 𝕜 𝕜 + d • continuous_linear_map.snd 𝕜 𝕜 𝕜) := continuous_linear_map.ext $ matrix.to_lin_fin_two_prod_apply _ _ _ _ end continuous_linear_map end normed_field
d989fc097d20d46b6b84c25349cc9ad129ebff4d
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/presheaf_on_basis.lean
9444ec6f493417336cad742fb2defd4385c83054
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
2,505
lean
/- Presheaf (of types) on basis. https://stacks.math.columbia.edu/tag/009I -/ import topology.basic import topology.opens universes u v open topological_space -- Presheaf of types where we only define sections on basis elements. structure presheaf_on_basis (α : Type u) [T : topological_space α] {B : set (opens α)} (HB : opens.is_basis B) := (F : Π {U}, U ∈ B → Type v) (res : ∀ {U V} (BU : U ∈ B) (BV : V ∈ B) (HVU : V ⊆ U), F BU → F BV) (Hid : ∀ {U} (BU : U ∈ B), (res BU BU (set.subset.refl U)) = id) (Hcomp : ∀ {U V W} (BU : U ∈ B) (BV : V ∈ B) (BW : W ∈ B) (HWV : W ⊆ V) (HVU : V ⊆ U), res BU BW (set.subset.trans HWV HVU) = (res BV BW HWV) ∘ (res BU BV HVU)) namespace presheaf_on_basis variables {α : Type u} [T : topological_space α] variables {B : set (opens α)} {HB : opens.is_basis B} instance : has_coe_to_fun (presheaf_on_basis α HB) := { F := λ _, Π {U}, U ∈ B → Type v, coe := presheaf_on_basis.F } -- Simplification lemmas. @[simp] lemma Hid' (F : presheaf_on_basis α HB) : ∀ {U} (BU : U ∈ B) (s : F BU), (F.res BU BU (set.subset.refl U)) s = s := λ U OU s, by rw F.Hid OU; simp @[simp] lemma Hcomp' (F : presheaf_on_basis α HB) : ∀ {U V W} (BU : U ∈ B) (BV : V ∈ B) (BW : W ∈ B) (HWV : W ⊆ V) (HVU : V ⊆ U) (s : F BU), (F.res BU BW (set.subset.trans HWV HVU)) s = (F.res BV BW HWV) ((F.res BU BV HVU) s) := λ U V W OU OV OW HWV HVU s, by rw F.Hcomp OU OV OW HWV HVU -- Morphism of presheaves on a basis (same as presheaves). structure morphism (F G : presheaf_on_basis α HB) := (map : ∀ {U} (HU : U ∈ B), F HU → G HU) (commutes : ∀ {U V} (HU : U ∈ B) (HV : V ∈ B) (Hsub : V ⊆ U), (G.res HU HV Hsub) ∘ (map HU) = (map HV) ∘ (F.res HU HV Hsub)) infix `⟶`:80 := morphism section morphism def comp {F G H : presheaf_on_basis α HB} (fg : F ⟶ G) (gh : G ⟶ H) : F ⟶ H := { map := λ U HU, gh.map HU ∘ fg.map HU, commutes := λ U V BU BV HVU, begin rw [←function.comp.assoc, gh.commutes BU BV HVU], symmetry, rw [function.comp.assoc, ←fg.commutes BU BV HVU] end } infix `⊚`:80 := comp def id (F : presheaf_on_basis α HB) : F ⟶ F := { map := λ U BU, id, commutes := λ U V BU BV HVU, by simp, } structure iso (F G : presheaf_on_basis α HB) := (mor : F ⟶ G) (inv : G ⟶ F) (mor_inv_id : mor ⊚ inv = id F) (inv_mor_id : inv ⊚ mor = id G) infix `≅`:80 := iso end morphism end presheaf_on_basis
b34c6af215128bb6f7a5723385b44c6c3a8f0c41
bb31430994044506fa42fd667e2d556327e18dfe
/src/analysis/convex/function.lean
27795ab9b7fedd1aed8a4ae124b21612d39f802b
[ "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
44,322
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, François Dupuis -/ import analysis.convex.basic /-! # Convex and concave functions This file defines convex and concave functions in vector spaces and proves the finite Jensen inequality. The integral version can be found in `analysis.convex.integral`. A function `f : E → β` 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 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. ## Main declarations * `convex_on 𝕜 s f`: The function `f` is convex on `s` with scalars `𝕜`. * `concave_on 𝕜 s f`: The function `f` is concave on `s` with scalars `𝕜`. * `strict_convex_on 𝕜 s f`: The function `f` is strictly convex on `s` with scalars `𝕜`. * `strict_concave_on 𝕜 s f`: The function `f` is strictly concave on `s` with scalars `𝕜`. -/ open finset linear_map set open_locale big_operators classical convex pointwise variables {𝕜 E F α β ι : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section ordered_add_comm_monoid variables [ordered_add_comm_monoid α] [ordered_add_comm_monoid β] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 α] [has_smul 𝕜 β] (s : set E) (f : E → β) {g : β → α} /-- Convexity of functions -/ def convex_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) /-- Strict convexity of functions -/ def strict_convex_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) < a • f x + b • f y /-- Strict concavity of functions -/ def strict_concave_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y < f (a • x + b • y) variables {𝕜 s f} open order_dual (to_dual of_dual) lemma convex_on.dual (hf : convex_on 𝕜 s f) : concave_on 𝕜 s (to_dual ∘ f) := hf lemma concave_on.dual (hf : concave_on 𝕜 s f) : convex_on 𝕜 s (to_dual ∘ f) := hf lemma strict_convex_on.dual (hf : strict_convex_on 𝕜 s f) : strict_concave_on 𝕜 s (to_dual ∘ f) := hf lemma strict_concave_on.dual (hf : strict_concave_on 𝕜 s f) : strict_convex_on 𝕜 s (to_dual ∘ f) := hf lemma convex_on_id {s : set β} (hs : convex 𝕜 s) : convex_on 𝕜 s id := ⟨hs, by { intros, refl }⟩ lemma concave_on_id {s : set β} (hs : convex 𝕜 s) : concave_on 𝕜 s id := ⟨hs, by { intros, refl }⟩ lemma convex_on.subset {t : set E} (hf : convex_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : convex_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma concave_on.subset {t : set E} (hf : concave_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : concave_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma strict_convex_on.subset {t : set E} (hf : strict_convex_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : strict_convex_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma strict_concave_on.subset {t : set E} (hf : strict_concave_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : strict_concave_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma convex_on.comp (hg : convex_on 𝕜 (f '' s) g) (hf : convex_on 𝕜 s f) (hg' : monotone_on g (f '' s)) : convex_on 𝕜 s (g ∘ f) := ⟨hf.1, λ x hx y hy a b ha hb hab, (hg' (mem_image_of_mem f $ hf.1 hx hy ha hb hab) (hg.1 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha hb hab) $ hf.2 hx hy ha hb hab).trans $ hg.2 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha hb hab⟩ lemma concave_on.comp (hg : concave_on 𝕜 (f '' s) g) (hf : concave_on 𝕜 s f) (hg' : monotone_on g (f '' s)) : concave_on 𝕜 s (g ∘ f) := ⟨hf.1, λ x hx y hy a b ha hb hab, (hg.2 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha hb hab).trans $ hg' (hg.1 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha hb hab) (mem_image_of_mem f $ hf.1 hx hy ha hb hab) $ hf.2 hx hy ha hb hab⟩ lemma convex_on.comp_concave_on (hg : convex_on 𝕜 (f '' s) g) (hf : concave_on 𝕜 s f) (hg' : antitone_on g (f '' s)) : convex_on 𝕜 s (g ∘ f) := hg.dual.comp hf hg' lemma concave_on.comp_convex_on (hg : concave_on 𝕜 (f '' s) g) (hf : convex_on 𝕜 s f) (hg' : antitone_on g (f '' s)) : concave_on 𝕜 s (g ∘ f) := hg.dual.comp hf hg' lemma strict_convex_on.comp (hg : strict_convex_on 𝕜 (f '' s) g) (hf : strict_convex_on 𝕜 s f) (hg' : strict_mono_on g (f '' s)) (hf' : s.inj_on f) : strict_convex_on 𝕜 s (g ∘ f) := ⟨hf.1, λ x hx y hy hxy a b ha hb hab, (hg' (mem_image_of_mem f $ hf.1 hx hy ha.le hb.le hab) (hg.1 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha.le hb.le hab) $ hf.2 hx hy hxy ha hb hab).trans $ hg.2 (mem_image_of_mem f hx) (mem_image_of_mem f hy) (mt (hf' hx hy) hxy) ha hb hab⟩ lemma strict_concave_on.comp (hg : strict_concave_on 𝕜 (f '' s) g) (hf : strict_concave_on 𝕜 s f) (hg' : strict_mono_on g (f '' s)) (hf' : s.inj_on f) : strict_concave_on 𝕜 s (g ∘ f) := ⟨hf.1, λ x hx y hy hxy a b ha hb hab, (hg.2 (mem_image_of_mem f hx) (mem_image_of_mem f hy) (mt (hf' hx hy) hxy) ha hb hab).trans $ hg' (hg.1 (mem_image_of_mem f hx) (mem_image_of_mem f hy) ha.le hb.le hab) (mem_image_of_mem f $ hf.1 hx hy ha.le hb.le hab) $ hf.2 hx hy hxy ha hb hab⟩ lemma strict_convex_on.comp_strict_concave_on (hg : strict_convex_on 𝕜 (f '' s) g) (hf : strict_concave_on 𝕜 s f) (hg' : strict_anti_on g (f '' s)) (hf' : s.inj_on f) : strict_convex_on 𝕜 s (g ∘ f) := hg.dual.comp hf hg' hf' lemma strict_concave_on.comp_strict_convex_on (hg : strict_concave_on 𝕜 (f '' s) g) (hf : strict_convex_on 𝕜 s f) (hg' : strict_anti_on g (f '' s)) (hf' : s.inj_on f) : strict_concave_on 𝕜 s (g ∘ f) := hg.dual.comp hf hg' hf' end has_smul section distrib_mul_action variables [has_smul 𝕜 E] [distrib_mul_action 𝕜 β] {s : set E} {f g : E → β} lemma convex_on.add (hf : convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y 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 + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma concave_on.add (hf : concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : concave_on 𝕜 s (f + g) := hf.dual.add hg end distrib_mul_action section module variables [has_smul 𝕜 E] [module 𝕜 β] {s : set E} {f : E → β} lemma convex_on_const (c : β) (hs : convex 𝕜 s) : convex_on 𝕜 s (λ x:E, c) := ⟨hs, λ x y _ _ a b _ _ hab, (convex.combo_self hab c).ge⟩ lemma concave_on_const (c : β) (hs : convex 𝕜 s) : concave_on 𝕜 s (λ x:E, c) := @convex_on_const _ _ βᵒᵈ _ _ _ _ _ _ c hs lemma convex_on_of_convex_epigraph (h : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}) : convex_on 𝕜 s f := ⟨λ x hx y hy a b ha hb hab, (@h (x, f x) ⟨hx, le_rfl⟩ (y, f y) ⟨hy, le_rfl⟩ a b ha hb hab).1, λ x hx y hy a b ha hb hab, (@h (x, f x) ⟨hx, le_rfl⟩ (y, f y) ⟨hy, le_rfl⟩ a b ha hb hab).2⟩ lemma concave_on_of_convex_hypograph (h : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1}) : concave_on 𝕜 s f := @convex_on_of_convex_epigraph 𝕜 E βᵒᵈ _ _ _ _ _ _ _ h end module section ordered_smul variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.convex_le (hf : convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := λ x hx y hy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha hb hab, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx.1 hy.1 ha hb hab ... ≤ a • r + b • r : add_le_add (smul_le_smul_of_nonneg hx.2 ha) (smul_le_smul_of_nonneg hy.2 hb) ... = r : convex.combo_self hab r⟩ lemma concave_on.convex_ge (hf : concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := hf.dual.convex_le r lemma convex_on.convex_epigraph (hf : convex_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintro ⟨x, r⟩ ⟨hx, hr⟩ ⟨y, t⟩ ⟨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 (smul_le_smul_of_nonneg hr ha) (smul_le_smul_of_nonneg ht hb) end lemma concave_on.convex_hypograph (hf : concave_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1} := hf.dual.convex_epigraph lemma convex_on_iff_convex_epigraph : convex_on 𝕜 s f ↔ convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2} := ⟨convex_on.convex_epigraph, convex_on_of_convex_epigraph⟩ lemma concave_on_iff_convex_hypograph : concave_on 𝕜 s f ↔ convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on_iff_convex_epigraph 𝕜 E βᵒᵈ _ _ _ _ _ _ _ f end ordered_smul section module variables [module 𝕜 E] [has_smul 𝕜 β] {s : set E} {f : E → β} /-- Right translation preserves convexity. -/ lemma convex_on.translate_right (hf : convex_on 𝕜 s f) (c : E) : convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := ⟨hf.1.translate_preimage_right _, λ x hx y hy a b ha hb hab, calc f (c + (a • x + b • y)) = f (a • (c + x) + b • (c + y)) : by rw [smul_add, smul_add, add_add_add_comm, convex.combo_self hab] ... ≤ a • f (c + x) + b • f (c + y) : hf.2 hx hy ha hb hab⟩ /-- Right translation preserves concavity. -/ lemma concave_on.translate_right (hf : concave_on 𝕜 s f) (c : E) : concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := hf.dual.translate_right _ /-- Left translation preserves convexity. -/ lemma convex_on.translate_left (hf : convex_on 𝕜 s f) (c : E) : convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ /-- Left translation preserves concavity. -/ lemma concave_on.translate_left (hf : concave_on 𝕜 s f) (c : E) : concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := hf.dual.translate_left _ end module section module variables [module 𝕜 E] [module 𝕜 β] lemma convex_on_iff_forall_pos {s : set E} {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y := begin refine and_congr_right' ⟨λ h x hx y hy a b ha hb hab, h hx hy ha.le hb.le hab, λ h x hx y hy a b ha hb hab, _⟩, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, subst b, simp_rw [zero_smul, zero_add, one_smul] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, subst a, simp_rw [zero_smul, add_zero, one_smul] }, exact h hx hy ha' hb' hab, end lemma concave_on_iff_forall_pos {s : set E} {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) := @convex_on_iff_forall_pos 𝕜 E βᵒᵈ _ _ _ _ _ _ _ lemma convex_on_iff_pairwise_pos {s : set E} {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) := begin rw convex_on_iff_forall_pos, refine and_congr_right' ⟨λ h x hx y hy _ a b ha hb hab, h hx hy ha hb hab, λ h x hx y hy a b ha hb hab, _⟩, obtain rfl | hxy := eq_or_ne x y, { rw [convex.combo_self hab, convex.combo_self hab] }, exact h hx hy hxy ha hb hab, end lemma concave_on_iff_pairwise_pos {s : set E} {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) := @convex_on_iff_pairwise_pos 𝕜 E βᵒᵈ _ _ _ _ _ _ _ /-- A linear map is convex. -/ lemma linear_map.convex_on (f : E →ₗ[𝕜] β) {s : set E} (hs : convex 𝕜 s) : convex_on 𝕜 s f := ⟨hs, λ _ _ _ _ _ _ _ _ _, by rw [f.map_add, f.map_smul, f.map_smul]⟩ /-- A linear map is concave. -/ lemma linear_map.concave_on (f : E →ₗ[𝕜] β) {s : set E} (hs : convex 𝕜 s) : concave_on 𝕜 s f := ⟨hs, λ _ _ _ _ _ _ _ _ _, by rw [f.map_add, f.map_smul, f.map_smul]⟩ lemma strict_convex_on.convex_on {s : set E} {f : E → β} (hf : strict_convex_on 𝕜 s f) : convex_on 𝕜 s f := convex_on_iff_pairwise_pos.mpr ⟨hf.1, λ x hx y hy hxy a b ha hb hab, (hf.2 hx hy hxy ha hb hab).le⟩ lemma strict_concave_on.concave_on {s : set E} {f : E → β} (hf : strict_concave_on 𝕜 s f) : concave_on 𝕜 s f := hf.dual.convex_on section ordered_smul variables [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma strict_convex_on.convex_lt (hf : strict_convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x < r} := convex_iff_pairwise_pos.2 $ λ x hx y hy hxy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha.le hb.le hab, calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx.1 hy.1 hxy ha hb hab ... ≤ a • r + b • r : add_le_add (smul_lt_smul_of_pos hx.2 ha).le (smul_lt_smul_of_pos hy.2 hb).le ... = r : convex.combo_self hab r⟩ lemma strict_concave_on.convex_gt (hf : strict_concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r < f x} := hf.dual.convex_lt r end ordered_smul section linear_order variables [linear_order E] {s : set E} {f : E → β} /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), 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 (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, 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 refine convex_on_iff_pairwise_pos.2 ⟨hs, λ x hx y hy hxy a b ha hb hab, _⟩, wlog h : x ≤ y using [x y a b, y x b a], { exact le_total _ _ }, exact hf hx hy (h.lt_of_ne hxy) ha hb hab, end /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` 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.concave_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on 𝕜 s f := @linear_order.convex_on_of_lt _ _ βᵒᵈ _ _ _ _ _ _ s f hs hf /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is strictly convex, it suffices to verify the inequality `f (a • x + b • y) < a • f x + b • f y` 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.strict_convex_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) < a • f x + b • f y) : strict_convex_on 𝕜 s f := begin refine ⟨hs, λ x hx y hy hxy a b ha hb hab, _⟩, wlog h : x ≤ y using [x y a b, y x b a], { exact le_total _ _ }, exact hf hx hy (h.lt_of_ne hxy) ha hb hab, end /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is strictly concave it suffices to verify the inequality `a • f x + b • f y < f (a • x + b • y)` 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.strict_concave_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y < f (a • x + b • y)) : strict_concave_on 𝕜 s f := @linear_order.strict_convex_on_of_lt _ _ βᵒᵈ _ _ _ _ _ _ _ _ hs hf end linear_order end module section module variables [module 𝕜 E] [module 𝕜 F] [has_smul 𝕜 β] /-- If `g` is convex on `s`, so is `(f ∘ g)` on `f ⁻¹' s` for a linear `f`. -/ lemma convex_on.comp_linear_map {f : F → β} {s : set F} (hf : convex_on 𝕜 s f) (g : E →ₗ[𝕜] F) : convex_on 𝕜 (g ⁻¹' s) (f ∘ g) := ⟨hf.1.linear_preimage _, λ x hx y hy a b ha hb hab, calc f (g (a • x + b • y)) = f (a • (g x) + b • (g y)) : by rw [g.map_add, g.map_smul, g.map_smul] ... ≤ a • f (g x) + b • f (g y) : hf.2 hx hy ha hb hab⟩ /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma concave_on.comp_linear_map {f : F → β} {s : set F} (hf : concave_on 𝕜 s f) (g : E →ₗ[𝕜] F) : concave_on 𝕜 (g ⁻¹' s) (f ∘ g) := hf.dual.comp_linear_map g end module end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] section distrib_mul_action variables [has_smul 𝕜 E] [distrib_mul_action 𝕜 β] {s : set E} {f g : E → β} lemma strict_convex_on.add_convex_on (hf : strict_convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y hy hxy 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_lt_add_of_lt_of_le (hf.2 hx hy hxy ha hb hab) (hg.2 hx hy ha.le hb.le hab) ... = a • (f x + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma convex_on.add_strict_convex_on (hf : convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := (add_comm g f) ▸ hg.add_convex_on hf lemma strict_convex_on.add (hf : strict_convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y hy hxy 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_lt_add (hf.2 hx hy hxy ha hb hab) (hg.2 hx hy hxy ha hb hab) ... = a • (f x + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma strict_concave_on.add_concave_on (hf : strict_concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add_convex_on hg.dual lemma concave_on.add_strict_concave_on (hf : concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add_strict_convex_on hg.dual lemma strict_concave_on.add (hf : strict_concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add hg end distrib_mul_action section module variables [module 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.convex_lt (hf : convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x < r} := convex_iff_forall_pos.2 $ λ x hx y hy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha.le hb.le hab, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx.1 hy.1 ha.le hb.le hab ... < a • r + b • r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx.2 ha) (smul_le_smul_of_nonneg hy.2.le hb.le) ... = r : convex.combo_self hab _⟩ lemma concave_on.convex_gt (hf : concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r < f x} := hf.dual.convex_lt r lemma convex_on.open_segment_subset_strict_epigraph (hf : convex_on 𝕜 s f) (p q : E × β) (hp : p.1 ∈ s ∧ f p.1 < p.2) (hq : q.1 ∈ s ∧ f q.1 ≤ q.2) : open_segment 𝕜 p q ⊆ {p : E × β | p.1 ∈ s ∧ f p.1 < p.2} := begin rintro _ ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨hf.1 hp.1 hq.1 ha.le hb.le hab, _⟩, calc f (a • p.1 + b • q.1) ≤ a • f p.1 + b • f q.1 : hf.2 hp.1 hq.1 ha.le hb.le hab ... < a • p.2 + b • q.2 : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hp.2 ha) (smul_le_smul_of_nonneg hq.2 hb.le) end lemma concave_on.open_segment_subset_strict_hypograph (hf : concave_on 𝕜 s f) (p q : E × β) (hp : p.1 ∈ s ∧ p.2 < f p.1) (hq : q.1 ∈ s ∧ q.2 ≤ f q.1) : open_segment 𝕜 p q ⊆ {p : E × β | p.1 ∈ s ∧ p.2 < f p.1} := hf.dual.open_segment_subset_strict_epigraph p q hp hq lemma convex_on.convex_strict_epigraph (hf : convex_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 < p.2} := convex_iff_open_segment_subset.mpr $ λ p hp q hq, hf.open_segment_subset_strict_epigraph p q hp ⟨hq.1, hq.2.le⟩ lemma concave_on.convex_strict_hypograph (hf : concave_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 < f p.1} := hf.dual.convex_strict_epigraph end module end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} /-- The pointwise maximum of convex functions is convex. -/ lemma convex_on.sup (hf : convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : convex_on 𝕜 s (f ⊔ g) := begin refine ⟨hf.left, λ x hx y hy a b ha hb hab, sup_le _ _⟩, { calc f (a • x + b • y) ≤ a • f x + b • f y : hf.right hx hy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_left ha) (smul_le_smul_of_nonneg le_sup_left hb) }, { calc g (a • x + b • y) ≤ a • g x + b • g y : hg.right hx hy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_right ha) (smul_le_smul_of_nonneg le_sup_right hb) } end /-- The pointwise minimum of concave functions is concave. -/ lemma concave_on.inf (hf : concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : concave_on 𝕜 s (f ⊓ g) := hf.dual.sup hg /-- The pointwise maximum of strictly convex functions is strictly convex. -/ lemma strict_convex_on.sup (hf : strict_convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f ⊔ g) := ⟨hf.left, λ x hx y hy hxy a b ha hb hab, max_lt (calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx hy hxy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_left ha.le) (smul_le_smul_of_nonneg le_sup_left hb.le)) (calc g (a • x + b • y) < a • g x + b • g y : hg.2 hx hy hxy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_right ha.le) (smul_le_smul_of_nonneg le_sup_right hb.le))⟩ /-- The pointwise minimum of strictly concave functions is strictly concave. -/ lemma strict_concave_on.inf (hf : strict_concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f ⊓ g) := hf.dual.sup hg /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment' (hf : convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (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 (smul_le_smul_of_nonneg (le_max_left _ _) ha) (smul_le_smul_of_nonneg (le_max_right _ _) hb) ... = max (f x) (f y) : convex.combo_self hab _ /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.ge_on_segment' (hf : concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := hf.dual.le_on_segment' hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ 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 /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.ge_on_segment (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x -[𝕜] y]) : min (f x) (f y) ≤ f z := hf.dual.le_on_segment hx hy hz /-- A strictly convex function on an open segment is strictly upper-bounded by the max of its endpoints. -/ lemma strict_convex_on.lt_on_open_segment' (hf : strict_convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) {a b : 𝕜} (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 hxy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha.le) (smul_le_smul_of_nonneg (le_max_right _ _) hb.le) ... = max (f x) (f y) : convex.combo_self hab _ /-- A strictly concave function on an open segment is strictly lower-bounded by the min of its endpoints. -/ lemma strict_concave_on.lt_on_open_segment' (hf : strict_concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) : min (f x) (f y) < f (a • x + b • y) := hf.dual.lt_on_open_segment' hx hy hxy ha hb hab /-- A strictly convex function on an open segment is strictly upper-bounded by the max of its endpoints. -/ lemma strict_convex_on.lt_on_open_segment (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) (hz : z ∈ open_segment 𝕜 x y) : f z < max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.lt_on_open_segment' hx hy hxy ha hb hab /-- A strictly concave function on an open segment is strictly lower-bounded by the min of its endpoints. -/ lemma strict_concave_on.lt_on_open_segment (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) (hz : z ∈ open_segment 𝕜 x y) : min (f x) (f y) < f z := hf.dual.lt_on_open_segment hx hy hxy hz end linear_ordered_add_comm_monoid section linear_ordered_cancel_add_comm_monoid variables [linear_ordered_cancel_add_comm_monoid β] section ordered_smul variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} lemma convex_on.le_left_of_right_le' (hf : convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 ≤ b) (hab : a + b = 1) (hfy : f y ≤ f (a • x + b • y)) : f (a • x + b • y) ≤ f x := le_of_not_lt $ λ h, lt_irrefl (f (a • x + b • y)) $ calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha.le hb hab ... < a • f (a • x + b • y) + b • f (a • x + b • y) : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos h ha) (smul_le_smul_of_nonneg hfy hb) ... = f (a • x + b • y) : convex.combo_self hab _ lemma concave_on.left_le_of_le_right' (hf : concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 ≤ b) (hab : a + b = 1) (hfy : f (a • x + b • y) ≤ f y) : f x ≤ f (a • x + b • y) := hf.dual.le_left_of_right_le' hx hy ha hb hab hfy lemma convex_on.le_right_of_left_le' (hf : convex_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 < b) (hab : a + b = 1) (hfx : f x ≤ f (a • x + b • y)) : f (a • x + b • y) ≤ f y := begin rw add_comm at ⊢ hab hfx, exact hf.le_left_of_right_le' hy hx hb ha hab hfx, end lemma concave_on.right_le_of_le_left' (hf : concave_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 < b) (hab : a + b = 1) (hfx : f (a • x + b • y) ≤ f x) : f y ≤ f (a • x + b • y) := hf.dual.le_right_of_left_le' hx hy ha hb hab hfx lemma convex_on.le_left_of_right_le (hf : convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f y ≤ f z) : f z ≤ f x := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.le_left_of_right_le' hx hy ha hb.le hab hyz, end lemma concave_on.left_le_of_le_right (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f z ≤ f y) : f x ≤ f z := hf.dual.le_left_of_right_le hx hy hz hyz lemma convex_on.le_right_of_left_le (hf : convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f x ≤ f z) : f z ≤ f y := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.le_right_of_left_le' hx hy ha.le hb hab hxz, end lemma concave_on.right_le_of_le_left (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f z ≤ f x) : f y ≤ f z := hf.dual.le_right_of_left_le hx hy hz hxz end ordered_smul section module variables [module 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} /- The following lemmas don't require `module 𝕜 E` if you add the hypothesis `x ≠ y`. At the time of the writing, we decided the resulting lemmas wouldn't be useful. Feel free to reintroduce them. -/ lemma strict_convex_on.lt_left_of_right_lt' (hf : strict_convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfy : f y < f (a • x + b • y)) : f (a • x + b • y) < f x := not_le.1 $ λ h, lt_irrefl (f (a • x + b • y)) $ calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx hy begin rintro rfl, rw convex.combo_self hab at hfy, exact lt_irrefl _ hfy, end ha hb hab ... < a • f (a • x + b • y) + b • f (a • x + b • y) : add_lt_add_of_le_of_lt (smul_le_smul_of_nonneg h ha.le) (smul_lt_smul_of_pos hfy hb) ... = f (a • x + b • y) : convex.combo_self hab _ lemma strict_concave_on.left_lt_of_lt_right' (hf : strict_concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfy : f (a • x + b • y) < f y) : f x < f (a • x + b • y) := hf.dual.lt_left_of_right_lt' hx hy ha hb hab hfy lemma strict_convex_on.lt_right_of_left_lt' (hf : strict_convex_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfx : f x < f (a • x + b • y)) : f (a • x + b • y) < f y := begin rw add_comm at ⊢ hab hfx, exact hf.lt_left_of_right_lt' hy hx hb ha hab hfx, end lemma strict_concave_on.lt_right_of_left_lt' (hf : strict_concave_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfx : f (a • x + b • y) < f x) : f y < f (a • x + b • y) := hf.dual.lt_right_of_left_lt' hx hy ha hb hab hfx lemma strict_convex_on.lt_left_of_right_lt (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f y < f z) : f z < f x := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.lt_left_of_right_lt' hx hy ha hb hab hyz, end lemma strict_concave_on.left_lt_of_lt_right (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f z < f y) : f x < f z := hf.dual.lt_left_of_right_lt hx hy hz hyz lemma strict_convex_on.lt_right_of_left_lt (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f x < f z) : f z < f y := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.lt_right_of_left_lt' hx hy ha hb hab hxz, end lemma strict_concave_on.lt_right_of_left_lt (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f z < f x) : f y < f z := hf.dual.lt_right_of_left_lt hx hy hz hxz end module end linear_ordered_cancel_add_comm_monoid section ordered_add_comm_group variables [ordered_add_comm_group β] [has_smul 𝕜 E] [module 𝕜 β] {s : set E} {f g : E → β} /-- A function `-f` is convex iff `f` is concave. -/ @[simp] lemma neg_convex_on_iff : convex_on 𝕜 s (-f) ↔ concave_on 𝕜 s f := begin split, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy a b ha hb hab, _⟩, simp [neg_apply, neg_le, add_comm] at h, exact h hx hy ha hb hab }, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy a b ha hb hab, _⟩, rw ←neg_le_neg_iff, simp_rw [neg_add, pi.neg_apply, smul_neg, neg_neg], exact h hx hy ha hb hab } end /-- A function `-f` is concave iff `f` is convex. -/ @[simp] lemma neg_concave_on_iff : concave_on 𝕜 s (-f) ↔ convex_on 𝕜 s f:= by rw [← neg_convex_on_iff, neg_neg f] /-- A function `-f` is strictly convex iff `f` is strictly concave. -/ @[simp] lemma neg_strict_convex_on_iff : strict_convex_on 𝕜 s (-f) ↔ strict_concave_on 𝕜 s f := begin split, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy hxy a b ha hb hab, _⟩, simp [neg_apply, neg_lt, add_comm] at h, exact h hx hy hxy ha hb hab }, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy hxy a b ha hb hab, _⟩, rw ←neg_lt_neg_iff, simp_rw [neg_add, pi.neg_apply, smul_neg, neg_neg], exact h hx hy hxy ha hb hab } end /-- A function `-f` is strictly concave iff `f` is strictly convex. -/ @[simp] lemma neg_strict_concave_on_iff : strict_concave_on 𝕜 s (-f) ↔ strict_convex_on 𝕜 s f := by rw [← neg_strict_convex_on_iff, neg_neg f] alias neg_convex_on_iff ↔ _ concave_on.neg alias neg_concave_on_iff ↔ _ convex_on.neg alias neg_strict_convex_on_iff ↔ _ strict_concave_on.neg alias neg_strict_concave_on_iff ↔ _ strict_convex_on.neg lemma convex_on.sub (hf : convex_on 𝕜 s f) (hg : concave_on 𝕜 s g) : convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma concave_on.sub (hf : concave_on 𝕜 s f) (hg : convex_on 𝕜 s g) : concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma strict_convex_on.sub (hf : strict_convex_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma strict_concave_on.sub (hf : strict_concave_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma convex_on.sub_strict_concave_on (hf : convex_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_strict_convex_on hg.neg lemma concave_on.sub_strict_convex_on (hf : concave_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_strict_concave_on hg.neg lemma strict_convex_on.sub_concave_on (hf : strict_convex_on 𝕜 s f) (hg : concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_convex_on hg.neg lemma strict_concave_on.sub_convex_on (hf : strict_concave_on 𝕜 s f) (hg : convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_concave_on hg.neg end ordered_add_comm_group end add_comm_monoid section add_cancel_comm_monoid variables [add_cancel_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [has_smul 𝕜 β] {s : set E} {f : E → β} /-- Right translation preserves strict convexity. -/ lemma strict_convex_on.translate_right (hf : strict_convex_on 𝕜 s f) (c : E) : strict_convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := ⟨hf.1.translate_preimage_right _, λ x hx y hy hxy a b ha hb hab, calc f (c + (a • x + b • y)) = f (a • (c + x) + b • (c + y)) : by rw [smul_add, smul_add, add_add_add_comm, convex.combo_self hab] ... < a • f (c + x) + b • f (c + y) : hf.2 hx hy ((add_right_injective c).ne hxy) ha hb hab⟩ /-- Right translation preserves strict concavity. -/ lemma strict_concave_on.translate_right (hf : strict_concave_on 𝕜 s f) (c : E) : strict_concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := hf.dual.translate_right _ /-- Left translation preserves strict convexity. -/ lemma strict_convex_on.translate_left (hf : strict_convex_on 𝕜 s f) (c : E) : strict_convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ /-- Left translation preserves strict concavity. -/ lemma strict_concave_on.translate_left (hf : strict_concave_on 𝕜 s f) (c : E) : strict_concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ end add_cancel_comm_monoid end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] [add_comm_monoid E] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section module variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.smul {c : 𝕜} (hc : 0 ≤ c) (hf : convex_on 𝕜 s f) : convex_on 𝕜 s (λ x, c • f x) := ⟨hf.1, λ x hx y hy a b ha hb hab, calc c • f (a • x + b • y) ≤ c • (a • f x + b • f y) : smul_le_smul_of_nonneg (hf.2 hx hy ha hb hab) hc ... = a • (c • f x) + b • (c • f y) : by rw [smul_add, smul_comm c, smul_comm c]; apply_instance⟩ lemma concave_on.smul {c : 𝕜} (hc : 0 ≤ c) (hf : concave_on 𝕜 s f) : concave_on 𝕜 s (λ x, c • f x) := hf.dual.smul hc end module end ordered_add_comm_monoid end ordered_comm_semiring section ordered_ring variables [linear_ordered_field 𝕜] [add_comm_group E] [add_comm_group F] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section module variables [module 𝕜 E] [module 𝕜 F] [has_smul 𝕜 β] /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ lemma convex_on.comp_affine_map {f : F → β} (g : E →ᵃ[𝕜] F) {s : set F} (hf : convex_on 𝕜 s f) : convex_on 𝕜 (g ⁻¹' s) (f ∘ g) := ⟨hf.1.affine_preimage _, λ x hx y hy a b ha hb hab, calc (f ∘ g) (a • x + b • y) = f (g (a • x + b • y)) : rfl ... = f (a • (g x) + b • (g y)) : by rw [convex.combo_affine_apply hab] ... ≤ a • f (g x) + b • f (g y) : hf.2 hx hy ha hb hab⟩ /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ lemma concave_on.comp_affine_map {f : F → β} (g : E →ᵃ[𝕜] F) {s : set F} (hf : concave_on 𝕜 s f) : concave_on 𝕜 (g ⁻¹' s) (f ∘ g) := hf.dual.comp_affine_map g end module end ordered_add_comm_monoid end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] [add_comm_monoid E] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section has_smul variables [has_smul 𝕜 E] [has_smul 𝕜 β] {s : set E} lemma convex_on_iff_div {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, 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 hx y hy a b ha hb hab, apply h hx hy (div_nonneg ha hab.le) (div_nonneg hb hab.le), rw [←add_div, div_self hab.ne'], end, begin intros h x hx y hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma concave_on_iff_div {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • f x + (b/(a+b)) • f y ≤ f ((a/(a+b)) • x + (b/(a+b)) • y) := @convex_on_iff_div _ _ βᵒᵈ _ _ _ _ _ _ _ lemma strict_convex_on_iff_div {f : E → β} : strict_convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < 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 hx y hy hxy a b ha hb, have hab := add_pos ha hb, apply h hx hy hxy (div_pos ha hab) (div_pos hb hab), rw [←add_div, div_self hab.ne'], end, begin intros h x hx y hy hxy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy hxy ha hb, end⟩ lemma strict_concave_on_iff_div {f : E → β} : strict_concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → (a/(a+b)) • f x + (b/(a+b)) • f y < f ((a/(a+b)) • x + (b/(a+b)) • y) := @strict_convex_on_iff_div _ _ βᵒᵈ _ _ _ _ _ _ _ end has_smul end ordered_add_comm_monoid end linear_ordered_field section variables [linear_ordered_field 𝕜] [linear_ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] {x y z : 𝕜} {s : set 𝕜} {f : 𝕜 → β} lemma convex_on.le_right_of_left_le'' (hf : convex_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y ≤ z) (h : f x ≤ f y) : f y ≤ f z := hyz.eq_or_lt.elim (λ hyz, (congr_arg f hyz).le) (λ hyz, hf.le_right_of_left_le hx hz (Ioo_subset_open_segment ⟨hxy, hyz⟩) h) lemma convex_on.le_left_of_right_le'' (hf : convex_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x ≤ y) (hyz : y < z) (h : f z ≤ f y) : f y ≤ f x := hxy.eq_or_lt.elim (λ hxy, (congr_arg f hxy).ge) (λ hxy, hf.le_left_of_right_le hx hz (Ioo_subset_open_segment ⟨hxy, hyz⟩) h) lemma concave_on.right_le_of_le_left'' (hf : concave_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y ≤ z) (h : f y ≤ f x) : f z ≤ f y := hf.dual.le_right_of_left_le'' hx hz hxy hyz h lemma concave_on.left_le_of_le_right'' (hf : concave_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x ≤ y) (hyz : y < z) (h : f y ≤ f z) : f x ≤ f y := hf.dual.le_left_of_right_le'' hx hz hxy hyz h end
2faa9ee9614203c1f92617ed90349360e5821df4
54deab7025df5d2df4573383df7e1e5497b7a2c2
/topology/uniform_space.lean
964a4a791781fb67d4a82aee1829d437c51f219c
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
70,628
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 Theory of uniform spaces. Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * completeness * completion (on Cauchy filters instead of Cauchy sequences) * extension of uniform continuous functions to complete spaces * uniform contiunuity & embedding * totally bounded * totally bounded ∧ complete → compact One reason to directly formalize uniform spaces is foundational: we define ℝ as a completion of ℚ. The central concept of uniform spaces is its uniformity: a filter relating two elemenets of the space. This filter is reflexive, symmetric and transitive. So a set (i.e. a relation) in this filter represents a 'distance': it is reflexive, symmetric and the uniformity contains a set for which the `triangular` rule holds. The formalization is mostly based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter topology.topological_space topology.continuity open set lattice filter classical local attribute [instance] decidable_inhabited prop_decidable set_option eqn_compiler.zeta true universes u v w x y section variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} lemma forall_quotient_iff [r : setoid α] {p : quotient r → Prop} : (∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) := ⟨assume h x, h _, assume h a, a.induction_on h⟩ def id_rel {α : Type u} := {p : α × α | p.1 = p.2} def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, comp_rel (f x) (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ comp_rel s t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : comp_rel id_rel r = r := set.ext $ assume ⟨a, b⟩, ⟨assume ⟨a', (heq : a = a'), ha'⟩, heq.symm ▸ ha', assume ha, ⟨a, rfl, ha⟩⟩ /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : principal id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, comp_rel s s) ≤ uniformity) def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { topological_space . is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity.sets, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, u.uniformity.upwards_sets (inter_mem_sets (hs x xs) (ht x xt)) $ assume p ⟨ps, pt⟩ h, ⟨ps h, pt h⟩, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, u.uniformity.upwards_sets (hs t ts x xt) $ assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*] /-- uniformity: usable typeclass incorporating a topology -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity.sets)) def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.refl _ } def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.refl _ } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨t₁, u₁, o₁⟩ ⟨t₂, u₂, o₂⟩ h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space α] def uniformity : filter (α × α) := (@uniform_space.to_core α _).uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ (@uniformity α _).sets) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : principal id_rel ≤ @uniformity α _ := (@uniform_space.to_core α _).refl lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ (@uniformity α _).sets) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap α α) uniformity ≤ uniformity := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : uniformity.lift' (λs:set (α×α), comp_rel s s) ≤ uniformity := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto prod.swap (@uniformity α _) uniformity := symm_le_uniformity lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ_, (a, a)) f uniformity := assume s hs, show {x | (a, a) ∈ s} ∈ f.sets, from univ_mem_sets' $ assume b, refl_mem_uniformity hs lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, comp_rel t t ⊆ s := have s ∈ (uniformity.lift' (λt:set (α×α), comp_rel t t)).sets, from comp_le_uniformity hs, (mem_lift'_iff $ monotone_comp_rel monotone_id monotone_id).mp this lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ (@uniformity α _).sets, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ comp_rel t t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : uniformity ≤ map (@prod.swap α α) uniformity := calc uniformity = id <$> uniformity : (functor.id_map _).symm ... = (prod.swap.{u u} ∘ prod.swap) <$> uniformity : congr_arg (λf : (α×α)→(α×α), f <$> uniformity) (by apply funext; intro x; cases x; refl) ... = (map prod.swap ∘ map prod.swap) uniformity : congr map_compose rfl ... ≤ prod.swap.{u u} <$> uniformity : map_mono symm_le_uniformity lemma uniformity_eq_symm : uniformity = (@prod.swap α α) <$> uniformity := le_antisymm uniformity_le_symm symm_le_uniformity theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : uniformity.lift (λs, g (preimage prod.swap s)) ≤ f) : uniformity.lift g ≤ f := le_trans (lift_mono uniformity_le_symm (le_refl _)) (by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h) lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f): uniformity.lift (λs, f (comp_rel s s)) ≤ uniformity.lift f := calc uniformity.lift (λs, f (comp_rel s s)) = (uniformity.lift' (λs:set (α×α), comp_rel s s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ uniformity.lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s)) ≤ uniformity := calc uniformity.lift' (λd, comp_rel d (comp_rel d d)) = uniformity.lift (λs, uniformity.lift' (λt:set(α×α), comp_rel s (comp_rel t t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ uniformity.lift (λs, uniformity.lift' (λt:set(α×α), comp_rel s t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (principal ∘ comp_rel s) $ monotone_comp (monotone_comp_rel monotone_const monotone_id) monotone_principal ... = uniformity.lift' (λs:set(α×α), comp_rel s s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ uniformity : comp_le_uniformity lemma mem_nhds_uniformity_iff {x : α} {s : set α} : (s ∈ (nhds x).sets) ↔ ({p : α × α | p.1 = x → p.2 ∈ s} ∈ (@uniformity α _).sets) := ⟨ begin simp [mem_nhds_sets_iff, is_open_uniformity], exact assume ⟨t, ts, xt, ht⟩, uniformity.upwards_sets (ht x xt) $ assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ (@uniformity α _).sets}, assume x', assume hx' : {p : α × α | p.fst = x' → p.snd ∈ s} ∈ (@uniformity α _).sets, refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in uniformity.upwards_sets ht $ assume ⟨a, b⟩ hp' (eq : a = x'), have hp : (x', b) ∈ t, from eq ▸ hp', show {p : α × α | p.fst = b → p.snd ∈ s} ∈ (@uniformity α _).sets, from uniformity.upwards_sets ht $ assume ⟨a, b'⟩ hp' (heq : a = b), have (b, b') ∈ t, from heq ▸ hp', have (x', b') ∈ comp_rel t t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma nhds_eq_uniformity {x : α} : nhds x = uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ s}) := filter_eq $ set.ext $ assume s, begin rw [mem_lift'_iff], tactic.swap, apply monotone_preimage, simp [mem_nhds_uniformity_iff], exact ⟨assume h, ⟨_, h, assume y h, h rfl⟩, assume ⟨t, h₁, h₂⟩, uniformity.upwards_sets h₁ $ assume ⟨x', y⟩ hp (eq : x' = x), h₂ $ show (x, y) ∈ t, from eq ▸ hp⟩ end lemma mem_nhds_left {x : α} {s : set (α×α)} (h : s ∈ (uniformity.sets : set (set (α×α)))) : {y : α | (x, y) ∈ s} ∈ (nhds x).sets := have nhds x ≤ principal {y : α | (x, y) ∈ s}, by rw [nhds_eq_uniformity]; exact infi_le_of_le s (infi_le _ h), by simp at this; assumption lemma mem_nhds_right {y : α} {s : set (α×α)} (h : s ∈ (uniformity.sets : set (set (α×α)))) : {x : α | (x, y) ∈ s} ∈ (nhds y).sets := mem_nhds_left (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (nhds a) uniformity := assume s hs, show {a' | (a', a) ∈ s} ∈ (nhds a).sets, from mem_nhds_right hs lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (nhds a) uniformity := assume s hs, show {a' | (a, a') ∈ s} ∈ (nhds a).sets, from mem_nhds_left hs lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_comp monotone_preimage $ monotone_comp monotone_preimage monotone_principal) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> uniformity).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ monotone_comp monotone_preimage hg ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : filter.prod (nhds a) (nhds b) = uniformity.lift (λs:set (α×α), uniformity.lift' (λt:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) := show (nhds a).lift (λs:set α, (nhds b).lift (λt:set α, principal (set.prod s t))) = _, begin rw [lift_nhds_right], apply congr_arg, apply funext, intro s, rw [lift_nhds_left], refl, exact monotone_comp (monotone_prod monotone_const monotone_id) monotone_principal, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : α} : nhds (a, b) = uniformity.lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ (@uniformity α _).sets) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ (nhds (x, y)).sets, begin rw [nhds_eq_uniformity_prod, mem_lift'_iff], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d∈(@uniformity α _).sets, comp_rel d (comp_rel t d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (nhds (a, b) ⊓ principal t ≠ ⊥) : by simp [closure_eq_nhds] ... ↔ (((@prod.swap α α) <$> uniformity).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap α α) uniformity).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by refl ... ↔ (uniformity.lift' (λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ principal t ≠ ⊥) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (∀s∈(@uniformity α _).sets, ∃x, x ∈ set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t) : begin rw [lift'_inf_principal_eq, lift'_neq_bot_iff], apply forall_congr, intro s, rw [ne_empty_iff_exists_mem], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (∀s∈(@uniformity α _).sets, (a, b) ∈ comp_rel s (comp_rel t s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : (@uniformity α _) = uniformity.lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; exact uniformity.upwards_sets hs subset_closure) (calc uniformity.lift' closure ≤ uniformity.lift' (λd, comp_rel d (comp_rel d d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≤ uniformity : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : (@uniformity α _) = uniformity.lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_iff $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ (@uniformity α _).sets, from (@uniformity α _).upwards_sets hs $ this, by simp [this]) (assume s hs, (uniformity.lift' interior).upwards_sets (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : interior s ∈ (@uniformity α _).sets := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed [uniform_space α] {s : set (α×α)} (h : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, is_closed t ∧ t ⊆ s := have s ∈ ((@uniformity α _).lift' closure).sets, by rwa [uniformity_eq_uniformity_closure] at h, have ∃t∈(@uniformity α _).sets, closure t ⊆ s, by rwa [mem_lift'_iff] at this; apply closure_mono, let ⟨t, ht, hst⟩ := this in ⟨closure t, uniformity.upwards_sets ht subset_closure, is_closed_closure, hst⟩ /- uniform continuity -/ definition uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) uniformity uniformity lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := @tendsto_const_uniformity _ _ _ b uniformity lemma uniform_continuous_compose [uniform_space β] [uniform_space γ] {f : α → β} {g : β → γ} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (g ∘ f) := tendsto_compose hf hg definition uniform_embedding [uniform_space β] (f : α → β) := (∀a₁ a₂, f a₁ = f a₂ → a₁ = a₂) ∧ vmap (λx:α×α, (f x.1, f x.2)) uniformity = uniformity lemma uniform_continuous_of_embedding [uniform_space β] {f : α → β} (hf : uniform_embedding f) : uniform_continuous f := by simp [uniform_continuous, hf.right.symm]; exact tendsto_vmap lemma dense_embedding_of_uniform_embedding [uniform_space β] {f : α → β} (h : uniform_embedding f) (hd : ∀x, x ∈ closure (f '' univ)) : dense_embedding f := { dense_embedding . dense := hd, inj := h.left, induced := begin intro a, simp [h.right.symm, nhds_eq_uniformity], rw [vmap_lift'_eq, vmap_lift'_eq2], refl, exact monotone_preimage, exact monotone_preimage end } lemma continuous_of_uniform [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_tendsto.mpr $ assume a, calc map f (nhds a) ≤ (map (λp:α×α, (f p.1, f p.2)) uniformity).lift' (λs:set (β×β), {y | (f a, y) ∈ s}) : begin rw [nhds_eq_uniformity, map_lift'_eq, map_lift'_eq2], exact (lift'_mono' $ assume s hs b ⟨a', (ha' : (_, a') ∈ s), a'_eq⟩, ⟨(a, a'), ha', show (f a, f a') = (f a, b), from a'_eq ▸ rfl⟩), exact monotone_preimage, exact monotone_preimage end ... ≤ nhds (f a) : by rw [nhds_eq_uniformity]; exact lift'_mono hf (le_refl _) lemma closure_image_mem_nhds_of_uniform_embedding [uniform_space α] [uniform_space β] {s : set (α×α)} {e : α → β} (b : β) (he₁ : uniform_embedding e) (he₂ : dense_embedding e) (hs : s ∈ (@uniformity α _).sets) : ∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ (nhds b).sets := have s ∈ (vmap (λp:α×α, (e p.1, e p.2)) $ uniformity).sets, from he₁.right.symm ▸ hs, let ⟨t₁, ht₁u, (ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s)⟩ := this in let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in have preimage e {b' | (b, b') ∈ t₂} ∈ (vmap e $ nhds b).sets, from preimage_mem_vmap $ mem_nhds_left ht₂u, let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := inhabited_of_mem_sets (he₂.vmap_nhds_neq_bot) this in have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ (@uniformity β _).sets → {y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s} ≠ ∅, from assume b' s' hb' hs', have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ (vmap e $ nhds b').sets, from preimage_mem_vmap $ mem_nhds_left $ inter_mem_sets hs' htu, let ⟨a₂, ha₂s', ha₂t⟩ := inhabited_of_mem_sets (he₂.vmap_nhds_neq_bot) this in have (e a, e a₂) ∈ t₁, from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t, have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s}, from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩, ne_empty_of_mem this, have ∀b', (b, b') ∈ t → nhds b' ⊓ principal (e '' {a' | (a, a') ∈ s}) ≠ ⊥, begin intros b' hb', rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_neq_bot_iff], exact assume s, this b' s hb', exact monotone_inter monotone_preimage monotone_const end, have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}), from assume b' hb', by rw [closure_eq_nhds]; exact this b' hb', ⟨a, (nhds b).upwards_sets (mem_nhds_left htu) this⟩ /- cauchy filters -/ definition cauchy (f : filter α) := f ≠ ⊥ ∧ filter.prod f f ≤ uniformity lemma cauchy_downwards {f g : filter α} (h_c : cauchy f) (hg : g ≠ ⊥) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy_nhds {a : α} : cauchy (nhds a) := ⟨nhds_neq_bot, calc filter.prod (nhds a) (nhds a) = uniformity.lift (λs:set (α×α), uniformity.lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ uniformity.lift' (λs:set (α×α), comp_rel 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_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ uniformity : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_downwards cauchy_nhds (show principal {a} ≠ ⊥, by simp) (return_le_nhds a) lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : f ⊓ nhds x ≠ ⊥) : f ≤ nhds x := have ∀s∈f.sets, x ∈ closure s, begin intros s hs, simp [closure_eq_nhds, inf_comm], exact assume h', adhs $ bot_unique $ h' ▸ inf_le_inf (by simp; exact hs) (le_refl _) end, calc f ≤ f.lift' (λs:set α, {y | x ∈ closure s ∧ y ∈ closure s}) : le_infi $ assume s, le_infi $ assume hs, begin rw [←forall_sets_neq_empty_iff_neq_bot] at adhs, simp [this s hs], exact f.upwards_sets hs subset_closure end ... ≤ f.lift' (λs:set α, {y | (x, y) ∈ closure (set.prod s s)}) : by simp [closure_prod_eq]; exact le_refl _ ... = (filter.prod f f).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : begin rw [prod_same_eq], rw [lift'_lift'_assoc], exact monotone_prod monotone_id monotone_id, exact monotone_comp (assume s t h x h', closure_mono h h') monotone_preimage end ... ≤ uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : lift'_mono hf.right (le_refl _) ... = (uniformity.lift' closure).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : begin rw [lift'_lift'_assoc], exact assume s t h, closure_mono h, exact monotone_preimage end ... = uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ s}) : by rw [←uniformity_eq_uniformity_closure] ... = nhds x : by rw [nhds_eq_uniformity] lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ nhds x ↔ f ⊓ nhds x ≠ ⊥ := ⟨assume h, (inf_of_le_left h).symm ▸ hf.left, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy_map [uniform_space β] {f : filter α} {m : α → β} (hm : uniform_continuous m) (hf : cauchy f) : cauchy (map m f) := ⟨have f ≠ ⊥, from hf.left, by simp; assumption, calc filter.prod (map m f) (map m f) = map (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) uniformity : map_mono hf.right ... ≤ uniformity : hm⟩ lemma cauchy_vmap [uniform_space β] {f : filter β} {m : α → β} (hm : vmap (λp:α×α, (m p.1, m p.2)) uniformity ≤ uniformity) (hf : cauchy f) (hb : vmap m f ≠ ⊥) : cauchy (vmap m f) := ⟨hb, calc filter.prod (vmap m f) (vmap m f) = vmap (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_vmap_vmap_eq ... ≤ vmap (λp:α×α, (m p.1, m p.2)) uniformity : vmap_mono hf.right ... ≤ uniformity : hm⟩ /- separated uniformity -/ protected def separation_rel (α : Type u) [u : uniform_space α] := (⋂₀ (@uniformity α _).sets) lemma separated_equiv : equivalence (λx y, (x, y) ∈ separation_rel α) := ⟨assume x, assume s, refl_mem_uniformity, assume x y, assume h (s : set (α×α)) hs, have preimage prod.swap s ∈ (@uniformity α _).sets, from symm_le_uniformity hs, h _ this, assume x y z (hxy : (x, y) ∈ separation_rel α) (hyz : (y, z) ∈ separation_rel α) s (hs : s ∈ (@uniformity α _).sets), let ⟨t, ht, (h_ts : comp_rel t t ⊆ s)⟩ := comp_mem_uniformity_sets hs in h_ts $ show (x, z) ∈ comp_rel t t, from ⟨y, hxy t ht, hyz t ht⟩⟩ protected def separation_setoid (α : Type u) [u : uniform_space α] : setoid α := ⟨λx y, (x, y) ∈ separation_rel α, separated_equiv⟩ @[class] definition separated (α : Type u) [u : uniform_space α] := separation_rel α = id_rel instance separated_t2 [s : separated α] : t2_space α := ⟨assume x y, assume h : x ≠ y, have separation_rel α = id_rel, from s, have (x, y) ∉ separation_rel α, by simp [this]; exact h, let ⟨d, hd, (hxy : (x, y) ∉ d)⟩ := classical.bexists_not_of_not_bforall this in let ⟨d', hd', (hd'd' : comp_rel d' d' ⊆ d)⟩ := comp_mem_uniformity_sets hd in have {y | (x, y) ∈ d'} ∈ (nhds x).sets, from mem_nhds_left hd', let ⟨u, hu₁, hu₂, hu₃⟩ := mem_nhds_sets_iff.mp this in have {x | (x, y) ∈ d'} ∈ (nhds y).sets, from mem_nhds_right hd', let ⟨v, hv₁, hv₂, hv₃⟩ := mem_nhds_sets_iff.mp this in have u ∩ v = ∅, from eq_empty_of_subset_empty $ assume z ⟨(h₁ : z ∈ u), (h₂ : z ∈ v)⟩, have (x, y) ∈ comp_rel d' d', from ⟨z, hu₁ h₁, hv₁ h₂⟩, hxy $ hd'd' this, ⟨u, v, hu₂, hv₂, hu₃, hv₃, this⟩⟩ instance separated_regular [separated α] : regular_space α := { separated_t2 with regular := λs a hs ha, have -s ∈ (nhds a).sets, from mem_nhds_sets hs ha, have {p : α × α | p.1 = a → p.2 ∈ -s} ∈ uniformity.sets, from mem_nhds_uniformity_iff.mp this, let ⟨d, hd, h⟩ := comp_mem_uniformity_sets this in let e := {y:α| (a, y) ∈ d} in have hae : a ∈ closure e, from subset_closure $ refl_mem_uniformity hd, have set.prod (closure e) (closure e) ⊆ comp_rel d (comp_rel (set.prod e e) d), begin rw [←closure_prod_eq, closure_eq_inter_uniformity], change (⨅d' ∈ uniformity.sets, _) ≤ comp_rel d (comp_rel _ d), exact (infi_le_of_le d $ infi_le_of_le hd $ le_refl _) end, have e_subset : closure e ⊆ -s, from assume a' ha', let ⟨x, (hx : (a, x) ∈ d), y, ⟨hx₁, hx₂⟩, (hy : (y, _) ∈ d)⟩ := @this ⟨a, a'⟩ ⟨hae, ha'⟩ in have (a, a') ∈ comp_rel d d, from ⟨y, hx₂, hy⟩, h this rfl, have closure e ∈ (nhds a).sets, from (nhds a).upwards_sets (mem_nhds_left hd) subset_closure, have nhds a ⊓ principal (-closure e) = ⊥, from (@inf_eq_bot_iff_le_compl _ _ _ (principal (- closure e)) (principal (closure e)) (by simp [principal_univ]) (by simp)).mpr (by simp [this]), ⟨- closure e, is_closed_closure, assume x h₁ h₂, @e_subset x h₂ h₁, this⟩ } /- totally bounded -/ def totally_bounded (s : set α) : Prop := ∀d ∈ (@uniformity α _).sets, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) lemma totally_bounded_subset [uniform_space α] {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_closure [uniform_space α] {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : closure_eq_of_is_closed $ is_closed_Union hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_prod_mk continuous_id continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ lemma totally_bounded_image [uniform_space α] [uniform_space β] {f : α → β} {s : set α} (hf : uniform_continuous f) (hs : totally_bounded s) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ (@uniformity α _).sets, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, finite_image hfc, begin simp [image_subset_iff_subset_preimage], simp [subset_def] at hct, exact (assume x hx, let ⟨i, hi, ht⟩ := hct x hx in ⟨f i, mem_image_of_mem f hi, ht⟩) end⟩ lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α} (hs : totally_bounded s) (hf : ultrafilter f) (h : f ≤ principal s) : cauchy f := ⟨hf.left, assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets, from f.upwards_sets (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets, from mem_of_finite_Union_ultrafilter hf hi this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, (filter.prod f f).upwards_sets (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c) := ⟨assume : totally_bounded s, assume f hf hs, ⟨ultrafilter_of f, ultrafilter_of_le, cauchy_of_totally_bounded_of_ultrafilter this (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hs)⟩, assume h : ∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c, assume d hd, classical.by_contradiction $ assume hs, have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}), by simp [not_exists_iff, not_and_iff] at hs; simp [implies_iff_not_or, hs], let f := ⨅t:{t : set α // finite t}, principal (s \ (⋃y∈t.val, {x | (x,y) ∈ d})), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h, hd_cover finite.empty $ h.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, finite_union ht₁ ht₂⟩, principal_mono.mpr $ diff_right_antimono $ Union_subset_Union $ assume t, Union_subset_Union_const or.inl, principal_mono.mpr $ diff_right_antimono $ Union_subset_Union $ assume t, Union_subset_Union_const or.inr⟩) (assume ⟨t, ht⟩, by simp [diff_neq_empty]; exact hd_cover ht), have f ≤ principal s, from infi_le_of_le ⟨∅, finite.empty⟩ $ by simp; exact subset.refl s, let ⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this, ⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α d c).mp $ hc₂.right hd in have c ≤ principal s, from le_trans ‹c ≤ f› this, have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this, let ⟨y, hym, hys⟩ := inhabited_of_mem_sets hc₂.left this in let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in have m ⊆ ys, from assume y' hy', show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}), by simp; exact @hmd (y', y) ⟨hy', hym⟩, have c ≤ principal (s - ys), from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_insert finite.empty⟩ $ le_refl _, have (s - ys) ∩ (m ∩ s) ∈ c.sets, from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›, have ∅ ∈ c.sets, from c.upwards_sets this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm, hc₂.left $ empty_in_sets_eq_bot.mp this⟩ lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f, ultrafilter f → f ≤ principal s → cauchy f) := ⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs, assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs, have cauchy (ultrafilter_of f), from h (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs), ⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩ lemma compact_of_totally_bounded_complete {s : set α} (ht : totally_bounded s) (hc : ∀{f:filter α}, cauchy f → f ≤ principal s → ∃x∈s, f ≤ nhds x) : compact s := begin rw [compact_iff_ultrafilter_le_nhds], rw [totally_bounded_iff_ultrafilter] at ht, exact assume f hf hfs, hc (ht _ hf hfs) hfs end /- complete space -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ nhds x) lemma complete_of_is_closed [complete_space α] {s : set α} {f : filter α} (h : is_closed s) (hf : cauchy f) (hfs : f ≤ principal s) : ∃x∈s, f ≤ nhds x := let ⟨x, hx⟩ := complete_space.complete hf in have x ∈ s, from is_closed_iff_nhds.mp h x $ neq_bot_of_le_neq_bot hf.left $ le_inf hx hfs, ⟨x, this, hx⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : compact s := @compact_of_totally_bounded_complete α _ s ht $ assume f, complete_of_is_closed hc lemma complete_space_extension [uniform_space β] {m : β → α} (hm : uniform_embedding m) (dense : ∀x, x ∈ closure (m '' univ)) (h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ nhds x) : complete_space α := ⟨assume (f : filter α), assume hf : cauchy f, let p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s}, g := uniformity.lift (λs, f.lift' (p s)) in have mp₀ : monotone p, from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩, have mp₁ : ∀{s}, monotone (p s), from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩, have f ≤ g, from le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, le_principal_iff.mpr $ f.upwards_sets ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩, have g ≠ ⊥, from neq_bot_of_le_neq_bot hf.left this, have vmap m g ≠ ⊥, from vmap_neq_bot $ assume t ht, let ⟨t', ht', ht_mem⟩ := (mem_lift_iff $ monotone_lift' monotone_const mp₀).mp ht in let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_iff mp₁).mp ht_mem in let ⟨x, (hx : x ∈ t'')⟩ := inhabited_of_mem_sets hf.left ht'' in have h₀ : nhds x ⊓ principal (m '' univ) ≠ ⊥, by simp [closure_eq_nhds] at dense; exact dense x, have h₁ : {y | (x, y) ∈ t'} ∈ (nhds x ⊓ principal (m '' univ)).sets, from @mem_inf_sets_of_left α (nhds x) (principal (m '' univ)) _ $ mem_nhds_left ht', have h₂ : m '' univ ∈ (nhds x ⊓ principal (m '' univ)).sets, from @mem_inf_sets_of_right α (nhds x) (principal (m '' univ)) _ $ subset.refl _, have {y | (x, y) ∈ t'} ∩ m '' univ ∈ (nhds x ⊓ principal (m '' univ)).sets, from @inter_mem_sets α (nhds x ⊓ principal (m '' univ)) _ _ h₁ h₂, let ⟨y, xyt', b, _, b_eq⟩ := inhabited_of_mem_sets h₀ this in ⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩, have cauchy g, from ⟨‹g ≠ ⊥›, assume s hs, let ⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs, ⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁, ⟨t, ht, (prod_t : set.prod t t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂) in have hg₁ : p (preimage prod.swap s₁) t ∈ g.sets, from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht, have hg₂ : p s₂ t ∈ g.sets, from mem_lift hs₂ $ @mem_lift' α α f _ t ht, have hg : set.prod (p (preimage prod.swap s₁) t) (p s₂ t) ∈ (filter.prod g g).sets, from @prod_mem_prod α α _ _ g g hg₁ hg₂, (filter.prod g g).upwards_sets hg (assume ⟨a, b⟩ ⟨⟨c₁, c₁t, (hc₁ : (a, c₁) ∈ s₁)⟩, ⟨c₂, c₂t, (hc₂ : (c₂, b) ∈ s₂)⟩⟩, have (c₁, c₂) ∈ set.prod t t, from ⟨c₁t, c₂t⟩, comp_s₁ $ prod_mk_mem_comp_rel hc₁ $ comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩, have cauchy (filter.vmap m g), from cauchy_vmap (le_of_eq hm.right) ‹cauchy g› (by assumption), let ⟨x, (hx : map m (filter.vmap m g) ≤ nhds x)⟩ := h _ this in have map m (filter.vmap m g) ⊓ nhds x ≠ ⊥, from (le_nhds_iff_adhp_of_cauchy (cauchy_map (uniform_continuous_of_embedding hm) this)).mp hx, have g ⊓ nhds x ≠ ⊥, from neq_bot_of_le_neq_bot this (inf_le_inf (assume s hs, ⟨s, hs, subset.refl _⟩) (le_refl _)), ⟨x, calc f ≤ g : by assumption ... ≤ nhds x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩ /- separation space -/ section separation_space local attribute [instance] separation_setoid instance {α : Type u} [u : uniform_space α] : uniform_space (quotient (separation_setoid α)) := { uniform_space . to_topological_space := u.to_topological_space.coinduced (λx, ⟦x⟧), uniformity := map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity, refl := assume s hs ⟨a, b⟩ (h : a = b), have ∀a:α, (a, a) ∈ preimage (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) s, from assume a, refl_mem_uniformity hs, h ▸ quotient.induction_on a this, symm := tendsto_map' $ by simp [prod.swap, (∘)]; exact tendsto_compose tendsto_swap_uniformity tendsto_map, comp := calc (map (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity).lift' (λs, comp_rel s s) = uniformity.lift' ((λs, comp_rel s s) ∘ image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧))) : map_lift'_eq2 $ monotone_comp_rel monotone_id monotone_id ... ≤ uniformity.lift' (image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ∘ (λs:set (α×α), comp_rel s (comp_rel s s))) : lift'_mono' $ assume s hs ⟨a, b⟩ ⟨c, ⟨⟨a₁, a₂⟩, ha, a_eq⟩, ⟨⟨b₁, b₂⟩, hb, b_eq⟩⟩, begin simp at a_eq, simp at b_eq, have h : ⟦a₂⟧ = ⟦b₁⟧, { rw [a_eq.right, b_eq.left] }, have h : (a₂, b₁) ∈ separation_rel α := quotient.exact h, simp [function.comp, set.image, comp_rel], exact ⟨a₁, a_eq.left, b₂, b_eq.right, a₂, ha, b₁, h s hs, hb⟩ end ... = map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) (uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s))) : by rw [map_lift'_eq]; exact monotone_comp_rel monotone_id (monotone_comp_rel monotone_id monotone_id) ... ≤ map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity : map_mono comp_le_uniformity3, is_open_uniformity := assume s, have ∀a, ⟦a⟧ ∈ s → ({p:α×α | p.1 = a → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets ↔ {p:α×α | ⟦p.1⟧ = ⟦a⟧ → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets), from assume a ha, ⟨assume h, let ⟨t, ht, hts⟩ := comp_mem_uniformity_sets h in have hts : ∀{a₁ a₂}, (a, a₁) ∈ t → (a₁, a₂) ∈ t → ⟦a₂⟧ ∈ s, from assume a₁ a₂ ha₁ ha₂, @hts (a, a₂) ⟨a₁, ha₁, ha₂⟩ rfl, have ht' : ∀{a₁ a₂}, ⟦a₁⟧ = ⟦a₂⟧ → (a₁, a₂) ∈ t, from assume a₁ a₂ h, sInter_subset_of_mem ht (quotient.exact h), uniformity.upwards_sets ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' h₂.symm) h₁, assume h, uniformity.upwards_sets h $ by simp {contextual := tt}⟩, begin simp [topological_space.coinduced, u.is_open_uniformity, uniformity, forall_quotient_iff], exact ⟨λh a ha, (this a ha).mp $ h a ha, λh a ha, (this a ha).mpr $ h a ha⟩ end } lemma uniform_continuous_quotient_mk : uniform_continuous (quotient.mk : α → quotient (separation_setoid α)) := le_refl _ lemma vmap_quotient_le_uniformity : vmap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity ≤ uniformity := assume t' ht', let ⟨t, ht, tt_t'⟩ := comp_mem_uniformity_sets ht' in let ⟨s, hs, ss_t⟩ := comp_mem_uniformity_sets ht in ⟨(λp:α×α, (⟦p.1⟧, ⟦p.2⟧)) '' s, (@uniformity α _).upwards_sets hs $ assume x hx, ⟨x, hx, rfl⟩, assume ⟨a₁, a₂⟩ ⟨⟨b₁, b₂⟩, hb, ab_eq⟩, have ⟦b₁⟧ = ⟦a₁⟧ ∧ ⟦b₂⟧ = ⟦a₂⟧, from prod.mk.inj ab_eq, have b₁ ≈ a₁ ∧ b₂ ≈ a₂, from and.imp quotient.exact quotient.exact this, have ab₁ : (a₁, b₁) ∈ t, from (setoid.symm this.left) t ht, have ba₂ : (b₂, a₂) ∈ s, from this.right s hs, tt_t' ⟨b₁, show ((a₁, a₂).1, b₁) ∈ t, from ab₁, ss_t ⟨b₂, show ((b₁, a₂).1, b₂) ∈ s, from hb, ba₂⟩⟩⟩ lemma vmap_quotient_eq_uniformity : vmap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity = uniformity := le_antisymm vmap_quotient_le_uniformity (assume s ⟨t, ht, hs⟩, uniformity.upwards_sets ht hs) lemma complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (vmap (λx, ⟦x⟧) f), from cauchy_vmap vmap_quotient_le_uniformity hf $ vmap_neq_bot_of_surj hf.left $ assume b, quotient.exists_rep _, let ⟨x, (hx : vmap (λx, ⟦x⟧) f ≤ nhds x)⟩ := complete_space.complete this in ⟨⟦x⟧, calc f ≤ map (λx, ⟦x⟧) (vmap (λx, ⟦x⟧) f) : le_map_vmap $ assume b, quotient.exists_rep _ ... ≤ map (λx, ⟦x⟧) (nhds x) : map_mono hx ... ≤ _ : continuous_iff_tendsto.mp (continuous_of_uniform uniform_continuous_quotient_mk) _⟩⟩ lemma separated_separation [h : complete_space α] : separated (quotient (separation_setoid α)) := set.ext $ assume ⟨a, b⟩, quotient.induction_on₂ a b $ assume a b, ⟨assume h, have a ≈ b, from assume s hs, have s ∈ (vmap (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity).sets, from vmap_quotient_le_uniformity hs, let ⟨t, ht, hts⟩ := this in hts begin dsimp, exact h t ht end, show ⟦a⟧ = ⟦b⟧, from quotient.sound this, assume heq : ⟦a⟧ = ⟦b⟧, assume h hs, heq ▸ refl_mem_uniformity hs⟩ end separation_space section uniform_extension variables [uniform_space β] [uniform_space γ] {e : β → α} (h_e : uniform_embedding e) (h_dense : ∀x, x ∈ closure (e '' univ)) {f : β → γ} (h_f : uniform_continuous f) [inhabited γ] local notation `ψ` := (dense_embedding_of_uniform_embedding h_e h_dense).ext f lemma uniformly_extend_of_emb [cγ : complete_space γ] [sγ : separated γ] {b : β} : ψ (e b) = f b := dense_embedding.ext_e_eq _ $ continuous_iff_tendsto.mp (continuous_of_uniform h_f) b lemma uniformly_extend_exists [complete_space γ] [sγ : separated γ] {a : α} : ∃c, tendsto f (vmap e (nhds a)) (nhds c) := let de := (dense_embedding_of_uniform_embedding h_e h_dense) in have cauchy (nhds a), from cauchy_nhds, have cauchy (vmap e (nhds a)), from cauchy_vmap (le_of_eq h_e.right) this de.vmap_nhds_neq_bot, have cauchy (map f (vmap e (nhds a))), from cauchy_map h_f this, complete_space.complete this lemma uniformly_extend_spec [complete_space γ] [sγ : separated γ] {a : α} : tendsto f (vmap e (nhds a)) (nhds (ψ a)) := lim_spec $ uniformly_extend_exists h_e h_dense h_f lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] [sγ : separated γ] : uniform_continuous ψ := assume d hd, let ⟨s, hs, (hs_comp : comp_rel s (comp_rel s s) ⊆ d)⟩ := (mem_lift'_iff $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in have h_pnt : ∀{a m}, m ∈ (nhds a).sets → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s, from assume a m hm, have nb : map f (vmap e (nhds a)) ≠ ⊥, from map_ne_bot (dense_embedding_of_uniform_embedding h_e h_dense).vmap_nhds_neq_bot, have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ (map f (vmap e (nhds a))).sets, from inter_mem_sets (image_mem_map $ preimage_mem_vmap $ hm) (uniformly_extend_spec h_e h_dense h_f $ inter_mem_sets (mem_nhds_right hs) (mem_nhds_left hs)), inhabited_of_mem_sets nb this, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ (@uniformity β _).sets, from h_f hs, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ (vmap (λx:β×β, (e x.1, e x.2)) uniformity).sets, by rwa [h_e.right.symm] at this, let ⟨t, ht, (ts : ∀p:(β×β), (e p.1, e p.2) ∈ t → (f p.1, f p.2) ∈ s)⟩ := this in show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ uniformity.sets, from (@uniformity α _).upwards_sets (interior_mem_uniformity ht) $ assume ⟨x₁, x₂⟩ hx_t, have nhds (x₁, x₂) ≤ principal (interior t), from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t, have interior t ∈ (filter.prod (nhds x₁) (nhds x₂)).sets, by rwa [nhds_prod_eq, le_principal_iff] at this, let ⟨m₁, hm₁, m₂, hm₂, (hm : set.prod m₁ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in have set.prod (preimage e m₁) (preimage e m₂) ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s, from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm ... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset ... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts, have set.prod (f '' preimage e m₁) (f '' preimage e m₂) ⊆ s, from calc set.prod (f '' preimage e m₁) (f '' preimage e m₂) = (λp:(β×β), (f p.1, f p.2)) '' (set.prod (preimage e m₁) (preimage e m₂)) : prod_image_image_eq ... ⊆ (λp:(β×β), (f p.1, f p.2)) '' preimage (λp:(β×β), (f p.1, f p.2)) s : mono_image this ... ⊆ s : image_subset_iff_subset_preimage.mpr $ subset.refl _, have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩, hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s), from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩ end uniform_extension end uniform_space end /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ (filter.prod (p.1.val) (p.2.val)).sets } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem_sets (α×α) (filter.prod (p.1.val) (p.2.val)) private lemma symm_gen : map prod.swap (uniformity.lift' gen) ≤ uniformity.lift' gen := calc map prod.swap (uniformity.lift' gen) = uniformity.lift' (λs:set (α×α), {p | s ∈ (filter.prod (p.2.val) (p.1.val)).sets }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem_sets, function.comp, image_swap_eq_preimage_swap] end ... ≤ uniformity.lift' gen : uniformity_lift_le_swap (monotone_comp (monotone_set_of $ assume p, @monotone_mem_sets (α×α) ((filter.prod ((p.2).val) ((p.1).val)))) monotone_principal) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h], exact le_refl _ end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val.sets), t₂, (ht₂ : t₂ ∈ h.val.sets), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val.sets), t₄, (ht₄ : t₄ ∈ g.val.sets), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val.sets, from inter_mem_sets ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := inhabited_of_mem_sets (h.property.left) this in (filter.prod f.val g.val).upwards_sets (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : (uniformity.lift' gen).lift' (λs, comp_rel s s) ≤ uniformity.lift' gen := calc (uniformity.lift' gen).lift' (λs, comp_rel s s) = uniformity.lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ uniformity.lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = (uniformity.lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ uniformity.lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance completion_space : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := uniformity.lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := ⟨assume a₁ a₂ h, have (pure_cauchy a₁).val = (pure_cauchy a₂).val, from congr_arg _ h, have {a₁} = ({a₂} : set α), from principal_eq_iff_eq.mp this, by simp at this; assumption, have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc vmap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) (uniformity.lift' gen) = uniformity.lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : vmap_lift'_eq monotone_gen ... = uniformity : by simp [this]⟩ lemma pure_cauchy_dense : ∀x, x ∈ closure (pure_cauchy '' univ) := assume f, have h_ex : ∀s∈(@uniformity (Cauchy α) _).sets, ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_iff monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ (filter.prod (f.val) (f.val)).sets, from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := inhabited_of_mem_sets f.property.left ht in have t'' ∈ (filter.prod f.val (pure x)).sets, from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, assume y, begin simp, intro h, simp [h], exact refl_mem_uniformity ht'₁ end, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp [closure_eq_nhds, nhds_eq_uniformity, lift'_inf_principal_eq], exact (lift'_neq_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ pure_cauchy '' univ ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_image_of_mem _ $ mem_univ y, hy⟩, ne_empty_of_mem this) end instance : complete_space (Cauchy α) := complete_space_extension uniform_embedding_pure_cauchy pure_cauchy_dense $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ uniformity.lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_iff monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (filter.prod f (pure x)).upwards_sets (prod_mem_prod ht' $ mem_pure hx) h, f.upwards_sets ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end end Cauchy instance nonempty_Cauchy {α : Type u} [h : nonempty α] [uniform_space α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a instance inhabited_Cauchy {α : Type u} [inhabited α] [uniform_space α] : inhabited (Cauchy α) := ⟨Cauchy.pure_cauchy $ default α⟩ section constructions variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} instance : partial_order (uniform_space α) := { le := λt s, s.uniformity ≤ t.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₂ h₁, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ h₂, @le_trans _ _ c.uniformity b.uniformity a.uniformity h₂ h₁ } instance : has_Sup (uniform_space α) := ⟨assume s, uniform_space.of_core { uniform_space.core . uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma le_Sup {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : t ≤ Sup tt := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma Sup_le {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t' ≤ t) : Sup tt ≤ t := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_bot (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_top (uniform_space α) := ⟨{ to_topological_space := ⊤, uniformity := principal id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], { simp, apply subset.refl }, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := by rw [topological_space.lattice.has_top]; simp [subset_def, id_rel] {contextual := tt } }⟩ instance : complete_lattice (uniform_space α) := { uniform_space.partial_order with sup := λa b, Sup {a, b}, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c h₁ h₂, Sup_le $ assume t', begin simp, intro h, cases h with h h, repeat { subst h; assumption } end, inf := λa b, Sup {x | x ≤ a ∧ x ≤ b}, le_inf := assume a b c h₁ h₂, le_Sup ⟨h₁, h₂⟩, inf_le_left := assume a b, Sup_le $ assume x ⟨ha, hb⟩, ha, inf_le_right := assume a b, Sup_le $ assume x ⟨ha, hb⟩, hb, top := ⊤, le_top := assume u, u.refl, bot := ⊥, bot_le := assume a, show a.uniformity ≤ ⊤, from le_top, Sup := Sup, le_Sup := assume s u, le_Sup, Sup_le := assume s u, Sup_le, Inf := λtt, Sup {t | ∀t'∈tt, t ≤ t'}, le_Inf := assume s a hs, le_Sup hs, Inf_le := assume s a ha, Sup_le $ assume u hs, hs _ ha } lemma supr_uniformity {ι : Sort v} {u : ι → uniform_space α} : (supr u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, a = u i), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : a = u i)⟩, ha.symm ▸ infi_le _ _) lemma sup_uniformity {u v : uniform_space α} : (u ⊔ v).uniformity = u.uniformity ⊓ v.uniformity := have (u ⊔ v) = (⨆i (h : i = u ∨ i = v), i), by by simp [supr_or, supr_sup_eq], calc (u ⊔ v).uniformity = ((⨆i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this] ... = _ : by simp [supr_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊤⟩ def uniform_space.vmap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.vmap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (vmap_mono u.refl), symm := tendsto_vmap' $ by simp [prod.swap, (∘)]; exact tendsto_compose tendsto_vmap tendsto_swap_uniformity, comp := le_trans begin rw [vmap_lift'_eq, vmap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), repeat { exact monotone_comp_rel monotone_id monotone_id } end (vmap_mono u.comp), is_open_uniformity := begin intro s, change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced_eq_vmap, mem_nhds_uniformity_iff, filter.vmap], exact (forall_congr $ assume x, forall_congr $ assume hx, ⟨assume ⟨t, hts, ht⟩, ⟨_, ht, assume ⟨x₁, x₂⟩, by simp [*, subset_def] at * {contextual := tt} ⟩, assume ⟨t, ht, hts⟩, ⟨{y:β | (f x, y) ∈ t}, assume y (hy : (f x, f y) ∈ t), @hts (x, y) hy rfl, mem_nhds_uniformity_iff.mp $ mem_nhds_left ht⟩⟩) end } lemma uniform_continuous_vmap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.vmap f u) u f := tendsto_vmap theorem to_topological_space_vmap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.vmap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := eq_of_nhds_eq_nhds $ assume a, begin simp [nhds_induced_eq_vmap, nhds_eq_uniformity, nhds_eq_uniformity], change vmap f (uniformity.lift' (preimage (λb, (f a, b)))) = (u.uniformity.vmap (λp:α×α, (f p.1, f p.2))).lift' (preimage (λa', (a, a'))), rw [vmap_lift'_eq monotone_preimage, vmap_lift'_eq2 monotone_preimage], exact rfl end lemma uniform_continuous_vmap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.vmap f v) g := tendsto_vmap' h lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _) lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := rfl lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := bot_unique $ assume s hs, classical.by_cases (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊥) (assume : s ≠ ∅, let ⟨x, hx⟩ := exists_mem_of_ne_empty this in have univ ⊆ _, from hs x hx, have s = univ, from top_unique $ assume y hy, @this (x, y) ⟨⟩ rfl, this.symm ▸ @is_open_univ _ ⊥) lemma to_topological_space_supr {ι : Sort v} {u : ι → uniform_space α} : @uniform_space.to_topological_space α (supr u) = (⨆i, @uniform_space.to_topological_space α (u i)) := classical.by_cases (assume h : nonempty ι, eq_of_nhds_eq_nhds $ assume a, begin rw [nhds_supr, nhds_eq_uniformity], change _ = (supr u).uniformity.lift' (preimage $ prod.mk a), begin rw [supr_uniformity, lift'_infi], exact (congr_arg _ $ funext $ assume i, @nhds_eq_uniformity α (u i) a), exact h, exact assume a b, rfl end end) (assume : ¬ nonempty ι, le_antisymm (have supr u = ⊥, from bot_unique $ supr_le $ assume i, (this ⟨i⟩).elim, have @uniform_space.to_topological_space _ (supr u) = ⊥, from this.symm ▸ to_topological_space_bot, this.symm ▸ bot_le) (supr_le $ assume i, to_topological_space_mono $ le_supr _ _)) lemma to_topological_space_Sup {s : set (uniform_space α)} : @uniform_space.to_topological_space α (Sup s) = (⨆i∈s, @uniform_space.to_topological_space α i) := begin rw [Sup_eq_supr, to_topological_space_supr], apply congr rfl, apply funext, intro x, exact to_topological_space_supr end lemma to_topological_space_sup {u v : uniform_space α} : @uniform_space.to_topological_space α (u ⊔ v) = @uniform_space.to_topological_space α u ⊔ @uniform_space.to_topological_space α v := ord_continuous_sup $ assume s, to_topological_space_Sup instance : uniform_space empty := ⊤ instance : uniform_space unit := ⊤ instance : uniform_space bool := ⊤ instance : uniform_space ℕ := ⊤ instance : uniform_space ℤ := ⊤ instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.vmap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : (@uniformity (subtype p) _) = vmap (λq:subtype p × subtype p, (q.1.1, q.2.1)) uniformity := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_vmap lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_vmap' hf instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := uniform_space.of_core_eq (u₁.vmap prod.fst ⊔ u₂.vmap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.vmap prod.fst ⊔ u₂.vmap prod.snd).to_topological_space : by rw [to_topological_space_sup, to_topological_space_vmap, to_topological_space_vmap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) lemma uniform_embedding_subtype_emb {α : Type u} {β : Type v} [uniform_space α] [uniform_space β] (p : α → Prop) {e : α → β} (ue : uniform_embedding e) (de : dense_embedding e) : uniform_embedding (de.subtype_emb p) := ⟨(de.subtype p).inj, by simp [vmap_vmap_comp, (∘), dense_embedding.subtype_emb, uniformity_subtype, ue.right.symm]⟩ lemma uniform_extend_subtype {α : Type u} {β : Type v} {γ : Type w} [uniform_space α] [uniform_space β] [uniform_space γ] [complete_space γ] [inhabited γ] [separated γ] {p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α} (hf : uniform_continuous (λx:subtype p, f x.val)) (he : uniform_embedding e) (hd : ∀x, x ∈ closure (e '' univ)) (hb : closure (e '' s) ∈ (nhds b).sets) (hs : is_closed s) (hp : ∀x∈s, p x) : ∃c, tendsto f (vmap e (nhds b)) (nhds c) := have de : dense_embedding e, from dense_embedding_of_uniform_embedding he hd, have de' : dense_embedding (de.subtype_emb p), by exact de.subtype p, have ue' : uniform_embedding (de.subtype_emb p), from uniform_embedding_subtype_emb _ he de, have b ∈ closure (e '' {x | p x}), from (closure_mono $ mono_image $ hp) (mem_of_nhds hb), let ⟨c, (hc : tendsto (f ∘ subtype.val) (vmap (de.subtype_emb p) (nhds ⟨b, this⟩)) (nhds c))⟩ := uniformly_extend_exists ue' de'.dense hf in begin rw [nhds_subtype_eq_vmap] at hc, simp [vmap_vmap_comp] at hc, change (tendsto (f ∘ @subtype.val α p) (vmap (e ∘ @subtype.val α p) (nhds b)) (nhds c)) at hc, rw [←vmap_vmap_comp] at hc, existsi c, apply tendsto_vmap'' s _ _ hc, exact ⟨_, hb, assume x, begin change e x ∈ (closure (e '' s)) → x ∈ s, rw [←closure_induced, closure_eq_nhds], dsimp, rw [nhds_induced_eq_vmap, de.induced], change x ∈ {x | nhds x ⊓ principal s ≠ ⊥} → x ∈ s, rw [←closure_eq_nhds, closure_eq_of_is_closed hs], exact id, exact de.inj end⟩, exact (assume x hx, ⟨⟨x, hp x hx⟩, rfl⟩) end /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ lemma uniformity_prod [uniform_space α] [uniform_space β] : @uniformity (α×β) _ = vmap (λp:(α×β)×(α×β), (p.1.1, p.2.1)) uniformity ⊓ vmap (λp:(α×β)×(α×β), (p.1.2, p.2.2)) uniformity := by rw [prod.uniform_space, uniform_space.of_core_eq_to_core, uniformity, sup_uniformity]; refl lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ (@uniformity α _).sets) (hb : b ∈ (@uniformity β _).sets) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _).sets := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_vmap ha) (preimage_mem_vmap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) uniformity uniformity := le_trans (map_mono (@le_sup_left (uniform_space (α×β)) _ _ _)) map_vmap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) uniformity uniformity := le_trans (map_mono (@le_sup_right (uniform_space (α×β)) _ _ _)) map_vmap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd lemma uniform_continuous_prod_mk [uniform_space α] [uniform_space β] [uniform_space γ] {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf (tendsto_vmap' h₁) (tendsto_vmap' h₂) lemma uniform_embedding_prod {α' : Type w} {β' : Type x} [uniform_space α] [uniform_space β] [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) : uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) := ⟨assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, by simp [prod.mk.inj_iff]; exact assume ⟨eq₁, eq₂⟩, ⟨h₁.left _ _ eq₁, h₂.left _ _ eq₂⟩, by simp [(∘), uniformity_prod, h₁.right.symm, h₂.right.symm, vmap_inf, vmap_vmap_comp]⟩ lemma to_topological_space_prod [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl end constructions
a3bae495cb4948125d871dc58738d5a12ff83210
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/init/reserved_notation.hlean
dbf2d60f07828e7efcbe5d3371382d5fc5771c96
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
2,183
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.datatypes notation `assume` binders `,` r:(scoped f, f) := r notation `take` binders `,` r:(scoped f, f) := r /- Global declarations of right binding strength If a module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ definition std.prec.max : num := 1024 -- the strength of application, identifiers, (, [, etc. definition std.prec.arrow : num := 25 /- The next definition is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ definition std.prec.max_plus := num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ std.prec.max))))))))) /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr `∧`:35 reserve infixr `/\`:35 reserve infixr `\/`:30 reserve infixr `∨`:30 reserve infix `<->`:20 reserve infix `↔`:20 reserve infix `=`:50 reserve infix `≠`:50 reserve infix `≈`:50 reserve infix `~`:50 reserve infix `≡`:50 reserve infixr `∘`:60 -- input with \comp reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv reserve infixl `⬝`:75 reserve infixr `▸`:75 /- types and type constructors -/ reserve infixr `⊎`:25 reserve infixr `×`:30 /- arithmetic operations -/ reserve infixl `+`:65 reserve infixl `-`:65 reserve infixl `*`:70 reserve infixl `div`:70 reserve infixl `mod`:70 reserve infixl `/`:70 reserve prefix `-`:100 reserve infix `^`:80 reserve infix `<=`:50 reserve infix `≤`:50 reserve infix `<`:50 reserve infix `>=`:50 reserve infix `≥`:50 reserve infix `>`:50 /- boolean operations -/ reserve infixl `&&`:70 reserve infixl `||`:65 /- set operations -/ reserve infix `∈`:50 reserve infix `∉`:50 reserve infixl `∩`:70 reserve infixl `∪`:65 /- other symbols -/ reserve infix `∣`:50 reserve infixl `++`:65 reserve infixr `::`:65
b8778326357f963c89b241885f99bc61ba1d8837
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/field_theory/galois.lean
f1bc6f0db964379fa9be37e1d8461d619a0ad830
[ "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
18,857
lean
/- Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import field_theory.primitive_element import field_theory.fixed import group_theory.group_action.fixing_subgroup /-! # Galois Extensions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define Galois extensions as extensions which are both separable and normal. ## Main definitions - `is_galois F E` where `E` is an extension of `F` - `fixed_field H` where `H : subgroup (E ≃ₐ[F] E)` - `fixing_subgroup K` where `K : intermediate_field F E` - `galois_correspondence` where `E/F` is finite dimensional and Galois ## Main results - `intermediate_field.fixing_subgroup_fixed_field` : If `E/F` is finite dimensional (but not necessarily Galois) then `fixing_subgroup (fixed_field H) = H` - `intermediate_field.fixed_field_fixing_subgroup`: If `E/F` is finite dimensional and Galois then `fixed_field (fixing_subgroup K) = K` Together, these two results prove the Galois correspondence. - `is_galois.tfae` : Equivalent characterizations of a Galois extension of finite degree -/ open_locale polynomial open finite_dimensional alg_equiv section variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] /-- A field extension E/F is galois if it is both separable and normal. Note that in mathlib a separable extension of fields is by definition algebraic. -/ class is_galois : Prop := [to_is_separable : is_separable F E] [to_normal : normal F E] variables {F E} theorem is_galois_iff : is_galois F E ↔ is_separable F E ∧ normal F E := ⟨λ h, ⟨h.1, h.2⟩, λ h, { to_is_separable := h.1, to_normal := h.2 }⟩ attribute [instance, priority 100] -- see Note [lower instance priority] is_galois.to_is_separable is_galois.to_normal variables (F E) namespace is_galois instance self : is_galois F F := ⟨⟩ variables (F) {E} lemma integral [is_galois F E] (x : E) : is_integral F x := to_normal.is_integral x lemma separable [is_galois F E] (x : E) : (minpoly F x).separable := is_separable.separable F x lemma splits [is_galois F E] (x : E) : (minpoly F x).splits (algebra_map F E) := normal.splits' x variables (F E) instance of_fixed_field (G : Type*) [group G] [finite G] [mul_semiring_action G E] : is_galois (fixed_points.subfield G E) E := ⟨⟩ lemma intermediate_field.adjoin_simple.card_aut_eq_finrank [finite_dimensional F E] {α : E} (hα : is_integral F α) (h_sep : (minpoly F α).separable) (h_splits : (minpoly F α).splits (algebra_map F F⟮α⟯)) : fintype.card (F⟮α⟯ ≃ₐ[F] F⟮α⟯) = finrank F F⟮α⟯ := begin letI : fintype (F⟮α⟯ →ₐ[F] F⟮α⟯) := intermediate_field.fintype_of_alg_hom_adjoin_integral F hα, rw intermediate_field.adjoin.finrank hα, rw ← intermediate_field.card_alg_hom_adjoin_integral F hα h_sep h_splits, exact fintype.card_congr (alg_equiv_equiv_alg_hom F F⟮α⟯) end lemma card_aut_eq_finrank [finite_dimensional F E] [is_galois F E] : fintype.card (E ≃ₐ[F] E) = finrank F E := begin cases field.exists_primitive_element F E with α hα, let iso : F⟮α⟯ ≃ₐ[F] E := { to_fun := λ e, e.val, inv_fun := λ e, ⟨e, by { rw hα, exact intermediate_field.mem_top }⟩, left_inv := λ _, by { ext, refl }, right_inv := λ _, rfl, map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, commutes' := λ _, rfl }, have H : is_integral F α := is_galois.integral F α, have h_sep : (minpoly F α).separable := is_galois.separable F α, have h_splits : (minpoly F α).splits (algebra_map F E) := is_galois.splits F α, replace h_splits : polynomial.splits (algebra_map F F⟮α⟯) (minpoly F α), { have p : iso.symm.to_alg_hom.to_ring_hom.comp (algebra_map F E) = (algebra_map F ↥F⟮α⟯), { ext, simp, }, simpa [p] using polynomial.splits_comp_of_splits (algebra_map F E) iso.symm.to_alg_hom.to_ring_hom h_splits, }, rw ← linear_equiv.finrank_eq iso.to_linear_equiv, rw ← intermediate_field.adjoin_simple.card_aut_eq_finrank F E H h_sep h_splits, apply fintype.card_congr, apply equiv.mk (λ ϕ, iso.trans (trans ϕ iso.symm)) (λ ϕ, iso.symm.trans (trans ϕ iso)), { intro ϕ, ext1, simp only [trans_apply, apply_symm_apply] }, { intro ϕ, ext1, simp only [trans_apply, symm_apply_apply] }, end end is_galois end section is_galois_tower variables (F K E : Type*) [field F] [field K] [field E] {E' : Type*} [field E'] [algebra F E'] variables [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] lemma is_galois.tower_top_of_is_galois [is_galois F E] : is_galois K E := { to_is_separable := is_separable_tower_top_of_is_separable F K E, to_normal := normal.tower_top_of_normal F K E } variables {F E} @[priority 100] -- see Note [lower instance priority] instance is_galois.tower_top_intermediate_field (K : intermediate_field F E) [h : is_galois F E] : is_galois K E := is_galois.tower_top_of_is_galois F K E lemma is_galois_iff_is_galois_bot : is_galois (⊥ : intermediate_field F E) E ↔ is_galois F E := begin split, { introI h, exact is_galois.tower_top_of_is_galois (⊥ : intermediate_field F E) F E }, { introI h, apply_instance }, end lemma is_galois.of_alg_equiv [h : is_galois F E] (f : E ≃ₐ[F] E') : is_galois F E' := { to_is_separable := is_separable.of_alg_hom F E f.symm, to_normal := normal.of_alg_equiv f } lemma alg_equiv.transfer_galois (f : E ≃ₐ[F] E') : is_galois F E ↔ is_galois F E' := ⟨λ h, by exactI is_galois.of_alg_equiv f, λ h, by exactI is_galois.of_alg_equiv f.symm⟩ lemma is_galois_iff_is_galois_top : is_galois F (⊤ : intermediate_field F E) ↔ is_galois F E := (intermediate_field.top_equiv : (⊤ : intermediate_field F E) ≃ₐ[F] E).transfer_galois instance is_galois_bot : is_galois F (⊥ : intermediate_field F E) := (intermediate_field.bot_equiv F E).transfer_galois.mpr (is_galois.self F) end is_galois_tower section galois_correspondence variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E] variables (H : subgroup (E ≃ₐ[F] E)) (K : intermediate_field F E) /-- The intermediate field of fixed points fixed by a monoid action that commutes with the `F`-action on `E`. -/ def fixed_points.intermediate_field (M : Type*) [monoid M] [mul_semiring_action M E] [smul_comm_class M F E] : intermediate_field F E := { carrier := mul_action.fixed_points M E, algebra_map_mem' := λ a g, by rw [algebra.algebra_map_eq_smul_one, smul_comm, smul_one], ..fixed_points.subfield M E } namespace intermediate_field /-- The intermediate_field fixed by a subgroup -/ def fixed_field : intermediate_field F E := fixed_points.intermediate_field H lemma finrank_fixed_field_eq_card [finite_dimensional F E] [decidable_pred (∈ H)] : finrank (fixed_field H) E = fintype.card H := fixed_points.finrank_eq_card H E /-- The subgroup fixing an intermediate_field -/ def fixing_subgroup : subgroup (E ≃ₐ[F] E) := fixing_subgroup (E ≃ₐ[F] E) (K : set E) lemma le_iff_le : K ≤ fixed_field H ↔ H ≤ fixing_subgroup K := ⟨λ h g hg x, h (subtype.mem x) ⟨g, hg⟩, λ h x hx g, h (subtype.mem g) ⟨x, hx⟩⟩ /-- The fixing_subgroup of `K : intermediate_field F E` is isomorphic to `E ≃ₐ[K] E` -/ def fixing_subgroup_equiv : fixing_subgroup K ≃* (E ≃ₐ[K] E) := { to_fun := λ ϕ, { commutes' := ϕ.mem, ..alg_equiv.to_ring_equiv ↑ϕ }, inv_fun := λ ϕ, ⟨ϕ.restrict_scalars _, ϕ.commutes⟩, left_inv := λ _, by { ext, refl }, right_inv := λ _, by { ext, refl }, map_mul' := λ _ _, by { ext, refl } } theorem fixing_subgroup_fixed_field [finite_dimensional F E] : fixing_subgroup (fixed_field H) = H := begin have H_le : H ≤ (fixing_subgroup (fixed_field H)) := (le_iff_le _ _).mp le_rfl, classical, suffices : fintype.card H = fintype.card (fixing_subgroup (fixed_field H)), { exact set_like.coe_injective (set.eq_of_inclusion_surjective ((fintype.bijective_iff_injective_and_card (set.inclusion H_le)).mpr ⟨set.inclusion_injective H_le, this⟩).2).symm }, apply fintype.card_congr, refine (fixed_points.to_alg_hom_equiv H E).trans _, refine (alg_equiv_equiv_alg_hom (fixed_field H) E).to_equiv.symm.trans _, exact (fixing_subgroup_equiv (fixed_field H)).to_equiv.symm end instance fixed_field.algebra : algebra K (fixed_field (fixing_subgroup K)) := { smul := λ x y, ⟨x*y, λ ϕ, by rw [smul_mul', (show ϕ • ↑x = ↑x, by exact subtype.mem ϕ x), (show ϕ • ↑y = ↑y, by exact subtype.mem y ϕ)]⟩, to_fun := λ x, ⟨x, λ ϕ, subtype.mem ϕ x⟩, map_zero' := rfl, map_add' := λ _ _, rfl, map_one' := rfl, map_mul' := λ _ _, rfl, commutes' := λ _ _, mul_comm _ _, smul_def' := λ _ _, rfl } instance fixed_field.is_scalar_tower : is_scalar_tower K (fixed_field (fixing_subgroup K)) E := ⟨λ _ _ _, mul_assoc _ _ _⟩ end intermediate_field namespace is_galois theorem fixed_field_fixing_subgroup [finite_dimensional F E] [h : is_galois F E] : intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) = K := begin have K_le : K ≤ intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) := (intermediate_field.le_iff_le _ _).mpr le_rfl, suffices : finrank K E = finrank (intermediate_field.fixed_field (intermediate_field.fixing_subgroup K)) E, { exact (intermediate_field.eq_of_le_of_finrank_eq' K_le this).symm }, classical, rw [intermediate_field.finrank_fixed_field_eq_card, fintype.card_congr (intermediate_field.fixing_subgroup_equiv K).to_equiv], exact (card_aut_eq_finrank K E).symm, end lemma card_fixing_subgroup_eq_finrank [decidable_pred (∈ intermediate_field.fixing_subgroup K)] [finite_dimensional F E] [is_galois F E] : fintype.card (intermediate_field.fixing_subgroup K) = finrank K E := by conv { to_rhs, rw [←fixed_field_fixing_subgroup K, intermediate_field.finrank_fixed_field_eq_card] } /-- The Galois correspondence from intermediate fields to subgroups -/ def intermediate_field_equiv_subgroup [finite_dimensional F E] [is_galois F E] : intermediate_field F E ≃o (subgroup (E ≃ₐ[F] E))ᵒᵈ := { to_fun := intermediate_field.fixing_subgroup, inv_fun := intermediate_field.fixed_field, left_inv := λ K, fixed_field_fixing_subgroup K, right_inv := λ H, intermediate_field.fixing_subgroup_fixed_field H, map_rel_iff' := λ K L, by { rw [←fixed_field_fixing_subgroup L, intermediate_field.le_iff_le, fixed_field_fixing_subgroup L], refl } } /-- The Galois correspondence as a galois_insertion -/ def galois_insertion_intermediate_field_subgroup [finite_dimensional F E] : galois_insertion (order_dual.to_dual ∘ (intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E))) ((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘ order_dual.to_dual) := { choice := λ K _, intermediate_field.fixing_subgroup K, gc := λ K H, (intermediate_field.le_iff_le H K).symm, le_l_u := λ H, le_of_eq (intermediate_field.fixing_subgroup_fixed_field H).symm, choice_eq := λ K _, rfl } /-- The Galois correspondence as a galois_coinsertion -/ def galois_coinsertion_intermediate_field_subgroup [finite_dimensional F E] [is_galois F E] : galois_coinsertion (order_dual.to_dual ∘ (intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E))) ((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘ order_dual.to_dual) := { choice := λ H _, intermediate_field.fixed_field H, gc := λ K H, (intermediate_field.le_iff_le H K).symm, u_l_le := λ K, le_of_eq (fixed_field_fixing_subgroup K), choice_eq := λ H _, rfl } end is_galois end galois_correspondence section galois_equivalent_definitions variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] namespace is_galois lemma is_separable_splitting_field [finite_dimensional F E] [is_galois F E] : ∃ p : F[X], p.separable ∧ p.is_splitting_field F E := begin cases field.exists_primitive_element F E with α h1, use [minpoly F α, separable F α, is_galois.splits F α], rw [eq_top_iff, ←intermediate_field.top_to_subalgebra, ←h1], rw intermediate_field.adjoin_simple_to_subalgebra_of_integral (integral F α), apply algebra.adjoin_mono, rw [set.singleton_subset_iff, polynomial.mem_root_set], exact ⟨minpoly.ne_zero (integral F α), minpoly.aeval _ _⟩ end lemma of_fixed_field_eq_bot [finite_dimensional F E] (h : intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥) : is_galois F E := begin rw [←is_galois_iff_is_galois_bot, ←h], classical, exact is_galois.of_fixed_field E (⊤ : subgroup (E ≃ₐ[F] E)), end lemma of_card_aut_eq_finrank [finite_dimensional F E] (h : fintype.card (E ≃ₐ[F] E) = finrank F E) : is_galois F E := begin apply of_fixed_field_eq_bot, have p : 0 < finrank (intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E))) E := finrank_pos, classical, rw [←intermediate_field.finrank_eq_one_iff, ←mul_left_inj' (ne_of_lt p).symm, finrank_mul_finrank, ←h, one_mul, intermediate_field.finrank_fixed_field_eq_card], apply fintype.card_congr, exact { to_fun := λ g, ⟨g, subgroup.mem_top g⟩, inv_fun := coe, left_inv := λ g, rfl, right_inv := λ _, by { ext, refl } }, end variables {F} {E} {p : F[X]} lemma of_separable_splitting_field_aux [hFE : finite_dimensional F E] [sp : p.is_splitting_field F E] (hp : p.separable) (K : Type*) [field K] [algebra F K] [algebra K E] [is_scalar_tower F K E] {x : E} (hx : x ∈ (p.map (algebra_map F E)).roots) -- these are both implied by `hFE`, but as they carry data this makes the lemma more general [fintype (K →ₐ[F] E)] [fintype (K⟮x⟯.restrict_scalars F →ₐ[F] E)] : fintype.card (K⟮x⟯.restrict_scalars F →ₐ[F] E) = fintype.card (K →ₐ[F] E) * finrank K K⟮x⟯ := begin have h : is_integral K x := is_integral_of_is_scalar_tower (is_integral_of_noetherian (is_noetherian.iff_fg.2 hFE) x), have h1 : p ≠ 0 := λ hp, by rwa [hp, polynomial.map_zero, polynomial.roots_zero] at hx, have h2 : (minpoly K x) ∣ p.map (algebra_map F K), { apply minpoly.dvd, rw [polynomial.aeval_def, polynomial.eval₂_map, ←polynomial.eval_map, ←is_scalar_tower.algebra_map_eq], exact (polynomial.mem_roots (polynomial.map_ne_zero h1)).mp hx }, let key_equiv : (K⟮x⟯.restrict_scalars F →ₐ[F] E) ≃ Σ (f : K →ₐ[F] E), @alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f), { change (K⟮x⟯ →ₐ[F] E) ≃ Σ (f : K →ₐ[F] E), _, exact alg_hom_equiv_sigma }, haveI : Π (f : K →ₐ[F] E), fintype (@alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f)) := λ f, by { apply fintype.of_injective (sigma.mk f) (λ _ _ H, eq_of_heq ((sigma.mk.inj H).2)), exact fintype.of_equiv _ key_equiv }, rw [fintype.card_congr key_equiv, fintype.card_sigma, intermediate_field.adjoin.finrank h], apply finset.sum_const_nat, intros f hf, rw ← @intermediate_field.card_alg_hom_adjoin_integral K _ E _ _ x E _ (ring_hom.to_algebra f) h, { apply fintype.card_congr, refl }, { exact polynomial.separable.of_dvd ((polynomial.separable_map (algebra_map F K)).mpr hp) h2 }, { refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero h1) _ h2, rw [polynomial.splits_map_iff, ←is_scalar_tower.algebra_map_eq], exact sp.splits }, end lemma of_separable_splitting_field [sp : p.is_splitting_field F E] (hp : p.separable) : is_galois F E := begin haveI hFE : finite_dimensional F E := polynomial.is_splitting_field.finite_dimensional E p, letI := classical.dec_eq E, let s := (p.map (algebra_map F E)).roots.to_finset, have adjoin_root : intermediate_field.adjoin F ↑s = ⊤, { apply intermediate_field.to_subalgebra_injective, rw [intermediate_field.top_to_subalgebra, ←top_le_iff, ←sp.adjoin_root_set], apply intermediate_field.algebra_adjoin_le_adjoin, }, let P : intermediate_field F E → Prop := λ K, fintype.card (K →ₐ[F] E) = finrank F K, suffices : P (intermediate_field.adjoin F ↑s), { rw adjoin_root at this, apply of_card_aut_eq_finrank, rw ← eq.trans this (linear_equiv.finrank_eq intermediate_field.top_equiv.to_linear_equiv), exact fintype.card_congr ((alg_equiv_equiv_alg_hom F E).to_equiv.trans (intermediate_field.top_equiv.symm.arrow_congr alg_equiv.refl)) }, apply intermediate_field.induction_on_adjoin_finset s P, { have key := intermediate_field.card_alg_hom_adjoin_integral F (show is_integral F (0 : E), by exact is_integral_zero), rw [minpoly.zero, polynomial.nat_degree_X] at key, specialize key polynomial.separable_X (polynomial.splits_X (algebra_map F E)), rw [←@subalgebra.finrank_bot F E _ _ _, ←intermediate_field.bot_to_subalgebra] at key, refine eq.trans _ key, apply fintype.card_congr, rw intermediate_field.adjoin_zero }, intros K x hx hK, simp only [P] at *, rw [of_separable_splitting_field_aux hp K (multiset.mem_to_finset.mp hx), hK, finrank_mul_finrank], symmetry, refine linear_equiv.finrank_eq _, refl, end /--Equivalent characterizations of a Galois extension of finite degree-/ theorem tfae [finite_dimensional F E] : tfae [is_galois F E, intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥, fintype.card (E ≃ₐ[F] E) = finrank F E, ∃ p : F[X], p.separable ∧ p.is_splitting_field F E] := begin tfae_have : 1 → 2, { exact λ h, order_iso.map_bot (@intermediate_field_equiv_subgroup F _ E _ _ _ h).symm }, tfae_have : 1 → 3, { introI _, exact card_aut_eq_finrank F E }, tfae_have : 1 → 4, { introI _, exact is_separable_splitting_field F E }, tfae_have : 2 → 1, { exact of_fixed_field_eq_bot F E }, tfae_have : 3 → 1, { exact of_card_aut_eq_finrank F E }, tfae_have : 4 → 1, { rintros ⟨h, hp1, _⟩, exactI of_separable_splitting_field hp1 }, tfae_finish, end end is_galois end galois_equivalent_definitions section normal_closure variables (k K F : Type*) [field k] [field K] [field F] [algebra k K] [algebra k F] [algebra K F] [is_scalar_tower k K F] [is_galois k F] instance is_galois.normal_closure : is_galois k (normal_closure k K F) := { to_is_separable := is_separable_tower_bot_of_is_separable k _ F } end normal_closure section is_alg_closure @[priority 100] instance is_alg_closure.is_galois (k K : Type*) [field k] [field K] [algebra k K] [is_alg_closure k K] [char_zero k] : is_galois k K := { } end is_alg_closure
63edaf4516f10647e5b4e44ec4909a8b7225cb2c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/convex/join.lean
1a0ec117a51c703eb0a3d889a04fd4d08eb97f89
[ "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
10,316
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import analysis.convex.combination /-! # Convex join > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the convex join of two sets. The convex join of `s` and `t` is the union of the segments with one end in `s` and the other in `t`. This is notably a useful gadget to deal with convex hulls of finite sets. -/ open set open_locale big_operators variables {ι : Sort*} {𝕜 E : Type*} section ordered_semiring variables (𝕜) [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] {s t s₁ s₂ t₁ t₂ u : set E} {x y : E} /-- The join of two sets is the union of the segments joining them. This can be interpreted as the topological join, but within the original space. -/ def convex_join (s t : set E) : set E := ⋃ (x ∈ s) (y ∈ t), segment 𝕜 x y variables {𝕜} lemma mem_convex_join : x ∈ convex_join 𝕜 s t ↔ ∃ (a ∈ s) (b ∈ t), x ∈ segment 𝕜 a b := by simp [convex_join] lemma convex_join_comm (s t : set E) : convex_join 𝕜 s t = convex_join 𝕜 t s := (Union₂_comm _).trans $ by simp_rw [convex_join, segment_symm] lemma convex_join_mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : convex_join 𝕜 s₁ t₁ ⊆ convex_join 𝕜 s₂ t₂ := bUnion_mono hs $ λ x hx, bUnion_mono ht $ λ y hy, subset.rfl lemma convex_join_mono_left (hs : s₁ ⊆ s₂) : convex_join 𝕜 s₁ t ⊆ convex_join 𝕜 s₂ t := convex_join_mono hs subset.rfl lemma convex_join_mono_right (ht : t₁ ⊆ t₂) : convex_join 𝕜 s t₁ ⊆ convex_join 𝕜 s t₂ := convex_join_mono subset.rfl ht @[simp] lemma convex_join_empty_left (t : set E) : convex_join 𝕜 ∅ t = ∅ := by simp [convex_join] @[simp] lemma convex_join_empty_right (s : set E) : convex_join 𝕜 s ∅ = ∅ := by simp [convex_join] @[simp] lemma convex_join_singleton_left (t : set E) (x : E) : convex_join 𝕜 {x} t = ⋃ (y ∈ t), segment 𝕜 x y := by simp [convex_join] @[simp] lemma convex_join_singleton_right (s : set E) (y : E) : convex_join 𝕜 s {y} = ⋃ (x ∈ s), segment 𝕜 x y := by simp [convex_join] @[simp] lemma convex_join_singletons (x : E) : convex_join 𝕜 {x} {y} = segment 𝕜 x y := by simp [convex_join] @[simp] lemma convex_join_union_left (s₁ s₂ t : set E) : convex_join 𝕜 (s₁ ∪ s₂) t = convex_join 𝕜 s₁ t ∪ convex_join 𝕜 s₂ t := by simp_rw [convex_join, mem_union, Union_or, Union_union_distrib] @[simp] lemma convex_join_union_right (s t₁ t₂ : set E) : convex_join 𝕜 s (t₁ ∪ t₂) = convex_join 𝕜 s t₁ ∪ convex_join 𝕜 s t₂ := by simp_rw [convex_join, mem_union, Union_or, Union_union_distrib] @[simp] lemma convex_join_Union_left (s : ι → set E) (t : set E) : convex_join 𝕜 (⋃ i, s i) t = ⋃ i, convex_join 𝕜 (s i) t := by { simp_rw [convex_join, mem_Union, Union_exists], exact Union_comm _ } @[simp] lemma convex_join_Union_right (s : set E) (t : ι → set E) : convex_join 𝕜 s (⋃ i, t i) = ⋃ i, convex_join 𝕜 s (t i) := by simp_rw [convex_join_comm s, convex_join_Union_left] lemma segment_subset_convex_join (hx : x ∈ s) (hy : y ∈ t) : segment 𝕜 x y ⊆ convex_join 𝕜 s t := (subset_Union₂ y hy).trans (subset_Union₂ x hx) lemma subset_convex_join_left (h : t.nonempty) : s ⊆ convex_join 𝕜 s t := λ x hx, let ⟨y, hy⟩ := h in segment_subset_convex_join hx hy $ left_mem_segment _ _ _ lemma subset_convex_join_right (h : s.nonempty) : t ⊆ convex_join 𝕜 s t := λ y hy, let ⟨x, hx⟩ := h in segment_subset_convex_join hx hy $ right_mem_segment _ _ _ lemma convex_join_subset (hs : s ⊆ u) (ht : t ⊆ u) (hu : convex 𝕜 u) : convex_join 𝕜 s t ⊆ u := Union₂_subset $ λ x hx, Union₂_subset $ λ y hy, hu.segment_subset (hs hx) (ht hy) lemma convex_join_subset_convex_hull (s t : set E) : convex_join 𝕜 s t ⊆ convex_hull 𝕜 (s ∪ t) := convex_join_subset ((subset_union_left _ _).trans $ subset_convex_hull _ _) ((subset_union_right _ _).trans $ subset_convex_hull _ _) $ convex_convex_hull _ _ end ordered_semiring section linear_ordered_field variables [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] {s t u : set E} {x y : E} lemma convex_join_assoc_aux (s t u : set E) : convex_join 𝕜 (convex_join 𝕜 s t) u ⊆ convex_join 𝕜 s (convex_join 𝕜 t u) := begin simp_rw [subset_def, mem_convex_join], rintro _ ⟨z, ⟨x, hx, y, hy, a₁, b₁, ha₁, hb₁, hab₁, rfl⟩, z, hz, a₂, b₂, ha₂, hb₂, hab₂, rfl⟩, obtain rfl | hb₂ := hb₂.eq_or_lt, { refine ⟨x, hx, y, ⟨y, hy, z, hz, left_mem_segment _ _ _⟩, a₁, b₁, ha₁, hb₁, hab₁, _⟩, rw add_zero at hab₂, rw [hab₂, one_smul, zero_smul, add_zero] }, have ha₂b₁ : 0 ≤ a₂ * b₁ := mul_nonneg ha₂ hb₁, have hab : 0 < a₂ * b₁ + b₂ := add_pos_of_nonneg_of_pos ha₂b₁ hb₂, refine ⟨x, hx, ((a₂ * b₁) / (a₂ * b₁ + b₂)) • y + (b₂ / (a₂ * b₁ + b₂)) • z, ⟨y, hy, z, hz, _, _, _, _, _, rfl⟩, a₂ * a₁, a₂ * b₁ + b₂, mul_nonneg ha₂ ha₁, hab.le, _, _⟩, { exact div_nonneg ha₂b₁ hab.le }, { exact div_nonneg hb₂.le hab.le }, { rw [←add_div, div_self hab.ne'] }, { rw [←add_assoc, ←mul_add, hab₁, mul_one, hab₂] }, { simp_rw [smul_add, ←mul_smul, mul_div_cancel' _ hab.ne', add_assoc] } end lemma convex_join_assoc (s t u : set E) : convex_join 𝕜 (convex_join 𝕜 s t) u = convex_join 𝕜 s (convex_join 𝕜 t u) := begin refine (convex_join_assoc_aux _ _ _).antisymm _, simp_rw [convex_join_comm s, convex_join_comm _ u], exact convex_join_assoc_aux _ _ _, end lemma convex_join_left_comm (s t u : set E) : convex_join 𝕜 s (convex_join 𝕜 t u) = convex_join 𝕜 t (convex_join 𝕜 s u) := by simp_rw [←convex_join_assoc, convex_join_comm] lemma convex_join_right_comm (s t u : set E) : convex_join 𝕜 (convex_join 𝕜 s t) u = convex_join 𝕜 (convex_join 𝕜 s u) t := by simp_rw [convex_join_assoc, convex_join_comm] lemma convex_join_convex_join_convex_join_comm (s t u v : set E) : convex_join 𝕜 (convex_join 𝕜 s t) (convex_join 𝕜 u v) = convex_join 𝕜 (convex_join 𝕜 s u) (convex_join 𝕜 t v) := by simp_rw [←convex_join_assoc, convex_join_right_comm] lemma convex_hull_insert (hs : s.nonempty) : convex_hull 𝕜 (insert x s) = convex_join 𝕜 {x} (convex_hull 𝕜 s) := begin classical, refine (convex_join_subset ((singleton_subset_iff.2 $ mem_insert _ _).trans $ subset_convex_hull _ _) (convex_hull_mono $ subset_insert _ _) $ convex_convex_hull _ _).antisymm' (λ x hx, _), rw convex_hull_eq at hx, obtain ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩ := hx, have : (∑ i in t.filter (λ i, z i = x), w i) • x + ∑ i in t.filter (λ i, z i ≠ x), w i • z i = t.center_mass w z, { rw [finset.center_mass_eq_of_sum_1 _ _ hw₁, finset.sum_smul], convert finset.sum_filter_add_sum_filter_not _ _ (w • z) using 2, refine finset.sum_congr rfl (λ i hi, _), rw [pi.smul_apply', (finset.mem_filter.1 hi).2] }, rw ←this, have hw₀' : ∀ i ∈ t.filter (λ i, z i ≠ x), 0 ≤ w i := λ i hi, hw₀ _ $ finset.filter_subset _ _ hi, obtain hw | hw := (finset.sum_nonneg hw₀').eq_or_gt, { rw [←finset.sum_filter_add_sum_filter_not _ (λ i, z i = x), hw, add_zero] at hw₁, rw [hw₁, one_smul, finset.sum_eq_zero, add_zero], { exact subset_convex_join_left hs.convex_hull (mem_singleton _) }, simp_rw finset.sum_eq_zero_iff_of_nonneg hw₀' at hw, rintro i hi, rw [hw _ hi, zero_smul] }, refine mem_convex_join.2 ⟨x, mem_singleton _, (t.filter $ λ i, z i ≠ x).center_mass w z, finset.center_mass_mem_convex_hull _ hw₀' hw (λ i hi, _), ∑ i in t.filter (λ i, z i = x), w i, ∑ i in t.filter (λ i, z i ≠ x), w i, finset.sum_nonneg (λ i hi, hw₀ _ $ finset.filter_subset _ _ hi), finset.sum_nonneg hw₀', _, _⟩, { rw finset.mem_filter at hi, exact mem_of_mem_insert_of_ne (hz _ hi.1) hi.2 }, { rw [finset.sum_filter_add_sum_filter_not, hw₁] }, { rw [finset.center_mass, smul_inv_smul₀ hw.ne', finset.sum_smul] } end lemma convex_join_segments (a b c d : E) : convex_join 𝕜 (segment 𝕜 a b) (segment 𝕜 c d) = convex_hull 𝕜 {a, b, c, d} := by simp only [convex_hull_insert, insert_nonempty, singleton_nonempty, convex_hull_pair, ←convex_join_assoc, convex_join_singletons] lemma convex_join_segment_singleton (a b c : E) : convex_join 𝕜 (segment 𝕜 a b) {c} = convex_hull 𝕜 {a, b, c} := by rw [←pair_eq_singleton, ←convex_join_segments, segment_same, pair_eq_singleton] lemma convex_join_singleton_segment (a b c : E) : convex_join 𝕜 {a} (segment 𝕜 b c) = convex_hull 𝕜 {a, b, c} := by rw [←segment_same 𝕜, convex_join_segments, insert_idem] protected lemma convex.convex_join (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (convex_join 𝕜 s t) := begin rw convex_iff_segment_subset at ⊢ ht hs, simp_rw mem_convex_join, rintro x ⟨xa, hxa, xb, hxb, hx⟩ y ⟨ya, hya, yb, hyb, hy⟩, refine (segment_subset_convex_join hx hy).trans _, have triv : ({xa, xb, ya, yb} : set E) = {xa, ya, xb, yb} := by simp only [set.insert_comm], rw [convex_join_segments, triv, ←convex_join_segments], exact convex_join_mono (hs hxa hya) (ht hxb hyb), end protected lemma convex.convex_hull_union (hs : convex 𝕜 s) (ht : convex 𝕜 t) (hs₀ : s.nonempty) (ht₀ : t.nonempty) : convex_hull 𝕜 (s ∪ t) = convex_join 𝕜 s t := (convex_hull_min (union_subset (subset_convex_join_left ht₀) $ subset_convex_join_right hs₀) $ hs.convex_join ht).antisymm $ convex_join_subset_convex_hull _ _ lemma convex_hull_union (hs : s.nonempty) (ht : t.nonempty) : convex_hull 𝕜 (s ∪ t) = convex_join 𝕜 (convex_hull 𝕜 s) (convex_hull 𝕜 t) := begin rw [←convex_hull_convex_hull_union_left, ←convex_hull_convex_hull_union_right], exact (convex_convex_hull 𝕜 s).convex_hull_union (convex_convex_hull 𝕜 t) hs.convex_hull ht.convex_hull, end end linear_ordered_field
f086d0d4adf8f01e5d860cca3ae63cbcbdb52df4
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/adjunction/basic.lean
5bdfc3a91b088ad32d4365fbed9e669abe012f8b
[ "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
19,646
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin, Bhavik Mehta -/ import category_theory.equivalence /-! # Adjunctions between functors `F ⊣ G` represents the data of an adjunction between two functors `F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint. We provide various useful constructors: * `mk_of_hom_equiv` * `mk_of_unit_counit` * `left_adjoint_of_equiv` / `right_adjoint_of equiv` construct a left/right adjoint of a given functor given the action on objects and the relevant equivalence of morphism spaces. * `adjunction_of_equiv_left` / `adjunction_of_equiv_right` witness that these constructions give adjunctions. There are also typeclasses `is_left_adjoint` / `is_right_adjoint`, carrying data witnessing that a given functor is a left or right adjoint. Given `[is_left_adjoint F]`, a right adjoint of `F` can be constructed as `right_adjoint F`. `adjunction.comp` composes adjunctions. `to_equivalence` upgrades an adjunction to an equivalence, given witnesses that the unit and counit are pointwise isomorphisms. Conversely `equivalence.to_adjunction` recovers the underlying adjunction from an equivalence. -/ namespace category_theory open category -- declare the `v`'s first; see `category_theory.category` for an explanation universes v₁ v₂ v₃ u₁ u₂ u₃ local attribute [elab_simple] whisker_left whisker_right variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- `F ⊣ G` represents the data of an adjunction between two functors `F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint. To construct an `adjunction` between two functors, it's often easier to instead use the constructors `mk_of_hom_equiv` or `mk_of_unit_counit`. To construct a left adjoint, there are also constructors `left_adjoint_of_equiv` and `adjunction_of_equiv_left` (as well as their duals) which can be simpler in practice. Uniqueness of adjoints is shown in `category_theory.adjunction.opposites`. See <https://stacks.math.columbia.edu/tag/0037>. -/ structure adjunction (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (hom_equiv_unit' : Π {X Y f}, (hom_equiv X Y) f = (unit : _ ⟶ _).app X ≫ G.map f . obviously) (hom_equiv_counit' : Π {X Y g}, (hom_equiv X Y).symm g = F.map g ≫ counit.app Y . obviously) infix ` ⊣ `:15 := adjunction /-- A class giving a chosen right adjoint to the functor `left`. -/ class is_left_adjoint (left : C ⥤ D) := (right : D ⥤ C) (adj : left ⊣ right) /-- A class giving a chosen left adjoint to the functor `right`. -/ class is_right_adjoint (right : D ⥤ C) := (left : C ⥤ D) (adj : left ⊣ right) /-- Extract the left adjoint from the instance giving the chosen adjoint. -/ def left_adjoint (R : D ⥤ C) [is_right_adjoint R] : C ⥤ D := is_right_adjoint.left R /-- Extract the right adjoint from the instance giving the chosen adjoint. -/ def right_adjoint (L : C ⥤ D) [is_left_adjoint L] : D ⥤ C := is_left_adjoint.right L /-- The adjunction associated to a functor known to be a left adjoint. -/ def adjunction.of_left_adjoint (left : C ⥤ D) [is_left_adjoint left] : adjunction left (right_adjoint left) := is_left_adjoint.adj /-- The adjunction associated to a functor known to be a right adjoint. -/ def adjunction.of_right_adjoint (right : C ⥤ D) [is_right_adjoint right] : adjunction (left_adjoint right) right := is_right_adjoint.adj namespace adjunction restate_axiom hom_equiv_unit' restate_axiom hom_equiv_counit' attribute [simp, priority 10] hom_equiv_unit hom_equiv_counit section variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X' X : C} {Y Y' : D} lemma hom_equiv_id (X : C) : adj.hom_equiv X _ (𝟙 _) = adj.unit.app X := by simp lemma hom_equiv_symm_id (X : D) : (adj.hom_equiv _ X).symm (𝟙 _) = adj.counit.app X := by simp @[simp, priority 10] lemma hom_equiv_naturality_left_symm (f : X' ⟶ X) (g : X ⟶ G.obj Y) : (adj.hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (adj.hom_equiv X Y).symm g := by rw [hom_equiv_counit, F.map_comp, assoc, adj.hom_equiv_counit.symm] @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp [-hom_equiv_unit] @[simp, priority 10] lemma hom_equiv_naturality_right (f : F.obj X ⟶ Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y') (f ≫ g) = (adj.hom_equiv X Y) f ≫ G.map g := by rw [hom_equiv_unit, G.map_comp, ← assoc, ←hom_equiv_unit] @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp [-hom_equiv_counit] @[simp] lemma left_triangle : (whisker_right adj.unit F) ≫ (whisker_left F adj.counit) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_counit, equiv.symm_apply_eq, adj.hom_equiv_unit], simp end @[simp] lemma right_triangle : (whisker_left G adj.unit) ≫ (whisker_right adj.counit G) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_unit, ← equiv.eq_symm_apply, adj.hom_equiv_counit], simp end @[simp, reassoc] lemma left_triangle_components : F.map (adj.unit.app X) ≫ adj.counit.app (F.obj X) = 𝟙 (F.obj X) := congr_arg (λ (t : nat_trans _ (𝟭 C ⋙ F)), t.app X) adj.left_triangle @[simp, reassoc] lemma right_triangle_components {Y : D} : adj.unit.app (G.obj Y) ≫ G.map (adj.counit.app Y) = 𝟙 (G.obj Y) := congr_arg (λ (t : nat_trans _ (G ⋙ 𝟭 C)), t.app Y) adj.right_triangle @[simp, reassoc] lemma counit_naturality {X Y : D} (f : X ⟶ Y) : F.map (G.map f) ≫ (adj.counit).app Y = (adj.counit).app X ≫ f := adj.counit.naturality f @[simp, reassoc] lemma unit_naturality {X Y : C} (f : X ⟶ Y) : (adj.unit).app X ≫ G.map (F.map f) = f ≫ (adj.unit).app Y := (adj.unit.naturality f).symm lemma hom_equiv_apply_eq {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : adj.hom_equiv A B f = g ↔ f = (adj.hom_equiv A B).symm g := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ lemma eq_hom_equiv_apply {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : g = adj.hom_equiv A B f ↔ (adj.hom_equiv A B).symm g = f := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ end end adjunction namespace adjunction /-- This is an auxiliary data structure useful for constructing adjunctions. See `adjunction.mk_of_hom_equiv`. This structure won't typically be used anywhere else. -/ @[nolint has_inhabited_instance] structure core_hom_equiv (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (hom_equiv_naturality_left_symm' : Π {X' X Y} (f : X' ⟶ X) (g : X ⟶ G.obj Y), (hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (hom_equiv X Y).symm g . obviously) (hom_equiv_naturality_right' : Π {X Y Y'} (f : F.obj X ⟶ Y) (g : Y ⟶ Y'), (hom_equiv X Y') (f ≫ g) = (hom_equiv X Y) f ≫ G.map g . obviously) namespace core_hom_equiv restate_axiom hom_equiv_naturality_left_symm' restate_axiom hom_equiv_naturality_right' attribute [simp, priority 10] hom_equiv_naturality_left_symm hom_equiv_naturality_right variables {F : C ⥤ D} {G : D ⥤ C} (adj : core_hom_equiv F G) {X' X : C} {Y Y' : D} @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp end core_hom_equiv /-- This is an auxiliary data structure useful for constructing adjunctions. See `adjunction.mk_of_unit_counit`. This structure won't typically be used anywhere else. -/ @[nolint has_inhabited_instance] structure core_unit_counit (F : C ⥤ D) (G : D ⥤ C) := (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (left_triangle' : whisker_right unit F ≫ (functor.associator F G F).hom ≫ whisker_left F counit = nat_trans.id (𝟭 C ⋙ F) . obviously) (right_triangle' : whisker_left G unit ≫ (functor.associator G F G).inv ≫ whisker_right counit G = nat_trans.id (G ⋙ 𝟭 C) . obviously) namespace core_unit_counit restate_axiom left_triangle' restate_axiom right_triangle' attribute [simp] left_triangle right_triangle end core_unit_counit variables {F : C ⥤ D} {G : D ⥤ C} /-- Construct an adjunction between `F` and `G` out of a natural bijection between each `F.obj X ⟶ Y` and `X ⟶ G.obj Y`. -/ @[simps] def mk_of_hom_equiv (adj : core_hom_equiv F G) : F ⊣ G := { unit := { app := λ X, (adj.hom_equiv X (F.obj X)) (𝟙 (F.obj X)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left, ← adj.hom_equiv_naturality_right], dsimp, simp -- See note [dsimp, simp]. end }, counit := { app := λ Y, (adj.hom_equiv _ _).inv_fun (𝟙 (G.obj Y)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm], dsimp, simp end }, hom_equiv_unit' := λ X Y f, by erw [← adj.hom_equiv_naturality_right]; simp, hom_equiv_counit' := λ X Y f, by erw [← adj.hom_equiv_naturality_left_symm]; simp, .. adj } /-- Construct an adjunction between functors `F` and `G` given a unit and counit for the adjunction satisfying the triangle identities. -/ @[simps] def mk_of_unit_counit (adj : core_unit_counit F G) : F ⊣ G := { hom_equiv := λ X Y, { to_fun := λ f, adj.unit.app X ≫ G.map f, inv_fun := λ g, F.map g ≫ adj.counit.app Y, left_inv := λ f, begin change F.map (_ ≫ _) ≫ _ = _, rw [F.map_comp, assoc, ←functor.comp_map, adj.counit.naturality, ←assoc], convert id_comp f, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.left_triangle, dsimp at t, simp only [id_comp] at t, exact t, end, right_inv := λ g, begin change _ ≫ G.map (_ ≫ _) = _, rw [G.map_comp, ←assoc, ←functor.comp_map, ←adj.unit.naturality, assoc], convert comp_id g, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.right_triangle, dsimp at t, simp only [id_comp] at t, exact t, end }, .. adj } /-- The adjunction between the identity functor on a category and itself. -/ def id : 𝟭 C ⊣ 𝟭 C := { hom_equiv := λ X Y, equiv.refl _, unit := 𝟙 _, counit := 𝟙 _ } -- Satisfy the inhabited linter. instance : inhabited (adjunction (𝟭 C) (𝟭 C)) := ⟨id⟩ /-- If F and G are naturally isomorphic functors, establish an equivalence of hom-sets. -/ @[simps] def equiv_homset_left_of_nat_iso {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} : (F.obj X ⟶ Y) ≃ (F'.obj X ⟶ Y) := { to_fun := λ f, iso.inv.app _ ≫ f, inv_fun := λ g, iso.hom.app _ ≫ g, left_inv := λ f, by simp, right_inv := λ g, by simp } /-- If G and H are naturally isomorphic functors, establish an equivalence of hom-sets. -/ @[simps] def equiv_homset_right_of_nat_iso {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} : (X ⟶ G.obj Y) ≃ (X ⟶ G'.obj Y) := { to_fun := λ f, f ≫ iso.hom.app _, inv_fun := λ g, g ≫ iso.inv.app _, left_inv := λ f, by simp, right_inv := λ g, by simp } /-- Transport an adjunction along an natural isomorphism on the left. -/ def of_nat_iso_left {F G : C ⥤ D} {H : D ⥤ C} (adj : F ⊣ H) (iso : F ≅ G) : G ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (equiv_homset_left_of_nat_iso iso.symm).trans (adj.hom_equiv X Y) } /-- Transport an adjunction along an natural isomorphism on the right. -/ def of_nat_iso_right {F : C ⥤ D} {G H : D ⥤ C} (adj : F ⊣ G) (iso : G ≅ H) : F ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (adj.hom_equiv X Y).trans (equiv_homset_right_of_nat_iso iso) } /-- Transport being a right adjoint along a natural isomorphism. -/ def right_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_right_adjoint F] : is_right_adjoint G := { left := r.left, adj := of_nat_iso_right r.adj h } /-- Transport being a left adjoint along a natural isomorphism. -/ def left_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_left_adjoint F] : is_left_adjoint G := { right := r.right, adj := of_nat_iso_left r.adj h } section variables {E : Type u₃} [ℰ : category.{v₃} E] {H : D ⥤ E} {I : E ⥤ D} /-- Composition of adjunctions. See <https://stacks.math.columbia.edu/tag/0DV0>. -/ def comp (adj₁ : F ⊣ G) (adj₂ : H ⊣ I) : F ⋙ H ⊣ I ⋙ G := { hom_equiv := λ X Z, equiv.trans (adj₂.hom_equiv _ _) (adj₁.hom_equiv _ _), unit := adj₁.unit ≫ (whisker_left F $ whisker_right adj₂.unit G) ≫ (functor.associator _ _ _).inv, counit := (functor.associator _ _ _).hom ≫ (whisker_left I $ whisker_right adj₁.counit H) ≫ adj₂.counit } /-- If `F` and `G` are left adjoints then `F ⋙ G` is a left adjoint too. -/ instance left_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] (F : C ⥤ D) (G : D ⥤ E) [Fl : is_left_adjoint F] [Gl : is_left_adjoint G] : is_left_adjoint (F ⋙ G) := { right := Gl.right ⋙ Fl.right, adj := Fl.adj.comp Gl.adj } /-- If `F` and `G` are right adjoints then `F ⋙ G` is a right adjoint too. -/ instance right_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] {F : C ⥤ D} {G : D ⥤ E} [Fr : is_right_adjoint F] [Gr : is_right_adjoint G] : is_right_adjoint (F ⋙ G) := { left := Gr.left ⋙ Fr.left, adj := Gr.adj.comp Fr.adj } end section construct_left -- Construction of a left adjoint. In order to construct a left -- adjoint to a functor G : D → C, it suffices to give the object part -- of a functor F : C → D together with isomorphisms Hom(FX, Y) ≃ -- Hom(X, GY) natural in Y. The action of F on morphisms can be -- constructed from this data. variables {F_obj : C → D} {G} variables (e : Π X Y, (F_obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) variables (he : ∀ X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g) include he private lemma he' {X Y Y'} (f g) : (e X Y').symm (f ≫ G.map g) = (e X Y).symm f ≫ g := by intros; rw [equiv.symm_apply_eq, he]; simp /-- Construct a left adjoint functor to `G`, given the functor's value on objects `F_obj` and a bijection `e` between `F_obj X ⟶ Y` and `X ⟶ G.obj Y` satisfying a naturality law `he : ∀ X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g`. Dual to `right_adjoint_of_equiv`. -/ @[simps] def left_adjoint_of_equiv : C ⥤ D := { obj := F_obj, map := λ X X' f, (e X (F_obj X')).symm (f ≫ e X' (F_obj X') (𝟙 _)), map_comp' := λ X X' X'' f f', begin rw [equiv.symm_apply_eq, he, equiv.apply_symm_apply], conv { to_rhs, rw [assoc, ←he, id_comp, equiv.apply_symm_apply] }, simp end } /-- Show that the functor given by `left_adjoint_of_equiv` is indeed left adjoint to `G`. Dual to `adjunction_of_equiv_right`. -/ @[simps] def adjunction_of_equiv_left : left_adjoint_of_equiv e he ⊣ G := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := begin intros, erw [← he' e he, ← equiv.apply_eq_iff_eq], simp [(he _ _ _ _ _).symm] end } end construct_left section construct_right -- Construction of a right adjoint, analogous to the above. variables {F} {G_obj : D → C} variables (e : Π X Y, (F.obj X ⟶ Y) ≃ (X ⟶ G_obj Y)) variables (he : ∀ X' X Y f g, e X' Y (F.map f ≫ g) = f ≫ e X Y g) include he private lemma he' {X' X Y} (f g) : F.map f ≫ (e X Y).symm g = (e X' Y).symm (f ≫ g) := by intros; rw [equiv.eq_symm_apply, he]; simp /-- Construct a right adjoint functor to `F`, given the functor's value on objects `G_obj` and a bijection `e` between `F.obj X ⟶ Y` and `X ⟶ G_obj Y` satisfying a naturality law `he : ∀ X Y Y' g h, e X' Y (F.map f ≫ g) = f ≫ e X Y g`. Dual to `left_adjoint_of_equiv`. -/ @[simps] def right_adjoint_of_equiv : D ⥤ C := { obj := G_obj, map := λ Y Y' g, (e (G_obj Y) Y') ((e (G_obj Y) Y).symm (𝟙 _) ≫ g), map_comp' := λ Y Y' Y'' g g', begin rw [← equiv.eq_symm_apply, ← he' e he, equiv.symm_apply_apply], conv { to_rhs, rw [← assoc, he' e he, comp_id, equiv.symm_apply_apply] }, simp end } /-- Show that the functor given by `right_adjoint_of_equiv` is indeed right adjoint to `F`. Dual to `adjunction_of_equiv_left`. -/ @[simps] def adjunction_of_equiv_right : F ⊣ right_adjoint_of_equiv e he := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := by intros; rw [equiv.symm_apply_eq, he]; simp, hom_equiv_naturality_right' := begin intros X Y Y' g h, erw [←he, equiv.apply_eq_iff_eq, ←assoc, he' e he, comp_id, equiv.symm_apply_apply] end } end construct_right /-- If the unit and counit of a given adjunction are (pointwise) isomorphisms, then we can upgrade the adjunction to an equivalence. -/ @[simps] noncomputable def to_equivalence (adj : F ⊣ G) [∀ X, is_iso (adj.unit.app X)] [∀ Y, is_iso (adj.counit.app Y)] : C ≌ D := { functor := F, inverse := G, unit_iso := nat_iso.of_components (λ X, as_iso (adj.unit.app X)) (by simp), counit_iso := nat_iso.of_components (λ Y, as_iso (adj.counit.app Y)) (by simp) } /-- If the unit and counit for the adjunction corresponding to a right adjoint functor are (pointwise) isomorphisms, then the functor is an equivalence of categories. -/ @[simps] noncomputable def is_right_adjoint_to_is_equivalence [is_right_adjoint G] [∀ X, is_iso ((adjunction.of_right_adjoint G).unit.app X)] [∀ Y, is_iso ((adjunction.of_right_adjoint G).counit.app Y)] : is_equivalence G := is_equivalence.of_equivalence_inverse (adjunction.of_right_adjoint G).to_equivalence end adjunction open adjunction namespace equivalence /-- The adjunction given by an equivalence of categories. (To obtain the opposite adjunction, simply use `e.symm.to_adjunction`. -/ def to_adjunction (e : C ≌ D) : e.functor ⊣ e.inverse := mk_of_unit_counit ⟨e.unit, e.counit, by { ext, dsimp, simp only [id_comp], exact e.functor_unit_comp _, }, by { ext, dsimp, simp only [id_comp], exact e.unit_inverse_comp _, }⟩ @[simp] lemma as_equivalence_to_adjunction_unit {e : C ≌ D} : e.functor.as_equivalence.to_adjunction.unit = e.unit := rfl @[simp] lemma as_equivalence_to_adjunction_counit {e : C ≌ D} : e.functor.as_equivalence.to_adjunction.counit = e.counit := rfl end equivalence namespace functor /-- An equivalence `E` is left adjoint to its inverse. -/ def adjunction (E : C ⥤ D) [is_equivalence E] : E ⊣ E.inv := (E.as_equivalence).to_adjunction /-- If `F` is an equivalence, it's a left adjoint. -/ @[priority 10] instance left_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_left_adjoint F := { right := _, adj := functor.adjunction F } @[simp] lemma right_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : right_adjoint F = inv F := rfl /-- If `F` is an equivalence, it's a right adjoint. -/ @[priority 10] instance right_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_right_adjoint F := { left := _, adj := functor.adjunction F.inv } @[simp] lemma left_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : left_adjoint F = inv F := rfl end functor end category_theory
37c4bb18652c493578bcb0ca2d26489eea1e06a6
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.37.lean
3045eb2f68cb2f5f41c82459e4ced7c6920f4989
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
5,709
lean
/- page 44 -/ import standard open classical variables p q r s : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := let swap := λ (p q : Prop), and.rec (λ (Hp : p) (Hq : q), and.intro Hq Hp) in iff.intro (swap p q) (swap q p) example : p ∨ q ↔ q ∨ p := let swap := λ (p q : Prop), or.rec (λ Hp : p, or.inr Hp) (λ Hq : q, or.inl Hq) in iff.intro (swap p q) (swap q p) -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := iff.intro (assume Hpqr, have Hpq : p ∧ q, from and.left Hpqr, have Hp : p, from and.left Hpq, have Hq : q, from and.right Hpq, have Hr : r, from and.right Hpqr, and.intro Hp (and.intro Hq Hr)) (assume Hpqr, have Hqr : q ∧ r, from and.right Hpqr, have Hp : p, from and.left Hpqr, have Hq : q, from and.left Hqr, have Hr : r, from and.right Hqr, and.intro (and.intro Hp Hq) Hr) example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := iff.intro (assume Hpqr, or.elim Hpqr (assume Hpq, or.elim Hpq (assume Hp, or.inl Hp) (assume Hq, or.inr (or.inl Hq))) (assume Hr, or.inr (or.inr Hr))) (assume Hpqr, or.elim Hpqr (assume Hp, or.inl (or.inl Hp)) (assume Hqr, or.elim Hqr (assume Hq, or.inl (or.inr Hq)) (assume Hr, or.inr Hr))) -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := iff.intro (assume H, have Hp : p, from and.left H, or.elim (and.right H) (assume Hq, or.inl (and.intro Hp Hq)) (assume Hr, or.inr (and.intro Hp Hr))) (assume H, or.elim H (assume Hpq, and.intro (and.left Hpq) (or.inl (and.right Hpq))) (assume Hpr, and.intro (and.left Hpr) (or.inr (and.right Hpr)))) example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := iff.intro (or.rec (λ Hp, and.intro (or.inl Hp) (or.inl Hp)) (λ Hqr, and.intro (or.inr (and.left Hqr)) (or.inr (and.right Hqr)))) (and.rec (λ Hpq Hpr, or.elim Hpq (λ Hp, or.inl Hp) (λ Hq, or.elim Hpr (λ Hp, or.inl Hp) (λ Hr, or.inr (and.intro Hq Hr))))) -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := iff.intro (assume H, assume Hpq, H (and.left Hpq) (and.right Hpq)) (assume H, assume Hp, assume Hq, H (and.intro Hp Hq)) example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := iff.intro (λ H, and.intro (λ Hp, H (or.inl Hp)) (λ Hq, H (or.inr Hq))) (λ H, or.rec (and.left H) (and.right H)) example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := assume H, or.elim (em p) (λ Hp, or.elim (H Hp) (λ Hr, or.inl (λ Hp, Hr)) (λ Hs, or.inr (λ Hp, Hs))) (λ Hnp, or.inl (λ Hp, absurd Hp Hnp)) example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := iff.intro (λ H, and.intro (λ Hp, H (or.inl Hp)) (λ Hq, H (or.inr Hq))) (λ H, λ H', or.elim H' (λ Hp, (and.left H) Hp) (λ Hq, (and.right H) Hq)) example : ¬p ∨ ¬q → ¬(p ∧ q) := λ H Hpq, or.elim H (λ Hnp, Hnp (and.left Hpq)) (λ Hnq, Hnq (and.right Hpq)) example : ¬(p ∧ ¬ p) := λ H, (and.right H) (and.left H) example : p ∧ ¬q → ¬(p → q) := λ H H', (and.right H) (H' (and.left H)) example : ¬p → (p → q) := λ Hnp Hp, absurd Hp Hnp example : (¬p ∨ q) → (p → q) := λ H Hp, or.elim H (λ Hnp, absurd Hp Hnp) (λ Hq, Hq) example : p ∨ false ↔ p := iff.intro (λ H, or.elim H (λ Hp, Hp) false.elim) (λ Hp, or.inl Hp) example : p ∧ false ↔ false := iff.intro (λ H, and.right H) false.elim example : ¬(p ↔ ¬p) := λ H, let Hnp := λ Hp, iff.mp H Hp Hp in let Hp := iff.mpr H Hnp in Hnp Hp example : (p → q) → (¬q → ¬p) := λ H Hnq Hp, Hnq (H Hp) -- these require classical reasoning -- example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := sorry *same as above* example : ¬(p ∧ q) → ¬p ∨ ¬q := λ H, or.elim (em p) (λ Hp, or.inr (λ Hq, H (and.intro Hp Hq))) (λ Hnp, or.inl Hnp) example : ¬(p → q) → p ∧ ¬q := λ H, or.elim (em p) (λ Hp, and.intro Hp (λ Hq, H (λ Hp, Hq))) (λ Hnp, false.elim (H (λ Hp, absurd Hp Hnp))) example : (p → q) → (¬p ∨ q) := λ H, or.elim (em p) (λ Hp, or.inr (H Hp)) (λ Hnp, or.inl Hnp) example : (¬q → ¬p) → (p → q) := λ H Hp, or.elim (em q) (λ Hq, Hq) (λ Hnq, absurd Hp (H Hnq)) example : p ∨ ¬p := em p example : (((p → q) → p) → p) := λ H, or.elim (em q) (λ Hq, H (λ Hp, Hq)) (λ Hnq, or.elim (em p) (λ Hp, Hp) (λ Hnp, H (λ Hp, absurd Hp Hnp)))
cfe4af02112bf59b620e00a4af53a7425f25944f
ad3e8f15221a986da27c99f371922c0b3f5792b6
/src/week-02/solutions/e04-trees.lean
491bbea8093c31328a055329b489217027807447
[]
no_license
VArtem/lean-itmo
a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1
dc44cd06f9f5b984d051831b3aaa7364e64c2dc4
refs/heads/main
1,683,761,214,467
1,622,821,295,000
1,622,821,295,000
357,236,048
12
0
null
null
null
null
UTF-8
Lean
false
false
7,298
lean
import data.nat.basic import tactic namespace tree -- Определим двоичное дерево, где у каждой внутренней вершины ровно два ребенка -- Такое дерево либо состоит из одной вершины - листа, либо состоит из корня, у которого есть левое и правое поддерево inductive Tree | Leaf : Tree | Branch (left : Tree) (right : Tree) : Tree -- Аналогично можно определить без названия аргументов inductive Tree2 | Leaf : Tree2 | Branch : Tree2 → Tree2 → Tree2 -- Откроем namespace Tree, чтобы писать Leaf и Branch вместо Tree.Leaf и Tree.Branch open Tree -- Для алгебраических типов данных Lean автоматически генерирует много функций и лемм (см. ниже) #print prefix tree.Tree /- Из них: Инъективность конструкторов: `Tree.Branch.inj : ∀ {l1 r1 l2 r2 : Tree}, Branch l1 r1 = Branch l2 r2 → l1 = l2 ∧ r1 = r2` Паттерн матчинг: Tree.cases_on : Π {C : Tree → Sort l} (n : Tree), C Leaf → (Π (left right : Tree), C (left.Branch right)) → C n Если мы можем доказать некоторое свойство `C` для `Leaf`, а также для `Branch left right` для любых `left` и `right`, то C верно для любого дерева. То же самое, если мы хотим определить функцию для дерева: чтобы определить функцию для дерева, нужно определить функцию на каждом из конструкторов. Рекурсор: Tree.rec_on : Π {C : Tree → Sort l} (n : Tree), C Leaf → (Π (left right : Tree), C left → C right → C (left.Branch right)) → C n Индукция по дереву: если свойство `C` верно для Leaf и из того, что верно C left и C right, верно C (Branch left right), то C верно для любого дерева. Аналогично можно определить функцию для любого дерева, для `cases_on` и `rec_on` нет разницы между утверждениями про деревья и функциями от деревьев (как мы знаем, утверждения <-> типы, и доказательства <-> элементы этого типа). -/ variables {T L R : Tree} -- Определим функцию "количество листьев" для дерева def leaves : Tree → ℕ | Leaf := 1 | (Branch left right) := leaves left + leaves right -- В примере выше важны скобки: надо явно показать, что это один аргумент типа `Tree` -- Определите самостоятельно функцию "количество внутренних вершин" для дерева def internal : Tree → ℕ | Leaf := 0 | (Branch left right) := 1 + internal left + internal right -- Доказывать утверждения про деревья можно по индукции! -- Когда цель выглядит как равенство натуральных чисел, используйте тактику `ring` или `linarith`, чтобы закрыть цель -- Или же можете поупражняться в применении `add_assoc` и `add_comm` lemma leaves_eq_internal_add_one : leaves T = 1 + internal T := begin induction T with L R hL hR, { rw [leaves, internal], }, { rw [leaves, internal, hL, hR], ring, }, end -- Добавим также леммы для `simp`, чтобы `leaves Leaf` и `leaves (Branch l r)` автоматически редуцировалось -- Они верны по определению, поэтому их доказательство - просто `rfl` (терм, а не тактика) @[simp] lemma leaves_Leaf : leaves Leaf = 1 := rfl @[simp] lemma leaves_Branch : leaves (Branch L R) = leaves L + leaves R := rfl -- Допишите самостоятельно simp-леммы для `internal` @[simp] lemma internal_Leaf : internal Leaf = 0 := rfl @[simp] lemma internal_Branch : internal (Branch L R) = 1 + internal L + internal R := rfl -- Теперь попробуйте максимально использовать simp при доказательстве -- После включения опции строчкой ниже simp будет выводить те переписывания, что происходили set_option trace.simplify.rewrite true example : leaves T = internal T + 1 := begin induction T with L R hL hR, { simp }, { simp [hL, hR], ring }, end -- Отключим опцию, чтобы не мешалась дальше set_option trace.simplify.rewrite false -- Потренируйтесь перед интервью в Google и напишите функцию, разворачивающую бинарное дерево def flip : Tree → Tree | Leaf := Leaf | (Branch left right) := Branch (flip right) (flip left) -- И докажите, что это инволюция lemma flip_flip : flip (flip T) = T := begin induction T with L R hL hR, { rwa [flip, flip], }, { rwa [flip, flip, hL, hR], } end -- Количество вершин в дереве не больше 2^высота - 1 -- Если вы не играли в max_minigame, то поищите в `data.nat.basic` полезные леммы про `max a b` -- Подсказки: -- С вычитанием ℕ работать неприятно, перейдите сначала к цели без вычитаний: для этого найдите подходящую функцию в библиотеке -- Постарайтесь свести цель к линейному неравенству, затем вызовите `linarith` -- Тактика `norm_num` помогает доказывать утверждения про конкретные числа, например, `1 ≤ 2` def depth : Tree → ℕ | Leaf := 1 | (Branch left right) := 1 + max (depth left) (depth right) def size : Tree → ℕ | Leaf := 1 | (Branch left right) := 1 + size left + size right lemma size_le_pow2_depth_minus_one : size T ≤ 2 ^ depth T - 1 := begin apply nat.le_sub_right_of_add_le, rw add_comm, induction T with L R hL hR, { rw [depth, size, pow_one], }, { rw [depth, size, pow_add, pow_one, two_mul], -- Тактика `replace` = `have` + `clear`, позволяет записать новое значение в `hL`, удалив старое replace hL : 1 + size L ≤ 2 ^ max (depth L) (depth R) := le_trans hL (pow_le_pow (by norm_num) (le_max_left _ _)), replace hR : 1 + size R ≤ 2 ^ max (depth L) (depth R) := le_trans hR (pow_le_pow (by norm_num) (le_max_right _ _)), linarith, } end -- Если вы можете придумать еще что-то интересное, что можно доказать про деревья, напишите! end tree
936843c5aeef894ae61b9cd19420bf44ed4d2616
2a2864136cf8f2871e86f5fd14e624e3daa8fd24
/Monads/List.lean
43928dc99d93e8a85d8a24f632d866f250614ca6
[ "MIT" ]
permissive
hargoniX/lean-monads
d054ac71a351b7c86f318a477977cc166117b8ec
2e87ca7ddf394641ea1b16bcbd8c384026d68e2f
refs/heads/main
1,693,530,528,286
1,633,100,386,000
1,633,100,386,000
412,509,769
1
0
null
null
null
null
UTF-8
Lean
false
false
10,843
lean
import Monads.Functor import Monads.Applicative import Monads.Monad namespace Monads inductive List (α : Type u) where | nil : List α | cons : α → List α → List α deriving Repr namespace List section Functor def map {α β : Type u} : (α → β) → (List α) → (List β) | f, nil => nil | f, (cons x xs) => cons (f x) (map f xs) #eval map (Nat.add 2) (cons 1 (cons 2 nil)) theorem map_nil (f : α → β) : map f nil = nil := by rfl theorem map_cons (f : α → β) (x : α) (xs : List α) : map f (cons x xs) = cons (f x) (map f xs) := by rfl theorem map_id_eq_id {xs : List α} : map id xs = xs := by induction xs with | nil => rw [map_nil] | cons x xs ih => rw [map_cons] rw [ih] rw [id_eq] instance : Functor List where fmap := map theorem fmap_def (f : α → β) (xs : List α) : f <$> xs = map f xs := by rfl instance : LawfulFunctor List where fmap_id := by intro α xs induction xs with | nil => rw [fmap_def, map_nil] | cons y ys ih => rw [fmap_def, map_cons] rw [id_eq] rw [←fmap_def] rw [ih] fmap_comp := by intro α β γ g h xs induction xs with | nil => rw [fmap_def, map_nil] rw [fmap_def g nil, map_nil] rw [fmap_def, map_nil] | cons y ys ih => rw [fmap_def, map_cons] rw [fmap_def g, map_cons] rw [fmap_def, map_cons] rw [←fmap_def, ←fmap_def, ←fmap_def] rw [ih] map_const_behaved := by -- Still the default definition so the proof is automated intro α β x y simp only [Functor.map_const, Functor.fmap] end Functor section Applicative def append : List α → List α → List α | nil, ys => ys | (cons x xs), ys => cons x (append xs ys) instance {α : Type u} : Append (List α) where append := append #eval (cons 1 (cons 2 nil)) ++ (cons 3 (cons 4 nil)) theorem nil_append (ys : List α) : nil ++ ys = ys := by rfl theorem cons_append (x : α) (xs : List α) (ys : List α) : (cons x xs) ++ ys = (cons x (xs ++ ys)) := by rfl theorem append_nil (xs : List α) : xs ++ nil = xs := by induction xs with | nil => rfl | cons x xs ih => rw [cons_append] rw [ih] theorem append_assoc {xs ys zs : List α} : xs ++ ys ++ zs = xs ++ (ys ++ zs) := by induction xs with | nil => rw [nil_append] rw [nil_append] | cons x xs ih => rw [cons_append] rw [cons_append] rw [cons_append] rw [ih] theorem append_eq_of_suffix (as bs cs : List α) : (as = bs) → (as ++ cs = bs ++ cs) := by intro h rw [h] def apply { α β : Type u} : List (α → β) → List α → List β | (cons f fs), xs => (f <$> xs) ++ (apply fs xs) | _, _ => nil instance : Applicative List where pure := λ x => (cons x nil) apply := apply theorem pure_def (x : α) : pure x = (cons x nil) := by rfl theorem apply_def {α β : Type u} (fs : List (α → β)) (xs : List α) : fs <*> xs = apply fs xs := by rfl theorem cons_apply {α β : Type u} (f : α → β) (fs : List (α → β)) (xs : List α) : (cons f fs) <*> xs = (f <$> xs) ++ (fs <*> xs) := by rfl theorem apply_nil {α β : Type u} (fs : List (α → β)) : fs <*> nil = nil := by induction fs with | nil => rfl | cons f fs ih => rw [cons_apply] rw [ih] rw [append_nil] rw [fmap_def, map_nil] theorem nil_apply {α β: Type u} (xs : List α) : @nil (α → β) <*> xs = nil := by rfl #eval Nat.add <$> (cons 1 (cons 2 nil)) <*> (cons 3 (cons 4 nil)) instance : LawfulApplicative List where apply_id := by intro α x rw [pure_def] rw [cons_apply] rw [nil_apply] rw [append_nil] rw [fmap_def, map_id_eq_id] apply_homomorphism := by intro α β x g rw [pure_def, pure_def, pure_def] rw [cons_apply] rw [nil_apply] rw [append_nil] rw [fmap_def, map_cons] rw [map_nil] apply_interchange := by intro α β x g rw [pure_def, pure_def] rw [cons_apply] rw [nil_apply] rw [append_nil] induction g with | nil => rw [nil_apply] rw [fmap_def, map_nil] | cons g gs ih => rw [cons_apply] rw [fmap_def, map_cons] rw [map_nil] rw [fmap_def, map_cons] rw [cons_append] rw [nil_append] rw [←fmap_def] rw [ih] apply_comp := sorry lifta2_behaved := by -- Definition wasn't changed -> auto proof intro α β γ g x y simp only [Applicative.liftA2, Applicative.apply] seq_right_behaved := by -- Definition wasn't changed -> auto proof intro α β a1 a2 simp only [Applicative.seq_right, Applicative.apply] seq_left_behaved := by -- Definition wasn't changed -> auto proof intro α β a1 a2 simp only [Applicative.seq_left, Applicative.liftA2] fmap_eq_pure_apply := by intro α β g x rw [pure_def] rw [cons_apply] rw [nil_apply] rw [append_nil] end Applicative section Monad def flatten : List (List α) → List α | nil => nil | (cons xs xss) => xs ++ flatten xss theorem flatten_nil : flatten nil = @nil α := by rfl theorem flatten_cons (xs : List α) (xss : List (List α)) : flatten (cons xs xss) = xs ++ flatten xss := by rfl def flat_map : List α → (α → List β) → List β | xs, f => flatten $ map f xs theorem flat_map_def (xs : List α) (f : α → List β) : flat_map xs f = (flatten $ map f xs) := by rfl theorem flat_map_nil (f : α → List β) : flat_map nil f = nil := by rfl theorem flat_map_cons (x : α) (xs : List α) (f : α → List β) : flat_map (cons x xs) f = (f x) ++ flat_map xs f := by rw [flat_map_def] rw [map_cons] rw [flatten_cons] rw [←flat_map_def] theorem flat_map_distrib_append {xs ys : List α} {f : α → List β} : flat_map (xs ++ ys) f = flat_map xs f ++ flat_map ys f := by induction xs with | nil => rw [nil_append] rw [flat_map_nil] rw [nil_append] | cons x xs ih => rw [cons_append] rw [flat_map_cons] rw [flat_map_cons] rw [ih] rw [append_assoc] theorem map_eq_constant_flat_map {α β : Type u} (f : α → β) (as : List α) : (map f as) = (flat_map as fun a => cons (f a) nil) := by induction as with | nil => rw [map_nil] rw [flat_map_nil] | cons a as ih => rw [map_cons] rw [flat_map_cons] rw [cons_append] rw [nil_append] rw [ih] instance : Monad List where ret := λ x => (cons x nil) bind := flat_map theorem ret_def (x : α) : ret x = cons x nil := by rfl theorem bind_def (xs : List α) (f : α → List β) : xs >>= f = flat_map xs f := by rfl #eval (cons 1 (cons 2 nil)) >>= (λ x => (cons x (cons x nil))) instance : LawfulMonad List where ret_left_id := by intro α β a h rw [ret_def] rw [bind_def, flat_map_def] rw [map_cons] rw [map_nil] rw [flatten_cons] rw [flatten_nil, append_nil] ret_right_id := by intro α as rw [bind_def] induction as with | nil => rw [flat_map_nil] | cons a as ih => rw [flat_map_cons] rw [ih] rw [ret_def] rw [cons_append] rw [nil_append] bind_assoc := by intro α β γ as g h induction as with | nil => rw [bind_def nil, flat_map_nil] rw [bind_def nil, flat_map_nil] rw [bind_def nil, flat_map_nil] | cons x xs ih => rw [bind_def (cons x xs), flat_map_cons] rw [bind_def (cons x xs), flat_map_cons] rw [←bind_def] rw [←bind_def] rw [←ih] rw [bind_def _ h] rw [flat_map_distrib_append] rw [←bind_def] rw [←bind_def] pure_behaved := by intro α a rw [pure_def, ret_def] apply_behaved := by intro α β mf ma induction mf with | nil => rw [nil_apply] rw [bind_def, flat_map_nil] | cons f fs fih => rw [cons_apply] rw [bind_def, flat_map_cons] rw [←bind_def] induction ma with | nil => rw [fmap_def, map_nil] rw [nil_append] rw [apply_nil] rw [bind_def, flat_map_nil] rw [nil_append] rw [←fih] rw [apply_nil] | cons a as aih => rw [←fih] rw [bind_def, flat_map_cons] rw [ret_def] rw [cons_append] rw [nil_append] rw [fmap_def, map_cons] rw [map_eq_constant_flat_map] -- we have to rewrite inside a function which is easier with simp only simp only [ret_def] and_then_behaved := by intro α β a1 a2 simp only [Monad.and_then, Applicative.seq_right] induction a1 with | nil => simp only [Functor.map_const] simp only [Function.comp] rw [map_nil] rw [←apply_def, nil_apply] rw [flat_map_nil] | cons x xs xih => rw [flat_map_cons] simp only [Function.const] simp only [Functor.map_const] simp only [Function.comp] rw [map_cons] simp only [Function.const] rw [←apply_def, cons_apply] rw [fmap_def, map_id_eq_id] rw [←xih] simp only [Functor.map_const] simp only [Function.comp] simp only [Function.const] rw [←apply_def] end Monad end List end Monads
5855dc61a24f4fd4d875ca8f200d40294a11caec
b147e1312077cdcfea8e6756207b3fa538982e12
/group_theory/subgroup.lean
43d88f2e31c30f7ae22d80730a95602ae805b6ff
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,670
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mitchell Rowett, Scott Morrison -/ import group_theory.submonoid open set function variables {α : Type*} {β : Type*} {s : set α} {a a₁ a₂ b c: α} section group variable [group α] lemma injective_mul {a : α} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ class is_subgroup [group α] (s : set α) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) instance subtype.group {s : set α} [is_subgroup s] : group s := { inv := λa, ⟨(a.1)⁻¹, is_subgroup.inv_mem a.2⟩, mul_left_inv := assume ⟨a, _⟩, subtype.eq $ mul_left_inv _, .. subtype.monoid } theorem is_subgroup.of_div [group α] (s : set α) (one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s): is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } def gpowers (x : α) : set α := {y | ∃i:ℤ, x^i = y} instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩ end group namespace is_subgroup open is_submonoid variable (s) variables [group α] [is_subgroup s] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := iff.intro (assume h, have a⁻¹⁻¹ ∈ s, from inv_mem h, by simpa) inv_mem lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := iff.intro (assume hba, have (b * a) * a⁻¹ ∈ s, from mul_mem hba (inv_mem h), by simpa) (assume hb, mul_mem hb h) lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := iff.intro (assume hab, have a⁻¹ * (a * b) ∈ s, from mul_mem (inv_mem h) hab, by simpa) (mul_mem h) end is_subgroup namespace group open is_submonoid is_subgroup variable [group α] inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : α} : in_closure a → in_closure a⁻¹ | mul {a b : α} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def closure (s : set α) : set α := {a | in_closure s a } lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic instance closure.is_subgroup (s : set α) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } theorem subset_closure {s : set α} : s ⊆ closure s := assume a, in_closure.basic theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} := subset.antisymm (assume x h, match x, h with _, ⟨i, rfl⟩ := gpow_mem (mem_closure $ by simp) end) (closure_subset $ by simp [mem_gpowers]) end group class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s) namespace is_subgroup variable [group α] -- Normal subgroup properties lemma mem_norm_comm {a b : α} {s : set α} [normal_subgroup s] (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h lemma mem_norm_comm_iff {a b : α} {s : set α} [normal_subgroup s] : a * b ∈ s ↔ b * a ∈ s := iff.intro mem_norm_comm mem_norm_comm /-- The trivial subgroup -/ def trivial (α : Type*) [group α] : set α := {1} @[simp] lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 := by simp [trivial] instance trivial_normal : normal_subgroup (trivial α) := by refine {..}; simp [trivial] {contextual := tt} lemma trivial_eq_closure : trivial α = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) instance univ_subgroup : normal_subgroup (@univ α) := by refine {..}; simp def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g} lemma mem_center {a : α} : a ∈ center α ↔ (∀g, g * a = a * g) := iff.refl _ instance center_normal : normal_subgroup (center α) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } end is_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup variables [group α] [group β] def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β) lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 := mem_trivial lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [mul f, inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←inv f, ←mul f] at this lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, mul f]⟩, one_mem := ⟨1, one_mem s, one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw inv f; simp *⟩ } instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [mul f, one f, inv f, @inv_mem β _ _ s] {contextual:=tt} instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [mul f, inv f] {contextual:=tt}⟩ instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial β) lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) : ker f = trivial α := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.one f] {contextual := tt}) lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] : function.injective f ↔ ker f = trivial α := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ end is_group_hom
9d71b8987411deed4720fe38eaa23f105df94320
9a0b1b3a653ea926b03d1495fef64da1d14b3174
/tidy/rewrite_search/discovery/collector/bundle.lean
7b11385ed64e1795110feae60ca4a989b0e318d8
[ "Apache-2.0" ]
permissive
khoek/mathlib-tidy
8623b27b4e04e7d598164e7eaf248610d58f768b
866afa6ab597c47f1b72e8fe2b82b97fff5b980f
refs/heads/master
1,585,598,975,772
1,538,659,544,000
1,538,659,544,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,054
lean
import data.rat import data.list import tidy.lib.list import tidy.rewrite_search.core.shared import ..types import .common open tactic open tidy.rewrite_search namespace tidy.rewrite_search.discovery def BUNDLE_CHUNK_SIZE := 1 -- TODO Be smarter about calculating this. meta def score_bundle (b : bundle_ref) (sample : list expr) : tactic ℚ := do mems ← b.get_members, mems.mfoldl (λ sum n, do e ← mk_const n, ret ← are_promising_rewrites (rewrite_list_from_lemma e) sample, return $ if ret then sum + 1 else sum ) 0 -- TODO report the lemma(s) which caused a selected bundle to be chosen, -- so that that lemma could just be tagged individually. -- TODO at the end of the search report which "desperations" things happened -- (bundles added, random lemmas found and used) so that they can be addressed -- more easily/conveniently. meta def try_bundles (conf : config) (p : progress) (sample : list expr) : tactic (progress × list (expr × bool)) := if p.persistence < persistence.try_bundles then return (p, []) else do bs ← list.filter (λ b, ¬p.seen_bundles.contains b) <$> get_bundles, bs ← bs.mmap $ λ b, (do s ← score_bundle b sample, return (b, s)), (awful_bs, interesting_bs) ← pure $ bs.partition $ λ b, b.2 = 0, let p := {p with seen_bundles := p.seen_bundles.append (awful_bs.map prod.fst)}, match interesting_bs.min_rel (λ a b, a.2 > b.2) with | none := do if conf.trace_discovery then discovery_trace format!"Could not find any promising bundles of the {bs.length} non-suggested bundles considered: {bs.map $ λ b, b.1.bundle.name}" else skip, return (p, []) | some (b, score) := do if conf.trace_discovery then discovery_trace format!"Found a promising bundle (of {bs.length} considered) \"{b.bundle.name}\"! If we succeed, please suggest this bundle for consideration." else skip, ms ← b.get_members >>= load_names, return (p, rewrite_list_from_lemmas ms) end end tidy.rewrite_search.discovery
795a511c19f9a73c18236999bf0d95faddd06d44
ca1ad81c8733787aba30f7a8d63f418508e12812
/clfrags/src/hilbert/wr/pt_neg.lean
aed1951b410c233d2a63db667332a706a2761556
[]
no_license
greati/hilbert-classical-fragments
5cdbe07851e979c8a03c621a5efd4d24bbfa333a
18a21ac6b2e890060eb4ae65752fc0245394d226
refs/heads/master
1,591,973,117,184
1,573,822,710,000
1,573,822,710,000
194,334,439
2
0
null
null
null
null
UTF-8
Lean
false
false
635
lean
import core.connectives import hilbert.wr.pt import hilbert.wr.proofs.pt namespace clfrags namespace hilbert namespace wr namespace pt_neg axiom n₁ : Π {a b : Prop}, a → neg a → b axiom ptn₁ : Π {a b c : Prop}, neg (pt a b c) → pt (neg a) b c axiom ptn₂ : Π {a b c : Prop}, pt (neg a) b c → neg (pt a b c) --axiom ptn₃ : Π {a b c : Prop}, neg (pt a b c) → pt a (neg b) c axiom ptn₃ : Π {a b c : Prop}, pt (neg a) b c → pt a (neg b) c end pt_neg end wr end hilbert end clfrags
d6a6cb8425bab9ce462c6a6eba8e386773e1acb4
a45212b1526d532e6e83c44ddca6a05795113ddc
/test/library_search/basic.lean
8bd6ba46087662c85170345d646b8cc077060351
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
1,746
lean
import data.nat.basic import tactic.library_search -- Turn off trace messages so they don't pollute the test build: set_option trace.silence_library_search true -- For debugging purposes, we can display the list of lemmas: -- set_option trace.library_search true -- Check that `library_search` fails if there are no goals. example : true := begin trivial, success_if_fail { library_search }, end example (a b : ℕ) : a + b = b + a := by library_search -- says: `exact add_comm a b` example {a b : ℕ} : a ≤ a + b := by library_search -- says: `exact le_add_right a b` example (n m k : ℕ) : n * (m - k) = n * m - n * k := by library_search -- says: `exact nat.mul_sub_left_distrib n m k` example {n m : ℕ} (h : m < n) : m ≤ n - 1 := by library_search -- says: `exact nat.le_pred_of_lt h` example {α : Type} (x y : α) : x = y ↔ y = x := by library_search -- says: `exact eq_comm` example (a b : ℕ) (ha : 0 < a) (hb : 0 < b) : 0 < a + b := by library_search -- says: `exact add_pos ha hb` example (a b : ℕ) : 0 < a → 0 < b → 0 < a + b := by library_search -- says: `exact add_pos` example (a b : ℕ) (h : a ∣ b) (w : b > 0) : a ≤ b := by library_search -- says: `exact nat.le_of_dvd w h` -- We even find `iff` results: example {b : ℕ} (w : b > 0) : b ≥ 1 := by library_search -- says: `exact nat.succ_le_iff.mpr w` example : ∀ P : Prop, ¬(P ↔ ¬P) := by library_search -- says: `λ (a : Prop), (iff_not_self a).mp` example {a b c : ℕ} (h₁ : a ∣ c) (h₂ : a ∣ b + c) : a ∣ b := by library_search -- says `exact (nat.dvd_add_left h₁).mp h₂` example {a b c : ℕ} (h₁ : a ∣ b) (h₂ : a ∣ b + c) : a ∣ c := by library_search -- says `exact (nat.dvd_add_left h₁).mp h₂`
c3ae2bddd9f8ff5a0096c7d5b79ebf7c12448281
367134ba5a65885e863bdc4507601606690974c1
/src/tactic/omega/main.lean
e2b6b50acfb71b58cea65539236097a28ed73cb3
[ "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
5,673
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ /- A tactic for discharging linear integer & natural number arithmetic goals using the Omega test. -/ import tactic.omega.int.main import tactic.omega.nat.main namespace omega open tactic meta def select_domain (t s : tactic (option bool)) : tactic (option bool) := do a ← t, b ← s, match a, b with | a, none := return a | none, b := return b | (some tt), (some tt) := return (some tt) | (some ff), (some ff) := return (some ff) | _, _ := failed end meta def type_domain (x : expr) : tactic (option bool) := if x = `(int) then return (some tt) else if x = `(nat) then return (some ff) else failed /-- Detects domain of a formula from its expr. * Returns none, if domain can be either ℤ or ℕ * Returns some tt, if domain is exclusively ℤ * Returns some ff, if domain is exclusively ℕ * Fails, if domain is neither ℤ nor ℕ -/ meta def form_domain : expr → tactic (option bool) | `(¬ %%px) := form_domain px | `(%%px ∨ %%qx) := select_domain (form_domain px) (form_domain qx) | `(%%px ∧ %%qx) := select_domain (form_domain px) (form_domain qx) | `(%%px ↔ %%qx) := select_domain (form_domain px) (form_domain qx) | `(%%(expr.pi _ _ px qx)) := monad.cond (if expr.has_var px then return tt else is_prop px) (select_domain (form_domain px) (form_domain qx)) (select_domain (type_domain px) (form_domain qx)) | `(@has_lt.lt %%dx %%h _ _) := type_domain dx | `(@has_le.le %%dx %%h _ _) := type_domain dx | `(@eq %%dx _ _) := type_domain dx | `(@ge %%dx %%h _ _) := type_domain dx | `(@gt %%dx %%h _ _) := type_domain dx | `(@ne %%dx _ _) := type_domain dx | `(true) := return none | `(false) := return none | x := failed meta def goal_domain_aux (x : expr) : tactic bool := (omega.int.wff x >> return tt) <|> (omega.nat.wff x >> return ff) /-- Use the current goal to determine. Return tt if the domain is ℤ, and return ff if it is ℕ -/ meta def goal_domain : tactic bool := do gx ← target, hxs ← local_context >>= monad.mapm infer_type, app_first goal_domain_aux (gx::hxs) /-- Return tt if the domain is ℤ, and return ff if it is ℕ -/ meta def determine_domain (opt : list name) : tactic bool := if `int ∈ opt then return tt else if `nat ∈ opt then return ff else goal_domain end omega open lean.parser interactive omega /-- Attempts to discharge goals in the quantifier-free fragment of linear integer and natural number arithmetic using the Omega test. Guesses the correct domain by looking at the goal and hypotheses, and then reverts all relevant hypotheses and variables. Use `omega manual` to disable automatic reverts, and `omega int` or `omega nat` to specify the domain. -/ meta def tactic.interactive.omega (opt : parse (many ident)) : tactic unit := do is_int ← determine_domain opt, let is_manual : bool := if `manual ∈ opt then tt else ff, if is_int then omega_int is_manual else omega_nat is_manual add_hint_tactic "omega" declare_trace omega /-- `omega` attempts to discharge goals in the quantifier-free fragment of linear integer and natural number arithmetic using the Omega test. In other words, the core procedure of `omega` works with goals of the form ```lean ∀ x₁, ... ∀ xₖ, P ``` where `x₁, ... xₖ` are integer (resp. natural number) variables, and `P` is a quantifier-free formula of linear integer (resp. natural number) arithmetic. For instance: ```lean example : ∀ (x y : int), (x ≤ 5 ∧ y ≤ 3) → x + y ≤ 8 := by omega ``` By default, `omega` tries to guess the correct domain by looking at the goal and hypotheses, and then reverts all relevant hypotheses and variables (e.g., all variables of type `nat` and `Prop`s in linear natural number arithmetic, if the domain was determined to be `nat`) to universally close the goal before calling the main procedure. Therefore, `omega` will often work even if the goal is not in the above form: ```lean example (x y : nat) (h : 2 * x + 1 = 2 * y) : false := by omega ``` But this behaviour is not always optimal, since it may revert irrelevant hypotheses or incorrectly guess the domain. Use `omega manual` to disable automatic reverts, and `omega int` or `omega nat` to specify the domain. ```lean example (x y z w : int) (h1 : 3 * y ≥ x) (h2 : z > 19 * w) : 3 * x ≤ 9 * y := by {revert h1 x y, omega manual} example (i : int) (n : nat) (h1 : i = 0) (h2 : n < n) : false := by omega nat example (n : nat) (h1 : n < 34) (i : int) (h2 : i * 9 = -72) : i = -8 := by {revert h2 i, omega manual int} ``` `omega` handles `nat` subtraction by repeatedly rewriting goals of the form `P[t-s]` into `P[x] ∧ (t = s + x ∨ (t ≤ s ∧ x = 0))`, where `x` is fresh. This means that each (distinct) occurrence of subtraction will cause the goal size to double during DNF transformation. `omega` implements the real shadow step of the Omega test, but not the dark and gray shadows. Therefore, it should (in principle) succeed whenever the negation of the goal has no real solution, but it may fail if a real solution exists, even if there is no integer/natural number solution. You can enable `set_option trace.omega true` to see how `omega` interprets your goal. -/ add_tactic_doc { name := "omega", category := doc_category.tactic, decl_names := [`tactic.interactive.omega], tags := ["finishing", "arithmetic", "decision procedure"] }
e2538f2b9055a963f0702719ba346f0114957baf
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/topology/instances/real.lean
2bb2fc59628e5f2151462514a77e57b273bf8930
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
14,892
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 The real numbers ℝ. They are constructed as the topological completion of ℚ. With the following steps: (1) prove that ℚ forms a uniform space. (2) subtraction and addition are uniform continuous functions in this space (3) for multiplication and inverse this only holds on bounded subsets (4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction) (5) extend the uniform continuous functions along the completion (6) proof field properties using the principle of extension of identities TODO generalizations: * topological groups & rings * order topologies * Archimedean fields -/ import topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring tactic.linarith noncomputable theory open classical set filter topological_space metric open_locale classical open_locale topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} instance : metric_space ℚ := metric_space.induced coe rat.cast_injective real.metric_space theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl @[norm_cast, simp] lemma rat.dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl section low_prio -- we want to ignore this instance for the next declaration local attribute [instance, priority 10] int.uniform_space instance : metric_space ℤ := begin letI M := metric_space.induced coe int.cast_injective real.metric_space, refine @metric_space.replace_uniformity _ int.uniform_space M (le_antisymm refl_le_uniformity $ λ r ru, mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h, mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩), have : (abs (↑a - ↑b) : ℝ) < 1 := h, have : abs (a - b) < 1, by norm_cast at this; assumption, have : abs (a - b) ≤ 0 := (@int.lt_add_one_iff _ 0).mp this, norm_cast, assumption end end low_prio theorem int.dist_eq (x y : ℤ) : dist x y = abs (x - y) := rfl @[norm_cast, simp] theorem int.dist_cast_real (x y : ℤ) : dist (x : ℝ) y = dist x y := rfl @[norm_cast, simp] theorem int.dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y := by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_of_rat.dense_embedding $ λ x, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.to_embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := uniform_embedding_of_rat.to_uniform_inducing.uniform_continuous_iff.2 $ by simp [(∘)]; exact real.uniform_continuous_add.comp ((uniform_continuous_of_rat.comp uniform_continuous_fst).prod_mk (uniform_continuous_of_rat.comp uniform_continuous_snd)) theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [real.dist_eq] using h⟩ theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℝ := uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg -- short-circuit type class inference instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : order_topology ℚ := induced_order_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma real.is_topological_basis_Ioo_rat : @is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_of_open_of_nhds (by simp [is_open_Ioo] {contextual:=tt}) (assume a v hav hv, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) := _ lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) := _ -/ lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) := by rw ← abs_pos_iff at r0; exact tendsto_of_uniform_continuous_subtype (real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h)) (mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma real.continuous_inv : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) := continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩, tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _) lemma real.continuous.inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) : continuous (λa, (f a)⁻¹) := show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩), from real.continuous_inv.comp (continuous_subtype_mk _ hf) lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := metric.uniform_continuous_iff.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma real.uniform_continuous_mul (s : set (ℝ × ℝ)) {r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩ protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (real.uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' (abs a₁ + 1)) (real.continuous_abs _ $ is_open_gt' (abs a₂ + 1))) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance -- short-circuit type class inference lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact real.continuous_mul.comp ((continuous_of_rat.comp continuous_fst).prod_mk (continuous_of_rat.comp continuous_snd)) instance : topological_ring ℚ := { continuous_mul := rat.continuous_mul, ..rat.topological_add_group } theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) := set.ext $ λ y, by rw [mem_ball, real.dist_eq, abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) := by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) := metric.totally_bounded_iff.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, use i, split, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end instance : complete_space ℝ := begin apply complete_of_cauchy_seq_tendsto, intros u hu, let c : cau_seq ℝ abs := ⟨u, cauchy_seq_iff'.1 hu⟩, refine ⟨c.lim, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, have := c.equiv_lim ε ε0, simp only [mem_map, mem_at_top_sets, mem_set_of_eq], refine this.imp (λ N hN n hn, hε (hN n hn)) end lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩ lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_coe_nat_real_at_top_iff.2 tendsto_id lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩ lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top := tendsto_coe_int_real_at_top_iff.2 tendsto_id section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2 (image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $ λ x hx, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ⟨_, hε (show abs _ < _, by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']), p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩ /- TODO(Mario): Put these back only if needed later lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} := _ lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) : closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} := _-/ lemma compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) instance : proper_space ℝ := { compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc } lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s := ⟨begin assume bdd, rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r) exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩ end, begin rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩, have I : s ⊆ Icc m M := λx hx, ⟨hm hx, hM hx⟩, have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) := by rw closed_ball_Icc; congr; ring, rw this at I, exact bounded.subset I bounded_closed_ball end⟩ end
c72e5c6cdc62f71d4f39f24f1e8dab054ad0ab88
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/measure_theory/outer_measure.lean
2f7a672caf5379c8abdc4fafc1cab5eaf2cd3eb4
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
35,360
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import analysis.specific_limits import measure_theory.measurable_space import topology.algebra.infinite_sum /-! # Outer Measures An outer measure is a function `μ : set α → ennreal`, from the powerset of a type to the extended nonnegative real numbers that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is monotone; 3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most the sum of the outer measure on the individual sets. Note that we do not need `α` to be measurable to define an outer measure. The outer measures on a type `α` form a complete lattice. Given an arbitrary function `m : set α → ennreal` that sends `∅` to `0` we can define an outer measure on `α` that on `s` is defined to be the infimum of `∑ᵢ, m (sᵢ)` for all collections of sets `sᵢ` that cover `s`. This is the unique maximal outer measure that is at most the given function. We also define this for functions `m` defined on a subset of `set α`, by treating the function as having value `∞` outside its domain. Given an outer measure `m`, the Carathéodory-measurable sets are the sets `s` such that for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. This forms a measurable space. ## Main definitions and statements * `outer_measure.of_function` is the greatest outer measure that is at most the given function. * `caratheodory` is the Carathéodory-measurable space of an outer measure. * `Inf_eq_of_function_Inf_gen` is a characterization of the infimum of outer measures. * `induced_outer_measure` is the measure induced by a function on a subset of `set α` ## References * <https://en.wikipedia.org/wiki/Outer_measure> * <https://en.wikipedia.org/wiki/Carath%C3%A9odory%27s_criterion> ## Tags outer measure, Carathéodory-measurable, Carathéodory's criterion -/ noncomputable theory open set finset function filter encodable open_locale classical big_operators namespace measure_theory /-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/ structure outer_measure (α : Type*) := (measure_of : set α → ennreal) (empty : measure_of ∅ = 0) (mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂) (Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ (∑'i, measure_of (s i))) namespace outer_measure section basic variables {α : Type*} {β : Type*} {ms : set (outer_measure α)} {m : outer_measure α} instance : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩ @[simp] lemma measure_of_eq_coe (m : outer_measure α) : m.measure_of = m := rfl @[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty theorem mono' (m : outer_measure α) {s₁ s₂} (h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h protected theorem Union (m : outer_measure α) {β} [encodable β] (s : β → set α) : m (⋃i, s i) ≤ (∑'i, m (s i)) := rel_supr_tsum m m.empty (≤) m.Union_nat s lemma Union_null (m : outer_measure α) {β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 := by simpa [h] using m.Union s protected lemma Union_finset (m : outer_measure α) (s : β → set α) (t : finset β) : m (⋃i ∈ t, s i) ≤ ∑ i in t, m (s i) := rel_supr_sum m m.empty (≤) m.Union_nat s t protected lemma union (m : outer_measure α) (s₁ s₂ : set α) : m (s₁ ∪ s₂) ≤ m s₁ + m s₂ := rel_sup_add m m.empty (≤) m.Union_nat s₁ s₂ lemma le_inter_add_diff {m : outer_measure α} {t : set α} (s : set α) : m t ≤ m (t ∩ s) + m (t \ s) := by { convert m.union _ _, rw inter_union_diff t s } lemma diff_null (m : outer_measure α) (s : set α) {t : set α} (ht : m t = 0) : m (s \ t) = m s := begin refine le_antisymm (m.mono $ diff_subset _ _) _, calc m s ≤ m (s ∩ t) + m (s \ t) : le_inter_add_diff _ ... ≤ m t + m (s \ t) : add_le_add_right (m.mono $ inter_subset_right _ _) _ ... = m (s \ t) : by rw [ht, zero_add] end lemma union_null (m : outer_measure α) {s₁ s₂ : set α} (h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 := by simpa [h₁, h₂] using m.union s₁ s₂ lemma coe_fn_injective ⦃μ₁ μ₂ : outer_measure α⦄ (h : ⇑μ₁ = μ₂) : μ₁ = μ₂ := by { cases μ₁, cases μ₂, congr, exact h } @[ext] lemma ext {μ₁ μ₂ : outer_measure α} (h : ∀ s, μ₁ s = μ₂ s) : μ₁ = μ₂ := coe_fn_injective $ funext h instance : has_zero (outer_measure α) := ⟨{ measure_of := λ_, 0, empty := rfl, mono := assume _ _ _, le_refl 0, Union_nat := assume s, zero_le _ }⟩ @[simp] theorem coe_zero : ⇑(0 : outer_measure α) = 0 := rfl instance : inhabited (outer_measure α) := ⟨0⟩ instance : has_add (outer_measure α) := ⟨λm₁ m₂, { measure_of := λs, m₁ s + m₂ s, empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty], mono := assume s₁ s₂ h, add_le_add (m₁.mono h) (m₂.mono h), Union_nat := assume s, calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤ (∑'i, m₁ (s i)) + (∑'i, m₂ (s i)) : add_le_add (m₁.Union_nat s) (m₂.Union_nat s) ... = _ : ennreal.tsum_add.symm}⟩ @[simp] theorem coe_add (m₁ m₂ : outer_measure α) : ⇑(m₁ + m₂) = m₁ + m₂ := rfl theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl instance add_comm_monoid : add_comm_monoid (outer_measure α) := { zero := 0, add := (+), .. injective.add_comm_monoid (show outer_measure α → set α → ennreal, from coe_fn) coe_fn_injective rfl (λ _ _, rfl) } instance : has_scalar ennreal (outer_measure α) := ⟨λ c m, { measure_of := λ s, c * m s, empty := by simp, mono := λ s t h, ennreal.mul_left_mono $ m.mono h, Union_nat := λ s, by { rw [ennreal.tsum_mul_left], exact ennreal.mul_left_mono (m.Union _) } }⟩ @[simp] lemma coe_smul (c : ennreal) (m : outer_measure α) : ⇑(c • m) = c • m := rfl lemma smul_apply (c : ennreal) (m : outer_measure α) (s : set α) : (c • m) s = c * m s := rfl instance : semimodule ennreal (outer_measure α) := { smul := (•), .. injective.semimodule ennreal ⟨show outer_measure α → set α → ennreal, from coe_fn, coe_zero, coe_add⟩ coe_fn_injective coe_smul } instance : has_bot (outer_measure α) := ⟨0⟩ instance outer_measure.order_bot : order_bot (outer_measure α) := { le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s, bot := 0, le_refl := assume a s, le_refl _, le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s), le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s), bot_le := assume a s, zero_le _ } section supremum instance : has_Sup (outer_measure α) := ⟨λms, { measure_of := λs, ⨆ m ∈ ms, (m : outer_measure α) s, empty := le_zero_iff_eq.1 $ bsupr_le $ λ m h, le_of_eq m.empty, mono := assume s₁ s₂ hs, bsupr_le_bsupr $ assume m hm, m.mono hs, Union_nat := assume f, bsupr_le $ assume m hm, calc m (⋃i, f i) ≤ (∑' (i : ℕ), m (f i)) : m.Union_nat _ ... ≤ (∑'i, ⨆ m ∈ ms, (m : outer_measure α) (f i)) : ennreal.tsum_le_tsum $ assume i, le_bsupr m hm }⟩ instance : complete_lattice (outer_measure α) := { .. outer_measure.order_bot, .. complete_lattice_of_Sup (outer_measure α) (λ ms, ⟨λ m hm s, le_bsupr m hm, λ m hm s, bsupr_le (λ m' hm', hm hm' s)⟩) } @[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) : (Sup ms) s = ⨆ m ∈ ms, (m : outer_measure α) s := rfl @[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) : (⨆ i : ι, f i) s = ⨆ i, f i s := by rw [supr, Sup_apply, supr_range, supr] @[norm_cast] theorem coe_supr {ι} (f : ι → outer_measure α) : ⇑(⨆ i, f i) = ⨆ i, f i := funext $ λ s, by rw [supr_apply, _root_.supr_apply] @[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s := by have := supr_apply (λ b, cond b m₁ m₂) s; rwa [supr_bool_eq, supr_bool_eq] at this end supremum /-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/ def map {β} (f : α → β) : outer_measure α →ₗ[ennreal] outer_measure β := { to_fun := λ m, { measure_of := λs, m (f ⁻¹' s), empty := m.empty, mono := λ s t h, m.mono (preimage_mono h), Union_nat := λ s, by rw [preimage_Union]; exact m.Union_nat (λ i, f ⁻¹' s i) }, map_add' := λ m₁ m₂, coe_fn_injective rfl, map_smul' := λ c m, coe_fn_injective rfl } @[simp] theorem map_apply {β} (f : α → β) (m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl @[simp] theorem map_id (m : outer_measure α) : map id m = m := ext $ λ s, rfl @[simp] theorem map_map {β γ} (f : α → β) (g : β → γ) (m : outer_measure α) : map g (map f m) = map (g ∘ f) m := ext $ λ s, rfl instance : functor outer_measure := {map := λ α β f, map f} instance : is_lawful_functor outer_measure := { id_map := λ α, map_id, comp_map := λ α β γ f g m, (map_map f g m).symm } /-- The dirac outer measure. -/ def dirac (a : α) : outer_measure α := { measure_of := λs, ⨆ h : a ∈ s, 1, empty := by simp, mono := λ s t h, supr_le_supr2 (λ h', ⟨h h', le_refl _⟩), Union_nat := λ s, supr_le $ λ h, let ⟨i, h⟩ := mem_Union.1 h in le_trans (by exact le_supr _ h) (ennreal.le_tsum i) } @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = ⨆ h : a ∈ s, 1 := rfl /-- The sum of an (arbitrary) collection of outer measures. -/ def sum {ι} (f : ι → outer_measure α) : outer_measure α := { measure_of := λs, ∑' i, f i s, empty := by simp, mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h), Union_nat := λ s, by rw ennreal.tsum_comm; exact ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) } @[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) : sum f s = ∑' i, f i s := rfl theorem smul_dirac_apply (a : ennreal) (b : α) (s : set α) : (a • dirac b) s = ⨆ h : b ∈ s, a := by by_cases b ∈ s; simp [h] /-- Pullback of an `outer_measure`: `comap f μ s = μ (f '' s)`. -/ def comap {β} (f : α → β) : outer_measure β →ₗ[ennreal] outer_measure α := { to_fun := λ m, { measure_of := λ s, m (f '' s), empty := by simp, mono := λ s t h, m.mono $ image_subset f h, Union_nat := λ s, by { rw [image_Union], apply m.Union_nat } }, map_add' := λ m₁ m₂, rfl, map_smul' := λ c m, rfl } @[simp] lemma comap_apply {β} (f : α → β) (m : outer_measure β) (s : set α) : comap f m s = m (f '' s) := rfl /-- Restrict an `outer_measure` to a set. -/ def restrict (s : set α) : outer_measure α →ₗ[ennreal] outer_measure α := (map coe).comp (comap (coe : s → α)) @[simp] lemma restrict_apply (s t : set α) (m : outer_measure α) : restrict s m t = m (t ∩ s) := by simp [restrict] theorem top_apply {s : set α} (h : s.nonempty) : (⊤ : outer_measure α) s = ⊤ := let ⟨a, as⟩ := h in top_unique $ le_trans (by simp [smul_dirac_apply, as]) (le_bsupr ((⊤ : ennreal) • dirac a) trivial) end basic section of_function set_option eqn_compiler.zeta true variables {α : Type*} (m : set α → ennreal) (m_empty : m ∅ = 0) include m_empty /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function : outer_measure α := let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑'i, m (f i) in { measure_of := μ, empty := le_antisymm (infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty]) (zero_le _), mono := assume s₁ s₂ hs, infi_le_infi $ assume f, infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩, Union_nat := assume s, ennreal.le_of_forall_epsilon_le $ begin assume ε hε (hb : (∑'i, μ (s i)) < ⊤), rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_lt_coe.2 hε) ℕ with ⟨ε', hε', hl⟩, refine le_trans _ (add_le_add_left (le_of_lt hl) _), rw ← ennreal.tsum_add, choose f hf using show ∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ (∑'i, m (f i)) < μ (s i) + ε' i, { intro, have : μ (s i) < μ (s i) + ε' i := ennreal.lt_add_right (lt_of_le_of_lt (by apply ennreal.le_tsum) hb) (by simpa using hε' i), simpa [μ, infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2), rw [← ennreal.tsum_prod, ← equiv.nat_prod_nat_equiv_nat.symm.tsum_eq], swap, {apply_instance}, refine infi_le_of_le _ (infi_le _ _), exact Union_subset (λ i, subset.trans (hf i).1 $ Union_subset $ λ j, subset.trans (by simp) $ subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)), end } variables {m m_empty} theorem of_function_le (s : set α) : outer_measure.of_function m m_empty s ≤ m s := let f : ℕ → set α := λi, nat.rec_on i s (λn s, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ calc (∑'i, m (f i)) = ∑ i in {0}, m (f i) : tsum_eq_sum $ by intro i; cases i; simp [m_empty] ... = m s : by simp; refl theorem of_function_eq (s : set α) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ (∑'i, m (s i))) : outer_measure.of_function m m_empty s = m s := le_antisymm (of_function_le s) $ le_infi $ λ f, le_infi $ λ hf, le_trans (m_mono hf) (m_subadd f) theorem le_of_function {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s := ⟨λ H s, le_trans (H s) (of_function_le s), λ H s, le_infi $ λ f, le_infi $ λ hs, le_trans (μ.mono hs) $ le_trans (μ.Union f) $ ennreal.tsum_le_tsum $ λ i, H _⟩ end of_function section caratheodory_measurable universe u parameters {α : Type u} (m : outer_measure α) include m local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc variables {s s₁ s₂ : set α} /-- A set `s` is Carathéodory-measurable for an outer measure `m` if for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. -/ def is_caratheodory (s : set α) : Prop := ∀t, m t = m (t ∩ s) + m (t \ s) lemma is_caratheodory_iff_le' {s : set α} : is_caratheodory s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ le_inter_add_diff _ @[simp] lemma is_caratheodory_empty : is_caratheodory ∅ := by simp [is_caratheodory, m.empty, diff_empty] lemma is_caratheodory_compl : is_caratheodory s₁ → is_caratheodory s₁ᶜ := by simp [is_caratheodory, diff_eq, add_comm] @[simp] lemma is_caratheodory_compl_iff : is_caratheodory sᶜ ↔ is_caratheodory s := ⟨λ h, by simpa using is_caratheodory_compl m h, is_caratheodory_compl⟩ lemma is_caratheodory_union (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) : is_caratheodory (s₁ ∪ s₂) := λ t, begin rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)), inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁, inter_eq_self_of_subset_right (set.subset_union_left _ _), union_diff_left, h₂ (t ∩ s₁)], simp [diff_eq, add_assoc] end lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : is_caratheodory s₁) {t : set α} : m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) := by rw [h₁, set.inter_assoc, set.union_inter_cancel_left, inter_diff_assoc, union_diff_cancel_left h] lemma is_caratheodory_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, is_caratheodory (s i)) → is_caratheodory (⋃i<n, s i) | 0 h := by simp [nat.not_lt_zero] | (n + 1) h := by rw Union_lt_succ; exact is_caratheodory_union m (h n (le_refl (n + 1))) (is_caratheodory_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) lemma is_caratheodory_inter (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) : is_caratheodory (s₁ ∩ s₂) := by { rw [← is_caratheodory_compl_iff, compl_inter], exact is_caratheodory_union _ (is_caratheodory_compl _ h₁) (is_caratheodory_compl _ h₂) } lemma is_caratheodory_sum {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) {t : set α} : ∀ {n}, ∑ i in finset.range n, m (t ∩ s i) = m (t ∩ ⋃i<n, s i) | 0 := by simp [nat.not_lt_zero, m.empty] | (nat.succ n) := begin simp [Union_lt_succ, range_succ], rw [measure_inter_union m _ (h n), is_caratheodory_sum], intro a, simpa [range_succ] using λ h₁ i hi h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩ end lemma is_caratheodory_Union_nat {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) : is_caratheodory (⋃i, s i) := is_caratheodory_iff_le'.2 $ λ t, begin have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)), { convert m.Union (λ i, t ∩ s i), { rw inter_Union }, { simp [ennreal.tsum_eq_supr_nat, is_caratheodory_sum m h hd] } }, refine le_trans (add_le_add_right hp _) _, rw ennreal.supr_add, refine supr_le (λ n, le_trans (add_le_add_left _ _) (ge_of_eq (is_caratheodory_Union_lt m (λ i _, h i) _))), refine m.mono (diff_subset_diff_right _), exact bUnion_subset (λ i _, subset_Union _ i), end lemma f_Union {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) := begin refine le_antisymm (m.Union_nat s) _, rw ennreal.tsum_eq_supr_nat, refine supr_le (λ n, _), have := @is_caratheodory_sum _ m _ h hd univ n, simp at this, simp [this], exact m.mono (bUnion_subset (λ i _, subset_Union _ i)), end /-- The Carathéodory-measurable sets for an outer measure `m` form a Dynkin system. -/ def caratheodory_dynkin : measurable_space.dynkin_system α := { has := is_caratheodory, has_empty := is_caratheodory_empty, has_compl := assume s, is_caratheodory_compl, has_Union_nat := assume f hf hn, is_caratheodory_Union_nat hn hf } /-- Given an outer measure `μ`, the Carathéodory-measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory : measurable_space α := caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, is_caratheodory_inter lemma is_caratheodory_iff {s : set α} : caratheodory.is_measurable' s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_iff_le {s : set α} : caratheodory.is_measurable' s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := is_caratheodory_iff_le' protected lemma Union_eq_of_caratheodory {s : ℕ → set α} (h : ∀i, caratheodory.is_measurable' (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) := f_Union h hd end caratheodory_measurable variables {α : Type*} lemma of_function_caratheodory {m : set α → ennreal} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.is_measurable' s := begin apply (is_caratheodory_iff_le _).mpr, refine λ t, le_infi (λ f, le_infi $ λ hf, _), refine le_trans (add_le_add (infi_le_of_le (λi, f i ∩ s) $ infi_le _ _) (infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _, { rw ← Union_inter, exact inter_subset_inter_left _ hf }, { rw ← Union_diff, exact diff_subset_diff_left hf }, { rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) } end @[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ := top_unique $ λ s _ t, (add_zero _).symm theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ := top_unique $ assume s hs, (is_caratheodory_iff_le _).2 $ assume t, t.eq_empty_or_nonempty.elim (λ ht, by simp [ht]) (λ ht, by simp only [ht, top_apply, le_top]) theorem le_add_caratheodory (m₁ m₂ : outer_measure α) : m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory := λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t, add_left_comm, add_assoc] theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) : (⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory := λ s h t, by simp [λ i, measurable_space.is_measurable_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ennreal) (m : outer_measure α) : m.caratheodory ≤ (a • m).caratheodory := λ s h t, by simp [h t, mul_add] @[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ := top_unique $ λ s _ t, begin by_cases a ∈ t; simp [h], by_cases a ∈ s; simp [h] end section Inf_gen /-- Given a set of outer measures, we define a new function that on a set `s` is defined to be the infimum of `μ(s)` for the outer measures `μ` in the collection. We ensure that this function is defined to be `0` on `∅`, even if the collection of outer measures is empty. The outer measure generated by this function is the infimum of the given outer measures. -/ def Inf_gen (m : set (outer_measure α)) (s : set α) : ennreal := ⨆(h : s.nonempty), ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s @[simp] lemma Inf_gen_empty (m : set (outer_measure α)) : Inf_gen m ∅ = 0 := by simp [Inf_gen, empty_not_nonempty] lemma Inf_gen_nonempty1 (m : set (outer_measure α)) (t : set α) (h : t.nonempty) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := by rw [Inf_gen, supr_pos h] lemma Inf_gen_nonempty2 (m : set (outer_measure α)) (μ) (h : μ ∈ m) (t) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin cases t.eq_empty_or_nonempty with ht ht, { simp [ht], refine (bot_unique $ infi_le_of_le μ $ _).symm, refine infi_le_of_le h (le_refl ⊥) }, { exact Inf_gen_nonempty1 m t ht } end lemma Inf_eq_of_function_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.of_function (Inf_gen m) (Inf_gen_empty m) := begin refine le_antisymm (assume t', le_of_function.2 (assume t, _) _) (le_Inf $ assume μ hμ t, le_trans (outer_measure.of_function_le _) _); cases t.eq_empty_or_nonempty with ht ht; simp [ht, Inf_gen_nonempty1], { assume μ hμ, exact (show Inf m ≤ μ, from _root_.Inf_le hμ) t }, { exact infi_le_of_le μ (infi_le _ hμ) } end end Inf_gen end outer_measure open outer_measure /-! ### Induced Outer Measure We can extend a function defined on a subset of `set α` to an outer measure. The underlying function is called `extend`, and the measure it induces is called `induced_outer_measure`. Some lemmas below are proven twice, once in the general case, and one where the function `m` is only defined on measurable sets (i.e. when `P = is_measurable`). In the latter cases, we can remove some hypotheses in the statement. The general version has the same name, but with a prime at the end. -/ section extend variables {α : Type*} {P : α → Prop} variables (m : Π (s : α), P s → ennreal) /-- We can trivially extend a function defined on a subclass of objects (with codomain `ennreal`) to all objects by defining it to be `∞` on the objects not in the class. -/ def extend (s : α) : ennreal := ⨅ h : P s, m s h lemma extend_eq {s : α} (h : P s) : extend m s = m s h := by simp [extend, h] lemma le_extend {s : α} (h : P s) : m s h ≤ extend m s := by { simp only [extend, le_infi_iff], intro, refl' } end extend section extend_set variables {α : Type*} {P : set α → Prop} variables {m : Π (s : set α), P s → ennreal} variables (P0 : P ∅) (m0 : m ∅ P0 = 0) variables (PU : ∀{{f : ℕ → set α}} (hm : ∀i, P (f i)), P (⋃i, f i)) variables (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)), pairwise (disjoint on f) → m (⋃i, f i) (PU hm) = (∑'i, m (f i) (hm i))) variables (msU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)), m (⋃i, f i) (PU hm) ≤ (∑'i, m (f i) (hm i))) variables (m_mono : ∀⦃s₁ s₂ : set α⦄ (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) lemma extend_empty : extend m ∅ = 0 := (extend_eq _ P0).trans m0 lemma extend_Union_nat {f : ℕ → set α} (hm : ∀i, P (f i)) (mU : m (⋃i, f i) (PU hm) = (∑'i, m (f i) (hm i))) : extend m (⋃i, f i) = (∑'i, extend m (f i)) := (extend_eq _ _).trans $ mU.trans $ by { congr' with i, rw extend_eq } section subadditive include PU msU lemma extend_Union_le_tsum_nat' (s : ℕ → set α) : extend m (⋃i, s i) ≤ (∑'i, extend m (s i)) := begin by_cases h : ∀i, P (s i), { rw [extend_eq _ (PU h), congr_arg tsum _], { apply msU h }, funext i, apply extend_eq _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } end end subadditive section mono include m_mono lemma extend_mono' ⦃s₁ s₂ : set α⦄ (h₁ : P s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := by { refine le_infi _, intro h₂, rw [extend_eq m h₁], exact m_mono h₁ h₂ hs } end mono section unions include P0 m0 PU mU lemma extend_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (hm : ∀i, P (f i)) : extend m (⋃i, f i) = (∑'i, extend m (f i)) := begin rw [← encodable.Union_decode2, ← tsum_Union_decode2], { exact extend_Union_nat PU (λ n, encodable.Union_decode2_cases P0 hm) (mU _ (encodable.Union_decode2_disjoint_on hd)) }, { exact extend_empty P0 m0 } end lemma extend_union {s₁ s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : P s₁) (h₂ : P s₂) : extend m (s₁ ∪ s₂) = extend m s₁ + extend m s₂ := begin rw [union_eq_Union, extend_Union P0 m0 PU mU (pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨h₂, h₁⟩), tsum_fintype], simp end end unions variable (m) /-- Given an arbitrary function on a subset of sets, we can define the outer measure corresponding to it (this is the unique maximal outer measure that is at most `m` on the domain of `m`). -/ def induced_outer_measure : outer_measure α := outer_measure.of_function (extend m) (extend_empty P0 m0) variables {m P0 m0} include msU m_mono lemma induced_outer_measure_eq_extend' {s : set α} (hs : P s) : induced_outer_measure m P0 m0 s = extend m s := of_function_eq s (λ t, extend_mono' m_mono hs) (extend_Union_le_tsum_nat' PU msU) lemma induced_outer_measure_eq' {s : set α} (hs : P s) : induced_outer_measure m P0 m0 s = m s hs := (induced_outer_measure_eq_extend' PU msU m_mono hs).trans $ extend_eq _ _ lemma induced_outer_measure_eq_infi (s : set α) : induced_outer_measure m P0 m0 s = ⨅ (t : set α) (ht : P t) (h : s ⊆ t), m t ht := begin apply le_antisymm, { simp only [le_infi_iff], intros t ht, simp only [le_infi_iff], intro hs, refine le_trans (mono' _ hs) _, exact le_of_eq (induced_outer_measure_eq' _ msU m_mono _) }, { refine le_infi _, intro f, refine le_infi _, intro hf, refine le_trans _ (extend_Union_le_tsum_nat' _ msU _), refine le_infi _, intro h2f, refine infi_le_of_le _ (infi_le_of_le h2f $ infi_le _ hf) } end lemma induced_outer_measure_preimage (f : α ≃ α) (Pm : ∀ (s : set α), P (f ⁻¹' s) ↔ P s) (mm : ∀ (s : set α) (hs : P s), m (f ⁻¹' s) ((Pm _).mpr hs) = m s hs) {A : set α} : induced_outer_measure m P0 m0 (f ⁻¹' A) = induced_outer_measure m P0 m0 A := begin simp only [induced_outer_measure_eq_infi _ msU m_mono], symmetry, refine infi_congr (preimage f) f.injective.preimage_surjective _, intro s, refine infi_congr_Prop (Pm s) _, intro hs, refine infi_congr_Prop f.surjective.preimage_subset_preimage_iff _, intro h2s, exact mm s hs end lemma induced_outer_measure_exists_set {s : set α} (hs : induced_outer_measure m P0 m0 s < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ (t : set α) (ht : P t), s ⊆ t ∧ induced_outer_measure m P0 m0 t ≤ induced_outer_measure m P0 m0 s + ε := begin have := ennreal.lt_add_right hs (ennreal.zero_lt_coe_iff.2 hε), conv at this {to_lhs, rw induced_outer_measure_eq_infi _ msU m_mono }, simp only [infi_lt_iff] at this, rcases this with ⟨t, h1t, h2t, h3t⟩, exact ⟨t, h1t, h2t, le_trans (le_of_eq $ induced_outer_measure_eq' _ msU m_mono h1t) (le_of_lt h3t)⟩ end /-- To test whether `s` is Carathéodory-measurable we only need to check the sets `t` for which `P t` holds. See `of_function_caratheodory` for another way to show the Carathéodory-measurability of `s`. -/ lemma induced_outer_measure_caratheodory (s : set α) : (induced_outer_measure m P0 m0).caratheodory.is_measurable' s ↔ ∀ (t : set α), P t → induced_outer_measure m P0 m0 (t ∩ s) + induced_outer_measure m P0 m0 (t \ s) ≤ induced_outer_measure m P0 m0 t := begin rw is_caratheodory_iff_le, split, { intros h t ht, exact h t }, { intros h u, conv_rhs { rw induced_outer_measure_eq_infi _ msU m_mono }, refine le_infi _, intro t, refine le_infi _, intro ht, refine le_infi _, intro h2t, refine le_trans _ (le_trans (h t ht) $ le_of_eq $ induced_outer_measure_eq' _ msU m_mono ht), refine add_le_add (mono' _ $ set.inter_subset_inter_left _ h2t) (mono' _ $ diff_subset_diff_left h2t) } end end extend_set /-! If `P` is `is_measurable` for some measurable space, then we can remove some hypotheses of the above lemmas. -/ section measurable_space variables {α : Type*} [measurable_space α] variables {m : Π (s : set α), is_measurable s → ennreal} variables (m0 : m ∅ is_measurable.empty = 0) variable (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union hm) = (∑'i, m (f i) (hm i))) include m0 mU lemma extend_mono {s₁ s₂ : set α} (h₁ : is_measurable s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := begin refine le_infi _, intro h₂, have := extend_union is_measurable.empty m0 is_measurable.Union mU disjoint_diff h₁ (h₂.diff h₁), rw union_diff_cancel hs at this, rw ← extend_eq m, exact le_iff_exists_add.2 ⟨_, this⟩, end lemma extend_Union_le_tsum_nat : ∀ (s : ℕ → set α), extend m (⋃i, s i) ≤ (∑'i, extend m (s i)) := begin refine extend_Union_le_tsum_nat' is_measurable.Union _, intros f h, simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (is_measurable.disjointed h) disjoint_disjointed], refine ennreal.tsum_le_tsum (λ i, _), rw [← extend_eq m, ← extend_eq m], exact extend_mono m0 mU (is_measurable.disjointed h _) (inter_subset_left _ _) end lemma induced_outer_measure_eq_extend {s : set α} (hs : is_measurable s) : induced_outer_measure m is_measurable.empty m0 s = extend m s := of_function_eq s (λ t, extend_mono m0 mU hs) (extend_Union_le_tsum_nat m0 mU) lemma induced_outer_measure_eq {s : set α} (hs : is_measurable s) : induced_outer_measure m is_measurable.empty m0 s = m s hs := (induced_outer_measure_eq_extend m0 mU hs).trans $ extend_eq _ _ end measurable_space namespace outer_measure variables {α : Type*} [measurable_space α] (m : outer_measure α) /-- Given an outer measure `m` we can forget its value on non-measurable sets, and then consider `m.trim`, the unique maximal outer measure less than that function. -/ def trim : outer_measure α := induced_outer_measure (λ s _, m s) is_measurable.empty m.empty theorem le_trim : m ≤ m.trim := le_of_function.mpr $ λ s, le_infi $ λ _, le_refl _ theorem trim_eq {s : set α} (hs : is_measurable s) : m.trim s = m s := induced_outer_measure_eq' is_measurable.Union (λ f hf, m.Union_nat f) (λ _ _ _ _ h, m.mono h) hs theorem trim_congr {m₁ m₂ : outer_measure α} (H : ∀ {s : set α}, is_measurable s → m₁ s = m₂ s) : m₁.trim = m₂.trim := by { unfold trim, congr, funext s hs, exact H hs } theorem trim_le_trim {m₁ m₂ : outer_measure α} (H : m₁ ≤ m₂) : m₁.trim ≤ m₂.trim := λ s, binfi_le_binfi $ λ f hs, ennreal.tsum_le_tsum $ λ b, infi_le_infi $ λ hf, H _ theorem le_trim_iff {m₁ m₂ : outer_measure α} : m₁ ≤ m₂.trim ↔ ∀ s, is_measurable s → m₁ s ≤ m₂ s := le_of_function.trans $ forall_congr $ λ s, le_infi_iff theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), m t := by { simp only [infi_comm] {single_pass := tt}, exact induced_outer_measure_eq_infi is_measurable.Union (λ f _, m.Union_nat f) (λ _ _ _ _ h, m.mono h) s } theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ is_measurable t}, m t := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim := le_antisymm (le_trim_iff.2 $ λ s hs, by simp [trim_eq _ hs, le_refl]) (le_trim _) @[simp] theorem trim_zero : (0 : outer_measure α).trim = 0 := ext $ λ s, le_antisymm (le_trans ((trim 0).mono (subset_univ s)) $ le_of_eq $ trim_eq _ is_measurable.univ) (zero_le _) theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim := begin ext1 s, simp only [trim_eq_infi', add_apply], rw ennreal.infi_add_infi, rintro ⟨t₁, st₁, ht₁⟩ ⟨t₂, st₂, ht₂⟩, exact ⟨⟨_, subset_inter_iff.2 ⟨st₁, st₂⟩, ht₁.inter ht₂⟩, add_le_add (m₁.mono' (inter_subset_left _ _)) (m₂.mono' (inter_subset_right _ _))⟩, end theorem trim_sum_ge {ι} (m : ι → outer_measure α) : sum (λ i, (m i).trim) ≤ (sum m).trim := λ s, by simp [trim_eq_infi]; exact λ t st ht, ennreal.tsum_le_tsum (λ i, infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht) lemma exists_is_measurable_superset_of_trim_eq_zero {m : outer_measure α} {s : set α} (h : m.trim s = 0) : ∃t, s ⊆ t ∧ is_measurable t ∧ m t = 0 := begin erw [trim_eq_infi, infi_eq_bot] at h, choose t ht using show ∀n:ℕ, ∃t, s ⊆ t ∧ is_measurable t ∧ m t < n⁻¹, { assume n, have : (0 : ennreal) < n⁻¹ := (ennreal.inv_pos.2 $ ennreal.nat_ne_top _), rcases h _ this with ⟨t, ht⟩, use [t], simpa only [infi_lt_iff, exists_prop] using ht }, refine ⟨⋂n, t n, subset_Inter (λn, (ht n).1), is_measurable.Inter (λn, (ht n).2.1), _⟩, refine le_antisymm _ (zero_le _), refine le_of_tendsto_of_tendsto tendsto_const_nhds ennreal.tendsto_inv_nat_nhds_zero (eventually_of_forall $ assume n, _), exact le_trans (m.mono' $ Inter_subset _ _) (le_of_lt (ht n).2.2) end theorem trim_smul (c : ennreal) (m : outer_measure α) : (c • m).trim = c • m.trim := begin ext1 s, simp only [trim_eq_infi', smul_apply], haveI : nonempty {t // s ⊆ t ∧ is_measurable t} := ⟨⟨univ, subset_univ _, is_measurable.univ⟩⟩, refine ennreal.infi_mul_left (assume hc hs, _), rw ← trim_eq_infi' at hs, simpa [and_assoc] using exists_is_measurable_superset_of_trim_eq_zero hs end end outer_measure end measure_theory
20838783f091eec8665507f5edccdeb4a81b7dac
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/Quotation.lean
34716cc4dd62009740b57edb79a2f8b8e4c83589
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
24,851
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich Elaboration of syntax quotations as terms and patterns (in `match_syntax`). See also `./Hygiene.lean` for the basic hygiene workings and data types. -/ import Lean.Syntax import Lean.ResolveName import Lean.Elab.Term import Lean.Elab.Quotation.Util import Lean.Elab.Quotation.Precheck import Lean.Parser.Term namespace Lean.Elab.Term.Quotation open Lean.Parser.Term open Lean.Syntax open Meta /-- `C[$(e)]` ~> `let a := e; C[$a]`. Used in the implementation of antiquot splices. -/ private partial def floatOutAntiquotTerms : Syntax → StateT (Syntax → TermElabM Syntax) TermElabM Syntax | stx@(Syntax.node k args) => do if isAntiquot stx && !isEscapedAntiquot stx then let e := getAntiquotTerm stx if !e.isIdent || !e.getId.isAtomic then return ← withFreshMacroScope do let a ← `(a) modify (fun cont stx => (`(let $a:ident := $e; $stx) : TermElabM _)) stx.setArg 2 a Syntax.node k (← args.mapM floatOutAntiquotTerms) | stx => pure stx private def getSepFromSplice (splice : Syntax) : Syntax := do if let Syntax.atom _ sep := getAntiquotSpliceSuffix splice then Syntax.mkStrLit (sep.dropRight 1) else unreachable! partial def mkTuple : Array Syntax → TermElabM Syntax | #[] => `(Unit.unit) | #[e] => e | es => do let stx ← mkTuple (es.eraseIdx 0) `(Prod.mk $(es[0]) $stx) def resolveSectionVariable (sectionVars : NameMap Name) (id : Name) : List (Name × List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id let rec loop : Name → List String → List (Name × List String) | id@(Name.str p s _), projs => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { extractionResult with name := id }.review match sectionVars.find? id with | some newId => [(newId, projs)] | none => loop p (s::projs) | _, _ => [] loop extractionResult.name [] /-- Transform sequence of pushes and appends into acceptable code -/ def ArrayStxBuilder := Sum (Array Syntax) Syntax namespace ArrayStxBuilder def empty : ArrayStxBuilder := Sum.inl #[] def build : ArrayStxBuilder → Syntax | Sum.inl elems => quote elems | Sum.inr arr => arr def push (b : ArrayStxBuilder) (elem : Syntax) : ArrayStxBuilder := match b with | Sum.inl elems => Sum.inl <| elems.push elem | Sum.inr arr => Sum.inr <| mkCApp ``Array.push #[arr, elem] def append (b : ArrayStxBuilder) (arr : Syntax) (appendName := ``Array.append) : ArrayStxBuilder := Sum.inr <| mkCApp appendName #[b.build, arr] end ArrayStxBuilder -- Elaborate the content of a syntax quotation term private partial def quoteSyntax : Syntax → TermElabM Syntax | Syntax.ident info rawVal val preresolved => do if !hygiene.get (← getOptions) then return ← `(Syntax.ident info $(quote rawVal) $(quote val) $(quote preresolved)) -- Add global scopes at compilation time (now), add macro scope at runtime (in the quotation). -- See the paper for details. let r ← resolveGlobalName val -- extension of the paper algorithm: also store unique section variable names as top-level scopes -- so they can be captured and used inside the section, but not outside let r' := resolveSectionVariable (← read).sectionVars val let preresolved := r ++ r' ++ preresolved let val := quote val -- `scp` is bound in stxQuot.expand `(Syntax.ident info $(quote rawVal) (addMacroScope mainModule $val scp) $(quote preresolved)) -- if antiquotation, insert contents as-is, else recurse | stx@(Syntax.node k _) => do if isAntiquot stx && !isEscapedAntiquot stx then getAntiquotTerm stx else if isTokenAntiquot stx && !isEscapedAntiquot stx then match stx[0] with | Syntax.atom _ val => `(Syntax.atom (Option.getD (getHeadInfo? $(getAntiquotTerm stx)) info) $(quote val)) | _ => throwErrorAt stx "expected token" else if isAntiquotSuffixSplice stx && !isEscapedAntiquot stx then -- splices must occur in a `many` node throwErrorAt stx "unexpected antiquotation splice" else if isAntiquotSplice stx && !isEscapedAntiquot stx then throwErrorAt stx "unexpected antiquotation splice" else -- if escaped antiquotation, decrement by one escape level let stx := unescapeAntiquot stx let mut args := ArrayStxBuilder.empty let appendName := if (← getEnv).contains ``Array.append then ``Array.append else ``Array.appendCore for arg in stx.getArgs do if k == nullKind && isAntiquotSuffixSplice arg then let antiquot := getAntiquotSuffixSpliceInner arg args := args.append (appendName := appendName) <| ← match antiquotSuffixSplice? arg with | `optional => `(match $(getAntiquotTerm antiquot):term with | some x => Array.empty.push x | none => Array.empty) | `many => getAntiquotTerm antiquot | `sepBy => `(@SepArray.elemsAndSeps $(getSepFromSplice arg) $(getAntiquotTerm antiquot)) | k => throwErrorAt arg "invalid antiquotation suffix splice kind '{k}'" else if k == nullKind && isAntiquotSplice arg then let k := antiquotSpliceKind? arg let (arg, bindLets) ← floatOutAntiquotTerms arg |>.run pure let inner ← (getAntiquotSpliceContents arg).mapM quoteSyntax let ids ← getAntiquotationIds arg if ids.isEmpty then throwErrorAt stx "antiquotation splice must contain at least one antiquotation" let arr ← match k with | `optional => `(match $[$ids:ident],* with | $[some $ids:ident],* => $(quote inner) | none => Array.empty) | _ => let arr ← ids[:ids.size-1].foldrM (fun id arr => `(Array.zip $id $arr)) ids.back `(Array.map (fun $(← mkTuple ids) => $(inner[0])) $arr) let arr ← if k == `sepBy then `(mkSepArray $arr (mkAtom $(getSepFromSplice arg))) else arr let arr ← bindLets arr args := args.append arr else do let arg ← quoteSyntax arg args := args.push arg `(Syntax.node $(quote k) $(args.build)) | Syntax.atom _ val => `(Syntax.atom info $(quote val)) | Syntax.missing => throwUnsupportedSyntax def stxQuot.expand (stx : Syntax) : TermElabM Syntax := do /- Syntax quotations are monadic values depending on the current macro scope. For efficiency, we bind the macro scope once for each quotation, then build the syntax tree in a completely pure computation depending on this binding. Note that regular function calls do not introduce a new macro scope (i.e. we preserve referential transparency), so we can refer to this same `scp` inside `quoteSyntax` by including it literally in a syntax quotation. -/ -- TODO: simplify to `(do scp ← getCurrMacroScope; pure $(quoteSyntax quoted)) let stx ← quoteSyntax stx.getQuotContent; `(Bind.bind MonadRef.mkInfoFromRefPos (fun info => Bind.bind getCurrMacroScope (fun scp => Bind.bind getMainModule (fun mainModule => Pure.pure $stx)))) /- NOTE: It may seem like the newly introduced binding `scp` may accidentally capture identifiers in an antiquotation introduced by `quoteSyntax`. However, note that the syntax quotation above enjoys the same hygiene guarantees as anywhere else in Lean; that is, we implement hygienic quotations by making use of the hygienic quotation support of the bootstrapped Lean compiler! Aside: While this might sound "dangerous", it is in fact less reliant on a "chain of trust" than other bootstrapping parts of Lean: because this implementation itself never uses `scp` (or any other identifier) both inside and outside quotations, it can actually correctly be compiled by an unhygienic (but otherwise correct) implementation of syntax quotations. As long as it is then compiled again with the resulting executable (i.e. up to stage 2), the result is a correct hygienic implementation. In this sense the implementation is "self-stabilizing". It was in fact originally compiled by an unhygienic prototype implementation. -/ macro "elab_stx_quot" kind:ident : command => `(@[builtinTermElab $kind:ident] def elabQuot : TermElab := adaptExpander stxQuot.expand) -- elab_stx_quot Parser.Level.quot elab_stx_quot Parser.Term.quot elab_stx_quot Parser.Term.funBinder.quot elab_stx_quot Parser.Term.bracketedBinder.quot elab_stx_quot Parser.Term.matchDiscr.quot elab_stx_quot Parser.Tactic.quot elab_stx_quot Parser.Tactic.quotSeq elab_stx_quot Parser.Term.stx.quot elab_stx_quot Parser.Term.prec.quot elab_stx_quot Parser.Term.attr.quot elab_stx_quot Parser.Term.prio.quot elab_stx_quot Parser.Term.doElem.quot elab_stx_quot Parser.Term.dynamicQuot /- match -/ -- an "alternative" of patterns plus right-hand side private abbrev Alt := List Syntax × Syntax /-- In a single match step, we match the first discriminant against the "head" of the first pattern of the first alternative. This datatype describes what kind of check this involves, which helps other patterns decide if they are covered by the same check and don't have to be checked again (see also `MatchResult`). -/ inductive HeadCheck where -- match step that always succeeds: _, x, `($x), ... | unconditional -- match step based on kind and, optionally, arity of discriminant -- If `arity` is given, that number of new discriminants is introduced. `covered` patterns should then introduce the -- same number of new patterns. -- We actually check the arity at run time only in the case of `null` nodes since it should otherwise by implied by -- the node kind. -- without arity: `($x:k) -- with arity: any quotation without an antiquotation head pattern | shape (k : SyntaxNodeKind) (arity : Option Nat) -- Match step that succeeds on `null` nodes of arity at least `numPrefix + numSuffix`, introducing discriminants -- for the first `numPrefix` children, one `null` node for those in between, and for the `numSuffix` last children. -- example: `([$x, $xs,*, $y]) is `slice 2 2` | slice (numPrefix numSuffix : Nat) -- other, complicated match step that will probably only cover identical patterns -- example: antiquotation splices `($[...]*) | other (pat : Syntax) open HeadCheck /-- Describe whether a pattern is covered by a head check (induced by the pattern itself or a different pattern). -/ inductive MatchResult where -- Pattern agrees with head check, remove and transform remaining alternative. -- If `exhaustive` is `false`, *also* include unchanged alternative in the "no" branch. | covered (f : Alt → TermElabM Alt) (exhaustive : Bool) -- Pattern disagrees with head check, include in "no" branch only | uncovered -- Pattern is not quite sure yet; include unchanged in both branches | undecided open MatchResult /-- All necessary information on a pattern head. -/ structure HeadInfo where -- check induced by the pattern check : HeadCheck -- compute compatibility of pattern with given head check onMatch (taken : HeadCheck) : MatchResult -- actually run the specified head check, with the discriminant bound to `discr` doMatch (yes : (newDiscrs : List Syntax) → TermElabM Syntax) (no : TermElabM Syntax) : TermElabM Syntax /-- Adapt alternatives that do not introduce new discriminants in `doMatch`, but are covered by those that do so. -/ private def noOpMatchAdaptPats : HeadCheck → Alt → Alt | shape k (some sz), (pats, rhs) => (List.replicate sz (Unhygienic.run `(_)) ++ pats, rhs) | slice p s, (pats, rhs) => (List.replicate (p + 1 + s) (Unhygienic.run `(_)) ++ pats, rhs) | _, alt => alt private def adaptRhs (fn : Syntax → TermElabM Syntax) : Alt → TermElabM Alt | (pats, rhs) => do (pats, ← fn rhs) private partial def getHeadInfo (alt : Alt) : TermElabM HeadInfo := let pat := alt.fst.head! let unconditionally (rhsFn) := pure { check := unconditional, doMatch := fun yes no => yes [], onMatch := fun taken => covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (match taken with | unconditional => true | _ => false) } -- quotation pattern if isQuot pat then let quoted := getQuotContent pat if quoted.isAtom then -- We assume that atoms are uniquely determined by the node kind and never have to be checked unconditionally pure else if quoted.isTokenAntiquot then unconditionally (`(let $(quoted.getAntiquotTerm) := discr; $(·))) else if isAntiquot quoted && !isEscapedAntiquot quoted then -- quotation contains a single antiquotation let k := antiquotKind? quoted |>.get! let rhsFn := match getAntiquotTerm quoted with | `(_) => pure | `($id:ident) => fun stx => `(let $id := discr; $(stx)) | anti => fun _ => throwErrorAt anti "unsupported antiquotation kind in pattern" -- Antiquotation kinds like `$id:ident` influence the parser, but also need to be considered by -- `match` (but not by quotation terms). For example, `($id:ident) and `($e) are not -- distinguishable without checking the kind of the node to be captured. Note that some -- antiquotations like the latter one for terms do not correspond to any actual node kind -- (signified by `k == Name.anonymous`), so we would only check for `ident` here. -- -- if stx.isOfKind `ident then -- let id := stx; let e := stx; ... -- else -- let e := stx; ... if k == Name.anonymous then unconditionally rhsFn else pure { check := shape k none, onMatch := fun | other _ => undecided | taken@(shape k' sz) => if k' == k then covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (exhaustive := sz.isNone) else uncovered | _ => uncovered, doMatch := fun yes no => do `(cond (Syntax.isOfKind discr $(quote k)) $(← yes []) $(← no)), } else if isAntiquotSuffixSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if isAntiquotSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if quoted.getArgs.size == 1 && isAntiquotSuffixSplice quoted[0] then let anti := getAntiquotTerm (getAntiquotSuffixSpliceInner quoted[0]) unconditionally fun rhs => match antiquotSuffixSplice? quoted[0] with | `optional => `(let $anti := Syntax.getOptional? discr; $rhs) | `many => `(let $anti := Syntax.getArgs discr; $rhs) | `sepBy => `(let $anti := @SepArray.mk $(getSepFromSplice quoted[0]) (Syntax.getArgs discr); $rhs) | k => throwErrorAt quoted "invalid antiquotation suffix splice kind '{k}'" else if quoted.getArgs.size == 1 && isAntiquotSplice quoted[0] then pure { check := other pat, onMatch := fun | other pat' => if pat' == pat then covered pure (exhaustive := true) else undecided | _ => undecided, doMatch := fun yes no => do let splice := quoted[0] let k := antiquotSpliceKind? splice let contents := getAntiquotSpliceContents splice let ids ← getAntiquotationIds splice let yes ← yes [] let no ← no match k with | `optional => let nones := mkArray ids.size (← `(none)) `(let_delayed yes _ $ids* := $yes; if discr.isNone then yes () $[ $nones]* else match discr with | `($(mkNullNode contents)) => yes () $[ (some $ids)]* | _ => $no) | _ => let mut discrs ← `(Syntax.getArgs discr) if k == `sepBy then discrs ← `(Array.getSepElems $discrs) let tuple ← mkTuple ids let mut yes := yes let resId ← match ids with | #[id] => id | _ => for id in ids do yes ← `(let $id := tuples.map (fun $tuple => $id); $yes) `(tuples) let contents := if contents.size == 1 then contents[0] else mkNullNode contents `(match OptionM.run ($(discrs).sequenceMap fun | `($contents) => some $tuple | _ => none) with | some $resId => $yes | none => $no) } else if let some idx := quoted.getArgs.findIdx? (fun arg => isAntiquotSuffixSplice arg || isAntiquotSplice arg) then do /- pattern of the form `match discr, ... with | `(pat_0 ... pat_(idx-1) $[...]* pat_(idx+1) ...), ...` transform to ``` if discr.getNumArgs >= $quoted.getNumArgs - 1 then match discr[0], ..., discr[idx-1], mkNullNode (discr.getArgs.extract idx (discr.getNumArgs - $numSuffix))), ..., discr[quoted.getNumArgs - 1] with | `(pat_0), ... `(pat_(idx-1)), `($[...])*, `(pat_(idx+1)), ... ``` -/ let numSuffix := quoted.getNumArgs - 1 - idx pure { check := slice idx numSuffix onMatch := fun | other _ => undecided | slice p s => if p == idx && s == numSuffix then let argPats := quoted.getArgs.mapIdx fun i arg => let arg := if (i : Nat) == idx then mkNullNode #[arg] else arg Unhygienic.run `(`($(arg))) covered (fun (pats, rhs) => (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => uncovered doMatch := fun yes no => do let prefixDiscrs ← (List.range idx).mapM (`(Syntax.getArg discr $(quote ·))) let sliceDiscr ← `(mkNullNode (discr.getArgs.extract $(quote idx) (discr.getNumArgs - $(quote numSuffix)))) let suffixDiscrs ← (List.range numSuffix).mapM fun i => `(Syntax.getArg discr (discr.getNumArgs - $(quote (numSuffix - i)))) `(ite (GE.ge discr.getNumArgs $(quote (quoted.getNumArgs - 1))) $(← yes (prefixDiscrs ++ sliceDiscr :: suffixDiscrs)) $(← no)) } else -- not an antiquotation, or an escaped antiquotation: match head shape let quoted := unescapeAntiquot quoted let kind := quoted.getKind let argPats := quoted.getArgs.map fun arg => Unhygienic.run `(`($(arg))) pure { check := if quoted.isIdent then -- identifiers only match identical identifiers -- NOTE: We could make this case more precise by including the matched identifier, -- if any, in the `shape` constructor, but matching on literal identifiers is quite -- rare. other quoted else shape kind argPats.size, onMatch := fun | other stx' => if quoted.isIdent && quoted == stx' then covered pure (exhaustive := true) else uncovered | shape k' sz => if k' == kind && sz == argPats.size then covered (fun (pats, rhs) => (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => uncovered, doMatch := fun yes no => do let cond ← match kind with | `null => `(Syntax.matchesNull discr $(quote argPats.size)) | `ident => `(Syntax.matchesIdent discr $(quote quoted.getId)) | _ => `(Syntax.isOfKind discr $(quote kind)) let newDiscrs ← (List.range argPats.size).mapM fun i => `(Syntax.getArg discr $(quote i)) `(ite (Eq $cond true) $(← yes newDiscrs) $(← no)) } else match pat with | `(_) => unconditionally pure | `($id:ident) => unconditionally (`(let $id := discr; $(·))) | `($id:ident@$pat) => do let info ← getHeadInfo (pat::alt.1.tail!, alt.2) { info with onMatch := fun taken => match info.onMatch taken with | covered f exh => covered (fun alt => f alt >>= adaptRhs (`(let $id := discr; $(·)))) exh | r => r } | _ => throwErrorAt pat "match (syntax) : unexpected pattern kind {pat}" -- Bind right-hand side to new `let_delayed` decl in order to prevent code duplication private def deduplicate (floatedLetDecls : Array Syntax) : Alt → TermElabM (Array Syntax × Alt) -- NOTE: new macro scope so that introduced bindings do not collide | (pats, rhs) => do if let `($f:ident $[ $args:ident]*) := rhs then -- looks simple enough/created by this function, skip return (floatedLetDecls, (pats, rhs)) withFreshMacroScope do match (← getPatternsVars pats.toArray) with | #[] => -- no antiquotations => introduce Unit parameter to preserve evaluation order let rhs' ← `(rhs Unit.unit) (floatedLetDecls.push (← `(letDecl|rhs _ := $rhs)), (pats, rhs')) | vars => let rhs' ← `(rhs $vars*) (floatedLetDecls.push (← `(letDecl|rhs $vars:ident* := $rhs)), (pats, rhs')) private partial def compileStxMatch (discrs : List Syntax) (alts : List Alt) : TermElabM Syntax := do trace[Elab.match_syntax] "match {discrs} with {alts}" match discrs, alts with | [], ([], rhs)::_ => pure rhs -- nothing left to match | _, [] => logError "non-exhaustive 'match' (syntax)" pure Syntax.missing | discr::discrs, alt::alts => do let info ← getHeadInfo alt let pat := alt.1.head! let alts ← (alt::alts).mapM fun alt => do ((← getHeadInfo alt).onMatch info.check, alt) let mut yesAlts := #[] let mut undecidedAlts := #[] let mut nonExhaustiveAlts := #[] let mut floatedLetDecls := #[] for alt in alts do let mut alt := alt match alt with | (covered f exh, alt') => -- we can only factor out a common check if there are no undecided patterns in between; -- otherwise we would change the order of alternatives if undecidedAlts.isEmpty then yesAlts ← yesAlts.push <$> f (alt'.1.tail!, alt'.2) if !exh then nonExhaustiveAlts := nonExhaustiveAlts.push alt' else (floatedLetDecls, alt) ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt nonExhaustiveAlts := nonExhaustiveAlts.push alt | (undecided, alt') => (floatedLetDecls, alt) ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt nonExhaustiveAlts := nonExhaustiveAlts.push alt | (uncovered, alt') => nonExhaustiveAlts := nonExhaustiveAlts.push alt' let mut stx ← info.doMatch (yes := fun newDiscrs => do let mut yesAlts := yesAlts if !undecidedAlts.isEmpty then -- group undecided alternatives in a new default case `| discr2, ... => match discr, discr2, ... with ...` let vars ← discrs.mapM fun _ => withFreshMacroScope `(discr) let pats := List.replicate newDiscrs.length (Unhygienic.run `(_)) ++ vars let alts ← undecidedAlts.mapM fun alt => `(matchAltExpr| | $(alt.1.toArray),* => $(alt.2)) let rhs ← `(match discr, $[$(vars.toArray):term],* with $alts:matchAlt*) yesAlts := yesAlts.push (pats, rhs) withFreshMacroScope $ compileStxMatch (newDiscrs ++ discrs) yesAlts.toList) (no := withFreshMacroScope $ compileStxMatch (discr::discrs) nonExhaustiveAlts.toList) for d in floatedLetDecls do stx ← `(let_delayed $d:letDecl; $stx) `(let discr := $discr; $stx) | _, _ => unreachable! def match_syntax.expand (stx : Syntax) : TermElabM Syntax := do match stx with | `(match $[$discrs:term],* with $[| $[$patss],* => $rhss]*) => do if !patss.any (·.any (fun | `($id@$pat) => pat.isQuot | pat => pat.isQuot)) then -- no quotations => fall back to regular `match` throwUnsupportedSyntax let stx ← compileStxMatch discrs.toList (patss.map (·.toList) |>.zip rhss).toList trace[Elab.match_syntax.result] "{stx}" stx | _ => throwUnsupportedSyntax @[builtinTermElab «match»] def elabMatchSyntax : TermElab := adaptExpander match_syntax.expand builtin_initialize registerTraceClass `Elab.match_syntax registerTraceClass `Elab.match_syntax.result end Lean.Elab.Term.Quotation
56cb56ae5c7d6a2799a813ca42f316031b8d8cdb
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/special_functions/exp.lean
7ab72f07801bc417fd3141f388add2b386761d64
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
11,534
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne -/ import analysis.complex.basic import data.complex.exponential /-! # Complex and real exponential In this file we prove continuity of `complex.exp` and `real.exp`. We also prove a few facts about limits of `real.exp` at infinity. ## Tags exp -/ noncomputable theory open finset filter metric asymptotics set function open_locale classical topological_space namespace complex variables {z y x : ℝ} lemma exp_bound_sq (x z : ℂ) (hz : ∥z∥ ≤ 1) : ∥exp (x + z) - exp x - z • exp x∥ ≤ ∥exp x∥ * ∥z∥ ^ 2 := calc ∥exp (x + z) - exp x - z * exp x∥ = ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring } ... = ∥exp x∥ * ∥exp z - 1 - z∥ : norm_mul _ _ ... ≤ ∥exp x∥ * ∥z∥^2 : mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le hz) (norm_nonneg _) lemma locally_lipschitz_exp {r : ℝ} (hr_nonneg : 0 ≤ r) (hr_le : r ≤ 1) (x y : ℂ) (hyx : ∥y - x∥ < r) : ∥exp y - exp x∥ ≤ (1 + r) * ∥exp x∥ * ∥y - x∥ := begin have hy_eq : y = x + (y - x), by abel, have hyx_sq_le : ∥y - x∥ ^ 2 ≤ r * ∥y - x∥, { rw pow_two, exact mul_le_mul hyx.le le_rfl (norm_nonneg _) hr_nonneg, }, have h_sq : ∀ z, ∥z∥ ≤ 1 → ∥exp (x + z) - exp x∥ ≤ ∥z∥ * ∥exp x∥ + ∥exp x∥ * ∥z∥ ^ 2, { intros z hz, have : ∥exp (x + z) - exp x - z • exp x∥ ≤ ∥exp x∥ * ∥z∥ ^ 2, from exp_bound_sq x z hz, rw [← sub_le_iff_le_add', ← norm_smul z], exact (norm_sub_norm_le _ _).trans this, }, calc ∥exp y - exp x∥ = ∥exp (x + (y - x)) - exp x∥ : by nth_rewrite 0 hy_eq ... ≤ ∥y - x∥ * ∥exp x∥ + ∥exp x∥ * ∥y - x∥ ^ 2 : h_sq (y - x) (hyx.le.trans hr_le) ... ≤ ∥y - x∥ * ∥exp x∥ + ∥exp x∥ * (r * ∥y - x∥) : add_le_add_left (mul_le_mul le_rfl hyx_sq_le (sq_nonneg _) (norm_nonneg _)) _ ... = (1 + r) * ∥exp x∥ * ∥y - x∥ : by ring, end @[continuity] lemma continuous_exp : continuous exp := continuous_iff_continuous_at.mpr $ λ x, continuous_at_of_locally_lipschitz zero_lt_one (2 * ∥exp x∥) (locally_lipschitz_exp zero_le_one le_rfl x) lemma continuous_on_exp {s : set ℂ} : continuous_on exp s := continuous_exp.continuous_on end complex section complex_continuous_exp_comp variable {α : Type*} open complex lemma filter.tendsto.cexp {l : filter α} {f : α → ℂ} {z : ℂ} (hf : tendsto f l (𝓝 z)) : tendsto (λ x, exp (f x)) l (𝓝 (exp z)) := (continuous_exp.tendsto _).comp hf variables [topological_space α] {f : α → ℂ} {s : set α} {x : α} lemma continuous_within_at.cexp (h : continuous_within_at f s x) : continuous_within_at (λ y, exp (f y)) s x := h.cexp lemma continuous_at.cexp (h : continuous_at f x) : continuous_at (λ y, exp (f y)) x := h.cexp lemma continuous_on.cexp (h : continuous_on f s) : continuous_on (λ y, exp (f y)) s := λ x hx, (h x hx).cexp lemma continuous.cexp (h : continuous f) : continuous (λ y, exp (f y)) := continuous_iff_continuous_at.2 $ λ x, h.continuous_at.cexp end complex_continuous_exp_comp namespace real @[continuity] lemma continuous_exp : continuous exp := complex.continuous_re.comp complex.continuous_of_real.cexp lemma continuous_on_exp {s : set ℝ} : continuous_on exp s := continuous_exp.continuous_on end real section real_continuous_exp_comp variable {α : Type*} open real lemma filter.tendsto.exp {l : filter α} {f : α → ℝ} {z : ℝ} (hf : tendsto f l (𝓝 z)) : tendsto (λ x, exp (f x)) l (𝓝 (exp z)) := (continuous_exp.tendsto _).comp hf variables [topological_space α] {f : α → ℝ} {s : set α} {x : α} lemma continuous_within_at.exp (h : continuous_within_at f s x) : continuous_within_at (λ y, exp (f y)) s x := h.exp lemma continuous_at.exp (h : continuous_at f x) : continuous_at (λ y, exp (f y)) x := h.exp lemma continuous_on.exp (h : continuous_on f s) : continuous_on (λ y, exp (f y)) s := λ x hx, (h x hx).exp lemma continuous.exp (h : continuous f) : continuous (λ y, exp (f y)) := continuous_iff_continuous_at.2 $ λ x, h.continuous_at.exp end real_continuous_exp_comp namespace real variables {x y z : ℝ} /-- The real exponential function tends to `+∞` at `+∞`. -/ lemma tendsto_exp_at_top : tendsto exp at_top at_top := begin have A : tendsto (λx:ℝ, x + 1) at_top at_top := tendsto_at_top_add_const_right at_top 1 tendsto_id, have B : ∀ᶠ x in at_top, x + 1 ≤ exp x := eventually_at_top.2 ⟨0, λx hx, add_one_le_exp x⟩, exact tendsto_at_top_mono' at_top B A end /-- The real exponential function tends to `0` at `-∞` or, equivalently, `exp(-x)` tends to `0` at `+∞` -/ lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp tendsto_exp_at_top).congr (λx, (exp_neg x).symm) /-- The real exponential function tends to `1` at `0`. -/ lemma tendsto_exp_nhds_0_nhds_1 : tendsto exp (𝓝 0) (𝓝 1) := by { convert continuous_exp.tendsto 0, simp } lemma tendsto_exp_at_bot : tendsto exp at_bot (𝓝 0) := (tendsto_exp_neg_at_top_nhds_0.comp tendsto_neg_at_bot_at_top).congr $ λ x, congr_arg exp $ neg_neg x lemma tendsto_exp_at_bot_nhds_within : tendsto exp at_bot (𝓝[>] 0) := tendsto_inf.2 ⟨tendsto_exp_at_bot, tendsto_principal.2 $ eventually_of_forall exp_pos⟩ /-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/ lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top := begin refine (at_top_basis_Ioi.tendsto_iff (at_top_basis' 1)).2 (λ C hC₁, _), have hC₀ : 0 < C, from zero_lt_one.trans_le hC₁, have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀), obtain ⟨N, hN⟩ : ∃ N, ∀ k ≥ N, (↑k ^ n : ℝ) / exp 1 ^ k < (exp 1 * C)⁻¹ := eventually_at_top.1 ((tendsto_pow_const_div_const_pow_of_one_lt n (one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)), simp only [← exp_nat_mul, mul_one, div_lt_iff, exp_pos, ← div_eq_inv_mul] at hN, refine ⟨N, trivial, λ x hx, _⟩, rw set.mem_Ioi at hx, have hx₀ : 0 < x, from N.cast_nonneg.trans_lt hx, rw [set.mem_Ici, le_div_iff (pow_pos hx₀ _), ← le_div_iff' hC₀], calc x ^ n ≤ ⌈x⌉₊ ^ n : pow_le_pow_of_le_left hx₀.le (nat.le_ceil _) _ ... ≤ exp ⌈x⌉₊ / (exp 1 * C) : (hN _ (nat.lt_ceil.2 hx).le).le ... ≤ exp (x + 1) / (exp 1 * C) : div_le_div_of_le (mul_pos (exp_pos _) hC₀).le (exp_le_exp.2 $ (nat.ceil_lt_add_one hx₀.le).le) ... = exp x / C : by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne'] end /-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/ lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx, by rw [comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg] /-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any natural number `n` and any real numbers `b` and `c` such that `b` is positive. -/ lemma tendsto_mul_exp_add_div_pow_at_top (b c : ℝ) (n : ℕ) (hb : 0 < b) : tendsto (λ x, (b * exp x + c) / x ^ n) at_top at_top := begin rcases n.eq_zero_or_pos with rfl | hn, { simp only [pow_zero, div_one], exact (tendsto_exp_at_top.const_mul_at_top hb).at_top_add tendsto_const_nhds }, refine tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) _) (((tendsto_exp_div_pow_at_top n).const_mul_at_top hb).at_top_add ((tendsto_pow_neg_at_top hn).mul (@tendsto_const_nhds _ _ _ c _))), intros x hx, simp only [zpow_neg₀ x n], ring, end /-- The function `(x ^ n) / (b * exp x + c)` tends to `0` at `+∞`, for any natural number `n` and any real numbers `b` and `c` such that `b` is nonzero. -/ lemma tendsto_div_pow_mul_exp_add_at_top (b c : ℝ) (n : ℕ) (hb : 0 ≠ b) : tendsto (λ x, x ^ n / (b * exp x + c)) at_top (𝓝 0) := begin have H : ∀ d e, 0 < d → tendsto (λ (x:ℝ), x^n / (d * (exp x) + e)) at_top (𝓝 0), { intros b' c' h, convert (tendsto_mul_exp_add_div_pow_at_top b' c' n h).inv_tendsto_at_top , ext x, simpa only [pi.inv_apply] using (inv_div _ _).symm }, cases lt_or_gt_of_ne hb, { exact H b c h }, { convert (H (-b) (-c) (neg_pos.mpr h)).neg, { ext x, field_simp, rw [← neg_add (b * exp x) c, neg_div_neg_eq] }, { exact neg_zero.symm } }, end /-- `real.exp` as an order isomorphism between `ℝ` and `(0, +∞)`. -/ def exp_order_iso : ℝ ≃o Ioi (0 : ℝ) := strict_mono.order_iso_of_surjective _ (exp_strict_mono.cod_restrict exp_pos) $ (continuous_subtype_mk _ continuous_exp).surjective (by simp only [tendsto_Ioi_at_top, subtype.coe_mk, tendsto_exp_at_top]) (by simp [tendsto_exp_at_bot_nhds_within]) @[simp] lemma coe_exp_order_iso_apply (x : ℝ) : (exp_order_iso x : ℝ) = exp x := rfl @[simp] lemma coe_comp_exp_order_iso : coe ∘ exp_order_iso = exp := rfl @[simp] lemma range_exp : range exp = Ioi 0 := by rw [← coe_comp_exp_order_iso, range_comp, exp_order_iso.range_eq, image_univ, subtype.range_coe] @[simp] lemma map_exp_at_top : map exp at_top = at_top := by rw [← coe_comp_exp_order_iso, ← filter.map_map, order_iso.map_at_top, map_coe_Ioi_at_top] @[simp] lemma comap_exp_at_top : comap exp at_top = at_top := by rw [← map_exp_at_top, comap_map exp_injective, map_exp_at_top] @[simp] lemma tendsto_exp_comp_at_top {α : Type*} {l : filter α} {f : α → ℝ} : tendsto (λ x, exp (f x)) l at_top ↔ tendsto f l at_top := by rw [← tendsto_comap_iff, comap_exp_at_top] lemma tendsto_comp_exp_at_top {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_top l ↔ tendsto f at_top l := by rw [← tendsto_map'_iff, map_exp_at_top] @[simp] lemma map_exp_at_bot : map exp at_bot = 𝓝[>] 0 := by rw [← coe_comp_exp_order_iso, ← filter.map_map, exp_order_iso.map_at_bot, ← map_coe_Ioi_at_bot] lemma comap_exp_nhds_within_Ioi_zero : comap exp (𝓝[>] 0) = at_bot := by rw [← map_exp_at_bot, comap_map exp_injective] lemma tendsto_comp_exp_at_bot {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_bot l ↔ tendsto f (𝓝[>] 0) l := by rw [← map_exp_at_bot, tendsto_map'_iff] lemma is_o_pow_exp_at_top {n : ℕ} : is_o (λ x, x^n) real.exp at_top := by simpa [is_o_iff_tendsto (λ x hx, ((exp_pos x).ne' hx).elim)] using tendsto_div_pow_mul_exp_add_at_top 1 0 n zero_ne_one end real namespace complex /-- `complex.abs (complex.exp z) → ∞` as `complex.re z → ∞`. TODO: use `bornology.cobounded`. -/ lemma tendsto_exp_comap_re_at_top : tendsto exp (comap re at_top) (comap abs at_top) := by simpa only [tendsto_comap_iff, (∘), abs_exp] using real.tendsto_exp_at_top.comp tendsto_comap /-- `complex.exp z → 0` as `complex.re z → -∞`.-/ lemma tendsto_exp_comap_re_at_bot : tendsto exp (comap re at_bot) (𝓝 0) := tendsto_zero_iff_norm_tendsto_zero.2 $ by simpa only [norm_eq_abs, abs_exp] using real.tendsto_exp_at_bot.comp tendsto_comap lemma tendsto_exp_comap_re_at_bot_nhds_within : tendsto exp (comap re at_bot) (𝓝[≠] 0) := tendsto_inf.2 ⟨tendsto_exp_comap_re_at_bot, tendsto_principal.2 $ eventually_of_forall $ exp_ne_zero⟩ end complex
cdd2dceec7b8b1be8516bca7f6c97e12c1b032ff
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/combinatorics/simple_graph/metric.lean
53bef525762481b384c28a09cbe536dba3249a56
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,653
lean
/- Copyright (c) 2022 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller, Vincent Beffara -/ import combinatorics.simple_graph.connectivity import data.nat.lattice /-! # Graph metric > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This module defines the `simple_graph.dist` function, which takes pairs of vertices to the length of the shortest walk between them. ## Main definitions - `simple_graph.dist` is the graph metric. ## Todo - Provide an additional computable version of `simple_graph.dist` for when `G` is connected. - Evaluate `nat` vs `enat` for the codomain of `dist`, or potentially having an additional `edist` when the objects under consideration are disconnected graphs. - When directed graphs exist, a directed notion of distance, likely `enat`-valued. ## Tags graph metric, distance -/ namespace simple_graph variables {V : Type*} (G : simple_graph V) /-! ## Metric -/ /-- The distance between two vertices is the length of the shortest walk between them. If no such walk exists, this uses the junk value of `0`. -/ noncomputable def dist (u v : V) : ℕ := Inf (set.range (walk.length : G.walk u v → ℕ)) variables {G} protected lemma reachable.exists_walk_of_dist {u v : V} (hr : G.reachable u v) : ∃ (p : G.walk u v), p.length = G.dist u v := nat.Inf_mem (set.range_nonempty_iff_nonempty.mpr hr) protected lemma connected.exists_walk_of_dist (hconn : G.connected) (u v : V) : ∃ (p : G.walk u v), p.length = G.dist u v := (hconn u v).exists_walk_of_dist lemma dist_le {u v : V} (p : G.walk u v) : G.dist u v ≤ p.length := nat.Inf_le ⟨p, rfl⟩ @[simp] lemma dist_eq_zero_iff_eq_or_not_reachable {u v : V} : G.dist u v = 0 ↔ u = v ∨ ¬ G.reachable u v := by simp [dist, nat.Inf_eq_zero, reachable] lemma dist_self {v : V} : dist G v v = 0 := by simp protected lemma reachable.dist_eq_zero_iff {u v : V} (hr : G.reachable u v) : G.dist u v = 0 ↔ u = v := by simp [hr] protected lemma reachable.pos_dist_of_ne {u v : V} (h : G.reachable u v) (hne : u ≠ v) : 0 < G.dist u v := nat.pos_of_ne_zero (by simp [h, hne]) protected lemma connected.dist_eq_zero_iff (hconn : G.connected) {u v : V} : G.dist u v = 0 ↔ u = v := by simp [hconn u v] protected lemma connected.pos_dist_of_ne {u v : V} (hconn : G.connected) (hne : u ≠ v) : 0 < G.dist u v := nat.pos_of_ne_zero (by simp [hconn.dist_eq_zero_iff, hne]) lemma dist_eq_zero_of_not_reachable {u v : V} (h : ¬ G.reachable u v) : G.dist u v = 0 := by simp [h] lemma nonempty_of_pos_dist {u v : V} (h : 0 < G.dist u v) : (set.univ : set (G.walk u v)).nonempty := by simpa [set.range_nonempty_iff_nonempty, set.nonempty_iff_univ_nonempty] using nat.nonempty_of_pos_Inf h protected lemma connected.dist_triangle (hconn : G.connected) {u v w : V} : G.dist u w ≤ G.dist u v + G.dist v w := begin obtain ⟨p, hp⟩ := hconn.exists_walk_of_dist u v, obtain ⟨q, hq⟩ := hconn.exists_walk_of_dist v w, rw [← hp, ← hq, ← walk.length_append], apply dist_le, end private lemma dist_comm_aux {u v : V} (h : G.reachable u v) : G.dist u v ≤ G.dist v u := begin obtain ⟨p, hp⟩ := h.symm.exists_walk_of_dist, rw [← hp, ← walk.length_reverse], apply dist_le, end lemma dist_comm {u v : V} : G.dist u v = G.dist v u := begin by_cases h : G.reachable u v, { apply le_antisymm (dist_comm_aux h) (dist_comm_aux h.symm), }, { have h' : ¬ G.reachable v u := λ h', absurd h'.symm h, simp [h, h', dist_eq_zero_of_not_reachable], }, end end simple_graph
14cd1cf8c0a7b67acb9eb0f047fe9a283573340d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/field_theory/intermediate_field.lean
a7bb7bfd6fbd16f6ff791468ceb8cff7eed42e0b
[ "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,916
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.minpoly import field_theory.subfield import field_theory.tower /-! # Intermediate fields Let `L / K` be a field extension, given as an instance `algebra K L`. This file defines the type of fields in between `K` and `L`, `intermediate_field K L`. An `intermediate_field K L` is a subfield of `L` which contains (the image of) `K`, i.e. it is a `subfield L` and a `subalgebra K L`. ## Main definitions * `intermediate_field K L` : the type of intermediate fields between `K` and `L`. * `subalgebra.to_intermediate_field`: turns a subalgebra closed under `⁻¹` into an intermediate field * `subfield.to_intermediate_field`: turns a subfield containing the image of `K` into an intermediate field * `intermediate_field.map`: map an intermediate field along an `alg_hom` * `intermediate_field.restrict_scalars`: restrict the scalars of an intermediate field to a smaller field in a tower of fields. ## Implementation notes Intermediate fields are defined with a structure extending `subfield` and `subalgebra`. A `subalgebra` is closed under all operations except `⁻¹`, ## Tags intermediate field, field extension -/ open finite_dimensional polynomial open_locale big_operators polynomial variables (K L L' : Type*) [field K] [field L] [field L'] [algebra K L] [algebra K L'] /-- `S : intermediate_field K L` is a subset of `L` such that there is a field tower `L / S / K`. -/ structure intermediate_field extends subalgebra K L := (neg_mem' : ∀ x ∈ carrier, -x ∈ carrier) (inv_mem' : ∀ x ∈ carrier, x⁻¹ ∈ carrier) /-- Reinterpret an `intermediate_field` as a `subalgebra`. -/ add_decl_doc intermediate_field.to_subalgebra variables {K L L'} (S : intermediate_field K L) namespace intermediate_field /-- Reinterpret an `intermediate_field` as a `subfield`. -/ def to_subfield : subfield L := { ..S.to_subalgebra, ..S } instance : set_like (intermediate_field K L) L := ⟨λ S, S.to_subalgebra.carrier, by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨h⟩, congr, }⟩ instance : subfield_class (intermediate_field K L) L := { add_mem := λ s _ _, s.add_mem', zero_mem := λ s, s.zero_mem', neg_mem := neg_mem', mul_mem := λ s _ _, s.mul_mem', one_mem := λ s, s.one_mem', inv_mem := inv_mem' } @[simp] lemma mem_carrier {s : intermediate_field K L} {x : L} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two intermediate fields are equal if they have the same elements. -/ @[ext] theorem ext {S T : intermediate_field K L} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h @[simp] lemma coe_to_subalgebra : (S.to_subalgebra : set L) = S := rfl @[simp] lemma coe_to_subfield : (S.to_subfield : set L) = S := rfl @[simp] lemma mem_mk (s : set L) (hK : ∀ x, algebra_map K L x ∈ s) (ho hm hz ha hn hi) (x : L) : x ∈ intermediate_field.mk (subalgebra.mk s ho hm hz ha hK) hn hi ↔ x ∈ s := iff.rfl @[simp] lemma mem_to_subalgebra (s : intermediate_field K L) (x : L) : x ∈ s.to_subalgebra ↔ x ∈ s := iff.rfl @[simp] lemma mem_to_subfield (s : intermediate_field K L) (x : L) : x ∈ s.to_subfield ↔ x ∈ s := iff.rfl /-- Copy of an intermediate field with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : intermediate_field K L) (s : set L) (hs : s = ↑S) : intermediate_field K L := { to_subalgebra := S.to_subalgebra.copy s (hs : s = (S.to_subalgebra).carrier), neg_mem' := have hs' : (S.to_subalgebra.copy s hs).carrier = (S.to_subalgebra).carrier := hs, hs'.symm ▸ S.neg_mem', inv_mem' := have hs' : (S.to_subalgebra.copy s hs).carrier = (S.to_subalgebra).carrier := hs, hs'.symm ▸ S.inv_mem' } @[simp] lemma coe_copy (S : intermediate_field K L) (s : set L) (hs : s = ↑S) : (S.copy s hs : set L) = s := rfl lemma copy_eq (S : intermediate_field K L) (s : set L) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs section inherited_lemmas /-! ### Lemmas inherited from more general structures The declarations in this section derive from the fact that an `intermediate_field` is also a subalgebra or subfield. Their use should be replaceable with the corresponding lemma from a subobject class. -/ /-- An intermediate field contains the image of the smaller field. -/ theorem algebra_map_mem (x : K) : algebra_map K L x ∈ S := S.algebra_map_mem' x /-- An intermediate field is closed under scalar multiplication. -/ theorem smul_mem {y : L} : y ∈ S → ∀ {x : K}, x • y ∈ S := S.to_subalgebra.smul_mem /-- An intermediate field contains the ring's 1. -/ protected theorem one_mem : (1 : L) ∈ S := one_mem S /-- An intermediate field contains the ring's 0. -/ protected theorem zero_mem : (0 : L) ∈ S := zero_mem S /-- An intermediate field is closed under multiplication. -/ protected theorem mul_mem {x y : L} : x ∈ S → y ∈ S → x * y ∈ S := mul_mem /-- An intermediate field is closed under addition. -/ protected theorem add_mem {x y : L} : x ∈ S → y ∈ S → x + y ∈ S := add_mem /-- An intermediate field is closed under subtraction -/ protected theorem sub_mem {x y : L} : x ∈ S → y ∈ S → x - y ∈ S := sub_mem /-- An intermediate field is closed under negation. -/ protected theorem neg_mem {x : L} : x ∈ S → -x ∈ S := neg_mem /-- An intermediate field is closed under inverses. -/ protected theorem inv_mem {x : L} : x ∈ S → x⁻¹ ∈ S := inv_mem /-- An intermediate field is closed under division. -/ protected theorem div_mem {x y : L} : x ∈ S → y ∈ S → x / y ∈ S := div_mem /-- Product of a list of elements in an intermediate_field is in the intermediate_field. -/ protected lemma list_prod_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.prod ∈ S := list_prod_mem /-- Sum of a list of elements in an intermediate field is in the intermediate_field. -/ protected lemma list_sum_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.sum ∈ S := list_sum_mem /-- Product of a multiset of elements in an intermediate field is in the intermediate_field. -/ protected lemma multiset_prod_mem (m : multiset L) : (∀ a ∈ m, a ∈ S) → m.prod ∈ S := multiset_prod_mem m /-- Sum of a multiset of elements in a `intermediate_field` is in the `intermediate_field`. -/ protected lemma multiset_sum_mem (m : multiset L) : (∀ a ∈ m, a ∈ S) → m.sum ∈ S := multiset_sum_mem m /-- Product of elements of an intermediate field indexed by a `finset` is in the intermediate_field. -/ protected lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) : ∏ i in t, f i ∈ S := prod_mem h /-- Sum of elements in a `intermediate_field` indexed by a `finset` is in the `intermediate_field`. -/ protected lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) : ∑ i in t, f i ∈ S := sum_mem h protected lemma pow_mem {x : L} (hx : x ∈ S) (n : ℤ) : x^n ∈ S := zpow_mem hx n protected lemma zsmul_mem {x : L} (hx : x ∈ S) (n : ℤ) : n • x ∈ S := zsmul_mem hx n protected lemma coe_int_mem (n : ℤ) : (n : L) ∈ S := coe_int_mem S n protected lemma coe_add (x y : S) : (↑(x + y) : L) = ↑x + ↑y := rfl protected lemma coe_neg (x : S) : (↑(-x) : L) = -↑x := rfl protected lemma coe_mul (x y : S) : (↑(x * y) : L) = ↑x * ↑y := rfl protected lemma coe_inv (x : S) : (↑(x⁻¹) : L) = (↑x)⁻¹ := rfl protected lemma coe_zero : ((0 : S) : L) = 0 := rfl protected lemma coe_one : ((1 : S) : L) = 1 := rfl protected lemma coe_pow (x : S) (n : ℕ) : (↑(x ^ n) : L) = ↑x ^ n := submonoid_class.coe_pow x n end inherited_lemmas lemma coe_nat_mem (n : ℕ) : (n : L) ∈ S := by simpa using coe_int_mem S n end intermediate_field /-- Turn a subalgebra closed under inverses into an intermediate field -/ def subalgebra.to_intermediate_field (S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) : intermediate_field K L := { neg_mem' := λ x, S.neg_mem, inv_mem' := inv_mem, .. S } @[simp] lemma to_subalgebra_to_intermediate_field (S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) : (S.to_intermediate_field inv_mem).to_subalgebra = S := by { ext, refl } @[simp] lemma to_intermediate_field_to_subalgebra (S : intermediate_field K L) : S.to_subalgebra.to_intermediate_field (λ x, S.inv_mem) = S := by { ext, refl } /-- Turn a subalgebra satisfying `is_field` into an intermediate_field -/ def subalgebra.to_intermediate_field' (S : subalgebra K L) (hS : is_field S) : intermediate_field K L := S.to_intermediate_field $ λ x hx, begin by_cases hx0 : x = 0, { rw [hx0, inv_zero], exact S.zero_mem }, letI hS' := hS.to_field, obtain ⟨y, hy⟩ := hS.mul_inv_cancel (show (⟨x, hx⟩ : S) ≠ 0, from subtype.ne_of_val_ne hx0), rw [subtype.ext_iff, S.coe_mul, S.coe_one, subtype.coe_mk, mul_eq_one_iff_inv_eq₀ hx0] at hy, exact hy.symm ▸ y.2, end @[simp] lemma to_subalgebra_to_intermediate_field' (S : subalgebra K L) (hS : is_field S) : (S.to_intermediate_field' hS).to_subalgebra = S := by { ext, refl } @[simp] lemma to_intermediate_field'_to_subalgebra (S : intermediate_field K L) : S.to_subalgebra.to_intermediate_field' (field.to_is_field S) = S := by { ext, refl } /-- Turn a subfield of `L` containing the image of `K` into an intermediate field -/ def subfield.to_intermediate_field (S : subfield L) (algebra_map_mem : ∀ x, algebra_map K L x ∈ S) : intermediate_field K L := { algebra_map_mem' := algebra_map_mem, .. S } namespace intermediate_field /-- An intermediate field inherits a field structure -/ instance to_field : field S := S.to_subfield.to_field @[simp, norm_cast] lemma coe_sum {ι : Type*} [fintype ι] (f : ι → S) : (↑∑ i, f i : L) = ∑ i, (f i : L) := begin classical, induction finset.univ using finset.induction_on with i s hi H, { simp }, { rw [finset.sum_insert hi, add_mem_class.coe_add, H, finset.sum_insert hi] } end @[simp, norm_cast] lemma coe_prod {ι : Type*} [fintype ι] (f : ι → S) : (↑∏ i, f i : L) = ∏ i, (f i : L) := begin classical, induction finset.univ using finset.induction_on with i s hi H, { simp }, { rw [finset.prod_insert hi, mul_mem_class.coe_mul, H, finset.prod_insert hi] } end /-! `intermediate_field`s inherit structure from their `subalgebra` coercions. -/ instance module' {R} [semiring R] [has_smul R K] [module R L] [is_scalar_tower R K L] : module R S := S.to_subalgebra.module' instance module : module K S := S.to_subalgebra.module instance is_scalar_tower {R} [semiring R] [has_smul R K] [module R L] [is_scalar_tower R K L] : is_scalar_tower R K S := S.to_subalgebra.is_scalar_tower @[simp] lemma coe_smul {R} [semiring R] [has_smul R K] [module R L] [is_scalar_tower R K L] (r : R) (x : S) : ↑(r • x) = (r • x : L) := rfl instance algebra' {K'} [comm_semiring K'] [has_smul K' K] [algebra K' L] [is_scalar_tower K' K L] : algebra K' S := S.to_subalgebra.algebra' instance algebra : algebra K S := S.to_subalgebra.algebra instance to_algebra {R : Type*} [semiring R] [algebra L R] : algebra S R := S.to_subalgebra.to_algebra instance is_scalar_tower_bot {R : Type*} [semiring R] [algebra L R] : is_scalar_tower S L R := is_scalar_tower.subalgebra _ _ _ S.to_subalgebra instance is_scalar_tower_mid {R : Type*} [semiring R] [algebra L R] [algebra K R] [is_scalar_tower K L R] : is_scalar_tower K S R := is_scalar_tower.subalgebra' _ _ _ S.to_subalgebra /-- Specialize `is_scalar_tower_mid` to the common case where the top field is `L` -/ instance is_scalar_tower_mid' : is_scalar_tower K S L := S.is_scalar_tower_mid /-- If `f : L →+* L'` fixes `K`, `S.map f` is the intermediate field between `L'` and `K` such that `x ∈ S ↔ f x ∈ S.map f`. -/ def map (f : L →ₐ[K] L') (S : intermediate_field K L) : intermediate_field K L' := { inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, S.inv_mem hx, map_inv₀ f x⟩ }, neg_mem' := λ x hx, (S.to_subalgebra.map f).neg_mem hx, .. S.to_subalgebra.map f} @[simp] lemma coe_map (f : L →ₐ[K] L') : (S.map f : set L') = f '' S := rfl lemma map_map {K L₁ L₂ L₃ : Type*} [field K] [field L₁] [algebra K L₁] [field L₂] [algebra K L₂] [field L₃] [algebra K L₃] (E : intermediate_field K L₁) (f : L₁ →ₐ[K] L₂) (g : L₂ →ₐ[K] L₃) : (E.map f).map g = E.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ /-- Given an equivalence `e : L ≃ₐ[K] L'` of `K`-field extensions and an intermediate field `E` of `L/K`, `intermediate_field_equiv_map e E` is the induced equivalence between `E` and `E.map e` -/ def intermediate_field_map (e : L ≃ₐ[K] L') (E : intermediate_field K L) : E ≃ₐ[K] (E.map e.to_alg_hom) := e.subalgebra_map E.to_subalgebra /- We manually add these two simp lemmas because `@[simps]` before `intermediate_field_map` led to a timeout. -/ @[simp] lemma intermediate_field_map_apply_coe (e : L ≃ₐ[K] L') (E : intermediate_field K L) (a : E) : ↑(intermediate_field_map e E a) = e a := rfl @[simp] lemma intermediate_field_map_symm_apply_coe (e : L ≃ₐ[K] L') (E : intermediate_field K L) (a : E.map e.to_alg_hom) : ↑((intermediate_field_map e E).symm a) = e.symm a := rfl end intermediate_field namespace alg_hom variables (f : L →ₐ[K] L') /-- The range of an algebra homomorphism, as an intermediate field. -/ @[simps to_subalgebra] def field_range : intermediate_field K L' := { .. f.range, .. (f : L →+* L').field_range } @[simp] lemma coe_field_range : ↑f.field_range = set.range f := rfl @[simp] lemma field_range_to_subfield : f.field_range.to_subfield = (f : L →+* L').field_range := rfl variables {f} @[simp] lemma mem_field_range {y : L'} : y ∈ f.field_range ↔ ∃ x, f x = y := iff.rfl end alg_hom namespace intermediate_field /-- The embedding from an intermediate field of `L / K` to `L`. -/ def val : S →ₐ[K] L := S.to_subalgebra.val @[simp] theorem coe_val : ⇑S.val = coe := rfl @[simp] lemma val_mk {x : L} (hx : x ∈ S) : S.val ⟨x, hx⟩ = x := rfl lemma range_val : S.val.range = S.to_subalgebra := S.to_subalgebra.range_val lemma aeval_coe {R : Type*} [comm_ring R] [algebra R K] [algebra R L] [is_scalar_tower R K L] (x : S) (P : R[X]) : aeval (x : L) P = aeval x P := begin refine polynomial.induction_on' P (λ f g hf hg, _) (λ n r, _), { rw [aeval_add, aeval_add, add_mem_class.coe_add, hf, hg] }, { simp only [mul_mem_class.coe_mul, aeval_monomial, submonoid_class.coe_pow, mul_eq_mul_right_iff], left, refl } end lemma coe_is_integral_iff {R : Type*} [comm_ring R] [algebra R K] [algebra R L] [is_scalar_tower R K L] {x : S} : is_integral R (x : L) ↔ _root_.is_integral R x := begin refine ⟨λ h, _, λ h, _⟩, { obtain ⟨P, hPmo, hProot⟩ := h, refine ⟨P, hPmo, (injective_iff_map_eq_zero _).1 (algebra_map ↥S L).injective _ _⟩, letI : is_scalar_tower R S L := is_scalar_tower.of_algebra_map_eq (congr_fun rfl), rwa [eval₂_eq_eval_map, ← eval₂_at_apply, eval₂_eq_eval_map, polynomial.map_map, ← is_scalar_tower.algebra_map_eq, ← eval₂_eq_eval_map] }, { obtain ⟨P, hPmo, hProot⟩ := h, refine ⟨P, hPmo, _⟩, rw [← aeval_def, aeval_coe, aeval_def, hProot, zero_mem_class.coe_zero] }, end /-- The map `E → F` when `E` is an intermediate field contained in the intermediate field `F`. This is the intermediate field version of `subalgebra.inclusion`. -/ def inclusion {E F : intermediate_field K L} (hEF : E ≤ F) : E →ₐ[K] F := subalgebra.inclusion hEF lemma inclusion_injective {E F : intermediate_field K L} (hEF : E ≤ F) : function.injective (inclusion hEF) := subalgebra.inclusion_injective hEF @[simp] lemma inclusion_self {E : intermediate_field K L}: inclusion (le_refl E) = alg_hom.id K E := subalgebra.inclusion_self @[simp] lemma inclusion_inclusion {E F G : intermediate_field K L} (hEF : E ≤ F) (hFG : F ≤ G) (x : E) : inclusion hFG (inclusion hEF x) = inclusion (le_trans hEF hFG) x := subalgebra.inclusion_inclusion hEF hFG x @[simp] lemma coe_inclusion {E F : intermediate_field K L} (hEF : E ≤ F) (e : E) : (inclusion hEF e : L) = e := rfl variables {S} lemma to_subalgebra_injective {S S' : intermediate_field K L} (h : S.to_subalgebra = S'.to_subalgebra) : S = S' := by { ext, rw [← mem_to_subalgebra, ← mem_to_subalgebra, h] } variables (S) lemma set_range_subset : set.range (algebra_map K L) ⊆ S := S.to_subalgebra.range_subset lemma field_range_le : (algebra_map K L).field_range ≤ S.to_subfield := λ x hx, S.to_subalgebra.range_subset (by rwa [set.mem_range, ← ring_hom.mem_field_range]) @[simp] lemma to_subalgebra_le_to_subalgebra {S S' : intermediate_field K L} : S.to_subalgebra ≤ S'.to_subalgebra ↔ S ≤ S' := iff.rfl @[simp] lemma to_subalgebra_lt_to_subalgebra {S S' : intermediate_field K L} : S.to_subalgebra < S'.to_subalgebra ↔ S < S' := iff.rfl variables {S} section tower /-- Lift an intermediate_field of an intermediate_field -/ def lift {F : intermediate_field K L} (E : intermediate_field K F) : intermediate_field K L := E.map (val F) instance has_lift {F : intermediate_field K L} : has_lift_t (intermediate_field K F) (intermediate_field K L) := ⟨lift⟩ section restrict_scalars variables (K) [algebra L' L] [is_scalar_tower K L' L] /-- Given a tower `L / ↥E / L' / K` of field extensions, where `E` is an `L'`-intermediate field of `L`, reinterpret `E` as a `K`-intermediate field of `L`. -/ def restrict_scalars (E : intermediate_field L' L) : intermediate_field K L := { carrier := E.carrier, ..E.to_subfield, ..E.to_subalgebra.restrict_scalars K } @[simp] lemma coe_restrict_scalars {E : intermediate_field L' L} : (restrict_scalars K E : set L) = (E : set L) := rfl @[simp] lemma restrict_scalars_to_subalgebra {E : intermediate_field L' L} : (E.restrict_scalars K).to_subalgebra = E.to_subalgebra.restrict_scalars K := set_like.coe_injective rfl @[simp] lemma restrict_scalars_to_subfield {E : intermediate_field L' L} : (E.restrict_scalars K).to_subfield = E.to_subfield := set_like.coe_injective rfl @[simp] lemma mem_restrict_scalars {E : intermediate_field L' L} {x : L} : x ∈ restrict_scalars K E ↔ x ∈ E := iff.rfl lemma restrict_scalars_injective : function.injective (restrict_scalars K : intermediate_field L' L → intermediate_field K L) := λ U V H, ext $ λ x, by rw [← mem_restrict_scalars K, H, mem_restrict_scalars] end restrict_scalars /-- This was formerly an instance called `lift2_alg`, but an instance above already provides it. -/ example {F : intermediate_field K L} {E : intermediate_field F L} : algebra K E := by apply_instance end tower section finite_dimensional variables (F E : intermediate_field K L) instance finite_dimensional_left [finite_dimensional K L] : finite_dimensional K F := left K F L instance finite_dimensional_right [finite_dimensional K L] : finite_dimensional F L := right K F L @[simp] lemma dim_eq_dim_subalgebra : module.rank K F.to_subalgebra = module.rank K F := rfl @[simp] lemma finrank_eq_finrank_subalgebra : finrank K F.to_subalgebra = finrank K F := rfl variables {F} {E} @[simp] lemma to_subalgebra_eq_iff : F.to_subalgebra = E.to_subalgebra ↔ F = E := by { rw [set_like.ext_iff, set_like.ext'_iff, set.ext_iff], refl } lemma eq_of_le_of_finrank_le [finite_dimensional K L] (h_le : F ≤ E) (h_finrank : finrank K E ≤ finrank K F) : F = E := to_subalgebra_injective $ subalgebra.to_submodule_injective $ eq_of_le_of_finrank_le h_le h_finrank lemma eq_of_le_of_finrank_eq [finite_dimensional K L] (h_le : F ≤ E) (h_finrank : finrank K F = finrank K E) : F = E := eq_of_le_of_finrank_le h_le h_finrank.ge lemma eq_of_le_of_finrank_le' [finite_dimensional K L] (h_le : F ≤ E) (h_finrank : finrank F L ≤ finrank E L) : F = E := begin apply eq_of_le_of_finrank_le h_le, have h1 := finrank_mul_finrank K F L, have h2 := finrank_mul_finrank K E L, have h3 : 0 < finrank E L := finrank_pos, nlinarith, end lemma eq_of_le_of_finrank_eq' [finite_dimensional K L] (h_le : F ≤ E) (h_finrank : finrank F L = finrank E L) : F = E := eq_of_le_of_finrank_le' h_le h_finrank.le end finite_dimensional lemma is_algebraic_iff {x : S} : is_algebraic K x ↔ is_algebraic K (x : L) := (is_algebraic_algebra_map_iff (algebra_map S L).injective).symm lemma is_integral_iff {x : S} : is_integral K x ↔ is_integral K (x : L) := by rw [←is_algebraic_iff_is_integral, is_algebraic_iff, is_algebraic_iff_is_integral] lemma minpoly_eq (x : S) : minpoly K x = minpoly K (x : L) := begin by_cases hx : is_integral K x, { exact minpoly.eq_of_algebra_map_eq (algebra_map S L).injective hx rfl }, { exact (minpoly.eq_zero hx).trans (minpoly.eq_zero (mt is_integral_iff.mpr hx)).symm }, end end intermediate_field /-- If `L/K` is algebraic, the `K`-subalgebras of `L` are all fields. -/ def subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L) : subalgebra K L ≃o intermediate_field K L := { to_fun := λ S, S.to_intermediate_field (λ x hx, S.inv_mem_of_algebraic (alg (⟨x, hx⟩ : S))), inv_fun := λ S, S.to_subalgebra, left_inv := λ S, to_subalgebra_to_intermediate_field _ _, right_inv := to_intermediate_field_to_subalgebra, map_rel_iff' := λ S S', iff.rfl } @[simp] lemma mem_subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L) {S : subalgebra K L} {x : L} : x ∈ subalgebra_equiv_intermediate_field alg S ↔ x ∈ S := iff.rfl @[simp] lemma mem_subalgebra_equiv_intermediate_field_symm (alg : algebra.is_algebraic K L) {S : intermediate_field K L} {x : L} : x ∈ (subalgebra_equiv_intermediate_field alg).symm S ↔ x ∈ S := iff.rfl
3a58d471592b684dad0767ce6577ff5caf97362f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/localization.lean
b922cbcf17c99521dff2779f971c848af82585a2
[]
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
74,815
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.equiv.ring import Mathlib.group_theory.monoid_localization import Mathlib.ring_theory.ideal.operations import Mathlib.ring_theory.algebraic import Mathlib.ring_theory.integral_closure import Mathlib.ring_theory.non_zero_divisors import Mathlib.PostPort universes u_1 u_2 l u_3 u_4 u_5 u_6 u_7 namespace Mathlib /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. Given such a localization map `f : R →+* S`, we can define the surjection `localization_map.mk'` sending `(x, y) : R × M` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. Similarly, given commutative rings `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : R →+* P` such that `g(M) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `S` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We show the localization as a quotient type, defined in `group_theory.monoid_localization` as `submonoid.localization`, is a `comm_ring` and that the natural ring hom `of : R →+* localization M` is a localization map. We show that a localization at the complement of a prime ideal is a local ring. We prove some lemmas about the `R`-algebra structure of `S`. When `R` is an integral domain, we define `fraction_map R K` as an abbreviation for `localization (non_zero_divisors R) K`, the natural map to `R`'s field of fractions. We show that a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field. We use this to show the field of fractions as a quotient type, `fraction_ring`, is a field. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A ring localization map is defined to be a localization map of the underlying `comm_monoid` (a `submonoid.localization_map`) which is also a ring hom. To prove most lemmas about a `localization_map` `f` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `f.to_localization_map`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `of : localization_map M (localization M)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ /-- The type of ring homomorphisms satisfying the characteristic predicate: if `f : R →+* S` satisfies this predicate, then `S` is isomorphic to the localization of `R` at `M`. We later define an instance coercing a localization map `f` to its codomain `S` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. -/ structure localization_map {R : Type u_1} [comm_ring R] (M : submonoid R) (S : Type u_2) [comm_ring S] extends R →+* S, submonoid.localization_map M S where /-- The ring hom underlying a `localization_map`. -/ /-- The `comm_monoid` `localization_map` underlying a `comm_ring` `localization_map`. See `group_theory.monoid_localization` for its definition. -/ namespace ring_hom /-- Makes a localization map from a `comm_ring` hom satisfying the characteristic predicate. -/ def to_localization_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : R →+* S) (H1 : ∀ (y : ↥M), is_unit (coe_fn f ↑y)) (H2 : ∀ (z : S), ∃ (x : R × ↥M), z * coe_fn f ↑(prod.snd x) = coe_fn f (prod.fst x)) (H3 : ∀ (x y : R), coe_fn f x = coe_fn f y ↔ ∃ (c : ↥M), x * ↑c = y * ↑c) : localization_map M S := localization_map.mk (to_fun f) sorry sorry sorry sorry H1 H2 H3 end ring_hom /-- Makes a `comm_ring` localization map from an additive `comm_monoid` localization map of `comm_ring`s. -/ def submonoid.localization_map.to_ring_localization {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : submonoid.localization_map M S) (h : ∀ (x y : R), coe_fn (submonoid.localization_map.to_map f) (x + y) = coe_fn (submonoid.localization_map.to_map f) x + coe_fn (submonoid.localization_map.to_map f) y) : localization_map M S := localization_map.mk (ring_hom.to_fun (ring_hom.mk' (submonoid.localization_map.to_monoid_hom f) h)) sorry sorry sorry sorry sorry sorry sorry namespace localization_map /-- We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know` the map needed to induce the instance. -/ def codomain {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) := S protected instance codomain.comm_ring {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : comm_ring (codomain f) := _inst_2 protected instance codomain.field {R : Type u_1} [comm_ring R] {M : submonoid R} {K : Type u_2} [field K] (f : localization_map M K) : field (codomain f) := _inst_4 /-- Short for `to_ring_hom`; used for applying a localization map as a function. -/ def to_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : R →+* S := to_ring_hom f theorem map_units {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (y : ↥M) : is_unit (coe_fn (to_map f) ↑y) := map_units' f y theorem surj {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (z : S) : ∃ (x : R × ↥M), z * coe_fn (to_map f) ↑(prod.snd x) = coe_fn (to_map f) (prod.fst x) := surj' f z theorem eq_iff_exists {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : R} : coe_fn (to_map f) x = coe_fn (to_map f) y ↔ ∃ (c : ↥M), x * ↑c = y * ↑c := eq_iff_exists' f x y theorem ext {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {g : localization_map M S} (h : ∀ (x : R), coe_fn (to_map f) x = coe_fn (to_map g) x) : f = g := sorry theorem ext_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {g : localization_map M S} : f = g ↔ ∀ (x : R), coe_fn (to_map f) x = coe_fn (to_map g) x := { mp := fun (h : f = g) (x : R) => h ▸ rfl, mpr := ext } theorem to_map_injective {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] : function.injective to_map := fun (_x _x_1 : localization_map M S) (h : to_map _x = to_map _x_1) => ext (iff.mp ring_hom.ext_iff h) /-- Given `a : S`, `S` a localization of `R`, `is_integer a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (a : S) := a ∈ set.range ⇑(to_map f) -- TODO: define a subalgebra of `is_integer`s theorem is_integer_zero {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : is_integer f 0 := Exists.intro 0 (ring_hom.map_zero (to_map f)) theorem is_integer_one {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : is_integer f 1 := Exists.intro 1 (ring_hom.map_one (to_map f)) theorem is_integer_add {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {a : S} {b : S} (ha : is_integer f a) (hb : is_integer f b) : is_integer f (a + b) := sorry theorem is_integer_mul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {a : S} {b : S} (ha : is_integer f a) (hb : is_integer f b) : is_integer f (a * b) := sorry theorem is_integer_smul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {a : R} {b : S} (hb : is_integer f b) : is_integer f (coe_fn (to_map f) a * b) := sorry /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ theorem exists_integer_multiple' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (a : S) : ∃ (b : ↥M), is_integer f (a * coe_fn (to_map f) ↑b) := sorry /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ theorem exists_integer_multiple {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (a : S) : ∃ (b : ↥M), is_integer f (coe_fn (to_map f) ↑b * a) := sorry /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ theorem sec_spec {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (z : S) : z * coe_fn (to_map f) ↑(prod.snd (submonoid.localization_map.sec (to_localization_map f) z)) = coe_fn (to_map f) (prod.fst (submonoid.localization_map.sec (to_localization_map f) z)) := classical.some_spec (surj f z) /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ theorem sec_spec' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (z : S) : coe_fn (to_map f) (prod.fst (submonoid.localization_map.sec (to_localization_map f) z)) = coe_fn (to_map f) ↑(prod.snd (submonoid.localization_map.sec (to_localization_map f) z)) * z := sorry /-- We can clear the denominators of a finite set of fractions. -/ theorem exist_integer_multiples_of_finset {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (s : finset S) : ∃ (b : ↥M), ∀ (a : S), a ∈ s → is_integer f (coe_fn (to_map f) ↑b * a) := sorry theorem map_right_cancel {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : R} {c : ↥M} (h : coe_fn (to_map f) (↑c * x) = coe_fn (to_map f) (↑c * y)) : coe_fn (to_map f) x = coe_fn (to_map f) y := submonoid.localization_map.map_right_cancel (to_localization_map f) h theorem map_left_cancel {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : R} {c : ↥M} (h : coe_fn (to_map f) (x * ↑c) = coe_fn (to_map f) (y * ↑c)) : coe_fn (to_map f) x = coe_fn (to_map f) y := submonoid.localization_map.map_left_cancel (to_localization_map f) h theorem eq_zero_of_fst_eq_zero {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {z : S} {x : R} {y : ↥M} (h : z * coe_fn (to_map f) ↑y = coe_fn (to_map f) x) (hx : x = 0) : z = 0 := iff.mp (is_unit.mul_left_eq_zero (map_units f y)) (eq.mp (Eq._oldrec (Eq.refl (z * coe_fn (to_map f) ↑y = coe_fn (to_map f) 0)) (ring_hom.map_zero (to_map f))) (eq.mp (Eq._oldrec (Eq.refl (z * coe_fn (to_map f) ↑y = coe_fn (to_map f) x)) hx) h)) /-- Given a localization map `f : R →+* S`, the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ def mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : S := submonoid.localization_map.mk' (to_localization_map f) x y @[simp] theorem mk'_sec {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (z : S) : mk' f (prod.fst (submonoid.localization_map.sec (to_localization_map f) z)) (prod.snd (submonoid.localization_map.sec (to_localization_map f) z)) = z := submonoid.localization_map.mk'_sec (to_localization_map f) z theorem mk'_mul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x₁ : R) (x₂ : R) (y₁ : ↥M) (y₂ : ↥M) : mk' f (x₁ * x₂) (y₁ * y₂) = mk' f x₁ y₁ * mk' f x₂ y₂ := submonoid.localization_map.mk'_mul (to_localization_map f) x₁ x₂ y₁ y₂ theorem mk'_one {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) : mk' f x 1 = coe_fn (to_map f) x := submonoid.localization_map.mk'_one (to_localization_map f) x @[simp] theorem mk'_spec {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : mk' f x y * coe_fn (to_map f) ↑y = coe_fn (to_map f) x := submonoid.localization_map.mk'_spec (to_localization_map f) x y @[simp] theorem mk'_spec' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : coe_fn (to_map f) ↑y * mk' f x y = coe_fn (to_map f) x := submonoid.localization_map.mk'_spec' (to_localization_map f) x y theorem eq_mk'_iff_mul_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : ↥M} {z : S} : z = mk' f x y ↔ z * coe_fn (to_map f) ↑y = coe_fn (to_map f) x := submonoid.localization_map.eq_mk'_iff_mul_eq (to_localization_map f) theorem mk'_eq_iff_eq_mul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : ↥M} {z : S} : mk' f x y = z ↔ coe_fn (to_map f) x = z * coe_fn (to_map f) ↑y := submonoid.localization_map.mk'_eq_iff_eq_mul (to_localization_map f) theorem mk'_surjective {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (z : S) : ∃ (x : R), ∃ (y : ↥M), mk' f x y = z := sorry theorem mk'_eq_iff_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x₁ : R} {x₂ : R} {y₁ : ↥M} {y₂ : ↥M} : mk' f x₁ y₁ = mk' f x₂ y₂ ↔ coe_fn (to_map f) (x₁ * ↑y₂) = coe_fn (to_map f) (x₂ * ↑y₁) := submonoid.localization_map.mk'_eq_iff_eq (to_localization_map f) theorem mk'_mem_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} {y : ↥M} {I : ideal S} : mk' f x y ∈ I ↔ coe_fn (to_map f) x ∈ I := sorry protected theorem eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {a₁ : R} {b₁ : R} {a₂ : ↥M} {b₂ : ↥M} : mk' f a₁ a₂ = mk' f b₁ b₂ ↔ ∃ (c : ↥M), a₁ * ↑b₂ * ↑c = b₁ * ↑a₂ * ↑c := submonoid.localization_map.eq (to_localization_map f) theorem eq_iff_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) (g : localization_map M P) {x : R} {y : R} : coe_fn (to_map f) x = coe_fn (to_map f) y ↔ coe_fn (to_map g) x = coe_fn (to_map g) y := submonoid.localization_map.eq_iff_eq (to_localization_map f) (to_localization_map g) theorem mk'_eq_iff_mk'_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) (g : localization_map M P) {x₁ : R} {x₂ : R} {y₁ : ↥M} {y₂ : ↥M} : mk' f x₁ y₁ = mk' f x₂ y₂ ↔ mk' g x₁ y₁ = mk' g x₂ y₂ := submonoid.localization_map.mk'_eq_iff_mk'_eq (to_localization_map f) (to_localization_map g) theorem mk'_eq_of_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {a₁ : R} {b₁ : R} {a₂ : ↥M} {b₂ : ↥M} (H : b₁ * ↑a₂ = a₁ * ↑b₂) : mk' f a₁ a₂ = mk' f b₁ b₂ := submonoid.localization_map.mk'_eq_of_eq (to_localization_map f) H @[simp] theorem mk'_self {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} (hx : x ∈ M) : mk' f x { val := x, property := hx } = 1 := submonoid.localization_map.mk'_self (to_localization_map f) x hx @[simp] theorem mk'_self' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : ↥M} : mk' f (↑x) x = 1 := submonoid.localization_map.mk'_self' (to_localization_map f) x theorem mk'_self'' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : ↥M} : mk' f (subtype.val x) x = 1 := mk'_self' f theorem mul_mk'_eq_mk'_of_mul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : R) (z : ↥M) : coe_fn (to_map f) x * mk' f y z = mk' f (x * y) z := submonoid.localization_map.mul_mk'_eq_mk'_of_mul (to_localization_map f) x y z theorem mk'_eq_mul_mk'_one {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : mk' f x y = coe_fn (to_map f) x * mk' f 1 y := Eq.symm (submonoid.localization_map.mul_mk'_one_eq_mk' (to_localization_map f) x y) @[simp] theorem mk'_mul_cancel_left {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : mk' f (↑y * x) y = coe_fn (to_map f) x := submonoid.localization_map.mk'_mul_cancel_left (to_localization_map f) x y theorem mk'_mul_cancel_right {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) : mk' f (x * ↑y) y = coe_fn (to_map f) x := submonoid.localization_map.mk'_mul_cancel_right (to_localization_map f) x y @[simp] theorem mk'_mul_mk'_eq_one {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : ↥M) (y : ↥M) : mk' f (↑x) y * mk' f (↑y) x = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (mk' f (↑x) y * mk' f (↑y) x = 1)) (Eq.symm (mk'_mul f (↑x) (↑y) y x)))) (eq.mpr (id (Eq._oldrec (Eq.refl (mk' f (↑x * ↑y) (y * x) = 1)) (mul_comm ↑x ↑y))) (mk'_self f (submonoid.has_mul._proof_1 M y x))) theorem mk'_mul_mk'_eq_one' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : R) (y : ↥M) (h : x ∈ M) : mk' f x y * mk' f ↑y { val := x, property := h } = 1 := mk'_mul_mk'_eq_one f { val := x, property := h } y theorem is_unit_comp {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) (j : S →+* P) (y : ↥M) : is_unit (coe_fn (ring_hom.comp j (to_map f)) ↑y) := submonoid.localization_map.is_unit_comp (to_localization_map f) (ring_hom.to_monoid_hom j) y /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ theorem eq_of_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) {x : R} {y : R} (h : coe_fn (to_map f) x = coe_fn (to_map f) y) : coe_fn g x = coe_fn g y := submonoid.localization_map.eq_of_eq (to_localization_map f) hg h theorem mk'_add {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x₁ : R) (x₂ : R) (y₁ : ↥M) (y₂ : ↥M) : mk' f (x₁ * ↑y₂ + x₂ * ↑y₁) (y₁ * y₂) = mk' f x₁ y₁ + mk' f x₂ y₂ := sorry /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ def lift {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) : S →+* P := ring_hom.mk' (submonoid.localization_map.lift (to_localization_map f) hg) sorry /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ theorem lift_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) (x : R) (y : ↥M) : coe_fn (lift f hg) (mk' f x y) = coe_fn g x * ↑(coe_fn (is_unit.lift_right (monoid_hom.mrestrict (ring_hom.to_monoid_hom g) M) hg) y⁻¹) := submonoid.localization_map.lift_mk' (to_localization_map f) hg x y theorem lift_mk'_spec {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) (x : R) (v : P) (y : ↥M) : coe_fn (lift f hg) (mk' f x y) = v ↔ coe_fn g x = coe_fn g ↑y * v := submonoid.localization_map.lift_mk'_spec (to_localization_map f) hg x v y @[simp] theorem lift_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) (x : R) : coe_fn (lift f hg) (coe_fn (to_map f) x) = coe_fn g x := submonoid.localization_map.lift_eq (to_localization_map f) hg x theorem lift_eq_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) {x : R × ↥M} {y : R × ↥M} : coe_fn (lift f hg) (mk' f (prod.fst x) (prod.snd x)) = coe_fn (lift f hg) (mk' f (prod.fst y) (prod.snd y)) ↔ coe_fn g (prod.fst x * ↑(prod.snd y)) = coe_fn g (prod.fst y * ↑(prod.snd x)) := submonoid.localization_map.lift_eq_iff (to_localization_map f) hg @[simp] theorem lift_comp {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) : ring_hom.comp (lift f hg) (to_map f) = g := ring_hom.ext (iff.mp monoid_hom.ext_iff (submonoid.localization_map.lift_comp (to_localization_map f) hg)) @[simp] theorem lift_of_comp {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) (j : S →+* P) : lift f (is_unit_comp f j) = j := ring_hom.ext (iff.mp monoid_hom.ext_iff (submonoid.localization_map.lift_of_comp (to_localization_map f) (ring_hom.to_monoid_hom j))) theorem epic_of_localization_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {j : S →+* P} {k : S →+* P} (h : ∀ (a : R), coe_fn (ring_hom.comp j (to_map f)) a = coe_fn (ring_hom.comp k (to_map f)) a) : j = k := ring_hom.ext (iff.mp monoid_hom.ext_iff (submonoid.localization_map.epic_of_localization_map (to_localization_map f) h)) theorem lift_unique {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) {j : S →+* P} (hj : ∀ (x : R), coe_fn j (coe_fn (to_map f) x) = coe_fn g x) : lift f hg = j := ring_hom.ext (iff.mp monoid_hom.ext_iff (submonoid.localization_map.lift_unique (to_localization_map f) hg hj)) @[simp] theorem lift_id {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : S) : coe_fn (lift f (map_units f)) x = x := submonoid.localization_map.lift_id (to_localization_map f) x /-- Given two localization maps `f : R →+* S, k : R →+* P` for a submonoid `M ⊆ R`, the hom from `P` to `S` induced by `f` is left inverse to the hom from `S` to `P` induced by `k`. -/ @[simp] theorem lift_left_inverse {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {k : localization_map M S} (z : S) : coe_fn (lift k (map_units f)) (coe_fn (lift f (map_units k)) z) = z := submonoid.localization_map.lift_left_inverse (to_localization_map f) z theorem lift_surjective_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) : function.surjective ⇑(lift f hg) ↔ ∀ (v : P), ∃ (x : R × ↥M), v * coe_fn g ↑(prod.snd x) = coe_fn g (prod.fst x) := submonoid.localization_map.lift_surjective_iff (to_localization_map f) hg theorem lift_injective_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} (hg : ∀ (y : ↥M), is_unit (coe_fn g ↑y)) : function.injective ⇑(lift f hg) ↔ ∀ (x y : R), coe_fn (to_map f) x = coe_fn (to_map f) y ↔ coe_fn g x = coe_fn g y := submonoid.localization_map.lift_injective_iff (to_localization_map f) hg /-- Given a `comm_ring` homomorphism `g : R →+* P` where for submonoids `M ⊆ R, T ⊆ P` we have `g(M) ⊆ T`, the induced ring homomorphism from the localization of `R` at `M` to the localization of `P` at `T`: if `f : R →+* S` and `k : P →+* Q` are localization maps for `M` and `T` respectively, we send `z : S` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ def map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] (k : localization_map T Q) : S →+* Q := lift f sorry theorem map_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} (x : R) : coe_fn (map f hy k) (coe_fn (to_map f) x) = coe_fn (to_map k) (coe_fn g x) := lift_eq f (fun (y : ↥M) => map_units k { val := coe_fn g ↑y, property := hy y }) x @[simp] theorem map_comp {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} : ring_hom.comp (map f hy k) (to_map f) = ring_hom.comp (to_map k) g := lift_comp f fun (y : ↥M) => map_units k { val := coe_fn g ↑y, property := hy y } theorem map_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} (x : R) (y : ↥M) : coe_fn (map f hy k) (mk' f x y) = mk' k (coe_fn g x) { val := coe_fn g ↑y, property := hy y } := submonoid.localization_map.map_mk' (to_localization_map f) hy x y @[simp] theorem map_id {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (z : S) : coe_fn (map f (fun (y : ↥M) => (fun (this : coe_fn (ring_hom.id R) ↑y ∈ M) => this) (subtype.property y)) f) z = z := lift_id f z /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ theorem map_comp_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {A : Type u_5} [comm_ring A] {U : submonoid A} {W : Type u_6} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ (w : ↥T), coe_fn l ↑w ∈ U) : ring_hom.comp (map k hl j) (map f hy k) = map f (fun (x : ↥M) => (fun (this : coe_fn (ring_hom.comp l g) ↑x ∈ U) => this) (hl { val := coe_fn g ↑x, property := hy x })) j := ring_hom.ext (iff.mp monoid_hom.ext_iff (submonoid.localization_map.map_comp_map (to_localization_map f) hy (to_localization_map j) hl)) /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ theorem map_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {A : Type u_5} [comm_ring A] {U : submonoid A} {W : Type u_6} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ (w : ↥T), coe_fn l ↑w ∈ U) (x : S) : coe_fn (map k hl j) (coe_fn (map f hy k) x) = coe_fn (map f (fun (x : ↥M) => (fun (this : coe_fn (ring_hom.comp l g) ↑x ∈ U) => this) (hl { val := coe_fn g ↑x, property := hy x })) j) x := sorry /-- Given localization maps `f : R →+* S, k : P →+* Q` for submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ def ring_equiv_of_ring_equiv {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {T : submonoid P} {Q : Type u_4} [comm_ring Q] (k : localization_map T Q) (h : R ≃+* P) (H : submonoid.map (ring_equiv.to_monoid_hom h) M = T) : S ≃+* Q := mul_equiv.to_ring_equiv (submonoid.localization_map.mul_equiv_of_mul_equiv (to_localization_map f) (to_localization_map k) H) sorry @[simp] theorem ring_equiv_of_ring_equiv_eq_map_apply {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {T : submonoid P} {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {j : R ≃+* P} (H : submonoid.map (ring_equiv.to_monoid_hom j) M = T) (x : S) : coe_fn (ring_equiv_of_ring_equiv f k j H) x = coe_fn (map f (fun (y : ↥M) => (fun (this : coe_fn (ring_equiv.to_monoid_hom j) ↑y ∈ T) => this) (H ▸ set.mem_image_of_mem (⇑j) (subtype.property y))) k) x := rfl theorem ring_equiv_of_ring_equiv_eq_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {T : submonoid P} {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {j : R ≃+* P} (H : submonoid.map (ring_equiv.to_monoid_hom j) M = T) : ring_equiv.to_monoid_hom (ring_equiv_of_ring_equiv f k j H) = ↑(map f (fun (y : ↥M) => (fun (this : coe_fn (ring_equiv.to_monoid_hom j) ↑y ∈ T) => this) (H ▸ set.mem_image_of_mem (⇑j) (subtype.property y))) k) := rfl @[simp] theorem ring_equiv_of_ring_equiv_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {T : submonoid P} {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {j : R ≃+* P} (H : submonoid.map (ring_equiv.to_monoid_hom j) M = T) (x : R) : coe_fn (ring_equiv_of_ring_equiv f k j H) (coe_fn (to_map f) x) = coe_fn (to_map k) (coe_fn j x) := submonoid.localization_map.mul_equiv_of_mul_equiv_eq (to_localization_map f) H x theorem ring_equiv_of_ring_equiv_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (f : localization_map M S) {T : submonoid P} {Q : Type u_4} [comm_ring Q] {k : localization_map T Q} {j : R ≃+* P} (H : submonoid.map (ring_equiv.to_monoid_hom j) M = T) (x : R) (y : ↥M) : coe_fn (ring_equiv_of_ring_equiv f k j H) (mk' f x y) = mk' k (coe_fn j x) { val := coe_fn j ↑y, property := H ▸ set.mem_image_of_mem (⇑j) (subtype.property y) } := submonoid.localization_map.mul_equiv_of_mul_equiv_mk' (to_localization_map f) H x y /-- Given `x : R`, the type of homomorphisms `f : R →* S` such that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ def away_map {R : Type u_1} [comm_ring R] (x : R) (S' : Type u_2) [comm_ring S'] := localization_map (submonoid.powers x) S' /-- Given `x : R` and a localization map `F : R →+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ def away_map.inv_self {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] (x : R) (F : away_map x S) : S := mk' F 1 { val := x, property := sorry } /-- Given `x : R`, a localization map `F : R →+* S` away from `x`, and a map of `comm_ring`s `g : R →+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ def away_map.lift {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] {g : R →+* P} (x : R) (F : away_map x S) (hg : is_unit (coe_fn g x)) : S →+* P := lift F sorry @[simp] theorem away_map.lift_eq {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] {g : R →+* P} (x : R) (F : away_map x S) (hg : is_unit (coe_fn g x)) (a : R) : coe_fn (away_map.lift x F hg) (coe_fn (to_map F) a) = coe_fn g a := lift_eq F (away_map.lift._proof_1 x hg) a @[simp] theorem away_map.lift_comp {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] {g : R →+* P} (x : R) (F : away_map x S) (hg : is_unit (coe_fn g x)) : ring_hom.comp (away_map.lift x F hg) (to_map F) = g := lift_comp F (away_map.lift._proof_1 x hg) /-- Given `x y : R` and localization maps `F : R →+* S, G : R →+* P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ def away_to_away_right {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] (x : R) (F : away_map x S) (y : R) (G : away_map (x * y) P) : S →* P := ↑(away_map.lift x F sorry) end localization_map namespace localization protected instance has_add {R : Type u_1} [comm_ring R] {M : submonoid R} : Add (localization M) := { add := fun (z w : localization M) => con.lift_on₂ z w (fun (x y : R × ↥M) => mk (↑(prod.snd x) * prod.fst y + ↑(prod.snd y) * prod.fst x) (prod.snd x * prod.snd y)) sorry } protected instance has_neg {R : Type u_1} [comm_ring R] {M : submonoid R} : Neg (localization M) := { neg := fun (z : localization M) => con.lift_on z (fun (x : R × ↥M) => mk (-prod.fst x) (prod.snd x)) sorry } protected instance has_zero {R : Type u_1} [comm_ring R] {M : submonoid R} : HasZero (localization M) := { zero := mk 0 1 } protected instance comm_ring {R : Type u_1} [comm_ring R] {M : submonoid R} : comm_ring (localization M) := comm_ring.mk Add.add sorry 0 sorry sorry Neg.neg (fun (x y : localization M) => x + -y) sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry sorry /-- Natural hom sending `x : R`, `R` a `comm_ring`, to the equivalence class of `(x, 1)` in the localization of `R` at a submonoid. -/ def of {R : Type u_1} [comm_ring R] (M : submonoid R) : localization_map M (localization M) := submonoid.localization_map.to_ring_localization (monoid_of M) sorry theorem monoid_of_eq_of {R : Type u_1} [comm_ring R] {M : submonoid R} (x : R) : coe_fn (submonoid.localization_map.to_map (monoid_of M)) x = coe_fn (localization_map.to_map (of M)) x := rfl theorem mk_one_eq_of {R : Type u_1} [comm_ring R] {M : submonoid R} (x : R) : mk x 1 = coe_fn (localization_map.to_map (of M)) x := rfl theorem mk_eq_mk'_apply {R : Type u_1} [comm_ring R] {M : submonoid R} (x : R) (y : ↥M) : mk x y = localization_map.mk' (of M) x y := mk_eq_monoid_of_mk'_apply x y @[simp] theorem mk_eq_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} : mk = localization_map.mk' (of M) := mk_eq_monoid_of_mk' /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ def ring_equiv_of_quotient {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : localization M ≃+* S := mul_equiv.to_ring_equiv (mul_equiv_of_quotient (localization_map.to_localization_map f)) sorry @[simp] theorem ring_equiv_of_quotient_apply {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : localization M) : coe_fn (ring_equiv_of_quotient f) x = coe_fn (localization_map.lift (of M) (localization_map.map_units f)) x := rfl @[simp] theorem ring_equiv_of_quotient_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) (y : ↥M) : coe_fn (ring_equiv_of_quotient f) (localization_map.mk' (of M) x y) = localization_map.mk' f x y := mul_equiv_of_quotient_mk' x y theorem ring_equiv_of_quotient_mk {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) (y : ↥M) : coe_fn (ring_equiv_of_quotient f) (mk x y) = localization_map.mk' f x y := mul_equiv_of_quotient_mk x y @[simp] theorem ring_equiv_of_quotient_of {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) : coe_fn (ring_equiv_of_quotient f) (coe_fn (localization_map.to_map (of M)) x) = coe_fn (localization_map.to_map f) x := mul_equiv_of_quotient_monoid_of x @[simp] theorem ring_equiv_of_quotient_symm_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) (y : ↥M) : coe_fn (ring_equiv.symm (ring_equiv_of_quotient f)) (localization_map.mk' f x y) = localization_map.mk' (of M) x y := mul_equiv_of_quotient_symm_mk' x y theorem ring_equiv_of_quotient_symm_mk {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) (y : ↥M) : coe_fn (ring_equiv.symm (ring_equiv_of_quotient f)) (localization_map.mk' f x y) = mk x y := mul_equiv_of_quotient_symm_mk x y @[simp] theorem ring_equiv_of_quotient_symm_of {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (x : R) : coe_fn (ring_equiv.symm (ring_equiv_of_quotient f)) (coe_fn (localization_map.to_map f) x) = coe_fn (localization_map.to_map (of M)) x := mul_equiv_of_quotient_symm_monoid_of x /-- Given `x : R`, the natural hom sending `y : R`, `R` a `comm_ring`, to the equivalence class of `(y, 1)` in the localization of `R` at the submonoid generated by `x`. -/ def away.of {R : Type u_1} [comm_ring R] (x : R) : localization_map.away_map x (away x) := of (submonoid.powers x) @[simp] theorem away.mk_eq_mk' {R : Type u_1} [comm_ring R] (x : R) : mk = localization_map.mk' (away.of x) := mk_eq_mk' /-- Given `x : R` and a localization map `f : R →+* S` away from `x`, we get an isomorphism between the localization of `R` at the submonoid generated by `x` as a quotient type and `S`. -/ def away.ring_equiv_of_quotient {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] (x : R) (f : localization_map.away_map x S) : away x ≃+* S := ring_equiv_of_quotient f end localization namespace ideal /-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/ def prime_compl {R : Type u_1} [comm_ring R] (I : ideal R) [hp : is_prime I] : submonoid R := submonoid.mk (↑Iᶜ) sorry sorry end ideal namespace localization_map /-- A localization map from `R` to `S` where the submonoid is the complement of a prime ideal of `R`. -/ def at_prime {R : Type u_1} [comm_ring R] (S : Type u_2) [comm_ring S] (I : ideal R) [hp : ideal.is_prime I] := localization_map (ideal.prime_compl I) S end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal, as a quotient type. -/ def at_prime {R : Type u_1} [comm_ring R] (I : ideal R) [hp : ideal.is_prime I] := localization (ideal.prime_compl I) end localization namespace localization_map /-- When `f` is a localization map from `R` at the complement of a prime ideal `I`, we use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `local_ring` instance on `S` can 'know' the map needed to induce the instance. -/ protected instance at_prime.local_ring {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {I : ideal R} [hp : ideal.is_prime I] (f : at_prime S I) : local_ring (codomain f) := sorry end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ protected instance at_prime.local_ring {R : Type u_1} [comm_ring R] (I : ideal R) [hp : ideal.is_prime I] : local_ring (localization (ideal.prime_compl I)) := localization_map.at_prime.local_ring (of (ideal.prime_compl I)) end localization namespace localization_map /-- Explicit characterization of the ideal given by `ideal.map f.to_map I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ theorem mem_map_to_map_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {I : ideal R} {z : S} : z ∈ ideal.map (to_map f) I ↔ ∃ (x : ↥I × ↥M), z * coe_fn (to_map f) ↑(prod.snd x) = coe_fn (to_map f) ↑(prod.fst x) := sorry theorem map_comap {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (J : ideal S) : ideal.map (to_map f) (ideal.comap (to_map f) J) = J := sorry theorem comap_map_of_is_prime_disjoint {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (I : ideal R) (hI : ideal.is_prime I) (hM : disjoint ↑M ↑I) : ideal.comap (to_map f) (ideal.map (to_map f) I) = I := sorry /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : ideal S ↪o ideal R := rel_embedding.mk (function.embedding.mk (fun (J : ideal S) => ideal.comap (to_map f) J) sorry) sorry /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ theorem is_prime_iff_is_prime_disjoint {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (J : ideal S) : ideal.is_prime J ↔ ideal.is_prime (ideal.comap (to_map f) J) ∧ disjoint ↑M ↑(ideal.comap (to_map f) J) := sorry /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ theorem is_prime_of_is_prime_disjoint {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (I : ideal R) (hp : ideal.is_prime I) (hd : disjoint ↑M ↑I) : ideal.is_prime (ideal.map (to_map f) I) := sorry /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : (Subtype fun (p : ideal S) => ideal.is_prime p) ≃o Subtype fun (p : ideal R) => ideal.is_prime p ∧ disjoint ↑M ↑p := rel_iso.mk (equiv.mk (fun (p : Subtype fun (p : ideal S) => ideal.is_prime p) => { val := ideal.comap (to_map f) (subtype.val p), property := sorry }) (fun (p : Subtype fun (p : ideal R) => ideal.is_prime p ∧ disjoint ↑M ↑p) => { val := ideal.map (to_map f) (subtype.val p), property := sorry }) sorry sorry) sorry /-- `quotient_map` applied to maximal ideals of a localization is `surjective`. The quotient by a maximal ideal is a field, so inverses to elements already exist, and the localization necessarily maps the equivalence class of the inverse in the localization -/ theorem surjective_quotient_map_of_maximal_of_localization {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {I : ideal S} [ideal.is_prime I] {J : ideal R} {H : J ≤ ideal.comap (to_map f) I} (hI : ideal.is_maximal (ideal.comap (to_map f) I)) : function.surjective ⇑(ideal.quotient_map I (to_map f) H) := sorry /-! ### `algebra` section Defines the `R`-algebra instance on a copy of `S` carrying the data of the localization map `f` needed to induce the `R`-algebra structure. -/ /-- We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ protected instance algebra {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : algebra R (codomain f) := ring_hom.to_algebra (to_map f) end localization_map namespace localization protected instance algebra {R : Type u_1} [comm_ring R] {M : submonoid R} : algebra R (localization M) := localization_map.algebra (of M) end localization namespace localization_map @[simp] theorem of_id {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (a : R) : coe_fn (algebra.of_id R (codomain f)) a = coe_fn (to_map f) a := rfl @[simp] theorem algebra_map_eq {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : algebra_map R (codomain f) = to_map f := rfl /-- Localization map `f` from `R` to `S` as an `R`-linear map. -/ def lin_coe {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : linear_map R R (codomain f) := linear_map.mk ⇑(to_map f) sorry sorry /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `f.codomain = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (I : ideal R) : submodule R (codomain f) := submodule.map (lin_coe f) I theorem mem_coe_submodule {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (I : ideal R) {x : S} : x ∈ coe_submodule f I ↔ ∃ (y : R), y ∈ I ∧ coe_fn (to_map f) y = x := iff.rfl @[simp] theorem lin_coe_apply {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {x : R} : coe_fn (lin_coe f) x = coe_fn (to_map f) x := rfl theorem map_smul {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {P : Type u_3} [comm_ring P] {f : localization_map M S} {g : R →+* P} {T : submonoid P} (hy : ∀ (y : ↥M), coe_fn g ↑y ∈ T) {Q : Type u_4} [comm_ring Q] (k : localization_map T Q) (x : codomain f) (z : R) : coe_fn (map f hy k) (z • x) = coe_fn g z • coe_fn (map f hy k) x := sorry theorem is_noetherian_ring {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (h : is_noetherian_ring R) : is_noetherian_ring (codomain f) := sorry end localization_map namespace localization /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an `R`-preserving isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ def alg_equiv_of_quotient {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : alg_equiv R (localization M) (localization_map.codomain f) := alg_equiv.mk (ring_equiv.to_fun (ring_equiv_of_quotient f)) (ring_equiv.inv_fun (ring_equiv_of_quotient f)) sorry sorry sorry sorry sorry theorem alg_equiv_of_quotient_apply {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : localization M) : coe_fn (alg_equiv_of_quotient f) x = coe_fn (ring_equiv_of_quotient f) x := rfl theorem alg_equiv_of_quotient_symm_apply {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (x : localization_map.codomain f) : coe_fn (alg_equiv.symm (alg_equiv_of_quotient f)) x = coe_fn (ring_equiv.symm (ring_equiv_of_quotient f)) x := rfl end localization namespace localization_map /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ def coeff_integer_normalization {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (p : polynomial (codomain f)) (i : ℕ) : R := dite (i ∈ finsupp.support p) (fun (hi : i ∈ finsupp.support p) => classical.some sorry) fun (hi : ¬i ∈ finsupp.support p) => 0 theorem coeff_integer_normalization_mem_support {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (p : polynomial (codomain f)) (i : ℕ) (h : coeff_integer_normalization p i ≠ 0) : i ∈ finsupp.support p := sorry /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ def integer_normalization {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) : polynomial (codomain f) → polynomial R := fun (p : polynomial (codomain f)) => finsupp.on_finset (finsupp.support p) (coeff_integer_normalization p) (coeff_integer_normalization_mem_support p) @[simp] theorem integer_normalization_coeff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (p : polynomial (codomain f)) (i : ℕ) : polynomial.coeff (integer_normalization f p) i = coeff_integer_normalization p i := rfl theorem integer_normalization_spec {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (p : polynomial (codomain f)) : ∃ (b : ↥M), ∀ (i : ℕ), coe_fn (to_map f) (polynomial.coeff (integer_normalization f p) i) = coe_fn (to_map f) ↑b * polynomial.coeff p i := sorry theorem integer_normalization_map_to_map {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} (p : polynomial (codomain f)) : ∃ (b : ↥M), polynomial.map (to_map f) (integer_normalization f p) = coe_fn (to_map f) ↑b • p := sorry theorem integer_normalization_eval₂_eq_zero {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {R' : Type u_4} [comm_ring R'] (g : codomain f →+* R') (p : polynomial (codomain f)) {x : R'} (hx : polynomial.eval₂ g x p = 0) : polynomial.eval₂ (ring_hom.comp g (to_map f)) x (integer_normalization f p) = 0 := sorry theorem integer_normalization_aeval_eq_zero {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {f : localization_map M S} {R' : Type u_4} [comm_ring R'] [algebra R R'] [algebra (codomain f) R'] [is_scalar_tower R (codomain f) R'] (p : polynomial (codomain f)) {x : R'} (hx : coe_fn (polynomial.aeval x) p = 0) : coe_fn (polynomial.aeval x) (integer_normalization f p) = 0 := sorry theorem to_map_eq_zero_iff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) {x : R} (hM : M ≤ non_zero_divisors R) : coe_fn (to_map f) x = 0 ↔ x = 0 := sorry theorem injective {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] (f : localization_map M S) (hM : M ≤ non_zero_divisors R) : function.injective ⇑(to_map f) := sorry protected theorem to_map_ne_zero_of_mem_non_zero_divisors {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] [nontrivial R] (f : localization_map M S) (hM : M ≤ non_zero_divisors R) (x : ↥(non_zero_divisors R)) : coe_fn (to_map f) ↑x ≠ 0 := map_ne_zero_of_mem_non_zero_divisors (injective f hM) /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. -/ def integral_domain_of_le_non_zero_divisors {S : Type u_2} [comm_ring S] {A : Type u_4} [integral_domain A] {M : submonoid A} (f : localization_map M S) (hM : M ≤ non_zero_divisors A) : integral_domain S := integral_domain.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry sorry sorry /-- The localization at of an integral domain to a set of non-zero elements is an integral domain -/ def integral_domain_localization {A : Type u_4} [integral_domain A] {M : submonoid A} (hM : M ≤ non_zero_divisors A) : integral_domain (localization M) := integral_domain_of_le_non_zero_divisors (localization.of M) hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ protected instance integral_domain_of_local_at_prime {A : Type u_4} [integral_domain A] {P : ideal A} (hp : ideal.is_prime P) : integral_domain (localization.at_prime P) := integral_domain_localization sorry end localization_map namespace localization /-- The image of `P` in the localization at `P.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ theorem at_prime.map_eq_maximal_ideal {R : Type u_1} [comm_ring R] {P : ideal R} [hP : ideal.is_prime P] : ideal.map (localization_map.to_map (of (ideal.prime_compl P))) P = local_ring.maximal_ideal (localization (ideal.prime_compl P)) := sorry /-- The unique maximal ideal of the localization at `P.prime_compl` lies over the ideal `P`. -/ theorem at_prime.comap_maximal_ideal {R : Type u_1} [comm_ring R] {P : ideal R} [ideal.is_prime P] : ideal.comap (localization_map.to_map (of (ideal.prime_compl P))) (local_ring.maximal_ideal (localization (ideal.prime_compl P))) = P := sorry end localization /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ theorem localization_map_bijective_of_field {R : Type u_1} {Rₘ : Type u_2} [integral_domain R] [comm_ring Rₘ] {M : submonoid R} (hM : ¬0 ∈ M) (hR : is_field R) (f : localization_map M Rₘ) : function.bijective ⇑(localization_map.to_map f) := sorry /-- Localization map from an integral domain `R` to its field of fractions. -/ def fraction_map (R : Type u_1) [comm_ring R] (K : Type u_5) [comm_ring K] := localization_map (non_zero_divisors R) K namespace fraction_map theorem to_map_eq_zero_iff {R : Type u_1} [comm_ring R] {K : Type u_5} [comm_ring K] (φ : fraction_map R K) {x : R} : coe_fn (localization_map.to_map φ) x = 0 ↔ x = 0 := localization_map.to_map_eq_zero_iff φ (le_of_eq rfl) protected theorem injective {R : Type u_1} [comm_ring R] {K : Type u_5} [comm_ring K] (φ : fraction_map R K) : function.injective ⇑(localization_map.to_map φ) := localization_map.injective φ (le_of_eq rfl) protected theorem to_map_ne_zero_of_mem_non_zero_divisors {R : Type u_1} [comm_ring R] {K : Type u_5} [nontrivial R] [comm_ring K] (φ : fraction_map R K) (x : ↥(non_zero_divisors R)) : coe_fn (localization_map.to_map φ) ↑x ≠ 0 := localization_map.to_map_ne_zero_of_mem_non_zero_divisors φ (le_of_eq rfl) x /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain {A : Type u_4} [integral_domain A] {K : Type u_5} [comm_ring K] (φ : fraction_map A K) : integral_domain K := localization_map.integral_domain_of_le_non_zero_divisors φ sorry /-- The inverse of an element in the field of fractions of an integral domain. -/ protected def inv {A : Type u_4} [integral_domain A] {K : Type u_5} [comm_ring K] (φ : fraction_map A K) (z : K) : K := dite (z = 0) (fun (h : z = 0) => 0) fun (h : ¬z = 0) => localization_map.mk' φ ↑(prod.snd (submonoid.localization_map.sec (localization_map.to_localization_map φ) z)) { val := prod.fst (submonoid.localization_map.sec (localization_map.to_localization_map φ) z), property := sorry } protected theorem mul_inv_cancel {A : Type u_4} [integral_domain A] {K : Type u_5} [comm_ring K] (φ : fraction_map A K) (x : K) (hx : x ≠ 0) : x * fraction_map.inv φ x = 1 := sorry /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ def to_field {A : Type u_4} [integral_domain A] {K : Type u_5} [comm_ring K] (φ : fraction_map A K) : field K := field.mk integral_domain.add sorry integral_domain.zero sorry sorry integral_domain.neg integral_domain.sub sorry sorry integral_domain.mul sorry integral_domain.one sorry sorry sorry sorry sorry (fraction_map.inv φ) sorry (fraction_map.mul_inv_cancel φ) sorry theorem mk'_eq_div {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] (f : fraction_map A K) {r : A} {s : ↥(non_zero_divisors A)} : localization_map.mk' f r s = coe_fn (localization_map.to_map f) r / coe_fn (localization_map.to_map f) ↑s := iff.mpr (localization_map.mk'_eq_iff_eq_mul f) (Eq.symm (div_mul_cancel (coe_fn (localization_map.to_map f) r) (fraction_map.to_map_ne_zero_of_mem_non_zero_divisors f s))) theorem is_unit_map_of_injective {A : Type u_4} [integral_domain A] {L : Type u_7} [field L] {g : A →+* L} (hg : function.injective ⇑g) (y : ↥(non_zero_divisors A)) : is_unit (coe_fn g ↑y) := is_unit.mk0 (coe_fn g ↑y) (map_ne_zero_of_mem_non_zero_divisors hg) /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ def lift {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] {L : Type u_7} [field L] (f : fraction_map A K) {g : A →+* L} (hg : function.injective ⇑g) : K →+* L := localization_map.lift f (is_unit_map_of_injective hg) /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] theorem lift_mk' {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] {L : Type u_7} [field L] (f : fraction_map A K) {g : A →+* L} (hg : function.injective ⇑g) (x : A) (y : ↥(non_zero_divisors A)) : coe_fn (lift f hg) (localization_map.mk' f x y) = coe_fn g x / coe_fn g ↑y := sorry /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ def map {A : Type u_4} [integral_domain A] {K : Type u_5} {B : Type u_6} [integral_domain B] [field K] {L : Type u_7} [field L] (f : fraction_map A K) (g : fraction_map B L) {j : A →+* B} (hj : function.injective ⇑j) : K →+* L := localization_map.map f sorry g /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ def field_equiv_of_ring_equiv {A : Type u_4} [integral_domain A] {K : Type u_5} {B : Type u_6} [integral_domain B] [field K] {L : Type u_7} [field L] (f : fraction_map A K) (g : fraction_map B L) (h : A ≃+* B) : K ≃+* L := localization_map.ring_equiv_of_ring_equiv f g h sorry /-- The cast from `int` to `rat` as a `fraction_map`. -/ def int.fraction_map : fraction_map ℤ ℚ := localization_map.mk coe sorry sorry sorry sorry sorry sorry sorry theorem integer_normalization_eq_zero_iff {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] (f : fraction_map A K) {p : polynomial (localization_map.codomain f)} : localization_map.integer_normalization f p = 0 ↔ p = 0 := sorry /-- A field is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ theorem comap_is_algebraic_iff {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] {L : Type u_7} [field L] (f : fraction_map A K) [algebra A L] [algebra (localization_map.codomain f) L] [is_scalar_tower A (localization_map.codomain f) L] : algebra.is_algebraic A L ↔ algebra.is_algebraic (localization_map.codomain f) L := sorry theorem exists_reduced_fraction {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) (x : localization_map.codomain φ) : ∃ (a : A), ∃ (b : ↥(non_zero_divisors A)), (∀ {d : A}, d ∣ a → d ∣ ↑b → is_unit d) ∧ localization_map.mk' φ a b = x := sorry /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ def num {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) (x : localization_map.codomain φ) : A := classical.some (exists_reduced_fraction φ x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ def denom {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) (x : localization_map.codomain φ) : ↥(non_zero_divisors A) := classical.some sorry theorem num_denom_reduced {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) (x : localization_map.codomain φ) {d : A} : d ∣ num φ x → d ∣ ↑(denom φ x) → is_unit d := and.left (classical.some_spec (classical.some_spec (exists_reduced_fraction φ x))) @[simp] theorem mk'_num_denom {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) (x : localization_map.codomain φ) : localization_map.mk' φ (num φ x) (denom φ x) = x := and.right (classical.some_spec (classical.some_spec (exists_reduced_fraction φ x))) theorem num_mul_denom_eq_num_iff_eq {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} {y : localization_map.codomain φ} : x * coe_fn (localization_map.to_map φ) ↑(denom φ y) = coe_fn (localization_map.to_map φ) (num φ y) ↔ x = y := sorry theorem num_mul_denom_eq_num_iff_eq' {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} {y : localization_map.codomain φ} : y * coe_fn (localization_map.to_map φ) ↑(denom φ x) = coe_fn (localization_map.to_map φ) (num φ x) ↔ x = y := sorry theorem num_mul_denom_eq_num_mul_denom_iff_eq {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} {y : localization_map.codomain φ} : num φ y * ↑(denom φ x) = num φ x * ↑(denom φ y) ↔ x = y := sorry theorem eq_zero_of_num_eq_zero {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} (h : num φ x = 0) : x = 0 := sorry theorem is_integer_of_is_unit_denom {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} (h : is_unit ↑(denom φ x)) : localization_map.is_integer φ x := sorry theorem is_unit_denom_of_num_eq_zero {A : Type u_4} [integral_domain A] {K : Type u_5} [field K] [unique_factorization_monoid A] (φ : fraction_map A K) {x : localization_map.codomain φ} (h : num φ x = 0) : is_unit ↑(denom φ x) := num_denom_reduced φ x (Eq.symm h ▸ dvd_zero ↑(denom φ x)) (dvd_refl ↑(denom φ x)) end fraction_map /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ def localization_algebra {R : Type u_1} [comm_ring R] {S : Type u_2} [comm_ring S] {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) : algebra Rₘ Sₘ := ring_hom.to_algebra (localization_map.map f sorry g) theorem algebra_map_mk' {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) (r : R) (m : ↥M) : coe_fn (algebra_map Rₘ Sₘ) (localization_map.mk' f r m) = localization_map.mk' g (coe_fn (algebra_map R S) r) { val := coe_fn (algebra_map R S) ↑m, property := algebra.mem_algebra_map_submonoid_of_mem m } := localization_map.map_mk' f (localization_algebra._proof_1 M) r m /-- Injectivity of a map descends to the map induced on localizations. -/ theorem map_injective_of_injective {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] {R : Type u_1} {S : Type u_2} [comm_ring R] [comm_ring S] (ϕ : R →+* S) (hϕ : function.injective ⇑ϕ) (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (submonoid.map (↑ϕ) M) Sₘ) (hM : submonoid.map (↑ϕ) M ≤ non_zero_divisors S) : function.injective ⇑(localization_map.map f (submonoid.mem_map_of_mem M ↑ϕ) g) := sorry /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization. -/ theorem localization_algebra_injective {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) (hRS : function.injective ⇑(algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≤ non_zero_divisors S) : function.injective ⇑(algebra_map Rₘ Sₘ) := map_injective_of_injective (algebra_map R S) hRS M f g hM theorem ring_hom.is_integral_elem_localization_at_leading_coeff {R : Type u_1} {S : Type u_2} [comm_ring R] [comm_ring S] (f : R →+* S) (x : S) (p : polynomial R) (hf : polynomial.eval₂ f x p = 0) (M : submonoid R) (hM : polynomial.leading_coeff p ∈ M) {Rₘ : Type u_3} {Sₘ : Type u_4} [comm_ring Rₘ] [comm_ring Sₘ] (ϕ : localization_map M Rₘ) (ϕ' : localization_map (submonoid.map (↑f) M) Sₘ) : ring_hom.is_integral_elem (localization_map.map ϕ (submonoid.mem_map_of_mem M ↑f) ϕ') (coe_fn (localization_map.to_map ϕ') x) := sorry /-- Given a particular witness to an element being algebraic over an algebra `R → S`, We can localize to a submonoid containing the leading coefficient to make it integral. Explicitly, the map between the localizations will be an integral ring morphism -/ theorem is_integral_localization_at_leading_coeff {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) {x : S} (p : polynomial R) (hp : coe_fn (polynomial.aeval x) p = 0) (hM : polynomial.leading_coeff p ∈ M) : ring_hom.is_integral_elem (localization_map.map f algebra.mem_algebra_map_submonoid_of_mem g) (coe_fn (localization_map.to_map g) x) := ring_hom.is_integral_elem_localization_at_leading_coeff (algebra_map R S) x p hp M hM f g /-- If `R → S` is an integral extension, `M` is a submonoid of `R`, `Rₘ` is the localization of `R` at `M`, and `Sₘ` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rₘ → Sₘ` is also an integral extension -/ theorem is_integral_localization {R : Type u_1} [comm_ring R] {M : submonoid R} {S : Type u_2} [comm_ring S] {Rₘ : Type u_6} {Sₘ : Type u_7} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) (H : algebra.is_integral R S) : ring_hom.is_integral (localization_map.map f algebra.mem_algebra_map_submonoid_of_mem g) := sorry theorem is_integral_localization' {R : Type u_1} {S : Type u_2} [comm_ring R] [comm_ring S] {f : R →+* S} (hf : ring_hom.is_integral f) (M : submonoid R) : ring_hom.is_integral (localization_map.map (localization.of M) (submonoid.mem_map_of_mem M ↑f) (localization.of (submonoid.map (↑f) M))) := is_integral_localization (localization.of M) (localization.of (submonoid.map (↑f) M)) hf namespace integral_closure /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_algebraic {A : Type u_4} [integral_domain A] {L : Type u_6} [field L] [algebra A L] (alg : algebra.is_algebraic A L) (inj : ∀ (x : A), coe_fn (algebra_map A L) x = 0 → x = 0) : fraction_map (↥(integral_closure A L)) L := ring_hom.to_localization_map (algebra_map (↥(integral_closure A L)) L) sorry sorry sorry /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_finite_extension {A : Type u_4} [integral_domain A] {K : Type u_5} (L : Type u_6) [field K] [field L] (f : fraction_map A K) [algebra A L] [algebra (localization_map.codomain f) L] [is_scalar_tower A (localization_map.codomain f) L] [finite_dimensional (localization_map.codomain f) L] : fraction_map (↥(integral_closure A L)) L := fraction_map_of_algebraic sorry sorry end integral_closure /-- The fraction field of an integral domain as a quotient type. -/ def fraction_ring (A : Type u_4) [integral_domain A] := localization (non_zero_divisors A) namespace fraction_ring /-- Natural hom sending `x : A`, `A` an integral domain, to the equivalence class of `(x, 1)` in the field of fractions of `A`. -/ def of (A : Type u_4) [integral_domain A] : fraction_map A (localization (non_zero_divisors A)) := localization.of (non_zero_divisors A) protected instance field {A : Type u_4} [integral_domain A] : field (fraction_ring A) := fraction_map.to_field (of A) @[simp] theorem mk_eq_div {A : Type u_4} [integral_domain A] {r : A} {s : ↥(non_zero_divisors A)} : localization.mk r s = coe_fn (localization_map.to_map (of A)) r / coe_fn (localization_map.to_map (of A)) ↑s := sorry /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ def alg_equiv_of_quotient {A : Type u_4} [integral_domain A] {K : Type u_1} [field K] (f : fraction_map A K) : alg_equiv A (fraction_ring A) (localization_map.codomain f) := localization.alg_equiv_of_quotient f protected instance algebra {A : Type u_4} [integral_domain A] : algebra A (fraction_ring A) := ring_hom.to_algebra (localization_map.to_map (of A))
f993c7f320a9c913500e570ebd0368ba4f2d7352
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/calculus/fderiv_symmetric.lean
a05a77aae3bf498ec008813bec2ea0005f7fd9b5
[ "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
19,496
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 analysis.calculus.deriv import analysis.calculus.mean_value import analysis.convex.topology /-! # Symmetry of the second derivative We show that, over the reals, the second derivative is symmetric. The most precise result is `convex.second_derivative_within_at_symmetric`. It asserts that, if a function is differentiable inside a convex set `s` with nonempty interior, and has a second derivative within `s` at a point `x`, then this second derivative at `x` is symmetric. Note that this result does not require continuity of the first derivative. The following particular cases of this statement are especially relevant: `second_derivative_symmetric_of_eventually` asserts that, if a function is differentiable on a neighborhood of `x`, and has a second derivative at `x`, then this second derivative is symmetric. `second_derivative_symmetric` asserts that, if a function is differentiable, and has a second derivative at `x`, then this second derivative is symmetric. ## Implementation note For the proof, we obtain an asymptotic expansion to order two of `f (x + v + w) - f (x + v)`, by using the mean value inequality applied to a suitable function along the segment `[x + v, x + v + w]`. This expansion involves `f'' ⬝ w` as we move along a segment directed by `w` (see `convex.taylor_approx_two_segment`). Consider the alternate sum `f (x + v + w) + f x - f (x + v) - f (x + w)`, corresponding to the values of `f` along a rectangle based at `x` with sides `v` and `w`. One can write it using the two sides directed by `w`, as `(f (x + v + w) - f (x + v)) - (f (x + w) - f x)`. Together with the previous asymptotic expansion, one deduces that it equals `f'' v w + o(1)` when `v, w` tends to `0`. Exchanging the roles of `v` and `w`, one instead gets an asymptotic expansion `f'' w v`, from which the equality `f'' v w = f'' w v` follows. In our most general statement, we only assume that `f` is differentiable inside a convex set `s`, so a few modifications have to be made. Since we don't assume continuity of `f` at `x`, we consider instead the rectangle based at `x + v + w` with sides `v` and `w`, in `convex.is_o_alternate_sum_square`, but the argument is essentially the same. It only works when `v` and `w` both point towards the interior of `s`, to make sure that all the sides of the rectangle are contained in `s` by convexity. The general case follows by linearity, though. -/ open asymptotics set open_locale topological_space variables {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {s : set E} (s_conv : convex s) {f : E → F} {f' : E → (E →L[ℝ] F)} {f'' : E →L[ℝ] (E →L[ℝ] F)} (hf : ∀ x ∈ interior s, has_fderiv_at f (f' x) x) {x : E} (xs : x ∈ s) (hx : has_fderiv_within_at f' f'' (interior s) x) include s_conv xs hx hf /-- Assume that `f` is differentiable inside a convex set `s`, and that its derivative `f'` is differentiable at a point `x`. Then, given two vectors `v` and `w` pointing inside `s`, one can Taylor-expand to order two the function `f` on the segment `[x + h v, x + h (v + w)]`, giving a bilinear estimate for `f (x + hv + hw) - f (x + hv)` in terms of `f' w` and of `f'' ⬝ w`, up to `o(h^2)`. This is a technical statement used to show that the second derivative is symmetric. -/ lemma convex.taylor_approx_two_segment {v w : E} (hv : x + v ∈ interior s) (hw : x + v + w ∈ interior s) : is_o (λ (h : ℝ), f (x + h • v + h • w) - f (x + h • v) - h • f' x w - h^2 • f'' v w - (h^2/2) • f'' w w) (λ h, h^2) (𝓝[Ioi (0 : ℝ)] 0) := begin -- it suffices to check that the expression is bounded by `ε * ((∥v∥ + ∥w∥) * ∥w∥) * h^2` for -- small enough `h`, for any positive `ε`. apply is_o.trans_is_O (is_o_iff.2 (λ ε εpos, _)) (is_O_const_mul_self ((∥v∥ + ∥w∥) * ∥w∥) _ _), -- consider a ball of radius `δ` around `x` in which the Taylor approximation for `f''` is -- good up to `δ`. rw [has_fderiv_within_at, has_fderiv_at_filter, is_o_iff] at hx, rcases metric.mem_nhds_within_iff.1 (hx εpos) with ⟨δ, δpos, sδ⟩, have E1 : ∀ᶠ h in 𝓝[Ioi (0:ℝ)] 0, h * (∥v∥ + ∥w∥) < δ, { have : filter.tendsto (λ h, h * (∥v∥ + ∥w∥)) (𝓝[Ioi (0:ℝ)] 0) (𝓝 (0 * (∥v∥ + ∥w∥))) := (continuous_id.mul continuous_const).continuous_within_at, apply (tendsto_order.1 this).2 δ, simpa using δpos }, have E2 : ∀ᶠ h in 𝓝[Ioi (0:ℝ)] 0, (h : ℝ) < 1 := mem_nhds_within_Ioi_iff_exists_Ioo_subset.2 ⟨(1 : ℝ), by simp, λ x hx, hx.2⟩, filter_upwards [E1, E2, self_mem_nhds_within], -- we consider `h` small enough that all points under consideration belong to this ball, -- and also with `0 < h < 1`. assume h hδ h_lt_1 hpos, replace hpos : 0 < h := hpos, have xt_mem : ∀ t ∈ Icc (0 : ℝ) 1, x + h • v + (t * h) • w ∈ interior s, { assume t ht, have : x + h • v ∈ interior s := s_conv.add_smul_mem_interior xs hv ⟨hpos, h_lt_1.le⟩, rw [← smul_smul], apply s_conv.interior.add_smul_mem this _ ht, rw add_assoc at hw, convert s_conv.add_smul_mem_interior xs hw ⟨hpos, h_lt_1.le⟩ using 1, simp only [add_assoc, smul_add] }, -- define a function `g` on `[0,1]` (identified with `[v, v + w]`) such that `g 1 - g 0` is the -- quantity to be estimated. We will check that its derivative is given by an explicit -- expression `g'`, that we can bound. Then the desired bound for `g 1 - g 0` follows from the -- mean value inequality. let g := λ t, f (x + h • v + (t * h) • w) - (t * h) • f' x w - (t * h^2) • f'' v w - ((t * h)^2/2) • f'' w w, set g' := λ t, f' (x + h • v + (t * h) • w) (h • w) - h • f' x w - h^2 • f'' v w - (t * h^2) • f'' w w with hg', -- check that `g'` is the derivative of `g`, by a straightforward computation have g_deriv : ∀ t ∈ Icc (0 : ℝ) 1, has_deriv_within_at g (g' t) (Icc 0 1) t, { assume t ht, apply_rules [has_deriv_within_at.sub, has_deriv_within_at.add], { refine (hf _ _).comp_has_deriv_within_at _ _, { exact xt_mem t ht }, apply has_deriv_at.has_deriv_within_at, suffices : has_deriv_at (λ u, x + h • v + (u * h) • w) (0 + 0 + (1 * h) • w) t, by simpa only [one_mul, zero_add], apply_rules [has_deriv_at.add, has_deriv_at_const, has_deriv_at.smul_const, has_deriv_at_id'] }, { suffices : has_deriv_within_at (λ u, (u * h) • f' x w) ((1 * h) • f' x w) (Icc 0 1) t, by simpa only [one_mul], apply_rules [has_deriv_at.has_deriv_within_at, has_deriv_at.smul_const, has_deriv_at_id'] }, { suffices : has_deriv_within_at (λ u, (u * h ^ 2) • f'' v w) ((1 * h^2) • f'' v w) (Icc 0 1) t, by simpa only [one_mul], apply_rules [has_deriv_at.has_deriv_within_at, has_deriv_at.smul_const, has_deriv_at_id'] }, { suffices H : has_deriv_within_at (λ u, ((u * h) ^ 2 / 2) • f'' w w) (((((2 : ℕ) : ℝ) * (t * h) ^ (2 - 1) * (1 * h))/2) • f'' w w) (Icc 0 1) t, { convert H using 2, simp only [one_mul, nat.cast_bit0, pow_one, nat.cast_one], ring }, apply_rules [has_deriv_at.has_deriv_within_at, has_deriv_at.smul_const, has_deriv_at_id', has_deriv_at.pow] } }, -- check that `g'` is uniformly bounded, with a suitable bound `ε * ((∥v∥ + ∥w∥) * ∥w∥) * h^2`. have g'_bound : ∀ t ∈ Ico (0 : ℝ) 1, ∥g' t∥ ≤ ε * ((∥v∥ + ∥w∥) * ∥w∥) * h^2, { assume t ht, have I : ∥h • v + (t * h) • w∥ ≤ h * (∥v∥ + ∥w∥) := calc ∥h • v + (t * h) • w∥ ≤ ∥h • v∥ + ∥(t * h) • w∥ : norm_add_le _ _ ... = h * ∥v∥ + t * (h * ∥w∥) : by simp only [norm_smul, real.norm_eq_abs, hpos.le, abs_of_nonneg, abs_mul, ht.left, mul_assoc] ... ≤ h * ∥v∥ + 1 * (h * ∥w∥) : add_le_add (le_refl _) (mul_le_mul_of_nonneg_right ht.2.le (mul_nonneg hpos.le (norm_nonneg _))) ... = h * (∥v∥ + ∥w∥) : by ring, calc ∥g' t∥ = ∥(f' (x + h • v + (t * h) • w) - f' x - f'' (h • v + (t * h) • w)) (h • w)∥ : begin rw hg', have : h * (t * h) = t * (h * h), by ring, simp only [continuous_linear_map.coe_sub', continuous_linear_map.map_add, pow_two, continuous_linear_map.add_apply, pi.smul_apply, smul_sub, smul_add, smul_smul, ← sub_sub, continuous_linear_map.coe_smul', pi.sub_apply, continuous_linear_map.map_smul, this] end ... ≤ ∥f' (x + h • v + (t * h) • w) - f' x - f'' (h • v + (t * h) • w)∥ * ∥h • w∥ : continuous_linear_map.le_op_norm _ _ ... ≤ (ε * ∥h • v + (t * h) • w∥) * (∥h • w∥) : begin apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), have H : x + h • v + (t * h) • w ∈ metric.ball x δ ∩ interior s, { refine ⟨_, xt_mem t ⟨ht.1, ht.2.le⟩⟩, rw [add_assoc, add_mem_ball_iff_norm], exact I.trans_lt hδ }, have := sδ H, simp only [mem_set_of_eq] at this, convert this; abel end ... ≤ (ε * (∥h • v∥ + ∥h • w∥)) * (∥h • w∥) : begin apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), apply mul_le_mul_of_nonneg_left _ (εpos.le), apply (norm_add_le _ _).trans, refine add_le_add (le_refl _) _, simp only [norm_smul, real.norm_eq_abs, abs_mul, abs_of_nonneg, ht.1, hpos.le, mul_assoc], exact mul_le_of_le_one_left (mul_nonneg hpos.le (norm_nonneg _)) ht.2.le, end ... = ε * ((∥v∥ + ∥w∥) * ∥w∥) * h^2 : by { simp only [norm_smul, real.norm_eq_abs, abs_mul, abs_of_nonneg, hpos.le], ring } }, -- conclude using the mean value inequality have I : ∥g 1 - g 0∥ ≤ ε * ((∥v∥ + ∥w∥) * ∥w∥) * h^2, by simpa using norm_image_sub_le_of_norm_deriv_le_segment' g_deriv g'_bound 1 (right_mem_Icc.2 zero_le_one), convert I using 1, { congr' 1, dsimp only [g], simp only [nat.one_ne_zero, add_zero, one_mul, zero_div, zero_mul, sub_zero, zero_smul, ne.def, not_false_iff, bit0_eq_zero, zero_pow'], abel }, { simp only [real.norm_eq_abs, abs_mul, add_nonneg (norm_nonneg v) (norm_nonneg w), abs_of_nonneg, mul_assoc, pow_bit0_abs, norm_nonneg, abs_pow] } end /-- One can get `f'' v w` as the limit of `h ^ (-2)` times the alternate sum of the values of `f` along the vertices of a quadrilateral with sides `h v` and `h w` based at `x`. In a setting where `f` is not guaranteed to be continuous at `f`, we can still get this if we use a quadrilateral based at `h v + h w`. -/ lemma convex.is_o_alternate_sum_square {v w : E} (h4v : x + (4 : ℝ) • v ∈ interior s) (h4w : x + (4 : ℝ) • w ∈ interior s) : is_o (λ (h : ℝ), f (x + h • (2 • v + 2 • w)) + f (x + h • (v + w)) - f (x + h • (2 • v + w)) - f (x + h • (v + 2 • w)) - h^2 • f'' v w) (λ h, h^2) (𝓝[Ioi (0 : ℝ)] 0) := begin have A : (1 : ℝ)/2 ∈ Ioc (0 : ℝ) 1 := ⟨by norm_num, by norm_num⟩, have B : (1 : ℝ)/2 ∈ Icc (0 : ℝ) 1 := ⟨by norm_num, by norm_num⟩, have C : ∀ (w : E), (2 : ℝ) • w = 2 • w := λ w, by simp only [two_smul], have h2v2w : x + (2 : ℝ) • v + (2 : ℝ) • w ∈ interior s, { convert s_conv.interior.add_smul_sub_mem h4v h4w B using 1, simp only [smul_sub, smul_smul, one_div, add_sub_add_left_eq_sub, mul_add, add_smul], norm_num, simp only [show (4 : ℝ) = (2 : ℝ) + (2 : ℝ), by norm_num, add_smul], abel }, have h2vww : x + (2 • v + w) + w ∈ interior s, { convert h2v2w using 1, simp only [two_smul], abel }, have h2v : x + (2 : ℝ) • v ∈ interior s, { convert s_conv.add_smul_sub_mem_interior xs h4v A using 1, simp only [smul_smul, one_div, add_sub_cancel', add_right_inj], norm_num }, have h2w : x + (2 : ℝ) • w ∈ interior s, { convert s_conv.add_smul_sub_mem_interior xs h4w A using 1, simp only [smul_smul, one_div, add_sub_cancel', add_right_inj], norm_num }, have hvw : x + (v + w) ∈ interior s, { convert s_conv.add_smul_sub_mem_interior xs h2v2w A using 1, simp only [smul_smul, one_div, add_sub_cancel', add_right_inj, smul_add, smul_sub], norm_num, abel }, have h2vw : x + (2 • v + w) ∈ interior s, { convert s_conv.interior.add_smul_sub_mem h2v h2v2w B using 1, simp only [smul_add, smul_sub, smul_smul, ← C], norm_num, abel }, have hvww : x + (v + w) + w ∈ interior s, { convert s_conv.interior.add_smul_sub_mem h2w h2v2w B using 1, simp only [one_div, add_sub_cancel', inv_smul_smul', add_sub_add_right_eq_sub, ne.def, not_false_iff, bit0_eq_zero, one_ne_zero], rw two_smul, abel }, have TA1 := s_conv.taylor_approx_two_segment hf xs hx h2vw h2vww, have TA2 := s_conv.taylor_approx_two_segment hf xs hx hvw hvww, convert TA1.sub TA2, ext h, simp only [two_smul, smul_add, ← add_assoc, continuous_linear_map.map_add, continuous_linear_map.add_apply, pi.smul_apply, continuous_linear_map.coe_smul', continuous_linear_map.map_smul], abel, end /-- Assume that `f` is differentiable inside a convex set `s`, and that its derivative `f'` is differentiable at a point `x`. Then, given two vectors `v` and `w` pointing inside `s`, one has `f'' v w = f'' w v`. Superseded by `convex.second_derivative_within_at_symmetric`, which removes the assumption that `v` and `w` point inside `s`. -/ lemma convex.second_derivative_within_at_symmetric_of_mem_interior {v w : E} (h4v : x + (4 : ℝ) • v ∈ interior s) (h4w : x + (4 : ℝ) • w ∈ interior s) : f'' w v = f'' v w := begin have A : is_o (λ (h : ℝ), h^2 • (f'' w v- f'' v w)) (λ h, h^2) (𝓝[Ioi (0 : ℝ)] 0), { convert (s_conv.is_o_alternate_sum_square hf xs hx h4v h4w).sub (s_conv.is_o_alternate_sum_square hf xs hx h4w h4v), ext h, simp only [add_comm, smul_add, smul_sub], abel }, have B : is_o (λ (h : ℝ), f'' w v - f'' v w) (λ h, (1 : ℝ)) (𝓝[Ioi (0 : ℝ)] 0), { have : is_O (λ (h : ℝ), 1/h^2) (λ h, 1/h^2) (𝓝[Ioi (0 : ℝ)] 0) := is_O_refl _ _, have C := this.smul_is_o A, apply C.congr' _ _, { filter_upwards [self_mem_nhds_within], assume h hpos, rw [← one_smul ℝ (f'' w v - f'' v w), smul_smul, smul_smul], congr' 1, field_simp [has_lt.lt.ne' hpos] }, { filter_upwards [self_mem_nhds_within], assume h hpos, field_simp [has_lt.lt.ne' hpos, has_scalar.smul] } }, simpa only [sub_eq_zero] using (is_o_const_const_iff (@one_ne_zero ℝ _ _)).1 B, end omit s_conv xs hx hf /-- If a function is differentiable inside a convex set with nonempty interior, and has a second derivative at a point of this convex set, then this second derivative is symmetric. -/ theorem convex.second_derivative_within_at_symmetric {s : set E} (s_conv : convex s) (hne : (interior s).nonempty) {f : E → F} {f' : E → (E →L[ℝ] F)} {f'' : E →L[ℝ] (E →L[ℝ] F)} (hf : ∀ x ∈ interior s, has_fderiv_at f (f' x) x) {x : E} (xs : x ∈ s) (hx : has_fderiv_within_at f' f'' (interior s) x) (v w : E) : f'' v w = f'' w v := begin /- we work around a point `x + 4 z` in the interior of `s`. For any vector `m`, then `x + 4 (z + t m)` also belongs to the interior of `s` for small enough `t`. This means that we will be able to apply `second_derivative_within_at_symmetric_of_mem_interior` to show that `f''` is symmetric, after cancelling all the contributions due to `z`. -/ rcases hne with ⟨y, hy⟩, obtain ⟨z, hz⟩ : ∃ z, z = ((1:ℝ) / 4) • (y - x) := ⟨((1:ℝ) / 4) • (y - x), rfl⟩, have A : ∀ (m : E), filter.tendsto (λ (t : ℝ), x + (4 : ℝ) • (z + t • m)) (𝓝 0) (𝓝 y), { assume m, have : x + (4 : ℝ) • (z + (0 : ℝ) • m) = y, by simp [hz], rw ← this, refine tendsto_const_nhds.add _, refine tendsto_const_nhds.smul _, refine tendsto_const_nhds.add _, exact continuous_at_id.smul continuous_at_const }, have B : ∀ (m : E), ∀ᶠ t in 𝓝[Ioi (0 : ℝ)] (0 : ℝ), x + (4 : ℝ) • (z + t • m) ∈ interior s, { assume m, apply nhds_within_le_nhds, apply A m, rw [mem_interior_iff_mem_nhds] at hy, exact interior_mem_nhds.2 hy }, -- we choose `t m > 0` such that `x + 4 (z + (t m) m)` belongs to the interior of `s`, for any -- vector `m`. choose t ts tpos using λ m, ((B m).and self_mem_nhds_within).exists, -- applying `second_derivative_within_at_symmetric_of_mem_interior` to the vectors `z` -- and `z + (t m) m`, we deduce that `f'' m z = f'' z m` for all `m`. have C : ∀ (m : E), f'' m z = f'' z m, { assume m, have : f'' (z + t m • m) (z + t 0 • 0) = f'' (z + t 0 • 0) (z + t m • m) := s_conv.second_derivative_within_at_symmetric_of_mem_interior hf xs hx (ts 0) (ts m), simp only [continuous_linear_map.map_add, continuous_linear_map.map_smul, add_right_inj, continuous_linear_map.add_apply, pi.smul_apply, continuous_linear_map.coe_smul', add_zero, continuous_linear_map.zero_apply, smul_zero, continuous_linear_map.map_zero] at this, exact smul_left_injective F (tpos m).ne' this }, -- applying `second_derivative_within_at_symmetric_of_mem_interior` to the vectors `z + (t v) v` -- and `z + (t w) w`, we deduce that `f'' v w = f'' w v`. Cross terms involving `z` can be -- eliminated thanks to the fact proved above that `f'' m z = f'' z m`. have : f'' (z + t v • v) (z + t w • w) = f'' (z + t w • w) (z + t v • v) := s_conv.second_derivative_within_at_symmetric_of_mem_interior hf xs hx (ts w) (ts v), simp only [continuous_linear_map.map_add, continuous_linear_map.map_smul, smul_add, smul_smul, continuous_linear_map.add_apply, pi.smul_apply, continuous_linear_map.coe_smul', C] at this, rw ← sub_eq_zero at this, abel at this, simp only [one_gsmul, neg_smul, sub_eq_zero, mul_comm, ← sub_eq_add_neg] at this, apply smul_left_injective F _ this, simp [(tpos v).ne', (tpos w).ne'] end /-- If a function is differentiable around `x`, and has two derivatives at `x`, then the second derivative is symmetric. -/ theorem second_derivative_symmetric_of_eventually {f : E → F} {f' : E → (E →L[ℝ] F)} {f'' : E →L[ℝ] (E →L[ℝ] F)} (hf : ∀ᶠ y in 𝓝 x, has_fderiv_at f (f' y) y) (hx : has_fderiv_at f' f'' x) (v w : E) : f'' v w = f'' w v := begin rcases metric.mem_nhds_iff.1 hf with ⟨ε, εpos, hε⟩, have A : (interior (metric.ball x ε)).nonempty, by { rw metric.is_open_ball.interior_eq, exact metric.nonempty_ball εpos }, exact convex.second_derivative_within_at_symmetric (convex_ball x ε) A (λ y hy, hε (interior_subset hy)) (metric.mem_ball_self εpos) hx.has_fderiv_within_at v w, end /-- If a function is differentiable, and has two derivatives at `x`, then the second derivative is symmetric. -/ theorem second_derivative_symmetric {f : E → F} {f' : E → (E →L[ℝ] F)} {f'' : E →L[ℝ] (E →L[ℝ] F)} (hf : ∀ y, has_fderiv_at f (f' y) y) (hx : has_fderiv_at f' f'' x) (v w : E) : f'' v w = f'' w v := second_derivative_symmetric_of_eventually (filter.eventually_of_forall hf) hx v w
d56cbf56625298685b10eb906aabdeeffe00606e
c777c32c8e484e195053731103c5e52af26a25d1
/src/category_theory/monoidal/internal/Module.lean
ba73dec00fd2aac68071f670f30668a0f6f6af39
[ "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
5,945
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Module.monoidal import algebra.category.Algebra.basic import category_theory.monoidal.Mon_ /-! # `Mon_ (Module R) ≌ Algebra R` The category of internal monoid objects in `Module R` is equivalent to the category of "native" bundled `R`-algebras. Moreover, this equivalence is compatible with the forgetful functors to `Module R`. -/ universes v u open category_theory open linear_map open_locale tensor_product local attribute [ext] tensor_product.ext namespace Module variables {R : Type u} [comm_ring R] namespace Mon_Module_equivalence_Algebra @[simps] instance (A : Mon_ (Module.{u} R)) : ring A.X := { one := A.one (1 : R), mul := λ x y, A.mul (x ⊗ₜ y), one_mul := λ x, by { convert linear_map.congr_fun A.one_mul ((1 : R) ⊗ₜ x), simp, }, mul_one := λ x, by { convert linear_map.congr_fun A.mul_one (x ⊗ₜ (1 : R)), simp, }, mul_assoc := λ x y z, by convert linear_map.congr_fun A.mul_assoc ((x ⊗ₜ y) ⊗ₜ z), left_distrib := λ x y z, begin convert A.mul.map_add (x ⊗ₜ y) (x ⊗ₜ z), rw ←tensor_product.tmul_add, refl, end, right_distrib := λ x y z, begin convert A.mul.map_add (x ⊗ₜ z) (y ⊗ₜ z), rw ←tensor_product.add_tmul, refl, end, ..(by apply_instance : add_comm_group A.X) } instance (A : Mon_ (Module.{u} R)) : algebra R A.X := { map_zero' := A.one.map_zero, map_one' := rfl, map_mul' := λ x y, begin have h := linear_map.congr_fun A.one_mul.symm (x ⊗ₜ (A.one y)), rwa [monoidal_category.left_unitor_hom_apply, ←A.one.map_smul] at h, end, commutes' := λ r a, begin dsimp, have h₁ := linear_map.congr_fun A.one_mul (r ⊗ₜ a), have h₂ := linear_map.congr_fun A.mul_one (a ⊗ₜ r), exact h₁.trans h₂.symm, end, smul_def' := λ r a, (linear_map.congr_fun A.one_mul (r ⊗ₜ a)).symm, ..A.one } @[simp] lemma algebra_map (A : Mon_ (Module.{u} R)) (r : R) : algebra_map R A.X r = A.one r := rfl /-- Converting a monoid object in `Module R` to a bundled algebra. -/ @[simps] def functor : Mon_ (Module.{u} R) ⥤ Algebra R := { obj := λ A, Algebra.of R A.X, map := λ A B f, { to_fun := f.hom, map_one' := linear_map.congr_fun f.one_hom (1 : R), map_mul' := λ x y, linear_map.congr_fun f.mul_hom (x ⊗ₜ y), commutes' := λ r, linear_map.congr_fun f.one_hom r, ..(f.hom.to_add_monoid_hom) }, }. /-- Converting a bundled algebra to a monoid object in `Module R`. -/ @[simps] def inverse_obj (A : Algebra.{u} R) : Mon_ (Module.{u} R) := { X := Module.of R A, one := algebra.linear_map R A, mul := linear_map.mul' R A, one_mul' := begin ext x, dsimp only [Algebra.id_apply, tensor_product.mk_apply, algebra.linear_map_apply, linear_map.compr₂_apply, function.comp_app, ring_hom.map_one, Module.monoidal_category.hom_apply, Algebra.coe_comp, Module.monoidal_category.left_unitor_hom_apply], rw [linear_map.mul'_apply, monoidal_category.left_unitor_hom_apply, ← algebra.smul_def] end, mul_one' := begin ext x, dsimp only [Algebra.id_apply, tensor_product.mk_apply, algebra.linear_map_apply, linear_map.compr₂_apply, function.comp_app, Module.monoidal_category.hom_apply, Algebra.coe_comp], rw [linear_map.mul'_apply, Module.monoidal_category.right_unitor_hom_apply, ← algebra.commutes, ← algebra.smul_def] end, mul_assoc' := begin ext x y z, dsimp only [Algebra.id_apply, tensor_product.mk_apply, linear_map.compr₂_apply, function.comp_app, Module.monoidal_category.hom_apply, Algebra.coe_comp, monoidal_category.associator_hom_apply], simp only [linear_map.mul'_apply, mul_assoc] end } /-- Converting a bundled algebra to a monoid object in `Module R`. -/ @[simps] def inverse : Algebra.{u} R ⥤ Mon_ (Module.{u} R) := { obj := inverse_obj, map := λ A B f, { hom := f.to_linear_map, one_hom' := linear_map.ext f.commutes, mul_hom' := tensor_product.ext $ linear_map.ext₂ $ map_mul f, } } end Mon_Module_equivalence_Algebra open Mon_Module_equivalence_Algebra /-- The category of internal monoid objects in `Module R` is equivalent to the category of "native" bundled `R`-algebras. -/ def Mon_Module_equivalence_Algebra : Mon_ (Module.{u} R) ≌ Algebra R := { functor := functor, inverse := inverse, unit_iso := nat_iso.of_components (λ A, { hom := { hom := { to_fun := id, map_add' := λ x y, rfl, map_smul' := λ r a, rfl, }, mul_hom' := by { ext, dsimp at *, refl } }, inv := { hom := { to_fun := id, map_add' := λ x y, rfl, map_smul' := λ r a, rfl, }, mul_hom' := by { ext, dsimp at *, refl } } }) (by tidy), counit_iso := nat_iso.of_components (λ A, { hom := { to_fun := id, map_zero' := rfl, map_add' := λ x y, rfl, map_one' := (algebra_map R A).map_one, map_mul' := λ x y, (@linear_map.mul'_apply R _ _ _ _ _ _ x y), commutes' := λ r, rfl, }, inv := { to_fun := id, map_zero' := rfl, map_add' := λ x y, rfl, map_one' := (algebra_map R A).map_one.symm, map_mul' := λ x y, (@linear_map.mul'_apply R _ _ _ _ _ _ x y).symm, commutes' := λ r, rfl } }) (by { intros, refl }), }. /-- The equivalence `Mon_ (Module R) ≌ Algebra R` is naturally compatible with the forgetful functors to `Module R`. -/ def Mon_Module_equivalence_Algebra_forget : Mon_Module_equivalence_Algebra.functor ⋙ forget₂ (Algebra.{u} R) (Module.{u} R) ≅ Mon_.forget (Module.{u} R):= nat_iso.of_components (λ A, { hom := { to_fun := id, map_add' := λ x y, rfl, map_smul' := λ c x, rfl }, inv := { to_fun := id, map_add' := λ x y, rfl, map_smul' := λ c x, rfl }, }) (by tidy) end Module
e5cdad63ad234aad38bc9bf29461a92a7d0e5949
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/AbstractBuildingBlocks.lean
753381efc018da6507919214c3c8e0044abed6b2
[ "MIT" ]
permissive
SReichelt/lean4-experiments
3e56830c8b2fbe3814eda071c48e3c8810d254a8
ff55357a01a34a91bf670d712637480089085ee4
refs/heads/main
1,683,977,454,907
1,622,991,121,000
1,622,991,121,000
340,765,677
2
0
null
null
null
null
UTF-8
Lean
false
false
13,535
lean
-- An abstract formalization of "isomorphism is equality up to relabeling" -- ------------------------------------------------------------------------- -- -- See `README.md` for more info. -- -- In this file, we define some basic building blocks which are closely related to the cases mentioned in -- `README.md`. import Structure.Basic import Structure.Forgetfulness import Structure.UniverseFunctor import Structure.AbstractPiSigma import mathlib4_experiments.Data.Equiv open Morphisms open Structure open StructureFunctor open Forgetfulness open PiSigma open PiSigmaEquivalences set_option autoBoundImplicitLocal false universes u v namespace BuildingBlocks -- If we think of `SigmaEquiv` as defining our concept of isomorphism, we can actually _prove_ that -- isomorphisms between particular structures match their concrete definitions. This even works for -- structures that lack an obvious concept of "morphism". -- -- We will call `SigmaExpr.fst` the "type" and `SigmaExpr.snd` the "instance" (that depends on the type). -- Correspondingly, `SigmaEquiv.fst` is the "type equivalence", and `SigmaEquiv.snd` is the "instance -- equivalence". -- We would like to state our theorems not about the entire `SigmaEquiv` but about how the instance -- equivalence depends on the type equivalence. -- Whenever we do want to reason about the entire `SigmaEquiv`, `isoEquiv` will provide the desired -- result. Although `isoEquiv` is a _definition_ (of an equivalence), it can also be read as the theorem -- that the two types are equivalent. def HasIso {F : StructureDependency} (a b : SigmaExpr F) (I : ∀ e : a.fst ≃ b.fst, Prop) := ∀ e : a.fst ≃ b.fst, a.snd ≈[congrArg F.F e] b.snd ↔ I e structure InstanceIsoCriterion {F : StructureDependency} (a b : SigmaExpr F) where {I : ∀ e : a.fst ≃ b.fst, Prop} (h : HasIso a b I) def IsoCriterion (F : StructureDependency) := ∀ a b : SigmaExpr F, InstanceIsoCriterion a b def isoEquiv {F : StructureDependency} {a b : SigmaExpr F} (c : InstanceIsoCriterion a b) : IsType.type (a ≃ b) ≃ Σ' e : a.fst ≃ b.fst, c.I e := { toFun := λ ⟨h₁, h₂⟩ => ⟨h₁, (c.h h₁).mp h₂⟩, invFun := λ ⟨h₁, h₂⟩ => ⟨h₁, (c.h h₁).mpr h₂⟩, leftInv := λ ⟨h₁, h₂⟩ => rfl, rightInv := λ ⟨h₁, h₂⟩ => rfl } -- Degenerate case: If the instance does not actually depend on the type, an isomorphism is just an -- equivalence between the types together with an equivalence between the instances. def independentPairDependency (S T : Structure) := StructureDependency.constDep S T def IndependentPair (S T : Structure) := SigmaExpr (independentPairDependency S T) namespace IndependentPair variable (S T : Structure) -- This degenerate case is actually equivalent to `PProd S T`. def equivProd : IndependentPair S T ≃ PProd (IsType.type S) (IsType.type T) := { toFun := λ ⟨x, y⟩ => ⟨x, y⟩, invFun := λ ⟨x, y⟩ => ⟨x, y⟩, leftInv := λ ⟨x, y⟩ => rfl, rightInv := λ ⟨x, y⟩ => rfl } -- The instance equivalence does not depend on the type equivalence. theorem iso (a b : IndependentPair S T) : HasIso a b (λ e => SetoidEquiv (IsType.type T) a.snd b.snd) := λ e => Iff.rfl def isoCriterion : IsoCriterion (independentPairDependency S T) := λ a b => ⟨iso S T a b⟩ -- For this particular case, we can also specialize `isoEquiv` a bit. def equiv (a b : IndependentPair S T) : SigmaExpr.SigmaEquiv a b ≃ PProd (IsType.type (a.fst ≃ b.fst)) (SetoidEquiv (IsType.type T) a.snd b.snd) := { toFun := λ ⟨h₁, h₂⟩ => ⟨h₁, h₂⟩, invFun := λ ⟨h₁, h₂⟩ => ⟨h₁, h₂⟩, leftInv := λ ⟨h₁, h₂⟩ => rfl, rightInv := λ ⟨h₁, h₂⟩ => rfl } -- And in particular, if any of the two structures is the unit structure, we just have the equivalence -- on the other side. -- TODO end IndependentPair -- Another simple case is that the instance on the right is just a particular instance of the type on the -- left, i.e. we have a generalized "pointed type" structure. Obviously, in that case an isomorphism -- should transport the instance along the type equivalence. def instanceDependency : StructureDependency := ⟨universeStructure, idFun⟩ def InstanceInstance := SigmaExpr instanceDependency namespace InstanceInstance def equivSigma : InstanceInstance ≃ Σ' S : Structure, IsType.type S := { toFun := λ ⟨S, x⟩ => ⟨S, x⟩, invFun := λ ⟨S, x⟩ => ⟨S, x⟩, leftInv := λ ⟨S, x⟩ => rfl, rightInv := λ ⟨S, x⟩ => rfl } theorem iso (a b : InstanceInstance) : HasIso a b (λ e => e.toFun a.snd ≈ b.snd) := λ e => SetoidInstanceEquiv.setoidInstanceEquiv e a.snd b.snd def isoCriterion : IsoCriterion instanceDependency := λ a b => ⟨iso a b⟩ end InstanceInstance -- The next building block is a bit opaque in its full generality. It covers the case where the instance -- is a functor between two structures that are both obtained from a `StructureDependency`. This is quite -- a powerful building block because functors are an abstraction of functions, so n-ary operations and -- relations are just special cases of this and the previous two building blocks composed in different -- ways. -- -- This is best explained by considering the main use case where the type is really `α : Sort u` and `F` -- and `G` are functions `Sort u → Sort _`. Then we can consider the analogue of -- `functorMap F G : Sort u → Sort _ := λ α => F α → G α` (defined below) -- to be a type class that assigns to each `α` a function between two types that depend on `α` in a very -- flexible way. -- -- For example: -- * `functorMap id (const _ γ)` is a type class that maps `α` to `α → γ`. In particular, if `γ` is -- `Prop`, we have a unary relation aka subset of `α`. -- * `functorMap id (functorMap id (const _ γ))` maps `α` to `α → α → γ`. In particular, if `γ` is `Prop`, -- we have a binary relation on `α`. -- * `functorMap id id` maps `α` to `α → α`, i.e. a unary operation. -- * `functorMap id (functorMap id id)` maps `α` to `α → α → α`, i.e. a binary operation. -- * `functorMap (const _ γ) id` maps `α` to `γ → α`, i.e. we have an instance of `α` that depends on some -- external constant. -- * `functorMap (const _ γ) (functorMap id id)` maps `α` to `γ → α → α`, which is an outer operation. -- -- To give a general isomorphism criterion for `functorMap F G`, we need to consider `F` and `G` as -- functors again. Then we can prove the following: An `e : α ≃ β` is an isomorphism between `⟨α, f⟩` and -- `⟨β, g⟩` (with functions `f` and `g`) iff for all `x y : α` such that `congrArg F e` transports `x` -- to `y`, `congrArg G e` transports `f x` to `g y`. -- -- Applying this criterion to the examples above, we obtain all familiar special cases: -- -- Example | Isomorphism criterion -- --------------------------------------+----------------------------- -- Function returning a constant | `f x = g (e x)` -- Unary relation | `f x ↔ g (e x)` -- Binary relation | `f x y ↔ g (e x) (e y)` -- Unary operation | `e (f x) = g (e x)` -- Binary operation | `e (f x y) = g (e x) (e y)` -- Instance depending on a constant `c` | `e (f c) = g c` -- Outer operation | `e (f c x) = g c (e x)` section FunctorInstanceDef variable {S : Structure} (F G : UniverseFunctor S) def functorMap (a : S) := functorStructure (F a) (G a) def functorToFun {a b : S} (e : a ≃ b) : SetoidStructureFunctor (functorMap F G a) (functorMap F G b) := { map := λ f => ((StructureFunctor.congrArg G e).toFun ⊙ f) ⊙ (StructureFunctor.congrArg F e).invFun, functor := sorry } -- TODO: Since we are dealing with setoid functors here, we just need to combine `compFun.congrArg'` etc. def functorFunDesc : SetoidUniverseFunctorDesc S := { map := functorMap F G, toFun := functorToFun F G, respectsSetoid := sorry, respectsComp := sorry, respectsId := sorry } def functorFun : UniverseFunctor S := SetoidUniverseFunctorDesc.universeFunctor (functorFunDesc F G) def functorDependency : StructureDependency := ⟨S, functorFun F G⟩ def FunctorInstance := SigmaExpr (functorDependency F G) namespace FunctorInstance -- The isomorphism criterion for the functors `a.snd` and `b.snd`, as described above. theorem iso (a b : FunctorInstance F G) : HasIso a b (λ e => ∀ x y, x ≈[congrArg F e] y → a.snd.map x ≈[congrArg G e] b.snd.map y) := λ e => let f := StructureFunctor.congrArg F e; let g := StructureFunctor.congrArg G e; ⟨λ h₁ x y h₂ => let h₄ : g.toFun ⊙ a.snd ≃ b.snd ⊙ f.toFun := sorry; let h₅ : g.toFun (a.snd.map x) ≃ b.snd.map (f.toFun x) := h₄.ext x; let h₆ : g.toFun (a.snd.map x) ≃ b.snd.map y := sorry; ⟨h₆⟩, sorry⟩ -- If we have isomorphism criteria for `F` and `G`, we can combine them with `iso`. This way, we can not -- only compose the building blocks but also their isomorphism criteria. theorem iso' (a b : FunctorInstance F G) (cF : IsoCriterion ⟨S, F⟩) (cG : IsoCriterion ⟨S, G⟩) : HasIso a b (λ e => ∀ x y, (cF ⟨a.fst, x⟩ ⟨b.fst, y⟩).I e → (cG ⟨a.fst, a.snd.map x⟩ ⟨b.fst, b.snd.map y⟩).I e) := λ e => let h₁ := iso F G a b e; let h₂ := λ x y => (cF ⟨a.fst, x⟩ ⟨b.fst, y⟩).h e; let h₃ := λ x y => (cG ⟨a.fst, a.snd.map x⟩ ⟨b.fst, b.snd.map y⟩).h e; ⟨λ h₄ x y h₅ => (h₃ x y).mp (h₁.mp h₄ x y ((h₂ x y).mpr h₅)), λ h₄ => h₁.mpr (λ x y h₅ => (h₃ x y).mpr (h₄ x y ((h₂ x y).mp h₅)))⟩ def isoCriterion (cF : IsoCriterion ⟨S, F⟩) (cG : IsoCriterion ⟨S, G⟩) : IsoCriterion (functorDependency F G) := λ a b => ⟨iso' F G a b cF cG⟩ end FunctorInstance end FunctorInstanceDef -- TODO: Generalize `FunctorInstance` to `PiExpr`, somewhat similarly to how we treat `SigmaExpr`. This -- would be useful because ultimately everything is built on Π types. -- TODO: At least we need to define some simple cases for dependent propositions. -- The previous building blocks give us criteria for the individual fields of a type class. To combine -- these fields, we need to give an isomorphism criterion for an instance that is a dependent pair. -- -- To specify the isomorphism criterion for a dependent pair `⟨x, y⟩`, we can consider a bundled instance -- `⟨α, ⟨x, y⟩⟩` canonically as `⟨⟨α, x⟩, y⟩`, as mentioned in the introduction. In this term, we can -- consider both the inner and the outer pair as a type with an instance, and if we have isomorphism -- criteria for these, we can combine them into an isomorphism criterion for the original term. -- -- It turns out that our setoid-based formalization prevents us from generically supporting all possible -- sigma instances on the right by defining a `StructureDependency` for generic sigma types. -- Therefore, the variables `F` and `G` are based on the variant where the dependent types are nested on -- the left, and then we convert that to a `StructureDependency` where the structure contained in `F` is -- moved to the outside. section SigmaInstanceDef variable (D : NestedDependency) def UncurriedSigmaInstance := SigmaExpr (innerPairDependency D) def SigmaInstance := SigmaExpr (nestedSigmaDependency D) namespace SigmaInstance def innerPair (a : SigmaInstance D) : innerPairStructure D.fst := ⟨a.fst, a.snd.fst⟩ def outerPair (a : SigmaInstance D) : UncurriedSigmaInstance D := ⟨innerPair D a, a.snd.snd⟩ -- TODO: Make use of `PiSigmaEquivalences` here (and finish those first). def applyInnerEquiv (a b : SigmaInstance D) (e : a.fst ≃ b.fst) (h : a.snd.fst ≈[UniverseStructureFunctor.congrArg D.fst e] b.snd.fst) : innerPair D a ≃ innerPair D b := sorry theorem iso (a b : SigmaInstance D) : HasIso a b (λ e => ∃ h : a.snd.fst ≈[UniverseStructureFunctor.congrArg D.fst e] b.snd.fst, a.snd.snd ≈[congrArg D.snd (applyInnerEquiv D a b e h)] b.snd.snd) := sorry --def applyInnerEquiv' (a b : SigmaInstance D) (cF : IsoCriterion D.fst) (e : a.fst ≃ b.fst) -- (h : (cF (innerPair D a) (innerPair D b)).I e) : -- innerPair D a ≃ innerPair D b := --sorry -- --theorem iso' (a b : SigmaInstance D) (cF : IsoCriterion D.fst) (cG : IsoCriterion (innerPairDependency D)) : -- HasIso a b (λ e => ∃ h : (cF (innerPair D a) (innerPair D b)).I e, -- (cG (outerPair D a) (outerPair D b)).I (applyInnerEquiv' D a b cF e h)) := --sorry -- --def isoCriterion (cF : IsoCriterion D.fst) (cG : IsoCriterion (innerPairDependency D)) : -- IsoCriterion (nestedSigmaDependency D) := --λ a b => ⟨iso' D a b cF cG⟩ end SigmaInstance end SigmaInstanceDef -- TODO: Also define building blocks for everything we need in order to formalize categories and -- groupoids, and then define isomorphism for the `HasStructure` type class. This gives us an interesting -- and probably quite powerful reflection principle. -- -- Maybe it will lead to a proof of something like univalence in the internal logic. Or rather: We can -- construct an internal logic where "isomorphism is equality" holds. end BuildingBlocks open BuildingBlocks
c7b69fecb0bfff6b178f50bea4641f719c9c013d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/preadditive/biproducts_auto.lean
1e15c4041374c4455805d01624008d0ed8ba66ad
[]
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
14,016
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.abel import Mathlib.category_theory.limits.shapes.biproducts import Mathlib.category_theory.preadditive.default import Mathlib.PostPort universes u v namespace Mathlib /-! # Basic facts about morphisms between biproducts in preadditive categories. * In any category (with zero morphisms), if `biprod.map f g` is an isomorphism, then both `f` and `g` are isomorphisms. The remaining lemmas hold in any preadditive category. * If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. * As a corollary of the previous two facts, if we have an isomorphism `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, we can construct an isomorphism `X₂ ≅ Y₂`. * If `f : W ⊞ X ⟶ Y ⊞ Z` is an isomorphism, either `𝟙 W = 0`, or at least one of the component maps `W ⟶ Y` and `W ⟶ Z` is nonzero. * If `f : ⨁ S ⟶ ⨁ T` is an isomorphism, then every column (corresponding to a nonzero summand in the domain) has some nonzero matrix entry. -/ namespace category_theory /-- If ``` (f 0) (0 g) ``` is invertible, then `f` is invertible. -/ def is_iso_left_of_is_iso_biprod_map {C : Type u} [category C] [limits.has_zero_morphisms C] [limits.has_binary_biproducts C] {W : C} {X : C} {Y : C} {Z : C} (f : W ⟶ Y) (g : X ⟶ Z) [is_iso (limits.biprod.map f g)] : is_iso f := is_iso.mk (limits.biprod.inl ≫ inv (limits.biprod.map f g) ≫ limits.biprod.fst) /-- If ``` (f 0) (0 g) ``` is invertible, then `g` is invertible. -/ def is_iso_right_of_is_iso_biprod_map {C : Type u} [category C] [limits.has_zero_morphisms C] [limits.has_binary_biproducts C] {W : C} {X : C} {Y : C} {Z : C} (f : W ⟶ Y) (g : X ⟶ Z) [is_iso (limits.biprod.map f g)] : is_iso g := let _inst : is_iso (limits.biprod.map g f) := eq.mpr sorry is_iso.comp_is_iso; is_iso_left_of_is_iso_biprod_map g f /-- The "matrix" morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` with specified components. -/ def biprod.of_components {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂ := limits.biprod.fst ≫ f₁₁ ≫ limits.biprod.inl + limits.biprod.fst ≫ f₁₂ ≫ limits.biprod.inr + limits.biprod.snd ≫ f₂₁ ≫ limits.biprod.inl + limits.biprod.snd ≫ f₂₂ ≫ limits.biprod.inr @[simp] theorem biprod.inl_of_components {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) : limits.biprod.inl ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ = f₁₁ ≫ limits.biprod.inl + f₁₂ ≫ limits.biprod.inr := sorry @[simp] theorem biprod.inr_of_components {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) : limits.biprod.inr ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ = f₂₁ ≫ limits.biprod.inl + f₂₂ ≫ limits.biprod.inr := sorry @[simp] theorem biprod.of_components_fst {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ limits.biprod.fst = limits.biprod.fst ≫ f₁₁ + limits.biprod.snd ≫ f₂₁ := sorry @[simp] theorem biprod.of_components_snd {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ limits.biprod.snd = limits.biprod.fst ≫ f₁₂ + limits.biprod.snd ≫ f₂₂ := sorry @[simp] theorem biprod.of_components_eq {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂) : biprod.of_components (limits.biprod.inl ≫ f ≫ limits.biprod.fst) (limits.biprod.inl ≫ f ≫ limits.biprod.snd) (limits.biprod.inr ≫ f ≫ limits.biprod.fst) (limits.biprod.inr ≫ f ≫ limits.biprod.snd) = f := sorry @[simp] theorem biprod.of_components_comp {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} {Z₁ : C} {Z₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) (g₁₁ : Y₁ ⟶ Z₁) (g₁₂ : Y₁ ⟶ Z₂) (g₂₁ : Y₂ ⟶ Z₁) (g₂₂ : Y₂ ⟶ Z₂) : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ biprod.of_components g₁₁ g₁₂ g₂₁ g₂₂ = biprod.of_components (f₁₁ ≫ g₁₁ + f₁₂ ≫ g₂₁) (f₁₁ ≫ g₁₂ + f₁₂ ≫ g₂₂) (f₂₁ ≫ g₁₁ + f₂₂ ≫ g₂₁) (f₂₁ ≫ g₁₂ + f₂₂ ≫ g₂₂) := sorry /-- The unipotent upper triangular matrix ``` (1 r) (0 1) ``` as an isomorphism. -/ @[simp] theorem biprod.unipotent_upper_hom {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} (r : X₁ ⟶ X₂) : iso.hom (biprod.unipotent_upper r) = biprod.of_components 𝟙 r 0 𝟙 := Eq.refl (iso.hom (biprod.unipotent_upper r)) /-- The unipotent lower triangular matrix ``` (1 0) (r 1) ``` as an isomorphism. -/ @[simp] theorem biprod.unipotent_lower_hom {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} (r : X₂ ⟶ X₁) : iso.hom (biprod.unipotent_lower r) = biprod.of_components 𝟙 0 r 𝟙 := Eq.refl (iso.hom (biprod.unipotent_lower r)) /-- If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. (This is the version of `biprod.gaussian` written in terms of components.) -/ def biprod.gaussian' {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) [is_iso f₁₁] : psigma fun (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) => psigma fun (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) => psigma fun (g₂₂ : X₂ ⟶ Y₂) => iso.hom L ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ iso.hom R = limits.biprod.map f₁₁ g₂₂ := psigma.mk (biprod.unipotent_lower (-f₂₁ ≫ inv f₁₁)) (psigma.mk (biprod.unipotent_upper (-inv f₁₁ ≫ f₁₂)) (psigma.mk (f₂₂ - f₂₁ ≫ inv f₁₁ ≫ f₁₂) sorry)) /-- If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. -/ def biprod.gaussian {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂) [is_iso (limits.biprod.inl ≫ f ≫ limits.biprod.fst)] : psigma fun (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) => psigma fun (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) => psigma fun (g₂₂ : X₂ ⟶ Y₂) => iso.hom L ≫ f ≫ iso.hom R = limits.biprod.map (limits.biprod.inl ≫ f ≫ limits.biprod.fst) g₂₂ := let this : psigma fun (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) => psigma fun (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) => psigma fun (g₂₂ : X₂ ⟶ Y₂) => iso.hom L ≫ biprod.of_components (limits.biprod.inl ≫ f ≫ limits.biprod.fst) (limits.biprod.inl ≫ f ≫ limits.biprod.snd) (limits.biprod.inr ≫ f ≫ limits.biprod.fst) (limits.biprod.inr ≫ f ≫ limits.biprod.snd) ≫ iso.hom R = limits.biprod.map (limits.biprod.inl ≫ f ≫ limits.biprod.fst) g₂₂ := biprod.gaussian' (limits.biprod.inl ≫ f ≫ limits.biprod.fst) (limits.biprod.inl ≫ f ≫ limits.biprod.snd) (limits.biprod.inr ≫ f ≫ limits.biprod.fst) (limits.biprod.inr ≫ f ≫ limits.biprod.snd); eq.mpr sorry (eq.mp sorry this) /-- If `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` via a two-by-two matrix whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct an isomorphism `X₂ ≅ Y₂`, via Gaussian elimination. -/ def biprod.iso_elim' {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) [is_iso f₁₁] [is_iso (biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂)] : X₂ ≅ Y₂ := psigma.cases_on (biprod.gaussian' f₁₁ f₁₂ f₂₁ f₂₂) fun (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) (snd : psigma fun (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) => psigma fun (g₂₂ : X₂ ⟶ Y₂) => iso.hom L ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ iso.hom R = limits.biprod.map f₁₁ g₂₂) => psigma.cases_on snd fun (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) (snd_snd : psigma fun (g₂₂ : X₂ ⟶ Y₂) => iso.hom L ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ iso.hom R = limits.biprod.map f₁₁ g₂₂) => psigma.cases_on snd_snd fun (g : X₂ ⟶ Y₂) (w : iso.hom L ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ iso.hom R = limits.biprod.map f₁₁ g) => let _inst : is_iso (limits.biprod.map f₁₁ g) := eq.mpr sorry is_iso.comp_is_iso; let _inst_6 : is_iso g := is_iso_right_of_is_iso_biprod_map f₁₁ g; as_iso g /-- If `f` is an isomorphism `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct an isomorphism `X₂ ≅ Y₂`, via Gaussian elimination. -/ def biprod.iso_elim {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {X₁ : C} {X₂ : C} {Y₁ : C} {Y₂ : C} (f : X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂) [is_iso (limits.biprod.inl ≫ iso.hom f ≫ limits.biprod.fst)] : X₂ ≅ Y₂ := let _inst : is_iso (biprod.of_components (limits.biprod.inl ≫ iso.hom f ≫ limits.biprod.fst) (limits.biprod.inl ≫ iso.hom f ≫ limits.biprod.snd) (limits.biprod.inr ≫ iso.hom f ≫ limits.biprod.fst) (limits.biprod.inr ≫ iso.hom f ≫ limits.biprod.snd)) := eq.mpr sorry (is_iso.of_iso f); biprod.iso_elim' (limits.biprod.inl ≫ iso.hom f ≫ limits.biprod.fst) (limits.biprod.inl ≫ iso.hom f ≫ limits.biprod.snd) (limits.biprod.inr ≫ iso.hom f ≫ limits.biprod.fst) (limits.biprod.inr ≫ iso.hom f ≫ limits.biprod.snd) theorem biprod.column_nonzero_of_iso {C : Type u} [category C] [preadditive C] [limits.has_binary_biproducts C] {W : C} {X : C} {Y : C} {Z : C} (f : W ⊞ X ⟶ Y ⊞ Z) [is_iso f] : 𝟙 = 0 ∨ limits.biprod.inl ≫ f ≫ limits.biprod.fst ≠ 0 ∨ limits.biprod.inl ≫ f ≫ limits.biprod.snd ≠ 0 := sorry theorem biproduct.column_nonzero_of_iso' {C : Type u} [category C] [preadditive C] {σ : Type v} {τ : Type v} [DecidableEq σ] [DecidableEq τ] [fintype τ] {S : σ → C} [limits.has_biproduct S] {T : τ → C} [limits.has_biproduct T] (s : σ) (f : ⨁ S ⟶ ⨁ T) [is_iso f] : (∀ (t : τ), limits.biproduct.ι S s ≫ f ≫ limits.biproduct.π T t = 0) → 𝟙 = 0 := sorry /-- If `f : ⨁ S ⟶ ⨁ T` is an isomorphism, and `s` is a non-trivial summand of the source, then there is some `t` in the target so that the `s, t` matrix entry of `f` is nonzero. -/ def biproduct.column_nonzero_of_iso {C : Type u} [category C] [preadditive C] {σ : Type v} {τ : Type v} [DecidableEq σ] [DecidableEq τ] [fintype τ] {S : σ → C} [limits.has_biproduct S] {T : τ → C} [limits.has_biproduct T] (s : σ) (nz : 𝟙 ≠ 0) [(t : τ) → DecidableEq (S s ⟶ T t)] (f : ⨁ S ⟶ ⨁ T) [is_iso f] : trunc (psigma fun (t : τ) => limits.biproduct.ι S s ≫ f ≫ limits.biproduct.π T t ≠ 0) := trunc_sigma_of_exists sorry end Mathlib
77e63ad05af38be264e9d555471409504f853057
8be24982c807641260370bd09243eac768750811
/src/e_trans_helpers2.lean
534e9fae9d32df4cbcef04f919c549033ba78da9
[]
no_license
jjaassoonn/transcendental
8008813253af3aa80b5a5c56551317e7ab4246ab
99bc6ea6089f04ed90a0f55f0533ebb7f47b22ff
refs/heads/master
1,607,869,957,944
1,599,659,687,000
1,599,659,687,000
234,444,826
9
1
null
1,598,218,307,000
1,579,224,232,000
HTML
UTF-8
Lean
false
false
40,845
lean
import measure_theory.interval_integral import measure_theory.lebesgue_measure import analysis.special_functions.exp_log import small_things noncomputable theory open_locale classical open_locale big_operators notation α`[X]` := polynomial α /-- # Definition and some theorems about differentiating multiple times -/ /-Definition For any integer polynomial $f$ and $n\in\mathbb N$ we define `deriv_n f n` to be the $n$-th derivative of polynomial $f$. $h^{[n]}$ means $h\circ h\circ h\cdots\circ h$ $n$-times. -/ def deriv_n (f : ℤ[X]) (n : ℕ) : ℤ[X] := polynomial.derivative ^[n] f /-Lemma the zeroth derivative of polynomial $f$ is $f$ itself. -/ lemma zeroth_deriv (f : ℤ[X]) : deriv_n f 0 = f := begin -- This is purely definition of `deriv_n f n` -- We also used $f^{[n]}=\mathrm{id}$ and $\mathrm{id} x = x$. rw deriv_n, simp only [id.def, function.iterate_zero], end /-Lemma the derivative of $f^{(n)}$ is $f^{(n+1)}$ -/ lemma deriv_succ (f : ℤ[X]) (n : ℕ) : (deriv_n f n).derivative = (deriv_n f (n+1)) := begin -- By definition and $h^{[n+1]}=h\circ h^{[n]}$ rw [deriv_n, deriv_n, function.iterate_succ'], end /-Lemma the $n$-th derivative of zero polynomial is $0$ -/ lemma deriv_zero_p (n : ℕ) : deriv_n 0 n = 0 := begin -- We prove by induction. Base case and inductive case can all be done with `simp` induction n with n hn; simp only [deriv_n, id.def, function.iterate_zero], rw <-deriv_n, assumption, end /-Lemma If the $n$-th coefficient of $f$ is $a_n$, then the $n$-th coefficient in $f^{(k)}$ is $\left(\prod_{i=0}^{k-1} (n+k-i)\right)a_{n+k}$ -/ lemma deriv_n_coeff (f : ℤ[X]) (k : ℕ) : ∀ n : ℕ, (deriv_n f k).coeff n = (∏ i in finset.range k, (n+k-i)) * (f.coeff (n+k)) := begin -- So we use induction on $k$ induction k with k ih, -- For the zeroth derivative, $f^{(0)}=f$. This case is true by `simp`. simp only [add_zero, nat.nat_zero_eq_zero, one_mul, finset.range_zero, finset.prod_empty], rw deriv_n, simp only [forall_const, id.def, eq_self_iff_true, function.iterate_zero], -- Let us assume our claim is true for $k$ and consider the $m$-th coefficient. intro m, -- We know that $f^{(k+1)}=\left(f^{(k)}\right)'$ and for any polynomial $g$, the $m$-th coefficient of $g'$ is $(m+1)\times (m+1)$-th coefficient of $g$. Then we can use induction hypothesis with $m+1$. rw [deriv_n, function.iterate_succ'], simp only [function.comp_app, int.coe_nat_succ], rw <-deriv_n, rw polynomial.coeff_derivative, rw ih (m+1), rw finset.prod_range_succ, simp only [int.coe_nat_succ, int.nat_cast_eq_coe_nat], -- The rest of the proves are trivial to a pair of human eyes. But we need to give computers some hint to solve this triviality. have triv : (∏ (x : ℕ) in finset.range k, ((m:ℤ) + 1 + ↑k - ↑x)) = ∏ (x : ℕ) in finset.range k, (↑m + (↑k + 1) - ↑x), { apply congr_arg, ext, simp only [sub_left_inj], ring, }, rw triv, replace triv : (m + 1 : ℤ) = (m + (k+1) - k:ℤ), { rw add_sub_assoc, simp only [add_sub_cancel'], }, rw triv, replace triv : f.coeff (m + 1 + k) = f.coeff (m + k.succ), { rw nat.succ_eq_add_one, ring, }, rw triv, ring, end /-Lemma Like first derivative, higher derivatives still respect addition -/ lemma deriv_n_add (p q :ℤ[X]) (n : ℕ) : (deriv_n (p+q) n) = (deriv_n p n) + (deriv_n q n) := begin induction n with n ih, rw [deriv_n, deriv_n, deriv_n], simp only [id.def, function.iterate_zero], rw [deriv_n, deriv_n, deriv_n, function.iterate_succ'], simp only [function.comp_app], rw [<-deriv_n,<-deriv_n,<-deriv_n, ih], simp only [polynomial.derivative_add], end /-Lemma For any polynomial $f$ with degree $d$, the $d+1$-th derivative is zero. -/ theorem deriv_too_much (f : ℤ[X]): (deriv_n f (f.nat_degree + 1)) = 0 := begin -- We prove that all coefficient of $f^{(d+1)}$ is zero. ext, -- We use lemma `deriv_n_coeff` and all coefficient of zero polynomial is $0$. rw deriv_n_coeff, simp only [int.coe_nat_succ, polynomial.coeff_zero, mul_eq_zero], right, -- Then the problem reduces to prove that $d+1$-th coeffcient of $f$ is zero. But $f$ has degree $d$. So we just need $d< d+1$. This is proved by `linarith`. apply polynomial.coeff_eq_zero_of_nat_degree_lt, linarith, end /-Lemma if $i+1\le n$ then $n-(i+1)+1=n-i$ -/ private lemma nat_sub_eq (n i : ℕ) (h : i + 1 ≤ n) : (n - (i + 1) + 1) = n - i := begin have triv : n - (i+1) = n - i - 1, exact rfl, rw triv, apply nat.sub_add_cancel, exact nat.le_sub_left_of_add_le h, end /-Lemma We have the pascal triangle \[{n\choose k+1}+{n\choose k} = {n+1\choose k+1}\] -/ private lemma pascal_triangle (n k : ℕ) : (n.choose (k+1)) + (n.choose k) = (n+1).choose (k+1) := begin -- This is actually how `mathlib` defined binomial coefficient. exact add_comm (nat.choose n (k + 1)) (nat.choose n k), end /-- Theorem We also have that for $p,q\in\mathbb Z[x]$, \[ (p\times q)^{(n)} = \sum_{i=0}^n\left({n\choose i}p^{(i)}q^{(n-i)}\right) \] -/ lemma deriv_n_poly_prod (p q : ℤ[X]) (n : ℕ) : deriv_n (p * q) n = ∑ k in finset.range n.succ, (polynomial.C (n.choose k:ℤ)) * (deriv_n p (n-k)) * (deriv_n q k) := begin -- We prove by induction on $n$. induction n with n IH, -- For $n=0$, we are using `zeroth_deriv`. simp only [zeroth_deriv, nat.choose_self, int.cast_coe_nat, ring_hom.eq_int_cast, one_mul, nat.cast_one, finset.sum_singleton, finset.range_one], { -- For inductive case -- We use $(pq)^{(n+1)}=d(pq)^{(n)} $, inductive hypothesis and that derivative is linear. rw deriv_n, rw function.iterate_succ', dsimp, rw <-deriv_n, rw IH, simp only [polynomial.derivative_sum, polynomial.derivative_mul, zero_mul, polynomial.derivative_C, zero_add, polynomial.derivative_sum, polynomial.derivative_mul, zero_mul, polynomial.derivative_C, zero_add], -- The rest of the proves is essentially openning and closing brackets and renaming summing indeces. rw finset.sum_add_distrib, conv_lhs {rw finset.sum_range_succ', rw finset.sum_range_succ, simp only [zeroth_deriv, nat.choose_self, one_mul, nat.choose_zero_right, int.coe_nat_zero, nat.sub_self, polynomial.C_1, int.coe_nat_succ, nat.sub_zero, zero_add]}, have eq : ∑ (i : ℕ) in finset.range n, polynomial.C (n.choose (i + 1):ℤ) * (deriv_n p (n - (i + 1))).derivative * deriv_n q (i + 1) + (deriv_n p n).derivative * q + (p * (deriv_n q n).derivative + ∑ (x : ℕ) in finset.range n, polynomial.C (n.choose x:ℤ) * deriv_n p (n - x) * (deriv_n q x).derivative) = (∑ (i : ℕ) in finset.range n, polynomial.C (n.choose (i + 1):ℤ) * (deriv_n p (n - (i + 1))).derivative * deriv_n q (i + 1)) + (∑ (x : ℕ) in finset.range n, polynomial.C (n.choose x:ℤ) * deriv_n p (n - x) * (deriv_n q x).derivative) + ((deriv_n p n).derivative * q + (p * (deriv_n q n).derivative)) := by ring, rw [eq, <-finset.sum_add_distrib], replace eq : (∑ (x : ℕ) in finset.range n, (polynomial.C (n.choose (x + 1):ℤ) * (deriv_n p (n - (x + 1))).derivative * deriv_n q (x + 1) + polynomial.C (n.choose x:ℤ) * deriv_n p (n - x) * (deriv_n q x).derivative)) = (∑ (x : ℕ) in finset.range n, (polynomial.C (n.choose (x + 1):ℤ) * (deriv_n p (n - x)) * deriv_n q (x + 1) + polynomial.C (n.choose x:ℤ) * deriv_n p (n - x) * (deriv_n q (x+1)))), { apply finset.sum_congr, exact rfl, intros i hi, simp only [deriv_succ, int.cast_coe_nat, ring_hom.eq_int_cast, add_left_inj], simp only [finset.mem_range] at hi, replace hi : i + 1 ≤ n := hi, rw nat_sub_eq _ _ hi, }, rw eq, replace eq : (∑ (x : ℕ) in finset.range n, (polynomial.C (n.choose (x + 1):ℤ) * (deriv_n p (n - x)) * deriv_n q (x + 1) + polynomial.C (n.choose x:ℤ) * deriv_n p (n - x) * (deriv_n q (x+1)))) = (∑ (x : ℕ) in finset.range n, ((polynomial.C (n.choose (x + 1):ℤ) + polynomial.C (n.choose x:ℤ)) * (deriv_n p (n - x)) * deriv_n q (x + 1))), { apply congr_arg, rw function.funext_iff, intro i, ring, }, rw eq, replace eq : (∑ (x : ℕ) in finset.range n, ((polynomial.C (n.choose (x + 1):ℤ) + polynomial.C (n.choose x:ℤ)) * (deriv_n p (n - x)) * deriv_n q (x + 1))) = (∑ (x : ℕ) in finset.range n, ((polynomial.C (n.choose (x + 1) + (n.choose x):ℤ)) * (deriv_n p (n - x)) * deriv_n q (x + 1))), { apply congr_arg, rw function.funext_iff, intro i, simp only [int.cast_add, ring_hom.eq_int_cast], }, rw eq, replace eq : (∑ (x : ℕ) in finset.range n, ((polynomial.C (n.choose (x + 1) + (n.choose x):ℤ)) * (deriv_n p (n - x)) * deriv_n q (x + 1))) = (∑ (x : ℕ) in finset.range n, ((polynomial.C ((n+1).choose (x + 1):ℤ)) * (deriv_n p (n - x)) * deriv_n q (x + 1))), { apply congr_arg, rw function.funext_iff, intro i, rw <-pascal_triangle, simp only [int.coe_nat_add], }, rw eq, conv_rhs {rw finset.sum_range_succ', rw finset.sum_range_succ}, simp only [deriv_succ, zeroth_deriv, nat.succ_eq_add_one, nat.choose_self, int.cast_coe_nat, ring_hom.eq_int_cast, one_mul, nat.succ_sub_succ_eq_sub, nat.choose_zero_right, int.coe_nat_zero, nat.sub_self, int.cast_one, int.coe_nat_succ, nat.sub_zero, zero_add], ring, } end /-Theorem For a polynomial $f$ then if $n>0$, we have $f^{(n)}=f^{(n-1)}\times f'$ -/ theorem poly_pow_deriv (f : ℤ[X]) (n : ℕ) (hn : n > 0) : (f ^ n).derivative = (polynomial.C (n:ℤ)) * (f ^ (n-1)) * f.derivative := begin induction n with n IH, exfalso, linarith, { cases n, simp only [ring_hom.eq_int_cast, one_mul, int.coe_nat_zero, int.cast_one, int.coe_nat_succ, pow_one, zero_add, pow_zero], replace IH := IH (nat.succ_pos n), rw nat.succ_eq_add_one, rw pow_add, simp only [int.cast_coe_nat, int.cast_add, ring_hom.eq_int_cast, polynomial.derivative_mul, int.cast_one, nat.succ_add_sub_one, int.coe_nat_succ, pow_one], rw IH, simp only [nat.succ_sub_succ_eq_sub, polynomial.C_add, polynomial.C_1, int.coe_nat_succ, nat.sub_zero, nat.succ_sub_succ_eq_sub, int.coe_nat_succ, nat.sub_zero], have eq1 : (polynomial.C ↑n + 1) * f ^ n * f.derivative * f = (polynomial.C ↑n + 1) * f ^ (n+1) * f.derivative, { rw pow_add, simp only [int.cast_coe_nat, ring_hom.eq_int_cast, pow_one], ring, } , rw eq1, rw nat.succ_eq_add_one, repeat {rw add_mul}, simp only [int.cast_coe_nat, ring_hom.eq_int_cast, one_mul], } end /-- # Assumption-/ /-Theorem fundamental theorem of calculus and integration by part is assumed. I am waiting for them to arrive in `mathlib` and I will update this part and prove relatvent additional assumptions. -/ private lemma ftc1 (f : ℝ -> ℝ) {hf : measurable f} {hf2 : continuous f} (a b : ℝ) (h : a ≤ b) {hf3 : measure_theory.integrable_on f (set.Icc a b)} (x0 : ℝ) (hx0 : x0 ∈ set.Icc a b) : has_deriv_at (λ (b : ℝ), ∫ (x : ℝ) in a..b, f x) (f x0) x0 := begin apply interval_integral.integral_has_deriv_at_of_tendsto_ae, simp only [], exact hf, unfold interval_integrable, split, apply measure_theory.integrable_on.mono hf3, intros y hy, simp only [set.mem_Ioc, set.mem_Icc] at ⊢ hy hx0, split, linarith, linarith, apply le_refl _, apply measure_theory.integrable_on.mono hf3, intros y hy, simp only [set.mem_Ioc, set.mem_Icc] at ⊢ hy hx0, split, linarith, linarith, apply le_refl _, apply filter.tendsto_inf_left, exact continuous.tendsto hf2 x0, end theorem ftc' (F f: ℝ -> ℝ) {hF : differentiable ℝ F} {F_deriv : deriv F = f} {hf : measurable f} {hf1 : continuous f} (a b : ℝ) (h : b ≥ a) : (∫ x in a..b, f x) = F b - F a := begin by_cases hab : (a = b), rw hab, simp only [interval_integral.integral_same, sub_self], set G := (λ x, (∫ t in a..x, f t)) with hG, have prop := ftc1 f a b h, rw <-hG at prop, have hG1 : differentiable_on ℝ G (set.Icc a b), { intros x hx, have prop := ftc1 f a b h x hx, refine differentiable_at.differentiable_within_at _, rw hG, exact has_deriv_at.differentiable_at prop, exact hf, exact hf1, apply continuous.integrable_on_compact _ hf1, exact real.locally_finite_volume, exact compact_Icc, }, have H : (set.Icc a b).indicator (deriv G) = (set.Icc a b).indicator f, { apply set.indicator_congr, intros x0 hx0, replace prop := prop x0 hx0, exact has_deriv_at.deriv prop, }, have H2 : (set.Icc a b).indicator (deriv G) = (set.Icc a b).indicator (deriv F), { exact eq.trans H (congr_arg (set.Icc a b).indicator (eq.symm F_deriv)), }, replace H2 : ∀ y ∈ set.Icc a b, (deriv (F - G)) y = 0, { intros y hy, change deriv (λ t, F t - G t) y = 0, rw deriv_sub, rw sub_eq_zero, have eq1 : (set.Icc a b).indicator (deriv F) y = deriv F y, exact if_pos hy, rw <-eq1, have eq2 : (set.Icc a b).indicator (deriv G) y = deriv G y, exact if_pos hy, rw <-eq2, exact congr_fun H2.symm y, dsimp only [], exact hF y, dsimp only [], exact has_deriv_at.differentiable_at (prop y hy), }, have key : ∀ y ∈ set.Ioc a b, (F - G) y = (F - G) a, { intros y hy, have ineq : a < y, simp only [set.mem_Ioc] at hy, exact hy.1, have key := exists_deriv_eq_slope (F - G) ineq _ _, rcases key with ⟨c, hc, hc2⟩, have hc' : c ∈ set.Icc a b, simp only [set.mem_Icc, set.mem_Ioc, set.mem_Ioo] at hy ⊢ hc, split, linarith, linarith, rw H2 c hc' at hc2, replace hc2 := eq.symm hc2, rw div_eq_zero_iff at hc2, cases hc2, exact sub_eq_zero.mp hc2, simp only [set.mem_Icc, set.mem_Ioc, set.mem_Ioo] at hy ⊢ hc, linarith, apply continuous_on.sub, simp only [], apply continuous.continuous_on, apply differentiable.continuous hF, have hG1' : continuous_on G (set.Icc a b), apply differentiable_on.continuous_on hG1, simp only [], apply continuous_on.mono hG1', apply set.Icc_subset_Icc, exact le_refl a, exact hy.2, apply differentiable_on.sub, simp only [], exact differentiable.differentiable_on hF, simp only [], apply differentiable_on.mono hG1, intros z hz, simp only [set.mem_Icc, set.mem_Ioc, set.mem_Ioo] at *, split, linarith, linarith, }, have G_a : G a = 0, { rw hG, simp only [interval_integral.integral_same], }, have G_b : G b = ∫ x in a .. b, f x, { rw hG, }, rw <-G_b, have eq : G b = G b - 0, rw sub_zero, rw eq, rw <-G_a, rw sub_eq_sub_iff_sub_eq_sub, suffices : F b - G b = F a - G a, linarith, replace key := key b _, simp only [pi.sub_apply] at key ⊢, exact key, simp only [set.mem_Icc, set.mem_Ioc, set.mem_Ioo] at *, split, exact lt_of_le_of_ne h hab, exact le_refl b, exact hf, exact hf1, apply measure_theory.integrable_on.mono_set (continuous.integrable_on_compact (@compact_Icc a b) hf1), exact set.subset.rfl, exact real.locally_finite_volume, end theorem ftc (f: ℝ -> ℝ) {hf : differentiable ℝ f} {hf2 : measurable (deriv f)} {hf3 : continuous (deriv f)} (a b : ℝ) (h : b ≥ a) : (∫ x in a..b, (deriv f) x) = f b - f a := begin refine ftc' f (deriv f) a b h, simp only [], exact hf, refl, exact hf2, exact hf3, end theorem integrate_by_part (f g : ℝ -> ℝ) {hf : differentiable ℝ f} {hf2 : measurable (deriv f)} {hf3 : measurable f} {hf4 : continuous (deriv f)} {hg : differentiable ℝ g} {hg2 : measurable (deriv g)} {hg3 : measurable g} {hg4 : continuous (deriv g)} (a b : ℝ) (h : b ≥ a) : (∫ x in a..b, (f x)*(deriv g x)) = (f b) * (g b) - (f a) * (g a) - (∫ x in a..b, (deriv f x) * (g x)) := begin have eq1 := ftc (f * g) a b h, have eq2 : (∫ (x : ℝ) in a..b, deriv (f * g) x) = (∫ (x : ℝ) in a..b, (deriv f x) * g x + f x * (deriv g x)), { rw interval_integral.integral_of_le h, rw interval_integral.integral_of_le h, apply congr_arg, ext y, apply deriv_mul, simp only [], exact hf y, simp only [], exact hg y, }, rw eq2 at eq1, rw interval_integral.integral_add at eq1, simp only [pi.mul_apply] at eq1 ⊢, rw <-eq1, simp only [add_sub_cancel'], apply measurable.mul, simp only [], exact hf2, simp only [], exact hg3, rw interval_integrable, have H1 : continuous (λ x, (deriv f x) * g x), { apply continuous.mul _ _, exact normed_ring_top_monoid, exact hf4, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _, exact hg, }, split, apply measure_theory.integrable_on.mono_set (continuous.integrable_on_compact (@compact_Icc a b) H1), exact set.Ioc_subset_Icc_self, exact real.locally_finite_volume, apply measure_theory.integrable_on.mono_set (continuous.integrable_on_compact (@compact_Icc a b) H1), rw set.Ioc_eq_empty, exact (set.Icc a b).empty_subset, exact h, exact real.locally_finite_volume, apply measurable.mul, exact hf3, exact hg2, rw interval_integrable, have H2 : continuous (λ x, f x * deriv g x), { apply continuous.mul _ _, exact normed_ring_top_monoid, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _, exact hf, exact hg4, }, split, apply measure_theory.integrable_on.mono_set (continuous.integrable_on_compact (@compact_Icc a b) H2), exact set.Ioc_subset_Icc_self, exact real.locally_finite_volume, apply measure_theory.integrable_on.mono_set (continuous.integrable_on_compact (@compact_Icc a b) H2), rw set.Ioc_eq_empty, exact (set.Icc a b).empty_subset, exact h, exact real.locally_finite_volume, apply differentiable.mul hf hg, apply continuous.measurable, { have eq1 : deriv (f * g) = (deriv f) * g + f * (deriv g), ext, simp only [pi.add_apply, pi.mul_apply], change (deriv (λ x, (f x * g x))) x = deriv f x * g x + f x * deriv g x, rw deriv_mul, simp only [], exact hf x, simp only [], exact hg x, rw eq1, apply continuous.add _ _, exact normed_top_monoid, apply continuous.mul _ _, exact normed_ring_top_monoid, exact hf4, apply differentiable.continuous hg, apply continuous.mul _ _, exact normed_ring_top_monoid, apply differentiable.continuous hf, exact hg4, }, { have eq1 : deriv (f * g) = (deriv f) * g + f * (deriv g), ext, simp only [pi.add_apply, pi.mul_apply], change (deriv (λ x, (f x * g x))) x = deriv f x * g x + f x * deriv g x, rw deriv_mul, simp only [], exact hf x, simp only [], exact hg x, rw eq1, apply continuous.add _ _, exact normed_top_monoid, apply continuous.mul _ _, exact normed_ring_top_monoid, exact hf4, apply differentiable.continuous hg, apply continuous.mul _ _, exact normed_ring_top_monoid, apply differentiable.continuous hf, exact hg4, }, end /-Theorem integrations on a set $S$ of two identical functions are indentical -/ theorem same_integral {S : set ℝ} (f g : ℝ -> ℝ) : f = g -> (∫ x in S, f x) = ∫ x in S, g x := begin intro h, simp only [], exact congr_arg (measure_theory.integral (measure_theory.measure_space.volume.restrict S)) h, end /-Theorem integrations of two identical functions are indentical -/ theorem same_integral' (f g : ℝ -> ℝ) : f = g -> (∫ x, f x) = ∫ x, g x := begin exact congr_arg (λ (f : ℝ → ℝ), ∫ (x : ℝ), f x), end /-Theorem identical function have identical derivatives -/ theorem same_deriv (f g : ℝ -> ℝ) (x : ℝ) : f = g -> deriv f x = deriv g x := λ h, congr_fun (congr_arg deriv h) x /-Theorem If forall $x\in(a,b), 0 \le f(x)\le c$ then $$ \int_a^b f\le (b-a)c $$ -/ theorem integral_le_max_times_length (f : ℝ -> ℝ) {h1 : measurable f} (a b : ℝ) (h : b ≥ a) (c : ℝ) (f_nonneg : ∀ x ∈ set.Icc a b, f x ≥ 0) (c_max : ∀ x ∈ set.Icc a b, f x ≤ c) : (∫ x in a..b, f x) ≤ (b - a) * c := begin have triv1 : (∫ x in a..b, f x) = ∥(∫ x in a..b, f x)∥, { rw real.norm_eq_abs, rw abs_of_nonneg, rw interval_integral.integral_of_le h, apply measure_theory.integral_nonneg_of_ae, apply (@measure_theory.ae_restrict_iff ℝ _ _ (set.Ioc a b) _ _).2, apply measure_theory.ae_of_all, intros x hx, simp only [and_imp, set.mem_Ioc, pi.zero_apply, ge_iff_le, set.mem_Icc] at *, refine f_nonneg x _ _, linarith, linarith, simp only [pi.zero_apply], refine is_measurable_le measurable_zero h1, }, rw triv1, have triv2 := @interval_integral.norm_integral_le_of_norm_le_const ℝ _ _ _ _ _ _ a b c f _, rw abs_of_nonneg at triv2, linarith, linarith, intros x hx, rw real.norm_eq_abs, rw abs_of_nonneg, have eq1 : min a b = a := min_eq_left h, have eq2 : max a b = b := max_eq_right h, simp only [set.mem_Ioc, eq1, eq2] at hx ⊢, refine c_max x _, simp only [set.mem_Icc], split, linarith, linarith, refine f_nonneg x _, have eq1 : min a b = a := min_eq_left h, have eq2 : max a b = b := max_eq_right h, simp only [eq1, eq2, set.mem_Ioc, set.mem_Icc] at ⊢ hx, split, linarith, linarith, end /-Theorem $$ \frac{\mathrm{d}\exp(t-x)}{\mathrm{d}x}=-\exp(t-x) $$ -/ theorem deriv_exp_t_x (t : ℝ) : deriv (λ x, real.exp (t-x)) = -(λ x, real.exp (t-x)) := begin have triv : (λ x, real.exp (t-x)) = real.exp ∘ (λ x, t - x) := by simp only [], ext, rw triv, rw deriv.scomp, simp only [neg_mul_eq_neg_mul_symm, deriv_exp, differentiable_at_const, mul_one, algebra.id.smul_eq_mul, one_mul, zero_sub, deriv_sub, differentiable_at_id', pi.neg_apply, deriv_id'', deriv_const'], simp only [differentiable_at_id', differentiable_at.exp], apply differentiable_at.const_sub, exact differentiable_at_id, end /-Theorem $$ \frac{\mathrm{d}-\exp(t-x)}{\mathrm{d}x}=\exp(t-x) $$ -/ theorem deriv_exp_t_x' (t : ℝ) : (deriv (λ x, - (real.exp (t-x)))) = (λ x, real.exp (t-x)) := begin simp only [deriv_exp, differentiable_at_const, mul_one, zero_sub, deriv_sub, differentiable_at_id', deriv_id'', deriv.neg', deriv_const', mul_neg_eq_neg_mul_symm, differentiable_at.sub, neg_neg], end /-- # about I -/ /-Definition Suppose $f$ is an integer polynomial with degree $n$ and $t\ge0$ then define \[I(f,t):=\int_0^t \exp(t-x)f(z)\mathrm{d}x\] We use integration by parts to prove \[I(f,t)=\exp(t)\left(\sum_{i=0}^n f^{(i)}(0)\right)-\sum_{i=0}^n f^{(i)}(t)\] The two different ways of representing $I(f,t)$ we give us upper bound and lower bound when we are using this on transcendence of $e$. -/ def I (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : ℝ := t.exp * (∑ i in finset.range f.nat_degree.succ, (f_eval_on_ℝ (deriv_n f i) 0)) - (∑ i in finset.range f.nat_degree.succ, (f_eval_on_ℝ (deriv_n f i) t)) def II (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : ℝ := ∫ x in 0..t, real.exp(t - x) * (f_eval_on_ℝ f x) /-Theorem $I(0,t)$ is 0. -/ theorem II_0 (t : ℝ) (ht : t ≥ 0) : II 0 t ht = 0 := begin -- We are integrating $\exp(t-x)\times 0$ rw II, unfold f_eval_on_ℝ, simp only [mul_zero, polynomial.eval_zero, polynomial.map_zero], rw interval_integral.integral_of_le ht, apply measure_theory.integral_zero, end /-Theorem By integration by part we have: \[I(f, t) = e^tf(0)-f(t)+I(f',t)\] -/ lemma II_integrate_by_part (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : (II f t ht) = (real.exp t) * (f_eval_on_ℝ f 0) - (f_eval_on_ℝ f t) + (II f.derivative t ht) := begin rw II, -- We have $$\int_0^t \exp(t-x)f(x)\mathrm{d}x=\int_0^t f(x)\frac{\mathrm{d}}{\mathrm{d}x}(-\exp(t-x))\mathrm{d}x$$ have eq : (∫ x in 0..t, (t - x).exp * f_eval_on_ℝ f x) = (∫ x in 0..t, f_eval_on_ℝ f x * (deriv (λ x, - (real.exp (t-x))) x)), { -- apply congr_arg, rw interval_integral.integral_of_le ht, rw interval_integral.integral_of_le ht, apply same_integral, ext, simp only [deriv_exp_t_x'], ring, }, rw eq, -- Apply integration by part to $$\int_0^t f(x)\frac{\mathrm{d}}{\mathrm{d}x}(-\exp(t-x))\mathrm{d}x$$. replace eq := integrate_by_part (f_eval_on_ℝ f) (λ (x : ℝ), -(t - x).exp) 0 t ht, rw eq, simp only [mul_one, neg_sub_neg, real.exp_zero, sub_zero, mul_neg_eq_neg_mul_symm, sub_self], replace eq : (∫ x in 0..t, -(deriv (f_eval_on_ℝ f) x * (t - x).exp)) = ∫ x in 0..t, -((λ x, (deriv (f_eval_on_ℝ f) x * (t - x).exp)) x), { rw interval_integral.integral_of_le ht, }, rw eq, rw interval_integral.integral_neg, simp only [sub_neg_eq_add], rw II, replace eq : (∫ (x : ℝ) in 0..t, (t - x).exp * f_eval_on_ℝ f.derivative x) = ∫ (x : ℝ) in 0..t, deriv (f_eval_on_ℝ f) x * (t - x).exp, { rw interval_integral.integral_of_le ht, rw interval_integral.integral_of_le ht, apply same_integral, ext, ring, rw f_eval_on_ℝ, rw derivative_emb, rw <-polynomial.deriv, have triv : deriv (λ (x : ℝ), polynomial.eval x (polynomial.map ℤembℝ f)) x = deriv (f_eval_on_ℝ f) x, { apply same_deriv, ext, rw f_eval_on_ℝ, }, rw triv, ring, }, rw eq, ring, apply polynomial.differentiable, apply continuous.measurable, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, rw f_eval_on_ℝ_deriv, apply polynomial.differentiable, apply continuous.measurable, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, apply polynomial.differentiable, rw f_eval_on_ℝ_deriv, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, apply polynomial.differentiable, apply differentiable.neg, change differentiable ℝ (real.exp ∘ (λ (y : ℝ), (t - y))), apply differentiable.comp, exact real.differentiable_exp, have : (λ (y : ℝ), t - y) = (λ (y : ℝ), -(y-t)), ext y, rw neg_sub, rw this, apply differentiable.neg, apply differentiable.sub_const, exact differentiable_id', apply continuous.measurable, rw deriv_exp_t_x', change continuous (real.exp ∘ (λ (y : ℝ), (t - y))), apply continuous.comp (real.continuous_exp) _, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, have : (λ (y : ℝ), t - y) = (λ (y : ℝ), -(y-t)), ext y, rw neg_sub, rw this, apply differentiable.neg, apply differentiable.sub_const, exact differentiable_id', apply continuous.measurable, apply continuous.neg _, exact normed_top_group, change continuous (real.exp ∘ (λ (y : ℝ), (t - y))), apply continuous.comp (real.continuous_exp) _, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, have : (λ (y : ℝ), t - y) = (λ (y : ℝ), -(y-t)), ext y, rw neg_sub, rw this, apply differentiable.neg, apply differentiable.sub_const, exact differentiable_id', rw deriv_exp_t_x', change continuous (real.exp ∘ (λ (y : ℝ), (t - y))), apply continuous.comp (real.continuous_exp) _, apply @differentiable.continuous ℝ _ ℝ _ _ ℝ _ _ _ _, have : (λ (y : ℝ), t - y) = (λ (y : ℝ), -(y-t)), ext y, rw neg_sub, rw this, apply differentiable.neg, apply differentiable.sub_const, exact differentiable_id', end /-Theorem Combine the theorem above with induction we get for all $m\in\mathbb N$ \[ I(f,t)=e^t\sum_{i=0}^m f^{(i)}(0)-\sum_{i=0}^m f^{(i)}(t) \] -/ lemma II_integrate_by_part_m (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) (m : ℕ) : II f t ht = t.exp * (∑ i in finset.range (m+1), (f_eval_on_ℝ (deriv_n f i) 0)) - (∑ i in finset.range (m+1), f_eval_on_ℝ (deriv_n f i) t) + (II (deriv_n f (m+1)) t ht) := begin induction m with m ih, rw [deriv_n,II_integrate_by_part], simp only [function.iterate_one, finset.sum_singleton, finset.range_one], rw deriv_n, simp only [id.def, function.iterate_zero], rw [ih, II_integrate_by_part], have triv : m.succ + 1 = (m+1).succ := by ring, rw triv, generalize hM : m + 1 = M, replace triv : t.exp * ∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) 0 - ∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) t + (t.exp * f_eval_on_ℝ (deriv_n f M) 0 - f_eval_on_ℝ (deriv_n f M) t + II (deriv_n f M).derivative t ht) = t.exp * ((∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) 0) + (f_eval_on_ℝ (deriv_n f M) 0)) - ((∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) t) + f_eval_on_ℝ (deriv_n f M) t) + II (deriv_n f M).derivative t ht := by ring, rw triv, replace triv : ∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) 0 + f_eval_on_ℝ (deriv_n f M) 0 = ∑ (i : ℕ) in finset.range M.succ, f_eval_on_ℝ (deriv_n f i) 0, rw finset.sum_range_succ, ring, rw triv, replace triv : (∑ (i : ℕ) in finset.range M, f_eval_on_ℝ (deriv_n f i) t + f_eval_on_ℝ (deriv_n f M) t) = (∑ (i : ℕ) in finset.range M.succ, f_eval_on_ℝ (deriv_n f i) t), rw finset.sum_range_succ, ring, rw triv, replace triv : (deriv_n f M).derivative= (deriv_n f M.succ), { conv_rhs {rw deriv_n}, rw function.iterate_succ', replace triv : (polynomial.derivative ∘ (polynomial.derivative^[M])) f = (polynomial.derivative (polynomial.derivative^[M] f)) := rfl, rw triv, rw <-deriv_n, }, rw triv, end /-Theorem So the using if $f$ has degree $n$, then $f^{(n+1)}$ is zero we have the two definition of $I(f,t)$ agrees. -/ theorem II_eq_I (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : II f t ht = I f t ht := begin have II_integrate_by_part_m := II_integrate_by_part_m f t ht f.nat_degree, have triv := deriv_too_much f, rw I, rw [triv, II_0, add_zero] at II_integrate_by_part_m, assumption, end /-Theorem \[\left|I(f,t)\right|\le \int_0^t \left|e^{t-x}f(x)\right|\mathrm{d}x\] -/ lemma abs_II_le1 (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : abs (II f t ht) ≤ ∫ x in 0..t, abs ((t-x).exp * (f_eval_on_ℝ f x)) := begin have triv : (∫ x in 0..t, abs ((t-x).exp * (f_eval_on_ℝ f x))) = ∫ x in 0..t, ∥(t-x).exp * (f_eval_on_ℝ f x)∥, { apply congr_arg, refl, }, rw triv, replace triv : abs (II f t ht) = ∥ (∫ (x : ℝ) in 0..t, (t - x).exp * f_eval_on_ℝ f x) ∥, { rw real.norm_eq_abs, apply congr_arg, unfold II, }, rw triv, have ineq := @interval_integral.norm_integral_le_abs_integral_norm ℝ ℝ _ _ _ _ _ _ _ _ 0 t (λ x:ℝ, (t-x).exp * f_eval_on_ℝ f x) _, rw abs_of_nonneg at ineq, refine ineq, { rw interval_integral.integral_of_le ht, apply measure_theory.integral_nonneg_of_ae, apply (@measure_theory.ae_restrict_iff ℝ _ _ (set.Ioc 0 t) _ _).2, apply measure_theory.ae_of_all, intros x hx, simp only [normed_field.norm_mul, pi.zero_apply], apply mul_nonneg, exact norm_nonneg (real.exp (t - x)), exact norm_nonneg (f_eval_on_ℝ f x), simp only [normed_field.norm_mul, pi.zero_apply], apply is_measurable.congr (is_measurable.univ), ext, split, intros hx, simp only [set.mem_univ, set.mem_set_of_eq] at hx ⊢, apply mul_nonneg, exact norm_nonneg _, exact norm_nonneg _, simp only [set.mem_univ, forall_true_iff], }, end /-- # $\bar{f}$ - Say $f(T)=a_0+a_1T+a_2T^2+\cdots+a_nT^n$. Then $\bar{f}=|a_0|+|a_1|T+|a_2|T^2+\cdots+|a_n|T^n$ - We proved some theorems about $\bar{f}$ -/ def f_bar (f : ℤ[X]) : ℤ[X] := { support := f.support, to_fun := λ n, abs (f.coeff n), mem_support_to_fun := λ n, ⟨λ hn, begin simp only [abs_eq_zero, ne.def], have h := (f.3 n).1 hn, simp only [ne.def] at h, assumption end, λ hn, begin simp only [abs_eq_zero, ne.def] at hn, apply (f.3 n).2, simpa only [], end⟩} /-Theorem By our construction the $n$-th coefficient of $\bar{f}$ is the absolute value of $n$-th coefficient of $f$ -/ theorem bar_coeff (f : ℤ[X]) (n : ℕ) : (f_bar f).coeff n = abs (f.coeff n) := begin -- true by definition dsimp [f_bar], refl, end /-Theorem By our construction, $\bar{f}$ and $f$ has the same support -/ theorem bar_supp (f : ℤ[X]) : (f_bar f).1 = f.1 := begin -- true by definition dsimp [f_bar], refl, end /-Theorem Since $\bar{f}$ and $f$ has the same support, they have the same degree. -/ theorem bar_same_deg (f : ℤ[X]) : (f_bar f).nat_degree = f.nat_degree := begin apply polynomial.nat_degree_eq_of_degree_eq, -- degree is defined to be $\sup$ of support. Since support of $\bar{f}$ and $f$ are the same, their degree is the same. rw polynomial.degree, rw polynomial.degree, rw bar_supp, end /-Theorem $\bar{0}=0$ -/ theorem f_bar_0 : f_bar 0 = 0 := begin ext, rw bar_coeff, simp only [abs_zero, polynomial.coeff_zero], end /-Theorem for any $f\in\mathbb Z$, if $\bar{f}=0$ then $f=0$ -/ theorem f_bar_eq_0 (f : ℤ[X]) : f_bar f = 0 -> f = 0 := begin intro h, rw polynomial.ext_iff at h, ext, have hn := h n, simp only [polynomial.coeff_zero] at hn, rw bar_coeff at hn, simp only [abs_eq_zero, polynomial.coeff_zero] at hn ⊢, assumption, end private lemma coeff_sum (f : ℕ -> (ℤ[X])) (m : ℕ) (s : finset ℕ) : (∑ i in s, (f i).coeff m) = (∑ i in s, f i).coeff m := begin apply finset.induction_on s, simp only [finset.sum_empty, polynomial.coeff_zero], intros a s ha, simp only [forall_prop_of_true, polynomial.finset_sum_coeff], end theorem coeff_f_bar_mul (f g : ℤ[X]) (n : ℕ) : (f_bar (f*g)).coeff n = abs(∑ p in finset.nat.antidiagonal n, (f.coeff p.1)*(g.coeff p.2)) := begin rw bar_coeff (f*g) n, rw polynomial.coeff_mul, end theorem f_bar_eq (f : ℤ[X]) : f_bar f = ∑ i in finset.range f.nat_degree.succ, polynomial.C (abs (f.coeff i)) * polynomial.X^i := begin ext, rw bar_coeff, rw <-coeff_sum, simp_rw [polynomial.coeff_C_mul_X], simp only [finset.mem_range, finset.sum_ite_eq], split_ifs, refl, simp only [not_lt] at h, rw polynomial.coeff_eq_zero_of_nat_degree_lt h, exact rfl, end /-Theorem For any $x\in(0,t)$ $|f(x)|\le \bar{f}(t)$ -/ lemma f_bar_ineq (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : ∀ x ∈ set.Icc 0 t, abs (f_eval_on_ℝ f x) ≤ f_eval_on_ℝ (f_bar f) t := begin intros x hx, -- If we write $f(X)=a_0+a_1X+\cdots+a_nX^n$. Then $f(x)=a_0+a_1x+\cdots+a_nx^n$ have lhs : f_eval_on_ℝ f x = ∑ i in f.support, (f.coeff i : ℝ) * x ^ i, { rw [f_eval_on_ℝ, polynomial.eval_map, polynomial.eval₂, finsupp.sum], apply congr_arg, ext, norm_cast, }, rw lhs, -- So $|f(x)|=|a_0+a_1x+\cdots+a_nx^n|\le |a_0|+|a_1|x+\cdots+|a_n|x^n$. (We assumed $x\ge0$). have ineq1 : abs (∑ (i : ℕ) in f.support, (f.coeff i:ℝ) * x ^ i) ≤ ∑ i in f.support, (abs (f.coeff i:ℝ) * (x ^ i)), { -- we have $|a_0+a_1x+\cdots+a_nx^n|\le|a_0|+|a_1x|+\cdots+|a_nx^n|$ have ineq1' := @finset.abs_sum_le_sum_abs ℝ ℕ _ (λ i, (f.coeff i:ℝ) * (x ^ i)) f.support, simp only [] at ineq1', -- and $|a_0|+|a_1x|+\cdots+|a_nx^n|=|a_0|+|a_1|x+\cdots+|a_n|x^n$ have eq1 : ∑ (x_1 : ℕ) in f.support, abs (↑(f.coeff x_1) * x ^ x_1) = ∑ (x_1 : ℕ) in f.support, abs (↑(f.coeff x_1)) * x ^ x_1, { apply congr_arg, ext, rw abs_mul, rw @abs_of_nonneg ℝ _ (x^x_1) _, apply pow_nonneg, exact (set.mem_Icc.1 hx).1, }, rw eq1 at ineq1', exact ineq1', }, -- $\bar{f}(t)=|a_0|+|a_1|t+\cdots+|a_n|t^n$ have rhs : f_eval_on_ℝ (f_bar f) t = ∑ i in (f_bar f).support, abs (f.coeff i:ℝ) * t ^ i, { rw [f_eval_on_ℝ, polynomial.eval_map, polynomial.eval₂, finsupp.sum], apply congr_arg, ext, norm_cast, }, rw rhs, -- Since $x^i\le t^i$, we have $|a_0|+|a_1|x+\cdots+|a_n|x^n\le|a_0|+|a_1|t+\cdots+|a_n|t^n$ have ineq2 : ∑ (i : ℕ) in f.support, abs (f.coeff i:ℝ) * x ^ i ≤ ∑ i in (f_bar f).support, abs (f.coeff i:ℝ) * t ^ i, { rw bar_supp, apply finset.sum_le_sum, intros n hn, suffices : x ^ n ≤ t ^ n, { apply mul_le_mul, exact le_refl (abs ↑(polynomial.coeff f n)), exact this, apply pow_nonneg, exact (set.mem_Icc.1 hx).1, exact abs_nonneg ↑(polynomial.coeff f n), }, apply pow_le_pow_of_le_left, exact (set.mem_Icc.1 hx).1, exact (set.mem_Icc.1 hx).2, }, -- combine `ineq1` and `ineq2` we are done. exact le_trans ineq1 ineq2, end private lemma continuous_exp_f (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : continuous (λ (x : ℝ), abs (real.exp (t - x) * f_eval_on_ℝ f x)) := begin -- $|e^{t-x}f(x)|$ is composition of absolute value function and $e^{t-x}f(x)$ have eq1 : (λ (x : ℝ), abs (real.exp (t - x) * f_eval_on_ℝ f x)) = abs ∘ (λ (x : ℝ), (real.exp (t - x) * f_eval_on_ℝ f x)) := by simp only [eq_self_iff_true], rw eq1, -- We know that absolute value function is continuous. have cont1 := real.continuous_abs, -- We now prove that $e^{t-x}f(x)$ is continuous by proving $e^{t-x}$ and $f(x)$ are continuous. have cont2 : continuous (λ (x : ℝ), real.exp (t - x) * f_eval_on_ℝ f x), { -- $e^{t-x}$ is composition of $\exp$ and $t-x$. have eq2 : (λ x : ℝ, real.exp (t-x)) = real.exp ∘ (λ x : ℝ, t - x), simp only [], have cont21 : continuous (λ x : ℝ, real.exp (t-x)), rw eq2, -- $\exp$ is continuous have cont20 := real.continuous_exp, -- $t-x$ is the constant function $t$ minus the identity function have cont201 : continuous (λ (x : ℝ), t - x), have eq3 : (λ (x : ℝ), t - x) = (λ (x : ℝ), (λ _, t) x - id x), ext, simp only [id], rw eq3, -- constant function is continuous have cont3 : continuous (λ _ :ℝ, t), exact continuous_const, -- identity function is continuous have cont3' : continuous (@id ℝ), exact continuous_id, -- hence $t-x$ is continuous have cont33 := continuous.sub cont3 cont3', assumption, -- hence $e^{t-x}$ is continuous exact continuous.comp cont20 cont201, -- evaluating a polynomial is continuous have cont4 : continuous (λ x , f_eval_on_ℝ f x), unfold f_eval_on_ℝ, exact polynomial.continuous (polynomial.map ℤembℝ f), -- hence the product is continuous exact continuous.mul cont21 cont4, }, exact continuous.comp cont1 cont2, end /-Theorem $$ \int_0^t |e^{t-x}f(d)|\mathrm{d}x\le te^t\bar{f}(t) $$ -/ private lemma II_le2' (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : (∫ x in 0..t, abs ((t-x).exp * (f_eval_on_ℝ f x))) ≤ t * t.exp * (f_eval_on_ℝ (f_bar f) t) := begin -- we are using `integral_le_max_times_length` -- so we need to prove $|e^{t-x}f(x)|\le e^t\bar{f}(t)$ have ineq1 := integral_le_max_times_length ((λ x, abs ((t - x).exp * f_eval_on_ℝ f x))) 0 t ht (t.exp * f_eval_on_ℝ (f_bar f) t) _ _, simp only [sub_zero] at ineq1, have triv : t * (t.exp * f_eval_on_ℝ (f_bar f) t) = t * t.exp * f_eval_on_ℝ (f_bar f) t := by ring, rw triv at ineq1, exact ineq1, -- This to prove the functions we used are measurable and integrable. { refine continuous.measurable _, apply continuous_exp_f _ _ ht, }, -- This is to prove $0\le|e^{t-x}f(x)|$ { intros x hx, simp only [ge_iff_le], exact abs_nonneg (real.exp (t - x) * f_eval_on_ℝ f x), }, -- This is to prove $|e^{t-x}f(x)|\le e^t \bar{f}(t)$ { intros x hx, simp only [set.indicator, set.mem_Icc], rw abs_mul, have triv : abs (t - x).exp = (t-x).exp, { apply abs_of_pos, exact (t - x).exp_pos, }, rw triv, -- We have $|f(x)|\le\bar{f}(t)$ have ineq1 := f_bar_ineq f t ht x hx, -- We have $e^{t-x}\le e^{t}$ have ineq2 : (t - x).exp ≤ t.exp, { rw real.exp_le_exp, rw sub_le, simp only [sub_self], exact hx.1, }, exact mul_le_mul ineq2 ineq1 (abs_nonneg _) (le_of_lt (real.exp_pos t)), }, end /-Theorem $$|I(f,t)|\le te^t\bar{f}(t)$$ -/ theorem abs_II_le2 (f : ℤ[X]) (t : ℝ) (ht : t ≥ 0) : abs (II f t ht) ≤ t * t.exp * (f_eval_on_ℝ (f_bar f) t) := begin -- combine `abs_II_le1` and previous lemma. have ineq1 := (abs_II_le1 f t ht), have ineq2 := (II_le2' f t ht), exact le_trans (abs_II_le1 f t ht) (II_le2' f t ht), end
ebfa5fe4f5f9373e8c8ea8fc7d310ff14097aaef
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/order/rel_iso.lean
3e409717f4b5fc0807d8a711ccbe77054a9c9114
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
29,284
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import logic.embedding import order.rel_classes import data.set.intervals.basic open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- A relation homomorphism with respect to a given pair of relations `r` and `s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/ @[nolint has_inhabited_instance] structure rel_hom {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) := (to_fun : α → β) (map_rel' : ∀ {a b}, r a b → s (to_fun a) (to_fun b)) infix ` →r `:25 := rel_hom namespace rel_hom instance : has_coe_to_fun (r →r s) := ⟨λ _, α → β, λ o, o.to_fun⟩ theorem map_rel (f : r →r s) : ∀ {a b}, r a b → s (f a) (f b) := f.map_rel' @[simp] theorem coe_fn_mk (f : α → β) (o) : (@rel_hom.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_fun (f : r →r s) : (f.to_fun : α → β) = f := rfl /-- The map `coe_fn : (r →r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r →r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation homomorphism. -/ @[refl] protected def id (r : α → α → Prop) : r →r r := ⟨id, λ a b, id⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans] protected def comp (g : s →r t) (f : r →r s) : r →r t := ⟨g.1 ∘ f.1, λ a b h, g.2 (f.2 h)⟩ @[simp] theorem id_apply (x : α) : rel_hom.id r x = x := rfl @[simp] theorem comp_apply (g : s →r t) (f : r →r s) (a : α) : (g.comp f) a = g (f a) := rfl /-- A relation homomorphism is also a relation homomorphism between dual relations. -/ protected def swap (f : r →r s) : swap r →r swap s := ⟨f, λ a b, f.map_rel⟩ /-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/ def preimage (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s := ⟨f, λ a b, id⟩ protected theorem is_irrefl : ∀ (f : r →r s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o h)⟩ protected theorem is_asymm : ∀ (f : r →r s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o h₁) (o h₂)⟩ protected theorem acc (f : r →r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r →r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ lemma map_inf {α β : Type*} [semilattice_inf α] [linear_order β] (a : ((<) : β → β → Prop) →r ((<) : α → α → Prop)) (m n : β) : a (m ⊓ n) = a m ⊓ a n := begin symmetry, cases le_or_lt n m with h, { rw [inf_eq_right.mpr h, inf_eq_right], exact strict_mono.monotone (λ x y, a.map_rel) h, }, { rw [inf_eq_left.mpr (le_of_lt h), inf_eq_left], exact le_of_lt (a.map_rel h), }, end lemma map_sup {α β : Type*} [semilattice_sup α] [linear_order β] (a : ((>) : β → β → Prop) →r ((>) : α → α → Prop)) (m n : β) : a (m ⊔ n) = a m ⊔ a n := begin symmetry, cases le_or_lt m n with h, { rw [sup_eq_right.mpr h, sup_eq_right], exact strict_mono.monotone (λ x y, a.swap.map_rel) h, }, { rw [sup_eq_left.mpr (le_of_lt h), sup_eq_left], exact le_of_lt (a.map_rel h), }, end end rel_hom /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀{x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end /-- An increasing function is injective -/ lemma rel_hom.injective_of_increasing [is_trichotomous α r] [is_irrefl β s] (f : r →r s) : injective f := injective_of_increasing r s f (λ x y, f.map_rel) theorem surjective.well_founded_iff {f : α → β} (hf : surjective f) (o : ∀ {a b}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s := iff.intro (begin apply rel_hom.well_founded, refine rel_hom.mk _ _, {exact classical.some hf.has_right_inverse}, intros a b h, apply o.2, convert h, iterate 2 { apply classical.some_spec hf.has_right_inverse }, end) (rel_hom.well_founded ⟨f, λ _ _, o.1⟩) /-- A relation embedding with respect to a given pair of relations `r` and `s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/ structure rel_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (map_rel_iff' : ∀ {a b}, s (to_embedding a) (to_embedding b) ↔ r a b) infix ` ↪r `:25 := rel_embedding /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/ abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] := @rel_embedding α β (≤) (≤) infix ` ↪o `:25 := order_embedding /-- The induced relation on a subtype is an embedding under the natural inclusion. -/ definition subtype.rel_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ↪r r := ⟨embedding.subtype p, λ x y, iff.rfl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace rel_embedding /-- A relation embedding is also a relation homomorphism -/ def to_rel_hom (f : r ↪r s) : (r →r s) := { to_fun := f.to_embedding.to_fun, map_rel' := λ x y, (map_rel_iff' f).mpr } instance : has_coe (r ↪r s) (r →r s) := ⟨to_rel_hom⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ↪r s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ @[simp] lemma to_rel_hom_eq_coe (f : r ↪r s) : f.to_rel_hom = f := rfl @[simp] lemma coe_coe_fn (f : r ↪r s) : ((f : r →r s) : α → β) = f := rfl theorem injective (f : r ↪r s) : injective f := f.inj' theorem map_rel_iff (f : r ↪r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@rel_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ↪r s) : (f.to_embedding : α → β) = f := rfl /-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r ↪r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation embedding. -/ @[refl] protected def refl (r : α → α → Prop) : r ↪r r := ⟨embedding.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation embeddings is a relation embedding. -/ @[trans] protected def trans (f : r ↪r s) (g : s ↪r t) : r ↪r t := ⟨f.1.trans g.1, λ a b, by simp [f.map_rel_iff, g.map_rel_iff]⟩ instance (r : α → α → Prop) : inhabited (r ↪r r) := ⟨rel_embedding.refl _⟩ @[simp] theorem refl_apply (x : α) : rel_embedding.refl r x = x := rfl theorem trans_apply (f : r ↪r s) (g : s ↪r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem coe_trans (f : r ↪r s) (g : s ↪r t) : ⇑(f.trans g) = g ∘ f := rfl /-- A relation embedding is also a relation embedding between dual relations. -/ protected def swap (f : r ↪r s) : swap r ↪r swap s := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- If `f` is injective, then it is a relation embedding from the preimage relation of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ↪r s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ↪r s) : r = f ⁻¹'o s := by { ext a b, exact f.map_rel_iff.symm } protected theorem is_irrefl (f : r ↪r s) [is_irrefl β s] : is_irrefl α r := ⟨λ a, mt f.map_rel_iff.2 (irrefl (f a))⟩ protected theorem is_refl (f : r ↪r s) [is_refl β s] : is_refl α r := ⟨λ a, f.map_rel_iff.1 $ refl _⟩ protected theorem is_symm (f : r ↪r s) [is_symm β s] : is_symm α r := ⟨λ a b, imp_imp_imp f.map_rel_iff.2 f.map_rel_iff.1 symm⟩ protected theorem is_asymm (f : r ↪r s) [is_asymm β s] : is_asymm α r := ⟨λ a b h₁ h₂, asymm (f.map_rel_iff.2 h₁) (f.map_rel_iff.2 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ↪r s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_trans : ∀ (f : r ↪r s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.1 (H _ _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_total : ∀ (f : r ↪r s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).1 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ↪r s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ↪r s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ↪r s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ↪r s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ↪r s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff o)).1 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ↪r s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ↪r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel_iff.2 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ↪r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ↪r s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict relations to show it is a relation embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ↪r s := begin haveI := @is_asymm.is_irrefl β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨λ h, _, H _ _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl /-- Embeddings of partial orders that preserve `<` also preserve `≤` -/ def order_embedding_of_lt_embedding [partial_order α] [partial_order β] (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : α ↪o β := { map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective] }, .. f } end rel_embedding namespace order_embedding variables [preorder α] [preorder β] (f : α ↪o β) /-- lt is preserved by order embeddings of preorders -/ def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) := { map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff], .. f } @[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl @[simp] theorem le_iff_le {a b} : (f a) ≤ (f b) ↔ a ≤ b := f.map_rel_iff @[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b := f.lt_embedding.map_rel_iff @[simp] lemma eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff protected theorem monotone : monotone f := λ x y, f.le_iff_le.2 protected theorem strict_mono : strict_mono f := λ x y, f.lt_iff_lt.2 protected theorem acc (a : α) : acc (<) (f a) → acc (<) a := f.lt_embedding.acc a protected theorem well_founded : well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) := f.lt_embedding.well_founded protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) := f.lt_embedding.is_well_order /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : order_dual α ↪o order_dual β := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- A sctrictly monotone map from a linear order is an order embedding. --/ def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β) (h : strict_mono f) : α ↪o β := { to_fun := f, inj' := strict_mono.injective h, map_rel_iff' := λ a b, h.le_iff_le } @[simp] lemma coe_of_strict_mono {α β} [linear_order α] [preorder β] {f : α → β} (h : strict_mono f) : ⇑(of_strict_mono f h) = f := rfl /-- Embedding of a subtype into the ambient type as an `order_embedding`. -/ def subtype (p : α → Prop) : subtype p ↪o α := ⟨embedding.subtype p, λ x y, iff.rfl⟩ @[simp] lemma coe_subtype (p : α → Prop) : ⇑(subtype p) = coe := rfl end order_embedding /-- A relation isomorphism is an equivalence that is also a relation embedding. -/ structure rel_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (map_rel_iff' : ∀ {a b}, s (to_equiv a) (to_equiv b) ↔ r a b) infix ` ≃r `:25 := rel_iso /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_iso (≤) (≤)`. -/ abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤) infix ` ≃o `:25 := order_iso namespace rel_iso /-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s` in the target type. -/ def to_rel_embedding (f : r ≃r s) : r ↪r s := ⟨f.to_equiv.to_embedding, f.map_rel_iff'⟩ instance : has_coe (r ≃r s) (r ↪r s) := ⟨to_rel_embedding⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ≃r s) := ⟨λ _, α → β, λ f, f⟩ @[simp] lemma to_rel_embedding_eq_coe (f : r ≃r s) : f.to_rel_embedding = f := rfl @[simp] lemma coe_coe_fn (f : r ≃r s) : ((f : r ↪r s) : α → β) = f := rfl theorem map_rel_iff (f : r ≃r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ≃ β) (o : ∀ ⦃a b⦄, s (f a) (f b) ↔ r a b) : (rel_iso.mk f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃r s) : (f.to_equiv : α → β) = f := rfl theorem injective_to_equiv : injective (to_equiv : (r ≃r s) → α ≃ β) | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h } /-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse `function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/ theorem injective_coe_fn : function.injective (λ (e : r ≃r s) (x : α), e x) := equiv.injective_coe_fn.comp injective_to_equiv @[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g := injective_coe_fn (funext h) theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation isomorphism. -/ @[refl] protected def refl (r : α → α → Prop) : r ≃r r := ⟨equiv.refl _, λ a b, iff.rfl⟩ /-- Inverse map of a relation isomorphism is a relation isomorphism. -/ @[symm] protected def symm (f : r ≃r s) : s ≃r r := ⟨f.to_equiv.symm, λ a b, by erw [← f.map_rel_iff, f.1.apply_symm_apply, f.1.apply_symm_apply]⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans] protected def trans (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, f₂.map_rel_iff.trans f₁.map_rel_iff⟩ instance (r : α → α → Prop) : inhabited (r ≃r r) := ⟨rel_iso.refl _⟩ @[simp] lemma default_def (r : α → α → Prop) : default (r ≃r r) = rel_iso.refl r := rfl /-- a relation isomorphism is also a relation isomorphism between dual relations. -/ protected def swap (f : r ≃r s) : (swap r) ≃r (swap s) := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@rel_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : rel_iso.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≃r s) (g : s ≃r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : r ≃r s) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] theorem symm_apply_apply (e : r ≃r s) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x theorem rel_symm_apply (e : r ≃r s) {x y} : r x (e.symm y) ↔ s (e x) y := by rw [← e.map_rel_iff, e.apply_symm_apply] theorem symm_apply_rel (e : r ≃r s) {x y} : r (e.symm x) y ↔ s x (e y) := by rw [← e.map_rel_iff, e.apply_symm_apply] protected lemma bijective (e : r ≃r s) : bijective e := e.to_equiv.bijective protected lemma injective (e : r ≃r s) : injective e := e.to_equiv.injective protected lemma surjective (e : r ≃r s) : surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : r ≃r s) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma eq_iff_eq (f : r ≃r s) {a b} : f a = f b ↔ a = b := f.injective.eq_iff /-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/ protected def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃r s := ⟨f, λ a b, iff.rfl⟩ /-- A surjective relation embedding is a relation isomorphism. -/ noncomputable def of_surjective (f : r ↪r s) (H : surjective f) : r ≃r s := ⟨equiv.of_bijective f ⟨f.injective, H⟩, λ a b, f.map_rel_iff⟩ @[simp] theorem of_surjective_coe (f : r ↪r s) (H) : (of_surjective f H : α → β) = f := rfl /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the sum. -/ def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃r sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the product. -/ def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃r prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, by simp [prod.lex_def, e₁.map_rel_iff, e₂.map_rel_iff]⟩ instance : group (r ≃r r) := { one := rel_iso.refl r, mul := λ f₁ f₂, f₂.trans f₁, inv := rel_iso.symm, mul_assoc := λ f₁ f₂ f₃, rfl, one_mul := λ f, ext $ λ _, rfl, mul_one := λ f, ext $ λ _, rfl, mul_left_inv := λ f, ext f.symm_apply_apply } @[simp] lemma coe_one : ⇑(1 : r ≃r r) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : r ≃r r) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl lemma mul_apply (e₁ e₂ : r ≃r r) (x : α) : (e₁ * e₂) x = e₁ (e₂ x) := rfl @[simp] lemma inv_apply_self (e : r ≃r r) (x) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : r ≃r r) (x) : e (e⁻¹ x) = x := e.apply_symm_apply x end rel_iso namespace order_iso /-- Reinterpret an order isomorphism as an order embedding. -/ def to_order_embedding [has_le α] [has_le β] (e : α ≃o β) : α ↪o β := e.to_rel_embedding @[simp] lemma coe_to_order_embedding [has_le α] [has_le β] (e : α ≃o β) : ⇑(e.to_order_embedding) = e := rfl variables [preorder α] [preorder β] [preorder γ] protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono @[simp] lemma le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff @[simp] lemma lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y := e.to_order_embedding.lt_iff_lt protected lemma bijective (e : α ≃o β) : bijective e := e.to_equiv.bijective protected lemma injective (e : α ≃o β) : injective e := e.to_equiv.injective protected lemma surjective (e : α ≃o β) : surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : α ≃o β) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y := e.to_equiv.apply_eq_iff_eq /-- Inverse of an order isomorphism. -/ def symm (e : α ≃o β) : β ≃o α := e.symm @[simp] lemma apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x := e.to_equiv.symm_apply_eq @[simp] lemma symm_symm (e : α ≃o β) : e.symm.symm = e := by { ext, refl } lemma symm_injective : injective (symm : (α ≃o β) → (β ≃o α)) := λ e e' h, by rw [← e.symm_symm, h, e'.symm_symm] /-- Composition of two order isomorphisms is an order isomorphism. -/ @[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := e.trans e' @[simp] lemma coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl lemma trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl open set @[simp] lemma preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' (Iic b) = Iic (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' (Ici b) = Ici (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' (Iio b) = Iio (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' (Ioi b) = Ioi (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' (Icc a b) = Icc (e.symm a) (e.symm b) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' (Ico a b) = Ico (e.symm a) (e.symm b) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_Ioc (e : α ≃o β) (a b : β) : e ⁻¹' (Ioc a b) = Ioc (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_Ioo (e : α ≃o β) (a b : β) : e ⁻¹' (Ioo a b) = Ioo (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iio] /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. --/ def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] }, { to_fun := f, inv_fun := g, left_inv := λ a, (gf a).symm, right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] }, map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert (h _ _).symm, apply gf } } /-- Order isomorphism between two equal sets. -/ def set_congr (s t : set α) (h : s = t) : s ≃o t := { to_equiv := equiv.set_congr h, map_rel_iff' := λ x y, iff.rfl } /-- Order isomorphism between `univ : set α` and `α`. -/ def set.univ : (set.univ : set α) ≃o α := { to_equiv := equiv.set.univ α, map_rel_iff' := λ x y, iff.rfl } end order_iso /-- If a function `f` is strictly monotone on a set `s`, then it defines an order isomorphism between `s` and its image. -/ protected noncomputable def strict_mono_incr_on.order_iso {α β} [linear_order α] [preorder β] (f : α → β) (s : set α) (hf : strict_mono_incr_on f s) : s ≃o f '' s := { to_equiv := hf.inj_on.bij_on_image.equiv _, map_rel_iff' := λ x y, hf.le_iff_le x.2 y.2 } /-- A strictly monotone function from a linear order is an order isomorphism between its domain and its range. -/ protected noncomputable def strict_mono.order_iso {α β} [linear_order α] [preorder β] (f : α → β) (h_mono : strict_mono f) : α ≃o set.range f := { to_equiv := equiv.set.range f h_mono.injective, map_rel_iff' := λ a b, h_mono.le_iff_le } /-- A strictly monotone surjective function from a linear order is an order isomorphism. -/ noncomputable def strict_mono.order_iso_of_surjective {α β} [linear_order α] [preorder β] (f : α → β) (h_mono : strict_mono f) (h_surj : surjective f) : α ≃o β := (h_mono.order_iso f).trans $ (order_iso.set_congr _ _ h_surj.range_eq).trans order_iso.set.univ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := (coe : p → α) ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel /-- The relation embedding from the inherited relation on a subset. -/ protected def rel_embedding (r : α → α → Prop) (p : set α) : subrel r p ↪r r := ⟨embedding.subtype _, λ a b, iff.rfl⟩ @[simp] theorem rel_embedding_apply (r : α → α → Prop) (p a) : subrel.rel_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := rel_embedding.is_well_order (subrel.rel_embedding r p) end subrel /-- Restrict the codomain of a relation embedding. -/ def rel_embedding.cod_restrict (p : set β) (f : r ↪r s) (H : ∀ a, f a ∈ p) : r ↪r subrel s p := ⟨f.to_embedding.cod_restrict p H, f.map_rel_iff'⟩ @[simp] theorem rel_embedding.cod_restrict_apply (p) (f : r ↪r s) (H a) : rel_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def order_iso.dual [preorder α] [preorder β] (f : α ≃o β) : order_dual α ≃o order_dual β := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y := by { refine le_antisymm _ (hy _), rw [← f.apply_symm_apply y, f.map_rel_iff], apply hx } lemma order_iso.map_bot [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ := f.map_bot' (λ _, bot_le) (λ _, bot_le) lemma order_iso.map_top' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y := f.dual.map_bot' hx hy lemma order_iso.map_top [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ := f.dual.map_bot lemma order_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β] (f : α ↪o β) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := f.monotone.map_inf_le x y lemma order_iso.map_inf [semilattice_inf α] [semilattice_inf β] (f : α ≃o β) (x y : α) : f (x ⊓ y) = f x ⊓ f y := begin refine (f.to_order_embedding.map_inf_le x y).antisymm _, simpa [← f.symm.le_iff_le] using f.symm.to_order_embedding.map_inf_le (f x) (f y) end lemma order_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β] (f : α ↪o β) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := f.monotone.le_map_sup x y lemma order_iso.map_sup [semilattice_sup α] [semilattice_sup β] (f : α ≃o β) (x y : α) : f (x ⊔ y) = f x ⊔ f y := f.dual.map_inf x y end lattice_isos
ac5c7de6275eeabf84a521593daf20c78a06ea9e
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/enriched/two_category.lean
8e41958fd0d9df8565a28b940a4c5cdbb8ef9348
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
3,387
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import .enriched_category import ..examples.categories.cartesian_product open tqft.categories open tqft.categories.functor open tqft.categories.natural_transformation open tqft.categories.enriched open tqft.categories.products open tqft.categories.monoidal_category namespace tqft.categories.enriched.two_category definition {u} TwoCategory := EnrichedCategory CartesianProductOfCategories.{u u} definition horizontal_composition_on_FunctorCategories { C D E : Category } : Functor (ProductCategory (FunctorCategory C D) (FunctorCategory D E)) (FunctorCategory C E) := { onObjects := λ p, FunctorComposition p.1 p.2, onMorphisms := λ _ _ p, horizontal_composition_of_NaturalTransformations p.1 p.2, identities := ♯, functoriality := ♯ } definition {u} CAT : TwoCategory.{u} := { Obj := Category.{u u}, Hom := λ C D, FunctorCategory C D, compose := λ _ _ _, horizontal_composition_on_FunctorCategories, identity := λ C, { onObjects := λ _, IdentityFunctor C, onMorphisms := λ _ _ _, IdentityNaturalTransformation (IdentityFunctor C), identities := ♮, functoriality := ♯ }, left_identity := begin intros, dunfold CategoryOfCategoriesAndFunctors, simp, fapply Functors_pointwise_equal, begin intros, unfold IdentityFunctor at X_1, unfold FunctorCategory at X_1, unfold_unfoldable, fapply Functors_pointwise_equal, begin intros, unfold_unfoldable_goals, trivial end, begin intros, unfold_unfoldable_goals, trivial end end, begin intros, unfold IdentityFunctor at X_1, unfold FunctorCategory at X_1, dsimp at X_1, unfold IdentityFunctor at Y_1, unfold FunctorCategory at Y_1, dsimp at Y_1, unfold IdentityFunctor at f, unfold FunctorCategory at f, dsimp at f, apply NaturalTransformations_componentwise_equal, intros, unfold_unfoldable, -- FIXME ugh, and we hit the same problem with `eq.rec (eq.refl A) B` end end, right_identity := sorry, associativity := sorry } -- PROJECT strict n-categories; for this we'll need to define products of enriched categories, and show that (n-1) categories are symmetric. end tqft.categories.enriched.two_category
f7c8c51cfd255f3e6794f70fb21404148279e335
abd85493667895c57a7507870867b28124b3998f
/src/data/pnat/basic.lean
2240d061d62d3a9f987b21b56162539a35bd353c
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
13,473
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Neil Strickland -/ import data.nat.prime /-- `ℕ+` is the type of positive natural numbers. It is defined as a subtype, and the VM representation of `ℕ+` is the same as `ℕ` because the proof is not stored. -/ def pnat := {n : ℕ // 0 < n} notation `ℕ+` := pnat instance coe_pnat_nat : has_coe ℕ+ ℕ := ⟨subtype.val⟩ instance : has_repr ℕ+ := ⟨λ n, repr n.1⟩ namespace nat /-- Convert a natural number to a positive natural number. The positivity assumption is inferred by `dec_trivial`. -/ def to_pnat (n : ℕ) (h : 0 < n . tactic.exact_dec_trivial) : ℕ+ := ⟨n, h⟩ /-- Write a successor as an element of `ℕ+`. -/ def succ_pnat (n : ℕ) : ℕ+ := ⟨succ n, succ_pos n⟩ @[simp] theorem succ_pnat_coe (n : ℕ) : (succ_pnat n : ℕ) = succ n := rfl theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m → n = m := λ h, by { let h' := congr_arg (coe : ℕ+ → ℕ) h, exact nat.succ_inj h' } /-- Convert a natural number to a pnat. `n+1` is mapped to itself, and `0` becomes `1`. -/ def to_pnat' (n : ℕ) : ℕ+ := succ_pnat (pred n) @[simp] theorem to_pnat'_coe : ∀ (n : ℕ), ((to_pnat' n) : ℕ) = ite (0 < n) n 1 | 0 := rfl | (m + 1) := by {rw [if_pos (succ_pos m)], refl} namespace primes instance coe_pnat : has_coe nat.primes ℕ+ := ⟨λ p, ⟨(p : ℕ), p.property.pos⟩⟩ theorem coe_pnat_nat (p : nat.primes) : ((p : ℕ+) : ℕ) = p := rfl theorem coe_pnat_inj (p q : nat.primes) : (p : ℕ+) = (q : ℕ+) → p = q := λ h, begin replace h : ((p : ℕ+) : ℕ) = ((q : ℕ+) : ℕ) := congr_arg subtype.val h, rw [coe_pnat_nat, coe_pnat_nat] at h, exact subtype.eq h, end end primes end nat namespace pnat open nat /-- We now define a long list of structures on ℕ+ induced by similar structures on ℕ. Most of these behave in a completely obvious way, but there are a few things to be said about subtraction, division and powers. -/ instance : decidable_eq ℕ+ := λ (a b : ℕ+), by apply_instance instance : decidable_linear_order ℕ+ := subtype.decidable_linear_order _ @[simp] lemma mk_le_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) ≤ ⟨k, hk⟩ ↔ n ≤ k := iff.rfl @[simp] lemma mk_lt_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) < ⟨k, hk⟩ ↔ n < k := iff.rfl @[simp, norm_cast] lemma coe_le_coe (n k : ℕ+) : (n:ℕ) ≤ k ↔ n ≤ k := iff.rfl @[simp, norm_cast] lemma coe_lt_coe (n k : ℕ+) : (n:ℕ) < k ↔ n < k := iff.rfl @[simp] theorem pos (n : ℕ+) : 0 < (n : ℕ) := n.2 theorem eq {m n : ℕ+} : (m : ℕ) = n → m = n := subtype.eq @[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : ℕ+) : ℕ) = n := rfl instance : add_comm_semigroup ℕ+ := { add := λ a b, ⟨(a + b : ℕ), add_pos a.pos b.pos⟩, add_comm := λ a b, subtype.eq (add_comm a b), add_assoc := λ a b c, subtype.eq (add_assoc a b c) } @[simp] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl instance coe_add_hom : is_add_hom (coe : ℕ+ → ℕ) := ⟨add_coe⟩ instance : add_left_cancel_semigroup ℕ+ := { add_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_right_inj (a : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } instance : add_right_cancel_semigroup ℕ+ := { add_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_left_inj (b : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } @[simp] theorem ne_zero (n : ℕ+) : (n : ℕ) ≠ 0 := ne_of_gt n.2 theorem to_pnat'_coe {n : ℕ} : 0 < n → (n.to_pnat' : ℕ) = n := succ_pred_eq_of_pos @[simp] theorem coe_to_pnat' (n : ℕ+) : (n : ℕ).to_pnat' = n := eq (to_pnat'_coe n.pos) instance : comm_monoid ℕ+ := { mul := λ m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩, mul_assoc := λ a b c, subtype.eq (mul_assoc _ _ _), one := succ_pnat 0, one_mul := λ a, subtype.eq (one_mul _), mul_one := λ a, subtype.eq (mul_one _), mul_comm := λ a b, subtype.eq (mul_comm _ _) } theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b := λ a b, nat.lt_add_one_iff theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b := λ a b, nat.add_one_le_iff @[simp] lemma one_le (n : ℕ+) : (1 : ℕ+) ≤ n := n.2 instance : order_bot ℕ+ := { bot := 1, bot_le := λ a, a.property, ..(by apply_instance : partial_order ℕ+) } @[simp] lemma bot_eq_zero : (⊥ : ℕ+) = 1 := rfl instance : inhabited ℕ+ := ⟨1⟩ -- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals. @[simp] lemma mk_one {h} : (⟨1, h⟩ : ℕ+) = (1 : ℕ+) := rfl @[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl @[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl -- Some lemmas that rewrite inequalities between explicit numerals in `pnat` -- into the corresponding inequalities in `nat`. -- TODO: perhaps this should not be attempted by `simp`, -- and instead we should expect `norm_num` to take care of these directly? -- TODO: these lemmas are perhaps incomplete: -- * 1 is not represented as a bit0 or bit1 -- * strict inequalities? @[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] theorem one_coe : ((1 : ℕ+) : ℕ) = 1 := rfl @[simp] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl instance coe_mul_hom : is_monoid_hom (coe : ℕ+ → ℕ) := {map_one := one_coe, map_mul := mul_coe} @[simp] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl @[simp] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl @[simp] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n := by induction n with n ih; [refl, rw [nat.pow_succ, pow_succ, mul_coe, mul_comm, ih]] instance : left_cancel_semigroup ℕ+ := { mul_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_right_inj a.pos).mp h)}, .. (pnat.comm_monoid) } instance : right_cancel_semigroup ℕ+ := { mul_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_left_inj b.pos).mp h)}, .. (pnat.comm_monoid) } instance : distrib ℕ+ := { left_distrib := λ a b c, eq (mul_add a b c), right_distrib := λ a b c, eq (add_mul a b c), ..(pnat.add_comm_semigroup), ..(pnat.comm_monoid) } /-- Subtraction a - b is defined in the obvious way when a > b, and by a - b = 1 if a ≤ b. -/ instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩ theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 := begin change ((to_pnat' ((a : ℕ) - (b : ℕ)) : ℕ)) = ite ((a : ℕ) > (b : ℕ)) ((a : ℕ) - (b : ℕ)) 1, split_ifs with h, { exact to_pnat'_coe (nat.sub_pos_of_lt h) }, { rw [nat.sub_eq_zero_iff_le.mpr (le_of_not_gt h)], refl } end theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b := λ h, eq $ by { rw [add_coe, sub_coe, if_pos h], exact nat.add_sub_of_le (le_of_lt h) } /-- We define m % k and m / k in the same way as for nat except that when m = n * k we take m % k = k and m / k = n - 1. This ensures that m % k is always positive and m = (m % k) + k * (m / k) in all cases. Later we define a function div_exact which gives the usual m / k in the case where k divides m. -/ def mod_div_aux : ℕ+ → ℕ → ℕ → ℕ+ × ℕ | k 0 q := ⟨k, q.pred⟩ | k (r + 1) q := ⟨⟨r + 1, nat.succ_pos r⟩, q⟩ lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)), (((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q)) | k 0 0 h := (h ⟨rfl, rfl⟩).elim | k 0 (q + 1) h := by { change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1), rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]} | k (r + 1) q h := rfl def mod_div (m k : ℕ+) : ℕ+ × ℕ := mod_div_aux k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) def mod (m k : ℕ+) : ℕ+ := (mod_div m k).1 def div (m k : ℕ+) : ℕ := (mod_div m k).2 theorem mod_add_div (m k : ℕ+) : (m : ℕ) = (mod m k) + k * (div m k) := begin let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ), have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0), by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀, exact (m.ne_zero h₀.symm).elim }, have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this, exact (this.trans h₀).symm, end theorem mod_coe (m k : ℕ+) : ((mod m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) (k : ℕ) ((m : ℕ) % (k : ℕ)) := begin dsimp [mod, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem div_coe (m k : ℕ+) : ((div m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) ((m : ℕ) / (k : ℕ)).pred ((m : ℕ) / (k : ℕ)) := begin dsimp [div, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k := begin change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ), rw [mod_coe], split_ifs, { have hm : (m : ℕ) > 0 := m.pos, rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢, by_cases h' : ((m : ℕ) / (k : ℕ)) = 0, { rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim}, { let h' := nat.mul_le_mul_left (k : ℕ) (nat.succ_le_of_lt (nat.pos_of_ne_zero h')), rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } }, { exact ⟨nat.mod_le (m : ℕ) (k : ℕ), le_of_lt (nat.mod_lt (m : ℕ) k.pos)⟩ } end instance : has_dvd ℕ+ := ⟨λ k m, (k : ℕ) ∣ (m : ℕ)⟩ theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := by {refl} theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k := begin change (k : ℕ) ∣ (m : ℕ) ↔ mod m k = k, rw [nat.dvd_iff_mod_eq_zero], split, { intro h, apply eq, rw [mod_coe, if_pos h] }, { intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0, { exact h'}, { replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h, rw [mod_coe, if_neg h'] at h, exact (ne_of_lt (nat.mod_lt (m : ℕ) k.pos) h).elim } } end def div_exact {m k : ℕ+} (h : k ∣ m) : ℕ+ := ⟨(div m k).succ, nat.succ_pos _⟩ theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact h) = m := begin apply eq, rw [mul_coe], change (k : ℕ) * (div m k).succ = m, rw [mod_add_div m k, dvd_iff'.mp h, nat.mul_succ, add_comm], end theorem dvd_iff'' {k n : ℕ+} : k ∣ n ↔ ∃ m, k * m = n := ⟨λ h, ⟨div_exact h, mul_div_exact h⟩, λ ⟨m, h⟩, dvd.intro (m : ℕ) ((mul_coe k m).symm.trans (congr_arg subtype.val h))⟩ theorem dvd_intro {k n : ℕ+} (m : ℕ+) (h : k * m = n) : k ∣ n := dvd_iff''.mpr ⟨m, h⟩ theorem dvd_refl (m : ℕ+) : m ∣ m := dvd_intro 1 (mul_one m) theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n := λ hmn hnm, subtype.eq (nat.dvd_antisymm hmn hnm) protected theorem dvd_trans {k m n : ℕ+} : k ∣ m → m ∣ n → k ∣ n := @dvd_trans ℕ _ (k : ℕ) (m : ℕ) (n : ℕ) theorem one_dvd (n : ℕ+) : 1 ∣ n := dvd_intro n (one_mul n) theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 := ⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩ def gcd (n m : ℕ+) : ℕ+ := ⟨nat.gcd (n : ℕ) (m : ℕ), nat.gcd_pos_of_pos_left (m : ℕ) n.pos⟩ def lcm (n m : ℕ+) : ℕ+ := ⟨nat.lcm (n : ℕ) (m : ℕ), by { let h := mul_pos n.pos m.pos, rw [← gcd_mul_lcm (n : ℕ) (m : ℕ), mul_comm] at h, exact pos_of_dvd_of_pos (dvd.intro (nat.gcd (n : ℕ) (m : ℕ)) rfl) h }⟩ @[simp] theorem gcd_coe (n m : ℕ+) : ((gcd n m) : ℕ) = nat.gcd n m := rfl @[simp] theorem lcm_coe (n m : ℕ+) : ((lcm n m) : ℕ) = nat.lcm n m := rfl theorem gcd_dvd_left (n m : ℕ+) : (gcd n m) ∣ n := nat.gcd_dvd_left (n : ℕ) (m : ℕ) theorem gcd_dvd_right (n m : ℕ+) : (gcd n m) ∣ m := nat.gcd_dvd_right (n : ℕ) (m : ℕ) theorem dvd_gcd {m n k : ℕ+} (hm : k ∣ m) (hn : k ∣ n) : k ∣ gcd m n := @nat.dvd_gcd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem dvd_lcm_left (n m : ℕ+) : n ∣ lcm n m := nat.dvd_lcm_left (n : ℕ) (m : ℕ) theorem dvd_lcm_right (n m : ℕ+) : m ∣ lcm n m := nat.dvd_lcm_right (n : ℕ) (m : ℕ) theorem lcm_dvd {m n k : ℕ+} (hm : m ∣ k) (hn : n ∣ k) : lcm m n ∣ k := @nat.lcm_dvd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem gcd_mul_lcm (n m : ℕ+) : (gcd n m) * (lcm n m) = n * m := subtype.eq (nat.gcd_mul_lcm (n : ℕ) (m : ℕ)) def prime (p : ℕ+) : Prop := (p : ℕ).prime end pnat
deaac220bae4542c3b08fb4b13f8e161883f549d
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/geometry/manifold/times_cont_mdiff_map.lean
a66010898bfa938d242ccb051306036bef3a8088
[ "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
3,693
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import geometry.manifold.times_cont_mdiff import topology.continuous_function.basic /-! # Smooth bundled map In this file we define the type `times_cont_mdiff_map` of `n` times continuously differentiable bundled maps. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') (M : Type*) [topological_space M] [charted_space H M] (M' : Type*) [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] (n : with_top ℕ) /-- Bundled `n` times continuously differentiable maps. -/ @[protect_proj] structure times_cont_mdiff_map := (to_fun : M → M') (times_cont_mdiff_to_fun : times_cont_mdiff I I' n to_fun) /-- Bundled smooth maps. -/ @[reducible] def smooth_map := times_cont_mdiff_map I I' M M' ⊤ localized "notation `C^` n `⟮` I `, ` M `; ` I' `, ` M' `⟯` := times_cont_mdiff_map I I' M M' n" in manifold localized "notation `C^` n `⟮` I `, ` M `; ` k `⟯` := times_cont_mdiff_map I (model_with_corners_self k k) M k n" in manifold open_locale manifold namespace times_cont_mdiff_map variables {I} {I'} {M} {M'} {n} instance : has_coe_to_fun C^n⟮I, M; I', M'⟯ := ⟨_, times_cont_mdiff_map.to_fun⟩ instance : has_coe C^n⟮I, M; I', M'⟯ C(M, M') := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff_to_fun.continuous⟩⟩ variables {f g : C^n⟮I, M; I', M'⟯} protected lemma times_cont_mdiff (f : C^n⟮I, M; I', M'⟯) : times_cont_mdiff I I' n f := f.times_cont_mdiff_to_fun protected lemma smooth (f : C^∞⟮I, M; I', M'⟯) : smooth I I' f := f.times_cont_mdiff_to_fun protected lemma mdifferentiable' (f : C^n⟮I, M; I', M'⟯) (hn : 1 ≤ n) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable hn protected lemma mdifferentiable (f : C^∞⟮I, M; I', M'⟯) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable le_top protected lemma mdifferentiable_at (f : C^∞⟮I, M; I', M'⟯) {x} : mdifferentiable_at I I' f x := f.mdifferentiable x lemma coe_inj ⦃f g : C^n⟮I, M; I', M'⟯⦄ (h : (f : M → M') = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext h /-- The identity as a smooth map. -/ def id : C^n⟮I, M; I, M⟯ := ⟨id, times_cont_mdiff_id⟩ /-- The composition of smooth maps, as a smooth map. -/ def comp (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) : C^n⟮I, M; I'', M''⟯ := { to_fun := λ a, f (g a), times_cont_mdiff_to_fun := f.times_cont_mdiff_to_fun.comp g.times_cont_mdiff_to_fun, } @[simp] lemma comp_apply (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) (x : M) : f.comp g x = f (g x) := rfl instance [inhabited M'] : inhabited C^n⟮I, M; I', M'⟯ := ⟨⟨λ _, default _, times_cont_mdiff_const⟩⟩ /-- Constant map as a smooth map -/ def const (y : M') : C^n⟮I, M; I', M'⟯ := ⟨λ x, y, times_cont_mdiff_const⟩ end times_cont_mdiff_map instance continuous_linear_map.has_coe_to_times_cont_mdiff_map : has_coe (E →L[𝕜] E') C^n⟮𝓘(𝕜, E), E; 𝓘(𝕜, E'), E'⟯ := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff⟩⟩